Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[16.0] [ADD] project_budget_subcontractor #54

Open
wants to merge 9 commits into
base: 16.0
Choose a base branch
from
Empty file.
1 change: 1 addition & 0 deletions project_budget_subcontractor/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from . import models
26 changes: 26 additions & 0 deletions project_budget_subcontractor/__manifest__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Copyright 2024 Akretion (https://www.akretion.com).
# @author Florian Mounier <florian.mounier@akretion.com>
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).

{
"name": "Project Budget Subcontractor",
"summary": "Add budget to projects",
"version": "16.0.1.0.0",
"development_status": "Alpha",
"category": "Uncategorized",
"website": "https://github.com/akretion/subcontractor",
"author": " Akretion",
"license": "AGPL-3",
"depends": [
"project_invoicing_subcontractor",
"project_timeline",
],
"data": [
"security/ir.model.access.csv",
"views/account_move_view.xml",
"views/res_partner_view.xml",
"views/project_project_view.xml",
"views/project_budget_view.xml",
],
"auto_install": True,
}
5 changes: 5 additions & 0 deletions project_budget_subcontractor/models/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from . import account_move
from . import analytic_account
from . import project
from . import project_budget
from . import res_partner
37 changes: 37 additions & 0 deletions project_budget_subcontractor/models/account_move.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# Copyright 2024 Akretion (http://www.akretion.com).
# @author Florian Mounier <florian.mounier@akretion.com>
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).

from odoo import _, api, fields, models
from odoo.exceptions import UserError


class AccountMove(models.Model):
_inherit = "account.move"

budget_date = fields.Date(
compute="_compute_budget_date",
store=True,
readonly=False,
)
use_budget = fields.Boolean(related="partner_id.use_budget")

@api.depends("date")
def _compute_budget_date(self):
for move in self:
if not move.budget_date:
move.budget_date = move.date

def _post(self, soft=True):
for move in self:
if move.use_budget:
if move.invoice_line_ids.filtered(
lambda line: not line.analytic_account_id
):
raise UserError(
_(
"You can't post a move containing lines without analytic "
"account for a customer with budget enabled."
)
)
return super()._post(soft=soft)
13 changes: 13 additions & 0 deletions project_budget_subcontractor/models/analytic_account.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Copyright 2024 Akretion (http://www.akretion.com).
# @author Florian Mounier <florian.mounier@akretion.com>
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).

from odoo import fields, models


class AccountAnalyticAccount(models.Model):
_inherit = "account.analytic.account"

invoice_line_ids = fields.One2many(
"account.move.line", inverse_name="analytic_account_id", string="Invoice Lines"
)
65 changes: 65 additions & 0 deletions project_budget_subcontractor/models/project.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
# Copyright 2024 Akretion (http://www.akretion.com).
# @author Florian Mounier <florian.mounier@akretion.com>
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).

from odoo import fields, models


class ProjectProject(models.Model):
_inherit = "project.project"

budget_ids = fields.One2many("project.budget", "project_id", string="Budgets")
use_budget = fields.Boolean(related="partner_id.use_budget")

current_budget_id = fields.Many2one(
"project.budget", compute="_compute_current_budget_id"
)
current_budget_amount = fields.Float(
string="Budget Amount", compute="_compute_current_budget_id"
)
current_invoiced_amount = fields.Float(
string="Invoiced Amount", compute="_compute_current_budget_id"
)
current_to_invoice_amount = fields.Float(
string="To Invoice Amount", compute="_compute_current_budget_id"
)
current_remaining_amount = fields.Float(
string="Remaining Amount", compute="_compute_current_budget_id"
)
current_remaining_budget = fields.Float(
string="Remaining Budget", compute="_compute_current_budget_id"
)
current_budget_amount_prorata = fields.Float(
string="Budget Amount Prorata", compute="_compute_current_budget_id"
)
current_budget_progress = fields.Float(
string="Budget Progress", compute="_compute_current_budget_id"
)
current_time_progress = fields.Float(
string="Time Progress", compute="_compute_current_budget_id"
)

def _compute_current_budget_id(self):
for project in self:
current = project.current_budget_id = project.budget_ids.filtered(
lambda b: b.start_date <= fields.Date.today() <= b.end_date
)

if current:
project.current_budget_amount = current.budget_amount
project.current_invoiced_amount = current.invoiced_amount
project.current_to_invoice_amount = current.to_invoice_amount
project.current_remaining_amount = current.remaining_amount
project.current_remaining_budget = current.remaining_budget
project.current_budget_amount_prorata = current.budget_amount_prorata
project.current_budget_progress = current.budget_progress
project.current_time_progress = current.time_progress
else:
project.current_budget_amount = False
project.current_invoiced_amount = False
project.current_to_invoice_amount = False
project.current_remaining_amount = False
project.current_remaining_budget = False
project.current_budget_amount_prorata = False
project.current_budget_progress = False
project.current_time_progress = False
152 changes: 152 additions & 0 deletions project_budget_subcontractor/models/project_budget.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
# Copyright 2024 Akretion (http://www.akretion.com).
# @author Florian Mounier <florian.mounier@akretion.com>
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).

