GnuCash  4.11-145-g15ce9be79a+
gncInvoice.c
1 /********************************************************************\
2  * gncInvoice.c -- the Core Business Invoice *
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 gnu@gnu.org *
20  * *
21 \********************************************************************/
22 
23 /*
24  * Copyright (C) 2001,2002,2006 Derek Atkins
25  * Copyright (C) 2003 Linas Vepstas <linas@linas.org>
26  * Copyright (c) 2005 Neil Williams <linux@codehelp.co.uk>
27  * Author: Derek Atkins <warlord@MIT.EDU>
28  */
29 
30 #include <config.h>
31 
32 #include <stdint.h>
33 #include <inttypes.h>
34 #include <glib.h>
35 #include <glib/gi18n.h>
36 #include <qofinstance-p.h>
37 
38 #include "Transaction.h"
39 #include "Account.h"
40 #include "gncBillTermP.h"
41 #include "gncEntry.h"
42 #include "gncEntryP.h"
43 #include "gnc-features.h"
44 #include "gncJobP.h"
45 #include "gncInvoice.h"
46 #include "gncInvoiceP.h"
47 #include "gncOwnerP.h"
48 #include "engine-helpers.h"
49 
51 {
52  QofInstance inst;
53 
54  const char *id;
55  const char *notes;
56  gboolean active;
57 
58  const char *billing_id;
59  char *printname;
60  GncBillTerm *terms;
61  GList *entries;
62  GList *prices;
63  GncOwner owner;
64  GncOwner billto;
65  GncJob *job;
66  time64 date_opened;
67  time64 date_posted;
68 
69  char *doclink;
70 
71  gnc_numeric to_charge_amount;
72 
73  gnc_commodity *currency;
74 
75  Account *posted_acc;
76  Transaction *posted_txn;
77  GNCLot *posted_lot;
78 };
79 
81 {
82  QofInstanceClass parent_class;
83 };
84 
85 static QofLogModule log_module = GNC_MOD_BUSINESS;
86 
87 #define _GNC_MOD_NAME GNC_ID_INVOICE
88 
89 #define GNC_INVOICE_IS_CN "credit-note"
90 #define GNC_INVOICE_DOCLINK "assoc_uri" // this is the old name for the document link, kept for compatibility
91 
92 #define SET_STR(obj, member, str) { \
93  if (!g_strcmp0 (member, str)) return; \
94  gncInvoiceBeginEdit (obj); \
95  CACHE_REPLACE (member, str); \
96  }
97 
98 static void mark_invoice (GncInvoice *invoice);
99 static void
100 mark_invoice (GncInvoice *invoice)
101 {
102  qof_instance_set_dirty (&invoice->inst);
103  qof_event_gen (&invoice->inst, QOF_EVENT_MODIFY, NULL);
104 }
105 
106 QofBook * gncInvoiceGetBook (GncInvoice *x)
107 {
108  return qof_instance_get_book (QOF_INSTANCE(x));
109 }
110 
111 /* ================================================================== */
112 
113 enum
114 {
115  PROP_0,
116 // PROP_ID, /* Table */
117 // PROP_DATE_OPENED, /* Table */
118 // PROP_DATE_POSTED, /* Table */
119  PROP_NOTES, /* Table */
120 // PROP_ACTIVE, /* Table */
121 // PROP_CURRENCY, /* Table */
122 // PROP_OWNER_TYPE, /* Table */
123 // PROP_OWNER, /* Table */
124 // PROP_TERMS, /* Table */
125 // PROP_BILLING_ID, /* Table */
126 // PROP_POST_TXN, /* Table */
127 // PROP_POST_LOT, /* Table */
128 // PROP_POST_ACCOUNT, /* Table */
129 // PROP_BILLTO_TYPE, /* Table */
130 // PROP_BILLTO, /* Table */
131 // PROP_CHARGE_AMOUNT, /* Table, (numeric) */
132 };
133 
134 /* GObject Initialization */
135 G_DEFINE_TYPE(GncInvoice, gnc_invoice, QOF_TYPE_INSTANCE);
136 
137 static void
138 gnc_invoice_init (GncInvoice* inv)
139 {
140  inv->date_posted = INT64_MAX;
141  inv->date_opened = INT64_MAX;
142 }
143 
144 static void
145 gnc_invoice_dispose (GObject *invp)
146 {
147  G_OBJECT_CLASS(gnc_invoice_parent_class)->dispose(invp);
148 }
149 
150 static void
151 gnc_invoice_finalize (GObject* invp)
152 {
153  G_OBJECT_CLASS(gnc_invoice_parent_class)->finalize(invp);
154 }
155 
156 static void
157 gnc_invoice_get_property (GObject *object,
158  guint prop_id,
159  GValue *value,
160  GParamSpec *pspec)
161 {
162  GncInvoice *inv;
163 
164  g_return_if_fail (GNC_IS_INVOICE(object));
165 
166  inv = GNC_INVOICE(object);
167  switch (prop_id)
168  {
169  case PROP_NOTES:
170  g_value_set_string (value, inv->notes);
171  break;
172  default:
173  G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
174  break;
175  }
176 }
177 
178 static void
179 gnc_invoice_set_property (GObject *object,
180  guint prop_id,
181  const GValue *value,
182  GParamSpec *pspec)
183 {
184  GncInvoice *inv;
185 
186  g_return_if_fail (GNC_IS_INVOICE(object));
187 
188  inv = GNC_INVOICE(object);
189  g_assert (qof_instance_get_editlevel (inv));
190 
191  switch (prop_id)
192  {
193  case PROP_NOTES:
194  gncInvoiceSetNotes (inv, g_value_get_string (value));
195  break;
196  default:
197  G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
198  break;
199  }
200 }
201 
203 static gchar*
204 impl_get_display_name (const QofInstance* inst)
205 {
206  GncInvoice* inv;
207  QofInstance* owner;
208  gchar* s;
209 
210  g_return_val_if_fail (inst != NULL, FALSE);
211  g_return_val_if_fail (GNC_IS_INVOICE(inst), FALSE);
212 
213  inv = GNC_INVOICE(inst);
214  owner = qofOwnerGetOwner (&inv->owner);
215  if (owner != NULL)
216  {
217  gchar* display_name;
218 
219  display_name = qof_instance_get_display_name (owner);
220  s = g_strdup_printf ("Invoice %s (%s)", inv->id, display_name);
221  g_free (display_name);
222  }
223  else
224  {
225  s = g_strdup_printf ("Invoice %s", inv->id);
226  }
227 
228  return s;
229 }
230 
232 static gboolean
233 impl_refers_to_object (const QofInstance* inst, const QofInstance* ref)
234 {
235  GncInvoice* inv;
236 
237  g_return_val_if_fail (inst != NULL, FALSE);
238  g_return_val_if_fail (GNC_IS_INVOICE(inst), FALSE);
239 
240  inv = GNC_INVOICE(inst);
241 
242  if (GNC_IS_BILLTERM(ref))
243  {
244  return (inv->terms == GNC_BILLTERM(ref));
245  }
246  else if (GNC_IS_JOB(ref))
247  {
248  return (inv->job == GNC_JOB(ref));
249  }
250  else if (GNC_IS_COMMODITY(ref))
251  {
252  return (inv->currency == GNC_COMMODITY(ref));
253  }
254  else if (GNC_IS_ACCOUNT(ref))
255  {
256  return (inv->posted_acc == GNC_ACCOUNT(ref));
257  }
258  else if (GNC_IS_TRANSACTION(ref))
259  {
260  return (inv->posted_txn == GNC_TRANSACTION(ref));
261  }
262  else if (GNC_IS_LOT(ref))
263  {
264  return (inv->posted_lot == GNC_LOT(ref));
265  }
266 
267  return FALSE;
268 }
269 
276 static GList*
277 impl_get_typed_referring_object_list (const QofInstance* inst, const QofInstance* ref)
278 {
279  if (!GNC_IS_BILLTERM(ref) && !GNC_IS_JOB(ref) && !GNC_IS_COMMODITY(ref) && !GNC_IS_ACCOUNT(ref)
280  && !GNC_IS_TRANSACTION(ref) && !GNC_IS_LOT(ref))
281  {
282  return NULL;
283  }
284 
286 }
287 
288 static const char*
289 is_unset = "unset";
290 
291 static void
292 gnc_invoice_class_init (GncInvoiceClass *klass)
293 {
294  GObjectClass *gobject_class = G_OBJECT_CLASS(klass);
295  QofInstanceClass* qof_class = QOF_INSTANCE_CLASS(klass);
296 
297  gobject_class->dispose = gnc_invoice_dispose;
298  gobject_class->finalize = gnc_invoice_finalize;
299  gobject_class->set_property = gnc_invoice_set_property;
300  gobject_class->get_property = gnc_invoice_get_property;
301 
302  qof_class->get_display_name = impl_get_display_name;
303  qof_class->refers_to_object = impl_refers_to_object;
304  qof_class->get_typed_referring_object_list = impl_get_typed_referring_object_list;
305 
306  g_object_class_install_property
307  (gobject_class,
308  PROP_NOTES,
309  g_param_spec_string ("notes",
310  "Invoice Notes",
311  "The invoice notes is an arbitrary string "
312  "assigned by the user to provide notes regarding "
313  "this invoice.",
314  NULL,
315  G_PARAM_READWRITE));
316 }
317 
318 /* Create/Destroy Functions */
319 GncInvoice *gncInvoiceCreate (QofBook *book)
320 {
321  GncInvoice *invoice;
322 
323  if (!book) return NULL;
324 
325  invoice = g_object_new (GNC_TYPE_INVOICE, NULL);
326  qof_instance_init_data (&invoice->inst, _GNC_MOD_NAME, book);
327 
328  invoice->id = CACHE_INSERT ("");
329  invoice->notes = CACHE_INSERT ("");
330  invoice->billing_id = CACHE_INSERT ("");
331 
332  invoice->billto.type = GNC_OWNER_CUSTOMER;
333  invoice->active = TRUE;
334 
335  invoice->to_charge_amount = gnc_numeric_zero ();
336  invoice->doclink = (char*) is_unset;
337 
338  qof_event_gen (&invoice->inst, QOF_EVENT_CREATE, NULL);
339 
340  return invoice;
341 }
342 
343 GncInvoice *gncInvoiceCopy (const GncInvoice *from)
344 {
345  GncInvoice *invoice;
346  QofBook* book;
347  GList *node;
348  GValue v = G_VALUE_INIT;
349 
350  g_assert (from);
351  book = qof_instance_get_book (from);
352  g_assert (book);
353 
354  invoice = g_object_new (GNC_TYPE_INVOICE, NULL);
355  qof_instance_init_data (&invoice->inst, _GNC_MOD_NAME, book);
356 
357  gncInvoiceBeginEdit (invoice);
358 
359  invoice->id = CACHE_INSERT (from->id);
360  invoice->notes = CACHE_INSERT (from->notes);
361  invoice->billing_id = CACHE_INSERT (from->billing_id);
362  invoice->active = from->active;
363 
364  qof_instance_get_kvp (QOF_INSTANCE (from), &v, 1, GNC_INVOICE_IS_CN);
365  if (G_VALUE_HOLDS_INT64 (&v))
366  qof_instance_set_kvp (QOF_INSTANCE (invoice), &v, 1, GNC_INVOICE_IS_CN);
367  g_value_unset (&v);
368 
369  invoice->terms = from->terms;
370  gncBillTermIncRef (invoice->terms);
371 
372  gncOwnerCopy (&from->billto, &invoice->billto);
373  gncOwnerCopy (&from->owner, &invoice->owner);
374  invoice->job = from->job; // FIXME: Need IncRef or similar here?!?
375 
376  invoice->to_charge_amount = from->to_charge_amount;
377  invoice->date_opened = from->date_opened;
378 
379  // Oops. Do not forget to copy the pointer to the correct currency here.
380  invoice->currency = from->currency;
381 
382  invoice->doclink = from->doclink;
383 
384  // Copy all invoice->entries
385  for (node = from->entries; node; node = node->next)
386  {
387  GncEntry *from_entry = node->data;
388  GncEntry *to_entry = gncEntryCreate (book);
389  gncEntryCopy (from_entry, to_entry, FALSE);
390 
391  switch (gncInvoiceGetOwnerType (invoice))
392  {
393  case GNC_OWNER_VENDOR:
394  case GNC_OWNER_EMPLOYEE:
395  // this is a vendor bill, or an expense voucher
396  gncBillAddEntry (invoice, to_entry);
397  break;
398  case GNC_OWNER_CUSTOMER:
399  default:
400  // this is an invoice
401  gncInvoiceAddEntry (invoice, to_entry);
402  break;
403  }
404  }
405 
406  // FIXME: The prices are not (yet) copied; is this a problem?
407 
408  // Posted-date and the posted Txn is intentionally not copied; the
409  // copy isn't "posted" but needs to be posted by the user.
410  mark_invoice (invoice);
411  gncInvoiceCommitEdit (invoice);
412 
413  return invoice;
414 }
415 
416 void gncInvoiceDestroy (GncInvoice *invoice)
417 {
418  if (!invoice) return;
419  qof_instance_set_destroying (invoice, TRUE);
420  gncInvoiceCommitEdit (invoice);
421 }
422 
423 static void gncInvoiceFree (GncInvoice *invoice)
424 {
425  if (!invoice) return;
426 
427  qof_event_gen (&invoice->inst, QOF_EVENT_DESTROY, NULL);
428 
429  CACHE_REMOVE (invoice->id);
430  CACHE_REMOVE (invoice->notes);
431  CACHE_REMOVE (invoice->billing_id);
432  g_list_free (invoice->entries);
433  g_list_free (invoice->prices);
434 
435  if (invoice->printname) g_free (invoice->printname);
436 
437  if (invoice->terms)
438  gncBillTermDecRef (invoice->terms);
439 
440  if (invoice->doclink != is_unset)
441  g_free (invoice->doclink);
442 
443  /* qof_instance_release (&invoice->inst); */
444  g_object_unref (invoice);
445 }
446 
447 /* ================================================================== */
448 /* Set Functions */
449 
450 void gncInvoiceSetID (GncInvoice *invoice, const char *id)
451 {
452  if (!invoice || !id) return;
453  SET_STR (invoice, invoice->id, id);
454  mark_invoice (invoice);
455  gncInvoiceCommitEdit (invoice);
456 }
457 
458 void gncInvoiceSetOwner (GncInvoice *invoice, GncOwner *owner)
459 {
460  if (!invoice || !owner) return;
461  if (gncOwnerEqual (&invoice->owner, owner)) return;
462  gncInvoiceBeginEdit (invoice);
463  gncOwnerCopy (owner, &invoice->owner);
464  mark_invoice (invoice);
465  gncInvoiceCommitEdit (invoice);
466 }
467 
468 static void
469 qofInvoiceSetOwner (GncInvoice *invoice, QofInstance *ent)
470 {
471  if (!invoice || !ent)
472  {
473  return;
474  }
475  gncInvoiceBeginEdit (invoice);
476  qofOwnerSetEntity (&invoice->owner, ent);
477  mark_invoice (invoice);
478  gncInvoiceCommitEdit (invoice);
479 }
480 
481 static void
482 qofInvoiceSetBillTo (GncInvoice *invoice, QofInstance *ent)
483 {
484  if (!invoice || !ent)
485  {
486  return;
487  }
488  gncInvoiceBeginEdit (invoice);
489  qofOwnerSetEntity (&invoice->billto, ent);
490  mark_invoice (invoice);
491  gncInvoiceCommitEdit (invoice);
492 }
493 
494 void gncInvoiceSetDateOpenedGDate (GncInvoice *invoice, const GDate *date)
495 {
496  g_assert (date);
497  gncInvoiceSetDateOpened(invoice, time64CanonicalDayTime (gdate_to_time64 (*date)));
498 }
499 
500 void gncInvoiceSetDateOpened (GncInvoice *invoice, time64 date)
501 {
502  if (!invoice) return;
503  if (date == invoice->date_opened) return;
504  gncInvoiceBeginEdit (invoice);
505  invoice->date_opened = date;
506  mark_invoice (invoice);
507  gncInvoiceCommitEdit (invoice);
508 }
509 
510 void gncInvoiceSetDatePosted (GncInvoice *invoice, time64 date)
511 {
512  if (!invoice) return;
513  if (date == invoice->date_posted) return;
514  gncInvoiceBeginEdit (invoice);
515  invoice->date_posted = date;
516  mark_invoice (invoice);
517  gncInvoiceCommitEdit (invoice);
518 }
519 
520 void gncInvoiceSetTerms (GncInvoice *invoice, GncBillTerm *terms)
521 {
522  if (!invoice) return;
523  if (invoice->terms == terms) return;
524  gncInvoiceBeginEdit (invoice);
525  if (invoice->terms)
526  gncBillTermDecRef (invoice->terms);
527  invoice->terms = terms;
528  if (invoice->terms)
529  gncBillTermIncRef (invoice->terms);
530  mark_invoice (invoice);
531  gncInvoiceCommitEdit (invoice);
532 }
533 
534 void gncInvoiceSetBillingID (GncInvoice *invoice, const char *billing_id)
535 {
536  if (!invoice) return;
537  SET_STR (invoice, invoice->billing_id, billing_id);
538  mark_invoice (invoice);
539  gncInvoiceCommitEdit (invoice);
540 }
541 
542 void gncInvoiceSetNotes (GncInvoice *invoice, const char *notes)
543 {
544  if (!invoice || !notes) return;
545  SET_STR (invoice, invoice->notes, notes);
546  mark_invoice (invoice);
547  gncInvoiceCommitEdit (invoice);
548 }
549 
550 void gncInvoiceSetDocLink (GncInvoice *invoice, const char *doclink)
551 {
552  if (!invoice || !doclink) return;
553 
554  if (invoice->doclink != is_unset)
555  {
556  if (!g_strcmp0 (doclink, invoice->doclink))
557  return;
558 
559  g_free (invoice->doclink);
560  }
561 
562  gncInvoiceBeginEdit (invoice);
563 
564  if (doclink[0] == '\0')
565  {
566  invoice->doclink = NULL;
567  qof_instance_set_kvp (QOF_INSTANCE (invoice), NULL, 1, GNC_INVOICE_DOCLINK);
568  }
569  else
570  {
571  GValue v = G_VALUE_INIT;
572  g_value_init (&v, G_TYPE_STRING);
573  g_value_set_string (&v, doclink);
574  qof_instance_set_kvp (QOF_INSTANCE (invoice), &v, 1, GNC_INVOICE_DOCLINK);
575  invoice->doclink = g_strdup (doclink);
576  g_value_unset (&v);
577  }
578  qof_instance_set_dirty (QOF_INSTANCE(invoice));
579  gncInvoiceCommitEdit (invoice);
580 }
581 
582 void gncInvoiceSetActive (GncInvoice *invoice, gboolean active)
583 {
584  if (!invoice) return;
585  if (invoice->active == active) return;
586  gncInvoiceBeginEdit (invoice);
587  invoice->active = active;
588  mark_invoice (invoice);
589  gncInvoiceCommitEdit (invoice);
590 }
591 
592 void gncInvoiceSetIsCreditNote (GncInvoice *invoice, gboolean credit_note)
593 {
594  GValue v = G_VALUE_INIT;
595  if (!invoice) return;
596  gncInvoiceBeginEdit (invoice);
597  g_value_init (&v, G_TYPE_INT64);
598  g_value_set_int64 (&v, credit_note ? 1 : 0);
599  qof_instance_set_kvp (QOF_INSTANCE (invoice), &v, 1, GNC_INVOICE_IS_CN);
600  g_value_unset (&v);
601  mark_invoice (invoice);
602  gncInvoiceCommitEdit (invoice);
603 
604  /* If this is a credit note, set a feature flag for it in the book
605  * This will prevent older GnuCash versions that don't support
606  * credit notes to open this file. */
607  if (credit_note)
608  gnc_features_set_used (gncInvoiceGetBook (invoice), GNC_FEATURE_CREDIT_NOTES);
609 }
610 
611 void gncInvoiceSetCurrency (GncInvoice *invoice, gnc_commodity *currency)
612 {
613  if (!invoice || !currency) return;
614  if (invoice->currency &&
615  gnc_commodity_equal (invoice->currency, currency))
616  return;
617  gncInvoiceBeginEdit (invoice);
618  invoice->currency = currency;
619  mark_invoice (invoice);
620  gncInvoiceCommitEdit (invoice);
621 }
622 
623 void gncInvoiceSetBillTo (GncInvoice *invoice, GncOwner *billto)
624 {
625  if (!invoice || !billto) return;
626  if (gncOwnerEqual (&invoice->billto, billto)) return;
627 
628  gncInvoiceBeginEdit (invoice);
629  gncOwnerCopy (billto, &invoice->billto);
630  mark_invoice (invoice);
631  gncInvoiceCommitEdit (invoice);
632 }
633 
634 void gncInvoiceSetToChargeAmount (GncInvoice *invoice, gnc_numeric amount)
635 {
636  if (!invoice) return;
637  if (gnc_numeric_equal (invoice->to_charge_amount, amount)) return;
638  gncInvoiceBeginEdit (invoice);
639  invoice->to_charge_amount = amount;
640  mark_invoice (invoice);
641  gncInvoiceCommitEdit (invoice);
642 }
643 
644 void gncInvoiceSetPostedTxn (GncInvoice *invoice, Transaction *txn)
645 {
646  if (!invoice) return;
647  g_return_if_fail (invoice->posted_txn == NULL);
648 
649  gncInvoiceBeginEdit (invoice);
650  invoice->posted_txn = txn;
651  mark_invoice (invoice);
652  gncInvoiceCommitEdit (invoice);
653 }
654 
655 void gncInvoiceSetPostedLot (GncInvoice *invoice, GNCLot *lot)
656 {
657  if (!invoice) return;
658  g_return_if_fail (invoice->posted_lot == NULL);
659 
660  gncInvoiceBeginEdit (invoice);
661  invoice->posted_lot = lot;
662  mark_invoice (invoice);
663  gncInvoiceCommitEdit (invoice);
664 }
665 
666 void gncInvoiceSetPostedAcc (GncInvoice *invoice, Account *acc)
667 {
668  if (!invoice) return;
669  g_return_if_fail (invoice->posted_acc == NULL);
670 
671  gncInvoiceBeginEdit (invoice);
672  invoice->posted_acc = acc;
673  mark_invoice (invoice);
674  gncInvoiceCommitEdit (invoice);
675 }
676 
677 void gncInvoiceAddEntry (GncInvoice *invoice, GncEntry *entry)
678 {
679  GncInvoice *old;
680 
681  g_assert (invoice);
682  g_assert (entry);
683  if (!invoice || !entry) return;
684 
685  old = gncEntryGetInvoice (entry);
686  if (old == invoice) return; /* I already own this one */
687  if (old) gncInvoiceRemoveEntry (old, entry);
688 
689  gncInvoiceBeginEdit (invoice);
690  gncEntrySetInvoice (entry, invoice);
691  invoice->entries = g_list_insert_sorted (invoice->entries, entry,
692  (GCompareFunc)gncEntryCompare);
693  mark_invoice (invoice);
694  gncInvoiceCommitEdit (invoice);
695 }
696 
697 void gncInvoiceRemoveEntry (GncInvoice *invoice, GncEntry *entry)
698 {
699  if (!invoice || !entry) return;
700 
701  gncInvoiceBeginEdit (invoice);
702  gncEntrySetInvoice (entry, NULL);
703  invoice->entries = g_list_remove (invoice->entries, entry);
704  mark_invoice (invoice);
705  gncInvoiceCommitEdit (invoice);
706 }
707 
708 void gncInvoiceAddPrice (GncInvoice *invoice, GNCPrice *price)
709 {
710  GList *node;
711  gnc_commodity *commodity;
712 
713  if (!invoice || !price) return;
714 
715  /* Keep only one price per commodity per invoice
716  * So if a price was set previously remove it first */
717  node = g_list_first (invoice->prices);
718  commodity = gnc_price_get_commodity (price);
719  while (node != NULL)
720  {
721  GNCPrice *curr = (GNCPrice*)node->data;
722  if (gnc_commodity_equal (commodity, gnc_price_get_commodity (curr)))
723  break;
724  node = g_list_next (node);
725  }
726 
727  gncInvoiceBeginEdit (invoice);
728  if (node)
729  invoice->prices = g_list_delete_link (invoice->prices, node);
730  invoice->prices = g_list_prepend (invoice->prices, price);
731  mark_invoice (invoice);
732  gncInvoiceCommitEdit (invoice);
733 }
734 
735 void gncBillAddEntry (GncInvoice *bill, GncEntry *entry)
736 {
737  GncInvoice *old;
738 
739  g_assert (bill);
740  g_assert (entry);
741  if (!bill || !entry) return;
742 
743  old = gncEntryGetBill (entry);
744  if (old == bill) return; /* I already own this one */
745  if (old) gncBillRemoveEntry (old, entry);
746 
747  gncInvoiceBeginEdit (bill);
748  gncEntrySetBill (entry, bill);
749  bill->entries = g_list_insert_sorted (bill->entries, entry,
750  (GCompareFunc)gncEntryCompare);
751  mark_invoice (bill);
752  gncInvoiceCommitEdit (bill);
753 }
754 
755 void gncBillRemoveEntry (GncInvoice *bill, GncEntry *entry)
756 {
757  if (!bill || !entry) return;
758 
759  gncInvoiceBeginEdit (bill);
760  gncEntrySetBill (entry, NULL);
761  bill->entries = g_list_remove (bill->entries, entry);
762  mark_invoice (bill);
763  gncInvoiceCommitEdit (bill);
764 }
765 
766 void gncInvoiceSortEntries (GncInvoice *invoice)
767 {
768  if (!invoice) return;
769  invoice->entries = g_list_sort (invoice->entries,
770  (GCompareFunc)gncEntryCompare);
771  gncInvoiceBeginEdit (invoice);
772  mark_invoice (invoice);
773  gncInvoiceCommitEdit (invoice);
774 }
775 
776 void gncInvoiceRemoveEntries (GncInvoice *invoice)
777 {
778  GList *node;
779 
780  if (!invoice) return;
781 
782  for (node = invoice->entries; node; node = node->next)
783  {
784  GncEntry *entry = node->data;
785 
786  switch (gncInvoiceGetOwnerType (invoice))
787  {
788  case GNC_OWNER_VENDOR:
789  case GNC_OWNER_EMPLOYEE:
790  // this is a vendor bill, or an expense voucher
791  gncBillRemoveEntry (invoice, entry);
792  break;
793  case GNC_OWNER_CUSTOMER:
794  default:
795  // this is an invoice
796  gncInvoiceRemoveEntry (invoice, entry);
797  break;
798  }
799 
800  /* If the entry is no longer referenced by any document,
801  * remove it.
802  */
803  if (!(gncEntryGetInvoice (entry) ||
804  gncEntryGetBill (entry) ||
805  gncEntryGetOrder (entry)))
806  {
807  gncEntryBeginEdit (entry);
808  gncEntryDestroy (entry);
809  }
810  }
811 }
812 
813 /* ================================================================== */
814 /* Get Functions */
815 
816 const char * gncInvoiceGetID (const GncInvoice *invoice)
817 {
818  if (!invoice) return NULL;
819  return invoice->id;
820 }
821 
822 const GncOwner * gncInvoiceGetOwner (const GncInvoice *invoice)
823 {
824  if (!invoice) return NULL;
825  return &invoice->owner;
826 }
827 
828 static QofInstance * qofInvoiceGetOwner (GncInvoice *invoice)
829 {
830  GncOwner *owner;
831 
832  if (!invoice)
833  {
834  return NULL;
835  }
836  owner = &invoice->owner;
837  return QOF_INSTANCE(owner);
838 }
839 
840 static QofInstance * qofInvoiceGetBillTo (GncInvoice *invoice)
841 {
842  GncOwner *billto;
843 
844  if (!invoice)
845  {
846  return NULL;
847  }
848  billto = &invoice->billto;
849  return QOF_INSTANCE(billto);
850 }
851 
852 time64 gncInvoiceGetDateOpened (const GncInvoice *invoice)
853 {
854  if (!invoice) return INT64_MAX;
855  return invoice->date_opened;
856 }
857 
858 time64 gncInvoiceGetDatePosted (const GncInvoice *invoice)
859 {
860  if (!invoice) return INT64_MAX;
861  return invoice->date_posted;
862 }
863 
864 time64 gncInvoiceGetDateDue (const GncInvoice *invoice)
865 {
866  Transaction *txn;
867  if (!invoice) return INT64_MAX;
868  txn = gncInvoiceGetPostedTxn (invoice);
869  if (!txn) return INT64_MAX;
870  return xaccTransRetDateDue (txn);
871 }
872 
873 GncBillTerm * gncInvoiceGetTerms (const GncInvoice *invoice)
874 {
875  if (!invoice) return NULL;
876  return invoice->terms;
877 }
878 
879 const char * gncInvoiceGetBillingID (const GncInvoice *invoice)
880 {
881  if (!invoice) return NULL;
882  return invoice->billing_id;
883 }
884 
885 const char * gncInvoiceGetNotes (const GncInvoice *invoice)
886 {
887  if (!invoice) return NULL;
888  return invoice->notes;
889 }
890 
891 const char * gncInvoiceGetDocLink (const GncInvoice *invoice)
892 {
893  if (!invoice) return NULL;
894  if (invoice->doclink == is_unset)
895  {
896  GValue v = G_VALUE_INIT;
897  GncInvoice *inv = (GncInvoice*) invoice;
898  qof_instance_get_kvp (QOF_INSTANCE(invoice), &v, 1, GNC_INVOICE_DOCLINK);
899  inv->doclink = G_VALUE_HOLDS_STRING(&v) ? g_value_dup_string (&v) : NULL;
900  g_value_unset (&v);
901  }
902  return invoice->doclink;
903 }
904 
905 GncOwnerType gncInvoiceGetOwnerType (const GncInvoice *invoice)
906 {
907  const GncOwner *owner;
908  g_return_val_if_fail (invoice, GNC_OWNER_NONE);
909 
910  owner = gncOwnerGetEndOwner (gncInvoiceGetOwner (invoice));
911  return (gncOwnerGetType (owner));
912 }
913 
914 static gnc_numeric gncInvoiceSumTaxesInternal (AccountValueList *taxes)
915 {
916  gnc_numeric tt = gnc_numeric_zero ();
917 
918  if (taxes)
919  {
920  GList *node;
921  // Note we can use GNC_DENOM_AUTO below for rounding because
922  // the values passed to this function should already have been rounded
923  // to the desired denom and addition will just preserve it in that case.
924  for (node = taxes; node; node=node->next)
925  {
926  GncAccountValue *acc_val = node->data;
927  tt = gnc_numeric_add (tt, acc_val->value, GNC_DENOM_AUTO,
929  }
930  }
931  return tt;
932 }
933 
934 static gnc_numeric gncInvoiceGetNetAndTaxesInternal (GncInvoice *invoice, gboolean use_value,
935  AccountValueList **taxes,
936  gboolean use_payment_type,
937  GncEntryPaymentType type)
938 {
939  GList *node;
940  gnc_numeric net_total = gnc_numeric_zero ();
941  gboolean is_cust_doc, is_cn;
942  AccountValueList *tv_list = NULL;
943  int denom = gnc_commodity_get_fraction (gncInvoiceGetCurrency (invoice));
944 
945  g_return_val_if_fail (invoice, net_total);
946 
947  ENTER ("");
948  /* Is the current document an invoice/credit note related to a customer or a vendor/employee ?
949  * The GncEntry code needs to know to return the proper entry amounts
950  */
951  is_cust_doc = (gncInvoiceGetOwnerType (invoice) == GNC_OWNER_CUSTOMER);
952  is_cn = gncInvoiceGetIsCreditNote (invoice);
953 
954 
955  for (node = gncInvoiceGetEntries (invoice); node; node = node->next)
956  {
957  GncEntry *entry = node->data;
958  gnc_numeric value, tax;
959 
960  if (use_payment_type && gncEntryGetBillPayment (entry) != type)
961  continue;
962 
963  if (use_value)
964  {
965  // Always use rounded net values to prevent creating imbalanced transactions on posting
966  // https://bugs.gnucash.org/show_bug.cgi?id=628903
967  value = gncEntryGetDocValue (entry, TRUE, is_cust_doc, is_cn);
968  if (gnc_numeric_check (value) == GNC_ERROR_OK)
969  net_total = gnc_numeric_add (net_total, value, GNC_DENOM_AUTO, GNC_HOW_DENOM_LCD);
970  else
971  PWARN ("bad value in our entry");
972  }
973 
974  if (taxes)
975  {
976  AccountValueList *entrytaxes = gncEntryGetDocTaxValues (entry, is_cust_doc, is_cn);
977  tv_list = gncAccountValueAddList (tv_list, entrytaxes);
978  gncAccountValueDestroy (entrytaxes);
979  }
980  }
981 
982  if (taxes)
983  {
984  GList *node;
985  // Round tax totals (accumulated per tax account) to prevent creating imbalanced transactions on posting
986  // which could otherwise happen when using a tax table with multiple tax rates
987  for (node = tv_list; node; node=node->next)
988  {
989  GncAccountValue *acc_val = node->data;
990  acc_val->value = gnc_numeric_convert (acc_val->value,
992  }
993  *taxes = tv_list;
994  }
995 
996  LEAVE ("%" PRId64 "/%" PRId64, net_total.num, net_total.denom);
997  return net_total;
998 }
999 
1000 static gnc_numeric gncInvoiceGetTotalInternal (GncInvoice *invoice, gboolean use_value,
1001  gboolean use_tax,
1002  gboolean use_payment_type, GncEntryPaymentType type)
1003 {
1004  AccountValueList *taxes;
1005  gnc_numeric total;
1006  int denom;
1007 
1008  if (!invoice) return gnc_numeric_zero ();
1009 
1010  ENTER ("");
1011  total = gncInvoiceGetNetAndTaxesInternal (invoice, use_value, use_tax? &taxes : NULL, use_payment_type, type);
1012 
1013  if (use_tax)
1014  {
1015  // Note we can use GNC_DENOM_AUTO below for rounding because
1016  // the values passed to this function should already have been rounded
1017  // to the desired denom and addition will just preserve it in that case.
1018  total = gnc_numeric_add (total, gncInvoiceSumTaxesInternal (taxes),
1020  gncAccountValueDestroy (taxes);
1021  }
1022  LEAVE ("%" PRId64 "/%" PRId64, total.num, total.denom);
1023  return total;
1024 }
1025 
1026 gnc_numeric gncInvoiceGetTotal (GncInvoice *invoice)
1027 {
1028  if (!invoice) return gnc_numeric_zero ();
1029  return gncInvoiceGetTotalInternal (invoice, TRUE, TRUE, FALSE, 0);
1030 }
1031 
1032 gnc_numeric gncInvoiceGetTotalSubtotal (GncInvoice *invoice)
1033 {
1034  if (!invoice) return gnc_numeric_zero ();
1035  return gncInvoiceGetTotalInternal (invoice, TRUE, FALSE, FALSE, 0);
1036 }
1037 
1038 gnc_numeric gncInvoiceGetTotalTax (GncInvoice *invoice)
1039 {
1040  if (!invoice) return gnc_numeric_zero ();
1041  return gncInvoiceGetTotalInternal (invoice, FALSE, TRUE, FALSE, 0);
1042 }
1043 
1044 gnc_numeric gncInvoiceGetTotalOf (GncInvoice *invoice, GncEntryPaymentType type)
1045 {
1046  if (!invoice) return gnc_numeric_zero ();
1047  return gncInvoiceGetTotalInternal (invoice, TRUE, TRUE, TRUE, type);
1048 }
1049 
1050 AccountValueList *gncInvoiceGetTotalTaxList (GncInvoice *invoice)
1051 {
1052  gnc_numeric unused;
1053  AccountValueList *taxes;
1054  if (!invoice) return NULL;
1055 
1056  gncInvoiceGetNetAndTaxesInternal (invoice, FALSE, &taxes, FALSE, 0);
1057  return taxes;
1058 }
1059 
1060 GList * gncInvoiceGetTypeListForOwnerType (GncOwnerType type)
1061 {
1062  GList *type_list = NULL;
1063  switch (type)
1064  {
1065  case GNC_OWNER_CUSTOMER:
1066  type_list = g_list_append (type_list, GINT_TO_POINTER(GNC_INVOICE_CUST_INVOICE));
1067  type_list = g_list_append (type_list, GINT_TO_POINTER(GNC_INVOICE_CUST_CREDIT_NOTE));
1068  return type_list;
1069  case GNC_OWNER_VENDOR:
1070  type_list = g_list_append (type_list, GINT_TO_POINTER(GNC_INVOICE_VEND_INVOICE));
1071  type_list = g_list_append (type_list, GINT_TO_POINTER(GNC_INVOICE_VEND_CREDIT_NOTE));
1072  return type_list;
1073  case GNC_OWNER_EMPLOYEE:
1074  type_list = g_list_append (type_list, GINT_TO_POINTER(GNC_INVOICE_EMPL_INVOICE));
1075  type_list = g_list_append (type_list, GINT_TO_POINTER(GNC_INVOICE_EMPL_CREDIT_NOTE));
1076  return type_list;
1077  default:
1078  PWARN("Bad owner type, no invoices.");
1079  return NULL;
1080  }
1081 
1082 }
1083 
1084 GncInvoiceType gncInvoiceGetType (const GncInvoice *invoice)
1085 {
1086  if (!invoice) return GNC_INVOICE_UNDEFINED;
1087  switch (gncInvoiceGetOwnerType (invoice))
1088  {
1089  case GNC_OWNER_CUSTOMER:
1090  return (gncInvoiceGetIsCreditNote (invoice) ?
1091  GNC_INVOICE_CUST_CREDIT_NOTE :
1092  GNC_INVOICE_CUST_INVOICE);
1093  case GNC_OWNER_VENDOR:
1094  return (gncInvoiceGetIsCreditNote (invoice) ?
1095  GNC_INVOICE_VEND_CREDIT_NOTE :
1096  GNC_INVOICE_VEND_INVOICE);
1097  case GNC_OWNER_EMPLOYEE:
1098  return (gncInvoiceGetIsCreditNote (invoice) ?
1099  GNC_INVOICE_EMPL_CREDIT_NOTE :
1100  GNC_INVOICE_EMPL_INVOICE);
1101  default:
1102  PWARN ("No invoice types defined for owner %d",
1103  gncInvoiceGetOwnerType (invoice));
1104  return GNC_INVOICE_UNDEFINED;
1105  }
1106 }
1107 
1108 const char * gncInvoiceGetTypeString (const GncInvoice *invoice)
1109 {
1110  GncInvoiceType type = gncInvoiceGetType (invoice);
1111  switch (type)
1112  {
1113  case GNC_INVOICE_CUST_INVOICE:
1114  return _("Invoice");
1115  case GNC_INVOICE_VEND_INVOICE:
1116  return _("Bill");
1117  case GNC_INVOICE_EMPL_INVOICE:
1118  return _("Expense");
1119  case GNC_INVOICE_CUST_CREDIT_NOTE:
1120  case GNC_INVOICE_VEND_CREDIT_NOTE:
1121  case GNC_INVOICE_EMPL_CREDIT_NOTE:
1122  return _("Credit Note");
1123  default:
1124  PWARN("Unknown invoice type");
1125  return NULL;
1126  }
1127 }
1128 
1129 gnc_commodity * gncInvoiceGetCurrency (const GncInvoice *invoice)
1130 {
1131  if (!invoice) return NULL;
1132  return invoice->currency;
1133 }
1134 
1135 GncOwner * gncInvoiceGetBillTo (GncInvoice *invoice)
1136 {
1137  if (!invoice) return NULL;
1138  return &invoice->billto;
1139 }
1140 
1141 GNCLot * gncInvoiceGetPostedLot (const GncInvoice *invoice)
1142 {
1143  if (!invoice) return NULL;
1144  return invoice->posted_lot;
1145 }
1146 
1147 Transaction * gncInvoiceGetPostedTxn (const GncInvoice *invoice)
1148 {
1149  if (!invoice) return NULL;
1150  return invoice->posted_txn;
1151 }
1152 
1153 Account * gncInvoiceGetPostedAcc (const GncInvoice *invoice)
1154 {
1155  if (!invoice) return NULL;
1156  return invoice->posted_acc;
1157 }
1158 
1159 gboolean gncInvoiceGetActive (const GncInvoice *invoice)
1160 {
1161  if (!invoice) return FALSE;
1162  return invoice->active;
1163 }
1164 
1165 gboolean gncInvoiceGetIsCreditNote (const GncInvoice *invoice)
1166 {
1167  GValue v = G_VALUE_INIT;
1168  gboolean retval;
1169  if (!invoice) return FALSE;
1170  qof_instance_get_kvp (QOF_INSTANCE(invoice), &v, 1, GNC_INVOICE_IS_CN);
1171  retval = G_VALUE_HOLDS_INT64(&v) && g_value_get_int64 (&v);
1172  g_value_unset (&v);
1173  return retval;
1174 }
1175 
1176 
1177 gnc_numeric gncInvoiceGetToChargeAmount (const GncInvoice *invoice)
1178 {
1179  if (!invoice) return gnc_numeric_zero ();
1180  return invoice->to_charge_amount;
1181 }
1182 
1183 EntryList * gncInvoiceGetEntries (GncInvoice *invoice)
1184 {
1185  if (!invoice) return NULL;
1186  return invoice->entries;
1187 }
1188 
1189 GNCPrice * gncInvoiceGetPrice (GncInvoice *invoice, gnc_commodity *commodity)
1190 {
1191  GList *node = g_list_first (invoice->prices);
1192 
1193  while (node != NULL)
1194  {
1195  GNCPrice *curr = (GNCPrice*)node->data;
1196 
1197  if (gnc_commodity_equal (commodity, gnc_price_get_commodity (curr)))
1198  return curr;
1199 
1200  node = g_list_next (node);
1201  }
1202 
1203  return NULL;
1204 }
1205 
1206 static QofCollection*
1207 qofInvoiceGetEntries (GncInvoice *invoice)
1208 {
1209  QofCollection *entry_coll;
1210  GList *list;
1211  QofInstance *entry;
1212 
1213  entry_coll = qof_collection_new (GNC_ID_ENTRY);
1214  for (list = gncInvoiceGetEntries (invoice); list != NULL; list = list->next)
1215  {
1216  entry = QOF_INSTANCE(list->data);
1217  qof_collection_add_entity (entry_coll, entry);
1218  }
1219  return entry_coll;
1220 }
1221 
1222 static void
1223 qofInvoiceEntryCB (QofInstance *ent, gpointer user_data)
1224 {
1225  GncInvoice *invoice;
1226 
1227  invoice = (GncInvoice*)user_data;
1228  if (!invoice || !ent)
1229  {
1230  return;
1231  }
1232  switch (gncInvoiceGetOwnerType (invoice))
1233  {
1234  case GNC_OWNER_VENDOR:
1235  {
1236  gncBillAddEntry (invoice, (GncEntry*) ent);
1237  break;
1238  }
1239  default :
1240  {
1241  gncInvoiceAddEntry (invoice, (GncEntry*)ent);
1242  break;
1243  }
1244  }
1245 }
1246 
1247 static void
1248 qofInvoiceSetEntries (GncInvoice *invoice, QofCollection *entry_coll)
1249 {
1250  if (!entry_coll)
1251  {
1252  return;
1253  }
1254  if (0 == g_strcmp0 (qof_collection_get_type (entry_coll), GNC_ID_ENTRY))
1255  {
1256  qof_collection_foreach (entry_coll, qofInvoiceEntryCB, invoice);
1257  }
1258 }
1259 
1260 static GncJob*
1261 qofInvoiceGetJob (const GncInvoice *invoice)
1262 {
1263  if (!invoice)
1264  {
1265  return NULL;
1266  }
1267  return invoice->job;
1268 }
1269 
1270 static void
1271 qofInvoiceSetJob (GncInvoice *invoice, GncJob *job)
1272 {
1273  if (!invoice)
1274  {
1275  return;
1276  }
1277  invoice->job = job;
1278 }
1279 
1280 void
1281 gncInvoiceDetachFromLot (GNCLot *lot)
1282 {
1283  if (!lot) return;
1284 
1285  gnc_lot_begin_edit (lot);
1286  qof_instance_set (QOF_INSTANCE (lot), "invoice", NULL, NULL);
1287  gnc_lot_commit_edit (lot);
1288  gnc_lot_set_cached_invoice (lot, NULL);
1289 }
1290 
1291 void
1292 gncInvoiceAttachToLot (GncInvoice *invoice, GNCLot *lot)
1293 {
1294  GncGUID *guid;
1295  if (!invoice || !lot)
1296  return;
1297 
1298  if (invoice->posted_lot) return; /* Cannot reset invoice's lot */
1299  guid = (GncGUID*)qof_instance_get_guid (QOF_INSTANCE(invoice));
1300  gnc_lot_begin_edit (lot);
1301  qof_instance_set (QOF_INSTANCE (lot), "invoice", guid, NULL);
1302  gnc_lot_commit_edit (lot);
1303  gnc_lot_set_cached_invoice (lot, invoice);
1304  gncInvoiceSetPostedLot (invoice, lot);
1305 }
1306 
1307 GncInvoice * gncInvoiceGetInvoiceFromLot (GNCLot *lot)
1308 {
1309  GncGUID *guid = NULL;
1310  QofBook *book;
1311  GncInvoice *invoice = NULL;
1312 
1313  if (!lot) return NULL;
1314 
1315  invoice = gnc_lot_get_cached_invoice (lot);
1316  if (!invoice)
1317  {
1318  book = gnc_lot_get_book (lot);
1319  qof_instance_get (QOF_INSTANCE(lot), "invoice", &guid, NULL);
1320  invoice = gncInvoiceLookup (book, guid);
1321  guid_free (guid);
1322  gnc_lot_set_cached_invoice (lot, invoice);
1323  }
1324 
1325  return invoice;
1326 }
1327 
1328 void
1329 gncInvoiceAttachToTxn (GncInvoice *invoice, Transaction *txn)
1330 {
1331  if (!invoice || !txn)
1332  return;
1333 
1334  if (invoice->posted_txn) return; /* Cannot reset invoice's txn */
1335 
1336  xaccTransBeginEdit (txn);
1337  qof_instance_set (QOF_INSTANCE (txn), "invoice", //Prop INVOICE
1338  qof_instance_get_guid (QOF_INSTANCE (invoice)), NULL);
1340  xaccTransCommitEdit (txn);
1341  gncInvoiceSetPostedTxn (invoice, txn);
1342 }
1343 
1344 GncInvoice *
1345 gncInvoiceGetInvoiceFromTxn (const Transaction *txn)
1346 {
1347  GncGUID *guid = NULL;
1348  QofBook *book;
1349  GncInvoice *invoice = NULL;
1350 
1351  if (!txn) return NULL;
1352 
1353  book = xaccTransGetBook (txn);
1354  qof_instance_get (QOF_INSTANCE (txn), "invoice", &guid, NULL);
1355  invoice = gncInvoiceLookup (book, guid);
1356  guid_free (guid);
1357  return invoice;
1358 }
1359 
1360 gboolean gncInvoiceAmountPositive (const GncInvoice *invoice)
1361 {
1362  switch (gncInvoiceGetType (invoice))
1363  {
1364  case GNC_INVOICE_CUST_INVOICE:
1365  case GNC_INVOICE_VEND_CREDIT_NOTE:
1366  case GNC_INVOICE_EMPL_CREDIT_NOTE:
1367  return TRUE;
1368  case GNC_INVOICE_CUST_CREDIT_NOTE:
1369  case GNC_INVOICE_VEND_INVOICE:
1370  case GNC_INVOICE_EMPL_INVOICE:
1371  return FALSE;
1372  case GNC_INVOICE_UNDEFINED:
1373  default:
1374  /* Should never be reached.
1375  * If it is, perhaps a new value is added to GncInvoiceType ? */
1376  g_assert_not_reached ();
1377  return FALSE;
1378  }
1379 }
1380 
1381 GHashTable *gncInvoiceGetForeignCurrencies (const GncInvoice *invoice)
1382 {
1383  EntryList *entries_iter;
1384  gboolean is_cust_doc = (gncInvoiceGetOwnerType (invoice) == GNC_OWNER_CUSTOMER);
1385  gboolean is_cn = gncInvoiceGetIsCreditNote (invoice);
1386  GHashTable *amt_hash = g_hash_table_new_full (g_direct_hash, g_direct_equal,
1387  NULL, g_free);
1388  ENTER ("");
1389 
1390  for (entries_iter = invoice->entries; entries_iter != NULL; entries_iter = g_list_next(entries_iter))
1391  {
1392  GncEntry *entry = (GncEntry*)entries_iter->data;
1393  Account *this_acc;
1394  gnc_commodity *account_currency;
1395  AccountValueList *tt_amts = NULL, *tt_iter;
1396 
1397  /* Check entry's account currency */
1398  this_acc = (is_cust_doc ? gncEntryGetInvAccount (entry) :
1399  gncEntryGetBillAccount (entry));
1400  account_currency = xaccAccountGetCommodity (this_acc);
1401 
1402  if (this_acc &&
1403  !gnc_commodity_equal (gncInvoiceGetCurrency (invoice), account_currency))
1404  {
1405  gnc_numeric *curr_amt = (gnc_numeric*) g_hash_table_lookup (amt_hash, account_currency);
1406  gnc_numeric *entry_amt = (gnc_numeric*) g_new0 (gnc_numeric, 1);
1407  *entry_amt = gncEntryGetDocValue (entry, FALSE, is_cust_doc, is_cn);
1408  if (curr_amt)
1409  *entry_amt = gnc_numeric_add (*entry_amt, *curr_amt, GNC_DENOM_AUTO, GNC_HOW_RND_ROUND_HALF_UP);
1410  g_hash_table_insert (amt_hash, account_currency, entry_amt);
1411  }
1412 
1413  /* Check currencies of each account in the tax table linked
1414  * to the current entry */
1415  tt_amts = gncEntryGetDocTaxValues (entry, is_cust_doc, is_cn);
1416 
1417  if (!tt_amts)
1418  continue;
1419 
1420  for (tt_iter = tt_amts; tt_iter != NULL; tt_iter = g_list_next(tt_iter))
1421  {
1422  GncAccountValue *tt_amt_val = (GncAccountValue*)tt_iter->data;
1423  Account *tt_acc = tt_amt_val->account;
1424  gnc_commodity *tt_acc_currency = xaccAccountGetCommodity (tt_acc);
1425 
1426  if (tt_acc &&
1427  !gnc_commodity_equal (gncInvoiceGetCurrency (invoice), tt_acc_currency))
1428  {
1429  gnc_numeric *curr_amt = (gnc_numeric*) g_hash_table_lookup (amt_hash, tt_acc_currency);
1430  gnc_numeric *tt_acc_amt = (gnc_numeric*) g_new0 (gnc_numeric, 1);
1431  *tt_acc_amt = tt_amt_val->value;
1432  if (curr_amt)
1433  *tt_acc_amt = gnc_numeric_add (*tt_acc_amt, *curr_amt, GNC_DENOM_AUTO, GNC_HOW_RND_ROUND_HALF_UP);
1434  g_hash_table_insert (amt_hash, tt_acc_currency, tt_acc_amt);
1435  }
1436  }
1437  gncAccountValueDestroy (tt_amts);
1438  }
1439 
1440  LEAVE ("");
1441  return amt_hash;
1442 }
1443 
1444 static gboolean gncInvoicePostAddSplit (QofBook *book,
1445  Account *acc,
1446  Transaction *txn,
1447  gnc_numeric value,
1448  const gchar *memo,
1449  const gchar *type,
1450  GncInvoice *invoice)
1451 {
1452  Split *split;
1453 
1454  ENTER ("");
1455  split = xaccMallocSplit (book);
1456  /* set action and memo? */
1457 
1458  xaccSplitSetMemo (split, memo);
1459  /* set per book option */
1460  gnc_set_num_action (NULL, split, gncInvoiceGetID (invoice), type);
1461 
1462  /* Need to insert this split into the account AND txn before
1463  * we set the Base Value. Otherwise SetBaseValue complains
1464  * that we don't have an account and fails to set the value.
1465  */
1466  xaccAccountBeginEdit (acc);
1467  xaccAccountInsertSplit (acc, split);
1468  xaccAccountCommitEdit (acc);
1469  xaccTransAppendSplit (txn, split);
1470 
1471  /* General note on the split creations below:
1472  * Invoice and bill amounts are always stored as positive values in entries
1473  * So to convert them to proper splits, the amounts may have to be reverted
1474  * to have the proper effect on the account balance.
1475  * Credit notes have the opposite effect of invoices/bills, but their amounts
1476  * are stored as negative values as well. So to convert them into splits
1477  * they can be treated exactly the same as their invoice/bill counter parts.
1478  * The net effect is that the owner type is sufficient to determine whether a
1479  * value has to be reverted when converting an invoice/bill/cn amount to a split.
1480  */
1481  if (gnc_commodity_equal (xaccAccountGetCommodity (acc), invoice->currency))
1482  {
1483  xaccSplitSetBaseValue (split, value,
1484  invoice->currency);
1485  }
1486  else
1487  {
1488  /*need to do conversion */
1489  GNCPrice *price = gncInvoiceGetPrice (invoice, xaccAccountGetCommodity (acc));
1490 
1491  if (price == NULL)
1492  {
1493  /*This is an error, which shouldn't even be able to happen.
1494  We can't really do anything sensible about it, and this is
1495  a user-interface free zone so we can't try asking the user
1496  again either, have to return NULL*/
1497  PERR("Multiple commodities with no price.");
1498  LEAVE ("FALSE");
1499  return FALSE;
1500  }
1501  else
1502  {
1503  gnc_numeric converted_amount;
1504  xaccSplitSetValue (split, value);
1505  converted_amount = gnc_numeric_div (value, gnc_price_get_value (price), GNC_DENOM_AUTO, GNC_HOW_RND_ROUND_HALF_UP);
1506  DEBUG("converting from %f to %f\n", gnc_numeric_to_double (value), gnc_numeric_to_double (converted_amount));
1507  xaccSplitSetAmount (split, converted_amount);
1508  }
1509  }
1510 
1511  LEAVE ("TRUE");
1512  return TRUE;
1513 }
1514 
1515 Transaction * gncInvoicePostToAccount (GncInvoice *invoice, Account *acc,
1516  time64 post_date, time64 due_date,
1517  const char * memo, gboolean accumulatesplits,
1518  gboolean autopay)
1519 {
1520  Transaction *txn;
1521  QofBook *book;
1522  GNCLot *lot = NULL;
1523  GList *iter;
1524  GList *splitinfo = NULL;
1525  gnc_numeric total;
1526  gboolean is_cust_doc;
1527  gboolean is_cn;
1528  const char *name, *type;
1529  char *lot_title;
1530  Account *ccard_acct = NULL;
1531  const GncOwner *owner;
1532  int denom = xaccAccountGetCommoditySCU (acc);
1533  AccountValueList *taxes;
1534 
1535  if (!invoice || !acc) return NULL;
1536  if (gncInvoiceIsPosted (invoice)) return NULL;
1537 
1538  ENTER ("");
1539  gncInvoiceBeginEdit (invoice);
1540  book = qof_instance_get_book (invoice);
1541 
1542  /* Stabilize the Billing Terms of this invoice */
1543  if (invoice->terms)
1544  gncInvoiceSetTerms (invoice,
1545  gncBillTermReturnChild (invoice->terms, TRUE));
1546 
1547  /* GncEntry functions need to know if the invoice/credit note is for a customer or a vendor/employee. */
1548  is_cust_doc = (gncInvoiceGetOwnerType (invoice) == GNC_OWNER_CUSTOMER);
1549  is_cn = gncInvoiceGetIsCreditNote (invoice);
1550 
1551  /* Figure out if we need to separate out "credit-card" items */
1552  owner = gncOwnerGetEndOwner (gncInvoiceGetOwner (invoice));
1553  if (gncInvoiceGetOwnerType (invoice) == GNC_OWNER_EMPLOYEE)
1554  ccard_acct = gncEmployeeGetCCard (gncOwnerGetEmployee (owner));
1555 
1556  /* Create a new lot for this invoice */
1557  lot = gnc_lot_new (book);
1558  gncInvoiceAttachToLot (invoice, lot);
1559  gnc_lot_begin_edit (lot);
1560 
1561  type = gncInvoiceGetTypeString (invoice);
1562 
1563  /* Set the lot title */
1564  lot_title = g_strdup_printf ("%s %s", type, gncInvoiceGetID (invoice));
1565  gnc_lot_set_title (lot, lot_title);
1566  g_free (lot_title);
1567 
1568  /* Create a new transaction */
1569  txn = xaccMallocTransaction (book);
1570  xaccTransBeginEdit (txn);
1571 
1572  name = gncOwnerGetName (gncOwnerGetEndOwner (gncInvoiceGetOwner (invoice)));
1573 
1574  /* Set Transaction Description (Owner Name) , Num (invoice ID or type, based
1575  * on book option), Currency */
1576  xaccTransSetDescription (txn, name ? name : "");
1577  gnc_set_num_action (txn, NULL, gncInvoiceGetID (invoice), type);
1578  xaccTransSetCurrency (txn, invoice->currency);
1579 
1580  /* Entered and Posted at date */
1581  xaccTransSetDateEnteredSecs (txn, gnc_time (NULL));
1582  xaccTransSetDatePostedSecsNormalized (txn, post_date);
1583  gncInvoiceSetDatePosted (invoice, xaccTransRetDatePosted(txn));
1584 
1585  xaccTransSetDateDue (txn, due_date);
1586 
1587  /* Get invoice total and taxes. */
1588  total = gncInvoiceGetTotal (invoice);
1589  taxes = gncInvoiceGetTotalTaxList (invoice);
1590  /* The two functions above return signs relative to the document
1591  * We need to convert them to balance values before we can use them here
1592  * Note the odd construct comparing two booleans is to xor them
1593  * that is, only evaluate true if both are different.
1594  */
1595  if (is_cust_doc != is_cn)
1596  {
1597  GList *node;
1598  total = gnc_numeric_neg (total);
1599  for (node = taxes; node; node = node->next)
1600  {
1601  GncAccountValue *acc_val = node->data;
1602  acc_val->value = gnc_numeric_neg (acc_val->value);
1603  }
1604  }
1605 
1606  /* Iterate through the entries; sum up everything for each account.
1607  * then create the appropriate splits in this txn.
1608  */
1609 
1610  for (iter = gncInvoiceGetEntries (invoice); iter; iter = iter->next)
1611  {
1612  gnc_numeric value, tax;
1613  GncEntry * entry = iter->data;
1614  Account *this_acc;
1615 
1616  /* Stabilize the TaxTable in this entry */
1617  gncEntryBeginEdit (entry);
1618  if (is_cust_doc)
1619  gncEntrySetInvTaxTable
1620  (entry, gncTaxTableReturnChild (gncEntryGetInvTaxTable (entry), TRUE));
1621  else
1622  {
1623  gncEntrySetBillTaxTable
1624  (entry, gncTaxTableReturnChild (gncEntryGetBillTaxTable (entry), TRUE));
1625 
1626  /* If this is a bill, and the entry came from an invoice originally, copy the price */
1627  if (gncEntryGetBillable (entry))
1628  {
1629  /* We need to set the net price since it may be another tax rate for invoices than bills */
1630  gncEntrySetInvPrice (entry, gncEntryGetPrice (entry, FALSE, TRUE));
1631  gncEntrySetInvTaxIncluded (entry, FALSE);
1632  }
1633  }
1634  gncEntryCommitEdit (entry);
1635 
1636  /* Obtain the Entry's Value and TaxValues
1637  Note we use rounded values here and below to prevent creating an imbalanced transaction */
1638  value = gncEntryGetBalValue (entry, TRUE, is_cust_doc);
1639  tax = gncEntryGetBalTaxValue (entry, TRUE, is_cust_doc);
1640 
1641  DEBUG ("Tax %" PRId64 "/%" PRId64 " on entry value %" PRId64 "/%" PRId64,
1642  tax.num, tax.denom, value.num, value.denom);
1643  /* add the value for the account split */
1644  this_acc = (is_cust_doc ? gncEntryGetInvAccount (entry) :
1645  gncEntryGetBillAccount (entry));
1646  if (this_acc)
1647  {
1648  if (gnc_numeric_check (value) == GNC_ERROR_OK)
1649  {
1650  if (accumulatesplits)
1651  splitinfo = gncAccountValueAdd (splitinfo, this_acc, value);
1652  /* Adding to total in case of accumulatesplits will be deferred to later when each split is effectively added */
1653  else if (!gncInvoicePostAddSplit (book, this_acc, txn, value,
1654  gncEntryGetDescription (entry),
1655  type, invoice))
1656  {
1657  /*This is an error, which shouldn't even be able to happen.
1658  We can't really do anything sensible about it, and this is
1659  a user-interface free zone so we can't try asking the user
1660  again either, have to return NULL*/
1661  PERR("Failed to add split %s", gncEntryGetDescription (entry));
1662  LEAVE ("NULL");
1663  return NULL;
1664  }
1665 
1666  /* If there is a credit-card account, and this is a CCard
1667  * payment type, subtract it from the total, and instead
1668  * create a split to the CC Acct with a memo of the entry
1669  * description instead of the provided memo. Note that the
1670  * value reversal is the same as the post account.
1671  *
1672  * Note: we don't have to worry about the tax values --
1673  * expense vouchers don't have them.
1674  */
1675  if (ccard_acct && gncEntryGetBillPayment (entry) == GNC_PAYMENT_CARD)
1676  {
1677  Split *split;
1678 
1679  total = gnc_numeric_sub (total, value, denom,
1681 
1682  split = xaccMallocSplit (book);
1683  xaccSplitSetMemo (split, gncEntryGetDescription (entry));
1684  /* set action based on book option */
1685  gnc_set_num_action (NULL, split, gncInvoiceGetID (invoice), type);
1686  xaccAccountBeginEdit (ccard_acct);
1687  xaccAccountInsertSplit (ccard_acct, split);
1688  xaccAccountCommitEdit (ccard_acct);
1689  xaccTransAppendSplit (txn, split);
1690  xaccSplitSetBaseValue (split, gnc_numeric_neg (value),
1691  invoice->currency);
1692 
1693  }
1694 
1695  }
1696  else
1697  PWARN ("bad value in our entry");
1698  }
1699 
1700  /* check the taxes */
1701  if (gnc_numeric_check (tax) != GNC_ERROR_OK)
1702  PWARN ("bad tax in our entry");
1703 
1704  } /* for */
1705 
1706 
1707  /* now merge in the TaxValues */
1708  splitinfo = gncAccountValueAddList (splitinfo, taxes);
1709  gncAccountValueDestroy (taxes);
1710 
1711  /* Iterate through the splitinfo list and generate the splits */
1712  if (splitinfo)
1713  PINFO ("Processing Split List");
1714  for (iter = splitinfo; iter; iter = iter->next)
1715  {
1716  GncAccountValue *acc_val = iter->data;
1717 
1718  //gnc_numeric amt_rounded = gnc_numeric_convert(acc_val->value,
1719  // denom, GNC_HOW_DENOM_EXACT | GNC_HOW_RND_ROUND_HALF_UP);
1720  if (!gncInvoicePostAddSplit (book, acc_val->account, txn, acc_val->value,
1721  memo, type, invoice))
1722  {
1723  /*This is an error, which shouldn't even be able to happen.
1724  We can't really do anything sensible about it, and this is
1725  a user-interface free zone so we can't try asking the user
1726  again either, have to return NULL*/
1727  PERR("Failed to add split %s, aborting accumulated splits.", memo);
1728  return NULL;
1729  }
1730  }
1731 
1732  /* If there is a ccard account, we may have an additional "to_card" payment.
1733  * we should make that now.
1734  */
1735  if (ccard_acct && !gnc_numeric_zero_p (invoice->to_charge_amount))
1736  {
1737  Split *split = xaccMallocSplit (book);
1738 
1739  /* To charge amount is stored in document value. We need balance value here
1740  * so convert if necessary. */
1741  gnc_numeric to_charge_bal_amount = (is_cn ? gnc_numeric_neg (invoice->to_charge_amount)
1742  : invoice->to_charge_amount);
1743 
1744  PINFO ("Process to_card payment split");
1745  /* Set memo. */
1746  xaccSplitSetMemo (split, _("Extra to Charge Card"));
1747  /* Set action based on book option */
1748  gnc_set_num_action (NULL, split, gncInvoiceGetID (invoice), type);
1749 
1750  xaccAccountBeginEdit (ccard_acct);
1751  xaccAccountInsertSplit (ccard_acct, split);
1752  xaccAccountCommitEdit (ccard_acct);
1753  xaccTransAppendSplit (txn, split);
1754  xaccSplitSetBaseValue (split, gnc_numeric_neg (to_charge_bal_amount),
1755  invoice->currency);
1756 
1757  total = gnc_numeric_sub (total, to_charge_bal_amount, denom,
1759  }
1760 
1761  /* Now create the Posted split (which is the opposite sign of the above splits) */
1762  {
1763  Split *split = xaccMallocSplit (book);
1764 
1765  PINFO ("Process to_card balancing split");
1766  /* Set memo */
1767  xaccSplitSetMemo (split, memo);
1768  /* Set action based on book option */
1769  gnc_set_num_action (NULL, split, gncInvoiceGetID (invoice), type);
1770 
1771  xaccAccountBeginEdit (acc);
1772  xaccAccountInsertSplit (acc, split);
1773  xaccAccountCommitEdit (acc);
1774  xaccTransAppendSplit (txn, split);
1775  xaccSplitSetBaseValue (split, gnc_numeric_neg (total),
1776  invoice->currency);
1777 
1778  /* add this split to the lot */
1779  gnc_lot_add_split (lot, split);
1780  }
1781 
1782  /* Now attach this invoice to the txn and account */
1783  gncInvoiceAttachToTxn (invoice, txn);
1784  gncInvoiceSetPostedAcc (invoice, acc);
1785 
1786  xaccTransSetReadOnly (txn, _("Generated from an invoice. Try unposting the invoice."));
1787  xaccTransCommitEdit (txn);
1788 
1789  gncAccountValueDestroy (splitinfo);
1790 
1791  gnc_lot_commit_edit (lot);
1792  /* Not strictly necessary, since it was done by the Set calls
1793  * above, but good insurance. */
1794  DEBUG("Committing Invoice %s", invoice->id);
1795  mark_invoice (invoice);
1796  gncInvoiceCommitEdit (invoice);
1797 
1798  /* If requested, attempt to automatically apply open payments
1799  * and reverse documents to this lot to close it (or at least
1800  * reduce its balance) */
1801  if (autopay)
1802  gncInvoiceAutoApplyPayments (invoice);
1803 
1804  LEAVE ("");
1805  return txn;
1806 }
1807 
1808 gboolean
1809 gncInvoiceUnpost (GncInvoice *invoice, gboolean reset_tax_tables)
1810 {
1811  Transaction *txn;
1812  GNCLot *lot;
1813  GList *lot_split_list, *lot_split_iter;
1814 
1815  if (!invoice) return FALSE;
1816  if (!gncInvoiceIsPosted (invoice)) return FALSE;
1817 
1818  txn = gncInvoiceGetPostedTxn (invoice);
1819  g_return_val_if_fail (txn, FALSE);
1820 
1821  lot = gncInvoiceGetPostedLot (invoice);
1822  g_return_val_if_fail (lot, FALSE);
1823 
1824  ENTER ("");
1825  /* Destroy the Posted Transaction */
1826  xaccTransClearReadOnly (txn);
1827  xaccTransBeginEdit (txn);
1828  xaccTransDestroy (txn);
1829  xaccTransCommitEdit (txn);
1830 
1831  /* Disconnect the lot from the invoice; re-attach to the invoice owner */
1832  gncInvoiceDetachFromLot (lot);
1833  gncOwnerAttachToLot (&invoice->owner, lot);
1834 
1835  /* Check if this invoice was linked to other lots (payments/inverse signed
1836  * invoices).
1837  * If this is the case, recreate the link transaction between all the remaining lots.
1838  *
1839  * Note that before GnuCash 2.6 payments were not stored in separate lots, but
1840  * always ended up in invoice lots when matched to an invoice. Over-payments
1841  * were copied to a new lot, to which later an invoice was added again and so on.
1842  * These over-payments were handled with automatic payment forward transactions.
1843  * You could consider these transactions to be links between lots as well, but
1844  * to avoid some unexpected behavior, these will not be altered here.
1845  */
1846 
1847  // Note: make a copy of the lot list here, when splits are deleted from the lot,
1848  // the original list may be destroyed by the lot code.
1849  lot_split_list = g_list_copy (gnc_lot_get_split_list (lot));
1850  if (lot_split_list)
1851  PINFO ("Recreating link transactions for remaining lots");
1852  for (lot_split_iter = lot_split_list; lot_split_iter; lot_split_iter = lot_split_iter->next)
1853  {
1854  Split *split = lot_split_iter->data;
1855  GList *other_split_list, *list_iter;
1856  Transaction *other_txn = xaccSplitGetParent (split);
1857  GList *lot_list = NULL;
1858 
1859  /* Only work with transactions that link invoices and payments.
1860  * Note: this check also catches the possible case of NULL splits. */
1861  if (xaccTransGetTxnType (other_txn) != TXN_TYPE_LINK)
1862  continue;
1863 
1864  /* Save a list of lots this linking transaction linked to */
1865  other_split_list = xaccTransGetSplitList (other_txn);
1866  for (list_iter = other_split_list; list_iter; list_iter = list_iter->next)
1867  {
1868  Split *other_split = list_iter->data;
1869  GNCLot *other_lot = xaccSplitGetLot (other_split);
1870 
1871  /* Omit the lot we are about to delete */
1872  if (other_lot == lot)
1873  continue;
1874 
1875  lot_list = g_list_prepend (lot_list, other_lot);
1876  }
1877  /* Maintain original split order */
1878  lot_list = g_list_reverse (lot_list);
1879 
1880  /* Now remove this link transaction. */
1881  xaccTransClearReadOnly (other_txn);
1882  xaccTransBeginEdit (other_txn);
1883  xaccTransDestroy (other_txn);
1884  xaccTransCommitEdit (other_txn);
1885 
1886  /* Re-balance the saved lots as well as is possible */
1887  gncOwnerAutoApplyPaymentsWithLots (&invoice->owner, lot_list);
1888 
1889  /* If any of the saved lots has no more splits, then destroy it.
1890  * Otherwise if any has an invoice associated with it,
1891  * send it a modified event to reset its paid status */
1892  for (list_iter = lot_list; list_iter; list_iter = list_iter->next)
1893  {
1894  GNCLot *other_lot = list_iter->data;
1895  GncInvoice *other_invoice = gncInvoiceGetInvoiceFromLot (other_lot);
1896 
1897  if (!gnc_lot_count_splits (other_lot))
1898  gnc_lot_destroy (other_lot);
1899  else if (other_invoice)
1900  qof_event_gen (QOF_INSTANCE(other_invoice), QOF_EVENT_MODIFY, NULL);
1901  }
1902  g_list_free (lot_list);
1903  }
1904  g_list_free (lot_split_list);
1905 
1906  /* If the lot has no splits, then destroy it */
1907  if (!gnc_lot_count_splits (lot))
1908  gnc_lot_destroy (lot);
1909 
1910  /* Clear out the invoice posted information */
1911  gncInvoiceBeginEdit (invoice);
1912 
1913  invoice->posted_acc = NULL;
1914  invoice->posted_txn = NULL;
1915  invoice->posted_lot = NULL;
1916  invoice->date_posted = INT64_MAX;
1917 
1918  /* if we've been asked to reset the tax tables, then do so */
1919  if (reset_tax_tables)
1920  {
1921  gboolean is_cust_doc = (gncInvoiceGetOwnerType (invoice) == GNC_OWNER_CUSTOMER);
1922  GList *iter;
1923 
1924  for (iter = gncInvoiceGetEntries (invoice); iter; iter = iter->next)
1925  {
1926  GncEntry *entry = iter->data;
1927 
1928  gncEntryBeginEdit (entry);
1929  if (is_cust_doc)
1930  gncEntrySetInvTaxTable (entry,
1931  gncTaxTableGetParent (gncEntryGetInvTaxTable( entry)));
1932  else
1933  gncEntrySetBillTaxTable (entry,
1934  gncTaxTableGetParent (gncEntryGetBillTaxTable (entry)));
1935  gncEntryCommitEdit (entry);
1936  }
1937  }
1938 
1939  mark_invoice (invoice);
1940  gncInvoiceCommitEdit (invoice);
1941 
1942  LEAVE ("TRUE");
1943 
1944  return TRUE;
1945 }
1946 
1947 struct lotmatch
1948 {
1949  const GncOwner *owner;
1950  gboolean positive_balance;
1951 };
1952 
1953 static gboolean
1954 gnc_lot_match_owner_balancing (GNCLot *lot, gpointer user_data)
1955 {
1956  struct lotmatch *lm = user_data;
1957  GncOwner owner_def;
1958  const GncOwner *owner;
1959  gnc_numeric balance = gnc_lot_get_balance (lot);
1960 
1961  /* Could (part of) this lot serve to balance the lot
1962  * for which this query was run ?*/
1963  if (lm->positive_balance == gnc_numeric_positive_p (balance))
1964  return FALSE;
1965 
1966  /* Is it ours? Either the lot owner or the lot invoice owner should match */
1967  if (!gncOwnerGetOwnerFromLot (lot, &owner_def))
1968  {
1969  const GncInvoice *invoice = gncInvoiceGetInvoiceFromLot (lot);
1970  if (!invoice)
1971  return FALSE;
1972  owner = gncOwnerGetEndOwner (gncInvoiceGetOwner (invoice));
1973  }
1974  else
1975  owner = gncOwnerGetEndOwner (&owner_def);
1976 
1977  return gncOwnerEqual (owner, lm->owner);
1978 }
1979 
1980 void gncInvoiceAutoApplyPayments (GncInvoice *invoice)
1981 {
1982  GNCLot *inv_lot;
1983  Account *acct;
1984  const GncOwner *owner;
1985  GList *lot_list;
1986  struct lotmatch lm;
1987 
1988  /* General note: "paying" in this context means balancing
1989  * a lot, by linking opposite signed lots together. So below the term
1990  * "payment" can both mean a true payment or it can mean a document of
1991  * the opposite sign (invoice vs credit note). It just
1992  * depends on what type of document was given as parameter
1993  * to this function. */
1994 
1995  /* Payments can only be applied to posted invoices */
1996  g_return_if_fail (invoice);
1997  g_return_if_fail (invoice->posted_lot);
1998 
1999  inv_lot = invoice->posted_lot;
2000  acct = invoice->posted_acc;
2001  owner = gncOwnerGetEndOwner (gncInvoiceGetOwner (invoice));
2002 
2003  /* Find all lots whose balance (or part of their balance) could be
2004  * used to close this lot.
2005  * To be eligible, the lots have to have an opposite signed balance
2006  * and be for the same owner.
2007  * For example, for an invoice lot, payment lots and credit note lots
2008  * could be used. */
2009  lm.positive_balance = gnc_numeric_positive_p (gnc_lot_get_balance (inv_lot));
2010  lm.owner = owner;
2011  lot_list = xaccAccountFindOpenLots (acct, gnc_lot_match_owner_balancing,
2012  &lm, NULL);
2013 
2014  lot_list = g_list_prepend (lot_list, inv_lot);
2015  gncOwnerAutoApplyPaymentsWithLots (owner, lot_list);
2016  g_list_free (lot_list);
2017 }
2018 
2019 /*
2020  * Create a payment of "amount" for the invoice owner and attempt
2021  * to balance it with the given invoice.
2022  */
2023 void
2024 gncInvoiceApplyPayment (const GncInvoice *invoice, Transaction *txn,
2025  Account *xfer_acc, gnc_numeric amount,
2026  gnc_numeric exch, time64 date,
2027  const char *memo, const char *num)
2028 {
2029  GNCLot *payment_lot;
2030  GList *selected_lots = NULL;
2031  const GncOwner *owner;
2032 
2033  /* Verify our arguments */
2034  if (!invoice || !gncInvoiceIsPosted (invoice) || !xfer_acc) return;
2035 
2036  owner = gncOwnerGetEndOwner (gncInvoiceGetOwner (invoice));
2037  g_return_if_fail (owner->owner.undefined);
2038 
2039  /* Create a lot for this payment */
2040  payment_lot = gncOwnerCreatePaymentLotSecs (owner, &txn,
2041  invoice->posted_acc,
2042  xfer_acc, amount, exch,
2043  date, memo, num);
2044 
2045  /* Select the invoice as only payment candidate */
2046  selected_lots = g_list_prepend (selected_lots, invoice->posted_lot);
2047 
2048  /* And link the invoice lot and the payment lot together as well as possible. */
2049  if (payment_lot)
2050  selected_lots = g_list_prepend (selected_lots, payment_lot);
2051  gncOwnerAutoApplyPaymentsWithLots (owner, selected_lots);
2052 }
2053 
2054 gboolean gncInvoiceIsPosted (const GncInvoice *invoice)
2055 {
2056  if (!invoice) return FALSE;
2057  return GNC_IS_TRANSACTION(gncInvoiceGetPostedTxn (invoice));
2058 }
2059 
2060 gboolean gncInvoiceIsPaid (const GncInvoice *invoice)
2061 {
2062  if (!invoice) return FALSE;
2063  if (!invoice->posted_lot) return FALSE;
2064  return gnc_lot_is_closed (invoice->posted_lot);
2065 }
2066 
2067 /* ================================================================== */
2068 
2069 void gncInvoiceBeginEdit (GncInvoice *invoice)
2070 {
2071  qof_begin_edit (&invoice->inst);
2072 }
2073 
2074 static void gncInvoiceOnError (QofInstance *inst, QofBackendError errcode)
2075 {
2076  PERR("Invoice QofBackend Failure: %d", errcode);
2077  gnc_engine_signal_commit_error (errcode);
2078 }
2079 
2080 static void gncInvoiceOnDone (QofInstance *invoice) { }
2081 
2082 static void invoice_free (QofInstance *inst)
2083 {
2084  GncInvoice *invoice = (GncInvoice *) inst;
2085  gncInvoiceFree (invoice);
2086 }
2087 
2088 void gncInvoiceCommitEdit (GncInvoice *invoice)
2089 {
2090  if (!qof_commit_edit (QOF_INSTANCE(invoice))) return;
2091  qof_commit_edit_part2 (&invoice->inst, gncInvoiceOnError,
2092  gncInvoiceOnDone, invoice_free);
2093 }
2094 
2095 int gncInvoiceCompare (const GncInvoice *a, const GncInvoice *b)
2096 {
2097  int compare;
2098 
2099  if (a == b) return 0;
2100  if (!a) return -1;
2101  if (!b) return 1;
2102 
2103  compare = g_strcmp0 (a->id, b->id);
2104  if (compare) return compare;
2105  if (a->date_opened != b->date_opened) return a->date_opened - b->date_opened;
2106  if (a->date_posted != b->date_posted) return a->date_posted - b->date_posted;
2107  return qof_instance_guid_compare(a, b);
2108 }
2109 
2110 gboolean gncInvoiceEqual(const GncInvoice *a, const GncInvoice *b)
2111 {
2112  if (a == NULL && b == NULL) return TRUE;
2113  if (a == NULL || b == NULL) return FALSE;
2114 
2115  g_return_val_if_fail (GNC_IS_INVOICE(a), FALSE);
2116  g_return_val_if_fail (GNC_IS_INVOICE(b), FALSE);
2117 
2118  if (g_strcmp0 (a->id, b->id) != 0)
2119  {
2120  PWARN("IDs differ: %s vs %s", a->id, b->id);
2121  return FALSE;
2122  }
2123 
2124  if (g_strcmp0 (a->notes, b->notes) != 0)
2125  {
2126  PWARN("Notes differ: %s vs %s", a->notes, b->notes);
2127  return FALSE;
2128  }
2129 
2130  if (g_strcmp0 (a->billing_id, b->billing_id) != 0)
2131  {
2132  PWARN("Billing IDs differ: %s vs %s", a->billing_id, b->billing_id);
2133  return FALSE;
2134  }
2135 
2136  if (g_strcmp0 (a->printname, b->printname) != 0)
2137  {
2138  PWARN("Printnames differ: %s vs %s", a->printname, b->printname);
2139  return FALSE;
2140  }
2141 
2142  if (a->active != b->active)
2143  {
2144  PWARN("Active flags differ");
2145  return FALSE;
2146  }
2147 
2148  if (!gncBillTermEqual (a->terms, b->terms))
2149  {
2150  PWARN("Billterms differ");
2151  return FALSE;
2152  }
2153 
2154  if (!gncJobEqual (a->job, b->job))
2155  {
2156  PWARN("Jobs differ");
2157  return FALSE;
2158  }
2159 
2160  if (!gnc_commodity_equal (a->currency, b->currency))
2161  {
2162  PWARN("Currencies differ");
2163  return FALSE;
2164  }
2165 
2166  if (!xaccAccountEqual (a->posted_acc, b->posted_acc, TRUE))
2167  {
2168  PWARN("Posted accounts differ");
2169  return FALSE;
2170  }
2171 
2172  if (!xaccTransEqual (a->posted_txn, b->posted_txn, TRUE, TRUE, TRUE, FALSE))
2173  {
2174  PWARN("Posted tx differ");
2175  return FALSE;
2176  }
2177 
2178 #if 0
2179  if (!gncLotEqual (a->posted_lot, b->posted_lot))
2180  {
2181  PWARN("Posted lots differ");
2182  return FALSE;
2183  }
2184 #endif
2185 
2186  /* FIXME: Need real checks */
2187 #if 0
2188  GList *entries;
2189  GList *prices;
2190  GncOwner owner;
2191  GncOwner billto;
2192  time64 date_opened;
2193  time64 date_posted;
2194 
2195  gnc_numeric to_charge_amount;
2196 #endif
2197 
2198  return TRUE;
2199 }
2200 
2201 /* ============================================================= */
2202 /* Package-Private functions */
2203 
2204 static const char * _gncInvoicePrintable (gpointer obj)
2205 {
2206  GncInvoice *invoice = obj;
2207 
2208  g_return_val_if_fail (invoice, NULL);
2209 
2210  if (qof_instance_get_dirty_flag (invoice) || invoice->printname == NULL)
2211  {
2212  if (invoice->printname) g_free (invoice->printname);
2213 
2214  invoice->printname =
2215  g_strdup_printf ("%s%s", invoice->id,
2216  gncInvoiceIsPosted (invoice) ? _(" (posted)") : "");
2217  }
2218 
2219  return invoice->printname;
2220 }
2221 
2222 static void
2223 destroy_invoice_on_book_close (QofInstance *ent, gpointer data)
2224 {
2225  GncInvoice* invoice = GNC_INVOICE(ent);
2226 
2227  gncInvoiceBeginEdit (invoice);
2228  gncInvoiceDestroy (invoice);
2229 }
2230 
2231 static void
2232 gnc_invoice_book_end (QofBook* book)
2233 {
2234  QofCollection *col;
2235 
2236  col = qof_book_get_collection (book, GNC_ID_INVOICE);
2237  qof_collection_foreach (col, destroy_invoice_on_book_close, NULL);
2238 }
2239 
2240 static QofObject gncInvoiceDesc =
2241 {
2242  DI(.interface_version = ) QOF_OBJECT_VERSION,
2243  DI(.e_type = ) _GNC_MOD_NAME,
2244  DI(.type_label = ) "Invoice",
2245  DI(.create = ) (gpointer)gncInvoiceCreate,
2246  DI(.book_begin = ) NULL,
2247  DI(.book_end = ) gnc_invoice_book_end,
2248  DI(.is_dirty = ) qof_collection_is_dirty,
2249  DI(.mark_clean = ) qof_collection_mark_clean,
2250  DI(.foreach = ) qof_collection_foreach,
2251  DI(.printable = ) _gncInvoicePrintable,
2252  DI(.version_cmp = ) (int (*)(gpointer, gpointer)) qof_instance_version_cmp,
2253 };
2254 
2255 static void
2256 reg_lot (void)
2257 {
2258  static QofParam params[] =
2259  {
2260  {
2261  INVOICE_FROM_LOT, _GNC_MOD_NAME,
2263  },
2264  { NULL },
2265  };
2266 
2267  qof_class_register (GNC_ID_LOT, NULL, params);
2268 }
2269 
2270 static void
2271 reg_txn (void)
2272 {
2273  static QofParam params[] =
2274  {
2275  {
2276  INVOICE_FROM_TXN, _GNC_MOD_NAME,
2278  },
2279  { NULL },
2280  };
2281 
2282  qof_class_register (GNC_ID_TRANS, NULL, params);
2283 }
2284 
2285 gboolean gncInvoiceRegister (void)
2286 {
2287  static QofParam params[] =
2288  {
2289  { INVOICE_ID, QOF_TYPE_STRING, (QofAccessFunc)gncInvoiceGetID, (QofSetterFunc)gncInvoiceSetID },
2290  { INVOICE_OWNER, GNC_ID_OWNER, (QofAccessFunc)gncInvoiceGetOwner, NULL },
2291  { INVOICE_OPENED, QOF_TYPE_DATE, (QofAccessFunc)gncInvoiceGetDateOpened, (QofSetterFunc)gncInvoiceSetDateOpened },
2292  { INVOICE_DUE, QOF_TYPE_DATE, (QofAccessFunc)gncInvoiceGetDateDue, NULL },
2293  { INVOICE_POSTED, QOF_TYPE_DATE, (QofAccessFunc)gncInvoiceGetDatePosted, (QofSetterFunc)gncInvoiceSetDatePosted },
2294  { INVOICE_IS_POSTED, QOF_TYPE_BOOLEAN, (QofAccessFunc)gncInvoiceIsPosted, NULL },
2295  { INVOICE_IS_PAID, QOF_TYPE_BOOLEAN, (QofAccessFunc)gncInvoiceIsPaid, NULL },
2296  { INVOICE_BILLINGID, QOF_TYPE_STRING, (QofAccessFunc)gncInvoiceGetBillingID, (QofSetterFunc)gncInvoiceSetBillingID },
2297  { INVOICE_NOTES, QOF_TYPE_STRING, (QofAccessFunc)gncInvoiceGetNotes, (QofSetterFunc)gncInvoiceSetNotes },
2298  { INVOICE_DOCLINK, QOF_TYPE_STRING, (QofAccessFunc)gncInvoiceGetDocLink, (QofSetterFunc)gncInvoiceSetDocLink },
2299  { INVOICE_ACC, GNC_ID_ACCOUNT, (QofAccessFunc)gncInvoiceGetPostedAcc, (QofSetterFunc)gncInvoiceSetPostedAcc },
2300  { INVOICE_POST_TXN, GNC_ID_TRANS, (QofAccessFunc)gncInvoiceGetPostedTxn, (QofSetterFunc)gncInvoiceSetPostedTxn },
2301  { INVOICE_POST_LOT, GNC_ID_LOT, (QofAccessFunc)gncInvoiceGetPostedLot, NULL/*(QofSetterFunc)gncInvoiceSetPostedLot*/ },
2302  { INVOICE_TYPE, QOF_TYPE_INT32, (QofAccessFunc)gncInvoiceGetType, NULL },
2303  { INVOICE_TYPE_STRING, QOF_TYPE_STRING, (QofAccessFunc)gncInvoiceGetTypeString, NULL },
2304  { INVOICE_TERMS, GNC_ID_BILLTERM, (QofAccessFunc)gncInvoiceGetTerms, (QofSetterFunc)gncInvoiceSetTerms },
2305  { INVOICE_BILLTO, GNC_ID_OWNER, (QofAccessFunc)gncInvoiceGetBillTo, NULL },
2306  { INVOICE_ENTRIES, QOF_TYPE_COLLECT, (QofAccessFunc)qofInvoiceGetEntries, (QofSetterFunc)qofInvoiceSetEntries },
2307  { INVOICE_JOB, GNC_ID_JOB, (QofAccessFunc)qofInvoiceGetJob, (QofSetterFunc)qofInvoiceSetJob },
2308  { QOF_PARAM_ACTIVE, QOF_TYPE_BOOLEAN, (QofAccessFunc)gncInvoiceGetActive, (QofSetterFunc)gncInvoiceSetActive },
2309  { INVOICE_IS_CN, QOF_TYPE_BOOLEAN, (QofAccessFunc)gncInvoiceGetIsCreditNote, (QofSetterFunc)gncInvoiceSetIsCreditNote },
2310  { QOF_PARAM_BOOK, QOF_ID_BOOK, (QofAccessFunc)qof_instance_get_book, NULL },
2311  { QOF_PARAM_GUID, QOF_TYPE_GUID, (QofAccessFunc)qof_instance_get_guid, NULL },
2312  { NULL },
2313  };
2314 
2315  qof_class_register (_GNC_MOD_NAME, (QofSortFunc)gncInvoiceCompare, params);
2316  reg_lot ();
2317  reg_txn ();
2318 
2319  /* Make the compiler happy... */
2320  if (0)
2321  {
2322  qofInvoiceSetEntries (NULL, NULL);
2323  qofInvoiceGetEntries (NULL);
2324  qofInvoiceSetOwner (NULL, NULL);
2325  qofInvoiceGetOwner (NULL);
2326  qofInvoiceSetBillTo (NULL, NULL);
2327  qofInvoiceGetBillTo (NULL);
2328  }
2329  if (!qof_choice_create (GNC_ID_INVOICE))
2330  {
2331  return FALSE;
2332  }
2333  return qof_object_register (&gncInvoiceDesc);
2334 }
2335 
2336 gchar *gncInvoiceNextID (QofBook *book, const GncOwner *owner)
2337 {
2338  gchar *nextID;
2339  switch (gncOwnerGetType (gncOwnerGetEndOwner (owner)))
2340  {
2341  case GNC_OWNER_CUSTOMER:
2342  nextID = qof_book_increment_and_format_counter (book, "gncInvoice");
2343  break;
2344  case GNC_OWNER_VENDOR:
2345  nextID = qof_book_increment_and_format_counter (book, "gncBill");
2346  break;
2347  case GNC_OWNER_EMPLOYEE:
2348  nextID = qof_book_increment_and_format_counter (book, "gncExpVoucher");
2349  break;
2350  default:
2351  nextID = qof_book_increment_and_format_counter (book, _GNC_MOD_NAME);
2352  break;
2353  }
2354  return nextID;
2355 }
void xaccSplitSetValue(Split *split, gnc_numeric val)
The xaccSplitSetValue() method sets the value of this split in the transaction&#39;s commodity.
Definition: gmock-Split.cpp:92
int qof_instance_version_cmp(const QofInstance *left, const QofInstance *right)
Compare two instances, based on their last update times.
#define xaccTransAppendSplit(t, s)
Add a split to the transaction.
Definition: Transaction.h:370
Transaction * xaccMallocTransaction(QofBook *book)
The xaccMallocTransaction() will malloc memory and initialize it.
Definition: Transaction.c:511
gboolean gnc_numeric_equal(gnc_numeric a, gnc_numeric b)
Equivalence predicate: Returns TRUE (1) if a and b represent the same number.
void xaccSplitSetBaseValue(Split *s, gnc_numeric value, const gnc_commodity *base_currency)
Depending on the base_currency, set either the value or the amount of this split or both: If the base...
Definition: Split.c:1323
void xaccTransSetDatePostedSecsNormalized(Transaction *trans, time64 time)
This function sets the posted date of the transaction, specified by a time64 (see ctime(3))...
int gnc_commodity_get_fraction(const gnc_commodity *cm)
Retrieve the fraction for the specified commodity.
#define QOF_TYPE_COLLECT
secondary collections are used for one-to-many references between entities and are implemented using ...
Definition: qofclass.h:101
const GncGUID * qof_instance_get_guid(gconstpointer inst)
Return the GncGUID of this instance.
void qof_instance_get(const QofInstance *inst, const gchar *first_prop,...)
Wrapper for g_object_get.
void qof_instance_set_kvp(QofInstance *, GValue const *value, unsigned count,...)
Sets a KVP slot to a value from a GValue.
QofBook * qof_instance_get_book(gconstpointer inst)
Return the book pointer.
gboolean qof_collection_is_dirty(const QofCollection *col)
Return value of &#39;dirty&#39; flag on collection.
Definition: qofid.cpp:257
char xaccTransGetTxnType(Transaction *trans)
Returns the Transaction Type: note this type will be derived from the transaction splits...
Definition: Transaction.c:2556
#define TXN_TYPE_INVOICE
Transaction is an invoice.
Definition: Transaction.h:126
#define PINFO(format, args...)
Print an informational note.
Definition: qoflog.h:256
GncInvoice * gnc_lot_get_cached_invoice(const GNCLot *lot)
The gnc_lot_get_cached_invoice() routine returns the invoice with which this lot is associated...
Definition: gnc-lot.c:400
QofBackendError
The errors that can be reported to the GUI & other front-end users.
Definition: qofbackend.h:57
int xaccAccountGetCommoditySCU(const Account *acc)
Return the SCU for the account.
Definition: Account.cpp:2701
gnc_numeric gnc_numeric_neg(gnc_numeric a)
Returns a newly created gnc_numeric that is the negative of the given gnc_numeric value...
gchar * qof_book_increment_and_format_counter(QofBook *book, const char *counter_name)
This will increment the named counter for this book and format it.
Definition: qofbook.cpp:703
GList * qof_instance_get_referring_object_list_from_collection(const QofCollection *coll, const QofInstance *ref)
Returns a list of objects from the collection which refer to the specific object. ...
#define DEBUG(format, args...)
Print a debugging message.
Definition: qoflog.h:264
void qof_instance_set(QofInstance *inst, const gchar *first_prop,...)
Wrapper for g_object_set Group setting multiple parameters in a single begin/commit/rollback.
void gnc_features_set_used(QofBook *book, const gchar *feature)
Indicate that the current book uses the given feature.
Definition: gnc-features.c:135
AccountValueList * gncEntryGetDocTaxValues(GncEntry *entry, gboolean is_cust_doc, gboolean is_cn)
Careful: the returned list is NOT owned by the entry and should be freed by the caller.
Definition: gncEntry.c:1544
void qof_class_register(QofIdTypeConst obj_name, QofSortFunc default_sort_function, const QofParam *params)
This function registers a new object class with the Qof subsystem.
Definition: qofclass.cpp:86
gboolean gnc_commodity_equal(const gnc_commodity *a, const gnc_commodity *b)
This routine returns TRUE if the two commodities are equal.
void gnc_lot_add_split(GNCLot *lot, Split *split)
The gnc_lot_add_split() routine adds a split to this lot.
Definition: gnc-lot.c:626
void xaccTransSetDescription(Transaction *trans, const char *desc)
Sets the transaction Description.
gboolean gncOwnerEqual(const GncOwner *a, const GncOwner *b)
Assess equality by checking.
Definition: gncOwner.c:405
gnc_numeric gnc_numeric_add(gnc_numeric a, gnc_numeric b, gint64 denom, gint how)
Return a+b.
void gncInvoiceRemoveEntries(GncInvoice *invoice)
Remove all entries from an invoice.
Definition: gncInvoice.c:776
gboolean gnc_numeric_zero_p(gnc_numeric a)
Returns 1 if the given gnc_numeric is 0 (zero), else returns 0.
Use any denominator which gives an exactly correct ratio of numerator to denominator.
Definition: gnc-numeric.h:189
Transaction * gncInvoicePostToAccount(GncInvoice *invoice, Account *acc, time64 post_date, time64 due_date, const char *memo, gboolean accumulatesplits, gboolean autopay)
Post this invoice to an account.
Definition: gncInvoice.c:1515
Transaction * xaccSplitGetParent(const Split *split)
Returns the parent transaction of the split.
QofCollection * qof_instance_get_collection(gconstpointer ptr)
Return the collection this instance belongs to.
int(* QofSortFunc)(gconstpointer, gconstpointer)
This function is the default sort function for a particular object type.
Definition: qofclass.h:222
GHashTable * gncInvoiceGetForeignCurrencies(const GncInvoice *invoice)
Return an overview of amounts on this invoice that will be posted to accounts in currencies that are ...
Definition: gncInvoice.c:1381
void gncInvoiceSortEntries(GncInvoice *invoice)
Call this function when an Entry is changed and you want to re-sort the list of entries.
Definition: gncInvoice.c:766
#define QOF_OBJECT_VERSION
Defines the version of the core object object registration interface.
Definition: qofobject.h:64
gboolean qof_commit_edit(QofInstance *inst)
commit_edit helpers
#define PERR(format, args...)
Log a serious error.
Definition: qoflog.h:244
#define ENTER(format, args...)
Print a function entry debugging message.
Definition: qoflog.h:272
Round to the nearest integer, rounding away from zero when there are two equidistant nearest integers...
Definition: gnc-numeric.h:166
#define QOF_PARAM_BOOK
"Known" Object Parameters – all objects must support these
Definition: qofquery.h:109
gnc_numeric gncInvoiceGetTotal(GncInvoice *invoice)
Return the "total" amount of the invoice as seen on the document (and shown to the user in the report...
Definition: gncInvoice.c:1026
void qof_collection_foreach(const QofCollection *col, QofInstanceForeachCB cb_func, gpointer user_data)
Call the callback for each entity in the collection.
Definition: qofid.cpp:323
void(* QofSetterFunc)(gpointer, gpointer)
The QofSetterFunc defines an function pointer for parameter setters.
Definition: qofclass.h:184
void qof_instance_get_kvp(QofInstance *, GValue *value, unsigned count,...)
Retrieves the contents of a KVP slot into a provided GValue.
gboolean gncBillTermEqual(const GncBillTerm *a, const GncBillTerm *b)
Check if all internal fields of a and b match.
Definition: gncBillTerm.c:643
QofInstance * qofOwnerGetOwner(const GncOwner *owner)
return the owner itself as an entity.
Definition: gncOwner.c:276
void gncInvoiceAutoApplyPayments(GncInvoice *invoice)
Attempt to pay the invoice using open payment lots and lots for documents of the opposite sign (credi...
Definition: gncInvoice.c:1980
void xaccTransSetCurrency(Transaction *trans, gnc_commodity *curr)
Set a new currency on a transaction.
Definition: Transaction.c:1426
void gncOwnerAutoApplyPaymentsWithLots(const GncOwner *owner, GList *lots)
Given a list of lots, try to balance as many of them as possible by creating balancing transactions b...
Definition: gncOwner.c:1259
GncInvoice * gncInvoiceGetInvoiceFromTxn(const Transaction *txn)
Given a transaction, find and return the Invoice.
Definition: gncInvoice.c:1345
void xaccTransDestroy(Transaction *trans)
Destroys a transaction.
#define PWARN(format, args...)
Log a warning.
Definition: qoflog.h:250
void qof_instance_init_data(QofInstance *inst, QofIdType type, QofBook *book)
Initialise the settings associated with an instance.
gboolean qof_begin_edit(QofInstance *inst)
begin_edit
void xaccTransSetTxnType(Transaction *trans, char type)
Set the Transaction Type: note the type will be saved into the Transaction kvp property as a backward...
Definition: Transaction.c:2111
double gnc_numeric_to_double(gnc_numeric in)
Convert numeric to floating-point value.
void xaccSplitSetAmount(Split *split, gnc_numeric amt)
The xaccSplitSetAmount() method sets the amount in the account&#39;s commodity that the split should have...
Definition: gmock-Split.cpp:77
gboolean xaccTransEqual(const Transaction *ta, const Transaction *tb, gboolean check_guids, gboolean check_splits, gboolean check_balances, gboolean assume_ordered)
Equality.
Definition: Transaction.c:875
Account handling public routines.
gnc_numeric gnc_numeric_convert(gnc_numeric n, gint64 denom, gint how)
Change the denominator of a gnc_numeric value to the specified denominator under standard arguments &#39;...
void xaccTransSetReadOnly(Transaction *trans, const char *reason)
Set the transaction to be ReadOnly by setting a non-NULL value as "reason".
Definition: Transaction.c:2147
void xaccSplitSetMemo(Split *split, const char *memo)
The memo is an arbitrary string associated with a split.
Definition: Split.c:1732
gboolean qof_instance_get_dirty_flag(gconstpointer ptr)
Retrieve the flag that indicates whether or not this object has been modified.
SplitList * gnc_lot_get_split_list(const GNCLot *lot)
The gnc_lot_get_split_list() routine returns a GList of all the splits in this lot.
Definition: gnc-lot.c:440
void gncBillAddEntry(GncInvoice *bill, GncEntry *entry)
Call this function when adding an entry to a bill instead of an invoice.
Definition: gncInvoice.c:735
time64 xaccTransRetDatePosted(const Transaction *trans)
Retrieve the posted date of the transaction.
Definition: Transaction.c:2492
gboolean qof_choice_create(char *type)
Set an object as using QOF_TYPE_CHOICE.
Definition: qofchoice.cpp:67
void qofOwnerSetEntity(GncOwner *owner, QofInstance *ent)
set the owner from the entity.
Definition: gncOwner.c:320
void gncInvoiceApplyPayment(const GncInvoice *invoice, Transaction *txn, Account *xfer_acc, gnc_numeric amount, gnc_numeric exch, time64 date, const char *memo, const char *num)
A convenience function to apply a payment to an invoice.
Definition: gncInvoice.c:2024
void gncOwnerAttachToLot(const GncOwner *owner, GNCLot *lot)
Attach an owner to a lot.
Definition: gncOwner.c:623
time64 gdate_to_time64(GDate d)
Turns a GDate into a time64, returning the first second of the day.
Definition: gnc-date.cpp:1256
gboolean qof_commit_edit_part2(QofInstance *inst, void(*on_error)(QofInstance *, QofBackendError), void(*on_done)(QofInstance *), void(*on_free)(QofInstance *))
part2 – deal with the backend
gboolean gncOwnerGetOwnerFromLot(GNCLot *lot, GncOwner *owner)
Get the owner from the lot.
Definition: gncOwner.c:637
gpointer(* QofAccessFunc)(gpointer object, const QofParam *param)
The QofAccessFunc defines an arbitrary function pointer for access functions.
Definition: qofclass.h:177
#define xaccTransGetBook(X)
Definition: Transaction.h:791
void qof_collection_mark_clean(QofCollection *)
reset value of dirty flag
Definition: qofid.cpp:263
void xaccTransSetDateDue(Transaction *trans, time64 time)
Dates and txn-type for A/R and A/P "invoice" postings.
Definition: Transaction.c:2097
Find the least common multiple of the arguments&#39; denominators and use that as the denominator of the ...
Definition: gnc-numeric.h:201
void xaccTransCommitEdit(Transaction *trans)
The xaccTransCommitEdit() method indicates that the changes to the transaction and its splits are com...
gnc_numeric gnc_numeric_div(gnc_numeric a, gnc_numeric b, gint64 denom, gint how)
Division.
void gncAccountValueDestroy(GList *list)
Destroy a list of accountvalues.
Definition: gncTaxTable.c:997
gboolean xaccAccountEqual(const Account *aa, const Account *ab, gboolean check_guids)
Compare two accounts for equality - this is a deep compare.
Definition: Account.cpp:1644
#define TXN_TYPE_LINK
Transaction is a link between (invoice and payment) lots.
Definition: Transaction.h:128
void xaccTransBeginEdit(Transaction *trans)
The xaccTransBeginEdit() method must be called before any changes are made to a transaction or any of...
GNCLot * gncOwnerCreatePaymentLotSecs(const GncOwner *owner, Transaction **preset_txn, Account *posted_acc, Account *xfer_acc, gnc_numeric amount, gnc_numeric exch, time64 date, const char *memo, const char *num)
Create a lot for a payment to the owner using the other parameters passed in.
Definition: gncOwner.c:751
gboolean gnc_numeric_positive_p(gnc_numeric a)
Returns 1 if a > 0, otherwise returns 0.
Split * xaccMallocSplit(QofBook *book)
Constructor.
Definition: gmock-Split.cpp:37
void gncInvoiceSetDateOpenedGDate(GncInvoice *invoice, const GDate *date)
Set the DateOpened using a GDate argument.
Definition: gncInvoice.c:494
gnc_numeric gnc_numeric_sub(gnc_numeric a, gnc_numeric b, gint64 denom, gint how)
Return a-b.
GncOwnerType gncOwnerGetType(const GncOwner *owner)
Returns the GncOwnerType of this owner.
Definition: gncOwner.c:201
const GncOwner * gncOwnerGetEndOwner(const GncOwner *owner)
Get the "parent" Owner or GncGUID thereof.
Definition: gncOwner.c:573
GncInvoice * gncInvoiceGetInvoiceFromLot(GNCLot *lot)
Given a LOT, find and return the Invoice attached to the lot.
Definition: gncInvoice.c:1307
Business Invoice Interface.
QofIdType qof_collection_get_type(const QofCollection *col)
return the type that the collection stores
Definition: qofid.cpp:76
gboolean gncInvoiceUnpost(GncInvoice *invoice, gboolean reset_tax_tables)
Unpost this invoice.
Definition: gncInvoice.c:1809
gboolean qof_collection_add_entity(QofCollection *coll, QofInstance *ent)
Add an entity to a QOF_TYPE_COLLECT.
Definition: qofid.cpp:112
gboolean gnc_lot_is_closed(GNCLot *lot)
The gnc_lot_is_closed() routine returns a boolean flag: is this lot closed? A lot is closed if its ba...
Definition: gnc-lot.c:382
void xaccAccountBeginEdit(Account *acc)
The xaccAccountBeginEdit() subroutine is the first phase of a two-phase-commit wrapper for account up...
Definition: Account.cpp:1449
gnc_commodity * xaccAccountGetCommodity(const Account *acc)
Get the account&#39;s commodity.
Definition: Account.cpp:3448
#define xaccAccountInsertSplit(acc, s)
The xaccAccountInsertSplit() method will insert the indicated split into the indicated account...
Definition: Account.h:1038
gint qof_instance_guid_compare(gconstpointer ptr1, gconstpointer ptr2)
Compare the GncGUID values of two instances.
GList * gncAccountValueAddList(GList *l1, GList *l2)
Merge l2 into l1.
Definition: gncTaxTable.c:970
#define LEAVE(format, args...)
Print a function exit debugging message.
Definition: qoflog.h:282
time64 gnc_time(time64 *tbuf)
get the current local time
Definition: gnc-date.cpp:273
GNCNumericErrorCode gnc_numeric_check(gnc_numeric in)
Check for error signal in value.
QofCollection * qof_book_get_collection(const QofBook *book, QofIdType entity_type)
Return The table of entities of the given type.
Definition: qofbook.cpp:604
gint64 time64
Many systems, including Microsoft Windows and BSD-derived Unixes like Darwin, are retaining the int-3...
Definition: gnc-date.h:93
gboolean qof_object_register(const QofObject *object)
Register new types of object objects.
Definition: qofobject.cpp:317
GList * gncAccountValueAdd(GList *list, Account *acc, gnc_numeric value)
This will add value to the account-value for acc, creating a new list object if necessary.
Definition: gncTaxTable.c:942
LotList * xaccAccountFindOpenLots(const Account *acc, gboolean(*match_func)(GNCLot *lot, gpointer user_data), gpointer user_data, GCompareFunc sort_func)
Find a list of open lots that match the match_func.
Definition: Account.cpp:4057
Business Entry Interface.
void xaccTransSetDateEnteredSecs(Transaction *trans, time64 secs)
Modify the date of when the transaction was entered.
Definition: Transaction.c:2054
gboolean gncInvoiceEqual(const GncInvoice *a, const GncInvoice *b)
Test support function used by test-dbi-business-stuff.c.
Definition: gncInvoice.c:2110
time64 time64CanonicalDayTime(time64 t)
convert a time64 on a certain day (localtime) to the time64 representing midday on that day...
Definition: gnc-date.cpp:413
void qof_event_gen(QofInstance *entity, QofEventId event_id, gpointer event_data)
Invoke all registered event handlers using the given arguments.
Definition: qofevent.cpp:231
GncEmployee * gncOwnerGetEmployee(const GncOwner *owner)
If the given owner is of type GNC_OWNER_EMPLOYEE, returns the pointer to the employee object...
Definition: gncOwner.c:391
No error.
Definition: gnc-numeric.h:224
AccountValueList * gncInvoiceGetTotalTaxList(GncInvoice *invoice)
Return a list of tax totals accumulated per tax account.
Definition: gncInvoice.c:1050
#define GNC_DENOM_AUTO
Values that can be passed as the &#39;denom&#39; argument.
Definition: gnc-numeric.h:246
API for Transactions and Splits (journal entries)
The type used to store guids in C.
Definition: guid.h:75
GncInvoice * gncInvoiceCopy(const GncInvoice *from)
Create a new GncInvoice object as a deep copy of the given other invoice.
Definition: gncInvoice.c:343
void xaccAccountCommitEdit(Account *acc)
ThexaccAccountCommitEdit() subroutine is the second phase of a two-phase-commit wrapper for account u...
Definition: Account.cpp:1490
time64 xaccTransRetDateDue(const Transaction *trans)
Dates and txn-type for A/R and A/P "invoice" postings.
Definition: Transaction.c:2539
SplitList * xaccTransGetSplitList(const Transaction *trans)
The xaccTransGetSplitList() method returns a GList of the splits in a transaction.
gboolean gncInvoiceAmountPositive(const GncInvoice *invoice)
Depending on the invoice type, invoices have a different effect on the balance.
Definition: gncInvoice.c:1360
gnc_numeric gnc_lot_get_balance(GNCLot *lot)
The gnc_lot_get_balance() routine returns the balance of the lot.
Definition: gnc-lot.c:534
GNCLot * xaccSplitGetLot(const Split *split)
Returns the pointer to the debited/credited Lot where this split belongs to, or NULL if it doesn&#39;t be...
Definition: Split.c:1885
QofCollection * qof_collection_new(QofIdType type)
create a new collection of entities of type
Definition: qofid.cpp:51
gchar * qof_instance_get_display_name(const QofInstance *inst)
Returns a displayable name for this object.
Utility functions for file access.