diff --git a/erpnext/projects/doctype/timesheet/test_timesheet.py b/erpnext/projects/doctype/timesheet/test_timesheet.py index e098c3e3c45..828a55e7bc1 100644 --- a/erpnext/projects/doctype/timesheet/test_timesheet.py +++ b/erpnext/projects/doctype/timesheet/test_timesheet.py @@ -161,6 +161,37 @@ class TestTimesheet(unittest.TestCase): to_time = timesheet.time_logs[0].to_time self.assertEqual(to_time, add_to_date(from_time, hours=2, as_datetime=True)) + def test_per_billed_hours(self): + """If amounts are 0, per_billed should be calculated based on hours.""" + ts = frappe.new_doc("Timesheet") + ts.total_billable_amount = 0 + ts.total_billed_amount = 0 + ts.total_billable_hours = 2 + + ts.total_billed_hours = 0.5 + ts.calculate_percentage_billed() + self.assertEqual(ts.per_billed, 25) + + ts.total_billed_hours = 2 + ts.calculate_percentage_billed() + self.assertEqual(ts.per_billed, 100) + + def test_per_billed_amount(self): + """If amounts are > 0, per_billed should be calculated based on amounts, regardless of hours.""" + ts = frappe.new_doc("Timesheet") + ts.total_billable_hours = 2 + ts.total_billed_hours = 1 + ts.total_billable_amount = 200 + ts.total_billed_amount = 50 + ts.calculate_percentage_billed() + self.assertEqual(ts.per_billed, 25) + + ts.total_billed_hours = 3 + ts.total_billable_amount = 200 + ts.total_billed_amount = 200 + ts.calculate_percentage_billed() + self.assertEqual(ts.per_billed, 100) + def make_timesheet( employee, diff --git a/erpnext/projects/doctype/timesheet/timesheet.py b/erpnext/projects/doctype/timesheet/timesheet.py index b9bb37a05cf..4a27807e4ec 100644 --- a/erpnext/projects/doctype/timesheet/timesheet.py +++ b/erpnext/projects/doctype/timesheet/timesheet.py @@ -58,6 +58,8 @@ class Timesheet(Document): self.per_billed = 0 if self.total_billed_amount > 0 and self.total_billable_amount > 0: self.per_billed = (self.total_billed_amount * 100) / self.total_billable_amount + elif self.total_billed_hours > 0 and self.total_billable_hours > 0: + self.per_billed = (self.total_billed_hours * 100) / self.total_billable_hours def update_billing_hours(self, args): if args.is_billable: