From 7e207c8901be2cfd24afc7d9970e4a0ca42bd0b9 Mon Sep 17 00:00:00 2001 From: marination Date: Thu, 31 Mar 2022 16:29:18 +0530 Subject: [PATCH 01/22] fix: Call Redisearch index creation functions on enabling redisearch in settings --- .../e_commerce_settings.json | 12 ++++++- .../e_commerce_settings.py | 17 ++++++++++ erpnext/e_commerce/redisearch_utils.py | 34 +++++++++---------- erpnext/templates/pages/product_search.py | 10 +++--- 4 files changed, 50 insertions(+), 23 deletions(-) diff --git a/erpnext/e_commerce/doctype/e_commerce_settings/e_commerce_settings.json b/erpnext/e_commerce/doctype/e_commerce_settings/e_commerce_settings.json index d5fb9697f8..62505e61db 100644 --- a/erpnext/e_commerce/doctype/e_commerce_settings/e_commerce_settings.json +++ b/erpnext/e_commerce/doctype/e_commerce_settings/e_commerce_settings.json @@ -47,6 +47,7 @@ "item_search_settings_section", "redisearch_warning", "search_index_fields", + "is_redisearch_enabled", "show_categories_in_search_autocomplete", "is_redisearch_loaded", "shop_by_category_section", @@ -303,6 +304,7 @@ }, { "default": "1", + "depends_on": "is_redisearch_enabled", "fieldname": "show_categories_in_search_autocomplete", "fieldtype": "Check", "label": "Show Categories in Search Autocomplete", @@ -365,12 +367,19 @@ "fieldname": "show_price_in_quotation", "fieldtype": "Check", "label": "Show Price in Quotation" + }, + { + "default": "0", + "fieldname": "is_redisearch_enabled", + "fieldtype": "Check", + "label": "Enable Redisearch", + "read_only_depends_on": "eval:!doc.is_redisearch_loaded" } ], "index_web_pages_for_search": 1, "issingle": 1, "links": [], - "modified": "2021-09-02 14:02:44.785824", + "modified": "2022-03-31 16:01:46.308663", "modified_by": "Administrator", "module": "E-commerce", "name": "E Commerce Settings", @@ -389,5 +398,6 @@ ], "sort_field": "modified", "sort_order": "DESC", + "states": [], "track_changes": 1 } \ No newline at end of file diff --git a/erpnext/e_commerce/doctype/e_commerce_settings/e_commerce_settings.py b/erpnext/e_commerce/doctype/e_commerce_settings/e_commerce_settings.py index b5cd067e38..2bb4ad6978 100644 --- a/erpnext/e_commerce/doctype/e_commerce_settings/e_commerce_settings.py +++ b/erpnext/e_commerce/doctype/e_commerce_settings/e_commerce_settings.py @@ -9,6 +9,7 @@ from frappe.utils import comma_and, flt, unique from erpnext.e_commerce.redisearch_utils import ( create_website_items_index, + define_autocomplete_dictionary, get_indexable_web_fields, is_search_module_loaded, ) @@ -21,6 +22,8 @@ class ShoppingCartSetupError(frappe.ValidationError): class ECommerceSettings(Document): def onload(self): self.get("__onload").quotation_series = frappe.get_meta("Quotation").get_options("naming_series") + + # flag >> if redisearch is installed and loaded self.is_redisearch_loaded = is_search_module_loaded() def validate(self): @@ -34,6 +37,20 @@ class ECommerceSettings(Document): frappe.clear_document_cache("E Commerce Settings", "E Commerce Settings") + self.is_redisearch_enabled_pre_save = frappe.db.get_single_value( + "E Commerce Settings", "is_redisearch_enabled" + ) + + def after_save(self): + self.create_redisearch_indexes() + + def create_redisearch_indexes(self): + # if redisearch is enabled (value changed) create indexes and dictionary + value_changed = self.is_redisearch_enabled != self.is_redisearch_enabled_pre_save + if self.is_redisearch_loaded and self.is_redisearch_enabled and value_changed: + define_autocomplete_dictionary() + create_website_items_index() + def validate_field_filters(self): if not (self.enable_field_filters and self.filter_fields): return diff --git a/erpnext/e_commerce/redisearch_utils.py b/erpnext/e_commerce/redisearch_utils.py index 82829bf1ef..78cc05af38 100644 --- a/erpnext/e_commerce/redisearch_utils.py +++ b/erpnext/e_commerce/redisearch_utils.py @@ -22,6 +22,12 @@ def get_indexable_web_fields(): return [df.fieldname for df in valid_fields] +def is_redisearch_enabled(): + "Return True only if redisearch is loaded and enabled." + is_redisearch_enabled = frappe.db.get_single_value("E Commerce Settings", "is_redisearch_enabled") + return is_search_module_loaded() and is_redisearch_enabled + + def is_search_module_loaded(): try: cache = frappe.cache() @@ -35,11 +41,11 @@ def is_search_module_loaded(): return False -def if_redisearch_loaded(function): - "Decorator to check if Redisearch is loaded." +def if_redisearch_enabled(function): + "Decorator to check if Redisearch is enabled." def wrapper(*args, **kwargs): - if is_search_module_loaded(): + if is_redisearch_enabled(): func = function(*args, **kwargs) return func return @@ -51,7 +57,7 @@ def make_key(key): return "{0}|{1}".format(frappe.conf.db_name, key).encode("utf-8") -@if_redisearch_loaded +@if_redisearch_enabled def create_website_items_index(): "Creates Index Definition." @@ -91,7 +97,7 @@ def to_search_field(field): return TextField(field) -@if_redisearch_loaded +@if_redisearch_enabled def insert_item_to_index(website_item_doc): # Insert item to index key = get_cache_key(website_item_doc.name) @@ -104,7 +110,7 @@ def insert_item_to_index(website_item_doc): insert_to_name_ac(website_item_doc.web_item_name, website_item_doc.name) -@if_redisearch_loaded +@if_redisearch_enabled def insert_to_name_ac(web_name, doc_name): ac = AutoCompleter(make_key(WEBSITE_ITEM_NAME_AUTOCOMPLETE), conn=frappe.cache()) ac.add_suggestions(Suggestion(web_name, payload=doc_name)) @@ -120,14 +126,14 @@ def create_web_item_map(website_item_doc): return web_item -@if_redisearch_loaded +@if_redisearch_enabled def update_index_for_item(website_item_doc): # Reinsert to Cache insert_item_to_index(website_item_doc) define_autocomplete_dictionary() -@if_redisearch_loaded +@if_redisearch_enabled def delete_item_from_index(website_item_doc): cache = frappe.cache() key = get_cache_key(website_item_doc.name) @@ -141,7 +147,7 @@ def delete_item_from_index(website_item_doc): return True -@if_redisearch_loaded +@if_redisearch_enabled def delete_from_ac_dict(website_item_doc): """Removes this items's name from autocomplete dictionary""" cache = frappe.cache() @@ -149,7 +155,7 @@ def delete_from_ac_dict(website_item_doc): name_ac.delete(website_item_doc.web_item_name) -@if_redisearch_loaded +@if_redisearch_enabled def define_autocomplete_dictionary(): """Creates an autocomplete search dictionary for `name`. Also creats autocomplete dictionary for `categories` if @@ -182,7 +188,7 @@ def define_autocomplete_dictionary(): return True -@if_redisearch_loaded +@if_redisearch_enabled def reindex_all_web_items(): items = frappe.get_all("Website Item", fields=get_fields_indexed(), filters={"published": True}) @@ -208,9 +214,3 @@ def get_fields_indexed(): fields_to_index = fields_to_index + mandatory_fields return fields_to_index - - -# TODO: Remove later -# # Figure out a way to run this at startup -define_autocomplete_dictionary() -create_website_items_index() diff --git a/erpnext/templates/pages/product_search.py b/erpnext/templates/pages/product_search.py index 77a749ef9e..ce04068cf6 100644 --- a/erpnext/templates/pages/product_search.py +++ b/erpnext/templates/pages/product_search.py @@ -9,7 +9,7 @@ from erpnext.e_commerce.redisearch_utils import ( WEBSITE_ITEM_CATEGORY_AUTOCOMPLETE, WEBSITE_ITEM_INDEX, WEBSITE_ITEM_NAME_AUTOCOMPLETE, - is_search_module_loaded, + is_redisearch_enabled, make_key, ) from erpnext.e_commerce.shopping_cart.product_info import set_product_info_for_website @@ -74,8 +74,8 @@ def search(query): def product_search(query, limit=10, fuzzy_search=True): search_results = {"from_redisearch": True, "results": []} - if not is_search_module_loaded(): - # Redisearch module not loaded + if not is_redisearch_enabled(): + # Redisearch module not enabled search_results["from_redisearch"] = False search_results["results"] = get_product_data(query, 0, limit) return search_results @@ -121,8 +121,8 @@ def convert_to_dict(redis_search_doc): def get_category_suggestions(query): search_results = {"results": []} - if not is_search_module_loaded(): - # Redisearch module not loaded, query db + if not is_redisearch_enabled(): + # Redisearch module not enabled, query db categories = frappe.db.get_all( "Item Group", filters={"name": ["like", "%{0}%".format(query)], "show_in_website": 1}, From 07f17453cd2b673311029764135c3e26128905b8 Mon Sep 17 00:00:00 2001 From: marination Date: Fri, 1 Apr 2022 18:47:01 +0530 Subject: [PATCH 02/22] fix: Use Payload in AutoCompleter (categories in search) and misc - Separate Item group and Item autocomplete dict definition - Add payload along with Item group, containing namke and route - Pass weightage while defining item group autocomplete dict (auto sort) - Use payload while getting results for categories in search - Remove check to show categories, always show - Search fields mandatory if reidsearch enabled - Code separation (rough) --- .../e_commerce_settings.json | 12 +---- erpnext/e_commerce/redisearch_utils.py | 46 +++++++++++++------ erpnext/templates/pages/product_search.py | 8 +++- 3 files changed, 41 insertions(+), 25 deletions(-) diff --git a/erpnext/e_commerce/doctype/e_commerce_settings/e_commerce_settings.json b/erpnext/e_commerce/doctype/e_commerce_settings/e_commerce_settings.json index 62505e61db..e6f08f708a 100644 --- a/erpnext/e_commerce/doctype/e_commerce_settings/e_commerce_settings.json +++ b/erpnext/e_commerce/doctype/e_commerce_settings/e_commerce_settings.json @@ -48,7 +48,6 @@ "redisearch_warning", "search_index_fields", "is_redisearch_enabled", - "show_categories_in_search_autocomplete", "is_redisearch_loaded", "shop_by_category_section", "slideshow", @@ -294,6 +293,7 @@ "fieldname": "search_index_fields", "fieldtype": "Small Text", "label": "Search Index Fields", + "mandatory_depends_on": "is_redisearch_enabled", "read_only_depends_on": "eval:!doc.is_redisearch_loaded" }, { @@ -302,14 +302,6 @@ "fieldtype": "Section Break", "label": "Item Search Settings" }, - { - "default": "1", - "depends_on": "is_redisearch_enabled", - "fieldname": "show_categories_in_search_autocomplete", - "fieldtype": "Check", - "label": "Show Categories in Search Autocomplete", - "read_only_depends_on": "eval:!doc.is_redisearch_loaded" - }, { "default": "0", "fieldname": "is_redisearch_loaded", @@ -379,7 +371,7 @@ "index_web_pages_for_search": 1, "issingle": 1, "links": [], - "modified": "2022-03-31 16:01:46.308663", + "modified": "2022-04-01 18:35:56.106756", "modified_by": "Administrator", "module": "E-commerce", "name": "E Commerce Settings", diff --git a/erpnext/e_commerce/redisearch_utils.py b/erpnext/e_commerce/redisearch_utils.py index 78cc05af38..32b35db04c 100644 --- a/erpnext/e_commerce/redisearch_utils.py +++ b/erpnext/e_commerce/redisearch_utils.py @@ -157,17 +157,14 @@ def delete_from_ac_dict(website_item_doc): @if_redisearch_enabled def define_autocomplete_dictionary(): - """Creates an autocomplete search dictionary for `name`. - Also creats autocomplete dictionary for `categories` if - checked in E Commerce Settings""" + """ + Defines/Redefines an autocomplete search dictionary for Website Item Name. + Also creats autocomplete dictionary for Published Item Groups. + """ cache = frappe.cache() - name_ac = AutoCompleter(make_key(WEBSITE_ITEM_NAME_AUTOCOMPLETE), conn=cache) - cat_ac = AutoCompleter(make_key(WEBSITE_ITEM_CATEGORY_AUTOCOMPLETE), conn=cache) - - ac_categories = frappe.db.get_single_value( - "E Commerce Settings", "show_categories_in_search_autocomplete" - ) + item_ac = AutoCompleter(make_key(WEBSITE_ITEM_NAME_AUTOCOMPLETE), conn=cache) + item_group_ac = AutoCompleter(make_key(WEBSITE_ITEM_CATEGORY_AUTOCOMPLETE), conn=cache) # Delete both autocomplete dicts try: @@ -176,16 +173,39 @@ def define_autocomplete_dictionary(): except Exception: return False + create_items_autocomplete_dict(autocompleter=item_ac) + create_item_groups_autocomplete_dict(autocompleter=item_group_ac) + + +@if_redisearch_enabled +def create_items_autocomplete_dict(autocompleter): + "Add items as suggestions in Autocompleter." items = frappe.get_all( "Website Item", fields=["web_item_name", "item_group"], filters={"published": 1} ) for item in items: - name_ac.add_suggestions(Suggestion(item.web_item_name)) - if ac_categories and item.item_group: - cat_ac.add_suggestions(Suggestion(item.item_group)) + autocompleter.add_suggestions(Suggestion(item.web_item_name)) - return True + +@if_redisearch_enabled +def create_item_groups_autocomplete_dict(autocompleter): + "Add item groups with weightage as suggestions in Autocompleter." + published_item_groups = frappe.get_all( + "Item Group", fields=["name", "route", "weightage"], filters={"show_in_website": 1} + ) + if not published_item_groups: + return + + for item_group in published_item_groups: + payload = {"name": item_group, "route": item_group.route} + autocompleter.add_suggestions( + Suggestion( + string=item_group.name, + score=item_group.weightage, + payload=payload, # additional info that can be retrieved later + ) + ) @if_redisearch_enabled diff --git a/erpnext/templates/pages/product_search.py b/erpnext/templates/pages/product_search.py index ce04068cf6..94e893e136 100644 --- a/erpnext/templates/pages/product_search.py +++ b/erpnext/templates/pages/product_search.py @@ -1,6 +1,8 @@ # Copyright (c) 2021, Frappe Technologies Pvt. Ltd. and Contributors # License: GNU General Public License v3. See license.txt +import json + import frappe from frappe.utils import cint, cstr from redisearch import AutoCompleter, Client, Query @@ -135,8 +137,10 @@ def get_category_suggestions(query): return search_results ac = AutoCompleter(make_key(WEBSITE_ITEM_CATEGORY_AUTOCOMPLETE), conn=frappe.cache()) - suggestions = ac.get_suggestions(query, num=10) + suggestions = ac.get_suggestions(query, num=10, with_payloads=True) - search_results["results"] = [s.string for s in suggestions] + results = [json.loads(s.payload) for s in suggestions] + + search_results["results"] = results return search_results From ea036e495854e0a65fc2c7fa2065078745e87987 Mon Sep 17 00:00:00 2001 From: marination Date: Mon, 4 Apr 2022 11:07:53 +0530 Subject: [PATCH 03/22] fix: Better Exception Handling and vaeiabl naming - Function to handle RS exceptions (create log and raise error) - Handle `ResponseError` where it is anticipated - Misc: Better variables --- erpnext/e_commerce/redisearch_utils.py | 44 ++++++++++++++++++-------- 1 file changed, 30 insertions(+), 14 deletions(-) diff --git a/erpnext/e_commerce/redisearch_utils.py b/erpnext/e_commerce/redisearch_utils.py index 32b35db04c..f9890cca1a 100644 --- a/erpnext/e_commerce/redisearch_utils.py +++ b/erpnext/e_commerce/redisearch_utils.py @@ -1,8 +1,10 @@ -# Copyright (c) 2021, Frappe Technologies Pvt. Ltd. and Contributors +# Copyright (c) 2022, Frappe Technologies Pvt. Ltd. and Contributors # License: GNU General Public License v3. See license.txt import frappe +from frappe import _ from frappe.utils.redis_wrapper import RedisWrapper +from redis import ResponseError from redisearch import AutoCompleter, Client, IndexDefinition, Suggestion, TagField, TextField WEBSITE_ITEM_INDEX = "website_items_index" @@ -38,7 +40,7 @@ def is_search_module_loaded(): ) return "search" in parsed_output except Exception: - return False + return False # handling older redis versions def if_redisearch_enabled(function): @@ -64,15 +66,18 @@ def create_website_items_index(): # CREATE index client = Client(make_key(WEBSITE_ITEM_INDEX), conn=frappe.cache()) - # DROP if already exists try: - client.drop_index() - except Exception: + client.drop_index() # drop if already exists + except ResponseError: + # will most likely raise a ResponseError if index does not exist + # ignore and create index pass + except Exception: + raise_redisearch_error() idx_def = IndexDefinition([make_key(WEBSITE_ITEM_KEY_PREFIX)]) - # Based on e-commerce settings + # Index fields mentioned in e-commerce settings idx_fields = frappe.db.get_single_value("E Commerce Settings", "search_index_fields") idx_fields = idx_fields.split(",") if idx_fields else [] @@ -104,8 +109,8 @@ def insert_item_to_index(website_item_doc): cache = frappe.cache() web_item = create_web_item_map(website_item_doc) - for k, v in web_item.items(): - super(RedisWrapper, cache).hset(make_key(key), k, v) + for field, value in web_item.items(): + super(RedisWrapper, cache).hset(make_key(key), field, value) insert_to_name_ac(website_item_doc.web_item_name, website_item_doc.name) @@ -120,8 +125,8 @@ def create_web_item_map(website_item_doc): fields_to_index = get_fields_indexed() web_item = {} - for f in fields_to_index: - web_item[f] = website_item_doc.get(f) or "" + for field in fields_to_index: + web_item[field] = website_item_doc.get(field) or "" return web_item @@ -141,7 +146,7 @@ def delete_item_from_index(website_item_doc): try: cache.delete(key) except Exception: - return False + raise_redisearch_error() delete_from_ac_dict(website_item_doc) return True @@ -171,7 +176,7 @@ def define_autocomplete_dictionary(): cache.delete(make_key(WEBSITE_ITEM_NAME_AUTOCOMPLETE)) cache.delete(make_key(WEBSITE_ITEM_CATEGORY_AUTOCOMPLETE)) except Exception: - return False + raise_redisearch_error() create_items_autocomplete_dict(autocompleter=item_ac) create_item_groups_autocomplete_dict(autocompleter=item_group_ac) @@ -217,8 +222,8 @@ def reindex_all_web_items(): web_item = create_web_item_map(item) key = make_key(get_cache_key(item.name)) - for k, v in web_item.items(): - super(RedisWrapper, cache).hset(key, k, v) + for field, value in web_item.items(): + super(RedisWrapper, cache).hset(key, field, value) def get_cache_key(name): @@ -234,3 +239,14 @@ def get_fields_indexed(): fields_to_index = fields_to_index + mandatory_fields return fields_to_index + + +def raise_redisearch_error(): + "Create an Error Log and raise error." + traceback = frappe.get_traceback() + log = frappe.log_error(traceback, frappe._("Redisearch Error")) + log_link = frappe.utils.get_link_to_form("Error Log", log.name) + + frappe.throw( + msg=_("Something went wrong. Check {0}").format(log_link), title=_("Redisearch Error") + ) From 97e3a855f7cb3f7efa4068e5480ad57e9158a37e Mon Sep 17 00:00:00 2001 From: marination Date: Mon, 4 Apr 2022 11:32:49 +0530 Subject: [PATCH 04/22] fix: Convert payload to string before adding to autocompleter --- erpnext/e_commerce/redisearch_utils.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/erpnext/e_commerce/redisearch_utils.py b/erpnext/e_commerce/redisearch_utils.py index f9890cca1a..95b74e0795 100644 --- a/erpnext/e_commerce/redisearch_utils.py +++ b/erpnext/e_commerce/redisearch_utils.py @@ -1,6 +1,8 @@ # Copyright (c) 2022, Frappe Technologies Pvt. Ltd. and Contributors # License: GNU General Public License v3. See license.txt +import json + import frappe from frappe import _ from frappe.utils.redis_wrapper import RedisWrapper @@ -203,7 +205,7 @@ def create_item_groups_autocomplete_dict(autocompleter): return for item_group in published_item_groups: - payload = {"name": item_group, "route": item_group.route} + payload = json.dumps({"name": item_group, "route": item_group.route}) autocompleter.add_suggestions( Suggestion( string=item_group.name, From 7ef1ccbe8489d38168517758a8badfde2c1dc6fb Mon Sep 17 00:00:00 2001 From: marination Date: Mon, 4 Apr 2022 12:04:35 +0530 Subject: [PATCH 05/22] fix: Add default score of 1 to Item Group Autocompleter - If score 0 is inserted into suggestions, RS does not consider that suggestion --- erpnext/e_commerce/redisearch_utils.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/erpnext/e_commerce/redisearch_utils.py b/erpnext/e_commerce/redisearch_utils.py index 95b74e0795..b2f97e308c 100644 --- a/erpnext/e_commerce/redisearch_utils.py +++ b/erpnext/e_commerce/redisearch_utils.py @@ -209,7 +209,7 @@ def create_item_groups_autocomplete_dict(autocompleter): autocompleter.add_suggestions( Suggestion( string=item_group.name, - score=item_group.weightage, + score=frappe.utils.flt(item_group.weightage) or 1.0, payload=payload, # additional info that can be retrieved later ) ) From 0c26f9a8c8100104cb5ef3923e5cf9e739f3adae Mon Sep 17 00:00:00 2001 From: Saqib Ansari Date: Mon, 4 Apr 2022 12:28:09 +0530 Subject: [PATCH 06/22] fix(india): cannot generate e-invoice for is_pos invoices * If mode of payment > 18 characters, the e-invoice portal throws error --- erpnext/regional/india/e_invoice/utils.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/erpnext/regional/india/e_invoice/utils.py b/erpnext/regional/india/e_invoice/utils.py index cbdec564ba..8fd9c1c43d 100644 --- a/erpnext/regional/india/e_invoice/utils.py +++ b/erpnext/regional/india/e_invoice/utils.py @@ -387,7 +387,7 @@ def update_other_charges( def get_payment_details(invoice): payee_name = invoice.company - mode_of_payment = ", ".join([d.mode_of_payment for d in invoice.payments]) + mode_of_payment = "" paid_amount = invoice.base_paid_amount outstanding_amount = invoice.outstanding_amount From 18a3c5d536647a453e0a6fc2c39e32d18dcced7f Mon Sep 17 00:00:00 2001 From: Deepesh Garg Date: Mon, 4 Apr 2022 12:32:33 +0530 Subject: [PATCH 07/22] fix: Ignore user perm for party account company --- erpnext/accounts/doctype/party_account/party_account.json | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/erpnext/accounts/doctype/party_account/party_account.json b/erpnext/accounts/doctype/party_account/party_account.json index c9f15a6a47..69330577ab 100644 --- a/erpnext/accounts/doctype/party_account/party_account.json +++ b/erpnext/accounts/doctype/party_account/party_account.json @@ -3,6 +3,7 @@ "creation": "2014-08-29 16:02:39.740505", "doctype": "DocType", "editable_grid": 1, + "engine": "InnoDB", "field_order": [ "company", "account" @@ -11,6 +12,7 @@ { "fieldname": "company", "fieldtype": "Link", + "ignore_user_permissions": 1, "in_list_view": 1, "label": "Company", "options": "Company", @@ -27,7 +29,7 @@ "index_web_pages_for_search": 1, "istable": 1, "links": [], - "modified": "2021-04-07 18:13:08.833822", + "modified": "2022-04-04 12:31:02.994197", "modified_by": "Administrator", "module": "Accounts", "name": "Party Account", @@ -35,5 +37,6 @@ "permissions": [], "quick_entry": 1, "sort_field": "modified", - "sort_order": "DESC" + "sort_order": "DESC", + "states": [] } \ No newline at end of file From 3445682563374f247327e1242fddd346f81d6c15 Mon Sep 17 00:00:00 2001 From: marination Date: Mon, 4 Apr 2022 12:33:25 +0530 Subject: [PATCH 08/22] fix: Payload incorrect data (pass item_group.name) --- erpnext/e_commerce/redisearch_utils.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/erpnext/e_commerce/redisearch_utils.py b/erpnext/e_commerce/redisearch_utils.py index b2f97e308c..f2dd796f2c 100644 --- a/erpnext/e_commerce/redisearch_utils.py +++ b/erpnext/e_commerce/redisearch_utils.py @@ -205,7 +205,7 @@ def create_item_groups_autocomplete_dict(autocompleter): return for item_group in published_item_groups: - payload = json.dumps({"name": item_group, "route": item_group.route}) + payload = json.dumps({"name": item_group.name, "route": item_group.route}) autocompleter.add_suggestions( Suggestion( string=item_group.name, From 397b46a08b4b5b1dde1cd89ccbd0ad25304395bc Mon Sep 17 00:00:00 2001 From: marination Date: Mon, 4 Apr 2022 13:24:56 +0530 Subject: [PATCH 09/22] chore: Add TODOs(perf) for Redisearch Query --- erpnext/templates/pages/product_search.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/erpnext/templates/pages/product_search.py b/erpnext/templates/pages/product_search.py index 94e893e136..3ed056f55e 100644 --- a/erpnext/templates/pages/product_search.py +++ b/erpnext/templates/pages/product_search.py @@ -88,6 +88,8 @@ def product_search(query, limit=10, fuzzy_search=True): red = frappe.cache() query = clean_up_query(query) + # TODO: Check perf/correctness with Suggestions & Query vs only Query + # TODO: Use Levenshtein Distance in Query (max=3) ac = AutoCompleter(make_key(WEBSITE_ITEM_NAME_AUTOCOMPLETE), conn=red) client = Client(make_key(WEBSITE_ITEM_INDEX), conn=red) suggestions = ac.get_suggestions( From b91bf40f1bb11162f5589f7bde408e266f3d89bd Mon Sep 17 00:00:00 2001 From: Saqib Ansari Date: Mon, 4 Apr 2022 14:40:07 +0530 Subject: [PATCH 10/22] fix: server error while viewing gst e-invoice --- .../print_format/gst_e_invoice/gst_e_invoice.html | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/erpnext/accounts/print_format/gst_e_invoice/gst_e_invoice.html b/erpnext/accounts/print_format/gst_e_invoice/gst_e_invoice.html index e658049309..605ce8383e 100644 --- a/erpnext/accounts/print_format/gst_e_invoice/gst_e_invoice.html +++ b/erpnext/accounts/print_format/gst_e_invoice/gst_e_invoice.html @@ -1,7 +1,8 @@ {%- from "templates/print_formats/standard_macros.html" import add_header, render_field, print_value -%} -{%- set einvoice = json.loads(doc.signed_einvoice) -%}
+ {% if doc.signed_einvoice %} + {%- set einvoice = json.loads(doc.signed_einvoice) -%}
{% if letter_head and not no_letterhead %}
{{ letter_head }}
@@ -170,4 +171,10 @@
+ {% else %} +
+ You must generate IRN before you can preview GST E-Invoice. +
+ {% endif %}
+ From 0a71cabab13075fa05a7df1942776a2f08c47089 Mon Sep 17 00:00:00 2001 From: Rohit Waghchaure Date: Mon, 4 Apr 2022 15:10:57 +0530 Subject: [PATCH 11/22] fix: if accepted warehouse not selected during rejection then stock ledger not created --- erpnext/controllers/buying_controller.py | 26 ++++++++++++++---------- 1 file changed, 15 insertions(+), 11 deletions(-) diff --git a/erpnext/controllers/buying_controller.py b/erpnext/controllers/buying_controller.py index 3a69e717bd..2bac726e5b 100644 --- a/erpnext/controllers/buying_controller.py +++ b/erpnext/controllers/buying_controller.py @@ -463,6 +463,9 @@ class BuyingController(StockController, Subcontracting): stock_items = self.get_stock_items() for d in self.get("items"): + if d.item_code not in stock_items: + continue + if d.item_code in stock_items and d.warehouse: pr_qty = flt(d.qty) * flt(d.conversion_factor) @@ -488,6 +491,7 @@ class BuyingController(StockController, Subcontracting): sle = self.get_sl_entries( d, {"actual_qty": flt(pr_qty), "serial_no": cstr(d.serial_no).strip()} ) + if self.is_return: outgoing_rate = get_rate_for_return( self.doctype, self.name, d.item_code, self.return_against, item_row=d @@ -517,18 +521,18 @@ class BuyingController(StockController, Subcontracting): sl_entries.append(from_warehouse_sle) - if flt(d.rejected_qty) != 0: - sl_entries.append( - self.get_sl_entries( - d, - { - "warehouse": d.rejected_warehouse, - "actual_qty": flt(d.rejected_qty) * flt(d.conversion_factor), - "serial_no": cstr(d.rejected_serial_no).strip(), - "incoming_rate": 0.0, - }, - ) + if flt(d.rejected_qty) != 0: + sl_entries.append( + self.get_sl_entries( + d, + { + "warehouse": d.rejected_warehouse, + "actual_qty": flt(d.rejected_qty) * flt(d.conversion_factor), + "serial_no": cstr(d.rejected_serial_no).strip(), + "incoming_rate": 0.0, + }, ) + ) self.make_sl_entries_for_supplier_warehouse(sl_entries) self.make_sl_entries( From 3538656a7d6b82f6e84a7031f1542f1ce2ec57f4 Mon Sep 17 00:00:00 2001 From: Rucha Mahabal Date: Mon, 4 Apr 2022 17:52:30 +0530 Subject: [PATCH 12/22] fix: total leaves allocated not validated and recalculated on updates post submission --- .../leave_allocation/leave_allocation.py | 68 ++++++++++++------- 1 file changed, 42 insertions(+), 26 deletions(-) diff --git a/erpnext/hr/doctype/leave_allocation/leave_allocation.py b/erpnext/hr/doctype/leave_allocation/leave_allocation.py index 98408afab6..27479a5e81 100755 --- a/erpnext/hr/doctype/leave_allocation/leave_allocation.py +++ b/erpnext/hr/doctype/leave_allocation/leave_allocation.py @@ -39,11 +39,15 @@ class LeaveAllocation(Document): def validate(self): self.validate_period() self.validate_allocation_overlap() - self.validate_back_dated_allocation() - self.set_total_leaves_allocated() - self.validate_total_leaves_allocated() self.validate_lwp() set_employee_name(self) + self.set_total_leaves_allocated() + self.validate_leave_days_and_dates() + + def validate_leave_days_and_dates(self): + # all validations that should run on save as well as on update after submit + self.validate_back_dated_allocation() + self.validate_total_leaves_allocated() self.validate_leave_allocation_days() def validate_leave_allocation_days(self): @@ -56,14 +60,19 @@ class LeaveAllocation(Document): leave_allocated = 0 if leave_period: leave_allocated = get_leave_allocation_for_period( - self.employee, self.leave_type, leave_period[0].from_date, leave_period[0].to_date + self.employee, + self.leave_type, + leave_period[0].from_date, + leave_period[0].to_date, + exclude_allocation=self.name, ) leave_allocated += flt(self.new_leaves_allocated) if leave_allocated > max_leaves_allowed: frappe.throw( _( - "Total allocated leaves are more days than maximum allocation of {0} leave type for employee {1} in the period" - ).format(self.leave_type, self.employee) + "Total allocated leaves are more than maximum allocation allowed for {0} leave type for employee {1} in the period" + ).format(self.leave_type, self.employee), + OverAllocationError, ) def on_submit(self): @@ -84,6 +93,12 @@ class LeaveAllocation(Document): def on_update_after_submit(self): if self.has_value_changed("new_leaves_allocated"): self.validate_against_leave_applications() + + # recalculate total leaves allocated + self.total_leaves_allocated = flt(self.unused_leaves) + flt(self.new_leaves_allocated) + # run required validations again since total leaves are being updated + self.validate_leave_days_and_dates() + leaves_to_be_added = self.new_leaves_allocated - self.get_existing_leave_count() args = { "leaves": leaves_to_be_added, @@ -92,6 +107,7 @@ class LeaveAllocation(Document): "is_carry_forward": 0, } create_leave_ledger_entry(self, args, True) + self.db_update() def get_existing_leave_count(self): ledger_entries = frappe.get_all( @@ -279,27 +295,27 @@ def get_previous_allocation(from_date, leave_type, employee): ) -def get_leave_allocation_for_period(employee, leave_type, from_date, to_date): - leave_allocated = 0 - leave_allocations = frappe.db.sql( - """ - select employee, leave_type, from_date, to_date, total_leaves_allocated - from `tabLeave Allocation` - where employee=%(employee)s and leave_type=%(leave_type)s - and docstatus=1 - and (from_date between %(from_date)s and %(to_date)s - or to_date between %(from_date)s and %(to_date)s - or (from_date < %(from_date)s and to_date > %(to_date)s)) - """, - {"from_date": from_date, "to_date": to_date, "employee": employee, "leave_type": leave_type}, - as_dict=1, - ) +def get_leave_allocation_for_period( + employee, leave_type, from_date, to_date, exclude_allocation=None +): + from frappe.query_builder.functions import Sum - if leave_allocations: - for leave_alloc in leave_allocations: - leave_allocated += leave_alloc.total_leaves_allocated - - return leave_allocated + Allocation = frappe.qb.DocType("Leave Allocation") + return ( + frappe.qb.from_(Allocation) + .select(Sum(Allocation.total_leaves_allocated).as_("total_allocated_leaves")) + .where( + (Allocation.employee == employee) + & (Allocation.leave_type == leave_type) + & (Allocation.docstatus == 1) + & (Allocation.name != exclude_allocation) + & ( + (Allocation.from_date.between(from_date, to_date)) + | (Allocation.to_date.between(from_date, to_date)) + | ((Allocation.from_date < from_date) & (Allocation.to_date > to_date)) + ) + ) + ).run()[0][0] or 0.0 @frappe.whitelist() From ac5df1abbe80255d685966c108835cdb75f90659 Mon Sep 17 00:00:00 2001 From: Rohit Waghchaure Date: Mon, 4 Apr 2022 15:38:42 +0530 Subject: [PATCH 13/22] test: test case to validate rejected qty without accepted warehouse --- erpnext/controllers/buying_controller.py | 2 +- .../purchase_receipt/test_purchase_receipt.py | 39 +++++++++++++++++++ 2 files changed, 40 insertions(+), 1 deletion(-) diff --git a/erpnext/controllers/buying_controller.py b/erpnext/controllers/buying_controller.py index 2bac726e5b..eda36868b9 100644 --- a/erpnext/controllers/buying_controller.py +++ b/erpnext/controllers/buying_controller.py @@ -466,7 +466,7 @@ class BuyingController(StockController, Subcontracting): if d.item_code not in stock_items: continue - if d.item_code in stock_items and d.warehouse: + if d.warehouse: pr_qty = flt(d.qty) * flt(d.conversion_factor) if pr_qty: diff --git a/erpnext/stock/doctype/purchase_receipt/test_purchase_receipt.py b/erpnext/stock/doctype/purchase_receipt/test_purchase_receipt.py index bfbdd56292..f3faba4f8d 100644 --- a/erpnext/stock/doctype/purchase_receipt/test_purchase_receipt.py +++ b/erpnext/stock/doctype/purchase_receipt/test_purchase_receipt.py @@ -647,6 +647,45 @@ class TestPurchaseReceipt(FrappeTestCase): return_pr.cancel() pr.cancel() + def test_purchase_receipt_for_rejected_gle_without_accepted_warehouse(self): + from erpnext.stock.doctype.warehouse.test_warehouse import get_warehouse + + rejected_warehouse = "_Test Rejected Warehouse - TCP1" + if not frappe.db.exists("Warehouse", rejected_warehouse): + get_warehouse( + company="_Test Company with perpetual inventory", + abbr=" - TCP1", + warehouse_name="_Test Rejected Warehouse", + ).name + + pr = make_purchase_receipt( + company="_Test Company with perpetual inventory", + warehouse="Stores - TCP1", + received_qty=2, + rejected_qty=2, + rejected_warehouse=rejected_warehouse, + do_not_save=True, + ) + + pr.items[0].qty = 0.0 + pr.items[0].warehouse = "" + pr.submit() + + actual_qty = frappe.db.get_value( + "Stock Ledger Entry", + { + "voucher_type": "Purchase Receipt", + "voucher_no": pr.name, + "warehouse": pr.items[0].rejected_warehouse, + "is_cancelled": 0, + }, + "actual_qty", + ) + + self.assertEqual(actual_qty, 2) + self.assertFalse(pr.items[0].warehouse) + pr.cancel() + def test_purchase_return_for_serialized_items(self): def _check_serial_no_values(serial_no, field_values): serial_no = frappe.get_doc("Serial No", serial_no) From 5499cecffd76f7e5414181855008cdb8e50634ef Mon Sep 17 00:00:00 2001 From: Rucha Mahabal Date: Mon, 4 Apr 2022 18:32:17 +0530 Subject: [PATCH 14/22] test: leave allocation validations and total value for updates done before and after submission --- .../leave_allocation/test_leave_allocation.py | 158 ++++++++++++++++-- 1 file changed, 148 insertions(+), 10 deletions(-) diff --git a/erpnext/hr/doctype/leave_allocation/test_leave_allocation.py b/erpnext/hr/doctype/leave_allocation/test_leave_allocation.py index a53d4a82ba..6b3636db35 100644 --- a/erpnext/hr/doctype/leave_allocation/test_leave_allocation.py +++ b/erpnext/hr/doctype/leave_allocation/test_leave_allocation.py @@ -1,24 +1,26 @@ import unittest import frappe +from frappe.tests.utils import FrappeTestCase from frappe.utils import add_days, add_months, getdate, nowdate import erpnext from erpnext.hr.doctype.employee.test_employee import make_employee +from erpnext.hr.doctype.leave_allocation.leave_allocation import ( + BackDatedAllocationError, + OverAllocationError, +) from erpnext.hr.doctype.leave_ledger_entry.leave_ledger_entry import process_expired_allocation from erpnext.hr.doctype.leave_type.test_leave_type import create_leave_type -class TestLeaveAllocation(unittest.TestCase): - @classmethod - def setUpClass(cls): - frappe.db.sql("delete from `tabLeave Period`") +class TestLeaveAllocation(FrappeTestCase): + def setUp(self): + frappe.db.delete("Leave Period") + frappe.db.delete("Leave Allocation") emp_id = make_employee("test_emp_leave_allocation@salary.com") - cls.employee = frappe.get_doc("Employee", emp_id) - - def tearDown(self): - frappe.db.rollback() + self.employee = frappe.get_doc("Employee", emp_id) def test_overlapping_allocation(self): leaves = [ @@ -65,7 +67,7 @@ class TestLeaveAllocation(unittest.TestCase): # invalid period self.assertRaises(frappe.ValidationError, doc.save) - def test_allocated_leave_days_over_period(self): + def test_validation_for_over_allocation(self): doc = frappe.get_doc( { "doctype": "Leave Allocation", @@ -80,7 +82,135 @@ class TestLeaveAllocation(unittest.TestCase): ) # allocated leave more than period - self.assertRaises(frappe.ValidationError, doc.save) + self.assertRaises(OverAllocationError, doc.save) + + def test_validation_for_over_allocation_post_submission(self): + allocation = frappe.get_doc( + { + "doctype": "Leave Allocation", + "__islocal": 1, + "employee": self.employee.name, + "employee_name": self.employee.employee_name, + "leave_type": "_Test Leave Type", + "from_date": getdate("2015-09-1"), + "to_date": getdate("2015-09-30"), + "new_leaves_allocated": 15, + } + ).submit() + allocation.reload() + # allocated leaves more than period after submission + allocation.new_leaves_allocated = 35 + self.assertRaises(OverAllocationError, allocation.save) + + def test_validation_for_over_allocation_based_on_leave_setup(self): + frappe.delete_doc_if_exists("Leave Period", "Test Allocation Period") + leave_period = frappe.get_doc( + dict( + name="Test Allocation Period", + doctype="Leave Period", + from_date=add_months(nowdate(), -6), + to_date=add_months(nowdate(), 6), + company="_Test Company", + is_active=1, + ) + ).insert() + + leave_type = create_leave_type(leave_type_name="_Test Allocation Validation", is_carry_forward=1) + leave_type.max_leaves_allowed = 25 + leave_type.save() + + # 15 leaves allocated in this period + allocation = create_leave_allocation( + leave_type=leave_type.name, + employee=self.employee.name, + employee_name=self.employee.employee_name, + from_date=leave_period.from_date, + to_date=nowdate(), + ) + allocation.submit() + + # trying to allocate additional 15 leaves + allocation = create_leave_allocation( + leave_type=leave_type.name, + employee=self.employee.name, + employee_name=self.employee.employee_name, + from_date=add_days(nowdate(), 1), + to_date=leave_period.to_date, + ) + self.assertRaises(OverAllocationError, allocation.save) + + def test_validation_for_over_allocation_based_on_leave_setup_post_submission(self): + frappe.delete_doc_if_exists("Leave Period", "Test Allocation Period") + leave_period = frappe.get_doc( + dict( + name="Test Allocation Period", + doctype="Leave Period", + from_date=add_months(nowdate(), -6), + to_date=add_months(nowdate(), 6), + company="_Test Company", + is_active=1, + ) + ).insert() + + leave_type = create_leave_type(leave_type_name="_Test Allocation Validation", is_carry_forward=1) + leave_type.max_leaves_allowed = 30 + leave_type.save() + + # 15 leaves allocated + allocation = create_leave_allocation( + leave_type=leave_type.name, + employee=self.employee.name, + employee_name=self.employee.employee_name, + from_date=leave_period.from_date, + to_date=nowdate(), + ) + allocation.submit() + allocation.reload() + + # allocate additional 15 leaves + allocation = create_leave_allocation( + leave_type=leave_type.name, + employee=self.employee.name, + employee_name=self.employee.employee_name, + from_date=add_days(nowdate(), 1), + to_date=leave_period.to_date, + ) + allocation.submit() + allocation.reload() + + # trying to allocate 25 leaves in 2nd alloc within leave period + # total leaves = 40 which is more than `max_leaves_allowed` setting i.e. 30 + allocation.new_leaves_allocated = 25 + self.assertRaises(OverAllocationError, allocation.save) + + def test_validate_back_dated_allocation_update(self): + leave_type = create_leave_type(leave_type_name="_Test_CF_leave", is_carry_forward=1) + leave_type.save() + + # initial leave allocation = 15 + leave_allocation = create_leave_allocation( + employee=self.employee.name, + employee_name=self.employee.employee_name, + leave_type="_Test_CF_leave", + from_date=add_months(nowdate(), -12), + to_date=add_months(nowdate(), -1), + carry_forward=0, + ) + leave_allocation.submit() + + # new_leaves = 15, carry_forwarded = 10 + leave_allocation_1 = create_leave_allocation( + employee=self.employee.name, + employee_name=self.employee.employee_name, + leave_type="_Test_CF_leave", + carry_forward=1, + ) + leave_allocation_1.submit() + + # try updating initial leave allocation + leave_allocation.reload() + leave_allocation.new_leaves_allocated = 20 + self.assertRaises(BackDatedAllocationError, leave_allocation.save) def test_carry_forward_calculation(self): leave_type = create_leave_type(leave_type_name="_Test_CF_leave", is_carry_forward=1) @@ -108,8 +238,10 @@ class TestLeaveAllocation(unittest.TestCase): carry_forward=1, ) leave_allocation_1.submit() + leave_allocation_1.reload() self.assertEqual(leave_allocation_1.unused_leaves, 10) + self.assertEqual(leave_allocation_1.total_leaves_allocated, 25) leave_allocation_1.cancel() @@ -197,9 +329,12 @@ class TestLeaveAllocation(unittest.TestCase): employee=self.employee.name, employee_name=self.employee.employee_name ) leave_allocation.submit() + leave_allocation.reload() self.assertTrue(leave_allocation.total_leaves_allocated, 15) + leave_allocation.new_leaves_allocated = 40 leave_allocation.submit() + leave_allocation.reload() self.assertTrue(leave_allocation.total_leaves_allocated, 40) def test_leave_subtraction_after_submit(self): @@ -207,9 +342,12 @@ class TestLeaveAllocation(unittest.TestCase): employee=self.employee.name, employee_name=self.employee.employee_name ) leave_allocation.submit() + leave_allocation.reload() self.assertTrue(leave_allocation.total_leaves_allocated, 15) + leave_allocation.new_leaves_allocated = 10 leave_allocation.submit() + leave_allocation.reload() self.assertTrue(leave_allocation.total_leaves_allocated, 10) def test_validation_against_leave_application_after_submit(self): From 793164ac2efe9588d16e88cc27141cb03cf57d36 Mon Sep 17 00:00:00 2001 From: Rucha Mahabal Date: Mon, 4 Apr 2022 19:08:27 +0530 Subject: [PATCH 15/22] fix(test): set company for employee in leave allocation test setup --- erpnext/hr/doctype/leave_allocation/test_leave_allocation.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/erpnext/hr/doctype/leave_allocation/test_leave_allocation.py b/erpnext/hr/doctype/leave_allocation/test_leave_allocation.py index 6b3636db35..dde52d7ad8 100644 --- a/erpnext/hr/doctype/leave_allocation/test_leave_allocation.py +++ b/erpnext/hr/doctype/leave_allocation/test_leave_allocation.py @@ -19,7 +19,7 @@ class TestLeaveAllocation(FrappeTestCase): frappe.db.delete("Leave Period") frappe.db.delete("Leave Allocation") - emp_id = make_employee("test_emp_leave_allocation@salary.com") + emp_id = make_employee("test_emp_leave_allocation@salary.com", company="_Test Company") self.employee = frappe.get_doc("Employee", emp_id) def test_overlapping_allocation(self): From e7962672148602d707377e0c44a18dbcdea00922 Mon Sep 17 00:00:00 2001 From: Sherin KR Date: Mon, 4 Apr 2022 06:37:51 -0700 Subject: [PATCH 16/22] fix: Validation for single threshold in Tax With Holding Category (#30382) (cherry picked from commit 0a2c72c594963f63551985a908c1c79302556e91) --- .../tax_withholding_category/tax_withholding_category.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/erpnext/accounts/doctype/tax_withholding_category/tax_withholding_category.py b/erpnext/accounts/doctype/tax_withholding_category/tax_withholding_category.py index 35c2f8494f..a519d8be73 100644 --- a/erpnext/accounts/doctype/tax_withholding_category/tax_withholding_category.py +++ b/erpnext/accounts/doctype/tax_withholding_category/tax_withholding_category.py @@ -34,7 +34,9 @@ class TaxWithholdingCategory(Document): def validate_thresholds(self): for d in self.get("rates"): - if d.cumulative_threshold and d.cumulative_threshold < d.single_threshold: + if ( + d.cumulative_threshold and d.single_threshold and d.cumulative_threshold < d.single_threshold + ): frappe.throw( _("Row #{0}: Cumulative threshold cannot be less than Single Transaction threshold").format( d.idx From d4301d6d2f4a2396b8dcfc2845574115e05636d1 Mon Sep 17 00:00:00 2001 From: marination Date: Tue, 5 Apr 2022 12:07:50 +0530 Subject: [PATCH 17/22] chore: Accessibility for E-commerce Doctypes - Add Website Item routing button and dashboard link in Item master - Group Item variant buttons together --- erpnext/stock/doctype/item/item.js | 18 ++++++++++++------ erpnext/stock/doctype/item/item_dashboard.py | 1 + 2 files changed, 13 insertions(+), 6 deletions(-) diff --git a/erpnext/stock/doctype/item/item.js b/erpnext/stock/doctype/item/item.js index 9e8b3bd463..23301a6a78 100644 --- a/erpnext/stock/doctype/item/item.js +++ b/erpnext/stock/doctype/item/item.js @@ -55,10 +55,15 @@ frappe.ui.form.on("Item", { if (frm.doc.has_variants) { frm.set_intro(__("This Item is a Template and cannot be used in transactions. Item attributes will be copied over into the variants unless 'No Copy' is set"), true); + frm.add_custom_button(__("Show Variants"), function() { frappe.set_route("List", "Item", {"variant_of": frm.doc.name}); }, __("View")); + frm.add_custom_button(__("Item Variant Settings"), function() { + frappe.set_route("Form", "Item Variant Settings"); + }, __("View")); + frm.add_custom_button(__("Variant Details Report"), function() { frappe.set_route("query-report", "Item Variant Details", {"item": frm.doc.name}); }, __("View")); @@ -110,6 +115,13 @@ frappe.ui.form.on("Item", { } }); }, __('Actions')); + } else { + frm.add_custom_button(__("Website Item"), function() { + frappe.db.get_value("Website Item", {item_code: frm.doc.name}, "name", (d) => { + if (!d.name) frappe.throw(__("Website Item not found")); + frappe.set_route("Form", "Website Item", d.name); + }); + }, __("View")); } erpnext.item.edit_prices_button(frm); @@ -131,12 +143,6 @@ frappe.ui.form.on("Item", { frappe.set_route('Form', 'Item', new_item.name); }); - if(frm.doc.has_variants) { - frm.add_custom_button(__("Item Variant Settings"), function() { - frappe.set_route("Form", "Item Variant Settings"); - }, __("View")); - } - const stock_exists = (frm.doc.__onload && frm.doc.__onload.stock_exists) ? 1 : 0; diff --git a/erpnext/stock/doctype/item/item_dashboard.py b/erpnext/stock/doctype/item/item_dashboard.py index 33acf4bfd8..3caed02d69 100644 --- a/erpnext/stock/doctype/item/item_dashboard.py +++ b/erpnext/stock/doctype/item/item_dashboard.py @@ -32,5 +32,6 @@ def get_data(): {"label": _("Manufacture"), "items": ["Production Plan", "Work Order", "Item Manufacturer"]}, {"label": _("Traceability"), "items": ["Serial No", "Batch"]}, {"label": _("Move"), "items": ["Stock Entry"]}, + {"label": _("E-commerce"), "items": ["Website Item"]}, ], } From 065623ce2526c3df6f3355675d3138fb86550af0 Mon Sep 17 00:00:00 2001 From: marination Date: Tue, 5 Apr 2022 12:30:02 +0530 Subject: [PATCH 18/22] chore: Add Prices, Stock and E-com Settings access from Website Item --- .../doctype/website_item/website_item.js | 25 ++++++++++++++++--- 1 file changed, 21 insertions(+), 4 deletions(-) diff --git a/erpnext/e_commerce/doctype/website_item/website_item.js b/erpnext/e_commerce/doctype/website_item/website_item.js index 7108cabfb3..7295e4b56a 100644 --- a/erpnext/e_commerce/doctype/website_item/website_item.js +++ b/erpnext/e_commerce/doctype/website_item/website_item.js @@ -2,7 +2,7 @@ // For license information, please see license.txt frappe.ui.form.on('Website Item', { - onload: function(frm) { + onload: (frm) => { // should never check Private frm.fields_dict["website_image"].df.is_private = 0; @@ -13,18 +13,35 @@ frappe.ui.form.on('Website Item', { }); }, - image: function() { + refresh: (frm) => { + frm.add_custom_button(__("Prices"), function() { + frappe.set_route("List", "Item Price", {"item_code": frm.doc.item_code}); + }, __("View")); + + frm.add_custom_button(__("Stock"), function() { + frappe.route_options = { + "item_code": frm.doc.item_code + }; + frappe.set_route("query-report", "Stock Balance"); + }, __("View")); + + frm.add_custom_button(__("E Commerce Settings"), function() { + frappe.set_route("Form", "E Commerce Settings"); + }, __("View")); + }, + + image: () => { refresh_field("image_view"); }, - copy_from_item_group: function(frm) { + copy_from_item_group: (frm) => { return frm.call({ doc: frm.doc, method: "copy_specification_from_item_group" }); }, - set_meta_tags(frm) { + set_meta_tags: (frm) => { frappe.utils.set_meta_tag(frm.doc.route); } }); From c8779aa4465ddf58db7751c39e1a89c9ea92eead Mon Sep 17 00:00:00 2001 From: Saqib Ansari Date: Tue, 5 Apr 2022 15:38:39 +0530 Subject: [PATCH 19/22] fix: fetch from fields not working in eway bill dialog --- erpnext/regional/india/e_invoice/einvoice.js | 32 +++++++++++++++++--- 1 file changed, 28 insertions(+), 4 deletions(-) diff --git a/erpnext/regional/india/e_invoice/einvoice.js b/erpnext/regional/india/e_invoice/einvoice.js index 348f0c6fee..17b018c65b 100644 --- a/erpnext/regional/india/e_invoice/einvoice.js +++ b/erpnext/regional/india/e_invoice/einvoice.js @@ -105,6 +105,30 @@ erpnext.setup_einvoice_actions = (doctype) => { }, primary_action_label: __('Submit') }); + d.fields_dict.transporter.df.onchange = function () { + const transporter = d.fields_dict.transporter.value; + if (transporter) { + frappe.db.get_value('Supplier', transporter, ['gst_transporter_id', 'supplier_name']) + .then(({ message }) => { + d.set_value('gst_transporter_id', message.gst_transporter_id); + d.set_value('transporter_name', message.supplier_name); + }); + } else { + d.set_value('gst_transporter_id', ''); + d.set_value('transporter_name', ''); + } + }; + d.fields_dict.driver.df.onchange = function () { + const driver = d.fields_dict.driver.value; + if (driver) { + frappe.db.get_value('Driver', driver, ['full_name']) + .then(({ message }) => { + d.set_value('driver_name', message.full_name); + }); + } else { + d.set_value('driver_name', ''); + } + }; d.show(); }; @@ -153,7 +177,6 @@ const get_ewaybill_fields = (frm) => { 'fieldname': 'gst_transporter_id', 'label': 'GST Transporter ID', 'fieldtype': 'Data', - 'fetch_from': 'transporter.gst_transporter_id', 'default': frm.doc.gst_transporter_id }, { @@ -189,9 +212,9 @@ const get_ewaybill_fields = (frm) => { 'fieldname': 'transporter_name', 'label': 'Transporter Name', 'fieldtype': 'Data', - 'fetch_from': 'transporter.name', 'read_only': 1, - 'default': frm.doc.transporter_name + 'default': frm.doc.transporter_name, + 'depends_on': 'transporter' }, { 'fieldname': 'mode_of_transport', @@ -206,7 +229,8 @@ const get_ewaybill_fields = (frm) => { 'fieldtype': 'Data', 'fetch_from': 'driver.full_name', 'read_only': 1, - 'default': frm.doc.driver_name + 'default': frm.doc.driver_name, + 'depends_on': 'driver' }, { 'fieldname': 'lr_date', From dec0c1b5bb06455ac5641e9cdc2846374a018259 Mon Sep 17 00:00:00 2001 From: Deepesh Garg Date: Wed, 6 Apr 2022 10:21:27 +0530 Subject: [PATCH 20/22] test: Ignore parent company account creation --- erpnext/setup/doctype/company/test_records.json | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/erpnext/setup/doctype/company/test_records.json b/erpnext/setup/doctype/company/test_records.json index 89be607d04..19b6ef27ac 100644 --- a/erpnext/setup/doctype/company/test_records.json +++ b/erpnext/setup/doctype/company/test_records.json @@ -8,7 +8,8 @@ "domain": "Manufacturing", "chart_of_accounts": "Standard", "default_holiday_list": "_Test Holiday List", - "enable_perpetual_inventory": 0 + "enable_perpetual_inventory": 0, + "allow_account_creation_against_child_company": 1 }, { "abbr": "_TC1", From f20890325043ed1c1caf462e5025ecca6a0990bd Mon Sep 17 00:00:00 2001 From: Rucha Mahabal Date: Wed, 6 Apr 2022 10:56:47 +0530 Subject: [PATCH 21/22] feat: Scheduling Multiple shifts and Auto Attendance (#29955) * refactor: overlapping shifts validation - convert raw query to frappe.qb - check for overlapping timings if dates overlap - translation friendly error messages with link to overlapping doc * refactor: consider timeslots in `get_employee_shift` * fix: handle shift grace overlap while finding current shift * refactor: handle shifts spanning over 2 different days * fix: fetching shift on timing boundaries * refactor: rewrite docstrings and add type hints for functions * refactor: Allow multiple attendance records creation for different shifts * feat: auto attendance marking for multiple shifts on the same day * refactor: mark absent for employees with no attendance - break down into smaller functions - make it work with multiple shifts - this will mark employee as absent per shift, meaning employee can be present for one shift and absent for another on the same day * chore: sort imports, remove unused imports * refactor: Monthly Attendance Sheet - split into smaller functions - add type hints - get rid of unnecessary db calls and loops - add docstrings for functions * feat: add colors for attendance status to lessen the cognitive load - legend with colors and full form for status abbreviations * feat: show shift-wise attendance in monthly attendance sheet * test: monthly attendance sheet * style: format code with black * chore: ignore formatting changes in blame * test: fetching shifts in Employee Checkins * fix(test): make holiday list for shift and checkin tests * fix: tests * test: shift assignment creation * fix: attendance fixes - check half day attendance threshold before absent threshold to avoid half day getting marked as absent - round working hours to 2 digits for better accuracy - start and end dates for absent attendance marking * test: Shift Type with Auto Attendance setup and working fix test setups * refactor: Overlapping validation for Shift Request - commonify time overlap function between request and assignment - add tests for shift request overlap * chore: remove unused import * fix: add validation for overlapping shift attendance - skip auto attendance in case of overlapping shift attendance record - this case won't occur in case of shift assignment, since it will not allow overlapping shifts to be assigned - can happen if manual attendance records are created * test: validations for duplicate and overlapping shift attendance records * test: skip auto attendance * fix: skip validation for overlapping shift attendance if no shift is linked * test: add holiday related shift and attendance tests * test: add attendance sheet tests for employee filter, half days * fix: sider --- .git-blame-ignore-revs | 3 + erpnext/hr/doctype/attendance/attendance.py | 169 +++- .../hr/doctype/attendance/test_attendance.py | 109 ++- .../employee_checkin/employee_checkin.py | 30 +- .../employee_checkin/test_employee_checkin.py | 185 +++- .../shift_assignment/shift_assignment.py | 508 ++++++---- .../shift_assignment/test_shift_assignment.py | 82 +- .../hr/doctype/shift_request/shift_request.py | 87 +- .../shift_request/test_shift_request.py | 153 +++- erpnext/hr/doctype/shift_type/shift_type.py | 141 ++- .../hr/doctype/shift_type/test_shift_type.py | 378 +++++++- .../monthly_attendance_sheet.js | 23 +- .../monthly_attendance_sheet.py | 865 +++++++++++------- .../test_monthly_attendance_sheet.py | 215 ++++- 14 files changed, 2287 insertions(+), 661 deletions(-) diff --git a/.git-blame-ignore-revs b/.git-blame-ignore-revs index e9cb6cf903..3bc22af96a 100644 --- a/.git-blame-ignore-revs +++ b/.git-blame-ignore-revs @@ -26,3 +26,6 @@ b147b85e6ac19a9220cd1e2958a6ebd99373283a # bulk format python code with black 494bd9ef78313436f0424b918f200dab8fc7c20b + +# bulk format python code with black +baec607ff5905b1c67531096a9cf50ec7ff00a5d \ No newline at end of file diff --git a/erpnext/hr/doctype/attendance/attendance.py b/erpnext/hr/doctype/attendance/attendance.py index 7f4bd83685..e43d40ef56 100644 --- a/erpnext/hr/doctype/attendance/attendance.py +++ b/erpnext/hr/doctype/attendance/attendance.py @@ -5,11 +5,21 @@ import frappe from frappe import _ 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 +from erpnext.hr.doctype.shift_assignment.shift_assignment import has_overlapping_timings from erpnext.hr.utils import get_holiday_dates_for_employee, validate_active_employee +class DuplicateAttendanceError(frappe.ValidationError): + pass + + +class OverlappingShiftAttendanceError(frappe.ValidationError): + pass + + class Attendance(Document): def validate(self): from erpnext.controllers.status_updater import validate_status @@ -18,6 +28,7 @@ class Attendance(Document): validate_active_employee(self.employee) self.validate_attendance_date() self.validate_duplicate_record() + self.validate_overlapping_shift_attendance() self.validate_employee_status() self.check_leave_record() @@ -35,21 +46,35 @@ class Attendance(Document): frappe.throw(_("Attendance date can not be less than employee's joining date")) def validate_duplicate_record(self): - res = frappe.db.sql( - """ - select name from `tabAttendance` - where employee = %s - and attendance_date = %s - and name != %s - and docstatus != 2 - """, - (self.employee, getdate(self.attendance_date), self.name), + duplicate = get_duplicate_attendance_record( + self.employee, self.attendance_date, self.shift, self.name ) - if res: + + if duplicate: frappe.throw( - _("Attendance for employee {0} is already marked for the date {1}").format( - frappe.bold(self.employee), frappe.bold(self.attendance_date) - ) + _("Attendance for employee {0} is already marked for the date {1}: {2}").format( + frappe.bold(self.employee), + frappe.bold(self.attendance_date), + get_link_to_form("Attendance", duplicate[0].name), + ), + title=_("Duplicate Attendance"), + exc=DuplicateAttendanceError, + ) + + def validate_overlapping_shift_attendance(self): + attendance = get_overlapping_shift_attendance( + self.employee, self.attendance_date, self.shift, self.name + ) + + if attendance: + frappe.throw( + _("Attendance for employee {0} is already marked for an overlapping shift {1}: {2}").format( + frappe.bold(self.employee), + frappe.bold(attendance.shift), + get_link_to_form("Attendance", attendance.name), + ), + title=_("Overlapping Shift Attendance"), + exc=OverlappingShiftAttendanceError, ) def validate_employee_status(self): @@ -103,6 +128,69 @@ class Attendance(Document): frappe.throw(_("Employee {0} is not active or does not exist").format(self.employee)) +def get_duplicate_attendance_record(employee, attendance_date, shift, name=None): + attendance = frappe.qb.DocType("Attendance") + query = ( + frappe.qb.from_(attendance) + .select(attendance.name) + .where((attendance.employee == employee) & (attendance.docstatus < 2)) + ) + + if shift: + query = query.where( + Criterion.any( + [ + Criterion.all( + [ + ((attendance.shift.isnull()) | (attendance.shift == "")), + (attendance.attendance_date == attendance_date), + ] + ), + Criterion.all( + [ + ((attendance.shift.isnotnull()) | (attendance.shift != "")), + (attendance.attendance_date == attendance_date), + (attendance.shift == shift), + ] + ), + ] + ) + ) + else: + query = query.where((attendance.attendance_date == attendance_date)) + + if name: + query = query.where(attendance.name != name) + + return query.run(as_dict=True) + + +def get_overlapping_shift_attendance(employee, attendance_date, shift, name=None): + if not shift: + return {} + + attendance = frappe.qb.DocType("Attendance") + query = ( + frappe.qb.from_(attendance) + .select(attendance.name, attendance.shift) + .where( + (attendance.employee == employee) + & (attendance.docstatus < 2) + & (attendance.attendance_date == attendance_date) + & (attendance.shift != shift) + ) + ) + + if name: + query = query.where(attendance.name != name) + + overlapping_attendance = query.run(as_dict=True) + + if overlapping_attendance and has_overlapping_timings(shift, overlapping_attendance[0].shift): + return overlapping_attendance[0] + return {} + + @frappe.whitelist() def get_events(start, end, filters=None): events = [] @@ -141,28 +229,39 @@ def add_attendance(events, start, end, conditions=None): def mark_attendance( - employee, attendance_date, status, shift=None, leave_type=None, ignore_validate=False + employee, + attendance_date, + status, + shift=None, + leave_type=None, + ignore_validate=False, + late_entry=False, + early_exit=False, ): - if not frappe.db.exists( - "Attendance", - {"employee": employee, "attendance_date": attendance_date, "docstatus": ("!=", "2")}, - ): - company = frappe.db.get_value("Employee", employee, "company") - attendance = frappe.get_doc( - { - "doctype": "Attendance", - "employee": employee, - "attendance_date": attendance_date, - "status": status, - "company": company, - "shift": shift, - "leave_type": leave_type, - } - ) - attendance.flags.ignore_validate = ignore_validate - attendance.insert() - attendance.submit() - return attendance.name + if get_duplicate_attendance_record(employee, attendance_date, shift): + return + + if get_overlapping_shift_attendance(employee, attendance_date, shift): + return + + company = frappe.db.get_value("Employee", employee, "company") + attendance = frappe.get_doc( + { + "doctype": "Attendance", + "employee": employee, + "attendance_date": attendance_date, + "status": status, + "company": company, + "shift": shift, + "leave_type": leave_type, + "late_entry": late_entry, + "early_exit": early_exit, + } + ) + attendance.flags.ignore_validate = ignore_validate + attendance.insert() + attendance.submit() + return attendance.name @frappe.whitelist() diff --git a/erpnext/hr/doctype/attendance/test_attendance.py b/erpnext/hr/doctype/attendance/test_attendance.py index 058bc93d72..762d0f7567 100644 --- a/erpnext/hr/doctype/attendance/test_attendance.py +++ b/erpnext/hr/doctype/attendance/test_attendance.py @@ -6,6 +6,8 @@ from frappe.tests.utils import FrappeTestCase from frappe.utils import add_days, get_year_ending, get_year_start, getdate, now_datetime, nowdate from erpnext.hr.doctype.attendance.attendance import ( + DuplicateAttendanceError, + OverlappingShiftAttendanceError, get_month_map, get_unmarked_days, mark_attendance, @@ -23,11 +25,112 @@ class TestAttendance(FrappeTestCase): from_date = get_year_start(getdate()) to_date = get_year_ending(getdate()) self.holiday_list = make_holiday_list(from_date=from_date, to_date=to_date) + frappe.db.delete("Attendance") + + def test_duplicate_attendance(self): + employee = make_employee("test_duplicate_attendance@example.com", company="_Test Company") + date = nowdate() + + mark_attendance(employee, date, "Present") + attendance = frappe.get_doc( + { + "doctype": "Attendance", + "employee": employee, + "attendance_date": date, + "status": "Absent", + "company": "_Test Company", + } + ) + + self.assertRaises(DuplicateAttendanceError, attendance.insert) + + def test_duplicate_attendance_with_shift(self): + from erpnext.hr.doctype.shift_type.test_shift_type import setup_shift_type + + employee = make_employee("test_duplicate_attendance@example.com", company="_Test Company") + date = nowdate() + + shift_1 = setup_shift_type(shift_type="Shift 1", start_time="08:00:00", end_time="10:00:00") + mark_attendance(employee, date, "Present", shift=shift_1.name) + + # attendance record with shift + attendance = frappe.get_doc( + { + "doctype": "Attendance", + "employee": employee, + "attendance_date": date, + "status": "Absent", + "company": "_Test Company", + "shift": shift_1.name, + } + ) + + self.assertRaises(DuplicateAttendanceError, attendance.insert) + + # attendance record without any shift + attendance = frappe.get_doc( + { + "doctype": "Attendance", + "employee": employee, + "attendance_date": date, + "status": "Absent", + "company": "_Test Company", + } + ) + + self.assertRaises(DuplicateAttendanceError, attendance.insert) + + def test_overlapping_shift_attendance_validation(self): + from erpnext.hr.doctype.shift_type.test_shift_type import setup_shift_type + + employee = make_employee("test_overlap_attendance@example.com", company="_Test Company") + date = nowdate() + + shift_1 = setup_shift_type(shift_type="Shift 1", start_time="08:00:00", end_time="10:00:00") + shift_2 = setup_shift_type(shift_type="Shift 2", start_time="09:30:00", end_time="11:00:00") + + mark_attendance(employee, date, "Present", shift=shift_1.name) + + # attendance record with overlapping shift + attendance = frappe.get_doc( + { + "doctype": "Attendance", + "employee": employee, + "attendance_date": date, + "status": "Absent", + "company": "_Test Company", + "shift": shift_2.name, + } + ) + + self.assertRaises(OverlappingShiftAttendanceError, attendance.insert) + + def test_allow_attendance_with_different_shifts(self): + # allows attendance with 2 different non-overlapping shifts + from erpnext.hr.doctype.shift_type.test_shift_type import setup_shift_type + + employee = make_employee("test_duplicate_attendance@example.com", company="_Test Company") + date = nowdate() + + shift_1 = setup_shift_type(shift_type="Shift 1", start_time="08:00:00", end_time="10:00:00") + shift_2 = setup_shift_type(shift_type="Shift 2", start_time="11:00:00", end_time="12:00:00") + + mark_attendance(employee, date, "Present", shift_1.name) + frappe.get_doc( + { + "doctype": "Attendance", + "employee": employee, + "attendance_date": date, + "status": "Absent", + "company": "_Test Company", + "shift": shift_2.name, + } + ).insert() def test_mark_absent(self): employee = make_employee("test_mark_absent@example.com") date = nowdate() - frappe.db.delete("Attendance", {"employee": employee, "attendance_date": date}) + attendance = mark_attendance(employee, date, "Absent") fetch_attendance = frappe.get_value( "Attendance", {"employee": employee, "attendance_date": date, "status": "Absent"} @@ -42,7 +145,6 @@ class TestAttendance(FrappeTestCase): employee = make_employee( "test_unmarked_days@example.com", date_of_joining=add_days(first_day, -1) ) - frappe.db.delete("Attendance", {"employee": employee}) frappe.db.set_value("Employee", employee, "holiday_list", self.holiday_list) first_sunday = get_first_sunday(self.holiday_list, for_date=first_day) @@ -67,8 +169,6 @@ class TestAttendance(FrappeTestCase): employee = make_employee( "test_unmarked_days@example.com", date_of_joining=add_days(first_day, -1) ) - frappe.db.delete("Attendance", {"employee": employee}) - frappe.db.set_value("Employee", employee, "holiday_list", self.holiday_list) first_sunday = get_first_sunday(self.holiday_list, for_date=first_day) @@ -95,7 +195,6 @@ class TestAttendance(FrappeTestCase): employee = make_employee( "test_unmarked_days_as_per_doj@example.com", date_of_joining=doj, relieving_date=relieving_date ) - frappe.db.delete("Attendance", {"employee": employee}) frappe.db.set_value("Employee", employee, "holiday_list", self.holiday_list) diff --git a/erpnext/hr/doctype/employee_checkin/employee_checkin.py b/erpnext/hr/doctype/employee_checkin/employee_checkin.py index 87f48b7e25..64eb019b00 100644 --- a/erpnext/hr/doctype/employee_checkin/employee_checkin.py +++ b/erpnext/hr/doctype/employee_checkin/employee_checkin.py @@ -7,6 +7,10 @@ from frappe import _ from frappe.model.document import Document from frappe.utils import cint, get_datetime +from erpnext.hr.doctype.attendance.attendance import ( + get_duplicate_attendance_record, + get_overlapping_shift_attendance, +) from erpnext.hr.doctype.shift_assignment.shift_assignment import ( get_actual_start_end_datetime_of_shift, ) @@ -33,24 +37,24 @@ class EmployeeCheckin(Document): shift_actual_timings = get_actual_start_end_datetime_of_shift( self.employee, get_datetime(self.time), True ) - if shift_actual_timings[0] and shift_actual_timings[1]: + if shift_actual_timings: if ( - shift_actual_timings[2].shift_type.determine_check_in_and_check_out + shift_actual_timings.shift_type.determine_check_in_and_check_out == "Strictly based on Log Type in Employee Checkin" and not self.log_type and not self.skip_auto_attendance ): frappe.throw( _("Log Type is required for check-ins falling in the shift: {0}.").format( - shift_actual_timings[2].shift_type.name + shift_actual_timings.shift_type.name ) ) if not self.attendance: - self.shift = shift_actual_timings[2].shift_type.name - self.shift_actual_start = shift_actual_timings[0] - self.shift_actual_end = shift_actual_timings[1] - self.shift_start = shift_actual_timings[2].start_datetime - self.shift_end = shift_actual_timings[2].end_datetime + self.shift = shift_actual_timings.shift_type.name + self.shift_actual_start = shift_actual_timings.actual_start + self.shift_actual_end = shift_actual_timings.actual_end + self.shift_start = shift_actual_timings.start_datetime + self.shift_end = shift_actual_timings.end_datetime else: self.shift = None @@ -136,10 +140,10 @@ def mark_attendance_and_link_log( return None elif attendance_status in ("Present", "Absent", "Half Day"): employee_doc = frappe.get_doc("Employee", employee) - if not frappe.db.exists( - "Attendance", - {"employee": employee, "attendance_date": attendance_date, "docstatus": ("!=", "2")}, - ): + duplicate = get_duplicate_attendance_record(employee, attendance_date, shift) + overlapping = get_overlapping_shift_attendance(employee, attendance_date, shift) + + if not duplicate and not overlapping: doc_dict = { "doctype": "Attendance", "employee": employee, @@ -232,7 +236,7 @@ def calculate_working_hours(logs, check_in_out_type, working_hours_calc_type): def time_diff_in_hours(start, end): - return round((end - start).total_seconds() / 3600, 1) + return round(float((end - start).total_seconds()) / 3600, 2) def find_index_in_dict(dict_list, key, value): diff --git a/erpnext/hr/doctype/employee_checkin/test_employee_checkin.py b/erpnext/hr/doctype/employee_checkin/test_employee_checkin.py index 97f76b0350..81b44f8fea 100644 --- a/erpnext/hr/doctype/employee_checkin/test_employee_checkin.py +++ b/erpnext/hr/doctype/employee_checkin/test_employee_checkin.py @@ -2,10 +2,19 @@ # See license.txt import unittest -from datetime import timedelta +from datetime import datetime, timedelta import frappe -from frappe.utils import now_datetime, nowdate +from frappe.tests.utils import FrappeTestCase +from frappe.utils import ( + add_days, + get_time, + get_year_ending, + get_year_start, + getdate, + now_datetime, + nowdate, +) from erpnext.hr.doctype.employee.test_employee import make_employee from erpnext.hr.doctype.employee_checkin.employee_checkin import ( @@ -13,9 +22,22 @@ from erpnext.hr.doctype.employee_checkin.employee_checkin import ( calculate_working_hours, mark_attendance_and_link_log, ) +from erpnext.hr.doctype.holiday_list.test_holiday_list import set_holiday_list +from erpnext.hr.doctype.leave_application.test_leave_application import get_first_sunday +from erpnext.hr.doctype.shift_type.test_shift_type import make_shift_assignment, setup_shift_type +from erpnext.payroll.doctype.salary_slip.test_salary_slip import make_holiday_list -class TestEmployeeCheckin(unittest.TestCase): +class TestEmployeeCheckin(FrappeTestCase): + def setUp(self): + frappe.db.delete("Shift Type") + frappe.db.delete("Shift Assignment") + frappe.db.delete("Employee Checkin") + + from_date = get_year_start(getdate()) + to_date = get_year_ending(getdate()) + self.holiday_list = make_holiday_list(from_date=from_date, to_date=to_date) + def test_add_log_based_on_employee_field(self): employee = make_employee("test_add_log_based_on_employee_field@example.com") employee = frappe.get_doc("Employee", employee) @@ -103,6 +125,163 @@ class TestEmployeeCheckin(unittest.TestCase): ) self.assertEqual(working_hours, (4.5, logs_type_2[1].time, logs_type_2[-1].time)) + def test_fetch_shift(self): + employee = make_employee("test_employee_checkin@example.com", company="_Test Company") + + # shift setup for 8-12 + shift_type = setup_shift_type() + date = getdate() + make_shift_assignment(shift_type.name, employee, date) + + # within shift time + timestamp = datetime.combine(date, get_time("08:45:00")) + log = make_checkin(employee, timestamp) + self.assertEqual(log.shift, shift_type.name) + + # "begin checkin before shift time" = 60 mins, so should work for 7:00:00 + timestamp = datetime.combine(date, get_time("07:00:00")) + log = make_checkin(employee, timestamp) + self.assertEqual(log.shift, shift_type.name) + + # "allow checkout after shift end time" = 60 mins, so should work for 13:00:00 + timestamp = datetime.combine(date, get_time("13:00:00")) + log = make_checkin(employee, timestamp) + self.assertEqual(log.shift, shift_type.name) + + # should not fetch this shift beyond allowed time + timestamp = datetime.combine(date, get_time("13:01:00")) + log = make_checkin(employee, timestamp) + self.assertIsNone(log.shift) + + def test_shift_start_and_end_timings(self): + employee = make_employee("test_employee_checkin@example.com", company="_Test Company") + + # shift setup for 8-12 + shift_type = setup_shift_type() + date = getdate() + make_shift_assignment(shift_type.name, employee, date) + + timestamp = datetime.combine(date, get_time("08:45:00")) + log = make_checkin(employee, timestamp) + + self.assertEqual(log.shift, shift_type.name) + self.assertEqual(log.shift_start, datetime.combine(date, get_time("08:00:00"))) + self.assertEqual(log.shift_end, datetime.combine(date, get_time("12:00:00"))) + self.assertEqual(log.shift_actual_start, datetime.combine(date, get_time("07:00:00"))) + self.assertEqual(log.shift_actual_end, datetime.combine(date, get_time("13:00:00"))) + + def test_fetch_shift_based_on_default_shift(self): + employee = make_employee("test_default_shift@example.com", company="_Test Company") + default_shift = setup_shift_type( + shift_type="Default Shift", start_time="14:00:00", end_time="16:00:00" + ) + + date = getdate() + frappe.db.set_value("Employee", employee, "default_shift", default_shift.name) + + timestamp = datetime.combine(date, get_time("14:45:00")) + log = make_checkin(employee, timestamp) + + # should consider default shift + self.assertEqual(log.shift, default_shift.name) + + def test_fetch_shift_spanning_over_two_days(self): + employee = make_employee("test_employee_checkin@example.com", company="_Test Company") + shift_type = setup_shift_type( + shift_type="Midnight Shift", start_time="23:00:00", end_time="01:00:00" + ) + date = getdate() + next_day = add_days(date, 1) + make_shift_assignment(shift_type.name, employee, date) + + # log falls in the first day + timestamp = datetime.combine(date, get_time("23:00:00")) + log = make_checkin(employee, timestamp) + + self.assertEqual(log.shift, shift_type.name) + self.assertEqual(log.shift_start, datetime.combine(date, get_time("23:00:00"))) + self.assertEqual(log.shift_end, datetime.combine(next_day, get_time("01:00:00"))) + self.assertEqual(log.shift_actual_start, datetime.combine(date, get_time("22:00:00"))) + self.assertEqual(log.shift_actual_end, datetime.combine(next_day, get_time("02:00:00"))) + + log.delete() + + # log falls in the second day + prev_day = add_days(date, -1) + timestamp = datetime.combine(date, get_time("01:30:00")) + log = make_checkin(employee, timestamp) + self.assertEqual(log.shift, shift_type.name) + self.assertEqual(log.shift_start, datetime.combine(prev_day, get_time("23:00:00"))) + self.assertEqual(log.shift_end, datetime.combine(date, get_time("01:00:00"))) + self.assertEqual(log.shift_actual_start, datetime.combine(prev_day, get_time("22:00:00"))) + self.assertEqual(log.shift_actual_end, datetime.combine(date, get_time("02:00:00"))) + + def test_no_shift_fetched_on_holiday_as_per_shift_holiday_list(self): + date = getdate() + from_date = get_year_start(date) + to_date = get_year_ending(date) + holiday_list = make_holiday_list(from_date=from_date, to_date=to_date) + + employee = make_employee("test_shift_with_holiday@example.com", company="_Test Company") + setup_shift_type(shift_type="Test Holiday Shift", holiday_list=holiday_list) + + first_sunday = get_first_sunday(holiday_list, for_date=date) + timestamp = datetime.combine(first_sunday, get_time("08:00:00")) + log = make_checkin(employee, timestamp) + + self.assertIsNone(log.shift) + + @set_holiday_list("Salary Slip Test Holiday List", "_Test Company") + def test_no_shift_fetched_on_holiday_as_per_employee_holiday_list(self): + employee = make_employee("test_shift_with_holiday@example.com", company="_Test Company") + shift_type = setup_shift_type(shift_type="Test Holiday Shift") + shift_type.holiday_list = None + shift_type.save() + + date = getdate() + + first_sunday = get_first_sunday(self.holiday_list, for_date=date) + timestamp = datetime.combine(first_sunday, get_time("08:00:00")) + log = make_checkin(employee, timestamp) + + self.assertIsNone(log.shift) + + def test_consecutive_shift_assignments_overlapping_within_grace_period(self): + # test adjustment for start and end times if they are overlapping + # within "begin_check_in_before_shift_start_time" and "allow_check_out_after_shift_end_time" periods + employee = make_employee("test_shift@example.com", company="_Test Company") + + # 8 - 12 + shift1 = setup_shift_type() + # 12:30 - 16:30 + shift2 = setup_shift_type( + shift_type="Consecutive Shift", start_time="12:30:00", end_time="16:30:00" + ) + + # the actual start and end times (with grace) for these shifts are 7 - 13 and 11:30 - 17:30 + date = getdate() + make_shift_assignment(shift1.name, employee, date) + make_shift_assignment(shift2.name, employee, date) + + # log at 12:30 should set shift2 and actual start as 12 and not 11:30 + timestamp = datetime.combine(date, get_time("12:30:00")) + log = make_checkin(employee, timestamp) + self.assertEqual(log.shift, shift2.name) + self.assertEqual(log.shift_start, datetime.combine(date, get_time("12:30:00"))) + self.assertEqual(log.shift_actual_start, datetime.combine(date, get_time("12:00:00"))) + + # log at 12:00 should set shift1 and actual end as 12 and not 1 since the next shift's grace starts + timestamp = datetime.combine(date, get_time("12:00:00")) + log = make_checkin(employee, timestamp) + self.assertEqual(log.shift, shift1.name) + self.assertEqual(log.shift_end, datetime.combine(date, get_time("12:00:00"))) + self.assertEqual(log.shift_actual_end, datetime.combine(date, get_time("12:00:00"))) + + # log at 12:01 should set shift2 + timestamp = datetime.combine(date, get_time("12:01:00")) + log = make_checkin(employee, timestamp) + self.assertEqual(log.shift, shift2.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/erpnext/hr/doctype/shift_assignment/shift_assignment.py b/erpnext/hr/doctype/shift_assignment/shift_assignment.py index f6bd15951d..0b21c00eac 100644 --- a/erpnext/hr/doctype/shift_assignment/shift_assignment.py +++ b/erpnext/hr/doctype/shift_assignment/shift_assignment.py @@ -3,83 +3,120 @@ from datetime import datetime, timedelta +from typing import Dict, List import frappe from frappe import _ from frappe.model.document import Document -from frappe.utils import cstr, getdate, now_datetime, nowdate +from frappe.query_builder import Criterion +from frappe.utils import cstr, get_datetime, get_link_to_form, get_time, getdate, now_datetime 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.utils import validate_active_employee +class OverlappingShiftError(frappe.ValidationError): + pass + + class ShiftAssignment(Document): def validate(self): validate_active_employee(self.employee) - self.validate_overlapping_dates() + self.validate_overlapping_shifts() if self.end_date: self.validate_from_to_dates("start_date", "end_date") - def validate_overlapping_dates(self): + def validate_overlapping_shifts(self): + overlapping_dates = self.get_overlapping_dates() + if len(overlapping_dates): + # if dates are overlapping, check if timings are overlapping, else allow + overlapping_timings = has_overlapping_timings(self.shift_type, overlapping_dates[0].shift_type) + if overlapping_timings: + self.throw_overlap_error(overlapping_dates[0]) + + def get_overlapping_dates(self): if not self.name: self.name = "New Shift Assignment" - condition = """and ( - end_date is null - or - %(start_date)s between start_date and end_date - """ - - if self.end_date: - condition += """ or - %(end_date)s between start_date and end_date - or - start_date between %(start_date)s and %(end_date)s - ) """ - else: - condition += """ ) """ - - assigned_shifts = frappe.db.sql( - """ - select name, shift_type, start_date ,end_date, docstatus, status - from `tabShift Assignment` - where - employee=%(employee)s and docstatus = 1 - and name != %(name)s - and status = "Active" - {0} - """.format( - condition - ), - { - "employee": self.employee, - "shift_type": self.shift_type, - "start_date": self.start_date, - "end_date": self.end_date, - "name": self.name, - }, - as_dict=1, + shift = frappe.qb.DocType("Shift Assignment") + query = ( + frappe.qb.from_(shift) + .select(shift.name, shift.shift_type, shift.docstatus, shift.status) + .where( + (shift.employee == self.employee) + & (shift.docstatus == 1) + & (shift.name != self.name) + & (shift.status == "Active") + ) ) - if len(assigned_shifts): - self.throw_overlap_error(assigned_shifts[0]) + if self.end_date: + query = query.where( + Criterion.any( + [ + Criterion.any( + [ + shift.end_date.isnull(), + ((self.start_date >= shift.start_date) & (self.start_date <= shift.end_date)), + ] + ), + Criterion.any( + [ + ((self.end_date >= shift.start_date) & (self.end_date <= shift.end_date)), + shift.start_date.between(self.start_date, self.end_date), + ] + ), + ] + ) + ) + else: + query = query.where( + shift.end_date.isnull() + | ((self.start_date >= shift.start_date) & (self.start_date <= shift.end_date)) + ) + + return query.run(as_dict=True) def throw_overlap_error(self, shift_details): shift_details = frappe._dict(shift_details) if shift_details.docstatus == 1 and shift_details.status == "Active": - msg = _("Employee {0} already has Active Shift {1}: {2}").format( - frappe.bold(self.employee), frappe.bold(self.shift_type), frappe.bold(shift_details.name) + msg = _( + "Employee {0} already has an active Shift {1}: {2} that overlaps within this period." + ).format( + frappe.bold(self.employee), + frappe.bold(shift_details.shift_type), + get_link_to_form("Shift Assignment", shift_details.name), ) - if shift_details.start_date: - msg += " " + _("from {0}").format(getdate(self.start_date).strftime("%d-%m-%Y")) - title = "Ongoing Shift" - if shift_details.end_date: - msg += " " + _("to {0}").format(getdate(self.end_date).strftime("%d-%m-%Y")) - title = "Active Shift" - if msg: - frappe.throw(msg, title=title) + frappe.throw(msg, title=_("Overlapping Shifts"), exc=OverlappingShiftError) + + +def has_overlapping_timings(shift_1: str, shift_2: str) -> bool: + """ + Accepts two shift types and checks whether their timings are overlapping + """ + curr_shift = frappe.db.get_value("Shift Type", shift_1, ["start_time", "end_time"], as_dict=True) + overlapping_shift = frappe.db.get_value( + "Shift Type", shift_2, ["start_time", "end_time"], as_dict=True + ) + + if ( + ( + curr_shift.start_time > overlapping_shift.start_time + and curr_shift.start_time < overlapping_shift.end_time + ) + or ( + curr_shift.end_time > overlapping_shift.start_time + and curr_shift.end_time < overlapping_shift.end_time + ) + or ( + curr_shift.start_time <= overlapping_shift.start_time + and curr_shift.end_time >= overlapping_shift.end_time + ) + ): + return True + return False @frappe.whitelist() @@ -155,102 +192,195 @@ def get_shift_type_timing(shift_types): return shift_timing_map +def get_shift_for_time(shifts: List[Dict], for_timestamp: datetime) -> Dict: + """Returns shift with details for given timestamp""" + valid_shifts = [] + + for entry in shifts: + shift_details = get_shift_details(entry.shift_type, for_timestamp=for_timestamp) + + if ( + get_datetime(shift_details.actual_start) + <= get_datetime(for_timestamp) + <= get_datetime(shift_details.actual_end) + ): + valid_shifts.append(shift_details) + + valid_shifts.sort(key=lambda x: x["actual_start"]) + + if len(valid_shifts) > 1: + for i in range(len(valid_shifts) - 1): + # comparing 2 consecutive shifts and adjusting start and end times + # if they are overlapping within grace period + curr_shift = valid_shifts[i] + next_shift = valid_shifts[i + 1] + + if curr_shift and next_shift: + next_shift.actual_start = ( + curr_shift.end_datetime + if next_shift.actual_start < curr_shift.end_datetime + else next_shift.actual_start + ) + curr_shift.actual_end = ( + next_shift.actual_start + if curr_shift.actual_end > next_shift.actual_start + else curr_shift.actual_end + ) + + valid_shifts[i] = curr_shift + valid_shifts[i + 1] = next_shift + + return get_exact_shift(valid_shifts, for_timestamp) or {} + + return (valid_shifts and valid_shifts[0]) or {} + + +def get_shifts_for_date(employee: str, for_timestamp: datetime) -> List[Dict[str, str]]: + """Returns list of shifts with details for given date""" + assignment = frappe.qb.DocType("Shift Assignment") + + return ( + frappe.qb.from_(assignment) + .select(assignment.name, assignment.shift_type) + .where( + (assignment.employee == employee) + & (assignment.docstatus == 1) + & (assignment.status == "Active") + & (assignment.start_date <= getdate(for_timestamp.date())) + & ( + Criterion.any( + [ + assignment.end_date.isnull(), + (assignment.end_date.isnotnull() & (getdate(for_timestamp.date()) >= assignment.end_date)), + ] + ) + ) + ) + ).run(as_dict=True) + + +def get_shift_for_timestamp(employee: str, for_timestamp: datetime) -> Dict: + shifts = get_shifts_for_date(employee, for_timestamp) + if shifts: + return get_shift_for_time(shifts, for_timestamp) + return {} + + def get_employee_shift( - employee, for_date=None, consider_default_shift=False, next_shift_direction=None -): + employee: str, + for_timestamp: datetime = None, + consider_default_shift: bool = False, + next_shift_direction: str = None, +) -> Dict: """Returns a Shift Type for the given employee on the given date. (excluding the holidays) :param employee: Employee for which shift is required. - :param for_date: Date on which shift are required + :param for_timestamp: DateTime on which shift is required :param consider_default_shift: If set to true, default shift is taken when no shift assignment is found. :param next_shift_direction: One of: None, 'forward', 'reverse'. Direction to look for next shift if shift not found on given date. """ - if for_date is None: - for_date = nowdate() + if for_timestamp is None: + for_timestamp = now_datetime() + + shift_details = get_shift_for_timestamp(employee, for_timestamp) + + # if shift assignment is not found, consider default shift default_shift = frappe.db.get_value("Employee", employee, "default_shift") - shift_type_name = None - shift_assignment_details = frappe.db.get_value( - "Shift Assignment", - {"employee": employee, "start_date": ("<=", for_date), "docstatus": "1", "status": "Active"}, - ["shift_type", "end_date"], + if not shift_details and consider_default_shift: + shift_details = get_shift_details(default_shift, for_timestamp) + + # if its a holiday, reset + if shift_details and is_holiday_date(employee, shift_details): + shift_details = None + + # if no shift is found, find next or prev shift assignment based on direction + if not shift_details and next_shift_direction: + shift_details = get_prev_or_next_shift( + employee, for_timestamp, consider_default_shift, default_shift, next_shift_direction + ) + + return shift_details or {} + + +def get_prev_or_next_shift( + employee: str, + for_timestamp: datetime, + consider_default_shift: bool, + default_shift: str, + next_shift_direction: str, +) -> Dict: + """Returns a dict of shift details for the next or prev shift based on the next_shift_direction""" + MAX_DAYS = 366 + shift_details = {} + + if consider_default_shift and default_shift: + direction = -1 if next_shift_direction == "reverse" else 1 + for i in range(MAX_DAYS): + date = for_timestamp + timedelta(days=direction * (i + 1)) + shift_details = get_employee_shift(employee, date, consider_default_shift, None) + if shift_details: + break + else: + direction = "<" if next_shift_direction == "reverse" else ">" + sort_order = "desc" if next_shift_direction == "reverse" else "asc" + dates = frappe.db.get_all( + "Shift Assignment", + ["start_date", "end_date"], + { + "employee": employee, + "start_date": (direction, for_timestamp.date()), + "docstatus": 1, + "status": "Active", + }, + as_list=True, + limit=MAX_DAYS, + order_by="start_date " + sort_order, + ) + + if dates: + for date in dates: + if date[1] and date[1] < for_timestamp.date(): + continue + shift_details = get_employee_shift( + employee, datetime.combine(date[0], for_timestamp.time()), consider_default_shift, None + ) + if shift_details: + break + + return shift_details or {} + + +def is_holiday_date(employee: str, shift_details: Dict) -> bool: + holiday_list_name = frappe.db.get_value( + "Shift Type", shift_details.shift_type.name, "holiday_list" ) - if shift_assignment_details: - shift_type_name = shift_assignment_details[0] + if not holiday_list_name: + holiday_list_name = get_holiday_list_for_employee(employee, False) - # if end_date present means that shift is over after end_date else it is a ongoing shift. - if shift_assignment_details[1] and for_date >= shift_assignment_details[1]: - shift_type_name = None - - if not shift_type_name and consider_default_shift: - shift_type_name = default_shift - if shift_type_name: - holiday_list_name = frappe.db.get_value("Shift Type", shift_type_name, "holiday_list") - if not holiday_list_name: - holiday_list_name = get_holiday_list_for_employee(employee, False) - if holiday_list_name and is_holiday(holiday_list_name, for_date): - shift_type_name = None - - if not shift_type_name and next_shift_direction: - MAX_DAYS = 366 - if consider_default_shift and default_shift: - direction = -1 if next_shift_direction == "reverse" else +1 - for i in range(MAX_DAYS): - date = for_date + timedelta(days=direction * (i + 1)) - shift_details = get_employee_shift(employee, date, consider_default_shift, None) - if shift_details: - shift_type_name = shift_details.shift_type.name - for_date = date - break - else: - direction = "<" if next_shift_direction == "reverse" else ">" - sort_order = "desc" if next_shift_direction == "reverse" else "asc" - dates = frappe.db.get_all( - "Shift Assignment", - ["start_date", "end_date"], - { - "employee": employee, - "start_date": (direction, for_date), - "docstatus": "1", - "status": "Active", - }, - as_list=True, - limit=MAX_DAYS, - order_by="start_date " + sort_order, - ) - - if dates: - for date in dates: - if date[1] and date[1] < for_date: - continue - shift_details = get_employee_shift(employee, date[0], consider_default_shift, None) - if shift_details: - shift_type_name = shift_details.shift_type.name - for_date = date[0] - break - - return get_shift_details(shift_type_name, for_date) + return holiday_list_name and is_holiday(holiday_list_name, shift_details.start_datetime.date()) -def get_employee_shift_timings(employee, for_timestamp=None, consider_default_shift=False): +def get_employee_shift_timings( + employee: str, for_timestamp: datetime = None, consider_default_shift: bool = False +) -> List[Dict]: """Returns previous shift, current/upcoming shift, next_shift for the given timestamp and employee""" if for_timestamp is None: for_timestamp = now_datetime() + # write and verify a test case for midnight shift. prev_shift = curr_shift = next_shift = None - curr_shift = get_employee_shift(employee, for_timestamp.date(), consider_default_shift, "forward") + curr_shift = get_employee_shift(employee, for_timestamp, consider_default_shift, "forward") if curr_shift: next_shift = get_employee_shift( - employee, - curr_shift.start_datetime.date() + timedelta(days=1), - consider_default_shift, - "forward", + employee, curr_shift.start_datetime + timedelta(days=1), consider_default_shift, "forward" ) prev_shift = get_employee_shift( - employee, for_timestamp.date() + timedelta(days=-1), consider_default_shift, "reverse" + employee, for_timestamp + timedelta(days=-1), consider_default_shift, "reverse" ) if curr_shift: + # adjust actual start and end times if they are overlapping with grace period (before start and after end) if prev_shift: curr_shift.actual_start = ( prev_shift.end_datetime @@ -273,31 +403,102 @@ def get_employee_shift_timings(employee, for_timestamp=None, consider_default_sh if curr_shift.actual_end > next_shift.actual_start else curr_shift.actual_end ) + return prev_shift, curr_shift, next_shift -def get_shift_details(shift_type_name, for_date=None): - """Returns Shift Details which contain some additional information as described below. - 'shift_details' contains the following keys: - 'shift_type' - Object of DocType Shift Type, - 'start_datetime' - Date and Time of shift start on given date, - 'end_datetime' - Date and Time of shift end on given date, - 'actual_start' - datetime of shift start after adding 'begin_check_in_before_shift_start_time', - 'actual_end' - datetime of shift end after adding 'allow_check_out_after_shift_end_time'(None is returned if this is zero) +def get_actual_start_end_datetime_of_shift( + employee: str, for_timestamp: datetime, consider_default_shift: bool = False +) -> Dict: + """Returns a Dict containing shift details with actual_start and actual_end datetime values + Here 'actual' means taking into account the "begin_check_in_before_shift_start_time" and "allow_check_out_after_shift_end_time". + Empty Dict is returned if the timestamp is outside any actual shift timings. - :param shift_type_name: shift type name for which shift_details is required. - :param for_date: Date on which shift_details are required + :param employee (str): Employee name + :param for_timestamp (datetime, optional): Datetime value of checkin, if not provided considers current datetime + :param consider_default_shift (bool, optional): Flag (defaults to False) to specify whether to consider + default shift in employee master if no shift assignment is found + """ + shift_timings_as_per_timestamp = get_employee_shift_timings( + employee, for_timestamp, consider_default_shift + ) + return get_exact_shift(shift_timings_as_per_timestamp, for_timestamp) + + +def get_exact_shift(shifts: List, for_timestamp: datetime) -> Dict: + """Returns the shift details (dict) for the exact shift in which the 'for_timestamp' value falls among multiple shifts""" + shift_details = dict() + timestamp_list = [] + + for shift in shifts: + if shift: + timestamp_list.extend([shift.actual_start, shift.actual_end]) + else: + timestamp_list.extend([None, None]) + + timestamp_index = None + for index, timestamp in enumerate(timestamp_list): + if not timestamp: + continue + + if for_timestamp < timestamp: + timestamp_index = index + elif for_timestamp == timestamp: + # on timestamp boundary + if index % 2 == 1: + timestamp_index = index + else: + timestamp_index = index + 1 + + if timestamp_index: + break + + if timestamp_index and timestamp_index % 2 == 1: + shift_details = shifts[int((timestamp_index - 1) / 2)] + + return shift_details + + +def get_shift_details(shift_type_name: str, for_timestamp: datetime = None) -> Dict: + """Returns a Dict containing shift details with the following data: + 'shift_type' - Object of DocType Shift Type, + 'start_datetime' - datetime of shift start on given timestamp, + 'end_datetime' - datetime of shift end on given timestamp, + 'actual_start' - datetime of shift start after adding 'begin_check_in_before_shift_start_time', + 'actual_end' - datetime of shift end after adding 'allow_check_out_after_shift_end_time' (None is returned if this is zero) + + :param shift_type_name (str): shift type name for which shift_details are required. + :param for_timestamp (datetime, optional): Datetime value of checkin, if not provided considers current datetime """ if not shift_type_name: - return None - if not for_date: - for_date = nowdate() + return {} + + if for_timestamp is None: + for_timestamp = now_datetime() + shift_type = frappe.get_doc("Shift Type", shift_type_name) - start_datetime = datetime.combine(for_date, datetime.min.time()) + shift_type.start_time - for_date = ( - for_date + timedelta(days=1) if shift_type.start_time > shift_type.end_time else for_date + shift_actual_start = shift_type.start_time - timedelta( + minutes=shift_type.begin_check_in_before_shift_start_time ) - end_datetime = datetime.combine(for_date, datetime.min.time()) + shift_type.end_time + + if shift_type.start_time > shift_type.end_time: + # shift spans accross 2 different days + if get_time(for_timestamp.time()) >= get_time(shift_actual_start): + # if for_timestamp is greater than start time, it's within the first day + start_datetime = datetime.combine(for_timestamp, datetime.min.time()) + shift_type.start_time + for_timestamp = for_timestamp + timedelta(days=1) + end_datetime = datetime.combine(for_timestamp, datetime.min.time()) + shift_type.end_time + + elif get_time(for_timestamp.time()) < get_time(shift_actual_start): + # if for_timestamp is less than start time, it's within the second day + end_datetime = datetime.combine(for_timestamp, datetime.min.time()) + shift_type.end_time + for_timestamp = for_timestamp + timedelta(days=-1) + start_datetime = datetime.combine(for_timestamp, datetime.min.time()) + shift_type.start_time + else: + # start and end timings fall on the same day + start_datetime = datetime.combine(for_timestamp, datetime.min.time()) + shift_type.start_time + end_datetime = datetime.combine(for_timestamp, datetime.min.time()) + shift_type.end_time + actual_start = start_datetime - timedelta( minutes=shift_type.begin_check_in_before_shift_start_time ) @@ -312,34 +513,3 @@ def get_shift_details(shift_type_name, for_date=None): "actual_end": actual_end, } ) - - -def get_actual_start_end_datetime_of_shift(employee, for_datetime, consider_default_shift=False): - """Takes a datetime and returns the 'actual' start datetime and end datetime of the shift in which the timestamp belongs. - Here 'actual' means - taking in to account the "begin_check_in_before_shift_start_time" and "allow_check_out_after_shift_end_time". - None is returned if the timestamp is outside any actual shift timings. - Shift Details is also returned(current/upcoming i.e. if timestamp not in any actual shift then details of next shift returned) - """ - actual_shift_start = actual_shift_end = shift_details = None - shift_timings_as_per_timestamp = get_employee_shift_timings( - employee, for_datetime, consider_default_shift - ) - timestamp_list = [] - for shift in shift_timings_as_per_timestamp: - if shift: - timestamp_list.extend([shift.actual_start, shift.actual_end]) - else: - timestamp_list.extend([None, None]) - timestamp_index = None - for index, timestamp in enumerate(timestamp_list): - if timestamp and for_datetime <= timestamp: - timestamp_index = index - break - if timestamp_index and timestamp_index % 2 == 1: - shift_details = shift_timings_as_per_timestamp[int((timestamp_index - 1) / 2)] - actual_shift_start = shift_details.actual_start - actual_shift_end = shift_details.actual_end - elif timestamp_index: - shift_details = shift_timings_as_per_timestamp[int(timestamp_index / 2)] - - return actual_shift_start, actual_shift_end, shift_details diff --git a/erpnext/hr/doctype/shift_assignment/test_shift_assignment.py b/erpnext/hr/doctype/shift_assignment/test_shift_assignment.py index 4a1ec293bd..0fe9108168 100644 --- a/erpnext/hr/doctype/shift_assignment/test_shift_assignment.py +++ b/erpnext/hr/doctype/shift_assignment/test_shift_assignment.py @@ -4,16 +4,23 @@ import unittest import frappe -from frappe.utils import add_days, nowdate +from frappe.tests.utils import FrappeTestCase +from frappe.utils import add_days, getdate, nowdate + +from erpnext.hr.doctype.employee.test_employee import make_employee +from erpnext.hr.doctype.shift_assignment.shift_assignment import OverlappingShiftError +from erpnext.hr.doctype.shift_type.test_shift_type import make_shift_assignment, setup_shift_type test_dependencies = ["Shift Type"] -class TestShiftAssignment(unittest.TestCase): +class TestShiftAssignment(FrappeTestCase): def setUp(self): - frappe.db.sql("delete from `tabShift Assignment`") + frappe.db.delete("Shift Assignment") + frappe.db.delete("Shift Type") def test_make_shift_assignment(self): + setup_shift_type(shift_type="Day Shift") shift_assignment = frappe.get_doc( { "doctype": "Shift Assignment", @@ -29,7 +36,7 @@ class TestShiftAssignment(unittest.TestCase): def test_overlapping_for_ongoing_shift(self): # shift should be Ongoing if Only start_date is present and status = Active - + setup_shift_type(shift_type="Day Shift") shift_assignment_1 = frappe.get_doc( { "doctype": "Shift Assignment", @@ -54,11 +61,11 @@ class TestShiftAssignment(unittest.TestCase): } ) - self.assertRaises(frappe.ValidationError, shift_assignment.save) + self.assertRaises(OverlappingShiftError, shift_assignment.save) def test_overlapping_for_fixed_period_shift(self): # shift should is for Fixed period if Only start_date and end_date both are present and status = Active - + setup_shift_type(shift_type="Day Shift") shift_assignment_1 = frappe.get_doc( { "doctype": "Shift Assignment", @@ -85,4 +92,65 @@ class TestShiftAssignment(unittest.TestCase): } ) - self.assertRaises(frappe.ValidationError, shift_assignment_3.save) + self.assertRaises(OverlappingShiftError, shift_assignment_3.save) + + def test_overlapping_for_a_fixed_period_shift_and_ongoing_shift(self): + employee = make_employee("test_shift_assignment@example.com", company="_Test Company") + + # shift setup for 8-12 + shift_type = setup_shift_type(shift_type="Shift 1", start_time="08:00:00", end_time="12:00:00") + date = getdate() + # shift with end date + make_shift_assignment(shift_type.name, employee, date, add_days(date, 30)) + + # shift setup for 11-15 + shift_type = setup_shift_type(shift_type="Shift 2", start_time="11:00:00", end_time="15:00:00") + date = getdate() + + # shift assignment without end date + shift2 = frappe.get_doc( + { + "doctype": "Shift Assignment", + "shift_type": shift_type.name, + "company": "_Test Company", + "employee": employee, + "start_date": date, + } + ) + self.assertRaises(OverlappingShiftError, shift2.insert) + + def test_overlap_validation_for_shifts_on_same_day_with_overlapping_timeslots(self): + employee = make_employee("test_shift_assignment@example.com", company="_Test Company") + + # shift setup for 8-12 + shift_type = setup_shift_type(shift_type="Shift 1", start_time="08:00:00", end_time="12:00:00") + date = getdate() + make_shift_assignment(shift_type.name, employee, date) + + # shift setup for 11-15 + shift_type = setup_shift_type(shift_type="Shift 2", start_time="11:00:00", end_time="15:00:00") + date = getdate() + + shift2 = frappe.get_doc( + { + "doctype": "Shift Assignment", + "shift_type": shift_type.name, + "company": "_Test Company", + "employee": employee, + "start_date": date, + } + ) + self.assertRaises(OverlappingShiftError, shift2.insert) + + def test_multiple_shift_assignments_for_same_day(self): + employee = make_employee("test_shift_assignment@example.com", company="_Test Company") + + # shift setup for 8-12 + shift_type = setup_shift_type(shift_type="Shift 1", start_time="08:00:00", end_time="12:00:00") + date = getdate() + make_shift_assignment(shift_type.name, employee, date) + + # shift setup for 13-15 + shift_type = setup_shift_type(shift_type="Shift 2", start_time="13:00:00", end_time="15:00:00") + date = getdate() + make_shift_assignment(shift_type.name, employee, date) diff --git a/erpnext/hr/doctype/shift_request/shift_request.py b/erpnext/hr/doctype/shift_request/shift_request.py index b5beef7a99..2bee2404aa 100644 --- a/erpnext/hr/doctype/shift_request/shift_request.py +++ b/erpnext/hr/doctype/shift_request/shift_request.py @@ -5,12 +5,14 @@ import frappe from frappe import _ from frappe.model.document import Document -from frappe.utils import formatdate, getdate +from frappe.query_builder import Criterion +from frappe.utils import get_link_to_form, getdate +from erpnext.hr.doctype.shift_assignment.shift_assignment import has_overlapping_timings from erpnext.hr.utils import share_doc_with_approver, validate_active_employee -class OverlapError(frappe.ValidationError): +class OverlappingShiftRequestError(frappe.ValidationError): pass @@ -18,7 +20,7 @@ class ShiftRequest(Document): def validate(self): validate_active_employee(self.employee) self.validate_dates() - self.validate_shift_request_overlap_dates() + self.validate_overlapping_shift_requests() self.validate_approver() self.validate_default_shift() @@ -79,37 +81,60 @@ class ShiftRequest(Document): if self.from_date and self.to_date and (getdate(self.to_date) < getdate(self.from_date)): frappe.throw(_("To date cannot be before from date")) - def validate_shift_request_overlap_dates(self): + def validate_overlapping_shift_requests(self): + overlapping_dates = self.get_overlapping_dates() + if len(overlapping_dates): + # if dates are overlapping, check if timings are overlapping, else allow + overlapping_timings = has_overlapping_timings(self.shift_type, overlapping_dates[0].shift_type) + if overlapping_timings: + self.throw_overlap_error(overlapping_dates[0]) + + def get_overlapping_dates(self): if not self.name: self.name = "New Shift Request" - d = frappe.db.sql( - """ - select - name, shift_type, from_date, to_date - from `tabShift Request` - where employee = %(employee)s and docstatus < 2 - and ((%(from_date)s >= from_date - and %(from_date)s <= to_date) or - ( %(to_date)s >= from_date - and %(to_date)s <= to_date )) - and name != %(name)s""", - { - "employee": self.employee, - "shift_type": self.shift_type, - "from_date": self.from_date, - "to_date": self.to_date, - "name": self.name, - }, - as_dict=1, + shift = frappe.qb.DocType("Shift Request") + query = ( + frappe.qb.from_(shift) + .select(shift.name, shift.shift_type) + .where((shift.employee == self.employee) & (shift.docstatus < 2) & (shift.name != self.name)) ) - for date_overlap in d: - if date_overlap["name"]: - self.throw_overlap_error(date_overlap) + if self.to_date: + query = query.where( + Criterion.any( + [ + Criterion.any( + [ + shift.to_date.isnull(), + ((self.from_date >= shift.from_date) & (self.from_date <= shift.to_date)), + ] + ), + Criterion.any( + [ + ((self.to_date >= shift.from_date) & (self.to_date <= shift.to_date)), + shift.from_date.between(self.from_date, self.to_date), + ] + ), + ] + ) + ) + else: + query = query.where( + shift.to_date.isnull() + | ((self.from_date >= shift.from_date) & (self.from_date <= shift.to_date)) + ) - def throw_overlap_error(self, d): - msg = _("Employee {0} has already applied for {1} between {2} and {3}").format( - self.employee, d["shift_type"], formatdate(d["from_date"]), formatdate(d["to_date"]) - ) + """ : {0}""".format(d["name"]) - frappe.throw(msg, OverlapError) + return query.run(as_dict=True) + + def throw_overlap_error(self, shift_details): + shift_details = frappe._dict(shift_details) + msg = _( + "Employee {0} has already applied for Shift {1}: {2} that overlaps within this period" + ).format( + frappe.bold(self.employee), + frappe.bold(shift_details.shift_type), + get_link_to_form("Shift Request", shift_details.name), + ) + + frappe.throw(msg, title=_("Overlapping Shift Requests"), exc=OverlappingShiftRequestError) diff --git a/erpnext/hr/doctype/shift_request/test_shift_request.py b/erpnext/hr/doctype/shift_request/test_shift_request.py index b4f5177215..c47418cfa8 100644 --- a/erpnext/hr/doctype/shift_request/test_shift_request.py +++ b/erpnext/hr/doctype/shift_request/test_shift_request.py @@ -4,23 +4,24 @@ import unittest import frappe +from frappe.tests.utils import FrappeTestCase from frappe.utils import add_days, nowdate from erpnext.hr.doctype.employee.test_employee import make_employee +from erpnext.hr.doctype.shift_request.shift_request import OverlappingShiftRequestError +from erpnext.hr.doctype.shift_type.test_shift_type import setup_shift_type test_dependencies = ["Shift Type"] -class TestShiftRequest(unittest.TestCase): +class TestShiftRequest(FrappeTestCase): def setUp(self): - for doctype in ["Shift Request", "Shift Assignment"]: - frappe.db.sql("delete from `tab{doctype}`".format(doctype=doctype)) - - def tearDown(self): - frappe.db.rollback() + for doctype in ["Shift Request", "Shift Assignment", "Shift Type"]: + frappe.db.delete(doctype) def test_make_shift_request(self): "Test creation/updation of Shift Assignment from Shift Request." + setup_shift_type(shift_type="Day Shift") department = frappe.get_value("Employee", "_T-Employee-00001", "department") set_shift_approver(department) approver = frappe.db.sql( @@ -48,6 +49,7 @@ class TestShiftRequest(unittest.TestCase): self.assertEqual(shift_assignment_docstatus, 2) def test_shift_request_approver_perms(self): + setup_shift_type(shift_type="Day Shift") employee = frappe.get_doc("Employee", "_T-Employee-00001") user = "test_approver_perm_emp@example.com" make_employee(user, "_Test Company") @@ -87,6 +89,145 @@ class TestShiftRequest(unittest.TestCase): employee.shift_request_approver = "" employee.save() + def test_overlap_for_request_without_to_date(self): + # shift should be Ongoing if Only from_date is present + user = "test_shift_request@example.com" + employee = make_employee(user, company="_Test Company", shift_request_approver=user) + setup_shift_type(shift_type="Day Shift") + + shift_request = frappe.get_doc( + { + "doctype": "Shift Request", + "shift_type": "Day Shift", + "company": "_Test Company", + "employee": employee, + "from_date": nowdate(), + "approver": user, + "status": "Approved", + } + ).submit() + + shift_request = frappe.get_doc( + { + "doctype": "Shift Request", + "shift_type": "Day Shift", + "company": "_Test Company", + "employee": employee, + "from_date": add_days(nowdate(), 2), + "approver": user, + "status": "Approved", + } + ) + + self.assertRaises(OverlappingShiftRequestError, shift_request.save) + + def test_overlap_for_request_with_from_and_to_dates(self): + user = "test_shift_request@example.com" + employee = make_employee(user, company="_Test Company", shift_request_approver=user) + setup_shift_type(shift_type="Day Shift") + + shift_request = frappe.get_doc( + { + "doctype": "Shift Request", + "shift_type": "Day Shift", + "company": "_Test Company", + "employee": employee, + "from_date": nowdate(), + "to_date": add_days(nowdate(), 30), + "approver": user, + "status": "Approved", + } + ).submit() + + shift_request = frappe.get_doc( + { + "doctype": "Shift Request", + "shift_type": "Day Shift", + "company": "_Test Company", + "employee": employee, + "from_date": add_days(nowdate(), 10), + "to_date": add_days(nowdate(), 35), + "approver": user, + "status": "Approved", + } + ) + + self.assertRaises(OverlappingShiftRequestError, shift_request.save) + + def test_overlapping_for_a_fixed_period_shift_and_ongoing_shift(self): + user = "test_shift_request@example.com" + employee = make_employee(user, company="_Test Company", shift_request_approver=user) + + # shift setup for 8-12 + shift_type = setup_shift_type(shift_type="Shift 1", start_time="08:00:00", end_time="12:00:00") + date = nowdate() + + # shift with end date + frappe.get_doc( + { + "doctype": "Shift Request", + "shift_type": shift_type.name, + "company": "_Test Company", + "employee": employee, + "from_date": date, + "to_date": add_days(date, 30), + "approver": user, + "status": "Approved", + } + ).submit() + + # shift setup for 11-15 + shift_type = setup_shift_type(shift_type="Shift 2", start_time="11:00:00", end_time="15:00:00") + shift2 = frappe.get_doc( + { + "doctype": "Shift Request", + "shift_type": shift_type.name, + "company": "_Test Company", + "employee": employee, + "from_date": date, + "approver": user, + "status": "Approved", + } + ) + + self.assertRaises(OverlappingShiftRequestError, shift2.insert) + + def test_allow_non_overlapping_shift_requests_for_same_day(self): + user = "test_shift_request@example.com" + employee = make_employee(user, company="_Test Company", shift_request_approver=user) + + # shift setup for 8-12 + shift_type = setup_shift_type(shift_type="Shift 1", start_time="08:00:00", end_time="12:00:00") + date = nowdate() + + # shift with end date + frappe.get_doc( + { + "doctype": "Shift Request", + "shift_type": shift_type.name, + "company": "_Test Company", + "employee": employee, + "from_date": date, + "to_date": add_days(date, 30), + "approver": user, + "status": "Approved", + } + ).submit() + + # shift setup for 13-15 + shift_type = setup_shift_type(shift_type="Shift 2", start_time="13:00:00", end_time="15:00:00") + frappe.get_doc( + { + "doctype": "Shift Request", + "shift_type": shift_type.name, + "company": "_Test Company", + "employee": employee, + "from_date": date, + "approver": user, + "status": "Approved", + } + ).submit() + def set_shift_approver(department): department_doc = frappe.get_doc("Department", department) diff --git a/erpnext/hr/doctype/shift_type/shift_type.py b/erpnext/hr/doctype/shift_type/shift_type.py index 3f5cb222bf..5e214cf7b7 100644 --- a/erpnext/hr/doctype/shift_type/shift_type.py +++ b/erpnext/hr/doctype/shift_type/shift_type.py @@ -3,21 +3,23 @@ import itertools -from datetime import timedelta +from datetime import datetime, timedelta import frappe from frappe.model.document import Document -from frappe.utils import cint, get_datetime, getdate +from frappe.utils import cint, get_datetime, get_time, getdate +from erpnext.buying.doctype.supplier_scorecard.supplier_scorecard import daterange from erpnext.hr.doctype.attendance.attendance import mark_attendance from erpnext.hr.doctype.employee.employee import get_holiday_list_for_employee from erpnext.hr.doctype.employee_checkin.employee_checkin import ( calculate_working_hours, mark_attendance_and_link_log, ) +from erpnext.hr.doctype.holiday_list.holiday_list import is_holiday from erpnext.hr.doctype.shift_assignment.shift_assignment import ( - get_actual_start_end_datetime_of_shift, get_employee_shift, + get_shift_details, ) @@ -30,8 +32,9 @@ class ShiftType(Document): or not self.last_sync_of_checkin ): return + filters = { - "skip_auto_attendance": "0", + "skip_auto_attendance": 0, "attendance": ("is", "not set"), "time": (">=", self.process_attendance_after), "shift_actual_end": ("<", self.last_sync_of_checkin), @@ -40,6 +43,7 @@ class ShiftType(Document): logs = frappe.db.get_list( "Employee Checkin", fields="*", filters=filters, order_by="employee,time" ) + for key, group in itertools.groupby( logs, key=lambda x: (x["employee"], x["shift_actual_start"]) ): @@ -52,6 +56,7 @@ class ShiftType(Document): in_time, out_time, ) = self.get_attendance(single_shift_logs) + mark_attendance_and_link_log( single_shift_logs, attendance_status, @@ -63,15 +68,16 @@ class ShiftType(Document): out_time, self.name, ) + for employee in self.get_assigned_employee(self.process_attendance_after, True): self.mark_absent_for_dates_with_no_attendance(employee) def get_attendance(self, logs): """Return attendance_status, working_hours, late_entry, early_exit, in_time, out_time for a set of logs belonging to a single shift. - Assumtion: - 1. These logs belongs to an single shift, single employee and is not in a holiday date. - 2. Logs are in chronological order + Assumptions: + 1. These logs belongs to a single shift, single employee and it's not in a holiday date. + 2. Logs are in chronological order """ late_entry = early_exit = False total_working_hours, in_time, out_time = calculate_working_hours( @@ -91,39 +97,68 @@ class ShiftType(Document): ): early_exit = True - if ( - self.working_hours_threshold_for_absent - and total_working_hours < self.working_hours_threshold_for_absent - ): - 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 ): return "Half Day", total_working_hours, late_entry, early_exit, in_time, out_time + if ( + self.working_hours_threshold_for_absent + and total_working_hours < self.working_hours_threshold_for_absent + ): + return "Absent", total_working_hours, late_entry, early_exit, in_time, out_time return "Present", total_working_hours, late_entry, early_exit, in_time, out_time def mark_absent_for_dates_with_no_attendance(self, employee): """Marks Absents for the given employee on working days in this shift which have no attendance marked. The Absent is marked starting from 'process_attendance_after' or employee creation date. """ + start_date, end_date = self.get_start_and_end_dates(employee) + + # no shift assignment found, no need to process absent attendance records + if start_date is None: + return + + holiday_list_name = self.holiday_list + if not holiday_list_name: + holiday_list_name = get_holiday_list_for_employee(employee, False) + + start_time = get_time(self.start_time) + + for date in daterange(getdate(start_date), getdate(end_date)): + if is_holiday(holiday_list_name, date): + # skip marking absent on a holiday + continue + + timestamp = datetime.combine(date, start_time) + shift_details = get_employee_shift(employee, timestamp, True) + + if shift_details and shift_details.shift_type.name == self.name: + mark_attendance(employee, date, "Absent", self.name) + + def get_start_and_end_dates(self, employee): + """Returns start and end dates for checking attendance and marking absent + return: start date = max of `process_attendance_after` and DOJ + return: end date = min of shift before `last_sync_of_checkin` and Relieving Date + """ date_of_joining, relieving_date, employee_creation = frappe.db.get_value( "Employee", employee, ["date_of_joining", "relieving_date", "creation"] ) + if not date_of_joining: date_of_joining = employee_creation.date() + start_date = max(getdate(self.process_attendance_after), date_of_joining) - actual_shift_datetime = get_actual_start_end_datetime_of_shift( - employee, get_datetime(self.last_sync_of_checkin), True - ) + end_date = None + + shift_details = get_shift_details(self.name, get_datetime(self.last_sync_of_checkin)) last_shift_time = ( - actual_shift_datetime[0] - if actual_shift_datetime[0] - else get_datetime(self.last_sync_of_checkin) - ) - prev_shift = get_employee_shift( - employee, last_shift_time.date() - timedelta(days=1), True, "reverse" + shift_details.actual_start if shift_details else get_datetime(self.last_sync_of_checkin) ) + + # check if shift is found for 1 day before the last sync of checkin + # absentees are auto-marked 1 day after the shift to wait for any manual attendance records + prev_shift = get_employee_shift(employee, last_shift_time - timedelta(days=1), True, "reverse") if prev_shift: end_date = ( min(prev_shift.start_datetime.date(), relieving_date) @@ -131,28 +166,21 @@ class ShiftType(Document): else prev_shift.start_datetime.date() ) else: - return - holiday_list_name = self.holiday_list - if not holiday_list_name: - holiday_list_name = get_holiday_list_for_employee(employee, False) - dates = get_filtered_date_list(employee, start_date, end_date, holiday_list=holiday_list_name) - for date in dates: - shift_details = get_employee_shift(employee, date, True) - if shift_details and shift_details.shift_type.name == self.name: - mark_attendance(employee, date, "Absent", self.name) + # no shift found + return None, None + return start_date, end_date def get_assigned_employee(self, from_date=None, consider_default_shift=False): - filters = {"start_date": (">", from_date), "shift_type": self.name, "docstatus": "1"} - if not from_date: - del filters["start_date"] + filters = {"shift_type": self.name, "docstatus": "1"} + if from_date: + filters["start_date"] = (">", from_date) - assigned_employees = frappe.get_all("Shift Assignment", "employee", filters, as_list=True) - assigned_employees = [x[0] for x in assigned_employees] + assigned_employees = frappe.get_all("Shift Assignment", filters=filters, pluck="employee") if consider_default_shift: filters = {"default_shift": self.name, "status": ["!=", "Inactive"]} - default_shift_employees = frappe.get_all("Employee", "name", filters, as_list=True) - default_shift_employees = [x[0] for x in default_shift_employees] + default_shift_employees = frappe.get_all("Employee", filters=filters, pluck="name") + return list(set(assigned_employees + default_shift_employees)) return assigned_employees @@ -162,42 +190,3 @@ def process_auto_attendance_for_all_shifts(): for shift in shift_list: doc = frappe.get_doc("Shift Type", shift[0]) doc.process_auto_attendance() - - -def get_filtered_date_list( - employee, start_date, end_date, filter_attendance=True, holiday_list=None -): - """Returns a list of dates after removing the dates with attendance and holidays""" - base_dates_query = """select adddate(%(start_date)s, t2.i*100 + t1.i*10 + t0.i) selected_date from - (select 0 i union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t0, - (select 0 i union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t1, - (select 0 i union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t2""" - condition_query = "" - if filter_attendance: - condition_query += """ and a.selected_date not in ( - select attendance_date from `tabAttendance` - where docstatus = 1 and employee = %(employee)s - and attendance_date between %(start_date)s and %(end_date)s)""" - if holiday_list: - condition_query += """ and a.selected_date not in ( - select holiday_date from `tabHoliday` where parenttype = 'Holiday List' and - parentfield = 'holidays' and parent = %(holiday_list)s - and holiday_date between %(start_date)s and %(end_date)s)""" - - dates = frappe.db.sql( - """select * from - ({base_dates_query}) as a - where a.selected_date <= %(end_date)s {condition_query} - """.format( - base_dates_query=base_dates_query, condition_query=condition_query - ), - { - "employee": employee, - "start_date": start_date, - "end_date": end_date, - "holiday_list": holiday_list, - }, - as_list=True, - ) - - return [getdate(date[0]) for date in dates] diff --git a/erpnext/hr/doctype/shift_type/test_shift_type.py b/erpnext/hr/doctype/shift_type/test_shift_type.py index 7d2f29cd6c..0d75292a1e 100644 --- a/erpnext/hr/doctype/shift_type/test_shift_type.py +++ b/erpnext/hr/doctype/shift_type/test_shift_type.py @@ -2,7 +2,381 @@ # See license.txt import unittest +from datetime import datetime, timedelta + +import frappe +from frappe.tests.utils import FrappeTestCase +from frappe.utils import add_days, get_time, get_year_ending, get_year_start, getdate, now_datetime + +from erpnext.hr.doctype.employee.test_employee import make_employee +from erpnext.hr.doctype.holiday_list.test_holiday_list import set_holiday_list +from erpnext.hr.doctype.leave_application.test_leave_application import get_first_sunday +from erpnext.payroll.doctype.salary_slip.test_salary_slip import make_holiday_list -class TestShiftType(unittest.TestCase): - pass +class TestShiftType(FrappeTestCase): + def setUp(self): + frappe.db.delete("Shift Type") + frappe.db.delete("Shift Assignment") + frappe.db.delete("Employee Checkin") + frappe.db.delete("Attendance") + + from_date = get_year_start(getdate()) + to_date = get_year_ending(getdate()) + self.holiday_list = make_holiday_list(from_date=from_date, to_date=to_date) + + def test_mark_attendance(self): + from erpnext.hr.doctype.employee_checkin.test_employee_checkin import make_checkin + + employee = make_employee("test_employee_checkin@example.com", company="_Test Company") + + shift_type = setup_shift_type() + date = getdate() + make_shift_assignment(shift_type.name, employee, date) + + timestamp = datetime.combine(date, get_time("08:00:00")) + log_in = make_checkin(employee, timestamp) + self.assertEqual(log_in.shift, shift_type.name) + + timestamp = datetime.combine(date, get_time("12:00:00")) + log_out = make_checkin(employee, timestamp) + self.assertEqual(log_out.shift, shift_type.name) + + shift_type.process_auto_attendance() + + attendance = frappe.db.get_value( + "Attendance", {"shift": shift_type.name}, ["status", "name"], as_dict=True + ) + self.assertEqual(attendance.status, "Present") + + def test_entry_and_exit_grace(self): + from erpnext.hr.doctype.employee_checkin.test_employee_checkin import make_checkin + + employee = make_employee("test_employee_checkin@example.com", company="_Test Company") + + # doesn't mark late entry until 60 mins after shift start i.e. till 9 + # doesn't mark late entry until 60 mins before shift end i.e. 11 + shift_type = setup_shift_type( + enable_entry_grace_period=1, + enable_exit_grace_period=1, + late_entry_grace_period=60, + early_exit_grace_period=60, + ) + date = getdate() + make_shift_assignment(shift_type.name, employee, date) + + timestamp = datetime.combine(date, get_time("09:30:00")) + log_in = make_checkin(employee, timestamp) + self.assertEqual(log_in.shift, shift_type.name) + + timestamp = datetime.combine(date, get_time("10:30:00")) + log_out = make_checkin(employee, timestamp) + self.assertEqual(log_out.shift, shift_type.name) + + shift_type.process_auto_attendance() + + attendance = frappe.db.get_value( + "Attendance", + {"shift": shift_type.name}, + ["status", "name", "late_entry", "early_exit"], + as_dict=True, + ) + self.assertEqual(attendance.status, "Present") + self.assertEqual(attendance.late_entry, 1) + self.assertEqual(attendance.early_exit, 1) + + def test_working_hours_threshold_for_half_day(self): + from erpnext.hr.doctype.employee_checkin.test_employee_checkin import make_checkin + + employee = make_employee("test_employee_checkin@example.com", company="_Test Company") + shift_type = setup_shift_type(shift_type="Half Day Test", working_hours_threshold_for_half_day=2) + date = getdate() + make_shift_assignment(shift_type.name, employee, date) + + timestamp = datetime.combine(date, get_time("08:00:00")) + log_in = make_checkin(employee, timestamp) + self.assertEqual(log_in.shift, shift_type.name) + + timestamp = datetime.combine(date, get_time("09:30:00")) + log_out = make_checkin(employee, timestamp) + self.assertEqual(log_out.shift, shift_type.name) + + shift_type.process_auto_attendance() + + attendance = frappe.db.get_value( + "Attendance", {"shift": shift_type.name}, ["status", "working_hours"], as_dict=True + ) + self.assertEqual(attendance.status, "Half Day") + self.assertEqual(attendance.working_hours, 1.5) + + def test_working_hours_threshold_for_absent(self): + from erpnext.hr.doctype.employee_checkin.test_employee_checkin import make_checkin + + employee = make_employee("test_employee_checkin@example.com", company="_Test Company") + shift_type = setup_shift_type(shift_type="Absent Test", working_hours_threshold_for_absent=2) + date = getdate() + make_shift_assignment(shift_type.name, employee, date) + + timestamp = datetime.combine(date, get_time("08:00:00")) + log_in = make_checkin(employee, timestamp) + self.assertEqual(log_in.shift, shift_type.name) + + timestamp = datetime.combine(date, get_time("09:30:00")) + log_out = make_checkin(employee, timestamp) + self.assertEqual(log_out.shift, shift_type.name) + + shift_type.process_auto_attendance() + + attendance = frappe.db.get_value( + "Attendance", {"shift": shift_type.name}, ["status", "working_hours"], as_dict=True + ) + self.assertEqual(attendance.status, "Absent") + self.assertEqual(attendance.working_hours, 1.5) + + def test_working_hours_threshold_for_absent_and_half_day_1(self): + # considers half day over absent + from erpnext.hr.doctype.employee_checkin.test_employee_checkin import make_checkin + + employee = make_employee("test_employee_checkin@example.com", company="_Test Company") + shift_type = setup_shift_type( + shift_type="Half Day + Absent Test", + working_hours_threshold_for_half_day=1, + working_hours_threshold_for_absent=2, + ) + date = getdate() + make_shift_assignment(shift_type.name, employee, date) + + timestamp = datetime.combine(date, get_time("08:00:00")) + log_in = make_checkin(employee, timestamp) + self.assertEqual(log_in.shift, shift_type.name) + + timestamp = datetime.combine(date, get_time("08:45:00")) + log_out = make_checkin(employee, timestamp) + self.assertEqual(log_out.shift, shift_type.name) + + shift_type.process_auto_attendance() + + attendance = frappe.db.get_value( + "Attendance", {"shift": shift_type.name}, ["status", "working_hours"], as_dict=True + ) + self.assertEqual(attendance.status, "Half Day") + self.assertEqual(attendance.working_hours, 0.75) + + def test_working_hours_threshold_for_absent_and_half_day_2(self): + # considers absent over half day + from erpnext.hr.doctype.employee_checkin.test_employee_checkin import make_checkin + + employee = make_employee("test_employee_checkin@example.com", company="_Test Company") + shift_type = setup_shift_type( + shift_type="Half Day + Absent Test", + working_hours_threshold_for_half_day=1, + working_hours_threshold_for_absent=2, + ) + date = getdate() + make_shift_assignment(shift_type.name, employee, date) + + timestamp = datetime.combine(date, get_time("08:00:00")) + log_in = make_checkin(employee, timestamp) + self.assertEqual(log_in.shift, shift_type.name) + + timestamp = datetime.combine(date, get_time("09:30:00")) + log_out = make_checkin(employee, timestamp) + self.assertEqual(log_out.shift, shift_type.name) + + shift_type.process_auto_attendance() + + attendance = frappe.db.get_value("Attendance", {"shift": shift_type.name}, "status") + self.assertEqual(attendance, "Absent") + + def test_mark_absent_for_dates_with_no_attendance(self): + employee = make_employee("test_employee_checkin@example.com", company="_Test Company") + shift_type = setup_shift_type(shift_type="Test Absent with no Attendance") + + # absentees are auto-marked one day after to wait for any manual attendance records + date = add_days(getdate(), -1) + make_shift_assignment(shift_type.name, employee, date) + + shift_type.process_auto_attendance() + + attendance = frappe.db.get_value( + "Attendance", {"attendance_date": date, "employee": employee}, "status" + ) + self.assertEqual(attendance, "Absent") + + @set_holiday_list("Salary Slip Test Holiday List", "_Test Company") + def test_skip_marking_absent_on_a_holiday(self): + employee = make_employee("test_employee_checkin@example.com", company="_Test Company") + shift_type = setup_shift_type(shift_type="Test Absent with no Attendance") + shift_type.holiday_list = None + shift_type.save() + + # should not mark any attendance if no shift assignment is created + shift_type.process_auto_attendance() + attendance = frappe.db.get_value("Attendance", {"employee": employee}, "status") + self.assertIsNone(attendance) + + first_sunday = get_first_sunday(self.holiday_list, for_date=getdate()) + make_shift_assignment(shift_type.name, employee, first_sunday) + + shift_type.process_auto_attendance() + + attendance = frappe.db.get_value( + "Attendance", {"attendance_date": first_sunday, "employee": employee}, "status" + ) + self.assertIsNone(attendance) + + def test_get_start_and_end_dates(self): + date = getdate() + + doj = add_days(date, -30) + relieving_date = add_days(date, -5) + employee = make_employee( + "test_employee_dates@example.com", + company="_Test Company", + date_of_joining=doj, + relieving_date=relieving_date, + ) + shift_type = setup_shift_type( + shift_type="Test Absent with no Attendance", process_attendance_after=add_days(doj, 2) + ) + + make_shift_assignment(shift_type.name, employee, add_days(date, -25)) + + shift_type.process_auto_attendance() + + # should not mark absent before shift assignment/process attendance after date + attendance = frappe.db.get_value( + "Attendance", {"attendance_date": doj, "employee": employee}, "name" + ) + self.assertIsNone(attendance) + + # mark absent on Relieving Date + attendance = frappe.db.get_value( + "Attendance", {"attendance_date": relieving_date, "employee": employee}, "status" + ) + self.assertEquals(attendance, "Absent") + + # should not mark absent after Relieving Date + attendance = frappe.db.get_value( + "Attendance", {"attendance_date": add_days(relieving_date, 1), "employee": employee}, "name" + ) + self.assertIsNone(attendance) + + def test_skip_auto_attendance_for_duplicate_record(self): + # Skip auto attendance in case of duplicate attendance record + from erpnext.hr.doctype.attendance.attendance import mark_attendance + from erpnext.hr.doctype.employee_checkin.test_employee_checkin import make_checkin + + employee = make_employee("test_employee_checkin@example.com", company="_Test Company") + + shift_type = setup_shift_type() + date = getdate() + + # mark attendance + mark_attendance(employee, date, "Present") + make_shift_assignment(shift_type.name, employee, date) + + timestamp = datetime.combine(date, get_time("08:00:00")) + log_in = make_checkin(employee, timestamp) + self.assertEqual(log_in.shift, shift_type.name) + + timestamp = datetime.combine(date, get_time("12:00:00")) + log_out = make_checkin(employee, timestamp) + self.assertEqual(log_out.shift, shift_type.name) + + # auto attendance should skip marking + shift_type.process_auto_attendance() + + log_in.reload() + log_out.reload() + self.assertEqual(log_in.skip_auto_attendance, 1) + self.assertEqual(log_out.skip_auto_attendance, 1) + + def test_skip_auto_attendance_for_overlapping_shift(self): + # Skip auto attendance in case of overlapping shift attendance record + # this case won't occur in case of shift assignment, since it will not allow overlapping shifts to be assigned + # can happen if manual attendance records are created + from erpnext.hr.doctype.attendance.attendance import mark_attendance + from erpnext.hr.doctype.employee_checkin.test_employee_checkin import make_checkin + + employee = make_employee("test_employee_checkin@example.com", company="_Test Company") + shift_1 = setup_shift_type(shift_type="Shift 1", start_time="08:00:00", end_time="10:00:00") + shift_2 = setup_shift_type(shift_type="Shift 2", start_time="09:30:00", end_time="11:00:00") + + date = getdate() + + # mark attendance + mark_attendance(employee, date, "Present", shift=shift_1.name) + make_shift_assignment(shift_2.name, employee, date) + + timestamp = datetime.combine(date, get_time("09:30:00")) + log_in = make_checkin(employee, timestamp) + self.assertEqual(log_in.shift, shift_2.name) + + timestamp = datetime.combine(date, get_time("11:00:00")) + log_out = make_checkin(employee, timestamp) + self.assertEqual(log_out.shift, shift_2.name) + + # auto attendance should be skipped for shift 2 + # since it is already marked for overlapping shift 1 + shift_2.process_auto_attendance() + + log_in.reload() + log_out.reload() + self.assertEqual(log_in.skip_auto_attendance, 1) + self.assertEqual(log_out.skip_auto_attendance, 1) + + +def setup_shift_type(**args): + args = frappe._dict(args) + date = getdate() + + shift_type = frappe.get_doc( + { + "doctype": "Shift Type", + "__newname": args.shift_type or "_Test Shift", + "start_time": "08:00:00", + "end_time": "12:00:00", + "enable_auto_attendance": 1, + "determine_check_in_and_check_out": "Alternating entries as IN and OUT during the same shift", + "working_hours_calculation_based_on": "First Check-in and Last Check-out", + "begin_check_in_before_shift_start_time": 60, + "allow_check_out_after_shift_end_time": 60, + "process_attendance_after": add_days(date, -2), + "last_sync_of_checkin": now_datetime() + timedelta(days=1), + } + ) + + holiday_list = "Employee Checkin Test Holiday List" + if not frappe.db.exists("Holiday List", "Employee Checkin Test Holiday List"): + holiday_list = frappe.get_doc( + { + "doctype": "Holiday List", + "holiday_list_name": "Employee Checkin Test Holiday List", + "from_date": get_year_start(date), + "to_date": get_year_ending(date), + } + ).insert() + holiday_list = holiday_list.name + + shift_type.holiday_list = holiday_list + shift_type.update(args) + shift_type.save() + + return shift_type + + +def make_shift_assignment(shift_type, employee, start_date, end_date=None): + shift_assignment = frappe.get_doc( + { + "doctype": "Shift Assignment", + "shift_type": shift_type, + "company": "_Test Company", + "employee": employee, + "start_date": start_date, + "end_date": end_date, + } + ).insert() + shift_assignment.submit() + + return shift_assignment diff --git a/erpnext/hr/report/monthly_attendance_sheet/monthly_attendance_sheet.js b/erpnext/hr/report/monthly_attendance_sheet/monthly_attendance_sheet.js index 42f7cdb50f..6f4bbd54fb 100644 --- a/erpnext/hr/report/monthly_attendance_sheet/monthly_attendance_sheet.js +++ b/erpnext/hr/report/monthly_attendance_sheet/monthly_attendance_sheet.js @@ -66,8 +66,7 @@ frappe.query_reports["Monthly Attendance Sheet"] = { "Default": 0, } ], - - "onload": function() { + onload: function() { return frappe.call({ method: "erpnext.hr.report.monthly_attendance_sheet.monthly_attendance_sheet.get_attendance_years", callback: function(r) { @@ -78,5 +77,25 @@ frappe.query_reports["Monthly Attendance Sheet"] = { year_filter.set_input(year_filter.df.default); } }); + }, + formatter: function(value, row, column, data, default_formatter) { + value = default_formatter(value, row, column, data); + const summarized_view = frappe.query_report.get_filter_value('summarized_view'); + const group_by = frappe.query_report.get_filter_value('group_by'); + + if (!summarized_view) { + if ((group_by && column.colIndex > 3) || (!group_by && column.colIndex > 2)) { + if (value == 'P' || value == 'WFH') + value = "" + value + ""; + else if (value == 'A') + value = "" + value + ""; + else if (value == 'HD') + value = "" + value + ""; + else if (value == 'L') + value = "" + value + ""; + } + } + + return value; } } diff --git a/erpnext/hr/report/monthly_attendance_sheet/monthly_attendance_sheet.py b/erpnext/hr/report/monthly_attendance_sheet/monthly_attendance_sheet.py index 8ea49899f2..efd2d382d5 100644 --- a/erpnext/hr/report/monthly_attendance_sheet/monthly_attendance_sheet.py +++ b/erpnext/hr/report/monthly_attendance_sheet/monthly_attendance_sheet.py @@ -3,365 +3,618 @@ from calendar import monthrange +from itertools import groupby +from typing import Dict, List, Optional, Tuple import frappe -from frappe import _, msgprint +from frappe import _ +from frappe.query_builder.functions import Count, Extract, Sum from frappe.utils import cint, cstr, getdate +Filters = frappe._dict + status_map = { + "Present": "P", "Absent": "A", "Half Day": "HD", - "Holiday": "H", - "Weekly Off": "WO", - "On Leave": "L", - "Present": "P", "Work From Home": "WFH", + "On Leave": "L", + "Holiday": "H", + "Weekly Off": "WO", } day_abbr = ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"] -def execute(filters=None): - if not filters: - filters = {} +def execute(filters: Optional[Filters] = None) -> Tuple: + filters = frappe._dict(filters or {}) - if filters.hide_year_field == 1: - filters.year = 2020 + if not (filters.month and filters.year): + frappe.throw(_("Please select month and year.")) - conditions, filters = get_conditions(filters) - columns, days = get_columns(filters) - att_map = get_attendance_list(conditions, filters) - if not att_map: + attendance_map = get_attendance_map(filters) + if not attendance_map: + frappe.msgprint(_("No attendance records found."), alert=True, indicator="orange") + return [], [], None, None + + columns = get_columns(filters) + data = get_data(filters, attendance_map) + + if not data: + frappe.msgprint( + _("No attendance records found for this criteria."), alert=True, indicator="orange" + ) return columns, [], None, None - if filters.group_by: - emp_map, group_by_parameters = get_employee_details(filters.group_by, filters.company) - holiday_list = [] - for parameter in group_by_parameters: - h_list = [ - emp_map[parameter][d]["holiday_list"] - for d in emp_map[parameter] - if emp_map[parameter][d]["holiday_list"] - ] - holiday_list += h_list - else: - emp_map = get_employee_details(filters.group_by, filters.company) - holiday_list = [emp_map[d]["holiday_list"] for d in emp_map if emp_map[d]["holiday_list"]] + message = get_message() if not filters.summarized_view else "" + chart = get_chart_data(attendance_map, filters) - default_holiday_list = frappe.get_cached_value( - "Company", filters.get("company"), "default_holiday_list" - ) - holiday_list.append(default_holiday_list) - holiday_list = list(set(holiday_list)) - holiday_map = get_holiday(holiday_list, filters["month"]) - - data = [] - - leave_types = frappe.db.get_list("Leave Type") - leave_list = None - if filters.summarized_view: - leave_list = [d.name + ":Float:120" for d in leave_types] - columns.extend(leave_list) - columns.extend([_("Total Late Entries") + ":Float:120", _("Total Early Exits") + ":Float:120"]) - - if filters.group_by: - emp_att_map = {} - for parameter in group_by_parameters: - emp_map_set = set([key for key in emp_map[parameter].keys()]) - att_map_set = set([key for key in att_map.keys()]) - if att_map_set & emp_map_set: - parameter_row = ["" + parameter + ""] + [ - "" for day in range(filters["total_days_in_month"] + 2) - ] - data.append(parameter_row) - record, emp_att_data = add_data( - emp_map[parameter], - att_map, - filters, - holiday_map, - conditions, - default_holiday_list, - leave_types=leave_types, - ) - emp_att_map.update(emp_att_data) - data += record - else: - record, emp_att_map = add_data( - emp_map, - att_map, - filters, - holiday_map, - conditions, - default_holiday_list, - leave_types=leave_types, - ) - data += record - - chart_data = get_chart_data(emp_att_map, days) - - return columns, data, None, chart_data + return columns, data, message, chart -def get_chart_data(emp_att_map, days): - labels = [] - datasets = [ - {"name": "Absent", "values": []}, - {"name": "Present", "values": []}, - {"name": "Leave", "values": []}, - ] - for idx, day in enumerate(days, start=0): - p = day.replace("::65", "") - labels.append(day.replace("::65", "")) - total_absent_on_day = 0 - total_leave_on_day = 0 - total_present_on_day = 0 - total_holiday = 0 - for emp in emp_att_map.keys(): - if emp_att_map[emp][idx]: - if emp_att_map[emp][idx] == "A": - total_absent_on_day += 1 - if emp_att_map[emp][idx] in ["P", "WFH"]: - total_present_on_day += 1 - if emp_att_map[emp][idx] == "HD": - total_present_on_day += 0.5 - total_leave_on_day += 0.5 - if emp_att_map[emp][idx] == "L": - total_leave_on_day += 1 +def get_message() -> str: + message = "" + colors = ["green", "red", "orange", "green", "#318AD8", "", ""] - datasets[0]["values"].append(total_absent_on_day) - datasets[1]["values"].append(total_present_on_day) - datasets[2]["values"].append(total_leave_on_day) + count = 0 + for status, abbr in status_map.items(): + message += f""" + + {status} - {abbr} + + """ + count += 1 - chart = {"data": {"labels": labels, "datasets": datasets}} - - chart["type"] = "line" - - return chart + return message -def add_data( - employee_map, att_map, filters, holiday_map, conditions, default_holiday_list, leave_types=None -): - - record = [] - emp_att_map = {} - for emp in employee_map: - emp_det = employee_map.get(emp) - if not emp_det or emp not in att_map: - continue - - row = [] - if filters.group_by: - row += [" "] - row += [emp, emp_det.employee_name] - - total_p = total_a = total_l = total_h = total_um = 0.0 - emp_status_map = [] - for day in range(filters["total_days_in_month"]): - status = None - status = att_map.get(emp).get(day + 1) - - if status is None and holiday_map: - emp_holiday_list = emp_det.holiday_list if emp_det.holiday_list else default_holiday_list - - if emp_holiday_list in holiday_map: - for idx, ele in enumerate(holiday_map[emp_holiday_list]): - if day + 1 == holiday_map[emp_holiday_list][idx][0]: - if holiday_map[emp_holiday_list][idx][1]: - status = "Weekly Off" - else: - status = "Holiday" - total_h += 1 - - abbr = status_map.get(status, "") - emp_status_map.append(abbr) - - if filters.summarized_view: - if status == "Present" or status == "Work From Home": - total_p += 1 - elif status == "Absent": - total_a += 1 - elif status == "On Leave": - total_l += 1 - elif status == "Half Day": - total_p += 0.5 - total_a += 0.5 - total_l += 0.5 - elif not status: - total_um += 1 - - if not filters.summarized_view: - row += emp_status_map - - if filters.summarized_view: - row += [total_p, total_l, total_a, total_h, total_um] - - if not filters.get("employee"): - filters.update({"employee": emp}) - conditions += " and employee = %(employee)s" - elif not filters.get("employee") == emp: - filters.update({"employee": emp}) - - if filters.summarized_view: - leave_details = frappe.db.sql( - """select leave_type, status, count(*) as count from `tabAttendance`\ - where leave_type is not NULL %s group by leave_type, status""" - % conditions, - filters, - as_dict=1, - ) - - time_default_counts = frappe.db.sql( - """select (select count(*) from `tabAttendance` where \ - late_entry = 1 %s) as late_entry_count, (select count(*) from tabAttendance where \ - early_exit = 1 %s) as early_exit_count""" - % (conditions, conditions), - filters, - ) - - leaves = {} - for d in leave_details: - if d.status == "Half Day": - d.count = d.count * 0.5 - if d.leave_type in leaves: - leaves[d.leave_type] += d.count - else: - leaves[d.leave_type] = d.count - - for d in leave_types: - if d.name in leaves: - row.append(leaves[d.name]) - else: - row.append("0.0") - - row.extend([time_default_counts[0][0], time_default_counts[0][1]]) - emp_att_map[emp] = emp_status_map - record.append(row) - - return record, emp_att_map - - -def get_columns(filters): - +def get_columns(filters: Filters) -> List[Dict]: columns = [] if filters.group_by: - columns = [_(filters.group_by) + ":Link/Branch:120"] + columns.append( + { + "label": _(filters.group_by), + "fieldname": frappe.scrub(filters.group_by), + "fieldtype": "Link", + "options": "Branch", + "width": 120, + } + ) - columns += [_("Employee") + ":Link/Employee:120", _("Employee Name") + ":Data/:120"] - days = [] - for day in range(filters["total_days_in_month"]): - date = str(filters.year) + "-" + str(filters.month) + "-" + str(day + 1) - day_name = day_abbr[getdate(date).weekday()] - days.append(cstr(day + 1) + " " + day_name + "::65") - if not filters.summarized_view: - columns += days - - if filters.summarized_view: - columns += [ - _("Total Present") + ":Float:120", - _("Total Leaves") + ":Float:120", - _("Total Absent") + ":Float:120", - _("Total Holidays") + ":Float:120", - _("Unmarked Days") + ":Float:120", + columns.extend( + [ + { + "label": _("Employee"), + "fieldname": "employee", + "fieldtype": "Link", + "options": "Employee", + "width": 135, + }, + {"label": _("Employee Name"), "fieldname": "employee_name", "fieldtype": "Data", "width": 120}, ] - return columns, days - - -def get_attendance_list(conditions, filters): - attendance_list = frappe.db.sql( - """select employee, day(attendance_date) as day_of_month, - status from tabAttendance where docstatus = 1 %s order by employee, attendance_date""" - % conditions, - filters, - as_dict=1, ) - if not attendance_list: - msgprint(_("No attendance record found"), alert=True, indicator="orange") + if filters.summarized_view: + columns.extend( + [ + { + "label": _("Total Present"), + "fieldname": "total_present", + "fieldtype": "Float", + "width": 110, + }, + {"label": _("Total Leaves"), "fieldname": "total_leaves", "fieldtype": "Float", "width": 110}, + {"label": _("Total Absent"), "fieldname": "total_absent", "fieldtype": "Float", "width": 110}, + { + "label": _("Total Holidays"), + "fieldname": "total_holidays", + "fieldtype": "Float", + "width": 120, + }, + { + "label": _("Unmarked Days"), + "fieldname": "unmarked_days", + "fieldtype": "Float", + "width": 130, + }, + ] + ) + columns.extend(get_columns_for_leave_types()) + columns.extend( + [ + { + "label": _("Total Late Entries"), + "fieldname": "total_late_entries", + "fieldtype": "Float", + "width": 140, + }, + { + "label": _("Total Early Exits"), + "fieldname": "total_early_exits", + "fieldtype": "Float", + "width": 140, + }, + ] + ) + else: + columns.append({"label": _("Shift"), "fieldname": "shift", "fieldtype": "Data", "width": 120}) + columns.extend(get_columns_for_days(filters)) + + return columns + + +def get_columns_for_leave_types() -> List[Dict]: + leave_types = frappe.db.get_all("Leave Type", pluck="name") + types = [] + for entry in leave_types: + types.append( + {"label": entry, "fieldname": frappe.scrub(entry), "fieldtype": "Float", "width": 120} + ) + + return types + + +def get_columns_for_days(filters: Filters) -> List[Dict]: + total_days = get_total_days_in_month(filters) + days = [] + + for day in range(1, total_days + 1): + # forms the dates from selected year and month from filters + date = "{}-{}-{}".format(cstr(filters.year), cstr(filters.month), cstr(day)) + # gets abbr from weekday number + weekday = day_abbr[getdate(date).weekday()] + # sets days as 1 Mon, 2 Tue, 3 Wed + label = "{} {}".format(cstr(day), weekday) + days.append({"label": label, "fieldtype": "Data", "fieldname": day, "width": 65}) + + return days + + +def get_total_days_in_month(filters: Filters) -> int: + return monthrange(cint(filters.year), cint(filters.month))[1] + + +def get_data(filters: Filters, attendance_map: Dict) -> List[Dict]: + employee_details, group_by_param_values = get_employee_related_details( + filters.group_by, filters.company + ) + holiday_map = get_holiday_map(filters) + data = [] + + if filters.group_by: + group_by_column = frappe.scrub(filters.group_by) + + for value in group_by_param_values: + if not value: + continue + + records = get_rows(employee_details[value], filters, holiday_map, attendance_map) + + if records: + data.append({group_by_column: frappe.bold(value)}) + data.extend(records) + else: + data = get_rows(employee_details, filters, holiday_map, attendance_map) + + return data + + +def get_attendance_map(filters: Filters) -> Dict: + """Returns a dictionary of employee wise attendance map as per shifts for all the days of the month like + { + 'employee1': { + 'Morning Shift': {1: 'Present', 2: 'Absent', ...} + 'Evening Shift': {1: 'Absent', 2: 'Present', ...} + }, + 'employee2': { + 'Afternoon Shift': {1: 'Present', 2: 'Absent', ...} + 'Night Shift': {1: 'Absent', 2: 'Absent', ...} + } + } + """ + Attendance = frappe.qb.DocType("Attendance") + query = ( + frappe.qb.from_(Attendance) + .select( + Attendance.employee, + Extract("day", Attendance.attendance_date).as_("day_of_month"), + Attendance.status, + Attendance.shift, + ) + .where( + (Attendance.docstatus == 1) + & (Attendance.company == filters.company) + & (Extract("month", Attendance.attendance_date) == filters.month) + & (Extract("year", Attendance.attendance_date) == filters.year) + ) + ) + if filters.employee: + query = query.where(Attendance.employee == filters.employee) + query = query.orderby(Attendance.employee, Attendance.attendance_date) + + attendance_list = query.run(as_dict=1) + attendance_map = {} - att_map = {} for d in attendance_list: - att_map.setdefault(d.employee, frappe._dict()).setdefault(d.day_of_month, "") - att_map[d.employee][d.day_of_month] = d.status + attendance_map.setdefault(d.employee, frappe._dict()).setdefault(d.shift, frappe._dict()) + attendance_map[d.employee][d.shift][d.day_of_month] = d.status - return att_map + return attendance_map -def get_conditions(filters): - if not (filters.get("month") and filters.get("year")): - msgprint(_("Please select month and year"), raise_exception=1) - - filters["total_days_in_month"] = monthrange(cint(filters.year), cint(filters.month))[1] - - conditions = " and month(attendance_date) = %(month)s and year(attendance_date) = %(year)s" - - if filters.get("company"): - conditions += " and company = %(company)s" - if filters.get("employee"): - conditions += " and employee = %(employee)s" - - return conditions, filters - - -def get_employee_details(group_by, company): - emp_map = {} - query = """select name, employee_name, designation, department, branch, company, - holiday_list from `tabEmployee` where company = %s """ % frappe.db.escape( - company +def get_employee_related_details(group_by: str, company: str) -> Tuple[Dict, List]: + """Returns + 1. nested dict for employee details + 2. list of values for the group by filter + """ + Employee = frappe.qb.DocType("Employee") + query = ( + frappe.qb.from_(Employee) + .select( + Employee.name, + Employee.employee_name, + Employee.designation, + Employee.grade, + Employee.department, + Employee.branch, + Employee.company, + Employee.holiday_list, + ) + .where(Employee.company == company) ) if group_by: group_by = group_by.lower() - query += " order by " + group_by + " ASC" + query = query.orderby(group_by) - employee_details = frappe.db.sql(query, as_dict=1) + employee_details = query.run(as_dict=True) + + group_by_param_values = [] + emp_map = {} - group_by_parameters = [] if group_by: + for parameter, employees in groupby(employee_details, key=lambda d: d[group_by]): + group_by_param_values.append(parameter) + emp_map.setdefault(parameter, frappe._dict()) - group_by_parameters = list( - set(detail.get(group_by, "") for detail in employee_details if detail.get(group_by, "")) - ) - for parameter in group_by_parameters: - emp_map[parameter] = {} - - for d in employee_details: - if group_by and len(group_by_parameters): - if d.get(group_by, None): - - emp_map[d.get(group_by)][d.name] = d - else: - emp_map[d.name] = d - - if not group_by: - return emp_map + for emp in employees: + emp_map[parameter][emp.name] = emp else: - return emp_map, group_by_parameters + for emp in employee_details: + emp_map[emp.name] = emp + + return emp_map, group_by_param_values -def get_holiday(holiday_list, month): +def get_holiday_map(filters: Filters) -> Dict[str, List[Dict]]: + """ + Returns a dict of holidays falling in the filter month and year + with list name as key and list of holidays as values like + { + 'Holiday List 1': [ + {'day_of_month': '0' , 'weekly_off': 1}, + {'day_of_month': '1', 'weekly_off': 0} + ], + 'Holiday List 2': [ + {'day_of_month': '0' , 'weekly_off': 1}, + {'day_of_month': '1', 'weekly_off': 0} + ] + } + """ + # add default holiday list too + holiday_lists = frappe.db.get_all("Holiday List", pluck="name") + default_holiday_list = frappe.get_cached_value("Company", filters.company, "default_holiday_list") + holiday_lists.append(default_holiday_list) + holiday_map = frappe._dict() - for d in holiday_list: - if d: - holiday_map.setdefault( - d, - frappe.db.sql( - """select day(holiday_date), weekly_off from `tabHoliday` - where parent=%s and month(holiday_date)=%s""", - (d, month), - ), + Holiday = frappe.qb.DocType("Holiday") + + for d in holiday_lists: + if not d: + continue + + holidays = ( + frappe.qb.from_(Holiday) + .select(Extract("day", Holiday.holiday_date).as_("day_of_month"), Holiday.weekly_off) + .where( + (Holiday.parent == d) + & (Extract("month", Holiday.holiday_date) == filters.month) + & (Extract("year", Holiday.holiday_date) == filters.year) ) + ).run(as_dict=True) + + holiday_map.setdefault(d, holidays) return holiday_map -@frappe.whitelist() -def get_attendance_years(): - year_list = frappe.db.sql_list( - """select distinct YEAR(attendance_date) from tabAttendance ORDER BY YEAR(attendance_date) DESC""" +def get_rows( + employee_details: Dict, filters: Filters, holiday_map: Dict, attendance_map: Dict +) -> List[Dict]: + records = [] + default_holiday_list = frappe.get_cached_value("Company", filters.company, "default_holiday_list") + + for employee, details in employee_details.items(): + emp_holiday_list = details.holiday_list or default_holiday_list + holidays = holiday_map.get(emp_holiday_list) + + if filters.summarized_view: + attendance = get_attendance_status_for_summarized_view(employee, filters, holidays) + if not attendance: + continue + + leave_summary = get_leave_summary(employee, filters) + entry_exits_summary = get_entry_exits_summary(employee, filters) + + row = {"employee": employee, "employee_name": details.employee_name} + set_defaults_for_summarized_view(filters, row) + row.update(attendance) + row.update(leave_summary) + row.update(entry_exits_summary) + + records.append(row) + else: + employee_attendance = attendance_map.get(employee) + if not employee_attendance: + continue + + attendance_for_employee = get_attendance_status_for_detailed_view( + employee, filters, employee_attendance, holidays + ) + # set employee details in the first row + attendance_for_employee[0].update( + {"employee": employee, "employee_name": details.employee_name} + ) + + records.extend(attendance_for_employee) + + return records + + +def set_defaults_for_summarized_view(filters, row): + for entry in get_columns(filters): + if entry.get("fieldtype") == "Float": + row[entry.get("fieldname")] = 0.0 + + +def get_attendance_status_for_summarized_view( + employee: str, filters: Filters, holidays: List +) -> Dict: + """Returns dict of attendance status for employee like + {'total_present': 1.5, 'total_leaves': 0.5, 'total_absent': 13.5, 'total_holidays': 8, 'unmarked_days': 5} + """ + summary, attendance_days = get_attendance_summary_and_days(employee, filters) + if not any(summary.values()): + return {} + + total_days = get_total_days_in_month(filters) + total_holidays = total_unmarked_days = 0 + + for day in range(1, total_days + 1): + if day in attendance_days: + continue + + status = get_holiday_status(day, holidays) + if status in ["Weekly Off", "Holiday"]: + total_holidays += 1 + elif not status: + total_unmarked_days += 1 + + return { + "total_present": summary.total_present + summary.total_half_days, + "total_leaves": summary.total_leaves + summary.total_half_days, + "total_absent": summary.total_absent + summary.total_half_days, + "total_holidays": total_holidays, + "unmarked_days": total_unmarked_days, + } + + +def get_attendance_summary_and_days(employee: str, filters: Filters) -> Tuple[Dict, List]: + Attendance = frappe.qb.DocType("Attendance") + + present_case = ( + frappe.qb.terms.Case() + .when(((Attendance.status == "Present") | (Attendance.status == "Work From Home")), 1) + .else_(0) ) - if not year_list: + sum_present = Sum(present_case).as_("total_present") + + absent_case = frappe.qb.terms.Case().when(Attendance.status == "Absent", 1).else_(0) + sum_absent = Sum(absent_case).as_("total_absent") + + leave_case = frappe.qb.terms.Case().when(Attendance.status == "On Leave", 1).else_(0) + sum_leave = Sum(leave_case).as_("total_leaves") + + half_day_case = frappe.qb.terms.Case().when(Attendance.status == "Half Day", 0.5).else_(0) + sum_half_day = Sum(half_day_case).as_("total_half_days") + + summary = ( + frappe.qb.from_(Attendance) + .select( + sum_present, + sum_absent, + sum_leave, + sum_half_day, + ) + .where( + (Attendance.docstatus == 1) + & (Attendance.employee == employee) + & (Attendance.company == filters.company) + & (Extract("month", Attendance.attendance_date) == filters.month) + & (Extract("year", Attendance.attendance_date) == filters.year) + ) + ).run(as_dict=True) + + days = ( + frappe.qb.from_(Attendance) + .select(Extract("day", Attendance.attendance_date).as_("day_of_month")) + .distinct() + .where( + (Attendance.docstatus == 1) + & (Attendance.employee == employee) + & (Attendance.company == filters.company) + & (Extract("month", Attendance.attendance_date) == filters.month) + & (Extract("year", Attendance.attendance_date) == filters.year) + ) + ).run(pluck=True) + + return summary[0], days + + +def get_attendance_status_for_detailed_view( + employee: str, filters: Filters, employee_attendance: Dict, holidays: List +) -> List[Dict]: + """Returns list of shift-wise attendance status for employee + [ + {'shift': 'Morning Shift', 1: 'A', 2: 'P', 3: 'A'....}, + {'shift': 'Evening Shift', 1: 'P', 2: 'A', 3: 'P'....} + ] + """ + total_days = get_total_days_in_month(filters) + attendance_values = [] + + for shift, status_dict in employee_attendance.items(): + row = {"shift": shift} + + for day in range(1, total_days + 1): + status = status_dict.get(day) + if status is None and holidays: + status = get_holiday_status(day, holidays) + + abbr = status_map.get(status, "") + row[day] = abbr + + attendance_values.append(row) + + return attendance_values + + +def get_holiday_status(day: int, holidays: List) -> str: + status = None + for holiday in holidays: + if day == holiday.get("day_of_month"): + if holiday.get("weekly_off"): + status = "Weekly Off" + else: + status = "Holiday" + break + return status + + +def get_leave_summary(employee: str, filters: Filters) -> Dict[str, float]: + """Returns a dict of leave type and corresponding leaves taken by employee like: + {'leave_without_pay': 1.0, 'sick_leave': 2.0} + """ + Attendance = frappe.qb.DocType("Attendance") + day_case = frappe.qb.terms.Case().when(Attendance.status == "Half Day", 0.5).else_(1) + sum_leave_days = Sum(day_case).as_("leave_days") + + leave_details = ( + frappe.qb.from_(Attendance) + .select(Attendance.leave_type, sum_leave_days) + .where( + (Attendance.employee == employee) + & (Attendance.docstatus == 1) + & (Attendance.company == filters.company) + & ((Attendance.leave_type.isnotnull()) | (Attendance.leave_type != "")) + & (Extract("month", Attendance.attendance_date) == filters.month) + & (Extract("year", Attendance.attendance_date) == filters.year) + ) + .groupby(Attendance.leave_type) + ).run(as_dict=True) + + leaves = {} + for d in leave_details: + leave_type = frappe.scrub(d.leave_type) + leaves[leave_type] = d.leave_days + + return leaves + + +def get_entry_exits_summary(employee: str, filters: Filters) -> Dict[str, float]: + """Returns total late entries and total early exits for employee like: + {'total_late_entries': 5, 'total_early_exits': 2} + """ + Attendance = frappe.qb.DocType("Attendance") + + late_entry_case = frappe.qb.terms.Case().when(Attendance.late_entry == "1", "1") + count_late_entries = Count(late_entry_case).as_("total_late_entries") + + early_exit_case = frappe.qb.terms.Case().when(Attendance.early_exit == "1", "1") + count_early_exits = Count(early_exit_case).as_("total_early_exits") + + entry_exits = ( + frappe.qb.from_(Attendance) + .select(count_late_entries, count_early_exits) + .where( + (Attendance.docstatus == 1) + & (Attendance.employee == employee) + & (Attendance.company == filters.company) + & (Extract("month", Attendance.attendance_date) == filters.month) + & (Extract("year", Attendance.attendance_date) == filters.year) + ) + ).run(as_dict=True) + + return entry_exits[0] + + +@frappe.whitelist() +def get_attendance_years() -> str: + """Returns all the years for which attendance records exist""" + Attendance = frappe.qb.DocType("Attendance") + year_list = ( + frappe.qb.from_(Attendance) + .select(Extract("year", Attendance.attendance_date).as_("year")) + .distinct() + ).run(as_dict=True) + + if year_list: + year_list.sort(key=lambda d: d.year, reverse=True) + else: year_list = [getdate().year] - return "\n".join(str(year) for year in year_list) + return "\n".join(cstr(entry.year) for entry in year_list) + + +def get_chart_data(attendance_map: Dict, filters: Filters) -> Dict: + days = get_columns_for_days(filters) + labels = [] + absent = [] + present = [] + leave = [] + + for day in days: + labels.append(day["label"]) + total_absent_on_day = total_leaves_on_day = total_present_on_day = 0 + + for employee, attendance_dict in attendance_map.items(): + for shift, attendance in attendance_dict.items(): + attendance_on_day = attendance.get(day["fieldname"]) + + if attendance_on_day == "Absent": + total_absent_on_day += 1 + elif attendance_on_day in ["Present", "Work From Home"]: + total_present_on_day += 1 + elif attendance_on_day == "Half Day": + total_present_on_day += 0.5 + total_leaves_on_day += 0.5 + elif attendance_on_day == "On Leave": + total_leaves_on_day += 1 + + absent.append(total_absent_on_day) + present.append(total_present_on_day) + leave.append(total_leaves_on_day) + + return { + "data": { + "labels": labels, + "datasets": [ + {"name": "Absent", "values": absent}, + {"name": "Present", "values": present}, + {"name": "Leave", "values": leave}, + ], + }, + "type": "line", + "colors": ["red", "green", "blue"], + } diff --git a/erpnext/hr/report/monthly_attendance_sheet/test_monthly_attendance_sheet.py b/erpnext/hr/report/monthly_attendance_sheet/test_monthly_attendance_sheet.py index 91da08eee5..cde7dd3fff 100644 --- a/erpnext/hr/report/monthly_attendance_sheet/test_monthly_attendance_sheet.py +++ b/erpnext/hr/report/monthly_attendance_sheet/test_monthly_attendance_sheet.py @@ -1,18 +1,32 @@ import frappe from dateutil.relativedelta import relativedelta from frappe.tests.utils import FrappeTestCase -from frappe.utils import now_datetime +from frappe.utils import get_year_ending, get_year_start, getdate, now_datetime from erpnext.hr.doctype.attendance.attendance import mark_attendance from erpnext.hr.doctype.employee.test_employee import make_employee +from erpnext.hr.doctype.holiday_list.test_holiday_list import set_holiday_list +from erpnext.hr.doctype.leave_application.test_leave_application import make_allocation_record from erpnext.hr.report.monthly_attendance_sheet.monthly_attendance_sheet import execute +from erpnext.payroll.doctype.salary_slip.test_salary_slip import ( + make_holiday_list, + make_leave_application, +) + +test_dependencies = ["Shift Type"] class TestMonthlyAttendanceSheet(FrappeTestCase): def setUp(self): - self.employee = make_employee("test_employee@example.com") - frappe.db.delete("Attendance", {"employee": self.employee}) + self.employee = make_employee("test_employee@example.com", company="_Test Company") + frappe.db.delete("Attendance") + date = getdate() + from_date = get_year_start(date) + to_date = get_year_ending(date) + make_holiday_list(from_date=from_date, to_date=to_date) + + @set_holiday_list("Salary Slip Test Holiday List", "_Test Company") def test_monthly_attendance_sheet_report(self): now = now_datetime() previous_month = now.month - 1 @@ -33,14 +47,203 @@ class TestMonthlyAttendanceSheet(FrappeTestCase): } ) report = execute(filters=filters) - employees = report[1][0] + + record = report[1][0] datasets = report[3]["data"]["datasets"] absent = datasets[0]["values"] present = datasets[1]["values"] leaves = datasets[2]["values"] - # ensure correct attendance is reflect on the report - self.assertIn(self.employee, employees) + # ensure correct attendance is reflected on the report + self.assertEqual(self.employee, record.get("employee")) self.assertEqual(absent[0], 1) self.assertEqual(present[1], 1) self.assertEqual(leaves[2], 1) + + @set_holiday_list("Salary Slip Test Holiday List", "_Test Company") + def test_monthly_attendance_sheet_with_detailed_view(self): + now = now_datetime() + previous_month = now.month - 1 + previous_month_first = now.replace(day=1).replace(month=previous_month).date() + + company = frappe.db.get_value("Employee", self.employee, "company") + + # attendance with shift + mark_attendance(self.employee, previous_month_first, "Absent", "Day Shift") + mark_attendance( + self.employee, previous_month_first + relativedelta(days=1), "Present", "Day Shift" + ) + + # attendance without shift + mark_attendance(self.employee, previous_month_first + relativedelta(days=2), "On Leave") + mark_attendance(self.employee, previous_month_first + relativedelta(days=3), "Present") + + filters = frappe._dict( + { + "month": previous_month, + "year": now.year, + "company": company, + } + ) + report = execute(filters=filters) + + day_shift_row = report[1][0] + row_without_shift = report[1][1] + + self.assertEqual(day_shift_row["shift"], "Day Shift") + self.assertEqual(day_shift_row[1], "A") # absent on the 1st day of the month + self.assertEqual(day_shift_row[2], "P") # present on the 2nd day + + self.assertEqual(row_without_shift["shift"], None) + self.assertEqual(row_without_shift[3], "L") # on leave on the 3rd day + self.assertEqual(row_without_shift[4], "P") # present on the 4th day + + @set_holiday_list("Salary Slip Test Holiday List", "_Test Company") + def test_monthly_attendance_sheet_with_summarized_view(self): + now = now_datetime() + previous_month = now.month - 1 + previous_month_first = now.replace(day=1).replace(month=previous_month).date() + + company = frappe.db.get_value("Employee", self.employee, "company") + + # attendance with shift + mark_attendance(self.employee, previous_month_first, "Absent", "Day Shift") + mark_attendance( + self.employee, previous_month_first + relativedelta(days=1), "Present", "Day Shift" + ) + mark_attendance( + self.employee, previous_month_first + relativedelta(days=2), "Half Day" + ) # half day + + mark_attendance( + self.employee, previous_month_first + relativedelta(days=3), "Present" + ) # attendance without shift + mark_attendance( + self.employee, previous_month_first + relativedelta(days=4), "Present", late_entry=1 + ) # late entry + mark_attendance( + self.employee, previous_month_first + relativedelta(days=5), "Present", early_exit=1 + ) # early exit + + leave_application = get_leave_application(self.employee) + + filters = frappe._dict( + {"month": previous_month, "year": now.year, "company": company, "summarized_view": 1} + ) + report = execute(filters=filters) + + row = report[1][0] + self.assertEqual(row["employee"], self.employee) + + # 4 present + half day absent 0.5 + self.assertEqual(row["total_present"], 4.5) + # 1 present + half day absent 0.5 + self.assertEqual(row["total_absent"], 1.5) + # leave days + half day leave 0.5 + self.assertEqual(row["total_leaves"], leave_application.total_leave_days + 0.5) + + self.assertEqual(row["_test_leave_type"], leave_application.total_leave_days) + self.assertEqual(row["total_late_entries"], 1) + self.assertEqual(row["total_early_exits"], 1) + + @set_holiday_list("Salary Slip Test Holiday List", "_Test Company") + def test_attendance_with_group_by_filter(self): + now = now_datetime() + previous_month = now.month - 1 + previous_month_first = now.replace(day=1).replace(month=previous_month).date() + + company = frappe.db.get_value("Employee", self.employee, "company") + + # attendance with shift + mark_attendance(self.employee, previous_month_first, "Absent", "Day Shift") + mark_attendance( + self.employee, previous_month_first + relativedelta(days=1), "Present", "Day Shift" + ) + + # attendance without shift + mark_attendance(self.employee, previous_month_first + relativedelta(days=2), "On Leave") + mark_attendance(self.employee, previous_month_first + relativedelta(days=3), "Present") + + filters = frappe._dict( + {"month": previous_month, "year": now.year, "company": company, "group_by": "Department"} + ) + report = execute(filters=filters) + + department = frappe.db.get_value("Employee", self.employee, "department") + department_row = report[1][0] + self.assertIn(department, department_row["department"]) + + day_shift_row = report[1][1] + row_without_shift = report[1][2] + + self.assertEqual(day_shift_row["shift"], "Day Shift") + self.assertEqual(day_shift_row[1], "A") # absent on the 1st day of the month + self.assertEqual(day_shift_row[2], "P") # present on the 2nd day + + self.assertEqual(row_without_shift["shift"], None) + self.assertEqual(row_without_shift[3], "L") # on leave on the 3rd day + self.assertEqual(row_without_shift[4], "P") # present on the 4th day + + def test_attendance_with_employee_filter(self): + now = now_datetime() + previous_month = now.month - 1 + previous_month_first = now.replace(day=1).replace(month=previous_month).date() + + company = frappe.db.get_value("Employee", self.employee, "company") + + # mark different attendance status on first 3 days of previous month + mark_attendance(self.employee, previous_month_first, "Absent") + mark_attendance(self.employee, previous_month_first + relativedelta(days=1), "Present") + mark_attendance(self.employee, previous_month_first + relativedelta(days=2), "On Leave") + + filters = frappe._dict( + {"month": previous_month, "year": now.year, "company": company, "employee": self.employee} + ) + report = execute(filters=filters) + + record = report[1][0] + datasets = report[3]["data"]["datasets"] + absent = datasets[0]["values"] + present = datasets[1]["values"] + leaves = datasets[2]["values"] + + # ensure correct attendance is reflected on the report + self.assertEqual(self.employee, record.get("employee")) + self.assertEqual(absent[0], 1) + self.assertEqual(present[1], 1) + self.assertEqual(leaves[2], 1) + + @set_holiday_list("Salary Slip Test Holiday List", "_Test Company") + def test_validations(self): + # validation error for filters without month and year + self.assertRaises(frappe.ValidationError, execute_report_with_invalid_filters) + + # execute report without attendance record + now = now_datetime() + previous_month = now.month - 1 + + company = frappe.db.get_value("Employee", self.employee, "company") + filters = frappe._dict( + {"month": previous_month, "year": now.year, "company": company, "group_by": "Department"} + ) + report = execute(filters=filters) + self.assertEqual(report, ([], [], None, None)) + + +def get_leave_application(employee): + now = now_datetime() + previous_month = now.month - 1 + + date = getdate() + year_start = getdate(get_year_start(date)) + year_end = getdate(get_year_ending(date)) + make_allocation_record(employee=employee, from_date=year_start, to_date=year_end) + + from_date = now.replace(day=7).replace(month=previous_month).date() + to_date = now.replace(day=8).replace(month=previous_month).date() + return make_leave_application(employee, from_date, to_date, "_Test Leave Type") + + +def execute_report_with_invalid_filters(): + filters = frappe._dict({"company": "_Test Company", "group_by": "Department"}) + execute(filters=filters) From c5850e3923e3c5101413750e8e01a800c6945c1f Mon Sep 17 00:00:00 2001 From: Rucha Mahabal Date: Wed, 6 Apr 2022 11:18:47 +0530 Subject: [PATCH 22/22] Revert "feat: Scheduling Multiple shifts and Auto Attendance" (#30587) --- .git-blame-ignore-revs | 3 - erpnext/hr/doctype/attendance/attendance.py | 169 +--- .../hr/doctype/attendance/test_attendance.py | 109 +-- .../employee_checkin/employee_checkin.py | 30 +- .../employee_checkin/test_employee_checkin.py | 185 +--- .../shift_assignment/shift_assignment.py | 504 ++++------ .../shift_assignment/test_shift_assignment.py | 82 +- .../hr/doctype/shift_request/shift_request.py | 87 +- .../shift_request/test_shift_request.py | 153 +--- erpnext/hr/doctype/shift_type/shift_type.py | 143 +-- .../hr/doctype/shift_type/test_shift_type.py | 378 +------- .../monthly_attendance_sheet.js | 23 +- .../monthly_attendance_sheet.py | 859 ++++++------------ .../test_monthly_attendance_sheet.py | 215 +---- 14 files changed, 657 insertions(+), 2283 deletions(-) diff --git a/.git-blame-ignore-revs b/.git-blame-ignore-revs index 3bc22af96a..e9cb6cf903 100644 --- a/.git-blame-ignore-revs +++ b/.git-blame-ignore-revs @@ -26,6 +26,3 @@ b147b85e6ac19a9220cd1e2958a6ebd99373283a # bulk format python code with black 494bd9ef78313436f0424b918f200dab8fc7c20b - -# bulk format python code with black -baec607ff5905b1c67531096a9cf50ec7ff00a5d \ No newline at end of file diff --git a/erpnext/hr/doctype/attendance/attendance.py b/erpnext/hr/doctype/attendance/attendance.py index e43d40ef56..7f4bd83685 100644 --- a/erpnext/hr/doctype/attendance/attendance.py +++ b/erpnext/hr/doctype/attendance/attendance.py @@ -5,21 +5,11 @@ import frappe from frappe import _ from frappe.model.document import Document -from frappe.query_builder import Criterion -from frappe.utils import cint, cstr, formatdate, get_datetime, get_link_to_form, getdate, nowdate +from frappe.utils import cint, cstr, formatdate, get_datetime, getdate, nowdate -from erpnext.hr.doctype.shift_assignment.shift_assignment import has_overlapping_timings from erpnext.hr.utils import get_holiday_dates_for_employee, validate_active_employee -class DuplicateAttendanceError(frappe.ValidationError): - pass - - -class OverlappingShiftAttendanceError(frappe.ValidationError): - pass - - class Attendance(Document): def validate(self): from erpnext.controllers.status_updater import validate_status @@ -28,7 +18,6 @@ class Attendance(Document): validate_active_employee(self.employee) self.validate_attendance_date() self.validate_duplicate_record() - self.validate_overlapping_shift_attendance() self.validate_employee_status() self.check_leave_record() @@ -46,35 +35,21 @@ class Attendance(Document): frappe.throw(_("Attendance date can not be less than employee's joining date")) def validate_duplicate_record(self): - duplicate = get_duplicate_attendance_record( - self.employee, self.attendance_date, self.shift, self.name + res = frappe.db.sql( + """ + select name from `tabAttendance` + where employee = %s + and attendance_date = %s + and name != %s + and docstatus != 2 + """, + (self.employee, getdate(self.attendance_date), self.name), ) - - if duplicate: + if res: frappe.throw( - _("Attendance for employee {0} is already marked for the date {1}: {2}").format( - frappe.bold(self.employee), - frappe.bold(self.attendance_date), - get_link_to_form("Attendance", duplicate[0].name), - ), - title=_("Duplicate Attendance"), - exc=DuplicateAttendanceError, - ) - - def validate_overlapping_shift_attendance(self): - attendance = get_overlapping_shift_attendance( - self.employee, self.attendance_date, self.shift, self.name - ) - - if attendance: - frappe.throw( - _("Attendance for employee {0} is already marked for an overlapping shift {1}: {2}").format( - frappe.bold(self.employee), - frappe.bold(attendance.shift), - get_link_to_form("Attendance", attendance.name), - ), - title=_("Overlapping Shift Attendance"), - exc=OverlappingShiftAttendanceError, + _("Attendance for employee {0} is already marked for the date {1}").format( + frappe.bold(self.employee), frappe.bold(self.attendance_date) + ) ) def validate_employee_status(self): @@ -128,69 +103,6 @@ class Attendance(Document): frappe.throw(_("Employee {0} is not active or does not exist").format(self.employee)) -def get_duplicate_attendance_record(employee, attendance_date, shift, name=None): - attendance = frappe.qb.DocType("Attendance") - query = ( - frappe.qb.from_(attendance) - .select(attendance.name) - .where((attendance.employee == employee) & (attendance.docstatus < 2)) - ) - - if shift: - query = query.where( - Criterion.any( - [ - Criterion.all( - [ - ((attendance.shift.isnull()) | (attendance.shift == "")), - (attendance.attendance_date == attendance_date), - ] - ), - Criterion.all( - [ - ((attendance.shift.isnotnull()) | (attendance.shift != "")), - (attendance.attendance_date == attendance_date), - (attendance.shift == shift), - ] - ), - ] - ) - ) - else: - query = query.where((attendance.attendance_date == attendance_date)) - - if name: - query = query.where(attendance.name != name) - - return query.run(as_dict=True) - - -def get_overlapping_shift_attendance(employee, attendance_date, shift, name=None): - if not shift: - return {} - - attendance = frappe.qb.DocType("Attendance") - query = ( - frappe.qb.from_(attendance) - .select(attendance.name, attendance.shift) - .where( - (attendance.employee == employee) - & (attendance.docstatus < 2) - & (attendance.attendance_date == attendance_date) - & (attendance.shift != shift) - ) - ) - - if name: - query = query.where(attendance.name != name) - - overlapping_attendance = query.run(as_dict=True) - - if overlapping_attendance and has_overlapping_timings(shift, overlapping_attendance[0].shift): - return overlapping_attendance[0] - return {} - - @frappe.whitelist() def get_events(start, end, filters=None): events = [] @@ -229,39 +141,28 @@ def add_attendance(events, start, end, conditions=None): def mark_attendance( - employee, - attendance_date, - status, - shift=None, - leave_type=None, - ignore_validate=False, - late_entry=False, - early_exit=False, + employee, attendance_date, status, shift=None, leave_type=None, ignore_validate=False ): - if get_duplicate_attendance_record(employee, attendance_date, shift): - return - - if get_overlapping_shift_attendance(employee, attendance_date, shift): - return - - company = frappe.db.get_value("Employee", employee, "company") - attendance = frappe.get_doc( - { - "doctype": "Attendance", - "employee": employee, - "attendance_date": attendance_date, - "status": status, - "company": company, - "shift": shift, - "leave_type": leave_type, - "late_entry": late_entry, - "early_exit": early_exit, - } - ) - attendance.flags.ignore_validate = ignore_validate - attendance.insert() - attendance.submit() - return attendance.name + if not frappe.db.exists( + "Attendance", + {"employee": employee, "attendance_date": attendance_date, "docstatus": ("!=", "2")}, + ): + company = frappe.db.get_value("Employee", employee, "company") + attendance = frappe.get_doc( + { + "doctype": "Attendance", + "employee": employee, + "attendance_date": attendance_date, + "status": status, + "company": company, + "shift": shift, + "leave_type": leave_type, + } + ) + attendance.flags.ignore_validate = ignore_validate + attendance.insert() + attendance.submit() + return attendance.name @frappe.whitelist() diff --git a/erpnext/hr/doctype/attendance/test_attendance.py b/erpnext/hr/doctype/attendance/test_attendance.py index 762d0f7567..058bc93d72 100644 --- a/erpnext/hr/doctype/attendance/test_attendance.py +++ b/erpnext/hr/doctype/attendance/test_attendance.py @@ -6,8 +6,6 @@ from frappe.tests.utils import FrappeTestCase from frappe.utils import add_days, get_year_ending, get_year_start, getdate, now_datetime, nowdate from erpnext.hr.doctype.attendance.attendance import ( - DuplicateAttendanceError, - OverlappingShiftAttendanceError, get_month_map, get_unmarked_days, mark_attendance, @@ -25,112 +23,11 @@ class TestAttendance(FrappeTestCase): from_date = get_year_start(getdate()) to_date = get_year_ending(getdate()) self.holiday_list = make_holiday_list(from_date=from_date, to_date=to_date) - frappe.db.delete("Attendance") - - def test_duplicate_attendance(self): - employee = make_employee("test_duplicate_attendance@example.com", company="_Test Company") - date = nowdate() - - mark_attendance(employee, date, "Present") - attendance = frappe.get_doc( - { - "doctype": "Attendance", - "employee": employee, - "attendance_date": date, - "status": "Absent", - "company": "_Test Company", - } - ) - - self.assertRaises(DuplicateAttendanceError, attendance.insert) - - def test_duplicate_attendance_with_shift(self): - from erpnext.hr.doctype.shift_type.test_shift_type import setup_shift_type - - employee = make_employee("test_duplicate_attendance@example.com", company="_Test Company") - date = nowdate() - - shift_1 = setup_shift_type(shift_type="Shift 1", start_time="08:00:00", end_time="10:00:00") - mark_attendance(employee, date, "Present", shift=shift_1.name) - - # attendance record with shift - attendance = frappe.get_doc( - { - "doctype": "Attendance", - "employee": employee, - "attendance_date": date, - "status": "Absent", - "company": "_Test Company", - "shift": shift_1.name, - } - ) - - self.assertRaises(DuplicateAttendanceError, attendance.insert) - - # attendance record without any shift - attendance = frappe.get_doc( - { - "doctype": "Attendance", - "employee": employee, - "attendance_date": date, - "status": "Absent", - "company": "_Test Company", - } - ) - - self.assertRaises(DuplicateAttendanceError, attendance.insert) - - def test_overlapping_shift_attendance_validation(self): - from erpnext.hr.doctype.shift_type.test_shift_type import setup_shift_type - - employee = make_employee("test_overlap_attendance@example.com", company="_Test Company") - date = nowdate() - - shift_1 = setup_shift_type(shift_type="Shift 1", start_time="08:00:00", end_time="10:00:00") - shift_2 = setup_shift_type(shift_type="Shift 2", start_time="09:30:00", end_time="11:00:00") - - mark_attendance(employee, date, "Present", shift=shift_1.name) - - # attendance record with overlapping shift - attendance = frappe.get_doc( - { - "doctype": "Attendance", - "employee": employee, - "attendance_date": date, - "status": "Absent", - "company": "_Test Company", - "shift": shift_2.name, - } - ) - - self.assertRaises(OverlappingShiftAttendanceError, attendance.insert) - - def test_allow_attendance_with_different_shifts(self): - # allows attendance with 2 different non-overlapping shifts - from erpnext.hr.doctype.shift_type.test_shift_type import setup_shift_type - - employee = make_employee("test_duplicate_attendance@example.com", company="_Test Company") - date = nowdate() - - shift_1 = setup_shift_type(shift_type="Shift 1", start_time="08:00:00", end_time="10:00:00") - shift_2 = setup_shift_type(shift_type="Shift 2", start_time="11:00:00", end_time="12:00:00") - - mark_attendance(employee, date, "Present", shift_1.name) - frappe.get_doc( - { - "doctype": "Attendance", - "employee": employee, - "attendance_date": date, - "status": "Absent", - "company": "_Test Company", - "shift": shift_2.name, - } - ).insert() def test_mark_absent(self): employee = make_employee("test_mark_absent@example.com") date = nowdate() - + frappe.db.delete("Attendance", {"employee": employee, "attendance_date": date}) attendance = mark_attendance(employee, date, "Absent") fetch_attendance = frappe.get_value( "Attendance", {"employee": employee, "attendance_date": date, "status": "Absent"} @@ -145,6 +42,7 @@ class TestAttendance(FrappeTestCase): employee = make_employee( "test_unmarked_days@example.com", date_of_joining=add_days(first_day, -1) ) + frappe.db.delete("Attendance", {"employee": employee}) frappe.db.set_value("Employee", employee, "holiday_list", self.holiday_list) first_sunday = get_first_sunday(self.holiday_list, for_date=first_day) @@ -169,6 +67,8 @@ class TestAttendance(FrappeTestCase): employee = make_employee( "test_unmarked_days@example.com", date_of_joining=add_days(first_day, -1) ) + frappe.db.delete("Attendance", {"employee": employee}) + frappe.db.set_value("Employee", employee, "holiday_list", self.holiday_list) first_sunday = get_first_sunday(self.holiday_list, for_date=first_day) @@ -195,6 +95,7 @@ class TestAttendance(FrappeTestCase): employee = make_employee( "test_unmarked_days_as_per_doj@example.com", date_of_joining=doj, relieving_date=relieving_date ) + frappe.db.delete("Attendance", {"employee": employee}) frappe.db.set_value("Employee", employee, "holiday_list", self.holiday_list) diff --git a/erpnext/hr/doctype/employee_checkin/employee_checkin.py b/erpnext/hr/doctype/employee_checkin/employee_checkin.py index 64eb019b00..87f48b7e25 100644 --- a/erpnext/hr/doctype/employee_checkin/employee_checkin.py +++ b/erpnext/hr/doctype/employee_checkin/employee_checkin.py @@ -7,10 +7,6 @@ from frappe import _ from frappe.model.document import Document from frappe.utils import cint, get_datetime -from erpnext.hr.doctype.attendance.attendance import ( - get_duplicate_attendance_record, - get_overlapping_shift_attendance, -) from erpnext.hr.doctype.shift_assignment.shift_assignment import ( get_actual_start_end_datetime_of_shift, ) @@ -37,24 +33,24 @@ class EmployeeCheckin(Document): shift_actual_timings = get_actual_start_end_datetime_of_shift( self.employee, get_datetime(self.time), True ) - if shift_actual_timings: + if shift_actual_timings[0] and shift_actual_timings[1]: if ( - shift_actual_timings.shift_type.determine_check_in_and_check_out + shift_actual_timings[2].shift_type.determine_check_in_and_check_out == "Strictly based on Log Type in Employee Checkin" and not self.log_type and not self.skip_auto_attendance ): frappe.throw( _("Log Type is required for check-ins falling in the shift: {0}.").format( - shift_actual_timings.shift_type.name + shift_actual_timings[2].shift_type.name ) ) if not self.attendance: - self.shift = shift_actual_timings.shift_type.name - self.shift_actual_start = shift_actual_timings.actual_start - self.shift_actual_end = shift_actual_timings.actual_end - self.shift_start = shift_actual_timings.start_datetime - self.shift_end = shift_actual_timings.end_datetime + self.shift = shift_actual_timings[2].shift_type.name + self.shift_actual_start = shift_actual_timings[0] + self.shift_actual_end = shift_actual_timings[1] + self.shift_start = shift_actual_timings[2].start_datetime + self.shift_end = shift_actual_timings[2].end_datetime else: self.shift = None @@ -140,10 +136,10 @@ def mark_attendance_and_link_log( return None elif attendance_status in ("Present", "Absent", "Half Day"): employee_doc = frappe.get_doc("Employee", employee) - duplicate = get_duplicate_attendance_record(employee, attendance_date, shift) - overlapping = get_overlapping_shift_attendance(employee, attendance_date, shift) - - if not duplicate and not overlapping: + if not frappe.db.exists( + "Attendance", + {"employee": employee, "attendance_date": attendance_date, "docstatus": ("!=", "2")}, + ): doc_dict = { "doctype": "Attendance", "employee": employee, @@ -236,7 +232,7 @@ def calculate_working_hours(logs, check_in_out_type, working_hours_calc_type): def time_diff_in_hours(start, end): - return round(float((end - start).total_seconds()) / 3600, 2) + return round((end - start).total_seconds() / 3600, 1) def find_index_in_dict(dict_list, key, value): diff --git a/erpnext/hr/doctype/employee_checkin/test_employee_checkin.py b/erpnext/hr/doctype/employee_checkin/test_employee_checkin.py index 81b44f8fea..97f76b0350 100644 --- a/erpnext/hr/doctype/employee_checkin/test_employee_checkin.py +++ b/erpnext/hr/doctype/employee_checkin/test_employee_checkin.py @@ -2,19 +2,10 @@ # See license.txt import unittest -from datetime import datetime, timedelta +from datetime import timedelta import frappe -from frappe.tests.utils import FrappeTestCase -from frappe.utils import ( - add_days, - get_time, - get_year_ending, - get_year_start, - getdate, - now_datetime, - nowdate, -) +from frappe.utils import now_datetime, nowdate from erpnext.hr.doctype.employee.test_employee import make_employee from erpnext.hr.doctype.employee_checkin.employee_checkin import ( @@ -22,22 +13,9 @@ from erpnext.hr.doctype.employee_checkin.employee_checkin import ( calculate_working_hours, mark_attendance_and_link_log, ) -from erpnext.hr.doctype.holiday_list.test_holiday_list import set_holiday_list -from erpnext.hr.doctype.leave_application.test_leave_application import get_first_sunday -from erpnext.hr.doctype.shift_type.test_shift_type import make_shift_assignment, setup_shift_type -from erpnext.payroll.doctype.salary_slip.test_salary_slip import make_holiday_list -class TestEmployeeCheckin(FrappeTestCase): - def setUp(self): - frappe.db.delete("Shift Type") - frappe.db.delete("Shift Assignment") - frappe.db.delete("Employee Checkin") - - from_date = get_year_start(getdate()) - to_date = get_year_ending(getdate()) - self.holiday_list = make_holiday_list(from_date=from_date, to_date=to_date) - +class TestEmployeeCheckin(unittest.TestCase): def test_add_log_based_on_employee_field(self): employee = make_employee("test_add_log_based_on_employee_field@example.com") employee = frappe.get_doc("Employee", employee) @@ -125,163 +103,6 @@ class TestEmployeeCheckin(FrappeTestCase): ) self.assertEqual(working_hours, (4.5, logs_type_2[1].time, logs_type_2[-1].time)) - def test_fetch_shift(self): - employee = make_employee("test_employee_checkin@example.com", company="_Test Company") - - # shift setup for 8-12 - shift_type = setup_shift_type() - date = getdate() - make_shift_assignment(shift_type.name, employee, date) - - # within shift time - timestamp = datetime.combine(date, get_time("08:45:00")) - log = make_checkin(employee, timestamp) - self.assertEqual(log.shift, shift_type.name) - - # "begin checkin before shift time" = 60 mins, so should work for 7:00:00 - timestamp = datetime.combine(date, get_time("07:00:00")) - log = make_checkin(employee, timestamp) - self.assertEqual(log.shift, shift_type.name) - - # "allow checkout after shift end time" = 60 mins, so should work for 13:00:00 - timestamp = datetime.combine(date, get_time("13:00:00")) - log = make_checkin(employee, timestamp) - self.assertEqual(log.shift, shift_type.name) - - # should not fetch this shift beyond allowed time - timestamp = datetime.combine(date, get_time("13:01:00")) - log = make_checkin(employee, timestamp) - self.assertIsNone(log.shift) - - def test_shift_start_and_end_timings(self): - employee = make_employee("test_employee_checkin@example.com", company="_Test Company") - - # shift setup for 8-12 - shift_type = setup_shift_type() - date = getdate() - make_shift_assignment(shift_type.name, employee, date) - - timestamp = datetime.combine(date, get_time("08:45:00")) - log = make_checkin(employee, timestamp) - - self.assertEqual(log.shift, shift_type.name) - self.assertEqual(log.shift_start, datetime.combine(date, get_time("08:00:00"))) - self.assertEqual(log.shift_end, datetime.combine(date, get_time("12:00:00"))) - self.assertEqual(log.shift_actual_start, datetime.combine(date, get_time("07:00:00"))) - self.assertEqual(log.shift_actual_end, datetime.combine(date, get_time("13:00:00"))) - - def test_fetch_shift_based_on_default_shift(self): - employee = make_employee("test_default_shift@example.com", company="_Test Company") - default_shift = setup_shift_type( - shift_type="Default Shift", start_time="14:00:00", end_time="16:00:00" - ) - - date = getdate() - frappe.db.set_value("Employee", employee, "default_shift", default_shift.name) - - timestamp = datetime.combine(date, get_time("14:45:00")) - log = make_checkin(employee, timestamp) - - # should consider default shift - self.assertEqual(log.shift, default_shift.name) - - def test_fetch_shift_spanning_over_two_days(self): - employee = make_employee("test_employee_checkin@example.com", company="_Test Company") - shift_type = setup_shift_type( - shift_type="Midnight Shift", start_time="23:00:00", end_time="01:00:00" - ) - date = getdate() - next_day = add_days(date, 1) - make_shift_assignment(shift_type.name, employee, date) - - # log falls in the first day - timestamp = datetime.combine(date, get_time("23:00:00")) - log = make_checkin(employee, timestamp) - - self.assertEqual(log.shift, shift_type.name) - self.assertEqual(log.shift_start, datetime.combine(date, get_time("23:00:00"))) - self.assertEqual(log.shift_end, datetime.combine(next_day, get_time("01:00:00"))) - self.assertEqual(log.shift_actual_start, datetime.combine(date, get_time("22:00:00"))) - self.assertEqual(log.shift_actual_end, datetime.combine(next_day, get_time("02:00:00"))) - - log.delete() - - # log falls in the second day - prev_day = add_days(date, -1) - timestamp = datetime.combine(date, get_time("01:30:00")) - log = make_checkin(employee, timestamp) - self.assertEqual(log.shift, shift_type.name) - self.assertEqual(log.shift_start, datetime.combine(prev_day, get_time("23:00:00"))) - self.assertEqual(log.shift_end, datetime.combine(date, get_time("01:00:00"))) - self.assertEqual(log.shift_actual_start, datetime.combine(prev_day, get_time("22:00:00"))) - self.assertEqual(log.shift_actual_end, datetime.combine(date, get_time("02:00:00"))) - - def test_no_shift_fetched_on_holiday_as_per_shift_holiday_list(self): - date = getdate() - from_date = get_year_start(date) - to_date = get_year_ending(date) - holiday_list = make_holiday_list(from_date=from_date, to_date=to_date) - - employee = make_employee("test_shift_with_holiday@example.com", company="_Test Company") - setup_shift_type(shift_type="Test Holiday Shift", holiday_list=holiday_list) - - first_sunday = get_first_sunday(holiday_list, for_date=date) - timestamp = datetime.combine(first_sunday, get_time("08:00:00")) - log = make_checkin(employee, timestamp) - - self.assertIsNone(log.shift) - - @set_holiday_list("Salary Slip Test Holiday List", "_Test Company") - def test_no_shift_fetched_on_holiday_as_per_employee_holiday_list(self): - employee = make_employee("test_shift_with_holiday@example.com", company="_Test Company") - shift_type = setup_shift_type(shift_type="Test Holiday Shift") - shift_type.holiday_list = None - shift_type.save() - - date = getdate() - - first_sunday = get_first_sunday(self.holiday_list, for_date=date) - timestamp = datetime.combine(first_sunday, get_time("08:00:00")) - log = make_checkin(employee, timestamp) - - self.assertIsNone(log.shift) - - def test_consecutive_shift_assignments_overlapping_within_grace_period(self): - # test adjustment for start and end times if they are overlapping - # within "begin_check_in_before_shift_start_time" and "allow_check_out_after_shift_end_time" periods - employee = make_employee("test_shift@example.com", company="_Test Company") - - # 8 - 12 - shift1 = setup_shift_type() - # 12:30 - 16:30 - shift2 = setup_shift_type( - shift_type="Consecutive Shift", start_time="12:30:00", end_time="16:30:00" - ) - - # the actual start and end times (with grace) for these shifts are 7 - 13 and 11:30 - 17:30 - date = getdate() - make_shift_assignment(shift1.name, employee, date) - make_shift_assignment(shift2.name, employee, date) - - # log at 12:30 should set shift2 and actual start as 12 and not 11:30 - timestamp = datetime.combine(date, get_time("12:30:00")) - log = make_checkin(employee, timestamp) - self.assertEqual(log.shift, shift2.name) - self.assertEqual(log.shift_start, datetime.combine(date, get_time("12:30:00"))) - self.assertEqual(log.shift_actual_start, datetime.combine(date, get_time("12:00:00"))) - - # log at 12:00 should set shift1 and actual end as 12 and not 1 since the next shift's grace starts - timestamp = datetime.combine(date, get_time("12:00:00")) - log = make_checkin(employee, timestamp) - self.assertEqual(log.shift, shift1.name) - self.assertEqual(log.shift_end, datetime.combine(date, get_time("12:00:00"))) - self.assertEqual(log.shift_actual_end, datetime.combine(date, get_time("12:00:00"))) - - # log at 12:01 should set shift2 - timestamp = datetime.combine(date, get_time("12:01:00")) - log = make_checkin(employee, timestamp) - self.assertEqual(log.shift, shift2.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/erpnext/hr/doctype/shift_assignment/shift_assignment.py b/erpnext/hr/doctype/shift_assignment/shift_assignment.py index 0b21c00eac..f6bd15951d 100644 --- a/erpnext/hr/doctype/shift_assignment/shift_assignment.py +++ b/erpnext/hr/doctype/shift_assignment/shift_assignment.py @@ -3,120 +3,83 @@ from datetime import datetime, timedelta -from typing import Dict, List import frappe from frappe import _ from frappe.model.document import Document -from frappe.query_builder import Criterion -from frappe.utils import cstr, get_datetime, get_link_to_form, get_time, getdate, now_datetime +from frappe.utils import cstr, getdate, now_datetime, nowdate 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.utils import validate_active_employee -class OverlappingShiftError(frappe.ValidationError): - pass - - class ShiftAssignment(Document): def validate(self): validate_active_employee(self.employee) - self.validate_overlapping_shifts() + self.validate_overlapping_dates() if self.end_date: self.validate_from_to_dates("start_date", "end_date") - def validate_overlapping_shifts(self): - overlapping_dates = self.get_overlapping_dates() - if len(overlapping_dates): - # if dates are overlapping, check if timings are overlapping, else allow - overlapping_timings = has_overlapping_timings(self.shift_type, overlapping_dates[0].shift_type) - if overlapping_timings: - self.throw_overlap_error(overlapping_dates[0]) - - def get_overlapping_dates(self): + def validate_overlapping_dates(self): if not self.name: self.name = "New Shift Assignment" - shift = frappe.qb.DocType("Shift Assignment") - query = ( - frappe.qb.from_(shift) - .select(shift.name, shift.shift_type, shift.docstatus, shift.status) - .where( - (shift.employee == self.employee) - & (shift.docstatus == 1) - & (shift.name != self.name) - & (shift.status == "Active") - ) - ) + condition = """and ( + end_date is null + or + %(start_date)s between start_date and end_date + """ if self.end_date: - query = query.where( - Criterion.any( - [ - Criterion.any( - [ - shift.end_date.isnull(), - ((self.start_date >= shift.start_date) & (self.start_date <= shift.end_date)), - ] - ), - Criterion.any( - [ - ((self.end_date >= shift.start_date) & (self.end_date <= shift.end_date)), - shift.start_date.between(self.start_date, self.end_date), - ] - ), - ] - ) - ) + condition += """ or + %(end_date)s between start_date and end_date + or + start_date between %(start_date)s and %(end_date)s + ) """ else: - query = query.where( - shift.end_date.isnull() - | ((self.start_date >= shift.start_date) & (self.start_date <= shift.end_date)) - ) + condition += """ ) """ - return query.run(as_dict=True) + assigned_shifts = frappe.db.sql( + """ + select name, shift_type, start_date ,end_date, docstatus, status + from `tabShift Assignment` + where + employee=%(employee)s and docstatus = 1 + and name != %(name)s + and status = "Active" + {0} + """.format( + condition + ), + { + "employee": self.employee, + "shift_type": self.shift_type, + "start_date": self.start_date, + "end_date": self.end_date, + "name": self.name, + }, + as_dict=1, + ) + + if len(assigned_shifts): + self.throw_overlap_error(assigned_shifts[0]) def throw_overlap_error(self, shift_details): shift_details = frappe._dict(shift_details) if shift_details.docstatus == 1 and shift_details.status == "Active": - msg = _( - "Employee {0} already has an active Shift {1}: {2} that overlaps within this period." - ).format( - frappe.bold(self.employee), - frappe.bold(shift_details.shift_type), - get_link_to_form("Shift Assignment", shift_details.name), + msg = _("Employee {0} already has Active Shift {1}: {2}").format( + frappe.bold(self.employee), frappe.bold(self.shift_type), frappe.bold(shift_details.name) ) - frappe.throw(msg, title=_("Overlapping Shifts"), exc=OverlappingShiftError) - - -def has_overlapping_timings(shift_1: str, shift_2: str) -> bool: - """ - Accepts two shift types and checks whether their timings are overlapping - """ - curr_shift = frappe.db.get_value("Shift Type", shift_1, ["start_time", "end_time"], as_dict=True) - overlapping_shift = frappe.db.get_value( - "Shift Type", shift_2, ["start_time", "end_time"], as_dict=True - ) - - if ( - ( - curr_shift.start_time > overlapping_shift.start_time - and curr_shift.start_time < overlapping_shift.end_time - ) - or ( - curr_shift.end_time > overlapping_shift.start_time - and curr_shift.end_time < overlapping_shift.end_time - ) - or ( - curr_shift.start_time <= overlapping_shift.start_time - and curr_shift.end_time >= overlapping_shift.end_time - ) - ): - return True - return False + if shift_details.start_date: + msg += " " + _("from {0}").format(getdate(self.start_date).strftime("%d-%m-%Y")) + title = "Ongoing Shift" + if shift_details.end_date: + msg += " " + _("to {0}").format(getdate(self.end_date).strftime("%d-%m-%Y")) + title = "Active Shift" + if msg: + frappe.throw(msg, title=title) @frappe.whitelist() @@ -192,195 +155,102 @@ def get_shift_type_timing(shift_types): return shift_timing_map -def get_shift_for_time(shifts: List[Dict], for_timestamp: datetime) -> Dict: - """Returns shift with details for given timestamp""" - valid_shifts = [] - - for entry in shifts: - shift_details = get_shift_details(entry.shift_type, for_timestamp=for_timestamp) - - if ( - get_datetime(shift_details.actual_start) - <= get_datetime(for_timestamp) - <= get_datetime(shift_details.actual_end) - ): - valid_shifts.append(shift_details) - - valid_shifts.sort(key=lambda x: x["actual_start"]) - - if len(valid_shifts) > 1: - for i in range(len(valid_shifts) - 1): - # comparing 2 consecutive shifts and adjusting start and end times - # if they are overlapping within grace period - curr_shift = valid_shifts[i] - next_shift = valid_shifts[i + 1] - - if curr_shift and next_shift: - next_shift.actual_start = ( - curr_shift.end_datetime - if next_shift.actual_start < curr_shift.end_datetime - else next_shift.actual_start - ) - curr_shift.actual_end = ( - next_shift.actual_start - if curr_shift.actual_end > next_shift.actual_start - else curr_shift.actual_end - ) - - valid_shifts[i] = curr_shift - valid_shifts[i + 1] = next_shift - - return get_exact_shift(valid_shifts, for_timestamp) or {} - - return (valid_shifts and valid_shifts[0]) or {} - - -def get_shifts_for_date(employee: str, for_timestamp: datetime) -> List[Dict[str, str]]: - """Returns list of shifts with details for given date""" - assignment = frappe.qb.DocType("Shift Assignment") - - return ( - frappe.qb.from_(assignment) - .select(assignment.name, assignment.shift_type) - .where( - (assignment.employee == employee) - & (assignment.docstatus == 1) - & (assignment.status == "Active") - & (assignment.start_date <= getdate(for_timestamp.date())) - & ( - Criterion.any( - [ - assignment.end_date.isnull(), - (assignment.end_date.isnotnull() & (getdate(for_timestamp.date()) >= assignment.end_date)), - ] - ) - ) - ) - ).run(as_dict=True) - - -def get_shift_for_timestamp(employee: str, for_timestamp: datetime) -> Dict: - shifts = get_shifts_for_date(employee, for_timestamp) - if shifts: - return get_shift_for_time(shifts, for_timestamp) - return {} - - def get_employee_shift( - employee: str, - for_timestamp: datetime = None, - consider_default_shift: bool = False, - next_shift_direction: str = None, -) -> Dict: + employee, for_date=None, consider_default_shift=False, next_shift_direction=None +): """Returns a Shift Type for the given employee on the given date. (excluding the holidays) :param employee: Employee for which shift is required. - :param for_timestamp: DateTime on which shift is required + :param for_date: Date on which shift are required :param consider_default_shift: If set to true, default shift is taken when no shift assignment is found. :param next_shift_direction: One of: None, 'forward', 'reverse'. Direction to look for next shift if shift not found on given date. """ - if for_timestamp is None: - for_timestamp = now_datetime() - - shift_details = get_shift_for_timestamp(employee, for_timestamp) - - # if shift assignment is not found, consider default shift + if for_date is None: + for_date = nowdate() default_shift = frappe.db.get_value("Employee", employee, "default_shift") - if not shift_details and consider_default_shift: - shift_details = get_shift_details(default_shift, for_timestamp) - - # if its a holiday, reset - if shift_details and is_holiday_date(employee, shift_details): - shift_details = None - - # if no shift is found, find next or prev shift assignment based on direction - if not shift_details and next_shift_direction: - shift_details = get_prev_or_next_shift( - employee, for_timestamp, consider_default_shift, default_shift, next_shift_direction - ) - - return shift_details or {} - - -def get_prev_or_next_shift( - employee: str, - for_timestamp: datetime, - consider_default_shift: bool, - default_shift: str, - next_shift_direction: str, -) -> Dict: - """Returns a dict of shift details for the next or prev shift based on the next_shift_direction""" - MAX_DAYS = 366 - shift_details = {} - - if consider_default_shift and default_shift: - direction = -1 if next_shift_direction == "reverse" else 1 - for i in range(MAX_DAYS): - date = for_timestamp + timedelta(days=direction * (i + 1)) - shift_details = get_employee_shift(employee, date, consider_default_shift, None) - if shift_details: - break - else: - direction = "<" if next_shift_direction == "reverse" else ">" - sort_order = "desc" if next_shift_direction == "reverse" else "asc" - dates = frappe.db.get_all( - "Shift Assignment", - ["start_date", "end_date"], - { - "employee": employee, - "start_date": (direction, for_timestamp.date()), - "docstatus": 1, - "status": "Active", - }, - as_list=True, - limit=MAX_DAYS, - order_by="start_date " + sort_order, - ) - - if dates: - for date in dates: - if date[1] and date[1] < for_timestamp.date(): - continue - shift_details = get_employee_shift( - employee, datetime.combine(date[0], for_timestamp.time()), consider_default_shift, None - ) - if shift_details: - break - - return shift_details or {} - - -def is_holiday_date(employee: str, shift_details: Dict) -> bool: - holiday_list_name = frappe.db.get_value( - "Shift Type", shift_details.shift_type.name, "holiday_list" + shift_type_name = None + shift_assignment_details = frappe.db.get_value( + "Shift Assignment", + {"employee": employee, "start_date": ("<=", for_date), "docstatus": "1", "status": "Active"}, + ["shift_type", "end_date"], ) - if not holiday_list_name: - holiday_list_name = get_holiday_list_for_employee(employee, False) + if shift_assignment_details: + shift_type_name = shift_assignment_details[0] - return holiday_list_name and is_holiday(holiday_list_name, shift_details.start_datetime.date()) + # if end_date present means that shift is over after end_date else it is a ongoing shift. + if shift_assignment_details[1] and for_date >= shift_assignment_details[1]: + shift_type_name = None + + if not shift_type_name and consider_default_shift: + shift_type_name = default_shift + if shift_type_name: + holiday_list_name = frappe.db.get_value("Shift Type", shift_type_name, "holiday_list") + if not holiday_list_name: + holiday_list_name = get_holiday_list_for_employee(employee, False) + if holiday_list_name and is_holiday(holiday_list_name, for_date): + shift_type_name = None + + if not shift_type_name and next_shift_direction: + MAX_DAYS = 366 + if consider_default_shift and default_shift: + direction = -1 if next_shift_direction == "reverse" else +1 + for i in range(MAX_DAYS): + date = for_date + timedelta(days=direction * (i + 1)) + shift_details = get_employee_shift(employee, date, consider_default_shift, None) + if shift_details: + shift_type_name = shift_details.shift_type.name + for_date = date + break + else: + direction = "<" if next_shift_direction == "reverse" else ">" + sort_order = "desc" if next_shift_direction == "reverse" else "asc" + dates = frappe.db.get_all( + "Shift Assignment", + ["start_date", "end_date"], + { + "employee": employee, + "start_date": (direction, for_date), + "docstatus": "1", + "status": "Active", + }, + as_list=True, + limit=MAX_DAYS, + order_by="start_date " + sort_order, + ) + + if dates: + for date in dates: + if date[1] and date[1] < for_date: + continue + shift_details = get_employee_shift(employee, date[0], consider_default_shift, None) + if shift_details: + shift_type_name = shift_details.shift_type.name + for_date = date[0] + break + + return get_shift_details(shift_type_name, for_date) -def get_employee_shift_timings( - employee: str, for_timestamp: datetime = None, consider_default_shift: bool = False -) -> List[Dict]: +def get_employee_shift_timings(employee, for_timestamp=None, consider_default_shift=False): """Returns previous shift, current/upcoming shift, next_shift for the given timestamp and employee""" if for_timestamp is None: for_timestamp = now_datetime() - # write and verify a test case for midnight shift. prev_shift = curr_shift = next_shift = None - curr_shift = get_employee_shift(employee, for_timestamp, consider_default_shift, "forward") + curr_shift = get_employee_shift(employee, for_timestamp.date(), consider_default_shift, "forward") if curr_shift: next_shift = get_employee_shift( - employee, curr_shift.start_datetime + timedelta(days=1), consider_default_shift, "forward" + employee, + curr_shift.start_datetime.date() + timedelta(days=1), + consider_default_shift, + "forward", ) prev_shift = get_employee_shift( - employee, for_timestamp + timedelta(days=-1), consider_default_shift, "reverse" + employee, for_timestamp.date() + timedelta(days=-1), consider_default_shift, "reverse" ) if curr_shift: - # adjust actual start and end times if they are overlapping with grace period (before start and after end) if prev_shift: curr_shift.actual_start = ( prev_shift.end_datetime @@ -403,102 +273,31 @@ def get_employee_shift_timings( if curr_shift.actual_end > next_shift.actual_start else curr_shift.actual_end ) - return prev_shift, curr_shift, next_shift -def get_actual_start_end_datetime_of_shift( - employee: str, for_timestamp: datetime, consider_default_shift: bool = False -) -> Dict: - """Returns a Dict containing shift details with actual_start and actual_end datetime values - Here 'actual' means taking into account the "begin_check_in_before_shift_start_time" and "allow_check_out_after_shift_end_time". - Empty Dict is returned if the timestamp is outside any actual shift timings. +def get_shift_details(shift_type_name, for_date=None): + """Returns Shift Details which contain some additional information as described below. + 'shift_details' contains the following keys: + 'shift_type' - Object of DocType Shift Type, + 'start_datetime' - Date and Time of shift start on given date, + 'end_datetime' - Date and Time of shift end on given date, + 'actual_start' - datetime of shift start after adding 'begin_check_in_before_shift_start_time', + 'actual_end' - datetime of shift end after adding 'allow_check_out_after_shift_end_time'(None is returned if this is zero) - :param employee (str): Employee name - :param for_timestamp (datetime, optional): Datetime value of checkin, if not provided considers current datetime - :param consider_default_shift (bool, optional): Flag (defaults to False) to specify whether to consider - default shift in employee master if no shift assignment is found - """ - shift_timings_as_per_timestamp = get_employee_shift_timings( - employee, for_timestamp, consider_default_shift - ) - return get_exact_shift(shift_timings_as_per_timestamp, for_timestamp) - - -def get_exact_shift(shifts: List, for_timestamp: datetime) -> Dict: - """Returns the shift details (dict) for the exact shift in which the 'for_timestamp' value falls among multiple shifts""" - shift_details = dict() - timestamp_list = [] - - for shift in shifts: - if shift: - timestamp_list.extend([shift.actual_start, shift.actual_end]) - else: - timestamp_list.extend([None, None]) - - timestamp_index = None - for index, timestamp in enumerate(timestamp_list): - if not timestamp: - continue - - if for_timestamp < timestamp: - timestamp_index = index - elif for_timestamp == timestamp: - # on timestamp boundary - if index % 2 == 1: - timestamp_index = index - else: - timestamp_index = index + 1 - - if timestamp_index: - break - - if timestamp_index and timestamp_index % 2 == 1: - shift_details = shifts[int((timestamp_index - 1) / 2)] - - return shift_details - - -def get_shift_details(shift_type_name: str, for_timestamp: datetime = None) -> Dict: - """Returns a Dict containing shift details with the following data: - 'shift_type' - Object of DocType Shift Type, - 'start_datetime' - datetime of shift start on given timestamp, - 'end_datetime' - datetime of shift end on given timestamp, - 'actual_start' - datetime of shift start after adding 'begin_check_in_before_shift_start_time', - 'actual_end' - datetime of shift end after adding 'allow_check_out_after_shift_end_time' (None is returned if this is zero) - - :param shift_type_name (str): shift type name for which shift_details are required. - :param for_timestamp (datetime, optional): Datetime value of checkin, if not provided considers current datetime + :param shift_type_name: shift type name for which shift_details is required. + :param for_date: Date on which shift_details are required """ if not shift_type_name: - return {} - - if for_timestamp is None: - for_timestamp = now_datetime() - + return None + if not for_date: + for_date = nowdate() shift_type = frappe.get_doc("Shift Type", shift_type_name) - shift_actual_start = shift_type.start_time - timedelta( - minutes=shift_type.begin_check_in_before_shift_start_time + start_datetime = datetime.combine(for_date, datetime.min.time()) + shift_type.start_time + for_date = ( + for_date + timedelta(days=1) if shift_type.start_time > shift_type.end_time else for_date ) - - if shift_type.start_time > shift_type.end_time: - # shift spans accross 2 different days - if get_time(for_timestamp.time()) >= get_time(shift_actual_start): - # if for_timestamp is greater than start time, it's within the first day - start_datetime = datetime.combine(for_timestamp, datetime.min.time()) + shift_type.start_time - for_timestamp = for_timestamp + timedelta(days=1) - end_datetime = datetime.combine(for_timestamp, datetime.min.time()) + shift_type.end_time - - elif get_time(for_timestamp.time()) < get_time(shift_actual_start): - # if for_timestamp is less than start time, it's within the second day - end_datetime = datetime.combine(for_timestamp, datetime.min.time()) + shift_type.end_time - for_timestamp = for_timestamp + timedelta(days=-1) - start_datetime = datetime.combine(for_timestamp, datetime.min.time()) + shift_type.start_time - else: - # start and end timings fall on the same day - start_datetime = datetime.combine(for_timestamp, datetime.min.time()) + shift_type.start_time - end_datetime = datetime.combine(for_timestamp, 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 ) @@ -513,3 +312,34 @@ def get_shift_details(shift_type_name: str, for_timestamp: datetime = None) -> D "actual_end": actual_end, } ) + + +def get_actual_start_end_datetime_of_shift(employee, for_datetime, consider_default_shift=False): + """Takes a datetime and returns the 'actual' start datetime and end datetime of the shift in which the timestamp belongs. + Here 'actual' means - taking in to account the "begin_check_in_before_shift_start_time" and "allow_check_out_after_shift_end_time". + None is returned if the timestamp is outside any actual shift timings. + Shift Details is also returned(current/upcoming i.e. if timestamp not in any actual shift then details of next shift returned) + """ + actual_shift_start = actual_shift_end = shift_details = None + shift_timings_as_per_timestamp = get_employee_shift_timings( + employee, for_datetime, consider_default_shift + ) + timestamp_list = [] + for shift in shift_timings_as_per_timestamp: + if shift: + timestamp_list.extend([shift.actual_start, shift.actual_end]) + else: + timestamp_list.extend([None, None]) + timestamp_index = None + for index, timestamp in enumerate(timestamp_list): + if timestamp and for_datetime <= timestamp: + timestamp_index = index + break + if timestamp_index and timestamp_index % 2 == 1: + shift_details = shift_timings_as_per_timestamp[int((timestamp_index - 1) / 2)] + actual_shift_start = shift_details.actual_start + actual_shift_end = shift_details.actual_end + elif timestamp_index: + shift_details = shift_timings_as_per_timestamp[int(timestamp_index / 2)] + + return actual_shift_start, actual_shift_end, shift_details diff --git a/erpnext/hr/doctype/shift_assignment/test_shift_assignment.py b/erpnext/hr/doctype/shift_assignment/test_shift_assignment.py index 0fe9108168..4a1ec293bd 100644 --- a/erpnext/hr/doctype/shift_assignment/test_shift_assignment.py +++ b/erpnext/hr/doctype/shift_assignment/test_shift_assignment.py @@ -4,23 +4,16 @@ import unittest import frappe -from frappe.tests.utils import FrappeTestCase -from frappe.utils import add_days, getdate, nowdate - -from erpnext.hr.doctype.employee.test_employee import make_employee -from erpnext.hr.doctype.shift_assignment.shift_assignment import OverlappingShiftError -from erpnext.hr.doctype.shift_type.test_shift_type import make_shift_assignment, setup_shift_type +from frappe.utils import add_days, nowdate test_dependencies = ["Shift Type"] -class TestShiftAssignment(FrappeTestCase): +class TestShiftAssignment(unittest.TestCase): def setUp(self): - frappe.db.delete("Shift Assignment") - frappe.db.delete("Shift Type") + frappe.db.sql("delete from `tabShift Assignment`") def test_make_shift_assignment(self): - setup_shift_type(shift_type="Day Shift") shift_assignment = frappe.get_doc( { "doctype": "Shift Assignment", @@ -36,7 +29,7 @@ class TestShiftAssignment(FrappeTestCase): def test_overlapping_for_ongoing_shift(self): # shift should be Ongoing if Only start_date is present and status = Active - setup_shift_type(shift_type="Day Shift") + shift_assignment_1 = frappe.get_doc( { "doctype": "Shift Assignment", @@ -61,11 +54,11 @@ class TestShiftAssignment(FrappeTestCase): } ) - self.assertRaises(OverlappingShiftError, shift_assignment.save) + self.assertRaises(frappe.ValidationError, shift_assignment.save) def test_overlapping_for_fixed_period_shift(self): # shift should is for Fixed period if Only start_date and end_date both are present and status = Active - setup_shift_type(shift_type="Day Shift") + shift_assignment_1 = frappe.get_doc( { "doctype": "Shift Assignment", @@ -92,65 +85,4 @@ class TestShiftAssignment(FrappeTestCase): } ) - self.assertRaises(OverlappingShiftError, shift_assignment_3.save) - - def test_overlapping_for_a_fixed_period_shift_and_ongoing_shift(self): - employee = make_employee("test_shift_assignment@example.com", company="_Test Company") - - # shift setup for 8-12 - shift_type = setup_shift_type(shift_type="Shift 1", start_time="08:00:00", end_time="12:00:00") - date = getdate() - # shift with end date - make_shift_assignment(shift_type.name, employee, date, add_days(date, 30)) - - # shift setup for 11-15 - shift_type = setup_shift_type(shift_type="Shift 2", start_time="11:00:00", end_time="15:00:00") - date = getdate() - - # shift assignment without end date - shift2 = frappe.get_doc( - { - "doctype": "Shift Assignment", - "shift_type": shift_type.name, - "company": "_Test Company", - "employee": employee, - "start_date": date, - } - ) - self.assertRaises(OverlappingShiftError, shift2.insert) - - def test_overlap_validation_for_shifts_on_same_day_with_overlapping_timeslots(self): - employee = make_employee("test_shift_assignment@example.com", company="_Test Company") - - # shift setup for 8-12 - shift_type = setup_shift_type(shift_type="Shift 1", start_time="08:00:00", end_time="12:00:00") - date = getdate() - make_shift_assignment(shift_type.name, employee, date) - - # shift setup for 11-15 - shift_type = setup_shift_type(shift_type="Shift 2", start_time="11:00:00", end_time="15:00:00") - date = getdate() - - shift2 = frappe.get_doc( - { - "doctype": "Shift Assignment", - "shift_type": shift_type.name, - "company": "_Test Company", - "employee": employee, - "start_date": date, - } - ) - self.assertRaises(OverlappingShiftError, shift2.insert) - - def test_multiple_shift_assignments_for_same_day(self): - employee = make_employee("test_shift_assignment@example.com", company="_Test Company") - - # shift setup for 8-12 - shift_type = setup_shift_type(shift_type="Shift 1", start_time="08:00:00", end_time="12:00:00") - date = getdate() - make_shift_assignment(shift_type.name, employee, date) - - # shift setup for 13-15 - shift_type = setup_shift_type(shift_type="Shift 2", start_time="13:00:00", end_time="15:00:00") - date = getdate() - make_shift_assignment(shift_type.name, employee, date) + self.assertRaises(frappe.ValidationError, shift_assignment_3.save) diff --git a/erpnext/hr/doctype/shift_request/shift_request.py b/erpnext/hr/doctype/shift_request/shift_request.py index 2bee2404aa..b5beef7a99 100644 --- a/erpnext/hr/doctype/shift_request/shift_request.py +++ b/erpnext/hr/doctype/shift_request/shift_request.py @@ -5,14 +5,12 @@ import frappe from frappe import _ from frappe.model.document import Document -from frappe.query_builder import Criterion -from frappe.utils import get_link_to_form, getdate +from frappe.utils import formatdate, getdate -from erpnext.hr.doctype.shift_assignment.shift_assignment import has_overlapping_timings from erpnext.hr.utils import share_doc_with_approver, validate_active_employee -class OverlappingShiftRequestError(frappe.ValidationError): +class OverlapError(frappe.ValidationError): pass @@ -20,7 +18,7 @@ class ShiftRequest(Document): def validate(self): validate_active_employee(self.employee) self.validate_dates() - self.validate_overlapping_shift_requests() + self.validate_shift_request_overlap_dates() self.validate_approver() self.validate_default_shift() @@ -81,60 +79,37 @@ class ShiftRequest(Document): if self.from_date and self.to_date and (getdate(self.to_date) < getdate(self.from_date)): frappe.throw(_("To date cannot be before from date")) - def validate_overlapping_shift_requests(self): - overlapping_dates = self.get_overlapping_dates() - if len(overlapping_dates): - # if dates are overlapping, check if timings are overlapping, else allow - overlapping_timings = has_overlapping_timings(self.shift_type, overlapping_dates[0].shift_type) - if overlapping_timings: - self.throw_overlap_error(overlapping_dates[0]) - - def get_overlapping_dates(self): + def validate_shift_request_overlap_dates(self): if not self.name: self.name = "New Shift Request" - shift = frappe.qb.DocType("Shift Request") - query = ( - frappe.qb.from_(shift) - .select(shift.name, shift.shift_type) - .where((shift.employee == self.employee) & (shift.docstatus < 2) & (shift.name != self.name)) + d = frappe.db.sql( + """ + select + name, shift_type, from_date, to_date + from `tabShift Request` + where employee = %(employee)s and docstatus < 2 + and ((%(from_date)s >= from_date + and %(from_date)s <= to_date) or + ( %(to_date)s >= from_date + and %(to_date)s <= to_date )) + and name != %(name)s""", + { + "employee": self.employee, + "shift_type": self.shift_type, + "from_date": self.from_date, + "to_date": self.to_date, + "name": self.name, + }, + as_dict=1, ) - if self.to_date: - query = query.where( - Criterion.any( - [ - Criterion.any( - [ - shift.to_date.isnull(), - ((self.from_date >= shift.from_date) & (self.from_date <= shift.to_date)), - ] - ), - Criterion.any( - [ - ((self.to_date >= shift.from_date) & (self.to_date <= shift.to_date)), - shift.from_date.between(self.from_date, self.to_date), - ] - ), - ] - ) - ) - else: - query = query.where( - shift.to_date.isnull() - | ((self.from_date >= shift.from_date) & (self.from_date <= shift.to_date)) - ) + for date_overlap in d: + if date_overlap["name"]: + self.throw_overlap_error(date_overlap) - return query.run(as_dict=True) - - def throw_overlap_error(self, shift_details): - shift_details = frappe._dict(shift_details) - msg = _( - "Employee {0} has already applied for Shift {1}: {2} that overlaps within this period" - ).format( - frappe.bold(self.employee), - frappe.bold(shift_details.shift_type), - get_link_to_form("Shift Request", shift_details.name), - ) - - frappe.throw(msg, title=_("Overlapping Shift Requests"), exc=OverlappingShiftRequestError) + def throw_overlap_error(self, d): + msg = _("Employee {0} has already applied for {1} between {2} and {3}").format( + self.employee, d["shift_type"], formatdate(d["from_date"]), formatdate(d["to_date"]) + ) + """ : {0}""".format(d["name"]) + frappe.throw(msg, OverlapError) diff --git a/erpnext/hr/doctype/shift_request/test_shift_request.py b/erpnext/hr/doctype/shift_request/test_shift_request.py index c47418cfa8..b4f5177215 100644 --- a/erpnext/hr/doctype/shift_request/test_shift_request.py +++ b/erpnext/hr/doctype/shift_request/test_shift_request.py @@ -4,24 +4,23 @@ import unittest import frappe -from frappe.tests.utils import FrappeTestCase from frappe.utils import add_days, nowdate from erpnext.hr.doctype.employee.test_employee import make_employee -from erpnext.hr.doctype.shift_request.shift_request import OverlappingShiftRequestError -from erpnext.hr.doctype.shift_type.test_shift_type import setup_shift_type test_dependencies = ["Shift Type"] -class TestShiftRequest(FrappeTestCase): +class TestShiftRequest(unittest.TestCase): def setUp(self): - for doctype in ["Shift Request", "Shift Assignment", "Shift Type"]: - frappe.db.delete(doctype) + for doctype in ["Shift Request", "Shift Assignment"]: + frappe.db.sql("delete from `tab{doctype}`".format(doctype=doctype)) + + def tearDown(self): + frappe.db.rollback() def test_make_shift_request(self): "Test creation/updation of Shift Assignment from Shift Request." - setup_shift_type(shift_type="Day Shift") department = frappe.get_value("Employee", "_T-Employee-00001", "department") set_shift_approver(department) approver = frappe.db.sql( @@ -49,7 +48,6 @@ class TestShiftRequest(FrappeTestCase): self.assertEqual(shift_assignment_docstatus, 2) def test_shift_request_approver_perms(self): - setup_shift_type(shift_type="Day Shift") employee = frappe.get_doc("Employee", "_T-Employee-00001") user = "test_approver_perm_emp@example.com" make_employee(user, "_Test Company") @@ -89,145 +87,6 @@ class TestShiftRequest(FrappeTestCase): employee.shift_request_approver = "" employee.save() - def test_overlap_for_request_without_to_date(self): - # shift should be Ongoing if Only from_date is present - user = "test_shift_request@example.com" - employee = make_employee(user, company="_Test Company", shift_request_approver=user) - setup_shift_type(shift_type="Day Shift") - - shift_request = frappe.get_doc( - { - "doctype": "Shift Request", - "shift_type": "Day Shift", - "company": "_Test Company", - "employee": employee, - "from_date": nowdate(), - "approver": user, - "status": "Approved", - } - ).submit() - - shift_request = frappe.get_doc( - { - "doctype": "Shift Request", - "shift_type": "Day Shift", - "company": "_Test Company", - "employee": employee, - "from_date": add_days(nowdate(), 2), - "approver": user, - "status": "Approved", - } - ) - - self.assertRaises(OverlappingShiftRequestError, shift_request.save) - - def test_overlap_for_request_with_from_and_to_dates(self): - user = "test_shift_request@example.com" - employee = make_employee(user, company="_Test Company", shift_request_approver=user) - setup_shift_type(shift_type="Day Shift") - - shift_request = frappe.get_doc( - { - "doctype": "Shift Request", - "shift_type": "Day Shift", - "company": "_Test Company", - "employee": employee, - "from_date": nowdate(), - "to_date": add_days(nowdate(), 30), - "approver": user, - "status": "Approved", - } - ).submit() - - shift_request = frappe.get_doc( - { - "doctype": "Shift Request", - "shift_type": "Day Shift", - "company": "_Test Company", - "employee": employee, - "from_date": add_days(nowdate(), 10), - "to_date": add_days(nowdate(), 35), - "approver": user, - "status": "Approved", - } - ) - - self.assertRaises(OverlappingShiftRequestError, shift_request.save) - - def test_overlapping_for_a_fixed_period_shift_and_ongoing_shift(self): - user = "test_shift_request@example.com" - employee = make_employee(user, company="_Test Company", shift_request_approver=user) - - # shift setup for 8-12 - shift_type = setup_shift_type(shift_type="Shift 1", start_time="08:00:00", end_time="12:00:00") - date = nowdate() - - # shift with end date - frappe.get_doc( - { - "doctype": "Shift Request", - "shift_type": shift_type.name, - "company": "_Test Company", - "employee": employee, - "from_date": date, - "to_date": add_days(date, 30), - "approver": user, - "status": "Approved", - } - ).submit() - - # shift setup for 11-15 - shift_type = setup_shift_type(shift_type="Shift 2", start_time="11:00:00", end_time="15:00:00") - shift2 = frappe.get_doc( - { - "doctype": "Shift Request", - "shift_type": shift_type.name, - "company": "_Test Company", - "employee": employee, - "from_date": date, - "approver": user, - "status": "Approved", - } - ) - - self.assertRaises(OverlappingShiftRequestError, shift2.insert) - - def test_allow_non_overlapping_shift_requests_for_same_day(self): - user = "test_shift_request@example.com" - employee = make_employee(user, company="_Test Company", shift_request_approver=user) - - # shift setup for 8-12 - shift_type = setup_shift_type(shift_type="Shift 1", start_time="08:00:00", end_time="12:00:00") - date = nowdate() - - # shift with end date - frappe.get_doc( - { - "doctype": "Shift Request", - "shift_type": shift_type.name, - "company": "_Test Company", - "employee": employee, - "from_date": date, - "to_date": add_days(date, 30), - "approver": user, - "status": "Approved", - } - ).submit() - - # shift setup for 13-15 - shift_type = setup_shift_type(shift_type="Shift 2", start_time="13:00:00", end_time="15:00:00") - frappe.get_doc( - { - "doctype": "Shift Request", - "shift_type": shift_type.name, - "company": "_Test Company", - "employee": employee, - "from_date": date, - "approver": user, - "status": "Approved", - } - ).submit() - def set_shift_approver(department): department_doc = frappe.get_doc("Department", department) diff --git a/erpnext/hr/doctype/shift_type/shift_type.py b/erpnext/hr/doctype/shift_type/shift_type.py index 5e214cf7b7..3f5cb222bf 100644 --- a/erpnext/hr/doctype/shift_type/shift_type.py +++ b/erpnext/hr/doctype/shift_type/shift_type.py @@ -3,23 +3,21 @@ import itertools -from datetime import datetime, timedelta +from datetime import timedelta import frappe from frappe.model.document import Document -from frappe.utils import cint, get_datetime, get_time, getdate +from frappe.utils import cint, get_datetime, getdate -from erpnext.buying.doctype.supplier_scorecard.supplier_scorecard import daterange from erpnext.hr.doctype.attendance.attendance import mark_attendance from erpnext.hr.doctype.employee.employee import get_holiday_list_for_employee from erpnext.hr.doctype.employee_checkin.employee_checkin import ( calculate_working_hours, mark_attendance_and_link_log, ) -from erpnext.hr.doctype.holiday_list.holiday_list import is_holiday from erpnext.hr.doctype.shift_assignment.shift_assignment import ( + get_actual_start_end_datetime_of_shift, get_employee_shift, - get_shift_details, ) @@ -32,9 +30,8 @@ class ShiftType(Document): or not self.last_sync_of_checkin ): return - filters = { - "skip_auto_attendance": 0, + "skip_auto_attendance": "0", "attendance": ("is", "not set"), "time": (">=", self.process_attendance_after), "shift_actual_end": ("<", self.last_sync_of_checkin), @@ -43,7 +40,6 @@ class ShiftType(Document): logs = frappe.db.get_list( "Employee Checkin", fields="*", filters=filters, order_by="employee,time" ) - for key, group in itertools.groupby( logs, key=lambda x: (x["employee"], x["shift_actual_start"]) ): @@ -56,7 +52,6 @@ class ShiftType(Document): in_time, out_time, ) = self.get_attendance(single_shift_logs) - mark_attendance_and_link_log( single_shift_logs, attendance_status, @@ -68,16 +63,15 @@ class ShiftType(Document): out_time, self.name, ) - for employee in self.get_assigned_employee(self.process_attendance_after, True): self.mark_absent_for_dates_with_no_attendance(employee) def get_attendance(self, logs): """Return attendance_status, working_hours, late_entry, early_exit, in_time, out_time for a set of logs belonging to a single shift. - Assumptions: - 1. These logs belongs to a single shift, single employee and it's not in a holiday date. - 2. Logs are in chronological order + Assumtion: + 1. These logs belongs to an single shift, single employee and is not in a holiday date. + 2. Logs are in chronological order """ late_entry = early_exit = False total_working_hours, in_time, out_time = calculate_working_hours( @@ -97,68 +91,39 @@ class ShiftType(Document): ): early_exit = True - if ( - self.working_hours_threshold_for_half_day - and total_working_hours < self.working_hours_threshold_for_half_day - ): - return "Half Day", total_working_hours, late_entry, early_exit, in_time, out_time if ( self.working_hours_threshold_for_absent and total_working_hours < self.working_hours_threshold_for_absent ): 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 + ): + return "Half Day", total_working_hours, late_entry, early_exit, in_time, out_time return "Present", total_working_hours, late_entry, early_exit, in_time, out_time def mark_absent_for_dates_with_no_attendance(self, employee): """Marks Absents for the given employee on working days in this shift which have no attendance marked. The Absent is marked starting from 'process_attendance_after' or employee creation date. """ - start_date, end_date = self.get_start_and_end_dates(employee) - - # no shift assignment found, no need to process absent attendance records - if start_date is None: - return - - holiday_list_name = self.holiday_list - if not holiday_list_name: - holiday_list_name = get_holiday_list_for_employee(employee, False) - - start_time = get_time(self.start_time) - - for date in daterange(getdate(start_date), getdate(end_date)): - if is_holiday(holiday_list_name, date): - # skip marking absent on a holiday - continue - - timestamp = datetime.combine(date, start_time) - shift_details = get_employee_shift(employee, timestamp, True) - - if shift_details and shift_details.shift_type.name == self.name: - mark_attendance(employee, date, "Absent", self.name) - - def get_start_and_end_dates(self, employee): - """Returns start and end dates for checking attendance and marking absent - return: start date = max of `process_attendance_after` and DOJ - return: end date = min of shift before `last_sync_of_checkin` and Relieving Date - """ date_of_joining, relieving_date, employee_creation = frappe.db.get_value( "Employee", employee, ["date_of_joining", "relieving_date", "creation"] ) - if not date_of_joining: date_of_joining = employee_creation.date() - start_date = max(getdate(self.process_attendance_after), date_of_joining) - end_date = None - - shift_details = get_shift_details(self.name, get_datetime(self.last_sync_of_checkin)) - last_shift_time = ( - shift_details.actual_start if shift_details else get_datetime(self.last_sync_of_checkin) + actual_shift_datetime = get_actual_start_end_datetime_of_shift( + employee, get_datetime(self.last_sync_of_checkin), True + ) + last_shift_time = ( + actual_shift_datetime[0] + if actual_shift_datetime[0] + else get_datetime(self.last_sync_of_checkin) + ) + prev_shift = get_employee_shift( + employee, last_shift_time.date() - timedelta(days=1), True, "reverse" ) - - # check if shift is found for 1 day before the last sync of checkin - # absentees are auto-marked 1 day after the shift to wait for any manual attendance records - prev_shift = get_employee_shift(employee, last_shift_time - timedelta(days=1), True, "reverse") if prev_shift: end_date = ( min(prev_shift.start_datetime.date(), relieving_date) @@ -166,21 +131,28 @@ class ShiftType(Document): else prev_shift.start_datetime.date() ) else: - # no shift found - return None, None - return start_date, end_date + return + holiday_list_name = self.holiday_list + if not holiday_list_name: + holiday_list_name = get_holiday_list_for_employee(employee, False) + dates = get_filtered_date_list(employee, start_date, end_date, holiday_list=holiday_list_name) + for date in dates: + shift_details = get_employee_shift(employee, date, True) + if shift_details and shift_details.shift_type.name == self.name: + mark_attendance(employee, date, "Absent", self.name) def get_assigned_employee(self, from_date=None, consider_default_shift=False): - filters = {"shift_type": self.name, "docstatus": "1"} - if from_date: - filters["start_date"] = (">", from_date) + filters = {"start_date": (">", from_date), "shift_type": self.name, "docstatus": "1"} + if not from_date: + del filters["start_date"] - assigned_employees = frappe.get_all("Shift Assignment", filters=filters, pluck="employee") + assigned_employees = frappe.get_all("Shift Assignment", "employee", filters, as_list=True) + assigned_employees = [x[0] for x in assigned_employees] if consider_default_shift: filters = {"default_shift": self.name, "status": ["!=", "Inactive"]} - default_shift_employees = frappe.get_all("Employee", filters=filters, pluck="name") - + default_shift_employees = frappe.get_all("Employee", "name", filters, as_list=True) + default_shift_employees = [x[0] for x in default_shift_employees] return list(set(assigned_employees + default_shift_employees)) return assigned_employees @@ -190,3 +162,42 @@ def process_auto_attendance_for_all_shifts(): for shift in shift_list: doc = frappe.get_doc("Shift Type", shift[0]) doc.process_auto_attendance() + + +def get_filtered_date_list( + employee, start_date, end_date, filter_attendance=True, holiday_list=None +): + """Returns a list of dates after removing the dates with attendance and holidays""" + base_dates_query = """select adddate(%(start_date)s, t2.i*100 + t1.i*10 + t0.i) selected_date from + (select 0 i union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t0, + (select 0 i union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t1, + (select 0 i union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t2""" + condition_query = "" + if filter_attendance: + condition_query += """ and a.selected_date not in ( + select attendance_date from `tabAttendance` + where docstatus = 1 and employee = %(employee)s + and attendance_date between %(start_date)s and %(end_date)s)""" + if holiday_list: + condition_query += """ and a.selected_date not in ( + select holiday_date from `tabHoliday` where parenttype = 'Holiday List' and + parentfield = 'holidays' and parent = %(holiday_list)s + and holiday_date between %(start_date)s and %(end_date)s)""" + + dates = frappe.db.sql( + """select * from + ({base_dates_query}) as a + where a.selected_date <= %(end_date)s {condition_query} + """.format( + base_dates_query=base_dates_query, condition_query=condition_query + ), + { + "employee": employee, + "start_date": start_date, + "end_date": end_date, + "holiday_list": holiday_list, + }, + as_list=True, + ) + + return [getdate(date[0]) for date in dates] diff --git a/erpnext/hr/doctype/shift_type/test_shift_type.py b/erpnext/hr/doctype/shift_type/test_shift_type.py index 0d75292a1e..7d2f29cd6c 100644 --- a/erpnext/hr/doctype/shift_type/test_shift_type.py +++ b/erpnext/hr/doctype/shift_type/test_shift_type.py @@ -2,381 +2,7 @@ # See license.txt import unittest -from datetime import datetime, timedelta -import frappe -from frappe.tests.utils import FrappeTestCase -from frappe.utils import add_days, get_time, get_year_ending, get_year_start, getdate, now_datetime -from erpnext.hr.doctype.employee.test_employee import make_employee -from erpnext.hr.doctype.holiday_list.test_holiday_list import set_holiday_list -from erpnext.hr.doctype.leave_application.test_leave_application import get_first_sunday -from erpnext.payroll.doctype.salary_slip.test_salary_slip import make_holiday_list - - -class TestShiftType(FrappeTestCase): - def setUp(self): - frappe.db.delete("Shift Type") - frappe.db.delete("Shift Assignment") - frappe.db.delete("Employee Checkin") - frappe.db.delete("Attendance") - - from_date = get_year_start(getdate()) - to_date = get_year_ending(getdate()) - self.holiday_list = make_holiday_list(from_date=from_date, to_date=to_date) - - def test_mark_attendance(self): - from erpnext.hr.doctype.employee_checkin.test_employee_checkin import make_checkin - - employee = make_employee("test_employee_checkin@example.com", company="_Test Company") - - shift_type = setup_shift_type() - date = getdate() - make_shift_assignment(shift_type.name, employee, date) - - timestamp = datetime.combine(date, get_time("08:00:00")) - log_in = make_checkin(employee, timestamp) - self.assertEqual(log_in.shift, shift_type.name) - - timestamp = datetime.combine(date, get_time("12:00:00")) - log_out = make_checkin(employee, timestamp) - self.assertEqual(log_out.shift, shift_type.name) - - shift_type.process_auto_attendance() - - attendance = frappe.db.get_value( - "Attendance", {"shift": shift_type.name}, ["status", "name"], as_dict=True - ) - self.assertEqual(attendance.status, "Present") - - def test_entry_and_exit_grace(self): - from erpnext.hr.doctype.employee_checkin.test_employee_checkin import make_checkin - - employee = make_employee("test_employee_checkin@example.com", company="_Test Company") - - # doesn't mark late entry until 60 mins after shift start i.e. till 9 - # doesn't mark late entry until 60 mins before shift end i.e. 11 - shift_type = setup_shift_type( - enable_entry_grace_period=1, - enable_exit_grace_period=1, - late_entry_grace_period=60, - early_exit_grace_period=60, - ) - date = getdate() - make_shift_assignment(shift_type.name, employee, date) - - timestamp = datetime.combine(date, get_time("09:30:00")) - log_in = make_checkin(employee, timestamp) - self.assertEqual(log_in.shift, shift_type.name) - - timestamp = datetime.combine(date, get_time("10:30:00")) - log_out = make_checkin(employee, timestamp) - self.assertEqual(log_out.shift, shift_type.name) - - shift_type.process_auto_attendance() - - attendance = frappe.db.get_value( - "Attendance", - {"shift": shift_type.name}, - ["status", "name", "late_entry", "early_exit"], - as_dict=True, - ) - self.assertEqual(attendance.status, "Present") - self.assertEqual(attendance.late_entry, 1) - self.assertEqual(attendance.early_exit, 1) - - def test_working_hours_threshold_for_half_day(self): - from erpnext.hr.doctype.employee_checkin.test_employee_checkin import make_checkin - - employee = make_employee("test_employee_checkin@example.com", company="_Test Company") - shift_type = setup_shift_type(shift_type="Half Day Test", working_hours_threshold_for_half_day=2) - date = getdate() - make_shift_assignment(shift_type.name, employee, date) - - timestamp = datetime.combine(date, get_time("08:00:00")) - log_in = make_checkin(employee, timestamp) - self.assertEqual(log_in.shift, shift_type.name) - - timestamp = datetime.combine(date, get_time("09:30:00")) - log_out = make_checkin(employee, timestamp) - self.assertEqual(log_out.shift, shift_type.name) - - shift_type.process_auto_attendance() - - attendance = frappe.db.get_value( - "Attendance", {"shift": shift_type.name}, ["status", "working_hours"], as_dict=True - ) - self.assertEqual(attendance.status, "Half Day") - self.assertEqual(attendance.working_hours, 1.5) - - def test_working_hours_threshold_for_absent(self): - from erpnext.hr.doctype.employee_checkin.test_employee_checkin import make_checkin - - employee = make_employee("test_employee_checkin@example.com", company="_Test Company") - shift_type = setup_shift_type(shift_type="Absent Test", working_hours_threshold_for_absent=2) - date = getdate() - make_shift_assignment(shift_type.name, employee, date) - - timestamp = datetime.combine(date, get_time("08:00:00")) - log_in = make_checkin(employee, timestamp) - self.assertEqual(log_in.shift, shift_type.name) - - timestamp = datetime.combine(date, get_time("09:30:00")) - log_out = make_checkin(employee, timestamp) - self.assertEqual(log_out.shift, shift_type.name) - - shift_type.process_auto_attendance() - - attendance = frappe.db.get_value( - "Attendance", {"shift": shift_type.name}, ["status", "working_hours"], as_dict=True - ) - self.assertEqual(attendance.status, "Absent") - self.assertEqual(attendance.working_hours, 1.5) - - def test_working_hours_threshold_for_absent_and_half_day_1(self): - # considers half day over absent - from erpnext.hr.doctype.employee_checkin.test_employee_checkin import make_checkin - - employee = make_employee("test_employee_checkin@example.com", company="_Test Company") - shift_type = setup_shift_type( - shift_type="Half Day + Absent Test", - working_hours_threshold_for_half_day=1, - working_hours_threshold_for_absent=2, - ) - date = getdate() - make_shift_assignment(shift_type.name, employee, date) - - timestamp = datetime.combine(date, get_time("08:00:00")) - log_in = make_checkin(employee, timestamp) - self.assertEqual(log_in.shift, shift_type.name) - - timestamp = datetime.combine(date, get_time("08:45:00")) - log_out = make_checkin(employee, timestamp) - self.assertEqual(log_out.shift, shift_type.name) - - shift_type.process_auto_attendance() - - attendance = frappe.db.get_value( - "Attendance", {"shift": shift_type.name}, ["status", "working_hours"], as_dict=True - ) - self.assertEqual(attendance.status, "Half Day") - self.assertEqual(attendance.working_hours, 0.75) - - def test_working_hours_threshold_for_absent_and_half_day_2(self): - # considers absent over half day - from erpnext.hr.doctype.employee_checkin.test_employee_checkin import make_checkin - - employee = make_employee("test_employee_checkin@example.com", company="_Test Company") - shift_type = setup_shift_type( - shift_type="Half Day + Absent Test", - working_hours_threshold_for_half_day=1, - working_hours_threshold_for_absent=2, - ) - date = getdate() - make_shift_assignment(shift_type.name, employee, date) - - timestamp = datetime.combine(date, get_time("08:00:00")) - log_in = make_checkin(employee, timestamp) - self.assertEqual(log_in.shift, shift_type.name) - - timestamp = datetime.combine(date, get_time("09:30:00")) - log_out = make_checkin(employee, timestamp) - self.assertEqual(log_out.shift, shift_type.name) - - shift_type.process_auto_attendance() - - attendance = frappe.db.get_value("Attendance", {"shift": shift_type.name}, "status") - self.assertEqual(attendance, "Absent") - - def test_mark_absent_for_dates_with_no_attendance(self): - employee = make_employee("test_employee_checkin@example.com", company="_Test Company") - shift_type = setup_shift_type(shift_type="Test Absent with no Attendance") - - # absentees are auto-marked one day after to wait for any manual attendance records - date = add_days(getdate(), -1) - make_shift_assignment(shift_type.name, employee, date) - - shift_type.process_auto_attendance() - - attendance = frappe.db.get_value( - "Attendance", {"attendance_date": date, "employee": employee}, "status" - ) - self.assertEqual(attendance, "Absent") - - @set_holiday_list("Salary Slip Test Holiday List", "_Test Company") - def test_skip_marking_absent_on_a_holiday(self): - employee = make_employee("test_employee_checkin@example.com", company="_Test Company") - shift_type = setup_shift_type(shift_type="Test Absent with no Attendance") - shift_type.holiday_list = None - shift_type.save() - - # should not mark any attendance if no shift assignment is created - shift_type.process_auto_attendance() - attendance = frappe.db.get_value("Attendance", {"employee": employee}, "status") - self.assertIsNone(attendance) - - first_sunday = get_first_sunday(self.holiday_list, for_date=getdate()) - make_shift_assignment(shift_type.name, employee, first_sunday) - - shift_type.process_auto_attendance() - - attendance = frappe.db.get_value( - "Attendance", {"attendance_date": first_sunday, "employee": employee}, "status" - ) - self.assertIsNone(attendance) - - def test_get_start_and_end_dates(self): - date = getdate() - - doj = add_days(date, -30) - relieving_date = add_days(date, -5) - employee = make_employee( - "test_employee_dates@example.com", - company="_Test Company", - date_of_joining=doj, - relieving_date=relieving_date, - ) - shift_type = setup_shift_type( - shift_type="Test Absent with no Attendance", process_attendance_after=add_days(doj, 2) - ) - - make_shift_assignment(shift_type.name, employee, add_days(date, -25)) - - shift_type.process_auto_attendance() - - # should not mark absent before shift assignment/process attendance after date - attendance = frappe.db.get_value( - "Attendance", {"attendance_date": doj, "employee": employee}, "name" - ) - self.assertIsNone(attendance) - - # mark absent on Relieving Date - attendance = frappe.db.get_value( - "Attendance", {"attendance_date": relieving_date, "employee": employee}, "status" - ) - self.assertEquals(attendance, "Absent") - - # should not mark absent after Relieving Date - attendance = frappe.db.get_value( - "Attendance", {"attendance_date": add_days(relieving_date, 1), "employee": employee}, "name" - ) - self.assertIsNone(attendance) - - def test_skip_auto_attendance_for_duplicate_record(self): - # Skip auto attendance in case of duplicate attendance record - from erpnext.hr.doctype.attendance.attendance import mark_attendance - from erpnext.hr.doctype.employee_checkin.test_employee_checkin import make_checkin - - employee = make_employee("test_employee_checkin@example.com", company="_Test Company") - - shift_type = setup_shift_type() - date = getdate() - - # mark attendance - mark_attendance(employee, date, "Present") - make_shift_assignment(shift_type.name, employee, date) - - timestamp = datetime.combine(date, get_time("08:00:00")) - log_in = make_checkin(employee, timestamp) - self.assertEqual(log_in.shift, shift_type.name) - - timestamp = datetime.combine(date, get_time("12:00:00")) - log_out = make_checkin(employee, timestamp) - self.assertEqual(log_out.shift, shift_type.name) - - # auto attendance should skip marking - shift_type.process_auto_attendance() - - log_in.reload() - log_out.reload() - self.assertEqual(log_in.skip_auto_attendance, 1) - self.assertEqual(log_out.skip_auto_attendance, 1) - - def test_skip_auto_attendance_for_overlapping_shift(self): - # Skip auto attendance in case of overlapping shift attendance record - # this case won't occur in case of shift assignment, since it will not allow overlapping shifts to be assigned - # can happen if manual attendance records are created - from erpnext.hr.doctype.attendance.attendance import mark_attendance - from erpnext.hr.doctype.employee_checkin.test_employee_checkin import make_checkin - - employee = make_employee("test_employee_checkin@example.com", company="_Test Company") - shift_1 = setup_shift_type(shift_type="Shift 1", start_time="08:00:00", end_time="10:00:00") - shift_2 = setup_shift_type(shift_type="Shift 2", start_time="09:30:00", end_time="11:00:00") - - date = getdate() - - # mark attendance - mark_attendance(employee, date, "Present", shift=shift_1.name) - make_shift_assignment(shift_2.name, employee, date) - - timestamp = datetime.combine(date, get_time("09:30:00")) - log_in = make_checkin(employee, timestamp) - self.assertEqual(log_in.shift, shift_2.name) - - timestamp = datetime.combine(date, get_time("11:00:00")) - log_out = make_checkin(employee, timestamp) - self.assertEqual(log_out.shift, shift_2.name) - - # auto attendance should be skipped for shift 2 - # since it is already marked for overlapping shift 1 - shift_2.process_auto_attendance() - - log_in.reload() - log_out.reload() - self.assertEqual(log_in.skip_auto_attendance, 1) - self.assertEqual(log_out.skip_auto_attendance, 1) - - -def setup_shift_type(**args): - args = frappe._dict(args) - date = getdate() - - shift_type = frappe.get_doc( - { - "doctype": "Shift Type", - "__newname": args.shift_type or "_Test Shift", - "start_time": "08:00:00", - "end_time": "12:00:00", - "enable_auto_attendance": 1, - "determine_check_in_and_check_out": "Alternating entries as IN and OUT during the same shift", - "working_hours_calculation_based_on": "First Check-in and Last Check-out", - "begin_check_in_before_shift_start_time": 60, - "allow_check_out_after_shift_end_time": 60, - "process_attendance_after": add_days(date, -2), - "last_sync_of_checkin": now_datetime() + timedelta(days=1), - } - ) - - holiday_list = "Employee Checkin Test Holiday List" - if not frappe.db.exists("Holiday List", "Employee Checkin Test Holiday List"): - holiday_list = frappe.get_doc( - { - "doctype": "Holiday List", - "holiday_list_name": "Employee Checkin Test Holiday List", - "from_date": get_year_start(date), - "to_date": get_year_ending(date), - } - ).insert() - holiday_list = holiday_list.name - - shift_type.holiday_list = holiday_list - shift_type.update(args) - shift_type.save() - - return shift_type - - -def make_shift_assignment(shift_type, employee, start_date, end_date=None): - shift_assignment = frappe.get_doc( - { - "doctype": "Shift Assignment", - "shift_type": shift_type, - "company": "_Test Company", - "employee": employee, - "start_date": start_date, - "end_date": end_date, - } - ).insert() - shift_assignment.submit() - - return shift_assignment +class TestShiftType(unittest.TestCase): + pass diff --git a/erpnext/hr/report/monthly_attendance_sheet/monthly_attendance_sheet.js b/erpnext/hr/report/monthly_attendance_sheet/monthly_attendance_sheet.js index 6f4bbd54fb..42f7cdb50f 100644 --- a/erpnext/hr/report/monthly_attendance_sheet/monthly_attendance_sheet.js +++ b/erpnext/hr/report/monthly_attendance_sheet/monthly_attendance_sheet.js @@ -66,7 +66,8 @@ frappe.query_reports["Monthly Attendance Sheet"] = { "Default": 0, } ], - onload: function() { + + "onload": function() { return frappe.call({ method: "erpnext.hr.report.monthly_attendance_sheet.monthly_attendance_sheet.get_attendance_years", callback: function(r) { @@ -77,25 +78,5 @@ frappe.query_reports["Monthly Attendance Sheet"] = { year_filter.set_input(year_filter.df.default); } }); - }, - formatter: function(value, row, column, data, default_formatter) { - value = default_formatter(value, row, column, data); - const summarized_view = frappe.query_report.get_filter_value('summarized_view'); - const group_by = frappe.query_report.get_filter_value('group_by'); - - if (!summarized_view) { - if ((group_by && column.colIndex > 3) || (!group_by && column.colIndex > 2)) { - if (value == 'P' || value == 'WFH') - value = "" + value + ""; - else if (value == 'A') - value = "" + value + ""; - else if (value == 'HD') - value = "" + value + ""; - else if (value == 'L') - value = "" + value + ""; - } - } - - return value; } } diff --git a/erpnext/hr/report/monthly_attendance_sheet/monthly_attendance_sheet.py b/erpnext/hr/report/monthly_attendance_sheet/monthly_attendance_sheet.py index efd2d382d5..8ea49899f2 100644 --- a/erpnext/hr/report/monthly_attendance_sheet/monthly_attendance_sheet.py +++ b/erpnext/hr/report/monthly_attendance_sheet/monthly_attendance_sheet.py @@ -3,618 +3,365 @@ from calendar import monthrange -from itertools import groupby -from typing import Dict, List, Optional, Tuple import frappe -from frappe import _ -from frappe.query_builder.functions import Count, Extract, Sum +from frappe import _, msgprint from frappe.utils import cint, cstr, getdate -Filters = frappe._dict - status_map = { - "Present": "P", "Absent": "A", "Half Day": "HD", - "Work From Home": "WFH", + "Holiday": "H", + "Weekly Off": "WO", "On Leave": "L", - "Holiday": "H", - "Weekly Off": "WO", + "Present": "P", + "Work From Home": "WFH", } day_abbr = ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"] -def execute(filters: Optional[Filters] = None) -> Tuple: - filters = frappe._dict(filters or {}) +def execute(filters=None): + if not filters: + filters = {} - if not (filters.month and filters.year): - frappe.throw(_("Please select month and year.")) + if filters.hide_year_field == 1: + filters.year = 2020 - attendance_map = get_attendance_map(filters) - if not attendance_map: - frappe.msgprint(_("No attendance records found."), alert=True, indicator="orange") - return [], [], None, None - - columns = get_columns(filters) - data = get_data(filters, attendance_map) - - if not data: - frappe.msgprint( - _("No attendance records found for this criteria."), alert=True, indicator="orange" - ) + conditions, filters = get_conditions(filters) + columns, days = get_columns(filters) + att_map = get_attendance_list(conditions, filters) + if not att_map: return columns, [], None, None - message = get_message() if not filters.summarized_view else "" - chart = get_chart_data(attendance_map, filters) + if filters.group_by: + emp_map, group_by_parameters = get_employee_details(filters.group_by, filters.company) + holiday_list = [] + for parameter in group_by_parameters: + h_list = [ + emp_map[parameter][d]["holiday_list"] + for d in emp_map[parameter] + if emp_map[parameter][d]["holiday_list"] + ] + holiday_list += h_list + else: + emp_map = get_employee_details(filters.group_by, filters.company) + holiday_list = [emp_map[d]["holiday_list"] for d in emp_map if emp_map[d]["holiday_list"]] - return columns, data, message, chart + default_holiday_list = frappe.get_cached_value( + "Company", filters.get("company"), "default_holiday_list" + ) + holiday_list.append(default_holiday_list) + holiday_list = list(set(holiday_list)) + holiday_map = get_holiday(holiday_list, filters["month"]) + + data = [] + + leave_types = frappe.db.get_list("Leave Type") + leave_list = None + if filters.summarized_view: + leave_list = [d.name + ":Float:120" for d in leave_types] + columns.extend(leave_list) + columns.extend([_("Total Late Entries") + ":Float:120", _("Total Early Exits") + ":Float:120"]) + + if filters.group_by: + emp_att_map = {} + for parameter in group_by_parameters: + emp_map_set = set([key for key in emp_map[parameter].keys()]) + att_map_set = set([key for key in att_map.keys()]) + if att_map_set & emp_map_set: + parameter_row = ["" + parameter + ""] + [ + "" for day in range(filters["total_days_in_month"] + 2) + ] + data.append(parameter_row) + record, emp_att_data = add_data( + emp_map[parameter], + att_map, + filters, + holiday_map, + conditions, + default_holiday_list, + leave_types=leave_types, + ) + emp_att_map.update(emp_att_data) + data += record + else: + record, emp_att_map = add_data( + emp_map, + att_map, + filters, + holiday_map, + conditions, + default_holiday_list, + leave_types=leave_types, + ) + data += record + + chart_data = get_chart_data(emp_att_map, days) + + return columns, data, None, chart_data -def get_message() -> str: - message = "" - colors = ["green", "red", "orange", "green", "#318AD8", "", ""] +def get_chart_data(emp_att_map, days): + labels = [] + datasets = [ + {"name": "Absent", "values": []}, + {"name": "Present", "values": []}, + {"name": "Leave", "values": []}, + ] + for idx, day in enumerate(days, start=0): + p = day.replace("::65", "") + labels.append(day.replace("::65", "")) + total_absent_on_day = 0 + total_leave_on_day = 0 + total_present_on_day = 0 + total_holiday = 0 + for emp in emp_att_map.keys(): + if emp_att_map[emp][idx]: + if emp_att_map[emp][idx] == "A": + total_absent_on_day += 1 + if emp_att_map[emp][idx] in ["P", "WFH"]: + total_present_on_day += 1 + if emp_att_map[emp][idx] == "HD": + total_present_on_day += 0.5 + total_leave_on_day += 0.5 + if emp_att_map[emp][idx] == "L": + total_leave_on_day += 1 - count = 0 - for status, abbr in status_map.items(): - message += f""" - - {status} - {abbr} - - """ - count += 1 + datasets[0]["values"].append(total_absent_on_day) + datasets[1]["values"].append(total_present_on_day) + datasets[2]["values"].append(total_leave_on_day) - return message + chart = {"data": {"labels": labels, "datasets": datasets}} + + chart["type"] = "line" + + return chart -def get_columns(filters: Filters) -> List[Dict]: +def add_data( + employee_map, att_map, filters, holiday_map, conditions, default_holiday_list, leave_types=None +): + + record = [] + emp_att_map = {} + for emp in employee_map: + emp_det = employee_map.get(emp) + if not emp_det or emp not in att_map: + continue + + row = [] + if filters.group_by: + row += [" "] + row += [emp, emp_det.employee_name] + + total_p = total_a = total_l = total_h = total_um = 0.0 + emp_status_map = [] + for day in range(filters["total_days_in_month"]): + status = None + status = att_map.get(emp).get(day + 1) + + if status is None and holiday_map: + emp_holiday_list = emp_det.holiday_list if emp_det.holiday_list else default_holiday_list + + if emp_holiday_list in holiday_map: + for idx, ele in enumerate(holiday_map[emp_holiday_list]): + if day + 1 == holiday_map[emp_holiday_list][idx][0]: + if holiday_map[emp_holiday_list][idx][1]: + status = "Weekly Off" + else: + status = "Holiday" + total_h += 1 + + abbr = status_map.get(status, "") + emp_status_map.append(abbr) + + if filters.summarized_view: + if status == "Present" or status == "Work From Home": + total_p += 1 + elif status == "Absent": + total_a += 1 + elif status == "On Leave": + total_l += 1 + elif status == "Half Day": + total_p += 0.5 + total_a += 0.5 + total_l += 0.5 + elif not status: + total_um += 1 + + if not filters.summarized_view: + row += emp_status_map + + if filters.summarized_view: + row += [total_p, total_l, total_a, total_h, total_um] + + if not filters.get("employee"): + filters.update({"employee": emp}) + conditions += " and employee = %(employee)s" + elif not filters.get("employee") == emp: + filters.update({"employee": emp}) + + if filters.summarized_view: + leave_details = frappe.db.sql( + """select leave_type, status, count(*) as count from `tabAttendance`\ + where leave_type is not NULL %s group by leave_type, status""" + % conditions, + filters, + as_dict=1, + ) + + time_default_counts = frappe.db.sql( + """select (select count(*) from `tabAttendance` where \ + late_entry = 1 %s) as late_entry_count, (select count(*) from tabAttendance where \ + early_exit = 1 %s) as early_exit_count""" + % (conditions, conditions), + filters, + ) + + leaves = {} + for d in leave_details: + if d.status == "Half Day": + d.count = d.count * 0.5 + if d.leave_type in leaves: + leaves[d.leave_type] += d.count + else: + leaves[d.leave_type] = d.count + + for d in leave_types: + if d.name in leaves: + row.append(leaves[d.name]) + else: + row.append("0.0") + + row.extend([time_default_counts[0][0], time_default_counts[0][1]]) + emp_att_map[emp] = emp_status_map + record.append(row) + + return record, emp_att_map + + +def get_columns(filters): + columns = [] if filters.group_by: - columns.append( - { - "label": _(filters.group_by), - "fieldname": frappe.scrub(filters.group_by), - "fieldtype": "Link", - "options": "Branch", - "width": 120, - } - ) + columns = [_(filters.group_by) + ":Link/Branch:120"] - columns.extend( - [ - { - "label": _("Employee"), - "fieldname": "employee", - "fieldtype": "Link", - "options": "Employee", - "width": 135, - }, - {"label": _("Employee Name"), "fieldname": "employee_name", "fieldtype": "Data", "width": 120}, - ] - ) + columns += [_("Employee") + ":Link/Employee:120", _("Employee Name") + ":Data/:120"] + days = [] + for day in range(filters["total_days_in_month"]): + date = str(filters.year) + "-" + str(filters.month) + "-" + str(day + 1) + day_name = day_abbr[getdate(date).weekday()] + days.append(cstr(day + 1) + " " + day_name + "::65") + if not filters.summarized_view: + columns += days if filters.summarized_view: - columns.extend( - [ - { - "label": _("Total Present"), - "fieldname": "total_present", - "fieldtype": "Float", - "width": 110, - }, - {"label": _("Total Leaves"), "fieldname": "total_leaves", "fieldtype": "Float", "width": 110}, - {"label": _("Total Absent"), "fieldname": "total_absent", "fieldtype": "Float", "width": 110}, - { - "label": _("Total Holidays"), - "fieldname": "total_holidays", - "fieldtype": "Float", - "width": 120, - }, - { - "label": _("Unmarked Days"), - "fieldname": "unmarked_days", - "fieldtype": "Float", - "width": 130, - }, - ] - ) - columns.extend(get_columns_for_leave_types()) - columns.extend( - [ - { - "label": _("Total Late Entries"), - "fieldname": "total_late_entries", - "fieldtype": "Float", - "width": 140, - }, - { - "label": _("Total Early Exits"), - "fieldname": "total_early_exits", - "fieldtype": "Float", - "width": 140, - }, - ] - ) - else: - columns.append({"label": _("Shift"), "fieldname": "shift", "fieldtype": "Data", "width": 120}) - columns.extend(get_columns_for_days(filters)) - - return columns + columns += [ + _("Total Present") + ":Float:120", + _("Total Leaves") + ":Float:120", + _("Total Absent") + ":Float:120", + _("Total Holidays") + ":Float:120", + _("Unmarked Days") + ":Float:120", + ] + return columns, days -def get_columns_for_leave_types() -> List[Dict]: - leave_types = frappe.db.get_all("Leave Type", pluck="name") - types = [] - for entry in leave_types: - types.append( - {"label": entry, "fieldname": frappe.scrub(entry), "fieldtype": "Float", "width": 120} - ) - - return types - - -def get_columns_for_days(filters: Filters) -> List[Dict]: - total_days = get_total_days_in_month(filters) - days = [] - - for day in range(1, total_days + 1): - # forms the dates from selected year and month from filters - date = "{}-{}-{}".format(cstr(filters.year), cstr(filters.month), cstr(day)) - # gets abbr from weekday number - weekday = day_abbr[getdate(date).weekday()] - # sets days as 1 Mon, 2 Tue, 3 Wed - label = "{} {}".format(cstr(day), weekday) - days.append({"label": label, "fieldtype": "Data", "fieldname": day, "width": 65}) - - return days - - -def get_total_days_in_month(filters: Filters) -> int: - return monthrange(cint(filters.year), cint(filters.month))[1] - - -def get_data(filters: Filters, attendance_map: Dict) -> List[Dict]: - employee_details, group_by_param_values = get_employee_related_details( - filters.group_by, filters.company +def get_attendance_list(conditions, filters): + attendance_list = frappe.db.sql( + """select employee, day(attendance_date) as day_of_month, + status from tabAttendance where docstatus = 1 %s order by employee, attendance_date""" + % conditions, + filters, + as_dict=1, ) - holiday_map = get_holiday_map(filters) - data = [] - if filters.group_by: - group_by_column = frappe.scrub(filters.group_by) - - for value in group_by_param_values: - if not value: - continue - - records = get_rows(employee_details[value], filters, holiday_map, attendance_map) - - if records: - data.append({group_by_column: frappe.bold(value)}) - data.extend(records) - else: - data = get_rows(employee_details, filters, holiday_map, attendance_map) - - return data - - -def get_attendance_map(filters: Filters) -> Dict: - """Returns a dictionary of employee wise attendance map as per shifts for all the days of the month like - { - 'employee1': { - 'Morning Shift': {1: 'Present', 2: 'Absent', ...} - 'Evening Shift': {1: 'Absent', 2: 'Present', ...} - }, - 'employee2': { - 'Afternoon Shift': {1: 'Present', 2: 'Absent', ...} - 'Night Shift': {1: 'Absent', 2: 'Absent', ...} - } - } - """ - Attendance = frappe.qb.DocType("Attendance") - query = ( - frappe.qb.from_(Attendance) - .select( - Attendance.employee, - Extract("day", Attendance.attendance_date).as_("day_of_month"), - Attendance.status, - Attendance.shift, - ) - .where( - (Attendance.docstatus == 1) - & (Attendance.company == filters.company) - & (Extract("month", Attendance.attendance_date) == filters.month) - & (Extract("year", Attendance.attendance_date) == filters.year) - ) - ) - if filters.employee: - query = query.where(Attendance.employee == filters.employee) - query = query.orderby(Attendance.employee, Attendance.attendance_date) - - attendance_list = query.run(as_dict=1) - attendance_map = {} + if not attendance_list: + msgprint(_("No attendance record found"), alert=True, indicator="orange") + att_map = {} for d in attendance_list: - attendance_map.setdefault(d.employee, frappe._dict()).setdefault(d.shift, frappe._dict()) - attendance_map[d.employee][d.shift][d.day_of_month] = d.status + att_map.setdefault(d.employee, frappe._dict()).setdefault(d.day_of_month, "") + att_map[d.employee][d.day_of_month] = d.status - return attendance_map + return att_map -def get_employee_related_details(group_by: str, company: str) -> Tuple[Dict, List]: - """Returns - 1. nested dict for employee details - 2. list of values for the group by filter - """ - Employee = frappe.qb.DocType("Employee") - query = ( - frappe.qb.from_(Employee) - .select( - Employee.name, - Employee.employee_name, - Employee.designation, - Employee.grade, - Employee.department, - Employee.branch, - Employee.company, - Employee.holiday_list, - ) - .where(Employee.company == company) +def get_conditions(filters): + if not (filters.get("month") and filters.get("year")): + msgprint(_("Please select month and year"), raise_exception=1) + + filters["total_days_in_month"] = monthrange(cint(filters.year), cint(filters.month))[1] + + conditions = " and month(attendance_date) = %(month)s and year(attendance_date) = %(year)s" + + if filters.get("company"): + conditions += " and company = %(company)s" + if filters.get("employee"): + conditions += " and employee = %(employee)s" + + return conditions, filters + + +def get_employee_details(group_by, company): + emp_map = {} + query = """select name, employee_name, designation, department, branch, company, + holiday_list from `tabEmployee` where company = %s """ % frappe.db.escape( + company ) if group_by: group_by = group_by.lower() - query = query.orderby(group_by) + query += " order by " + group_by + " ASC" - employee_details = query.run(as_dict=True) - - group_by_param_values = [] - emp_map = {} + employee_details = frappe.db.sql(query, as_dict=1) + group_by_parameters = [] if group_by: - for parameter, employees in groupby(employee_details, key=lambda d: d[group_by]): - group_by_param_values.append(parameter) - emp_map.setdefault(parameter, frappe._dict()) - for emp in employees: - emp_map[parameter][emp.name] = emp + group_by_parameters = list( + set(detail.get(group_by, "") for detail in employee_details if detail.get(group_by, "")) + ) + for parameter in group_by_parameters: + emp_map[parameter] = {} + + for d in employee_details: + if group_by and len(group_by_parameters): + if d.get(group_by, None): + + emp_map[d.get(group_by)][d.name] = d + else: + emp_map[d.name] = d + + if not group_by: + return emp_map else: - for emp in employee_details: - emp_map[emp.name] = emp - - return emp_map, group_by_param_values + return emp_map, group_by_parameters -def get_holiday_map(filters: Filters) -> Dict[str, List[Dict]]: - """ - Returns a dict of holidays falling in the filter month and year - with list name as key and list of holidays as values like - { - 'Holiday List 1': [ - {'day_of_month': '0' , 'weekly_off': 1}, - {'day_of_month': '1', 'weekly_off': 0} - ], - 'Holiday List 2': [ - {'day_of_month': '0' , 'weekly_off': 1}, - {'day_of_month': '1', 'weekly_off': 0} - ] - } - """ - # add default holiday list too - holiday_lists = frappe.db.get_all("Holiday List", pluck="name") - default_holiday_list = frappe.get_cached_value("Company", filters.company, "default_holiday_list") - holiday_lists.append(default_holiday_list) - +def get_holiday(holiday_list, month): holiday_map = frappe._dict() - Holiday = frappe.qb.DocType("Holiday") - - for d in holiday_lists: - if not d: - continue - - holidays = ( - frappe.qb.from_(Holiday) - .select(Extract("day", Holiday.holiday_date).as_("day_of_month"), Holiday.weekly_off) - .where( - (Holiday.parent == d) - & (Extract("month", Holiday.holiday_date) == filters.month) - & (Extract("year", Holiday.holiday_date) == filters.year) + for d in holiday_list: + if d: + holiday_map.setdefault( + d, + frappe.db.sql( + """select day(holiday_date), weekly_off from `tabHoliday` + where parent=%s and month(holiday_date)=%s""", + (d, month), + ), ) - ).run(as_dict=True) - - holiday_map.setdefault(d, holidays) return holiday_map -def get_rows( - employee_details: Dict, filters: Filters, holiday_map: Dict, attendance_map: Dict -) -> List[Dict]: - records = [] - default_holiday_list = frappe.get_cached_value("Company", filters.company, "default_holiday_list") - - for employee, details in employee_details.items(): - emp_holiday_list = details.holiday_list or default_holiday_list - holidays = holiday_map.get(emp_holiday_list) - - if filters.summarized_view: - attendance = get_attendance_status_for_summarized_view(employee, filters, holidays) - if not attendance: - continue - - leave_summary = get_leave_summary(employee, filters) - entry_exits_summary = get_entry_exits_summary(employee, filters) - - row = {"employee": employee, "employee_name": details.employee_name} - set_defaults_for_summarized_view(filters, row) - row.update(attendance) - row.update(leave_summary) - row.update(entry_exits_summary) - - records.append(row) - else: - employee_attendance = attendance_map.get(employee) - if not employee_attendance: - continue - - attendance_for_employee = get_attendance_status_for_detailed_view( - employee, filters, employee_attendance, holidays - ) - # set employee details in the first row - attendance_for_employee[0].update( - {"employee": employee, "employee_name": details.employee_name} - ) - - records.extend(attendance_for_employee) - - return records - - -def set_defaults_for_summarized_view(filters, row): - for entry in get_columns(filters): - if entry.get("fieldtype") == "Float": - row[entry.get("fieldname")] = 0.0 - - -def get_attendance_status_for_summarized_view( - employee: str, filters: Filters, holidays: List -) -> Dict: - """Returns dict of attendance status for employee like - {'total_present': 1.5, 'total_leaves': 0.5, 'total_absent': 13.5, 'total_holidays': 8, 'unmarked_days': 5} - """ - summary, attendance_days = get_attendance_summary_and_days(employee, filters) - if not any(summary.values()): - return {} - - total_days = get_total_days_in_month(filters) - total_holidays = total_unmarked_days = 0 - - for day in range(1, total_days + 1): - if day in attendance_days: - continue - - status = get_holiday_status(day, holidays) - if status in ["Weekly Off", "Holiday"]: - total_holidays += 1 - elif not status: - total_unmarked_days += 1 - - return { - "total_present": summary.total_present + summary.total_half_days, - "total_leaves": summary.total_leaves + summary.total_half_days, - "total_absent": summary.total_absent + summary.total_half_days, - "total_holidays": total_holidays, - "unmarked_days": total_unmarked_days, - } - - -def get_attendance_summary_and_days(employee: str, filters: Filters) -> Tuple[Dict, List]: - Attendance = frappe.qb.DocType("Attendance") - - present_case = ( - frappe.qb.terms.Case() - .when(((Attendance.status == "Present") | (Attendance.status == "Work From Home")), 1) - .else_(0) - ) - sum_present = Sum(present_case).as_("total_present") - - absent_case = frappe.qb.terms.Case().when(Attendance.status == "Absent", 1).else_(0) - sum_absent = Sum(absent_case).as_("total_absent") - - leave_case = frappe.qb.terms.Case().when(Attendance.status == "On Leave", 1).else_(0) - sum_leave = Sum(leave_case).as_("total_leaves") - - half_day_case = frappe.qb.terms.Case().when(Attendance.status == "Half Day", 0.5).else_(0) - sum_half_day = Sum(half_day_case).as_("total_half_days") - - summary = ( - frappe.qb.from_(Attendance) - .select( - sum_present, - sum_absent, - sum_leave, - sum_half_day, - ) - .where( - (Attendance.docstatus == 1) - & (Attendance.employee == employee) - & (Attendance.company == filters.company) - & (Extract("month", Attendance.attendance_date) == filters.month) - & (Extract("year", Attendance.attendance_date) == filters.year) - ) - ).run(as_dict=True) - - days = ( - frappe.qb.from_(Attendance) - .select(Extract("day", Attendance.attendance_date).as_("day_of_month")) - .distinct() - .where( - (Attendance.docstatus == 1) - & (Attendance.employee == employee) - & (Attendance.company == filters.company) - & (Extract("month", Attendance.attendance_date) == filters.month) - & (Extract("year", Attendance.attendance_date) == filters.year) - ) - ).run(pluck=True) - - return summary[0], days - - -def get_attendance_status_for_detailed_view( - employee: str, filters: Filters, employee_attendance: Dict, holidays: List -) -> List[Dict]: - """Returns list of shift-wise attendance status for employee - [ - {'shift': 'Morning Shift', 1: 'A', 2: 'P', 3: 'A'....}, - {'shift': 'Evening Shift', 1: 'P', 2: 'A', 3: 'P'....} - ] - """ - total_days = get_total_days_in_month(filters) - attendance_values = [] - - for shift, status_dict in employee_attendance.items(): - row = {"shift": shift} - - for day in range(1, total_days + 1): - status = status_dict.get(day) - if status is None and holidays: - status = get_holiday_status(day, holidays) - - abbr = status_map.get(status, "") - row[day] = abbr - - attendance_values.append(row) - - return attendance_values - - -def get_holiday_status(day: int, holidays: List) -> str: - status = None - for holiday in holidays: - if day == holiday.get("day_of_month"): - if holiday.get("weekly_off"): - status = "Weekly Off" - else: - status = "Holiday" - break - return status - - -def get_leave_summary(employee: str, filters: Filters) -> Dict[str, float]: - """Returns a dict of leave type and corresponding leaves taken by employee like: - {'leave_without_pay': 1.0, 'sick_leave': 2.0} - """ - Attendance = frappe.qb.DocType("Attendance") - day_case = frappe.qb.terms.Case().when(Attendance.status == "Half Day", 0.5).else_(1) - sum_leave_days = Sum(day_case).as_("leave_days") - - leave_details = ( - frappe.qb.from_(Attendance) - .select(Attendance.leave_type, sum_leave_days) - .where( - (Attendance.employee == employee) - & (Attendance.docstatus == 1) - & (Attendance.company == filters.company) - & ((Attendance.leave_type.isnotnull()) | (Attendance.leave_type != "")) - & (Extract("month", Attendance.attendance_date) == filters.month) - & (Extract("year", Attendance.attendance_date) == filters.year) - ) - .groupby(Attendance.leave_type) - ).run(as_dict=True) - - leaves = {} - for d in leave_details: - leave_type = frappe.scrub(d.leave_type) - leaves[leave_type] = d.leave_days - - return leaves - - -def get_entry_exits_summary(employee: str, filters: Filters) -> Dict[str, float]: - """Returns total late entries and total early exits for employee like: - {'total_late_entries': 5, 'total_early_exits': 2} - """ - Attendance = frappe.qb.DocType("Attendance") - - late_entry_case = frappe.qb.terms.Case().when(Attendance.late_entry == "1", "1") - count_late_entries = Count(late_entry_case).as_("total_late_entries") - - early_exit_case = frappe.qb.terms.Case().when(Attendance.early_exit == "1", "1") - count_early_exits = Count(early_exit_case).as_("total_early_exits") - - entry_exits = ( - frappe.qb.from_(Attendance) - .select(count_late_entries, count_early_exits) - .where( - (Attendance.docstatus == 1) - & (Attendance.employee == employee) - & (Attendance.company == filters.company) - & (Extract("month", Attendance.attendance_date) == filters.month) - & (Extract("year", Attendance.attendance_date) == filters.year) - ) - ).run(as_dict=True) - - return entry_exits[0] - - @frappe.whitelist() -def get_attendance_years() -> str: - """Returns all the years for which attendance records exist""" - Attendance = frappe.qb.DocType("Attendance") - year_list = ( - frappe.qb.from_(Attendance) - .select(Extract("year", Attendance.attendance_date).as_("year")) - .distinct() - ).run(as_dict=True) - - if year_list: - year_list.sort(key=lambda d: d.year, reverse=True) - else: +def get_attendance_years(): + year_list = frappe.db.sql_list( + """select distinct YEAR(attendance_date) from tabAttendance ORDER BY YEAR(attendance_date) DESC""" + ) + if not year_list: year_list = [getdate().year] - return "\n".join(cstr(entry.year) for entry in year_list) - - -def get_chart_data(attendance_map: Dict, filters: Filters) -> Dict: - days = get_columns_for_days(filters) - labels = [] - absent = [] - present = [] - leave = [] - - for day in days: - labels.append(day["label"]) - total_absent_on_day = total_leaves_on_day = total_present_on_day = 0 - - for employee, attendance_dict in attendance_map.items(): - for shift, attendance in attendance_dict.items(): - attendance_on_day = attendance.get(day["fieldname"]) - - if attendance_on_day == "Absent": - total_absent_on_day += 1 - elif attendance_on_day in ["Present", "Work From Home"]: - total_present_on_day += 1 - elif attendance_on_day == "Half Day": - total_present_on_day += 0.5 - total_leaves_on_day += 0.5 - elif attendance_on_day == "On Leave": - total_leaves_on_day += 1 - - absent.append(total_absent_on_day) - present.append(total_present_on_day) - leave.append(total_leaves_on_day) - - return { - "data": { - "labels": labels, - "datasets": [ - {"name": "Absent", "values": absent}, - {"name": "Present", "values": present}, - {"name": "Leave", "values": leave}, - ], - }, - "type": "line", - "colors": ["red", "green", "blue"], - } + return "\n".join(str(year) for year in year_list) diff --git a/erpnext/hr/report/monthly_attendance_sheet/test_monthly_attendance_sheet.py b/erpnext/hr/report/monthly_attendance_sheet/test_monthly_attendance_sheet.py index cde7dd3fff..91da08eee5 100644 --- a/erpnext/hr/report/monthly_attendance_sheet/test_monthly_attendance_sheet.py +++ b/erpnext/hr/report/monthly_attendance_sheet/test_monthly_attendance_sheet.py @@ -1,32 +1,18 @@ import frappe from dateutil.relativedelta import relativedelta from frappe.tests.utils import FrappeTestCase -from frappe.utils import get_year_ending, get_year_start, getdate, now_datetime +from frappe.utils import now_datetime from erpnext.hr.doctype.attendance.attendance import mark_attendance from erpnext.hr.doctype.employee.test_employee import make_employee -from erpnext.hr.doctype.holiday_list.test_holiday_list import set_holiday_list -from erpnext.hr.doctype.leave_application.test_leave_application import make_allocation_record from erpnext.hr.report.monthly_attendance_sheet.monthly_attendance_sheet import execute -from erpnext.payroll.doctype.salary_slip.test_salary_slip import ( - make_holiday_list, - make_leave_application, -) - -test_dependencies = ["Shift Type"] class TestMonthlyAttendanceSheet(FrappeTestCase): def setUp(self): - self.employee = make_employee("test_employee@example.com", company="_Test Company") - frappe.db.delete("Attendance") + self.employee = make_employee("test_employee@example.com") + frappe.db.delete("Attendance", {"employee": self.employee}) - date = getdate() - from_date = get_year_start(date) - to_date = get_year_ending(date) - make_holiday_list(from_date=from_date, to_date=to_date) - - @set_holiday_list("Salary Slip Test Holiday List", "_Test Company") def test_monthly_attendance_sheet_report(self): now = now_datetime() previous_month = now.month - 1 @@ -47,203 +33,14 @@ class TestMonthlyAttendanceSheet(FrappeTestCase): } ) report = execute(filters=filters) - - record = report[1][0] + employees = report[1][0] datasets = report[3]["data"]["datasets"] absent = datasets[0]["values"] present = datasets[1]["values"] leaves = datasets[2]["values"] - # ensure correct attendance is reflected on the report - self.assertEqual(self.employee, record.get("employee")) + # ensure correct attendance is reflect on the report + self.assertIn(self.employee, employees) self.assertEqual(absent[0], 1) self.assertEqual(present[1], 1) self.assertEqual(leaves[2], 1) - - @set_holiday_list("Salary Slip Test Holiday List", "_Test Company") - def test_monthly_attendance_sheet_with_detailed_view(self): - now = now_datetime() - previous_month = now.month - 1 - previous_month_first = now.replace(day=1).replace(month=previous_month).date() - - company = frappe.db.get_value("Employee", self.employee, "company") - - # attendance with shift - mark_attendance(self.employee, previous_month_first, "Absent", "Day Shift") - mark_attendance( - self.employee, previous_month_first + relativedelta(days=1), "Present", "Day Shift" - ) - - # attendance without shift - mark_attendance(self.employee, previous_month_first + relativedelta(days=2), "On Leave") - mark_attendance(self.employee, previous_month_first + relativedelta(days=3), "Present") - - filters = frappe._dict( - { - "month": previous_month, - "year": now.year, - "company": company, - } - ) - report = execute(filters=filters) - - day_shift_row = report[1][0] - row_without_shift = report[1][1] - - self.assertEqual(day_shift_row["shift"], "Day Shift") - self.assertEqual(day_shift_row[1], "A") # absent on the 1st day of the month - self.assertEqual(day_shift_row[2], "P") # present on the 2nd day - - self.assertEqual(row_without_shift["shift"], None) - self.assertEqual(row_without_shift[3], "L") # on leave on the 3rd day - self.assertEqual(row_without_shift[4], "P") # present on the 4th day - - @set_holiday_list("Salary Slip Test Holiday List", "_Test Company") - def test_monthly_attendance_sheet_with_summarized_view(self): - now = now_datetime() - previous_month = now.month - 1 - previous_month_first = now.replace(day=1).replace(month=previous_month).date() - - company = frappe.db.get_value("Employee", self.employee, "company") - - # attendance with shift - mark_attendance(self.employee, previous_month_first, "Absent", "Day Shift") - mark_attendance( - self.employee, previous_month_first + relativedelta(days=1), "Present", "Day Shift" - ) - mark_attendance( - self.employee, previous_month_first + relativedelta(days=2), "Half Day" - ) # half day - - mark_attendance( - self.employee, previous_month_first + relativedelta(days=3), "Present" - ) # attendance without shift - mark_attendance( - self.employee, previous_month_first + relativedelta(days=4), "Present", late_entry=1 - ) # late entry - mark_attendance( - self.employee, previous_month_first + relativedelta(days=5), "Present", early_exit=1 - ) # early exit - - leave_application = get_leave_application(self.employee) - - filters = frappe._dict( - {"month": previous_month, "year": now.year, "company": company, "summarized_view": 1} - ) - report = execute(filters=filters) - - row = report[1][0] - self.assertEqual(row["employee"], self.employee) - - # 4 present + half day absent 0.5 - self.assertEqual(row["total_present"], 4.5) - # 1 present + half day absent 0.5 - self.assertEqual(row["total_absent"], 1.5) - # leave days + half day leave 0.5 - self.assertEqual(row["total_leaves"], leave_application.total_leave_days + 0.5) - - self.assertEqual(row["_test_leave_type"], leave_application.total_leave_days) - self.assertEqual(row["total_late_entries"], 1) - self.assertEqual(row["total_early_exits"], 1) - - @set_holiday_list("Salary Slip Test Holiday List", "_Test Company") - def test_attendance_with_group_by_filter(self): - now = now_datetime() - previous_month = now.month - 1 - previous_month_first = now.replace(day=1).replace(month=previous_month).date() - - company = frappe.db.get_value("Employee", self.employee, "company") - - # attendance with shift - mark_attendance(self.employee, previous_month_first, "Absent", "Day Shift") - mark_attendance( - self.employee, previous_month_first + relativedelta(days=1), "Present", "Day Shift" - ) - - # attendance without shift - mark_attendance(self.employee, previous_month_first + relativedelta(days=2), "On Leave") - mark_attendance(self.employee, previous_month_first + relativedelta(days=3), "Present") - - filters = frappe._dict( - {"month": previous_month, "year": now.year, "company": company, "group_by": "Department"} - ) - report = execute(filters=filters) - - department = frappe.db.get_value("Employee", self.employee, "department") - department_row = report[1][0] - self.assertIn(department, department_row["department"]) - - day_shift_row = report[1][1] - row_without_shift = report[1][2] - - self.assertEqual(day_shift_row["shift"], "Day Shift") - self.assertEqual(day_shift_row[1], "A") # absent on the 1st day of the month - self.assertEqual(day_shift_row[2], "P") # present on the 2nd day - - self.assertEqual(row_without_shift["shift"], None) - self.assertEqual(row_without_shift[3], "L") # on leave on the 3rd day - self.assertEqual(row_without_shift[4], "P") # present on the 4th day - - def test_attendance_with_employee_filter(self): - now = now_datetime() - previous_month = now.month - 1 - previous_month_first = now.replace(day=1).replace(month=previous_month).date() - - company = frappe.db.get_value("Employee", self.employee, "company") - - # mark different attendance status on first 3 days of previous month - mark_attendance(self.employee, previous_month_first, "Absent") - mark_attendance(self.employee, previous_month_first + relativedelta(days=1), "Present") - mark_attendance(self.employee, previous_month_first + relativedelta(days=2), "On Leave") - - filters = frappe._dict( - {"month": previous_month, "year": now.year, "company": company, "employee": self.employee} - ) - report = execute(filters=filters) - - record = report[1][0] - datasets = report[3]["data"]["datasets"] - absent = datasets[0]["values"] - present = datasets[1]["values"] - leaves = datasets[2]["values"] - - # ensure correct attendance is reflected on the report - self.assertEqual(self.employee, record.get("employee")) - self.assertEqual(absent[0], 1) - self.assertEqual(present[1], 1) - self.assertEqual(leaves[2], 1) - - @set_holiday_list("Salary Slip Test Holiday List", "_Test Company") - def test_validations(self): - # validation error for filters without month and year - self.assertRaises(frappe.ValidationError, execute_report_with_invalid_filters) - - # execute report without attendance record - now = now_datetime() - previous_month = now.month - 1 - - company = frappe.db.get_value("Employee", self.employee, "company") - filters = frappe._dict( - {"month": previous_month, "year": now.year, "company": company, "group_by": "Department"} - ) - report = execute(filters=filters) - self.assertEqual(report, ([], [], None, None)) - - -def get_leave_application(employee): - now = now_datetime() - previous_month = now.month - 1 - - date = getdate() - year_start = getdate(get_year_start(date)) - year_end = getdate(get_year_ending(date)) - make_allocation_record(employee=employee, from_date=year_start, to_date=year_end) - - from_date = now.replace(day=7).replace(month=previous_month).date() - to_date = now.replace(day=8).replace(month=previous_month).date() - return make_leave_application(employee, from_date, to_date, "_Test Leave Type") - - -def execute_report_with_invalid_filters(): - filters = frappe._dict({"company": "_Test Company", "group_by": "Department"}) - execute(filters=filters)