add: creditnote

This commit is contained in:
Frederik Jaeckel 2023-06-30 15:29:51 +02:00
parent c4571a28b9
commit 28a96bc7c9
4 changed files with 259 additions and 6 deletions

View file

@ -22,6 +22,19 @@ class Invoice(Invoice):
if getattr(self.invoice, 'sales', None) is not None:
return ', '.join([x.number for x in self.invoice.sales])
def negate_amount(self, amount):
""" amount * -1.0
"""
if amount is not None and amount:
if isinstance(amount, Decimal):
return amount.copy_negate()
elif isinstance(amount, float):
return -1.0 * amount
elif isinstance(amount, int):
return -1 * amount
else :
return amount
def prepaid_amount(self, invoice):
""" compute already paid amount
"""
@ -108,7 +121,12 @@ class Invoice(Invoice):
loader = genshi.template.TemplateLoader(
os.path.join(os.path.dirname(__file__), 'template'),
auto_reload=True)
return loader.load(os.path.join(version, 'XRechnung.xml'))
if self.type_code in ['380', '389']:
return loader.load(os.path.join(version, 'XRechnung_invoice.xml'))
elif self.type_code in ['381', '261']:
return loader.load(os.path.join(version, 'XRechnung_credit.xml'))
else :
raise ValueError('invalid type-code "%s"' % self.type_code)
else:
return super(Invoice, self)._get_template(version)

View file

