GnuCash  5.6-150-g038405b370+
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
gnc-invoice-xml-v2.cpp
1 /********************************************************************\
2  * gnc-invoice-xml-v2.c -- invoice 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 <glib.h>
25 
26 #include <config.h>
27 #include <stdlib.h>
28 #include <string.h>
29 
30 #include "gncBillTermP.h"
31 #include "gncInvoiceP.h"
32 
33 #include "gnc-xml-helper.h"
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 #include "gnc-invoice-xml-v2.h"
41 #include "gnc-owner-xml-v2.h"
42 #include "gnc-bill-term-xml-v2.h"
43 
44 #include "gnc-xml.h"
45 #include "io-gncxml-gen.h"
46 #include "io-gncxml-v2.h"
47 
48 #define _GNC_MOD_NAME GNC_ID_INVOICE
49 
50 static QofLogModule log_module = GNC_MOD_IO;
51 
52 const gchar* invoice_version_string = "2.0.0";
53 
54 /* ids */
55 #define gnc_invoice_string "gnc:GncInvoice"
56 #define invoice_guid_string "invoice:guid"
57 #define invoice_id_string "invoice:id"
58 #define invoice_owner_string "invoice:owner"
59 #define invoice_opened_string "invoice:opened"
60 #define invoice_posted_string "invoice:posted"
61 #define invoice_terms_string "invoice:terms"
62 #define invoice_billing_id_string "invoice:billing_id"
63 #define invoice_notes_string "invoice:notes"
64 #define invoice_active_string "invoice:active"
65 #define invoice_posttxn_string "invoice:posttxn"
66 #define invoice_postlot_string "invoice:postlot"
67 #define invoice_postacc_string "invoice:postacc"
68 #define invoice_currency_string "invoice:currency"
69 #define invoice_billto_string "invoice:billto"
70 #define invoice_tochargeamt_string "invoice:charge-amt"
71 #define invoice_slots_string "invoice:slots"
72 
73 static void
74 maybe_add_string (xmlNodePtr ptr, const char* tag, const char* str)
75 {
76  if (str && *str)
77  xmlAddChild (ptr, text_to_dom_tree (tag, str));
78 }
79 
80 static void
81 maybe_add_time64 (xmlNodePtr ptr, const char* tag, time64 time)
82 {
83  if (time != INT64_MAX)
84  xmlAddChild (ptr, time64_to_dom_tree (tag, time));
85 }
86 
87 static xmlNodePtr
88 invoice_dom_tree_create (GncInvoice* invoice)
89 {
90  xmlNodePtr ret;
91  time64 time;
92  Transaction* txn;
93  GNCLot* lot;
94  Account* acc;
95  GncBillTerm* term;
96  GncOwner* billto;
97  gnc_numeric amt;
98 
99  ret = xmlNewNode (NULL, BAD_CAST gnc_invoice_string);
100  xmlSetProp (ret, BAD_CAST "version", BAD_CAST invoice_version_string);
101 
102  xmlAddChild (ret, guid_to_dom_tree (invoice_guid_string,
103  qof_instance_get_guid (QOF_INSTANCE (invoice))));
104 
105  xmlAddChild (ret, text_to_dom_tree (invoice_id_string,
106  gncInvoiceGetID (invoice)));
107 
108  xmlAddChild (ret, gnc_owner_to_dom_tree (invoice_owner_string,
109  gncInvoiceGetOwner (invoice)));
110 
111  time = gncInvoiceGetDateOpened (invoice);
112  xmlAddChild (ret, time64_to_dom_tree (invoice_opened_string, time));
113 
114  maybe_add_time64 (ret, invoice_posted_string, gncInvoiceGetDatePosted (invoice));
115 
116  term = gncInvoiceGetTerms (invoice);
117  if (term)
118  xmlAddChild (ret, guid_to_dom_tree (invoice_terms_string,
119  qof_instance_get_guid (QOF_INSTANCE (term))));
120 
121  maybe_add_string (ret, invoice_billing_id_string,
122  gncInvoiceGetBillingID (invoice));
123  maybe_add_string (ret, invoice_notes_string, gncInvoiceGetNotes (invoice));
124 
125  xmlAddChild (ret, int_to_dom_tree (invoice_active_string,
126  gncInvoiceGetActive (invoice)));
127 
128  txn = gncInvoiceGetPostedTxn (invoice);
129  if (txn)
130  xmlAddChild (ret, guid_to_dom_tree (invoice_posttxn_string,
131  xaccTransGetGUID (txn)));
132 
133  lot = gncInvoiceGetPostedLot (invoice);
134  if (lot)
135  xmlAddChild (ret, guid_to_dom_tree (invoice_postlot_string,
136  gnc_lot_get_guid (lot)));
137 
138  acc = gncInvoiceGetPostedAcc (invoice);
139  if (acc)
140  xmlAddChild (ret, guid_to_dom_tree (invoice_postacc_string,
141  qof_instance_get_guid (QOF_INSTANCE (acc))));
142 
143  xmlAddChild
144  (ret,
145  commodity_ref_to_dom_tree (invoice_currency_string,
146  gncInvoiceGetCurrency (invoice)));
147 
148  billto = gncInvoiceGetBillTo (invoice);
149  if (billto && billto->owner.undefined != NULL)
150  xmlAddChild (ret, gnc_owner_to_dom_tree (invoice_billto_string, billto));
151 
152  amt = gncInvoiceGetToChargeAmount (invoice);
153  if (! gnc_numeric_zero_p (amt))
154  xmlAddChild (ret, gnc_numeric_to_dom_tree (invoice_tochargeamt_string, &amt));
155 
156  /* xmlAddChild won't do anything with a NULL, so tests are superfluous. */
157  xmlAddChild (ret, qof_instance_slots_to_dom_tree (invoice_slots_string,
158  QOF_INSTANCE (invoice)));
159  return ret;
160 }
161 
162 /***********************************************************************/
163 
165 {
166  GncInvoice* invoice;
167  QofBook* book;
168 };
169 
170 static inline gboolean
171 set_string (xmlNodePtr node, GncInvoice* invoice,
172  void (*func) (GncInvoice* invoice, const char* txt))
173 {
174  char* txt = dom_tree_to_text (node);
175  g_return_val_if_fail (txt, FALSE);
176 
177  func (invoice, txt);
178 
179  g_free (txt);
180  return TRUE;
181 }
182 
183 static inline gboolean
184 set_time64 (xmlNodePtr node, GncInvoice* invoice,
185  void (*func) (GncInvoice* invoice, time64 time))
186 {
187  time64 time = dom_tree_to_time64 (node);
188  if (!dom_tree_valid_time64 (time, node->name)) time = 0;
189  func (invoice, time);
190  return TRUE;
191 }
192 
193 static gboolean
194 invoice_guid_handler (xmlNodePtr node, gpointer invoice_pdata)
195 {
196  struct invoice_pdata* pdata = static_cast<decltype (pdata)> (invoice_pdata);
197  GncGUID* guid;
198  GncInvoice* invoice;
199 
200  guid = dom_tree_to_guid (node);
201  g_return_val_if_fail (guid, FALSE);
202  invoice = gncInvoiceLookup (pdata->book, guid);
203  if (invoice)
204  {
205  gncInvoiceDestroy (pdata->invoice);
206  pdata->invoice = invoice;
207  gncInvoiceBeginEdit (invoice);
208  }
209  else
210  {
211  gncInvoiceSetGUID (pdata->invoice, guid);
212  }
213 
214  guid_free (guid);
215 
216  return TRUE;
217 }
218 
219 static gboolean
220 invoice_id_handler (xmlNodePtr node, gpointer invoice_pdata)
221 {
222  struct invoice_pdata* pdata = static_cast<decltype (pdata)> (invoice_pdata);
223 
224  return set_string (node, pdata->invoice, gncInvoiceSetID);
225 }
226 
227 static gboolean
228 invoice_owner_handler (xmlNodePtr node, gpointer invoice_pdata)
229 {
230  struct invoice_pdata* pdata = static_cast<decltype (pdata)> (invoice_pdata);
231  GncOwner owner;
232  gboolean ret;
233 
234  ret = gnc_dom_tree_to_owner (node, &owner, pdata->book);
235  if (ret)
236  gncInvoiceSetOwner (pdata->invoice, &owner);
237 
238  return ret;
239 }
240 
241 static gboolean
242 invoice_opened_handler (xmlNodePtr node, gpointer invoice_pdata)
243 {
244  struct invoice_pdata* pdata = static_cast<decltype (pdata)> (invoice_pdata);
245  return set_time64 (node, pdata->invoice, gncInvoiceSetDateOpened);
246 }
247 
248 static gboolean
249 invoice_posted_handler (xmlNodePtr node, gpointer invoice_pdata)
250 {
251  struct invoice_pdata* pdata = static_cast<decltype (pdata)> (invoice_pdata);
252  return set_time64 (node, pdata->invoice, gncInvoiceSetDatePosted);
253 }
254 
255 static gboolean
256 invoice_billing_id_handler (xmlNodePtr node, gpointer invoice_pdata)
257 {
258  struct invoice_pdata* pdata = static_cast<decltype (pdata)> (invoice_pdata);
259 
260  return set_string (node, pdata->invoice, gncInvoiceSetBillingID);
261 }
262 
263 static gboolean
264 invoice_notes_handler (xmlNodePtr node, gpointer invoice_pdata)
265 {
266  struct invoice_pdata* pdata = static_cast<decltype (pdata)> (invoice_pdata);
267 
268  return set_string (node, pdata->invoice, gncInvoiceSetNotes);
269 }
270 
271 static gboolean
272 invoice_active_handler (xmlNodePtr node, gpointer invoice_pdata)
273 {
274  struct invoice_pdata* pdata = static_cast<decltype (pdata)> (invoice_pdata);
275  gint64 val;
276  gboolean ret;
277 
278  ret = dom_tree_to_integer (node, &val);
279  if (ret)
280  gncInvoiceSetActive (pdata->invoice, (gboolean)val);
281 
282  return ret;
283 }
284 
285 static gboolean
286 invoice_terms_handler (xmlNodePtr node, gpointer invoice_pdata)
287 {
288  struct invoice_pdata* pdata = static_cast<decltype (pdata)> (invoice_pdata);
289  GncGUID* guid;
290  GncBillTerm* term;
291 
292  guid = dom_tree_to_guid (node);
293  g_return_val_if_fail (guid, FALSE);
294  term = gnc_billterm_xml_find_or_create (pdata->book, guid);
295  g_assert (term);
296  guid_free (guid);
297  gncInvoiceSetTerms (pdata->invoice, term);
298 
299  return TRUE;
300 }
301 
302 static gboolean
303 invoice_posttxn_handler (xmlNodePtr node, gpointer invoice_pdata)
304 {
305  struct invoice_pdata* pdata = static_cast<decltype (pdata)> (invoice_pdata);
306  GncGUID* guid;
307  Transaction* txn;
308 
309  guid = dom_tree_to_guid (node);
310  g_return_val_if_fail (guid, FALSE);
311  txn = xaccTransLookup (guid, pdata->book);
312  guid_free (guid);
313  g_return_val_if_fail (txn, FALSE);
314 
315  gncInvoiceSetPostedTxn (pdata->invoice, txn);
316  return TRUE;
317 }
318 
319 static gboolean
320 invoice_postlot_handler (xmlNodePtr node, gpointer invoice_pdata)
321 {
322  struct invoice_pdata* pdata = static_cast<decltype (pdata)> (invoice_pdata);
323  GncGUID* guid;
324  GNCLot* lot;
325 
326  guid = dom_tree_to_guid (node);
327  g_return_val_if_fail (guid, FALSE);
328  lot = gnc_lot_lookup (guid, pdata->book);
329  guid_free (guid);
330  g_return_val_if_fail (lot, FALSE);
331 
332  gncInvoiceSetPostedLot (pdata->invoice, lot);
333  return TRUE;
334 }
335 
336 static gboolean
337 invoice_postacc_handler (xmlNodePtr node, gpointer invoice_pdata)
338 {
339  struct invoice_pdata* pdata = static_cast<decltype (pdata)> (invoice_pdata);
340  GncGUID* guid;
341  Account* acc;
342 
343  guid = dom_tree_to_guid (node);
344  g_return_val_if_fail (guid, FALSE);
345  acc = xaccAccountLookup (guid, pdata->book);
346  guid_free (guid);
347  g_return_val_if_fail (acc, FALSE);
348 
349  gncInvoiceSetPostedAcc (pdata->invoice, acc);
350  return TRUE;
351 }
352 
353 static gboolean
354 invoice_currency_handler (xmlNodePtr node, gpointer invoice_pdata)
355 {
356  struct invoice_pdata* pdata = static_cast<decltype (pdata)> (invoice_pdata);
357  gnc_commodity* com;
358 
359  com = dom_tree_to_commodity_ref (node, pdata->book);
360  g_return_val_if_fail (com, FALSE);
361 
362  gncInvoiceSetCurrency (pdata->invoice, com);
363 
364  return TRUE;
365 }
366 
367 static gboolean
368 invoice_billto_handler (xmlNodePtr node, gpointer invoice_pdata)
369 {
370  struct invoice_pdata* pdata = static_cast<decltype (pdata)> (invoice_pdata);
371  GncOwner owner;
372  gboolean ret;
373 
374  ret = gnc_dom_tree_to_owner (node, &owner, pdata->book);
375  if (ret)
376  gncInvoiceSetBillTo (pdata->invoice, &owner);
377 
378  return ret;
379 }
380 
381 static gboolean
382 invoice_tochargeamt_handler (xmlNodePtr node, gpointer invoice_pdata)
383 {
384  struct invoice_pdata* pdata = static_cast<decltype (pdata)> (invoice_pdata);
385 
386  gncInvoiceSetToChargeAmount (pdata->invoice, dom_tree_to_gnc_numeric (node));
387  return TRUE;
388 }
389 
390 static gboolean
391 invoice_slots_handler (xmlNodePtr node, gpointer invoice_pdata)
392 {
393  struct invoice_pdata* pdata = static_cast<decltype (pdata)> (invoice_pdata);
394  return dom_tree_create_instance_slots (node, QOF_INSTANCE (pdata->invoice));
395 }
396 
397 static struct dom_tree_handler invoice_handlers_v2[] =
398 {
399  { invoice_guid_string, invoice_guid_handler, 1, 0 },
400  { invoice_id_string, invoice_id_handler, 1, 0 },
401  { invoice_owner_string, invoice_owner_handler, 1, 0 },
402  { invoice_opened_string, invoice_opened_handler, 1, 0 },
403  { invoice_posted_string, invoice_posted_handler, 0, 0 },
404  { invoice_billing_id_string, invoice_billing_id_handler, 0, 0 },
405  { invoice_notes_string, invoice_notes_handler, 0, 0 },
406  { invoice_active_string, invoice_active_handler, 1, 0 },
407  { invoice_terms_string, invoice_terms_handler, 0, 0 },
408  { invoice_posttxn_string, invoice_posttxn_handler, 0, 0 },
409  { invoice_postlot_string, invoice_postlot_handler, 0, 0 },
410  { invoice_postacc_string, invoice_postacc_handler, 0, 0 },
411  { invoice_currency_string, invoice_currency_handler, 0, 0 },
412  { "invoice:commodity", invoice_currency_handler, 0, 0 },
413  { invoice_billto_string, invoice_billto_handler, 0, 0 },
414  { invoice_tochargeamt_string, invoice_tochargeamt_handler, 0, 0},
415  { invoice_slots_string, invoice_slots_handler, 0, 0 },
416  { NULL, 0, 0, 0 }
417 };
418 
419 static GncInvoice*
420 dom_tree_to_invoice (xmlNodePtr node, QofBook* book)
421 {
423  gboolean successful;
424 
425  invoice_pdata.invoice = gncInvoiceCreate (book);
426  invoice_pdata.book = book;
427  gncInvoiceBeginEdit (invoice_pdata.invoice);
428 
429  successful = dom_tree_generic_parse (node, invoice_handlers_v2,
430  &invoice_pdata);
431 
432  if (successful)
433  gncInvoiceCommitEdit (invoice_pdata.invoice);
434  else
435  {
436  PERR ("failed to parse invoice tree");
437  gncInvoiceDestroy (invoice_pdata.invoice);
438  invoice_pdata.invoice = NULL;
439  }
440 
441  return invoice_pdata.invoice;
442 }
443 
444 static gboolean
445 gnc_invoice_end_handler (gpointer data_for_children,
446  GSList* data_from_children, GSList* sibling_data,
447  gpointer parent_data, gpointer global_data,
448  gpointer* result, const gchar* tag)
449 {
450  GncInvoice* invoice;
451  xmlNodePtr tree = (xmlNodePtr)data_for_children;
452  gxpf_data* gdata = (gxpf_data*)global_data;
453  QofBook* book = static_cast<decltype (book)> (gdata->bookdata);
454 
455  if (parent_data)
456  {
457  return TRUE;
458  }
459 
460  /* OK. For some messed up reason this is getting called again with a
461  NULL tag. So we ignore those cases */
462  if (!tag)
463  {
464  return TRUE;
465  }
466 
467  g_return_val_if_fail (tree, FALSE);
468 
469  invoice = dom_tree_to_invoice (tree, book);
470  if (invoice != NULL)
471  {
472  gdata->cb (tag, gdata->parsedata, invoice);
473  }
474 
475  xmlFreeNode (tree);
476 
477  return invoice != NULL;
478 }
479 
480 static sixtp*
481 invoice_sixtp_parser_create (void)
482 {
483  return sixtp_dom_parser_new (gnc_invoice_end_handler, NULL, NULL);
484 }
485 
486 static gboolean
487 invoice_should_be_saved (GncInvoice* invoice)
488 {
489  const char* id;
490 
491  /* make sure this is a valid invoice before we save it -- should have an ID */
492  id = gncInvoiceGetID (invoice);
493  if (id == NULL || *id == '\0')
494  return FALSE;
495 
496  return TRUE;
497 }
498 
499 static void
500 do_count (QofInstance* invoice_p, gpointer count_p)
501 {
502  int* count = static_cast<decltype (count)> (count_p);
503  if (invoice_should_be_saved ((GncInvoice*)invoice_p))
504  (*count)++;
505 }
506 
507 static int
508 invoice_get_count (QofBook* book)
509 {
510  int count = 0;
511  qof_object_foreach (_GNC_MOD_NAME, book, do_count, (gpointer) &count);
512  return count;
513 }
514 
515 static void
516 xml_add_invoice (QofInstance* invoice_p, gpointer out_p)
517 {
518  xmlNodePtr node;
519  GncInvoice* invoice = (GncInvoice*) invoice_p;
520  FILE* out = static_cast<decltype (out)> (out_p);
521 
522  if (ferror (out))
523  return;
524  if (!invoice_should_be_saved (invoice))
525  return;
526 
527  node = invoice_dom_tree_create (invoice);
528  xmlElemDump (out, NULL, node);
529  xmlFreeNode (node);
530  if (ferror (out) || fprintf (out, "\n") < 0)
531  return;
532 }
533 
534 static gboolean
535 invoice_write (FILE* out, QofBook* book)
536 {
537  qof_object_foreach_sorted (_GNC_MOD_NAME, book, xml_add_invoice,
538  (gpointer) out);
539  return ferror (out) == 0;
540 }
541 
542 static gboolean
543 invoice_ns (FILE* out)
544 {
545  g_return_val_if_fail (out, FALSE);
546  return gnc_xml2_write_namespace_decl (out, "invoice");
547 }
548 
549 void
550 gnc_invoice_xml_initialize (void)
551 {
552  static GncXmlDataType_t be_data =
553  {
554  GNC_FILE_BACKEND_VERS,
555  gnc_invoice_string,
556  invoice_sixtp_parser_create,
557  NULL, /* add_item */
558  invoice_get_count,
559  invoice_write,
560  NULL, /* scrub */
561  invoice_ns,
562  };
563 
564  gnc_xml_register_backend(be_data);
565 }
Definition: sixtp.h:129
const GncGUID * qof_instance_get_guid(gconstpointer inst)
Return the GncGUID of this instance.
STRUCTS.
gboolean gnc_numeric_zero_p(gnc_numeric a)
Returns 1 if the given gnc_numeric is 0 (zero), else returns 0.
#define PERR(format, args...)
Log a serious error.
Definition: qoflog.h:244
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
Transaction * xaccTransLookup(const GncGUID *guid, QofBook *book)
The xaccTransLookup() subroutine will return the transaction associated with the given id...
api for GnuCash version 2 XML-based file format
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
#define xaccTransGetGUID(X)
Definition: Transaction.h:788
gint64 time64
Most systems that are currently maintained, including Microsoft Windows, BSD-derived Unixes and Linux...
Definition: gnc-date.h:87
The type used to store guids in C.
Definition: guid.h:75
Account * xaccAccountLookup(const GncGUID *guid, QofBook *book)
The xaccAccountLookup() subroutine will return the account associated with the given id...
Definition: Account.cpp:2052