diff --git a/hr/doctype/holiday_block_list/holiday_block_list.py b/hr/doctype/holiday_block_list/holiday_block_list.py index 619f373e37..87ab7052f1 100644 --- a/hr/doctype/holiday_block_list/holiday_block_list.py +++ b/hr/doctype/holiday_block_list/holiday_block_list.py @@ -23,7 +23,8 @@ class DocType: test_records = [[{ "doctype":"Holiday Block List", "holiday_block_list_name": "_Test Holiday Block List", - "year": "_Test Fiscal Year" + "year": "_Test Fiscal Year", + "company": "_Test Company" }, { "doctype": "Holiday Block List Date", "parent": "_Test Holiday Block List", diff --git a/hr/doctype/holiday_block_list/holiday_block_list.txt b/hr/doctype/holiday_block_list/holiday_block_list.txt index 31b2f98ee1..23b70e2ecf 100644 --- a/hr/doctype/holiday_block_list/holiday_block_list.txt +++ b/hr/doctype/holiday_block_list/holiday_block_list.txt @@ -2,7 +2,7 @@ { "creation": "2013-02-04 15:31:29", "docstatus": 0, - "modified": "2013-02-06 14:39:09", + "modified": "2013-02-07 08:47:25", "modified_by": "Administrator", "owner": "Administrator" }, @@ -53,6 +53,21 @@ "options": "Fiscal Year", "reqd": 1 }, + { + "doctype": "DocField", + "fieldname": "company", + "fieldtype": "Link", + "label": "Company", + "options": "Company", + "reqd": 1 + }, + { + "description": "If not checked, the list will have to be added to each Department where it has to be applied.", + "doctype": "DocField", + "fieldname": "applies_to_all_departments", + "fieldtype": "Check", + "label": "Applies to all Departments" + }, { "description": "Stop users from making Leave Applications on following days.", "doctype": "DocField", diff --git a/hr/doctype/leave_application/leave_application.py b/hr/doctype/leave_application/leave_application.py index cab8f17b69..9c6278d902 100755 --- a/hr/doctype/leave_application/leave_application.py +++ b/hr/doctype/leave_application/leave_application.py @@ -46,22 +46,39 @@ class DocType: raise_exception=True) def validate_block_days(self): - from_date = getdate(self.doc.from_date) - to_date = getdate(self.doc.to_date) - + for block_list in self.get_applicable_block_lists(): + self.check_block_dates(block_list) + + def get_applicable_block_lists(self): + block_lists = [] + def add_block_list(block_list): + if block_list: + if not self.is_user_in_allow_list(block_list): + block_lists.append(block_list) + + # per department department = webnotes.conn.get_value("Employee", self.doc.employee, "department") if department: block_list = webnotes.conn.get_value("Department", department, "holiday_block_list") - if block_list: - if self.is_user_in_allow_list(block_list): - return - for d in webnotes.conn.sql("""select block_date, reason from - `tabHoliday Block List Date` where parent=%s""", block_list, as_dict=1): - block_date = getdate(d.block_date) - if block_date > from_date and block_date < to_date: - webnotes.msgprint(_("You cannot apply for a leave on the following date because it is blocked") - + ": " + formatdate(d.block_date) + _(" Reason: ") + d.reason) - raise LeaveDayBlockedError + add_block_list(block_list) + + # global + for block_list in webnotes.conn.sql_list("""select name from `tabHoliday Block List` + where ifnull(applies_to_all_departments,0)=1 and company=%s""", self.doc.company): + add_block_list(block_list) + + return block_lists + + def check_block_dates(self, block_list): + from_date = getdate(self.doc.from_date) + to_date = getdate(self.doc.to_date) + for d in webnotes.conn.sql("""select block_date, reason from + `tabHoliday Block List Date` where parent=%s""", block_list, as_dict=1): + block_date = getdate(d.block_date) + if block_date > from_date and block_date < to_date: + webnotes.msgprint(_("You cannot apply for a leave on the following date because it is blocked") + + ": " + formatdate(d.block_date) + _(" Reason: ") + d.reason) + raise LeaveDayBlockedError def is_user_in_allow_list(self, block_list): return webnotes.session.user in webnotes.conn.sql_list("""select allow_user diff --git a/hr/doctype/leave_application/leave_application.txt b/hr/doctype/leave_application/leave_application.txt index 4436d3c2f8..e105dff0f1 100644 --- a/hr/doctype/leave_application/leave_application.txt +++ b/hr/doctype/leave_application/leave_application.txt @@ -1,8 +1,8 @@ [ { - "creation": "2013-01-10 16:34:14", + "creation": "2013-02-02 14:40:08", "docstatus": 0, - "modified": "2013-02-01 10:34:14", + "modified": "2013-02-07 08:54:22", "modified_by": "Administrator", "owner": "Administrator" }, @@ -180,6 +180,15 @@ "reqd": 1, "search_index": 0 }, + { + "doctype": "DocField", + "fieldname": "company", + "fieldtype": "Link", + "label": "Company", + "options": "Company", + "permlevel": 0, + "reqd": 1 + }, { "doctype": "DocField", "fieldname": "letter_head", @@ -241,4 +250,4 @@ "role": "Leave Approver", "submit": 0 } -] +] \ No newline at end of file diff --git a/hr/doctype/leave_application/test_leave_application.py b/hr/doctype/leave_application/test_leave_application.py index e485289ffa..e7c1bccd00 100644 --- a/hr/doctype/leave_application/test_leave_application.py +++ b/hr/doctype/leave_application/test_leave_application.py @@ -4,20 +4,33 @@ import unittest from hr.doctype.leave_application.leave_application import LeaveDayBlockedError class TestLeaveApplication(unittest.TestCase): + def get_application(self): + application = webnotes.model_wrapper(test_records[1]) + application.doc.from_date = "2013-01-01" + application.doc.to_date = "2013-01-05" + return application + def test_block_list(self): import webnotes webnotes.conn.set_value("Employee", "_T-Employee-0001", "department", "_Test Department with Block List") - application = webnotes.model_wrapper(test_records[1]) - application.doc.from_date = "2013-01-01" - application.doc.to_date = "2013-01-05" + application = self.get_application() self.assertRaises(LeaveDayBlockedError, application.insert) webnotes.session.user = "test1@erpnext.com" webnotes.get_obj("Profile", "test1@erpnext.com").add_role("HR User") self.assertTrue(application.insert()) + def test_global_block_list(self): + application = self.get_application() + webnotes.conn.set_value("Holiday Block List", "_Test Holiday Block List", + "applies_to_all_departments", 1) + webnotes.conn.set_value("Employee", "_T-Employee-0001", "department", + "_Test Department") + webnotes.session.user = "test@erpnext.com" + + self.assertRaises(LeaveDayBlockedError, application.insert) test_records = [ [{ @@ -35,7 +48,8 @@ test_records = [ "to_date": "2013-05-05", "posting_date": "2013-01-02", "fiscal_year": "_Test Fiscal Year", - "employee": "_T-Employee-0001" + "employee": "_T-Employee-0001", + "company": "_Test Company" }]] if __name__=="__main__": diff --git a/patches/february_2013/update_company_in_leave_application.py b/patches/february_2013/update_company_in_leave_application.py new file mode 100644 index 0000000000..098c7e32b1 --- /dev/null +++ b/patches/february_2013/update_company_in_leave_application.py @@ -0,0 +1,9 @@ +def execute(): + import webnotes + webnotes.conn.sql("""update `tabLeave Application`, `tabEmployee` set + `tabLeave Application`.company = `tabEmployee`.company where + `tabLeave Application`.employee = `tabEmployee`.name""") + + company = webnotes.conn.get_default("company") + webnotes.conn.sql("""update `tabLeave Application` + set company = %s where ifnull(company,'')=''""", company) \ No newline at end of file diff --git a/patches/patch_list.py b/patches/patch_list.py index cf9c189cb4..467c6b05cf 100644 --- a/patches/patch_list.py +++ b/patches/patch_list.py @@ -166,4 +166,5 @@ patch_list = [ "patches.february_2013.remove_sales_order_pending_items", "patches.february_2013.account_negative_balance", "patches.february_2013.remove_account_utils_folder", + "patches.february_2013.update_company_in_leave_application" ] \ No newline at end of file