GnuCash 2.4.99
gncVendor.c
00001 /********************************************************************\
00002  * gncVendor.c -- the Core Vendor Interface                         *
00003  *                                                                  *
00004  * This program is free software; you can redistribute it and/or    *
00005  * modify it under the terms of the GNU General Public License as   *
00006  * published by the Free Software Foundation; either version 2 of   *
00007  * the License, or (at your option) any later version.              *
00008  *                                                                  *
00009  * This program is distributed in the hope that it will be useful,  *
00010  * but WITHOUT ANY WARRANTY; without even the implied warranty of   *
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the    *
00012  * GNU General Public License for more details.                     *
00013  *                                                                  *
00014  * You should have received a copy of the GNU General Public License*
00015  * along with this program; if not, contact:                        *
00016  *                                                                  *
00017  * Free Software Foundation           Voice:  +1-617-542-5942       *
00018  * 51 Franklin Street, Fifth Floor    Fax:    +1-617-542-2652       *
00019  * Boston, MA  02110-1301,  USA       gnu@gnu.org                   *
00020  *                                                                  *
00021 \********************************************************************/
00022 
00023 /*
00024  * Copyright (C) 2001, 2002 Derek Atkins
00025  * Copyright (C) 2003 <linas@linas.org>
00026  * Author: Derek Atkins <warlord@MIT.EDU>
00027  */
00028 
00029 #include "config.h"
00030 
00031 #include <glib.h>
00032 #include <string.h>
00033 
00034 #include "gnc-commodity.h"
00035 #include "gncAddressP.h"
00036 #include "gncBillTermP.h"
00037 #include "gncInvoice.h"
00038 #include "gncJobP.h"
00039 #include "gncTaxTableP.h"
00040 #include "gncVendor.h"
00041 #include "gncVendorP.h"
00042 
00043 static gint gs_address_event_handler_id = 0;
00044 static void listen_for_address_events(QofInstance *entity, QofEventId event_type,
00045                                       gpointer user_data, gpointer event_data);
00046 static void qofVendorSetAddr (GncVendor *vendor, QofInstance *addr_ent);
00047 static const char* qofVendorGetTaxIncluded(const GncVendor *vendor);
00048 static void qofVendorSetTaxIncluded(GncVendor *vendor, const char* type_string);
00049 
00050 struct _gncVendor
00051 {
00052     QofInstance     inst;
00053 
00054     char *          id;
00055     char *          name;
00056     char *          notes;
00057     GncBillTerm *   terms;
00058     GncAddress *    addr;
00059     gnc_commodity * currency;
00060     GncTaxTable*    taxtable;
00061     gboolean        taxtable_override;
00062     GncTaxIncluded  taxincluded;
00063     gboolean        active;
00064     GList *         jobs;
00065 };
00066 
00067 struct _gncVendorClass
00068 {
00069     QofInstanceClass parent_class;
00070 };
00071 
00072 static QofLogModule log_module = GNC_MOD_BUSINESS;
00073 
00074 #define _GNC_MOD_NAME        GNC_ID_VENDOR
00075 
00076 /* ============================================================ */
00077 /* Misc inline funcs */
00078 
00079 G_INLINE_FUNC void mark_vendor (GncVendor *vendor);
00080 void mark_vendor (GncVendor *vendor)
00081 {
00082     qof_instance_set_dirty(&vendor->inst);
00083     qof_event_gen (&vendor->inst, QOF_EVENT_MODIFY, NULL);
00084 }
00085 
00086 /* ============================================================== */
00087 
00088 enum
00089 {
00090     PROP_0,
00091     PROP_NAME,
00092     PROP_ID,
00093     PROP_NOTES,
00094     PROP_CURRENCY,
00095     PROP_ACTIVE,
00096     PROP_TAXTABLE_OVERRIDE,
00097     PROP_BILLTERMS,
00098     PROP_TAXTABLE,
00099     PROP_ADDRESS,
00100     PROP_TAX_INCLUDED,
00101     PROP_TAX_INCLUDED_STR
00102 };
00103 
00104 /* GObject Initialization */
00105 G_DEFINE_TYPE(GncVendor, gnc_vendor, QOF_TYPE_INSTANCE);
00106 
00107 static void
00108 gnc_vendor_init(GncVendor* vendor)
00109 {
00110 }
00111 
00112 static void
00113 gnc_vendor_dispose(GObject *vendorp)
00114 {
00115     G_OBJECT_CLASS(gnc_vendor_parent_class)->dispose(vendorp);
00116 }
00117 
00118 static void
00119 gnc_vendor_finalize(GObject* vendorp)
00120 {
00121     G_OBJECT_CLASS(gnc_vendor_parent_class)->finalize(vendorp);
00122 }
00123 
00124 static void
00125 gnc_vendor_get_property (GObject         *object,
00126                          guint            prop_id,
00127                          GValue          *value,
00128                          GParamSpec      *pspec)
00129 {
00130     GncVendor *vendor;
00131 
00132     g_return_if_fail(GNC_IS_VENDOR(object));
00133 
00134     vendor = GNC_VENDOR(object);
00135     switch (prop_id)
00136     {
00137     case PROP_NAME:
00138         g_value_set_string(value, vendor->name);
00139         break;
00140     case PROP_ID:
00141         g_value_set_string(value, vendor->id);
00142         break;
00143     case PROP_NOTES:
00144         g_value_set_string(value, vendor->notes);
00145         break;
00146     case PROP_CURRENCY:
00147         g_value_set_object(value, vendor->currency);
00148         break;
00149     case PROP_ACTIVE:
00150         g_value_set_boolean(value, vendor->active);
00151         break;
00152     case PROP_TAXTABLE_OVERRIDE:
00153         g_value_set_boolean(value, vendor->taxtable_override);
00154         break;
00155     case PROP_BILLTERMS:
00156         g_value_set_object(value, vendor->terms);
00157         break;
00158     case PROP_TAXTABLE:
00159         g_value_set_object(value, vendor->taxtable);
00160         break;
00161     case PROP_ADDRESS:
00162         g_value_set_object(value, vendor->addr);
00163         break;
00164     case PROP_TAX_INCLUDED:
00165         g_value_set_int(value, vendor->taxincluded);
00166         break;
00167     case PROP_TAX_INCLUDED_STR:
00168         g_value_set_string(value, qofVendorGetTaxIncluded(vendor));
00169         break;
00170     default:
00171         G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
00172         break;
00173     }
00174 }
00175 
00176 static void
00177 gnc_vendor_set_property (GObject         *object,
00178                          guint            prop_id,
00179                          const GValue          *value,
00180                          GParamSpec      *pspec)
00181 {
00182     GncVendor *vendor;
00183 
00184     g_return_if_fail(GNC_IS_VENDOR(object));
00185 
00186     vendor = GNC_VENDOR(object);
00187     switch (prop_id)
00188     {
00189     case PROP_NAME:
00190         gncVendorSetName(vendor, g_value_get_string(value));
00191         break;
00192     case PROP_ID:
00193         gncVendorSetID(vendor, g_value_get_string(value));
00194         break;
00195     case PROP_NOTES:
00196         gncVendorSetNotes(vendor, g_value_get_string(value));
00197         break;
00198     case PROP_CURRENCY:
00199         gncVendorSetCurrency(vendor, g_value_get_object(value));
00200         break;
00201     case PROP_ACTIVE:
00202         gncVendorSetActive(vendor, g_value_get_boolean(value));
00203         break;
00204     case PROP_TAXTABLE_OVERRIDE:
00205         gncVendorSetTaxTableOverride(vendor, g_value_get_boolean(value));
00206         break;
00207     case PROP_BILLTERMS:
00208         gncVendorSetTerms(vendor, g_value_get_object(value));
00209         break;
00210     case PROP_TAXTABLE:
00211         gncVendorSetTaxTable(vendor, g_value_get_object(value));
00212         break;
00213     case PROP_ADDRESS:
00214         qofVendorSetAddr(vendor, g_value_get_object(value));
00215         break;
00216     case PROP_TAX_INCLUDED:
00217         gncVendorSetTaxIncluded(vendor, (GncTaxIncluded)g_value_get_int(value));
00218         break;
00219     case PROP_TAX_INCLUDED_STR:
00220         qofVendorSetTaxIncluded(vendor, g_value_get_string(value));
00221         break;
00222     default:
00223         G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
00224         break;
00225     }
00226 }
00227 
00229 static gboolean
00230 impl_refers_to_object(const QofInstance* inst, const QofInstance* ref)
00231 {
00232     GncVendor* v;
00233 
00234     g_return_val_if_fail(inst != NULL, FALSE);
00235     g_return_val_if_fail(GNC_IS_VENDOR(inst), FALSE);
00236 
00237     v = GNC_VENDOR(inst);
00238 
00239     if (GNC_IS_BILLTERM(ref))
00240     {
00241         return (v->terms == GNC_BILLTERM(ref));
00242     }
00243     else if (GNC_IS_TAXTABLE(ref))
00244     {
00245         return (v->taxtable == GNC_TAXTABLE(ref));
00246     }
00247 
00248     return FALSE;
00249 }
00250 
00257 static GList*
00258 impl_get_typed_referring_object_list(const QofInstance* inst, const QofInstance* ref)
00259 {
00260     if (!GNC_IS_BILLTERM(ref) && !GNC_IS_TAXTABLE(ref))
00261     {
00262         return NULL;
00263     }
00264 
00265     return qof_instance_get_referring_object_list_from_collection(qof_instance_get_collection(inst), ref);
00266 }
00267 
00268 static void
00269 gnc_vendor_class_init (GncVendorClass *klass)
00270 {
00271     GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
00272     QofInstanceClass* qof_class = QOF_INSTANCE_CLASS(klass);
00273 
00274     gobject_class->dispose = gnc_vendor_dispose;
00275     gobject_class->finalize = gnc_vendor_finalize;
00276     gobject_class->set_property = gnc_vendor_set_property;
00277     gobject_class->get_property = gnc_vendor_get_property;
00278 
00279     qof_class->get_display_name = NULL;
00280     qof_class->refers_to_object = impl_refers_to_object;
00281     qof_class->get_typed_referring_object_list = impl_get_typed_referring_object_list;
00282 
00283     g_object_class_install_property
00284     (gobject_class,
00285      PROP_NAME,
00286      g_param_spec_string ("name",
00287                           "Vendor Name",
00288                           "The vendor name is an arbitrary string "
00289                           "assigned by the user to provide the vendor name.",
00290                           NULL,
00291                           G_PARAM_READWRITE));
00292 
00293     g_object_class_install_property
00294     (gobject_class,
00295      PROP_ID,
00296      g_param_spec_string ("id",
00297                           "Vendor ID",
00298                           "The vendor id is an arbitrary string "
00299                           "assigned by the user to identify the vendor.",
00300                           NULL,
00301                           G_PARAM_READWRITE));
00302 
00303     g_object_class_install_property
00304     (gobject_class,
00305      PROP_NOTES,
00306      g_param_spec_string ("notes",
00307                           "Vendor notes",
00308                           "The vendor notes is an arbitrary string "
00309                           "assigned by the user to add extra information about the vendor.",
00310                           NULL,
00311                           G_PARAM_READWRITE));
00312 
00313     g_object_class_install_property
00314     (gobject_class,
00315      PROP_CURRENCY,
00316      g_param_spec_object ("currency",
00317                           "Currency",
00318                           "The currency property denotes the currency used by this vendor.",
00319                           GNC_TYPE_COMMODITY,
00320                           G_PARAM_READWRITE));
00321 
00322     g_object_class_install_property
00323     (gobject_class,
00324      PROP_ACTIVE,
00325      g_param_spec_boolean ("active",
00326                            "Active",
00327                            "TRUE if the vendor is active.  FALSE if inactive.",
00328                            FALSE,
00329                            G_PARAM_READWRITE));
00330 
00331     g_object_class_install_property
00332     (gobject_class,
00333      PROP_TAXTABLE_OVERRIDE,
00334      g_param_spec_boolean ("tax-table-override",
00335                            "Tax table override",
00336                            "TRUE if the vendor has a specific tax table which overrides the default "
00337                            "tax table.  FALSE if the default table should be used.",
00338                            FALSE,
00339                            G_PARAM_READWRITE));
00340 
00341     g_object_class_install_property
00342     (gobject_class,
00343      PROP_BILLTERMS,
00344      g_param_spec_object ("terms",
00345                           "Terms",
00346                           "The billing terms used by this vendor.",
00347                           GNC_TYPE_COMMODITY,
00348                           G_PARAM_READWRITE));
00349 
00350     g_object_class_install_property
00351     (gobject_class,
00352      PROP_TAXTABLE,
00353      g_param_spec_object ("tax-table",
00354                           "Tax table",
00355                           "The tax table which applies to this vendor.",
00356                           GNC_TYPE_COMMODITY,
00357                           G_PARAM_READWRITE));
00358 
00359     g_object_class_install_property
00360     (gobject_class,
00361      PROP_ADDRESS,
00362      g_param_spec_object ("address",
00363                           "Address",
00364                           "The address property contains the address information for this vendor.",
00365                           GNC_TYPE_ADDRESS,
00366                           G_PARAM_READWRITE));
00367 
00368     g_object_class_install_property
00369     (gobject_class,
00370      PROP_TAX_INCLUDED,
00371      g_param_spec_int  ("tax-included",
00372                         "Tax included",
00373                         "The tax-included property contains the information about tax calculation this vendor.",
00374                         GNC_TAXINCLUDED_YES,       /* min */
00375                         GNC_TAXINCLUDED_USEGLOBAL, /* max */
00376                         GNC_TAXINCLUDED_USEGLOBAL, /* default */
00377                         G_PARAM_READWRITE));
00378 
00379     g_object_class_install_property
00380     (gobject_class,
00381      PROP_TAX_INCLUDED_STR,
00382      g_param_spec_string("tax-included-string",
00383                          "Tax included string",
00384                          "The tax-included-string property contains a character version of tax-included.",
00385                          FALSE,
00386                          G_PARAM_READWRITE));
00387 }
00388 
00389 /* Create/Destroy Functions */
00390 GncVendor *gncVendorCreate (QofBook *book)
00391 {
00392     GncVendor *vendor;
00393 
00394     if (!book) return NULL;
00395 
00396     vendor = g_object_new (GNC_TYPE_VENDOR, NULL);
00397     qof_instance_init_data (&vendor->inst, _GNC_MOD_NAME, book);
00398 
00399     vendor->id = CACHE_INSERT ("");
00400     vendor->name = CACHE_INSERT ("");
00401     vendor->notes = CACHE_INSERT ("");
00402     vendor->addr = gncAddressCreate (book, &vendor->inst);
00403     vendor->taxincluded = GNC_TAXINCLUDED_USEGLOBAL;
00404     vendor->active = TRUE;
00405     vendor->jobs = NULL;
00406 
00407     if (gs_address_event_handler_id == 0)
00408     {
00409         gs_address_event_handler_id = qof_event_register_handler(listen_for_address_events, NULL);
00410     }
00411 
00412     qof_event_gen (&vendor->inst, QOF_EVENT_CREATE, NULL);
00413 
00414     return vendor;
00415 }
00416 
00417 void gncVendorDestroy (GncVendor *vendor)
00418 {
00419     if (!vendor) return;
00420     qof_instance_set_destroying(vendor, TRUE);
00421     gncVendorCommitEdit (vendor);
00422 }
00423 
00424 static void gncVendorFree (GncVendor *vendor)
00425 {
00426     if (!vendor) return;
00427 
00428     qof_event_gen (&vendor->inst, QOF_EVENT_DESTROY, NULL);
00429 
00430     CACHE_REMOVE (vendor->id);
00431     CACHE_REMOVE (vendor->name);
00432     CACHE_REMOVE (vendor->notes);
00433     gncAddressBeginEdit (vendor->addr);
00434     gncAddressDestroy (vendor->addr);
00435     g_list_free (vendor->jobs);
00436 
00437     if (vendor->terms)
00438         gncBillTermDecRef (vendor->terms);
00439     if (vendor->taxtable)
00440         gncTaxTableDecRef (vendor->taxtable);
00441 
00442     /* qof_instance_release (&vendor->inst); */
00443     g_object_unref (vendor);
00444 }
00445 
00446 /* ============================================================== */
00447 /* Set Functions */
00448 
00449 #define SET_STR(obj, member, str) { \
00450         char * tmp; \
00451         \
00452         if (!safe_strcmp (member, str)) return; \
00453         gncVendorBeginEdit (obj); \
00454         tmp = CACHE_INSERT (str); \
00455         CACHE_REMOVE (member); \
00456         member = tmp; \
00457         }
00458 
00459 void gncVendorSetID (GncVendor *vendor, const char *id)
00460 {
00461     if (!vendor) return;
00462     if (!id) return;
00463     SET_STR(vendor, vendor->id, id);
00464     mark_vendor (vendor);
00465     gncVendorCommitEdit (vendor);
00466 }
00467 
00468 void gncVendorSetName (GncVendor *vendor, const char *name)
00469 {
00470     if (!vendor) return;
00471     if (!name) return;
00472     SET_STR(vendor, vendor->name, name);
00473     mark_vendor (vendor);
00474     gncVendorCommitEdit (vendor);
00475 }
00476 
00477 void gncVendorSetNotes (GncVendor *vendor, const char *notes)
00478 {
00479     if (!vendor) return;
00480     if (!notes) return;
00481     SET_STR(vendor, vendor->notes, notes);
00482     mark_vendor (vendor);
00483     gncVendorCommitEdit (vendor);
00484 }
00485 
00486 void gncVendorSetTerms (GncVendor *vendor, GncBillTerm *terms)
00487 {
00488     if (!vendor) return;
00489     if (vendor->terms == terms) return;
00490 
00491     gncVendorBeginEdit (vendor);
00492     if (vendor->terms)
00493         gncBillTermDecRef (vendor->terms);
00494     vendor->terms = terms;
00495     if (vendor->terms)
00496         gncBillTermIncRef (vendor->terms);
00497     mark_vendor (vendor);
00498     gncVendorCommitEdit (vendor);
00499 }
00500 
00501 void gncVendorSetTaxIncluded (GncVendor *vendor, GncTaxIncluded taxincl)
00502 {
00503     if (!vendor) return;
00504     if (taxincl == vendor->taxincluded) return;
00505     gncVendorBeginEdit (vendor);
00506     vendor->taxincluded = taxincl;
00507     mark_vendor (vendor);
00508     gncVendorCommitEdit (vendor);
00509 }
00510 
00511 void gncVendorSetCurrency (GncVendor *vendor, gnc_commodity *currency)
00512 {
00513     if (!vendor || !currency) return;
00514     if (vendor->currency &&
00515             gnc_commodity_equal (vendor->currency, currency))
00516         return;
00517     gncVendorBeginEdit (vendor);
00518     vendor->currency = currency;
00519     mark_vendor (vendor);
00520     gncVendorCommitEdit (vendor);
00521 }
00522 
00523 void gncVendorSetActive (GncVendor *vendor, gboolean active)
00524 {
00525     if (!vendor) return;
00526     if (active == vendor->active) return;
00527     gncVendorBeginEdit (vendor);
00528     vendor->active = active;
00529     mark_vendor (vendor);
00530     gncVendorCommitEdit (vendor);
00531 }
00532 
00533 void gncVendorSetTaxTableOverride (GncVendor *vendor, gboolean override)
00534 {
00535     if (!vendor) return;
00536     if (vendor->taxtable_override == override) return;
00537     gncVendorBeginEdit (vendor);
00538     vendor->taxtable_override = override;
00539     mark_vendor (vendor);
00540     gncVendorCommitEdit (vendor);
00541 }
00542 
00543 void gncVendorSetTaxTable (GncVendor *vendor, GncTaxTable *table)
00544 {
00545     if (!vendor) return;
00546     if (vendor->taxtable == table) return;
00547     gncVendorBeginEdit (vendor);
00548     if (vendor->taxtable)
00549         gncTaxTableDecRef (vendor->taxtable);
00550     if (table)
00551         gncTaxTableIncRef (table);
00552     vendor->taxtable = table;
00553     mark_vendor (vendor);
00554     gncVendorCommitEdit (vendor);
00555 }
00556 
00557 static void
00558 qofVendorSetAddr (GncVendor *vendor, QofInstance *addr_ent)
00559 {
00560     GncAddress *addr;
00561 
00562     if (!vendor || !addr_ent)
00563     {
00564         return;
00565     }
00566     addr = (GncAddress*)addr_ent;
00567     if (addr == vendor->addr)
00568     {
00569         return;
00570     }
00571     if (vendor->addr != NULL)
00572     {
00573         gncAddressBeginEdit(vendor->addr);
00574         gncAddressDestroy(vendor->addr);
00575     }
00576     gncVendorBeginEdit(vendor);
00577     vendor->addr = addr;
00578     gncVendorCommitEdit(vendor);
00579 }
00580 
00581 static void
00582 qofVendorSetTaxIncluded(GncVendor *vendor, const char* type_string)
00583 {
00584     GncTaxIncluded inc;
00585 
00586     if (!gncTaxIncludedStringToType(type_string, &inc))
00587     {
00588         return;
00589     }
00590     gncVendorBeginEdit(vendor);
00591     vendor->taxincluded = inc;
00592     gncVendorCommitEdit(vendor);
00593 }
00594 
00595 /* ============================================================== */
00596 /* Get Functions */
00597 
00598 const char * gncVendorGetID (const GncVendor *vendor)
00599 {
00600     if (!vendor) return NULL;
00601     return vendor->id;
00602 }
00603 
00604 const char * gncVendorGetName (const GncVendor *vendor)
00605 {
00606     if (!vendor) return NULL;
00607     return vendor->name;
00608 }
00609 
00610 GncAddress * gncVendorGetAddr (const GncVendor *vendor)
00611 {
00612     if (!vendor) return NULL;
00613     return vendor->addr;
00614 }
00615 
00616 const char * gncVendorGetNotes (const GncVendor *vendor)
00617 {
00618     if (!vendor) return NULL;
00619     return vendor->notes;
00620 }
00621 
00622 GncBillTerm * gncVendorGetTerms (const GncVendor *vendor)
00623 {
00624     if (!vendor) return 0;
00625     return vendor->terms;
00626 }
00627 
00628 GncTaxIncluded gncVendorGetTaxIncluded (const GncVendor *vendor)
00629 {
00630     if (!vendor) return GNC_TAXINCLUDED_USEGLOBAL;
00631     return vendor->taxincluded;
00632 }
00633 
00634 gnc_commodity * gncVendorGetCurrency (const GncVendor *vendor)
00635 {
00636     if (!vendor) return NULL;
00637     return vendor->currency;
00638 }
00639 
00640 gboolean gncVendorGetActive (const GncVendor *vendor)
00641 {
00642     if (!vendor) return FALSE;
00643     return vendor->active;
00644 }
00645 
00646 gboolean gncVendorGetTaxTableOverride (const GncVendor *vendor)
00647 {
00648     if (!vendor) return FALSE;
00649     return vendor->taxtable_override;
00650 }
00651 
00652 GncTaxTable* gncVendorGetTaxTable (const GncVendor *vendor)
00653 {
00654     if (!vendor) return NULL;
00655     return vendor->taxtable;
00656 }
00657 
00658 static const char*
00659 qofVendorGetTaxIncluded(const GncVendor *vendor)
00660 {
00661     return gncTaxIncludedTypeToString(vendor->taxincluded);
00662 }
00663 
00664 /* Note that JobList changes do not affect the "dirtiness" of the vendor */
00665 void gncVendorAddJob (GncVendor *vendor, GncJob *job)
00666 {
00667     if (!vendor) return;
00668     if (!job) return;
00669 
00670     if (g_list_index(vendor->jobs, job) == -1)
00671         vendor->jobs = g_list_insert_sorted (vendor->jobs, job,
00672                                              (GCompareFunc)gncJobCompare);
00673 
00674     qof_event_gen (&vendor->inst, QOF_EVENT_MODIFY, NULL);
00675 }
00676 
00677 void gncVendorRemoveJob (GncVendor *vendor, GncJob *job)
00678 {
00679     GList *node;
00680 
00681     if (!vendor) return;
00682     if (!job) return;
00683 
00684     node = g_list_find (vendor->jobs, job);
00685     if (!node)
00686     {
00687         /*    PERR ("split not in account"); */
00688     }
00689     else
00690     {
00691         vendor->jobs = g_list_remove_link (vendor->jobs, node);
00692         g_list_free_1 (node);
00693     }
00694 
00695     qof_event_gen (&vendor->inst, QOF_EVENT_MODIFY, NULL);
00696 }
00697 
00698 void gncVendorBeginEdit (GncVendor *vendor)
00699 {
00700     qof_begin_edit(&vendor->inst);
00701 }
00702 
00703 static void gncVendorOnError (QofInstance *vendor, QofBackendError errcode)
00704 {
00705     PERR("Vendor QofBackend Failure: %d", errcode);
00706     gnc_engine_signal_commit_error( errcode );
00707 }
00708 
00709 static void gncVendorOnDone (QofInstance *inst)
00710 {
00711     GncVendor *vendor = (GncVendor *) inst;
00712     gncAddressClearDirty (vendor->addr);
00713 }
00714 
00715 static void vendor_free (QofInstance *inst)
00716 {
00717     GncVendor *vendor = (GncVendor *) inst;
00718     gncVendorFree (vendor);
00719 }
00720 
00721 void gncVendorCommitEdit (GncVendor *vendor)
00722 {
00723     if (!qof_commit_edit (QOF_INSTANCE(vendor))) return;
00724     qof_commit_edit_part2 (&vendor->inst, gncVendorOnError,
00725                            gncVendorOnDone, vendor_free);
00726 }
00727 
00728 /* ============================================================== */
00729 /* Other functions */
00730 
00731 int gncVendorCompare (const GncVendor *a, const GncVendor *b)
00732 {
00733     if (!a && !b) return 0;
00734     if (!a && b) return 1;
00735     if (a && !b) return -1;
00736 
00737     return(strcmp(a->name, b->name));
00738 }
00739 
00740 gboolean gncVendorEqual(const GncVendor *a, const GncVendor *b)
00741 {
00742     if (a == NULL && b == NULL) return TRUE;
00743     if (a == NULL ||  b == NULL) return FALSE;
00744 
00745     g_return_val_if_fail(GNC_IS_VENDOR(a), FALSE);
00746     g_return_val_if_fail(GNC_IS_VENDOR(b), FALSE);
00747 
00748     if (safe_strcmp(a->id, b->id) != 0)
00749     {
00750         PWARN("IDs differ: %s vs %s", a->id, b->id);
00751         return FALSE;
00752     }
00753 
00754     if (safe_strcmp(a->name, b->name) != 0)
00755     {
00756         PWARN("Names differ: %s vs %s", a->name, b->name);
00757         return FALSE;
00758     }
00759 
00760     if (safe_strcmp(a->notes, b->notes) != 0)
00761     {
00762         PWARN("Notes differ");
00763         return FALSE;
00764     }
00765 
00766     if (!gncBillTermEqual(a->terms, b->terms))
00767     {
00768         PWARN("BillTerms differ");
00769         return FALSE;
00770     }
00771 
00772     if (!gncAddressEqual(a->addr, b->addr))
00773     {
00774         PWARN("Addresses differ");
00775         return FALSE;
00776     }
00777 
00778     if (!gnc_commodity_equal(a->currency, b->currency))
00779     {
00780         PWARN("Currencies differ");
00781         return FALSE;
00782     }
00783 
00784     if (!gncTaxTableEqual(a->taxtable, b->taxtable))
00785     {
00786         PWARN("Tax tables differ");
00787         return FALSE;
00788     }
00789 
00790     if (a->taxtable_override != b->taxtable_override)
00791     {
00792         PWARN("Tax table override flags differ");
00793         return FALSE;
00794     }
00795 
00796     if (a->taxincluded != b->taxincluded)
00797     {
00798         PWARN("Tax included flags differ");
00799         return FALSE;
00800     }
00801 
00802     if (a->active != b->active)
00803     {
00804         PWARN("Active flags differ");
00805         return FALSE;
00806     }
00807 
00808 //    GList *         jobs;
00809     return TRUE;
00810 }
00811 
00812 gboolean
00813 gncVendorIsDirty (const GncVendor *vendor)
00814 {
00815     if (!vendor) return FALSE;
00816     return (qof_instance_get_dirty_flag(vendor)
00817             || gncAddressIsDirty (vendor->addr));
00818 }
00819 
00829 static void
00830 listen_for_address_events(QofInstance *entity, QofEventId event_type,
00831                           gpointer user_data, gpointer event_data)
00832 {
00833     GncVendor* v;
00834 
00835     if ((event_type & QOF_EVENT_MODIFY) == 0)
00836     {
00837         return;
00838     }
00839     if (!GNC_IS_ADDRESS(entity))
00840     {
00841         return;
00842     }
00843     if (!GNC_IS_VENDOR(event_data))
00844     {
00845         return;
00846     }
00847     v = GNC_VENDOR(event_data);
00848     gncVendorBeginEdit(v);
00849     mark_vendor(v);
00850     gncVendorCommitEdit(v);
00851 }
00852 /* ============================================================== */
00853 /* Package-Private functions */
00854 
00855 static const char * _gncVendorPrintable (gpointer item)
00856 {
00857     GncVendor *v = item;
00858     if (!item) return NULL;
00859     return v->name;
00860 }
00861 
00862 static void
00863 destroy_vendor_on_book_close(QofInstance *ent, gpointer data)
00864 {
00865     GncVendor* v = GNC_VENDOR(ent);
00866 
00867     gncVendorBeginEdit(v);
00868     gncVendorDestroy(v);
00869 }
00870 
00875 static void
00876 gnc_vendor_book_end(QofBook* book)
00877 {
00878     QofCollection *col;
00879 
00880     col = qof_book_get_collection(book, GNC_ID_VENDOR);
00881     qof_collection_foreach(col, destroy_vendor_on_book_close, NULL);
00882 }
00883 
00884 static QofObject gncVendorDesc =
00885 {
00886     DI(.interface_version = ) QOF_OBJECT_VERSION,
00887     DI(.e_type            = ) _GNC_MOD_NAME,
00888     DI(.type_label        = ) "Vendor",
00889     DI(.create            = ) (gpointer)gncVendorCreate,
00890     DI(.book_begin        = ) NULL,
00891     DI(.book_end          = ) gnc_vendor_book_end,
00892     DI(.is_dirty          = ) qof_collection_is_dirty,
00893     DI(.mark_clean        = ) qof_collection_mark_clean,
00894     DI(.foreach           = ) qof_collection_foreach,
00895     DI(.printable         = ) _gncVendorPrintable,
00896     DI(.version_cmp       = ) (int (*)(gpointer, gpointer)) qof_instance_version_cmp,
00897 };
00898 
00899 gboolean gncVendorRegister (void)
00900 {
00901     static QofParam params[] =
00902     {
00903         { VENDOR_ID, QOF_TYPE_STRING, (QofAccessFunc)gncVendorGetID, (QofSetterFunc)gncVendorSetID },
00904         { VENDOR_NAME, QOF_TYPE_STRING, (QofAccessFunc)gncVendorGetName, (QofSetterFunc)gncVendorSetName },
00905         { VENDOR_ADDR,    GNC_ID_ADDRESS, (QofAccessFunc)gncVendorGetAddr, (QofSetterFunc)qofVendorSetAddr },
00906         { VENDOR_NOTES,   QOF_TYPE_STRING, (QofAccessFunc)gncVendorGetNotes, (QofSetterFunc)gncVendorSetNotes },
00907         { VENDOR_TERMS,   GNC_ID_BILLTERM, (QofAccessFunc)gncVendorGetTerms, (QofSetterFunc)gncVendorSetTerms },
00908         {
00909             VENDOR_TAX_OVERRIDE, QOF_TYPE_BOOLEAN, (QofAccessFunc)gncVendorGetTaxTableOverride,
00910             (QofSetterFunc)gncVendorSetTaxTableOverride
00911         },
00912         {
00913             VENDOR_TAX_TABLE, GNC_ID_TAXTABLE, (QofAccessFunc)gncVendorGetTaxTable,
00914             (QofSetterFunc)gncVendorSetTaxTable
00915         },
00916         {
00917             VENDOR_TAX_INC, QOF_TYPE_STRING, (QofAccessFunc)qofVendorGetTaxIncluded,
00918             (QofSetterFunc)qofVendorSetTaxIncluded
00919         },
00920         { QOF_PARAM_BOOK, QOF_ID_BOOK, (QofAccessFunc)qof_instance_get_book, NULL },
00921         { QOF_PARAM_GUID, QOF_TYPE_GUID, (QofAccessFunc)qof_instance_get_guid, NULL },
00922         { QOF_PARAM_ACTIVE, QOF_TYPE_BOOLEAN, (QofAccessFunc)gncVendorGetActive, NULL },
00923         { NULL },
00924     };
00925 
00926     if (!qof_choice_add_class(GNC_ID_INVOICE, GNC_ID_VENDOR, INVOICE_OWNER))
00927     {
00928         return FALSE;
00929     }
00930     if (!qof_choice_add_class(GNC_ID_JOB, GNC_ID_VENDOR, JOB_OWNER))
00931     {
00932         return FALSE;
00933     }
00934 
00935     qof_class_register (_GNC_MOD_NAME, (QofSortFunc)gncVendorCompare, params);
00936 
00937     return qof_object_register (&gncVendorDesc);
00938 }
00939 
00940 gchar *gncVendorNextID (QofBook *book)
00941 {
00942     return qof_book_increment_and_format_counter (book, _GNC_MOD_NAME);
00943 }
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines