pjb-invoices.el.restore 80.7 KB
Newer Older
Pascal Bourguignon's avatar
Pascal Bourguignon committed
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
;;******************************************************************************
;;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
467
;;;                                        (+ (amount-ht self)
Pascal Bourguignon's avatar
Pascal Bourguignon committed
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 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922
;;;                                           (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)
                (date-format (date-to-universal-time (date self))
                             :language cl-language))
        (printf " \n")

        (printf " %s %s\n"
                (string-pad (localize invoice-strings cl-language
                                      "Invoice no.:") increment)
                (invoice-number self))
        (printf " \n")

        (generate-person-address
         (concat " " (localize invoice-strings cl-language "Billing address:"))
         (+ 2 increment) payer :language cl-language)
        (printf " \n")
        );;let*


      (let (
            (line-form " %-52s   %-17s\n")
            (desc-line (make-string 52 ?-))
            (pric-line (make-string 17 ?-))
            )
        (printf line-form desc-line pric-line)
        (printf line-form
                (localize invoice-strings cl-language "Description")
                (localize invoice-strings cl-language "Price"))
        (printf line-form desc-line pric-line)
        (loop for invo-line in (lines self)
              do
              (let ((last-length 0))
                (mapcar (lambda (desc-line)
                          (let ((item (chop-spaces desc-line)))
                            (printf "\n %s" item)
                            (setq last-length (length item))))
                        (split-string (description invo-line) "[\f\n\r\v]"))
                (if (<= last-length 55)
                    (printf "%s %12.2f %-3s"
                            (make-string (- 55 last-length) 32)
                            (amount-ht invo-line)
                            (devise invo-line))
                  (printf "\n %52s   %12.2f %-3s"
                          ""
                          (amount-ht invo-line)
                          (devise invo-line))))
              );;loop
        (printf " \n")
        );;let

      (show-tva (total-ht self) (devise self) cl-language :es)
      (printf " \n")
      (let ((bankref (bank-reference issuer)))
        (when bankref
          (mapcar (lambda (line) (printf " %s\n" (chop-spaces line)))
                  (split-string
                   (format
                    (localize invoice-strings cl-language "PAYMENT-METHOD")
                    (date-format (+ (* 30 DAY)
                                    (date-to-universal-time (date self)))
                                 :language cl-language))
                   "[\f\n\r\v]"))
          (printf " \n")
          (let* ((fields '("Payment Bank" "" "Branch Name"
                           "Account Number (IBAN)" "Beneficiary"
                           "SWIFT Code"))
                 (slots  '(bank-name bank-address branch-name
                                     account-number beneficiary-name swift-code))
                 (increment (longest-localized-length
                             invoice-strings cl-language fields))
                 )


            (loop for fname in fields
                  for slot  in slots
                  when (slot-value bankref slot)
                  do (printf
                      " %s%s : %s\n"
                      (string-pad "" 8)
                      (string-pad (localize invoice-strings cl-language fname)
                                  increment)
                      (align-following-lines (slot-value bankref slot)
                                             (+ increment 10))))

            )));;let bankref
      (printf " \n")
      (printf " \n")
      (save-buffer 0)
      (kill-buffer (current-buffer)))
    ));;let*
  );;generate-invoice



;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; InvoiceSet


(defun invoices-load ()
  "DOES:   Load the invoice set from `invoice-set-file-path' file."
  (load invoice-set-file-path)
  );;invoices-load


(defun invoices-save (invoice-set)
  "DOES:   Save the invoice set into the `invoice-set-file-path' file."
  (find-file invoice-set-file-path)
  (erase-buffer)
  (insert (format  "(setq invoice-set '%S)\n" invoice-set))
  (save-buffer nil)
  (kill-buffer (current-buffer))
  );;invoices-save




