[receipt.eguile] simplify

1. taxtables? was a hack to disable tax display; this is now obsolete
since bug 573645 was fixed with 8221aada. the equivalent
taxinvoice.eguile.scm hack was removed with 0eb2c2b3 but never removed
in receipt.eguile.scm

2. break out date<? comparator

3. use lispy for-each instead of pythonic for

3. compact code
This commit is contained in:
Christopher Lam 2020-04-05 21:29:25 +08:00
parent 9116fece7d
commit 0f56bed0d9

View File

@ -25,7 +25,12 @@
;; Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA ;; Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
;; 02111-1307 USA ;; 02111-1307 USA
(define (display-report opt-invoice owner endowner ownertype) (define (display-report opt-invoice)
(define (date<? s1 s2)
(< (xaccTransGetDate (xaccSplitGetParent s1))
(xaccTransGetDate (xaccSplitGetParent s2))))
;; Main function that creates the receipt invoice report ;; Main function that creates the receipt invoice report
(let* (; invoice and company details (let* (; invoice and company details
(invoiceid (gncInvoiceGetID opt-invoice)) (invoiceid (gncInvoiceGetID opt-invoice))
@ -40,7 +45,7 @@
(txn (gncInvoiceGetPostedTxn opt-invoice)) (txn (gncInvoiceGetPostedTxn opt-invoice))
(currency (gncInvoiceGetCurrency opt-invoice)) (currency (gncInvoiceGetCurrency opt-invoice))
(entries (gncInvoiceGetEntries opt-invoice)) (entries (gncInvoiceGetEntries opt-invoice))
(splits '()) (splits (sort (gnc-lot-get-split-list lot) date<?))
(coyname (gnc:company-info book gnc:*company-name*)) (coyname (gnc:company-info book gnc:*company-name*))
(coycontact (gnc:company-info book gnc:*company-contact*)) (coycontact (gnc:company-info book gnc:*company-contact*))
(coyaddr (gnc:company-info book gnc:*company-addy*)) (coyaddr (gnc:company-info book gnc:*company-addy*))
@ -49,55 +54,32 @@
(coyfax (gnc:company-info book gnc:*company-fax*)) (coyfax (gnc:company-info book gnc:*company-fax*))
(coyurl (gnc:company-info book gnc:*company-url*)) (coyurl (gnc:company-info book gnc:*company-url*))
(coyemail (gnc:company-info book gnc:*company-email*)) (coyemail (gnc:company-info book gnc:*company-email*))
(owner (gncInvoiceGetOwner opt-invoice))
(owneraddr (gnc:owner-get-name-and-address-dep owner)) (owneraddr (gnc:owner-get-name-and-address-dep owner))
(billcontact (gncAddressGetName (gnc:owner-get-address owner))) (billcontact (gncAddressGetName (gnc:owner-get-address owner)))
; flags and counters ; flags and counters
(discount? #f) ; any discounts on this invoice? (discount? #f) ; any discounts on this invoice?
(tax? #f) ; any taxable entries on this invoice? (tax? #f) ; any taxable entries on this invoice?
(taxtables? #t) ; are tax tables available in this version?
(payments? #f) ; have any payments been made on this invoice? (payments? #f) ; have any payments been made on this invoice?
(units? #f) ; does any row specify units? (units? #f) ; does any row specify units?
(qty? #f) ; does any row have qty <> 1? (qty? #f) ; does any row have qty <> 1?
(maxcols 5) ; cols of product line (maxcols 5)) ; cols of product line
(no-of-items 0)) ; number of items
; load splits, if any ;; pre-scan invoice entries to look for discounts and taxes
(if (not (null? lot)) (for-each
(set! splits (lambda (entry)
(sort-list (gnc-lot-get-split-list lot) ; sort by date (unless (string-null? (gncEntryGetAction entry)) (set! units? #t))
(lambda (s1 s2) (unless (= 1 (gncEntryGetQuantity entry)) (set! qty? #t))
(let ((t1 (xaccSplitGetParent s1)) (unless (zero? (gncEntryGetInvDiscount entry)) (set! discount? #t))
(t2 (xaccSplitGetParent s2))) (unless (null? (gncEntryGetInvTaxTable entry)) (set! tax? #t)))
(< (xaccTransGetDate t1) entries)
(xaccTransGetDate t2)))))))
; pre-scan invoice entries to look for discounts and taxes ;; pre-scan invoice splits to see if any payments have been made
(for entry in entries do (let lp ((splits splits))
(let ((action (gncEntryGetAction entry)) (cond
(qty (gncEntryGetQuantity entry)) ((null? splits) #f)
(discount (gncEntryGetInvDiscount entry)) ((equal? (xaccSplitGetParent (car splits)) txn) (lp (cdr splits)))
(taxtable (gncEntryGetInvTaxTable entry))) (else (set! payments? #t))))
(set! no-of-items (+ no-of-items 1))
(if (not (string=? action ""))
(set! units? #t))
(if (not (= (gnc-numeric-to-double qty) 1.0))
(set! qty? #t))
(if (not (gnc-numeric-zero-p discount)) (set! discount? #t))
;(if taxable - no, this flag is redundant
(if (not (eq? taxtable '()))
(begin ; presence of a tax table means it's taxed
(set! tax? #t)
(let ((ttentries (gncTaxTableGetEntries taxtable)))
(if (string-prefix? "#<swig-pointer PriceList" (object->string ttentries))
; error in SWIG binding -- disable display of tax details
; (see https://bugs.gnucash.org/show_bug.cgi?id=573645)
(set! taxtables? #f))))))) ; hack required until Swig is fixed
; pre-scan invoice splits to see if any payments have been made
(for split in splits do
(let* ((t (xaccSplitGetParent split)))
(if (not (equal? t txn))
(set! payments? #t))))
?> ?>
@ -174,7 +156,8 @@
(dsc-total (- inv-total tax-total sub-total)) (dsc-total (- inv-total tax-total sub-total))
(total-col (gnc:make-commodity-collector))) (total-col (gnc:make-commodity-collector)))
(total-col 'add currency inv-total) (total-col 'add currency inv-total)
(for entry in entries do (for-each
(lambda (entry)
(let ((qty (gncEntryGetQuantity entry)) (let ((qty (gncEntryGetQuantity entry))
(each (gncEntryGetInvPrice entry)) (each (gncEntryGetInvPrice entry))
(action (gncEntryGetAction entry)) (action (gncEntryGetAction entry))
@ -194,7 +177,7 @@
<td align="right"><?scm:d (format #f "~4,2,,,'0f" (gnc-numeric-to-double each)) ?></td> <td align="right"><?scm:d (format #f "~4,2,,,'0f" (gnc-numeric-to-double each)) ?></td>
<td align="right" nowrap><?scm:d (format #f "~4,2,,,'0f" (gnc-numeric-to-double rval)) ?> <td align="right" nowrap><?scm:d (format #f "~4,2,,,'0f" (gnc-numeric-to-double rval)) ?>
<!-- <td align="right" nowrap><?scm:d (fmtnumeric rval) ?> --> <!-- <td align="right" nowrap><?scm:d (fmtnumeric rval) ?> -->
<?scm (if (and tax? taxtables?) (begin ?> <?scm (if tax? (begin ?>
&nbsp;T &nbsp;T
<?scm ) (begin ?> <?scm ) (begin ?>
&nbsp;&nbsp; &nbsp;&nbsp;
@ -202,20 +185,21 @@
</td> </td>
</tr> </tr>
<?scm (if (not(equal? 0 (string-length (gncEntryGetNotes entry)))) (begin ?> <?scm (if (string-null? (gncEntryGetNotes entry)) (begin ?>
<tr> <tr>
<td align="left">&nbsp;</td> <td align="left">&nbsp;</td>
<td align="left" colspan="<?scm:d (- maxcols 1) ?>"><?scm:d (gncEntryGetNotes entry) ?></td> <td align="left" colspan="<?scm:d (- maxcols 1) ?>"><?scm:d (gncEntryGetNotes entry) ?></td>
</tr> </tr>
<?scm )) ?> <?scm )) ?>
<?scm )) ?> <?scm ))
entries) ?>
<!-- display subtotals row --> <!-- display subtotals row -->
<tr valign="top"> <tr valign="top">
<td align="center" class="total total_first" colspan="<?scm:d maxcols ?>"> <td align="center" class="total total_first" colspan="<?scm:d maxcols ?>">
<?scm:d "Total No. Items:" ?>&nbsp; <?scm:d "Total No. Items:" ?>&nbsp;
<?scm:d no-of-items ?> <?scm:d (length entries) ?>
</td> </td>
</tr> </tr>
@ -240,10 +224,11 @@
<?scm )) ?> <?scm )) ?>
<?scm <?scm
(if payments? (when payments?
(for split in splits do (for-each
(lambda (split)
(let ((t (xaccSplitGetParent split))) (let ((t (xaccSplitGetParent split)))
(if (not (equal? t txn)) ; don't process the entry itself as a split (unless (equal? t txn) ; don't process the entry itself as a split
(let ((c (xaccTransGetCurrency t)) (let ((c (xaccTransGetCurrency t))
(a (xaccSplitGetValue split))) (a (xaccSplitGetValue split)))
(total-col 'add c a) (total-col 'add c a)
@ -253,7 +238,8 @@
<td align="left" colspan="<?scm:d (- maxcols 3) ?>"><?scm:d opt-payment-recd-heading ?></td> <td align="left" colspan="<?scm:d (- maxcols 3) ?>"><?scm:d opt-payment-recd-heading ?></td>
<td align="right" colspan="2"><?scm:d (fmtmoney c a) ?></td> <td align="right" colspan="2"><?scm:d (fmtmoney c a) ?></td>
</tr> </tr>
<?scm ))))) ?> <?scm ))))
splits)) ?>
<!-- total row --> <!-- total row -->
<tr valign="top"> <tr valign="top">
@ -287,18 +273,20 @@
<?scm )) ; end of display-report function <?scm )) ; end of display-report function
; 'mainline' code: check for a valid invoice, then display the report ; 'mainline' code: check for a valid invoice, then display the report
(if (null? opt-invoice) (cond
(begin ((null? opt-invoice)
(display (string-append "<h2>" (_ "Receipt") "</h2>")) (display (string-append "<h2>" (_ "Receipt") "</h2>"))
(display (string-append "<p>" (_ "No invoice has been selected -- please use the Options menu to select one.") "</p>"))) (display (string-append "<p>" (_ "No invoice has been selected -- please use the Options menu to select one.") "</p>")))
(let* ((owner (gncInvoiceGetOwner opt-invoice))
(endowner (gncOwnerGetEndOwner owner)) ((not (eqv? GNC-OWNER-CUSTOMER
(ownertype (gncOwnerGetType endowner))) (gncOwnerGetType
(if (not (eqv? ownertype GNC-OWNER-CUSTOMER)) (gncOwnerGetEndOwner
(begin (gncInvoiceGetOwner opt-invoice)))))
(display (string-append "<h2>" (_ "Receipt") "</h2>")) (display (string-append "<h2>" (_ "Receipt") "</h2>"))
(display (string-append "<p>" (_ "This report is designed for customer (sales) invoices only. Please use the Options menu to select an <em>Invoice</em>, not a Bill or Expense Voucher.") "</p>"))) (display (string-append "<p>" (_ "This report is designed for customer (sales) invoices only. Please use the Options menu to select an <em>Invoice</em>, not a Bill or Expense Voucher.") "</p>")))
(display-report opt-invoice owner endowner ownertype))))
(else
(display-report opt-invoice)))
?> ?>
</div> </div>