| 1 |
# Part of Odoo. See LICENSE file for full copyright and licensing details. |
| 2 |
|
| 3 |
import logging |
| 4 |
import contextlib |
| 5 |
|
| 6 |
from datetime import timedelta |
| 7 |
from markupsafe import Markup |
| 8 |
|
| 9 |
from odoo import fields |
| 10 |
from odoo.exceptions import AccessError |
| 11 |
from odoo.addons.l10n_in_ewaybill.models.error_codes import ERROR_CODES |
| 12 |
from odoo.tools import _, LazyTranslate |
| 13 |
|
| 14 |
|
| 15 |
_logger = logging.getLogger(__name__) |
| 16 |
|
| 17 |
|
| 18 |
class EWayBillError(Exception): |
| 19 |
|
| 20 |
def __init__(self, response): |
| 21 |
self.error_json = self._set_missing_error_message(response) |
| 22 |
self.error_json.setdefault('odoo_warning', []) |
| 23 |
self.error_codes = self.get_error_codes() |
| 24 |
super().__init__(response) |
| 25 |
|
| 26 |
def _set_missing_error_message(self, response): |
| 27 |
for error in response.get('error', []): |
| 28 |
if error.get('code') and not error.get('message'): |
| 29 |
error['message'] = self._find_missing_error_message(error.get('code')) |
| 30 |
return response |
| 31 |
|
| 32 |
@staticmethod |
| 33 |
def _find_missing_error_message(code): |
| 34 |
return ( |
| 35 |
ERROR_CODES.get(code) |
| 36 |
or _("We don't know the error message for this error code. Please contact support.") |
| 37 |
) |
| 38 |
|
| 39 |
def get_all_error_message(self): |
| 40 |
return Markup("<br/>").join( |
| 41 |
["[%s] %s" % (e.get("code"), e.get("message")) for e in self.error_json.get('error')] |
| 42 |
) |
| 43 |
|
| 44 |
def get_error_codes(self): |
| 45 |
return [e.get("code") for e in self.error_json['error']] |
| 46 |
|
| 47 |
|
| 48 |
class EWayBillApi: |
| 49 |
|
| 50 |
def __init__(self, company): |
| 51 |
company.ensure_one() |
| 52 |
self.company = company |
| 53 |
self.env = self.company.env |
| 54 |
|
| 55 |
def _ewaybill_jsonrpc_to_server(self, url_path, params): |
| 56 |
params.update({ |
| 57 |
"username": self.company.sudo().l10n_in_ewaybill_username, |
| 58 |
"gstin": self.company.vat, |
| 59 |
}) |
| 60 |
try: |
| 61 |
response = self.env['iap.account']._l10n_in_connect_to_server( |
| 62 |
is_production=self.company.sudo().l10n_in_edi_production_env, |
| 63 |
params=params, |
| 64 |
url_path=url_path, |
| 65 |
config_parameter="l10n_in_edi_ewaybill.endpoint", |
| 66 |
timeout=10 |
| 67 |
) |
| 68 |
if response.get('error'): |
| 69 |
raise EWayBillError(response) |
| 70 |
except AccessError as e: |
| 71 |
_logger.warning("Connection error: %s", e.args[0]) |
| 72 |
raise EWayBillError({ |
| 73 |
"error": [{ |
| 74 |
"code": "access_error", |
| 75 |
"message": _( |
| 76 |
"Unable to connect to the E-WayBill service." |
| 77 |
"The web service may be temporary down. Please try again in a moment." |
| 78 |
) |
| 79 |
}] |
| 80 |
}) |
| 81 |
return response |
| 82 |
|
| 83 |
def _ewaybill_check_authentication(self): |
| 84 |
sudo_company = self.company.sudo() |
| 85 |
if sudo_company.l10n_in_ewaybill_username and sudo_company._l10n_in_ewaybill_token_is_valid(): |
| 86 |
return True |
| 87 |
elif sudo_company.l10n_in_ewaybill_username and sudo_company.l10n_in_ewaybill_password: |
| 88 |
try: |
| 89 |
self._ewaybill_authenticate() |
| 90 |
return True |
| 91 |
except EWayBillError: |
| 92 |
return False |
| 93 |
return False |
| 94 |
|
| 95 |
def _ewaybill_authenticate(self): |
| 96 |
params = {"password": self.company.sudo().l10n_in_ewaybill_password} |
| 97 |
response = self._ewaybill_jsonrpc_to_server( |
| 98 |
url_path="/iap/l10n_in_edi_ewaybill/1/authenticate", |
| 99 |
params=params |
| 100 |
) |
| 101 |
if response and response.get("status_cd") == "1": |
| 102 |
self.company.sudo().l10n_in_ewaybill_auth_validity = ( |
| 103 |
fields.Datetime.now() |
| 104 |
+ timedelta(hours=6, minutes=00, seconds=00) |
| 105 |
) |
| 106 |
|
| 107 |
def _ewaybill_make_transaction(self, operation_type, json_payload): |
| 108 |
""" |
| 109 |
:params operation_type: operation_type must be strictly `generate` or `cancel` |
| 110 |
:params json_payload: to be sent as params |
| 111 |
This method handles the common errors in generating and canceling the ewaybill |
| 112 |
""" |
| 113 |
try: |
| 114 |
if not self._ewaybill_check_authentication(): |
| 115 |
self._raise_ewaybill_no_config_error() |
| 116 |
params = {"json_payload": json_payload} |
| 117 |
url_path = f"/iap/l10n_in_edi_ewaybill/1/{operation_type}" |
| 118 |
response = self._ewaybill_jsonrpc_to_server( |
| 119 |
url_path=url_path, |
| 120 |
params=params |
| 121 |
) |
| 122 |
return response |
| 123 |
except EWayBillError as e: |
| 124 |
if "no-credit" in e.error_codes: |
| 125 |
e.error_json['odoo_warning'].append({ |
| 126 |
'message': self.env['account.move']._l10n_in_edi_get_iap_buy_credits_message() |
| 127 |
}) |
| 128 |
raise |
| 129 |
|
| 130 |
if '238' in e.error_codes: |
| 131 |
# Invalid token eror then create new token and send generate request again. |
| 132 |
# This happens when authenticate called from another odoo instance with same credentials |
| 133 |
# (like. Demo/Test) |
| 134 |
with contextlib.suppress(EWayBillError): |
| 135 |
self._ewaybill_authenticate() |
| 136 |
return self._ewaybill_jsonrpc_to_server( |
| 137 |
url_path=url_path, |
| 138 |
params=params, |
| 139 |
) |
| 140 |
|
| 141 |
if operation_type == "cancel" and "312" in e.error_codes: |
| 142 |
# E-waybill is already canceled |
| 143 |
# this happens when timeout from the Government portal but IRN is generated |
| 144 |
# Avoid raising error in this case, since it is already cancelled |
| 145 |
response = e.error_json |
| 146 |
response['odoo_warning'].append({ |
| 147 |
'message': Markup("%s<br/>%s:<br/>%s") % ( |
| 148 |
self.env['l10n.in.ewaybill']._get_default_help_message( |
| 149 |
self.env._('cancelled') |
| 150 |
), |
| 151 |
_("Error"), |
| 152 |
e.get_all_error_message() |
| 153 |
), |
| 154 |
'message_post': True |
| 155 |
}) |
| 156 |
# We return the error json as this a government document |
| 157 |
# On which in case of error 312, consider the ewaybill |
| 158 |
# as already cancelled |
| 159 |
return response |
| 160 |
|
| 161 |
if operation_type == "generate" and "604" in e.error_codes: |
| 162 |
# Get E-waybill by details in case of E-waybill is already generated |
| 163 |
# this happens when timeout from the Government portal but E-waybill is generated |
| 164 |
response = self._ewaybill_get_by_consigner( |
| 165 |
document_type=json_payload.get("docType"), |
| 166 |
document_number=json_payload.get("docNo") |
| 167 |
) |
| 168 |
return response |
| 169 |
raise |
| 170 |
|
| 171 |
def _ewaybill_generate(self, json_payload): |
| 172 |
return self._ewaybill_make_transaction("generate", json_payload) |
| 173 |
|
| 174 |
def _ewaybill_cancel(self, json_payload): |
| 175 |
return self._ewaybill_make_transaction("cancel", json_payload) |
| 176 |
|
| 177 |
def _ewaybill_get_by_consigner(self, document_type, document_number): |
| 178 |
if not self._ewaybill_check_authentication(): |
| 179 |
self._raise_ewaybill_no_config_error() |
| 180 |
params = {"document_type": document_type, "document_number": document_number} |
| 181 |
response = self._ewaybill_jsonrpc_to_server( |
| 182 |
url_path="/iap/l10n_in_edi_ewaybill/1/getewaybillgeneratedbyconsigner", |
| 183 |
params=params |
| 184 |
) |
| 185 |
# Add warning that ewaybill was already generated |
| 186 |
response.update({ |
| 187 |
'odoo_warning': [{ |
| 188 |
'message': self.env['l10n.in.ewaybill']._get_default_help_message( |
| 189 |
self.env._('generated') |
| 190 |
), |
| 191 |
'message_post': True |
| 192 |
}] |
| 193 |
}) |
| 194 |
return response |
| 195 |
|
| 196 |
@staticmethod |
| 197 |
def _raise_ewaybill_no_config_error(): |
| 198 |
raise EWayBillError({ |
| 199 |
"error": [{ |
| 200 |
"code": "0", |
| 201 |
"message": _( |
| 202 |
"Unable to send E-waybill." |
| 203 |
"Create an API user in NIC portal, and set it using the top menu: " |
| 204 |
"Configuration > Settings." |
| 205 |
) |
| 206 |
}] |
| 207 |
}) |