Debian Codegraph @

addons/l10n_es_edi_verifactu/models/account_move_send.py

raw symbols SCIP
1 from odoo import _, api, models
2 from odoo.exceptions import RedirectWarning
3
4
5 class AccountMoveSend(models.AbstractModel):
6 _inherit = 'account.move.send'
7
8 @api.model
9 def _l10n_es_edi_verifactu_get_move_info(self, moves):
10 # We do not support sending a registration for already registered move or in case we are waiting to send a document already
11 verifactu_moves = moves.filtered(lambda move: move.l10n_es_edi_verifactu_required)
12 waiting_moves = verifactu_moves.filtered(lambda m: m.l10n_es_edi_verifactu_document_ids._filter_waiting())
13 registered_moves = verifactu_moves.filtered(
14 lambda m: m.l10n_es_edi_verifactu_state in ('registered_with_errors', 'accepted')
15 )
16 return {
17 'verifactu_moves': verifactu_moves,
18 'waiting_moves': waiting_moves,
19 'registered_moves': registered_moves,
20 'moves_to_send': verifactu_moves - waiting_moves - registered_moves,
21 }
22
23 @api.model
24 def _is_es_verifactu_applicable(self, move):
25 return bool(self._l10n_es_edi_verifactu_get_move_info(move)['verifactu_moves'])
26
27 def _get_all_extra_edis(self) -> dict:
28 # EXTENDS 'account'
29 res = super()._get_all_extra_edis()
30 res.update({'es_verifactu': {'label': _("Veri*Factu"), 'is_applicable': self._is_es_verifactu_applicable}})
31 return res
32
33 # -------------------------------------------------------------------------
34 # ALERTS
35 # -------------------------------------------------------------------------
36
37 def _get_alerts(self, moves, moves_data):
38 # EXTENDS 'account'
39 alerts = super()._get_alerts(moves, moves_data)
40 verifactu_info = self._l10n_es_edi_verifactu_get_move_info(moves)
41 if verifactu_info['waiting_moves']:
42 alerts['l10n_es_edi_verifactu_warning_waiting_moves'] = {
43 'message': _(
44 "The following entries wil be skipped. They are already waiting to send Veri*Factu records to the AEAT: %s.",
45 ', '.join(verifactu_info['waiting_moves'].mapped('name'))
46 ),
47 'action_text': _("View Invoice(s)"),
48 'action': verifactu_info['waiting_moves']._get_records_action(name=_("Check Invoice(s)")),
49 }
50 if verifactu_info['registered_moves']:
51 alerts['l10n_es_edi_verifactu_warning_registered_moves'] = {
52 'message': _(
53 "The following entries wil be skipped. They are already registered with the AEAT: %s.",
54 ', '.join(verifactu_info['registered_moves'].mapped('name'))
55 ),
56 'action_text': _("View Invoice(s)"),
57 'action': verifactu_info['registered_moves']._get_records_action(name=_("Check Invoice(s)")),
58 }
59 return alerts
60
61 # -------------------------------------------------------------------------
62 # SENDING METHODS
63 # -------------------------------------------------------------------------
64
65 @api.model
66 def _call_web_service_before_invoice_pdf_render(self, invoices_data):
67 # EXTENDS 'account'
68 super()._call_web_service_before_invoice_pdf_render(invoices_data)
69
70 for invoice, invoice_data in invoices_data.items():
71 if 'es_verifactu' not in invoice_data['extra_edis']:
72 continue
73 vals = invoice._l10n_es_edi_verifactu_get_record_values()
74 action = msg = None
75
76 if vals['verifactu_move_type'] == 'correction_substitution' and not vals['substituted_document']:
77 substituted_move = invoice.l10n_es_edi_verifactu_substituted_entry_id
78 msg = _("There is no Veri*Factu document for the substituted record.")
79 action = invoice._l10n_es_edi_verifactu_action_go_to_journal_entry(substituted_move)
80 elif vals['verifactu_move_type'] == 'correction_substitution' and not vals['substituted_document_reversal_document']:
81 substituted_move = invoice.l10n_es_edi_verifactu_substituted_entry_id
82 msg = _("There is no Veri*Factu document for the reversal of the substituted record.")
83 action = invoice._l10n_es_edi_verifactu_action_go_to_journal_entry(substituted_move.reversal_move_ids)
84 elif vals['verifactu_move_type'] in ('correction_incremental', 'reversal_for_substitution') and not vals['refunded_document']:
85 reversed_move = invoice.reversed_entry_id
86 msg = _("There is no Veri*Factu document for the refunded record.")
87 action = invoice._l10n_es_edi_verifactu_action_go_to_journal_entry(reversed_move)
88
89 if action and msg:
90 invoice_data['error'] = {
91 'verifactu_redirect_action': action,
92 'error_title': _("Go to the journal entry"),
93 'errors': [msg],
94 }
95
96 checked_invoices = self.env['account.move'].browse([
97 invoice.id for invoice, invoice_data in invoices_data.items()
98 if 'es_verifactu' in invoice_data['extra_edis'] and not invoice_data.get('error', {}).get('verifactu_redirect_action')
99 ])
100 invoices_to_send = self._l10n_es_edi_verifactu_get_move_info(checked_invoices)['moves_to_send']
101
102 created_document = invoices_to_send._l10n_es_edi_verifactu_mark_for_next_batch()
103
104 for invoice in invoices_to_send:
105 if not created_document[invoice].chain_index:
106 invoices_data[invoice]['error'] = {
107 'error_title': _("The Veri*Factu document could not be created for all invoices."),
108 'errors': [_("See the 'Veri*Factu' tab for more information.")],
109 }
110
111 if created_document and self._can_commit():
112 self.env.cr.commit()
113
114 def _hook_if_errors(self, moves_data, allow_raising=True):
115 # EXTENDS 'account'
116 if allow_raising:
117 for move, move_data in moves_data.items():
118 error = move_data.get('error', {})
119 if isinstance(error, dict) and error.get('verifactu_redirect_action'):
120 raise RedirectWarning('\n'.join(error['errors']), error['verifactu_redirect_action'], error['error_title'])
121 super()._hook_if_errors(moves_data, allow_raising=allow_raising)