@ -0,0 +1,182 @@
<?xml version="1.0" encoding="UTF-8"?>
<ubl:CreditNote
xmlns:ubl="urn:oasis:names:specification:ubl:schema:xsd:CreditNote-2"
xmlns:cac="urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2"
xmlns:cbc="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:qdt="urn:oasis:names:specification:ubl:schema:xsd:QualifiedDataTypes-2"
xmlns:udt="urn:oasis:names:specification:ubl:schema:xsd:UnqualifiedDataTypes-2"
xmlns:py="http://genshi.edgewall.org/"
xsi:schemaLocation="urn:oasis:names:specification:ubl:schema:xsd:CreditNote-2 http://docs.oasis-open.org/ubl/os-UBL-2.1/xsd/maindoc/UBL-CreditNote-2.1.xsd">
<py:def function="PostalAddress(value)">
<cbc:StreetName>${', '.join((getattr(value, 'street', None) or '').split('\n'))}</cbc:StreetName>
<cbc:CityName>${getattr(value, 'city', None) or ''}</cbc:CityName>
<cbc:PostalZone>${getattr(value, 'postal_code', None) or ''}</cbc:PostalZone>
<cac:Country>
<cbc:IdentificationCode>${getattr(getattr(value, 'country', None), 'code', None)}</cbc:IdentificationCode>
</cac:Country>
</py:def>
<py:def function="PartyTaxScheme(value)">
<cbc:CompanyID>${this.taxident_data(value)['code']}</cbc:CompanyID>
<cac:TaxScheme>
<cbc:ID>${this.taxident_data(value)['id']}</cbc:ID>
</cac:TaxScheme>
</py:def>
<py:def function="Contact(value)">
<cbc:Name>${value.name}</cbc:Name>
<py:if test="value.phone">
<cbc:Telephone>${value.phone}</cbc:Telephone>
</py:if>
<py:if test="value.email">
<cbc:ElectronicMail>${value.email}</cbc:ElectronicMail>
</py:if>
</py:def>
<py:def function="PartyLegalEntity(value, company_id=True)">
<cbc:RegistrationName>${value.name}</cbc:RegistrationName>
<cbc:CompanyID py:if="company_id">${this.taxident_data(this.seller_trade_tax_identifier)['code']}</cbc:CompanyID>
</py:def>
<py:def function="PayeeFinancialAccount(value)">
<py:if test="len(value.bank_accounts)>0">
<cbc:ID>${value.bank_accounts[0].numbers[0].number_compact}</cbc:ID>
<cbc:Name>${value.name}</cbc:Name>
</py:if>
</py:def>
<py:def function="TaxCategory(value, incl_reason=False)">
<cbc:ID>${this.tax_category_code(value)}</cbc:ID>
<cbc:Percent>${this.tax_rate(value)}</cbc:Percent>
<cbc:TaxExemptionReason
py:if="(this.tax_category_code(value) in ['E']) and (incl_reason==True)">${value.legal_notice or '-'}</cbc:TaxExemptionReason>
<cac:TaxScheme>
<cbc:ID>VAT</cbc:ID>
</cac:TaxScheme>
</py:def>
<py:def function="TaxSubTotal(value)">
<cac:TaxSubtotal>
<cbc:TaxableAmount py:attrs="{'currencyID': value.currency.code}">${this.negate_amount(value.base)}</cbc:TaxableAmount>
<cbc:TaxAmount py:attrs="{'currencyID': value.currency.code}">${this.negate_amount(value.amount)}</cbc:TaxAmount>
<cac:TaxCategory>
${TaxCategory(value.tax, True)}
</cac:TaxCategory>
</cac:TaxSubtotal>
</py:def>
<py:def function="CreditNoteLine(value)">
<cac:CreditNoteLine>
<cbc:ID>${value.id}</cbc:ID>
<cbc:CreditedQuantity py:attrs="{'unitCode': this.uom_unece_code(value)}">${this.negate_amount(value.quantity)}</cbc:CreditedQuantity>
<cbc:LineExtensionAmount py:attrs="{'currencyID': value.currency.code}">${this.negate_amount(value.amount)}</cbc:LineExtensionAmount>
<cac:Item>
<cbc:Name>${value.description or getattr(value.product, 'name', None)}</cbc:Name>
<cac:SellersItemIdentification py:if="getattr(value.product, 'code', None)">
<cbc:ID>${value.product.code}</cbc:ID>
</cac:SellersItemIdentification>
<cac:ClassifiedTaxCategory>
${TaxCategory(this.invoice_line_tax(value))}
</cac:ClassifiedTaxCategory>
</cac:Item>
<cac:Price>
<cbc:PriceAmount py:attrs="{'currencyID': value.currency.code}">${value.unit_price}</cbc:PriceAmount>
</cac:Price>
</cac:CreditNoteLine>
</py:def>
<cbc:CustomizationID>urn:cen.eu:en16931:2017#compliant#urn:xoev-de:kosit:standard:xrechnung_2.1</cbc:CustomizationID>
<cbc:ID>${this.invoice.number}</cbc:ID>
<cbc:IssueDate>${this.invoice.invoice_date.isoformat()}</cbc:IssueDate>
<cbc:CreditNoteTypeCode>${this.type_code}</cbc:CreditNoteTypeCode>
<cbc:Note py:if="this.invoice_note()">${this.invoice_note()}</cbc:Note>
<cbc:DocumentCurrencyCode>${this.invoice.currency.code}</cbc:DocumentCurrencyCode>
<cbc:BuyerReference>${this.invoice.party.get_xrechnung_route_id()}</cbc:BuyerReference>
<cac:OrderReference py:if="this.invoice.reference">
<cbc:ID>${this.invoice.reference}</cbc:ID>
<cbc:SalesOrderID py:if="this.sales_order_nums()">${this.sales_order_nums()}</cbc:SalesOrderID>
</cac:OrderReference>
<cac:ContractDocumentReference py:if="False">
<cbc:ID>vertrags-nr</cbc:ID>
</cac:ContractDocumentReference>
<cac:ProjectReference py:if="False">
<cbc:ID>proj-referenz</cbc:ID>
</cac:ProjectReference>
<cac:AccountingSupplierParty>
<cac:Party>
<cac:PostalAddress>
${PostalAddress(this.seller_trade_address)}
</cac:PostalAddress>
<cac:PartyTaxScheme>
${PartyTaxScheme(this.seller_trade_tax_identifier)}
</cac:PartyTaxScheme>
<cac:PartyLegalEntity>
${PartyLegalEntity(this.seller_trade_party)}
</cac:PartyLegalEntity>
<cac:Contact>
${Contact(this.seller_trade_party)}
</cac:Contact>
</cac:Party>
</cac:AccountingSupplierParty>
<cac:AccountingCustomerParty>
<cac:Party>
<cac:PartyIdentification>
<cbc:ID>${this.buyer_trade_party.code}</cbc:ID>
</cac:PartyIdentification>
<cac:PostalAddress>
${PostalAddress(this.buyer_trade_address)}
</cac:PostalAddress>
<cac:PartyTaxScheme>
${PartyTaxScheme(this.buyer_trade_tax_identifier)}
</cac:PartyTaxScheme>
<cac:PartyLegalEntity>
${PartyLegalEntity(this.buyer_trade_party, False)}
</cac:PartyLegalEntity>
<cac:Contact>
${Contact(this.buyer_trade_party)}
</cac:Contact>
</cac:Party>
</cac:AccountingCustomerParty>
<cac:PaymentMeans>
<cbc:PaymentMeansCode>30</cbc:PaymentMeansCode>
<cbc:PaymentID>${this.payment_reference}</cbc:PaymentID>
<cac:PayeeFinancialAccount>
${PayeeFinancialAccount(this.seller_trade_party)}
</cac:PayeeFinancialAccount>
</cac:PaymentMeans>
<cac:PaymentTerms py:if="this.invoice.payment_term is not None">
<cbc:Note>${this.invoice.payment_term.rec_name}</cbc:Note>
</cac:PaymentTerms>
<cac:AllowanceCharge py:if="False">
<cbc:ChargeIndicator>false</cbc:ChargeIndicator>
<cbc:AllowanceChargeReason>Neukundenrabatt</cbc:AllowanceChargeReason>
<cbc:Amount currencyID="EUR">5.00</cbc:Amount>
<cac:TaxCategory>
<cbc:ID>S</cbc:ID>
<cbc:Percent>19.00</cbc:Percent>
<cac:TaxScheme>
<cbc:ID>VAT</cbc:ID>
</cac:TaxScheme>
</cac:TaxCategory>
</cac:AllowanceCharge>
<cac:TaxTotal>
<cbc:TaxAmount py:attrs="{'currencyID': this.invoice.currency.code}">${this.negate_amount(this.invoice.tax_amount)}</cbc:TaxAmount>
<py:for each="taxline in this.invoice.taxes">
${TaxSubTotal(taxline)}
</py:for>
</cac:TaxTotal>
<cac:LegalMonetaryTotal>
<cbc:LineExtensionAmount py:attrs="{'currencyID': this.invoice.currency.code}">${this.negate_amount(this.invoice.untaxed_amount)}</cbc:LineExtensionAmount>
<cbc:TaxExclusiveAmount py:attrs="{'currencyID': this.invoice.currency.code}">${this.negate_amount(this.invoice.untaxed_amount)}</cbc:TaxExclusiveAmount>
<cbc:TaxInclusiveAmount py:attrs="{'currencyID': this.invoice.currency.code}">${this.negate_amount(this.invoice.total_amount)}</cbc:TaxInclusiveAmount>
<cbc:AllowanceTotalAmount py:if="False" py:attrs="{'currencyID': this.invoice.currency.code}">0.00</cbc:AllowanceTotalAmount>
<cbc:ChargeTotalAmount py:if="False" py:attrs="{'currencyID': this.invoice.currency.code}">0.00</cbc:ChargeTotalAmount>
<cbc:PrepaidAmount py:attrs="{'currencyID': this.invoice.currency.code}">${this.negate_amount(this.prepaid_amount(this.invoice))}</cbc:PrepaidAmount>
<cbc:PayableAmount py:attrs="{'currencyID': this.invoice.currency.code}">${this.negate_amount(this.invoice.amount_to_pay)}</cbc:PayableAmount>
</cac:LegalMonetaryTotal>
<py:for each="line in this.lines">
${CreditNoteLine(line)}
</py:for>
</ubl:CreditNote>

