Branch data Line data Source code
1 : : /********************************************************************\
2 : : * gnc-entry-xml-v2.c -- entry 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 "gncEntryP.h"
31 : : #include "gncOrderP.h"
32 : : #include "gncInvoiceP.h"
33 : : #include "gncTaxTableP.h"
34 : :
35 : : #include "gnc-xml-helper.h"
36 : : #include "sixtp.h"
37 : : #include "sixtp-utils.h"
38 : : #include "sixtp-parsers.h"
39 : : #include "sixtp-utils.h"
40 : : #include "sixtp-dom-parsers.h"
41 : : #include "sixtp-dom-generators.h"
42 : :
43 : : #include "gnc-xml.h"
44 : : #include "io-gncxml-gen.h"
45 : : #include "io-gncxml-v2.h"
46 : : #include "gnc-entry-xml-v2.h"
47 : : #include "gnc-owner-xml-v2.h"
48 : :
49 : : #define _GNC_MOD_NAME GNC_ID_ENTRY
50 : :
51 : : static QofLogModule log_module = GNC_MOD_IO;
52 : :
53 : : const gchar* entry_version_string = "2.0.0";
54 : :
55 : : /* ids */
56 : : #define gnc_entry_string "gnc:GncEntry"
57 : : #define entry_guid_string "entry:guid"
58 : : #define entry_date_string "entry:date"
59 : : #define entry_dateentered_string "entry:entered"
60 : : #define entry_description_string "entry:description"
61 : : #define entry_action_string "entry:action"
62 : : #define entry_notes_string "entry:notes"
63 : : #define entry_qty_string "entry:qty"
64 : :
65 : : /* cust inv */
66 : : #define entry_invacct_string "entry:i-acct"
67 : : #define entry_iprice_string "entry:i-price"
68 : : #define entry_idiscount_string "entry:i-discount"
69 : : #define entry_idisctype_string "entry:i-disc-type"
70 : : #define entry_idischow_string "entry:i-disc-how"
71 : : #define entry_itaxable_string "entry:i-taxable"
72 : : #define entry_itaxincluded_string "entry:i-taxincluded"
73 : : #define entry_itaxtable_string "entry:i-taxtable"
74 : :
75 : : /* vend bill */
76 : : #define entry_billacct_string "entry:b-acct"
77 : : #define entry_bprice_string "entry:b-price"
78 : : #define entry_btaxable_string "entry:b-taxable"
79 : : #define entry_btaxincluded_string "entry:b-taxincluded"
80 : : #define entry_btaxtable_string "entry:b-taxtable"
81 : : #define entry_billable_string "entry:billable"
82 : : #define entry_billto_string "entry:billto"
83 : :
84 : : /* emp bill */
85 : : #define entry_billpayment_string "entry:b-pay"
86 : :
87 : : /* other stuff */
88 : : #define entry_order_string "entry:order"
89 : : #define entry_invoice_string "entry:invoice"
90 : : #define entry_bill_string "entry:bill"
91 : : #define entry_slots_string "entry:slots"
92 : :
93 : : static void
94 : 8 : maybe_add_string (xmlNodePtr ptr, const char* tag, const char* str)
95 : : {
96 : 8 : if (str && *str)
97 : 6 : xmlAddChild (ptr, text_to_dom_tree (tag, str));
98 : 8 : }
99 : :
100 : : static void
101 : 8 : maybe_add_numeric (xmlNodePtr ptr, const char* tag, gnc_numeric num)
102 : : {
103 : 8 : if (!gnc_numeric_zero_p (num))
104 : 4 : xmlAddChild (ptr, gnc_numeric_to_dom_tree (tag, &num));
105 : 8 : }
106 : :
107 : : static xmlNodePtr
108 : 2 : entry_dom_tree_create (GncEntry* entry)
109 : : {
110 : : xmlNodePtr ret;
111 : : Account* acc;
112 : : GncTaxTable* taxtable;
113 : : GncOrder* order;
114 : : GncInvoice* invoice;
115 : :
116 : 2 : ret = xmlNewNode (NULL, BAD_CAST gnc_entry_string);
117 : 2 : xmlSetProp (ret, BAD_CAST "version", BAD_CAST entry_version_string);
118 : :
119 : 2 : xmlAddChild (ret, guid_to_dom_tree (entry_guid_string,
120 : 2 : qof_instance_get_guid (QOF_INSTANCE (entry))));
121 : :
122 : 2 : auto time = gncEntryGetDate (entry);
123 : 2 : xmlAddChild (ret, time64_to_dom_tree (entry_date_string, time));
124 : :
125 : 2 : time = gncEntryGetDateEntered (entry);
126 : 2 : xmlAddChild (ret, time64_to_dom_tree (entry_dateentered_string, time));
127 : :
128 : 2 : maybe_add_string (ret, entry_description_string,
129 : : gncEntryGetDescription (entry));
130 : 2 : maybe_add_string (ret, entry_action_string, gncEntryGetAction (entry));
131 : 2 : maybe_add_string (ret, entry_notes_string, gncEntryGetNotes (entry));
132 : :
133 : 2 : maybe_add_numeric (ret, entry_qty_string, gncEntryGetQuantity (entry));
134 : :
135 : : /* cust invoice */
136 : :
137 : 2 : acc = gncEntryGetInvAccount (entry);
138 : 2 : if (acc)
139 : 0 : xmlAddChild (ret, guid_to_dom_tree (entry_invacct_string,
140 : 0 : qof_instance_get_guid (QOF_INSTANCE (acc))));
141 : :
142 : 2 : maybe_add_numeric (ret, entry_iprice_string, gncEntryGetInvPrice (entry));
143 : :
144 : 2 : maybe_add_numeric (ret, entry_idiscount_string,
145 : : gncEntryGetInvDiscount (entry));
146 : :
147 : 2 : invoice = gncEntryGetInvoice (entry);
148 : 2 : if (invoice)
149 : : {
150 : 0 : xmlAddChild (ret, guid_to_dom_tree (entry_invoice_string,
151 : 0 : qof_instance_get_guid (QOF_INSTANCE (invoice))));
152 : :
153 : 0 : xmlAddChild (ret, text_to_dom_tree (entry_idisctype_string,
154 : : gncAmountTypeToString (
155 : : gncEntryGetInvDiscountType (entry))));
156 : 0 : xmlAddChild (ret, text_to_dom_tree (entry_idischow_string,
157 : : gncEntryDiscountHowToString (
158 : : gncEntryGetInvDiscountHow (entry))));
159 : :
160 : 0 : xmlAddChild (ret, int_to_dom_tree (entry_itaxable_string,
161 : 0 : gncEntryGetInvTaxable (entry)));
162 : 0 : xmlAddChild (ret, int_to_dom_tree (entry_itaxincluded_string,
163 : 0 : gncEntryGetInvTaxIncluded (entry)));
164 : : }
165 : :
166 : 2 : taxtable = gncEntryGetInvTaxTable (entry);
167 : 2 : if (taxtable)
168 : 0 : xmlAddChild (ret, guid_to_dom_tree (entry_itaxtable_string,
169 : 0 : qof_instance_get_guid (QOF_INSTANCE (taxtable))));
170 : :
171 : : /* vendor bills */
172 : :
173 : 2 : acc = gncEntryGetBillAccount (entry);
174 : 2 : if (acc)
175 : 2 : xmlAddChild (ret, guid_to_dom_tree (entry_billacct_string,
176 : 2 : qof_instance_get_guid (QOF_INSTANCE (acc))));
177 : :
178 : 2 : maybe_add_numeric (ret, entry_bprice_string, gncEntryGetBillPrice (entry));
179 : :
180 : 2 : invoice = gncEntryGetBill (entry);
181 : 2 : if (invoice)
182 : : {
183 : : GncOwner* owner;
184 : 2 : xmlAddChild (ret, guid_to_dom_tree (entry_bill_string,
185 : 2 : qof_instance_get_guid (QOF_INSTANCE (invoice))));
186 : 2 : xmlAddChild (ret, int_to_dom_tree (entry_billable_string,
187 : 2 : gncEntryGetBillable (entry)));
188 : 2 : owner = gncEntryGetBillTo (entry);
189 : 2 : if (owner && owner->owner.undefined != NULL)
190 : 0 : xmlAddChild (ret, gnc_owner_to_dom_tree (entry_billto_string, owner));
191 : :
192 : 2 : xmlAddChild (ret, int_to_dom_tree (entry_btaxable_string,
193 : 2 : gncEntryGetBillTaxable (entry)));
194 : 2 : xmlAddChild (ret, int_to_dom_tree (entry_btaxincluded_string,
195 : 2 : gncEntryGetBillTaxIncluded (entry)));
196 : 2 : maybe_add_string (ret, entry_billpayment_string,
197 : : gncEntryPaymentTypeToString (gncEntryGetBillPayment (entry)));
198 : : }
199 : :
200 : 2 : taxtable = gncEntryGetBillTaxTable (entry);
201 : 2 : if (taxtable)
202 : 0 : xmlAddChild (ret, guid_to_dom_tree (entry_btaxtable_string,
203 : 0 : qof_instance_get_guid (QOF_INSTANCE (taxtable))));
204 : :
205 : : /* Other stuff */
206 : :
207 : 2 : order = gncEntryGetOrder (entry);
208 : 2 : if (order)
209 : 0 : xmlAddChild (ret, guid_to_dom_tree (entry_order_string,
210 : 0 : qof_instance_get_guid (QOF_INSTANCE (order))));
211 : :
212 : : /* xmlAddChild won't do anything with a NULL, so tests are superfluous. */
213 : 2 : xmlAddChild (ret, qof_instance_slots_to_dom_tree (entry_slots_string,
214 : 2 : QOF_INSTANCE (entry)));
215 : 2 : return ret;
216 : : }
217 : :
218 : : /***********************************************************************/
219 : :
220 : : struct entry_pdata
221 : : {
222 : : GncEntry* entry;
223 : : QofBook* book;
224 : : Account* acc;
225 : : };
226 : :
227 : : static inline gboolean
228 : 4 : set_string (xmlNodePtr node, GncEntry* entry,
229 : : void (*func) (GncEntry* entry, const char* txt))
230 : : {
231 : 4 : char* txt = dom_tree_to_text (node);
232 : 4 : g_return_val_if_fail (txt, FALSE);
233 : :
234 : 4 : func (entry, txt);
235 : 4 : g_free (txt);
236 : 4 : return TRUE;
237 : : }
238 : :
239 : : static inline gboolean
240 : 6 : set_time64 (xmlNodePtr node, GncEntry* entry,
241 : : void (*func) (GncEntry* entry, time64 ts))
242 : : {
243 : 6 : time64 time = dom_tree_to_time64 (node);
244 : 6 : if (!dom_tree_valid_time64 (time, node->name)) time = 0;
245 : 6 : func (entry, time);
246 : 6 : return TRUE;
247 : : }
248 : :
249 : : static inline gboolean
250 : 4 : set_numeric (xmlNodePtr node, GncEntry* entry,
251 : : void (*func) (GncEntry* entry, gnc_numeric num))
252 : : {
253 : 4 : func (entry, dom_tree_to_gnc_numeric (node));
254 : 4 : return TRUE;
255 : : }
256 : :
257 : : static inline gboolean
258 : 9 : set_boolean (xmlNodePtr node, GncEntry* entry,
259 : : void (*func) (GncEntry* entry, gboolean val))
260 : : {
261 : : gint64 val;
262 : :
263 : 9 : if (!dom_tree_to_integer (node, &val))
264 : 0 : return FALSE;
265 : 9 : func (entry, (gboolean)val);
266 : 9 : return TRUE;
267 : : }
268 : :
269 : : static inline gboolean
270 : 2 : set_account (xmlNodePtr node, struct entry_pdata* pdata,
271 : : void (*func) (GncEntry* entry, Account* acc))
272 : : {
273 : : GncGUID* guid;
274 : : Account* acc;
275 : :
276 : 2 : guid = dom_tree_to_guid (node);
277 : 2 : g_return_val_if_fail (guid, FALSE);
278 : 2 : acc = xaccAccountLookup (guid, pdata->book);
279 : 2 : guid_free (guid);
280 : 2 : g_return_val_if_fail (acc, FALSE);
281 : :
282 : 2 : if (func)
283 : 2 : func (pdata->entry, acc);
284 : : else
285 : 0 : pdata->acc = acc;
286 : 2 : return TRUE;
287 : : }
288 : :
289 : : static inline gboolean
290 : 0 : set_taxtable (xmlNodePtr node, struct entry_pdata* pdata,
291 : : void (*func) (GncEntry* entry, GncTaxTable* taxtable))
292 : : {
293 : : GncGUID* guid;
294 : : GncTaxTable* taxtable;
295 : :
296 : 0 : guid = dom_tree_to_guid (node);
297 : 0 : g_return_val_if_fail (guid, FALSE);
298 : 0 : taxtable = gncTaxTableLookup (pdata->book, guid);
299 : 0 : if (!taxtable)
300 : : {
301 : 0 : taxtable = gncTaxTableCreate (pdata->book);
302 : 0 : gncTaxTableBeginEdit (taxtable);
303 : 0 : gncTaxTableSetGUID (taxtable, guid);
304 : 0 : gncTaxTableCommitEdit (taxtable);
305 : : }
306 : : else
307 : 0 : gncTaxTableDecRef (taxtable);
308 : :
309 : 0 : func (pdata->entry, taxtable);
310 : 0 : guid_free (guid);
311 : 0 : return TRUE;
312 : : }
313 : :
314 : : static gboolean
315 : 3 : entry_guid_handler (xmlNodePtr node, gpointer entry_pdata)
316 : : {
317 : 3 : struct entry_pdata* pdata = static_cast<decltype (pdata)> (entry_pdata);
318 : : GncGUID* guid;
319 : : GncEntry* entry;
320 : :
321 : 3 : guid = dom_tree_to_guid (node);
322 : 3 : g_return_val_if_fail (guid, FALSE);
323 : 3 : entry = gncEntryLookup (pdata->book, guid);
324 : 3 : if (entry)
325 : : {
326 : 0 : gncEntryDestroy (pdata->entry);
327 : 0 : pdata->entry = entry;
328 : 0 : gncEntryBeginEdit (entry);
329 : : }
330 : : else
331 : : {
332 : 3 : gncEntrySetGUID (pdata->entry, guid);
333 : : }
334 : :
335 : 3 : guid_free (guid);
336 : :
337 : 3 : return TRUE;
338 : : }
339 : :
340 : : static gboolean
341 : 3 : entry_date_handler (xmlNodePtr node, gpointer entry_pdata)
342 : : {
343 : 3 : struct entry_pdata* pdata = static_cast<decltype (pdata)> (entry_pdata);
344 : 3 : return set_time64 (node, pdata->entry, gncEntrySetDate);
345 : : }
346 : :
347 : : static gboolean
348 : 3 : entry_dateentered_handler (xmlNodePtr node, gpointer entry_pdata)
349 : : {
350 : 3 : struct entry_pdata* pdata = static_cast<decltype (pdata)> (entry_pdata);
351 : 3 : return set_time64 (node, pdata->entry, gncEntrySetDateEntered);
352 : : }
353 : :
354 : : static gboolean
355 : 2 : entry_description_handler (xmlNodePtr node, gpointer entry_pdata)
356 : : {
357 : 2 : struct entry_pdata* pdata = static_cast<decltype (pdata)> (entry_pdata);
358 : :
359 : 2 : return set_string (node, pdata->entry, gncEntrySetDescription);
360 : : }
361 : :
362 : : static gboolean
363 : 2 : entry_action_handler (xmlNodePtr node, gpointer entry_pdata)
364 : : {
365 : 2 : struct entry_pdata* pdata = static_cast<decltype (pdata)> (entry_pdata);
366 : :
367 : 2 : return set_string (node, pdata->entry, gncEntrySetAction);
368 : : }
369 : :
370 : : static gboolean
371 : 0 : entry_notes_handler (xmlNodePtr node, gpointer entry_pdata)
372 : : {
373 : 0 : struct entry_pdata* pdata = static_cast<decltype (pdata)> (entry_pdata);
374 : :
375 : 0 : return set_string (node, pdata->entry, gncEntrySetNotes);
376 : : }
377 : :
378 : : static gboolean
379 : 2 : entry_qty_handler (xmlNodePtr node, gpointer entry_pdata)
380 : : {
381 : 2 : struct entry_pdata* pdata = static_cast<decltype (pdata)> (entry_pdata);
382 : :
383 : 2 : return set_numeric (node, pdata->entry, gncEntrySetQuantity);
384 : : }
385 : :
386 : : /* Cust invoice */
387 : :
388 : : static gboolean
389 : 0 : entry_invacct_handler (xmlNodePtr node, gpointer entry_pdata)
390 : : {
391 : 0 : struct entry_pdata* pdata = static_cast<decltype (pdata)> (entry_pdata);
392 : 0 : return set_account (node, pdata, gncEntrySetInvAccount);
393 : : }
394 : :
395 : : static gboolean
396 : 0 : entry_iprice_handler (xmlNodePtr node, gpointer entry_pdata)
397 : : {
398 : 0 : struct entry_pdata* pdata = static_cast<decltype (pdata)> (entry_pdata);
399 : :
400 : 0 : return set_numeric (node, pdata->entry, gncEntrySetInvPrice);
401 : : }
402 : :
403 : : static gboolean
404 : 0 : entry_idiscount_handler (xmlNodePtr node, gpointer entry_pdata)
405 : : {
406 : 0 : struct entry_pdata* pdata = static_cast<decltype (pdata)> (entry_pdata);
407 : :
408 : 0 : return set_numeric (node, pdata->entry, gncEntrySetInvDiscount);
409 : : }
410 : :
411 : : static gboolean
412 : 0 : entry_idisctype_handler (xmlNodePtr node, gpointer entry_pdata)
413 : : {
414 : 0 : struct entry_pdata* pdata = static_cast<decltype (pdata)> (entry_pdata);
415 : : GncAmountType type;
416 : : char* str;
417 : : gboolean ret;
418 : :
419 : 0 : str = dom_tree_to_text (node);
420 : 0 : g_return_val_if_fail (str, FALSE);
421 : :
422 : 0 : ret = gncAmountStringToType (str, &type);
423 : 0 : g_free (str);
424 : :
425 : 0 : if (ret)
426 : 0 : gncEntrySetInvDiscountType (pdata->entry, type);
427 : :
428 : 0 : return ret;
429 : : }
430 : :
431 : : static gboolean
432 : 0 : entry_idischow_handler (xmlNodePtr node, gpointer entry_pdata)
433 : : {
434 : 0 : struct entry_pdata* pdata = static_cast<decltype (pdata)> (entry_pdata);
435 : : GncDiscountHow how;
436 : : char* str;
437 : : gboolean ret;
438 : :
439 : 0 : str = dom_tree_to_text (node);
440 : 0 : g_return_val_if_fail (str, FALSE);
441 : :
442 : 0 : ret = gncEntryDiscountStringToHow (str, &how);
443 : 0 : g_free (str);
444 : :
445 : 0 : if (ret)
446 : 0 : gncEntrySetInvDiscountHow (pdata->entry, how);
447 : :
448 : 0 : return ret;
449 : : }
450 : :
451 : : static gboolean
452 : 0 : entry_itaxable_handler (xmlNodePtr node, gpointer entry_pdata)
453 : : {
454 : 0 : struct entry_pdata* pdata = static_cast<decltype (pdata)> (entry_pdata);
455 : 0 : return set_boolean (node, pdata->entry, gncEntrySetInvTaxable);
456 : : }
457 : :
458 : : static gboolean
459 : 0 : entry_itaxincluded_handler (xmlNodePtr node, gpointer entry_pdata)
460 : : {
461 : 0 : struct entry_pdata* pdata = static_cast<decltype (pdata)> (entry_pdata);
462 : 0 : return set_boolean (node, pdata->entry, gncEntrySetInvTaxIncluded);
463 : : }
464 : :
465 : : static gboolean
466 : 0 : entry_itaxtable_handler (xmlNodePtr node, gpointer entry_pdata)
467 : : {
468 : 0 : struct entry_pdata* pdata = static_cast<decltype (pdata)> (entry_pdata);
469 : 0 : return set_taxtable (node, pdata, gncEntrySetInvTaxTable);
470 : : }
471 : :
472 : : /* vendor bills */
473 : :
474 : : static gboolean
475 : 2 : entry_billacct_handler (xmlNodePtr node, gpointer entry_pdata)
476 : : {
477 : 2 : struct entry_pdata* pdata = static_cast<decltype (pdata)> (entry_pdata);
478 : 2 : return set_account (node, pdata, gncEntrySetBillAccount);
479 : : }
480 : :
481 : : static gboolean
482 : 2 : entry_bprice_handler (xmlNodePtr node, gpointer entry_pdata)
483 : : {
484 : 2 : struct entry_pdata* pdata = static_cast<decltype (pdata)> (entry_pdata);
485 : :
486 : 2 : return set_numeric (node, pdata->entry, gncEntrySetBillPrice);
487 : : }
488 : :
489 : : static gboolean
490 : 3 : entry_btaxable_handler (xmlNodePtr node, gpointer entry_pdata)
491 : : {
492 : 3 : struct entry_pdata* pdata = static_cast<decltype (pdata)> (entry_pdata);
493 : 3 : return set_boolean (node, pdata->entry, gncEntrySetBillTaxable);
494 : : }
495 : :
496 : : static gboolean
497 : 3 : entry_btaxincluded_handler (xmlNodePtr node, gpointer entry_pdata)
498 : : {
499 : 3 : struct entry_pdata* pdata = static_cast<decltype (pdata)> (entry_pdata);
500 : 3 : return set_boolean (node, pdata->entry, gncEntrySetBillTaxIncluded);
501 : : }
502 : :
503 : : static gboolean
504 : 0 : entry_btaxtable_handler (xmlNodePtr node, gpointer entry_pdata)
505 : : {
506 : 0 : struct entry_pdata* pdata = static_cast<decltype (pdata)> (entry_pdata);
507 : 0 : return set_taxtable (node, pdata, gncEntrySetBillTaxTable);
508 : : }
509 : :
510 : : static gboolean
511 : 3 : entry_billable_handler (xmlNodePtr node, gpointer entry_pdata)
512 : : {
513 : 3 : struct entry_pdata* pdata = static_cast<decltype (pdata)> (entry_pdata);
514 : 3 : return set_boolean (node, pdata->entry, gncEntrySetBillable);
515 : : }
516 : :
517 : : static gboolean
518 : 0 : entry_billto_handler (xmlNodePtr node, gpointer entry_pdata)
519 : : {
520 : 0 : struct entry_pdata* pdata = static_cast<decltype (pdata)> (entry_pdata);
521 : : GncOwner billto;
522 : : gboolean ret;
523 : :
524 : 0 : ret = gnc_dom_tree_to_owner (node, &billto, pdata->book);
525 : 0 : if (ret)
526 : 0 : gncEntrySetBillTo (pdata->entry, &billto);
527 : :
528 : 0 : return ret;
529 : : }
530 : :
531 : : /* employee bills */
532 : : static gboolean
533 : 3 : entry_billpayment_handler (xmlNodePtr node, gpointer entry_pdata)
534 : : {
535 : 3 : struct entry_pdata* pdata = static_cast<decltype (pdata)> (entry_pdata);
536 : : GncEntryPaymentType type;
537 : : char* str;
538 : : gboolean ret;
539 : :
540 : 3 : str = dom_tree_to_text (node);
541 : 3 : g_return_val_if_fail (str, FALSE);
542 : :
543 : 3 : ret = gncEntryPaymentStringToType (str, &type);
544 : 3 : g_free (str);
545 : :
546 : 3 : if (ret)
547 : 3 : gncEntrySetBillPayment (pdata->entry, type);
548 : :
549 : 3 : return ret;
550 : : }
551 : :
552 : : /* The rest of the stuff */
553 : :
554 : : static gboolean
555 : 0 : entry_order_handler (xmlNodePtr node, gpointer entry_pdata)
556 : : {
557 : 0 : struct entry_pdata* pdata = static_cast<decltype (pdata)> (entry_pdata);
558 : : GncGUID* guid;
559 : : GncOrder* order;
560 : :
561 : 0 : guid = dom_tree_to_guid (node);
562 : 0 : g_return_val_if_fail (guid, FALSE);
563 : 0 : order = gncOrderLookup (pdata->book, guid);
564 : 0 : if (!order)
565 : : {
566 : 0 : order = gncOrderCreate (pdata->book);
567 : 0 : gncOrderBeginEdit (order);
568 : 0 : gncOrderSetGUID (order, guid);
569 : 0 : gncOrderCommitEdit (order);
570 : : }
571 : 0 : gncOrderBeginEdit (order);
572 : 0 : gncOrderAddEntry (order, pdata->entry);
573 : 0 : gncOrderCommitEdit (order);
574 : :
575 : 0 : guid_free (guid);
576 : 0 : return TRUE;
577 : : }
578 : :
579 : : static gboolean
580 : 0 : entry_invoice_handler (xmlNodePtr node, gpointer entry_pdata)
581 : : {
582 : 0 : struct entry_pdata* pdata = static_cast<decltype (pdata)> (entry_pdata);
583 : : GncGUID* guid;
584 : : GncInvoice* invoice;
585 : :
586 : 0 : guid = dom_tree_to_guid (node);
587 : 0 : g_return_val_if_fail (guid, FALSE);
588 : 0 : invoice = gncInvoiceLookup (pdata->book, guid);
589 : 0 : if (!invoice)
590 : : {
591 : 0 : invoice = gncInvoiceCreate (pdata->book);
592 : 0 : gncInvoiceBeginEdit (invoice);
593 : 0 : gncInvoiceSetGUID (invoice, guid);
594 : 0 : gncInvoiceCommitEdit (invoice);
595 : : }
596 : 0 : gncInvoiceBeginEdit (invoice);
597 : 0 : gncInvoiceAddEntry (invoice, pdata->entry);
598 : 0 : gncInvoiceCommitEdit (invoice);
599 : :
600 : 0 : guid_free (guid);
601 : 0 : return TRUE;
602 : : }
603 : :
604 : : static gboolean
605 : 3 : entry_bill_handler (xmlNodePtr node, gpointer entry_pdata)
606 : : {
607 : 3 : struct entry_pdata* pdata = static_cast<decltype (pdata)> (entry_pdata);
608 : : GncGUID* guid;
609 : : GncInvoice* invoice;
610 : :
611 : 3 : guid = dom_tree_to_guid (node);
612 : 3 : g_return_val_if_fail (guid, FALSE);
613 : 3 : invoice = gncInvoiceLookup (pdata->book, guid);
614 : 3 : if (!invoice)
615 : : {
616 : 3 : invoice = gncInvoiceCreate (pdata->book);
617 : 3 : gncInvoiceBeginEdit (invoice);
618 : 3 : gncInvoiceSetGUID (invoice, guid);
619 : 3 : gncInvoiceCommitEdit (invoice);
620 : : }
621 : 3 : gncInvoiceBeginEdit (invoice);
622 : 3 : gncBillAddEntry (invoice, pdata->entry);
623 : 3 : gncInvoiceCommitEdit (invoice);
624 : :
625 : 3 : guid_free (guid);
626 : 3 : return TRUE;
627 : : }
628 : :
629 : : /* Support for older XML versions */
630 : :
631 : : static gboolean
632 : 0 : entry_acct_handler (xmlNodePtr node, gpointer entry_pdata)
633 : : {
634 : 0 : struct entry_pdata* pdata = static_cast<decltype (pdata)> (entry_pdata);
635 : : /* XXX: try to figure out if this is an 'invoice' or a 'bill' --
636 : : * we have to wait until the end!
637 : : */
638 : :
639 : 0 : return set_account (node, pdata, NULL);
640 : : }
641 : :
642 : : static gboolean
643 : 0 : entry_price_handler (xmlNodePtr node, gpointer entry_pdata)
644 : : {
645 : 0 : struct entry_pdata* pdata = static_cast<decltype (pdata)> (entry_pdata);
646 : : gboolean res;
647 : :
648 : : /* just set both.. Don't worry about extra data if it's wrong */
649 : 0 : res = set_numeric (node, pdata->entry, gncEntrySetInvPrice);
650 : 0 : if (res)
651 : 0 : gncEntrySetBillPrice (pdata->entry, gncEntryGetInvPrice (pdata->entry));
652 : 0 : return res;
653 : : }
654 : :
655 : : static gboolean
656 : 0 : entry_slots_handler (xmlNodePtr node, gpointer entry_pdata)
657 : : {
658 : 0 : struct entry_pdata* pdata = static_cast<decltype (pdata)> (entry_pdata);
659 : :
660 : 0 : return dom_tree_create_instance_slots (node, QOF_INSTANCE (pdata->entry));
661 : : }
662 : :
663 : : static struct dom_tree_handler entry_handlers_v2[] =
664 : : {
665 : : { entry_guid_string, entry_guid_handler, 1, 0 },
666 : : { entry_date_string, entry_date_handler, 1, 0 },
667 : : { entry_dateentered_string, entry_dateentered_handler, 1, 0 },
668 : : { entry_description_string, entry_description_handler, 0, 0 },
669 : : { entry_action_string, entry_action_handler, 0, 0 },
670 : : { entry_notes_string, entry_notes_handler, 0, 0 },
671 : : { entry_qty_string, entry_qty_handler, 0, 0 },
672 : :
673 : : /* cust invoice */
674 : : { entry_invacct_string, entry_invacct_handler, 0, 0 },
675 : : { entry_iprice_string, entry_iprice_handler, 0, 0 },
676 : : { entry_idiscount_string, entry_idiscount_handler, 0, 0 },
677 : : { entry_idisctype_string, entry_idisctype_handler, 0, 0 },
678 : : { entry_idischow_string, entry_idischow_handler, 0, 0 },
679 : : { entry_itaxable_string, entry_itaxable_handler, 0, 0 },
680 : : { entry_itaxincluded_string, entry_itaxincluded_handler, 0, 0 },
681 : : { entry_itaxtable_string, entry_itaxtable_handler, 0, 0 },
682 : :
683 : : /* vendor invoice */
684 : : { entry_billacct_string, entry_billacct_handler, 0, 0 },
685 : : { entry_bprice_string, entry_bprice_handler, 0, 0 },
686 : : { entry_btaxable_string, entry_btaxable_handler, 0, 0 },
687 : : { entry_btaxincluded_string, entry_btaxincluded_handler, 0, 0 },
688 : : { entry_btaxtable_string, entry_btaxtable_handler, 0, 0 },
689 : : { entry_billable_string, entry_billable_handler, 0, 0 },
690 : : { entry_billto_string, entry_billto_handler, 0, 0 },
691 : :
692 : : /* employee stuff */
693 : : { entry_billpayment_string, entry_billpayment_handler, 0, 0 },
694 : :
695 : : /* Other stuff */
696 : : { entry_order_string, entry_order_handler, 0, 0 },
697 : : { entry_invoice_string, entry_invoice_handler, 0, 0 },
698 : : { entry_bill_string, entry_bill_handler, 0, 0 },
699 : : { entry_slots_string, entry_slots_handler, 0, 0 },
700 : :
701 : : /* Old XML support */
702 : : { "entry:acct", entry_acct_handler, 0, 0 },
703 : : { "entry:price", entry_price_handler, 0, 0 },
704 : : { "entry:discount", entry_idiscount_handler, 0, 0 },
705 : : { "entry:disc-type", entry_idisctype_handler, 0, 0 },
706 : : { "entry:disc-how", entry_idischow_handler, 0, 0 },
707 : : { "entry:taxable", entry_itaxable_handler, 0, 0 },
708 : : { "entry:taxincluded", entry_itaxincluded_handler, 0, 0 },
709 : : { "entry:taxtable", entry_itaxtable_handler, 0, 0 },
710 : : { NULL, 0, 0, 0 }
711 : : };
712 : :
713 : : static GncEntry*
714 : 3 : dom_tree_to_entry (xmlNodePtr node, QofBook* book)
715 : : {
716 : : struct entry_pdata entry_pdata;
717 : : gboolean successful;
718 : :
719 : 3 : entry_pdata.entry = gncEntryCreate (book);
720 : 3 : entry_pdata.book = book;
721 : 3 : entry_pdata.acc = NULL;
722 : 3 : gncEntryBeginEdit (entry_pdata.entry);
723 : :
724 : 3 : successful = dom_tree_generic_parse (node, entry_handlers_v2,
725 : : &entry_pdata);
726 : 3 : if (entry_pdata.acc != NULL)
727 : : {
728 : 0 : if (gncEntryGetBill (entry_pdata.entry))
729 : 0 : gncEntrySetBillAccount (entry_pdata.entry, entry_pdata.acc);
730 : : else
731 : 0 : gncEntrySetInvAccount (entry_pdata.entry, entry_pdata.acc);
732 : : }
733 : :
734 : 3 : if (successful)
735 : 3 : gncEntryCommitEdit (entry_pdata.entry);
736 : : else
737 : : {
738 : 0 : PERR ("failed to parse entry tree");
739 : 0 : gncEntryDestroy (entry_pdata.entry);
740 : 0 : entry_pdata.entry = NULL;
741 : : }
742 : :
743 : 3 : return entry_pdata.entry;
744 : : }
745 : :
746 : : static gboolean
747 : 43 : gnc_entry_end_handler (gpointer data_for_children,
748 : : GSList* data_from_children, GSList* sibling_data,
749 : : gpointer parent_data, gpointer global_data,
750 : : gpointer* result, const gchar* tag)
751 : : {
752 : : GncEntry* entry;
753 : 43 : xmlNodePtr tree = (xmlNodePtr)data_for_children;
754 : 43 : gxpf_data* gdata = (gxpf_data*)global_data;
755 : 43 : QofBook* book = static_cast<decltype (book)> (gdata->bookdata);
756 : :
757 : :
758 : 43 : if (parent_data)
759 : : {
760 : 40 : return TRUE;
761 : : }
762 : :
763 : : /* OK. For some messed up reason this is getting called again with a
764 : : NULL tag. So we ignore those cases */
765 : 3 : if (!tag)
766 : : {
767 : 0 : return TRUE;
768 : : }
769 : :
770 : 3 : g_return_val_if_fail (tree, FALSE);
771 : :
772 : 3 : entry = dom_tree_to_entry (tree, book);
773 : 3 : if (entry != NULL)
774 : : {
775 : 3 : gdata->cb (tag, gdata->parsedata, entry);
776 : : }
777 : :
778 : 3 : xmlFreeNode (tree);
779 : :
780 : 3 : return entry != NULL;
781 : : }
782 : :
783 : : static sixtp*
784 : 21 : entry_sixtp_parser_create (void)
785 : : {
786 : 21 : return sixtp_dom_parser_new (gnc_entry_end_handler, NULL, NULL);
787 : : }
788 : :
789 : : static void
790 : 2 : do_count (QofInstance* entry_p, gpointer count_p)
791 : : {
792 : 2 : int* count = static_cast<decltype (count)> (count_p);
793 : 2 : (*count)++;
794 : 2 : }
795 : :
796 : : static int
797 : 4 : entry_get_count (QofBook* book)
798 : : {
799 : 4 : int count = 0;
800 : 4 : qof_object_foreach (_GNC_MOD_NAME, book, do_count, (gpointer) &count);
801 : 4 : return count;
802 : : }
803 : :
804 : : static void
805 : 2 : xml_add_entry (QofInstance* entry_p, gpointer out_p)
806 : : {
807 : : xmlNodePtr node;
808 : 2 : GncEntry* entry = (GncEntry*) entry_p;
809 : 2 : FILE* out = static_cast<decltype (out)> (out_p);
810 : :
811 : 2 : if (ferror (out))
812 : 0 : return;
813 : :
814 : : /* Don't save non-attached entries! */
815 : 4 : if (! (gncEntryGetOrder (entry) || gncEntryGetInvoice (entry) ||
816 : 2 : gncEntryGetBill (entry)))
817 : 0 : return;
818 : :
819 : 2 : node = entry_dom_tree_create (entry);
820 : 2 : xmlElemDump (out, NULL, node);
821 : 2 : xmlFreeNode (node);
822 : 2 : if (ferror (out) || fprintf (out, "\n") < 0)
823 : 0 : return;
824 : : }
825 : :
826 : : static gboolean
827 : 4 : entry_write (FILE* out, QofBook* book)
828 : : {
829 : 4 : qof_object_foreach_sorted (_GNC_MOD_NAME, book, xml_add_entry, (gpointer) out);
830 : 4 : return ferror (out) == 0;
831 : : }
832 : :
833 : : static gboolean
834 : 4 : entry_ns (FILE* out)
835 : : {
836 : 4 : g_return_val_if_fail (out, FALSE);
837 : 4 : return gnc_xml2_write_namespace_decl (out, "entry");
838 : : }
839 : :
840 : : void
841 : 61 : gnc_entry_xml_initialize (void)
842 : : {
843 : : static GncXmlDataType_t be_data =
844 : : {
845 : : GNC_FILE_BACKEND_VERS,
846 : : gnc_entry_string,
847 : : entry_sixtp_parser_create,
848 : : NULL, /* add_item */
849 : : entry_get_count,
850 : : entry_write,
851 : : NULL, /* scrub */
852 : : entry_ns,
853 : : };
854 : :
855 : 61 : gnc_xml_register_backend (be_data);
856 : 61 : }
|