Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: holiday list calendar event #1436

Merged
merged 15 commits into from
Jul 15, 2021
85 changes: 85 additions & 0 deletions erpnext/hr/custom/event.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
{
"custom_fields": [
{
"_assign": null,
"_comments": null,
"_liked_by": null,
"_user_tags": null,
"allow_in_quick_entry": 0,
"allow_on_submit": 0,
"bold": 0,
"collapsible": 0,
"collapsible_depends_on": null,
"columns": 0,
"creation": "2021-07-12 04:03:31.815370",
"default": null,
"depends_on": null,
"description": null,
"docstatus": 0,
"dt": "Event",
"fetch_from": null,
"fetch_if_empty": 0,
"fieldname": "holiday_list",
"fieldtype": "Link",
"hidden": 0,
"idx": 4,
"ignore_user_permissions": 0,
"ignore_xss_filter": 0,
"in_global_search": 0,
"in_list_view": 0,
"in_standard_filter": 0,
"insert_after": "event_category",
"label": "Holiday List",
"length": 0,
"modified": "2021-07-12 04:03:31.815370",
"modified_by": "Administrator",
"name": "Event-holiday_list",
"no_copy": 0,
"options": "Holiday List",
"owner": "Administrator",
"parent": null,
"parentfield": null,
"parenttype": null,
"permlevel": 0,
"precision": "",
"print_hide": 0,
"print_hide_if_no_value": 0,
"print_width": null,
"read_only": 0,
"report_hide": 0,
"reqd": 0,
"search_index": 0,
"translatable": 0,
"unique": 0,
"width": null
}
],
"custom_perms": [],
"doctype": "Event",
"property_setters": [
{
"_assign": null,
"_comments": null,
"_liked_by": null,
"_user_tags": null,
"creation": "2021-07-12 04:42:04.488238",
"default_value": null,
"doc_type": "Event",
"docstatus": 0,
"doctype_or_field": "DocField",
"field_name": "event_category",
"idx": 0,
"modified": "2021-07-12 04:42:04.488238",
"modified_by": "Administrator",
"name": "Event-event_category-options",
"owner": "Administrator",
"parent": null,
"parentfield": null,
"parenttype": null,
"property": "options",
"property_type": "Text",
"value": "Event\nMeeting\nCall\nSent/Received Email\nHoliday\nOther"
}
],
"sync_on_migrate": 1
}
122 changes: 42 additions & 80 deletions erpnext/hr/doctype/holiday/holiday.json
Original file line number Diff line number Diff line change
@@ -1,87 +1,49 @@
{
"allow_copy": 0,
"allow_import": 0,
"allow_rename": 0,
"beta": 0,
"creation": "2013-02-22 01:27:46",
"custom": 0,
"docstatus": 0,
"doctype": "DocType",
"document_type": "Setup",
"editable_grid": 1,
"creation": "2013-02-22 01:27:46",
"doctype": "DocType",
"document_type": "Setup",
"editable_grid": 1,
"engine": "InnoDB",
"field_order": [
"holiday_date",
"description",
"is_weekly_off"
],
"fields": [
{
"allow_on_submit": 0,
"bold": 0,
"collapsible": 0,
"fieldname": "holiday_date",
"fieldtype": "Date",
"hidden": 0,
"ignore_user_permissions": 0,
"ignore_xss_filter": 0,
"in_filter": 0,
"in_list_view": 1,
"label": "Date",
"length": 0,
"no_copy": 0,
"oldfieldname": "holiday_date",
"oldfieldtype": "Date",
"permlevel": 0,
"print_hide": 0,
"print_hide_if_no_value": 0,
"read_only": 0,
"report_hide": 0,
"reqd": 1,
"search_index": 0,
"set_only_once": 0,
"unique": 0
},
"fieldname": "holiday_date",
"fieldtype": "Date",
"in_list_view": 1,
"label": "Date",
"oldfieldname": "holiday_date",
"oldfieldtype": "Date",
"reqd": 1
},
{
"allow_on_submit": 0,
"bold": 0,
"collapsible": 0,
"fieldname": "description",
"fieldtype": "Text Editor",
"hidden": 0,
"ignore_user_permissions": 0,
"ignore_xss_filter": 0,
"in_filter": 0,
"in_list_view": 1,
"label": "Description",
"length": 0,
"no_copy": 0,
"permlevel": 0,
"print_hide": 0,
"print_hide_if_no_value": 0,
"print_width": "300px",
"read_only": 0,
"report_hide": 0,
"reqd": 1,
"search_index": 0,
"set_only_once": 0,
"unique": 0,
"fieldname": "description",
"fieldtype": "Text Editor",
"in_list_view": 1,
"label": "Description",
"print_width": "300px",
"reqd": 1,
"width": "300px"
},
{
"default": "0",
"fieldname": "is_weekly_off",
"fieldtype": "Check",
"label": "Is Weekly Off",
"read_only": 1
}
],
"hide_heading": 0,
"hide_toolbar": 0,
"idx": 1,
"image_view": 0,
"in_create": 0,

