r21990 - gnucash/trunk/src - Introduce two convenience functions to simplify quantity handling for invoice/credit note entries.

Geert Janssens gjanssens at code.gnucash.org
Fri Feb 10 10:33:15 EST 2012


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

Modified:
   gnucash/trunk/src/business/business-ledger/gncEntryLedgerControl.c
   gnucash/trunk/src/business/business-ledger/gncEntryLedgerModel.c
   gnucash/trunk/src/engine/gncEntry.c
   gnucash/trunk/src/engine/gncEntry.h
   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
Log:
Introduce two convenience functions to simplify quantity handling for invoice/credit note entries.

Modified: gnucash/trunk/src/business/business-ledger/gncEntryLedgerControl.c
===================================================================
--- gnucash/trunk/src/business/business-ledger/gncEntryLedgerControl.c	2012-02-10 15:33:06 UTC (rev 21989)
+++ gnucash/trunk/src/business/business-ledger/gncEntryLedgerControl.c	2012-02-10 15:33:15 UTC (rev 21990)
@@ -316,6 +316,7 @@
         break;
     default:
         use_invoice = FALSE;
+        break;
     };
 
     query = new_query_for_entry_desc(reg, desc, use_invoice);
@@ -543,21 +544,25 @@
     set_value_combo_cell (cell, account_name);
     g_free (account_name);
 
-    /* Auto-complete quantity cell
-     * Note: we always autofill a positive quantity value. This allows us to
-     * - reuse invoice entries on credit note ledgers, meaning you can credit
-     *   some invoice entry via autofill without having to manually fix the sign
-     *   on the credit note.
-     * - autofill credit note entries on other credit note entries (without having
-     *   to juggle sign reversals internally)
-     * - autofill credit note entries on invoice ledgers
-     *
-     * Disadvantage: invoice entries with explicitly set negative quantities will
-     * be autofilled to positive quantities in later uses. But it seems less common
-     * to me to require a negative entry again next time.
-     */
-    cell = gnc_table_layout_get_cell (ledger->table->layout, ENTRY_QTY_CELL);
-    set_value_price_cell (cell, gnc_numeric_abs(gncEntryGetQuantity (auto_entry)));
+    /* Auto-complete quantity cell. Note that this requires some care because
+     * credit notes store quantities with a reversed sign. So we need to figure
+     * out if the original document from which we extract the autofill entry
+     * was a credit note or not. */
+    {
+        gboolean orig_is_cn;
+        switch (ledger->type)
+        {
+        case GNCENTRY_INVOICE_ENTRY:
+        case GNCENTRY_CUST_CREDIT_NOTE_ENTRY:
+            orig_is_cn = gncInvoiceGetIsCreditNote (gncEntryGetInvoice (auto_entry));
+            break;
+        default:
+            orig_is_cn = gncInvoiceGetIsCreditNote (gncEntryGetBill (auto_entry));
+            break;
+        }
+        cell = gnc_table_layout_get_cell (ledger->table->layout, ENTRY_QTY_CELL);
+        set_value_price_cell (cell, gncEntryGetDocQuantity (auto_entry, orig_is_cn));
+    }
 
     /* Auto-complete price cell */
     {
@@ -570,6 +575,7 @@
             break;
         default:
             price = gncEntryGetBillPrice (auto_entry);
+            break;
         }
 
         /* Auto-complete price cell */
@@ -595,6 +601,7 @@
             taxable = gncEntryGetBillTaxable (auto_entry);
             taxincluded = gncEntryGetBillTaxIncluded (auto_entry);
             taxtable = gncEntryGetBillTaxTable (auto_entry);
+            break;
         }
 
         /* Taxable? cell */

Modified: gnucash/trunk/src/business/business-ledger/gncEntryLedgerModel.c
===================================================================
--- gnucash/trunk/src/business/business-ledger/gncEntryLedgerModel.c	2012-02-10 15:33:06 UTC (rev 21989)
+++ gnucash/trunk/src/business/business-ledger/gncEntryLedgerModel.c	2012-02-10 15:33:15 UTC (rev 21990)
@@ -313,18 +313,11 @@
     gnc_numeric qty;
 
     entry = gnc_entry_ledger_get_entry (ledger, virt_loc.vcell_loc);
-    qty = gncEntryGetQuantity (entry);
+    qty = gncEntryGetDocQuantity (entry, ledger->is_credit_note);
 
     if (gnc_numeric_zero_p (qty))
         return NULL;
 
