# -*- co# -*- coding: utf-8 -*-
# Odoo, Open Source Custom Tireston.
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).#
from odoo import api, models, _
from odoo.exceptions import UserError


class PosInvoiceReport(models.AbstractModel):
    _inherit = 'report.point_of_sale.report_invoice'

    @api.model
    def render_html(self, docids, data=None):
        Report = self.env['report']
        PosOrder = self.env['pos.order']
        ids_to_print = []
        invoiced_posorders_ids = []
        selected_orders = PosOrder.browse(docids)
        payment_method = ''
        payment_lines ={}
        for order in selected_orders.filtered(lambda o: o.invoice_id):
            payment_method += ', '.join(map(lambda x: x.journal_id.name, order.statement_ids))
            ids_to_print.append(order.invoice_id.id)
            invoiced_posorders_ids.append(order.id)
            for o in order.statement_ids :
                payment_lines[o.journal_id.name] = o.amount
        not_invoiced_orders_ids = list(set(docids) - set(invoiced_posorders_ids))
        if not_invoiced_orders_ids:
            not_invoiced_posorders = PosOrder.browse(not_invoiced_orders_ids)
            not_invoiced_orders_names = list(map(lambda a: a.name, not_invoiced_posorders))
            raise UserError(_('No link to an invoice for %s.') % ', '.join(not_invoiced_orders_names))

        return Report.sudo().render('account.report_invoice', {'payment_lines':payment_lines,'payment_method':payment_method or '','docs': self.env['account.invoice'].sudo().browse(ids_to_print)})