"is_submittable": 0,
"issingle": 0,
"istable": 1,
"max_attachments": 0,
"modified": "2016-07-11 03:28:00.660849",
"modified_by": "Administrator",
"module": "HR",
"name": "Holiday",
"owner": "Administrator",
"permissions": [],
"quick_entry": 0,
"read_only": 0,
"read_only_onload": 0,
"sort_order": "ASC",
"track_seen": 0
],
"idx": 1,
"istable": 1,
"modified": "2021-07-13 22:20:34.900236",
"modified_by": "Administrator",
"module": "HR",
"name": "Holiday",
"owner": "Administrator",
"permissions": [],
"sort_field": "modified",
"sort_order": "ASC"
}
42 changes: 41 additions & 1 deletion erpnext/hr/doctype/holiday_list/holiday_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from __future__ import unicode_literals
import frappe
import json
from frappe.utils import cint, getdate, formatdate, today, add_days, get_date_str
from frappe.utils import cint, getdate, formatdate, today, add_days, get_date_str, add_to_date
from frappe import throw, _
from frappe.model.document import Document

Expand All @@ -24,6 +24,7 @@ def get_weekly_off_dates(self):
ch.description = self.weekly_off
ch.holiday_date = d
ch.idx = last_idx + i + 1
ch.is_weekly_off = 1

def validate_values(self):
if not self.weekly_off:
Expand Down Expand Up @@ -62,6 +63,45 @@ def get_weekly_off_date_list(self, start_date, end_date):
def clear_table(self):
self.set('holidays', [])

def on_update(self):
frappe.enqueue(create_events, holiday_list=self.name)

def create_events(holiday_list):
def create_holiday_event(holiday, holiday_list):
frappe.get_doc({
"doctype": "Event",
"subject": "Holiday " + add_to_date(holiday.holiday_date, as_string=True),
"starts_on": holiday.holiday_date,
"event_category": "Holiday",
"holiday_list": holiday_list,
"event_type": "Public",
"all_day":1
}).insert(ignore_permissions=True)

doc = frappe.get_doc("Holiday List", holiday_list)
calendar_events = frappe.get_all("Event", filters={"holiday_list": doc.name}, fields=["name", "starts_on"])
if not calendar_events:
for holiday in doc.holidays:
if holiday.is_weekly_off:
continue

create_holiday_event(holiday, doc.name)
else:
holidays = [frappe.utils.get_datetime_str(holiday.holiday_date) for holiday in doc.holidays if not holiday.is_weekly_off]
starts_on = []

for event in calendar_events:
starts_on.append(frappe.utils.get_datetime_str(event.starts_on))
if not frappe.utils.get_datetime_str(event.starts_on) in holidays:
frappe.delete_doc("Event", event.name)

for holiday in doc.holidays:
if holiday.is_weekly_off or frappe.utils.get_datetime_str(holiday.holiday_date) in starts_on:
continue

create_holiday_event(holiday, doc.name)


@frappe.whitelist()
def get_events(start, end, filters=None):
"""Returns events for Gantt / Calendar view rendering.
Expand Down