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

refactor: validate dates in project and task #33217

Merged
merged 3 commits into from
Dec 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions erpnext/projects/doctype/project/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ def validate(self):
self.send_welcome_email()
self.update_costing()
self.update_percent_complete()
self.validate_from_to_dates("expected_start_date", "expected_end_date")
self.validate_from_to_dates("actual_start_date", "actual_end_date")

def copy_from_template(self):
"""
Expand Down
84 changes: 29 additions & 55 deletions erpnext/projects/doctype/task/task.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,14 @@
from frappe.desk.form.assign_to import clear, close_all_assignments
from frappe.model.mapper import get_mapped_doc
from frappe.utils import add_days, cstr, date_diff, flt, get_link_to_form, getdate, today
from frappe.utils.data import format_date
from frappe.utils.nestedset import NestedSet


class CircularReferenceError(frappe.ValidationError):
pass


class EndDateCannotBeGreaterThanProjectEndDateError(frappe.ValidationError):
pass


class Task(NestedSet):
nsm_parent_field = "parent_task"

Expand All @@ -34,60 +31,49 @@ def get_customer_details(self):

def validate(self):
self.validate_dates()
self.validate_parent_expected_end_date()
self.validate_parent_project_dates()
self.validate_progress()
self.validate_status()
self.update_depends_on()
self.validate_dependencies_for_template_task()
self.validate_completed_on()

def validate_dates(self):
if (
self.exp_start_date
and self.exp_end_date
and getdate(self.exp_start_date) > getdate(self.exp_end_date)
):
frappe.throw(
_("{0} can not be greater than {1}").format(
frappe.bold("Expected Start Date"), frappe.bold("Expected End Date")
)
)
self.validate_from_to_dates("exp_start_date", "exp_end_date")
self.validate_from_to_dates("act_start_date", "act_end_date")
self.validate_parent_expected_end_date()
self.validate_parent_project_dates()

def validate_parent_expected_end_date(self):
if not self.parent_task or not self.exp_end_date:
return

parent_exp_end_date = frappe.db.get_value("Task", self.parent_task, "exp_end_date")
if not parent_exp_end_date:
return

if (
self.act_start_date
and self.act_end_date
and getdate(self.act_start_date) > getdate(self.act_end_date)
):
if getdate(self.exp_end_date) > getdate(parent_exp_end_date):
frappe.throw(
_("{0} can not be greater than {1}").format(
frappe.bold("Actual Start Date"), frappe.bold("Actual End Date")
)
_(
"Expected End Date should be less than or equal to parent task's Expected End Date {0}."
).format(format_date(parent_exp_end_date)),
frappe.exceptions.InvalidDates,
)

def validate_parent_expected_end_date(self):
if self.parent_task:
parent_exp_end_date = frappe.db.get_value("Task", self.parent_task, "exp_end_date")
if parent_exp_end_date and getdate(self.get("exp_end_date")) > getdate(parent_exp_end_date):
frappe.throw(
_(
"Expected End Date should be less than or equal to parent task's Expected End Date {0}."
).format(getdate(parent_exp_end_date))
)

def validate_parent_project_dates(self):
if not self.project or frappe.flags.in_test:
return

expected_end_date = frappe.db.get_value("Project", self.project, "expected_end_date")

if expected_end_date:
validate_project_dates(
getdate(expected_end_date), self, "exp_start_date", "exp_end_date", "Expected"
)
validate_project_dates(
getdate(expected_end_date), self, "act_start_date", "act_end_date", "Actual"
)
if project_end_date := frappe.db.get_value("Project", self.project, "expected_end_date"):
project_end_date = getdate(project_end_date)
for fieldname in ("exp_start_date", "exp_end_date", "act_start_date", "act_end_date"):
task_date = self.get(fieldname)
if task_date and date_diff(project_end_date, getdate(task_date)) < 0:
frappe.throw(
_("Task's {0} cannot be after Project's Expected End Date.").format(
_(self.meta.get_label(fieldname))
),
frappe.exceptions.InvalidDates,
)

def validate_status(self):
if self.is_template and self.status != "Template":
Expand Down Expand Up @@ -398,15 +384,3 @@ def add_multiple_tasks(data, parent):

def on_doctype_update():
frappe.db.add_index("Task", ["lft", "rgt"])


def validate_project_dates(project_end_date, task, task_start, task_end, actual_or_expected_date):
if task.get(task_start) and date_diff(project_end_date, getdate(task.get(task_start))) < 0:
frappe.throw(
_("Task's {0} Start Date cannot be after Project's End Date.").format(actual_or_expected_date)
)

if task.get(task_end) and date_diff(project_end_date, getdate(task.get(task_end))) < 0:
frappe.throw(
_("Task's {0} End Date cannot be after Project's End Date.").format(actual_or_expected_date)
)