Debian Codegraph @

addons/l10n_it_edi/models/account_move_send.py

raw symbols SCIP
1 # Part of Odoo. See LICENSE file for full copyright and licensing details.
2
3 from odoo import _, api, models
4 import base64
5
6
7 class AccountMoveSend(models.AbstractModel):
8 _inherit = 'account.move.send'
9
10 @api.model
11 def _is_it_edi_applicable(self, move):
12 return all([
13 move.company_id.account_fiscal_country_id.code == 'IT'
14 and move._l10n_it_edi_ready_for_xml_export()
15 and move.l10n_it_edi_state != 'rejected'
16 ])
17
18 def _get_all_extra_edis(self) -> dict:
19 # EXTENDS 'account'
20 res = super()._get_all_extra_edis()
21 res.update({'it_edi_send': {'label': _("Send to Tax Agency"), 'is_applicable': self._is_it_edi_applicable, 'help': _("Send the e-invoice XML to the Italian Tax Agency.")}})
22 return res
23
24 # -------------------------------------------------------------------------
25 # ALERTS
26 # -------------------------------------------------------------------------
27
28 def _get_alerts(self, moves, moves_data):
29 # EXTENDS 'account'
30 alerts = super()._get_alerts(moves, moves_data)
31 if it_moves := moves.filtered(lambda m: 'it_edi_send' in moves_data[m]['extra_edis'] or moves_data[m]['invoice_edi_format'] == 'it_edi_xml'):
32 if it_alerts := it_moves._l10n_it_edi_export_data_check():
33 alerts.update(**it_alerts)
34
35 # Invite the user to authorize Odoo and start using IT EDI in production mode
36 if 'prod' not in it_moves.mapped('l10n_it_edi_proxy_mode'):
37 alerts['l10n_it_edi_invite_authorize'] = {
38 'level': 'info',
39 'message': _("You must authorize Odoo in the Settings to use the IT EDI in production mode."),
40 'action_text': _("View Settings"),
41 'action': {
42 'name': _("Settings"),
43 'type': 'ir.actions.act_url',
44 'target': 'self',
45 'url': '/odoo/settings#l10n_it_edi_setting',
46 },
47 }
48
49 return alerts
50
51 # -------------------------------------------------------------------------
52 # SENDING METHODS
53 # -------------------------------------------------------------------------
54
55 def _get_invoice_extra_attachments(self, invoice):
56 # EXTENDS 'account'
57 return super()._get_invoice_extra_attachments(invoice) + self.env['ir.attachment'].search([
58 ('res_model', '=', 'account.move'),
59 ('res_field', '=', 'l10n_it_edi_attachment_file'),
60 ('res_id', 'in', invoice.ids),
61 ])
62
63 def _hook_invoice_document_before_pdf_report_render(self, invoice, invoice_data):
64 # EXTENDS 'account'
65 super()._hook_invoice_document_before_pdf_report_render(invoice, invoice_data)
66 if (
67 ('it_edi_send' in invoice_data['extra_edis'] and not invoice.l10n_it_edi_attachment_file)
68 or (invoice_data['invoice_edi_format'] == 'it_edi_xml' and invoice._l10n_it_edi_ready_for_xml_export())
69 ):
70 if errors := invoice._l10n_it_edi_export_data_check():
71 invoice_data['error'] = {
72 'error_title': _("Errors occurred while creating the e-invoice file:"),
73 'errors': [error['message'] for error in errors.values()],
74 }
75
76 def _hook_invoice_document_after_pdf_report_render(self, invoice, invoice_data):
77 # EXTENDS 'account'
78 super()._hook_invoice_document_after_pdf_report_render(invoice, invoice_data)
79 if (
80 invoice_data.get('pdf_attachment_values')
81 and (
82 ('it_edi_send' in invoice_data['extra_edis'] and not invoice.l10n_it_edi_attachment_file)
83 or (invoice_data['invoice_edi_format'] == 'it_edi_xml' and invoice._l10n_it_edi_ready_for_xml_export())
84 )
85 ):
86 invoice_data['l10n_it_edi_values'] = invoice._l10n_it_edi_get_attachment_values(
87 pdf_values=invoice_data['pdf_attachment_values'])
88
89 def _call_web_service_after_invoice_pdf_render(self, invoices_data):
90 # EXTENDS 'account'
91 super()._call_web_service_after_invoice_pdf_render(invoices_data)
92 attachments_vals = {}
93 moves = self.env['account.move']
94
95 # Filter only l10n_it_edi attachments
96 moves_data = {
97 move: move_data
98 for move, move_data in invoices_data.items()
99 if 'it_edi_send' in move_data['extra_edis']
100 }
101
102 # Prepare attachment data
103 for move, move_data in moves_data.items():
104 if attachment := move.l10n_it_edi_attachment_file:
105 attachments_vals[move] = {'name': move.l10n_it_edi_attachment_name, 'raw': base64.b64decode(attachment)}
106 moves |= move
107 elif edi_values := move_data.get('l10n_it_edi_values'):
108 attachments_vals[move] = edi_values
109 moves |= move
110
111 # Send
112 results = moves._l10n_it_edi_send(attachments_vals)
113
114 # Eventually update attachments with signed data
115 for move, move_data in moves_data.items():
116 if move.l10n_it_edi_attachment_file:
117 attachment_name = move.l10n_it_edi_attachment_name
118 elif attachment := move_data.get('l10n_it_edi_values'):
119 attachment_name = attachment['name']
120 attachment_data = results.get(attachment_name, {})
121 if attachment_data.get('signed') and (signed_data := attachment_data.get('signed_data')):
122 attachment['raw'] = signed_data.encode()
123 # Show that those moves couldn't be sent
124 if 'error_message' in attachment_data:
125 moves_data[move]['error'] = {'error_title': attachment_data['error_message']}
126
127 def _link_invoice_documents(self, invoices_data):
128 # EXTENDS 'account'
129 super()._link_invoice_documents(invoices_data)
130
131 move_ids_to_names = {}
132 for move, data in invoices_data.items():
133 if values := data.get('l10n_it_edi_values'):
134 move.l10n_it_edi_attachment_file = base64.b64encode(values['raw'])
135 move.l10n_it_edi_attachment_name = values['name']
136 move.invalidate_recordset(fnames=['l10n_it_edi_attachment_name', 'l10n_it_edi_attachment_file'])
137 move_ids_to_names[move.id] = values['name']
138
139 if move_ids_to_names:
140 attachments = self.env['ir.attachment'].search([
141 ('res_model', '=', 'account.move'),
142 ('res_field', '=', 'l10n_it_edi_attachment_file'),
143 ('res_id', 'in', list(move_ids_to_names)),
144 ])
145 for attachment in attachments:
146 attachment.name = move_ids_to_names.get(attachment.res_id)