|
41 | 41 | from django.utils.timezone import now |
42 | 42 | from django.utils.translation import gettext, override |
43 | 43 | from lxml import etree |
| 44 | +from pycheval import ( |
| 45 | + BankAccount, |
| 46 | + EN16931Invoice, |
| 47 | + EN16931LineItem, |
| 48 | + Money, |
| 49 | + PaymentMeans, |
| 50 | + PaymentTerms, |
| 51 | + PostalAddress, |
| 52 | + Tax, |
| 53 | + TradeParty, |
| 54 | + generate_xml, |
| 55 | +) |
| 56 | +from pycheval.quantities import QuantityCode |
| 57 | +from pycheval.type_codes import DocumentTypeCode, PaymentMeansCode, TaxCategoryCode |
| 58 | +from weasyprint import Attachment |
44 | 59 |
|
45 | 60 | from weblate_web.exchange_rates import ExchangeRates |
46 | 61 | from weblate_web.pdf import render_pdf |
@@ -540,8 +555,14 @@ def xml_path(self) -> Path: |
540 | 555 | """XML path object.""" |
541 | 556 | return settings.INVOICES_PATH / self.get_filename("xml") |
542 | 557 |
|
| 558 | + @property |
| 559 | + def en_16931_xml_path(self) -> Path: |
| 560 | + """XML path object.""" |
| 561 | + return settings.INVOICES_PATH / self.get_filename("einvoice.xml") |
| 562 | + |
543 | 563 | def generate_files(self) -> None: |
544 | 564 | self.generate_money_s3_xml() |
| 565 | + self.generate_en_16931_xml() |
545 | 566 | self.generate_pdf() |
546 | 567 | self.sync_files() |
547 | 568 |
|
@@ -657,7 +678,7 @@ def add_amounts(root, in_czk: bool = False) -> None: |
657 | 678 | add_element(adresa, "Ulice", self.customer.address) |
658 | 679 | add_element(adresa, "Misto", self.customer.city) |
659 | 680 | add_element(adresa, "PSC", self.customer.postcode) |
660 | | - add_element(adresa, "Stat", self.customer.country) |
| 681 | + add_element(adresa, "Stat", self.customer.country.code) |
661 | 682 | if self.customer.vat: |
662 | 683 | add_element(prijemce, "PlatceDPH", "1") |
663 | 684 | add_element(prijemce, "FyzOsoba", "0") |
@@ -690,13 +711,151 @@ def generate_money_s3_xml(self) -> None: |
690 | 711 | settings.INVOICES_PATH.mkdir(exist_ok=True) |
691 | 712 | self.save_invoice_xml(document, self.xml_path) |
692 | 713 |
|
| 714 | + def get_en_16931_xml(self) -> EN16931Invoice: |
| 715 | + type_code = DocumentTypeCode.INVOICING_DATA_SHEET |
| 716 | + if self.kind == InvoiceKind.INVOICE: |
| 717 | + type_code = DocumentTypeCode.INVOICE |
| 718 | + elif self.kind == InvoiceKind.QUOTE: |
| 719 | + type_code = DocumentTypeCode.VALIDATED_PRICED_TENDER |
| 720 | + elif self.kind == InvoiceKind.PROFORMA: |
| 721 | + type_code = DocumentTypeCode.PRO_FORMA_INVOICE |
| 722 | + total_amount = Money(self.total_amount, self.get_currency_display()) |
| 723 | + |
| 724 | + tax_amount = Money(self.total_vat, self.get_currency_display()) |
| 725 | + tax_basis_amount = Money(self.total_amount_no_vat, self.get_currency_display()) |
| 726 | + |
| 727 | + tax_category = ( |
| 728 | + TaxCategoryCode.STANDARD_RATE |
| 729 | + if self.vat_rate |
| 730 | + else TaxCategoryCode.REVERSE_CHARGE |
| 731 | + ) |
| 732 | + tax = Tax( |
| 733 | + category_code=tax_category, |
| 734 | + calculated_amount=tax_amount, |
| 735 | + basis_amount=tax_basis_amount, |
| 736 | + rate_percent=self.vat_rate or None, |
| 737 | + exemption_reason="Reverse charge" if not self.vat_rate else None, |
| 738 | + ) |
| 739 | + |
| 740 | + line_items = [ |
| 741 | + EN16931LineItem( |
| 742 | + id=item.package.name if item.package else "ITEM", |
| 743 | + name=item.description, |
| 744 | + net_price=Money(item.unit_price, self.get_currency_display()), |
| 745 | + billed_quantity=( |
| 746 | + item.quantity, |
| 747 | + QuantityCode.ONE, |
| 748 | + ), # TODO: convert quantity_unit to QuantityCode |
| 749 | + billed_total=Money(item.total_price, self.get_currency_display()), |
| 750 | + tax_rate=self.vat_rate or None, |
| 751 | + tax_category=tax_category, |
| 752 | + billing_period=(item.start_date, item.end_date) |
| 753 | + if item.has_date_range |
| 754 | + else None, |
| 755 | + ) |
| 756 | + for item in self.all_items |
| 757 | + ] |
| 758 | + # TODO: There might be model for discount |
| 759 | + if self.discount: |
| 760 | + line_items.append( |
| 761 | + EN16931LineItem( |
| 762 | + id="DISCOUNT", |
| 763 | + name=self.discount.description, |
| 764 | + description=self.discount.display_percents, |
| 765 | + net_price=Money(self.total_discount, self.get_currency_display()), |
| 766 | + billed_quantity=(1, QuantityCode.ONE), |
| 767 | + billed_total=Money( |
| 768 | + self.total_discount, self.get_currency_display() |
| 769 | + ), |
| 770 | + tax_rate=self.vat_rate, |
| 771 | + tax_category=tax_category, |
| 772 | + ) |
| 773 | + ) |
| 774 | + |
| 775 | + payment_means = [] |
| 776 | + if self.prepaid: |
| 777 | + prepaid_amount = total_amount |
| 778 | + due_payable_amount = Money(Decimal(0), self.get_currency_display()) |
| 779 | + payment_reference = None |
| 780 | + payment_terms = None |
| 781 | + else: |
| 782 | + prepaid_amount = None |
| 783 | + due_payable_amount = total_amount |
| 784 | + payment_reference = self.number |
| 785 | + if self.currency == Currency.EUR: |
| 786 | + # TODO: support other currencies here |
| 787 | + payment_means = [ |
| 788 | + PaymentMeans( |
| 789 | + type_code=PaymentMeansCode.BANK_PAYMENT, |
| 790 | + payee_account=BankAccount( |
| 791 | + name=self.bank_account.holder, |
| 792 | + bank_id=self.bank_account.bic, |
| 793 | + iban=self.bank_account.raw_iban, |
| 794 | + ), |
| 795 | + payee_bic=self.bank_account.bic, |
| 796 | + ) |
| 797 | + ] |
| 798 | + payment_terms = PaymentTerms(due_date=self.due_date) |
| 799 | + |
| 800 | + return EN16931Invoice( |
| 801 | + invoice_number=self.number, |
| 802 | + invoice_date=self.issue_date, |
| 803 | + currency_code=self.get_currency_display(), |
| 804 | + grand_total_amount=total_amount, |
| 805 | + tax_basis_total_amount=total_amount, |
| 806 | + tax_total_amounts=[tax_amount] if self.vat_rate else [], |
| 807 | + due_payable_amount=due_payable_amount, |
| 808 | + prepaid_amount=prepaid_amount, |
| 809 | + payment_reference=payment_reference, |
| 810 | + payment_means=payment_means, |
| 811 | + payment_terms=payment_terms, |
| 812 | + line_total_amount=total_amount, |
| 813 | + line_items=line_items, |
| 814 | + type_code=type_code, |
| 815 | + seller=TradeParty( |
| 816 | + name="Weblate s.r.o.", |
| 817 | + address=PostalAddress( |
| 818 | + country_code="CZ", |
| 819 | + post_code="471 54", |
| 820 | + city="Cvikov", |
| 821 | + line_one="Nábřežní 694", |
| 822 | + ), |
| 823 | + vat_id="CZ21668027", |
| 824 | + ), |
| 825 | + buyer=TradeParty( |
| 826 | + name=self.customer.name, |
| 827 | + address=PostalAddress( |
| 828 | + country_code=self.customer.country.code, |
| 829 | + post_code=self.customer.postcode, |
| 830 | + city=self.customer.city, |
| 831 | + line_one=self.customer.address, |
| 832 | + line_two=self.customer.address_2 or None, |
| 833 | + ), |
| 834 | + vat_id=self.customer.vat or None, |
| 835 | + ), |
| 836 | + tax=[tax], |
| 837 | + ) |
| 838 | + |
| 839 | + def generate_en_16931_xml(self) -> None: |
| 840 | + invoice = self.get_en_16931_xml() |
| 841 | + xml_string = generate_xml(invoice) |
| 842 | + self.en_16931_xml_path.write_text(xml_string) |
| 843 | + |
693 | 844 | def generate_pdf(self) -> None: |
694 | 845 | """Render invoice as PDF.""" |
695 | 846 | # Create directory to store invoices |
696 | 847 | settings.INVOICES_PATH.mkdir(exist_ok=True) |
697 | 848 | render_pdf( |
698 | 849 | html=self.render_html(), |
699 | 850 | output=settings.INVOICES_PATH / self.filename, |
| 851 | + pdf_variant="pdf/a-3b", |
| 852 | + attachments=[ |
| 853 | + Attachment( |
| 854 | + string=generate_xml(self.get_en_16931_xml()), |
| 855 | + base_url="factur-x.xml", |
| 856 | + description="Factur-x invoice", |
| 857 | + ) |
| 858 | + ], |
700 | 859 | ) |
701 | 860 |
|
702 | 861 | def duplicate( # noqa: PLR0913 |
|
0 commit comments