From 8520d9ea2976dad7a658094aa837ca82ff312472 Mon Sep 17 00:00:00 2001 From: "bit4bit@riseup.net" Date: Fri, 6 Nov 2020 00:50:12 +0000 Subject: [PATCH] facho/fe/form(Invoice): ident segun FAJ50,CAJ50,DAJ50. FossilOrigin-Name: 66a185301ea1f32d9d837bf0811d35d5e8e15539486620b7b88d58b3824190a4 --- facho/fe/form/__init__.py | 41 +++++++++++++++++++++++++++++++++++---- tests/test_form.py | 23 ++++++++++++++++++++++ 2 files changed, 60 insertions(+), 4 deletions(-) diff --git a/facho/fe/form/__init__.py b/facho/fe/form/__init__.py index 2adf261..fd1f21c 100644 --- a/facho/fe/form/__init__.py +++ b/facho/fe/form/__init__.py @@ -520,20 +520,36 @@ class Invoice: self.invoice_issue = dtime + def _set_ident_prefix_automatic(self): + if not self.invoice_ident_prefix: + prefix = '' + for idx, val in enumerate(self.invoice_ident): + if val.isalpha(): + prefix += val + else: + break + + if len(prefix) <= 4: + self.invoice_ident_prefix = prefix + else: + raise ValueError('ident prefix failed to get, expected 0 to 4 chars') + def set_ident(self, ident: str): """ identificador de factura; prefijo + consecutivo """ self.invoice_ident = ident - if not self.invoice_ident_prefix: - self.invoice_ident_prefix = ident[0:4] + self._set_ident_prefix_automatic() + + def _check_ident_prefix(self, prefix): + if len(prefix) != 4: + raise ValueError('prefix must be 4 length') def set_ident_prefix(self, prefix: str): """ prefijo de facturacion: ejemplo SETP """ - if len(prefix) != 4: - raise ValueError('prefix must be 4 length') + self._check_ident_prefix(prefix) self.invoice_ident_prefix = prefix def set_supplier(self, party: Party): @@ -624,6 +640,14 @@ class CreditNote(Invoice): def _get_codelist_tipo_operacion(self): return codelist.TipoOperacionNC + def _check_ident_prefix(self, prefix): + if len(prefix) != 6: + raise ValueError('prefix must be 6 length') + + def _set_ident_prefix_automatic(self): + if not self.invoice_ident_prefix: + self.invoice_ident_prefix = self.invoice_ident[0:6] + class DebitNote(Invoice): def __init__(self, invoice_document_reference: BillingReference): @@ -635,3 +659,12 @@ class DebitNote(Invoice): def _get_codelist_tipo_operacion(self): return codelist.TipoOperacionND + + def _check_ident_prefix(self, prefix): + if len(prefix) != 6: + raise ValueError('prefix must be 6 length') + + def _set_ident_prefix_automatic(self): + if not self.invoice_ident_prefix: + self.invoice_ident_prefix = self.invoice_ident[0:6] + diff --git a/tests/test_form.py b/tests/test_form.py index d3252f0..1c39483 100644 --- a/tests/test_form.py +++ b/tests/test_form.py @@ -174,13 +174,36 @@ def test_invoice_legalmonetary_with_taxes(): assert inv.invoice_legal_monetary_total.charge_total_amount == form.Amount(0.0) assert inv.invoice_legal_monetary_total.payable_amount == form.Amount(100.0) + +def test_invoice_ident_prefix_automatic_invalid(): + inv = form.NationalSalesInvoice() + with pytest.raises(ValueError): + inv.set_ident('SETPQJQJ1234567') + def test_invoice_ident_prefix_automatic(): inv = form.NationalSalesInvoice() inv.set_ident('SETP1234567') assert inv.invoice_ident_prefix == 'SETP' + inv = form.NationalSalesInvoice() + inv.set_ident('SET1234567') + assert inv.invoice_ident_prefix == 'SET' + inv = form.NationalSalesInvoice() + inv.set_ident('SE1234567') + assert inv.invoice_ident_prefix == 'SE' + inv = form.NationalSalesInvoice() + inv.set_ident('S1234567') + assert inv.invoice_ident_prefix == 'S' + inv = form.NationalSalesInvoice() + inv.set_ident('1234567') + assert inv.invoice_ident_prefix == '' def test_invoice_ident_prefix_manual(): inv = form.NationalSalesInvoice() inv.set_ident('SETP1234567') inv.set_ident_prefix('SETA') assert inv.invoice_ident_prefix == 'SETA' + +def test_invoice_ident_prefix_automatic_debit(): + inv = form.DebitNote(form.BillingReference('','','')) + inv.set_ident('ABCDEF1234567') + assert inv.invoice_ident_prefix == 'ABCDEF'