Skip to content

Commit

Permalink
[ADD] project_workload_milestone
Browse files Browse the repository at this point in the history
  • Loading branch information
paradoxxxzero committed Mar 5, 2024
1 parent 7f6053e commit 8fd9284
Show file tree
Hide file tree
Showing 8 changed files with 129 additions and 0 deletions.
1 change: 1 addition & 0 deletions project_workload_milestone/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from . import models
22 changes: 22 additions & 0 deletions project_workload_milestone/__manifest__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Copyright 2024 Akretion (https://www.akretion.com).
# @author Florian Mounier <[email protected]>
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).

{
"name": "Project Workload Milestone",
"summary": "Add Milestone Support to Project Workload",
"version": "14.0.1.0.0",
"development_status": "Alpha",
"category": "Uncategorized",
"website": "https://github.com/akretion/ak-odoo-incubator",
"author": " Akretion",
"license": "AGPL-3",
"depends": [
"project_workload",
"project_milestone",
],
"data": [
"views/project_milestone.xml",
],
"auto_install": True,
}
2 changes: 2 additions & 0 deletions project_workload_milestone/models/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
from . import project_milestone
from . import project_task
49 changes: 49 additions & 0 deletions project_workload_milestone/models/project_milestone.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# Copyright 2024 Akretion (http://www.akretion.com).
# @author Florian Mounier <[email protected]>
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
from datetime import timedelta

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


class ProjectMilestone(models.Model):
_inherit = "project.milestone"

start_date = fields.Date(
string="Start Date",
help="The date when the Milestone should start.",
compute="_compute_milestone_start_date",
store=True,
readonly=False,
)

@api.constrains("start_date", "target_date")
def _check_start_date(self):
for milestone in self:
if milestone.target_date < milestone.start_date:
raise ValidationError(
_("The end date cannot be earlier than the start date.")
)

@api.depends("target_date", "project_id")
def _compute_milestone_start_date(self):
for record in self:
if record.start_date:
continue
if not record.target_date:
record.start_date = False
continue

# Use a simple algorithm to find the start date
# Find previous milestone
previous_milestones = self.search(
[
("project_id", "=", record.project_id.id),
("target_date", "<", record.target_date),
],
order="target_date desc",
limit=1,
)
# The start date will be the end date of the previous milestone
record.start_date = previous_milestones.target_date + timedelta(days=1)
27 changes: 27 additions & 0 deletions project_workload_milestone/models/project_task.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Copyright 2024 Akretion (http://www.akretion.com).
# @author Florian Mounier <[email protected]>
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).

from odoo import api, models


class ProjectTask(models.Model):
_inherit = "project.task"

def _prepare_task_dates_vals_from_milestone(self, vals):
# If we create a task with a milestone or affect it after,
# we set the task dates to the milestone dates

if "milestone_id" in vals:
milestone = self.env["project.milestone"].browse(vals["milestone_id"])
if milestone:
vals["date_end"] = milestone.target_date
vals["date_start"] = milestone.start_date
return vals

@api.model
def create(self, vals):
return super().create(self._prepare_task_dates_vals_from_milestone(vals))

def write(self, vals):
return super().write(self._prepare_task_dates_vals_from_milestone(vals))
21 changes: 21 additions & 0 deletions project_workload_milestone/views/project_milestone.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?xml version="1.0" encoding="utf-8" ?>
<!-- Copyright 2024 Akretion (http://www.akretion.com).
@author Florian Mounier <[email protected]>
License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). -->
<odoo>

<record id="project_milestone_view_form" model="ir.ui.view">
<field name="model">project.milestone</field>
<field name="inherit_id" ref="project_milestone.project_milestone_view_form" />
<field name="arch" type="xml">
<field name="project_id" position="replace" />
<field name="target_date" position="before">
<field name="project_id" />
</field>
<field name="target_date" position="after">
<field name="start_date" />
</field>
</field>
</record>

</odoo>
6 changes: 6 additions & 0 deletions setup/project_workload_milestone/setup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import setuptools

setuptools.setup(
setup_requires=['setuptools-odoo'],
odoo_addon=True,
)

0 comments on commit 8fd9284

Please sign in to comment.