GnuCash  2.6.99
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
gncVendor.c
1 /********************************************************************\
2  * gncVendor.c -- the Core Vendor Interface *
3  * *
4  * This program is free software; you can redistribute it and/or *
5  * modify it under the terms of the GNU General Public License as *
6  * published by the Free Software Foundation; either version 2 of *
7  * the License, or (at your option) any later version. *
8  * *
9  * This program is distributed in the hope that it will be useful, *
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
12  * GNU General Public License for more details. *
13  * *
14  * You should have received a copy of the GNU General Public License*
15  * along with this program; if not, contact: *
16  * *
17  * Free Software Foundation Voice: +1-617-542-5942 *
18  * 51 Franklin Street, Fifth Floor Fax: +1-617-542-2652 *
19  * Boston, MA 02110-1301, USA [email protected] *
20  * *
21 \********************************************************************/
22 
23 /*
24  * Copyright (C) 2001, 2002 Derek Atkins
25  * Copyright (C) 2003 <[email protected]>
26  * Author: Derek Atkins <[email protected]>
27  */
28 
29 #include <config.h>
30 
31 #include <glib.h>
32 #include <string.h>
33 #include <qofinstance-p.h>
34 
35 #include "gnc-commodity.h"
36 #include "gncAddressP.h"
37 #include "gncBillTermP.h"
38 #include "gncInvoice.h"
39 #include "gncJobP.h"
40 #include "gncTaxTableP.h"
41 #include "gncVendor.h"
42 #include "gncVendorP.h"
43 
44 static gint gs_address_event_handler_id = 0;
45 static void listen_for_address_events(QofInstance *entity, QofEventId event_type,
46  gpointer user_data, gpointer event_data);
47 static void qofVendorSetAddr (GncVendor *vendor, QofInstance *addr_ent);
48 static const char* qofVendorGetTaxIncluded(const GncVendor *vendor);
49 static void qofVendorSetTaxIncluded(GncVendor *vendor, const char* type_string);
50 
51 struct _gncVendor
52 {
53  QofInstance inst;
54 
55  char * id;
56  char * name;
57  char * notes;
58  GncBillTerm * terms;
59  GncAddress * addr;
60  gnc_commodity * currency;
61  GncTaxTable* taxtable;
62  gboolean taxtable_override;
63  GncTaxIncluded taxincluded;
64  gboolean active;
65  GList * jobs;
66 };
67 
69 {
70  QofInstanceClass parent_class;
71 };
72 
73 static QofLogModule log_module = GNC_MOD_BUSINESS;
74 
75 #define _GNC_MOD_NAME GNC_ID_VENDOR
76 
77 /* ============================================================ */
78 /* Misc inline funcs */
79 
80 G_INLINE_FUNC void mark_vendor (GncVendor *vendor);
81 void mark_vendor (GncVendor *vendor)
82 {
83  qof_instance_set_dirty(&vendor->inst);
84  qof_event_gen (&vendor->inst, QOF_EVENT_MODIFY, NULL);
85 }
86 
87 /* ============================================================== */
88 
89 enum
90 {
91  PROP_0,
92  PROP_NAME, /* Table */
93  PROP_ID, /* Table */
94  PROP_NOTES, /* Table */
95  PROP_CURRENCY, /* Table */
96  PROP_ACTIVE, /* Table */
97  PROP_TAXTABLE_OVERRIDE, /* Table */
98  PROP_BILLTERMS, /* Table */
99  PROP_TAXTABLE, /* Table */
100  PROP_ADDRESS, /* Table, 8 fields */
101  PROP_TAX_INCLUDED, /* Table */
102  PROP_TAX_INCLUDED_STR, /* Alternate setter for PROP_TAX_INCLUDED */
103  PROP_PDF_DIRNAME, /* KVP */
104  PROP_LAST_POSTED, /* KVP */
105  PROP_PAYMENT_LAST_ACCT, /* KVP */
106 };
107 
108 /* GObject Initialization */
109 G_DEFINE_TYPE(GncVendor, gnc_vendor, QOF_TYPE_INSTANCE);
110 
111 static void
112 gnc_vendor_init(GncVendor* vendor)
113 {
114 }
115 
116 static void
117 gnc_vendor_dispose(GObject *vendorp)
118 {
119  G_OBJECT_CLASS(gnc_vendor_parent_class)->dispose(vendorp);
120 }
121 
122 static void
123 gnc_vendor_finalize(GObject* vendorp)
124 {
125  G_OBJECT_CLASS(gnc_vendor_parent_class)->finalize(vendorp);
126 }
127 
128 /* Note that g_value_set_object() refs the object, as does
129  * g_object_get(). But g_object_get() only unrefs once when it disgorges
130  * the object, leaving an unbalanced ref, which leaks. So instead of
131  * using g_value_set_object(), use g_value_take_object() which doesn't
132  * ref the object when used in get_property().
133  */
134 static void
135 gnc_vendor_get_property (GObject *object,
136  guint prop_id,
137  GValue *value,
138  GParamSpec *pspec)
139 {
140  GncVendor *vendor;
141  gchar *key;
142 
143  g_return_if_fail(GNC_IS_VENDOR(object));
144 
145  vendor = GNC_VENDOR(object);
146  switch (prop_id)
147  {
148  case PROP_NAME:
149  g_value_set_string(value, vendor->name);
150  break;
151  case PROP_ID:
152  g_value_set_string(value, vendor->id);
153  break;
154  case PROP_NOTES:
155  g_value_set_string(value, vendor->notes);
156  break;
157  case PROP_CURRENCY:
158  g_value_take_object(value, vendor->currency);
159  break;
160  case PROP_ACTIVE:
161  g_value_set_boolean(value, vendor->active);
162  break;
163  case PROP_TAXTABLE_OVERRIDE:
164  g_value_set_boolean(value, vendor->taxtable_override);
165  break;
166  case PROP_BILLTERMS:
167  g_value_take_object(value, vendor->terms);
168  break;
169  case PROP_TAXTABLE:
170  g_value_take_object(value, vendor->taxtable);
171  break;
172  case PROP_ADDRESS:
173  g_value_take_object(value, vendor->addr);
174  break;
175  case PROP_TAX_INCLUDED:
176  g_value_set_int(value, vendor->taxincluded);
177  break;
178  case PROP_TAX_INCLUDED_STR:
179  g_value_set_string(value, qofVendorGetTaxIncluded(vendor));
180  break;
181  case PROP_PDF_DIRNAME:
182  key = OWNER_EXPORT_PDF_DIRNAME;
183  qof_instance_get_kvp (QOF_INSTANCE (vendor), key, value);
184  break;
185  case PROP_LAST_POSTED:
186  key = LAST_POSTED_TO_ACCT;
187  qof_instance_get_kvp (QOF_INSTANCE (vendor), key, value);
188  break;
189  case PROP_PAYMENT_LAST_ACCT:
190  key = GNC_PAYMENT "/" GNC_LAST_ACCOUNT;
191  qof_instance_get_kvp (QOF_INSTANCE (vendor), key, value);
192  break;
193  default:
194  G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
195  break;
196  }
197 }
198 
199 static void
200 gnc_vendor_set_property (GObject *object,
201  guint prop_id,
202  const GValue *value,
203  GParamSpec *pspec)
204 {
205  GncVendor *vendor;
206  gchar *key;
207 
208  g_return_if_fail(GNC_IS_VENDOR(object));
209 
210  vendor = GNC_VENDOR(object);
211  g_assert (qof_instance_get_editlevel(vendor));
212 
213  switch (prop_id)
214  {
215  case PROP_NAME:
216  gncVendorSetName(vendor, g_value_get_string(value));
217  break;
218  case PROP_ID:
219  gncVendorSetID(vendor, g_value_get_string(value));
220  break;
221  case PROP_NOTES:
222  gncVendorSetNotes(vendor, g_value_get_string(value));
223  break;
224  case PROP_CURRENCY:
225  gncVendorSetCurrency(vendor, g_value_get_object(value));
226  break;
227  case PROP_ACTIVE:
228  gncVendorSetActive(vendor, g_value_get_boolean(value));
229  break;
230  case PROP_TAXTABLE_OVERRIDE:
231  gncVendorSetTaxTableOverride(vendor, g_value_get_boolean(value));
232  break;
233  case PROP_BILLTERMS:
234  gncVendorSetTerms(vendor, g_value_get_object(value));
235  break;
236  case PROP_TAXTABLE:
237  gncVendorSetTaxTable(vendor, g_value_get_object(value));
238  break;
239  case PROP_ADDRESS:
240  qofVendorSetAddr(vendor, g_value_get_object(value));
241  break;
242  case PROP_TAX_INCLUDED:
243  gncVendorSetTaxIncluded(vendor, (GncTaxIncluded)g_value_get_int(value));
244  break;
245  case PROP_TAX_INCLUDED_STR:
246  qofVendorSetTaxIncluded(vendor, g_value_get_string(value));
247  break;
248  case PROP_PDF_DIRNAME:
249  key = OWNER_EXPORT_PDF_DIRNAME;
250  qof_instance_set_kvp (QOF_INSTANCE (vendor), key, value);
251  break;
252  case PROP_LAST_POSTED:
253  key = LAST_POSTED_TO_ACCT;
254  qof_instance_set_kvp (QOF_INSTANCE (vendor), key, value);
255  break;
256  case PROP_PAYMENT_LAST_ACCT:
257  key = GNC_PAYMENT "/" GNC_LAST_ACCOUNT;
258  qof_instance_set_kvp (QOF_INSTANCE (vendor), key, value);
259  break;
260  default:
261  G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
262  break;
263  }
264 }
265 
267 static gboolean
268 impl_refers_to_object(const QofInstance* inst, const QofInstance* ref)
269 {
270  GncVendor* v;
271 
272  g_return_val_if_fail(inst != NULL, FALSE);
273  g_return_val_if_fail(GNC_IS_VENDOR(inst), FALSE);
274 
275  v = GNC_VENDOR(inst);
276 
277  if (GNC_IS_BILLTERM(ref))
278  {
279  return (v->terms == GNC_BILLTERM(ref));
280  }
281  else if (GNC_IS_TAXTABLE(ref))
282  {
283  return (v->taxtable == GNC_TAXTABLE(ref));
284  }
285 
286  return FALSE;
287 }
288 
295 static GList*
296 impl_get_typed_referring_object_list(const QofInstance* inst, const QofInstance* ref)
297 {
298  if (!GNC_IS_BILLTERM(ref) && !GNC_IS_TAXTABLE(ref))
299  {
300  return NULL;
301  }
302 
304 }
305 
306 static void
307 gnc_vendor_class_init (GncVendorClass *klass)
308 {
309  GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
310  QofInstanceClass* qof_class = QOF_INSTANCE_CLASS(klass);
311 
312  gobject_class->dispose = gnc_vendor_dispose;
313  gobject_class->finalize = gnc_vendor_finalize;
314  gobject_class->set_property = gnc_vendor_set_property;
315  gobject_class->get_property = gnc_vendor_get_property;
316 
317  qof_class->get_display_name = NULL;
318  qof_class->refers_to_object = impl_refers_to_object;
319  qof_class->get_typed_referring_object_list = impl_get_typed_referring_object_list;
320 
321  g_object_class_install_property
322  (gobject_class,
323  PROP_NAME,
324  g_param_spec_string ("name",
325  "Vendor Name",
326  "The vendor name is an arbitrary string "
327  "assigned by the user to provide the vendor name.",
328  NULL,
329  G_PARAM_READWRITE));
330 
331  g_object_class_install_property
332  (gobject_class,
333  PROP_ID,
334  g_param_spec_string ("id",
335  "Vendor ID",
336  "The vendor id is an arbitrary string "
337  "assigned by the user to identify the vendor.",
338  NULL,
339  G_PARAM_READWRITE));
340 
341  g_object_class_install_property
342  (gobject_class,
343  PROP_NOTES,
344  g_param_spec_string ("notes",
345  "Vendor notes",
346  "The vendor notes is an arbitrary string "
347  "assigned by the user to add extra information about the vendor.",
348  NULL,
349  G_PARAM_READWRITE));
350 
351  g_object_class_install_property
352  (gobject_class,
353  PROP_CURRENCY,
354  g_param_spec_object ("currency",
355  "Currency",
356  "The currency property denotes the currency used by this vendor.",
357  GNC_TYPE_COMMODITY,
358  G_PARAM_READWRITE));
359 
360  g_object_class_install_property
361  (gobject_class,
362  PROP_ACTIVE,
363  g_param_spec_boolean ("active",
364  "Active",
365  "TRUE if the vendor is active. FALSE if inactive.",
366  FALSE,
367  G_PARAM_READWRITE));
368 
369  g_object_class_install_property
370  (gobject_class,
371  PROP_TAXTABLE_OVERRIDE,
372  g_param_spec_boolean ("tax-table-override",
373  "Tax table override",
374  "TRUE if the vendor has a specific tax table which overrides the default "
375  "tax table. FALSE if the default table should be used.",
376  FALSE,
377  G_PARAM_READWRITE));
378 
379  g_object_class_install_property
380  (gobject_class,
381  PROP_BILLTERMS,
382  g_param_spec_object ("terms",
383  "Terms",
384  "The billing terms used by this vendor.",
385  GNC_TYPE_COMMODITY,
386  G_PARAM_READWRITE));
387 
388  g_object_class_install_property
389  (gobject_class,
390  PROP_TAXTABLE,
391  g_param_spec_object ("tax-table",
392  "Tax table",
393  "The tax table which applies to this vendor.",
394  GNC_TYPE_COMMODITY,
395  G_PARAM_READWRITE));
396 
397  g_object_class_install_property
398  (gobject_class,
399  PROP_ADDRESS,
400  g_param_spec_object ("address",
401  "Address",
402  "The address property contains the address information for this vendor.",
403  GNC_TYPE_ADDRESS,
404  G_PARAM_READWRITE));
405 
406  g_object_class_install_property
407  (gobject_class,
408  PROP_TAX_INCLUDED,
409  g_param_spec_int ("tax-included",
410  "Tax included",
411  "The tax-included property contains the information about tax calculation this vendor.",
412  GNC_TAXINCLUDED_YES, /* min */
413  GNC_TAXINCLUDED_USEGLOBAL, /* max */
414  GNC_TAXINCLUDED_USEGLOBAL, /* default */
415  G_PARAM_READWRITE));
416 
417  g_object_class_install_property
418  (gobject_class,
419  PROP_TAX_INCLUDED_STR,
420  g_param_spec_string("tax-included-string",
421  "Tax included string",
422  "The tax-included-string property contains a character version of tax-included.",
423  FALSE,
424  G_PARAM_READWRITE));
425  g_object_class_install_property
426  (gobject_class,
427  PROP_PDF_DIRNAME,
428  g_param_spec_string ("export-pdf-dir",
429  "Export PDF Directory Name",
430  "A subdirectory for exporting PDF reports which is "
431  "appended to the target directory when writing them "
432  "out. It is retrieved from preferences and stored on "
433  "each 'Owner' object which prints items after "
434  "printing.",
435  NULL,
436  G_PARAM_READWRITE));
437 
438  g_object_class_install_property(
439  gobject_class,
440  PROP_LAST_POSTED,
441  g_param_spec_boxed("invoice-last-posted-account",
442  "Invoice Last Posted Account",
443  "The last account to which an invoice belonging to "
444  "this owner was posted.",
445  GNC_TYPE_GUID,
446  G_PARAM_READWRITE));
447 
448  g_object_class_install_property(
449  gobject_class,
450  PROP_PAYMENT_LAST_ACCT,
451  g_param_spec_boxed("payment-last-account",
452  "Payment Last Account",
453  "The last account to which an payment belonging to "
454  "this owner was posted.",
455  GNC_TYPE_GUID,
456  G_PARAM_READWRITE));
457 }
458 
459 /* Create/Destroy Functions */
460 GncVendor *gncVendorCreate (QofBook *book)
461 {
462  GncVendor *vendor;
463 
464  if (!book) return NULL;
465 
466  vendor = g_object_new (GNC_TYPE_VENDOR, NULL);
467  qof_instance_init_data (&vendor->inst, _GNC_MOD_NAME, book);
468 
469  vendor->id = CACHE_INSERT ("");
470  vendor->name = CACHE_INSERT ("");
471  vendor->notes = CACHE_INSERT ("");
472  vendor->addr = gncAddressCreate (book, &vendor->inst);
473  vendor->taxincluded = GNC_TAXINCLUDED_USEGLOBAL;
474  vendor->active = TRUE;
475  vendor->jobs = NULL;
476 
477  if (gs_address_event_handler_id == 0)
478  {
479  gs_address_event_handler_id = qof_event_register_handler(listen_for_address_events, NULL);
480  }
481 
482  qof_event_gen (&vendor->inst, QOF_EVENT_CREATE, NULL);
483 
484  return vendor;
485 }
486 
487 void gncVendorDestroy (GncVendor *vendor)
488 {
489  if (!vendor) return;
490  qof_instance_set_destroying(vendor, TRUE);
491  gncVendorCommitEdit (vendor);
492 }
493 
494 static void gncVendorFree (GncVendor *vendor)
495 {
496  if (!vendor) return;
497 
498  qof_event_gen (&vendor->inst, QOF_EVENT_DESTROY, NULL);
499 
500  CACHE_REMOVE (vendor->id);
501  CACHE_REMOVE (vendor->name);
502  CACHE_REMOVE (vendor->notes);
503  gncAddressBeginEdit (vendor->addr);
504  gncAddressDestroy (vendor->addr);
505  g_list_free (vendor->jobs);
506 
507  if (vendor->terms)
508  gncBillTermDecRef (vendor->terms);
509  if (vendor->taxtable)
510  gncTaxTableDecRef (vendor->taxtable);
511 
512  /* qof_instance_release (&vendor->inst); */
513  g_object_unref (vendor);
514 }
515 
516 /* ============================================================== */
517 /* Set Functions */
518 
519 #define SET_STR(obj, member, str) { \
520  char * tmp; \
521  \
522  if (!g_strcmp0 (member, str)) return; \
523  gncVendorBeginEdit (obj); \
524  tmp = CACHE_INSERT (str); \
525  CACHE_REMOVE (member); \
526  member = tmp; \
527  }
528 
529 void gncVendorSetID (GncVendor *vendor, const char *id)
530 {
531  if (!vendor) return;
532  if (!id) return;
533  SET_STR(vendor, vendor->id, id);
534  mark_vendor (vendor);
535  gncVendorCommitEdit (vendor);
536 }
537 
538 void gncVendorSetName (GncVendor *vendor, const char *name)
539 {
540  if (!vendor) return;
541  if (!name) return;
542  SET_STR(vendor, vendor->name, name);
543  mark_vendor (vendor);
544  gncVendorCommitEdit (vendor);
545 }
546 
547 void gncVendorSetNotes (GncVendor *vendor, const char *notes)
548 {
549  if (!vendor) return;
550  if (!notes) return;
551  SET_STR(vendor, vendor->notes, notes);
552  mark_vendor (vendor);
553  gncVendorCommitEdit (vendor);
554 }
555 
556 void gncVendorSetTerms (GncVendor *vendor, GncBillTerm *terms)
557 {
558  if (!vendor) return;
559  if (vendor->terms == terms) return;
560 
561  gncVendorBeginEdit (vendor);
562  if (vendor->terms)
563  gncBillTermDecRef (vendor->terms);
564  vendor->terms = terms;
565  if (vendor->terms)
566  gncBillTermIncRef (vendor->terms);
567  mark_vendor (vendor);
568  gncVendorCommitEdit (vendor);
569 }
570 
571 void gncVendorSetTaxIncluded (GncVendor *vendor, GncTaxIncluded taxincl)
572 {
573  if (!vendor) return;
574  if (taxincl == vendor->taxincluded) return;
575  gncVendorBeginEdit (vendor);
576  vendor->taxincluded = taxincl;
577  mark_vendor (vendor);
578  gncVendorCommitEdit (vendor);
579 }
580 
581 void gncVendorSetCurrency (GncVendor *vendor, gnc_commodity *currency)
582 {
583  if (!vendor || !currency) return;
584  if (vendor->currency &&
585  gnc_commodity_equal (vendor->currency, currency))
586  return;
587  gncVendorBeginEdit (vendor);
588  vendor->currency = currency;
589  mark_vendor (vendor);
590  gncVendorCommitEdit (vendor);
591 }
592 
593 void gncVendorSetActive (GncVendor *vendor, gboolean active)
594 {
595  if (!vendor) return;
596  if (active == vendor->active) return;
597  gncVendorBeginEdit (vendor);
598  vendor->active = active;
599  mark_vendor (vendor);
600  gncVendorCommitEdit (vendor);
601 }
602 
603 void gncVendorSetTaxTableOverride (GncVendor *vendor, gboolean override)
604 {
605  if (!vendor) return;
606  if (vendor->taxtable_override == override) return;
607  gncVendorBeginEdit (vendor);
608  vendor->taxtable_override = override;
609  mark_vendor (vendor);
610  gncVendorCommitEdit (vendor);
611 }
612 
613 void gncVendorSetTaxTable (GncVendor *vendor, GncTaxTable *table)
614 {
615  if (!vendor) return;
616  if (vendor->taxtable == table) return;
617  gncVendorBeginEdit (vendor);
618  if (vendor->taxtable)
619  gncTaxTableDecRef (vendor->taxtable);
620  if (table)
621  gncTaxTableIncRef (table);
622  vendor->taxtable = table;
623  mark_vendor (vendor);
624  gncVendorCommitEdit (vendor);
625 }
626 
627 static void
628 qofVendorSetAddr (GncVendor *vendor, QofInstance *addr_ent)
629 {
630  GncAddress *addr;
631 
632  if (!vendor || !addr_ent)
633  {
634  return;
635  }
636  addr = (GncAddress*)addr_ent;
637  if (addr == vendor->addr)
638  {
639  return;
640  }
641  if (vendor->addr != NULL)
642  {
643  gncAddressBeginEdit(vendor->addr);
644  gncAddressDestroy(vendor->addr);
645  }
646  gncVendorBeginEdit(vendor);
647  vendor->addr = addr;
648  gncVendorCommitEdit(vendor);
649 }
650 
651 static void
652 qofVendorSetTaxIncluded(GncVendor *vendor, const char* type_string)
653 {
654  GncTaxIncluded inc;
655 
656  if (!gncTaxIncludedStringToType(type_string, &inc))
657  {
658  return;
659  }
660  gncVendorBeginEdit(vendor);
661  vendor->taxincluded = inc;
662  gncVendorCommitEdit(vendor);
663 }
664 
665 /* ============================================================== */
666 /* Get Functions */
667 
668 const char * gncVendorGetID (const GncVendor *vendor)
669 {
670  if (!vendor) return NULL;
671  return vendor->id;
672 }
673 
674 const char * gncVendorGetName (const GncVendor *vendor)
675 {
676  if (!vendor) return NULL;
677  return vendor->name;
678 }
679 
680 GncAddress * gncVendorGetAddr (const GncVendor *vendor)
681 {
682  if (!vendor) return NULL;
683  return vendor->addr;
684 }
685 
686 const char * gncVendorGetNotes (const GncVendor *vendor)
687 {
688  if (!vendor) return NULL;
689  return vendor->notes;
690 }
691 
692 GncBillTerm * gncVendorGetTerms (const GncVendor *vendor)
693 {
694  if (!vendor) return 0;
695  return vendor->terms;
696 }
697 
698 GncTaxIncluded gncVendorGetTaxIncluded (const GncVendor *vendor)
699 {
700  if (!vendor) return GNC_TAXINCLUDED_USEGLOBAL;
701  return vendor->taxincluded;
702 }
703 
704 gnc_commodity * gncVendorGetCurrency (const GncVendor *vendor)
705 {
706  if (!vendor) return NULL;
707  return vendor->currency;
708 }
709 
710 gboolean gncVendorGetActive (const GncVendor *vendor)
711 {
712  if (!vendor) return FALSE;
713  return vendor->active;
714 }
715 
716 gboolean gncVendorGetTaxTableOverride (const GncVendor *vendor)
717 {
718  if (!vendor) return FALSE;
719  return vendor->taxtable_override;
720 }
721 
722 GncTaxTable* gncVendorGetTaxTable (const GncVendor *vendor)
723 {
724  if (!vendor) return NULL;
725  return vendor->taxtable;
726 }
727 
728 static const char*
729 qofVendorGetTaxIncluded(const GncVendor *vendor)
730 {
731  return gncTaxIncludedTypeToString(vendor->taxincluded);
732 }
733 
734 /* Note that JobList changes do not affect the "dirtiness" of the vendor */
735 void gncVendorAddJob (GncVendor *vendor, GncJob *job)
736 {
737  if (!vendor) return;
738  if (!job) return;
739 
740  if (g_list_index(vendor->jobs, job) == -1)
741  vendor->jobs = g_list_insert_sorted (vendor->jobs, job,
742  (GCompareFunc)gncJobCompare);
743 
744  qof_event_gen (&vendor->inst, QOF_EVENT_MODIFY, NULL);
745 }
746 
747 void gncVendorRemoveJob (GncVendor *vendor, GncJob *job)
748 {
749  GList *node;
750 
751  if (!vendor) return;
752  if (!job) return;
753 
754  node = g_list_find (vendor->jobs, job);
755  if (!node)
756  {
757  /* PERR ("split not in account"); */
758  }
759  else
760  {
761  vendor->jobs = g_list_remove_link (vendor->jobs, node);
762  g_list_free_1 (node);
763  }
764 
765  qof_event_gen (&vendor->inst, QOF_EVENT_MODIFY, NULL);
766 }
767 
768 void gncVendorBeginEdit (GncVendor *vendor)
769 {
770  qof_begin_edit(&vendor->inst);
771 }
772 
773 static void gncVendorOnError (QofInstance *vendor, QofBackendError errcode)
774 {
775  PERR("Vendor QofBackend Failure: %d", errcode);
776  gnc_engine_signal_commit_error( errcode );
777 }
778 
779 static void gncVendorOnDone (QofInstance *inst)
780 {
781  GncVendor *vendor = (GncVendor *) inst;
782  gncAddressClearDirty (vendor->addr);
783 }
784 
785 static void vendor_free (QofInstance *inst)
786 {
787  GncVendor *vendor = (GncVendor *) inst;
788  gncVendorFree (vendor);
789 }
790 
791 void gncVendorCommitEdit (GncVendor *vendor)
792 {
793  if (!qof_commit_edit (QOF_INSTANCE(vendor))) return;
794  qof_commit_edit_part2 (&vendor->inst, gncVendorOnError,
795  gncVendorOnDone, vendor_free);
796 }
797 
798 /* ============================================================== */
799 /* Other functions */
800 
801 int gncVendorCompare (const GncVendor *a, const GncVendor *b)
802 {
803  if (!a && !b) return 0;
804  if (!a && b) return 1;
805  if (a && !b) return -1;
806 
807  return(strcmp(a->name, b->name));
808 }
809 
810 gboolean gncVendorEqual(const GncVendor *a, const GncVendor *b)
811 {
812  if (a == NULL && b == NULL) return TRUE;
813  if (a == NULL || b == NULL) return FALSE;
814 
815  g_return_val_if_fail(GNC_IS_VENDOR(a), FALSE);
816  g_return_val_if_fail(GNC_IS_VENDOR(b), FALSE);
817 
818  if (g_strcmp0(a->id, b->id) != 0)
819  {
820  PWARN("IDs differ: %s vs %s", a->id, b->id);
821  return FALSE;
822  }
823 
824  if (g_strcmp0(a->name, b->name) != 0)
825  {
826  PWARN("Names differ: %s vs %s", a->name, b->name);
827  return FALSE;
828  }
829 
830  if (g_strcmp0(a->notes, b->notes) != 0)
831  {
832  PWARN("Notes differ");
833  return FALSE;
834  }
835 
836  if (!gncBillTermEqual(a->terms, b->terms))
837  {
838  PWARN("BillTerms differ");
839  return FALSE;
840  }
841 
842  if (!gncAddressEqual(a->addr, b->addr))
843  {
844  PWARN("Addresses differ");
845  return FALSE;
846  }
847 
848  if (!gnc_commodity_equal(a->currency, b->currency))
849  {
850  PWARN("Currencies differ");
851  return FALSE;
852  }
853 
854  if (!gncTaxTableEqual(a->taxtable, b->taxtable))
855  {
856  PWARN("Tax tables differ");
857  return FALSE;
858  }
859 
860  if (a->taxtable_override != b->taxtable_override)
861  {
862  PWARN("Tax table override flags differ");
863  return FALSE;
864  }
865 
866  if (a->taxincluded != b->taxincluded)
867  {
868  PWARN("Tax included flags differ");
869  return FALSE;
870  }
871 
872  if (a->active != b->active)
873  {
874  PWARN("Active flags differ");
875  return FALSE;
876  }
877 
878 // GList * jobs;
879  return TRUE;
880 }
881 
882 gboolean
883 gncVendorIsDirty (const GncVendor *vendor)
884 {
885  if (!vendor) return FALSE;
886  return (qof_instance_get_dirty_flag(vendor)
887  || gncAddressIsDirty (vendor->addr));
888 }
889 
899 static void
900 listen_for_address_events(QofInstance *entity, QofEventId event_type,
901  gpointer user_data, gpointer event_data)
902 {
903  GncVendor* v;
904 
905  if ((event_type & QOF_EVENT_MODIFY) == 0)
906  {
907  return;
908  }
909  if (!GNC_IS_ADDRESS(entity))
910  {
911  return;
912  }
913  if (!GNC_IS_VENDOR(event_data))
914  {
915  return;
916  }
917  v = GNC_VENDOR(event_data);
918  gncVendorBeginEdit(v);
919  mark_vendor(v);
920  gncVendorCommitEdit(v);
921 }
922 /* ============================================================== */
923 /* Package-Private functions */
924 
925 static const char * _gncVendorPrintable (gpointer item)
926 {
927  GncVendor *v = item;
928  if (!item) return NULL;
929  return v->name;
930 }
931 
932 static void
933 destroy_vendor_on_book_close(QofInstance *ent, gpointer data)
934 {
935  GncVendor* v = GNC_VENDOR(ent);
936 
937  gncVendorBeginEdit(v);
938  gncVendorDestroy(v);
939 }
940 
945 static void
946 gnc_vendor_book_end(QofBook* book)
947 {
948  QofCollection *col;
949 
950  col = qof_book_get_collection(book, GNC_ID_VENDOR);
951  qof_collection_foreach(col, destroy_vendor_on_book_close, NULL);
952 }
953 
954 static QofObject gncVendorDesc =
955 {
956  DI(.interface_version = ) QOF_OBJECT_VERSION,
957  DI(.e_type = ) _GNC_MOD_NAME,
958  DI(.type_label = ) "Vendor",
959  DI(.create = ) (gpointer)gncVendorCreate,
960  DI(.book_begin = ) NULL,
961  DI(.book_end = ) gnc_vendor_book_end,
962  DI(.is_dirty = ) qof_collection_is_dirty,
963  DI(.mark_clean = ) qof_collection_mark_clean,
964  DI(.foreach = ) qof_collection_foreach,
965  DI(.printable = ) _gncVendorPrintable,
966  DI(.version_cmp = ) (int (*)(gpointer, gpointer)) qof_instance_version_cmp,
967 };
968 
969 gboolean gncVendorRegister (void)
970 {
971  static QofParam params[] =
972  {
973  { VENDOR_ID, QOF_TYPE_STRING, (QofAccessFunc)gncVendorGetID, (QofSetterFunc)gncVendorSetID },
974  { VENDOR_NAME, QOF_TYPE_STRING, (QofAccessFunc)gncVendorGetName, (QofSetterFunc)gncVendorSetName },
975  { VENDOR_ADDR, GNC_ID_ADDRESS, (QofAccessFunc)gncVendorGetAddr, (QofSetterFunc)qofVendorSetAddr },
976  { VENDOR_NOTES, QOF_TYPE_STRING, (QofAccessFunc)gncVendorGetNotes, (QofSetterFunc)gncVendorSetNotes },
977  { VENDOR_TERMS, GNC_ID_BILLTERM, (QofAccessFunc)gncVendorGetTerms, (QofSetterFunc)gncVendorSetTerms },
978  {
979  VENDOR_TAX_OVERRIDE, QOF_TYPE_BOOLEAN, (QofAccessFunc)gncVendorGetTaxTableOverride,
980  (QofSetterFunc)gncVendorSetTaxTableOverride
981  },
982  {
983  VENDOR_TAX_TABLE, GNC_ID_TAXTABLE, (QofAccessFunc)gncVendorGetTaxTable,
984  (QofSetterFunc)gncVendorSetTaxTable
985  },
986  {
987  VENDOR_TAX_INC, QOF_TYPE_STRING, (QofAccessFunc)qofVendorGetTaxIncluded,
988  (QofSetterFunc)qofVendorSetTaxIncluded
989  },
990  { QOF_PARAM_BOOK, QOF_ID_BOOK, (QofAccessFunc)qof_instance_get_book, NULL },
991  { QOF_PARAM_GUID, QOF_TYPE_GUID, (QofAccessFunc)qof_instance_get_guid, NULL },
992  { QOF_PARAM_ACTIVE, QOF_TYPE_BOOLEAN, (QofAccessFunc)gncVendorGetActive, NULL },
993  { NULL },
994  };
995 
996  if (!qof_choice_add_class(GNC_ID_INVOICE, GNC_ID_VENDOR, INVOICE_OWNER))
997  {
998  return FALSE;
999  }
1000  if (!qof_choice_add_class(GNC_ID_JOB, GNC_ID_VENDOR, JOB_OWNER))
1001  {
1002  return FALSE;
1003  }
1004 
1005  qof_class_register (_GNC_MOD_NAME, (QofSortFunc)gncVendorCompare, params);
1006 
1007  return qof_object_register (&gncVendorDesc);
1008 }
1009 
1010 gchar *gncVendorNextID (QofBook *book)
1011 {
1012  return qof_book_increment_and_format_counter (book, _GNC_MOD_NAME);
1013 }
int qof_instance_version_cmp(const QofInstance *left, const QofInstance *right)
const GncGUID * qof_instance_get_guid(gconstpointer)
QofBook * qof_instance_get_book(gconstpointer)
gboolean qof_collection_is_dirty(const QofCollection *col)
GncTaxIncluded
Definition: gncTaxTable.h:100
QofBackendError
The errors that can be reported to the GUI & other front-end users.
Definition: qofbackend.h:59
gchar * qof_book_increment_and_format_counter(QofBook *book, const char *counter_name)
GList * qof_instance_get_referring_object_list_from_collection(const QofCollection *coll, const QofInstance *ref)
void qof_class_register(QofIdTypeConst obj_name, QofSortFunc default_sort_fcn, const QofParam *params)
gboolean gnc_commodity_equal(const gnc_commodity *a, const gnc_commodity *b)
QofCollection * qof_instance_get_collection(gconstpointer inst)
gpointer(* QofAccessFunc)(gpointer object, const QofParam *param)
Definition: qofclass.h:177
#define QOF_OBJECT_VERSION
Definition: qofobject.h:64
gboolean qof_commit_edit(QofInstance *inst)
#define PERR(format, args...)
Definition: qoflog.h:237
#define QOF_PARAM_BOOK
Definition: qofquery.h:109
void qof_collection_foreach(const QofCollection *, QofInstanceForeachCB, gpointer user_data)
gboolean gncBillTermEqual(const GncBillTerm *a, const GncBillTerm *b)
Definition: gncBillTerm.c:647
gint qof_event_register_handler(QofEventHandler handler, gpointer handler_data)
Register a handler for events.
#define PWARN(format, args...)
Definition: qoflog.h:243
void qof_instance_init_data(QofInstance *, QofIdType, QofBook *)
gboolean qof_begin_edit(QofInstance *inst)
gint QofEventId
Definition: qofevent.h:45
Definition: gncJob.c:41
gboolean qof_instance_get_dirty_flag(gconstpointer ptr)
int(* QofSortFunc)(gconstpointer, gconstpointer)
Definition: qofclass.h:222
gboolean qof_commit_edit_part2(QofInstance *inst, void(*on_error)(QofInstance *, QofBackendError), void(*on_done)(QofInstance *), void(*on_free)(QofInstance *))
void qof_collection_mark_clean(QofCollection *)
int gncVendorCompare(const GncVendor *a, const GncVendor *b)
Definition: gncVendor.c:801
gboolean gncAddressEqual(const GncAddress *a, const GncAddress *b)
Deeply compare two addresses.
Definition: gncAddress.c:579
Business Invoice Interface.
gboolean qof_choice_add_class(const char *choice, char *add, char *param_name)
Add the choices for this parameter to the object.
gboolean gncVendorEqual(const GncVendor *a, const GncVendor *b)
Definition: gncVendor.c:810
void(* QofSetterFunc)(gpointer, gpointer)
Definition: qofclass.h:184
Vendor Interface.
QofCollection * qof_book_get_collection(const QofBook *, QofIdType)
gboolean qof_object_register(const QofObject *object)
void qof_event_gen(QofInstance *entity, QofEventId event_type, gpointer event_data)
Invoke all registered event handlers using the given arguments.
Commodity handling public routines.
const gchar * QofLogModule
Definition: qofid.h:89