|
| 1 | +# Copyright 2026 Tecnativa - Pedro M. Baeza |
| 2 | +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). |
| 3 | +from odoo import models |
| 4 | + |
| 5 | + |
| 6 | +class SiiMatchReport(models.Model): |
| 7 | + _inherit = "l10n.es.aeat.sii.match.report" |
| 8 | + |
| 9 | + def _get_aeat_odoo_invoices_by_csv(self, sii_response): |
| 10 | + # Try to match with PoS orders the remaining invoices by CSV |
| 11 | + matched_invoices, left_invoices = super()._get_aeat_odoo_invoices_by_csv( |
| 12 | + sii_response |
| 13 | + ) |
| 14 | + if self.invoice_type == "in": |
| 15 | + return matched_invoices, left_invoices |
| 16 | + for invoice in left_invoices: |
| 17 | + csv = invoice["DatosPresentacion"]["CSV"] |
| 18 | + odoo_order = self.env["pos.order"].search([("sii_csv", "=", csv)]) |
| 19 | + if odoo_order: |
| 20 | + matched_invoices[odoo_order] = invoice |
| 21 | + left_invoices.remove(invoice) |
| 22 | + return matched_invoices, left_invoices |
| 23 | + |
| 24 | + def _get_aeat_odoo_invoices_by_num(self, left_invoices, matched_invoices): |
| 25 | + # Try to match with PoS orders the remaining invoices by number |
| 26 | + matched_invoices, left_invoices = super()._get_aeat_odoo_invoices_by_num( |
| 27 | + left_invoices, matched_invoices |
| 28 | + ) |
| 29 | + if self.invoice_type == "in": |
| 30 | + return matched_invoices, left_invoices |
| 31 | + PosOrder = self.env["pos.order"] |
| 32 | + for invoice in left_invoices: |
| 33 | + name = invoice["IDFactura"]["NumSerieFacturaEmisor"] |
| 34 | + odoo_order = PosOrder.search( |
| 35 | + [ |
| 36 | + ("l10n_es_unique_id", "=", name), |
| 37 | + ("state", "in", PosOrder._get_valid_document_states()), |
| 38 | + ], |
| 39 | + ) |
| 40 | + if odoo_order: |
| 41 | + matched_invoices[odoo_order] = invoice |
| 42 | + left_invoices.remove(invoice) |
| 43 | + return matched_invoices, left_invoices |
| 44 | + |
| 45 | + def _get_not_in_sii_invoices(self, invoices): |
| 46 | + # Add the PoS orders not in SII |
| 47 | + res = super()._get_not_in_sii_invoices(invoices) |
| 48 | + if self.invoice_type == "in": |
| 49 | + return res |
| 50 | + date_start, date_end = self._get_date_interval() |
| 51 | + prev_order_ids = [x.id for x in invoices.keys() if x._name == "pos.order"] |
| 52 | + domain = [ |
| 53 | + ("id", "not in", prev_order_ids), |
| 54 | + ("date_order", ">=", date_start), |
| 55 | + ("date_order", "<", date_end), |
| 56 | + ("company_id", "=", self.company_id.id), |
| 57 | + ("sii_enabled", "=", True), |
| 58 | + ] |
| 59 | + for order in self.env["pos.order"].search(domain): |
| 60 | + res.append( |
| 61 | + { |
| 62 | + "invoice": order._get_document_serial_number(), |
| 63 | + "invoice_id": order.id, |
| 64 | + "model": order._name, |
| 65 | + "sii_contrast_state": "no_exist", |
| 66 | + "invoice_location": "odoo", |
| 67 | + } |
| 68 | + ) |
| 69 | + return res |
0 commit comments