r21985 - gnucash/trunk/src - Rework entry value getters

Geert Janssens gjanssens at code.gnucash.org
Fri Feb 10 10:32:28 EST 2012


Author: gjanssens
Date: 2012-02-10 10:32:27 -0500 (Fri, 10 Feb 2012)
New Revision: 21985
Trac: http://svn.gnucash.org/trac/changeset/21985

Modified:
   gnucash/trunk/src/business/business-gnome/dialog-invoice.c
   gnucash/trunk/src/business/business-ledger/gncEntryLedgerModel.c
   gnucash/trunk/src/engine/gncEntry.c
   gnucash/trunk/src/engine/gncEntry.h
   gnucash/trunk/src/engine/gncInvoice.c
   gnucash/trunk/src/report/business-reports/easy-invoice.scm
   gnucash/trunk/src/report/business-reports/fancy-invoice.scm
   gnucash/trunk/src/report/business-reports/invoice.scm
   gnucash/trunk/src/report/business-reports/taxinvoice.eguile.scm
Log:
Rework entry value getters
Make it clear when the values are rounded or not and that the values are
as on the document (opposed to how they impact the balance).

Modified: gnucash/trunk/src/business/business-gnome/dialog-invoice.c
===================================================================
--- gnucash/trunk/src/business/business-gnome/dialog-invoice.c	2012-02-10 15:32:16 UTC (rev 21984)
+++ gnucash/trunk/src/business/business-gnome/dialog-invoice.c	2012-02-10 15:32:27 UTC (rev 21985)
@@ -661,6 +661,7 @@
     EntryList *entries, *entries_iter;
     GncEntry* entry;
     gboolean is_cust_doc;
+    gboolean is_cn;
     gboolean show_dialog = TRUE;
     gboolean post_ok = TRUE;
 
@@ -681,6 +682,7 @@
     }
 
     is_cust_doc = (gncInvoiceGetOwnerType (invoice) == GNC_OWNER_CUSTOMER);
+    is_cn = gncInvoiceGetIsCreditNote (invoice);
 
 //    /* Make sure that the invoice/credit note has a positive balance */
 //    if (gnc_numeric_negative_p(gncInvoiceGetTotal(invoice)))
@@ -810,7 +812,8 @@
                  */
 
                 /* Obtain the Entry's total value (net + tax) */
-                gncEntryGetValue (entry, is_cust_doc, &value, NULL, &tax, NULL);
+                value = gncEntryGetDocValue (entry, FALSE, is_cust_doc, is_cn);
+                tax   = gncEntryGetDocTaxValue (entry, FALSE, is_cust_doc, is_cn);
                 amount = gnc_numeric_add (value, tax,
                                           gnc_commodity_get_fraction (account_currency),
                                           GNC_HOW_RND_ROUND_HALF_UP );

Modified: gnucash/trunk/src/business/business-ledger/gncEntryLedgerModel.c
===================================================================
--- gnucash/trunk/src/business/business-ledger/gncEntryLedgerModel.c	2012-02-10 15:32:16 UTC (rev 21984)
+++ gnucash/trunk/src/business/business-ledger/gncEntryLedgerModel.c	2012-02-10 15:32:27 UTC (rev 21985)
@@ -454,22 +454,14 @@
     GncEntryLedger *ledger = user_data;
     gnc_numeric value;
 
-    /* Credit notes need some attention here: the ledger displays values
-     * as on the document, meaning positive for credit notes. Credit note
-     * values are negative internally though. So depending on which values
-     * are used to calculate the subtotal, the resulting subtotal has to be
-     * sign-reversed before displaying.
-     */
     /* Check if this is the current cursor */
     if (virt_cell_loc_equal (ledger->table->current_cursor_loc.vcell_loc,
                              virt_loc.vcell_loc))
     {
-        gnc_entry_ledger_compute_value (ledger, &value, NULL);
-        /* Credit note info: this function works with values as seen
+        /* Sign attention: this function works with values as seen
          * on-screen in the ledger, so they are always in the proper sign.
-         * As per the above no sign reversal is needed for
-         * credit note type ledgers.
          */
+        gnc_entry_ledger_compute_value (ledger, &value, NULL);
     }
     else
     {
@@ -478,14 +470,10 @@
         if (entry == gnc_entry_ledger_get_blank_entry (ledger))
             return NULL;
 
-        value = gncEntryReturnValue (entry, ledger->is_cust_doc);
-        /* Credit note info: this function works with internal values,
-         * so they are negative for credit note type ledgers and have to
-         * be sign-reversed as per the above.
+        /* Ledger should display values with the same sign as on the document
+         * so get the document value instead of the internal value here.
          */
-
-        if (ledger->is_credit_note)
-            value = gnc_numeric_neg (value);
+        value = gncEntryGetDocValue (entry, TRUE, ledger->is_cust_doc, ledger->is_credit_note);
     }
 
     return xaccPrintAmount (value, gnc_default_print_info (FALSE));