from odoo import api, fields, models


class ProjectBudget(models.Model):
_name = "project.budget"
_description = "Project Budget"
_inherit = ["mail.thread", "mail.activity.mixin"]

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", 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"
)
budget_progress = fields.Float(compute="_compute_budget_progress")
time_progress = fields.Float(compute="_compute_time_progress")

@api.depends("budget_amount", "start_date", "end_date")
def _compute_budget_amount_prorata(self):
today = fields.Date.today()
for budget in self:
if (
not budget.start_date
or not budget.end_date
or budget.start_date > today
):
budget.budget_amount_prorata = 0.0
continue

if budget.end_date < today:
budget.budget_amount_prorata = budget.budget_amount
continue

total_days = (budget.end_date - budget.start_date).days + 1
spent_days = (today - budget.start_date).days + 1
budget.budget_amount_prorata = (
budget.budget_amount * spent_days / total_days
)

@api.depends("project_id.timesheet_ids.invoice_line_id", "start_date", "end_date")
def _compute_invoiced_amount(self):
for budget in self:
if not budget.start_date or not budget.end_date:
budget.invoiced_amount = 0.0
continue
move_lines = (
budget.project_id.analytic_account_id.invoice_line_ids.filtered(
lambda ml, budget=budget: ml.parent_state == "posted"
and ml.move_id.budget_date
and ml.move_id.budget_date >= budget.start_date
and ml.move_id.budget_date <= budget.end_date
)
)
budget.invoiced_amount = sum(move_lines.mapped("price_subtotal"))

@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
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(
"project_id.task_ids", "project_id.price_unit", "start_date", "end_date"
)
def _compute_remaining_amount(self):
for budget in self:
if not budget.start_date or not budget.end_date:
budget.remaining_amount = 0.0
continue

remaining_days = sum(
budget.project_id.task_ids.filtered(
lambda t, budget=budget: t.planned_date_start
and t.planned_date_end
and t.planned_date_start.date() >= budget.start_date
and t.planned_date_end.date() <= budget.end_date
and not t.is_closed
).mapped("remaining_days")
)
budget.remaining_amount = remaining_days * budget.project_id.price_unit

@api.depends("budget_amount", "invoiced_amount", "to_invoice_amount")
def _compute_remaining_budget(self):
for budget in self:
budget.remaining_budget = budget.budget_amount - (
budget.invoiced_amount + budget.to_invoice_amount
)

@api.depends("budget_amount", "invoiced_amount", "to_invoice_amount")
def _compute_budget_progress(self):
for budget in self:
if not budget.budget_amount:
budget.budget_progress = 0.0
continue
budget.budget_progress = (
budget.invoiced_amount + budget.to_invoice_amount
) / budget.budget_amount

@api.depends("start_date", "end_date")
def _compute_time_progress(self):
for budget in self:
if not budget.start_date or not budget.end_date:
budget.time_progress = 0.0
continue
today = fields.Date.today()
total_days = (budget.end_date - budget.start_date).days + 1
spent_days = (today - budget.start_date).days + 1
budget.time_progress = spent_days / total_days
13 changes: 13 additions & 0 deletions project_budget_subcontractor/models/res_partner.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Copyright 2024 Akretion (http://www.akretion.com).
# @author Florian Mounier <florian.mounier@akretion.com>
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).

from odoo import fields, models


class ResPartner(models.Model):
_inherit = "res.partner"

use_budget = fields.Boolean(
help="If checked, this partner's projects will use budgets",
)
3 changes: 3 additions & 0 deletions project_budget_subcontractor/security/ir.model.access.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
id,name,model_id:id,group_id:id,perm_read,perm_write,perm_create,perm_unlink
project_budget_read_access,access_project_budget_read,model_project_budget,project.group_project_user,1,0,0,0
project_budget_all_access,access_project_budget_all,model_project_budget,project.group_project_manager,1,1,1,1
19 changes: 19 additions & 0 deletions project_budget_subcontractor/views/account_move_view.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?xml version="1.0" encoding="utf-8" ?>
<!-- Copyright 2024 Akretion (http://www.akretion.com).
@author Florian Mounier <florian.mounier@akretion.com>
License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). -->
<odoo>
<record id="view_move_form" model="ir.ui.view">
<field name="inherit_id" ref="account.view_move_form" />
<field name="model">account.move</field>
<field type="xml" name="arch">
<field name="invoice_date" position="after">
<field name="use_budget" invisible="1" />
<field
name="budget_date"
attrs="{'invisible': [('use_budget', '=', False)]}"
/>
</field>
</field>
</record>
</odoo>
Loading
Loading