# -*- coding: utf-8 -*-
from odoo import models


class NacefFiscalClosing(models.Model):
    _inherit = "nacef.fiscal.closing"

    def _get_period_turnover(self):
        """Aggregate POS turnover for the period (E1204). Excludes TRAINING
        (E1003) and non-finalised orders."""
        self.ensure_one()
        orders = self.env["pos.order"].search([
            ("company_id", "=", self.company_id.id),
            ("date_order", ">=", self.date_start),
            ("date_order", "<", self.date_stop),
            ("nacef_imdf", "=", self.imdf),
            ("nacef_transaction_type", "=", "SALE"),
            ("nacef_operation_type", "in", ["TICKET", "REFUND"]),
            ("state", "in", ["paid", "done", "invoiced"]),
        ])
        return sum(orders.mapped("amount_total")), len(orders)
