Skip to content

Commit b63d32f

Browse files
committed
Alternative implementation using cog.
1 parent fcbb9af commit b63d32f

File tree

4 files changed

+102
-19
lines changed

4 files changed

+102
-19
lines changed

README.rst

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,22 +47,30 @@ Parsing::
4747

4848
``Document.parse()`` taskes a boolean parameter ``strict`` which defaults to ``True``. This means that the parser will raise an error if it encounters any unknown element. If you set it to ``False``, the parser will not raise an error and parse whatever it can.
4949

50-
Generating::
50+
.. [[[cog
51+
# Re-run this with `cog -r README.rst`
52+
53+
from pathlib import Path
54+
from textwrap import indent
55+
56+
import cog
5157
58+
cog.outl("Generating::\n")
59+
cog.outl(indent(Path("example.py").read_text(encoding="UTF-8"), " "), dedent=False)
60+
.. ]]]
61+
Generating::
5262
from datetime import date, datetime, timedelta, timezone
5363
from decimal import Decimal
5464

5565
from drafthorse.models.accounting import ApplicableTradeTax
5666
from drafthorse.models.document import Document
5767
from drafthorse.models.note import IncludedNote
5868
from drafthorse.models.party import TaxRegistration
59-
from drafthorse.models.payment import PaymentTerms
60-
from drafthorse.models.payment import PaymentMeans
69+
from drafthorse.models.payment import PaymentMeans, PaymentTerms
6170
from drafthorse.models.trade import AdvancePayment, IncludedTradeTax
6271
from drafthorse.models.tradelines import LineItem
6372
from drafthorse.pdf import attach_xml
6473

65-
6674
# Build data structure
6775
doc = Document()
6876
doc.context.guideline_parameter.id = "urn:cen.eu:en16931:2017"
@@ -137,6 +145,8 @@ Generating::
137145
# Generate XML file
138146
xml = doc.serialize(schema="FACTUR-X_EXTENDED")
139147

148+
.. [[[end]]]
149+
140150
# Attach XML to an existing PDF.
141151
# Note that the existing PDF should be compliant to PDF/A-3!
142152
# You can validate this here: https://www.pdf-online.com/osa/validate.aspx

example.py

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
from datetime import date, datetime, timedelta, timezone
2+
from decimal import Decimal
3+
4+
from drafthorse.models.accounting import ApplicableTradeTax
5+
from drafthorse.models.document import Document
6+
from drafthorse.models.note import IncludedNote
7+
from drafthorse.models.party import TaxRegistration
8+
from drafthorse.models.payment import PaymentMeans, PaymentTerms
9+
from drafthorse.models.trade import AdvancePayment, IncludedTradeTax
10+
from drafthorse.models.tradelines import LineItem
11+
from drafthorse.pdf import attach_xml
12+
13+
# Build data structure
14+
doc = Document()
15+
doc.context.guideline_parameter.id = "urn:cen.eu:en16931:2017"
16+
doc.header.id = "RE1337"
17+
doc.header.type_code = "380"
18+
doc.header.issue_date_time = date.today()
19+
20+
doc.header.notes.add(IncludedNote(content="Test Note 1"))
21+
22+
doc.trade.agreement.buyer.name = "Kunde GmbH"
23+
doc.trade.agreement.buyer.address.country_id = "DE"
24+
25+
doc.trade.settlement.currency_code = "EUR"
26+
doc.trade.settlement.payment_means.add(PaymentMeans(type_code="ZZZ"))
27+
28+
doc.trade.agreement.seller.name = "Lieferant GmbH"
29+
doc.trade.agreement.seller.address.country_id = "DE"
30+
doc.trade.agreement.seller.address.country_subdivision = "Bayern"
31+
doc.trade.agreement.seller.tax_registrations.add(
32+
TaxRegistration(
33+
id=("VA", "DE000000000")
34+
)
35+
)
36+
37+
advance = AdvancePayment(
38+
received_date=datetime.now(timezone.utc), paid_amount=Decimal(42)
39+
)
40+
advance.included_trade_tax.add(
41+
IncludedTradeTax(
42+
calculated_amount=Decimal(0),
43+
type_code="VAT",
44+
category_code="E",
45+
rate_applicable_percent=Decimal(0),
46+
)
47+
)
48+
doc.trade.settlement.advance_payment.add(advance)
49+
50+
li = LineItem()
51+
li.document.line_id = "1"
52+
li.product.name = "Rainbow"
53+
li.agreement.gross.amount = Decimal("1198.8")
54+
li.agreement.gross.basis_quantity = (Decimal("1.0000"), "C62") # C62 == unit
55+
li.agreement.net.amount = Decimal("999")
56+
li.agreement.net.basis_quantity = (Decimal("1.0000"), "C62") # C62 == unit
57+
li.delivery.billed_quantity = (Decimal("1.0000"), "C62") # C62 == unit
58+
li.settlement.trade_tax.type_code = "VAT"
59+
li.settlement.trade_tax.category_code = "S"
60+
li.settlement.trade_tax.rate_applicable_percent = Decimal("20.00")
61+
li.settlement.monetary_summation.total_amount = Decimal("999.00")
62+
doc.trade.items.add(li)
63+
64+
trade_tax = ApplicableTradeTax()
65+
trade_tax.calculated_amount = Decimal("199.80")
66+
trade_tax.basis_amount = Decimal("999.00")
67+
trade_tax.type_code = "VAT"
68+
trade_tax.category_code = "S"
69+
trade_tax.rate_applicable_percent = Decimal("20.00")
70+
doc.trade.settlement.trade_tax.add(trade_tax)
71+
72+
doc.trade.settlement.monetary_summation.line_total = Decimal("999.00")
73+
doc.trade.settlement.monetary_summation.charge_total = Decimal("0.00")
74+
doc.trade.settlement.monetary_summation.allowance_total = Decimal("0.00")
75+
doc.trade.settlement.monetary_summation.tax_basis_total = Decimal("999.00")
76+
doc.trade.settlement.monetary_summation.tax_total = (Decimal("199.80"), "EUR")
77+
doc.trade.settlement.monetary_summation.grand_total = Decimal("1198.8")
78+
doc.trade.settlement.monetary_summation.due_amount = Decimal("1198.8")
79+
80+
terms = PaymentTerms()
81+
terms.due = datetime.now(timezone.utc) + timedelta(days=30)
82+
doc.trade.settlement.terms.add(terms)
83+
84+
# Generate XML file
85+
xml = doc.serialize(schema="FACTUR-X_EXTENDED")

requirements_dev.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
cogapp
12
lxml
23
pypdf
34
pytest

tests/conftest.py

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,11 @@
11
import os
22
import pytest
3-
import re
4-
from pathlib import Path
5-
from textwrap import dedent
6-
7-
README_PATH = Path(__file__).parent.parent / "README.rst"
3+
from example import doc
84

95

106
@pytest.fixture
117
def invoice_document(request):
12-
readme = README_PATH.read_text(encoding="UTF-8")
13-
readme_example = re.search(
14-
"Generating::$" "(?P<example>.*)" "# Attach XML to an existing PDF.$",
15-
readme,
16-
flags=re.MULTILINE | re.DOTALL,
17-
)
18-
assert readme_example
19-
local_ns = {}
20-
exec(dedent(readme_example["example"]), locals=local_ns)
21-
return local_ns['doc']
8+
return doc
229

2310

2411
@pytest.fixture

0 commit comments

Comments
 (0)