(defmethod get-person-with-fiscal-id ((self InvoiceSet) fiscal-id )
  (object-assoc fiscal-id 'fiscal-id (persons self))
  );;get-person-with-fiscal-id



(put 'add-person 'lisp-indent-function 2)
(defmethod add-person ((self InvoiceSet) (person FiscalPerson)
&optional fisc)
  (let ((old (get-person-with-fiscal-id self (fiscal-id person))))
  (if old
      (list-replace-member-in-place (persons self) old person)
    (push person (slot-value self 'persons)))
  );;let
  (when (and fisc  (not (member (fiscal-id person) (fisc-fiscal-ids self))))
  (push (fiscal-id person) (slot-value self 'fisc-fiscal-ids)))
  );;add-person


(defmethod get-invoice-with-issuer-and-number
  ((self InvoiceSet) issuer-fiscal-id number)
  (let ((i (invoices self))
        result)
  (while i
    (if (and (equal issuer-fiscal-id (issuer-fiscal-id (car i)))
             (equal number           (invoice-number   (car i))))
        (setq result (car i)
              i nil)
      (setq i (cdr i))))
  result)
  );;get-invoice-with-issuer-and-number


(put 'add-invoice 'lisp-indent-function 2)
(defmethod add-invoice ((self InvoiceSet) (invoice Invoice))
  (let ((old (get-invoice-with-issuer-and-number
              self
              (issuer-fiscal-id invoice)
              (invoice-number   invoice))))
  (if old
      (list-replace-member-in-place (slot-value self 'invoices) old invoice)
    (push invoice (slot-value self 'invoices)))
  );;let
  );;add-invoice



;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Movement

(defclass Movement (PjbObject)
  ((date
    :initform nil
    :initarg  :date
    :accessor date
    :type     (or null string)
    :documentation
    "'YYYY-MM-DD' Date of the movement.")
  (amount-ttc
   :initform 0.00
   :initarg  :amount-ttc
   :accessor amount-ttc
   :type     number
   :documentation
   "(number) The amount paid (including taxes).")
  (amount-vat
   :initform 0.00
   :initarg  :amount-vat
   :accessor amount-vat
   :type     number
   :documentation
   "(number) The VAT of the movement.")
  (description
   :initform ""
   :initarg  :description
   :accessor description
   :type     string
   :documentation
   "(string) A description of the movement.")
  (kind
   :initform nil
   :initarg  :kind
   :accessor kind
   :type     (or null symbol)
   :documentation
   "(symbol) A kind of movement, for tax reporting purpose.
PRESTACION-NACIONAL, PRESTACION-INTRACOMUNITARIA, IMPUESTO, INVERSION,
GASTO-CORRIENTE, ADQUISICION-INTRACOMUNITARIA")
  (invoice-fiscal-id
   :initform nil
   :initarg  :invoice-fiscal-id
   :accessor invoice-fiscal-id
   :type     (or null string)
   :documentation
   "The fiscal id of the common issuer of the following invoices
related to this movement.")
  (invoice-numbers
   :initform nil
   :initarg  :invoice-numbers
   :accessor invoice-numbers
   :type     list
   :documentation
   "(list of string) The list of invoice numbers related to this entry.
Note that one journal entry may relate to several invoices (grouped payment)
and one invoice may relate to several movements (part payments, or
corrections.")
  )
  (:documentation
  "An entry in the journal.
A movement with a positive amount is a credit,
while a movement with a negative amount is a debit."))


(defmethod shared-initialize ((self Movement) fields)
  "
DOES:    Checks that the values for the fields are within limits.
"
  (let ((date                (plist-get fields :date))
        (amount-ttc          (plist-get fields :amount-ttc))
        (amount-ht           (plist-get fields :amount-ht))
        (amount-vat          (plist-get fields :amount-vat))
        (description         (plist-get fields :description))
        (kind                (plist-get fields :kind))
        (invoice-fiscal-id   (plist-get fields :invoice-fiscal-id))
        (invoice-numbers     (plist-get fields :invoice-numbers))
        )
  (when amount-ttc
    (if (< 15000.00 (abs amount-ttc))
        (error "amount-ttc too big for movement %S." fields)))

  (when amount-ht
    (if (< 15000.00 (abs amount-ht))
        (error "amount-ht too big for movement %S." fields)))

  (when invoice-fiscal-id
    (unless (get-person-with-fiscal-id invoice-set invoice-fiscal-id)
      (error "Unknown person (fiscal-id=%S) for movement %S."
             invoice-fiscal-id fields)))
  );;let
  (call-next-method)
  );;shared-initialize



;; kinds of Movement:
(defconst PRESTACION-NACIONAL
  'PRESTACION-NACIONAL          "Kind of entry of journal.")
(defconst PRESTACION-INTRACOMUNITARIA
  'PRESTACION-INTRACOMUNITARIA  "Kind of entry of journal.")
(defconst IMPUESTO
  'IMPUESTO                     "Kind of entry of journal.")
(defconst INVERSION
  'INVERSION                    "Kind of entry of journal.")
(defconst GASTO-CORRIENTE
  'GASTO-CORRIENTE              "Kind of entry of journal.")
(defconst ADQUISICION-INTRACOMUNITARIA
  'ADQUISICION-INTRACOMUNITARIA "Kind of entry of journal.")

(defmethod amount-ht ((self Movement))
  (euro-round (- (amount-ttc self) (amount-vat self)) :EUR))



(defun make-journal-entry-from-invoice (invoice)
  "RETURN: A new instance of Movement filled with data from invoice."
  (let (kind amount-sign fiscal-id)
  (cond
   ((member (issuer-fiscal-id invoice)
            (fisc-fiscal-ids invoice-set))
    (setq kind 'IMPUESTO
          amount-sign -1.0
          fiscal-id (issuer-fiscal-id invoice)))
   ((equal (issuer-fiscal-id invoice)
           (fiscal-id invoice-set))
    (setq kind (if (= 0.00 (total-vat invoice))
                   'PRESTACION-INTRACOMUNITARIA
                 'PRESTACION-NACIONAL)
          amount-sign 1.0
          fiscal-id (payer-fiscal-id invoice)))
   (t
    (setq kind 'GASTO-CORRIENTE
          amount-sign -1.0
          fiscal-id (issuer-fiscal-id invoice))))
  (message "amount-sign=%S" amount-sign)
  (Movement
   ""
   :date                (date                     invoice)
   :amount-ttc          (* amount-sign (total-ttc invoice))
   :amount-vat          (* amount-sign (total-vat invoice))
   :description         (title                    invoice)
   :kind                kind
   :invoice-fiscal-id   fiscal-id
   :invoice-numbers     (list (invoice-number     invoice))))
  );;make-journal-entry-from-invoice




(defun make-journal-entry (date amount-ttc vat-rate nif fac-l description kind)
  "RETURN: A new instance of Movement filled with the given data."
  (if (not (member kind
                   '(PRESTACION-NACIONAL
                     PRESTACION-INTRACOMUNITARIA
                     IMPUESTO
                     INVERSION GASTO-CORRIENTE ADQUISICION-INTRACOMUNITARIA)))
  (error "Invalid kind %s." kind))

  (if (< vat-rate 0.00)
  (error "VAT-RATE must always be >= 0.00."))

  (if (eq kind PRESTACION-INTRACOMUNITARIA)
  t
  (if (member kind '(PRESTACION-NACIONAL));;
      (if (>= 0.00 amount-ttc)
          (error "AMOUNT-TTC must be > 0.00 for an entry of kind %s." kind))
    ;;(if (<= 0.00 amount-ttc)
    ;;    (error "AMOUNT-TTC must be < 0.00 for an entry of kind %s." kind))
    )
  )

  (cond
  ((eq kind PRESTACION-NACIONAL)
   (if (>= 0.00 vat-rate)
       (error "VAT-RATE must be > 0.00 for an entry of kind %s." kind)))
  ((member kind '(PRESTACION-INTRACOMUNITARIA IMPUESTO))
   (if (< 0.00 vat-rate)
       (error "VAT-RATE must be = 0.00 for an entry of kind %s." kind)))
  (t
   ;;(if (= 0.00 vat-rate)
   ;; (error "VAT-RATE must be > 0.00 for an entry of kind %s." kind))
   t
   ))

  (Movement
  ""
  :date              date
  :amount-ttc        amount-ttc
  :amount-vat        (euro-round
                      (/ (* amount-ttc vat-rate) (+ 1.0 vat-rate)) :EUR)
  :description       description
  :kind              kind
  :invoice-fiscal-id nif
  :invoice-numbers   fac-l)
  );;make-journal-entry



(defmethod is-credit   ((self Movement))
  "RETURN: Whether the SELF is a credit movement."
  (< 0.00 (amount-ht self)))

(defmethod vat-rate    ((self Movement))
  "
RETURN: A computed VAT rate for this entry.
"
  (* (round (/ (amount-vat self) (amount-ht self)) 0.01) 0.01)
  );;vat-rate


(defmethod credit-ht   ((self Movement))
  (if (is-credit self)
  (amount-ht self)
  0.00))


(defmethod credit-iva  ((self Movement))
  (if (is-credit self)
  (amount-vat self)
  0.00))

(defmethod debit-ht    ((self Movement))
  (if (is-credit self)
  0.00
  (- 0.00 (amount-ht self))))


(defmethod debit-iva   ((self Movement))
  (if (is-credit self)
  0.00
  (- 0.00 (amount-vat self))))


(defmethod debit-iva-inversion  ((self Movement))
  (if (eq INVERSION (kind self))
  (debit-iva self)
  0.00))


(defmethod debit-iva-corriente  ((self Movement))
  (if (eq GASTO-CORRIENTE (kind self))
  (debit-iva self)
  0.00))





(defmethod invoices ((self Movement))
  "RETURN: A list of Invoice instances related to this entry."
  (let ((fiscal-id  (if (and (is-credit self)
                             (not (equal (kind self) 'GASTO-CORRIENTE)))
                        (fiscal-id invoice-set)
                      (invoice-fiscal-id self))))
  (remove nil
          (mapcar
           (lambda (number)
             (get-invoice-with-issuer-and-number invoice-set fiscal-id number))
           (invoice-numbers self))))
  );;invoices



(defun chop-justify-and-split-text (text width)
  "DOES:    chop spaces on each line. justify each paragraph."
  (let (
        ;; chop the lines.
        (lines (mapcar (lambda (line) (chop-spaces line))
                       (split-string text "[\n\r\v]")))
        paragraphs current-paragraph
        jlines
        )

    ;; group the paragraphs.
  (while lines
    (let ((line (car lines)))
      (if (string-equal line "")
          (progn
            (if current-paragraph
                (setq paragraphs (cons current-paragraph paragraphs)))
            (setq current-paragraph nil))
        (setq current-paragraph (concat current-paragraph " " line)))
      (setq lines (cdr lines))))
  (if current-paragraph
      (setq paragraphs (cons current-paragraph paragraphs)))
  (setq paragraphs (nreverse paragraphs))

  ;; justify the paragraphs.
  (while paragraphs
    (setq jlines (cons (string-justify-left (car paragraphs) width 0) jlines))
    (setq paragraphs (cdr paragraphs)))

  ;; don't return nil.
  (if (null jlines)
      (setq jlines (list " ")))

  ;; split the justified paragraphs.
  (apply 'append
         (mapcar (lambda (lines) (split-string lines "[\n\r\v]"))
                 (nreverse jlines)))
  ));;chop-justify-and-split-text




(defmethod insert-formated ((self Movement))
  "DOES:    format and insert this entry."

  (let ((id  (invoice-fiscal-id self))
        person
        (name "")  name-l
        invoices
        (movement-sign (if (is-credit self) 1.0 -1.0))
        (invoices-sign 1))

    ;; first line:
  (if id
      (progn
        (setq person (get-person-with-fiscal-id invoice-set id))
        (if person (setq name (name person))))
    (setq id ""))
  (setq name-l (chop-justify-and-split-text name 38))
  ;; SEN FECHA IDENTIFICATION NOMBRE
  (insert (format "%3s %10s %-23s %s\n"
                  (cond
                   ((is-credit self) "ING")
                   ((eq 'IMPUESTO (kind self)) "IMP")
                   (t "GAS"))
                  (date self) id
                  (car name-l)))

  ;; optional next lines:
  ;;                          NOMBRE (continuación)
  (setq name-l (cdr name-l))
  (while name-l
    (insert (format "%38s %s\n" "" (car name-l)))
    (setq name-l (cdr name-l)))

  (let ((t-ht  0.00)
        (t-vat 0.00)
        (t-ttc 0.00))
    ;; invoice lines:
    ;;     IMPORTE  IVA%   +IVA   TOTAL  NUMERO FACTURA o DESCRIPCION

    ;; Let's find the invoices-sign
    (setq invoices (invoices self))
    (let ((t-ttc 0.00))
      (while invoices
        (setq t-ttc (+ t-ttc (total-ttc (car invoices)))
              invoices (cdr invoices)))
      (setq invoices-sign (* movement-sign (if (< t-ttc 0)  -1 1))))

    ;; Let's print the invoices
    (setq invoices (invoices self))
    (while invoices
      (let* ((invoice (car invoices))
             (title-l (chop-justify-and-split-text
                       (title invoice) 38))
             (title (car title-l))
             (i-ht  (total-ht invoice))
             (i-vat (total-vat invoice))
             (i-ttc (total-ttc invoice)))
        (setq title-l (cdr title-l))
        (setq t-ht  (+ t-ht   i-ht)
              t-vat (+ t-vat  i-vat)
              t-ttc (+ t-ttc  i-ttc))
        (insert (format "    %9.2f %4.1f%% %8.2f %9.2f %s\n"
                        (* invoices-sign i-ht)
                        (* 100.0 (vat-rate invoice))
                        (* invoices-sign i-vat)
                        (* invoices-sign  i-ttc)
                        (invoice-number invoice)))
        (insert (format "%38s %s\n" "" title))
        (while title-l
          (insert (format "%38s %s\n" "" (car title-l)))
          (setq title-l (cdr title-l))));;let*
      (setq invoices (cdr invoices)))

    ;;(if (not (= (+ t-ht t-vat)  t-ttc))
    ;; (insert (format "**** %f + %f != %f ****\n" t-ht t-vat t-ttc)))

    (if (and (= 0.0 t-ht) (= 0.0 t-vat) (= 0.0 t-ttc))
        t;; No invoices, no differences
      ;; Invoices, let's see if there's a difference.
      (if (not (= (amount-ttc self) (* invoices-sign t-ttc)))
          (let* ((diff-ht  (-  (amount-ht  self) (* invoices-sign t-ht ) ))
                 (diff-vat (-  (amount-vat self) (* invoices-sign t-vat) ))
                 (diff-ttc (-  (amount-ttc self) (* invoices-sign t-ttc) ))
                 )
            (insert (format "    %9.2f %5s %8.2f %9.2f %s\n"
                            diff-ht
                            ""
                            diff-vat
                            diff-ttc
                            "Diferencia"))))
      (insert "    --------- ----- -------- ---------\n"));;if
    );;let

  ;; total entry lines:
  (let* ((desc-l (chop-justify-and-split-text (description self) 38))
         (desc   (car desc-l)))
    (insert (format "    %9.2f %4.1f%% %8.2f %9.2f %s\n"
                    (amount-ht  self)
                    (* 100.0 (vat-rate   self))
                    (amount-vat self)
                    (amount-ttc self)
                    desc))
    (setq desc-l (cdr desc-l))
    (while desc-l
      (insert (format "%38s %s\n" "" (car desc-l)))
      (setq desc-l (cdr desc-l))))

  (insert "--- ---------- ----------------------- ----------------------------------------\n")
  ));;insert-formated



(commented-out

 (if (null description)
 (setq description ""))
 (let* ((jdesc-1r (journal-split-and-justify-description description 38))
        (jdesc-1  (car jdesc-1r))
        (jdesc-r  (cdr jdesc-1r))
        amount iva)
 (if (< amount-ht 0)
     (progn
       (setq amount  (- 0 amount-ht))
       (setq iva     (euro-round (* vat amount) :EUR))
       (insert (format "\n%10s                %7.2f %6.2f %-38s\n"
                       date amount iva  jdesc-1 )))
   (progn
     (setq amount  amount-ht)
     (setq iva     (euro-round (* vat amount) :EUR))
     (insert (format "\n%10s %7.2f %6.2f                %-38s\n"
                     date amount iva  jdesc-1 ))))
 (while jdesc-r
   (insert (format "%40s %-38s\n" "" (car jdesc-r)))
   (setq jdesc-r (cdr jdesc-r)))
 (insert (format "%40s %-38s\n" ""
                 (format "%7.2f + %6.2f = %7.2f"
                         amount iva (+ amount iva))))))



;; (show (journal-entries))




;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; journal-entry object
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;




(defun journal-entry-new (date amount-ht vat-rate nif fac-l description kind)

  (if (not (member kind
                   '(PRESTACION-NACIONAL
                     PRESTACION-INTRACOMUNITARIA
                     IMPUESTO
                     INVERSION GASTO-CORRIENTE ADQUISICION-INTRACOMUNITARIA)))
  (error "Invalid kind %s." kind))

  (if (< vat-rate 0.00)
  (error "VAT-RATE must always be >= 0.00."))

  (if (eq kind PRESTACION-INTRACOMUNITARIA)
  t
  (if (member kind '(PRESTACION-NACIONAL));;
      (if (>= 0.00 amount-ht)
          (error "AMOUNT-HT must be > 0.00 for an entry of kind %s." kind))
    ;; (if (<= 0.00 amount-ht)
    ;;   (error "AMOUNT-HT must be < 0.00 for an entry of kind %s." kind))
    )
  )

  (cond
  ((eq kind PRESTACION-NACIONAL)
   (if (>= 0.00 vat-rate)
       (error "VAT-RATE must be > 0.00 for an entry of kind %s." kind)))
  ((member kind '(PRESTACION-INTRACOMUNITARIA IMPUESTO))
   (if (< 0.00 vat-rate)
       (error "VAT-RATE must be = 0.00 for an entry of kind %s." kind)))
  (t
   (if (= 0.00 vat-rate)
       (error "VAT-RATE must be > 0.00 for an entry of kind %s." kind))))

  (list date amount-ht vat-rate description kind nif fac-l))



(defun journal-entry-date        (entry)  (nth 0 entry))
(defun journal-entry-amount-ht   (entry)  (nth 1 entry))
(defun journal-entry-vat-rate    (entry)  (nth 2 entry))
(defun journal-entry-description (entry)  (nth 3 entry))
(defun journal-entry-kind        (entry)  (nth 4 entry))
(defun journal-entry-nif         (entry)  (nth 5 entry))
(defun journal-entry-facturas    (entry)  (nth 6 entry))


(defun journal-entry-is-credit   (entry)
  "RETURN: Whether the ENTRY is a credit."
  (< 0.00 (journal-entry-amount-ht entry)))


(defun journal-entry-credit-ht   (entry)
  (if (journal-entry-is-credit entry)
  (journal-entry-amount-ht entry)
  0.00))


(defun journal-entry-credit-iva  (entry)
  (euro-round (* (journal-entry-vat-rate entry)
                 (journal-entry-credit-ht entry)) :EUR))


(defun journal-entry-debit-ht    (entry)
  (if (journal-entry-is-credit entry)
  0.00
  (- 0 (journal-entry-amount-ht entry))))


(defun journal-entry-debit-iva   (entry)
  (euro-round (* (journal-entry-vat-rate entry)
                 (journal-entry-debit-ht entry)) :EUR))


(defun journal-entry-debit-iva-inversion  (entry)
  (if (eq INVERSION (journal-entry-kind entry))
  (journal-entry-debit-iva entry)
  0.00))


(defun journal-entry-debit-iva-corriente  (entry)
  (if (eq GASTO-CORRIENTE (journal-entry-kind entry))
  (journal-entry-debit-iva entry)
  0.00))







;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; journal object
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;


(defvar journal-data nil "Private journal data.")


(defun journal-is-sorted ()
  "PRIVATE"
  (car journal-data))


(defun journal-entries ()
  "PRIVATE"
  (cdr journal-data))


(defun journal-reset ()
  "POST: (null (journal-entries))"
  (setq journal-data '(t)))


(defun journal-add-entry (entry)
  "DOES:   Add the ENTRY into the journal.
        ENTRY is a list containing the journal entry data.
POST:   (and (member entry (journal-entries)) (not (journal-is-sorted)))"
  (setq journal-data (cons nil (cons entry (journal-entries)))))

(defun journal-entry-ht (date amount-ht vat nif fac description kind)
  "OBSOLETE
DOES:   Add a new journal entry.
        AMOUNT-HT is the total excluding the VAT expressed in Euros.
        VAT is the V.A.T percentage: 0.00 or 0.16."
  (journal-add-entry
  (make-journal-entry date (* (+ 1.0 vat) amount-ht) vat nif
                      (if (listp fac) fac (list fac)) description kind)))


(defun journal-entry (date amount-ttc vat-rate nif fac description kind)
  "
DOES:   Add a new journal entry.
        AMOUNT-TTC is the total paid (including VAT) expressed in Euros.
        VAT-RATE is the V.A.T percentage."
  (journal-add-entry
  (make-journal-entry date amount-ttc vat-rate nif
                      (if (listp fac) fac (list fac)) description kind)))


(defun journal-sort ()
  "POST:   (journal-is-sorted)"
  (setq journal-data
  (cons  t (sort (journal-entries)
                 (lambda (a b) (string-lessp (date a) (date  b)))))))



(defun journal-date-in-year-trimestre (date-str year trimestre)
  "RETURN: Whether the given date string (formated as \"YYYY-MM-DD\") is within
        the given YEAR and TRIMESTRE."
  (let* ((ymd (split-string date-str "-"))
         (y   (string-to-number (nth 0 ymd)))
         (m   (string-to-number (nth 1 ymd)))
         (ml  (nth (- trimestre 1) '((1 2 3) (4 5 6) (7 8 9) (10 11 12)))))
  (and (= y year) (member m ml))))



(defun journal-extract (year trimestre)
  "RETURN: The entries of the journal corresponding
        to the given YEAR and TRIMESTRE."
  (unless (journal-is-sorted) (journal-sort))
  (list-extract-predicate
  (journal-entries)
  (lambda (entry)
    (journal-date-in-year-trimestre (date entry) year trimestre))))




(defun journal-totals-of-entries (entries)
  "PRIVATE
RETURN: a list containing the totals:
        credit-ht credit-iva debit-ht debit-iva-inversion debit-iva-corriente"
  (let ((credit-ht   0.00)
        (credit-iva  0.00)
        (debit-ht    0.00)
        (debit-iva-c 0.00)
        (debit-iva-i 0.00)
        sch sci sdh sdi)
  (mapcar (lambda (entry)
            (setq credit-ht   (+ credit-ht   (credit-ht  entry))
                  credit-iva  (+ credit-iva  (credit-iva entry))
                  debit-ht    (+ debit-ht    (debit-ht   entry))
                  debit-iva-c (+ debit-iva-c
                                 (debit-iva-corriente  entry))
                  debit-iva-i (+ debit-iva-i
                                 (debit-iva-inversion  entry))))
          entries)
  (list credit-ht credit-iva debit-ht debit-iva-i debit-iva-c)))




(defun journal-split-and-justify-description (description width)
  "PRIVATE"
  (let ((lines (split-string description "[\n\r\v]+"))
        (jlines nil)
        )
  (while lines
    (setq jlines (cons (string-justify-left (car lines) width 0) jlines))
    (setq lines (cdr lines)))
  (if (null jlines)
      (setq jlines (list " ")))
  (apply 'append
         (mapcar (lambda (lines) (split-string lines "[\n\r\v]+"))
                 (nreverse jlines)))))


(defun journal-insert-header (year trimestre)
  "PRIVATE"
  (insert "-------------------------------------------------------------------------------\n")
  (insert (format "%36d - TRIMESTRE %d\n" year trimestre))
  (insert "--- ---------- ----------------------- ----------------------------------------\n")
  (insert "SEN FECHA      IDENTIFICACION          NOMBRE\n")
  (insert "TID   IMPORTE  IVA%     +IVA     TOTAL NUMERO FACTURA / DESCRIPTION\n")
  (insert "--- --------- ----- -------- --------- ---------------------------------------\n")
  )


(defun journal-insert-trailer ()
  "PRIVATE"
  (insert "      IMPORTE            IVA     TOTAL\n")
  (insert "--- --------- ----- -------- --------- ---------------------------------------\n")
  )



;; (defun journal-format-entry (date amount-ht vat description &rest rest)
;;   "PRIVATE"
;;   (if (null description)
;;       (setq description ""))
;;   (let* ((jdesc-1r (journal-split-and-justify-description description 38))
;;          (jdesc-1  (car jdesc-1r))
;;          (jdesc-r  (cdr jdesc-1r))
;;          amount iva)
;;     (if (< amount-ht 0)
;;         (progn
;;           (setq amount  (- 0 amount-ht))
;;           (setq iva     (euro-round (* vat amount) :EUR))
;;           (insert (format "\n%10s                %7.2f %6.2f %-38s\n"
;;                           date amount iva  jdesc-1 )))
;;       (progn
;;         (setq amount  amount-ht)
;;         (setq iva     (euro-round (* vat amount) :EUR))
;;         (insert (format "\n%10s %7.2f %6.2f                %-38s\n"
;;                         date amount iva  jdesc-1 ))))
;;     (while jdesc-r
;;       (insert (format "%40s %-38s\n" "" (car jdesc-r)))
;;       (setq jdesc-r (cdr jdesc-r)))
;;     (insert (format "%40s %-38s\n" ""
;;                     (format "%7.2f + %6.2f = %7.2f"
;;                             amount iva (+ amount iva))))))

(defun journal-insert-totals (totals)
  "PRIVATE"
  (let ((credit-ht  (nth 0 totals))
        (credit-vat (nth 1 totals))
        (debit-ht   (nth 2 totals))
        (debit-vat  (+ (nth 3 totals) (nth 4 totals)))
        )

  (insert (format "    %9.2f %5s %8.2f %9s %s\n"
                  credit-ht "" credit-vat "" "Credito"))

  (insert (format "    %9.2f %5s %8.2f %9s %s\n"
                  (- 0.0 debit-ht) "" (- 0.0 debit-vat) "" "Debido"))

  (insert (format "    %9.2f %5s %8.2f %9s %s\n"
                  (- credit-ht debit-ht)
                  ""
                  (- credit-vat debit-vat)
                  "" "Saldo"))

  ));;journal-insert-totals


(defun old-journal-insert-totals (totals)
  "PRIVATE"
  (let ((credit-ht  (nth 0 totals))
        (credit-iva (nth 1 totals))
        (debit-ht   (nth 2 totals))
        (debit-iva  (+ (nth 3 totals) (nth 4 totals)))
        sch sci sdh sdi)
  (insert (format "%10s%8.2f%7.2f%8.2f%7.2f\n" "Total: "
                  credit-ht credit-iva debit-ht debit-iva))

  (if (< debit-ht credit-ht)
      (setq sch (format "%8.2f" (- credit-ht debit-ht))
            sdh "")
    (setq sdh (format "%8.2f" (- debit-ht credit-ht))
          sch ""))
  (if (< debit-iva credit-iva)
      (setq sci (format "%7.2f" (- credit-iva debit-iva))
            sdi "")
    (setq sdi (format "%7.2f" (- debit-iva credit-iva))
          sci ""))
  (insert (format "%10s%8s%7s%8s%7s\n" "Solde: " sch sci sdh sdi))
  ))


(defun kind-to-order (kind)
1923
  (cond
Pascal Bourguignon's avatar
Pascal Bourguignon committed
1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935
   ((eq kind PRESTACION-NACIONAL)          1)
   ((eq kind PRESTACION-INTRACOMUNITARIA)  2)
   ((eq kind IMPUESTO)                     3)
   ((eq kind INVERSION)                    4)
   ((eq kind GASTO-CORRIENTE)              5)
   ((eq kind ADQUISICION-INTRACOMUNITARIA) 6)
   (t 7)))

(defun journal-insert (year trimestre)
  "DOES:   Insert the formated entries of the journal into the current buffer
        at the current point."
  (journal-sort)
1936
  (let* ((entries
Pascal Bourguignon's avatar
Pascal Bourguignon committed
1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048
          (sort (journal-extract year trimestre)
                (lambda (a b)
                  (cond
                   ((= (kind-to-order (kind a)) (kind-to-order (kind b)))
                    (string<=  (date a) (date b)))
                   (t (< (kind-to-order (kind a)) (kind-to-order (kind b)))))
                  )))
         (totals  (journal-totals-of-entries entries)))
    (journal-insert-header year trimestre)
    (mapcar (lambda (entry) (insert-formated entry)) entries)
    (journal-insert-trailer)
    (journal-insert-totals totals)
    (insert "\n")
    (apply 'impuestos (list year trimestre entries ))
    (insert "\n")
    (insert "\n")
    ))



;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; impuestos: IVA, IRPF.
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;


(defvar impuestos-data nil "Private impuestos data.")

(defun impuestos-reset ()
  "DOES:   Reinitialize the impuestos data and the impuestos file.
SEE-ALSO:    impuestos-file-path."
  (interactive)
  (setq impuestos-data nil)
  (impuestos-save impuestos-data))


(defun impuestos-iva-get (year trimestre)
  "RETURN: The IVA data for the given TRIMESTRE."
  (assoc 'iva (assoc trimestre impuestos-data)))


(defun impuestos-iva-put (year trimestre data)
  "DOES:   Insert the DATA for the TRIMESTRE into the impuestos data."
  (let ((trim-data (cdr (assoc trimestre impuestos-data))))
  (setq trim-data (assoc-setq 'iva data trim-data))
  (setq impuestos-data (assoc-setq trimestre trim-data impuestos-data))))


(defun impuestos-irpf-get (year trimestre)
  "RETURN: The IRPF data for the given TRIMESTRE."
  (assoc 'irpf (assoc trimestre impuestos-data)))


(defun impuestos-irpf-put (year trimestre data)
  "DOES:   Insert the DATA for the TRIMESTRE into the impuestos data."
  (let ((trim-data (cdr (assoc trimestre impuestos-data))))
  (setq trim-data (assoc-setq 'irpf data trim-data))
  (setq impuestos-data (assoc-setq trimestre trim-data impuestos-data))))


(defun impuestos-trimestre-get (year trimestre)
  "RETURN: The impuestos data for the given trimestre."
  (if (not (member trimestre '( 1 2 3 4 )))
  (error "TRIMESTRE must be either 1, 2, 3 or 4."))
  (assoc 'trim-data (assoc trimestre impuestos-data)))


(defun impuestos-trimestre-put
  (year trimestre credit-ht credit-iva debit-ht iva-corriente iva-inversion)
  "DOES:   Insert the trimestre data into the impuestos file."
  (let ((trim-data (cdr (assoc trimestre impuestos-data)))
        (data      (list credit-ht credit-iva
                         debit-ht  iva-corriente iva-inversion)))
  (setq trim-data (assoc-setq 'trim-data data trim-data))
  (setq impuestos-data (assoc-setq trimestre trim-data impuestos-data))))



(defconst impuestos-file-path "~/.pjb-impuestos"
  "Path to the file where impuestos data is stored.")


(defun impuestos-load ()
  "DOES:   Load the impuestos file."
  (load impuestos-file-path))


(defun impuestos-save (impuestos-data)
  "DOES:   Save the impuestos data."
  (find-file impuestos-file-path)
  (erase-buffer)
  (insert (format  "(setq impuestos-data '%S)\n" impuestos-data))
  (save-buffer nil)
  (kill-buffer (current-buffer)))





(defun trimestre-credit-ht      (trimdata)  (nth 1 trimdata))
(defun trimestre-credit-iva     (trimdata)  (nth 2 trimdata))
(defun trimestre-debit-ht       (trimdata)  (nth 3 trimdata))
(defun trimestre-iva-corriente  (trimdata)  (nth 4 trimdata))
(defun trimestre-iva-inversion  (trimdata)  (nth 5 trimdata))


(defun check-vat (amount-ttc amount-ht amount-vat vat-rate)

  (unless (member vat-rate  vat-rates)
  (error "Invalid VAT rate (%f not in %S)." vat-rate vat-rates))

  (unless (member (/ (round (abs (/  amount-vat amount-ht)) 0.01) 100.0)
                  vat-rates)
2049
  (error "Invalid VAT amount (%f/%f not in %S)."
Pascal Bourguignon's avatar
Pascal Bourguignon committed
2050 2051
         amount-vat amount-ht vat-rates))

2052
  (when (/= (round amount-ttc 0.01)
Pascal Bourguignon's avatar
Pascal Bourguignon committed
2053 2054 2055 2056 2057 2058 2059
            (+ (round  amount-ht 0.01) (round amount-vat 0.01)))
    (error "amount-ttc /= amount-ht + amount-vat (%f /= %f + %f)."
           amount-ttc amount-ht amount-vat))

  (when (/= (round (/ amount-vat amount-ht) 0.01) (round vat-rate 0.01))
    (error "vat-rate /= amount-vat / amount-ht (%f /= %f / %f)."
           vat-rate amount-vat amount-ht))
2060

Pascal Bourguignon's avatar
Pascal Bourguignon committed
2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089
  );;check-vat


(defun impuestos-iva (year trimestre entries)
  "DOES:   Update the data for the given TRIMESTRE from the given ENTRIES.
RETURN: the IVA data for the given ENTRIES of journal."

  (let ((base-iva-04 0.00)
        (base-iva-07 0.00)
        (base-iva-16 0.00)
        (iva-ded-int 0.00)
        (iva-ded-adi 0.00)
        entry kind vat-rate)

    (while entries
      (setq entry   (car entries)
            entries (cdr entries))
      (setq kind     (kind entry))
      (setq vat-rate (vat-rate entry))
      (cond
       ((eq kind PRESTACION-NACIONAL)
        (cond
         ((= vat-rate 0.04)
          (setq base-iva-04 (+ base-iva-04 (credit-ht entry))))
         ((= vat-rate 0.07)
          (setq base-iva-07 (+ base-iva-07 (credit-ht entry))))
         ((= vat-rate 0.16)
          (setq base-iva-16 (+ base-iva-16 (credit-ht entry))))
         (t
2090
          (error "Invalid VAT rate %f in entry %S of journal."
Pascal Bourguignon's avatar
Pascal Bourguignon committed
2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512
                 vat-rate entry))))
       ((member kind '(PRESTACION-INTRACOMUNITARIA IMPUESTO))
        ;; Nada que hacer.
        )
       ((member kind '(INVERSION GASTO-CORRIENTE))
        (setq iva-ded-int (+ iva-ded-int (debit-iva entry))))
       ((eq kind ADQUISICION-INTRACOMUNITARIA )
        (setq iva-ded-adi (+ iva-ded-int (debit-iva entry))))
       (t
        (error "Invalid kind %s in entries of journal." kind))))

    (let* ((iva-04 (euro-round (* 0.04 base-iva-04) EUR))
           (iva-07 (euro-round (* 0.07 base-iva-07) EUR))
           (iva-16 (euro-round (* 0.16 base-iva-16) EUR))
           (total-cuota-devengada (euro-round (+ iva-04 iva-07 iva-16) EUR))
           (total-a-deducir       (euro-round (+ iva-ded-int iva-ded-adi) EUR))
           (diferencia  (euro-round
                         (- total-cuota-devengada total-a-deducir) EUR))
           (a-compensar-anterior
            (let ((anterior (if (< 1 trimestre)
                                (impuestos-assoc-val
                                 34 (impuestos-iva-get year (- trimestre 1)))
                              0.00)))
              (if (< 0.00 anterior) 0.00 (- 0.00 anterior))))
           (resultado (euro-round
                       (- diferencia a-compensar-anterior) EUR))
           (trim-data (list
                       (cons 'kind      'iva-300-euro)
                       (cons 'year      year)
                       (cons 'trimestre trimestre)
                       (cons  1 base-iva-04)
                       (cons  2 4.00)
                       (cons  3 iva-04)
                       (cons  4 base-iva-07)
                       (cons  5 7.00)
                       (cons  6 iva-07)
                       (cons  7 base-iva-16)
                       (cons  8 16.00)
                       (cons  9 iva-16)
                       (cons 21 total-cuota-devengada)
                       (cons 22 iva-ded-int)
                       (cons 24 iva-ded-adi)
                       (cons 27 total-a-deducir)
                       (cons 28 diferencia)
                       (cons 30 diferencia)
                       (cons 31 a-compensar-anterior)
                       (cons 34 resultado))
                      ))
      (impuestos-iva-put year trimestre trim-data)
      trim-data)))



(defun impuestos-irpf (year trimestre entries)
  "RETURN: The IRPF data for the given trimestre."
  (if (not (member trimestre '( 1 2 3 4 )))
      (error "TRIMESTRE must be either 1, 2, 3 or 4."))
  (let ((trim 1)
        (ingresos-computables 0.00)
        (gastos-deducibles    0.00)
        (pagos-anteriores     0.00)
        rendimiento veinte-por-ciento pago-fraccionado
        entry kind
        )

    ;; Get the totals for the current trimestre.
    (while entries
      (setq entry   (car entries)
            entries (cdr entries))
      (setq kind     (kind entry))
      (if (is-credit entry)
          (if (member kind '(PRESTACION-NACIONAL
                             PRESTACION-INTRACOMUNITARIA))
              (setq ingresos-computables (+ ingresos-computables
                                            (credit-ht entry))))
        (if (member kind '(INVERSION
                           GASTO-CORRIENTE
                           ADQUISICION-INTRACOMUNITARIA
                           PRESTACION-NACIONAL
                           PRESTACION-INTRACOMUNITARIA))
            (setq gastos-deducibles    (+ gastos-deducibles
                                          (debit-ht entry)))))
      )

    ;; Add the totals for the previous trimestres.
    (if (< 1 trimestre)
        (let ((irpf-data       (impuestos-irpf-get year (- trimestre 1))))
          (setq ingresos-computables (+ ingresos-computables
                                        (impuestos-assoc-val 1 irpf-data))
                gastos-deducibles    (+ gastos-deducibles
                                        (impuestos-assoc-val 2 irpf-data)))))
    (while (< trim trimestre)
      (let ((irpf-data       (impuestos-irpf-get year trim)))
        (setq pagos-anteriores     (+ pagos-anteriores
                                      (impuestos-assoc-val 7 irpf-data))))
      (setq trim (+ trim 1)))

    ;; Compute the IRFP data.
    (setq rendimiento          (euro-round (- ingresos-computables
                                              gastos-deducibles) EUR))
    (setq veinte-por-ciento    (if (< 0.00 rendimiento)
                                   (euro-round (* 0.20 rendimiento)   EUR)
                                 0.00))
    (if (>= 0.00 veinte-por-ciento)
        (setq pagos-anteriores 0.00))
    (setq pago-fraccionado
          (euro-round (- veinte-por-ciento pagos-anteriores) EUR))
    (if (>= 0.00 pago-fraccionado)
        (setq pago-fraccionado 0.00))

    (let ((trim-data (list
                      (cons 'kind      'irpf-130-euro)
                      (cons 'year      year)
                      (cons 'trimestre trimestre)
                      (cons 1 ingresos-computables)
                      (cons 2 gastos-deducibles)
                      (cons 3 rendimiento)
                      (cons 4 veinte-por-ciento)
                      (cons 5 pagos-anteriores)
                      (cons 7 pago-fraccionado)
                      (cons 12 pago-fraccionado)
                      (cons 14 pago-fraccionado))))
      (impuestos-irpf-put year trimestre trim-data)
      trim-data)
    ))




(defun impuestos-assoc-val (key alist)
  (let ((value (assoc-val key alist)))
    (if value value 0.00)))


(defun impuestos-iva-display (data)
  (insert (format "\n\n%s\n" (impuestos-assoc-val 'kind data)))
  (insert (format "Trimestre: %d\n" (impuestos-assoc-val 'trimestre data)))
  (insert (format "01 : %10.2f     02 :  %5.2f %%     03 : %10.2f \n"
                  (impuestos-assoc-val 1 data)
                  (impuestos-assoc-val 2 data)
                  (impuestos-assoc-val 3 data)))
  (insert (format "04 : %10.2f     05 :  %5.2f %%     06 : %10.2f \n"
                  (impuestos-assoc-val 4 data)
                  (impuestos-assoc-val 5 data)
                  (impuestos-assoc-val 6 data)))
  (insert (format "07 : %10.2f     08 :  %5.2f %%     09 : %10.2f \n"
                  (impuestos-assoc-val 7 data)
                  (impuestos-assoc-val 8 data)
                  (impuestos-assoc-val 9 data)))
  (insert (format "%38s21 : %10.2f\n"  "Total cuota devengada: "
                  (impuestos-assoc-val 21 data)))
  (insert (format "%38s22 : %10.2f\n"  "I.V.A. deducible oper. interiores: "
                  (impuestos-assoc-val 22 data)))
  (insert (format "%38s24 : %10.2f\n"  "I.V.A. deducible adq. intracomun.: "
                  (impuestos-assoc-val 24 data)))
  (insert (format "%38s27 : %10.2f\n"  "Total a deducir: "
                  (impuestos-assoc-val 27 data)))
  (insert (format "%38s28 : %10.2f\n"  "Diferencia: "
                  (impuestos-assoc-val 28 data)))
  (insert (format "%38s30 : %10.2f\n"  "" (impuestos-assoc-val 30 data)))
  (insert (format "%38s31 : %10.2f\n"  "Cuotas a compensar de p. anteriores: "
                  (impuestos-assoc-val 31 data)))
  (insert (format "%38s34 : %10.2f\n"  "Resultado: "
                  (impuestos-assoc-val 34 data)))
  )


(defun impuestos-irpf-display (data)
  (insert (format "\n\n%s\n" (impuestos-assoc-val 'kind data)))
  (insert (format "Trimestre: %d\n" (impuestos-assoc-val 'trimestre data)))
  (insert (format "%38s01 : %10.2f\n"  "Ingresos computables: "
                  (impuestos-assoc-val  1 data)))
  (insert (format "%38s02 : %10.2f\n"  "Gastos fiscalmente deducibles: "
                  (impuestos-assoc-val  2 data)))
  (insert (format "%38s03 : %10.2f\n"  "Rendimiento neto: "
                  (impuestos-assoc-val  3 data)))
  (insert (format "%38s04 : %10.2f\n"  "20 por 100 de rendimiento neto: "
                  (impuestos-assoc-val  4 data)))
  (insert (format "%38s05 : %10.2f\n"  "Pagos fraccionados anteriores: "
                  (impuestos-assoc-val  5 data)))
  (insert (format "%38s06 : %10.2f\n"  "Retenciones y ingresos: "
                  (impuestos-assoc-val  6 data)))
  (insert (format "%38s07 : %10.2f\n"  "Pago fraccionado: "
                  (impuestos-assoc-val  7 data)))
  (insert (format "%38s12 : %10.2f\n"  "Suma de los pagos fraccionados: "
                  (impuestos-assoc-val 12 data)))
  (insert (format "%38s13 : %10.2f\n"  "A deducir: "
                  (impuestos-assoc-val 13 data)))
  (insert (format "%38s14 : %10.2f\n"  "Cuota a ingresar: "
                  (impuestos-assoc-val 14 data)))
  )



(defun impuestos (year trimestre &optional entries)
  "DOES:   Computes and inserts into the current buffer at the current point
        the IVA and the IRPF from the data in the journal for the given YEAR
        and TRIMESTRE."

  (if (not (member trimestre '( 1 2 3 4 )))
      (error "TRIMESTRE must be either 1, 2, 3 or 4."))

  (if (null entries)  (setq entries (journal-extract year trimestre)))
  ;;  (if (null totals)   (setq totals  (journal-totals-of-entries entries)))

  (let (iva-data irpf-data)

    (impuestos-load)

    (setq iva-data  (impuestos-iva  year trimestre entries))
    (impuestos-iva-display  iva-data)

    (setq irpf-data (impuestos-irpf year trimestre entries))
    (impuestos-irpf-display irpf-data)

    (impuestos-save impuestos-data)
    ))





(defun date-after (date limite)
  "
RETURN:  Whether date is after limite.
"
  (or (< (nth 2 limite) (nth 2 date));; year is after
      (and (= (nth 2 limite) (nth 2 date));; same year
           (or (< (nth 0 limite) (nth 0 date));; month is after
               (and (= (nth 0 limite) (nth 0 date));; same month
                    (< (nth 1 limite) (nth 1 date))))));; day is after
  );;date-after




;;; (defun old-show-tva (montant-ht taux &optional devise &rest options)
;;;   "Affiche le montant HT donné, la TVA, le montant TTC, et éventuellement
;;; le montant dans une autre devise optionnelle. La langue peut aussi être
;;; indiquée ('es, 'fr, 'en).
;;;
;;; Le TAUX  peut être nil  auquel cas la  facture est émise hors  taxe et
;;; sans la ligne TVA.
;;;
;;; Si la DEVISE n'est pas spécifiée, la facture est en EURO.
;;;
;;;
;;;  (show-tva 750.00 EUR FRF)  donne :
;;;  ----------------------------------------------------   -----------------
;;;                      (Base  imponible)  Total HT    :          750.00 EUR
;;;                      (IVA   16.0 %)     TVA  16.0 % :   +      120.00 EUR
;;;                      (Total factura)    Total TTC   :   =      870.00 EUR
;;;  ----------------------------------------------------   -----------------
;;; "
;;;
;;;    (let* (
;;;           (strings '(
;;;   (es "Base imponible "  "IVA %4.1f %%     "  "Total factura  " "   " "   " "   ")
;;;   (fr "Total HT       "  "TVA %4.1f %%     "  "Total TTC      " "HT " "   " "TTC")
;;;   (en "Total          "  "VAT %4.1f %%     "  "Total VAT Inc. " "   " "   " "   ")
;;;   ))
;;;
;;;           (line-form " %52s     %21s\n")
;;;           (desc-line (make-string 52 ?-))
;;;           (pric-line (make-string 21 ?-))
;;;
;;;           (base-lab-gau "")
;;;           (tvat-lab-gau "")
;;;           (tota-lab-gau "")
;;;
;;;           (base-lab-dro)
;;;           (tvat-lab-dro)
;;;           (tota-lab-dro)
;;;
;;;           (base-lab-com)
;;;           (tvat-lab-com)
;;;           (tota-lab-com)
;;;
;;;           (montant-tva  (* montant-ht taux))
;;;           (montant-ttc  (* montant-ht (+ 1.0 taux)))
;;;           (montant-aut)
;;;
;;;           (lang-pri     'es)
;;;           (lang-sec     'es)
;;;
;;;           (devises (mapcar 'car euro-parities))
;;;           (ltrings)
;;;           )
;;;
;;;      (while options
;;;        (let ((op (car options)))
;;;          (setq options (cdr options))
;;;          (cond
;;;           ((member op '(fr en es))  (setq lang-sec lang-pri lang-pri op))
;;;           ((member op devises)      (setq devi-sec op))
;;;           (t (error (concat "Option inconnue : %S "
;;;                             "(ni une langue connue, ni une devise connue).")
;;;                     op)))))
;;;
;;;      (if (equal lang-pri lang-sec)
;;;          (setq lang-sec nil))
;;;
;;;      (setq montant-aut
;;;            (if (and devi-sec (not (equal devi-pri devi-sec)))
;;;                (euro-to-value devi-sec (euro-from-value montant-ttc devi-pri))
;;;              nil))
;;;
;;;      (setq ltrings (assoc lang-pri strings))
;;;      (setq base-lab-dro (format "%s :" (nth 1 ltrings))
;;;            tvat-lab-dro (format "%s :" (format (nth 2 ltrings) (* 100.0 taux)))
;;;            tota-lab-dro (format "%s :" (nth 3 ltrings))
;;;            base-lab-com (nth 4 ltrings)
;;;            tvat-lab-com (nth 5 ltrings)
;;;            tota-lab-com (nth 6 ltrings))
;;;      (if lang-sec
;;;          (progn
;;;            (setq ltrings (assoc lang-sec strings))
;;;            (setq base-lab-gau (nth 1 ltrings)
;;;                  tvat-lab-gau (format (nth 2 ltrings) (* 100.0 taux))
;;;                  tota-lab-gau (nth 3 ltrings))
;;;            (setq base-lab-gau (format "(%s) " base-lab-gau)
;;;                  tvat-lab-gau (format "(%s) " tvat-lab-gau)
;;;                  tota-lab-gau (format "(%s) " tota-lab-gau))
;;;            ))
;;;
;;;      (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 %s" montant-ht  devise base-lab-com)))
;;;      (insert (format line-form
;;;                      (concat tvat-lab-gau tvat-lab-dro)
;;;                      (format "+%12.2f %-3s %s" montant-tva devise tvat-lab-com)))
;;;      (insert (format line-form
;;;                      (concat tota-lab-gau tota-lab-dro)
;;;                      (format "=%12.2f %-3s %s" montant-ttc devise tota-lab-com)))
;;;      (if devi-sec
;;;          (insert (format line-form ""
;;;                          (format "=%12.2f %-3s %s"
;;;                                  montant-aut devi-sec tota-lab-com))))
;;;      (insert (format line-form desc-line pric-line))
;;; ));;old-show-tva
;;; )


(defun solde (credit debit devise)
  "Affiche le solde d'un compte (pour les fichiers comptes)."
  (interactive)
  (let ((lines "---------- ---------- ---------- ------- ------------------------ --------------\n")
        (value (- credit debit))
        (value-eur (euro-round (/ (- credit debit)
                                  (euro-get-ratio devise)) :EUR)))
    (insert-string
     (format
      (concat
       "\n" lines "           "
       (euro-get-format :EUR)
       " "
       (euro-get-format :EUR)
       "\n" lines "           "
       (euro-get-format :EUR)
       "\n")
      credit debit value value-eur)
     )
    ;;(let ((value-frf (euro-to-value :FRF value-eur))
    ;;(value-esp (euro-to-value :ESP value-eur)))
    ;;"Solde actuel Euro        "
    ;;(euro-get-format :EUR) " EUR"
    ;;"\n                                         "
    ;;"Solde actuel Franc       "
    ;;(euro-get-format :FRF) " FRF"
    ;;"\n                                         "
    ;;"Solde actuel Peseta      "
    ;;(euro-get-format :ESP) " ESP"
    ;;"\n                                         "
    ;; )
    ;;value-frf value-esp)
    ));;solde



(defun solde-line (credit debit devise title)
  "Affiche une ligne de solde d'un compte (pour les fichiers comptes)."
  (interactive)
  (let ((value (- credit debit))
        (value-eur (euro-round (/ (- credit debit)
                                  (euro-get-ratio devise)) :EUR)))
    (insert
     (format
      (concat
       "%-20s  "
       (euro-get-format :EUR) " EUR  "
       "\n"
       )
      title value-eur)
     )
    ));;solde-line




(defun show-impots (kind ca)
  "Formate le chiffre d'affaire HT donné avec la provision pour impôts (20%)."
  (let* ((cattc (if (eq kind 'TTC) ca nil))
         (caht  (if (eq kind 'HT)  ca (/ ca 1.16)))
         (impot (* caht 0.20))
         (line  (format "%-10s+%-8s+%-37s+%-18s\n"
                        (make-string 10 ?-)
                        (make-string 8 ?-)
                        (make-string 37 ?-)
                        (make-string 14 ?-)))
         (form  "%-10s|%-8s|%-37s|%10.2f EUR\n"))
    (insert line)
    (if (eq kind 'TTC)
        (insert (format form "" "" "Total Réglé (TTC) :"        cattc)))
    (insert (format form "" "" "Total       (HT)  :"        caht))
    (insert (format form "" "" "Provision Impôts (20%) :"   impot))
    (insert (format form "" "" "Reste :"   (- caht impot) ""))
    (insert line)
    ))

;;;; pjb-invoices.el                  -- 2001-03-21 16:15:28 -- pascal   ;;;;