Skip to content

Commit

Permalink
project_budget_subcontractor: improve/fix budget
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastienbeau committed Jul 9, 2024
1 parent 27a3fc5 commit 25639be
Show file tree
Hide file tree
Showing 3 changed files with 114 additions and 16 deletions.
1 change: 1 addition & 0 deletions project_budget_subcontractor/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"views/account_move_view.xml",
"views/res_partner_view.xml",
"views/project_project_view.xml",
"views/project_budget_view.xml",
],
"auto_install": True,
}
47 changes: 31 additions & 16 deletions project_budget_subcontractor/models/project_budget.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,28 @@ class ProjectBudget(models.Model):

name = fields.Char(required=True)
project_id = fields.Many2one("project.project", required=True, tracking=True)
partner_id = fields.Many2one(
"res.partner", related="project_id.partner_id", store=True
)
start_date = fields.Date(required=True, tracking=True)
end_date = fields.Date(required=True, tracking=True)
currency_id = fields.Many2one(
"res.currency", default=lambda self: self.env.company.currency_id
)
budget_amount = fields.Float(required=True, tracking=True)

invoiced_amount = fields.Float(compute="_compute_invoiced_amount")
to_invoice_amount = fields.Float(compute="_compute_to_invoice_amount")
remaining_amount = fields.Float(compute="_compute_remaining_amount")
remaining_budget = fields.Float(compute="_compute_remaining_budget")
invoiced_amount = fields.Float(
compute="_compute_invoiced_amount", string="Montant Facturé"
)
to_invoice_amount = fields.Float(
compute="_compute_to_invoice_amount", string="Montant à Facturer"
)
remaining_amount = fields.Float(
compute="_compute_remaining_amount", string="Montant Restant Estimé"
)
remaining_budget = fields.Float(
compute="_compute_remaining_budget", string="Budget Restant"
)
budget_amount_prorata = fields.Float(
compute="_compute_budget_amount_prorata", string="Time Prorata"
)
Expand Down Expand Up @@ -69,22 +80,26 @@ def _compute_invoiced_amount(self):
@api.depends("project_id.timesheet_ids", "start_date", "end_date")
def _compute_to_invoice_amount(self):
today = fields.Date.today()
data = self.env["account.analytic.line"].read_group(
[
("project_id", "in", self.project_id.ids),
"|",
("invoice_line_id", "=", False),
("invoice_line_id.parent_state", "=", "draft"),
],
["invoiceable_amount:sum"],
["project_id"],
)
p2hours = {item["project_id"][0]: item["invoiceable_amount"] for item in data}
for budget in self:
if not budget.start_date or not budget.end_date or budget.end_date < today:
budget.to_invoice_amount = 0.0
continue
account_analytic_line = budget.project_id.timesheet_ids.filtered(
lambda aal: (
not aal.invoice_line_id
or aal.invoice_line_id.parent_state == "draft"
)
)
budget.to_invoice_amount = -sum(
budget.project_id.convert_hours_to_days(
aal.unit_amount * (1 - aal.discount / 100.0)
)
* aal.amount
for aal in account_analytic_line
project = budget.project_id
if isinstance(project.id, models.NewId):
project = project._origin
budget.to_invoice_amount = (
project.convert_hours_to_days(p2hours[project.id]) * project.price_unit
)

@api.depends(
Expand Down
82 changes: 82 additions & 0 deletions project_budget_subcontractor/views/project_budget_view.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
<?xml version="1.0" encoding="UTF-8" ?>
<odoo>
<record id="project_budget_view_tree" model="ir.ui.view">
<field name="model">project.budget</field>
<field name="arch" type="xml">
<tree create="0" edit="0">
<field name="partner_id" />
<field name="project_id" />
<field name="name" />
<field name="start_date" />
<field name="end_date" />
<field
name="budget_amount"
widget="monetary"
options="{'currency_field': 'currency_id'}"
/>
<field
name="invoiced_amount"
widget="monetary"
options="{'currency_field': 'currency_id'}"
/>
<field
name="to_invoice_amount"
widget="monetary"
options="{'currency_field': 'currency_id'}"
/>
<field
name="remaining_amount"
widget="monetary"
options="{'currency_field': 'currency_id'}"
/>
<field
name="remaining_budget"
widget="monetary"
options="{'currency_field': 'currency_id'}"
/>
</tree>
</field>
</record>

<record id="project_budget_view_search" model="ir.ui.view">
<field name="model">project.budget</field>
<field name="arch" type="xml">
<search string="Budget">
<field name="project_id" />
<filter
name="current"
domain="[
('start_date', '&lt;=', context_today().strftime('%Y-%m-%d')),
('end_date', '&gt;=', context_today().strftime('%Y-%m-%d')),
]"
/>
<group expand="0" string="Group By">
<filter
string="Customer"
name="customer"
domain="[]"
context="{'group_by': 'partner_id'}"
/>
</group>
</search>
</field>
</record>

<record model="ir.actions.act_window" id="project_budget_action">
<field name="name">Budget</field>
<field name="type">ir.actions.act_window</field>
<field name="res_model">project.budget</field>
<field name="view_mode">tree</field>
<field name="search_view_id" ref="project_budget_view_search" />
<field name="domain">[]</field>
<field name="context">{"search_default_current": 1}</field>
</record>

<menuitem
id="project_budget_menu"
parent="project.menu_main_pm"
sequence="20"
action="project_budget_action"
/>

</odoo>

0 comments on commit 25639be

Please sign in to comment.