| 1 |
import time |
| 2 |
from datetime import timedelta |
| 3 |
|
| 4 |
from odoo import api, fields, models, _ |
| 5 |
from odoo.addons.l10n_hu_edi.models.l10n_hu_edi_connection import L10nHuEdiConnection |
| 6 |
|
| 7 |
|
| 8 |
class AccountMoveSend(models.AbstractModel): |
| 9 |
_inherit = 'account.move.send' |
| 10 |
|
| 11 |
@api.model |
| 12 |
def _is_hu_edi_applicable(self, move): |
| 13 |
return 'upload' in move._origin._l10n_hu_edi_get_valid_actions() |
| 14 |
|
| 15 |
def _get_all_extra_edis(self) -> dict: |
| 16 |
# EXTENDS 'account' |
| 17 |
res = super()._get_all_extra_edis() |
| 18 |
res.update({'hu_nav_30': {'label': _("NAV 3.0"), 'is_applicable': self._is_hu_edi_applicable}}) |
| 19 |
return res |
| 20 |
|
| 21 |
# ------------------------------------------------------------------------- |
| 22 |
# ALERTS |
| 23 |
# ------------------------------------------------------------------------- |
| 24 |
|
| 25 |
def _get_alerts(self, moves, moves_data): |
| 26 |
# EXTENDS 'account' |
| 27 |
alerts = super()._get_alerts(moves, moves_data) |
| 28 |
hu_moves = moves.filtered(lambda m: 'hu_nav_30' in moves_data[m]['extra_edis']) |
| 29 |
enabled_moves = moves.filtered(lambda m: 'upload' in m._l10n_hu_edi_get_valid_actions())._origin |
| 30 |
if hu_moves - enabled_moves: |
| 31 |
alerts['l10n_hu_edi_checkbox_not_ticked'] = { |
| 32 |
'message': _("Invoices issued in Hungary must, with few exceptions, be reported to the NAV's Online-Invoice system.") |
| 33 |
} |
| 34 |
else: |
| 35 |
alerts.update(enabled_moves._l10n_hu_edi_check_invoices()) |
| 36 |
return alerts |
| 37 |
|
| 38 |
# ------------------------------------------------------------------------- |
| 39 |
# SENDING METHODS |
| 40 |
# ------------------------------------------------------------------------- |
| 41 |
|
| 42 |
@api.model |
| 43 |
def _l10n_hu_edi_cron_update_status(self): |
| 44 |
final_states = [False, 'confirmed', 'confirmed_warning', 'rejected', 'cancel_pending', 'cancelled'] |
| 45 |
invoices_pending = self.env['account.move'].search([('l10n_hu_edi_state', 'not in', final_states)]) |
| 46 |
invoices_pending.l10n_hu_edi_button_update_status(from_cron=True) |
| 47 |
|
| 48 |
if any(m.state not in final_states for m in invoices_pending): |
| 49 |
# Trigger cron again in 10 minutes. |
| 50 |
self.env.ref('l10n_hu_edi.ir_cron_update_status')._trigger(at=fields.Datetime.now() + timedelta(minutes=10)) |
| 51 |
|
| 52 |
def _call_web_service_before_invoice_pdf_render(self, invoices_data): |
| 53 |
# EXTENDS 'account' |
| 54 |
super()._call_web_service_before_invoice_pdf_render(invoices_data) |
| 55 |
|
| 56 |
invoices_hu = self.env['account.move'].browse([ |
| 57 |
invoice.id |
| 58 |
for invoice, invoice_data in invoices_data.items() |
| 59 |
if 'hu_nav_30' in invoice_data['extra_edis'] |
| 60 |
and 'upload' in invoice._l10n_hu_edi_get_valid_actions() |
| 61 |
]) |
| 62 |
|
| 63 |
if not invoices_hu: |
| 64 |
return |
| 65 |
|
| 66 |
# Pre-emptively acquire write lock on all invoices to be processed |
| 67 |
# Otherwise, we will get a serialization error later |
| 68 |
# (bad, because Odoo will try to retry the entire request, leading to duplicate sending to NAV) |
| 69 |
invoices_hu._l10n_hu_edi_acquire_lock() |
| 70 |
|
| 71 |
# STEP 1: Generate and send the invoice XMLs. |
| 72 |
invoices_to_upload = invoices_hu.filtered(lambda m: 'upload' in m._l10n_hu_edi_get_valid_actions()) |
| 73 |
|
| 74 |
# If we need to re-generate the PDF, break the link between the existing attachment and the 'invoice_pdf_report_file' field. |
| 75 |
# The existing PDF will remain linked to the invoice, but no longer as primary attachment. |
| 76 |
invoices_to_upload.invoice_pdf_report_id.write({'res_field': False}) |
| 77 |
invoices_to_upload.invalidate_recordset(fnames=['invoice_pdf_report_id', 'invoice_pdf_report_file']) |
| 78 |
|
| 79 |
with L10nHuEdiConnection(self.env) as connection: |
| 80 |
invoices_to_upload._l10n_hu_edi_upload(connection) |
| 81 |
if self._can_commit(): |
| 82 |
self.env.cr.commit() |
| 83 |
|
| 84 |
if any(m.l10n_hu_edi_state == 'sent' for m in invoices_hu): |
| 85 |
# If any invoices were just sent, wait so that NAV has enough time to process them |
| 86 |
time.sleep(2) |
| 87 |
|
| 88 |
# STEP 2: Query status |
| 89 |
invoices_hu.filtered(lambda m: 'query_status' in m._l10n_hu_edi_get_valid_actions())._l10n_hu_edi_query_status(connection) |
| 90 |
|
| 91 |
# STEP 3: Schedule update status of pending invoices in 10 minutes. |
| 92 |
if any(m.l10n_hu_edi_state not in [False, 'confirmed', 'confirmed_warning', 'rejected'] for m in invoices_hu): |
| 93 |
self.env.ref('l10n_hu_edi.ir_cron_update_status')._trigger(at=fields.Datetime.now() + timedelta(minutes=10)) |
| 94 |
|
| 95 |
# STEP 4: Error / success handling. |
| 96 |
for invoice in invoices_hu: |
| 97 |
# Log outcome in chatter |
| 98 |
formatted_message = self._format_error_html(invoice.l10n_hu_edi_messages) |
| 99 |
invoice.message_post(body=formatted_message) |
| 100 |
|
| 101 |
# Update invoice_data with errors |
| 102 |
blocking_level = invoice.l10n_hu_edi_messages.get('blocking_level') |
| 103 |
if blocking_level == 'error': |
| 104 |
invoices_data[invoice]['error'] = invoice.l10n_hu_edi_messages |
| 105 |
|
| 106 |
if self._can_commit(): |
| 107 |
self.env.cr.commit() |