diff --git a/hrms/hr/doctype/employee_checkin/test_employee_checkin.py b/hrms/hr/doctype/employee_checkin/test_employee_checkin.py index 95fa05d0f8f..eb75512d4a8 100644 --- a/hrms/hr/doctype/employee_checkin/test_employee_checkin.py +++ b/hrms/hr/doctype/employee_checkin/test_employee_checkin.py @@ -868,6 +868,82 @@ def test_half_day_attendance_when_employee_checkins_exists_and_attendance_is_ful self.assertEqual(attendance[0].in_time, in_log.time) self.assertEqual(attendance[0].out_time, out_log.time) + def test_half_day_attendance_not_closed_before_shift_actually_ends(self): + today = getdate() + shift = setup_shift_type( + shift_type="_Test Half Day Premature Close", + start_time="08:00:00", + end_time="12:00:00", + allow_check_out_after_shift_end_time=630, + working_hours_threshold_for_half_day=3, + last_sync_of_checkin=datetime.combine(today, get_time("15:00:00")), + ) + emp = make_employee("testhalfday5@example.com", company="_Test Company", default_shift=shift.name) + employee = frappe.get_doc("Employee", emp) + # create attendance from leave + leave_type = create_leave_type(leave_type_name="_Test Half Day", include_holidays=0) + create_leave_allocation( + employee=employee, + leave_type=leave_type, + from_date=add_days(today, -2), + to_date=add_days(today, 30), + new_leaves_allocated=15, + ) + make_leave_application( + leave_type=leave_type.name, + employee=emp, + from_date=today, + to_date=today, + half_day=1, + half_day_date=today, + ) + + in_time = datetime.combine(today, get_time("08:00:00")) + out_time = datetime.combine(today, get_time("10:00:00")) + in_log = make_checkin(emp, in_time) + out_log = make_checkin(emp, out_time) + + attendance_filters = {"leave_type": leave_type.name, "employee": emp, "attendance_date": today} + attendance_fields = [ + "name", + "status", + "half_day_status", + "shift", + "working_hours", + "in_time", + "out_time", + "modify_half_day_status", + ] + + # sync before shift end: checkins aren't processed yet, half day shouldn't be force-closed + shift.process_auto_attendance() + + attendance = frappe.get_all("Attendance", filters=attendance_filters, fields=attendance_fields) + self.assertEqual(len(attendance), 1) + self.assertEqual(attendance[0].status, "Half Day") + self.assertEqual(attendance[0].half_day_status, "Present") + self.assertEqual(attendance[0].modify_half_day_status, 1) + + # sync after shift end: checkins should update the same attendance, not raise DuplicateAttendanceError + shift.last_sync_of_checkin = datetime.combine(add_days(today, 1), get_time("01:00:00")) + shift.save() + shift.process_auto_attendance() + + attendance = frappe.get_all("Attendance", filters=attendance_filters, fields=attendance_fields) + self.assertEqual(len(attendance), 1) + self.assertEqual(attendance[0].status, "Half Day") + self.assertEqual(attendance[0].half_day_status, "Present") + self.assertEqual(attendance[0].shift, shift.name) + self.assertEqual(attendance[0].modify_half_day_status, 0) + self.assertEqual(attendance[0].working_hours, 2) + self.assertEqual(attendance[0].in_time, in_log.time) + self.assertEqual(attendance[0].out_time, out_log.time) + + in_log.reload() + out_log.reload() + self.assertEqual(in_log.attendance, attendance[0].name) + self.assertEqual(out_log.attendance, attendance[0].name) + def make_n_checkins(employee, n, hours_to_reverse=1): logs = [make_checkin(employee, now_datetime() - timedelta(hours=hours_to_reverse, minutes=n + 1))] diff --git a/hrms/hr/doctype/goal/goal.py b/hrms/hr/doctype/goal/goal.py index 01ae30227e4..f21345c83c0 100644 --- a/hrms/hr/doctype/goal/goal.py +++ b/hrms/hr/doctype/goal/goal.py @@ -6,7 +6,7 @@ import frappe from frappe import _ from frappe.query_builder.functions import Avg -from frappe.utils import cint, flt +from frappe.utils import cint, flt, has_common from frappe.utils.nestedset import NestedSet from hrms.hr.doctype.appraisal_cycle.appraisal_cycle import validate_active_appraisal_cycle @@ -225,9 +225,26 @@ def _update_goal_completion_status(goals: list[dict]) -> list[dict]: return goals +HR_ROLES = ("System Manager", "HR Manager", "HR User") + + +def validate_goal_access(employee: str) -> None: + if has_common(HR_ROLES, frappe.get_roles()): + return + + is_own_goal = frappe.db.get_value("Employee", employee, "user_id") == frappe.session.user + if not is_own_goal: + frappe.throw( + _("You are not permitted to update another employee's goal."), + frappe.PermissionError, + title=_("Not Permitted"), + ) + + @frappe.whitelist(methods=["POST"]) def update_progress(progress: float, goal: str) -> None: goal = frappe.get_doc("Goal", goal) + validate_goal_access(goal.employee) goal.progress = progress goal.flags.ignore_mandatory = True goal.save() @@ -244,6 +261,7 @@ def update_status(status: str, goals: str | list) -> None: for goal in goals: goal = frappe.get_doc("Goal", goal) + validate_goal_access(goal.employee) goal.status = status if status == "Completed": goal.progress = 100 diff --git a/hrms/hr/doctype/leave_application/leave_application.py b/hrms/hr/doctype/leave_application/leave_application.py index 63a1eb5a5a3..fabec224670 100755 --- a/hrms/hr/doctype/leave_application/leave_application.py +++ b/hrms/hr/doctype/leave_application/leave_application.py @@ -1371,6 +1371,11 @@ def get_leaves_for_period( leave_days += leave_entry.leaves elif leave_entry.transaction_type == "Leave Application": + if inclusive_period: + # use the ledger's recorded value instead of recomputing it + leave_days += leave_entry.leaves + continue + if leave_entry.from_date < getdate(from_date): leave_entry.from_date = from_date if leave_entry.to_date > getdate(to_date): diff --git a/hrms/hr/doctype/shift_type/shift_type.py b/hrms/hr/doctype/shift_type/shift_type.py index 890486e230f..b1617c1b76c 100644 --- a/hrms/hr/doctype/shift_type/shift_type.py +++ b/hrms/hr/doctype/shift_type/shift_type.py @@ -473,10 +473,15 @@ def mark_absent_for_half_day_dates(self, employee): fields=["name", "attendance_date"], ) start_time = get_time(self.start_time) + last_sync_of_checkin = get_datetime(self.last_sync_of_checkin) for attendance in half_day_attendances: timestamp = datetime.combine(attendance.attendance_date, start_time) shift_details = get_employee_shift(employee, timestamp, True) - if shift_details and shift_details.shift_type.name == self.name: + if ( + shift_details + and shift_details.shift_type.name == self.name + and shift_details.actual_end < last_sync_of_checkin + ): frappe.db.set_value( "Attendance", attendance.name, diff --git a/hrms/hr/report/employee_leave_balance/test_employee_leave_balance.py b/hrms/hr/report/employee_leave_balance/test_employee_leave_balance.py index a7794f0ba5c..0ac18f16853 100644 --- a/hrms/hr/report/employee_leave_balance/test_employee_leave_balance.py +++ b/hrms/hr/report/employee_leave_balance/test_employee_leave_balance.py @@ -101,6 +101,36 @@ def test_employee_leave_balance(self): self.assertEqual(report[1], expected_data) + @assign_holiday_list("_Test Emp Balance Holiday List", "_Test Company") + def test_leaves_taken_matches_ledger_when_holiday_list_changes_after_approval(self): + frappe.get_doc(test_records[0]).insert() + + allocation = make_allocation_record( + employee=self.employee_id, from_date=self.year_start, to_date=self.year_end + ) + + first_sunday = get_first_sunday(self.holiday_list, for_date=self.year_start) + leave_application = make_leave_application( + self.employee_id, add_days(first_sunday, 1), add_days(first_sunday, 4), "_Test Leave Type" + ) + leave_application.reload() + + # holiday list is edited after the leave is already approved + holiday_list = frappe.get_doc("Holiday List", self.holiday_list) + holiday_list.append( + "holidays", + {"holiday_date": add_days(first_sunday, 2), "description": "Half Day", "is_half_day": 1}, + ) + holiday_list.save() + + filters = frappe._dict( + {"from_date": allocation.from_date, "to_date": allocation.to_date, "employee": self.employee_id} + ) + report = execute(filters) + + # should match the ledger, not a recalculation against the new holiday list + self.assertEqual(report[1][0].leaves_taken, flt(leave_application.total_leave_days)) + @assign_holiday_list("_Test Emp Balance Holiday List", "_Test Company") def test_opening_balance_on_alloc_boundary_dates(self): frappe.get_doc(test_records[0]).insert() diff --git a/hrms/overrides/employee_payment_entry.py b/hrms/overrides/employee_payment_entry.py index dfa403d08fa..26ab9b2f937 100644 --- a/hrms/overrides/employee_payment_entry.py +++ b/hrms/overrides/employee_payment_entry.py @@ -2,8 +2,9 @@ # License: GNU General Public License v3. See license.txt import frappe +from frappe import _ from frappe.model.document import Document -from frappe.utils import flt, nowdate +from frappe.utils import flt, get_link_to_form, nowdate import erpnext from erpnext.accounts.doctype.payment_entry.payment_entry import ( @@ -93,6 +94,22 @@ def get_payment_entry_for_employee( # bank or cash bank = get_bank_cash_account(doc, bank_account) + if not bank.get("account"): + company_link = get_link_to_form("Company", doc.company + "#accounts_tab", _("here")) + if frappe.get_meta(dt).has_field("mode_of_payment"): + frappe.throw( + _( + "Please set a {0} on this document, or a Default Bank/Cash Account in the Company {1}" + ).format( + get_link_to_form(dt, dn + "#mode_of_payment", _("Mode of Payment")), + company_link, + ), + title=_("Bank/Cash Account Required"), + ) + frappe.throw( + _("Please set a Default Bank/Cash Account in the Company {0}").format(company_link), + title=_("Bank/Cash Account Required"), + ) pe = frappe.new_doc("Payment Entry") pe.payment_type = payment_type