From d111b4b3438bf1ed2f7dc04eafa50cbbf7dabf46 Mon Sep 17 00:00:00 2001 From: Stanislas Gueniffey Date: Fri, 6 May 2022 09:56:59 +0000 Subject: [PATCH] [IMP] l10n_ec: user-friendly document number Do not require user to type all digits of the document number Instead, fill the dash-separated numbers with zeroes, as needed closes odoo/odoo#90865 X-original-commit: 9ed26751bfbe84444f291835de5f47bbac3f7dde Signed-off-by: Josse Colpaert Signed-off-by: Stanislas Gueniffey (stgu) --- .../models/l10n_latam_document_type.py | 21 +++++++++++-------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/addons/l10n_ec/models/l10n_latam_document_type.py b/addons/l10n_ec/models/l10n_latam_document_type.py index 1ef67d8acf0..eb6cfcef241 100644 --- a/addons/l10n_ec/models/l10n_latam_document_type.py +++ b/addons/l10n_ec/models/l10n_latam_document_type.py @@ -24,13 +24,16 @@ class L10nLatamDocumentType(models.Model): return super()._format_document_number(document_number) if not document_number: return False - if ( - not re.match(r"\d{3}-\d{3}-\d{9}$", document_number) - and self.l10n_ec_check_format - and not self.env.context.get("l10n_ec_foreign", False) - ): - raise UserError( - _(u"Ecuadorian Document %s must be like 001-001-123456789") - % (self.display_name) - ) + if self.l10n_ec_check_format: + document_number = re.sub(r'\s+', "", document_number) # remove any whitespace + num_match = re.match(r'(\d{1,3})-(\d{1,3})-(\d{1,9})', document_number) + if num_match: + # Fill each number group with zeroes (3, 3 and 9 respectively) + document_number = "-".join([n.zfill(3 if i < 2 else 9) for i, n in enumerate(num_match.groups())]) + else: + raise UserError( + _(u"Ecuadorian Document %s must be like 001-001-123456789") + % (self.display_name) + ) + return document_number