/
invoice.scm
948 lines (802 loc) · 39.2 KB
/
invoice.scm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
;; -*-scheme-*-
;; invoice.scm -- an Invoice Report, used to print a GncInvoice
;;
;; Created by: Derek Atkins <warlord@MIT.EDU>
;; Copyright (c) 2002, 2003 Derek Atkins <warlord@MIT.EDU>
;;
;; This program is free software; you can redistribute it and/or
;; modify it under the terms of the GNU General Public License as
;; published by the Free Software Foundation; either version 2 of
;; the License, or (at your option) any later version.
;;
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;;
;; You should have received a copy of the GNU General Public License
;; along with this program; if not, contact:
;;
;; Free Software Foundation Voice: +1-617-542-5942
;; 51 Franklin Street, Fifth Floor Fax: +1-617-542-2652
;; Boston, MA 02110-1301, USA gnu@gnu.org
(define-module (gnucash report invoice))
(use-modules (srfi srfi-1))
(use-modules (gnucash gnc-module))
(use-modules (gnucash gettext))
(use-modules (gnucash utilities))
(gnc:module-load "gnucash/report/report-system" 0)
(use-modules (gnucash report standard-reports))
(use-modules (gnucash report business-reports))
(define (addif pred . data) (if pred data '()))
(define base-css "/* advanced users only */
.div-align-right { float: right; }
.div-align-right .maybe-align-right { text-align: right }
.entries-table * { border-width: 1px; border-style:solid; border-collapse: collapse}
.entries-table > table { width: 100% }
.company-table > table * { padding: 0px; }
.client-table > table * { padding: 0px; }
.invoice-details-table > table * { padding: 0px; }
")
(define (date-col columns-used)
(vector-ref columns-used 0))
(define (description-col columns-used)
(vector-ref columns-used 1))
(define (action-col columns-used)
(vector-ref columns-used 2))
(define (quantity-col columns-used)
(vector-ref columns-used 3))
(define (price-col columns-used)
(vector-ref columns-used 4))
(define (discount-col columns-used)
(vector-ref columns-used 5))
(define (tax-col columns-used)
(vector-ref columns-used 6))
(define (taxvalue-col columns-used)
(vector-ref columns-used 7))
(define (value-col columns-used)
(vector-ref columns-used 8))
(define (num-columns-required columns-used)
;; count number of columns where (vector-ref columns-used col) is #t
(count identity (vector->list columns-used)))
(define (build-column-used options)
(define (opt-val section name)
(gnc:option-value
(gnc:lookup-option options section name)))
(vector
(opt-val "Display Columns" "Date")
(opt-val "Display Columns" "Description")
(opt-val "Display Columns" "Action")
(opt-val "Display Columns" "Quantity")
(opt-val "Display Columns" "Price")
(opt-val "Display Columns" "Discount")
(opt-val "Display Columns" "Taxable")
(opt-val "Display Columns" "Tax Amount")
(opt-val "Display Columns" "Total")))
(define (make-heading-list column-vector)
(append
(addif (date-col column-vector)
(_ "Date"))
(addif (description-col column-vector)
(_ "Description"))
(addif (action-col column-vector)
(_ "Action"))
(addif (quantity-col column-vector)
(_ "Quantity"))
(addif (price-col column-vector)
(_ "Unit Price"))
(addif (discount-col column-vector)
(_ "Discount"))
(addif (tax-col column-vector)
(_ "Taxable"))
(addif (taxvalue-col column-vector)
(_ "Tax Amount"))
(addif (value-col column-vector)
(_ "Total"))))
(define (monetary-or-percent numeric currency entry-type)
(if (eqv? entry-type GNC-AMT-TYPE-PERCENT)
(string-append (gnc:default-html-gnc-numeric-renderer numeric #f) " " (_ "%"))
(gnc:make-gnc-monetary currency numeric)))
(define layout-key-list
;; Translators: "Their details" refer to the invoice 'other party' details i.e. client/vendor name/address/ID
(list (cons 'client (list (cons 'text (_ "Their details"))
(cons 'tip (_ "Client or vendor name, address and ID"))))
;; Translators: "Our details" refer to the book owner's details i.e. name/address/tax-ID
(cons 'company (list (cons 'text (_ "Our details"))
(cons 'tip (_ "Company name, address and tax-ID"))))
(cons 'invoice (list (cons 'text (_ "Invoice details"))
(cons 'tip (_ "Invoice date, due date, billing ID, terms, job details"))))
(cons 'today (list (cons 'text (_ "Today's date"))
(cons 'tip (_ "Today's date"))))
(cons 'picture (list (cons 'text (_ "Picture"))
(cons 'tip (_ "Picture"))))
;; Translators: "(empty)" refers to invoice header section being left blank
(cons 'none (list (cons 'text (_ "(empty)"))
(cons 'tip (_ "Empty space"))))))
(define variant-list
(list
(cons 'invoice (list (cons '1a 'none)
(cons '1b 'invoice)
(cons '2a 'client)
(cons '2b 'company)
(cons '3a 'none)
(cons '3b 'today)
(cons 'css base-css)))
(cons 'easy-invoice (list (cons '1a 'none)
(cons '1b 'invoice)
(cons '2a 'client)
(cons '2b 'company)
(cons '3a 'none)
(cons '3b 'today)
(cons 'css (string-append base-css "
.invoice-in-progress { color:red }
.invoice-title { font-weight: bold; text-decoration: underline }
.main-table > table { margin: auto }
.invoice-details-table > table { display: block; }
.invoice-notes { margin-top: 20px }
.entries-table > table { min-width: 600px }"))))
(cons 'fancy-invoice (list (cons '1a 'company)
(cons '1b 'invoice)
(cons '2a 'client)
(cons '2b 'company)
(cons '3a 'none)
(cons '3b 'none)
(cons 'css (string-append base-css "
.company-name {font-size: x-large; }
.client-name {font-size: x-large; }"))))))
(define (keylist-get-info keylist key info)
(cdr (assq info (cdr (assq key keylist)))))
(define (keylist->vectorlist keylist)
(map
(lambda (item)
(vector
(car item)
(keylist-get-info keylist (car item) 'text)
(keylist-get-info keylist (car item) 'tip)))
keylist))
(define (multiline-to-html-text str)
;; simple function - splits string containing #\newline into
;; substrings, and convert to a gnc:make-html-text construct which
;; adds gnc:html-markup-br after each substring.
(let loop ((list-of-substrings (string-split str #\newline))
(result '()))
(if (null? list-of-substrings)
(apply gnc:make-html-text (if (null? result) '() (reverse (cdr result))))
(loop (cdr list-of-substrings)
(cons* (gnc:html-markup-br) (car list-of-substrings) result)))))
(define (options-generator variant)
(define gnc:*report-options* (gnc:new-options))
(define (gnc:register-inv-option new-option)
(gnc:register-option gnc:*report-options* new-option))
(gnc:register-inv-option
(gnc:make-invoice-option gnc:pagename-general gnc:optname-invoice-number "x" ""
(lambda () '()) #f))
(gnc:register-inv-option
(gnc:make-string-option
gnc:pagename-general (N_ "Custom Title")
"z" (N_ "A custom string to replace Invoice, Bill or Expense Voucher.")
""))
(gnc:register-inv-option
(gnc:make-text-option
(N_ "Layout") (N_ "CSS") "zz" "CSS code. This field specifies the CSS code
for styling the invoice. Please see the exported report for the CSS class names."
(keylist-get-info variant-list variant 'css)))
(gnc:register-inv-option
(gnc:make-pixmap-option
(N_ "Layout") (N_ "Picture Location") "zy" "Location for Picture"
""))
(gnc:register-inv-option
(gnc:make-simple-boolean-option
(N_ "Display Columns") (N_ "Date")
"b" (N_ "Display the date?") #t))
(gnc:register-inv-option
(gnc:make-simple-boolean-option
(N_ "Display Columns") (N_ "Description")
"d" (N_ "Display the description?") #t))
(gnc:register-inv-option
(gnc:make-simple-boolean-option
(N_ "Display Columns") (N_ "Action")
"g" (N_ "Display the action?") #t))
(gnc:register-inv-option
(gnc:make-simple-boolean-option
(N_ "Display Columns") (N_ "Quantity")
"ha" (N_ "Display the quantity of items?") #t))
(gnc:register-inv-option
(gnc:make-simple-boolean-option
(N_ "Display Columns") (N_ "Price")
"hb" (N_ "Display the price per item?") #t))
(gnc:register-inv-option
(gnc:make-simple-boolean-option
(N_ "Display Columns") (N_ "Discount")
"k" (N_ "Display the entry's discount?") #t))
(gnc:register-inv-option
(gnc:make-simple-boolean-option
(N_ "Display Columns") (N_ "Taxable")
"l" (N_ "Display the entry's taxable status?") #t))
(gnc:register-inv-option
(gnc:make-simple-boolean-option
(N_ "Display Columns") (N_ "Tax Amount")
"m" (N_ "Display each entry's total total tax?") #f))
(gnc:register-inv-option
(gnc:make-simple-boolean-option
(N_ "Display Columns") (N_ "Total")
"n" (N_ "Display the entry's value?") #t))
;; company details can now be toggled via Layout tab
;; and IMHO company-tax-id should be rendered if present
(gnc:register-inv-option (gnc:make-internal-option "Display" "My Company" #f))
(gnc:register-inv-option (gnc:make-internal-option "Display" "My Company ID" #f))
(gnc:register-inv-option
(gnc:make-simple-boolean-option
(N_ "Display") (N_ "Due Date")
"c" (N_ "Display due date?") #t))
(gnc:register-inv-option
(gnc:make-simple-boolean-option
(N_ "Display") (N_ "Subtotal")
"d" (N_ "Display the subtotals?") #t))
(gnc:register-inv-option
(gnc:make-complex-boolean-option
(N_ "Display") (N_ "Payable to")
"ua1" (N_ "Display the Payable to: information.") #f #f
(lambda (x)
(gnc-option-db-set-option-selectable-by-name
gnc:*report-options* "Display" "Payable to string" x))))
(gnc:register-inv-option
(gnc:make-text-option
(N_ "Display") (N_ "Payable to string")
"ua2" (N_ "The phrase for specifying to whom payments should be made.")
(_ "Please make all checks payable to")))
(gnc:register-inv-option
(gnc:make-complex-boolean-option
(N_ "Display") (N_ "Company contact")
"ub1" (N_ "Display the Company contact information.") #f #f
(lambda (x) (gnc-option-db-set-option-selectable-by-name
gnc:*report-options* "Display" "Company contact string" x))))
(gnc:register-inv-option
(gnc:make-text-option
(N_ "Display") (N_ "Company contact string")
"ub2" (N_ "The phrase used to introduce the company contact.")
(_ "Please direct all enquiries to")))
(gnc:register-inv-option
(gnc:make-number-range-option
(N_ "Display") (N_ "Minimum # of entries")
"zz" (N_ "The minimum number of invoice entries to display.") 1
0 23 0 1))
(gnc:register-inv-option
(gnc:make-simple-boolean-option
(N_ "Display") (N_ "Use Detailed Tax Summary")
"o" (N_ "Display all tax categories separately (one per line) instead of one single tax line.?") #f))
(gnc:register-inv-option (gnc:make-internal-option "Display" "Totals" #t))
(gnc:register-inv-option
(gnc:make-simple-boolean-option
(N_ "Display") (N_ "References")
"s" (N_ "Display the invoice references?") #t))
(gnc:register-inv-option
(gnc:make-simple-boolean-option
(N_ "Display") (N_ "Billing Terms")
"t" (N_ "Display the invoice billing terms?") #t))
(gnc:register-inv-option
(gnc:make-simple-boolean-option
(N_ "Display") (N_ "Billing ID")
"ta" (N_ "Display the billing id?") #t))
(gnc:register-inv-option
(gnc:make-simple-boolean-option
(N_ "Display") (N_ "Invoice owner ID")
"tam" (N_ "Display the customer/vendor id?") #f))
(gnc:register-inv-option
(gnc:make-simple-boolean-option
(N_ "Display") (N_ "Invoice Notes")
"tb" (N_ "Display the invoice notes?") #f))
(gnc:register-inv-option
(gnc:make-simple-boolean-option
(N_ "Display") (N_ "Payments")
"tc" (N_ "Display the payments applied to this invoice?") #t))
(gnc:register-inv-option
(gnc:make-simple-boolean-option
(N_ "Display") (N_ "Job Details")
"td" (N_ "Display the job name for this invoice?") #f))
(gnc:register-inv-option
(gnc:make-text-option
(N_ "Display") (N_ "Extra Notes")
"u" (N_ "Extra notes to put on the invoice.")
(_ "Thank you for your patronage!")))
(gnc:register-inv-option
(gnc:make-multichoice-option
(N_ "Layout") (N_ "Row 1 Left")
"1a" "1st row, left"
(keylist-get-info variant-list variant '1a)
(keylist->vectorlist layout-key-list)))
(gnc:register-inv-option
(gnc:make-multichoice-option
(N_ "Layout") (N_ "Row 1 Right")
"1b" "1st row, right"
(keylist-get-info variant-list variant '1b)
(keylist->vectorlist layout-key-list)))
(gnc:register-inv-option
(gnc:make-multichoice-option
(N_ "Layout") (N_ "Row 2 Left")
"2a" "2nd row, left"
(keylist-get-info variant-list variant '2a)
(keylist->vectorlist layout-key-list)))
(gnc:register-inv-option
(gnc:make-multichoice-option
(N_ "Layout") (N_ "Row 2 Right")
"2b" "2nd row, right"
(keylist-get-info variant-list variant '2b)
(keylist->vectorlist layout-key-list)))
(gnc:register-inv-option
(gnc:make-multichoice-option
(N_ "Layout") (N_ "Row 3 Left")
"3a" "3rd row, left"
(keylist-get-info variant-list variant '3a)
(keylist->vectorlist layout-key-list)))
(gnc:register-inv-option
(gnc:make-multichoice-option
(N_ "Layout") (N_ "Row 3 Right")
"3b" "3rd row, right"
(keylist-get-info variant-list variant '3b)
(keylist->vectorlist layout-key-list)))
(gnc:options-set-default-section gnc:*report-options* "General")
gnc:*report-options*)
(define (make-entry-table invoice options cust-doc? credit-note?)
(define (opt-val section name)
(gnc:option-value
(gnc:lookup-option options section name)))
(let ((show-payments (opt-val "Display" "Payments"))
(display-all-taxes (opt-val "Display" "Use Detailed Tax Summary"))
(display-subtotal? (opt-val "Display" "Subtotal"))
(lot (gncInvoiceGetPostedLot invoice))
(txn (gncInvoiceGetPostedTxn invoice))
(currency (gncInvoiceGetCurrency invoice))
(reverse-payments? (not (gncInvoiceAmountPositive invoice))))
(define (display-subtotal monetary used-columns)
(if (value-col used-columns)
monetary
(let ((amt (gnc:gnc-monetary-amount monetary)))
(if amt
(if (negative? amt)
(gnc:monetary-neg monetary)
monetary)
monetary))))
(define (add-payment-row table used-columns split total-collector reverse-payments?)
(let* ((t (xaccSplitGetParent split))
(currency (xaccTransGetCurrency t))
;; Depending on the document type, the payments may need to be sign-reversed
(amt (gnc:make-gnc-monetary currency
(if reverse-payments?
(- (xaccSplitGetValue split))
(xaccSplitGetValue split)))))
(total-collector 'add
(gnc:gnc-monetary-commodity amt)
(gnc:gnc-monetary-amount amt))
(gnc:html-table-append-row/markup!
table "grand-total"
(append
(addif (date-col used-columns)
(qof-print-date (xaccTransGetDate t)))
(addif (description-col used-columns)
(_ "Payment, thank you!"))
(list (gnc:make-html-table-cell/size/markup
1 (- (max 3 (num-columns-required used-columns))
(if (date-col used-columns) 1 0)
(if (description-col used-columns) 1 0))
"total-number-cell"
(display-subtotal amt used-columns)))))))
(let* ((table (gnc:make-html-table))
(used-columns (build-column-used options))
(entries (gncInvoiceGetEntries invoice)))
(define (add-entry-row entry row-style)
(gnc:html-table-append-row/markup!
table row-style
(append
(addif (date-col used-columns)
(qof-print-date (gncEntryGetDate entry)))
(addif (description-col used-columns)
(gncEntryGetDescription entry))
(addif (action-col used-columns)
(gncEntryGetAction entry))
(addif (quantity-col used-columns)
(gnc:make-html-table-cell/markup
"number-cell"
(gncEntryGetDocQuantity entry credit-note?)))
(addif (price-col used-columns)
(gnc:make-html-table-cell/markup
"number-cell"
(gnc:make-gnc-monetary
currency (if cust-doc?
(gncEntryGetInvPrice entry)
(gncEntryGetBillPrice entry)))))
(addif (discount-col used-columns)
(if cust-doc?
(gnc:make-html-table-cell/markup
"number-cell"
(monetary-or-percent (gncEntryGetInvDiscount entry)
currency
(gncEntryGetInvDiscountType entry)))
""))
(addif (tax-col used-columns)
(if (if cust-doc?
(and (gncEntryGetInvTaxable entry)
(gncEntryGetInvTaxTable entry))
(and (gncEntryGetBillTaxable entry)
(gncEntryGetBillTaxTable entry)))
;; Translators: This "T" is displayed in the taxable column, if this entry contains tax
(_ "T") ""))
(addif (taxvalue-col used-columns)
(gnc:make-html-table-cell/markup
"number-cell"
(gnc:make-gnc-monetary
currency (gncEntryGetDocTaxValue entry #t cust-doc? credit-note?))))
(addif (value-col used-columns)
(gnc:make-html-table-cell/markup
"number-cell"
(gnc:make-gnc-monetary
currency (gncEntryGetDocValue entry #t cust-doc? credit-note?)))))))
(define (add-subtotal-row subtotal subtotal-style subtotal-label)
(gnc:html-table-append-row/markup!
table subtotal-style
(list (gnc:make-html-table-cell/markup
"total-label-cell" subtotal-label)
(gnc:make-html-table-cell/size/markup
1 (max 3 (num-columns-required used-columns))
"total-number-cell"
(display-subtotal (gnc:make-gnc-monetary currency subtotal) used-columns)))))
(gnc:html-table-set-col-headers! table
(make-heading-list used-columns))
(let do-rows-with-subtotals ((entries entries)
(odd-row? #t)
(num-entries 0))
(if (null? entries)
;; all entries done, add subtotals
(let ((total-collector (gnc:make-commodity-collector)))
;; minimum number of entries- replicating fancy-invoice option
(let loop ((num-entries-left (- (opt-val "Display" "Minimum # of entries" ) num-entries))
(odd-row? odd-row?))
(when (positive? num-entries-left)
(gnc:html-table-append-row/markup!
table (if odd-row? "normal-row" "alternate-row")
(gnc:html-make-empty-cells (num-columns-required used-columns)))
(loop (1- num-entries-left)
(not odd-row?))))
(if display-subtotal?
(add-subtotal-row (gncInvoiceGetTotalSubtotal invoice)
"grand-total" (_ "Net Price")))
(if display-all-taxes
(for-each
(lambda (parm)
(let ((value (cdr parm))
(acct (car parm)))
(add-subtotal-row value
"grand-total" (xaccAccountGetName acct))))
(gncInvoiceGetTotalTaxList invoice))
;; nope, just show the total tax.
(add-subtotal-row (gncInvoiceGetTotalTax invoice)
"grand-total" (_ "Tax")))
(add-subtotal-row (gncInvoiceGetTotal invoice)
"grand-total" (_ "Total Price"))
(total-collector 'add currency (gncInvoiceGetTotal invoice))
(if (and show-payments (not (null? lot)))
(let ((splits (sort-list!
(gnc-lot-get-split-list lot)
(lambda (s1 s2)
(let ((t1 (xaccSplitGetParent s1))
(t2 (xaccSplitGetParent s2)))
(< (xaccTransOrder t1 t2) 0))))))
(for-each
(lambda (split)
(if (not (equal? (xaccSplitGetParent split) txn))
(add-payment-row table used-columns
split total-collector
reverse-payments?)))
splits)))
(add-subtotal-row (cadr (total-collector 'getpair currency #f))
"grand-total" (_ "Amount Due")))
(begin
(add-entry-row (car entries)
(if odd-row? "normal-row" "alternate-row"))
(do-rows-with-subtotals (cdr entries)
(not odd-row?)
(1+ num-entries)))))
table)))
(define (make-invoice-details-table invoice options)
;; dual-column. invoice date/due, billingID, terms, job name/number
(define (opt-val section name)
(gnc:option-value
(gnc:lookup-option options section name)))
(let* ((invoice-details-table (gnc:make-html-table))
(book (gncInvoiceGetBook invoice))
(date-format (gnc:options-fancy-date book))
(jobnumber (gncJobGetID (gncOwnerGetJob (gncInvoiceGetOwner invoice))))
(jobname (gncJobGetName (gncOwnerGetJob (gncInvoiceGetOwner invoice)))))
(if (gncInvoiceIsPosted invoice)
(begin
(gnc:html-table-append-row!
invoice-details-table
(make-date-row (_ "Date") (gncInvoiceGetDatePosted invoice) date-format))
(if (opt-val "Display" "Due Date")
(gnc:html-table-append-row!
invoice-details-table
(make-date-row (_ "Due Date") (gncInvoiceGetDateDue invoice) date-format))))
(gnc:html-table-append-row! invoice-details-table
(gnc:make-html-table-cell/size
1 2 (gnc:make-html-span/markup
"invoice-in-progress"
(gnc:make-html-text
(_ "Invoice in progress..."))))))
(if (opt-val "Display" "Billing ID")
(let ((billing-id (gncInvoiceGetBillingID invoice)))
(if (and billing-id (not (string-null? billing-id)))
(begin
(gnc:html-table-append-row! invoice-details-table
(list
(_ "Reference:")
(gnc:make-html-div/markup
"div-align-right"
(multiline-to-html-text billing-id))))
(gnc:html-table-append-row! invoice-details-table '())))))
(if (opt-val "Display" "Billing Terms")
(let* ((term (gncInvoiceGetTerms invoice))
(terms (gncBillTermGetDescription term)))
(if (and terms (not (string-null? terms)))
(gnc:html-table-append-row! invoice-details-table
(list
(_ "Terms:")
(gnc:make-html-div/markup
"div-align-right"
(multiline-to-html-text terms)))))))
;; Add job number and name to invoice if requested and if it exists
(if (and (opt-val "Display" "Job Details")
(not (string-null? jobnumber)))
(begin
(gnc:html-table-append-row! invoice-details-table
(list (_ "Job number:")
(gnc:make-html-div/markup
"div-align-right"
jobnumber)))
(gnc:html-table-append-row! invoice-details-table
(list (_ "Job name:")
(gnc:make-html-div/markup
"div-align-right"
jobname)))))
invoice-details-table))
(define (make-img img-url)
;; just an image
(gnc:make-html-text
(gnc:html-markup-img
(make-file-url img-url))))
(define (make-client-table owner orders options)
(define (opt-val section name)
(gnc:option-value
(gnc:lookup-option options section name)))
;; this is a single-column table.
(let ((table (gnc:make-html-table)))
(gnc:html-table-append-row! table
(list
(gnc:make-html-div/markup
"maybe-align-right client-name"
(gnc:owner-get-name-dep owner))))
(gnc:html-table-append-row! table
(list
(gnc:make-html-div/markup
"maybe-align-right client-address"
(multiline-to-html-text
(gnc:owner-get-address-dep owner)))))
(if (opt-val "Display" "Invoice owner ID")
(gnc:html-table-append-row! table
(list
(gnc:make-html-div/markup
"maybe-align-right client-id"
(multiline-to-html-text
(gnc:owner-get-owner-id owner))))))
(for-each
(lambda (order)
(let ((reference (gncOrderGetReference order)))
(if (and reference (not (string-null? reference)))
(gnc:html-table-append-row! table
(list (string-append
(_ "REF") " "
reference))))))
orders)
table))
(define (make-date-row label date date-format)
(list
(string-append label ":")
(gnc:make-html-div/markup
"div-align-right"
(gnc-print-time64 date date-format))))
(define (make-company-table book)
;; single-column table. my name, address, and printdate
(let* ((table (gnc:make-html-table))
(name (gnc:company-info book gnc:*company-name*))
(addy (gnc:company-info book gnc:*company-addy*))
(phone (gnc:company-info book gnc:*company-phone*))
(fax (gnc:company-info book gnc:*company-fax*))
(email (gnc:company-info book gnc:*company-email*))
(url (gnc:company-info book gnc:*company-url*))
(taxid (gnc:company-info book gnc:*company-id*)))
(if (and name (not (string-null? name)))
(gnc:html-table-append-row! table (list
(gnc:make-html-div/markup
"maybe-align-right company-name" name))))
(if (and addy (not (string-null? addy)))
(gnc:html-table-append-row! table (list
(gnc:make-html-div/markup
"maybe-align-right company-address" (multiline-to-html-text addy)))))
(if (and phone (not (string-null? phone)))
(gnc:html-table-append-row! table (list
(gnc:make-html-div/markup
"maybe-align-right company-phone" phone))))
(if (and fax (not (string-null? fax)))
(gnc:html-table-append-row! table (list
(gnc:make-html-div/markup
"maybe-align-right company-fax" fax))))
(if (and email (not (string-null? email)))
(gnc:html-table-append-row! table (list
(gnc:make-html-div/markup
"maybe-align-right company-email" email))))
(if (and url (not (string-null? url)))
(gnc:html-table-append-row! table (list
(gnc:make-html-div/markup
"maybe-align-right company-url" url))))
(if (and taxid (not (string-null? taxid)))
(gnc:html-table-append-row! table (list
(gnc:make-html-div/markup
"maybe-align-right company-tax-id" taxid))))
table))
(define (reg-renderer report-obj)
(let* ((document (gnc:make-html-document))
(options (gnc:report-options report-obj))
(opt-val (lambda (section name) (gnc:option-value (gnc:lookup-option options section name))))
(invoice (opt-val gnc:pagename-general gnc:optname-invoice-number))
(references? (opt-val "Display" "References"))
(custom-title (opt-val gnc:pagename-general "Custom Title")))
(if (null? invoice)
(gnc:html-document-add-object! document
(gnc:make-html-text
(_ "No valid invoice selected. Click on the Options button and select the invoice to use.")))
(let* ((book (gncInvoiceGetBook invoice))
(owner (gncInvoiceGetOwner invoice))
(type (gncInvoiceGetType invoice))
(orders (if references? (delete-duplicates (map gncEntryGetOrder (gncInvoiceGetEntries invoice))) '()))
(cust-doc? (memv type (list GNC-INVOICE-CUST-INVOICE GNC-INVOICE-CUST-CREDIT-NOTE)))
(credit-note? (memv type (list GNC-INVOICE-CUST-CREDIT-NOTE GNC-INVOICE-VEND-CREDIT-NOTE GNC-INVOICE-EMPL-CREDIT-NOTE)))
(default-title (cond
((eqv? type GNC-INVOICE-VEND-INVOICE)
(_ "Bill"))
((eqv? type GNC-INVOICE-EMPL-INVOICE)
(_ "Expense Voucher"))
((memv type (list GNC-INVOICE-CUST-CREDIT-NOTE
GNC-INVOICE-VEND-CREDIT-NOTE
GNC-INVOICE-EMPL-CREDIT-NOTE))
(_ "Credit Note"))
(else
(_ "Invoice"))))
(title (if (string-null? custom-title) default-title custom-title))
;; Translators: This is the format of the invoice title.
;; The first ~a is "Invoice", "Credit Note"... and the second the number.
;; Replace " #" by whatever is common as number abbreviation, i.e. "~a Nr. ~a"
(invoice-title (format #f (_"~a #~a") title (gncInvoiceGetID invoice)))
(layout-lookup-table (list (cons 'none #f)
(cons 'picture (gnc:make-html-div/markup
"picture"
(make-img (opt-val "Layout" "Picture Location"))))
(cons 'invoice (gnc:make-html-div/markup
"invoice-details-table"
(make-invoice-details-table
invoice options)))
(cons 'client (gnc:make-html-div/markup
"client-table"
(make-client-table
owner orders options)))
(cons 'company (gnc:make-html-div/markup
"company-table"
(make-company-table book)))
(cons 'today (gnc:make-html-div/markup
"invoice-print-date"
(qof-print-date (current-time))))))
(layout-lookup (lambda (loc) (cdr (assq (opt-val "Layout" loc) layout-lookup-table)))))
(gnc:html-document-set-style-text! document (opt-val "Layout" "CSS"))
(let ((main-table (gnc:make-html-table)))
(gnc:html-table-append-row! main-table
(gnc:make-html-table-cell/size
1 2 (gnc:make-html-div/markup
"invoice-title" invoice-title)))
(gnc:html-table-append-row! main-table
(list (layout-lookup "Row 1 Left")
(gnc:make-html-div/markup
"div-align-right"
(layout-lookup "Row 1 Right"))))
(gnc:html-table-append-row! main-table
(list (layout-lookup "Row 2 Left")
(gnc:make-html-div/markup
"div-align-right"
(layout-lookup "Row 2 Right"))))
(gnc:html-table-append-row! main-table
(list (layout-lookup "Row 3 Left")
(gnc:make-html-div/markup
"div-align-right"
(layout-lookup "Row 3 Right"))))
(gnc:html-table-append-row! main-table
(gnc:make-html-table-cell/size
1 2 (gnc:make-html-div/markup
"entries-table"
(make-entry-table invoice options
cust-doc? credit-note?))))
(if (opt-val "Display" "Invoice Notes")
(let ((notes (gncInvoiceGetNotes invoice)))
(gnc:html-table-append-row! main-table
(gnc:make-html-table-cell/size
1 2 (gnc:make-html-div/markup
"invoice-notes"
(multiline-to-html-text notes))))))
(if (opt-val "Display" "Payable to")
(let* ((name (gnc:company-info book gnc:*company-name*))
(name-str (opt-val "Display" "Payable to string")))
(if (and name (not (string-null? name)))
(gnc:html-table-append-row!
main-table
(gnc:make-html-div/markup
"invoice-footer-payable-to"
(multiline-to-html-text
(string-append name-str ": " name)))))))
(if (opt-val "Display" "Company contact")
(let* ((contact (gnc:company-info book gnc:*company-contact*))
(contact-str (opt-val "Display" "Company contact string")))
(if (and contact (not (string-null? contact)))
(gnc:html-table-append-row!
main-table
(gnc:make-html-div/markup
"invoice-footer-company-contact"
(multiline-to-html-text
(string-append contact-str ": " contact)))))))
(gnc:html-table-append-row! main-table
(gnc:make-html-table-cell/size
1 2 (gnc:make-html-div/markup
"invoice-notes"
(multiline-to-html-text
(opt-val "Display" "Extra Notes")))))
(gnc:html-document-add-object! document (gnc:make-html-div/markup
"main-table" main-table)))))
document))
(define invoice-report-guid "5123a759ceb9483abf2182d01c140e8d")
(define easy-invoice-guid "67112f318bef4fc496bdc27d106bbda4")
(define fancy-invoice-guid "3ce293441e894423a2425d7a22dd1ac6")
(gnc:define-report
'version 1
'name (N_ "Printable Invoice")
'report-guid invoice-report-guid
'menu-path (list gnc:menuname-business-reports)
'options-generator (lambda () (options-generator 'invoice))
'renderer reg-renderer
'in-menu? #t)
(gnc:define-report
'version 1
'name (N_ "Easy Invoice")
'report-guid easy-invoice-guid
'menu-path (list gnc:menuname-business-reports)
'options-generator (lambda () (options-generator 'easy-invoice))
'renderer reg-renderer
'in-menu? #t)
(gnc:define-report
'version 1
'name (N_ "Fancy Invoice")
'report-guid fancy-invoice-guid
'menu-path (list gnc:menuname-business-reports)
'options-generator (lambda () (options-generator 'fancy-invoice))
'renderer reg-renderer
'in-menu? #t)
(define (gnc:easy-invoice-report-create-internal invoice)
(issue-deprecation-warning
"gnc:easy-invoice-report-create-internal is unused")
(let* ((options (gnc:make-report-options easy-invoice-guid))
(invoice-op (gnc:lookup-option options gnc:pagename-general gnc:optname-invoice-number)))
(gnc:option-set-value invoice-op invoice)
(gnc:make-report easy-invoice-guid options)))
(export gnc:easy-invoice-report-create-internal)
(define (gnc:fancy-invoice-report-create-internal invoice)
(issue-deprecation-warning
"gnc:fancy-invoice-report-create-internal is unused")
(let* ((options (gnc:make-report-options fancy-invoice-guid))
(invoice-op (gnc:lookup-option options gnc:pagename-general gnc:optname-invoice-number)))
(gnc:option-set-value invoice-op invoice)
(gnc:make-report fancy-invoice-guid options)))
(export gnc:fancy-invoice-report-create-internal)