From f5ed0c83b7bae03e657b7a6920bd8a8b490d3917 Mon Sep 17 00:00:00 2001 From: Himanshu Date: Fri, 21 Jan 2022 13:05:47 +0530 Subject: [PATCH] fix(Timesheets): calculate to time (#28589) Co-authored-by: Ankush Menat Co-authored-by: barredterra <14891507+barredterra@users.noreply.github.com> --- .../doctype/timesheet/test_timesheet.py | 23 ++++++++++++++++++- .../projects/doctype/timesheet/timesheet.py | 11 ++++++++- 2 files changed, 32 insertions(+), 2 deletions(-) diff --git a/erpnext/projects/doctype/timesheet/test_timesheet.py b/erpnext/projects/doctype/timesheet/test_timesheet.py index 148d8ba29c..989bcd1670 100644 --- a/erpnext/projects/doctype/timesheet/test_timesheet.py +++ b/erpnext/projects/doctype/timesheet/test_timesheet.py @@ -5,7 +5,7 @@ import datetime import unittest import frappe -from frappe.utils import add_months, now_datetime, nowdate +from frappe.utils import add_months, add_to_date, now_datetime, nowdate from erpnext.accounts.doctype.sales_invoice.test_sales_invoice import create_sales_invoice from erpnext.hr.doctype.employee.test_employee import make_employee @@ -151,6 +151,27 @@ class TestTimesheet(unittest.TestCase): settings.ignore_employee_time_overlap = initial_setting settings.save() + def test_to_time(self): + emp = make_employee("test_employee_6@salary.com") + from_time = now_datetime() + + timesheet = frappe.new_doc("Timesheet") + timesheet.employee = emp + timesheet.append( + 'time_logs', + { + "billable": 1, + "activity_type": "_Test Activity Type", + "from_time": from_time, + "hours": 2, + "company": "_Test Company" + } + ) + timesheet.save() + + to_time = timesheet.time_logs[0].to_time + self.assertEqual(to_time, add_to_date(from_time, hours=2, as_datetime=True)) + def make_salary_structure_for_timesheet(employee, company=None): salary_structure_name = "Timesheet Salary Structure Test" diff --git a/erpnext/projects/doctype/timesheet/timesheet.py b/erpnext/projects/doctype/timesheet/timesheet.py index e92785e06c..dd0b5f90f4 100644 --- a/erpnext/projects/doctype/timesheet/timesheet.py +++ b/erpnext/projects/doctype/timesheet/timesheet.py @@ -7,7 +7,7 @@ import json import frappe from frappe import _ from frappe.model.document import Document -from frappe.utils import flt, getdate, time_diff_in_hours +from frappe.utils import add_to_date, flt, getdate, time_diff_in_hours from erpnext.controllers.queries import get_match_cond from erpnext.hr.utils import validate_active_employee @@ -136,10 +136,19 @@ class Timesheet(Document): def validate_time_logs(self): for data in self.get('time_logs'): + self.set_to_time(data) self.validate_overlap(data) self.set_project(data) self.validate_project(data) + def set_to_time(self, data): + if not (data.from_time and data.hours): + return + + _to_time = add_to_date(data.from_time, hours=data.hours, as_datetime=True) + if data.to_time != _to_time: + data.to_time = _to_time + def validate_overlap(self, data): settings = frappe.get_single('Projects Settings') self.validate_overlap_for("user", data, self.user, settings.ignore_user_time_overlap)