30 #include "gncBillTermP.h" 31 #include "gncCustomerP.h" 32 #include "gncTaxTableP.h" 34 #include "gnc-xml-helper.h" 35 #include "gnc-customer-xml-v2.h" 36 #include "gnc-address-xml-v2.h" 37 #include "gnc-bill-term-xml-v2.h" 39 #include "sixtp-utils.h" 40 #include "sixtp-parsers.h" 41 #include "sixtp-utils.h" 42 #include "sixtp-dom-parsers.h" 43 #include "sixtp-dom-generators.h" 46 #include "io-gncxml-gen.h" 49 #include "xml-helpers.h" 51 #define _GNC_MOD_NAME GNC_ID_CUSTOMER 53 static QofLogModule log_module = GNC_MOD_IO;
55 const gchar* customer_version_string =
"2.0.0";
58 #define gnc_customer_string "gnc:GncCustomer" 59 #define cust_name_string "cust:name" 60 #define cust_guid_string "cust:guid" 61 #define cust_id_string "cust:id" 62 #define cust_addr_string "cust:addr" 63 #define cust_shipaddr_string "cust:shipaddr" 64 #define cust_notes_string "cust:notes" 65 #define cust_terms_string "cust:terms" 66 #define cust_taxincluded_string "cust:taxincluded" 67 #define cust_active_string "cust:active" 68 #define cust_discount_string "cust:discount" 69 #define cust_credit_string "cust:credit" 70 #define cust_currency_string "cust:currency" 71 #define cust_taxtable_string "cust:taxtable" 72 #define cust_taxtableoverride_string "cust:use-tt" 73 #define cust_slots_string "cust:slots" 83 ret = xmlNewNode (NULL, BAD_CAST gnc_customer_string);
84 xmlSetProp (ret, BAD_CAST
"version", BAD_CAST customer_version_string);
86 xmlAddChild (ret, guid_to_dom_tree (cust_guid_string,
89 xmlAddChild (ret, text_to_dom_tree (cust_name_string,
90 gncCustomerGetName (cust)));
92 xmlAddChild (ret, text_to_dom_tree (cust_id_string,
93 gncCustomerGetID (cust)));
95 xmlAddChild (ret, gnc_address_to_dom_tree (cust_addr_string,
96 gncCustomerGetAddr (cust)));
98 xmlAddChild (ret, gnc_address_to_dom_tree (cust_shipaddr_string,
99 gncCustomerGetShipAddr (cust)));
101 maybe_add_string (ret, cust_notes_string, gncCustomerGetNotes (cust));
103 term = gncCustomerGetTerms (cust);
105 xmlAddChild (ret, guid_to_dom_tree (cust_terms_string,
108 xmlAddChild (ret, text_to_dom_tree (cust_taxincluded_string,
109 gncTaxIncludedTypeToString (
110 gncCustomerGetTaxIncluded (cust))));
112 xmlAddChild (ret, int_to_dom_tree (cust_active_string,
113 gncCustomerGetActive (cust)));
115 num = gncCustomerGetDiscount (cust);
116 xmlAddChild (ret, gnc_numeric_to_dom_tree (cust_discount_string, &num));
118 num = gncCustomerGetCredit (cust);
119 xmlAddChild (ret, gnc_numeric_to_dom_tree (cust_credit_string, &num));
123 commodity_ref_to_dom_tree (cust_currency_string,
124 gncCustomerGetCurrency (cust)));
126 xmlAddChild (ret, int_to_dom_tree (cust_taxtableoverride_string,
127 gncCustomerGetTaxTableOverride (cust)));
128 taxtable = gncCustomerGetTaxTable (cust);
130 xmlAddChild (ret, guid_to_dom_tree (cust_taxtable_string,
134 xmlAddChild (ret, qof_instance_slots_to_dom_tree (cust_slots_string,
135 QOF_INSTANCE (cust)));
156 ret = dom_tree_to_integer (node, &val);
158 func (cust, (gboolean)val);
164 customer_name_handler (xmlNodePtr node, gpointer cust_pdata)
166 struct customer_pdata* pdata =
static_cast<decltype (pdata)
> (cust_pdata);
168 return apply_xmlnode_text (gncCustomerSetName, pdata->customer, node);
172 customer_guid_handler (xmlNodePtr node, gpointer cust_pdata)
174 struct customer_pdata* pdata =
static_cast<decltype (pdata)
> (cust_pdata);
177 auto guid = dom_tree_to_guid (node);
178 g_return_val_if_fail (guid, FALSE);
179 cust = gncCustomerLookup (pdata->book, &*guid);
182 gncCustomerDestroy (pdata->customer);
183 pdata->customer = cust;
184 gncCustomerBeginEdit (cust);
188 gncCustomerSetGUID (pdata->customer, &*guid);
195 customer_id_handler (xmlNodePtr node, gpointer cust_pdata)
197 struct customer_pdata* pdata =
static_cast<decltype (pdata)
> (cust_pdata);
199 return apply_xmlnode_text (gncCustomerSetID, pdata->customer, node);
203 customer_notes_handler (xmlNodePtr node, gpointer cust_pdata)
205 struct customer_pdata* pdata =
static_cast<decltype (pdata)
> (cust_pdata);
207 return apply_xmlnode_text (gncCustomerSetNotes, pdata->customer, node);
211 customer_terms_handler (xmlNodePtr node, gpointer cust_pdata)
213 struct customer_pdata* pdata =
static_cast<decltype (pdata)
> (cust_pdata);
216 auto guid = dom_tree_to_guid (node);
217 g_return_val_if_fail (guid, FALSE);
218 term = gnc_billterm_xml_find_or_create (pdata->book, &*guid);
220 gncCustomerSetTerms (pdata->customer, term);
226 customer_addr_handler (xmlNodePtr node, gpointer cust_pdata)
228 struct customer_pdata* pdata =
static_cast<decltype (pdata)
> (cust_pdata);
230 return gnc_dom_tree_to_address (node, gncCustomerGetAddr (pdata->customer));
234 customer_shipaddr_handler (xmlNodePtr node, gpointer cust_pdata)
236 struct customer_pdata* pdata =
static_cast<decltype (pdata)
> (cust_pdata);
238 return gnc_dom_tree_to_address (node,
239 gncCustomerGetShipAddr (pdata->customer));
244 customer_taxincluded_handler (xmlNodePtr node, gpointer cust_pdata)
246 struct customer_pdata* pdata =
static_cast<decltype (pdata)
> (cust_pdata);
247 auto set_tax_included = [](
GncCustomer* cust,
const char *str)
250 if (gncTaxIncludedStringToType (str, &type))
251 gncCustomerSetTaxIncluded (cust, type);
253 return apply_xmlnode_text (set_tax_included, pdata->customer, node);
257 customer_active_handler (xmlNodePtr node, gpointer cust_pdata)
259 struct customer_pdata* pdata =
static_cast<decltype (pdata)
> (cust_pdata);
260 return set_boolean (node, pdata->customer, gncCustomerSetActive);
264 customer_discount_handler (xmlNodePtr node, gpointer cust_pdata)
266 struct customer_pdata* pdata =
static_cast<decltype (pdata)
> (cust_pdata);
268 gncCustomerSetDiscount (pdata->customer, dom_tree_to_gnc_numeric (node));
273 customer_credit_handler (xmlNodePtr node, gpointer cust_pdata)
275 struct customer_pdata* pdata =
static_cast<decltype (pdata)
> (cust_pdata);
277 gncCustomerSetCredit (pdata->customer, dom_tree_to_gnc_numeric (node));
282 customer_currency_handler (xmlNodePtr node, gpointer
customer_pdata)
287 com = dom_tree_to_commodity_ref (node, pdata->book);
288 g_return_val_if_fail (com, FALSE);
290 gncCustomerSetCurrency (pdata->customer, com);
296 customer_taxtable_handler (xmlNodePtr node, gpointer cust_pdata)
298 struct customer_pdata* pdata =
static_cast<decltype (pdata)
> (cust_pdata);
301 auto guid = dom_tree_to_guid (node);
302 g_return_val_if_fail (guid, FALSE);
303 taxtable = gncTaxTableLookup (pdata->book, &*guid);
306 taxtable = gncTaxTableCreate (pdata->book);
307 gncTaxTableBeginEdit (taxtable);
308 gncTaxTableSetGUID (taxtable, &*guid);
309 gncTaxTableCommitEdit (taxtable);
312 gncTaxTableDecRef (taxtable);
314 gncCustomerSetTaxTable (pdata->customer, taxtable);
319 customer_taxtableoverride_handler (xmlNodePtr node, gpointer cust_pdata)
321 struct customer_pdata* pdata =
static_cast<decltype (pdata)
> (cust_pdata);
322 return set_boolean (node, pdata->customer, gncCustomerSetTaxTableOverride);
326 customer_slots_handler (xmlNodePtr node, gpointer cust_pdata)
328 struct customer_pdata* pdata =
static_cast<decltype (pdata)
> (cust_pdata);
329 return dom_tree_create_instance_slots (node, QOF_INSTANCE (pdata->customer));
334 { cust_name_string, customer_name_handler, 1, 0 },
335 { cust_guid_string, customer_guid_handler, 1, 0 },
336 { cust_id_string, customer_id_handler, 1, 0 },
337 { cust_addr_string, customer_addr_handler, 1, 0 },
338 { cust_shipaddr_string, customer_shipaddr_handler, 1, 0 },
339 { cust_notes_string, customer_notes_handler, 0, 0 },
340 { cust_terms_string, customer_terms_handler, 0, 0 },
341 { cust_taxincluded_string, customer_taxincluded_handler, 1, 0 },
342 { cust_active_string, customer_active_handler, 1, 0 },
343 { cust_discount_string, customer_discount_handler, 1, 0 },
344 { cust_credit_string, customer_credit_handler, 1, 0 },
345 { cust_currency_string, customer_currency_handler, 0, 0 },
346 {
"cust:commodity", customer_currency_handler, 0, 0 },
347 { cust_taxtable_string, customer_taxtable_handler, 0, 0 },
348 { cust_taxtableoverride_string, customer_taxtableoverride_handler, 0, 0 },
349 { cust_slots_string, customer_slots_handler, 0, 0 },
354 dom_tree_to_customer (xmlNodePtr node, QofBook* book)
359 cust_pdata.customer = gncCustomerCreate (book);
360 cust_pdata.book = book;
361 gncCustomerBeginEdit (cust_pdata.customer);
363 successful = dom_tree_generic_parse (node, customer_handlers_v2,
367 gncCustomerCommitEdit (cust_pdata.customer);
370 PERR (
"failed to parse customer tree");
371 gncCustomerDestroy (cust_pdata.customer);
372 cust_pdata.customer = NULL;
375 return cust_pdata.customer;
379 gnc_customer_end_handler (gpointer data_for_children,
380 GSList* data_from_children, GSList* sibling_data,
381 gpointer parent_data, gpointer global_data,
382 gpointer* result,
const gchar* tag)
385 xmlNodePtr tree = (xmlNodePtr)data_for_children;
386 gxpf_data* gdata = (gxpf_data*)global_data;
387 QofBook* book =
static_cast<decltype (book)
> (gdata->bookdata);
402 g_return_val_if_fail (tree, FALSE);
404 cust = dom_tree_to_customer (tree, book);
407 gdata->cb (tag, gdata->parsedata, cust);
416 customer_sixtp_parser_create (
void)
418 return sixtp_dom_parser_new (gnc_customer_end_handler, NULL, NULL);
427 id = gncCustomerGetID (customer);
428 if (
id == NULL || *
id ==
'\0')
437 int* count =
static_cast<decltype (count)
> (count_p);
438 if (customer_should_be_saved ((
GncCustomer*)cust_p))
443 customer_get_count (QofBook* book)
451 xml_add_customer (
QofInstance* cust_p, gpointer out_p)
455 FILE* out =
static_cast<decltype (out)
> (out_p);
459 if (!customer_should_be_saved (cust))
462 node = customer_dom_tree_create (cust);
463 xmlElemDump (out, NULL, node);
465 if (ferror (out) || fprintf (out,
"\n") < 0)
470 customer_write (FILE* out, QofBook* book)
474 return ferror (out) == 0;
478 customer_ns (FILE* out)
480 g_return_val_if_fail (out, FALSE);
481 return gnc_xml2_write_namespace_decl (out,
"cust");
485 gnc_customer_xml_initialize (
void)
487 static GncXmlDataType_t be_data =
489 GNC_FILE_BACKEND_VERS,
491 customer_sixtp_parser_create,
499 gnc_xml_register_backend (be_data);
const GncGUID * qof_instance_get_guid(gconstpointer inst)
Return the GncGUID of this instance.
GncTaxIncluded
How to interpret the TaxIncluded.
#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.
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.
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.
modtime is the internal date of the last modtime See libgnucash/engine/TaxTableBillTermImmutability.txt for an explanation of the following Code that handles refcount, parent, child, invisible and children is identical to that in ::GncBillTerm