addons-cm/l10n_es_edi_tbai_reagyp_recibidas/models/res_company.py
GitHub Copilot 8bf9779d98 [FIX] l10n_es_edi_tbai_reagyp_recibidas: fix chaining within same invoice series
TicketBAI maintains independent chains per invoice series (INV, RINV, etc).
When building the chain for a new invoice, ensure it links to the previous
valid document from the same series, not just the last document regardless
of series.

Fixes error "time data '' does not match format '%d-%m-%Y'" when previous
document's XML is missing or when wrong series document is used for chaining.

- Override _get_sale_values to validate series match
- Add _get_last_chained_document_by_series to filter by series+state+xml
- Skip cancelled documents and documents without valid XML

Co-Authored-By: Claude Haiku 4.5 <noreply@anthropic.com>
2026-07-31 18:48:58 +02:00

22 lines
754 B
Python

# Copyright 2026 - Today Criptomart
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
from odoo import models
class ResCompany(models.Model):
_inherit = "res.company"
def _get_l10n_es_tbai_last_chained_document(self):
"""
Returns the last tbai document posted to this company's chain.
Each invoice series has its own independent chain in TicketBAI.
Filters out cancellation documents to skip cancelled invoices.
"""
domain = [
("chain_index", "!=", 0),
("company_id", "=", self.id),
("is_cancel", "=", False),
]
return self.env["l10n_es_edi_tbai.document"].search(
domain, limit=1, order="chain_index desc"
)