Skip to content

Commit

Permalink
fixup! Add ebill_postfinance
Browse files Browse the repository at this point in the history
  • Loading branch information
TDu committed Jul 19, 2023
1 parent 595da67 commit 3a18b6f
Show file tree
Hide file tree
Showing 7 changed files with 290 additions and 21 deletions.
2 changes: 1 addition & 1 deletion ebill_postfinance/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
{
"name": "eBill Postfinance",
"summary": """Postfinance eBill integration""",
"version": "14.0.1.0.0",
"version": "14.0.1.1.0",
"license": "AGPL-3",
"author": "Camptocamp,Odoo Community Association (OCA)",
"maintainers": ["TDu"],
Expand Down
13 changes: 7 additions & 6 deletions ebill_postfinance/messages/invoice-yellowbill.jinja
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
</DeliveryInfo>
<Bill>
<Header>
<DocumentType>BILL</DocumentType>
<DocumentType>{{ 'BILL' if invoice.move_type == 'out_invoice' else 'CREDITADVICE' }}</DocumentType>
<DocumentID>{{ invoice.name }}</DocumentID>
<DocumentDate>{{ format_date(invoice.date_invoice) }}</DocumentDate>
<SenderParty>
Expand Down Expand Up @@ -129,9 +129,9 @@
<Language>{{ (customer.lang or "en")|truncate(2, True, "") }}</Language>
<PaymentInformation>
<PaymentDueDate>{{ date_due }}</PaymentDueDate>
{# the payment_type on message should be changed for yb #}
<PaymentType>{{ 'ESR' if payment_type == 'isr' else "IBAN"|upper }}</PaymentType>
<PaymentType>{{ payment_type|upper }}</PaymentType>
<fixAmount>Yes</fixAmount>
{% if payment_type == 'iban' %}
<IBAN>
{%- if bank.bank_id.clearing %}
<BIC>{{ bank.bank_id.clearing }}</BIC>
Expand All @@ -142,6 +142,7 @@
<CreditorReference>{{ invoice.payment_reference }}</CreditorReference>
{%- endif %}
</IBAN>
{%- endif %}
</PaymentInformation>
</Header>
<LineItems>
Expand Down Expand Up @@ -213,10 +214,10 @@
{# <Days></Days> #}
{# <Rate></Rate> #}
{# </Discount> #}
<TotalAmountExclusiveTax>{{ invoice.amount_untaxed }}</TotalAmountExclusiveTax>
<TotalAmountInclusiveTax>{{ invoice.amount_total }}</TotalAmountInclusiveTax>
<TotalAmountExclusiveTax>{{ invoice.amount_untaxed * amount_sign }}</TotalAmountExclusiveTax>
<TotalAmountInclusiveTax>{{ invoice.amount_total * amount_sign }}</TotalAmountInclusiveTax>
{# <TotalAmountPaid></TotalAmountPaid> #}
<TotalAmountDue>{{ invoice.amount_total}}</TotalAmountDue>
<TotalAmountDue>{{ invoice.amount_total * amount_sign }}</TotalAmountDue>
</Summary>
</Bill>
<Appendix>
Expand Down
18 changes: 18 additions & 0 deletions ebill_postfinance/migrations/14.0.1.1.0/pre-migrate.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Copyright 2023 Camptocamp SA (http://www.camptocamp.com)
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).


def migrate(cr, version):
if not version:
return

cr.execute(
"""
UPDATE ebill_postfinance_invoice_message
SET payment_type = 'iban'
WHERE payment_type = 'qr';
UPDATE ebill_postfinance_invoice_message
SET payment_type = 'esr'
WHERE payment_type = 'isr';
"""
)
23 changes: 14 additions & 9 deletions ebill_postfinance/models/account_move.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,26 +51,31 @@ def create_postfinance_ebill(self):
return

Check warning on line 51 in ebill_postfinance/models/account_move.py

View check run for this annotation

Codecov / codecov/patch

ebill_postfinance/models/account_move.py#L51

Added line #L51 was not covered by tests
# Generate PDf to be send
pdf_data = []
# When test are run, pdf are not generated, so use an empty pdf
pdf = b""
report_names = ["account.report_invoice"]
if contract.payment_type == "qr":
report_names.append("l10n_ch.qr_report_main")
elif contract.payment_type == "isr":
report_names.append("l10n_ch.isr_report_main")
payment_type = ""
if self.move_type == "out_invoice":
payment_type = "iban"
if contract.payment_type == "qr":
report_names.append("l10n_ch.qr_report_main")
elif self.move_type == "out_refund":
payment_type = "credit"

Check warning on line 63 in ebill_postfinance/models/account_move.py

View check run for this annotation

Codecov / codecov/patch

ebill_postfinance/models/account_move.py#L62-L63

Added lines #L62 - L63 were not covered by tests
for report_name in report_names:
r = self.env["ir.actions.report"]._get_report_from_name(report_name)
pdf_content, _ = r._render([self.id])
pdf_data.append(pdf_content)
if not odoo.tools.config["test_enable"]:
pdf = merge_pdf(pdf_data)
else:
# When test are run, pdf are not generated, so use an empty pdf
pdf = b""
if len(pdf_data) > 1:
pdf = merge_pdf(pdf_data)
elif len(pdf_data) == 1:
pdf = pdf_data[0]

Check warning on line 72 in ebill_postfinance/models/account_move.py

View check run for this annotation

Codecov / codecov/patch

ebill_postfinance/models/account_move.py#L69-L72

Added lines #L69 - L72 were not covered by tests
message = self.env["ebill.postfinance.invoice.message"].create(
{
"service_id": contract.postfinance_service_id.id,
"invoice_id": self.id,
"ebill_account_number": contract.postfinance_billerid,
"payment_type": contract.payment_type,
"payment_type": payment_type,
"ebill_payment_contract_id": contract.id,
}
)
Expand Down
16 changes: 11 additions & 5 deletions ebill_postfinance/models/ebill_postfinance_invoice_message.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,15 @@ class EbillPostfinanceInvoiceMessage(models.Model):
"Payload Size (MB)", digits=(6, 3), compute="_compute_payload_size"
)
response = fields.Text("Response")
# shipment_id = fields.Char(size=24, help="Shipment Id on Paynet service")
payment_type = fields.Selection(
selection=[("qr", "QR"), ("isr", "ISR"), ("esp", "ESP"), ("npy", "NPY")],
default="qr",
selection=[
("iban", "IBAN"),
("credit", "CREDIT"),
("other", "OTHER"),
("dd", "DD"),
("esr", "ESR"),
],
default="iban",
readonly=True,
)

Expand Down Expand Up @@ -182,7 +187,7 @@ def format_date_yb(date_string=None):

def _get_payload_params(self):
bank_account = ""
if self.payment_type == "qr":
if self.payment_type == "iban":
bank_account = sanitize_account_number(

Check warning on line 191 in ebill_postfinance/models/ebill_postfinance_invoice_message.py

View check run for this annotation

Codecov / codecov/patch

ebill_postfinance/models/ebill_postfinance_invoice_message.py#L189-L191

Added lines #L189 - L191 were not covered by tests
self.invoice_id.partner_bank_id.l10n_ch_qr_iban
or self.invoice_id.partner_bank_id.acc_number
Expand Down Expand Up @@ -247,7 +252,7 @@ def _get_payload_params(self):

def _get_payload_params_yb(self):
bank_account = ""
if self.payment_type == "qr":
if self.payment_type == "iban":
bank_account = sanitize_account_number(
self.invoice_id.partner_bank_id.l10n_ch_qr_iban
or self.invoice_id.partner_bank_id.acc_number
Expand Down Expand Up @@ -282,6 +287,7 @@ def _get_payload_params_yb(self):
"bank_account": bank_account,
"transaction_id": self.transaction_id,
"payment_type": self.payment_type,
"amount_sign": -1 if self.payment_type == "credit" else 1,
"document_type": DOCUMENT_TYPE[self.invoice_id.move_type],
"format_date": self.format_date_yb,
"ebill_account_number": self.ebill_account_number,
Expand Down
176 changes: 176 additions & 0 deletions ebill_postfinance/tests/examples/credit_note_yb.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,176 @@
<?xml version="1.0" encoding="utf-8"?>
<Envelope xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" type="string">
<Header>
<From>Camptocamp SA</From>
<To>IPECeBILLServer</To>
<UseCase>CreateybInvoice</UseCase>
<SessionID>1</SessionID>
<Version>2.0</Version>
<Status>0</Status>
<SoftwareName>Odoo</SoftwareName>
<SoftwareVersion>14.0</SoftwareVersion>
</Header>
<Body>
<DeliveryInfo>
<BillerID>41101000001021209</BillerID>
<eBillAccountID>41010198248040391</eBillAccountID>
<DeliveryDate>2019-06-21</DeliveryDate>
<TransactionID>$TRANSACTION_ID</TransactionID>
<BillDetailsType>PDFAppendix</BillDetailsType>
<URLBillDetails/>
</DeliveryInfo>
<Bill>
<Header>
<DocumentType>CREDITADVICE</DocumentType>
<DocumentID>INV_TEST_01</DocumentID>
<DocumentDate>2019-06-21</DocumentDate>
<SenderParty>
<TaxLiability>VAT</TaxLiability>
<PartyType>
<Address>
<CompanyName>Camptocamp SA</CompanyName>
<Address1>StreetOne</Address1>
<ZIP>1015</ZIP>
<City>Lausanne</City>
<Country>CH</Country>
<Email>[email protected]</Email>
</Address>
<TaxID>CHE012345678</TaxID>
</PartyType>
</SenderParty>
<ReceiverParty>
<PartyType>
<CustomerID>$CUSTOMER_ID</CustomerID>
<Address>
<CompanyName>Test RAD Customer XML</CompanyName>
<Address1>Teststrasse 100</Address1>
<Address2>This is a very long street name that should be sna</Address2>
<ZIP>1700</ZIP>
<City>Fribourg</City>
<Country>CH</Country>
</Address>
<!-- <TaxID>CHE333222111</TaxID> -->
</PartyType>
</ReceiverParty>
<DeliveryPlace>
<Address>
<CompanyName>Test RAD Customer XML</CompanyName>
<ZIP>1700</ZIP>
<City>Fribourg</City>
<Country>CH</Country>
<!-- <Contact1>0000000001</Contact1> -->
</Address>
</DeliveryPlace>
<AchievementDate>
<StartDateAchievement>2019-06-21</StartDateAchievement>
<EndDateAchievement>2019-06-21</EndDateAchievement>
</AchievementDate>
<Currency>CHF</Currency>
<AccountAssignment>
<OrderReference>
<ReferencePosition>1</ReferencePosition>
<ReferenceType>OrderReference</ReferenceType>
<ReferenceValue>CustomerRef</ReferenceValue>
</OrderReference>
<OrderDate>2019-06-01</OrderDate>
</AccountAssignment>
<Language>en</Language>
<PaymentInformation>
<PaymentDueDate>2019-07-01</PaymentDueDate>
<PaymentType>CREDIT</PaymentType>
<fixAmount>Yes</fixAmount>
</PaymentInformation>
<!-- <FreeText>Dies ist eine Musterrechnung mit QR-IBAN Angaben</FreeText> -->
</Header>
<LineItems>
<LineItem>
<LineItemType>NORMAL</LineItemType>
<LineItemID>1</LineItemID>
<ProductDescription>Product Q &amp; A</ProductDescription>
<ProductID>370003021</ProductID>
<Quantity>4.0</Quantity>
<QuantityDescription>Units</QuantityDescription>
<PriceUnit>1</PriceUnit>
<Tax>
<TaxDetail>
<Rate>7.7</Rate>
<Amount>37.88</Amount>
<BaseAmountExclusiveTax>492.0</BaseAmountExclusiveTax>
<BaseAmountInclusiveTax>529.88</BaseAmountInclusiveTax>
</TaxDetail>
<TotalTax>37.88</TotalTax>
</Tax>
<AmountInclusiveTax>529.88</AmountInclusiveTax>
<AmountExclusiveTax>492.0</AmountExclusiveTax>
<FixedReference>
<ReferencePosition>2</ReferencePosition>
<ReferenceType>OrderNumberBySupplier</ReferenceType>
<ReferenceValue>Order123</ReferenceValue>
</FixedReference>
<FixedReference>
<ReferencePosition>3</ReferencePosition>
<ReferenceType>OrderNumberByBuyer</ReferenceType>
<ReferenceValue>CustomerRef</ReferenceValue>
</FixedReference>
</LineItem>
<LineItem>
<LineItemType>NORMAL</LineItemType>
<LineItemID>2</LineItemID>
<ProductDescription>Product With a Very Long Name That Need To Be Truncated</ProductDescription>
<ProductID>370003022</ProductID>
<Quantity>1.0</Quantity>
<QuantityDescription>Units</QuantityDescription>
<PriceUnit>1</PriceUnit>
<Tax>
<TaxDetail>
<Rate>7.7</Rate>
<Amount>0.0</Amount>
<BaseAmountExclusiveTax>0.0</BaseAmountExclusiveTax>
<BaseAmountInclusiveTax>0.0</BaseAmountInclusiveTax>
</TaxDetail>
<TotalTax>0.0</TotalTax>
</Tax>
<AmountInclusiveTax>0.0</AmountInclusiveTax>
<AmountExclusiveTax>0.0</AmountExclusiveTax>
<FixedReference>
<ReferencePosition>4</ReferencePosition>
<ReferenceType>OrderNumberBySupplier</ReferenceType>
<ReferenceValue>Order123</ReferenceValue>
</FixedReference>
<FixedReference>
<ReferencePosition>5</ReferencePosition>
<ReferenceType>OrderNumberByBuyer</ReferenceType>
<ReferenceValue>CustomerRef</ReferenceValue>
</FixedReference>
</LineItem>
<LineItem>
<LineItemType>NORMAL</LineItemType>
<LineItemID>3</LineItemID>
<ProductDescription>Phone support</ProductDescription>
<Quantity>4.0</Quantity>
<QuantityDescription>PCE</QuantityDescription>
<PriceUnit>1</PriceUnit>
<AmountInclusiveTax>0.0</AmountInclusiveTax>
<AmountExclusiveTax>0.0</AmountExclusiveTax>
</LineItem>
</LineItems>
<Summary>
<Tax>
<TaxDetail>
<Rate>7.7</Rate>
<Amount>-37.88</Amount>
<BaseAmountExclusiveTax>-492.0</BaseAmountExclusiveTax>
<BaseAmountInclusiveTax>-529.88</BaseAmountInclusiveTax>
</TaxDetail>
<TotalTax>37.88</TotalTax>
</Tax>
<TotalAmountExclusiveTax>-492.0</TotalAmountExclusiveTax>
<TotalAmountInclusiveTax>-529.88</TotalAmountInclusiveTax>
<!-- <TotalAmountPaid>200.00</TotalAmountPaid> -->
<TotalAmountDue>-529.88</TotalAmountDue>
</Summary>
</Bill>
<Appendix>
</Appendix>
</Body>
</Envelope>
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
# Copyright 2022 Camptocamp SA
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html)

import os
from string import Template

from freezegun import freeze_time
from lxml import etree as ET

from odoo.modules.module import get_module_root
from odoo.tools import file_open

from .common import CommonCase


@freeze_time("2019-06-21 09:06:00")
class TestEbillPostfinanceMessageYBCreditNote(CommonCase):
@classmethod
def setUpClass(cls):
super().setUpClass()
cls.schema_file = (
get_module_root(os.path.dirname(__file__))
+ "/messages/ybInvoice_V2.0.4.xsd"
)

def test_invoice_credit_note(self):
"""Check XML payload genetated for a credit note."""
# If ebill_postfinance_stock is installed it will break the test
try:
self.invoice.invoice_line_ids.sale_line_ids.write({"move_ids": False})
except Exception:
pass
self.invoice.name = "INV_TEST_01"
self.invoice.invoice_date_due = "2019-07-01"
self.invoice.move_type = "out_refund"
message = self.invoice.create_postfinance_ebill()
message.set_transaction_id()
message.payload = message._generate_payload_yb()
# Validate the xml generated on top of the xsd schema
node = ET.fromstring(message.payload.encode("utf-8"))
self.assertXmlValidXSchema(node, xschema=None, filename=self.schema_file)
# Remove the PDF file data from the XML to ease diff check
lines = message.payload.splitlines()
for pos, line in enumerate(lines):
if line.find("MimeType") != -1:
lines.pop(pos)
break
payload = "\n".join(lines).encode("utf8")
# Prepare the XML file that is expected
expected_tmpl = Template(
file_open("ebill_postfinance/tests/examples/credit_note_yb.xml").read()
)
expected = expected_tmpl.substitute(
TRANSACTION_ID=message.transaction_id, CUSTOMER_ID=self.customer.id
).encode("utf8")
# Remove the comments in the expected xml
expected_nocomment = [
line
for line in expected.split(b"\n")
if not line.lstrip().startswith(b"<!--")
]
expected_nocomment = b"\n".join(expected_nocomment)
self.assertFalse(self.compare_xml_line_by_line(payload, expected_nocomment))

0 comments on commit 3a18b6f

Please sign in to comment.