Normalize formatted tax identifiers for ID type
Publicar paquete PyPI / publish (push) Successful in 2m54s

This commit is contained in:
José Arturo García
2026-08-23 12:23:37 -04:00
parent 76c18fbc39
commit 54649bb8e9
3 changed files with 54 additions and 11 deletions
+22 -10
View File
@@ -1109,16 +1109,28 @@ class Invoice(metaclass=PoolMeta):
return {'amount_invoiced_goods': amount_invoiced_goods} return {'amount_invoiced_goods': amount_invoiced_goods}
def get_id_type(self, name): def get_id_type(self, name):
if self.party: if not self.party or not self.party_tax_identifier:
if not self.party_tax_identifier: return None
return None
code_length = len(self.party_tax_identifier.code) identifier = self.party_tax_identifier
if code_length == 9: identifier_type = getattr(identifier, 'type', None)
return 1 if identifier_type in {'do_rnc', 'do_vat'}:
elif code_length == 11: return 1
return 2 if identifier_type == 'do_cedula':
else: return 2
return 3
# Tryton 8.0 stores a formatted display code (for example
# ``1-01-01063-2`` or ``001-1391820-5``). Use code_compact when
# available and still normalize it for migrated records whose compact
# value may retain the old formatting.
code = (getattr(identifier, 'code_compact', None)
or getattr(identifier, 'code', None) or '')
digits = re.sub(r'\D', '', code)
if len(digits) == 9:
return 1
if len(digits) == 11:
return 2
return 3
@fields.depends('taxes', 'lines') @fields.depends('taxes', 'lines')
def on_change_with_itbis_invoiced(self, name=None): def on_change_with_itbis_invoiced(self, name=None):
+31
View File
@@ -370,6 +370,37 @@ del ModuleTestCase
class NcfManagerUnitTestCase(unittest.TestCase): class NcfManagerUnitTestCase(unittest.TestCase):
@staticmethod
def invoice_with_identifier(code, type=None, code_compact=None):
identifier = SimpleNamespace(code=code, type=type)
if code_compact is not None:
identifier.code_compact = code_compact
return SimpleNamespace(
party=object(), party_tax_identifier=identifier)
def test_id_type_accepts_formatted_rnc(self):
invoice = self.invoice_with_identifier('1-01-01063-2')
self.assertEqual(Invoice.get_id_type(invoice, 'id_type'), 1)
def test_id_type_accepts_formatted_cedula(self):
invoice = self.invoice_with_identifier('001-1391820-5')
self.assertEqual(Invoice.get_id_type(invoice, 'id_type'), 2)
def test_id_type_uses_dominican_identifier_type(self):
rnc = self.invoice_with_identifier('formatted', type='do_vat')
cedula = self.invoice_with_identifier('formatted', type='do_cedula')
self.assertEqual(Invoice.get_id_type(rnc, 'id_type'), 1)
self.assertEqual(Invoice.get_id_type(cedula, 'id_type'), 2)
def test_id_type_normalizes_migrated_compact_code(self):
invoice = self.invoice_with_identifier(
'display value', code_compact='001-1391820-5')
self.assertEqual(Invoice.get_id_type(invoice, 'id_type'), 2)
def test_id_type_returns_none_without_identifier(self):
invoice = SimpleNamespace(party=object(), party_tax_identifier=None)
self.assertIsNone(Invoice.get_id_type(invoice, 'id_type'))
def test_invoice_report_uses_current_tax_field_names(self): def test_invoice_report_uses_current_tax_field_names(self):
path = Path(__file__).parents[1] / 'invoice.fodt' path = Path(__file__).parents[1] / 'invoice.fodt'
template = path.read_text(encoding='utf-8') template = path.read_text(encoding='utf-8')
+1 -1
View File
@@ -1,5 +1,5 @@
[tryton] [tryton]
version=8.0.0 version=8.0.1
depends: depends:
account_do account_do
account_invoice_do account_invoice_do