@@ -503,6 +491,9 @@
     if (virt_cell_loc_equal (ledger->table->current_cursor_loc.vcell_loc,
                              virt_loc.vcell_loc))
     {
+        /* Sign attention: this function works with values as seen
+         * on-screen in the ledger, so they are always in the proper sign.
+         */
         gnc_entry_ledger_compute_value (ledger, NULL, &value);
     }
     else
@@ -512,16 +503,12 @@
         if (entry == gnc_entry_ledger_get_blank_entry (ledger))
             return NULL;
 
-        value = gncEntryReturnTaxValue (entry, ledger->is_cust_doc);
+        /* Ledger should display values with the same sign as on the document
+         * so get the document value instead of the internal value here.
+         */
+        value = gncEntryGetDocTaxValue (entry, TRUE, ledger->is_cust_doc, ledger->is_credit_note);
     }
 
-    /* Credit notes have negative values, but the ledger should
-     * display it as on the document, meaning positive.
-     * So reverse the value for credit notes.
-     */
-    if (ledger->is_credit_note)
-        value = gnc_numeric_neg (value);
-
     return xaccPrintAmount (value, gnc_default_print_info (FALSE));
 }
 

Modified: gnucash/trunk/src/engine/gncEntry.c
===================================================================
--- gnucash/trunk/src/engine/gncEntry.c	2012-02-10 15:32:16 UTC (rev 21984)
+++ gnucash/trunk/src/engine/gncEntry.c	2012-02-10 15:32:27 UTC (rev 21985)
@@ -1344,50 +1344,95 @@
     entry->values_dirty = FALSE;
 }
 
-void gncEntryGetValue (GncEntry *entry, gboolean is_cust_doc, gnc_numeric *value,
-                       gnc_numeric *discount_value, gnc_numeric *tax_value,
-                       GList **tax_values)
+gnc_numeric gncEntryGetIntValue (GncEntry *entry, gboolean round, gboolean is_cust_doc)
 {
-    if (!entry) return;
-    gncEntryRecomputeValues (entry);
-    if (value)
-        *value = (is_cust_doc ? entry->i_value : entry->b_value);
-    if (discount_value)
-        *discount_value = (is_cust_doc ? entry->i_disc_value : gnc_numeric_zero());
-    if (tax_value)
-        *tax_value = (is_cust_doc ? entry->i_tax_value : entry->b_tax_value);
-    if (tax_values)
-        *tax_values = (is_cust_doc ? entry->i_tax_values : entry->b_tax_values);
-}
-
-gnc_numeric gncEntryReturnValue (GncEntry *entry, gboolean is_cust_doc)
-{
     if (!entry) return gnc_numeric_zero();
     gncEntryRecomputeValues (entry);
-    return (is_cust_doc ? entry->i_value_rounded : entry->b_value_rounded);
+    if (round)
+        return (is_cust_doc ? entry->i_value_rounded : entry->b_value_rounded);
+    else
+        return (is_cust_doc ? entry->i_value : entry->b_value);
 }
 
-gnc_numeric gncEntryReturnTaxValue (GncEntry *entry, gboolean is_cust_doc)
+gnc_numeric gncEntryGetIntTaxValue (GncEntry *entry, gboolean round, gboolean is_cust_doc)
 {
     if (!entry) return gnc_numeric_zero();
     gncEntryRecomputeValues (entry);
-    return (is_cust_doc ? entry->i_tax_value_rounded : entry->b_tax_value_rounded);
+    if (round)
+        return (is_cust_doc ? entry->i_tax_value_rounded : entry->b_tax_value_rounded);
+    else
+        return (is_cust_doc ? entry->i_tax_value : entry->b_tax_value);
 }
 
