Debian Codegraph @

addons/l10n_in_edi/models/res_partner.py

raw
1 import re
2
3 from odoo import _, models
4
5
6 class ResPartner(models.Model):
7 _inherit = 'res.partner'
8
9 # E-Invoice Validation
10 def _l10n_in_edi_strict_error_validation(self):
11 """
12 This method is used to check the strict validation of the partner data
13 as per government API json schema (https://einv-apisandbox.nic.in/version1.03/generate-irn.html#requestSampleJSON)
14 In case of any error, it will return the error message
15 Note - We stimulate as error message from API, so that user can understand the error
16 Also restrict unwanted request to government servers and avoid getting black listed
17 """
18 message = []
19 if not re.match("^.{3,100}$", self.street or ""):
20 message.append(_("- Street required min 3 and max 100 characters"))
21 if not re.match("^.{3,100}$", self.city or ""):
22 message.append(_("- City required min 3 and max 100 characters"))
23 if self.country_id.code == "IN" and not re.match("^.{3,50}$", self.state_id.name or ""):
24 message.append(_("- State required min 3 and max 50 characters"))
25 if self.country_id.code == "IN" and not re.match("^([1-9][0-9]{5})$", self.zip or ""):
26 message.append(_("- ZIP code required 6 digits ranging from 100000 to 999999"))
27 if (
28 self.country_id.code == "IN"
29 and not re.match(r"^(?!0+$)([0-9]{2})$", self.state_id.l10n_in_tin or "")
30 ):
31 message.append(_("- State TIN Number must be exactly 2 digits."))
32 if message:
33 message.insert(0, self.display_name)
34 return message
35
36 def _l10n_in_check_einvoice_validation(self):
37 checks = {
38 'partner_address_missing': {
39 'fields': ('street', 'zip', 'city', 'state_id', 'country_id',),
40 'message': _(
41 "Partners should have a complete address, verify their Street, City, State, Country and Zip code."
42 ),
43 },
44 }
45 return {
46 f"l10n_in_edi_{key}": {
47 'message': check['message'],
48 'action_text': (
49 _("View Partners") if len(invalid_records) > 1
50 else _("View %s", invalid_records.name)
51 ),
52 'action': invalid_records._get_records_action(name=_("Check Partner Data")),
53 }
54 for key, check in checks.items()
55 if (invalid_records := self.filtered(lambda record: any(not record[field] for field in check['fields'])))
56 }