Adapt printable invoice report for credit notes

git-svn-id: svn+ssh://svn.gnucash.org/repo/gnucash/trunk@21575 57a11ea4-9604-0410-9ed3-97b8803252fd
This commit is contained in:
Geert Janssens 2011-11-17 22:24:31 +00:00
parent 3338a102e3
commit f1ac0f18ac

View File

@ -126,11 +126,21 @@
(define (make-account-hash) (make-hash-table 23)) (define (make-account-hash) (make-hash-table 23))
(define (update-account-hash hash values) ;; 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 credit-note?)
(for-each (for-each
(lambda (item) (lambda (item)
(let* ((acct (car item)) (let* ((acct (car item))
(val (cdr item)) (val (inv-or-cn-value(cdr item) credit-note?))
(ref (hash-ref hash acct))) (ref (hash-ref hash acct)))
(hash-set! hash acct (if ref (gnc-numeric-add-fixed ref val) val)))) (hash-set! hash acct (if ref (gnc-numeric-add-fixed ref val) val))))
@ -154,14 +164,14 @@
table) table)
(gnc:make-gnc-monetary currency numeric))) (gnc:make-gnc-monetary currency numeric)))
(define (add-entry-row table currency entry column-vector row-style invoice?) (define (add-entry-row table currency entry column-vector row-style cust-doc? credit-note?)
(let* ((row-contents '()) (let* ((row-contents '())
(entry-value (gnc:make-gnc-monetary (entry-value (gnc:make-gnc-monetary
currency currency
(gncEntryReturnValue entry invoice?))) (inv-or-cn-value (gncEntryReturnValue entry cust-doc?) credit-note?)))
(entry-tax-value (gnc:make-gnc-monetary (entry-tax-value (gnc:make-gnc-monetary
currency currency
(gncEntryReturnTaxValue entry invoice?)))) (inv-or-cn-value (gncEntryReturnTaxValue entry cust-doc?) credit-note?))))
(if (date-col column-vector) (if (date-col column-vector)
(addto! row-contents (addto! row-contents
@ -179,19 +189,19 @@
(addto! row-contents (addto! row-contents
(gnc:make-html-table-cell/markup (gnc:make-html-table-cell/markup
"number-cell" "number-cell"
(gncEntryGetQuantity entry)))) (inv-or-cn-value (gncEntryGetQuantity entry) credit-note?))))
(if (price-col column-vector) (if (price-col column-vector)
(addto! row-contents (addto! row-contents
(gnc:make-html-table-cell/markup (gnc:make-html-table-cell/markup
"number-cell" "number-cell"
(gnc:make-gnc-monetary (gnc:make-gnc-monetary
currency (if invoice? (gncEntryGetInvPrice entry) currency (if cust-doc? (gncEntryGetInvPrice entry)
(gncEntryGetBillPrice entry)))))) (gncEntryGetBillPrice entry))))))
(if (discount-col column-vector) (if (discount-col column-vector)
(addto! row-contents (addto! row-contents
(if invoice? (if cust-doc?
(gnc:make-html-table-cell/markup (gnc:make-html-table-cell/markup
"number-cell" "number-cell"
(monetary-or-percent (gncEntryGetInvDiscount entry) (monetary-or-percent (gncEntryGetInvDiscount entry)
@ -201,7 +211,7 @@
(if (tax-col column-vector) (if (tax-col column-vector)
(addto! row-contents (addto! row-contents
(if (if invoice? (if (if cust-doc?
(and (gncEntryGetInvTaxable entry) (and (gncEntryGetInvTaxable entry)
(gncEntryGetInvTaxTable entry)) (gncEntryGetInvTaxTable entry))
(and (gncEntryGetBillTaxable entry) (and (gncEntryGetBillTaxable entry)
@ -321,7 +331,7 @@
(gnc:register-inv-option (gnc:register-inv-option
(gnc:make-simple-boolean-option (gnc:make-simple-boolean-option
(N_ "Display") (N_ "Payments") (N_ "Display") (N_ "Payments")
"tc" (N_ "Display the payments applied to this invoice?") #f)) "tc" (N_ "Display the payments applied to this cust-doc?") #f))
(gnc:register-inv-option (gnc:register-inv-option
(gnc:make-text-option (gnc:make-text-option
@ -340,7 +350,7 @@
gnc:*report-options*) gnc:*report-options*)
(define (make-entry-table invoice options add-order invoice?) (define (make-entry-table invoice options add-order cust-doc? credit-note?)
(define (opt-val section name) (define (opt-val section name)
(gnc:option-value (gnc:option-value
(gnc:lookup-option options section name))) (gnc:lookup-option options section name)))
@ -349,7 +359,8 @@
(display-all-taxes (opt-val "Display" "Individual Taxes")) (display-all-taxes (opt-val "Display" "Individual Taxes"))
(lot (gncInvoiceGetPostedLot invoice)) (lot (gncInvoiceGetPostedLot invoice))
(txn (gncInvoiceGetPostedTxn invoice)) (txn (gncInvoiceGetPostedTxn invoice))
(currency (gncInvoiceGetCurrency invoice))) (currency (gncInvoiceGetCurrency invoice))
(reverse-payments? (not (gncInvoiceAmountPositive invoice))))
(define (colspan monetary used-columns) (define (colspan monetary used-columns)
(cond (cond
@ -385,34 +396,22 @@
(display-subtotal currency used-columns)))))) (display-subtotal currency used-columns))))))
currency-totals))) currency-totals)))
(define (add-payment-row table used-columns split total-collector) (define (add-payment-row table used-columns split total-collector reverse-payments?)
(let* ((t (xaccSplitGetParent split)) (let* ((t (xaccSplitGetParent split))
(currency (xaccTransGetCurrency t)) (currency (xaccTransGetCurrency t))
(invoice (opt-val invoice-page invoice-name)) (invoice (opt-val invoice-page invoice-name))
(owner '()) (owner '())
;; XXX Need to know when to reverse the value ;; Depending on the document type, the payments may need to be sign-reversed
(amt (gnc:make-gnc-monetary currency (xaccSplitGetValue split))) (amt (gnc:make-gnc-monetary currency
(if reverse-payments?
(gnc-numeric-neg(xaccSplitGetValue split))
(xaccSplitGetValue split))))
(payment-style "grand-total") (payment-style "grand-total")
(row '())) (row '()))
; Update to fix bug 564380, payment on bill doubles bill Mike Evans <mikee@saxicola.co.uk> (total-collector 'add
;; Reverse the value when needed (gnc:gnc-monetary-commodity amt)
(if (not (null? invoice)) (gnc:gnc-monetary-amount amt))
(begin
(set! owner (gncInvoiceGetOwner invoice))
(let ((type (gncOwnerGetType
(gncOwnerGetEndOwner owner))))
(cond
((eqv? type GNC-OWNER-CUSTOMER)
(total-collector 'add
(gnc:gnc-monetary-commodity amt)
(gnc:gnc-monetary-amount amt)))
((eqv? type GNC-OWNER-VENDOR)
(total-collector 'add
(gnc:gnc-monetary-commodity amt)
(gnc:gnc-monetary-amount (gnc:monetary-neg amt))))
))))
(if (date-col used-columns) (if (date-col used-columns)
(addto! row (addto! row
@ -473,7 +472,7 @@
(lambda (split) (lambda (split)
(if (not (equal? (xaccSplitGetParent split) txn)) (if (not (equal? (xaccSplitGetParent split) txn))
(add-payment-row table used-columns (add-payment-row table used-columns
split total-collector))) split total-collector reverse-payments?)))
splits))) splits)))
(add-subtotal-row table used-columns total-collector (add-subtotal-row table used-columns total-collector
@ -492,10 +491,10 @@
current current
used-columns used-columns
current-row-style current-row-style
invoice?))) cust-doc? credit-note?)))
(if display-all-taxes (if display-all-taxes
(let ((tax-list (gncEntryReturnTaxValues current invoice?))) (let ((tax-list (gncEntryReturnTaxValues current cust-doc?)))
(update-account-hash acct-hash tax-list)) (update-account-hash acct-hash tax-list))
(tax-collector 'add (tax-collector 'add
(gnc:gnc-monetary-commodity (cdr entry-values)) (gnc:gnc-monetary-commodity (cdr entry-values))
@ -663,7 +662,8 @@
(default-title (_ "Invoice")) (default-title (_ "Invoice"))
(custom-title (opt-val invoice-page "Custom Title")) (custom-title (opt-val invoice-page "Custom Title"))
(title "") (title "")
(invoice? #f)) (cust-doc? #f)
(credit-note? #f))
(define (add-order o) (define (add-order o)
(if (and references? (not (member o orders))) (if (and references? (not (member o orders)))
@ -671,18 +671,29 @@
(if (not (null? invoice)) (if (not (null? invoice))
(begin (begin
(set! owner (gncInvoiceGetOwner invoice)) (let ((type (gncInvoiceGetType invoice)))
(let ((type (gncOwnerGetType
(gncOwnerGetEndOwner owner))))
(cond (cond
((eqv? type GNC-OWNER-CUSTOMER) ((eqv? type GNC-INVOICE-CUST-INVOICE)
(set! invoice? #t)) (set! cust-doc? #t))
((eqv? type GNC-OWNER-VENDOR) ((eqv? type GNC-INVOICE-VEND-INVOICE)
(set! default-title (_ "Bill"))) (set! default-title (_ "Bill")))
((eqv? type GNC-OWNER-EMPLOYEE) ((eqv? type GNC-INVOICE-EMPL-INVOICE)
(set! default-title (_ "Expense Voucher"))))))) (set! default-title (_ "Expense Voucher")))
((eqv? type GNC-INVOICE-CUST-CREDIT-NOTE)
(begin
(set! cust-doc? #t)
(set! credit-note? #t)
(set! default-title (_ "Credit Note"))))
((eqv? type GNC-INVOICE-VEND-CREDIT-NOTE)
(begin
(set! credit-note? #t)
(set! default-title (_ "Credit Note"))))
((eqv? type GNC-INVOICE-EMPL-CREDIT-NOTE)
(begin
(set! credit-note? #t)
(set! default-title (_ "Credit Note"))))))
(set! title (title-string default-title custom-title)) (set! title (title-string default-title custom-title))))
(gnc:html-document-set-title! document (sprintf #f (_"%s #%d") title (gnc:html-document-set-title! document (sprintf #f (_"%s #%d") title
(gncInvoiceGetID invoice))) (gncInvoiceGetID invoice)))
@ -691,7 +702,7 @@
(let ((book (gncInvoiceGetBook invoice))) (let ((book (gncInvoiceGetBook invoice)))
(set! table (make-entry-table invoice (set! table (make-entry-table invoice
(gnc:report-options report-obj) (gnc:report-options report-obj)
add-order invoice?)) add-order cust-doc? credit-note?))
(gnc:html-table-set-style! (gnc:html-table-set-style!
table "table" table "table"