diff --git a/src/business/business-core/file/gnc-customer-xml-v2.c b/src/business/business-core/file/gnc-customer-xml-v2.c index 430307d7f8..b79051d1ec 100644 --- a/src/business/business-core/file/gnc-customer-xml-v2.c +++ b/src/business/business-core/file/gnc-customer-xml-v2.c @@ -89,7 +89,7 @@ customer_dom_tree_create (GncCustomer *cust) xmlSetProp(ret, "version", customer_version_string); xmlAddChild(ret, guid_to_dom_tree(cust_guid_string, - gncCustomerGetGUID (cust))); + qof_instance_get_guid(QOF_INSTANCE(cust)))); xmlAddChild(ret, text_to_dom_tree(cust_name_string, gncCustomerGetName (cust))); diff --git a/src/business/business-core/file/gnc-tax-table-xml-v2.c b/src/business/business-core/file/gnc-tax-table-xml-v2.c index 6d9a760f99..e46ee3c3c1 100644 --- a/src/business/business-core/file/gnc-tax-table-xml-v2.c +++ b/src/business/business-core/file/gnc-tax-table-xml-v2.c @@ -91,7 +91,7 @@ ttentry_dom_tree_create (GncTaxTableEntry *entry) account = gncTaxTableEntryGetAccount (entry); if (account) xmlAddChild(ret, guid_to_dom_tree (ttentry_account_string, - xaccAccountGetGUID (account))); + qof_instance_get_guid (QOF_INSTANCE(account)))); amount = gncTaxTableEntryGetAmount (entry); xmlAddChild (ret, gnc_numeric_to_dom_tree (ttentry_amount_string, &amount)); diff --git a/src/business/business-core/file/gnc-vendor-xml-v2.c b/src/business/business-core/file/gnc-vendor-xml-v2.c index 0c896bea39..6941605e3d 100644 --- a/src/business/business-core/file/gnc-vendor-xml-v2.c +++ b/src/business/business-core/file/gnc-vendor-xml-v2.c @@ -85,7 +85,7 @@ vendor_dom_tree_create (GncVendor *vendor) xmlSetProp(ret, "version", vendor_version_string); xmlAddChild(ret, guid_to_dom_tree(vendor_guid_string, - gncVendorGetGUID (vendor))); + qof_instance_get_guid (QOF_INSTANCE(vendor)))); xmlAddChild(ret, text_to_dom_tree(vendor_name_string, gncVendorGetName (vendor))); diff --git a/src/business/business-core/gncBusPeriod.c b/src/business/business-core/gncBusPeriod.c index 84a8099050..8513177249 100644 --- a/src/business/business-core/gncBusPeriod.c +++ b/src/business/business-core/gncBusPeriod.c @@ -1,16 +1,41 @@ XXX TODO: --- billterm and taxtermss are incompletely cloned, not sure +-- instance should somehow reference the qoftype, so that + things like gnc_engine_generate_event wouldn't + need three args ... + (in fact, so that many things wouldn't need to pass type explicitly) + + entity node already stores type, so entity_node should be merged + with instance (or be parent of instance ?!) + +-- bus obj has per-type hash tables that are identical to the entity + per-type hash tables, should be merged together. + +-- bus object foreach should be made into a for-each by type + (requires modes to book for each to be a type-foreach). + (this would also add a per-type edit/commmit-edit, clone, dirty, + etc. functions!) Yahoo! + +-- billterm and taxtables are incompletely cloned, not sure what to do with refcount, ask warlord, need to explore. --- jobs incomplete cloned, neeed to handle owners +-- onwers incomplete cloned, neeed to handle remaining 'owners' -- turn clone into a generic object callback, so that the ObtainTwin could be qof_instance_obtain_twin, a generic function. (right now its copied everywhere) --- most of the busienss objects have an id/name/desc +-- The following busines objects have an id/name/desc/active this could be abstracted into an common object. + vendor (customer) + bill term (but bill terms doesn't have id or active) + job + +-- gnVendor should be a base class to gncCustomer (they're + identical, but customer adds more stuff). + + Enployee could be base class for vendor, its got some of the + things (name, addr, active, currency) -- TaxTable and BillTerm have common parent/child code. this could be abstracted into common code. diff --git a/src/business/business-core/gncCustomer.c b/src/business/business-core/gncCustomer.c index bd347b424a..8710f02955 100644 --- a/src/business/business-core/gncCustomer.c +++ b/src/business/business-core/gncCustomer.c @@ -58,22 +58,24 @@ struct _gncCustomer { QofInstance inst; + + /* The following fields are identical to 'vendor' */ char * id; char * name; char * notes; GncBillTerm * terms; GncAddress * addr; - GncAddress * shipaddr; gnc_commodity * currency; - gnc_numeric discount; - gnc_numeric credit; + GncTaxTable* taxtable; + gboolean taxtable_override; GncTaxIncluded taxincluded; - gboolean active; GList * jobs; - GncTaxTable* taxtable; - gboolean taxtable_override; + /* The following fields aer unique to 'customer' */ + gnc_numeric credit; + gnc_numeric discount; + GncAddress * shipaddr; }; static short module = MOD_BUSINESS; @@ -117,17 +119,19 @@ GncCustomer *gncCustomerCreate (QofBook *book) cust = g_new0 (GncCustomer, 1); qof_instance_init (&cust->inst, book); + cust->id = CACHE_INSERT (""); cust->name = CACHE_INSERT (""); cust->notes = CACHE_INSERT (""); cust->addr = gncAddressCreate (book, &cust->inst.guid, _GNC_MOD_NAME); - cust->shipaddr = gncAddressCreate (book, &cust->inst.guid, _GNC_MOD_NAME); - cust->discount = gnc_numeric_zero(); - cust->credit = gnc_numeric_zero(); cust->taxincluded = GNC_TAXINCLUDED_USEGLOBAL; cust->active = TRUE; cust->jobs = NULL; + cust->discount = gnc_numeric_zero(); + cust->credit = gnc_numeric_zero(); + cust->shipaddr = gncAddressCreate (book, &cust->inst.guid, _GNC_MOD_NAME); + addObj (cust); gnc_engine_generate_event (&cust->inst.guid, _GNC_MOD_NAME, GNC_EVENT_CREATE); @@ -416,20 +420,19 @@ void gncCustomerBeginEdit (GncCustomer *cust) GNC_BEGIN_EDIT (&cust->inst, _GNC_MOD_NAME); } -static void gncCustomerOnError (QofInstance *inst, QofBackendError errcode) +static inline void gncCustomerOnError (QofInstance *inst, QofBackendError errcode) { PERR("Customer QofBackend Failure: %d", errcode); } -static void gncCustomerOnDone (QofInstance *inst) +static inline void gncCustomerOnDone (QofInstance *inst) { GncCustomer *cust = (GncCustomer *) inst; - cust->inst.dirty = FALSE; gncAddressClearDirty (cust->addr); gncAddressClearDirty (cust->shipaddr); } -static void cust_free (QofInstance *inst) +static inline void cust_free (QofInstance *inst) { GncCustomer *cust = (GncCustomer *) inst; gncCustomerFree (cust); diff --git a/src/business/business-core/gncOwner.c b/src/business/business-core/gncOwner.c index 21540a1e00..45c56cbfad 100644 --- a/src/business/business-core/gncOwner.c +++ b/src/business/business-core/gncOwner.c @@ -232,7 +232,7 @@ const GUID * gncOwnerGetGUID (GncOwner *owner) case GNC_OWNER_JOB: return qof_instance_get_guid (QOF_INSTANCE(owner->owner.job)); case GNC_OWNER_VENDOR: - return gncVendorGetGUID (owner->owner.vendor); + return qof_instance_get_guid (QOF_INSTANCE(owner->owner.vendor)); case GNC_OWNER_EMPLOYEE: return gncEmployeeGetGUID (owner->owner.employee); } diff --git a/src/business/business-core/gncVendor.c b/src/business/business-core/gncVendor.c index f26fb8e9e2..5741e5b80f 100644 --- a/src/business/business-core/gncVendor.c +++ b/src/business/business-core/gncVendor.c @@ -1,5 +1,26 @@ +/********************************************************************\ + * gncVendor.c -- the Core Vendor Interface * + * * + * This program is free software; you can redistribute it and/or * + * modify it under the terms of the GNU General Public License as * + * published by the Free Software Foundation; either version 2 of * + * the License, or (at your option) any later version. * + * * + * This program is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * + * GNU General Public License for more details. * + * * + * You should have received a copy of the GNU General Public License* + * along with this program; if not, contact: * + * * + * Free Software Foundation Voice: +1-617-542-5942 * + * 59 Temple Place - Suite 330 Fax: +1-617-542-2652 * + * Boston, MA 02111-1307, USA gnu@gnu.org * + * * +\********************************************************************/ + /* - * gncVendor.c -- the Core Vendor Interface * Copyright (C) 2001, 2002 Derek Atkins * Author: Derek Atkins */ @@ -11,63 +32,71 @@ #include "guid.h" #include "messages.h" -#include "gnc-book.h" #include "gnc-commodity.h" #include "gnc-engine-util.h" #include "gnc-event-p.h" -#include "qofid.h" -#include "qofquerycore.h" -#include "qofquery.h" -#include "qofclass.h" #include "gnc-be-utils.h" + +#include "qofbook.h" +#include "qofclass.h" +#include "qofid.h" #include "qofid-p.h" +#include "qofinstance.h" +#include "qofquery.h" +#include "qofquerycore.h" #include "gncBusiness.h" #include "gncVendor.h" #include "gncVendorP.h" #include "gncAddress.h" -struct _gncVendor { - QofBook * book; - GUID guid; - char * id; - char * name; - char * notes; - GncBillTerm * terms; - GncAddress * addr; +struct _gncVendor +{ + QofInstance inst; + + char * id; + char * name; + char * notes; + GncBillTerm * terms; + GncAddress * addr; gnc_commodity * currency; - GncTaxIncluded taxincluded; - gboolean active; - GList * jobs; - GncTaxTable* taxtable; - gboolean taxtable_override; - - int editlevel; - gboolean do_free; - - gboolean dirty; + GncTaxTable* taxtable; + gboolean taxtable_override; + GncTaxIncluded taxincluded; + gboolean active; + GList * jobs; }; -static short module = MOD_BUSINESS; +static short module = MOD_BUSINESS; -#define _GNC_MOD_NAME GNC_VENDOR_MODULE_NAME +#define _GNC_MOD_NAME GNC_VENDOR_MODULE_NAME + +/* ============================================================ */ +/* Misc inline funcs */ #define CACHE_INSERT(str) g_cache_insert(gnc_engine_get_string_cache(), (gpointer)(str)); #define CACHE_REMOVE(str) g_cache_remove(gnc_engine_get_string_cache(), (str)); -static void addObj (GncVendor *vendor); -static void remObj (GncVendor *vendor); +static inline void addObj (GncVendor *vendor) +{ + gncBusinessAddObject (vendor->inst.book, _GNC_MOD_NAME, vendor, &vendor->inst.guid); +} + +static inline void remObj (GncVendor *vendor) +{ + gncBusinessRemoveObject (vendor->inst.book, _GNC_MOD_NAME, &vendor->inst.guid); +} G_INLINE_FUNC void mark_vendor (GncVendor *vendor); G_INLINE_FUNC void mark_vendor (GncVendor *vendor) { - vendor->dirty = TRUE; - gncBusinessSetDirtyFlag (vendor->book, _GNC_MOD_NAME, TRUE); - - gnc_engine_generate_event (&vendor->guid, _GNC_MOD_NAME, GNC_EVENT_MODIFY); + vendor->inst.dirty = TRUE; + gncBusinessSetDirtyFlag (vendor->inst.book, _GNC_MOD_NAME, TRUE); + gnc_engine_generate_event (&vendor->inst.guid, _GNC_MOD_NAME, GNC_EVENT_MODIFY); } +/* ============================================================== */ /* Create/Destroy Functions */ GncVendor *gncVendorCreate (QofBook *book) @@ -77,19 +106,18 @@ GncVendor *gncVendorCreate (QofBook *book) if (!book) return NULL; vendor = g_new0 (GncVendor, 1); - vendor->book = book; - vendor->dirty = FALSE; + qof_instance_init (&vendor->inst, book); + vendor->id = CACHE_INSERT (""); vendor->name = CACHE_INSERT (""); vendor->notes = CACHE_INSERT (""); - vendor->addr = gncAddressCreate (book, &vendor->guid, _GNC_MOD_NAME); + vendor->addr = gncAddressCreate (book, &vendor->inst.guid, _GNC_MOD_NAME); vendor->taxincluded = GNC_TAXINCLUDED_USEGLOBAL; vendor->active = TRUE; + vendor->jobs = NULL; - qof_entity_guid_new (qof_book_get_entity_table (book), &vendor->guid); addObj (vendor); - - gnc_engine_generate_event (&vendor->guid, _GNC_MOD_NAME, GNC_EVENT_CREATE); + gnc_engine_generate_event (&vendor->inst.guid, _GNC_MOD_NAME, GNC_EVENT_CREATE); return vendor; } @@ -97,7 +125,7 @@ GncVendor *gncVendorCreate (QofBook *book) void gncVendorDestroy (GncVendor *vendor) { if (!vendor) return; - vendor->do_free = TRUE; + vendor->inst.do_free = TRUE; gncVendorCommitEdit (vendor); } @@ -105,7 +133,7 @@ static void gncVendorFree (GncVendor *vendor) { if (!vendor) return; - gnc_engine_generate_event (&vendor->guid, _GNC_MOD_NAME, GNC_EVENT_DESTROY); + gnc_engine_generate_event (&vendor->inst.guid, _GNC_MOD_NAME, GNC_EVENT_DESTROY); CACHE_REMOVE (vendor->id); CACHE_REMOVE (vendor->name); @@ -123,17 +151,18 @@ static void gncVendorFree (GncVendor *vendor) g_free (vendor); } +/* ============================================================== */ /* Set Functions */ #define SET_STR(obj, member, str) { \ - char * tmp; \ - \ - if (!safe_strcmp (member, str)) return; \ - gncVendorBeginEdit (obj); \ - tmp = CACHE_INSERT (str); \ - CACHE_REMOVE (member); \ - member = tmp; \ - } + char * tmp; \ + \ + if (!safe_strcmp (member, str)) return; \ + gncVendorBeginEdit (obj); \ + tmp = CACHE_INSERT (str); \ + CACHE_REMOVE (member); \ + member = tmp; \ + } void gncVendorSetID (GncVendor *vendor, const char *id) { @@ -165,11 +194,12 @@ void gncVendorSetNotes (GncVendor *vendor, const char *notes) void gncVendorSetGUID (GncVendor *vendor, const GUID *guid) { if (!vendor || !guid) return; - if (guid_equal (guid, &vendor->guid)) return; + if (guid_equal (guid, &vendor->inst.guid)) return; + /* XXX this looks fishy to me, this can't possibly be right */ gncVendorBeginEdit (vendor); remObj (vendor); - vendor->guid = *guid; + vendor->inst.guid = *guid; addObj (vendor); gncVendorCommitEdit (vendor); } @@ -245,20 +275,9 @@ void gncVendorSetTaxTable (GncVendor *vendor, GncTaxTable *table) gncVendorCommitEdit (vendor); } +/* ============================================================== */ /* Get Functions */ -QofBook * gncVendorGetBook (GncVendor *vendor) -{ - if (!vendor) return NULL; - return vendor->book; -} - -const GUID * gncVendorGetGUID (GncVendor *vendor) -{ - if (!vendor) return NULL; - return &vendor->guid; -} - const char * gncVendorGetID (GncVendor *vendor) { if (!vendor) return NULL; @@ -327,9 +346,9 @@ void gncVendorAddJob (GncVendor *vendor, GncJob *job) if (g_list_index(vendor->jobs, job) == -1) vendor->jobs = g_list_insert_sorted (vendor->jobs, job, - (GCompareFunc)gncJobCompare); + (GCompareFunc)gncJobCompare); - gnc_engine_generate_event (&vendor->guid, _GNC_MOD_NAME, GNC_EVENT_MODIFY); + gnc_engine_generate_event (&vendor->inst.guid, _GNC_MOD_NAME, GNC_EVENT_MODIFY); } void gncVendorRemoveJob (GncVendor *vendor, GncJob *job) @@ -347,32 +366,39 @@ void gncVendorRemoveJob (GncVendor *vendor, GncJob *job) g_list_free_1 (node); } - gnc_engine_generate_event (&vendor->guid, _GNC_MOD_NAME, GNC_EVENT_MODIFY); + gnc_engine_generate_event (&vendor->inst.guid, _GNC_MOD_NAME, GNC_EVENT_MODIFY); } void gncVendorBeginEdit (GncVendor *vendor) { - GNC_BEGIN_EDIT (vendor, _GNC_MOD_NAME); + GNC_BEGIN_EDIT (&vendor->inst, _GNC_MOD_NAME); } -static void gncVendorOnError (GncVendor *vendor, QofBackendError errcode) +static inline void gncVendorOnError (QofInstance *vendor, QofBackendError errcode) { PERR("Vendor QofBackend Failure: %d", errcode); } -static void gncVendorOnDone (GncVendor *vendor) +static inline void gncVendorOnDone (QofInstance *inst) { - vendor->dirty = FALSE; + GncVendor *vendor = (GncVendor *) inst; gncAddressClearDirty (vendor->addr); } +static inline void vendor_free (QofInstance *inst) +{ + GncVendor *vendor = (GncVendor *) inst; + gncVendorFree (vendor); +} + void gncVendorCommitEdit (GncVendor *vendor) { - GNC_COMMIT_EDIT_PART1 (vendor); - GNC_COMMIT_EDIT_PART2 (vendor, _GNC_MOD_NAME, gncVendorOnError, - gncVendorOnDone, gncVendorFree); + GNC_COMMIT_EDIT_PART1 (&vendor->inst); + GNC_COMMIT_EDIT_PART2 (&vendor->inst, _GNC_MOD_NAME, gncVendorOnError, + gncVendorOnDone, vendor_free); } +/* ============================================================== */ /* Other functions */ int gncVendorCompare (GncVendor *a, GncVendor *b) @@ -395,7 +421,7 @@ GList * gncVendorGetJoblist (GncVendor *vendor, gboolean show_all) for (iterator = vendor->jobs; iterator; iterator=iterator->next) { GncJob *j = iterator->data; if (gncJobGetActive (j)) - list = g_list_append (list, j); + list = g_list_append (list, j); } return list; } @@ -406,7 +432,7 @@ GUID gncVendorRetGUID (GncVendor *vendor) if (!vendor) return *guid_null(); - return vendor->guid; + return vendor->inst.guid; } GncVendor * gncVendorLookupDirect (GUID guid, QofBook *book) @@ -419,27 +445,18 @@ GncVendor * gncVendorLookup (QofBook *book, const GUID *guid) { if (!book || !guid) return NULL; return qof_entity_lookup (gnc_book_get_entity_table (book), - guid, _GNC_MOD_NAME); + guid, _GNC_MOD_NAME); } gboolean gncVendorIsDirty (GncVendor *vendor) { if (!vendor) return FALSE; - return (vendor->dirty || gncAddressIsDirty (vendor->addr)); + return (vendor->inst.dirty || gncAddressIsDirty (vendor->addr)); } +/* ============================================================== */ /* Package-Private functions */ -static void addObj (GncVendor *vendor) -{ - gncBusinessAddObject (vendor->book, _GNC_MOD_NAME, vendor, &vendor->guid); -} - -static void remObj (GncVendor *vendor) -{ - gncBusinessRemoveObject (vendor->book, _GNC_MOD_NAME, &vendor->guid); -} - static void _gncVendorCreate (QofBook *book) { gncBusinessCreate (book, _GNC_MOD_NAME); @@ -461,7 +478,7 @@ static void _gncVendorMarkClean (QofBook *book) } static void _gncVendorForeach (QofBook *book, QofEntityForeachCB cb, - gpointer user_data) + gpointer user_data) { gncBusinessForeach (book, _GNC_MOD_NAME, cb, user_data); } @@ -494,8 +511,8 @@ gboolean gncVendorRegister (void) { VENDOR_ID, QOF_TYPE_STRING, (QofAccessFunc)gncVendorGetID, NULL }, { VENDOR_NAME, QOF_TYPE_STRING, (QofAccessFunc)gncVendorGetName, NULL }, { VENDOR_ADDR, GNC_ADDRESS_MODULE_NAME, (QofAccessFunc)gncVendorGetAddr, NULL }, - { QOF_QUERY_PARAM_BOOK, QOF_ID_BOOK, (QofAccessFunc)gncVendorGetBook, NULL }, - { QOF_QUERY_PARAM_GUID, QOF_TYPE_GUID, (QofAccessFunc)gncVendorGetGUID, NULL }, + { QOF_QUERY_PARAM_BOOK, QOF_ID_BOOK, (QofAccessFunc)qof_instance_get_book, NULL }, + { QOF_QUERY_PARAM_GUID, QOF_TYPE_GUID, (QofAccessFunc)qof_instance_get_guid, NULL }, { QOF_QUERY_PARAM_ACTIVE, QOF_TYPE_BOOLEAN, (QofAccessFunc)gncVendorGetActive, NULL }, { NULL }, }; diff --git a/src/business/business-core/gncVendor.h b/src/business/business-core/gncVendor.h index d319a09d38..5998d8b24f 100644 --- a/src/business/business-core/gncVendor.h +++ b/src/business/business-core/gncVendor.h @@ -43,8 +43,6 @@ void gncVendorCommitEdit (GncVendor *vendor); /* Get Functions */ -QofBook * gncVendorGetBook (GncVendor *vendor); -const GUID * gncVendorGetGUID (GncVendor *vendor); const char * gncVendorGetID (GncVendor *vendor); const char * gncVendorGetName (GncVendor *vendor); GncAddress * gncVendorGetAddr (GncVendor *vendor); @@ -57,6 +55,9 @@ gboolean gncVendorGetActive (GncVendor *vendor); gboolean gncVendorGetTaxTableOverride (GncVendor *vendor); GncTaxTable* gncVendorGetTaxTable (GncVendor *vendor); +/** XXX should be renamed to RetJobList to be consistent with + * other usage, since caller must free the copied list + */ GList * gncVendorGetJoblist (GncVendor *vendor, gboolean show_all); GUID gncVendorRetGUID (GncVendor *vendor); @@ -70,4 +71,8 @@ int gncVendorCompare (GncVendor *a, GncVendor *b); #define VENDOR_NAME "name" #define VENDOR_ADDR "addr" +/** deprecated functions */ +#define gncVendorGetBook(X) qof_instance_get_book (QOF_INSTANCE(X)) +#define gncVendorGetGUID(X) qof_instance_get_guid (QOF_INSTANCE(X)) + #endif /* GNC_VENDOR_H_ */ diff --git a/src/business/business-core/test/test-vendor.c b/src/business/business-core/test/test-vendor.c index 7c9ba4b768..642fc85eab 100644 --- a/src/business/business-core/test/test-vendor.c +++ b/src/business/business-core/test/test-vendor.c @@ -49,7 +49,7 @@ test_vendor (void) do_test (gncVendorCreate (NULL) == NULL, "vendor create NULL"); vendor = gncVendorCreate (book); do_test (vendor != NULL, "vendor create"); - do_test (gncVendorGetBook (vendor) == book, + do_test (qof_instance_get_book (QOF_INSTANCE(vendor)) == book, "getbook"); gncVendorBeginEdit (vendor); @@ -75,7 +75,7 @@ test_vendor (void) guid_new (&guid); vendor = gncVendorCreate (book); count++; gncVendorSetGUID (vendor, &guid); - do_test (guid_equal (&guid, gncVendorGetGUID (vendor)), "guid compare"); + do_test (guid_equal (&guid, qof_instance_get_guid(QOF_INSTANCE(vendor))), "guid compare"); } #if 0 {