Debian Codegraph @

addons/l10n_in_ewaybill_irn/models/l10n_in_ewaybill.py

raw symbols SCIP
1 # Part of Odoo. See LICENSE file for full copyright and licensing details.
2
3 import base64
4 import json
5
6 from odoo import api, fields, models
7 from odoo.exceptions import UserError
8 from odoo.addons.l10n_in_ewaybill.tools.ewaybill_api import EWayBillApi, EWayBillError
9
10
11 class L10nInEwaybill(models.Model):
12
13 _inherit = 'l10n.in.ewaybill'
14
15 is_process_through_irn = fields.Boolean(compute='_compute_is_process_through_irn')
16 is_sent_through_irn = fields.Boolean(readonly=True)
17
18 @api.depends('account_move_id.l10n_in_edi_status')
19 def _compute_is_process_through_irn(self):
20 for ewaybill in self:
21 account_move_id = ewaybill.account_move_id
22 ewaybill.is_process_through_irn = (
23 account_move_id
24 # E-way Bill is not generated by IRN for document types of Debit Note and Credit Note.
25 and not (account_move_id.move_type == 'out_refund' or account_move_id.debit_origin_id)
26 and account_move_id.l10n_in_edi_status == 'sent'
27 )
28
29 def _prepare_ewaybill_transportation_json_payload(self):
30 if self.is_process_through_irn:
31 return dict(
32 filter(lambda kv: kv[1], {
33 "TransId": self.transporter_id.vat,
34 "TransName": self.transporter_id.name,
35 "TransMode": self.mode,
36 "TransDocNo": self.transportation_doc_no,
37 "TransDocDt": self.transportation_doc_date and self.transportation_doc_date.strftime("%d/%m/%Y"),
38 "VehNo": self.vehicle_no,
39 "VehType": self.vehicle_type,
40 }.items())
41 )
42 return super()._prepare_ewaybill_transportation_json_payload()
43
44 def _ewaybill_generate_irn_json(self):
45 json_payload = {
46 'Irn': self.account_move_id._get_l10n_in_edi_response_json().get('Irn'),
47 'Distance': str(self.distance),
48 **self._prepare_ewaybill_transportation_json_payload(),
49 'DispDtls': self.env['account.move']._get_l10n_in_edi_partner_details(
50 self.partner_ship_from_id, set_phone_and_email=False, set_vat=False)
51 }
52 if self.account_move_id.l10n_in_gst_treatment == 'overseas':
53 json_payload["ExpShipDtls"] = self.env['account.move']._get_l10n_in_edi_partner_details(
54 self.partner_ship_to_id, set_phone_and_email=False, set_vat=False)
55 json_payload["ExpShipDtls"].pop('Nm') # 'Nm'(Name) is not included in ExpShipDtls as per the JSON schema
56 return json_payload
57
58 def _compute_content(self):
59 irn_ewaybill = self.filtered('is_process_through_irn')
60 for ewaybill in irn_ewaybill:
61 ewaybill_json = ewaybill._ewaybill_generate_irn_json()
62 ewaybill.content = base64.b64encode(json.dumps(ewaybill_json).encode())
63 super(L10nInEwaybill, self - irn_ewaybill)._compute_content()
64
65 def action_generate_ewaybill(self):
66 irn_ewaybill = self.filtered('is_process_through_irn')
67 for ewaybill in irn_ewaybill:
68 if errors := ewaybill._check_configuration():
69 raise UserError('\n'.join(errors))
70 ewaybill._generate_ewaybill_by_irn()
71 super(L10nInEwaybill, self - irn_ewaybill).action_generate_ewaybill()
72
73 def action_reset_to_pending(self):
74 res = super().action_reset_to_pending()
75 self.is_sent_through_irn = False
76 return res
77
78 def _generate_ewaybill_by_irn(self):
79 self.ensure_one()
80 self._log_retry_message_on_generate()
81 self._lock_ewaybill()
82 try:
83 response = self._ewaybill_generate_by_irn(self._ewaybill_generate_irn_json())
84 except EWayBillError as error:
85 self._handle_error(error)
86 return False
87 self._handle_internal_warning_if_present(response) # In case of error 604
88 response_data = response.get('data', {})
89 name = response_data.get('EwbNo')
90 self._create_and_post_response_attachment(name, response)
91 # Note: response keys are different then the direct one
92 self._write_successfully_response({
93 'name': name,
94 'state': 'generated',
95 'is_sent_through_irn': True,
96 'ewaybill_date': self._convert_str_datetime_to_date(
97 response_data['EwbDt']
98 ),
99 'ewaybill_expiry_date': self._convert_str_datetime_to_date(
100 response_data.get('EwbValidTill')
101 ),
102 **self._l10n_in_ewaybill_handle_zero_distance_alert_if_present(response_data)
103 })
104 self.env.cr.commit()
105
106 def _get_edi_irn_number(self):
107 self.ensure_one()
108 l10n_in_edi_response_json = self.account_move_id._get_l10n_in_edi_response_json()
109 if not l10n_in_edi_response_json:
110 raise EWayBillError({
111 'error': [{
112 'code': 'waiting',
113 'message': self.env._("waiting for IRN generation to create E-waybill")
114 }]
115 })
116 return l10n_in_edi_response_json['Irn']
117
118 def _ewaybill_generate_by_irn(self, json_payload):
119 self.ensure_one()
120 if not self.company_id._l10n_in_edi_get_token():
121 raise EWayBillError({
122 'error': [{
123 'code': '0',
124 'message': self.env._(
125 "Unable to send E-waybill by IRN."
126 "Ensure GST Number set on company setting and EDI and Ewaybilll"
127 " credentials are Verified."
128 )
129 }]
130 })
131 response = self.account_move_id._l10n_in_edi_connect_to_server(
132 url_end_point='generate_ewaybill_by_irn',
133 json_payload=json_payload
134 )
135 if response.get('error'):
136 error_codes = [error.get('code') for error in response.get('error')]
137 if 'no-credit' in error_codes:
138 response['odoo_warning'].append({
139 'message': self.env['account.move']._l10n_in_edi_get_iap_buy_credits_message()
140 })
141 if '4002' in error_codes or '4026' in error_codes:
142 # Get E-waybill by details in case of IRN is already generated
143 # this happens when timeout from the Government portal but E-waybill is generated
144 response = self.account_move_id._l10n_in_edi_connect_to_server(
145 url_end_point='get_ewaybill_by_irn',
146 params={"irn": self._get_edi_irn_number()}
147 )
148 response.update({
149 'odoo_warning': [{
150 'message': self._get_default_help_message(self.env._('generated')),
151 'message_post': True
152 }]
153 })
154 if response.get('error'):
155 raise EWayBillError(response)
156 return response