feat: Overtime based on Attendance and Timesheet

This commit is contained in:
Anurag Mishra
2021-06-14 18:32:32 +05:30
parent 3851d15360
commit da2e95dbcc
18 changed files with 634 additions and 289 deletions

View File

@@ -25,16 +25,13 @@
"out_time", "out_time",
"column_break_18", "column_break_18",
"standard_working_time", "standard_working_time",
"standard_working_time_delta",
"working_time", "working_time",
"working_timedelta",
"late_entry", "late_entry",
"early_exit", "early_exit",
"overtime_details_section", "overtime_details_section",
"overtime_type", "overtime_type",
"overtime_duration",
"column_break_27", "column_break_27",
"overtime_duration_words", "overtime_duration",
"amended_from" "amended_from"
], ],
"fields": [ "fields": [
@@ -215,41 +212,23 @@
"description": "Shift duration for a day", "description": "Shift duration for a day",
"fetch_from": "shift.standard_working_time", "fetch_from": "shift.standard_working_time",
"fieldname": "standard_working_time", "fieldname": "standard_working_time",
"fieldtype": "Data", "fieldtype": "Duration",
"label": " Standard Working Time", "label": " Standard Working Time",
"read_only": 1 "read_only": 1
}, },
{
"fetch_from": "shift.working_time_delta",
"fieldname": "standard_working_time_delta",
"fieldtype": "Time",
"hidden": 1,
"label": "Standard Working Time(Delta)"
},
{ {
"depends_on": "working_time", "depends_on": "working_time",
"fieldname": "working_time", "fieldname": "working_time",
"fieldtype": "Data", "fieldtype": "Duration",
"label": "Total Working Time", "label": "Total Working Time",
"precision": "1", "precision": "1",
"read_only": 1 "read_only": 1
}, },
{ {
"fieldname": "working_timedelta", "default": "0000",
"fieldtype": "Time",
"hidden": 1,
"label": "Working Time(Delta)"
},
{
"fieldname": "overtime_duration_words",
"fieldtype": "Data",
"label": "Overtime Duration(Words)",
"read_only": 1
},
{
"default": "00:00:00",
"fieldname": "overtime_duration", "fieldname": "overtime_duration",
"fieldtype": "Time", "fieldtype": "Duration",
"hide_days": 1,
"label": "Overtime Duration" "label": "Overtime Duration"
}, },
{ {
@@ -267,7 +246,7 @@
"idx": 1, "idx": 1,
"is_submittable": 1, "is_submittable": 1,
"links": [], "links": [],
"modified": "2021-05-26 16:44:33.219313", "modified": "2021-06-09 13:42:36.176547",
"modified_by": "Administrator", "modified_by": "Administrator",
"module": "HR", "module": "HR",
"name": "Attendance", "name": "Attendance",

View File

@@ -22,6 +22,9 @@ class Attendance(Document):
self.set_overtime_type() self.set_overtime_type()
self.set_default_shift() self.set_default_shift()
if not frappe.db.get_single_value('Payroll Settings', 'fetch_standard_working_hours_from_shift_type'):
self.standard_working_time = None
def validate_attendance_date(self): def validate_attendance_date(self):
date_of_joining = frappe.db.get_value("Employee", self.employee, "date_of_joining") date_of_joining = frappe.db.get_value("Employee", self.employee, "date_of_joining")
@@ -54,6 +57,18 @@ class Attendance(Document):
def set_overtime_type(self): def set_overtime_type(self):
self.overtime_type = get_overtime_type(self.employee) self.overtime_type = get_overtime_type(self.employee)
if self.overtime_type:
if frappe.db.get_single_value("Payroll Settings", "overtime_based_on") != "Attendance":
frappe.msgprint(_('Set "Calculate Overtime Based On Attendance" to Attendance for Overtime Slip Creation'))
maximum_overtime_hours_allowed = frappe.db.get_single_value("Payroll Settings", "maximum_overtime_hours_allowed")
if maximum_overtime_hours_allowed and maximum_overtime_hours_allowed * 3600 < self.overtime_duration:
self.overtime_duration = maximum_overtime_hours_allowed * 3600
frappe.msgprint(_("Overtime Duration can not be greater than {0} Hours. You can change this in Payroll settings").format(
str(maximum_overtime_hours_allowed)
))
def check_leave_record(self): def check_leave_record(self):
leave_record = frappe.db.sql(""" leave_record = frappe.db.sql("""
select leave_type, half_day, half_day_date select leave_type, half_day, half_day_date
@@ -91,11 +106,9 @@ class Attendance(Document):
def calculate_overtime_duration(self): def calculate_overtime_duration(self):
#this method is only for Calculation of overtime based on Attendance through Employee Checkins #this method is only for Calculation of overtime based on Attendance through Employee Checkins
overtime_duration = self.working_timedelta - self.standard_working_time_delta self.overtime_duration = None
self.overtime_duration = overtime_duration if int(self.working_time) > int(self.standard_working_time):
overtime_duration = str(overtime_duration).split(':') self.overtime_duration = int(self.working_time) - int(self.standard_working_time)
if int(overtime_duration[0]) or int(overtime_duration[1]):
self.overtime_duration_words = overtime_duration[0] + " Hours " + overtime_duration[1] + " Minutes"
@frappe.whitelist() @frappe.whitelist()
def get_shift_type(employee, attendance_date): def get_shift_type(employee, attendance_date):
@@ -123,9 +136,6 @@ def get_shift_type(employee, attendance_date):
@frappe.whitelist() @frappe.whitelist()
def get_overtime_type(employee): def get_overtime_type(employee):
overtime_based_on = frappe.db.get_single_value("Payroll Settings", "overtime_based_on")
if overtime_based_on == "Attendance":
emp_department = frappe.db.get_value("Employee", employee, "department") emp_department = frappe.db.get_value("Employee", employee, "department")
if emp_department: if emp_department:
overtime_type = frappe.get_list("Overtime Type", filters={"party_type": "Department", "party": emp_department}, fields=['name']) overtime_type = frappe.get_list("Overtime Type", filters={"party_type": "Department", "party": emp_department}, fields=['name'])

View File

@@ -4,7 +4,7 @@
from __future__ import unicode_literals from __future__ import unicode_literals
import frappe import frappe
from frappe.utils import now, cint, get_datetime from frappe.utils import cint, get_datetime
from frappe.model.document import Document from frappe.model.document import Document
from datetime import timedelta from datetime import timedelta
from math import modf from math import modf
@@ -42,8 +42,8 @@ class EmployeeCheckin(Document):
self.shift_start = shift_actual_timings[2].start_datetime self.shift_start = shift_actual_timings[2].start_datetime
self.shift_end = shift_actual_timings[2].end_datetime self.shift_end = shift_actual_timings[2].end_datetime
elif frappe.db.get_value("Shift Type", shift_actual_timings[2].shift_type.name, "allow_overtime"): elif frappe.db.get_value("Shift Type", shift_actual_timings[2].shift_type.name, "allow_overtime"):
# #because after Actual time it takes check-in/out invalid #because after Actual time it takes check-in/out invalid
# #if employee checkout late or check-in before before shift timing adding time buffer. #if employee checkout late or check-in before before shift timing adding time buffer.
self.shift = shift_actual_timings[2].shift_type.name self.shift = shift_actual_timings[2].shift_type.name
self.shift_start = shift_actual_timings[2].start_datetime self.shift_start = shift_actual_timings[2].start_datetime
self.shift_end = shift_actual_timings[2].end_datetime self.shift_end = shift_actual_timings[2].end_datetime
@@ -111,10 +111,6 @@ def mark_attendance_and_link_log(logs, attendance_status, attendance_date, worki
from erpnext.hr.doctype.shift_type.shift_type import convert_time_into_duration from erpnext.hr.doctype.shift_type.shift_type import convert_time_into_duration
working_time = convert_time_into_duration(working_timedelta) working_time = convert_time_into_duration(working_timedelta)
print("working")
print(working_timedelta)
print(working_time)
doc_dict = { doc_dict = {
'doctype': 'Attendance', 'doctype': 'Attendance',
'employee': employee, 'employee': employee,

View File

@@ -6,7 +6,7 @@ from __future__ import unicode_literals
import frappe import frappe
from frappe import _ from frappe import _
from frappe.model.document import Document from frappe.model.document import Document
from frappe.utils import cint, cstr, date_diff, flt, formatdate, getdate, now_datetime, nowdate from frappe.utils import cint, cstr, getdate, now_datetime, nowdate
from erpnext.hr.doctype.employee.employee import get_holiday_list_for_employee from erpnext.hr.doctype.employee.employee import get_holiday_list_for_employee
from erpnext.hr.doctype.holiday_list.holiday_list import is_holiday from erpnext.hr.doctype.holiday_list.holiday_list import is_holiday
from erpnext.hr.utils import validate_active_employee from erpnext.hr.utils import validate_active_employee
@@ -236,13 +236,15 @@ def get_shift_details(shift_type_name, for_date=nowdate()):
end_datetime = datetime.combine(for_date, datetime.min.time()) + shift_type.end_time end_datetime = datetime.combine(for_date, datetime.min.time()) + shift_type.end_time
actual_start = start_datetime - timedelta(minutes=shift_type.begin_check_in_before_shift_start_time) actual_start = start_datetime - timedelta(minutes=shift_type.begin_check_in_before_shift_start_time)
actual_end = end_datetime + timedelta(minutes=shift_type.allow_check_out_after_shift_end_time) actual_end = end_datetime + timedelta(minutes=shift_type.allow_check_out_after_shift_end_time)
allow_overtime = shift_type.allow_overtime
return frappe._dict({ return frappe._dict({
'shift_type': shift_type, 'shift_type': shift_type,
'start_datetime': start_datetime, 'start_datetime': start_datetime,
'end_datetime': end_datetime, 'end_datetime': end_datetime,
'actual_start': actual_start, 'actual_start': actual_start,
'actual_end': actual_end 'actual_end': actual_end,
'allow_overtime': allow_overtime
}) })
@@ -254,12 +256,18 @@ def get_actual_start_end_datetime_of_shift(employee, for_datetime, consider_defa
""" """
actual_shift_start = actual_shift_end = shift_details = None actual_shift_start = actual_shift_end = shift_details = None
shift_timings_as_per_timestamp = get_employee_shift_timings(employee, for_datetime, consider_default_shift) shift_timings_as_per_timestamp = get_employee_shift_timings(employee, for_datetime, consider_default_shift)
if not shift_timings_as_per_timestamp[0].allow_overtime:
# If Shift is not allowed for automatic calculation of overtime, then previous, current and next
# shift will also should be considered for valid and invalid checkins.
# if checkin time is not in current shift thenit will check prev and next shift for checkin validation.
timestamp_list = [] timestamp_list = []
for shift in shift_timings_as_per_timestamp: for shift in shift_timings_as_per_timestamp:
if shift: if shift:
timestamp_list.extend([shift.actual_start, shift.actual_end]) timestamp_list.extend([shift.actual_start, shift.actual_end])
else: else:
timestamp_list.extend([None, None]) timestamp_list.extend([None, None])
timestamp_index = None timestamp_index = None
for index, timestamp in enumerate(timestamp_list): for index, timestamp in enumerate(timestamp_list):
if timestamp and for_datetime <= timestamp: if timestamp and for_datetime <= timestamp:
@@ -271,5 +279,9 @@ def get_actual_start_end_datetime_of_shift(employee, for_datetime, consider_defa
actual_shift_end = shift_details.actual_end actual_shift_end = shift_details.actual_end
elif timestamp_index: elif timestamp_index:
shift_details = shift_timings_as_per_timestamp[int(timestamp_index/2)] shift_details = shift_timings_as_per_timestamp[int(timestamp_index/2)]
else:
# for overtime calculation there is no valid and invalid checkins it should return the current shift and after that total working
# hours will be taken in consideration for overtime calculation. there will be no actual_shift_start/end.
shift_details = shift_timings_as_per_timestamp[1]
return actual_shift_start, actual_shift_end, shift_details return actual_shift_start, actual_shift_end, shift_details

View File

@@ -9,7 +9,6 @@
"start_time", "start_time",
"end_time", "end_time",
"standard_working_time", "standard_working_time",
"working_time_delta",
"column_break_3", "column_break_3",
"holiday_list", "holiday_list",
"enable_auto_attendance", "enable_auto_attendance",
@@ -169,16 +168,10 @@
}, },
{ {
"fieldname": "standard_working_time", "fieldname": "standard_working_time",
"fieldtype": "Data", "fieldtype": "Duration",
"label": "Standard Working Time", "label": "Standard Working Time",
"read_only": 1 "read_only": 1
}, },
{
"fieldname": "working_time_delta",
"fieldtype": "Time",
"hidden": 1,
"label": "Working time(delta)"
},
{ {
"default": "0", "default": "0",
"depends_on": "enable_auto_attendance", "depends_on": "enable_auto_attendance",
@@ -198,7 +191,7 @@
} }
], ],
"links": [], "links": [],
"modified": "2021-05-26 14:10:09.574202", "modified": "2021-06-09 13:38:25.697100",
"modified_by": "Administrator", "modified_by": "Administrator",
"module": "HR", "module": "HR",
"name": "Shift Type", "name": "Shift Type",

View File

@@ -36,8 +36,6 @@ class ShiftType(Document):
time_difference = shift_start - shift_end time_difference = shift_start - shift_end
self.standard_working_time = convert_time_into_duration(time_difference) self.standard_working_time = convert_time_into_duration(time_difference)
def validate_overtime(self): def validate_overtime(self):
if not frappe.db.get_single_value("Payroll Settings", "fetch_standard_working_hours_from_shift_type") and self.allow_overtime: if not frappe.db.get_single_value("Payroll Settings", "fetch_standard_working_hours_from_shift_type") and self.allow_overtime:
frappe.throw(_('Please enable "Fetch Standard Working Hours from Shift Type" in payroll Settings for Overtime.')) frappe.throw(_('Please enable "Fetch Standard Working Hours from Shift Type" in payroll Settings for Overtime.'))
@@ -47,6 +45,7 @@ class ShiftType(Document):
@frappe.whitelist() @frappe.whitelist()
def process_auto_attendance(self): def process_auto_attendance(self):
self.validate_overtime()
if not cint(self.enable_auto_attendance) or not self.process_attendance_after or not self.last_sync_of_checkin: if not cint(self.enable_auto_attendance) or not self.process_attendance_after or not self.last_sync_of_checkin:
return return
filters = { filters = {
@@ -57,11 +56,8 @@ class ShiftType(Document):
'shift': self.name 'shift': self.name
} }
logs = frappe.db.get_list('Employee Checkin', fields="*", filters=filters, order_by="employee,time") logs = frappe.db.get_list('Employee Checkin', fields="*", filters=filters, order_by="employee,time")
from pprint import pprint
pprint(logs)
if self.allow_overtime == 1: if self.allow_overtime == 1:
print("chumma")
checkins_log = itertools.groupby(logs, key=lambda x: (x['employee'], x['shift_start'])) checkins_log = itertools.groupby(logs, key=lambda x: (x['employee'], x['shift_start']))
else: else:
checkins_log = itertools.groupby(logs, key=lambda x: (x['employee'], x['shift_actual_start'])) checkins_log = itertools.groupby(logs, key=lambda x: (x['employee'], x['shift_actual_start']))
@@ -69,7 +65,6 @@ class ShiftType(Document):
for key, group in checkins_log: for key, group in checkins_log:
single_shift_logs = list(group) single_shift_logs = list(group)
attendance_status, working_hours, late_entry, early_exit, in_time, out_time = self.get_attendance(single_shift_logs) attendance_status, working_hours, late_entry, early_exit, in_time, out_time = self.get_attendance(single_shift_logs)
print(attendance_status, working_hours, late_entry, early_exit, in_time, out_time)
mark_attendance_and_link_log(single_shift_logs, attendance_status, key[1].date(), working_hours, late_entry, early_exit, in_time, out_time, self.name) mark_attendance_and_link_log(single_shift_logs, attendance_status, key[1].date(), working_hours, late_entry, early_exit, in_time, out_time, self.name)
@@ -86,7 +81,6 @@ class ShiftType(Document):
late_entry = early_exit = False late_entry = early_exit = False
total_working_hours, in_time, out_time = calculate_working_hours(logs, self.determine_check_in_and_check_out, self.working_hours_calculation_based_on) total_working_hours, in_time, out_time = calculate_working_hours(logs, self.determine_check_in_and_check_out, self.working_hours_calculation_based_on)
print(total_working_hours)
if cint(self.enable_entry_grace_period) and in_time and in_time > logs[0].shift_start + timedelta(minutes=cint(self.late_entry_grace_period)): if cint(self.enable_entry_grace_period) and in_time and in_time > logs[0].shift_start + timedelta(minutes=cint(self.late_entry_grace_period)):
late_entry = True late_entry = True
@@ -95,7 +89,6 @@ class ShiftType(Document):
early_exit = True early_exit = True
if self.working_hours_threshold_for_absent and total_working_hours < self.working_hours_threshold_for_absent: if self.working_hours_threshold_for_absent and total_working_hours < self.working_hours_threshold_for_absent:
print("------->>", 'Here', print(self.working_hours_threshold_for_absent))
return 'Absent', total_working_hours, late_entry, early_exit, in_time, out_time return 'Absent', total_working_hours, late_entry, early_exit, in_time, out_time
if self.working_hours_threshold_for_half_day and total_working_hours < self.working_hours_threshold_for_half_day: if self.working_hours_threshold_for_half_day and total_working_hours < self.working_hours_threshold_for_half_day:

View File

@@ -7,15 +7,15 @@
"field_order": [ "field_order": [
"reference_document_type", "reference_document_type",
"reference_document", "reference_document",
"column_break_2",
"date", "date",
"start_time", "start_date",
"end_time", "end_date",
"section_break_5",
"overtime_type", "overtime_type",
"total_working_time",
"working_timedelta",
"overtime_duration", "overtime_duration",
"overtime_durationtime", "column_break_10",
"overtime_amount" "standard_working_time"
], ],
"fields": [ "fields": [
{ {
@@ -28,17 +28,9 @@
{ {
"fieldname": "date", "fieldname": "date",
"fieldtype": "Date", "fieldtype": "Date",
"label": "Date" "in_list_view": 1,
}, "label": "Date",
{ "reqd": 1
"fieldname": "start_time",
"fieldtype": "Datetime",
"label": "Start Time "
},
{
"fieldname": "end_time",
"fieldtype": "Datetime",
"label": "End Time"
}, },
{ {
"fieldname": "overtime_type", "fieldname": "overtime_type",
@@ -48,49 +40,57 @@
"options": "Overtime Type", "options": "Overtime Type",
"reqd": 1 "reqd": 1
}, },
{
"fieldname": "total_working_time",
"fieldtype": "Data",
"label": "Total Working Time"
},
{
"default": "00:00:00",
"fieldname": "working_timedelta",
"fieldtype": "Time",
"label": "Working Time(Delta)"
},
{ {
"fieldname": "overtime_duration", "fieldname": "overtime_duration",
"fieldtype": "Data", "fieldtype": "Duration",
"hide_days": 1,
"in_list_view": 1, "in_list_view": 1,
"label": "Overtime Duration", "label": "Overtime Duration",
"reqd": 1 "reqd": 1
}, },
{
"default": "00:00:00",
"fieldname": "overtime_durationtime",
"fieldtype": "Time",
"hidden": 1,
"label": "Overtime Duration(Time)"
},
{
"fieldname": "overtime_amount",
"fieldtype": "Data",
"in_list_view": 1,
"label": "Overtime Amount",
"reqd": 1
},
{ {
"fieldname": "reference_document", "fieldname": "reference_document",
"fieldtype": "Dynamic Link", "fieldtype": "Dynamic Link",
"in_list_view": 1,
"label": "Reference Document", "label": "Reference Document",
"options": "reference_document_type" "options": "reference_document_type",
"read_only": 1
},
{
"fieldname": "column_break_2",
"fieldtype": "Column Break"
},
{
"fieldname": "section_break_5",
"fieldtype": "Section Break"
},
{
"fieldname": "start_date",
"fieldtype": "Date",
"label": "Start Date",
"read_only": 1
},
{
"fieldname": "end_date",
"fieldtype": "Date",
"label": "End Date",
"read_only": 1
},
{
"fieldname": "column_break_10",
"fieldtype": "Column Break"
},
{
"fieldname": "standard_working_time",
"fieldtype": "Duration",
"label": "Standard Working Time",
"read_only": 1
} }
], ],
"index_web_pages_for_search": 1, "index_web_pages_for_search": 1,
"istable": 1, "istable": 1,
"links": [], "links": [],
"modified": "2021-05-27 13:43:11.578682", "modified": "2021-06-14 17:39:36.147530",
"modified_by": "Administrator", "modified_by": "Administrator",
"module": "Payroll", "module": "Payroll",
"name": "Overtime Details", "name": "Overtime Details",

View File

@@ -2,26 +2,33 @@
// For license information, please see license.txt // For license information, please see license.txt
frappe.ui.form.on('Overtime Slip', { frappe.ui.form.on('Overtime Slip', {
onload: function() { onload: function (frm) {
frm.set_query("employee", () => {
return {
query: "erpnext.controllers.queries.employee_query"
};
});
}, },
employee: function (frm) { employee: function (frm) {
if (frm.doc.employee) { if (frm.doc.employee) {
frm.events.set_frequency_and_dates(frm); frm.events.set_frequency_and_dates(frm).then(() => {
frm.events.get_emp_details_and_overtime_duration(frm); frm.events.get_emp_details_and_overtime_duration(frm);
});
} }
}, },
from_date: function (frm) { from_date: function (frm) {
if (frm.doc.employee) { if (frm.doc.employee) {
frm.events.set_frequency_and_dates(frm); frm.events.set_frequency_and_dates(frm).then(() => {
frm.events.get_emp_details_and_overtime_duration(frm); frm.events.get_emp_details_and_overtime_duration(frm);
});
} }
}, },
set_frequency_and_dates: function (frm) { set_frequency_and_dates: function (frm) {
frappe.call({ return frappe.call({
method: "erpnext.payroll.doctype.overtime_slip.overtime_slip.get_frequency_and_dates", method: "erpnext.payroll.doctype.overtime_slip.overtime_slip.get_frequency_and_dates",
args: { args: {
employee: frm.doc.employee, employee: frm.doc.employee,
@@ -29,9 +36,10 @@ frappe.ui.form.on('Overtime Slip', {
}, },
callback: function (r) { callback: function (r) {
frm.set_value("payroll_frequency", r.message[1]); frm.set_value("payroll_frequency", r.message[1]);
frm.doc.from_date = r.message[0].start_date; if (r.message[0].start_date != frm.doc.from_date) {
frm.doc.to_date = r.message[0].end_date; frm.set_value("from_date", r.message[0].start_date);
frm.refresh(); }
frm.set_value("to_date", r.message[0].end_date);
} }
}); });
}, },
@@ -41,14 +49,32 @@ frappe.ui.form.on('Overtime Slip', {
return frappe.call({ return frappe.call({
method: 'get_emp_and_overtime_details', method: 'get_emp_and_overtime_details',
doc: frm.doc, doc: frm.doc,
callback: function(r) { callback: function () {
frm.refresh();
} }
}); });
} }
}, },
});
reset_value: function(frm) { frappe.ui.form.on('Overtime Details', {
date: function (frm, cdt, cdn) {
let child = locals[cdt][cdn];
if (child.date) {
frappe.call({
method: "erpnext.payroll.doctype.overtime_slip.overtime_slip.get_standard_working_hours",
args: {
employee: frm.doc.employee,
date: child.date,
},
callback: function (r) {
if (r.message) {
frappe.model.set_value(cdt, cdn, 'standard_working_time', r.message);
}
}
});
} else {
frappe.model.set_value(cdt, cdn, 'standard_working_time', 0);
}
} }
}); });

View File

@@ -1,5 +1,6 @@
{ {
"actions": [], "actions": [],
"autoname": "HR-OVR-SLIP-.#####",
"creation": "2021-05-27 12:47:32.372698", "creation": "2021-05-27 12:47:32.372698",
"doctype": "DocType", "doctype": "DocType",
"editable_grid": 1, "editable_grid": 1,
@@ -21,10 +22,7 @@
"overtime_details", "overtime_details",
"section_break_13", "section_break_13",
"total_overtime_duration", "total_overtime_duration",
"total_overtime_durationtime",
"column_break_17", "column_break_17",
"amount",
"name1",
"amended_from" "amended_from"
], ],
"fields": [ "fields": [
@@ -119,19 +117,9 @@
}, },
{ {
"fieldname": "total_overtime_duration", "fieldname": "total_overtime_duration",
"fieldtype": "Data", "fieldtype": "Duration",
"label": "Total Overtime Duration" "label": "Total Overtime Duration"
}, },
{
"fieldname": "total_overtime_durationtime",
"fieldtype": "Time",
"label": "Total Overtime Duration(Time)"
},
{
"fieldname": "amount",
"fieldtype": "Currency",
"label": "Amount"
},
{ {
"fieldname": "section_break_12", "fieldname": "section_break_12",
"fieldtype": "Section Break" "fieldtype": "Section Break"
@@ -140,11 +128,6 @@
"fieldname": "column_break_17", "fieldname": "column_break_17",
"fieldtype": "Column Break" "fieldtype": "Column Break"
}, },
{
"fieldname": "name1",
"fieldtype": "Duration",
"label": "name"
},
{ {
"default": "Today", "default": "Today",
"fieldname": "posting_date", "fieldname": "posting_date",
@@ -156,7 +139,7 @@
"index_web_pages_for_search": 1, "index_web_pages_for_search": 1,
"is_submittable": 1, "is_submittable": 1,
"links": [], "links": [],
"modified": "2021-05-31 15:07:39.485473", "modified": "2021-06-10 13:35:57.511257",
"modified_by": "Administrator", "modified_by": "Administrator",
"module": "Payroll", "module": "Payroll",
"name": "Overtime Slip", "name": "Overtime Slip",

View File

@@ -1,15 +1,17 @@
# Copyright (c) 2021, Frappe Technologies Pvt. Ltd. and contributors # Copyright (c) 2021, Frappe Technologies Pvt. Ltd. and contributors
# For license information, please see license.txt # For license information, please see license.txt
from erpnext.hr.doctype.attendance.attendance import get_overtime_type
import frappe import frappe
from frappe import _ from frappe import _
from frappe.utils import get_datetime from frappe.utils import get_datetime, getdate
from erpnext.payroll.doctype.payroll_entry.payroll_entry import get_start_end_dates from erpnext.payroll.doctype.payroll_entry.payroll_entry import get_start_end_dates
from erpnext.payroll.doctype.gratuity.gratuity import get_salary_structure from erpnext.payroll.doctype.gratuity.gratuity import get_salary_structure
from frappe.model.document import Document from frappe.model.document import Document
from pprint import pprint
class OvertimeSlip(Document): class OvertimeSlip(Document):
def on_submit(self):
if self.status == "Pending":
frappe.throw(_("Overtime Slip with Status 'Approved' or 'Rejected' are allowed for Submission"))
@frappe.whitelist() @frappe.whitelist()
def get_emp_and_overtime_details(self): def get_emp_and_overtime_details(self):
@@ -17,22 +19,69 @@ class OvertimeSlip(Document):
records = [] records = []
if overtime_based_on == "Attendance": if overtime_based_on == "Attendance":
records = self.get_attendance_record() records = self.get_attendance_record()
if len(records):
self.create_overtime_details_row_for_attendance(records)
elif overtime_based_on == "Timesheet": elif overtime_based_on == "Timesheet":
records = self.get_timesheet_record() records = self.get_timesheet_record()
if len(records):
self.create_overtime_details_row_for_timesheet(records)
else: else:
frappe.throw(_('Select "Calculate Overtime Hours Based On" in Payroll Settings')) frappe.throw(_('Select "Calculate Overtime Hours Based On" in Payroll Settings'))
if len(records): if len(self.overtime_details):
self.create_overtime_details_row(records) self.total_overtime_duration = sum([int(detail.overtime_duration) for detail in self.overtime_details])
if not len(records):
self.overtime_details = []
frappe.msgprint(_("No {0} records found for Overtime").format(overtime_based_on))
def create_overtime_details_row_for_attendance(self, records):
self.overtime_details = []
for record in records:
if record.standard_working_time:
standard_working_time = record.standard_working_time
else: else:
frappe.throw(_("No {0} records found for Overtime").format(overtime_based_on)) standard_working_time = frappe.db.get_single_value("HR Settings", "standard_working_hours") * 3600
if not standard_working_time:
frappe.throw(_('Please Set "Standard Working Hours" in HR settings'))
def create_overtime_details_row(self, records): if record.overtime_duration:
pprint(records) self.append("overtime_details", {
"reference_document_type": "Attendance",
"reference_document": record.name,
"date": record.attendance_date,
"overtime_type": record.overtime_type,
"overtime_duration": record.overtime_duration,
"standard_working_time": standard_working_time,
})
def create_overtime_details_row_for_timesheet(self, records):
self.overtime_details = []
from math import modf
standard_working_time = frappe.db.get_single_value("HR Settings", "standard_working_hours") * 3600
if not standard_working_time:
frappe.throw(_('Please Set "Standard Working Hours" in HR settings'))
for record in records:
if record.overtime_hours:
overtime_hours = modf(record.overtime_hours)
record.overtime_hours = overtime_hours[1]*3600 + overtime_hours[0]*60
self.append("overtime_details", {
"reference_document_type": "Timesheet",
"reference_document": record.name,
"date": record.overtime_on,
"start_date": record.start_date,
"end_date": record.end_date,
"overtime_type": record.overtime_type,
"overtime_duration": record.overtime_hours,
"standard_working_time": standard_working_time
})
def get_attendance_record(self): def get_attendance_record(self):
records = frappe.db.sql("""SELECT overtime_duration, employee, name, attendance_date, overtime_type if self.from_date and self.to_date:
records = frappe.db.sql("""SELECT overtime_duration, name, attendance_date, overtime_type, standard_working_time
FROM `TabAttendance` FROM `TabAttendance`
WHERE WHERE
attendance_date >= %s AND attendance_date <= %s attendance_date >= %s AND attendance_date <= %s
@@ -41,19 +90,67 @@ class OvertimeSlip(Document):
AND ( AND (
overtime_duration IS NOT NULL OR overtime_duration != '00:00:00.000000' overtime_duration IS NOT NULL OR overtime_duration != '00:00:00.000000'
) )
""", (get_datetime(self.from_date), get_datetime(self.to_date), self.employee), as_dict=1) """, (getdate(self.from_date), getdate(self.to_date), self.employee), as_dict=1, debug = 1)
return records return records
return []
def get_timesheet_record(self):
if self.from_date and self.to_date:
"""SELECT Orders.OrderID, Customers.CustomerName, Orders.OrderDate
FROM Orders
INNER JOIN Customers ON Orders.CustomerID=Customers.CustomerID;"""
records = frappe.db.sql("""SELECT ts.name, ts.start_date, ts.end_date, tsd.overtime_on, tsd.overtime_type, tsd.overtime_hours
FROM `TabTimesheet` AS ts
INNER JOIN `tabTimesheet Detail` As tsd ON tsd.parent = ts.name
WHERE
ts.docstatus = 1
AND end_date > %(from_date)s AND end_date <= %(to_date)s
AND start_date >= %(from_date)s AND start_date < %(to_date)s
AND employee = %(employee)s
AND (
total_overtime_hours IS NOT NULL OR total_overtime_hours != 0
)
""", {"from_date": get_datetime(self.from_date), "to_date": get_datetime(self.to_date),"employee": self.employee}, as_dict=1, debug = 1)
return records
return []
@frappe.whitelist()
def get_standard_working_hours(employee, date):
shift_assignment = frappe.db.sql('''SELECT shift_type FROM `tabShift Assignment`
WHERE employee = %(employee)s
AND start_date < %(date)s
and (end_date > %(date)s or end_date is NULL or end_date = "") ''', {
"employee": employee, "date": get_datetime(date)}
, as_dict=1, debug=1)
standard_working_time = 0
fetch_from_shift = frappe.db.get_single_value("Payroll Settings", "fetch_standard_working_hours_from_shift_type")
if len(shift_assignment) and fetch_from_shift:
standard_working_time = frappe.db.get_value("Shift Type", shift_assignment[0].shift_type, "standard_working_time")
elif not len(shift_assignment) and fetch_from_shift:
shift = frappe.db.get_value("Employee", employee, "default_shift")
if shift:
standard_working_time = frappe.db.get_value("Shift Type", shift, "standard_working_time")
else:
frappe.throw(_("Set Default Shift in Employee:{0}").format(employee))
elif not fetch_from_shift:
standard_working_time = frappe.db.get_single_value("HR Settings", "standard_working_hours") * 3600
if not standard_working_time:
frappe.throw(_('Please Set "Standard Working Hours" in HR settings'))
return standard_working_time
@frappe.whitelist() @frappe.whitelist()
def get_frequency_and_dates(employee, date): def get_frequency_and_dates(employee, date):
print(date)
salary_structure = get_salary_structure(employee) salary_structure = get_salary_structure(employee)
if salary_structure: if salary_structure:
payroll_frequency = frappe.db.get_value('Salary Structure', salary_structure, 'payroll_frequency') payroll_frequency = frappe.db.get_value('Salary Structure', salary_structure, 'payroll_frequency')
date_details = get_start_end_dates(payroll_frequency, date, frappe.db.get_value('Employee', employee, 'company')) date_details = get_start_end_dates(payroll_frequency, date, frappe.db.get_value('Employee', employee, 'company'))
print(date_details)
return [date_details, payroll_frequency] return [date_details, payroll_frequency]
else: else:
frappe.throw(_("No Salary Structure Assignment found for Employee: {0}").format(employee)) frappe.throw(_("No Salary Structure Assignment found for Employee: {0}").format(employee))

View File

@@ -16,7 +16,7 @@
"weekend_multiplier", "weekend_multiplier",
"column_break_9", "column_break_9",
"applicable_for_public_holiday", "applicable_for_public_holiday",
"public_holiday_multipliers" "public_holiday_multiplier"
], ],
"fields": [ "fields": [
{ {
@@ -83,15 +83,15 @@
}, },
{ {
"depends_on": "eval: doc.applicable_for_public_holiday == 1", "depends_on": "eval: doc.applicable_for_public_holiday == 1",
"fieldname": "public_holiday_multipliers", "fieldname": "public_holiday_multiplier",
"fieldtype": "Float", "fieldtype": "Float",
"label": "Public Holiday Multipliers", "label": "Public Holiday Multiplier",
"mandatory_depends_on": "eval: doc.applicable_for_public_holiday == 1" "mandatory_depends_on": "eval: doc.applicable_for_public_holiday == 1"
} }
], ],
"index_web_pages_for_search": 1, "index_web_pages_for_search": 1,
"links": [], "links": [],
"modified": "2021-05-25 13:21:11.318945", "modified": "2021-06-09 15:43:43.891270",
"modified_by": "Administrator", "modified_by": "Administrator",
"module": "Payroll", "module": "Payroll",
"name": "Overtime Type", "name": "Overtime Type",

View File

@@ -11,6 +11,7 @@
"amount", "amount",
"year_to_date", "year_to_date",
"section_break_5", "section_break_5",
"overtime_slips",
"additional_salary", "additional_salary",
"statistical_component", "statistical_component",
"depends_on_payment_days", "depends_on_payment_days",
@@ -235,11 +236,25 @@
"label": "Year To Date", "label": "Year To Date",
"options": "currency", "options": "currency",
"read_only": 1 "read_only": 1
},
{
"default": "0",
"depends_on": "eval:doc.parenttype=='Salary Slip' && doc.parentfield=='earnings' && doc.additional_salary",
"fieldname": "is_recurring_additional_salary",
"fieldtype": "Check",
"label": "Is Recurring Additional Salary",
"read_only": 1
},
{
"fieldname": "overtime_slips",
"fieldtype": "Small Text",
"label": "Overtime Slip(s)",
"read_only": 1
} }
], ],
"istable": 1, "istable": 1,
"links": [], "links": [],
"modified": "2021-01-14 13:39:15.847158", "modified": "2021-08-09 17:00:13.386980",
"modified_by": "Administrator", "modified_by": "Administrator",
"module": "Payroll", "module": "Payroll",
"name": "Salary Detail", "name": "Salary Detail",

View File

@@ -336,9 +336,9 @@ class SalarySlip(TransactionBase):
return payment_days return payment_days
def get_holidays_for_employee(self, start_date, end_date): def get_holidays_for_employee(self, start_date, end_date, as_dict = 0):
holiday_list = get_holiday_list_for_employee(self.employee) holiday_list = get_holiday_list_for_employee(self.employee)
holidays = frappe.db.sql_list('''select holiday_date from `tabHoliday` holidays = frappe.db.sql('''select holiday_date, weekly_off from `tabHoliday`
where where
parent=%(holiday_list)s parent=%(holiday_list)s
and holiday_date >= %(start_date)s and holiday_date >= %(start_date)s
@@ -346,10 +346,11 @@ class SalarySlip(TransactionBase):
"holiday_list": holiday_list, "holiday_list": holiday_list,
"start_date": start_date, "start_date": start_date,
"end_date": end_date "end_date": end_date
}) }, as_dict=1)
if as_dict:
holidays = [cstr(i) for i in holidays] return holidays
else:
holidays = [cstr(data.holiday_date)for data in holidays]
return holidays return holidays
def calculate_lwp_or_ppl_based_on_leave_application(self, holidays, working_days): def calculate_lwp_or_ppl_based_on_leave_application(self, holidays, working_days):
@@ -496,6 +497,7 @@ class SalarySlip(TransactionBase):
payroll_period = get_payroll_period(self.start_date, self.end_date, self.company) payroll_period = get_payroll_period(self.start_date, self.end_date, self.company)
self.add_structure_components(component_type) self.add_structure_components(component_type)
self.process_overtime_slips()
self.add_additional_salary_components(component_type) self.add_additional_salary_components(component_type)
if component_type == "earnings": if component_type == "earnings":
self.add_employee_benefits(payroll_period) self.add_employee_benefits(payroll_period)
@@ -509,6 +511,105 @@ class SalarySlip(TransactionBase):
if amount and struct_row.statistical_component == 0: if amount and struct_row.statistical_component == 0:
self.update_component_row(struct_row, amount, component_type) self.update_component_row(struct_row, amount, component_type)
def process_overtime_slips(self):
overtime_slips = self.get_overtime_slips()
amounts, processed_overtime_slips = self.get_overtime_amount(overtime_slips)
self.add_overtime_component(amounts, processed_overtime_slips)
def get_overtime_slips(self):
return frappe.get_all("Overtime Slip", filters = {
'employee': self.employee,
'posting_date': (">=", self.start_date),
'posting_date': ("<=", self.end_date),
'docstatus': 1
}, fields = ["name", "from_date", 'to_date'])
def get_overtime_amount(self, overtime_slips):
standard_duration_amount = 0; weekends_duration_amount= 0; public_holidays_duration_amount = 0
calculated_amount = 0
processed_overtime_slips = []
overtime_types_details = {}
for slip in overtime_slips:
holiday_date = self.get_holidays_for_employee(slip.from_date, slip.to_date, as_dict=1)
holiday_date_map = {}
for date in holiday_date:
holiday_date_map[cstr(date.holiday_date)] = date
details = self.get_overtime_details(slip.name)
for detail in details:
overtime_hours = detail.overtime_duration / 3600
if not detail.overtime_type in overtime_types_details:
details, applicable_components = self.get_overtime_type_detail(detail.overtime_type)
overtime_types_details[detail.overtime_type] = details
if len(applicable_components):
overtime_types_details[detail.overtime_type]["components"] = applicable_components
else:
frappe.throw(_("Select applicable components in Overtime Type: {0}").format(
frappe.bold(detail.overtime_type)))
if "applicable_amount" not in overtime_types_details[detail.overtime_type].keys():
component_amount = sum([data.default_amount for data in self.earnings \
if data.salary_component in overtime_types_details[detail.overtime_type]["components"] \
and not data.get('additional_salary', None)])
overtime_types_details[detail.overtime_type]["applicable_daily_amount"] = component_amount/self.total_working_days
standard_working_hours = detail.standard_working_time/3600
applicable_hourly_wages = overtime_types_details[detail.overtime_type]["applicable_daily_amount"]/standard_working_hours
overtime_date = cstr(detail.date)
if overtime_date in holiday_date_map.keys():
if holiday_date_map[overtime_date].weekly_off == 1:
calculated_amount = overtime_hours * applicable_hourly_wages *\
overtime_types_details[detail.overtime_type]['weekend_multiplier']
weekends_duration_amount += calculated_amount
elif holiday_date_map[overtime_date].weekly_off == 0:
calculated_amount = overtime_hours * applicable_hourly_wages *\
overtime_types_details[detail.overtime_type]['public_holiday_multiplier']
public_holidays_duration_amount += calculated_amount
else:
calculated_amount = overtime_hours * applicable_hourly_wages *\
overtime_types_details[detail.overtime_type]['standard_multiplier']
standard_duration_amount += calculated_amount
processed_overtime_slips.append(slip.name)
return [weekends_duration_amount, public_holidays_duration_amount, standard_duration_amount] , processed_overtime_slips
def add_overtime_component(self, amounts, processed_overtime_slips):
if len(amounts):
overtime_salary_component = frappe.db.get_single_value("Payroll Settings", "overtime_salary_component")
if not overtime_salary_component:
frappe.throw(_('Select {0} in {1}').format(
frappe.bold("Overtime Salary Component"), frappe.bold("Payroll Settings")
))
else:
self.update_component_row(
get_salary_component_data(overtime_salary_component),
sum(amounts),
'earnings',
processed_overtime_slips = processed_overtime_slips
)
def get_overtime_details(self, parent):
return frappe.get_all(
"Overtime Details",
filters = {"parent": parent},
fields = ["date", "overtime_type", "overtime_duration", "standard_working_time"]
)
def get_overtime_type_detail(self, name):
detail = frappe.get_all("Overtime Type", filters = {"name": name}, fields = ["name", "standard_multiplier", "weekend_multiplier", "public_holiday_multiplier"])[0]
components = frappe.get_all("Overtime Salary Component",
filters = {"parent": name}, fields = ["salary_component"])
components = [data. salary_component for data in components]
return detail, components
def get_data_for_eval(self): def get_data_for_eval(self):
'''Returns data for evaluating formula''' '''Returns data for evaluating formula'''
data = frappe._dict() data = frappe._dict()
@@ -639,7 +740,7 @@ class SalarySlip(TransactionBase):
tax_row = get_salary_component_data(d) tax_row = get_salary_component_data(d)
self.update_component_row(tax_row, tax_amount, "deductions") self.update_component_row(tax_row, tax_amount, "deductions")
def update_component_row(self, component_data, amount, component_type, additional_salary=None): def update_component_row(self, component_data, amount, component_type, additional_salary=None, processed_overtime_slips =[]):
component_row = None component_row = None
for d in self.get(component_type): for d in self.get(component_type):
if d.salary_component != component_data.salary_component: if d.salary_component != component_data.salary_component:
@@ -679,6 +780,10 @@ class SalarySlip(TransactionBase):
abbr = component_data.get('abbr') or component_data.get('salary_component_abbr') abbr = component_data.get('abbr') or component_data.get('salary_component_abbr')
component_row.set('abbr', abbr) component_row.set('abbr', abbr)
processed_overtime_slips = ", ".join(processed_overtime_slips)
if processed_overtime_slips:
component_row.overtime_slips = processed_overtime_slips
if additional_salary: if additional_salary:
component_row.default_amount = 0 component_row.default_amount = 0
component_row.additional_amount = amount component_row.additional_amount = amount