-    /* Credit notes have negative quantities, but the ledger should
-     * display it as on the document, meaning positive.
-     * So reverse the quantity for credit notes.
-     */
-    if (ledger->is_credit_note)
-        qty = gnc_numeric_neg (qty);
-
     return xaccPrintAmount (qty, gnc_default_print_info (FALSE));
 }
 
@@ -1079,13 +1072,7 @@
 
         if (gnc_entry_ledger_get_numeric (ledger, ENTRY_QTY_CELL, &amount))
         {
-            /* Credit notes have negative quantities, but the ledger should
-             * display it as on the document, meaning positive.
-             * So reverse the quantity for credit notes.
-             */
-            if (ledger->is_credit_note)
-                amount = gnc_numeric_neg (amount);
-            gncEntrySetQuantity (entry, amount);
+            gncEntrySetDocQuantity (entry, amount, ledger->is_credit_note);
         }
     }
 

Modified: gnucash/trunk/src/engine/gncEntry.c
===================================================================
--- gnucash/trunk/src/engine/gncEntry.c	2012-02-10 15:33:06 UTC (rev 21989)
+++ gnucash/trunk/src/engine/gncEntry.c	2012-02-10 15:33:15 UTC (rev 21990)
@@ -531,6 +531,17 @@
     gncEntryCommitEdit (entry);
 }
 
+void gncEntrySetDocQuantity (GncEntry *entry, gnc_numeric quantity, gboolean is_cn)
+{
+    if (!entry) return;
+    if (gnc_numeric_eq (entry->quantity, quantity)) return;
+    gncEntryBeginEdit (entry);
+    entry->quantity = (is_cn ? gnc_numeric_neg (quantity) : quantity);
+    entry->values_dirty = TRUE;
+    mark_entry (entry);
+    gncEntryCommitEdit (entry);
+}
+
 /* Customer Invoices */
 
 void gncEntrySetInvAccount (GncEntry *entry, Account *acc)
@@ -883,6 +894,12 @@
     return entry->quantity;
 }
 
+gnc_numeric gncEntryGetDocQuantity (const GncEntry *entry, gboolean is_cn)
+{
+    gnc_numeric value = gncEntryGetQuantity (entry);
+    return (is_cn ? gnc_numeric_neg (value) : value);
+}
+
 /* Customer Invoice */
 
 Account * gncEntryGetInvAccount (const GncEntry *entry)

Modified: gnucash/trunk/src/engine/gncEntry.h
===================================================================
--- gnucash/trunk/src/engine/gncEntry.h	2012-02-10 15:33:06 UTC (rev 21989)
+++ gnucash/trunk/src/engine/gncEntry.h	2012-02-10 15:33:15 UTC (rev 21990)
@@ -106,7 +106,19 @@
 void gncEntrySetDescription (GncEntry *entry, const char *desc);
 void gncEntrySetAction (GncEntry *entry, const char *action);
 void gncEntrySetNotes (GncEntry *entry, const char *notes);
+/** Set the internal quantity without any conversion.
+ *  This distinction is made because credit notes store their quantity
+ *  sign-reversed compared to how the quantity is written on the
+ *  actual credit note (and hence how the ledger and reports show it
+ *  to the user). */
 void gncEntrySetQuantity (GncEntry *entry, gnc_numeric quantity);
+/** Set the internal quantity converting from the quantity as
+ *  visible on the physical document.
+ *  This distinction is made because credit notes store their quantity
+ *  sign-reversed compared to how the quantity is written on the
+ *  actual credit note (and hence how the ledger and reports show it
+ *  to the user). */
+void gncEntrySetDocQuantity (GncEntry *entry, gnc_numeric quantity, gboolean is_cn);
 /** @} */
 
 /** @name Customer Invoices
@@ -152,7 +164,18 @@
 const char * gncEntryGetDescription (const GncEntry *entry);
 const char * gncEntryGetAction (const GncEntry *entry);
 const char * gncEntryGetNotes (const GncEntry *notes);
+/** Get the quantity as stored internally.
+ *  This distinction is made because credit notes store their quantity
+ *  sign-reversed compared to how the quantity is written on the
+ *  actual credit note (and hence how the ledger and reports show it
+ *  to the user). */
 gnc_numeric gncEntryGetQuantity (const GncEntry *entry);
