-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpjb-invoices.el.restore
2512 lines (2099 loc) · 80.7 KB
/
pjb-invoices.el.restore
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
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
;;******************************************************************************
;;FILE: pjb-invoices.el
;;LANGUAGE: emacs lisp
;;SYSTEM: emacs
;;USER-INTERFACE: emacs
;;DESCRIPTION
;;
;; This module exports classes and functions used for accounting:
;; invoices, customers/providers, movements, taxes...
;;
;;AUTHORS
;; <PJB> Pascal J. Bourguignon
;;MODIFICATIONS
;; 199?-??-?? <PJB> Creation.
;; 2002-09-09 <PJB> Added generate-invoice.
;;BUGS
;;LEGAL
;; LGPL
;;
;; Copyright Pascal J. Bourguignon 1990 - 2002
;;
;; This library is free software; you can redistribute it and/or
;; modify it under the terms of the GNU Lesser General Public
;; License as published by the Free Software Foundation; either
;; version 2 of the License, or (at your option) any later version.
;;
;; This library 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
;; Lesser General Public License for more details.
;;
;; You should have received a copy of the GNU Lesser General Public
;; License along with this library; if not, write to the Free Software
;; Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
;;
;;******************************************************************************
(require 'pjb-euro)
(require 'pjb-lists)
(require 'pjb-strings)
(require 'pjb-object)
(require 'pjb-cl)
(provide 'pjb-invoices)
(defconstant vat-rates '(0.00 0.04 0.07 0.16) "The valid VAT rates.")
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; BankReference
(defclass BankReference (PjbObject)
(
(bank-name
:initform nil
:initarg :bank-name
:accessor bank-name
:type (or null string)
:documentation
"The name of the bank.")
(bank-address
:initform nil
:initarg :bank-address
:accessor bank-address
:type (or null string)
:documentation
"The address of the bank.")
(branch-name
:initform nil
:initarg :branch-name
:accessor branch-name
:type (or null string)
:documentation
"The name of the branch.")
(swift-code
:initform nil
:initarg :swift-code
:accessor swift-code
:type (or null string)
:documentation
"The swift-code of the bank.")
(account-number
:initform nil
:initarg :account-number
:accessor account-number
:type (or null string)
:documentation
"The account number.")
(beneficiary-name
:initform nil
:initarg :beneficiary-name
:accessor beneficiary-name
:type (or null string)
:documentation
"The beneficiary's name.")
)
(:documentation
"A bank account reference.")
);;BankReference
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; FiscalPerson
(defclass FiscalPerson (PjbObject)
(
(fiscal-id
:initform nil
:initarg :fiscal-id
:accessor fiscal-id
:type (or null string)
:documentation
"The fiscal ID of the person.")
(name
:initform nil
:initarg :name
:accessor name
:type (or null string)
:documentation
"The name of the person.")
(address
:initform nil
:initarg :address
:accessor address
:type (or null string)
:documentation
"The address of the person.")
(phone
:initform nil
:initarg :phone
:accessor phone
:type (or null string)
:documentation
"The phone number of the person.")
(fax
:initform nil
:initarg :fax
:accessor fax
:type (or null string)
:documentation
"The fax number of the person.")
(email
:initform nil
:initarg :email
:accessor email
:type (or null string)
:documentation
"The fax number of the person.")
(bank-reference
:initform nil
:initarg :bank-reference
:accessor bank-reference
:type (or null BankReference)
:documentation
"The bank reference of the person.")
(language
:initform :es
:initarg :language
:accessor language
:type symbol;; :es :en :fr :de
:documentation
"The language (two-letter code) used by this person.")
)
(:documentation
"A person (physical or moral) identified by a fiscal identification number."
))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; InvoiceLine
(defclass InvoiceLine (PjbObject)
((description
:initform ""
:initarg :description
:accessor description
:type string
:documentation
"The description of this line.")
(devise
:initform :EUR
:initarg :devise
:accessor devise
:type symbol
:documentation
"The devise of this line.")
(amount-ht
:initform 0.00
:initarg :amount-ht
:accessor amount-ht
:type number
:documentation
"The amount excluding the taxes of this line.")
(vat-rate
:initform 0.00
:initarg :vat-rate
:accessor vat-rate
:type number
:documentation
"The rate of VAT for this line (0.00 <= vat-rate <= 0.50).")
(amount-vat
:initform 0.00
:initarg :amount-vat
:accessor amount-vat
:type number
:documentation
"The amount of VAT for this line. ( = amount-ht * (1+vat-rate) )")
(amount-ttc
:initform 0.00
:initarg :amount-ttc
:accessor amount-ttc
:type number
:documentation
"The amount including the taxes of this line.")
)
(:documentation
"An Invoice Line."))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Invoice
(defclass Invoice (PjbObject)
((date
:initform nil
:initarg :date
:accessor date
:type (or null string)
:documentation
"'YYYY-MM-DD' The date of the invoice.")
(issuer-fiscal-id
:initform nil
:initarg :issuer-fiscal-id
:accessor issuer-fiscal-id
:type (or null string)
:documentation
"The fiscal ID of the issuer of this invoice.")
(invoice-number
:initform nil
:initarg :invoice-number
:accessor invoice-number
:type (or null string)
:documentation
"The invoice number.")
(payer-fiscal-id
:initform nil
:initarg :payer-fiscal-id
:accessor payer-fiscal-id
:type (or null string)
:documentation
"The fiscal ID of the payer of this invoice.")
(title
:initform ""
:initarg :title
:accessor title
:type (or null string)
:documentation
"The title of this invoice.")
(devise
:initform :EUR
:initarg :devise
:accessor devise
:type symbol
:documentation
"The devise of this invoice.")
(lines
:initform nil
:accessor lines
:type list
:documentation
"(list of InvoiceLine) The line items of this invoice.")
(total-ht
:initform 0.00
:accessor total-ht
:type number
:documentation
"The total excluding taxes of this invoice.")
(total-vat
:initform 0.00
:accessor total-vat
:type number
:documentation
"The total of VAT.")
(total-ttc
:initform 0.00
:accessor total-ttc
:type number
:documentation
"The total including taxes of this invoice.")
)
(:documentation
"An invoice, either outgoing or incoming.
The amounts of the invoice may be negative when it's a refund.
"))
(defvar invoice-directory "/home/pascal/jobs/free-lance/invoices"
"The directory where the generated invoices are stored.")
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; InvoiceSet
(defclass InvoiceSet (PjbObject)
((fiscal-id
:initform nil
:initarg :fiscal-id
:accessor fiscal-id
:type (or null string)
:documentation
"The fiscal id of the owner of this invoice set.")
(fisc-fiscal-ids
:initform nil
:initarg :fisc-fiscal-ids
:accessor fisc-fiscal-ids
:type list
:documentation
"(list of string) List of fiscal-id of fisc entity. An invoice issued by
on of these entities is actually a tax.")
(persons
:initform nil
:initarg :persons
:accessor persons
:type list
:documentation
"The list of known FiscalPerson.")
(invoices
:initform nil
:initarg :invoices
:accessor invoices
:type list
:documentation
"The list of known Invoices.")
)
(:documentation
"This class gather all the data sets about invoices and fiscal persons."))
(defconstant invoice-set-file-path "~/.pjb-invoices"
"Path to the file where invoices data is stored.")
(defvar invoice-set (make-instance InvoiceSet
:object-id "Default Invoice Set")
"Default Invoice Set.")
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; InvoiceLine
;; check = ( a-ttc | a-ht ) & ( a-vat | vat-rate )
;; error = ~check | ( ~a-ttc & ~a-ht ) | ( ~a-vat & ~vat-rate ) )
;; (insert
;; (carnot '( a-ttc a-ht a-vat vat-rate check)
;; '((check . (lambda (a-ttc a-ht a-vat vat-rate check)
;; (and (or a-ttc a-ht) (or a-vat vat-rate))
;; ))
;; (error . (lambda (a-ttc a-ht a-vat vat-rate check)
;; (or (not check)
;; (and (not a-ttc) (not a-ht))
;; (and (not a-vat) (not vat-rate)))
;; ))
;; ))
;; )
;; +-------+------+-------+----------+-------+-------+-------+
;; | a-ttc | a-ht | a-vat | vat-rate | check | check | error |
;; +-------+------+-------+----------+-------+-------+-------+
;; | OUI | OUI | OUI | OUI | OUI | × | · |
;; | OUI | OUI | OUI | OUI | NON | × | × |
;; | OUI | OUI | OUI | NON | OUI | × | · |
;; | OUI | OUI | OUI | NON | NON | × | × |
;; | OUI | OUI | NON | OUI | OUI | × | · |
;; | OUI | OUI | NON | OUI | NON | × | × |
;; | OUI | OUI | NON | NON | OUI | · | × |
;; | OUI | OUI | NON | NON | NON | · | × |
;; | OUI | NON | OUI | OUI | OUI | × | · |
;; | OUI | NON | OUI | OUI | NON | × | × |
;; | OUI | NON | OUI | NON | OUI | × | · |
;; | OUI | NON | OUI | NON | NON | × | × |
;; | OUI | NON | NON | OUI | OUI | × | · |
;; | OUI | NON | NON | OUI | NON | × | × |
;; | OUI | NON | NON | NON | OUI | · | × |
;; | OUI | NON | NON | NON | NON | · | × |
;; | NON | OUI | OUI | OUI | OUI | × | · |
;; | NON | OUI | OUI | OUI | NON | × | × |
;; | NON | OUI | OUI | NON | OUI | × | · |
;; | NON | OUI | OUI | NON | NON | × | × |
;; | NON | OUI | NON | OUI | OUI | × | · |
;; | NON | OUI | NON | OUI | NON | × | × |
;; | NON | OUI | NON | NON | OUI | · | × |
;; | NON | OUI | NON | NON | NON | · | × |
;; | NON | NON | OUI | OUI | OUI | · | × |
;; | NON | NON | OUI | OUI | NON | · | × |
;; | NON | NON | OUI | NON | OUI | · | × |
;; | NON | NON | OUI | NON | NON | · | × |
;; | NON | NON | NON | OUI | OUI | · | × |
;; | NON | NON | NON | OUI | NON | · | × |
;; | NON | NON | NON | NON | OUI | · | × |
;; | NON | NON | NON | NON | NON | · | × |
;; +-------+------+-------+----------+-------+-------+-------+
(defmethod shared-initialize ((self InvoiceLine) fields)
"
DOES: Checks that the values for the fields are within limits.
"
;; (mapc (lambda (a) (let ((slot (intern (concat ":" (symbol-name a)))))
;; (printf "(%-20s (plist-get fields %s))\n" a slot)))
;; (class-attributes InvoiceLine))
(let (
(object-id (plist-get fields :object-id))
(description (plist-get fields :description))
(devise (plist-get fields :devise))
(amount-ht (plist-get fields :amount-ht))
(vat-rate (plist-get fields :vat-rate))
(amount-vat (plist-get fields :amount-vat))
(amount-ttc (plist-get fields :amount-ttc))
)
(when (or (and (not amount-ttc) (not amount-ht))
(and (not amount-vat) (not vat-rate)))
(error "Not enought amount data defined for this line %S." fields))
(unless amount-ttc
(setq amount-ttc
(cond
(amount-vat (+ amount-ht amount-vat))
(vat-rate (* amount-ht (+ 1.0 vat-rate)))
;; last case should not occur.
(t (/ (* amount-vat (+ 1.0 vat-rate)) vat-rate)))))
(unless amount-ht
(setq amount-ht
(cond
(amount-vat (- amount-ttc amount-vat))
(vat-rate (/ amount-ttc (+ 1.0 vat-rate)))
;; last case should not occur.
(t (/ amount-vat vat-rate)))))
(unless amount-vat
(setq amount-vat (- amount-ttc amount-ht)))
(unless vat-rate
(setq vat-rate (* (round (/ amount-vat amount-ht) 0.01) 0.01)))
(check-vat amount-ttc amount-ht amount-vat vat-rate)
(call-next-method self (list :object-id object-id
:description description
:devise devise
:amount-ht amount-ht
:vat-rate vat-rate
:amount-vat amount-vat
:amount-ttc amount-ttc ))
));;shared-initialize
;;; (defmethod compute-amount-ttc ((self InvoiceLine))
;;; "DOES: compute the amount-ttc."
;;; (if (vat-rate self)
;;; (setf (slot-value self 'amount-vat) (euro-round
;;; (* (amount-ht self)
;;; (vat-rate self)) (devise self))))
;;; (setf (slot-value self 'amount-ttc) (euro-round
;;; (+ (amount-ht self)
;;; (amount-vat self)) (devise self)))
;;; );;compute-amount-ttc
;;; (defmethod vat-rate ((self InvoiceLine))
;;; "
;;; RETURN: A computed VAT rate.
;;; "
;;; (let ((result
;;; (/ (round (abs (/ (amount-vat self) (amount-ht self))) 0.01) 100.0)
;;; )) (message "InvoiceLine vat-rate = %f" result) result)
;;; );;vat-rate
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Invoice
(defmethod compute-totals ((self Invoice))
"DOES: Compute the totals."
(let ((th 0.00) (tv 0.00) (tt 0.00))
(mapcar (lambda (line)
(setq th (+ th (amount-ht line))
tv (+ tv (amount-vat line))
tt (+ tt (amount-ttc line)))
)
(slot-value self 'lines))
(setf (slot-value self 'total-ht) th)
(setf (slot-value self 'total-vat) tv)
(setf (slot-value self 'total-ttc) tt))
);;compute-totals
(defmethod vat-rate ((self Invoice))
"RETURN: A computed VAT rate."
(let ((result
(/ (round (abs (/ (total-vat self) (total-ht self))) 0.01) 100.0)
)) (message "vat-rate=%f" result) result)
);;vat-rate
(put 'add-line 'lisp-indent-function 2)
(defmethod add-line ((self Invoice) (line InvoiceLine))
"DOES: Add the line."
;; (compute-amount-ttc line)
(setf (slot-value self 'lines) (append (slot-value self 'lines) (list line)))
(compute-totals self)
);;add-line
(defmethod is-refund ((self Invoice))
"RETURN: Whether this invoice is a refund invoice."
(< (total-ttc self) 0.00))
(defun clean-title-for-file-name (title-string)
"
RETURN: A string containing the first word of title-string as plain ASCII.
DOES: Remove accents from the returned word.
"
(string-remove-accents
(downcase
(substring title-string 0
(string-match "[-_ \n\t\v\f]" title-string 0))))
);;clean-title-for-file-name
(defvar invoice-strings nil "Localization data for this module.")
(mapcar
(lambda (slt)
(deftranslation invoice-strings (nth 0 slt) (nth 1 slt) (nth 2 slt)))
'(
("Phone:" :en :idem)
("Phone:" :fr "Téléphone :")
("Phone:" :es "Teléfono :")
("Fax:" :en :idem)
("Fax:" :fr "Télécopie :")
("Fax:" :es "Telécopia :")
("Email:" :en :idem)
("Email:" :fr "Email :")
("Email:" :es "Email :")
("VAT Immatriculation:" :en :idem)
("VAT Immatriculation:" :fr "TVA Intracommunautaire :")
("VAT Immatriculation:" :es "Imatriculación IVA :")
("INVOICE" :en :idem)
("INVOICE" :fr "FACTURE")
("INVOICE" :es "FACTURA")
("Date:" :en :idem)
("Date:" :fr "Date :")
("Date:" :es "Fecha :")
("Invoice no.:" :en :idem)
("Invoice no.:" :fr "Facture nº :")
("Invoice no.:" :es "Nº de factura :")
("Billing address:" :en :idem)
("Billing address:" :fr "Adresse de facturation :")
("Billing address:" :es "Dirección de factura :")
("Description" :en :idem)
("Description" :fr "Description")
("Description" :es "Descripción")
("Price" :en :idem)
("Price" :fr "Prix")
("Price" :es "Precio")
("Total" :en :idem)
("Total" :fr "Total HT")
("Total" :es "Base imponible")
("VAT %5.1f %%" :en :idem)
("VAT %5.1f %%" :fr "TVA %5.1f %%")
("VAT %5.1f %%" :es "IVA %5.1f %%")
("IRPF %5.1f %%" :en "")
("IRPF %5.1f %%" :fr "")
("IRPF %5.1f %%" :es :idem)
("Total VAT Incl." :en :idem)
("Total VAT Incl." :fr "Total TTC")
("Total VAT Incl." :es "Total factura")
("PAYMENT-METHOD" :en
"Method of Payment: Bank Transfer
Please make your payment using the details below,
before %s.")
("PAYMENT-METHOD" :fr
"Mode de règlement : À régler par virement bancaire au compte suivant,
avant le %s.")
("PAYMENT-METHOD" :es
"Forma de pago : Transferencia bancaria a la cuenta siguiente,
antes del %s.")
("Payment Bank" :en :idem)
("Payment Bank" :fr "Banque destinataire")
("Payment Bank" :es "Banco")
("Branch Name" :en :idem)
("Branch Name" :fr "Agence")
("Branch Name" :es "Oficina")
("Account Number (IBAN)" :en :idem)
("Account Number (IBAN)" :fr "Numéro de compte (IBAN)")
("Account Number (IBAN)" :es "Número de cuenta (IBAN)")
("Beneficiary" :en :idem)
("Beneficiary" :fr "Bénéficiaire")
("Beneficiary" :es "Beneficiario")
("SWIFT Code" :en :idem)
("SWIFT Code" :fr "Code SWIFT")
("SWIFT Code" :es "Código SWIFT")
))
(defmacro longest-localized-length (table language fields)
`(loop for fname in ,fields
maximize (length (localize ,table ,language fname)) into increment
finally return increment
))
(defun generate-person-address (title left-margin person &rest cl-keys)
"
DOES: insert into the current buffer at the current point the address
and phone, fax and email of the given person,
prefixed by the title and with a left-margin of `left-margin'
characters. If the length of the title is greater than the
`left-margin' then the length of the title is used instead.
cl-keys: may contain the key: :language. The default language is French (:fr),
:en and :es are also available for English and Spanish.
"
(cl-parsing-keywords ((:language :fr)) nil
(unless title (setq title ""))
(when (< left-margin (length title))
(setq left-margin (length title)))
;; title / name
(printf "%s%s\n" (string-pad title left-margin) (name person))
;; address
(mapcar (lambda (line) (printf "%s%s\n"
(string-pad "" left-margin)
(chop-spaces line)))
(split-string (address person) "[\f\n\r\v]+"))
;; other fields
(let* ((fields '("Phone:" "Fax:" "Email:" "VAT Immatriculation:"))
(slots '(phone fax email fiscal-id))
(increment
(loop for fname in fields
for slot in slots
when (slot-value person slot)
maximize (length (localize invoice-strings
cl-language fname)) into increment
finally return increment
))
)
(loop for fname in fields
for slot in slots
when (slot-value person slot)
do (printf "%s%s %s\n"
(string-pad "" left-margin)
(string-pad (localize invoice-strings cl-language fname)
increment)
(slot-value person slot)))
);;let*
));;generate-person-address
;;; (generate-person-address
;;; "" 1
;;; (get-person-with-fiscal-id invoice-set "ESX3156225G")
;;; :language :en)
(defun show-tva (montant-ht &rest options)
"Affiche le montant HT donné, la TVA, le montant TTC, et éventuellement
le montant dans une autre devise optionnelle.
En option une valeur numérique représente un taux de TVA.
Une devise peut être spécifiée (sinon la facture est en EURO).
Une ou deux langues peuvent aussi être indiquées (:es, :fr, :en).
Une option :irpf ou :no-irpf peut être indiquée pour forcer la déduction IRPF,
sinon elle est appliquée par défaut uniquement dans le cas où le taux de TVA
est 16% et la langue est 'es (sans langue secondaire) et la devise :EUR.
(show-tva 750.00 0.16 EUR :en :es)
donne :
---------------------------------------------------- -----------------
(Base imponible ) Total : 750.00 EUR
(IVA 16.0 % ) VAT 16.0 % : + 120.00 EUR
(Total factura ) Total VAT Incl. : = 870.00 EUR
---------------------------------------------------- -----------------
"
(let* (
(line-form " %52s %17s\n")
(desc-line (make-string 52 ?-))
(pric-line (make-string 17 ?-))
(base-lab-gau "")
(tvat-lab-gau "")
(irpf-lab-gau "")
(tota-lab-gau "")
(base-lab-dro)
(tvat-lab-dro)
(irpf-lab-dro)
(tota-lab-dro)
(taux-tva nil)
(taux-tva-present nil)
(devise nil)
;; empeze la actividad el 2000/07 entonces desde el 2003/07 es -18%.
(taux-irpf (if (date-after (calendar-current-date) '(6 30 2003))
-0.18 -0.09))
(show-irpf nil)
(force-show-irpf nil)
(montant-tva)
(montant-irpf)
(montant-ttc)
(lang-pri nil)
(lang-sec nil)
(devises (mapcar 'car euro-parities))
(ltrings)
)
(while options
(let ((op (car options)))
(setq options (cdr options))
(cond
((eq op :irpf)
(setq show-irpf t force-show-irpf t))
((eq op :no-irpf)
(setq show-irpf nil force-show-irpf t))
((member op '(:fr :en :es :de))
(if lang-pri
(setq lang-sec op)
(setq lang-pri op)))
((member op devises)
(if devise (error "C:est fini ces bétises avec deux devises !"))
(setq devise op))
((numberp op)
(if taux-tva (error "Un taux-tva a déjà été spécifié (%f)." taux-tva))
(setq taux-tva op))
(t (error (concat "Option inconnue : %S "
"(ni une langue connue, ni une devise connue).")
op))
);;cond
));;while options
(if taux-tva
(setq taux-tva-present t)
(setq taux-tva 0.00
taux-tva-present nil))
(if (null devise)
(setq devise EUR))
(if (equal lang-pri lang-sec)
(setq lang-sec nil))
(unless lang-pri
(setq lang-pri :es))
(if (and (null lang-sec)
(not taux-tva-present)
(eq lang-pri :es))
(setq taux-tva 0.16))
(unless force-show-irpf
(setq show-irpf (and (eq devise EUR)
(eq lang-pri :es)
(null lang-sec)
(= taux-tva 0.16)))
)
(setq montant-tva (* montant-ht taux-tva))
(setq montant-irpf (if show-irpf (* montant-ht taux-irpf) 0.00))
(setq montant-ttc (+ montant-ht montant-tva montant-irpf))
(setq base-lab-dro
(format "%-16s :"
(localize invoice-strings lang-pri "Total")))
(setq tvat-lab-dro
(format "%-16s :"
(format (localize invoice-strings lang-pri "VAT %5.1f %%")
(* 100.0 taux-tva))))
(setq irpf-lab-dro
(format "%-16s :"
(format (localize invoice-strings lang-pri "IRPF %5.1f %%")
(* 100.0 taux-irpf))))
(setq tota-lab-dro
(format "%-16s :"
(localize invoice-strings lang-pri "Total VAT Incl.")))
(when lang-sec
(setq base-lab-gau
(format "(%-16s) "
(localize invoice-strings lang-sec "Total")))
(setq tvat-lab-gau
(format "(%-16s) "
(format (localize invoice-strings lang-sec "VAT %5.1f %%")
(* 100.0 taux-tva))))
(setq irpf-lab-gau
(format "(%-16s) "
(format (localize invoice-strings lang-sec "IRPF %5.1f %%")
(* 100.0 taux-irpf))))
(setq tota-lab-gau
(format "(%-16s) "
(localize invoice-strings lang-sec "Total VAT Incl.")))
);;when lang-sec
(insert "\n")
(insert (format line-form desc-line pric-line))
(insert (format line-form
(concat base-lab-gau base-lab-dro)
(format " %12.2f %-3s" montant-ht devise )))
(insert (format line-form
(concat tvat-lab-gau tvat-lab-dro)
(format "+%12.2f %-3s" montant-tva devise)))
(when show-irpf
(insert (format line-form
(concat irpf-lab-gau irpf-lab-dro)
(format "-%12.2f %-3s" (- 0.00 montant-irpf) devise)))
);;when show-irpf
(insert (format line-form
(concat tota-lab-gau tota-lab-dro)
(format "=%12.2f %-3s" montant-ttc devise)))
(insert (format line-form desc-line pric-line))
));;show-tva
(defun date-to-universal-time (date-string)
"
RETURN: a number of seconds since 1900-01-01 00:00:00 GMT.
"
(let ((ymd (split-string date-string "-")))
(encode-universal-time
0 0 0
(string-to-number (nth 2 ymd))
(string-to-number (nth 1 ymd))
(string-to-number (nth 0 ymd))
0))
);;date-to-universal-time
(defun universal-time-to-date (utime)
"
RETURN: the given universal time formated in the ISO8601 YYYY-MM-DD format.
"
(let ((smhdmydlz (multiple-value-list (decode-universal-time utime 0))))
(format "%04d-%02d-%02d"
(nth 5 smhdmydlz) (nth 4 smhdmydlz) (nth 3 smhdmydlz)))
);;universal-time-to-date
(defconstant DAY (* 24 3600) "Number of seconds in a day.")
(defun date-format (utime &rest cl-keys)
(let* ((smhdmydlz (multiple-value-list (decode-universal-time utime 0)))
(day (nth 3 smhdmydlz))
(month (nth 4 smhdmydlz))
(year (nth 5 smhdmydlz))
)
(cl-parsing-keywords ((:language :en)) nil
(cond
((eq cl-language :fr)
(format "%d%s %s %d"
day
(if (= 1 day) "er" "")
(aref ["Janvier" "Février" "Mars" "Avril"
"Mai" "Juin" "Juillet" "Août"
"Septembre" "Octobre" "Novembre" "Décembre"]
(1- month))
year))
((eq cl-language :es)
(format "%d de %s de %d"
day
(aref ["Enero" "Febrero" "Marzo" "Abril"
"Mayo" "Junio" "Julio" "Augosto"
"Septiembre" "Octobre" "Noviembre" "Diciembre"]
(1- month))
year))
(t
(format "%s %d%s, %d"
(aref ["January" "February" "March" "April"
"May" "June" "July" "August"
"September" "October" "November" "December"]
(1- month))
day
(cond ((= 1 (% day 10)) "st")
((= 2 (% day 10)) "nd")
((= 3 (% day 10)) "rd")
(t "th"))
year))
)))
);;date-format
(defun align-following-lines (text left-margin)
(let ((first t)
(margin (make-string left-margin 32)))
(unsplit-string
(mapcar (lambda (line)
(if first
(progn
(setq first nil)
(chop-spaces line))
(concat margin (chop-spaces line))))
(split-string text "[\f\n\r\v]"))
"\n"))
);;align-following-lines
(defmethod generate-invoice ((self Invoice) &rest cl-keys)
"
DOES: Generate this invoice into a file in directory `invoice-directory'.
"
(let* ((payer (get-person-with-fiscal-id invoice-set
(payer-fiscal-id self)))
(issuer (get-person-with-fiscal-id invoice-set
(issuer-fiscal-id self)))
(file-path (format "%s/%s-%s-%s.txt"
invoice-directory
(string-replace (invoice-number self) "/" "" t t)
(clean-title-for-file-name (object-id payer))
(clean-title-for-file-name (title self))))
)
(cl-parsing-keywords (:language) nil
(unless cl-language
(setq cl-language (language payer)))
(unless cl-language
(setq cl-language :es))
(save-excursion
(find-file file-path)
(erase-buffer)
(generate-person-address "" 1 issuer :language cl-language)
(printf " \n")
(let* ((title (localize invoice-strings cl-language "INVOICE"))
(width (+ 8 (length title)))
(title-b (concat "|" (string-pad title width :center) "|"))
(line-b (concat "+" (make-string width ?-) "+")))
(printf " %s\n" (string-pad line-b 72 :center))
(printf " %s\n" (string-pad title-b 72 :center))
(printf " %s\n" (string-pad line-b 72 :center))
)
(printf " \n")
(let ( (increment (longest-localized-length
invoice-strings cl-language
'("Date:" "Invoice no.:" "Billing address:"))) )
(printf " %s %s\n"
(string-pad (localize invoice-strings cl-language
"Date:") increment)