View file

@ -27,8 +27,12 @@
<py:def function="Contact(value)">
<cbc:Name>${value.name}</cbc:Name>
<py:if test="value.phone">
<cbc:Telephone>${value.phone}</cbc:Telephone>
</py:if>
<py:if test="value.email">
<cbc:ElectronicMail>${value.email}</cbc:ElectronicMail>
</py:if>
</py:def>
<py:def function="PartyLegalEntity(value, company_id=True)">

View file

@ -15,8 +15,8 @@ class EdocTestCase(ModuleTestCase):
module = 'edocument_xrechnung'
@with_transaction()
def test_xrechn_export_xml(self):
""" run export
def test_xrechn_export_xml_invoice(self):
""" run export - invoice
"""
pool = Pool()
Template = pool.get('edocument.xrechnung.invoice')
@ -49,7 +49,56 @@ class EdocTestCase(ModuleTestCase):
template = Template(invoice)
invoice_string = template.render('XRechnung-2.2')
with open('xrechnung-test.xml', 'wt') as fhdl:
with open('xrechnung-test-invoice.xml', 'wt') as fhdl:
fhdl.write(invoice_string.decode('utf8'))
@with_transaction()
def test_xrechn_export_xml_creditnote(self):
""" run export - creditnote
"""
pool = Pool()
Template = pool.get('edocument.xrechnung.invoice')
Identifier = pool.get('party.identifier')
Party = pool.get('party.party')
Bank = pool.get('bank')
BankAccount = pool.get('bank.account')
BankNumber = pool.get('bank.account.number')
invoice = get_invoice()
# credit note
invoice.lines[0].quantity = -1
invoice.lines[0].amount = Decimal('-100.0')
invoice.taxes[0].base = Decimal('-100.0')
invoice.taxes[0].amount = Decimal('-10.0')
invoice.untaxed_amount = Decimal('-100.0')
invoice.tax_amount = Decimal('-10.0')
invoice.total_amount = Decimal('-110.0')
invoice.lines_to_pay[0].debit = Decimal('-110.0')
invoice.party.get_xrechnung_route_id = Mock(
return_value='xrechn-route-id-123')
invoice.company.party.bank_accounts = [
Mock(
spec=BankAccount,
currency=invoice.currency,
bank=Mock(spec=Bank, party=Mock(spec=Party, name='Bank')),
owners=[invoice.company.party],
numbers=[Mock(spec=BankNumber, type='other', number='123456')],
)]
invoice.description = 'description of invoice'
invoice.comment = 'note line 1\nnote line 2'
invoice.taxes[0].tax.rate = Decimal('0.1')
invoice.identifiers = [
Mock(
spec=Identifier,
type='edoc_route_id',
code='xrechn-route-id-123')
]
template = Template(invoice)
invoice_string = template.render('XRechnung-2.2')
with open('xrechnung-test-creditnote.xml', 'wt') as fhdl:
fhdl.write(invoice_string.decode('utf8'))
# end EdocTestCase