# -*- coding: utf-8 -*- from odoo import fields, models class PurchaseReport(models.Model): _inherit = "purchase.report" billed_total = fields.Float('Total Billed', readonly=True) billed_untaxed_total = fields.Float('Untaxed Total Billed', readonly=True) def _select(self): select_str = super()._select() # aml.balance is in company currency. # We estimate tax included amount in company currency using the ratio of price_total/price_subtotal from the original currency. select_str += """ , sum(sub_aml.billed_total_cc)::decimal(16,2) * currency_table.rate as billed_total , sum(sub_aml.billed_untaxed_total_cc)::decimal(16,2) * currency_table.rate as billed_untaxed_total """ return select_str def _from(self): from_str = super()._from() from_str += """ left join ( select aml.purchase_line_id, sum(aml.balance * CASE WHEN aml.price_subtotal != 0 THEN aml.price_total / aml.price_subtotal ELSE 1.0 END) as billed_total_cc, sum(aml.balance) as billed_untaxed_total_cc from account_move_line aml join account_move am on (aml.move_id = am.id) where am.move_type in ('in_invoice', 'in_refund') and am.state != 'cancel' and aml.purchase_line_id is not null group by aml.purchase_line_id ) sub_aml on (l.id = sub_aml.purchase_line_id) """ return from_str