GnuCash  5.6-150-g038405b370+
gnc-bill-term-xml-v2.cpp
1 /********************************************************************\
2  * gnc-bill-term-xml-v2.c -- billing term xml i/o implementation *
3  * *
4  * Copyright (C) 2002 Derek Atkins <warlord@MIT.EDU> *
5  * *
6  * This program is free software; you can redistribute it and/or *
7  * modify it under the terms of the GNU General Public License as *
8  * published by the Free Software Foundation; either version 2 of *
9  * the License, or (at your option) any later version. *
10  * *
11  * This program is distributed in the hope that it will be useful, *
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14  * GNU General Public License for more details. *
15  * *
16  * You should have received a copy of the GNU General Public License*
17  * along with this program; if not, contact: *
18  * *
19  * Free Software Foundation Voice: +1-617-542-5942 *
20  * 51 Franklin Street, Fifth Floor Fax: +1-617-542-2652 *
21  * Boston, MA 02110-1301, USA gnu@gnu.org *
22  * *
23 \********************************************************************/
24 #include <config.h>
25 #include <stdlib.h>
26 #include <string.h>
27 
28 #include "gncBillTermP.h"
29 #include "gncInvoice.h"
30 #include "qof.h"
31 
32 #include "gnc-xml-helper.h"
33 
34 #include "sixtp.h"
35 #include "sixtp-utils.h"
36 #include "sixtp-parsers.h"
37 #include "sixtp-utils.h"
38 #include "sixtp-dom-parsers.h"
39 #include "sixtp-dom-generators.h"
40 
41 #include "gnc-xml.h"
42 #include "io-gncxml-gen.h"
43 #include "io-gncxml-v2.h"
44 #include "gnc-bill-term-xml-v2.h"
45 
46 #include "xml-helpers.h"
47 
48 #define _GNC_MOD_NAME GNC_ID_BILLTERM
49 
50 static QofLogModule log_module = GNC_MOD_IO;
51 
52 const gchar* billterm_version_string = "2.0.0";
53 
54 /* ids */
55 #define gnc_billterm_string "gnc:GncBillTerm"
56 #define billterm_guid_string "billterm:guid"
57 #define billterm_name_string "billterm:name"
58 #define billterm_desc_string "billterm:desc"
59 #define billterm_refcount_string "billterm:refcount"
60 #define billterm_invisible_string "billterm:invisible"
61 #define billterm_parent_string "billterm:parent"
62 #define billterm_child_string "billterm:child"
63 #define billterm_slots_string "billterm:slots"
64 
65 #define gnc_daystype_string "billterm:days"
66 #define days_duedays_string "bt-days:due-days"
67 #define days_discdays_string "bt-days:disc-days"
68 #define days_discount_string "bt-days:discount"
69 
70 #define gnc_proximotype_string "billterm:proximo"
71 #define prox_dueday_string "bt-prox:due-day"
72 #define prox_discday_string "bt-prox:disc-day"
73 #define prox_discount_string "bt-prox:discount"
74 #define prox_cutoff_string "bt-prox:cutoff-day"
75 
76 static xmlNodePtr
77 billterm_dom_tree_create (GncBillTerm* term)
78 {
79  xmlNodePtr ret, data;
80 
81  ret = xmlNewNode (NULL, BAD_CAST gnc_billterm_string);
82  xmlSetProp (ret, BAD_CAST "version", BAD_CAST billterm_version_string);
83 
84  maybe_add_guid (ret, billterm_guid_string, QOF_INSTANCE (term));
85  xmlAddChild (ret, text_to_dom_tree (billterm_name_string,
86  gncBillTermGetName (term)));
87  xmlAddChild (ret, text_to_dom_tree (billterm_desc_string,
88  gncBillTermGetDescription (term)));
89 
90  xmlAddChild (ret, int_to_dom_tree (billterm_refcount_string,
91  gncBillTermGetRefcount (term)));
92  xmlAddChild (ret, int_to_dom_tree (billterm_invisible_string,
93  gncBillTermGetInvisible (term)));
94 
95  /* xmlAddChild won't do anything with a NULL, so tests are superfluous. */
96  xmlAddChild (ret, qof_instance_slots_to_dom_tree (billterm_slots_string,
97  QOF_INSTANCE (term)));
98 
99  /* We should not be our own child */
100  if (gncBillTermGetChild (term) != term)
101  maybe_add_guid (ret, billterm_child_string,
102  QOF_INSTANCE (gncBillTermGetChild (term)));
103 
104  maybe_add_guid (ret, billterm_parent_string,
105  QOF_INSTANCE (gncBillTermGetParent (term)));
106 
107  switch (gncBillTermGetType (term))
108  {
109  case GNC_TERM_TYPE_DAYS:
110  data = xmlNewChild (ret, NULL, BAD_CAST gnc_daystype_string, NULL);
111  maybe_add_int (data, days_duedays_string, gncBillTermGetDueDays (term));
112  maybe_add_int (data, days_discdays_string,
113  gncBillTermGetDiscountDays (term));
114  maybe_add_numeric (data, days_discount_string,
115  gncBillTermGetDiscount (term));
116  break;
117 
118  case GNC_TERM_TYPE_PROXIMO:
119  data = xmlNewChild (ret, NULL, BAD_CAST gnc_proximotype_string, NULL);
120  maybe_add_int (data, prox_dueday_string, gncBillTermGetDueDays (term));
121  maybe_add_int (data, prox_discday_string,
122  gncBillTermGetDiscountDays (term));
123  maybe_add_numeric (data, prox_discount_string,
124  gncBillTermGetDiscount (term));
125  maybe_add_int (data, prox_cutoff_string, gncBillTermGetCutoff (term));
126  break;
127  }
128 
129  return ret;
130 }
131 
132 /***********************************************************************/
133 
135 {
136  GncBillTerm* term;
137  QofBook* book;
138 };
139 
140 static gboolean
141 set_int (xmlNodePtr node, GncBillTerm* term,
142  void (*func) (GncBillTerm*, gint))
143 {
144  gint64 val;
145  dom_tree_to_integer (node, &val);
146  func (term, val);
147  return TRUE;
148 }
149 
150 static gboolean
151 set_numeric (xmlNodePtr node, GncBillTerm* term,
152  void (*func) (GncBillTerm*, gnc_numeric))
153 {
154  func (term, dom_tree_to_gnc_numeric (node));
155  return TRUE;
156 }
157 
158 /***********************************************************************/
159 
160 static gboolean
161 days_duedays_handler (xmlNodePtr node, gpointer billterm_pdata)
162 {
163  struct billterm_pdata* pdata = static_cast<decltype (pdata)> (billterm_pdata);
164  return set_int (node, pdata->term, gncBillTermSetDueDays);
165 }
166 
167 static gboolean
168 days_discdays_handler (xmlNodePtr node, gpointer billterm_pdata)
169 {
170  struct billterm_pdata* pdata = static_cast<decltype (pdata)> (billterm_pdata);
171  return set_int (node, pdata->term, gncBillTermSetDiscountDays);
172 }
173 
174 static gboolean
175 days_discount_handler (xmlNodePtr node, gpointer billterm_pdata)
176 {
177  struct billterm_pdata* pdata = static_cast<decltype (pdata)> (billterm_pdata);
178  return set_numeric (node, pdata->term, gncBillTermSetDiscount);
179 }
180 
181 static struct dom_tree_handler days_data_handlers_v2[] =
182 {
183  { days_duedays_string, days_duedays_handler, 0, 0 },
184  { days_discdays_string, days_discdays_handler, 0, 0 },
185  { days_discount_string, days_discount_handler, 0, 0 },
186  { NULL, 0, 0, 0 }
187 };
188 
189 static gboolean
190 dom_tree_to_days_data (xmlNodePtr node, struct billterm_pdata* pdata)
191 {
192  gboolean successful;
193 
194  successful = dom_tree_generic_parse (node, days_data_handlers_v2, pdata);
195 
196  if (!successful)
197  PERR ("failed to parse billing term days data");
198 
199  return successful;
200 }
201 
202 /***********************************************************************/
203 
204 static gboolean
205 prox_dueday_handler (xmlNodePtr node, gpointer billterm_pdata)
206 {
207  struct billterm_pdata* pdata = static_cast<decltype (pdata)> (billterm_pdata);
208  return set_int (node, pdata->term, gncBillTermSetDueDays);
209 }
210 
211 static gboolean
212 prox_discday_handler (xmlNodePtr node, gpointer billterm_pdata)
213 {
214  struct billterm_pdata* pdata = static_cast<decltype (pdata)> (billterm_pdata);
215  return set_int (node, pdata->term, gncBillTermSetDiscountDays);
216 }
217 
218 static gboolean
219 prox_discount_handler (xmlNodePtr node, gpointer billterm_pdata)
220 {
221  struct billterm_pdata* pdata = static_cast<decltype (pdata)> (billterm_pdata);
222  return set_numeric (node, pdata->term, gncBillTermSetDiscount);
223 }
224 
225 static gboolean
226 prox_cutoff_handler (xmlNodePtr node, gpointer billterm_pdata)
227 {
228  struct billterm_pdata* pdata = static_cast<decltype (pdata)> (billterm_pdata);
229  return set_int (node, pdata->term, gncBillTermSetCutoff);
230 }
231 
232 static struct dom_tree_handler prox_data_handlers_v2[] =
233 {
234  { prox_dueday_string, prox_dueday_handler, 0, 0 },
235  { prox_discday_string, prox_discday_handler, 0, 0 },
236  { prox_discount_string, prox_discount_handler, 0, 0 },
237  { prox_cutoff_string, prox_cutoff_handler, 0, 0 },
238  { NULL, 0, 0, 0 }
239 };
240 
241 static gboolean
242 dom_tree_to_prox_data (xmlNodePtr node, struct billterm_pdata* pdata)
243 {
244  gboolean successful;
245 
246  successful = dom_tree_generic_parse (node, prox_data_handlers_v2, pdata);
247 
248  if (!successful)
249  PERR ("failed to parse billing term prox data");
250 
251  return successful;
252 }
253 
254 /***********************************************************************/
255 
256 static gboolean
257 set_parent_child (xmlNodePtr node, struct billterm_pdata* pdata,
258  void (*func) (GncBillTerm*, GncBillTerm*))
259 {
260  GncGUID* guid;
261  GncBillTerm* term;
262 
263  guid = dom_tree_to_guid (node);
264  g_return_val_if_fail (guid, FALSE);
265  term = gncBillTermLookup (pdata->book, guid);
266  if (!term)
267  {
268  term = gncBillTermCreate (pdata->book);
269  gncBillTermBeginEdit (term);
270  gncBillTermSetGUID (term, guid);
271  gncBillTermCommitEdit (term);
272  }
273  guid_free (guid);
274  g_return_val_if_fail (term, FALSE);
275  func (pdata->term, term);
276 
277  return TRUE;
278 }
279 
280 static gboolean
281 set_string (xmlNodePtr node, GncBillTerm* term,
282  void (*func) (GncBillTerm*, const char*))
283 {
284  char* txt = dom_tree_to_text (node);
285  g_return_val_if_fail (txt, FALSE);
286  func (term, txt);
287  g_free (txt);
288  return TRUE;
289 }
290 
291 static gboolean
292 billterm_guid_handler (xmlNodePtr node, gpointer billterm_pdata)
293 {
294  struct billterm_pdata* pdata = static_cast<decltype (pdata)> (billterm_pdata);
295  GncGUID* guid;
296  GncBillTerm* term;
297 
298  guid = dom_tree_to_guid (node);
299  g_return_val_if_fail (guid, FALSE);
300  term = gncBillTermLookup (pdata->book, guid);
301  if (term)
302  {
303  gncBillTermDestroy (pdata->term);
304  pdata->term = term;
305  gncBillTermBeginEdit (term);
306  }
307  else
308  {
309  gncBillTermSetGUID (pdata->term, guid);
310  }
311 
312  guid_free (guid);
313 
314  return TRUE;
315 }
316 
317 static gboolean
318 billterm_name_handler (xmlNodePtr node, gpointer billterm_pdata)
319 {
320  struct billterm_pdata* pdata = static_cast<decltype (pdata)> (billterm_pdata);
321  return set_string (node, pdata->term, gncBillTermSetName);
322 }
323 
324 static gboolean
325 billterm_desc_handler (xmlNodePtr node, gpointer billterm_pdata)
326 {
327  struct billterm_pdata* pdata = static_cast<decltype (pdata)> (billterm_pdata);
328  return set_string (node, pdata->term, gncBillTermSetDescription);
329 }
330 
331 static gboolean
332 billterm_refcount_handler (xmlNodePtr node, gpointer billterm_pdata)
333 {
334  struct billterm_pdata* pdata = static_cast<decltype (pdata)> (billterm_pdata);
335  gint64 val;
336 
337  dom_tree_to_integer (node, &val);
338  gncBillTermSetRefcount (pdata->term, val);
339  return TRUE;
340 }
341 
342 static gboolean
343 billterm_invisible_handler (xmlNodePtr node, gpointer billterm_pdata)
344 {
345  struct billterm_pdata* pdata = static_cast<decltype (pdata)> (billterm_pdata);
346  gint64 val;
347 
348  dom_tree_to_integer (node, &val);
349  if (val)
350  gncBillTermMakeInvisible (pdata->term);
351  return TRUE;
352 }
353 
354 static gboolean
355 billterm_parent_handler (xmlNodePtr node, gpointer billterm_pdata)
356 {
357  struct billterm_pdata* pdata = static_cast<decltype (pdata)> (billterm_pdata);
358  return set_parent_child (node, pdata, gncBillTermSetParent);
359 }
360 
361 static gboolean
362 billterm_child_handler (xmlNodePtr node, gpointer billterm_pdata)
363 {
364  struct billterm_pdata* pdata = static_cast<decltype (pdata)> (billterm_pdata);
365  return set_parent_child (node, pdata, gncBillTermSetChild);
366 }
367 
368 static gboolean
369 billterm_days_data_handler (xmlNodePtr node, gpointer billterm_pdata)
370 {
371  struct billterm_pdata* pdata = static_cast<decltype (pdata)> (billterm_pdata);
372 
373  g_return_val_if_fail (node, FALSE);
374  g_return_val_if_fail (gncBillTermGetType (pdata->term) == 0, FALSE);
375 
376  gncBillTermSetType (pdata->term, GNC_TERM_TYPE_DAYS);
377  return dom_tree_to_days_data (node, pdata);
378 }
379 
380 static gboolean
381 billterm_prox_data_handler (xmlNodePtr node, gpointer billterm_pdata)
382 {
383  struct billterm_pdata* pdata = static_cast<decltype (pdata)> (billterm_pdata);
384 
385  g_return_val_if_fail (node, FALSE);
386  g_return_val_if_fail (gncBillTermGetType (pdata->term) == 0, FALSE);
387 
388  gncBillTermSetType (pdata->term, GNC_TERM_TYPE_PROXIMO);
389  return dom_tree_to_prox_data (node, pdata);
390 }
391 
392 static gboolean
393 billterm_slots_handler (xmlNodePtr node, gpointer billterm_pdata)
394 {
395  struct billterm_pdata* pdata = static_cast<decltype (pdata)> (billterm_pdata);
396  return dom_tree_create_instance_slots (node, QOF_INSTANCE (pdata->term));
397 }
398 
399 static struct dom_tree_handler billterm_handlers_v2[] =
400 {
401  { billterm_guid_string, billterm_guid_handler, 1, 0 },
402  { billterm_name_string, billterm_name_handler, 1, 0 },
403  { billterm_desc_string, billterm_desc_handler, 1, 0 },
404  { billterm_refcount_string, billterm_refcount_handler, 1, 0 },
405  { billterm_invisible_string, billterm_invisible_handler, 1, 0 },
406  { billterm_parent_string, billterm_parent_handler, 0, 0 },
407  { billterm_child_string, billterm_child_handler, 0, 0 },
408  { billterm_slots_string, billterm_slots_handler, 0, 0 },
409  { gnc_daystype_string, billterm_days_data_handler, 0, 0 },
410  { gnc_proximotype_string, billterm_prox_data_handler, 0, 0 },
411  { NULL, 0, 0, 0 }
412 };
413 
414 static GncBillTerm*
415 dom_tree_to_billterm (xmlNodePtr node, QofBook* book)
416 {
418  gboolean successful;
419 
420  billterm_pdata.term = gncBillTermCreate (book);
421  billterm_pdata.book = book;
422  gncBillTermBeginEdit (billterm_pdata.term);
423 
424  successful = dom_tree_generic_parse (node, billterm_handlers_v2,
425  &billterm_pdata);
426 
427  if (successful)
428  {
429  gncBillTermCommitEdit (billterm_pdata.term);
430  }
431  else
432  {
433  PERR ("failed to parse billing term tree");
434  gncBillTermDestroy (billterm_pdata.term);
435  billterm_pdata.term = NULL;
436  }
437 
438  return billterm_pdata.term;
439 }
440 
441 static gboolean
442 gnc_billterm_end_handler (gpointer data_for_children,
443  GSList* data_from_children, GSList* sibling_data,
444  gpointer parent_data, gpointer global_data,
445  gpointer* result, const gchar* tag)
446 {
447  GncBillTerm* term;
448  xmlNodePtr tree = (xmlNodePtr)data_for_children;
449  gxpf_data* gdata = (gxpf_data*)global_data;
450  QofBook* book = static_cast<decltype (book)> (gdata->bookdata);
451 
452 
453  if (parent_data)
454  {
455  return TRUE;
456  }
457 
458  /* OK. For some messed up reason this is getting called again with a
459  NULL tag. So we ignore those cases */
460  if (!tag)
461  {
462  return TRUE;
463  }
464 
465  g_return_val_if_fail (tree, FALSE);
466 
467  term = dom_tree_to_billterm (tree, book);
468  if (term != NULL)
469  {
470  gdata->cb (tag, gdata->parsedata, term);
471  }
472 
473  xmlFreeNode (tree);
474 
475  return term != NULL;
476 }
477 
478 static sixtp*
479 billterm_sixtp_parser_create (void)
480 {
481  return sixtp_dom_parser_new (gnc_billterm_end_handler, NULL, NULL);
482 }
483 
484 static void
485 do_count (QofInstance* term_p, gpointer count_p)
486 {
487  int* count = static_cast<decltype (count)> (count_p);
488  (*count)++;
489 }
490 
491 static int
492 billterm_get_count (QofBook* book)
493 {
494  int count = 0;
495  qof_object_foreach (_GNC_MOD_NAME, book, do_count, (gpointer) &count);
496  return count;
497 }
498 
499 static void
500 xml_add_billterm (QofInstance* term_p, gpointer out_p)
501 {
502  xmlNodePtr node;
503  GncBillTerm* term = (GncBillTerm*) term_p;
504  FILE* out = static_cast<decltype (out)> (out_p);
505 
506  if (ferror (out))
507  return;
508 
509  node = billterm_dom_tree_create (term);
510  xmlElemDump (out, NULL, node);
511  xmlFreeNode (node);
512  if (ferror (out) || fprintf (out, "\n") < 0)
513  return;
514 }
515 
516 static gboolean
517 billterm_write (FILE* out, QofBook* book)
518 {
519  qof_object_foreach_sorted (_GNC_MOD_NAME, book, xml_add_billterm,
520  (gpointer) out);
521  return ferror (out) == 0;
522 }
523 
524 static gboolean
525 billterm_is_grandchild (GncBillTerm* term)
526 {
527  return (gncBillTermGetParent (gncBillTermGetParent (term)) != NULL);
528 }
529 
530 static GncBillTerm*
531 billterm_find_senior (GncBillTerm* term)
532 {
533  GncBillTerm* temp, *parent, *gp = NULL;
534 
535  temp = term;
536  do
537  {
538  /* See if "temp" is a grandchild */
539  parent = gncBillTermGetParent (temp);
540  if (!parent)
541  break;
542  gp = gncBillTermGetParent (parent);
543  if (!gp)
544  break;
545 
546  /* Yep, this is a grandchild. Move up one generation and try again */
547  temp = parent;
548  }
549  while (TRUE);
550 
551  /* Ok, at this point temp points to the most senior child and parent
552  * should point to the top billterm (and gp should be NULL). If
553  * parent is NULL then we are the most senior child (and have no
554  * children), so do nothing. If temp == term then there is no
555  * grandparent, so do nothing.
556  *
557  * Do something if parent != NULL && temp != term
558  */
559  g_assert (gp == NULL);
560 
561  /* return the most senior term */
562  return temp;
563 }
564 
565 /* build a list of bill terms that are grandchildren or bogus (empty entry list). */
566 static void
567 billterm_scrub_cb (QofInstance* term_p, gpointer list_p)
568 {
569  GncBillTerm* term = GNC_BILLTERM (term_p);
570  GList** list = static_cast<decltype (list)> (list_p);
571 
572  if (billterm_is_grandchild (term))
573  {
574  *list = g_list_prepend (*list, term);
575 
576  }
577  else if (!gncBillTermGetType (term))
578  {
579  GncBillTerm* t = gncBillTermGetParent (term);
580  if (t)
581  {
582  /* Fix up the broken "copy" function */
583  gchar guidstr[GUID_ENCODING_LENGTH + 1];
584  guid_to_string_buff (qof_instance_get_guid (QOF_INSTANCE (term)), guidstr);
585  PWARN ("Fixing broken child billterm: %s", guidstr);
586 
587  gncBillTermBeginEdit (term);
588  gncBillTermSetType (term, gncBillTermGetType (t));
589  gncBillTermSetDueDays (term, gncBillTermGetDueDays (t));
590  gncBillTermSetDiscountDays (term, gncBillTermGetDiscountDays (t));
591  gncBillTermSetDiscount (term, gncBillTermGetDiscount (t));
592  gncBillTermSetCutoff (term, gncBillTermGetCutoff (t));
593  gncBillTermCommitEdit (term);
594 
595  }
596  else
597  {
598  /* No parent? Must be a standalone */
599  *list = g_list_prepend (*list, term);
600  }
601  }
602 }
603 
604 /* for each invoice, check the bill terms. If the bill terms are
605  * grandchildren, then fix them to point to the most senior child
606  */
607 static void
608 billterm_scrub_invoices (QofInstance* invoice_p, gpointer ht_p)
609 {
610  GHashTable* ht = static_cast<decltype (ht)> (ht_p);
611  GncInvoice* invoice = GNC_INVOICE (invoice_p);
612  GncBillTerm* term, *new_bt;
613  gint32 count;
614 
615  term = gncInvoiceGetTerms (invoice);
616  if (term)
617  {
618  if (billterm_is_grandchild (term))
619  {
620  gchar guidstr[GUID_ENCODING_LENGTH + 1];
621  guid_to_string_buff (qof_instance_get_guid (QOF_INSTANCE (invoice)), guidstr);
622  PWARN ("Fixing i-billterm on invoice %s\n", guidstr);
623  new_bt = billterm_find_senior (term);
624  gncInvoiceBeginEdit (invoice);
625  gncInvoiceSetTerms (invoice, new_bt);
626  gncInvoiceCommitEdit (invoice);
627  term = new_bt;
628  }
629  if (term)
630  {
631  count = GPOINTER_TO_INT (g_hash_table_lookup (ht, term));
632  count++;
633  g_hash_table_insert (ht, term, GINT_TO_POINTER (count));
634  }
635  }
636 }
637 
638 static void
639 billterm_scrub_cust (QofInstance* cust_p, gpointer ht_p)
640 {
641  GHashTable* ht = static_cast<decltype (ht)> (ht_p);
642  GncCustomer* cust = GNC_CUSTOMER (cust_p);
643  GncBillTerm* term;
644  gint32 count;
645 
646  term = gncCustomerGetTerms (cust);
647  if (term)
648  {
649  count = GPOINTER_TO_INT (g_hash_table_lookup (ht, term));
650  count++;
651  g_hash_table_insert (ht, term, GINT_TO_POINTER (count));
652  if (billterm_is_grandchild (term))
653  {
654  gchar custstr[GUID_ENCODING_LENGTH + 1];
655  gchar termstr[GUID_ENCODING_LENGTH + 1];
656  guid_to_string_buff (qof_instance_get_guid (QOF_INSTANCE (cust)), custstr);
657  guid_to_string_buff (qof_instance_get_guid (QOF_INSTANCE (term)), termstr);
658  PWARN ("customer %s has grandchild billterm %s\n", custstr, termstr);
659  }
660  }
661 }
662 
663 static void
664 billterm_scrub_vendor (QofInstance* vendor_p, gpointer ht_p)
665 {
666  GHashTable* ht = static_cast<decltype (ht)> (ht_p);
667  GncVendor* vendor = GNC_VENDOR (vendor_p);
668  GncBillTerm* term;
669  gint32 count;
670 
671  term = gncVendorGetTerms (vendor);
672  if (term)
673  {
674  count = GPOINTER_TO_INT (g_hash_table_lookup (ht, term));
675  count++;
676  g_hash_table_insert (ht, term, GINT_TO_POINTER (count));
677  if (billterm_is_grandchild (term))
678  {
679  gchar vendstr[GUID_ENCODING_LENGTH + 1];
680  gchar termstr[GUID_ENCODING_LENGTH + 1];
681  guid_to_string_buff (qof_instance_get_guid (QOF_INSTANCE (vendor)), vendstr);
682  guid_to_string_buff (qof_instance_get_guid (QOF_INSTANCE (term)), termstr);
683  PWARN ("vendor %s has grandchild billterm %s\n", vendstr, termstr);
684  }
685  }
686 }
687 
688 static void
689 billterm_reset_refcount (gpointer key, gpointer value, gpointer notused)
690 {
691  GncBillTerm* term = static_cast<decltype (term)> (key);
692  gint32 count = GPOINTER_TO_INT (value);
693 
694  if (count != gncBillTermGetRefcount (term) && !gncBillTermGetInvisible (term))
695  {
696  gchar termstr[GUID_ENCODING_LENGTH + 1];
697  guid_to_string_buff (qof_instance_get_guid (QOF_INSTANCE (term)), termstr);
698  PWARN ("Fixing refcount on billterm %s (%" G_GINT64_FORMAT " -> %d)\n",
699  termstr, gncBillTermGetRefcount (term), count);
700  gncBillTermSetRefcount (term, count);
701  }
702 }
703 
704 static void
705 billterm_scrub (QofBook* book)
706 {
707  GList* list = NULL;
708  GList* node;
709  GncBillTerm* parent, *term;
710  GHashTable* ht = g_hash_table_new (g_direct_hash, g_direct_equal);
711 
712  DEBUG ("scrubbing billterms...");
713  qof_object_foreach (GNC_ID_INVOICE, book, billterm_scrub_invoices, ht);
714  qof_object_foreach (GNC_ID_CUSTOMER, book, billterm_scrub_cust, ht);
715  qof_object_foreach (GNC_ID_VENDOR, book, billterm_scrub_vendor, ht);
716  qof_object_foreach (GNC_ID_BILLTERM, book, billterm_scrub_cb, &list);
717 
718  /* destroy the list of "grandchildren" bill terms */
719  for (node = list; node; node = node->next)
720  {
721  gchar termstr[GUID_ENCODING_LENGTH + 1];
722  term = static_cast<decltype (term)> (node->data);
723 
724  guid_to_string_buff (qof_instance_get_guid (QOF_INSTANCE (term)), termstr);
725  PWARN ("deleting grandchild billterm: %s\n", termstr);
726 
727  /* Make sure the parent has no children */
728  parent = gncBillTermGetParent (term);
729  gncBillTermSetChild (parent, NULL);
730 
731  /* Destroy this bill term */
732  gncBillTermBeginEdit (term);
733  gncBillTermDestroy (term);
734  }
735 
736  /* reset the refcounts as necessary */
737  g_hash_table_foreach (ht, billterm_reset_refcount, NULL);
738 
739  g_list_free (list);
740  g_hash_table_destroy (ht);
741 }
742 
743 static gboolean
744 billterm_ns (FILE* out)
745 {
746  g_return_val_if_fail (out, FALSE);
747  return
748  gnc_xml2_write_namespace_decl (out, "billterm")
749  && gnc_xml2_write_namespace_decl (out, "bt-days")
750  && gnc_xml2_write_namespace_decl (out, "bt-prox");
751 }
752 
753 void
754 gnc_billterm_xml_initialize (void)
755 {
756  static GncXmlDataType_t be_data =
757  {
758  GNC_FILE_BACKEND_VERS,
759  gnc_billterm_string,
760  billterm_sixtp_parser_create,
761  NULL, /* add_item */
762  billterm_get_count,
763  billterm_write,
764  billterm_scrub,
765  billterm_ns,
766  };
767 
768  gnc_xml_register_backend(be_data);
769 }
770 
771 GncBillTerm*
772 gnc_billterm_xml_find_or_create (QofBook* book, GncGUID* guid)
773 {
774  GncBillTerm* term;
775  gchar guidstr[GUID_ENCODING_LENGTH + 1];
776 
777  guid_to_string_buff (guid, guidstr);
778  g_return_val_if_fail (book, NULL);
779  g_return_val_if_fail (guid, NULL);
780  term = gncBillTermLookup (book, guid);
781  DEBUG ("looking for billterm %s, found %p", guidstr, term);
782  if (!term)
783  {
784  term = gncBillTermCreate (book);
785  gncBillTermBeginEdit (term);
786  gncBillTermSetGUID (term, guid);
787  gncBillTermCommitEdit (term);
788  DEBUG ("Created term: %p", term);
789  }
790  else
791  gncBillTermDecRef (term);
792 
793  return term;
794 }
Definition: sixtp.h:129
const GncGUID * qof_instance_get_guid(gconstpointer inst)
Return the GncGUID of this instance.
#define DEBUG(format, args...)
Print a debugging message.
Definition: qoflog.h:264
gchar * guid_to_string_buff(const GncGUID *guid, gchar *str)
The guid_to_string_buff() routine puts a null-terminated string encoding of the id into the memory po...
Definition: guid.cpp:173
#define PERR(format, args...)
Log a serious error.
Definition: qoflog.h:244
#define PWARN(format, args...)
Log a warning.
Definition: qoflog.h:250
void qof_object_foreach_sorted(QofIdTypeConst type_name, QofBook *book, QofInstanceForeachCB cb, gpointer user_data)
Invoke callback &#39;cb&#39; on each instance in guid orted order.
Definition: qofobject.cpp:223
api for GnuCash version 2 XML-based file format
#define GUID_ENCODING_LENGTH
Number of characters needed to encode a guid as a string not including the null terminator.
Definition: guid.h:84
void qof_object_foreach(QofIdTypeConst type_name, QofBook *book, QofInstanceForeachCB cb, gpointer user_data)
Invoke the callback &#39;cb&#39; on every instance ov a particular object type.
Definition: qofobject.cpp:185
credit, discount and shipaddr are unique to GncCustomer id, name, notes, terms, addr, currency, taxtable, taxtable_override taxincluded, active and jobs are identical to ::GncVendor.
Business Invoice Interface.
The type used to store guids in C.
Definition: guid.h:75