| 1 |
import pytz |
| 2 |
|
| 3 |
from odoo import api, fields, models, _ |
| 4 |
from odoo.exceptions import ValidationError |
| 5 |
from odoo.exceptions import AccessError, ValidationError |
| 6 |
from stdnum.in_ import pan, gstin |
| 7 |
|
| 8 |
|
| 9 |
class ResCompany(models.Model): |
| 10 |
_inherit = 'res.company' |
| 11 |
|
| 12 |
# E-Invoice fields |
| 13 |
l10n_in_edi_feature = fields.Boolean(string="Indian E-Invoicing") |
| 14 |
l10n_in_edi_username = fields.Char( |
| 15 |
string="E-invoice (IN) Username", |
| 16 |
groups="base.group_system" |
| 17 |
) |
| 18 |
l10n_in_edi_password = fields.Char( |
| 19 |
string="E-invoice (IN) Password", |
| 20 |
groups="base.group_system" |
| 21 |
) |
| 22 |
l10n_in_edi_token = fields.Char( |
| 23 |
string="E-invoice (IN) Token", |
| 24 |
groups="base.group_system" |
| 25 |
) |
| 26 |
l10n_in_edi_token_validity = fields.Datetime( |
| 27 |
string="E-invoice (IN) Valid Until", |
| 28 |
groups="base.group_system" |
| 29 |
) |
| 30 |
|
| 31 |
|
| 32 |
# E-Invoice Business Methods |
| 33 |
|
| 34 |
def _l10n_in_edi_token_is_valid(self): |
| 35 |
self.ensure_one() |
| 36 |
return self.l10n_in_edi_token and self.l10n_in_edi_token_validity > fields.Datetime.now() |
| 37 |
|
| 38 |
def _l10n_in_edi_get_token(self): |
| 39 |
self_sudo = self.sudo() |
| 40 |
if self_sudo.l10n_in_edi_username and self_sudo._l10n_in_edi_token_is_valid(): |
| 41 |
return self_sudo.l10n_in_edi_token |
| 42 |
elif self_sudo.l10n_in_edi_username and self_sudo.l10n_in_edi_password: |
| 43 |
self_sudo._l10n_in_edi_authenticate() |
| 44 |
return self_sudo.l10n_in_edi_token |
| 45 |
return False |
| 46 |
|
| 47 |
def _l10n_in_edi_authenticate(self): |
| 48 |
self_sudo = self.sudo() |
| 49 |
params = { |
| 50 |
"username": self_sudo.l10n_in_edi_username, |
| 51 |
"password": self_sudo.l10n_in_edi_password, |
| 52 |
"gstin": self_sudo.vat, |
| 53 |
} |
| 54 |
try: |
| 55 |
response = self.env['iap.account']._l10n_in_connect_to_server( |
| 56 |
self_sudo.l10n_in_edi_production_env, |
| 57 |
params, |
| 58 |
"/iap/l10n_in_edi/1/authenticate", |
| 59 |
"l10n_in_edi.endpoint" |
| 60 |
) |
| 61 |
except AccessError as e: |
| 62 |
return { |
| 63 |
"error": [{ |
| 64 |
"code": "404", |
| 65 |
"message": _( |
| 66 |
"Unable to connect to the online E-invoice service. " |
| 67 |
"The web service may be temporary down. Please try again in a moment." |
| 68 |
) |
| 69 |
}] |
| 70 |
} |
| 71 |
# validity data-time in Indian standard time(UTC+05:30) convert IST to UTC |
| 72 |
if data := response.get('data'): |
| 73 |
tz = pytz.timezone("Asia/Kolkata") |
| 74 |
local_time = tz.localize(fields.Datetime.to_datetime(data["TokenExpiry"])) |
| 75 |
utc_time = local_time.astimezone(pytz.utc) |
| 76 |
self_sudo.write({ |
| 77 |
'l10n_in_edi_token_validity': fields.Datetime.to_string(utc_time), |
| 78 |
'l10n_in_edi_token': data['AuthToken'], |
| 79 |
}) |
| 80 |
return response |
| 81 |
|
| 82 |
def _l10n_in_check_einvoice_validation(self): |
| 83 |
checks = { |
| 84 |
'company_address_missing': { |
| 85 |
'fields': ('street', 'zip', 'city', 'state_id', 'country_id',), |
| 86 |
'message': _("Companies should have a complete address, verify their Street, City, State, Country and Zip code."), |
| 87 |
}, |
| 88 |
} |
| 89 |
return { |
| 90 |
f"l10n_in_edi_{key}": { |
| 91 |
'message': check['message'], |
| 92 |
'action_text': ( |
| 93 |
_("View Companies") if len(invalid_records) > 1 |
| 94 |
else _("View %s", invalid_records.name) |
| 95 |
), |
| 96 |
'action': invalid_records._get_records_action(name=_("Check Company Data")), |
| 97 |
} |
| 98 |
for key, check in checks.items() |
| 99 |
if (invalid_records := self.filtered(lambda record: any(not record[field] for field in check['fields']))) |
| 100 |
} |