|
GnuCash 2.4.99
|
00001 /********************************************************************\ 00002 * gncCustomer.c -- the Core Customer 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 Vepstas <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 00036 #include "gncAddressP.h" 00037 #include "gncBillTermP.h" 00038 #include "gncInvoice.h" 00039 #ifdef GNUCASH_MAJOR_VERSION 00040 #include "gncBusiness.h" 00041 #endif 00042 00043 #include "gncCustomer.h" 00044 #include "gncCustomerP.h" 00045 #include "gncJobP.h" 00046 #include "gncTaxTableP.h" 00047 00048 static gint gs_address_event_handler_id = 0; 00049 static void listen_for_address_events(QofInstance *entity, QofEventId event_type, 00050 gpointer user_data, gpointer event_data); 00051 00052 struct _gncCustomer 00053 { 00054 QofInstance inst; 00055 00056 /* The following fields are identical to 'vendor' */ 00057 char * id; 00058 char * name; 00059 char * notes; 00060 GncBillTerm * terms; 00061 GncAddress * addr; 00062 gnc_commodity * currency; 00063 GncTaxTable* taxtable; 00064 gboolean taxtable_override; 00065 GncTaxIncluded taxincluded; 00066 gboolean active; 00067 GList * jobs; 00068 00069 /* The following fields are unique to 'customer' */ 00070 gnc_numeric credit; 00071 gnc_numeric discount; 00072 GncAddress * shipaddr; 00073 }; 00074 00075 struct _gncCustomerClass 00076 { 00077 QofInstanceClass parent_class; 00078 }; 00079 00080 static QofLogModule log_module = GNC_MOD_BUSINESS; 00081 00082 #define _GNC_MOD_NAME GNC_ID_CUSTOMER 00083 00084 /* ============================================================== */ 00085 /* misc inline funcs */ 00086 00087 G_INLINE_FUNC void mark_customer (GncCustomer *customer); 00088 void mark_customer (GncCustomer *customer) 00089 { 00090 qof_instance_set_dirty(&customer->inst); 00091 qof_event_gen (&customer->inst, QOF_EVENT_MODIFY, NULL); 00092 } 00093 00094 /* ============================================================== */ 00095 00096 enum 00097 { 00098 PROP_0, 00099 PROP_NAME 00100 }; 00101 00102 /* GObject Initialization */ 00103 G_DEFINE_TYPE(GncCustomer, gnc_customer, QOF_TYPE_INSTANCE); 00104 00105 static void 00106 gnc_customer_init(GncCustomer* cust) 00107 { 00108 } 00109 00110 static void 00111 gnc_customer_dispose(GObject *custp) 00112 { 00113 G_OBJECT_CLASS(gnc_customer_parent_class)->dispose(custp); 00114 } 00115 00116 static void 00117 gnc_customer_finalize(GObject* custp) 00118 { 00119 G_OBJECT_CLASS(gnc_customer_parent_class)->finalize(custp); 00120 } 00121 00122 static void 00123 gnc_customer_get_property (GObject *object, 00124 guint prop_id, 00125 GValue *value, 00126 GParamSpec *pspec) 00127 { 00128 GncCustomer *cust; 00129 00130 g_return_if_fail(GNC_IS_CUSTOMER(object)); 00131 00132 cust = GNC_CUSTOMER(object); 00133 switch (prop_id) 00134 { 00135 case PROP_NAME: 00136 g_value_set_string(value, cust->name); 00137 break; 00138 default: 00139 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec); 00140 break; 00141 } 00142 } 00143 00144 static void 00145 gnc_customer_set_property (GObject *object, 00146 guint prop_id, 00147 const GValue *value, 00148 GParamSpec *pspec) 00149 { 00150 GncCustomer *cust; 00151 00152 g_return_if_fail(GNC_IS_CUSTOMER(object)); 00153 00154 cust = GNC_CUSTOMER(object); 00155 switch (prop_id) 00156 { 00157 case PROP_NAME: 00158 gncCustomerSetName(cust, g_value_get_string(value)); 00159 break; 00160 default: 00161 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec); 00162 break; 00163 } 00164 } 00165 00167 static gchar* 00168 impl_get_display_name(const QofInstance* inst) 00169 { 00170 GncCustomer* cust; 00171 00172 g_return_val_if_fail(inst != NULL, FALSE); 00173 g_return_val_if_fail(GNC_IS_CUSTOMER(inst), FALSE); 00174 00175 cust = GNC_CUSTOMER(inst); 00176 /* XXX internationalization of "Customer" */ 00177 return g_strdup_printf("Customer %s", cust->name); 00178 } 00179 00181 static gboolean 00182 impl_refers_to_object(const QofInstance* inst, const QofInstance* ref) 00183 { 00184 GncCustomer* cust; 00185 00186 g_return_val_if_fail(inst != NULL, FALSE); 00187 g_return_val_if_fail(GNC_IS_CUSTOMER(inst), FALSE); 00188 00189 cust = GNC_CUSTOMER(inst); 00190 00191 if (GNC_IS_BILLTERM(ref)) 00192 { 00193 return (cust->terms == GNC_BILLTERM(ref)); 00194 } 00195 else if (GNC_IS_TAXTABLE(ref)) 00196 { 00197 return (cust->taxtable == GNC_TAXTABLE(ref)); 00198 } 00199 00200 return FALSE; 00201 } 00202 00209 static GList* 00210 impl_get_typed_referring_object_list(const QofInstance* inst, const QofInstance* ref) 00211 { 00212 if (!GNC_IS_BILLTERM(ref) && !GNC_IS_TAXTABLE(ref)) 00213 { 00214 return NULL; 00215 } 00216 00217 return qof_instance_get_referring_object_list_from_collection(qof_instance_get_collection(inst), ref); 00218 } 00219 00220 static void 00221 gnc_customer_class_init (GncCustomerClass *klass) 00222 { 00223 GObjectClass *gobject_class = G_OBJECT_CLASS (klass); 00224 QofInstanceClass* qof_class = QOF_INSTANCE_CLASS(klass); 00225 00226 gobject_class->dispose = gnc_customer_dispose; 00227 gobject_class->finalize = gnc_customer_finalize; 00228 gobject_class->set_property = gnc_customer_set_property; 00229 gobject_class->get_property = gnc_customer_get_property; 00230 00231 qof_class->get_display_name = impl_get_display_name; 00232 qof_class->refers_to_object = impl_refers_to_object; 00233 qof_class->get_typed_referring_object_list = impl_get_typed_referring_object_list; 00234 00235 g_object_class_install_property 00236 (gobject_class, 00237 PROP_NAME, 00238 g_param_spec_string ("name", 00239 "Customer Name", 00240 "The customer is an arbitrary string " 00241 "assigned by the user which provides the " 00242 "customer name.", 00243 NULL, 00244 G_PARAM_READWRITE)); 00245 } 00246 00247 /* Create/Destroy Functions */ 00248 GncCustomer *gncCustomerCreate (QofBook *book) 00249 { 00250 GncCustomer *cust; 00251 00252 if (!book) return NULL; 00253 00254 cust = g_object_new (GNC_TYPE_CUSTOMER, NULL); 00255 qof_instance_init_data (&cust->inst, _GNC_MOD_NAME, book); 00256 00257 cust->id = CACHE_INSERT (""); 00258 cust->name = CACHE_INSERT (""); 00259 cust->notes = CACHE_INSERT (""); 00260 cust->addr = gncAddressCreate (book, &cust->inst); 00261 cust->taxincluded = GNC_TAXINCLUDED_USEGLOBAL; 00262 cust->active = TRUE; 00263 cust->jobs = NULL; 00264 00265 cust->discount = gnc_numeric_zero(); 00266 cust->credit = gnc_numeric_zero(); 00267 cust->shipaddr = gncAddressCreate (book, &cust->inst); 00268 00269 if (gs_address_event_handler_id == 0) 00270 { 00271 gs_address_event_handler_id = qof_event_register_handler(listen_for_address_events, NULL); 00272 } 00273 00274 qof_event_gen (&cust->inst, QOF_EVENT_CREATE, NULL); 00275 00276 return cust; 00277 } 00278 00279 void gncCustomerDestroy (GncCustomer *cust) 00280 { 00281 if (!cust) return; 00282 qof_instance_set_destroying(cust, TRUE); 00283 qof_instance_set_dirty (&cust->inst); 00284 gncCustomerCommitEdit (cust); 00285 } 00286 00287 static void gncCustomerFree (GncCustomer *cust) 00288 { 00289 if (!cust) return; 00290 00291 qof_event_gen (&cust->inst, QOF_EVENT_DESTROY, NULL); 00292 00293 CACHE_REMOVE (cust->id); 00294 CACHE_REMOVE (cust->name); 00295 CACHE_REMOVE (cust->notes); 00296 gncAddressBeginEdit (cust->addr); 00297 gncAddressDestroy (cust->addr); 00298 gncAddressBeginEdit (cust->shipaddr); 00299 gncAddressDestroy (cust->shipaddr); 00300 g_list_free (cust->jobs); 00301 00302 if (cust->terms) 00303 gncBillTermDecRef (cust->terms); 00304 if (cust->taxtable) 00305 { 00306 gncTaxTableDecRef (cust->taxtable); 00307 } 00308 00309 /* qof_instance_release (&cust->inst); */ 00310 g_object_unref (cust); 00311 } 00312 00313 /* ============================================================== */ 00314 /* Set Functions */ 00315 00316 #define SET_STR(obj, member, str) { \ 00317 char * tmp; \ 00318 \ 00319 if (!safe_strcmp (member, str)) return; \ 00320 gncCustomerBeginEdit (obj); \ 00321 tmp = CACHE_INSERT (str); \ 00322 CACHE_REMOVE (member); \ 00323 member = tmp; \ 00324 } 00325 00326 void gncCustomerSetID (GncCustomer *cust, const char *id) 00327 { 00328 if (!cust) return; 00329 if (!id) return; 00330 SET_STR(cust, cust->id, id); 00331 mark_customer (cust); 00332 gncCustomerCommitEdit (cust); 00333 } 00334 00335 void gncCustomerSetName (GncCustomer *cust, const char *name) 00336 { 00337 if (!cust) return; 00338 if (!name) return; 00339 SET_STR(cust, cust->name, name); 00340 mark_customer (cust); 00341 gncCustomerCommitEdit (cust); 00342 } 00343 00344 void gncCustomerSetNotes (GncCustomer *cust, const char *notes) 00345 { 00346 if (!cust) return; 00347 if (!notes) return; 00348 SET_STR(cust, cust->notes, notes); 00349 mark_customer (cust); 00350 gncCustomerCommitEdit (cust); 00351 } 00352 00353 void gncCustomerSetTerms (GncCustomer *cust, GncBillTerm *terms) 00354 { 00355 if (!cust) return; 00356 if (cust->terms == terms) return; 00357 00358 gncCustomerBeginEdit (cust); 00359 if (cust->terms) 00360 gncBillTermDecRef (cust->terms); 00361 cust->terms = terms; 00362 if (cust->terms) 00363 gncBillTermIncRef (cust->terms); 00364 mark_customer (cust); 00365 gncCustomerCommitEdit (cust); 00366 } 00367 00368 void gncCustomerSetTaxIncluded (GncCustomer *cust, GncTaxIncluded taxincl) 00369 { 00370 if (!cust) return; 00371 if (taxincl == cust->taxincluded) return; 00372 gncCustomerBeginEdit (cust); 00373 cust->taxincluded = taxincl; 00374 mark_customer (cust); 00375 gncCustomerCommitEdit (cust); 00376 } 00377 00378 void gncCustomerSetActive (GncCustomer *cust, gboolean active) 00379 { 00380 if (!cust) return; 00381 if (active == cust->active) return; 00382 gncCustomerBeginEdit (cust); 00383 cust->active = active; 00384 mark_customer (cust); 00385 gncCustomerCommitEdit (cust); 00386 } 00387 00388 void gncCustomerSetDiscount (GncCustomer *cust, gnc_numeric discount) 00389 { 00390 if (!cust) return; 00391 if (gnc_numeric_equal (discount, cust->discount)) return; 00392 gncCustomerBeginEdit (cust); 00393 cust->discount = discount; 00394 mark_customer (cust); 00395 gncCustomerCommitEdit (cust); 00396 } 00397 00398 void gncCustomerSetCredit (GncCustomer *cust, gnc_numeric credit) 00399 { 00400 if (!cust) return; 00401 if (gnc_numeric_equal (credit, cust->credit)) return; 00402 gncCustomerBeginEdit (cust); 00403 cust->credit = credit; 00404 mark_customer (cust); 00405 gncCustomerCommitEdit (cust); 00406 } 00407 00408 void gncCustomerSetCurrency (GncCustomer *cust, gnc_commodity *currency) 00409 { 00410 if (!cust || !currency) return; 00411 if (cust->currency && gnc_commodity_equal (cust->currency, currency)) return; 00412 gncCustomerBeginEdit (cust); 00413 cust->currency = currency; 00414 mark_customer (cust); 00415 gncCustomerCommitEdit (cust); 00416 } 00417 00418 void gncCustomerSetTaxTableOverride (GncCustomer *customer, gboolean override) 00419 { 00420 if (!customer) return; 00421 if (customer->taxtable_override == override) return; 00422 gncCustomerBeginEdit (customer); 00423 customer->taxtable_override = override; 00424 mark_customer (customer); 00425 gncCustomerCommitEdit (customer); 00426 } 00427 00428 void gncCustomerSetTaxTable (GncCustomer *customer, GncTaxTable *table) 00429 { 00430 if (!customer) return; 00431 if (customer->taxtable == table) return; 00432 00433 gncCustomerBeginEdit (customer); 00434 if (customer->taxtable) 00435 gncTaxTableDecRef (customer->taxtable); 00436 if (table) 00437 gncTaxTableIncRef (table); 00438 customer->taxtable = table; 00439 mark_customer (customer); 00440 gncCustomerCommitEdit (customer); 00441 } 00442 00443 /* Note that JobList changes do not affect the "dirtiness" of the customer */ 00444 void gncCustomerAddJob (GncCustomer *cust, GncJob *job) 00445 { 00446 if (!cust) return; 00447 if (!job) return; 00448 00449 if (g_list_index(cust->jobs, job) == -1) 00450 cust->jobs = g_list_insert_sorted (cust->jobs, job, 00451 (GCompareFunc)gncJobCompare); 00452 00453 qof_event_gen (&cust->inst, QOF_EVENT_MODIFY, NULL); 00454 } 00455 00456 void gncCustomerRemoveJob (GncCustomer *cust, GncJob *job) 00457 { 00458 GList *node; 00459 00460 if (!cust) return; 00461 if (!job) return; 00462 00463 node = g_list_find (cust->jobs, job); 00464 if (!node) 00465 { 00466 /* PERR ("split not in account"); */ 00467 } 00468 else 00469 { 00470 cust->jobs = g_list_remove_link (cust->jobs, node); 00471 g_list_free_1 (node); 00472 } 00473 qof_event_gen (&cust->inst, QOF_EVENT_MODIFY, NULL); 00474 } 00475 00476 void gncCustomerBeginEdit (GncCustomer *cust) 00477 { 00478 qof_begin_edit (&cust->inst); 00479 } 00480 00481 static void gncCustomerOnError (QofInstance *inst, QofBackendError errcode) 00482 { 00483 PERR("Customer QofBackend Failure: %d", errcode); 00484 gnc_engine_signal_commit_error( errcode ); 00485 } 00486 00487 static void gncCustomerOnDone (QofInstance *inst) 00488 { 00489 GncCustomer *cust = (GncCustomer *) inst; 00490 gncAddressClearDirty (cust->addr); 00491 gncAddressClearDirty (cust->shipaddr); 00492 } 00493 00494 static void cust_free (QofInstance *inst) 00495 { 00496 GncCustomer *cust = (GncCustomer *) inst; 00497 gncCustomerFree (cust); 00498 } 00499 00500 void gncCustomerCommitEdit (GncCustomer *cust) 00501 { 00502 if (!qof_commit_edit (QOF_INSTANCE(cust))) return; 00503 qof_commit_edit_part2 (&cust->inst, gncCustomerOnError, 00504 gncCustomerOnDone, cust_free); 00505 } 00506 00507 /* ============================================================== */ 00508 /* Get Functions */ 00509 00510 const char * gncCustomerGetID (const GncCustomer *cust) 00511 { 00512 if (!cust) return NULL; 00513 return cust->id; 00514 } 00515 00516 const char * gncCustomerGetName (const GncCustomer *cust) 00517 { 00518 if (!cust) return NULL; 00519 return cust->name; 00520 } 00521 00522 GncAddress * gncCustomerGetAddr (const GncCustomer *cust) 00523 { 00524 if (!cust) return NULL; 00525 return cust->addr; 00526 } 00527 00528 static void 00529 qofCustomerSetAddr (GncCustomer *cust, QofInstance *addr_ent) 00530 { 00531 GncAddress *addr; 00532 00533 if (!cust || !addr_ent) 00534 { 00535 return; 00536 } 00537 addr = (GncAddress*)addr_ent; 00538 if (addr == cust->addr) 00539 { 00540 return; 00541 } 00542 if (cust->addr != NULL) 00543 { 00544 gncAddressBeginEdit(cust->addr); 00545 gncAddressDestroy(cust->addr); 00546 } 00547 gncCustomerBeginEdit(cust); 00548 cust->addr = addr; 00549 gncCustomerCommitEdit(cust); 00550 } 00551 00552 static void 00553 qofCustomerSetShipAddr (GncCustomer *cust, QofInstance *ship_addr_ent) 00554 { 00555 GncAddress *ship_addr; 00556 00557 if (!cust || !ship_addr_ent) 00558 { 00559 return; 00560 } 00561 ship_addr = (GncAddress*)ship_addr_ent; 00562 if (ship_addr == cust->shipaddr) 00563 { 00564 return; 00565 } 00566 if (cust->shipaddr != NULL) 00567 { 00568 gncAddressBeginEdit(cust->shipaddr); 00569 gncAddressDestroy(cust->shipaddr); 00570 } 00571 gncCustomerBeginEdit(cust); 00572 cust->shipaddr = ship_addr; 00573 gncCustomerCommitEdit(cust); 00574 } 00575 00576 GncAddress * gncCustomerGetShipAddr (const GncCustomer *cust) 00577 { 00578 if (!cust) return NULL; 00579 return cust->shipaddr; 00580 } 00581 00582 const char * gncCustomerGetNotes (const GncCustomer *cust) 00583 { 00584 if (!cust) return NULL; 00585 return cust->notes; 00586 } 00587 00588 GncBillTerm * gncCustomerGetTerms (const GncCustomer *cust) 00589 { 00590 if (!cust) return NULL; 00591 return cust->terms; 00592 } 00593 00594 GncTaxIncluded gncCustomerGetTaxIncluded (const GncCustomer *cust) 00595 { 00596 if (!cust) return GNC_TAXINCLUDED_USEGLOBAL; 00597 return cust->taxincluded; 00598 } 00599 00600 gnc_commodity * gncCustomerGetCurrency (const GncCustomer *cust) 00601 { 00602 if (!cust) return NULL; 00603 return cust->currency; 00604 } 00605 00606 gboolean gncCustomerGetActive (const GncCustomer *cust) 00607 { 00608 if (!cust) return FALSE; 00609 return cust->active; 00610 } 00611 00612 gnc_numeric gncCustomerGetDiscount (const GncCustomer *cust) 00613 { 00614 if (!cust) return gnc_numeric_zero(); 00615 return cust->discount; 00616 } 00617 00618 gnc_numeric gncCustomerGetCredit (const GncCustomer *cust) 00619 { 00620 if (!cust) return gnc_numeric_zero(); 00621 return cust->credit; 00622 } 00623 00624 gboolean gncCustomerGetTaxTableOverride (const GncCustomer *customer) 00625 { 00626 if (!customer) return FALSE; 00627 return customer->taxtable_override; 00628 } 00629 00630 GncTaxTable* gncCustomerGetTaxTable (const GncCustomer *customer) 00631 { 00632 if (!customer) return NULL; 00633 return customer->taxtable; 00634 } 00635 00636 GList * gncCustomerGetJoblist (const GncCustomer *cust, gboolean show_all) 00637 { 00638 if (!cust) return NULL; 00639 00640 if (show_all) 00641 { 00642 return (g_list_copy (cust->jobs)); 00643 } 00644 else 00645 { 00646 GList *list = NULL, *iterator; 00647 for (iterator = cust->jobs; iterator; iterator = iterator->next) 00648 { 00649 GncJob *j = iterator->data; 00650 if (gncJobGetActive (j)) 00651 list = g_list_append (list, j); 00652 } 00653 return list; 00654 } 00655 } 00656 00657 gboolean gncCustomerIsDirty (GncCustomer *cust) 00658 { 00659 if (!cust) return FALSE; 00660 return (qof_instance_is_dirty(&cust->inst) || 00661 gncAddressIsDirty (cust->addr) || 00662 gncAddressIsDirty (cust->shipaddr)); 00663 } 00664 00665 /* Other functions */ 00666 00667 int gncCustomerCompare (const GncCustomer *a, const GncCustomer *b) 00668 { 00669 if (!a && !b) return 0; 00670 if (!a && b) return 1; 00671 if (a && !b) return -1; 00672 00673 return(strcmp(a->name, b->name)); 00674 } 00675 00676 gboolean 00677 gncCustomerEqual(const GncCustomer *a, const GncCustomer *b) 00678 { 00679 if (a == NULL && b == NULL) return TRUE; 00680 if (a == NULL || b == NULL) return FALSE; 00681 00682 g_return_val_if_fail(GNC_IS_CUSTOMER(a), FALSE); 00683 g_return_val_if_fail(GNC_IS_CUSTOMER(b), FALSE); 00684 00685 if (safe_strcmp(a->id, b->id) != 0) 00686 { 00687 PWARN("IDs differ: %s vs %s", a->id, b->id); 00688 return FALSE; 00689 } 00690 00691 if (safe_strcmp(a->name, b->name) != 0) 00692 { 00693 PWARN("Names differ: %s vs %s", a->name, b->name); 00694 return FALSE; 00695 } 00696 00697 if (safe_strcmp(a->notes, b->notes) != 0) 00698 { 00699 PWARN("Notes differ: %s vs %s", a->notes, b->notes); 00700 return FALSE; 00701 } 00702 00703 if (!gncBillTermEqual(a->terms, b->terms)) 00704 { 00705 PWARN("Bill terms differ"); 00706 return FALSE; 00707 } 00708 00709 if (!gnc_commodity_equal(a->currency, b->currency)) 00710 { 00711 PWARN("currencies differ"); 00712 return FALSE; 00713 } 00714 00715 if (!gncTaxTableEqual(a->taxtable, b->taxtable)) 00716 { 00717 PWARN("tax tables differ"); 00718 return FALSE; 00719 } 00720 00721 if (a->taxtable_override != b->taxtable_override) 00722 { 00723 PWARN("Tax table override flags differ"); 00724 return FALSE; 00725 } 00726 00727 if (a->taxincluded != b->taxincluded) 00728 { 00729 PWARN("Tax included flags differ"); 00730 return FALSE; 00731 } 00732 00733 if (a->active != b->active) 00734 { 00735 PWARN("Active flags differ"); 00736 return FALSE; 00737 } 00738 00739 if (!gncAddressEqual(a->addr, b->addr)) 00740 { 00741 PWARN("addresses differ"); 00742 return FALSE; 00743 } 00744 if (!gncAddressEqual(a->shipaddr, b->shipaddr)) 00745 { 00746 PWARN("addresses differ"); 00747 return FALSE; 00748 } 00749 00750 if (!gnc_numeric_equal(a->credit, b->credit)) 00751 { 00752 PWARN("Credit amounts differ"); 00753 return FALSE; 00754 } 00755 00756 if (!gnc_numeric_equal(a->discount, b->discount)) 00757 { 00758 PWARN("Discount amounts differ"); 00759 return FALSE; 00760 } 00761 00762 /* FIXME: Need to check jobs list 00763 GList * jobs; 00764 */ 00765 00766 return TRUE; 00767 } 00768 00778 static void 00779 listen_for_address_events(QofInstance *entity, QofEventId event_type, 00780 gpointer user_data, gpointer event_data) 00781 { 00782 GncCustomer* cust; 00783 00784 if ((event_type & QOF_EVENT_MODIFY) == 0) 00785 { 00786 return; 00787 } 00788 if (!GNC_IS_ADDRESS(entity)) 00789 { 00790 return; 00791 } 00792 if (!GNC_IS_CUSTOMER(event_data)) 00793 { 00794 return; 00795 } 00796 cust = GNC_CUSTOMER(event_data); 00797 gncCustomerBeginEdit(cust); 00798 mark_customer(cust); 00799 gncCustomerCommitEdit(cust); 00800 } 00801 /* ============================================================== */ 00802 /* Package-Private functions */ 00803 static const char * _gncCustomerPrintable (gpointer item) 00804 { 00805 // GncCustomer *c = item; 00806 if (!item) return "failed"; 00807 return gncCustomerGetName((GncCustomer*)item); 00808 } 00809 00810 static void 00811 destroy_customer_on_book_close(QofInstance *ent, gpointer data) 00812 { 00813 GncCustomer* c = GNC_CUSTOMER(ent); 00814 00815 gncCustomerBeginEdit(c); 00816 gncCustomerDestroy(c); 00817 } 00818 00823 static void 00824 gnc_customer_book_end(QofBook* book) 00825 { 00826 QofCollection *col; 00827 00828 col = qof_book_get_collection(book, GNC_ID_CUSTOMER); 00829 qof_collection_foreach(col, destroy_customer_on_book_close, NULL); 00830 } 00831 00832 static QofObject gncCustomerDesc = 00833 { 00834 DI(.interface_version = ) QOF_OBJECT_VERSION, 00835 DI(.e_type = ) _GNC_MOD_NAME, 00836 DI(.type_label = ) "Customer", 00837 DI(.create = ) (gpointer)gncCustomerCreate, 00838 DI(.book_begin = ) NULL, 00839 DI(.book_end = ) gnc_customer_book_end, 00840 DI(.is_dirty = ) qof_collection_is_dirty, 00841 DI(.mark_clean = ) qof_collection_mark_clean, 00842 DI(.foreach = ) qof_collection_foreach, 00843 DI(.printable = ) (const char * (*)(gpointer))gncCustomerGetName, 00844 DI(.version_cmp = ) (int (*)(gpointer, gpointer)) qof_instance_version_cmp, 00845 }; 00846 00847 gboolean gncCustomerRegister (void) 00848 { 00849 static QofParam params[] = 00850 { 00851 { CUSTOMER_ID, QOF_TYPE_STRING, (QofAccessFunc)gncCustomerGetID, (QofSetterFunc)gncCustomerSetID }, 00852 { CUSTOMER_NAME, QOF_TYPE_STRING, (QofAccessFunc)gncCustomerGetName, (QofSetterFunc)gncCustomerSetName }, 00853 { CUSTOMER_NOTES, QOF_TYPE_STRING, (QofAccessFunc)gncCustomerGetNotes, (QofSetterFunc)gncCustomerSetNotes }, 00854 { 00855 CUSTOMER_DISCOUNT, QOF_TYPE_NUMERIC, (QofAccessFunc)gncCustomerGetDiscount, 00856 (QofSetterFunc)gncCustomerSetDiscount 00857 }, 00858 { 00859 CUSTOMER_CREDIT, QOF_TYPE_NUMERIC, (QofAccessFunc)gncCustomerGetCredit, 00860 (QofSetterFunc)gncCustomerSetCredit 00861 }, 00862 { CUSTOMER_ADDR, GNC_ID_ADDRESS, (QofAccessFunc)gncCustomerGetAddr, (QofSetterFunc)qofCustomerSetAddr }, 00863 { CUSTOMER_SHIPADDR, GNC_ID_ADDRESS, (QofAccessFunc)gncCustomerGetShipAddr, (QofSetterFunc)qofCustomerSetShipAddr }, 00864 { 00865 CUSTOMER_TT_OVER, QOF_TYPE_BOOLEAN, (QofAccessFunc)gncCustomerGetTaxTableOverride, 00866 (QofSetterFunc)gncCustomerSetTaxTableOverride 00867 }, 00868 { CUSTOMER_TERMS, GNC_ID_BILLTERM, (QofAccessFunc)gncCustomerGetTerms, (QofSetterFunc)gncCustomerSetTerms }, 00869 { CUSTOMER_SLOTS, QOF_TYPE_KVP, (QofAccessFunc)qof_instance_get_slots, NULL }, 00870 { QOF_PARAM_ACTIVE, QOF_TYPE_BOOLEAN, (QofAccessFunc)gncCustomerGetActive, (QofSetterFunc)gncCustomerSetActive }, 00871 { QOF_PARAM_BOOK, QOF_ID_BOOK, (QofAccessFunc)qof_instance_get_book, NULL }, 00872 { QOF_PARAM_GUID, QOF_TYPE_GUID, (QofAccessFunc)qof_instance_get_guid, NULL }, 00873 { NULL }, 00874 }; 00875 00876 if (!qof_choice_add_class(GNC_ID_INVOICE, GNC_ID_CUSTOMER, INVOICE_OWNER)) 00877 { 00878 return FALSE; 00879 } 00880 if (!qof_choice_add_class(GNC_ID_JOB, GNC_ID_CUSTOMER, JOB_OWNER)) 00881 { 00882 return FALSE; 00883 } 00884 qof_class_register (_GNC_MOD_NAME, (QofSortFunc)gncCustomerCompare, params); 00885 if (!qof_choice_create(GNC_ID_CUSTOMER)) 00886 { 00887 return FALSE; 00888 } 00889 /* temp */ 00890 _gncCustomerPrintable(NULL); 00891 return qof_object_register (&gncCustomerDesc); 00892 } 00893 00894 gchar *gncCustomerNextID (QofBook *book) 00895 { 00896 return qof_book_increment_and_format_counter (book, _GNC_MOD_NAME); 00897 }
1.7.4