Skip to content

Commit

Permalink
refactor(Timesheet): move validation to Timesheet Detail
Browse files Browse the repository at this point in the history
  • Loading branch information
barredterra committed Oct 29, 2024
1 parent b42e7d4 commit 2a0d509
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 9 deletions.
11 changes: 2 additions & 9 deletions erpnext/projects/doctype/timesheet/timesheet.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,15 +143,8 @@ def on_submit(self):
self.update_task_and_project()

def validate_mandatory_fields(self):
for data in self.time_logs:
if not data.from_time and not data.to_time:
frappe.throw(_("Row {0}: From Time and To Time is mandatory.").format(data.idx))

if not data.activity_type and self.employee:
frappe.throw(_("Row {0}: Activity Type is mandatory.").format(data.idx))

if flt(data.hours) == 0.0:
frappe.throw(_("Row {0}: Hours value must be greater than zero.").format(data.idx))
for time_log in self.time_logs:
time_log.validate_mandatory_fields(needs_activity_type=bool(self.employee))

def update_task_and_project(self):
tasks, projects = [], []
Expand Down
22 changes: 22 additions & 0 deletions erpnext/projects/doctype/timesheet_detail/timesheet_detail.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,3 +123,25 @@ def validate_billing_hours(self):
indicator="orange",
alert=True,
)

def validate_mandatory_fields(self, needs_activity_type: bool = False):
for fieldname in ("from_time", "to_time"):
if not self.get(fieldname):
frappe.throw(
_("Row {0}: Please set {1}.").format(
self.idx,
_(self.meta.get_label(fieldname)),
)
)

if needs_activity_type and not self.activity_type:
frappe.throw(
_("Row {0}: please set {1}.").format(self.idx, _(self.meta.get_label("activity_type")))
)

if flt(self.hours) == 0.0:
frappe.throw(
_("Row {0}: The value of the field {1} cannot be 0.").format(
self.idx, _(self.meta.get_label("hours"))
)
)

0 comments on commit 2a0d509

Please sign in to comment.