+/** Get the quantity as on the physical document.
+ *  This distinction is made because credit notes store their quantity
+ *  sign-reversed compared to how the quantity is written on the
+ *  actual credit note (and hence how the ledger and reports show it
+ *  to the user). */
+gnc_numeric gncEntryGetDocQuantity (const GncEntry *entry, gboolean is_cn);
 /** @} */
 
 /** @name Customer Invoices

Modified: gnucash/trunk/src/report/business-reports/easy-invoice.scm
===================================================================
--- gnucash/trunk/src/report/business-reports/easy-invoice.scm	2012-02-10 15:33:06 UTC (rev 21989)
+++ gnucash/trunk/src/report/business-reports/easy-invoice.scm	2012-02-10 15:33:15 UTC (rev 21990)
@@ -129,16 +129,6 @@
 
 (define (make-account-hash) (make-hash-table 23))
 
-;; Internally invoice values are positive and credit-note values are negative
-;; However on the invoice/cn document they are always displayed as positive
-;; So depending on the document type the internal values have to be reversed
-;; before they are printed on the document. This function handles that.
-;; It should be called for each internal value that is to be displayed on the document.
-(define (inv-or-cn-value value credit-note?)
-  (if (not credit-note?)
-	value
-	(gnc-numeric-neg value)))
-
 (define (update-account-hash hash values)
   (for-each
    (lambda (item)
@@ -191,7 +181,7 @@
 	(addto! row-contents
 		(gnc:make-html-table-cell/markup
 		 "number-cell"
-		 (inv-or-cn-value (gncEntryGetQuantity entry) credit-note?))))
+		 (gncEntryGetDocQuantity entry credit-note?))))
 
     (if (price-col column-vector)
 	(addto! row-contents

Modified: gnucash/trunk/src/report/business-reports/fancy-invoice.scm
===================================================================
--- gnucash/trunk/src/report/business-reports/fancy-invoice.scm	2012-02-10 15:33:06 UTC (rev 21989)
+++ gnucash/trunk/src/report/business-reports/fancy-invoice.scm	2012-02-10 15:33:15 UTC (rev 21990)
@@ -147,16 +147,6 @@
 
 (define (make-account-hash) (make-hash-table 23))
 
-;; Internally invoice values are positive and credit-note values are negative
-;; However on the invoice/cn document they are always displayed as positive
-;; So depending on the document type the internal values have to be reversed
-;; before they are printed on the document. This function handles that.
-;; It should be called for each internal value that is to be displayed on the document.
-(define (inv-or-cn-value value credit-note?)
-  (if (not credit-note?)
-	value
-	(gnc-numeric-neg value)))
-
 (define (update-account-hash hash values)
   (for-each
    (lambda (item)
@@ -199,7 +189,7 @@
 	(addto! row-contents
 		(gnc:make-html-table-cell/markup
 		 "number-cell"
-		 (inv-or-cn-value (gncEntryGetQuantity entry) credit-note?))))
+		 (gncEntryGetDocQuantity entry credit-note?))))
 
     (if (price-col column-vector)
 	(addto! row-contents

Modified: gnucash/trunk/src/report/business-reports/invoice.scm
===================================================================
--- gnucash/trunk/src/report/business-reports/invoice.scm	2012-02-10 15:33:06 UTC (rev 21989)
+++ gnucash/trunk/src/report/business-reports/invoice.scm	2012-02-10 15:33:15 UTC (rev 21990)
@@ -123,16 +123,6 @@
 
 (define (make-account-hash) (make-hash-table 23))
 
-;; Internally invoice values are positive and credit-note values are negative
-;; However on the invoice/cn document they are always displayed as positive
-;; So depending on the document type the internal values have to be reversed
-;; before they are printed on the document. This function handles that.
-;; It should be called for each internal value that is to be displayed on the document.
-(define (inv-or-cn-value value credit-note?)
-  (if (not credit-note?)
-	value
-	(gnc-numeric-neg value)))
-
 (define (update-account-hash hash values)
   (for-each
    (lambda (item)
@@ -186,7 +176,7 @@
 	(addto! row-contents
 		(gnc:make-html-table-cell/markup
 		 "number-cell"
-		 (inv-or-cn-value (gncEntryGetQuantity entry) credit-note?))))
+		 (gncEntryGetDocQuantity entry credit-note?))))
 
     (if (price-col column-vector)
 	(addto! row-contents



More information about the gnucash-changes mailing list