| 1 |
from odoo import _, api, fields, models |
| 2 |
|
| 3 |
|
| 4 |
class AccountMove(models.Model): |
| 5 |
_inherit = 'account.move' |
| 6 |
|
| 7 |
l10n_es_edi_verifactu_required = fields.Boolean( |
| 8 |
string="Veri*Factu Required", |
| 9 |
related='company_id.l10n_es_edi_verifactu_required', |
| 10 |
) |
| 11 |
l10n_es_edi_verifactu_document_ids = fields.One2many( |
| 12 |
comodel_name='l10n_es_edi_verifactu.document', |
| 13 |
inverse_name='move_id', |
| 14 |
string="Veri*Factu Documents", |
| 15 |
) |
| 16 |
l10n_es_edi_verifactu_state = fields.Selection( |
| 17 |
string="Veri*Factu Status", |
| 18 |
selection=[ |
| 19 |
('rejected', "Rejected"), |
| 20 |
('registered_with_errors', "Registered with Errors"), |
| 21 |
('accepted', "Accepted"), |
| 22 |
('cancelled', "Cancelled"), |
| 23 |
], |
| 24 |
compute='_compute_l10n_es_edi_verifactu_state', store=True, |
| 25 |
help="""- Rejected: Successfully sent to the AEAT, but it was rejected during validation |
| 26 |
- Registered with Errors: Registered at the AEAT, but the AEAT has some issues with the sent document |
| 27 |
- Accepted: Registered by the AEAT without errors |
| 28 |
- Cancelled: Registered by the AEAT as cancelled""", |
| 29 |
) |
| 30 |
l10n_es_edi_verifactu_warning_level = fields.Char( |
| 31 |
string="Veri*Factu Warning Level", |
| 32 |
compute="_compute_l10n_es_edi_verifactu_warning", |
| 33 |
) |
| 34 |
l10n_es_edi_verifactu_warning = fields.Html( |
| 35 |
string="Veri*Factu Warning", |
| 36 |
compute="_compute_l10n_es_edi_verifactu_warning", |
| 37 |
) |
| 38 |
l10n_es_edi_verifactu_qr_code = fields.Char( |
| 39 |
string="Veri*Factu QR Code", |
| 40 |
compute='_compute_l10n_es_edi_verifactu_qr_code', |
| 41 |
) |
| 42 |
l10n_es_edi_verifactu_show_cancel_button = fields.Boolean( |
| 43 |
string="Show Veri*Factu Cancel Button", |
| 44 |
compute='_compute_l10n_es_edi_verifactu_show_cancel_button', |
| 45 |
) |
| 46 |
l10n_es_edi_verifactu_available_clave_regimens = fields.Char( |
| 47 |
string="Available Veri*Factu Regime Key", |
| 48 |
compute='_compute_l10n_es_edi_verifactu_available_clave_regimens', |
| 49 |
help="Technical field to enable a dynamic selection of the field \"Veri*Factu Regime Key\"", |
| 50 |
) |
| 51 |
l10n_es_edi_verifactu_clave_regimen = fields.Selection( |
| 52 |
string="Veri*Factu Regime Key", |
| 53 |
selection='_l10n_es_edi_verifactu_clave_regimen_selection', |
| 54 |
compute='_compute_l10n_es_edi_verifactu_clave_regimen', store=True, readonly=False, |
| 55 |
) |
| 56 |
l10n_es_edi_verifactu_substituted_entry_id = fields.Many2one( |
| 57 |
comodel_name='account.move', |
| 58 |
string="Substitution of", |
| 59 |
index='btree_not_null', |
| 60 |
readonly=True, |
| 61 |
copy=False, |
| 62 |
check_company=True, |
| 63 |
) |
| 64 |
l10n_es_edi_verifactu_substitution_move_ids = fields.One2many( |
| 65 |
string="Substituted by", |
| 66 |
comodel_name='account.move', |
| 67 |
inverse_name='l10n_es_edi_verifactu_substituted_entry_id', |
| 68 |
) |
| 69 |
l10n_es_edi_verifactu_refund_reason = fields.Selection( |
| 70 |
selection=[ |
| 71 |
('R1', "R1: Art 80.1 and 80.2 and error of law"), |
| 72 |
('R2', "R2: Art. 80.3"), |
| 73 |
('R3', "R3: Art. 80.4"), |
| 74 |
('R4', "R4: Rest"), |
| 75 |
('R5', "R5: Corrective invoices concerning simplified invoices"), |
| 76 |
], |
| 77 |
string="Veri*Factu Refund Reason", |
| 78 |
copy=False, |
| 79 |
) |
| 80 |
|
| 81 |
@api.model |
| 82 |
def _l10n_es_edi_verifactu_clave_regimen_selection(self): |
| 83 |
return [ |
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
('01', _("General regime operation")), |
| 91 |
('02', _("Export")), |
| 92 |
('11', _("Leasing of business premises")), |
| 93 |
|
| 94 |
('17_iva', _("Operation under one of the regimes provided for in Chapter XI of Title IX (OSS and IOSS).")), |
| 95 |
('18_iva', _("Recargo de equivalencia")), |
| 96 |
('19_iva', _("Operations of activities included in the Special Regime for Agriculture, Livestock and Fishing (REAGYP)")), |
| 97 |
('20_iva', _("Simplified Regime")), |
| 98 |
|
| 99 |
('17_igic', _("Special retailer regime")), |
| 100 |
] |
| 101 |
|
| 102 |
def _l10n_es_edi_verifactu_get_tax_applicability(self): |
| 103 |
""" |
| 104 |
Currently we only support a single Veri*Factu Tax Applicability per Veri*Factu document. |
| 105 |
In `_check_record_values` of model 'l10n_es_edi_verifactu.document' we check: |
| 106 |
There is only a single Veri*Factu Tax Applicability on the whole move. |
| 107 |
""" |
| 108 |
self.ensure_one() |
| 109 |
if not self.l10n_es_edi_verifactu_required: |
| 110 |
return False |
| 111 |
|
| 112 |
taxes = self.invoice_line_ids.tax_ids.flatten_taxes_hierarchy() |
| 113 |
return taxes._l10n_es_edi_verifactu_get_applicability() |
| 114 |
|
| 115 |
@api.model |
| 116 |
def _l10n_es_edi_verifactu_get_available_clave_regimens_map(self): |
| 117 |
""" |
| 118 |
Return dictionary (Veri*Factu Tax Applicability -> set(operation types)) |
| 119 |
""" |
| 120 |
clave_regimen_selection = self._l10n_es_edi_verifactu_clave_regimen_selection() |
| 121 |
return { |
| 122 |
'01': {ot for ot, _desc in clave_regimen_selection if len(ot.removesuffix('_iva')) == 2}, |
| 123 |
'03': {ot for ot, _desc in clave_regimen_selection if len(ot.removesuffix('_igic')) == 2}, |
| 124 |
} |
| 125 |
|
| 126 |
def _l10n_es_edi_verifactu_get_suggested_clave_regimen(self): |
| 127 |
""" |
| 128 |
Currently we only support a single Clave Regimen per Veri*Factu document. |
| 129 |
""" |
| 130 |
self.ensure_one() |
| 131 |
|
| 132 |
tax_applicability = self._l10n_es_edi_verifactu_get_tax_applicability() |
| 133 |
if not tax_applicability: |
| 134 |
return False |
| 135 |
|
| 136 |
taxes = self.invoice_line_ids.tax_ids.flatten_taxes_hierarchy() |
| 137 |
special_regime = self.company_id.l10n_es_edi_verifactu_special_vat_regime |
| 138 |
return taxes._l10n_es_edi_verifactu_get_suggested_clave_regimen( |
| 139 |
special_regime, forced_tax_applicability=tax_applicability |
| 140 |
) |
| 141 |
|
| 142 |
@api.depends('invoice_line_ids.tax_ids') |
| 143 |
def _compute_l10n_es_edi_verifactu_available_clave_regimens(self): |
| 144 |
available_clave_regimens = { |
| 145 |
tax_applicability: ','.join(clave_regimens) |
| 146 |
for tax_applicability, clave_regimens in self._l10n_es_edi_verifactu_get_available_clave_regimens_map().items() |
| 147 |
} |
| 148 |
for move in self: |
| 149 |
tax_applicability = move._l10n_es_edi_verifactu_get_tax_applicability() |
| 150 |
move.l10n_es_edi_verifactu_available_clave_regimens = available_clave_regimens.get(tax_applicability, False) |
| 151 |
|
| 152 |
@api.depends('invoice_line_ids.tax_ids') |
| 153 |
def _compute_l10n_es_edi_verifactu_clave_regimen(self): |
| 154 |
|
| 155 |
available_clave_regimens = self._l10n_es_edi_verifactu_get_available_clave_regimens_map() |
| 156 |
for move in self: |
| 157 |
clave_regimen = move.l10n_es_edi_verifactu_clave_regimen |
| 158 |
tax_applicability = move._l10n_es_edi_verifactu_get_tax_applicability() |
| 159 |
if clave_regimen not in available_clave_regimens.get(tax_applicability, set()): |
| 160 |
clave_regimen = move._l10n_es_edi_verifactu_get_suggested_clave_regimen() |
| 161 |
move.l10n_es_edi_verifactu_clave_regimen = clave_regimen |
| 162 |
|
| 163 |
@api.depends('l10n_es_edi_verifactu_document_ids', 'l10n_es_edi_verifactu_document_ids.state') |
| 164 |
def _compute_l10n_es_edi_verifactu_state(self): |
| 165 |
for move in self: |
| 166 |
state = move.l10n_es_edi_verifactu_document_ids._get_state() |
| 167 |
move.l10n_es_edi_verifactu_state = state |
| 168 |
|
| 169 |
@api.depends('l10n_es_edi_verifactu_document_ids', 'l10n_es_edi_verifactu_document_ids.json_attachment_id') |
| 170 |
def _compute_l10n_es_edi_verifactu_qr_code(self): |
| 171 |
for move in self: |
| 172 |
last_submission = move.l10n_es_edi_verifactu_document_ids._get_last('submission') |
| 173 |
url = last_submission._get_qr_code_img_url() if last_submission else False |
| 174 |
move.l10n_es_edi_verifactu_qr_code = url |
| 175 |
|
| 176 |
@api.depends('state', 'l10n_es_edi_verifactu_state', 'l10n_es_edi_verifactu_document_ids', |
| 177 |
'l10n_es_edi_verifactu_document_ids.state', 'l10n_es_edi_verifactu_document_ids.errors') |
| 178 |
def _compute_l10n_es_edi_verifactu_warning(self): |
| 179 |
for move in self: |
| 180 |
last_document = move.l10n_es_edi_verifactu_document_ids.sorted()[:1] |
| 181 |
|
| 182 |
warning = False |
| 183 |
warning_level = False |
| 184 |
if last_document.state == 'registered_with_errors': |
| 185 |
warning = last_document.errors |
| 186 |
warning_level = 'warning' |
| 187 |
elif last_document.errors: |
| 188 |
warning = last_document.errors |
| 189 |
warning_level = 'danger' |
| 190 |
elif move.state == 'draft': |
| 191 |
if move.l10n_es_edi_verifactu_state: |
| 192 |
warning = _("You are modifying a journal entry for which a Veri*Factu document has been sent to the AEAT already.") |
| 193 |
warning_level = 'warning' |
| 194 |
elif last_document._filter_waiting(): |
| 195 |
warning = _("You are modifying a journal entry for which a Veri*Factu document is waiting to be sent.") |
| 196 |
warning_level = 'warning' |
| 197 |
|
| 198 |
if last_document._filter_waiting(): |
| 199 |
warning = _("%(existing_warning)sA Veri*Factu document is waiting to be sent as soon as possible.", |
| 200 |
existing_warning=(warning + '\n' if warning else '')) |
| 201 |
warning_level = warning_level or 'info' |
| 202 |
|
| 203 |
move.l10n_es_edi_verifactu_warning = warning |
| 204 |
move.l10n_es_edi_verifactu_warning_level = warning_level |
| 205 |
|
| 206 |
@api.depends('l10n_es_edi_verifactu_state') |
| 207 |
def _compute_l10n_es_edi_verifactu_show_cancel_button(self): |
| 208 |
for move in self: |
| 209 |
move.l10n_es_edi_verifactu_show_cancel_button = move.l10n_es_edi_verifactu_state in ('registered_with_errors', 'accepted') |
| 210 |
|
| 211 |
@api.depends('l10n_es_edi_verifactu_state', 'l10n_es_edi_verifactu_document_ids', |
| 212 |
'l10n_es_edi_verifactu_document_ids.state', 'l10n_es_edi_verifactu_document_ids.json_attachment_id') |
| 213 |
def _compute_show_reset_to_draft_button(self): |
| 214 |
""" |
| 215 |
Disallow resetting to draft in the following cases: |
| 216 |
* The move is registered (accepted, regsitered_with_errors, cancelled) |
| 217 |
* We are waiting to sent a document to the AEAT |
| 218 |
""" |
| 219 |
|
| 220 |
super()._compute_show_reset_to_draft_button() |
| 221 |
for move in self: |
| 222 |
if (move.l10n_es_edi_verifactu_state in ('registered_with_errors', 'accepted', 'cancelled') |
| 223 |
or move.l10n_es_edi_verifactu_document_ids._filter_waiting()): |
| 224 |
move.show_reset_to_draft_button = False |
| 225 |
|
| 226 |
@api.model |
| 227 |
def _l10n_es_edi_verifactu_action_go_to_journal_entry(self, move): |
| 228 |
return { |
| 229 |
'type': 'ir.actions.act_window', |
| 230 |
'view_mode': 'form', |
| 231 |
'res_model': 'account.move', |
| 232 |
'res_id': move.id, |
| 233 |
'views': [(self.env.ref('account.view_move_form').id, 'form')], |
| 234 |
'context': self.env.context, |
| 235 |
} |
| 236 |
|
| 237 |
def l10n_es_edi_verifactu_button_cancel(self): |
| 238 |
self._l10n_es_edi_verifactu_mark_for_next_batch(cancellation=True) |
| 239 |
|
| 240 |
def _l10n_es_edi_verifactu_check(self, cancellation=False): |
| 241 |
self.ensure_one() |
| 242 |
errors = [] |
| 243 |
|
| 244 |
if self.state != 'posted': |
| 245 |
errors.append(_("The journal entry has to be posted.")) |
| 246 |
|
| 247 |
tax_applicability = self._l10n_es_edi_verifactu_get_tax_applicability() |
| 248 |
selected_clave_regimen = self.l10n_es_edi_verifactu_clave_regimen |
| 249 |
available_clave_regimens_map = self._l10n_es_edi_verifactu_get_available_clave_regimens_map() |
| 250 |
if selected_clave_regimen and selected_clave_regimen not in available_clave_regimens_map.get(tax_applicability, set()): |
| 251 |
errors.append(_("The Veri*Factu Regime Key is not compatible with the Veri*Factu Tax Applicability.")) |
| 252 |
|
| 253 |
return errors |
| 254 |
|
| 255 |
def _l10n_es_edi_verifactu_get_record_values(self, cancellation=False): |
| 256 |
self.ensure_one() |
| 257 |
|
| 258 |
company = self.company_id |
| 259 |
document_type = 'cancellation' if cancellation else 'submission' |
| 260 |
vals = { |
| 261 |
'company': company, |
| 262 |
'record': self, |
| 263 |
'cancellation': cancellation, |
| 264 |
'errors': self._l10n_es_edi_verifactu_check(cancellation=cancellation), |
| 265 |
'document_vals': { |
| 266 |
'move_id': self.id, |
| 267 |
'company_id': company.id, |
| 268 |
'document_type': document_type, |
| 269 |
}, |
| 270 |
} |
| 271 |
|
| 272 |
if vals['errors']: |
| 273 |
return vals |
| 274 |
|
| 275 |
documents = self.l10n_es_edi_verifactu_document_ids |
| 276 |
|
| 277 |
. |
| 278 |
rejected_before = documents._get_last(document_type).state == 'rejected' |
| 279 |
|
| 280 |
tax_applicability = self._l10n_es_edi_verifactu_get_tax_applicability() |
| 281 |
selected_clave_regimen = self.l10n_es_edi_verifactu_clave_regimen |
| 282 |
clave_regimen = selected_clave_regimen and selected_clave_regimen.split('_', 1)[0] |
| 283 |
substituted_move = self.l10n_es_edi_verifactu_substituted_entry_id |
| 284 |
reversed_move = self.reversed_entry_id |
| 285 |
|
| 286 |
move_type = self.move_type |
| 287 |
if move_type == 'out_invoice' and substituted_move: |
| 288 |
verifactu_move_type = 'correction_substitution' |
| 289 |
elif move_type == 'out_invoice': |
| 290 |
verifactu_move_type = 'invoice' |
| 291 |
elif move_type == 'out_refund' and reversed_move.l10n_es_edi_verifactu_substitution_move_ids: |
| 292 |
verifactu_move_type = 'reversal_for_substitution' |
| 293 |
else: |
| 294 |
|
| 295 |
verifactu_move_type = 'correction_incremental' |
| 296 |
|
| 297 |
vals.update({ |
| 298 |
'rejected_before': rejected_before, |
| 299 |
'verifactu_state': self.l10n_es_edi_verifactu_state, |
| 300 |
'delivery_date': self.delivery_date, |
| 301 |
'description': self.invoice_origin[:500] if self.invoice_origin else None, |
| 302 |
'invoice_date': self.invoice_date, |
| 303 |
'is_simplified': self.l10n_es_is_simplified, |
| 304 |
'move_type': move_type, |
| 305 |
'verifactu_move_type': verifactu_move_type, |
| 306 |
'sign': -1 if move_type == 'out_refund' else 1, |
| 307 |
'name': self.name, |
| 308 |
'partner': self.commercial_partner_id, |
| 309 |
'refund_reason': self.l10n_es_edi_verifactu_refund_reason, |
| 310 |
'refunded_document': reversed_move.l10n_es_edi_verifactu_document_ids._get_last('submission'), |
| 311 |
'substituted_document': substituted_move.l10n_es_edi_verifactu_document_ids._get_last('submission'), |
| 312 |
'substituted_document_reversal_document': substituted_move.reversal_move_ids.l10n_es_edi_verifactu_document_ids._get_last('submission'), |
| 313 |
'documents': documents, |
| 314 |
'record_identifier': documents._get_last('submission')._get_record_identifier(), |
| 315 |
'l10n_es_applicability': tax_applicability, |
| 316 |
'clave_regimen': clave_regimen or None, |
| 317 |
}) |
| 318 |
|
| 319 |
base_amls = self.line_ids.filtered(lambda x: x.display_type == 'product') |
| 320 |
base_lines = [self._prepare_product_base_line_for_taxes_computation(x) for x in base_amls] |
| 321 |
epd_amls = self.line_ids.filtered(lambda line: line.display_type == 'epd') |
| 322 |
base_lines += [self._prepare_epd_base_line_for_taxes_computation(line) for line in epd_amls] |
| 323 |
cash_rounding_amls = self.line_ids \ |
| 324 |
.filtered(lambda line: line.display_type == 'rounding' and not line.tax_repartition_line_id) |
| 325 |
base_lines += [self._prepare_cash_rounding_base_line_for_taxes_computation(line) for line in cash_rounding_amls] |
| 326 |
tax_amls = self.line_ids.filtered('tax_repartition_line_id') |
| 327 |
tax_lines = [self._prepare_tax_line_for_taxes_computation(x) for x in tax_amls] |
| 328 |
vals['tax_details'] = self.env['l10n_es_edi_verifactu.document']._get_tax_details(base_lines, company, tax_lines=tax_lines) |
| 329 |
|
| 330 |
return vals |
| 331 |
|
| 332 |
def _l10n_es_edi_verifactu_create_documents(self, cancellation=False): |
| 333 |
record_values_list = [ |
| 334 |
move._l10n_es_edi_verifactu_get_record_values(cancellation=cancellation) |
| 335 |
for move in self |
| 336 |
] |
| 337 |
return { |
| 338 |
record_values['record']: self.env['l10n_es_edi_verifactu.document']._create_for_record(record_values) |
| 339 |
for record_values in record_values_list |
| 340 |
} |
| 341 |
|
| 342 |
def _l10n_es_edi_verifactu_mark_for_next_batch(self, cancellation=False): |
| 343 |
document_map = self._l10n_es_edi_verifactu_create_documents(cancellation=cancellation) |
| 344 |
self.env['l10n_es_edi_verifactu.document'].trigger_next_batch() |
| 345 |
return document_map |