|
| 1 | +# Copyright (c) 2022 Open Source Integrators |
| 2 | +# License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl). |
| 3 | +from odoo import fields, models |
| 4 | + |
| 5 | + |
| 6 | +class CrossoveredBudgetLines(models.Model): |
| 7 | + _inherit = "crossovered.budget.lines" |
| 8 | + |
| 9 | + committed_amount = fields.Float( |
| 10 | + compute="_compute_committ_uncommitt_amount", string="Committed Amount" |
| 11 | + ) |
| 12 | + uncommitted_amount = fields.Float( |
| 13 | + compute="_compute_committ_uncommitt_amount", string="Uncommitted Amount" |
| 14 | + ) |
| 15 | + over_budget = fields.Boolean(compute="_compute_over_budget", string="Over Budget") |
| 16 | + |
| 17 | + def _compute_committ_uncommitt_amount(self): |
| 18 | + purchase_line_obj = self.env["purchase.order.line"] |
| 19 | + for budget_line in self: |
| 20 | + commit_domain = [ |
| 21 | + ("date_order", ">=", budget_line.date_from), |
| 22 | + ("date_order", "<=", budget_line.date_to), |
| 23 | + ("order_id.state", "in", ("purchase", "done")), |
| 24 | + "|", |
| 25 | + ("account_analytic_id", "=", budget_line.analytic_account_id.id), |
| 26 | + ("analytic_tag_ids", "in", budget_line.analytic_tag_id.ids), |
| 27 | + "|", |
| 28 | + ( |
| 29 | + "product_id.property_account_expense_id", |
| 30 | + "in", |
| 31 | + budget_line.general_budget_id.account_ids.ids, |
| 32 | + ), |
| 33 | + ( |
| 34 | + "product_id.categ_id.property_account_expense_categ_id", |
| 35 | + "in", |
| 36 | + budget_line.general_budget_id.account_ids.ids, |
| 37 | + ), |
| 38 | + ] |
| 39 | + po_lines_commimt = purchase_line_obj.search(commit_domain) |
| 40 | + if budget_line.crossovered_budget_id.amount_include_tax: |
| 41 | + budget_line.committed_amount = -sum( |
| 42 | + [ |
| 43 | + line.price_unit * (line.product_qty - line.qty_invoiced) |
| 44 | + + line.price_tax |
| 45 | + for line in po_lines_commimt |
| 46 | + ] |
| 47 | + ) |
| 48 | + else: |
| 49 | + budget_line.committed_amount = -sum( |
| 50 | + [ |
| 51 | + line.price_unit * (line.product_qty - line.qty_invoiced) |
| 52 | + for line in po_lines_commimt |
| 53 | + ] |
| 54 | + ) |
| 55 | + uncommit_domain = [ |
| 56 | + ("date_order", ">=", budget_line.date_from), |
| 57 | + ("date_order", "<=", budget_line.date_to), |
| 58 | + ("order_id.state", "not in", ("purchase", "done", "cancel")), |
| 59 | + "|", |
| 60 | + ("account_analytic_id", "=", budget_line.analytic_account_id.id), |
| 61 | + ("analytic_tag_ids", "in", budget_line.analytic_tag_id.ids), |
| 62 | + "|", |
| 63 | + ( |
| 64 | + "product_id.property_account_expense_id", |
| 65 | + "in", |
| 66 | + budget_line.general_budget_id.account_ids.ids, |
| 67 | + ), |
| 68 | + ( |
| 69 | + "product_id.categ_id.property_account_expense_categ_id", |
| 70 | + "in", |
| 71 | + budget_line.general_budget_id.account_ids.ids, |
| 72 | + ), |
| 73 | + ] |
| 74 | + po_lines_uncommit = purchase_line_obj.search(uncommit_domain) |
| 75 | + if budget_line.crossovered_budget_id.amount_include_tax: |
| 76 | + budget_line.uncommitted_amount = -sum( |
| 77 | + [line.price_subtotal + line.price_tax for line in po_lines_uncommit] |
| 78 | + ) |
| 79 | + else: |
| 80 | + budget_line.uncommitted_amount = -sum( |
| 81 | + [line.price_subtotal for line in po_lines_uncommit] |
| 82 | + ) |
| 83 | + |
| 84 | + def _compute_over_budget(self): |
| 85 | + for rec in self: |
| 86 | + rec.over_budget = False |
| 87 | + if ( |
| 88 | + abs(rec.practical_amount) |
| 89 | + + abs(rec.committed_amount) |
| 90 | + + abs(rec.uncommitted_amount) |
| 91 | + ) > abs(rec.planned_amount): |
| 92 | + rec.over_budget = True |
0 commit comments