30 #include "gncBillTermP.h" 31 #include "gncInvoiceP.h" 33 #include "gnc-xml-helper.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" 45 #include "io-gncxml-gen.h" 48 #define _GNC_MOD_NAME GNC_ID_INVOICE 50 static QofLogModule log_module = GNC_MOD_IO;
52 const gchar* invoice_version_string =
"2.0.0";
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" 74 maybe_add_string (xmlNodePtr ptr,
const char* tag,
const char* str)
77 xmlAddChild (ptr, text_to_dom_tree (tag, str));
81 maybe_add_time64 (xmlNodePtr ptr,
const char* tag,
time64 time)
83 if (time != INT64_MAX)
84 xmlAddChild (ptr, time64_to_dom_tree (tag, time));
88 invoice_dom_tree_create (GncInvoice* invoice)
99 ret = xmlNewNode (NULL, BAD_CAST gnc_invoice_string);
100 xmlSetProp (ret, BAD_CAST
"version", BAD_CAST invoice_version_string);
102 xmlAddChild (ret, guid_to_dom_tree (invoice_guid_string,
105 xmlAddChild (ret, text_to_dom_tree (invoice_id_string,
106 gncInvoiceGetID (invoice)));
108 xmlAddChild (ret, gnc_owner_to_dom_tree (invoice_owner_string,
109 gncInvoiceGetOwner (invoice)));
111 time = gncInvoiceGetDateOpened (invoice);
112 xmlAddChild (ret, time64_to_dom_tree (invoice_opened_string, time));
114 maybe_add_time64 (ret, invoice_posted_string, gncInvoiceGetDatePosted (invoice));
116 term = gncInvoiceGetTerms (invoice);
118 xmlAddChild (ret, guid_to_dom_tree (invoice_terms_string,
121 maybe_add_string (ret, invoice_billing_id_string,
122 gncInvoiceGetBillingID (invoice));
123 maybe_add_string (ret, invoice_notes_string, gncInvoiceGetNotes (invoice));
125 xmlAddChild (ret, int_to_dom_tree (invoice_active_string,
126 gncInvoiceGetActive (invoice)));
128 txn = gncInvoiceGetPostedTxn (invoice);
130 xmlAddChild (ret, guid_to_dom_tree (invoice_posttxn_string,
133 lot = gncInvoiceGetPostedLot (invoice);
135 xmlAddChild (ret, guid_to_dom_tree (invoice_postlot_string,
136 gnc_lot_get_guid (lot)));
138 acc = gncInvoiceGetPostedAcc (invoice);
140 xmlAddChild (ret, guid_to_dom_tree (invoice_postacc_string,
145 commodity_ref_to_dom_tree (invoice_currency_string,
146 gncInvoiceGetCurrency (invoice)));
148 billto = gncInvoiceGetBillTo (invoice);
149 if (billto && billto->owner.undefined != NULL)
150 xmlAddChild (ret, gnc_owner_to_dom_tree (invoice_billto_string, billto));
152 amt = gncInvoiceGetToChargeAmount (invoice);
154 xmlAddChild (ret, gnc_numeric_to_dom_tree (invoice_tochargeamt_string, &amt));
157 xmlAddChild (ret, qof_instance_slots_to_dom_tree (invoice_slots_string,
158 QOF_INSTANCE (invoice)));
171 static inline gboolean
172 set_time64 (xmlNodePtr node, GncInvoice* invoice,
173 void (*func) (GncInvoice* invoice,
time64 time))
175 time64 time = dom_tree_to_time64 (node);
176 if (!dom_tree_valid_time64 (time, node->name)) time = 0;
177 func (invoice, time);
182 invoice_guid_handler (xmlNodePtr node, gpointer
invoice_pdata)
187 auto guid = dom_tree_to_guid (node);
188 g_return_val_if_fail (guid, FALSE);
189 invoice = gncInvoiceLookup (pdata->book, &*guid);
192 gncInvoiceDestroy (pdata->invoice);
193 pdata->invoice = invoice;
194 gncInvoiceBeginEdit (invoice);
198 gncInvoiceSetGUID (pdata->invoice, &*guid);
209 return apply_xmlnode_text (gncInvoiceSetID, pdata->invoice, node);
213 invoice_owner_handler (xmlNodePtr node, gpointer
invoice_pdata)
219 ret = gnc_dom_tree_to_owner (node, &owner, pdata->book);
221 gncInvoiceSetOwner (pdata->invoice, &owner);
227 invoice_opened_handler (xmlNodePtr node, gpointer
invoice_pdata)
230 return set_time64 (node, pdata->invoice, gncInvoiceSetDateOpened);
234 invoice_posted_handler (xmlNodePtr node, gpointer
invoice_pdata)
237 return set_time64 (node, pdata->invoice, gncInvoiceSetDatePosted);
241 invoice_billing_id_handler (xmlNodePtr node, gpointer
invoice_pdata)
245 return apply_xmlnode_text (gncInvoiceSetBillingID, pdata->invoice, node);
249 invoice_notes_handler (xmlNodePtr node, gpointer
invoice_pdata)
253 return apply_xmlnode_text (gncInvoiceSetNotes, pdata->invoice, node);
257 invoice_active_handler (xmlNodePtr node, gpointer
invoice_pdata)
263 ret = dom_tree_to_integer (node, &val);
265 gncInvoiceSetActive (pdata->invoice, (gboolean)val);
271 invoice_terms_handler (xmlNodePtr node, gpointer
invoice_pdata)
276 auto guid = dom_tree_to_guid (node);
277 g_return_val_if_fail (guid, FALSE);
278 term = gnc_billterm_xml_find_or_create (pdata->book, &*guid);
280 gncInvoiceSetTerms (pdata->invoice, term);
286 invoice_posttxn_handler (xmlNodePtr node, gpointer
invoice_pdata)
291 auto guid = dom_tree_to_guid (node);
292 g_return_val_if_fail (guid, FALSE);
294 g_return_val_if_fail (txn, FALSE);
296 gncInvoiceSetPostedTxn (pdata->invoice, txn);
301 invoice_postlot_handler (xmlNodePtr node, gpointer
invoice_pdata)
306 auto guid = dom_tree_to_guid (node);
307 g_return_val_if_fail (guid, FALSE);
308 lot = gnc_lot_lookup (&*guid, pdata->book);
309 g_return_val_if_fail (lot, FALSE);
311 gncInvoiceSetPostedLot (pdata->invoice, lot);
316 invoice_postacc_handler (xmlNodePtr node, gpointer
invoice_pdata)
321 auto guid = dom_tree_to_guid (node);
322 g_return_val_if_fail (guid, FALSE);
324 g_return_val_if_fail (acc, FALSE);
326 gncInvoiceSetPostedAcc (pdata->invoice, acc);
331 invoice_currency_handler (xmlNodePtr node, gpointer
invoice_pdata)
336 com = dom_tree_to_commodity_ref (node, pdata->book);
337 g_return_val_if_fail (com, FALSE);
339 gncInvoiceSetCurrency (pdata->invoice, com);
345 invoice_billto_handler (xmlNodePtr node, gpointer
invoice_pdata)
351 ret = gnc_dom_tree_to_owner (node, &owner, pdata->book);
353 gncInvoiceSetBillTo (pdata->invoice, &owner);
359 invoice_tochargeamt_handler (xmlNodePtr node, gpointer
invoice_pdata)
363 gncInvoiceSetToChargeAmount (pdata->invoice, dom_tree_to_gnc_numeric (node));
368 invoice_slots_handler (xmlNodePtr node, gpointer
invoice_pdata)
371 return dom_tree_create_instance_slots (node, QOF_INSTANCE (pdata->invoice));
376 { invoice_guid_string, invoice_guid_handler, 1, 0 },
377 { invoice_id_string, invoice_id_handler, 1, 0 },
378 { invoice_owner_string, invoice_owner_handler, 1, 0 },
379 { invoice_opened_string, invoice_opened_handler, 1, 0 },
380 { invoice_posted_string, invoice_posted_handler, 0, 0 },
381 { invoice_billing_id_string, invoice_billing_id_handler, 0, 0 },
382 { invoice_notes_string, invoice_notes_handler, 0, 0 },
383 { invoice_active_string, invoice_active_handler, 1, 0 },
384 { invoice_terms_string, invoice_terms_handler, 0, 0 },
385 { invoice_posttxn_string, invoice_posttxn_handler, 0, 0 },
386 { invoice_postlot_string, invoice_postlot_handler, 0, 0 },
387 { invoice_postacc_string, invoice_postacc_handler, 0, 0 },
388 { invoice_currency_string, invoice_currency_handler, 0, 0 },
389 {
"invoice:commodity", invoice_currency_handler, 0, 0 },
390 { invoice_billto_string, invoice_billto_handler, 0, 0 },
391 { invoice_tochargeamt_string, invoice_tochargeamt_handler, 0, 0},
392 { invoice_slots_string, invoice_slots_handler, 0, 0 },
397 dom_tree_to_invoice (xmlNodePtr node, QofBook* book)
406 successful = dom_tree_generic_parse (node, invoice_handlers_v2,
413 PERR (
"failed to parse invoice tree");
422 gnc_invoice_end_handler (gpointer data_for_children,
423 GSList* data_from_children, GSList* sibling_data,
424 gpointer parent_data, gpointer global_data,
425 gpointer* result,
const gchar* tag)
428 xmlNodePtr tree = (xmlNodePtr)data_for_children;
429 gxpf_data* gdata = (gxpf_data*)global_data;
430 QofBook* book =
static_cast<decltype (book)
> (gdata->bookdata);
444 g_return_val_if_fail (tree, FALSE);
446 invoice = dom_tree_to_invoice (tree, book);
449 gdata->cb (tag, gdata->parsedata, invoice);
454 return invoice != NULL;
458 invoice_sixtp_parser_create (
void)
460 return sixtp_dom_parser_new (gnc_invoice_end_handler, NULL, NULL);
464 invoice_should_be_saved (GncInvoice* invoice)
469 id = gncInvoiceGetID (invoice);
470 if (
id == NULL || *
id ==
'\0')
477 do_count (
QofInstance* invoice_p, gpointer count_p)
479 int* count =
static_cast<decltype (count)
> (count_p);
480 if (invoice_should_be_saved ((GncInvoice*)invoice_p))
485 invoice_get_count (QofBook* book)
493 xml_add_invoice (
QofInstance* invoice_p, gpointer out_p)
496 GncInvoice* invoice = (GncInvoice*) invoice_p;
497 FILE* out =
static_cast<decltype (out)
> (out_p);
501 if (!invoice_should_be_saved (invoice))
504 node = invoice_dom_tree_create (invoice);
505 xmlElemDump (out, NULL, node);
507 if (ferror (out) || fprintf (out,
"\n") < 0)
512 invoice_write (FILE* out, QofBook* book)
516 return ferror (out) == 0;
520 invoice_ns (FILE* out)
522 g_return_val_if_fail (out, FALSE);
523 return gnc_xml2_write_namespace_decl (out,
"invoice");
527 gnc_invoice_xml_initialize (
void)
529 static GncXmlDataType_t be_data =
531 GNC_FILE_BACKEND_VERS,
533 invoice_sixtp_parser_create,
541 gnc_xml_register_backend(be_data);
const GncGUID * qof_instance_get_guid(gconstpointer inst)
Return the GncGUID of this instance.
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.
void qof_object_foreach_sorted(QofIdTypeConst type_name, QofBook *book, QofInstanceForeachCB cb, gpointer user_data)
Invoke callback 'cb' on each instance in guid orted order.
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 'cb' on every instance ov a particular object type.
#define xaccTransGetGUID(X)
gint64 time64
Most systems that are currently maintained, including Microsoft Windows, BSD-derived Unixes and Linux...
Account * xaccAccountLookup(const GncGUID *guid, QofBook *book)
The xaccAccountLookup() subroutine will return the account associated with the given id...