View File

@@ -45,12 +45,16 @@ frappe.ui.form.on("Timesheet", {
refresh: function (frm) { refresh: function (frm) {
if (frm.doc.docstatus == 1) { if (frm.doc.docstatus == 1) {
if (frm.doc.per_billed < 100 && frm.doc.total_billable_hours && frm.doc.total_billable_hours > frm.doc.total_billed_hours) { if (frm.doc.per_billed < 100 && frm.doc.total_billable_hours && frm.doc.total_billable_hours > frm.doc.total_billed_hours) {
frm.add_custom_button(__('Create Sales Invoice'), function() { frm.trigger("make_invoice") }, frm.add_custom_button(__('Create Sales Invoice'), function () {
frm.trigger("make_invoice");
},
"fa fa-file-text"); "fa fa-file-text");
} }
if (!frm.doc.salary_slip && frm.doc.employee) { if (!frm.doc.salary_slip && frm.doc.employee) {
frm.add_custom_button(__('Create Salary Slip'), function() { frm.trigger("make_salary_slip") }, frm.add_custom_button(__('Create Salary Slip'), function () {
frm.trigger("make_salary_slip");
},
"fa fa-file-text"); "fa fa-file-text");
} }
} }
@@ -283,6 +287,13 @@ frappe.ui.form.on("Timesheet Detail", {
calculate_time_and_amount(frm); calculate_time_and_amount(frm);
}, },
is_overtime: function(frm, cdt, cdn) {
let child = locals[cdt][cdn];
if (child.is_overtime) {
get_overtime_type(frm, cdt, cdn);
}
},
activity_type: function (frm, cdt, cdn) { activity_type: function (frm, cdt, cdn) {
frappe.call({ frappe.call({
method: "erpnext.projects.doctype.timesheet.timesheet.get_activity_cost", method: "erpnext.projects.doctype.timesheet.timesheet.get_activity_cost",
@@ -302,6 +313,28 @@ frappe.ui.form.on("Timesheet Detail", {
} }
}); });
var get_overtime_type = function(frm, cdt, cdn) {
if (frm.doc.employee) {
frappe.call({
method: "erpnext.hr.doctype.attendance.attendance.get_overtime_type",
args: {
employee: frm.doc.employee
},
callback: function (r) {
if (r.message) {
frappe.model.set_value(cdt, cdn, 'overtime_type', r.message);
} else {
frappe.model.set_value(cdt, cdn, 'is_overtime', 0);
frappe.throw(__("Define Overtime Type for Employee "+frm.doc.employee+" "));
}
}
});
} else {
frappe.model.set_value(cdt, cdn, 'is_overtime', 0);
frappe.throw({message: __("Select Employee if applicable for overtime"), title: "Employee Missing"});
}
};
var calculate_end_time = function (frm, cdt, cdn) { var calculate_end_time = function (frm, cdt, cdn) {
let child = locals[cdt][cdn]; let child = locals[cdt][cdn];
@@ -382,9 +415,13 @@ var calculate_time_and_amount = function(frm) {
// set employee (and company) to the one that's currently logged in // set employee (and company) to the one that's currently logged in
const set_employee_and_company = function (frm) { const set_employee_and_company = function (frm) {
const options = { user_id: frappe.session.user }; const options = {
user_id: frappe.session.user
};
const fields = ['name', 'company']; const fields = ['name', 'company'];
frappe.db.get_value('Employee', options, fields).then(({ message }) => { frappe.db.get_value('Employee', options, fields).then(({
message
}) => {
if (message) { if (message) {
// there is an employee with the currently logged in user_id // there is an employee with the currently logged in user_id
frm.set_value("employee", message.name); frm.set_value("employee", message.name);

View File

@@ -14,11 +14,11 @@
"customer", "customer",
"currency", "currency",
"exchange_rate", "exchange_rate",
"sales_invoice",
"column_break_3", "column_break_3",
"salary_slip",
"status", "status",
"parent_project", "parent_project",
"salary_slip",
"sales_invoice",
"employee_detail", "employee_detail",
"employee", "employee",
"employee_name", "employee_name",
@@ -29,7 +29,10 @@
"end_date", "end_date",
"section_break_5", "section_break_5",
"time_logs", "time_logs",
"working_hours", "overtime_details_section",
"overtime_type",
"total_overtime_hours",
"column_break_26",
"total_hours", "total_hours",
"billing_details", "billing_details",
"total_billable_hours", "total_billable_hours",
@@ -173,10 +176,6 @@
"options": "Timesheet Detail", "options": "Timesheet Detail",
"reqd": 1 "reqd": 1
}, },
{
"fieldname": "working_hours",
"fieldtype": "Section Break"
},
{ {
"allow_on_submit": 1, "allow_on_submit": 1,
"default": "0", "default": "0",
@@ -313,13 +312,36 @@
"fieldname": "exchange_rate", "fieldname": "exchange_rate",
"fieldtype": "Float", "fieldtype": "Float",
"label": "Exchange Rate" "label": "Exchange Rate"
},
{
"depends_on": "eval: doc.total_overtime_hours",
"fieldname": "overtime_details_section",
"fieldtype": "Section Break",
"label": "Overtime Details"
},
{
"fieldname": "overtime_type",
"fieldtype": "Link",
"label": "Overtime Type",
"options": "Overtime Type",
"read_only": 1
},
{
"fieldname": "total_overtime_hours",
"fieldtype": "Float",
"label": "Total Overtime Hours",
"read_only": 1
},
{
"fieldname": "column_break_26",
"fieldtype": "Column Break"
} }
], ],
"icon": "fa fa-clock-o", "icon": "fa fa-clock-o",
"idx": 1, "idx": 1,
"is_submittable": 1, "is_submittable": 1,
"links": [], "links": [],
"modified": "2021-05-18 16:10:08.249619", "modified": "2021-06-14 17:10:31.434084",
"modified_by": "Administrator", "modified_by": "Administrator",
"module": "Projects", "module": "Projects",
"name": "Timesheet", "name": "Timesheet",

View File

@@ -4,16 +4,12 @@
from __future__ import unicode_literals from __future__ import unicode_literals
import frappe import frappe
from frappe import _
import json import json
from frappe import _
from datetime import timedelta from datetime import timedelta
from erpnext.controllers.queries import get_match_cond from erpnext.controllers.queries import get_match_cond
from frappe.utils import flt, time_diff_in_hours, get_datetime, getdate, cint, date_diff, add_to_date from frappe.utils import flt, time_diff_in_hours, getdate
from frappe.model.document import Document from frappe.model.document import Document
from erpnext.manufacturing.doctype.workstation.workstation import (check_if_within_operating_hours,
WorkstationHolidayError)
from erpnext.manufacturing.doctype.manufacturing_settings.manufacturing_settings import get_mins_between_operations
from erpnext.setup.utils import get_exchange_rate from erpnext.setup.utils import get_exchange_rate
from erpnext.hr.utils import validate_active_employee from erpnext.hr.utils import validate_active_employee
@@ -31,6 +27,7 @@ class Timesheet(Document):
self.update_cost() self.update_cost()
self.calculate_total_amounts() self.calculate_total_amounts()
self.calculate_percentage_billed() self.calculate_percentage_billed()
self.validate_overtime()
self.set_dates() self.set_dates()
def set_employee_name(self): def set_employee_name(self):
@@ -65,6 +62,45 @@ class Timesheet(Document):
if self.total_billed_amount > 0 and self.total_billable_amount > 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 self.per_billed = (self.total_billed_amount * 100) / self.total_billable_amount
def validate_overtime(self):
total_overtime_hours= 0
overtime_type = None
for data in self.time_logs:
overtime_type = data.overtime_type
if data.is_overtime:
if frappe.db.get_single_value("Payroll Settings", "overtime_based_on") == "Timesheet":
if not self.employee:
frappe.throw("Select Employee, if applicable for overtime")
if not data.overtime_type:
frappe.throw(_("Define Overtime Type for Employee {0}").format(self.employee))
if data.overtime_on:
if data.overtime_on <= data.from_time or data.overtime_on >= data.to_time:
frappe.throw(_("Row {0}: {3} should be within {1} and {2}").format(
str(data.idx),
data.from_time,
data.to_time,
frappe.bold("Overtime On"))
)
maximum_overtime_hours_allowed = frappe.db.get_single_value("Payroll Settings", "maximum_overtime_hours_allowed")
if data.overtime_hours <= maximum_overtime_hours_allowed:
total_overtime_hours += data.overtime_hours
else:
frappe.throw(_("Row {0}: Overtime Hours can not be greater than {1} for a day. You can change this in Payroll Settings").
format(
str(data.idx),
frappe.bold(str(maximum_overtime_hours_allowed))
))
else:
frappe.throw(_('Please Set "Calculate Overtime Based On" to TimeSheet In Payroll Settings'))
if total_overtime_hours:
self.total_overtime_hours = total_overtime_hours
self.overtime_type =overtime_type
def update_billing_hours(self, args): def update_billing_hours(self, args):
if args.is_billable: if args.is_billable:
if flt(args.billing_hours) == 0.0: if flt(args.billing_hours) == 0.0:

View File

@@ -14,10 +14,16 @@
"to_time", "to_time",
"hours", "hours",
"completed", "completed",
"section_break_9",
"is_overtime",
"overtime_type",
"column_break_12",
"overtime_on",
"overtime_hours",
"section_break_7", "section_break_7",
"completed_qty", "completed_qty",
"workstation", "workstation",
"column_break_12", "column_break_18",
"operation", "operation",
"operation_id", "operation_id",
"project_details", "project_details",
@@ -70,7 +76,7 @@
"fieldname": "hours", "fieldname": "hours",
"fieldtype": "Float", "fieldtype": "Float",
"in_list_view": 1, "in_list_view": 1,
"label": "Hrs" "label": "Working Hours"
}, },
{ {
"fieldname": "to_time", "fieldname": "to_time",
@@ -262,12 +268,47 @@
"label": "Costing Amount", "label": "Costing Amount",
"print_hide": 1, "print_hide": 1,
"read_only": 1 "read_only": 1
},
{
"fieldname": "section_break_9",
"fieldtype": "Section Break"
},
{
"default": "0",
"fieldname": "is_overtime",
"fieldtype": "Check",
"label": "Is Applicable For Overtime"
},
{
"depends_on": "eval: doc.is_overtime",
"fieldname": "overtime_on",
"fieldtype": "Date",
"label": "Overtime On",
"mandatory_depends_on": "eval: doc.is_overtime"
},
{
"depends_on": "eval: doc.is_overtime",
"fieldname": "overtime_hours",
"fieldtype": "Float",
"label": "Overtime Hours",
"mandatory_depends_on": "eval: doc.is_overtime"
},
{
"depends_on": "eval: doc.is_overtime",
"fieldname": "overtime_type",
"fieldtype": "Link",
"label": "Overtime Type",
"options": "Overtime Type"
},
{
"fieldname": "column_break_18",
"fieldtype": "Column Break"
} }
], ],
"idx": 1, "idx": 1,
"istable": 1, "istable": 1,
"links": [], "links": [],
"modified": "2021-05-18 12:19:33.205940", "modified": "2021-06-10 15:17:20.846091",
"modified_by": "Administrator", "modified_by": "Administrator",
"module": "Projects", "module": "Projects",
"name": "Timesheet Detail", "name": "Timesheet Detail",