|
| 1 | +from odoo import models, api |
| 2 | + |
| 3 | +class AccountInvoiceReport(models.Model): |
| 4 | + _inherit = "account.invoice.report" |
| 5 | + |
| 6 | + @api.model |
| 7 | + def _select(self): |
| 8 | + return ''' |
| 9 | + WITH currency_rate AS MATERIALIZED ( |
| 10 | + SELECT |
| 11 | + r.currency_id, |
| 12 | + COALESCE(r.company_id, c.id) as company_id, |
| 13 | + 1/r.rate as rate, |
| 14 | + r.name AS date_start, |
| 15 | + (SELECT name FROM res_currency_rate r2 |
| 16 | + WHERE r2.name > r.name AND |
| 17 | + r2.currency_id = r.currency_id AND |
| 18 | + (r2.company_id is null or r2.company_id = c.id) |
| 19 | + ORDER BY r2.name ASC |
| 20 | + LIMIT 1) AS date_end |
| 21 | + FROM res_currency_rate r |
| 22 | + JOIN res_company c ON (r.company_id is null or r.company_id = c.id) |
| 23 | + WHERE c.id = %s |
| 24 | + ) |
| 25 | + SELECT |
| 26 | + line.id, |
| 27 | + line.move_id, |
| 28 | + line.product_id, |
| 29 | + line.account_id, |
| 30 | + line.journal_id, |
| 31 | + line.company_id, |
| 32 | + line.company_currency_id, |
| 33 | + line.partner_id AS commercial_partner_id, |
| 34 | + account.account_type AS user_type, |
| 35 | + move.state, |
| 36 | + move.move_type, |
| 37 | + move.partner_id, |
| 38 | + move.invoice_user_id, |
| 39 | + move.fiscal_position_id, |
| 40 | + move.payment_state, |
| 41 | + move.invoice_date, |
| 42 | + move.invoice_date_due, |
| 43 | + uom_template.id AS product_uom_id, |
| 44 | + template.categ_id AS product_categ_id, |
| 45 | + line.quantity / NULLIF(COALESCE(uom_line.factor, 1) / COALESCE(uom_template.factor, 1), 0.0) * (CASE WHEN move.move_type IN ('in_invoice','out_refund','in_receipt') THEN -1 ELSE 1 END) |
| 46 | + AS quantity, |
| 47 | + CASE WHEN line.currency_id <> rc.currency_id THEN -line.balance * currency_table.rate ELSE -line.balance END AS price_subtotal, |
| 48 | + line.price_total * (CASE WHEN move.move_type IN ('in_invoice','out_refund','in_receipt') THEN -1 ELSE 1 END) |
| 49 | + AS price_total, |
| 50 | + CASE WHEN line.currency_id <> rc.currency_id THEN |
| 51 | + -COALESCE( |
| 52 | + -- Average line price |
| 53 | + (line.balance / NULLIF(line.quantity, 0.0)) * (CASE WHEN move.move_type IN ('in_invoice','out_refund','in_receipt') THEN -1 ELSE 1 END) |
| 54 | + -- convert to template uom |
| 55 | + * (NULLIF(COALESCE(uom_line.factor, 1), 0.0) / NULLIF(COALESCE(uom_template.factor, 1), 0.0)), |
| 56 | + 0.0) * currency_table.rate |
| 57 | + ELSE -COALESCE( |
| 58 | + -- Average line price |
| 59 | + (line.balance / NULLIF(line.quantity, 0.0)) * (CASE WHEN move.move_type IN ('in_invoice','out_refund','in_receipt') THEN -1 ELSE 1 END) |
| 60 | + -- convert to template uom |
| 61 | + * (NULLIF(COALESCE(uom_line.factor, 1), 0.0) / NULLIF(COALESCE(uom_template.factor, 1), 0.0)), |
| 62 | + 0.0) END |
| 63 | + AS price_average, |
| 64 | + COALESCE(partner.country_id, commercial_partner.country_id) AS country_id, |
| 65 | + line.currency_id AS currency_id |
| 66 | + ''' % self.env.company.id |
| 67 | + |
| 68 | + @api.model |
| 69 | + def _from(self): |
| 70 | + return ''' |
| 71 | + FROM account_move_line line |
| 72 | + LEFT JOIN res_partner partner ON partner.id = line.partner_id |
| 73 | + LEFT JOIN product_product product ON product.id = line.product_id |
| 74 | + LEFT JOIN account_account account ON account.id = line.account_id |
| 75 | + LEFT JOIN product_template template ON template.id = product.product_tmpl_id |
| 76 | + LEFT JOIN uom_uom uom_line ON uom_line.id = line.product_uom_id |
| 77 | + LEFT JOIN uom_uom uom_template ON uom_template.id = template.uom_id |
| 78 | + INNER JOIN account_move move ON move.id = line.move_id |
| 79 | + LEFT JOIN res_partner commercial_partner ON commercial_partner.id = move.commercial_partner_id |
| 80 | + lEFT JOIN currency_rate currency_table on |
| 81 | + (currency_table.company_id = line.company_id and |
| 82 | + currency_table.currency_id = line.currency_id and |
| 83 | + currency_table.date_start <= COALESCE(line.date, NOW()) and |
| 84 | + (currency_table.date_end IS NULL OR currency_table.date_end > COALESCE(line.date, NOW()))) |
| 85 | + LEFT JOIN res_company rc on rc.id=line.company_id |
| 86 | + ''' |
0 commit comments