-AccountValueList * gncEntryReturnTaxValues (GncEntry *entry, gboolean is_cust_doc)
+AccountValueList * gncEntryGetIntTaxValues (GncEntry *entry, gboolean is_cust_doc)
 {
     if (!entry) return NULL;
     gncEntryRecomputeValues (entry);
     return (is_cust_doc ? entry->i_tax_values : entry->b_tax_values);
 }
 
-gnc_numeric gncEntryReturnDiscountValue (GncEntry *entry, gboolean is_cust_doc)
+gnc_numeric gncEntryGetIntDiscountValue (GncEntry *entry, gboolean round, gboolean is_cust_doc)
 {
     if (!entry) return gnc_numeric_zero();
     gncEntryRecomputeValues (entry);
-    return (is_cust_doc ? entry->i_disc_value_rounded : gnc_numeric_zero());
+    if (round)
+        return (is_cust_doc ? entry->i_disc_value_rounded : gnc_numeric_zero());
+    else
+        return (is_cust_doc ? entry->i_disc_value : gnc_numeric_zero());
 }
 
+gnc_numeric gncEntryGetDocValue (GncEntry *entry, gboolean round, gboolean is_cust_doc, gboolean is_cn)
+{
+    gnc_numeric value = gncEntryGetIntValue (entry, round, is_cust_doc);
+    return (is_cn ? gnc_numeric_neg (value) : value);
+}
+
+gnc_numeric gncEntryGetDocTaxValue (GncEntry *entry, gboolean round, gboolean is_cust_doc, gboolean is_cn)
+{
+    gnc_numeric value = gncEntryGetIntTaxValue (entry, round, is_cust_doc);
+    return (is_cn ? gnc_numeric_neg (value) : value);
+}
+
+AccountValueList * gncEntryGetDocTaxValues (GncEntry *entry, gboolean is_cust_doc, gboolean is_cn)
+{
+    AccountValueList *values = gncEntryGetIntTaxValues (entry, is_cust_doc);
+    g_assert_not_reached ();
+    /* FIXME How to invert the values in this case ? */
+    return (is_cn ? values : values);
+}
+
+gnc_numeric gncEntryGetDocDiscountValue (GncEntry *entry, gboolean round, gboolean is_cust_doc, gboolean is_cn)
+{
+    gnc_numeric value = gncEntryGetIntDiscountValue (entry, round, is_cust_doc);
+    return (is_cn ? gnc_numeric_neg (value) : value);
+}
+
+gnc_numeric gncEntryGetBalValue (GncEntry *entry, gboolean round, gboolean is_cust_doc, gboolean is_cn)
+{
+    gnc_numeric value = gncEntryGetIntValue (entry, round, is_cust_doc);
+    return (is_cn ? gnc_numeric_neg (value) : value);
+}
+
+gnc_numeric gncEntryGetBalTaxValue (GncEntry *entry, gboolean round, gboolean is_cust_doc, gboolean is_cn)
+{
+    gnc_numeric value = gncEntryGetIntTaxValue (entry, round, is_cust_doc);
+    return (is_cn ? gnc_numeric_neg (value) : value);
+}
+
+AccountValueList * gncEntryGetBalTaxValues (GncEntry *entry, gboolean is_cust_doc, gboolean is_cn)
+{
+    AccountValueList *values = gncEntryGetIntTaxValues (entry, is_cust_doc);
+    g_assert_not_reached ();
+    /* FIXME How to invert the values in this case ? */
+    return (is_cn ? values : values);
+}
+
+gnc_numeric gncEntryGetBalDiscountValue (GncEntry *entry, gboolean round, gboolean is_cust_doc, gboolean is_cn)
+{
+    gnc_numeric value = gncEntryGetIntDiscountValue (entry, round, is_cust_doc);
+    return (is_cn ? gnc_numeric_neg (value) : value);
+}
+
 /* XXX this existence of this routine is just wrong */
 gboolean gncEntryIsOpen (const GncEntry *entry)
 {

Modified: gnucash/trunk/src/engine/gncEntry.h
===================================================================
--- gnucash/trunk/src/engine/gncEntry.h	2012-02-10 15:32:16 UTC (rev 21984)
+++ gnucash/trunk/src/engine/gncEntry.h	2012-02-10 15:32:27 UTC (rev 21985)
@@ -185,33 +185,70 @@
 void gncEntryCopy (const GncEntry *src, GncEntry *dest);
 
 /** @name Getting Values
-
- * The first three return the rounded values -- the last returns the
- * list of unrounded account-values.  The list belongs to the entry
- * and will be destroyed, so use it quickly.
+ *
+ * An entry has three important values:
+ * - entry value: the amount the merchant gets
+ * - tax value: the amount the government gets
+ * - discount value: the amount the customer saved
+ *
+ * These values can be retrieved in several variants. depending on
+ * how they will be used some sign reversals can be applied on
+ * the values:
+ * - Int value: the value as stored internally. No sign reversals will
+ *              ever be done on this value
+ * - Doc value: the value as listed on the document. This is usually
+ *              a positive value, unless the document was a
+ *              negative invoice/bill or negative credit note.
+ *              Since credit note entry values are stored negatively
+ *              internally, they will be sign-reversed before returning
+ *              them.
+ * - Bal value: the value as it will impact the balance. Customer
+ *              invoices and vendor credit notes have a positive
+ *              influence on the balance, so these values will be positive.
+ *              For vendor bills and customer credit notes, the
+ *              values will be negative.
+ *
+ * For tax there are TaxValue and TaxValues variants: the first one
+ * returns to total tax value for this entry, meaning the sum of all
+ * individual taxes. The second one returns a list of all the individual
+ * tax values for this entry. This list holds unrounded values only, there's
+ * no variant with rounded values.
+ *
+ * The list is owned by the entry and will be destroyed automatically,
+ * so use it quickly.
+ *
+ * Finally, there are rounded and unrounded variants of most of
+ * these functions.
  @{
 */
-gnc_numeric gncEntryReturnValue (GncEntry *entry, gboolean is_inv);
-gnc_numeric gncEntryReturnDiscountValue (GncEntry *entry, gboolean is_inv);
-gnc_numeric gncEntryReturnTaxValue (GncEntry *entry, gboolean is_inv);
 typedef GList AccountValueList;
-AccountValueList * gncEntryReturnTaxValues (GncEntry *entry, gboolean is_inv);
+gnc_numeric gncEntryGetIntValue (GncEntry *entry, gboolean round, gboolean is_cust_doc);
+gnc_numeric gncEntryGetIntTaxValue (GncEntry *entry, gboolean round, gboolean is_cust_doc);
+AccountValueList * gncEntryGetIntTaxValues (GncEntry *entry, gboolean is_cust_doc);
+gnc_numeric gncEntryGetIntDiscountValue (GncEntry *entry, gboolean round, gboolean is_cust_doc);
 
-/** Compute the Entry value, tax-value, and discount_value, based on
- * the quantity, price, discount, tax-table, and types.  The value is
+gnc_numeric gncEntryGetDocValue (GncEntry *entry, gboolean round, gboolean is_cust_doc, gboolean is_cn);
+gnc_numeric gncEntryGetDocTaxValue (GncEntry *entry, gboolean round, gboolean is_cust_doc, gboolean is_cn);
+AccountValueList * gncEntryGetDocTaxValues (GncEntry *entry, gboolean is_cust_doc, gboolean is_cn);
+gnc_numeric gncEntryGetDocDiscountValue (GncEntry *entry, gboolean round, gboolean is_cust_doc, gboolean is_cn);
+
+gnc_numeric gncEntryGetBalValue (GncEntry *entry, gboolean round, gboolean is_cust_doc, gboolean is_cn);
+gnc_numeric gncEntryGetBalTaxValue (GncEntry *entry, gboolean round, gboolean is_cust_doc, gboolean is_cn);
+AccountValueList * gncEntryGetBalTaxValues (GncEntry *entry, gboolean is_cust_doc, gboolean is_cn);
+gnc_numeric gncEntryGetBalDiscountValue (GncEntry *entry, gboolean round, gboolean is_cust_doc, gboolean is_cn);
+
+/** Compute the Entry value, tax_value, and discount_value, based on
+ * the quantity, price, discount, tax_-table, and types.  The value is
  * the amount the merchant gets, the taxes are what the gov't gets,
  * and the discount is how much the customer saved.  The SCU is the
  * target denominator of the value and tax -- it should be the
  * account or commodity SCU of the target.
  *
- * The tax_values list is the property of the entry and will be
- * destroyed automatically, so use it quickly.  Note that all return
- * values from these two functions are NOT rounded.
+ *  The return values are NOT rounded.
+ *
+ * The tax_values list is owned by the entry and will be
+ * destroyed automatically, so use it quickly.
  */
-void gncEntryGetValue (GncEntry *entry, gboolean is_inv, gnc_numeric *value,
-                       gnc_numeric *discount, gnc_numeric *tax_value,
-                       GList **tax_values);
-
 void gncEntryComputeValue (gnc_numeric qty, gnc_numeric price,
                            const GncTaxTable *tax_table, gboolean tax_included,
                            gnc_numeric discount, GncAmountType discount_type,

Modified: gnucash/trunk/src/engine/gncInvoice.c
===================================================================
--- gnucash/trunk/src/engine/gncInvoice.c	2012-02-10 15:32:16 UTC (rev 21984)
+++ gnucash/trunk/src/engine/gncInvoice.c	2012-02-10 15:32:27 UTC (rev 21985)
@@ -800,8 +800,7 @@
         if (use_payment_type && gncEntryGetBillPayment (entry) != type)
             continue;
 
-        gncEntryGetValue (entry, is_cust_doc, &value, NULL, &tax, NULL);
-
+        value = gncEntryGetIntValue (entry, FALSE, is_cust_doc);
         if (gnc_numeric_check (value) == GNC_ERROR_OK)
         {
             if (use_value)
@@ -810,13 +809,14 @@
         else
             g_warning ("bad value in our entry");
 
-        if (gnc_numeric_check (tax) == GNC_ERROR_OK)
+        if (use_tax)
         {
-            if (use_tax)
+            tax = gncEntryGetIntTaxValue (entry, FALSE, is_cust_doc);
+            if (gnc_numeric_check (tax) == GNC_ERROR_OK)
                 total = gnc_numeric_add (total, tax, GNC_DENOM_AUTO, GNC_HOW_DENOM_LCD);
+            else
+                g_warning ("bad tax-value in our entry");
         }
-        else
-            g_warning ("bad tax-value in our entry");
     }
     return total;
 }
@@ -1327,7 +1327,9 @@
         gncEntryCommitEdit (entry);
 
         /* Obtain the Entry's Value and TaxValues */
-        gncEntryGetValue (entry, is_cust_doc, &value, NULL, &tax, &taxes);
+        value = gncEntryGetIntValue (entry, FALSE, is_cust_doc);
+        tax   = gncEntryGetIntTaxValue (entry, FALSE, is_cust_doc);
+        taxes = gncEntryGetIntTaxValues (entry, is_cust_doc);
 
         /* add the value for the account split */
         this_acc = (is_cust_doc ? gncEntryGetInvAccount (entry) :

Modified: gnucash/trunk/src/report/business-reports/easy-invoice.scm
===================================================================
--- gnucash/trunk/src/report/business-reports/easy-invoice.scm	2012-02-10 15:32:16 UTC (rev 21984)
+++ gnucash/trunk/src/report/business-reports/easy-invoice.scm	2012-02-10 15:32:27 UTC (rev 21985)
@@ -170,10 +170,10 @@
   (let* ((row-contents '())
 	 (entry-value (gnc:make-gnc-monetary
 		       currency
-		       (inv-or-cn-value (gncEntryReturnValue entry cust-doc?) credit-note?)))
+		       (gncEntryGetDocValue entry #t cust-doc? credit-note?)))
 	 (entry-tax-value (gnc:make-gnc-monetary
 			   currency
-			   (inv-or-cn-value (gncEntryReturnTaxValue entry cust-doc?) credit-note?))))
+			   (gncEntryGetDocTaxValue entry #t cust-doc? credit-note?))))
 
     (if (date-col column-vector)
         (addto! row-contents
@@ -524,7 +524,7 @@
 					      cust-doc? credit-note?)))
 
 	    (if display-all-taxes
-		(let ((tax-list (gncEntryReturnTaxValues current cust-doc?)))
+		(let ((tax-list (gncEntryGetIntTaxValues current cust-doc?)))
 		  (update-account-hash acct-hash tax-list credit-note?))
 		(tax-collector 'add
 			       (gnc:gnc-monetary-commodity (cdr entry-values))

Modified: gnucash/trunk/src/report/business-reports/fancy-invoice.scm
===================================================================
--- gnucash/trunk/src/report/business-reports/fancy-invoice.scm	2012-02-10 15:32:16 UTC (rev 21984)
+++ gnucash/trunk/src/report/business-reports/fancy-invoice.scm	2012-02-10 15:32:27 UTC (rev 21985)
@@ -168,10 +168,10 @@
   (let* ((row-contents '())
 	 (entry-value (gnc:make-gnc-monetary
 		       currency
-		       (gncEntryReturnValue entry invoice?)))
+		       (gncEntryGetIntValue entry #t invoice?)))
 	 (entry-tax-value (gnc:make-gnc-monetary
 			   currency
-			   (gncEntryReturnTaxValue entry invoice?))))
+			   (gncEntryGetIntTaxValue entry #t invoice?))))
 
     (if (date-col column-vector)
         (addto! row-contents
@@ -566,7 +566,7 @@
 					      invoice?)))
 
 	    (if display-all-taxes
-		(let ((tax-list (gncEntryReturnTaxValues current invoice?)))
+		(let ((tax-list (gncEntryGetIntTaxValues current invoice?)))
 		  (update-account-hash acct-hash tax-list))
 		(tax-collector 'add
 			       (gnc:gnc-monetary-commodity (cdr entry-values))

Modified: gnucash/trunk/src/report/business-reports/invoice.scm
===================================================================
--- gnucash/trunk/src/report/business-reports/invoice.scm	2012-02-10 15:32:16 UTC (rev 21984)
+++ gnucash/trunk/src/report/business-reports/invoice.scm	2012-02-10 15:32:27 UTC (rev 21985)
@@ -165,10 +165,10 @@
   (let* ((row-contents '())
 	 (entry-value (gnc:make-gnc-monetary
 		       currency
-		       (inv-or-cn-value (gncEntryReturnValue entry cust-doc?) credit-note?)))
+		       (inv-or-cn-value (gncEntryGetIntValue entry #t cust-doc?) credit-note?)))
 	 (entry-tax-value (gnc:make-gnc-monetary
 			   currency
-			   (inv-or-cn-value (gncEntryReturnTaxValue entry cust-doc?) credit-note?))))
+			   (inv-or-cn-value (gncEntryGetIntTaxValue entry #t cust-doc?) credit-note?))))
 
     (if (date-col column-vector)
         (addto! row-contents
@@ -491,7 +491,7 @@
 					      cust-doc? credit-note?)))
 
 	    (if display-all-taxes
-		(let ((tax-list (gncEntryReturnTaxValues current cust-doc?)))
+		(let ((tax-list (gncEntryGetIntTaxValues current cust-doc?)))
 		  (update-account-hash acct-hash tax-list credit-note?))
 		(tax-collector 'add
 			       (gnc:gnc-monetary-commodity (cdr entry-values))

Modified: gnucash/trunk/src/report/business-reports/taxinvoice.eguile.scm
===================================================================
--- gnucash/trunk/src/report/business-reports/taxinvoice.eguile.scm	2012-02-10 15:32:16 UTC (rev 21984)
+++ gnucash/trunk/src/report/business-reports/taxinvoice.eguile.scm	2012-02-10 15:32:27 UTC (rev 21985)
@@ -285,9 +285,9 @@
             (let ((qty       (gncEntryGetQuantity entry))
                   (each      (gncEntryGetInvPrice entry)) 
                   (action    (gncEntryGetAction entry)) 
-                  (rval      (gncEntryReturnValue entry #t)) 
-                  (rdiscval  (gncEntryReturnDiscountValue entry #t)) 
-                  (rtaxval   (gncEntryReturnTaxValue entry #t)) 
+                  (rval      (gncEntryGetIntValue entry #t #t)) 
+                  (rdiscval  (gncEntryGetIntDiscountValue entry #t #t)) 
+                  (rtaxval   (gncEntryGetIntTaxValue entry #t #t)) 
                   (disc      (gncEntryGetInvDiscount entry))
                   (disctype  (gncEntryGetInvDiscountType entry))
                   (acc       (gncEntryGetInvAccount entry))



More information about the gnucash-changes mailing list