# Copyright 2024 Criptomart
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).

from odoo import fields, models


class PurchaseReport(models.Model):
    _inherit = "purchase.report"

    price_received = fields.Float(
        string="Received Amount",
        readonly=True,
        help="Amount of received quantity (with discount applied)",
    )
    price_received_undiscounted = fields.Float(
        string="Received Amount (Undiscounted)",
        readonly=True,
        help="Amount of received quantity without discount",
    )

    def _select(self):
        res = super()._select()
        # Add received amount with discount (uses the already discounted price from purchase_discount)
        res += """,
                    sum(
                        l.qty_received / line_uom.factor * product_uom.factor
                        * (1.0 - COALESCE(l.discount, 0.0) / 100.0) * l.price_unit
                        / COALESCE(po.currency_rate, 1.0)
                    )::decimal(16,2) * currency_table.rate as price_received"""
        # Add received amount without discount
        res += """,
                    sum(
                        l.qty_received / line_uom.factor * product_uom.factor
                        * l.price_unit
                        / COALESCE(po.currency_rate, 1.0)
                    )::decimal(16,2) * currency_table.rate as price_received_undiscounted"""
        return res
