feat: explicit time period for mark attendance
This commit is contained in:
committed by
Rucha Mahabal
parent
985c47fe3b
commit
d2f86ead74
@@ -5,7 +5,8 @@
|
|||||||
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, formatdate, get_datetime, getdate, nowdate
|
from frappe.query_builder import Criterion
|
||||||
|
from frappe.utils import cint, cstr, formatdate, get_datetime, get_link_to_form, getdate, nowdate, add_days
|
||||||
|
|
||||||
from erpnext.hr.utils import get_holiday_dates_for_employee, validate_active_employee
|
from erpnext.hr.utils import get_holiday_dates_for_employee, validate_active_employee
|
||||||
|
|
||||||
@@ -221,75 +222,39 @@ def mark_bulk_attendance(data):
|
|||||||
attendance.submit()
|
attendance.submit()
|
||||||
|
|
||||||
|
|
||||||
def get_month_map():
|
|
||||||
return frappe._dict(
|
|
||||||
{
|
|
||||||
"January": 1,
|
|
||||||
"February": 2,
|
|
||||||
"March": 3,
|
|
||||||
"April": 4,
|
|
||||||
"May": 5,
|
|
||||||
"June": 6,
|
|
||||||
"July": 7,
|
|
||||||
"August": 8,
|
|
||||||
"September": 9,
|
|
||||||
"October": 10,
|
|
||||||
"November": 11,
|
|
||||||
"December": 12,
|
|
||||||
}
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
@frappe.whitelist()
|
@frappe.whitelist()
|
||||||
def get_unmarked_days(employee, month, exclude_holidays=0):
|
def get_unmarked_days(employee, from_date, to_date, exclude_holidays=0):
|
||||||
import calendar
|
|
||||||
|
|
||||||
month_map = get_month_map()
|
|
||||||
today = get_datetime()
|
|
||||||
|
|
||||||
joining_date, relieving_date = frappe.get_cached_value(
|
joining_date, relieving_date = frappe.get_cached_value(
|
||||||
"Employee", employee, ["date_of_joining", "relieving_date"]
|
"Employee", employee, ["date_of_joining", "relieving_date"]
|
||||||
)
|
)
|
||||||
start_day = 1
|
|
||||||
end_day = calendar.monthrange(today.year, month_map[month])[1] + 1
|
|
||||||
|
|
||||||
if joining_date and joining_date.year == today.year and joining_date.month == month_map[month]:
|
from_date = max(getdate(from_date), joining_date or getdate(from_date))
|
||||||
start_day = joining_date.day
|
to_date = min(getdate(to_date), relieving_date or getdate(to_date))
|
||||||
|
|
||||||
if (
|
|
||||||
relieving_date and relieving_date.year == today.year and relieving_date.month == month_map[month]
|
|
||||||
):
|
|
||||||
end_day = relieving_date.day + 1
|
|
||||||
|
|
||||||
dates_of_month = [
|
|
||||||
"{}-{}-{}".format(today.year, month_map[month], r) for r in range(start_day, end_day)
|
|
||||||
]
|
|
||||||
month_start, month_end = dates_of_month[0], dates_of_month[-1]
|
|
||||||
|
|
||||||
records = frappe.get_all(
|
records = frappe.get_all(
|
||||||
"Attendance",
|
"Attendance",
|
||||||
fields=["attendance_date", "employee"],
|
fields=["attendance_date", "employee"],
|
||||||
filters=[
|
filters=[
|
||||||
["attendance_date", ">=", month_start],
|
["attendance_date", ">=", from_date],
|
||||||
["attendance_date", "<=", month_end],
|
["attendance_date", "<=", to_date],
|
||||||
["employee", "=", employee],
|
["employee", "=", employee],
|
||||||
["docstatus", "!=", 2],
|
["docstatus", "!=", 2],
|
||||||
],
|
],
|
||||||
)
|
)
|
||||||
|
|
||||||
marked_days = [get_datetime(record.attendance_date) for record in records]
|
marked_days = [getdate(record.attendance_date) for record in records]
|
||||||
|
|
||||||
if cint(exclude_holidays):
|
if cint(exclude_holidays):
|
||||||
holiday_dates = get_holiday_dates_for_employee(employee, month_start, month_end)
|
holiday_dates = get_holiday_dates_for_employee(employee, from_date, to_date)
|
||||||
holidays = [get_datetime(record) for record in holiday_dates]
|
holidays = [getdate(record) for record in holiday_dates]
|
||||||
marked_days.extend(holidays)
|
marked_days.extend(holidays)
|
||||||
|
|
||||||
unmarked_days = []
|
unmarked_days = []
|
||||||
|
|
||||||
for date in dates_of_month:
|
while from_date <= to_date:
|
||||||
date_time = get_datetime(date)
|
if from_date not in marked_days:
|
||||||
if today.day <= date_time.day and today.month <= date_time.month:
|
unmarked_days.append(from_date)
|
||||||
break
|
|
||||||
if date_time not in marked_days:
|
from_date = add_days(from_date, 1)
|
||||||
unmarked_days.append(date)
|
|
||||||
|
|
||||||
return unmarked_days
|
return unmarked_days
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
frappe.listview_settings['Attendance'] = {
|
frappe.listview_settings["Attendance"] = {
|
||||||
add_fields: ["status", "attendance_date"],
|
add_fields: ["status", "attendance_date"],
|
||||||
|
|
||||||
get_indicator: function (doc) {
|
get_indicator: function (doc) {
|
||||||
if (["Present", "Work From Home"].includes(doc.status)) {
|
if (["Present", "Work From Home"].includes(doc.status)) {
|
||||||
return [__(doc.status), "green", "status,=," + doc.status];
|
return [__(doc.status), "green", "status,=," + doc.status];
|
||||||
@@ -10,92 +11,74 @@ frappe.listview_settings['Attendance'] = {
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
onload: function(list_view) {
|
onload: function (list_view) {
|
||||||
let me = this;
|
let me = this;
|
||||||
const months = moment.months();
|
|
||||||
const curMonth = moment().format("MMMM");
|
list_view.page.add_inner_button(__("Mark Attendance"), function () {
|
||||||
months.splice(months.indexOf(curMonth) + 1);
|
let first_day_of_month = moment().startOf('month');
|
||||||
list_view.page.add_inner_button(__("Mark Attendance"), function() {
|
|
||||||
|
if (moment().toDate().getDate() === 1) {
|
||||||
|
first_day_of_month = first_day_of_month.subtract(1, "month");
|
||||||
|
}
|
||||||
|
|
||||||
let dialog = new frappe.ui.Dialog({
|
let dialog = new frappe.ui.Dialog({
|
||||||
title: __("Mark Attendance"),
|
title: __("Mark Attendance"),
|
||||||
fields: [{
|
fields: [
|
||||||
fieldname: 'employee',
|
{
|
||||||
label: __('For Employee'),
|
fieldname: "employee",
|
||||||
fieldtype: 'Link',
|
label: __("For Employee"),
|
||||||
options: 'Employee',
|
fieldtype: "Link",
|
||||||
|
options: "Employee",
|
||||||
get_query: () => {
|
get_query: () => {
|
||||||
return {query: "erpnext.controllers.queries.employee_query"};
|
return {
|
||||||
|
query: "erpnext.controllers.queries.employee_query",
|
||||||
|
};
|
||||||
},
|
},
|
||||||
reqd: 1,
|
reqd: 1,
|
||||||
onchange: function() {
|
onchange: () => me.reset_dialog(dialog),
|
||||||
dialog.set_df_property("unmarked_days", "hidden", 1);
|
|
||||||
dialog.set_df_property("status", "hidden", 1);
|
|
||||||
dialog.set_df_property("exclude_holidays", "hidden", 1);
|
|
||||||
dialog.set_df_property("month", "value", '');
|
|
||||||
dialog.set_df_property("unmarked_days", "options", []);
|
|
||||||
dialog.no_unmarked_days_left = false;
|
|
||||||
}
|
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
label: __("For Month"),
|
fieldtype: "Section Break",
|
||||||
fieldtype: "Select",
|
fieldname: "time_period_section",
|
||||||
fieldname: "month",
|
hidden: 1,
|
||||||
options: months,
|
},
|
||||||
|
{
|
||||||
|
label: __("Start"),
|
||||||
|
fieldtype: "Date",
|
||||||
|
fieldname: "from_date",
|
||||||
reqd: 1,
|
reqd: 1,
|
||||||
onchange: function() {
|
default: first_day_of_month.toDate(),
|
||||||
if (dialog.fields_dict.employee.value && dialog.fields_dict.month.value) {
|
onchange: () => me.get_unmarked_days(dialog),
|
||||||
dialog.set_df_property("status", "hidden", 0);
|
},
|
||||||
dialog.set_df_property("exclude_holidays", "hidden", 0);
|
{
|
||||||
dialog.set_df_property("unmarked_days", "options", []);
|
fieldtype: "Column Break",
|
||||||
dialog.no_unmarked_days_left = false;
|
fieldname: "time_period_column",
|
||||||
me.get_multi_select_options(
|
},
|
||||||
dialog.fields_dict.employee.value,
|
{
|
||||||
dialog.fields_dict.month.value,
|
label: __("End"),
|
||||||
dialog.fields_dict.exclude_holidays.get_value()
|
fieldtype: "Date",
|
||||||
).then(options => {
|
fieldname: "to_date",
|
||||||
if (options.length > 0) {
|
reqd: 1,
|
||||||
dialog.set_df_property("unmarked_days", "hidden", 0);
|
default: moment().subtract(1, 'days').toDate(),
|
||||||
dialog.set_df_property("unmarked_days", "options", options);
|
onchange: () => me.get_unmarked_days(dialog),
|
||||||
} else {
|
},
|
||||||
dialog.no_unmarked_days_left = true;
|
{
|
||||||
}
|
fieldtype: "Section Break",
|
||||||
});
|
fieldname: "days_section",
|
||||||
}
|
hidden: 1,
|
||||||
}
|
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
label: __("Status"),
|
label: __("Status"),
|
||||||
fieldtype: "Select",
|
fieldtype: "Select",
|
||||||
fieldname: "status",
|
fieldname: "status",
|
||||||
options: ["Present", "Absent", "Half Day", "Work From Home"],
|
options: ["Present", "Absent", "Half Day", "Work From Home"],
|
||||||
hidden: 1,
|
|
||||||
reqd: 1,
|
reqd: 1,
|
||||||
|
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
label: __("Exclude Holidays"),
|
label: __("Exclude Holidays"),
|
||||||
fieldtype: "Check",
|
fieldtype: "Check",
|
||||||
fieldname: "exclude_holidays",
|
fieldname: "exclude_holidays",
|
||||||
hidden: 1,
|
onchange: () => me.get_unmarked_days(dialog),
|
||||||
onchange: function() {
|
|
||||||
if (dialog.fields_dict.employee.value && dialog.fields_dict.month.value) {
|
|
||||||
dialog.set_df_property("status", "hidden", 0);
|
|
||||||
dialog.set_df_property("unmarked_days", "options", []);
|
|
||||||
dialog.no_unmarked_days_left = false;
|
|
||||||
me.get_multi_select_options(
|
|
||||||
dialog.fields_dict.employee.value,
|
|
||||||
dialog.fields_dict.month.value,
|
|
||||||
dialog.fields_dict.exclude_holidays.get_value()
|
|
||||||
).then(options => {
|
|
||||||
if (options.length > 0) {
|
|
||||||
dialog.set_df_property("unmarked_days", "hidden", 0);
|
|
||||||
dialog.set_df_property("unmarked_days", "options", options);
|
|
||||||
} else {
|
|
||||||
dialog.no_unmarked_days_left = true;
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
label: __("Unmarked Attendance for days"),
|
label: __("Unmarked Attendance for days"),
|
||||||
@@ -103,64 +86,110 @@ frappe.listview_settings['Attendance'] = {
|
|||||||
fieldtype: "MultiCheck",
|
fieldtype: "MultiCheck",
|
||||||
options: [],
|
options: [],
|
||||||
columns: 2,
|
columns: 2,
|
||||||
hidden: 1
|
},
|
||||||
}],
|
],
|
||||||
primary_action(data) {
|
primary_action(data) {
|
||||||
if (cur_dialog.no_unmarked_days_left) {
|
if (cur_dialog.no_unmarked_days_left) {
|
||||||
frappe.msgprint(__("Attendance for the month of {0} , has already been marked for the Employee {1}",
|
frappe.msgprint(
|
||||||
[dialog.fields_dict.month.value, dialog.fields_dict.employee.value]));
|
__(
|
||||||
|
"Attendance from {0} to {1} has already been marked for the Employee {2}",
|
||||||
|
[data.from_date, data.to_date, data.employee]
|
||||||
|
)
|
||||||
|
);
|
||||||
} else {
|
} else {
|
||||||
frappe.confirm(__('Mark attendance as {0} for {1} on selected dates?', [data.status, data.month]), () => {
|
frappe.confirm(
|
||||||
|
__("Mark attendance as {0} for {1} on selected dates?", [
|
||||||
|
data.status,
|
||||||
|
data.employee,
|
||||||
|
]),
|
||||||
|
() => {
|
||||||
frappe.call({
|
frappe.call({
|
||||||
method: "erpnext.hr.doctype.attendance.attendance.mark_bulk_attendance",
|
method: "erpnext.hr.doctype.attendance.attendance.mark_bulk_attendance",
|
||||||
args: {
|
args: {
|
||||||
data: data
|
data: data,
|
||||||
},
|
},
|
||||||
callback: function (r) {
|
callback: function (r) {
|
||||||
if (r.message === 1) {
|
if (r.message === 1) {
|
||||||
frappe.show_alert({
|
frappe.show_alert({
|
||||||
message: __("Attendance Marked"),
|
message: __("Attendance Marked"),
|
||||||
indicator: 'blue'
|
indicator: "blue",
|
||||||
});
|
});
|
||||||
cur_dialog.hide();
|
cur_dialog.hide();
|
||||||
}
|
}
|
||||||
|
},
|
||||||
|
});
|
||||||
}
|
}
|
||||||
});
|
);
|
||||||
});
|
|
||||||
}
|
}
|
||||||
dialog.hide();
|
dialog.hide();
|
||||||
list_view.refresh();
|
list_view.refresh();
|
||||||
},
|
},
|
||||||
primary_action_label: __('Mark Attendance')
|
primary_action_label: __("Mark Attendance"),
|
||||||
|
|
||||||
});
|
});
|
||||||
dialog.show();
|
dialog.show();
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
|
|
||||||
get_multi_select_options: function(employee, month, exclude_holidays) {
|
reset_dialog: function (dialog) {
|
||||||
return new Promise(resolve => {
|
let fields = dialog.fields_dict;
|
||||||
frappe.call({
|
|
||||||
method: 'erpnext.hr.doctype.attendance.attendance.get_unmarked_days',
|
dialog.set_df_property(
|
||||||
|
"time_period_section",
|
||||||
|
"hidden",
|
||||||
|
fields.employee.value ? 0 : 1
|
||||||
|
);
|
||||||
|
|
||||||
|
dialog.set_df_property("days_section", "hidden", 1);
|
||||||
|
dialog.set_df_property("unmarked_days", "options", []);
|
||||||
|
dialog.no_unmarked_days_left = false;
|
||||||
|
fields.exclude_holidays.value = false;
|
||||||
|
|
||||||
|
fields.to_date.datepicker.update({
|
||||||
|
maxDate: moment().subtract(1, 'days').toDate()
|
||||||
|
});
|
||||||
|
|
||||||
|
this.get_unmarked_days(dialog)
|
||||||
|
},
|
||||||
|
|
||||||
|
get_unmarked_days: function (dialog) {
|
||||||
|
let fields = dialog.fields_dict;
|
||||||
|
if (fields.employee.value && fields.from_date.value && fields.to_date.value) {
|
||||||
|
dialog.set_df_property("days_section", "hidden", 0);
|
||||||
|
dialog.set_df_property("status", "hidden", 0);
|
||||||
|
dialog.set_df_property("exclude_holidays", "hidden", 0);
|
||||||
|
dialog.no_unmarked_days_left = false;
|
||||||
|
|
||||||
|
frappe
|
||||||
|
.call({
|
||||||
|
method: "erpnext.hr.doctype.attendance.attendance.get_unmarked_days",
|
||||||
async: false,
|
async: false,
|
||||||
args: {
|
args: {
|
||||||
employee: employee,
|
employee: fields.employee.value,
|
||||||
month: month,
|
from_date: fields.from_date.value,
|
||||||
exclude_holidays: exclude_holidays
|
to_date: fields.to_date.value,
|
||||||
}
|
exclude_holidays: fields.exclude_holidays.value,
|
||||||
}).then(r => {
|
},
|
||||||
|
})
|
||||||
|
.then((r) => {
|
||||||
var options = [];
|
var options = [];
|
||||||
|
|
||||||
for (var d in r.message) {
|
for (var d in r.message) {
|
||||||
var momentObj = moment(r.message[d], 'YYYY-MM-DD');
|
var momentObj = moment(r.message[d], "YYYY-MM-DD");
|
||||||
var date = momentObj.format('DD-MM-YYYY');
|
var date = momentObj.format("DD-MM-YYYY");
|
||||||
options.push({
|
options.push({
|
||||||
"label": date,
|
label: date,
|
||||||
"value": r.message[d],
|
value: r.message[d],
|
||||||
"checked": 1
|
checked: 1,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
resolve(options);
|
|
||||||
});
|
dialog.set_df_property(
|
||||||
|
"unmarked_days",
|
||||||
|
"options",
|
||||||
|
options.length > 0 ? options : []
|
||||||
|
);
|
||||||
|
dialog.no_unmarked_days_left = options.length === 0;
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
},
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user