LCOV - code coverage report
Current view: top level - libgnucash/backend/xml - gnc-entry-xml-v2.cpp (source / functions) Coverage Total Hit
Test: gnucash.info Lines: 73.4 % 319 234
Test Date: 2026-11-24 04:31:04 Functions: 81.6 % 49 40
Legend: Lines:     hit not hit

            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            8 : set_time64 (xmlNodePtr node, GncEntry* entry,
     229              :               void (*func) (GncEntry* entry, time64 ts))
     230              : {
     231            8 :     time64 time = dom_tree_to_time64 (node);
     232            8 :     if (!dom_tree_valid_time64 (time, node->name)) time = 0;
     233            8 :     func (entry, time);
     234            8 :     return TRUE;
     235              : }
     236              : 
     237              : static inline gboolean
     238            7 : set_numeric (xmlNodePtr node, GncEntry* entry,
     239              :              void (*func) (GncEntry* entry, gnc_numeric num))
     240              : {
     241            7 :     func (entry, dom_tree_to_gnc_numeric (node));
     242            7 :     return TRUE;
     243              : }
     244              : 
     245              : static inline gboolean
     246           11 : set_boolean (xmlNodePtr node, GncEntry* entry,
     247              :              void (*func) (GncEntry* entry, gboolean val))
     248              : {
     249              :     gint64 val;
     250              : 
     251           11 :     if (!dom_tree_to_integer (node, &val))
     252            0 :         return FALSE;
     253           11 :     func (entry, (gboolean)val);
     254           11 :     return TRUE;
     255              : }
     256              : 
     257              : static inline gboolean
     258            3 : set_account (xmlNodePtr node, struct entry_pdata* pdata,
     259              :              void (*func) (GncEntry* entry, Account* acc))
     260              : {
     261              :     Account* acc;
     262              : 
     263            3 :     auto guid = dom_tree_to_guid (node);
     264            3 :     g_return_val_if_fail (guid, FALSE);
     265            3 :     acc = xaccAccountLookup (&*guid, pdata->book);
     266            3 :     g_return_val_if_fail (acc, FALSE);
     267              : 
     268            3 :     if (func)
     269            3 :         func (pdata->entry, acc);
     270              :     else
     271            0 :         pdata->acc = acc;
     272            3 :     return TRUE;
     273              : }
     274              : 
     275              : static inline gboolean
     276            0 : set_taxtable (xmlNodePtr node, struct entry_pdata* pdata,
     277              :               void (*func) (GncEntry* entry, GncTaxTable* taxtable))
     278              : {
     279              :     GncTaxTable* taxtable;
     280              : 
     281            0 :     auto guid = dom_tree_to_guid (node);
     282            0 :     g_return_val_if_fail (guid, FALSE);
     283            0 :     taxtable = gncTaxTableLookup (pdata->book, &*guid);
     284            0 :     if (!taxtable)
     285              :     {
     286            0 :         taxtable = gncTaxTableCreate (pdata->book);
     287            0 :         gncTaxTableBeginEdit (taxtable);
     288            0 :         gncTaxTableSetGUID (taxtable, &*guid);
     289            0 :         gncTaxTableCommitEdit (taxtable);
     290              :     }
     291              :     else
     292            0 :         gncTaxTableDecRef (taxtable);
     293              : 
     294            0 :     func (pdata->entry, taxtable);
     295            0 :     return TRUE;
     296              : }
     297              : 
     298              : static gboolean
     299            4 : entry_guid_handler (xmlNodePtr node, gpointer entry_pdata)
     300              : {
     301            4 :     struct entry_pdata* pdata = static_cast<decltype (pdata)> (entry_pdata);
     302              :     GncEntry* entry;
     303              : 
     304            4 :     auto guid = dom_tree_to_guid (node);
     305            4 :     g_return_val_if_fail (guid, FALSE);
     306            4 :     entry = gncEntryLookup (pdata->book, &*guid);
     307            4 :     if (entry)
     308              :     {
     309            0 :         gncEntryDestroy (pdata->entry);
     310            0 :         pdata->entry = entry;
     311            0 :         gncEntryBeginEdit (entry);
     312              :     }
     313              :     else
     314              :     {
     315            4 :         gncEntrySetGUID (pdata->entry, &*guid);
     316              :     }
     317              : 
     318            4 :     return TRUE;
     319              : }
     320              : 
     321              : static gboolean
     322            4 : entry_date_handler (xmlNodePtr node, gpointer entry_pdata)
     323              : {
     324            4 :     struct entry_pdata* pdata = static_cast<decltype (pdata)> (entry_pdata);
     325            4 :     return set_time64 (node, pdata->entry, gncEntrySetDate);
     326              : }
     327              : 
     328              : static gboolean
     329            4 : entry_dateentered_handler (xmlNodePtr node, gpointer entry_pdata)
     330              : {
     331            4 :     struct entry_pdata* pdata = static_cast<decltype (pdata)> (entry_pdata);
     332            4 :     return set_time64 (node, pdata->entry, gncEntrySetDateEntered);
     333              : }
     334              : 
     335              : static gboolean
     336            3 : entry_description_handler (xmlNodePtr node, gpointer entry_pdata)
     337              : {
     338            3 :     struct entry_pdata* pdata = static_cast<decltype (pdata)> (entry_pdata);
     339              : 
     340            3 :     return apply_xmlnode_text (gncEntrySetDescription, pdata->entry, node);
     341              : }
     342              : 
     343              : static gboolean
     344            2 : entry_action_handler (xmlNodePtr node, gpointer entry_pdata)
     345              : {
     346            2 :     struct entry_pdata* pdata = static_cast<decltype (pdata)> (entry_pdata);
     347              : 
     348            2 :     return apply_xmlnode_text (gncEntrySetAction, pdata->entry, node);
     349              : }
     350              : 
     351              : static gboolean
     352            0 : entry_notes_handler (xmlNodePtr node, gpointer entry_pdata)
     353              : {
     354            0 :     struct entry_pdata* pdata = static_cast<decltype (pdata)> (entry_pdata);
     355              : 
     356            0 :     return apply_xmlnode_text (gncEntrySetNotes, pdata->entry, node);
     357              : }
     358              : 
     359              : static gboolean
     360            3 : entry_qty_handler (xmlNodePtr node, gpointer entry_pdata)
     361              : {
     362            3 :     struct entry_pdata* pdata = static_cast<decltype (pdata)> (entry_pdata);
     363              : 
     364            3 :     return set_numeric (node, pdata->entry, gncEntrySetQuantity);
     365              : }
     366              : 
     367              : /* Cust invoice */
     368              : 
     369              : static gboolean
     370            1 : entry_invacct_handler (xmlNodePtr node, gpointer entry_pdata)
     371              : {
     372            1 :     struct entry_pdata* pdata = static_cast<decltype (pdata)> (entry_pdata);
     373            1 :     return set_account (node, pdata, gncEntrySetInvAccount);
     374              : }
     375              : 
     376              : static gboolean
     377            1 : entry_iprice_handler (xmlNodePtr node, gpointer entry_pdata)
     378              : {
     379            1 :     struct entry_pdata* pdata = static_cast<decltype (pdata)> (entry_pdata);
     380              : 
     381            1 :     return set_numeric (node, pdata->entry, gncEntrySetInvPrice);
     382              : }
     383              : 
     384              : static gboolean
     385            1 : entry_idiscount_handler (xmlNodePtr node, gpointer entry_pdata)
     386              : {
     387            1 :     struct entry_pdata* pdata = static_cast<decltype (pdata)> (entry_pdata);
     388              : 
     389            1 :     return set_numeric (node, pdata->entry, gncEntrySetInvDiscount);
     390              : }
     391              : 
     392              : static gboolean
     393            1 : entry_idisctype_handler (xmlNodePtr node, gpointer entry_pdata)
     394              : {
     395            1 :     struct entry_pdata* pdata = static_cast<decltype (pdata)> (entry_pdata);
     396            1 :     auto entry = pdata->entry;
     397            1 :     auto set_discount_type = [entry](auto str)
     398              :     {
     399              :         GncAmountType type;
     400            1 :         if (!gncAmountStringToType (str, &type)) return false;
     401            1 :         gncEntrySetInvDiscountType (entry, type);
     402            1 :         return true;
     403            1 :     };
     404            2 :     return apply_xmlnode_text (set_discount_type, node, FALSE);
     405              : }
     406              : 
     407              : static gboolean
     408            1 : entry_idischow_handler (xmlNodePtr node, gpointer entry_pdata)
     409              : {
     410            1 :     struct entry_pdata* pdata = static_cast<decltype (pdata)> (entry_pdata);
     411            1 :     auto entry = pdata->entry;
     412            1 :     auto set_discount_how = [entry](auto str)
     413              :     {
     414              :         GncDiscountHow how;
     415            1 :         if (!gncEntryDiscountStringToHow (str, &how)) return false;
     416            1 :         gncEntrySetInvDiscountHow (entry, how);
     417            1 :         return true;
     418            1 :     };
     419            2 :     return apply_xmlnode_text (set_discount_how, node, FALSE);
     420              : }
     421              : 
     422              : static gboolean
     423            1 : entry_itaxable_handler (xmlNodePtr node, gpointer entry_pdata)
     424              : {
     425            1 :     struct entry_pdata* pdata = static_cast<decltype (pdata)> (entry_pdata);
     426            1 :     return set_boolean (node, pdata->entry, gncEntrySetInvTaxable);
     427              : }
     428              : 
     429              : static gboolean
     430            1 : entry_itaxincluded_handler (xmlNodePtr node, gpointer entry_pdata)
     431              : {
     432            1 :     struct entry_pdata* pdata = static_cast<decltype (pdata)> (entry_pdata);
     433            1 :     return set_boolean (node, pdata->entry, gncEntrySetInvTaxIncluded);
     434              : }
     435              : 
     436              : static gboolean
     437            0 : entry_itaxtable_handler (xmlNodePtr node, gpointer entry_pdata)
     438              : {
     439            0 :     struct entry_pdata* pdata = static_cast<decltype (pdata)> (entry_pdata);
     440            0 :     return set_taxtable (node, pdata, gncEntrySetInvTaxTable);
     441              : }
     442              : 
     443              : /* vendor bills */
     444              : 
     445              : static gboolean
     446            2 : entry_billacct_handler (xmlNodePtr node, gpointer entry_pdata)
     447              : {
     448            2 :     struct entry_pdata* pdata = static_cast<decltype (pdata)> (entry_pdata);
     449            2 :     return set_account (node, pdata, gncEntrySetBillAccount);
     450              : }
     451              : 
     452              : static gboolean
     453            2 : entry_bprice_handler (xmlNodePtr node, gpointer entry_pdata)
     454              : {
     455            2 :     struct entry_pdata* pdata = static_cast<decltype (pdata)> (entry_pdata);
     456              : 
     457            2 :     return set_numeric (node, pdata->entry, gncEntrySetBillPrice);
     458              : }
     459              : 
     460              : static gboolean
     461            3 : entry_btaxable_handler (xmlNodePtr node, gpointer entry_pdata)
     462              : {
     463            3 :     struct entry_pdata* pdata = static_cast<decltype (pdata)> (entry_pdata);
     464            3 :     return set_boolean (node, pdata->entry, gncEntrySetBillTaxable);
     465              : }
     466              : 
     467              : static gboolean
     468            3 : entry_btaxincluded_handler (xmlNodePtr node, gpointer entry_pdata)
     469              : {
     470            3 :     struct entry_pdata* pdata = static_cast<decltype (pdata)> (entry_pdata);
     471            3 :     return set_boolean (node, pdata->entry, gncEntrySetBillTaxIncluded);
     472              : }
     473              : 
     474              : static gboolean
     475            0 : entry_btaxtable_handler (xmlNodePtr node, gpointer entry_pdata)
     476              : {
     477            0 :     struct entry_pdata* pdata = static_cast<decltype (pdata)> (entry_pdata);
     478            0 :     return set_taxtable (node, pdata, gncEntrySetBillTaxTable);
     479              : }
     480              : 
     481              : static gboolean
     482            3 : entry_billable_handler (xmlNodePtr node, gpointer entry_pdata)
     483              : {
     484            3 :     struct entry_pdata* pdata = static_cast<decltype (pdata)> (entry_pdata);
     485            3 :     return set_boolean (node, pdata->entry, gncEntrySetBillable);
     486              : }
     487              : 
     488              : static gboolean
     489            0 : entry_billto_handler (xmlNodePtr node, gpointer entry_pdata)
     490              : {
     491            0 :     struct entry_pdata* pdata = static_cast<decltype (pdata)> (entry_pdata);
     492              :     GncOwner billto;
     493              :     gboolean ret;
     494              : 
     495            0 :     ret = gnc_dom_tree_to_owner (node, &billto, pdata->book);
     496            0 :     if (ret)
     497            0 :         gncEntrySetBillTo (pdata->entry, &billto);
     498              : 
     499            0 :     return ret;
     500              : }
     501              : 
     502              : /* employee bills */
     503              : static gboolean
     504            3 : entry_billpayment_handler (xmlNodePtr node, gpointer entry_pdata)
     505              : {
     506            3 :     struct entry_pdata* pdata = static_cast<decltype (pdata)> (entry_pdata);
     507            3 :     auto entry = pdata->entry;
     508            3 :     auto set_billpayment = [entry](auto str)
     509              :     {
     510              :         GncEntryPaymentType type;
     511            3 :         if (!gncEntryPaymentStringToType (str, &type)) return false;
     512            3 :         gncEntrySetBillPayment (entry, type);
     513            3 :         return true;
     514            3 :     };
     515            6 :     return apply_xmlnode_text (set_billpayment, node, FALSE);
     516              : }
     517              : 
     518              : /* The rest of the stuff */
     519              : 
     520              : static gboolean
     521            0 : entry_order_handler (xmlNodePtr node, gpointer entry_pdata)
     522              : {
     523            0 :     struct entry_pdata* pdata = static_cast<decltype (pdata)> (entry_pdata);
     524              :     GncOrder* order;
     525              : 
     526            0 :     auto guid = dom_tree_to_guid (node);
     527            0 :     g_return_val_if_fail (guid, FALSE);
     528            0 :     order = gncOrderLookup (pdata->book, &*guid);
     529            0 :     if (!order)
     530              :     {
     531            0 :         order = gncOrderCreate (pdata->book);
     532            0 :         gncOrderBeginEdit (order);
     533            0 :         gncOrderSetGUID (order, &*guid);
     534            0 :         gncOrderCommitEdit (order);
     535              :     }
     536            0 :     gncOrderBeginEdit (order);
     537            0 :     gncOrderAddEntry (order, pdata->entry);
     538            0 :     gncOrderCommitEdit (order);
     539              : 
     540            0 :     return TRUE;
     541              : }
     542              : 
     543              : static gboolean
     544            1 : entry_invoice_handler (xmlNodePtr node, gpointer entry_pdata)
     545              : {
     546            1 :     struct entry_pdata* pdata = static_cast<decltype (pdata)> (entry_pdata);
     547              :     GncInvoice* invoice;
     548              : 
     549            1 :     auto guid = dom_tree_to_guid (node);
     550            1 :     g_return_val_if_fail (guid, FALSE);
     551            1 :     invoice = gncInvoiceLookup (pdata->book, &*guid);
     552            1 :     if (!invoice)
     553              :     {
     554            1 :         invoice = gncInvoiceCreate (pdata->book);
     555            1 :         gncInvoiceBeginEdit (invoice);
     556            1 :         gncInvoiceSetGUID (invoice, &*guid);
     557            1 :         gncInvoiceCommitEdit (invoice);
     558              :     }
     559            1 :     gncInvoiceBeginEdit (invoice);
     560            1 :     gncInvoiceAddEntry (invoice, pdata->entry);
     561            1 :     gncInvoiceCommitEdit (invoice);
     562              : 
     563            1 :     return TRUE;
     564              : }
     565              : 
     566              : static gboolean
     567            3 : entry_bill_handler (xmlNodePtr node, gpointer entry_pdata)
     568              : {
     569            3 :     struct entry_pdata* pdata = static_cast<decltype (pdata)> (entry_pdata);
     570              :     GncInvoice* invoice;
     571              : 
     572            3 :     auto guid = dom_tree_to_guid (node);
     573            3 :     g_return_val_if_fail (guid, FALSE);
     574            3 :     invoice = gncInvoiceLookup (pdata->book, &*guid);
     575            3 :     if (!invoice)
     576              :     {
     577            3 :         invoice = gncInvoiceCreate (pdata->book);
     578            3 :         gncInvoiceBeginEdit (invoice);
     579            3 :         gncInvoiceSetGUID (invoice, &*guid);
     580            3 :         gncInvoiceCommitEdit (invoice);
     581              :     }
     582            3 :     gncInvoiceBeginEdit (invoice);
     583            3 :     gncBillAddEntry (invoice, pdata->entry);
     584            3 :     gncInvoiceCommitEdit (invoice);
     585              : 
     586            3 :     return TRUE;
     587              : }
     588              : 
     589              : /* Support for older XML versions */
     590              : 
     591              : static gboolean
     592            0 : entry_acct_handler (xmlNodePtr node, gpointer entry_pdata)
     593              : {
     594            0 :     struct entry_pdata* pdata = static_cast<decltype (pdata)> (entry_pdata);
     595              :     /* XXX: try to figure out if this is an 'invoice' or a 'bill' --
     596              :      * we have to wait until the end!
     597              :      */
     598              : 
     599            0 :     return set_account (node, pdata, NULL);
     600              : }
     601              : 
     602              : static gboolean
     603            0 : entry_price_handler (xmlNodePtr node, gpointer entry_pdata)
     604              : {
     605            0 :     struct entry_pdata* pdata = static_cast<decltype (pdata)> (entry_pdata);
     606              :     gboolean res;
     607              : 
     608              :     /* just set both.. Don't worry about extra data if it's wrong */
     609            0 :     res = set_numeric (node, pdata->entry, gncEntrySetInvPrice);
     610            0 :     if (res)
     611            0 :         gncEntrySetBillPrice (pdata->entry, gncEntryGetInvPrice (pdata->entry));
     612            0 :     return res;
     613              : }
     614              : 
     615              : static gboolean
     616            0 : entry_slots_handler (xmlNodePtr node, gpointer entry_pdata)
     617              : {
     618            0 :     struct entry_pdata* pdata = static_cast<decltype (pdata)> (entry_pdata);
     619              : 
     620            0 :     return dom_tree_create_instance_slots (node, QOF_INSTANCE (pdata->entry));
     621              : }
     622              : 
     623              : static struct dom_tree_handler entry_handlers_v2[] =
     624              : {
     625              :     { entry_guid_string, entry_guid_handler, 1, 0 },
     626              :     { entry_date_string, entry_date_handler, 1, 0 },
     627              :     { entry_dateentered_string, entry_dateentered_handler, 1, 0 },
     628              :     { entry_description_string, entry_description_handler, 0, 0 },
     629              :     { entry_action_string, entry_action_handler, 0, 0 },
     630              :     { entry_notes_string, entry_notes_handler, 0, 0 },
     631              :     { entry_qty_string, entry_qty_handler, 0, 0 },
     632              : 
     633              :     /* cust invoice */
     634              :     { entry_invacct_string, entry_invacct_handler, 0, 0 },
     635              :     { entry_iprice_string, entry_iprice_handler, 0, 0 },
     636              :     { entry_idiscount_string, entry_idiscount_handler, 0, 0 },
     637              :     { entry_idisctype_string, entry_idisctype_handler, 0, 0 },
     638              :     { entry_idischow_string, entry_idischow_handler, 0, 0 },
     639              :     { entry_itaxable_string, entry_itaxable_handler, 0, 0 },
     640              :     { entry_itaxincluded_string, entry_itaxincluded_handler, 0, 0 },
     641              :     { entry_itaxtable_string, entry_itaxtable_handler, 0, 0 },
     642              : 
     643              :     /* vendor invoice */
     644              :     { entry_billacct_string, entry_billacct_handler, 0, 0 },
     645              :     { entry_bprice_string, entry_bprice_handler, 0, 0 },
     646              :     { entry_btaxable_string, entry_btaxable_handler, 0, 0 },
     647              :     { entry_btaxincluded_string, entry_btaxincluded_handler, 0, 0 },
     648              :     { entry_btaxtable_string, entry_btaxtable_handler, 0, 0 },
     649              :     { entry_billable_string, entry_billable_handler, 0, 0 },
     650              :     { entry_billto_string, entry_billto_handler, 0, 0 },
     651              : 
     652              :     /* employee stuff */
     653              :     { entry_billpayment_string, entry_billpayment_handler, 0, 0 },
     654              : 
     655              :     /* Other stuff */
     656              :     { entry_order_string, entry_order_handler, 0, 0 },
     657              :     { entry_invoice_string, entry_invoice_handler, 0, 0 },
     658              :     { entry_bill_string, entry_bill_handler, 0, 0 },
     659              :     { entry_slots_string, entry_slots_handler, 0, 0 },
     660              : 
     661              :     /* Old XML support */
     662              :     { "entry:acct", entry_acct_handler, 0, 0 },
     663              :     { "entry:price", entry_price_handler, 0, 0 },
     664              :     { "entry:discount", entry_idiscount_handler, 0, 0 },
     665              :     { "entry:disc-type", entry_idisctype_handler, 0, 0 },
     666              :     { "entry:disc-how", entry_idischow_handler, 0, 0 },
     667              :     { "entry:taxable", entry_itaxable_handler, 0, 0 },
     668              :     { "entry:taxincluded", entry_itaxincluded_handler, 0, 0 },
     669              :     { "entry:taxtable", entry_itaxtable_handler, 0, 0 },
     670              :     { NULL, 0, 0, 0 }
     671              : };
     672              : 
     673              : static GncEntry*
     674            4 : dom_tree_to_entry (xmlNodePtr node, QofBook* book)
     675              : {
     676              :     struct entry_pdata entry_pdata;
     677              :     gboolean successful;
     678              : 
     679            4 :     entry_pdata.entry = gncEntryCreate (book);
     680            4 :     entry_pdata.book = book;
     681            4 :     entry_pdata.acc = NULL;
     682            4 :     gncEntryBeginEdit (entry_pdata.entry);
     683              : 
     684            4 :     successful = dom_tree_generic_parse (node, entry_handlers_v2,
     685              :                                          &entry_pdata);
     686            4 :     if (entry_pdata.acc != NULL)
     687              :     {
     688            0 :         if (gncEntryGetBill (entry_pdata.entry))
     689            0 :             gncEntrySetBillAccount (entry_pdata.entry, entry_pdata.acc);
     690              :         else
     691            0 :             gncEntrySetInvAccount (entry_pdata.entry, entry_pdata.acc);
     692              :     }
     693              : 
     694            4 :     if (successful)
     695            4 :         gncEntryCommitEdit (entry_pdata.entry);
     696              :     else
     697              :     {
     698            0 :         PERR ("failed to parse entry tree");
     699            0 :         gncEntryDestroy (entry_pdata.entry);
     700            0 :         entry_pdata.entry = NULL;
     701              :     }
     702              : 
     703            4 :     return entry_pdata.entry;
     704              : }
     705              : 
     706              : static gboolean
     707           59 : gnc_entry_end_handler (gpointer data_for_children,
     708              :                        GSList* data_from_children, GSList* sibling_data,
     709              :                        gpointer parent_data, gpointer global_data,
     710              :                        gpointer* result, const gchar* tag)
     711              : {
     712              :     GncEntry* entry;
     713           59 :     xmlNodePtr tree = (xmlNodePtr)data_for_children;
     714           59 :     gxpf_data* gdata = (gxpf_data*)global_data;
     715           59 :     QofBook* book = static_cast<decltype (book)> (gdata->bookdata);
     716              : 
     717              : 
     718           59 :     if (parent_data)
     719              :     {
     720           55 :         return TRUE;
     721              :     }
     722              : 
     723              :     /* OK.  For some messed up reason this is getting called again with a
     724              :        NULL tag.  So we ignore those cases */
     725            4 :     if (!tag)
     726              :     {
     727            0 :         return TRUE;
     728              :     }
     729              : 
     730            4 :     g_return_val_if_fail (tree, FALSE);
     731              : 
     732            4 :     entry = dom_tree_to_entry (tree, book);
     733            4 :     if (entry != NULL)
     734              :     {
     735            4 :         gdata->cb (tag, gdata->parsedata, entry);
     736              :     }
     737              : 
     738            4 :     xmlFreeNode (tree);
     739              : 
     740            4 :     return entry != NULL;
     741              : }
     742              : 
     743              : static sixtp*
     744           22 : entry_sixtp_parser_create (void)
     745              : {
     746           22 :     return sixtp_dom_parser_new (gnc_entry_end_handler, NULL, NULL);
     747              : }
     748              : 
     749              : static void
     750            2 : do_count (QofInstance* entry_p, gpointer count_p)
     751              : {
     752            2 :     int* count = static_cast<decltype (count)> (count_p);
     753            2 :     (*count)++;
     754            2 : }
     755              : 
     756              : static int
     757            4 : entry_get_count (QofBook* book)
     758              : {
     759            4 :     int count = 0;
     760            4 :     qof_object_foreach (_GNC_MOD_NAME, book, do_count, (gpointer) &count);
     761            4 :     return count;
     762              : }
     763              : 
     764              : static void
     765            2 : xml_add_entry (QofInstance* entry_p, gpointer out_p)
     766              : {
     767              :     xmlNodePtr node;
     768            2 :     GncEntry* entry = (GncEntry*) entry_p;
     769            2 :     FILE* out = static_cast<decltype (out)> (out_p);
     770              : 
     771            2 :     if (ferror (out))
     772            0 :         return;
     773              : 
     774              :     /* Don't save non-attached entries! */
     775            4 :     if (! (gncEntryGetOrder (entry) || gncEntryGetInvoice (entry) ||
     776            2 :            gncEntryGetBill (entry)))
     777            0 :         return;
     778              : 
     779            2 :     node = entry_dom_tree_create (entry);
     780            2 :     xmlElemDump (out, NULL, node);
     781            2 :     xmlFreeNode (node);
     782            2 :     if (ferror (out) || fprintf (out, "\n") < 0)
     783            0 :         return;
     784              : }
     785              : 
     786              : static gboolean
     787            4 : entry_write (FILE* out, QofBook* book)
     788              : {
     789            4 :     qof_object_foreach_sorted (_GNC_MOD_NAME, book, xml_add_entry, (gpointer) out);
     790            4 :     return ferror (out) == 0;
     791              : }
     792              : 
     793              : static gboolean
     794            4 : entry_ns (FILE* out)
     795              : {
     796            4 :     g_return_val_if_fail (out, FALSE);
     797            4 :     return gnc_xml2_write_namespace_decl (out, "entry");
     798              : }
     799              : 
     800              : void
     801           15 : gnc_entry_xml_initialize (void)
     802              : {
     803              :     static GncXmlDataType_t be_data =
     804              :     {
     805              :         GNC_FILE_BACKEND_VERS,
     806              :         gnc_entry_string,
     807              :         entry_sixtp_parser_create,
     808              :         NULL,           /* add_item */
     809              :         entry_get_count,
     810              :         entry_write,
     811              :         NULL,           /* scrub */
     812              :         entry_ns,
     813              :     };
     814              : 
     815           15 :     gnc_xml_register_backend (be_data);
     816           15 : }
        

Generated by: LCOV version 2.0-1