|
| 1 | +# Copyright 2026 Innovara Ltd - Manuel Fombuena |
| 2 | +# License AGPL-3 - See http://www.gnu.org/licenses/agpl-3.0.html |
| 3 | + |
| 4 | +from psycopg2 import sql |
| 5 | + |
| 6 | +from odoo import fields, models, tools |
| 7 | + |
| 8 | + |
| 9 | +class SaleTimesheetCostsRevenues(models.Model): |
| 10 | + """Read-only reporting model backed by a SQL view over timesheet |
| 11 | + (analytic) lines, enriched with sale-order and project data. One row per |
| 12 | + timesheet line; the timesheet date drives the period grouping.""" |
| 13 | + |
| 14 | + _name = "sale.timesheet.costs.revenues" |
| 15 | + _description = "Timesheet Costs and Revenues" |
| 16 | + _auto = False # this model is a database VIEW, not a table |
| 17 | + _rec_name = "project_id" |
| 18 | + _order = "date desc" |
| 19 | + |
| 20 | + # --- grouping dimensions (use as pivot rows/columns) --- |
| 21 | + project_id = fields.Many2one("project.project", readonly=True) |
| 22 | + account_id = fields.Many2one( |
| 23 | + "account.analytic.account", string="Analytic Account", readonly=True |
| 24 | + ) |
| 25 | + partner_id = fields.Many2one("res.partner", string="Customer", readonly=True) |
| 26 | + project_user_id = fields.Many2one( |
| 27 | + "res.users", string="Project Manager", readonly=True |
| 28 | + ) |
| 29 | + company_id = fields.Many2one("res.company", string="Project Company", readonly=True) |
| 30 | + currency_id = fields.Many2one( |
| 31 | + "res.currency", string="Project Currency", readonly=True |
| 32 | + ) |
| 33 | + product_id = fields.Many2one("product.product", readonly=True) |
| 34 | + sale_order_id = fields.Many2one("sale.order", readonly=True) |
| 35 | + so_line_id = fields.Many2one( |
| 36 | + "sale.order.line", string="Sale Order Line", readonly=True |
| 37 | + ) |
| 38 | + date = fields.Date(readonly=True) |
| 39 | + so_confirmation_date = fields.Datetime( |
| 40 | + string="Sales Order Confirmation Date", readonly=True |
| 41 | + ) |
| 42 | + # extras not present in the original report, handy for drill-down: |
| 43 | + task_id = fields.Many2one("project.task", readonly=True) |
| 44 | + employee_id = fields.Many2one("hr.employee", readonly=True) |
| 45 | + |
| 46 | + # --- measures --- |
| 47 | + timesheet_duration = fields.Float(readonly=True) |
| 48 | + timesheet_cost = fields.Monetary(readonly=True, currency_field="currency_id") |
| 49 | + amount_to_invoice = fields.Monetary( |
| 50 | + string="Untaxed Amount to Invoice", readonly=True, currency_field="currency_id" |
| 51 | + ) |
| 52 | + amount_invoiced = fields.Monetary( |
| 53 | + string="Untaxed Amount Invoiced", readonly=True, currency_field="currency_id" |
| 54 | + ) |
| 55 | + |
| 56 | + def init(self): |
| 57 | + # The analytic-account column on account.analytic.line is named |
| 58 | + # `account_id`; guard it so the view still builds if a given install |
| 59 | + # has reworked analytic plans and renamed/removed it. |
| 60 | + self.env.cr.execute( |
| 61 | + """ |
| 62 | + SELECT 1 FROM information_schema.columns |
| 63 | + WHERE table_name = 'account_analytic_line' |
| 64 | + AND column_name = 'account_id' |
| 65 | + """ |
| 66 | + ) |
| 67 | + account_col = "aal.account_id" if self.env.cr.fetchone() else "NULL::integer" |
| 68 | + |
| 69 | + tools.drop_view_if_exists(self.env.cr, self._table) |
| 70 | + # Notes on the billable value: |
| 71 | + # - Only sale order lines billed on delivered timesheets count as |
| 72 | + # revenue (qty_delivered_method in timesheet/manual). Fixed-price and |
| 73 | + # milestone lines still contribute cost and hours, but no revenue, |
| 74 | + # since hours x price is meaningless for them. |
| 75 | + # - Timesheet hours (in the timesheet encoding UoM) are converted into |
| 76 | + # the sale order line's UoM before multiplying by its price, so a |
| 77 | + # per-day rate billed against hour-encoded timesheets is correct. |
| 78 | + # Conversion only applies within a UoM category; otherwise the raw |
| 79 | + # duration is used. |
| 80 | + # - Identifiers are composed with psycopg2.sql so the dynamic table |
| 81 | + # name and the (code-chosen) analytic-account column cannot be a |
| 82 | + # SQL-injection vector. |
| 83 | + self.env.cr.execute( |
| 84 | + sql.SQL( |
| 85 | + """ |
| 86 | + CREATE OR REPLACE VIEW {table} AS ( |
| 87 | + SELECT |
| 88 | + aal.id AS id, |
| 89 | + aal.project_id AS project_id, |
| 90 | + {account_col} AS account_id, |
| 91 | + p.partner_id AS partner_id, |
| 92 | + p.user_id AS project_user_id, |
| 93 | + p.company_id AS company_id, |
| 94 | + c.currency_id AS currency_id, |
| 95 | + sol.product_id AS product_id, |
| 96 | + sol.order_id AS sale_order_id, |
| 97 | + aal.so_line AS so_line_id, |
| 98 | + aal.task_id AS task_id, |
| 99 | + aal.employee_id AS employee_id, |
| 100 | + aal.date AS date, |
| 101 | + so.date_order AS so_confirmation_date, |
| 102 | + aal.unit_amount AS timesheet_duration, |
| 103 | + aal.amount AS timesheet_cost, |
| 104 | + CASE |
| 105 | + WHEN aal.timesheet_invoice_id IS NULL |
| 106 | + AND aal.so_line IS NOT NULL |
| 107 | + AND sol.qty_delivered_method IN ('timesheet', 'manual') |
| 108 | + THEN aal.unit_amount |
| 109 | + * CASE |
| 110 | + WHEN ts_uom.category_id = sol_uom.category_id |
| 111 | + AND ts_uom.factor <> 0 |
| 112 | + THEN sol_uom.factor / ts_uom.factor |
| 113 | + ELSE 1.0 |
| 114 | + END |
| 115 | + * sol.price_unit |
| 116 | + * (1.0 - COALESCE(sol.discount, 0.0) / 100.0) |
| 117 | + ELSE 0.0 |
| 118 | + END AS amount_to_invoice, |
| 119 | + CASE |
| 120 | + WHEN aal.timesheet_invoice_id IS NOT NULL |
| 121 | + AND aal.so_line IS NOT NULL |
| 122 | + AND sol.qty_delivered_method IN ('timesheet', 'manual') |
| 123 | + THEN aal.unit_amount |
| 124 | + * CASE |
| 125 | + WHEN ts_uom.category_id = sol_uom.category_id |
| 126 | + AND ts_uom.factor <> 0 |
| 127 | + THEN sol_uom.factor / ts_uom.factor |
| 128 | + ELSE 1.0 |
| 129 | + END |
| 130 | + * sol.price_unit |
| 131 | + * (1.0 - COALESCE(sol.discount, 0.0) / 100.0) |
| 132 | + ELSE 0.0 |
| 133 | + END AS amount_invoiced |
| 134 | + FROM account_analytic_line aal |
| 135 | + JOIN project_project p |
| 136 | + ON p.id = aal.project_id |
| 137 | + LEFT JOIN res_company c |
| 138 | + ON c.id = COALESCE(p.company_id, aal.company_id) |
| 139 | + LEFT JOIN sale_order_line sol |
| 140 | + ON sol.id = aal.so_line |
| 141 | + LEFT JOIN sale_order so |
| 142 | + ON so.id = sol.order_id |
| 143 | + LEFT JOIN uom_uom ts_uom |
| 144 | + ON ts_uom.id = aal.product_uom_id |
| 145 | + LEFT JOIN uom_uom sol_uom |
| 146 | + ON sol_uom.id = sol.product_uom |
| 147 | + WHERE aal.project_id IS NOT NULL |
| 148 | + ) |
| 149 | + """ |
| 150 | + ).format( |
| 151 | + table=sql.Identifier(self._table), |
| 152 | + account_col=sql.SQL(account_col), |
| 153 | + ) |
| 154 | + ) |
0 commit comments