From 3c35c9b6aee0f7e199aa78837f3d11f225b5a2ae Mon Sep 17 00:00:00 2001 From: hrzzz Date: Sat, 14 May 2022 10:45:26 -0300 Subject: [PATCH 001/105] fix: correction of the calculation to the average value when there is a discount on the document and not on the items --- .../doctype/authorization_control/authorization_control.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/erpnext/setup/doctype/authorization_control/authorization_control.py b/erpnext/setup/doctype/authorization_control/authorization_control.py index 309658d2601..cfe3d62b8c8 100644 --- a/erpnext/setup/doctype/authorization_control/authorization_control.py +++ b/erpnext/setup/doctype/authorization_control/authorization_control.py @@ -135,8 +135,8 @@ class AuthorizationControl(TransactionBase): price_list_rate, base_rate = 0, 0 for d in doc_obj.get("items"): if d.base_rate: - price_list_rate += flt(d.base_price_list_rate) or flt(d.base_rate) - base_rate += flt(d.base_rate) + price_list_rate += (flt(d.base_price_list_rate) or flt(d.base_rate)) * flt(d.qty) + base_rate += flt(d.base_rate) * flt(d.qty) if doc_obj.get("discount_amount"): base_rate -= flt(doc_obj.discount_amount) From b6e46eea80da79d57bfcebdfbe5831f68b7e60e3 Mon Sep 17 00:00:00 2001 From: marination Date: Wed, 18 May 2022 13:00:00 +0530 Subject: [PATCH 002/105] perf: `get_boms_in_bottom_up_order` - Create child-parent map once and fetch value from child key to get parents - Get parents recursively for a leaf node (get all ancestors) - Approx. 44 secs for 4lakh 70k boms --- erpnext/manufacturing/doctype/bom/bom.py | 92 +++++++++++++++++------- 1 file changed, 67 insertions(+), 25 deletions(-) diff --git a/erpnext/manufacturing/doctype/bom/bom.py b/erpnext/manufacturing/doctype/bom/bom.py index 220ce1dbd89..a828869c36a 100644 --- a/erpnext/manufacturing/doctype/bom/bom.py +++ b/erpnext/manufacturing/doctype/bom/bom.py @@ -1,11 +1,11 @@ -# Copyright (c) 2015, 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 functools import re -from collections import deque +from collections import defaultdict, deque from operator import itemgetter -from typing import List +from typing import List, Optional import frappe from frappe import _ @@ -1130,35 +1130,77 @@ def get_children(parent=None, is_root=False, **filters): return bom_items -def get_boms_in_bottom_up_order(bom_no=None): - def _get_parent(bom_no): - return frappe.db.sql_list( - """ - select distinct bom_item.parent from `tabBOM Item` bom_item - where bom_item.bom_no = %s and bom_item.docstatus=1 and bom_item.parenttype='BOM' - and exists(select bom.name from `tabBOM` bom where bom.name=bom_item.parent and bom.is_active=1) - """, - bom_no, - ) +def get_boms_in_bottom_up_order(bom_no: Optional[str] = None) -> List: + def _generate_child_parent_map(): + bom = frappe.qb.DocType("BOM") + bom_item = frappe.qb.DocType("BOM Item") - count = 0 - bom_list = [] - if bom_no: - bom_list.append(bom_no) - else: - # get all leaf BOMs - bom_list = frappe.db.sql_list( + bom_parents = ( + frappe.qb.from_(bom_item) + .join(bom) + .on(bom_item.parent == bom.name) + .select(bom_item.bom_no, bom_item.parent) + .where( + (bom_item.bom_no.isnotnull()) + & (bom_item.bom_no != "") + & (bom.docstatus == 1) + & (bom.is_active == 1) + & (bom_item.parenttype == "BOM") + ) + ).run(as_dict=True) + + child_parent_map = defaultdict(list) + for bom in bom_parents: + child_parent_map[bom.bom_no].append(bom.parent) + + return child_parent_map + + def _get_flat_parent_map(leaf, child_parent_map): + parents_list = [] + + def _get_parents(node, parents_list): + "Returns updated ancestors list." + first_parents = child_parent_map.get(node) # immediate parents of node + if not first_parents: # top most node + return parents_list + + parents_list.extend(first_parents) + parents_list = list(dict.fromkeys(parents_list).keys()) # remove duplicates + + for nth_node in first_parents: + # recursively find parents + parents_list = _get_parents(nth_node, parents_list) + + return parents_list + + parents_list = _get_parents(leaf, parents_list) + return parents_list + + def _get_leaf_boms(): + return frappe.db.sql_list( """select name from `tabBOM` bom where docstatus=1 and is_active=1 and not exists(select bom_no from `tabBOM Item` where parent=bom.name and ifnull(bom_no, '')!='')""" ) - while count < len(bom_list): - for child_bom in _get_parent(bom_list[count]): - if child_bom not in bom_list: - bom_list.append(child_bom) - count += 1 + bom_list = [] + if bom_no: + bom_list.append(bom_no) + else: + bom_list = _get_leaf_boms() + + child_parent_map = _generate_child_parent_map() + + for leaf_bom in bom_list: + # generate list recursively bottom to top + parent_list = _get_flat_parent_map(leaf_bom, child_parent_map) + + if not parent_list: + continue + + bom_list.extend(parent_list) + bom_list = list(dict.fromkeys(bom_list).keys()) # remove duplicates return bom_list From 81c82c8d535dc6ec4ca84eaf87872a31cf12ec4a Mon Sep 17 00:00:00 2001 From: Rucha Mahabal Date: Wed, 16 Mar 2022 10:31:34 +0530 Subject: [PATCH 003/105] fix(ux): inform the user about salary slip creation/submission happening in the background --- erpnext/payroll/doctype/payroll_entry/payroll_entry.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/erpnext/payroll/doctype/payroll_entry/payroll_entry.py b/erpnext/payroll/doctype/payroll_entry/payroll_entry.py index 54d56f9612f..5937e81fed9 100644 --- a/erpnext/payroll/doctype/payroll_entry/payroll_entry.py +++ b/erpnext/payroll/doctype/payroll_entry/payroll_entry.py @@ -174,9 +174,11 @@ class PayrollEntry(Document): } ) if len(employees) > 30: - frappe.enqueue(create_salary_slips_for_employees, timeout=600, employees=employees, args=args) + frappe.enqueue(create_salary_slips_for_employees, timeout=600, employees=employees, args=args, publish_progress=False) + frappe.msgprint(_("Salary Slip creation has been queued. It may take a few minutes."), + alert=True, indicator="orange") else: - create_salary_slips_for_employees(employees, args, publish_progress=False) + create_salary_slips_for_employees(employees, args, publish_progress=True) # since this method is called via frm.call this doc needs to be updated manually self.reload() @@ -209,6 +211,8 @@ class PayrollEntry(Document): frappe.enqueue( submit_salary_slips_for_employees, timeout=600, payroll_entry=self, salary_slips=ss_list ) + frappe.msgprint(_("Salary Slip submission has been queued. It may take a few minutes."), + alert=True, indicator="orange") else: submit_salary_slips_for_employees(self, ss_list, publish_progress=False) From 5932e9d78a70fc53f9f1c57ed201815f80960384 Mon Sep 17 00:00:00 2001 From: marination Date: Thu, 19 May 2022 20:22:13 +0530 Subject: [PATCH 004/105] fix: DB update child items, remove redundancy, fix perf MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Move `get_boms_in_bottom_up_order` in bom update tool’s file - Remove repeated rm cost update from `update_cost`. `calculate_cost` handles RM cost update - db_update children in `calculate_cost` optionally - Don’t call `update_exploded_items` and regenerate exploded items in `update_cost`. They will stay the same (except cost) --- erpnext/manufacturing/doctype/bom/bom.py | 121 ++---------------- .../bom_update_tool/bom_update_tool.py | 97 +++++++++++++- 2 files changed, 105 insertions(+), 113 deletions(-) diff --git a/erpnext/manufacturing/doctype/bom/bom.py b/erpnext/manufacturing/doctype/bom/bom.py index a828869c36a..047bcc5239c 100644 --- a/erpnext/manufacturing/doctype/bom/bom.py +++ b/erpnext/manufacturing/doctype/bom/bom.py @@ -3,9 +3,9 @@ import functools import re -from collections import defaultdict, deque +from collections import deque from operator import itemgetter -from typing import List, Optional +from typing import List import frappe from frappe import _ @@ -383,35 +383,9 @@ class BOM(WebsiteGenerator): existing_bom_cost = self.total_cost - for d in self.get("items"): - if not d.item_code: - continue - - rate = self.get_rm_rate( - { - "company": self.company, - "item_code": d.item_code, - "bom_no": d.bom_no, - "qty": d.qty, - "uom": d.uom, - "stock_uom": d.stock_uom, - "conversion_factor": d.conversion_factor, - "sourced_by_supplier": d.sourced_by_supplier, - } - ) - - if rate: - d.rate = rate - d.amount = flt(d.rate) * flt(d.qty) - d.base_rate = flt(d.rate) * flt(self.conversion_rate) - d.base_amount = flt(d.amount) * flt(self.conversion_rate) - - if save: - d.db_update() - if self.docstatus == 1: self.flags.ignore_validate_update_after_submit = True - self.calculate_cost(update_hour_rate) + self.calculate_cost(save_updates=save, update_hour_rate=update_hour_rate) if save: self.db_update() @@ -613,11 +587,11 @@ class BOM(WebsiteGenerator): bom_list.reverse() return bom_list - def calculate_cost(self, update_hour_rate=False): + def calculate_cost(self, save_update=False, update_hour_rate=False): """Calculate bom totals""" self.calculate_op_cost(update_hour_rate) - self.calculate_rm_cost() - self.calculate_sm_cost() + self.calculate_rm_cost(save=save_update) + self.calculate_sm_cost(save=save_update) self.total_cost = self.operating_cost + self.raw_material_cost - self.scrap_material_cost self.base_total_cost = ( self.base_operating_cost + self.base_raw_material_cost - self.base_scrap_material_cost @@ -659,7 +633,7 @@ class BOM(WebsiteGenerator): if update_hour_rate: row.db_update() - def calculate_rm_cost(self): + def calculate_rm_cost(self, save=False): """Fetch RM rate as per today's valuation rate and calculate totals""" total_rm_cost = 0 base_total_rm_cost = 0 @@ -674,11 +648,13 @@ class BOM(WebsiteGenerator): total_rm_cost += d.amount base_total_rm_cost += d.base_amount + if save: + d.db_update() self.raw_material_cost = total_rm_cost self.base_raw_material_cost = base_total_rm_cost - def calculate_sm_cost(self): + def calculate_sm_cost(self, save=False): """Fetch RM rate as per today's valuation rate and calculate totals""" total_sm_cost = 0 base_total_sm_cost = 0 @@ -693,6 +669,8 @@ class BOM(WebsiteGenerator): ) total_sm_cost += d.amount base_total_sm_cost += d.base_amount + if save: + d.db_update() self.scrap_material_cost = total_sm_cost self.base_scrap_material_cost = base_total_sm_cost @@ -1130,81 +1108,6 @@ def get_children(parent=None, is_root=False, **filters): return bom_items -def get_boms_in_bottom_up_order(bom_no: Optional[str] = None) -> List: - def _generate_child_parent_map(): - bom = frappe.qb.DocType("BOM") - bom_item = frappe.qb.DocType("BOM Item") - - bom_parents = ( - frappe.qb.from_(bom_item) - .join(bom) - .on(bom_item.parent == bom.name) - .select(bom_item.bom_no, bom_item.parent) - .where( - (bom_item.bom_no.isnotnull()) - & (bom_item.bom_no != "") - & (bom.docstatus == 1) - & (bom.is_active == 1) - & (bom_item.parenttype == "BOM") - ) - ).run(as_dict=True) - - child_parent_map = defaultdict(list) - for bom in bom_parents: - child_parent_map[bom.bom_no].append(bom.parent) - - return child_parent_map - - def _get_flat_parent_map(leaf, child_parent_map): - parents_list = [] - - def _get_parents(node, parents_list): - "Returns updated ancestors list." - first_parents = child_parent_map.get(node) # immediate parents of node - if not first_parents: # top most node - return parents_list - - parents_list.extend(first_parents) - parents_list = list(dict.fromkeys(parents_list).keys()) # remove duplicates - - for nth_node in first_parents: - # recursively find parents - parents_list = _get_parents(nth_node, parents_list) - - return parents_list - - parents_list = _get_parents(leaf, parents_list) - return parents_list - - def _get_leaf_boms(): - return frappe.db.sql_list( - """select name from `tabBOM` bom - where docstatus=1 and is_active=1 - and not exists(select bom_no from `tabBOM Item` - where parent=bom.name and ifnull(bom_no, '')!='')""" - ) - - bom_list = [] - if bom_no: - bom_list.append(bom_no) - else: - bom_list = _get_leaf_boms() - - child_parent_map = _generate_child_parent_map() - - for leaf_bom in bom_list: - # generate list recursively bottom to top - parent_list = _get_flat_parent_map(leaf_bom, child_parent_map) - - if not parent_list: - continue - - bom_list.extend(parent_list) - bom_list = list(dict.fromkeys(bom_list).keys()) # remove duplicates - - return bom_list - - def add_additional_cost(stock_entry, work_order): # Add non stock items cost in the additional cost stock_entry.additional_costs = [] diff --git a/erpnext/manufacturing/doctype/bom_update_tool/bom_update_tool.py b/erpnext/manufacturing/doctype/bom_update_tool/bom_update_tool.py index b0e7da12017..5b073b7539e 100644 --- a/erpnext/manufacturing/doctype/bom_update_tool/bom_update_tool.py +++ b/erpnext/manufacturing/doctype/bom_update_tool/bom_update_tool.py @@ -2,7 +2,8 @@ # For license information, please see license.txt import json -from typing import TYPE_CHECKING, Dict, Literal, Optional, Union +from collections import defaultdict +from typing import TYPE_CHECKING, Dict, List, Literal, Optional, Union if TYPE_CHECKING: from erpnext.manufacturing.doctype.bom_update_log.bom_update_log import BOMUpdateLog @@ -10,8 +11,6 @@ if TYPE_CHECKING: import frappe from frappe.model.document import Document -from erpnext.manufacturing.doctype.bom.bom import get_boms_in_bottom_up_order - class BOMUpdateTool(Document): pass @@ -47,7 +46,10 @@ def update_cost() -> None: """Updates Cost for all BOMs from bottom to top.""" bom_list = get_boms_in_bottom_up_order() for bom in bom_list: - frappe.get_doc("BOM", bom).update_cost(update_parent=False, from_child_bom=True) + bom_doc = frappe.get_doc("BOM", bom) + bom_doc.calculate_cost(save_updates=True, update_hour_rate=True) + # bom_doc.update_exploded_items(save=True) #TODO: edit exploded items rate + bom_doc.db_update() def create_bom_update_log( @@ -67,3 +69,90 @@ def create_bom_update_log( "update_type": update_type, } ).submit() + + +def get_boms_in_bottom_up_order(bom_no: Optional[str] = None) -> List: + """ + Eg: Main BOM + |- Sub BOM 1 + |- Leaf BOM 1 + |- Sub BOM 2 + |- Leaf BOM 2 + Result: [Leaf BOM 1, Leaf BOM 2, Sub BOM 1, Sub BOM 2, Main BOM] + """ + leaf_boms = [] + if bom_no: + leaf_boms.append(bom_no) + else: + leaf_boms = _get_leaf_boms() + + child_parent_map = _generate_child_parent_map() + bom_list = leaf_boms.copy() + + for leaf_bom in leaf_boms: + parent_list = _get_flat_parent_map(leaf_bom, child_parent_map) + + if not parent_list: + continue + + bom_list.extend(parent_list) + bom_list = list(dict.fromkeys(bom_list).keys()) # remove duplicates + + return bom_list + + +def _generate_child_parent_map(): + bom = frappe.qb.DocType("BOM") + bom_item = frappe.qb.DocType("BOM Item") + + bom_parents = ( + frappe.qb.from_(bom_item) + .join(bom) + .on(bom_item.parent == bom.name) + .select(bom_item.bom_no, bom_item.parent) + .where( + (bom_item.bom_no.isnotnull()) + & (bom_item.bom_no != "") + & (bom.docstatus == 1) + & (bom.is_active == 1) + & (bom_item.parenttype == "BOM") + ) + ).run(as_dict=True) + + child_parent_map = defaultdict(list) + for bom in bom_parents: + child_parent_map[bom.bom_no].append(bom.parent) + + return child_parent_map + + +def _get_flat_parent_map(leaf, child_parent_map): + "Get ancestors at all levels of a leaf BOM." + parents_list = [] + + def _get_parents(node, parents_list): + "Returns recursively updated ancestors list." + first_parents = child_parent_map.get(node) # immediate parents of node + if not first_parents: # top most node + return parents_list + + parents_list.extend(first_parents) + parents_list = list(dict.fromkeys(parents_list).keys()) # remove duplicates + + for nth_node in first_parents: + # recursively find parents + parents_list = _get_parents(nth_node, parents_list) + + return parents_list + + parents_list = _get_parents(leaf, parents_list) + return parents_list + + +def _get_leaf_boms(): + return frappe.db.sql_list( + """select name from `tabBOM` bom + where docstatus=1 and is_active=1 + and not exists(select bom_no from `tabBOM Item` + where parent=bom.name and ifnull(bom_no, '')!='')""" + ) From 9dc30830887c3b6e303df6bb92ba1148799afea6 Mon Sep 17 00:00:00 2001 From: marination Date: Thu, 19 May 2022 20:33:48 +0530 Subject: [PATCH 005/105] fix: Call `calculate_cost` for Draft BOM and typo in argument --- erpnext/manufacturing/doctype/bom/bom.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/erpnext/manufacturing/doctype/bom/bom.py b/erpnext/manufacturing/doctype/bom/bom.py index 047bcc5239c..399eb5a0879 100644 --- a/erpnext/manufacturing/doctype/bom/bom.py +++ b/erpnext/manufacturing/doctype/bom/bom.py @@ -385,7 +385,9 @@ class BOM(WebsiteGenerator): if self.docstatus == 1: self.flags.ignore_validate_update_after_submit = True - self.calculate_cost(save_updates=save, update_hour_rate=update_hour_rate) + + self.calculate_cost(save_updates=save, update_hour_rate=update_hour_rate) + if save: self.db_update() @@ -587,11 +589,11 @@ class BOM(WebsiteGenerator): bom_list.reverse() return bom_list - def calculate_cost(self, save_update=False, update_hour_rate=False): + def calculate_cost(self, save_updates=False, update_hour_rate=False): """Calculate bom totals""" self.calculate_op_cost(update_hour_rate) - self.calculate_rm_cost(save=save_update) - self.calculate_sm_cost(save=save_update) + self.calculate_rm_cost(save=save_updates) + self.calculate_sm_cost(save=save_updates) self.total_cost = self.operating_cost + self.raw_material_cost - self.scrap_material_cost self.base_total_cost = ( self.base_operating_cost + self.base_raw_material_cost - self.base_scrap_material_cost From ef8164f188005942409123ca6bc136ad29b3c503 Mon Sep 17 00:00:00 2001 From: Rucha Mahabal Date: Thu, 19 May 2022 20:33:55 +0530 Subject: [PATCH 006/105] refactor: UX for Salary Slip creation and submission via Payroll Entry - Add status for Queued/Failed - log errors and show corrective actions in payroll entry --- .../doctype/payroll_entry/payroll_entry.js | 31 ++- .../doctype/payroll_entry/payroll_entry.json | 50 ++++- .../doctype/payroll_entry/payroll_entry.py | 208 ++++++++++++------ .../payroll_entry/payroll_entry_list.js | 18 ++ 4 files changed, 230 insertions(+), 77 deletions(-) create mode 100644 erpnext/payroll/doctype/payroll_entry/payroll_entry_list.js diff --git a/erpnext/payroll/doctype/payroll_entry/payroll_entry.js b/erpnext/payroll/doctype/payroll_entry/payroll_entry.js index 62e183e59c7..a33f7665bda 100644 --- a/erpnext/payroll/doctype/payroll_entry/payroll_entry.js +++ b/erpnext/payroll/doctype/payroll_entry/payroll_entry.js @@ -64,6 +64,32 @@ frappe.ui.form.on('Payroll Entry', { if (frm.custom_buttons) frm.clear_custom_buttons(); frm.events.add_context_buttons(frm); } + + if (frm.doc.status == "Failed" && frm.doc.error_message) { + const issue = `issue`; + let process = (cint(frm.doc.salary_slips_created)) ? "submission" : "creation"; + + frm.dashboard.set_headline( + __("Salary Slip {0} failed. You can resolve the {1} and retry {0}.", [process, issue]) + ); + + $("#jump_to_error").on("click", (e) => { + e.preventDefault(); + frappe.utils.scroll_to( + frm.get_field("error_message").$wrapper, + true, + 30 + ); + }); + } + + frappe.realtime.on("completed_salary_slip_creation", function() { + frm.reload_doc(); + }); + + frappe.realtime.on("completed_salary_slip_submission", function() { + frm.reload_doc(); + }); }, get_employee_details: function (frm) { @@ -88,7 +114,7 @@ frappe.ui.form.on('Payroll Entry', { doc: frm.doc, method: "create_salary_slips", callback: function () { - frm.refresh(); + frm.reload_doc(); frm.toolbar.refresh(); } }); @@ -97,7 +123,7 @@ frappe.ui.form.on('Payroll Entry', { add_context_buttons: function (frm) { if (frm.doc.salary_slips_submitted || (frm.doc.__onload && frm.doc.__onload.submitted_ss)) { frm.events.add_bank_entry_button(frm); - } else if (frm.doc.salary_slips_created) { + } else if (frm.doc.salary_slips_created && frm.doc.status != 'Queued') { frm.add_custom_button(__("Submit Salary Slip"), function () { submit_salary_slip(frm); }).addClass("btn-primary"); @@ -331,6 +357,7 @@ const submit_salary_slip = function (frm) { method: 'submit_salary_slips', args: {}, callback: function () { + frm.reload_doc(); frm.events.refresh(frm); }, doc: frm.doc, diff --git a/erpnext/payroll/doctype/payroll_entry/payroll_entry.json b/erpnext/payroll/doctype/payroll_entry/payroll_entry.json index 0444134aa4d..17882eb5d94 100644 --- a/erpnext/payroll/doctype/payroll_entry/payroll_entry.json +++ b/erpnext/payroll/doctype/payroll_entry/payroll_entry.json @@ -8,11 +8,11 @@ "engine": "InnoDB", "field_order": [ "section_break0", - "column_break0", "posting_date", "payroll_frequency", "company", "column_break1", + "status", "currency", "exchange_rate", "payroll_payable_account", @@ -41,11 +41,14 @@ "cost_center", "account", "payment_account", - "amended_from", "column_break_33", "bank_account", "salary_slips_created", - "salary_slips_submitted" + "salary_slips_submitted", + "failure_details_section", + "error_message", + "section_break_41", + "amended_from" ], "fields": [ { @@ -53,11 +56,6 @@ "fieldtype": "Section Break", "label": "Select Employees" }, - { - "fieldname": "column_break0", - "fieldtype": "Column Break", - "width": "50%" - }, { "default": "Today", "fieldname": "posting_date", @@ -231,6 +229,7 @@ "fieldtype": "Check", "hidden": 1, "label": "Salary Slips Created", + "no_copy": 1, "read_only": 1 }, { @@ -239,6 +238,7 @@ "fieldtype": "Check", "hidden": 1, "label": "Salary Slips Submitted", + "no_copy": 1, "read_only": 1 }, { @@ -284,15 +284,44 @@ "label": "Payroll Payable Account", "options": "Account", "reqd": 1 + }, + { + "collapsible": 1, + "collapsible_depends_on": "error_message", + "depends_on": "eval:doc.status=='Failed';", + "fieldname": "failure_details_section", + "fieldtype": "Section Break", + "label": "Failure Details" + }, + { + "depends_on": "eval:doc.status=='Failed';", + "fieldname": "error_message", + "fieldtype": "Small Text", + "label": "Error Message", + "no_copy": 1, + "read_only": 1 + }, + { + "fieldname": "section_break_41", + "fieldtype": "Section Break" + }, + { + "fieldname": "status", + "fieldtype": "Select", + "label": "Status", + "options": "Draft\nSubmitted\nCancelled\nQueued\nFailed", + "print_hide": 1, + "read_only": 1 } ], "icon": "fa fa-cog", "is_submittable": 1, "links": [], - "modified": "2020-12-17 15:13:17.766210", + "modified": "2022-03-16 12:45:21.662765", "modified_by": "Administrator", "module": "Payroll", "name": "Payroll Entry", + "naming_rule": "Expression (old style)", "owner": "Administrator", "permissions": [ { @@ -308,5 +337,6 @@ } ], "sort_field": "modified", - "sort_order": "DESC" + "sort_order": "DESC", + "states": [] } \ No newline at end of file diff --git a/erpnext/payroll/doctype/payroll_entry/payroll_entry.py b/erpnext/payroll/doctype/payroll_entry/payroll_entry.py index 5937e81fed9..86be813b91a 100644 --- a/erpnext/payroll/doctype/payroll_entry/payroll_entry.py +++ b/erpnext/payroll/doctype/payroll_entry/payroll_entry.py @@ -1,6 +1,7 @@ # Copyright (c) 2017, Frappe Technologies Pvt. Ltd. and contributors # For license information, please see license.txt +import json import frappe from dateutil.relativedelta import relativedelta @@ -16,6 +17,7 @@ from frappe.utils import ( comma_and, date_diff, flt, + get_link_to_form, getdate, ) @@ -39,8 +41,10 @@ class PayrollEntry(Document): def validate(self): self.number_of_employees = len(self.employees) + self.set_status() def on_submit(self): + self.set_status(update=True) self.create_salary_slips() def before_submit(self): @@ -49,6 +53,15 @@ class PayrollEntry(Document): if self.validate_employee_attendance(): frappe.throw(_("Cannot Submit, Employees left to mark attendance")) + def set_status(self, status=None, update=True): + if not status: + status = {0: "Draft", 1: "Submitted", 2: "Cancelled"}[self.docstatus or 0] + + if update: + self.db_set("status", status) + else: + self.status = status + def validate_employee_details(self): emp_with_sal_slip = [] for employee_details in self.employees: @@ -77,6 +90,7 @@ class PayrollEntry(Document): ) self.db_set("salary_slips_created", 0) self.db_set("salary_slips_submitted", 0) + self.set_status(update=True) def get_emp_list(self): """ @@ -174,11 +188,21 @@ class PayrollEntry(Document): } ) if len(employees) > 30: - frappe.enqueue(create_salary_slips_for_employees, timeout=600, employees=employees, args=args, publish_progress=False) - frappe.msgprint(_("Salary Slip creation has been queued. It may take a few minutes."), - alert=True, indicator="orange") + self.db_set("status", "Queued") + frappe.enqueue( + create_salary_slips_for_employees, + timeout=600, + employees=employees, + args=args, + publish_progress=False, + ) + frappe.msgprint( + _("Salary Slip creation is queued. It may take a few minutes"), + alert=True, + indicator="blue", + ) else: - create_salary_slips_for_employees(employees, args, publish_progress=True) + create_salary_slips_for_employees(employees, args, publish_progress=False) # since this method is called via frm.call this doc needs to be updated manually self.reload() @@ -208,11 +232,19 @@ class PayrollEntry(Document): self.check_permission("write") ss_list = self.get_sal_slip_list(ss_status=0) if len(ss_list) > 30: + self.db_set("status", "Queued") frappe.enqueue( - submit_salary_slips_for_employees, timeout=600, payroll_entry=self, salary_slips=ss_list + submit_salary_slips_for_employees, + timeout=600, + payroll_entry=self, + salary_slips=ss_list, + publish_progress=False, + ) + frappe.msgprint( + _("Salary Slip submission is queued. It may take a few minutes"), + alert=True, + indicator="blue", ) - frappe.msgprint(_("Salary Slip submission has been queued. It may take a few minutes."), - alert=True, indicator="orange") else: submit_salary_slips_for_employees(self, ss_list, publish_progress=False) @@ -227,7 +259,11 @@ class PayrollEntry(Document): ) if not account: - frappe.throw(_("Please set account in Salary Component {0}").format(salary_component)) + frappe.throw( + _("Please set account in Salary Component {0}").format( + get_link_to_form("Salary Component", salary_component) + ) + ) return account @@ -784,37 +820,81 @@ def payroll_entry_has_bank_entries(name): return response +def log_payroll_failure(process, payroll_entry, error): + error_log = frappe.log_error( + title=_("Salary Slip {0} failed for Payroll Entry {1}").format(process, payroll_entry.name) + ) + message_log = frappe.message_log.pop() if frappe.message_log else str(error) + + try: + error_message = json.loads(message_log).get("message") + except Exception: + error_message = message_log + + error_message += "\n" + _("Check Error Log {0} for more details.").format( + get_link_to_form("Error Log", error_log.name) + ) + + payroll_entry.db_set({"error_message": error_message, "status": "Failed"}) + + def create_salary_slips_for_employees(employees, args, publish_progress=True): - salary_slips_exists_for = get_existing_salary_slips(employees, args) - count = 0 - salary_slips_not_created = [] - for emp in employees: - if emp not in salary_slips_exists_for: - args.update({"doctype": "Salary Slip", "employee": emp}) - ss = frappe.get_doc(args) - ss.insert() - count += 1 - if publish_progress: - frappe.publish_progress( - count * 100 / len(set(employees) - set(salary_slips_exists_for)), - title=_("Creating Salary Slips..."), - ) + try: + frappe.db.savepoint("salary_slip_creation") + payroll_entry = frappe.get_doc("Payroll Entry", args.payroll_entry) + salary_slips_exist_for = get_existing_salary_slips(employees, args) + count = 0 - else: - salary_slips_not_created.append(emp) + for emp in employees: + if emp not in salary_slips_exist_for: + args.update({"doctype": "Salary Slip", "employee": emp}) + frappe.get_doc(args).insert() - payroll_entry = frappe.get_doc("Payroll Entry", args.payroll_entry) - payroll_entry.db_set("salary_slips_created", 1) - payroll_entry.notify_update() + count += 1 + if publish_progress: + frappe.publish_progress( + count * 100 / len(set(employees) - set(salary_slips_exist_for)), + title=_("Creating Salary Slips..."), + ) - if salary_slips_not_created: + payroll_entry.db_set({"status": "Submitted", "salary_slips_created": 1}) + + if salary_slips_exist_for: + frappe.msgprint( + _( + "Salary Slips already exist for employees {}, and will not be processed by this payroll." + ).format(frappe.bold(", ".join(emp for emp in salary_slips_exist_for))), + title=_("Message"), + indicator="orange", + ) + + except Exception as e: + frappe.db.rollback(save_point="salary_slip_creation") + log_payroll_failure("creation", payroll_entry, e) + + finally: + frappe.db.commit() + frappe.publish_realtime("completed_salary_slip_creation") + + +def show_payroll_submission_status(submitted, not_submitted, salary_slip): + if not submitted and not not_submitted: frappe.msgprint( _( - "Salary Slips already exists for employees {}, and will not be processed by this payroll." - ).format(frappe.bold(", ".join([emp for emp in salary_slips_not_created]))), - title=_("Message"), - indicator="orange", + "No salary slip found to submit for the above selected criteria OR salary slip already submitted" + ) ) + return + + if submitted: + frappe.msgprint( + _("Salary Slip submitted for period from {0} to {1}").format( + salary_slip.start_date, salary_slip.end_date + ) + ) + + if not_submitted: + frappe.msgprint(_("Could not submit some Salary Slips")) def get_existing_salary_slips(employees, args): @@ -831,45 +911,43 @@ def get_existing_salary_slips(employees, args): def submit_salary_slips_for_employees(payroll_entry, salary_slips, publish_progress=True): - submitted_ss = [] - not_submitted_ss = [] - frappe.flags.via_payroll_entry = True + try: + frappe.db.savepoint("salary_slip_submission") - count = 0 - for ss in salary_slips: - ss_obj = frappe.get_doc("Salary Slip", ss[0]) - if ss_obj.net_pay < 0: - not_submitted_ss.append(ss[0]) - else: - try: - ss_obj.submit() - submitted_ss.append(ss_obj) - except frappe.ValidationError: - not_submitted_ss.append(ss[0]) + submitted = [] + not_submitted = [] + frappe.flags.via_payroll_entry = True + count = 0 - count += 1 - if publish_progress: - frappe.publish_progress(count * 100 / len(salary_slips), title=_("Submitting Salary Slips...")) - if submitted_ss: - payroll_entry.make_accrual_jv_entry() - frappe.msgprint( - _("Salary Slip submitted for period from {0} to {1}").format(ss_obj.start_date, ss_obj.end_date) - ) + for entry in salary_slips: + salary_slip = frappe.get_doc("Salary Slip", entry[0]) + if salary_slip.net_pay < 0: + not_submitted.append(entry[0]) + else: + try: + salary_slip.submit() + submitted.append(salary_slip) + except frappe.ValidationError: + not_submitted.append(entry[0]) - payroll_entry.email_salary_slip(submitted_ss) + count += 1 + if publish_progress: + frappe.publish_progress(count * 100 / len(salary_slips), title=_("Submitting Salary Slips...")) - payroll_entry.db_set("salary_slips_submitted", 1) - payroll_entry.notify_update() + if submitted: + payroll_entry.make_accrual_jv_entry() + payroll_entry.email_salary_slip(submitted) + payroll_entry.db_set({"salary_slips_submitted": 1, "status": "Submitted"}) - if not submitted_ss and not not_submitted_ss: - frappe.msgprint( - _( - "No salary slip found to submit for the above selected criteria OR salary slip already submitted" - ) - ) + show_payroll_submission_status(submitted, not_submitted, salary_slip) - if not_submitted_ss: - frappe.msgprint(_("Could not submit some Salary Slips")) + except Exception as e: + frappe.db.rollback(save_point="salary_slip_submission") + log_payroll_failure("submission", payroll_entry, e) + + finally: + frappe.db.commit() + frappe.publish_realtime("completed_salary_slip_submission") frappe.flags.via_payroll_entry = False diff --git a/erpnext/payroll/doctype/payroll_entry/payroll_entry_list.js b/erpnext/payroll/doctype/payroll_entry/payroll_entry_list.js new file mode 100644 index 00000000000..56390b79d8b --- /dev/null +++ b/erpnext/payroll/doctype/payroll_entry/payroll_entry_list.js @@ -0,0 +1,18 @@ +// Copyright (c) 2015, Frappe Technologies Pvt. Ltd. and Contributors +// License: GNU General Public License v3. See license.txt + +// render +frappe.listview_settings['Payroll Entry'] = { + has_indicator_for_draft: 1, + get_indicator: function(doc) { + var status_color = { + 'Draft': 'red', + 'Submitted': 'blue', + 'Queued': 'orange', + 'Failed': 'red', + 'Cancelled': 'red' + + }; + return [__(doc.status), status_color[doc.status], 'status,=,'+doc.status]; + } +}; From 7d4872aedd254efe85f61d88d98a3285859da11d Mon Sep 17 00:00:00 2001 From: Rucha Mahabal Date: Thu, 19 May 2022 20:35:56 +0530 Subject: [PATCH 007/105] patch: set payroll entry status --- erpnext/patches.txt | 1 + .../patches/v13_0/set_payroll_entry_status.py | 16 ++++++++++++++++ 2 files changed, 17 insertions(+) create mode 100644 erpnext/patches/v13_0/set_payroll_entry_status.py diff --git a/erpnext/patches.txt b/erpnext/patches.txt index 4d9a7e06bfd..e710aa33897 100644 --- a/erpnext/patches.txt +++ b/erpnext/patches.txt @@ -372,3 +372,4 @@ erpnext.patches.v14_0.discount_accounting_separation erpnext.patches.v14_0.delete_employee_transfer_property_doctype erpnext.patches.v13_0.create_accounting_dimensions_in_orders erpnext.patches.v13_0.set_per_billed_in_return_delivery_note +erpnext.patches.v13_0.set_payroll_entry_status diff --git a/erpnext/patches/v13_0/set_payroll_entry_status.py b/erpnext/patches/v13_0/set_payroll_entry_status.py new file mode 100644 index 00000000000..97adff9295f --- /dev/null +++ b/erpnext/patches/v13_0/set_payroll_entry_status.py @@ -0,0 +1,16 @@ +import frappe +from frappe.query_builder import Case + + +def execute(): + PayrollEntry = frappe.qb.DocType("Payroll Entry") + + ( + frappe.qb.update(PayrollEntry).set( + "status", + Case() + .when(PayrollEntry.docstatus == 0, "Draft") + .when(PayrollEntry.docstatus == 1, "Submitted") + .else_("Cancelled"), + ) + ).run() From 9a7e9d902d2b2960034cf2d7ad0d084dee01c1b4 Mon Sep 17 00:00:00 2001 From: marination Date: Thu, 19 May 2022 21:24:31 +0530 Subject: [PATCH 008/105] perf: Use cached doc instead of `get_doc` - Doc is only used to iterate over items(which wont change) and change rate/amount of rows - These changes are inserted in db via `db_update`, so no harm - Tested locally: refetching cached doc after db update, reflects fresh data. --- .../manufacturing/doctype/bom_update_tool/bom_update_tool.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/erpnext/manufacturing/doctype/bom_update_tool/bom_update_tool.py b/erpnext/manufacturing/doctype/bom_update_tool/bom_update_tool.py index 5b073b7539e..e7657253408 100644 --- a/erpnext/manufacturing/doctype/bom_update_tool/bom_update_tool.py +++ b/erpnext/manufacturing/doctype/bom_update_tool/bom_update_tool.py @@ -46,7 +46,7 @@ def update_cost() -> None: """Updates Cost for all BOMs from bottom to top.""" bom_list = get_boms_in_bottom_up_order() for bom in bom_list: - bom_doc = frappe.get_doc("BOM", bom) + bom_doc = frappe.get_cached_doc("BOM", bom) bom_doc.calculate_cost(save_updates=True, update_hour_rate=True) # bom_doc.update_exploded_items(save=True) #TODO: edit exploded items rate bom_doc.db_update() From dd99c00eb64dc16b0f54a0cbf8e2c3b7f0f7e5fe Mon Sep 17 00:00:00 2001 From: marination Date: Thu, 19 May 2022 21:48:24 +0530 Subject: [PATCH 009/105] fix: Get fresh RM rate in `calculate_rm_cost` --- erpnext/manufacturing/doctype/bom/bom.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/erpnext/manufacturing/doctype/bom/bom.py b/erpnext/manufacturing/doctype/bom/bom.py index 399eb5a0879..560019a86d5 100644 --- a/erpnext/manufacturing/doctype/bom/bom.py +++ b/erpnext/manufacturing/doctype/bom/bom.py @@ -641,6 +641,18 @@ class BOM(WebsiteGenerator): base_total_rm_cost = 0 for d in self.get("items"): + d.rate = self.get_rm_rate( + { + "company": self.company, + "item_code": d.item_code, + "bom_no": d.bom_no, + "qty": d.qty, + "uom": d.uom, + "stock_uom": d.stock_uom, + "conversion_factor": d.conversion_factor, + "sourced_by_supplier": d.sourced_by_supplier, + } + ) d.base_rate = flt(d.rate) * flt(self.conversion_rate) d.amount = flt(d.rate, d.precision("rate")) * flt(d.qty, d.precision("qty")) d.base_amount = d.amount * flt(self.conversion_rate) From 90d4dc0cd6428d2befa0805c9903446e668c9ba8 Mon Sep 17 00:00:00 2001 From: marination Date: Fri, 20 May 2022 01:02:56 +0530 Subject: [PATCH 010/105] fix: `test_work_order_with_non_stock_item` - Use the right price list and currency to avoid rate conversion (1000/62.9), since rates are reset correctly now - Use RM rate based on Price List in BOM. Non stock item has no valuation --- .../production_plan/test_production_plan.py | 1 - .../doctype/work_order/test_work_order.py | 15 ++++++++++++--- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/erpnext/manufacturing/doctype/production_plan/test_production_plan.py b/erpnext/manufacturing/doctype/production_plan/test_production_plan.py index 891a4978789..e88049d810d 100644 --- a/erpnext/manufacturing/doctype/production_plan/test_production_plan.py +++ b/erpnext/manufacturing/doctype/production_plan/test_production_plan.py @@ -798,7 +798,6 @@ def make_bom(**args): for item in args.raw_materials: item_doc = frappe.get_doc("Item", item) - bom.append( "items", { diff --git a/erpnext/manufacturing/doctype/work_order/test_work_order.py b/erpnext/manufacturing/doctype/work_order/test_work_order.py index 2aba48231be..27e7e24a823 100644 --- a/erpnext/manufacturing/doctype/work_order/test_work_order.py +++ b/erpnext/manufacturing/doctype/work_order/test_work_order.py @@ -417,7 +417,7 @@ class TestWorkOrder(FrappeTestCase): "doctype": "Item Price", "item_code": "_Test FG Non Stock Item", "price_list_rate": 1000, - "price_list": "Standard Buying", + "price_list": "_Test Price List India", } ).insert(ignore_permissions=True) @@ -426,8 +426,17 @@ class TestWorkOrder(FrappeTestCase): item_code="_Test FG Item", target="_Test Warehouse - _TC", qty=1, basic_rate=100 ) - if not frappe.db.get_value("BOM", {"item": fg_item}): - make_bom(item=fg_item, rate=1000, raw_materials=["_Test FG Item", "_Test FG Non Stock Item"]) + if not frappe.db.get_value("BOM", {"item": fg_item, "docstatus": 1}): + bom = make_bom( + item=fg_item, + rate=1000, + raw_materials=["_Test FG Item", "_Test FG Non Stock Item"], + do_not_save=True, + ) + bom.rm_cost_as_per = "Price List" # non stock item won't have valuation rate + bom.buying_price_list = "_Test Price List India" + bom.currency = "INR" + bom.save() wo = make_wo_order_test_record(production_item=fg_item) From ab2d95a74d8beda1d751f7d795f37058826fff18 Mon Sep 17 00:00:00 2001 From: marination Date: Mon, 23 May 2022 13:00:00 +0530 Subject: [PATCH 011/105] feat: Level-wise BOM cost updation - Process BOMs level wise and Pause after level is complete - Cron job will resume Paused jobs, which will again process the new level and pause at the end - This will go on until all BOMs are updated - Added Progress section with fields to track updated BOMs in Log - Cleanup: Add BOM Updation utils file to contain helper functions/sub-functions - Cleanup: BOM Update Log file will only contain functions that are in direct context of the Log Co-authored-by: Gavin D'souza --- erpnext/hooks.py | 5 +- .../bom_update_log/bom_update_log.json | 29 ++- .../doctype/bom_update_log/bom_update_log.py | 169 ++++++------- .../bom_update_log/bom_updation_utils.py | 223 ++++++++++++++++++ .../bom_update_log/test_bom_update_log.py | 6 +- .../bom_update_tool/bom_update_tool.py | 102 +------- 6 files changed, 335 insertions(+), 199 deletions(-) create mode 100644 erpnext/manufacturing/doctype/bom_update_log/bom_updation_utils.py diff --git a/erpnext/hooks.py b/erpnext/hooks.py index 813ac17ca01..05f06b3bda2 100644 --- a/erpnext/hooks.py +++ b/erpnext/hooks.py @@ -392,9 +392,12 @@ after_migrate = ["erpnext.setup.install.update_select_perm_after_install"] scheduler_events = { "cron": { + "0/5 * * * *": [ + "erpnext.manufacturing.doctype.bom_update_log.bom_update_log.resume_bom_cost_update_jobs", + ], "0/30 * * * *": [ "erpnext.utilities.doctype.video.video.update_youtube_data", - ] + ], }, "all": [ "erpnext.projects.doctype.project.project.project_status_update_reminder", diff --git a/erpnext/manufacturing/doctype/bom_update_log/bom_update_log.json b/erpnext/manufacturing/doctype/bom_update_log/bom_update_log.json index 98c1acb71ce..3455b866573 100644 --- a/erpnext/manufacturing/doctype/bom_update_log/bom_update_log.json +++ b/erpnext/manufacturing/doctype/bom_update_log/bom_update_log.json @@ -13,6 +13,10 @@ "update_type", "status", "error_log", + "progress_section", + "current_boms", + "parent_boms", + "processed_boms", "amended_from" ], "fields": [ @@ -47,7 +51,7 @@ "fieldname": "status", "fieldtype": "Select", "label": "Status", - "options": "Queued\nIn Progress\nCompleted\nFailed" + "options": "Queued\nIn Progress\nPaused\nCompleted\nFailed" }, { "fieldname": "amended_from", @@ -63,13 +67,34 @@ "fieldtype": "Link", "label": "Error Log", "options": "Error Log" + }, + { + "fieldname": "progress_section", + "fieldtype": "Section Break", + "label": "Progress" + }, + { + "fieldname": "current_boms", + "fieldtype": "Text", + "label": "Current BOMs" + }, + { + "description": "Immediate parent BOMs", + "fieldname": "parent_boms", + "fieldtype": "Text", + "label": "Parent BOMs" + }, + { + "fieldname": "processed_boms", + "fieldtype": "Text", + "label": "Processed BOMs" } ], "in_create": 1, "index_web_pages_for_search": 1, "is_submittable": 1, "links": [], - "modified": "2022-03-31 12:51:44.885102", + "modified": "2022-05-23 14:42:14.725914", "modified_by": "Administrator", "module": "Manufacturing", "name": "BOM Update Log", diff --git a/erpnext/manufacturing/doctype/bom_update_log/bom_update_log.py b/erpnext/manufacturing/doctype/bom_update_log/bom_update_log.py index c0770fac90a..639628ac383 100644 --- a/erpnext/manufacturing/doctype/bom_update_log/bom_update_log.py +++ b/erpnext/manufacturing/doctype/bom_update_log/bom_update_log.py @@ -1,13 +1,19 @@ # Copyright (c) 2022, Frappe Technologies Pvt. Ltd. and contributors # For license information, please see license.txt -from typing import Dict, List, Literal, Optional +import json +from typing import Dict, Optional import frappe from frappe import _ from frappe.model.document import Document -from frappe.utils import cstr, flt +from frappe.utils import cstr -from erpnext.manufacturing.doctype.bom_update_tool.bom_update_tool import update_cost +from erpnext.manufacturing.doctype.bom_update_log.bom_updation_utils import ( + get_leaf_boms, + handle_exception, + replace_bom, + set_values_in_log, +) class BOMMissingError(frappe.ValidationError): @@ -49,116 +55,93 @@ class BOMUpdateLog(Document): if self.update_type == "Replace BOM": boms = {"current_bom": self.current_bom, "new_bom": self.new_bom} frappe.enqueue( - method="erpnext.manufacturing.doctype.bom_update_log.bom_update_log.run_bom_job", + method="erpnext.manufacturing.doctype.bom_update_log.bom_update_log.run_replace_bom_job", doc=self, boms=boms, timeout=40000, ) else: - frappe.enqueue( - method="erpnext.manufacturing.doctype.bom_update_log.bom_update_log.run_bom_job", - doc=self, - update_type="Update Cost", - timeout=40000, - ) + process_boms_cost_level_wise(self) -def replace_bom(boms: Dict) -> None: - """Replace current BOM with new BOM in parent BOMs.""" - current_bom = boms.get("current_bom") - new_bom = boms.get("new_bom") - - unit_cost = get_new_bom_unit_cost(new_bom) - update_new_bom_in_bom_items(unit_cost, current_bom, new_bom) - - frappe.cache().delete_key("bom_children") - parent_boms = get_parent_boms(new_bom) - - for bom in parent_boms: - bom_obj = frappe.get_doc("BOM", bom) - # this is only used for versioning and we do not want - # to make separate db calls by using load_doc_before_save - # which proves to be expensive while doing bulk replace - bom_obj._doc_before_save = bom_obj - bom_obj.update_exploded_items() - bom_obj.calculate_cost() - bom_obj.update_parent_cost() - bom_obj.db_update() - if bom_obj.meta.get("track_changes") and not bom_obj.flags.ignore_version: - bom_obj.save_version() - - -def update_new_bom_in_bom_items(unit_cost: float, current_bom: str, new_bom: str) -> None: - bom_item = frappe.qb.DocType("BOM Item") - ( - frappe.qb.update(bom_item) - .set(bom_item.bom_no, new_bom) - .set(bom_item.rate, unit_cost) - .set(bom_item.amount, (bom_item.stock_qty * unit_cost)) - .where( - (bom_item.bom_no == current_bom) & (bom_item.docstatus < 2) & (bom_item.parenttype == "BOM") - ) - ).run() - - -def get_parent_boms(new_bom: str, bom_list: Optional[List] = None) -> List: - bom_list = bom_list or [] - bom_item = frappe.qb.DocType("BOM Item") - - parents = ( - frappe.qb.from_(bom_item) - .select(bom_item.parent) - .where((bom_item.bom_no == new_bom) & (bom_item.docstatus < 2) & (bom_item.parenttype == "BOM")) - .run(as_dict=True) - ) - - for d in parents: - if new_bom == d.parent: - frappe.throw(_("BOM recursion: {0} cannot be child of {1}").format(new_bom, d.parent)) - - bom_list.append(d.parent) - get_parent_boms(d.parent, bom_list) - - return list(set(bom_list)) - - -def get_new_bom_unit_cost(new_bom: str) -> float: - bom = frappe.qb.DocType("BOM") - new_bom_unitcost = ( - frappe.qb.from_(bom).select(bom.total_cost / bom.quantity).where(bom.name == new_bom).run() - ) - - return flt(new_bom_unitcost[0][0]) - - -def run_bom_job( +def run_replace_bom_job( doc: "BOMUpdateLog", boms: Optional[Dict[str, str]] = None, - update_type: Literal["Replace BOM", "Update Cost"] = "Replace BOM", ) -> None: try: doc.db_set("status", "In Progress") + if not frappe.flags.in_test: frappe.db.commit() frappe.db.auto_commit_on_many_writes = 1 - boms = frappe._dict(boms or {}) - - if update_type == "Replace BOM": - replace_bom(boms) - else: - update_cost() + replace_bom(boms) doc.db_set("status", "Completed") - except Exception: - frappe.db.rollback() - error_log = doc.log_error("BOM Update Tool Error") - - doc.db_set("status", "Failed") - doc.db_set("error_log", error_log.name) - + handle_exception(doc) finally: frappe.db.auto_commit_on_many_writes = 0 frappe.db.commit() # nosemgrep + + +def process_boms_cost_level_wise(update_doc: "BOMUpdateLog") -> None: + "Queue jobs at the start of new BOM Level in 'Update Cost' Jobs." + + current_boms, parent_boms = {}, [] + values = {} + + if update_doc.status == "Queued": + # First level yet to process. On Submit. + current_boms = {bom: False for bom in get_leaf_boms()} + values = { + "current_boms": json.dumps(current_boms), + "parent_boms": "[]", + "processed_boms": json.dumps({}), + "status": "In Progress", + } + else: + # status is Paused, resume. via Cron Job. + current_boms, parent_boms = json.loads(update_doc.current_boms), json.loads( + update_doc.parent_boms + ) + if not current_boms: + # Process the next level BOMs. Stage parents as current BOMs. + current_boms = {bom: False for bom in parent_boms} + values = { + "current_boms": json.dumps(current_boms), + "parent_boms": "[]", + "status": "In Progress", + } + + set_values_in_log(update_doc.name, values, commit=True) + queue_bom_cost_jobs(current_boms, update_doc) + + +def queue_bom_cost_jobs(current_boms: Dict, update_doc: "BOMUpdateLog") -> None: + "Queue batches of 20k BOMs of the same level to process parallelly" + current_boms_list = [bom for bom in current_boms] + + while current_boms_list: + boms_to_process = current_boms_list[:20000] # slice out batch of 20k BOMs + + # update list to exclude 20K (queued) BOMs + current_boms_list = current_boms_list[20000:] if len(current_boms_list) > 20000 else [] + frappe.enqueue( + method="erpnext.manufacturing.doctype.bom_update_log.bom_updation_utils.update_cost_in_level", + doc=update_doc, + bom_list=boms_to_process, + timeout=40000, + ) + + +def resume_bom_cost_update_jobs(): + "Called every 10 minutes via Cron job." + paused_jobs = frappe.db.get_all("BOM Update Log", {"status": "Paused"}) + if not paused_jobs: + return + + for job in paused_jobs: + # resume from next level + process_boms_cost_level_wise(update_doc=frappe.get_doc("BOM Update Log", job.name)) diff --git a/erpnext/manufacturing/doctype/bom_update_log/bom_updation_utils.py b/erpnext/manufacturing/doctype/bom_update_log/bom_updation_utils.py new file mode 100644 index 00000000000..b5964cec9d4 --- /dev/null +++ b/erpnext/manufacturing/doctype/bom_update_log/bom_updation_utils.py @@ -0,0 +1,223 @@ +# Copyright (c) 2022, Frappe Technologies Pvt. Ltd. and contributors +# For license information, please see license.txt + +import json +from collections import defaultdict +from typing import TYPE_CHECKING, Dict, List, Optional + +if TYPE_CHECKING: + from erpnext.manufacturing.doctype.bom_update_log.bom_update_log import BOMUpdateLog + +import frappe +from frappe import _ + + +def replace_bom(boms: Dict) -> None: + """Replace current BOM with new BOM in parent BOMs.""" + current_bom = boms.get("current_bom") + new_bom = boms.get("new_bom") + + unit_cost = get_bom_unit_cost(new_bom) + update_new_bom_in_bom_items(unit_cost, current_bom, new_bom) + + frappe.cache().delete_key("bom_children") + parent_boms = get_ancestor_boms(new_bom) + + for bom in parent_boms: + bom_obj = frappe.get_doc("BOM", bom) + # this is only used for versioning and we do not want + # to make separate db calls by using load_doc_before_save + # which proves to be expensive while doing bulk replace + bom_obj._doc_before_save = bom_obj + bom_obj.update_exploded_items() + bom_obj.calculate_cost() + bom_obj.update_parent_cost() + bom_obj.db_update() + if bom_obj.meta.get("track_changes") and not bom_obj.flags.ignore_version: + bom_obj.save_version() + + +def update_cost_in_level(doc: "BOMUpdateLog", bom_list: List[str]) -> None: + "Updates Cost for BOMs within a given level. Runs via background jobs." + try: + status = frappe.db.get_value("BOM Update Log", doc.name, "status") + if status == "Failed": + return + + frappe.db.auto_commit_on_many_writes = 1 + # main updation logic + job_data = update_cost_in_boms(bom_list=bom_list, docname=doc.name) + + set_values_in_log( + doc.name, + values={ + "current_boms": json.dumps(job_data.get("current_boms")), + "processed_boms": json.dumps(job_data.get("processed_boms")), + }, + commit=True, + ) + + process_if_level_is_complete(doc.name, job_data["current_boms"], job_data["processed_boms"]) + except Exception: + handle_exception(doc) + finally: + frappe.db.auto_commit_on_many_writes = 0 + frappe.db.commit() # nosemgrep + + +def get_ancestor_boms(new_bom: str, bom_list: Optional[List] = None) -> List: + bom_list = bom_list or [] + bom_item = frappe.qb.DocType("BOM Item") + + parents = ( + frappe.qb.from_(bom_item) + .select(bom_item.parent) + .where((bom_item.bom_no == new_bom) & (bom_item.docstatus < 2) & (bom_item.parenttype == "BOM")) + .run(as_dict=True) + ) + + for d in parents: + if new_bom == d.parent: + frappe.throw(_("BOM recursion: {0} cannot be child of {1}").format(new_bom, d.parent)) + + bom_list.append(d.parent) + get_ancestor_boms(d.parent, bom_list) + + return list(set(bom_list)) + + +def update_new_bom_in_bom_items(unit_cost: float, current_bom: str, new_bom: str) -> None: + bom_item = frappe.qb.DocType("BOM Item") + ( + frappe.qb.update(bom_item) + .set(bom_item.bom_no, new_bom) + .set(bom_item.rate, unit_cost) + .set(bom_item.amount, (bom_item.stock_qty * unit_cost)) + .where( + (bom_item.bom_no == current_bom) & (bom_item.docstatus < 2) & (bom_item.parenttype == "BOM") + ) + ).run() + + +def get_bom_unit_cost(new_bom: str) -> float: + bom = frappe.qb.DocType("BOM") + new_bom_unitcost = ( + frappe.qb.from_(bom).select(bom.total_cost / bom.quantity).where(bom.name == new_bom).run() + ) + + return frappe.utils.flt(new_bom_unitcost[0][0]) + + +def update_cost_in_boms(bom_list: List[str], docname: str) -> Dict: + "Updates cost in given BOMs. Returns current and total updated BOMs." + updated_boms = {} # current boms that have been updated + + for bom in bom_list: + bom_doc = frappe.get_cached_doc("BOM", bom) + bom_doc.calculate_cost(save_updates=True, update_hour_rate=True) + # bom_doc.update_exploded_items(save=True) #TODO: edit exploded items rate + bom_doc.db_update() + updated_boms[bom] = True + + # Update processed BOMs in Log + log_data = frappe.db.get_values( + "BOM Update Log", docname, ["current_boms", "processed_boms"], as_dict=True + )[0] + + for field in ("current_boms", "processed_boms"): + log_data[field] = json.loads(log_data.get(field)) + log_data[field].update(updated_boms) + + return log_data + + +def process_if_level_is_complete(docname: str, current_boms: Dict, processed_boms: Dict) -> None: + "Prepare and set higher level BOMs in Log if current level is complete." + processing_complete = all(current_boms.get(bom) for bom in current_boms) + if not processing_complete: + return + + parent_boms = get_next_higher_level_boms(child_boms=current_boms, processed_boms=processed_boms) + set_values_in_log( + docname, + values={ + "current_boms": json.dumps({}), + "parent_boms": json.dumps(parent_boms), + "status": "Completed" if not parent_boms else "Paused", + }, + commit=True, + ) + + +def get_next_higher_level_boms(child_boms: Dict, processed_boms: Dict): + "Generate immediate higher level dependants with no unresolved dependencies." + + def _all_children_are_processed(parent): + bom_doc = frappe.get_cached_doc("BOM", parent) + return all(processed_boms.get(row.bom_no) for row in bom_doc.items if row.bom_no) + + dependants_map = _generate_dependants_map() + dependants = set() + for bom in child_boms: + parents = dependants_map.get(bom) or [] + for parent in parents: + if _all_children_are_processed(parent): + dependants.add(parent) + + return list(dependants) + + +def get_leaf_boms(): + return frappe.db.sql_list( + """select name from `tabBOM` bom + where docstatus=1 and is_active=1 + and not exists(select bom_no from `tabBOM Item` + where parent=bom.name and ifnull(bom_no, '')!='')""" + ) + + +def _generate_dependants_map(): + bom = frappe.qb.DocType("BOM") + bom_item = frappe.qb.DocType("BOM Item") + + bom_parents = ( + frappe.qb.from_(bom_item) + .join(bom) + .on(bom_item.parent == bom.name) + .select(bom_item.bom_no, bom_item.parent) + .where( + (bom_item.bom_no.isnotnull()) + & (bom_item.bom_no != "") + & (bom.docstatus == 1) + & (bom.is_active == 1) + & (bom_item.parenttype == "BOM") + ) + ).run(as_dict=True) + + child_parent_map = defaultdict(list) + for bom in bom_parents: + child_parent_map[bom.bom_no].append(bom.parent) + + return child_parent_map + + +def set_values_in_log(log_name: str, values: Dict, commit: bool = False) -> None: + "Update BOM Update Log record." + if not values: + return + + bom_update_log = frappe.qb.DocType("BOM Update Log") + query = frappe.qb.update(bom_update_log).where(bom_update_log.name == log_name) + + for key, value in values.items(): + query = query.set(key, value) + query.run() + + if commit: + frappe.db.commit() + + +def handle_exception(doc: "BOMUpdateLog"): + frappe.db.rollback() + error_log = doc.log_error("BOM Update Tool Error") + set_values_in_log(doc.name, {"status": "Failed", "error_log": error_log.name}) diff --git a/erpnext/manufacturing/doctype/bom_update_log/test_bom_update_log.py b/erpnext/manufacturing/doctype/bom_update_log/test_bom_update_log.py index 47efea961b4..4f151334a2a 100644 --- a/erpnext/manufacturing/doctype/bom_update_log/test_bom_update_log.py +++ b/erpnext/manufacturing/doctype/bom_update_log/test_bom_update_log.py @@ -6,7 +6,7 @@ from frappe.tests.utils import FrappeTestCase from erpnext.manufacturing.doctype.bom_update_log.bom_update_log import ( BOMMissingError, - run_bom_job, + run_replace_bom_job, ) from erpnext.manufacturing.doctype.bom_update_tool.bom_update_tool import enqueue_replace_bom @@ -71,7 +71,7 @@ class TestBOMUpdateLog(FrappeTestCase): # Explicitly commits log, new bom (setUp) and replacement impact. # Is run via background jobs IRL - run_bom_job( + run_replace_bom_job( doc=log, boms=self.boms, update_type="Replace BOM", @@ -88,7 +88,7 @@ class TestBOMUpdateLog(FrappeTestCase): log2 = enqueue_replace_bom( boms=self.boms, ) - run_bom_job( # Explicitly commits + run_replace_bom_job( # Explicitly commits doc=log2, boms=boms, update_type="Replace BOM", diff --git a/erpnext/manufacturing/doctype/bom_update_tool/bom_update_tool.py b/erpnext/manufacturing/doctype/bom_update_tool/bom_update_tool.py index e7657253408..4a2e03fb188 100644 --- a/erpnext/manufacturing/doctype/bom_update_tool/bom_update_tool.py +++ b/erpnext/manufacturing/doctype/bom_update_tool/bom_update_tool.py @@ -2,8 +2,7 @@ # For license information, please see license.txt import json -from collections import defaultdict -from typing import TYPE_CHECKING, Dict, List, Literal, Optional, Union +from typing import TYPE_CHECKING, Dict, Literal, Optional, Union if TYPE_CHECKING: from erpnext.manufacturing.doctype.bom_update_log.bom_update_log import BOMUpdateLog @@ -39,17 +38,7 @@ def enqueue_update_cost() -> "BOMUpdateLog": def auto_update_latest_price_in_all_boms() -> None: """Called via hooks.py.""" if frappe.db.get_single_value("Manufacturing Settings", "update_bom_costs_automatically"): - update_cost() - - -def update_cost() -> None: - """Updates Cost for all BOMs from bottom to top.""" - bom_list = get_boms_in_bottom_up_order() - for bom in bom_list: - bom_doc = frappe.get_cached_doc("BOM", bom) - bom_doc.calculate_cost(save_updates=True, update_hour_rate=True) - # bom_doc.update_exploded_items(save=True) #TODO: edit exploded items rate - bom_doc.db_update() + create_bom_update_log(update_type="Update Cost") def create_bom_update_log( @@ -69,90 +58,3 @@ def create_bom_update_log( "update_type": update_type, } ).submit() - - -def get_boms_in_bottom_up_order(bom_no: Optional[str] = None) -> List: - """ - Eg: Main BOM - |- Sub BOM 1 - |- Leaf BOM 1 - |- Sub BOM 2 - |- Leaf BOM 2 - Result: [Leaf BOM 1, Leaf BOM 2, Sub BOM 1, Sub BOM 2, Main BOM] - """ - leaf_boms = [] - if bom_no: - leaf_boms.append(bom_no) - else: - leaf_boms = _get_leaf_boms() - - child_parent_map = _generate_child_parent_map() - bom_list = leaf_boms.copy() - - for leaf_bom in leaf_boms: - parent_list = _get_flat_parent_map(leaf_bom, child_parent_map) - - if not parent_list: - continue - - bom_list.extend(parent_list) - bom_list = list(dict.fromkeys(bom_list).keys()) # remove duplicates - - return bom_list - - -def _generate_child_parent_map(): - bom = frappe.qb.DocType("BOM") - bom_item = frappe.qb.DocType("BOM Item") - - bom_parents = ( - frappe.qb.from_(bom_item) - .join(bom) - .on(bom_item.parent == bom.name) - .select(bom_item.bom_no, bom_item.parent) - .where( - (bom_item.bom_no.isnotnull()) - & (bom_item.bom_no != "") - & (bom.docstatus == 1) - & (bom.is_active == 1) - & (bom_item.parenttype == "BOM") - ) - ).run(as_dict=True) - - child_parent_map = defaultdict(list) - for bom in bom_parents: - child_parent_map[bom.bom_no].append(bom.parent) - - return child_parent_map - - -def _get_flat_parent_map(leaf, child_parent_map): - "Get ancestors at all levels of a leaf BOM." - parents_list = [] - - def _get_parents(node, parents_list): - "Returns recursively updated ancestors list." - first_parents = child_parent_map.get(node) # immediate parents of node - if not first_parents: # top most node - return parents_list - - parents_list.extend(first_parents) - parents_list = list(dict.fromkeys(parents_list).keys()) # remove duplicates - - for nth_node in first_parents: - # recursively find parents - parents_list = _get_parents(nth_node, parents_list) - - return parents_list - - parents_list = _get_parents(leaf, parents_list) - return parents_list - - -def _get_leaf_boms(): - return frappe.db.sql_list( - """select name from `tabBOM` bom - where docstatus=1 and is_active=1 - and not exists(select bom_no from `tabBOM Item` - where parent=bom.name and ifnull(bom_no, '')!='')""" - ) From 9f5f18e94da4254255a32d792abc94407ca5fde0 Mon Sep 17 00:00:00 2001 From: marination Date: Tue, 24 May 2022 18:17:40 +0530 Subject: [PATCH 012/105] style: Update docstrings and fix/add type hints + Collapsible progress section in Log --- .../bom_update_log/bom_update_log.json | 3 +- .../bom_update_log/bom_updation_utils.py | 45 ++++++++++++++----- 2 files changed, 35 insertions(+), 13 deletions(-) diff --git a/erpnext/manufacturing/doctype/bom_update_log/bom_update_log.json b/erpnext/manufacturing/doctype/bom_update_log/bom_update_log.json index 3455b866573..db5f58d04f5 100644 --- a/erpnext/manufacturing/doctype/bom_update_log/bom_update_log.json +++ b/erpnext/manufacturing/doctype/bom_update_log/bom_update_log.json @@ -69,6 +69,7 @@ "options": "Error Log" }, { + "collapsible": 1, "fieldname": "progress_section", "fieldtype": "Section Break", "label": "Progress" @@ -94,7 +95,7 @@ "index_web_pages_for_search": 1, "is_submittable": 1, "links": [], - "modified": "2022-05-23 14:42:14.725914", + "modified": "2022-05-24 17:52:21.824710", "modified_by": "Administrator", "module": "Manufacturing", "name": "BOM Update Log", diff --git a/erpnext/manufacturing/doctype/bom_update_log/bom_updation_utils.py b/erpnext/manufacturing/doctype/bom_update_log/bom_updation_utils.py index b5964cec9d4..d246d3064f5 100644 --- a/erpnext/manufacturing/doctype/bom_update_log/bom_updation_utils.py +++ b/erpnext/manufacturing/doctype/bom_update_log/bom_updation_utils.py @@ -3,7 +3,7 @@ import json from collections import defaultdict -from typing import TYPE_CHECKING, Dict, List, Optional +from typing import TYPE_CHECKING, Any, Dict, List, Optional if TYPE_CHECKING: from erpnext.manufacturing.doctype.bom_update_log.bom_update_log import BOMUpdateLog @@ -13,7 +13,8 @@ from frappe import _ def replace_bom(boms: Dict) -> None: - """Replace current BOM with new BOM in parent BOMs.""" + "Replace current BOM with new BOM in parent BOMs." + current_bom = boms.get("current_bom") new_bom = boms.get("new_bom") @@ -39,6 +40,7 @@ def replace_bom(boms: Dict) -> None: def update_cost_in_level(doc: "BOMUpdateLog", bom_list: List[str]) -> None: "Updates Cost for BOMs within a given level. Runs via background jobs." + try: status = frappe.db.get_value("BOM Update Log", doc.name, "status") if status == "Failed": @@ -66,6 +68,8 @@ def update_cost_in_level(doc: "BOMUpdateLog", bom_list: List[str]) -> None: def get_ancestor_boms(new_bom: str, bom_list: Optional[List] = None) -> List: + "Recursively get all ancestors of BOM." + bom_list = bom_list or [] bom_item = frappe.qb.DocType("BOM Item") @@ -99,17 +103,18 @@ def update_new_bom_in_bom_items(unit_cost: float, current_bom: str, new_bom: str ).run() -def get_bom_unit_cost(new_bom: str) -> float: +def get_bom_unit_cost(bom_name: str) -> float: bom = frappe.qb.DocType("BOM") new_bom_unitcost = ( - frappe.qb.from_(bom).select(bom.total_cost / bom.quantity).where(bom.name == new_bom).run() + frappe.qb.from_(bom).select(bom.total_cost / bom.quantity).where(bom.name == bom_name).run() ) return frappe.utils.flt(new_bom_unitcost[0][0]) -def update_cost_in_boms(bom_list: List[str], docname: str) -> Dict: +def update_cost_in_boms(bom_list: List[str], docname: str) -> Dict[str, Dict]: "Updates cost in given BOMs. Returns current and total updated BOMs." + updated_boms = {} # current boms that have been updated for bom in bom_list: @@ -131,8 +136,11 @@ def update_cost_in_boms(bom_list: List[str], docname: str) -> Dict: return log_data -def process_if_level_is_complete(docname: str, current_boms: Dict, processed_boms: Dict) -> None: - "Prepare and set higher level BOMs in Log if current level is complete." +def process_if_level_is_complete( + docname: str, current_boms: Dict[str, bool], processed_boms: Dict[str, bool] +) -> None: + "Prepare and set higher level BOMs/dependants in Log if current level is complete." + processing_complete = all(current_boms.get(bom) for bom in current_boms) if not processing_complete: return @@ -149,7 +157,9 @@ def process_if_level_is_complete(docname: str, current_boms: Dict, processed_bom ) -def get_next_higher_level_boms(child_boms: Dict, processed_boms: Dict): +def get_next_higher_level_boms( + child_boms: Dict[str, bool], processed_boms: Dict[str, bool] +) -> List[str]: "Generate immediate higher level dependants with no unresolved dependencies." def _all_children_are_processed(parent): @@ -167,7 +177,9 @@ def get_next_higher_level_boms(child_boms: Dict, processed_boms: Dict): return list(dependants) -def get_leaf_boms(): +def get_leaf_boms() -> List[str]: + "Get BOMs that have no dependencies." + return frappe.db.sql_list( """select name from `tabBOM` bom where docstatus=1 and is_active=1 @@ -176,7 +188,13 @@ def get_leaf_boms(): ) -def _generate_dependants_map(): +def _generate_dependants_map() -> defaultdict: + """ + Generate map such as: { BOM-1: [Dependant-BOM-1, Dependant-BOM-2, ..] }. + Here BOM-1 is the leaf/lower level node/dependency. + The list contains one level higher nodes/dependants that depend on BOM-1. + """ + bom = frappe.qb.DocType("BOM") bom_item = frappe.qb.DocType("BOM Item") @@ -201,8 +219,9 @@ def _generate_dependants_map(): return child_parent_map -def set_values_in_log(log_name: str, values: Dict, commit: bool = False) -> None: +def set_values_in_log(log_name: str, values: Dict[str, Any], commit: bool = False) -> None: "Update BOM Update Log record." + if not values: return @@ -217,7 +236,9 @@ def set_values_in_log(log_name: str, values: Dict, commit: bool = False) -> None frappe.db.commit() -def handle_exception(doc: "BOMUpdateLog"): +def handle_exception(doc: "BOMUpdateLog") -> None: + "Rolls back and fails BOM Update Log." + frappe.db.rollback() error_log = doc.log_error("BOM Update Tool Error") set_values_in_log(doc.name, {"status": "Failed", "error_log": error_log.name}) From 96d8b1ef3cb68011eb350386c229fb3fbcd067af Mon Sep 17 00:00:00 2001 From: Deepesh Garg Date: Wed, 25 May 2022 14:19:10 +0530 Subject: [PATCH 013/105] feat: Auto accrue loan interest for backdated term loans --- erpnext/loan_management/doctype/loan/loan.py | 12 ++++++++++++ .../payroll/doctype/salary_slip/salary_slip.py | 16 ++++++++++++++-- 2 files changed, 26 insertions(+), 2 deletions(-) diff --git a/erpnext/loan_management/doctype/loan/loan.py b/erpnext/loan_management/doctype/loan/loan.py index a0ef1b971cd..3b76ba4edb6 100644 --- a/erpnext/loan_management/doctype/loan/loan.py +++ b/erpnext/loan_management/doctype/loan/loan.py @@ -68,6 +68,8 @@ class Loan(AccountsController): def on_submit(self): self.link_loan_security_pledge() + # Interest accrual for backdated term loans + self.accrue_loan_interest() def on_cancel(self): self.unlink_loan_security_pledge() @@ -187,6 +189,16 @@ class Loan(AccountsController): self.db_set("maximum_loan_amount", maximum_loan_value) + def accrue_loan_interest(self): + from erpnext.loan_management.doctype.process_loan_interest_accrual.process_loan_interest_accrual import ( + process_loan_interest_accrual_for_term_loans, + ) + + if getdate(self.repayment_start_date) < getdate() and self.is_term_loan: + process_loan_interest_accrual_for_term_loans( + posting_date=getdate(), loan_type=self.loan_type, loan=self.name + ) + def unlink_loan_security_pledge(self): pledges = frappe.get_all("Loan Security Pledge", fields=["name"], filters={"loan": self.name}) pledge_list = [d.name for d in pledges] diff --git a/erpnext/payroll/doctype/salary_slip/salary_slip.py b/erpnext/payroll/doctype/salary_slip/salary_slip.py index 6a7f72b0132..b55bfaa5863 100644 --- a/erpnext/payroll/doctype/salary_slip/salary_slip.py +++ b/erpnext/payroll/doctype/salary_slip/salary_slip.py @@ -29,6 +29,9 @@ from erpnext.loan_management.doctype.loan_repayment.loan_repayment import ( calculate_amounts, create_repayment_entry, ) +from erpnext.loan_management.doctype.process_loan_interest_accrual.process_loan_interest_accrual import ( + process_loan_interest_accrual_for_term_loans, +) from erpnext.payroll.doctype.additional_salary.additional_salary import get_additional_salaries from erpnext.payroll.doctype.employee_benefit_application.employee_benefit_application import ( get_benefit_component_amount, @@ -1364,9 +1367,9 @@ class SalarySlip(TransactionBase): self.total_loan_repayment += payment.total_payment def get_loan_details(self): - return frappe.get_all( + loan_details = frappe.get_all( "Loan", - fields=["name", "interest_income_account", "loan_account", "loan_type"], + fields=["name", "interest_income_account", "loan_account", "loan_type", "is_term_loan"], filters={ "applicant": self.employee, "docstatus": 1, @@ -1375,6 +1378,15 @@ class SalarySlip(TransactionBase): }, ) + if loan_details: + for loan in loan_details: + if loan.is_term_loan: + process_loan_interest_accrual_for_term_loans( + posting_date=self.posting_date, loan_type=loan.loan_type, loan=loan.name + ) + + return loan_details + def make_loan_repayment_entry(self): payroll_payable_account = get_payroll_payable_account(self.company, self.payroll_entry) for loan in self.loans: From eabd8290d40db9745a046c422ed17e024a685ae2 Mon Sep 17 00:00:00 2001 From: marination Date: Wed, 25 May 2022 15:32:42 +0530 Subject: [PATCH 014/105] feat: Only update exploded items rate and amount - Generate RM-Rate map from Items table (will include subassembly items with rate) - Function to reset exploded item rate from above map - `db_update` exploded item rate only if rate is changed - Via Update Cost, only update exploded items rate, do not regenerate table again - Exploded Items are regenerated on Save and Replace BOM job - `calculate_exploded_cost` is run only via non doc events (Update Cost button, Update BOMs Cost Job) --- erpnext/manufacturing/doctype/bom/bom.py | 39 +++++++++++++++++-- .../bom_explosion_item.json | 4 +- .../bom_update_log/bom_updation_utils.py | 1 - 3 files changed, 39 insertions(+), 5 deletions(-) diff --git a/erpnext/manufacturing/doctype/bom/bom.py b/erpnext/manufacturing/doctype/bom/bom.py index 560019a86d5..6d53cfe7070 100644 --- a/erpnext/manufacturing/doctype/bom/bom.py +++ b/erpnext/manufacturing/doctype/bom/bom.py @@ -5,7 +5,7 @@ import functools import re from collections import deque from operator import itemgetter -from typing import List +from typing import Dict, List import frappe from frappe import _ @@ -185,6 +185,7 @@ class BOM(WebsiteGenerator): self.validate_transfer_against() self.set_routing_operations() self.validate_operations() + self.update_exploded_items(save=False) self.calculate_cost() self.update_stock_qty() self.update_cost(update_parent=False, from_child_bom=True, update_hour_rate=False, save=False) @@ -391,8 +392,6 @@ class BOM(WebsiteGenerator): if save: self.db_update() - self.update_exploded_items(save=save) - # update parent BOMs if self.total_cost != existing_bom_cost and update_parent: parent_boms = frappe.db.sql_list( @@ -594,6 +593,10 @@ class BOM(WebsiteGenerator): self.calculate_op_cost(update_hour_rate) self.calculate_rm_cost(save=save_updates) self.calculate_sm_cost(save=save_updates) + if save_updates: + # not via doc event, table is not regenerated and needs updation + self.calculate_exploded_cost() + self.total_cost = self.operating_cost + self.raw_material_cost - self.scrap_material_cost self.base_total_cost = ( self.base_operating_cost + self.base_raw_material_cost - self.base_scrap_material_cost @@ -689,6 +692,36 @@ class BOM(WebsiteGenerator): self.scrap_material_cost = total_sm_cost self.base_scrap_material_cost = base_total_sm_cost + def calculate_exploded_cost(self): + "Set exploded row cost from it's parent BOM." + rm_rate_map = self.get_rm_rate_map() + + for row in self.get("exploded_items"): + old_rate = flt(row.rate) + row.rate = rm_rate_map.get(row.item_code) + row.amount = flt(row.stock_qty) * row.rate + + if old_rate != row.rate: + # Only db_update if unchanged + row.db_update() + + def get_rm_rate_map(self) -> Dict[str, float]: + "Create Raw Material-Rate map for Exploded Items. Fetch rate from Items table or Subassembly BOM." + rm_rate_map = {} + + for item in self.get("items"): + if item.bom_no: + # Get Item-Rate from Subassembly BOM + explosion_items = frappe.db.get_all( + "BOM Explosion Item", filters={"parent": item.bom_no}, fields=["item_code", "rate"] + ) + explosion_item_rate = {item.item_code: flt(item.rate) for item in explosion_items} + rm_rate_map.update(explosion_item_rate) + else: + rm_rate_map[item.item_code] = flt(item.base_rate) / flt(item.conversion_factor or 1.0) + + return rm_rate_map + def update_exploded_items(self, save=True): """Update Flat BOM, following will be correct data""" self.get_exploded_items() diff --git a/erpnext/manufacturing/doctype/bom_explosion_item/bom_explosion_item.json b/erpnext/manufacturing/doctype/bom_explosion_item/bom_explosion_item.json index f01d856e72a..9b1db63494b 100644 --- a/erpnext/manufacturing/doctype/bom_explosion_item/bom_explosion_item.json +++ b/erpnext/manufacturing/doctype/bom_explosion_item/bom_explosion_item.json @@ -169,13 +169,15 @@ "index_web_pages_for_search": 1, "istable": 1, "links": [], - "modified": "2020-10-08 16:21:29.386212", + "modified": "2022-05-27 13:42:23.305455", "modified_by": "Administrator", "module": "Manufacturing", "name": "BOM Explosion Item", + "naming_rule": "Random", "owner": "Administrator", "permissions": [], "sort_field": "modified", "sort_order": "DESC", + "states": [], "track_changes": 1 } \ No newline at end of file diff --git a/erpnext/manufacturing/doctype/bom_update_log/bom_updation_utils.py b/erpnext/manufacturing/doctype/bom_update_log/bom_updation_utils.py index d246d3064f5..1ec15f0d3ae 100644 --- a/erpnext/manufacturing/doctype/bom_update_log/bom_updation_utils.py +++ b/erpnext/manufacturing/doctype/bom_update_log/bom_updation_utils.py @@ -120,7 +120,6 @@ def update_cost_in_boms(bom_list: List[str], docname: str) -> Dict[str, Dict]: for bom in bom_list: bom_doc = frappe.get_cached_doc("BOM", bom) bom_doc.calculate_cost(save_updates=True, update_hour_rate=True) - # bom_doc.update_exploded_items(save=True) #TODO: edit exploded items rate bom_doc.db_update() updated_boms[bom] = True From 59499462650965b483a2b0f9f377d59b98f756e6 Mon Sep 17 00:00:00 2001 From: marination Date: Fri, 27 May 2022 17:04:21 +0530 Subject: [PATCH 015/105] chore: Change BOM Progress field types to Long Text --- erpnext/manufacturing/doctype/bom/bom.py | 2 +- .../doctype/bom_update_log/bom_update_log.json | 8 ++++---- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/erpnext/manufacturing/doctype/bom/bom.py b/erpnext/manufacturing/doctype/bom/bom.py index 6d53cfe7070..15048ec9906 100644 --- a/erpnext/manufacturing/doctype/bom/bom.py +++ b/erpnext/manufacturing/doctype/bom/bom.py @@ -702,7 +702,7 @@ class BOM(WebsiteGenerator): row.amount = flt(row.stock_qty) * row.rate if old_rate != row.rate: - # Only db_update if unchanged + # Only db_update if changed row.db_update() def get_rm_rate_map(self) -> Dict[str, float]: diff --git a/erpnext/manufacturing/doctype/bom_update_log/bom_update_log.json b/erpnext/manufacturing/doctype/bom_update_log/bom_update_log.json index db5f58d04f5..bea3cf03733 100644 --- a/erpnext/manufacturing/doctype/bom_update_log/bom_update_log.json +++ b/erpnext/manufacturing/doctype/bom_update_log/bom_update_log.json @@ -76,18 +76,18 @@ }, { "fieldname": "current_boms", - "fieldtype": "Text", + "fieldtype": "Long Text", "label": "Current BOMs" }, { "description": "Immediate parent BOMs", "fieldname": "parent_boms", - "fieldtype": "Text", + "fieldtype": "Long Text", "label": "Parent BOMs" }, { "fieldname": "processed_boms", - "fieldtype": "Text", + "fieldtype": "Long Text", "label": "Processed BOMs" } ], @@ -95,7 +95,7 @@ "index_web_pages_for_search": 1, "is_submittable": 1, "links": [], - "modified": "2022-05-24 17:52:21.824710", + "modified": "2022-05-27 17:03:34.712010", "modified_by": "Administrator", "module": "Manufacturing", "name": "BOM Update Log", From 2de2491e1737a9935183ca8f1db953f77f6c1185 Mon Sep 17 00:00:00 2001 From: marination Date: Fri, 27 May 2022 20:33:14 +0530 Subject: [PATCH 016/105] perf: `get_next_higher_level_boms` - Separate getting dependants and checking if they are valid (loop within loop led to redundant processing that slowed down function) - Adding to above, the same dependant(parent) was repeatedly processed as many children shared it. Expensive. - Use a parent-child map similar to child-parent map to check if all children are resolved - `map.get()` reduced time: 10 mins -> 0.9s~1 second (as compared to `get_cached_doc` or query) - Total time: 17 seconds to process 6599 leaf boms and 4.2L parent boms - Previous Total time: >10 mins (I terminated it due to not wanting to waste time XD) --- .../bom_update_log/bom_updation_utils.py | 44 ++++++++++++------- 1 file changed, 28 insertions(+), 16 deletions(-) diff --git a/erpnext/manufacturing/doctype/bom_update_log/bom_updation_utils.py b/erpnext/manufacturing/doctype/bom_update_log/bom_updation_utils.py index 1ec15f0d3ae..790a79b3333 100644 --- a/erpnext/manufacturing/doctype/bom_update_log/bom_updation_utils.py +++ b/erpnext/manufacturing/doctype/bom_update_log/bom_updation_utils.py @@ -159,21 +159,29 @@ def process_if_level_is_complete( def get_next_higher_level_boms( child_boms: Dict[str, bool], processed_boms: Dict[str, bool] ) -> List[str]: - "Generate immediate higher level dependants with no unresolved dependencies." + "Generate immediate higher level dependants with no unresolved dependencies (children)." - def _all_children_are_processed(parent): - bom_doc = frappe.get_cached_doc("BOM", parent) - return all(processed_boms.get(row.bom_no) for row in bom_doc.items if row.bom_no) + def _all_children_are_processed(parent_bom): + child_boms = dependency_map.get(parent_bom) + return all(processed_boms.get(bom) for bom in child_boms) - dependants_map = _generate_dependants_map() - dependants = set() + dependants_map, dependency_map = _generate_dependence_map() + + dependants = [] for bom in child_boms: + # generate list of immediate dependants parents = dependants_map.get(bom) or [] - for parent in parents: - if _all_children_are_processed(parent): - dependants.add(parent) + dependants.extend(parents) - return list(dependants) + dependants = set(dependants) # remove duplicates + resolved_dependants = set() + + # consider only if children are all resolved + for parent_bom in dependants: + if _all_children_are_processed(parent_bom): + resolved_dependants.add(parent_bom) + + return list(resolved_dependants) def get_leaf_boms() -> List[str]: @@ -187,17 +195,19 @@ def get_leaf_boms() -> List[str]: ) -def _generate_dependants_map() -> defaultdict: +def _generate_dependence_map() -> defaultdict: """ - Generate map such as: { BOM-1: [Dependant-BOM-1, Dependant-BOM-2, ..] }. + Generate maps such as: { BOM-1: [Dependant-BOM-1, Dependant-BOM-2, ..] }. Here BOM-1 is the leaf/lower level node/dependency. The list contains one level higher nodes/dependants that depend on BOM-1. + + Generate and return the reverse as well. """ bom = frappe.qb.DocType("BOM") bom_item = frappe.qb.DocType("BOM Item") - bom_parents = ( + bom_items = ( frappe.qb.from_(bom_item) .join(bom) .on(bom_item.parent == bom.name) @@ -212,10 +222,12 @@ def _generate_dependants_map() -> defaultdict: ).run(as_dict=True) child_parent_map = defaultdict(list) - for bom in bom_parents: - child_parent_map[bom.bom_no].append(bom.parent) + parent_child_map = defaultdict(list) + for row in bom_items: + child_parent_map[row.bom_no].append(row.parent) + parent_child_map[row.parent].append(row.bom_no) - return child_parent_map + return child_parent_map, parent_child_map def set_values_in_log(log_name: str, values: Dict[str, Any], commit: bool = False) -> None: From 978ba5238fae64fe0e348091d42c3210b999df02 Mon Sep 17 00:00:00 2001 From: marination Date: Fri, 27 May 2022 21:59:59 +0530 Subject: [PATCH 017/105] fix: Safe cast `row.rate` (in case of faulty exploded items, edge case but oh well) --- erpnext/manufacturing/doctype/bom/bom.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/erpnext/manufacturing/doctype/bom/bom.py b/erpnext/manufacturing/doctype/bom/bom.py index 15048ec9906..7055efac289 100644 --- a/erpnext/manufacturing/doctype/bom/bom.py +++ b/erpnext/manufacturing/doctype/bom/bom.py @@ -699,7 +699,7 @@ class BOM(WebsiteGenerator): for row in self.get("exploded_items"): old_rate = flt(row.rate) row.rate = rm_rate_map.get(row.item_code) - row.amount = flt(row.stock_qty) * row.rate + row.amount = flt(row.stock_qty) * flt(row.rate) if old_rate != row.rate: # Only db_update if changed From de5515799732cbfb06a9e05ee3951f180a34e841 Mon Sep 17 00:00:00 2001 From: Rucha Mahabal Date: Mon, 30 May 2022 12:47:26 +0530 Subject: [PATCH 018/105] chore: rename method `get_salary_component_account` method to `set` - since it doesn't return any value --- .../payroll/doctype/payroll_entry/test_payroll_entry.py | 8 ++++---- erpnext/payroll/doctype/salary_slip/test_salary_slip.py | 4 ++-- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/erpnext/payroll/doctype/payroll_entry/test_payroll_entry.py b/erpnext/payroll/doctype/payroll_entry/test_payroll_entry.py index fda0fcf8bee..3fffeb8966e 100644 --- a/erpnext/payroll/doctype/payroll_entry/test_payroll_entry.py +++ b/erpnext/payroll/doctype/payroll_entry/test_payroll_entry.py @@ -22,10 +22,10 @@ from erpnext.loan_management.doctype.process_loan_interest_accrual.process_loan_ from erpnext.payroll.doctype.payroll_entry.payroll_entry import get_end_date, get_start_end_dates from erpnext.payroll.doctype.salary_slip.test_salary_slip import ( create_account, - get_salary_component_account, make_deduction_salary_component, make_earning_salary_component, make_employee_salary_slip, + set_salary_component_account, ) from erpnext.payroll.doctype.salary_structure.test_salary_structure import ( create_salary_structure_assignment, @@ -66,7 +66,7 @@ class TestPayrollEntry(unittest.TestCase): if not frappe.db.get_value( "Salary Component Account", {"parent": data.name, "company": company}, "name" ): - get_salary_component_account(data.name) + set_salary_component_account(data.name) employee = frappe.db.get_value("Employee", {"company": company}) company_doc = frappe.get_doc("Company", company) @@ -95,7 +95,7 @@ class TestPayrollEntry(unittest.TestCase): if not frappe.db.get_value( "Salary Component Account", {"parent": data.name, "company": company}, "name" ): - get_salary_component_account(data.name) + set_salary_component_account(data.name) company_doc = frappe.get_doc("Company", company) salary_structure = make_salary_structure( @@ -148,7 +148,7 @@ class TestPayrollEntry(unittest.TestCase): if not frappe.db.get_value( "Salary Component Account", {"parent": data.name, "company": "_Test Company"}, "name" ): - get_salary_component_account(data.name) + set_salary_component_account(data.name) if not frappe.db.exists("Department", "cc - _TC"): frappe.get_doc( diff --git a/erpnext/payroll/doctype/salary_slip/test_salary_slip.py b/erpnext/payroll/doctype/salary_slip/test_salary_slip.py index 1bc37419228..91d335274e1 100644 --- a/erpnext/payroll/doctype/salary_slip/test_salary_slip.py +++ b/erpnext/payroll/doctype/salary_slip/test_salary_slip.py @@ -1046,10 +1046,10 @@ def make_salary_component(salary_components, test_tax, company_list=None): doc.update(salary_component) doc.insert() - get_salary_component_account(doc, company_list) + set_salary_component_account(doc, company_list) -def get_salary_component_account(sal_comp, company_list=None): +def set_salary_component_account(sal_comp, company_list=None): company = erpnext.get_default_company() if company_list and company not in company_list: From 08bf0baaae3dad9fc74f15b0c42553b83f82e95d Mon Sep 17 00:00:00 2001 From: Saqib Ansari Date: Mon, 30 May 2022 14:50:50 +0530 Subject: [PATCH 019/105] chore!: remove unused bill no & date from purchase receipt (#31163) --- .../purchase_receipt/purchase_receipt.json | 22 +------------------ 1 file changed, 1 insertion(+), 21 deletions(-) diff --git a/erpnext/stock/doctype/purchase_receipt/purchase_receipt.json b/erpnext/stock/doctype/purchase_receipt/purchase_receipt.json index 983b62a09a4..923ceb36cd7 100755 --- a/erpnext/stock/doctype/purchase_receipt/purchase_receipt.json +++ b/erpnext/stock/doctype/purchase_receipt/purchase_receipt.json @@ -108,8 +108,6 @@ "terms_section_break", "tc_name", "terms", - "bill_no", - "bill_date", "more_info", "status", "amended_from", @@ -867,24 +865,6 @@ "oldfieldname": "terms", "oldfieldtype": "Text Editor" }, - { - "fieldname": "bill_no", - "fieldtype": "Data", - "hidden": 1, - "label": "Bill No", - "oldfieldname": "bill_no", - "oldfieldtype": "Data", - "print_hide": 1 - }, - { - "fieldname": "bill_date", - "fieldtype": "Date", - "hidden": 1, - "label": "Bill Date", - "oldfieldname": "bill_date", - "oldfieldtype": "Date", - "print_hide": 1 - }, { "collapsible": 1, "fieldname": "more_info", @@ -1168,7 +1148,7 @@ "idx": 261, "is_submittable": 1, "links": [], - "modified": "2022-04-26 13:41:32.625197", + "modified": "2022-05-27 15:59:18.550583", "modified_by": "Administrator", "module": "Stock", "name": "Purchase Receipt", From 42f6bca9354d07855573235ab8508c49e5ba9cac Mon Sep 17 00:00:00 2001 From: Rucha Mahabal Date: Mon, 30 May 2022 16:04:07 +0530 Subject: [PATCH 020/105] fix: reset Error Message on successful operation and fix status update on submit/cancel --- .../doctype/payroll_entry/payroll_entry.py | 23 ++++++++++--------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/erpnext/payroll/doctype/payroll_entry/payroll_entry.py b/erpnext/payroll/doctype/payroll_entry/payroll_entry.py index 86be813b91a..266621d4d11 100644 --- a/erpnext/payroll/doctype/payroll_entry/payroll_entry.py +++ b/erpnext/payroll/doctype/payroll_entry/payroll_entry.py @@ -44,7 +44,7 @@ class PayrollEntry(Document): self.set_status() def on_submit(self): - self.set_status(update=True) + self.set_status(update=True, status="Submitted") self.create_salary_slips() def before_submit(self): @@ -90,7 +90,8 @@ class PayrollEntry(Document): ) self.db_set("salary_slips_created", 0) self.db_set("salary_slips_submitted", 0) - self.set_status(update=True) + self.set_status(update=True, status="Cancelled") + self.db_set("error_message", "") def get_emp_list(self): """ @@ -187,7 +188,7 @@ class PayrollEntry(Document): "currency": self.currency, } ) - if len(employees) > 30: + if len(employees) > 30 or frappe.flags.enqueue_payroll_entry: self.db_set("status", "Queued") frappe.enqueue( create_salary_slips_for_employees, @@ -230,14 +231,14 @@ class PayrollEntry(Document): @frappe.whitelist() def submit_salary_slips(self): self.check_permission("write") - ss_list = self.get_sal_slip_list(ss_status=0) - if len(ss_list) > 30: + salary_slips = self.get_sal_slip_list(ss_status=0) + if len(salary_slips) > 30 or frappe.flags.enqueue_payroll_entry: self.db_set("status", "Queued") frappe.enqueue( submit_salary_slips_for_employees, timeout=600, payroll_entry=self, - salary_slips=ss_list, + salary_slips=salary_slips, publish_progress=False, ) frappe.msgprint( @@ -246,7 +247,7 @@ class PayrollEntry(Document): indicator="blue", ) else: - submit_salary_slips_for_employees(self, ss_list, publish_progress=False) + submit_salary_slips_for_employees(self, salary_slips, publish_progress=False) def email_salary_slip(self, submitted_ss): if frappe.db.get_single_value("Payroll Settings", "email_salary_slip_to_employee"): @@ -857,7 +858,7 @@ def create_salary_slips_for_employees(employees, args, publish_progress=True): title=_("Creating Salary Slips..."), ) - payroll_entry.db_set({"status": "Submitted", "salary_slips_created": 1}) + payroll_entry.db_set({"status": "Submitted", "salary_slips_created": 1, "error_message": ""}) if salary_slips_exist_for: frappe.msgprint( @@ -873,7 +874,7 @@ def create_salary_slips_for_employees(employees, args, publish_progress=True): log_payroll_failure("creation", payroll_entry, e) finally: - frappe.db.commit() + frappe.db.commit() # nosemgrep frappe.publish_realtime("completed_salary_slip_creation") @@ -937,7 +938,7 @@ def submit_salary_slips_for_employees(payroll_entry, salary_slips, publish_progr if submitted: payroll_entry.make_accrual_jv_entry() payroll_entry.email_salary_slip(submitted) - payroll_entry.db_set({"salary_slips_submitted": 1, "status": "Submitted"}) + payroll_entry.db_set({"salary_slips_submitted": 1, "status": "Submitted", "error_message": ""}) show_payroll_submission_status(submitted, not_submitted, salary_slip) @@ -946,7 +947,7 @@ def submit_salary_slips_for_employees(payroll_entry, salary_slips, publish_progr log_payroll_failure("submission", payroll_entry, e) finally: - frappe.db.commit() + frappe.db.commit() # nosemgrep frappe.publish_realtime("completed_salary_slip_submission") frappe.flags.via_payroll_entry = False From 78c39e947bf747032c935dab6d76092836fa0e61 Mon Sep 17 00:00:00 2001 From: Rucha Mahabal Date: Mon, 30 May 2022 16:07:19 +0530 Subject: [PATCH 021/105] test: Salary Slip operations queuing, failure, and payroll entry status - fix multicurrency test, remove redundant doc creation --- .../payroll_entry/test_payroll_entry.py | 172 ++++++++++++++---- .../salary_structure/test_salary_structure.py | 3 - 2 files changed, 137 insertions(+), 38 deletions(-) diff --git a/erpnext/payroll/doctype/payroll_entry/test_payroll_entry.py b/erpnext/payroll/doctype/payroll_entry/test_payroll_entry.py index 3fffeb8966e..5c68bd35ef9 100644 --- a/erpnext/payroll/doctype/payroll_entry/test_payroll_entry.py +++ b/erpnext/payroll/doctype/payroll_entry/test_payroll_entry.py @@ -5,6 +5,7 @@ import unittest import frappe from dateutil.relativedelta import relativedelta +from frappe.tests.utils import FrappeTestCase from frappe.utils import add_months import erpnext @@ -35,14 +36,12 @@ from erpnext.payroll.doctype.salary_structure.test_salary_structure import ( test_dependencies = ["Holiday List"] -class TestPayrollEntry(unittest.TestCase): - @classmethod - def setUpClass(cls): +class TestPayrollEntry(FrappeTestCase): + def setUp(self): frappe.db.set_value( "Company", erpnext.get_default_company(), "default_holiday_list", "_Test Holiday List" ) - def setUp(self): for dt in [ "Salary Slip", "Salary Component", @@ -88,40 +87,40 @@ class TestPayrollEntry(unittest.TestCase): currency=company_doc.default_currency, ) - def test_multi_currency_payroll_entry(self): # pylint: disable=no-self-use - company = erpnext.get_default_company() - employee = make_employee("test_muti_currency_employee@payroll.com", company=company) + def test_multi_currency_payroll_entry(self): + company = frappe.get_doc("Company", "_Test Company") + employee = make_employee( + "test_muti_currency_employee@payroll.com", company=company.name, department="Accounts - _TC" + ) + for data in frappe.get_all("Salary Component", fields=["name"]): if not frappe.db.get_value( - "Salary Component Account", {"parent": data.name, "company": company}, "name" + "Salary Component Account", {"parent": data.name, "company": company.name}, "name" ): set_salary_component_account(data.name) - company_doc = frappe.get_doc("Company", company) - salary_structure = make_salary_structure( - "_Test Multi Currency Salary Structure", "Monthly", company=company, currency="USD" - ) - create_salary_structure_assignment( - employee, salary_structure.name, company=company, currency="USD" - ) - frappe.db.sql( - """delete from `tabSalary Slip` where employee=%s""", - (frappe.db.get_value("Employee", {"user_id": "test_muti_currency_employee@payroll.com"})), - ) - salary_slip = get_salary_slip( - "test_muti_currency_employee@payroll.com", "Monthly", "_Test Multi Currency Salary Structure" + salary_struct = make_salary_structure( + "_Test Multi Currency Salary Structure", + "Monthly", + employee, + currency="USD", + company=company.name, ) + + frappe.db.delete("Salary Slip", {"employee": employee}) dates = get_start_end_dates("Monthly", nowdate()) payroll_entry = make_payroll_entry( start_date=dates.start_date, end_date=dates.end_date, - payable_account=company_doc.default_payroll_payable_account, + payable_account=company.default_payroll_payable_account, currency="USD", exchange_rate=70, + company=company.name, ) payroll_entry.make_payment_entry() - salary_slip.load_from_db() + salary_slip = frappe.db.get_value("Salary Slip", {"payroll_entry": payroll_entry.name}) + salary_slip = frappe.get_doc("Salary Slip", salary_slip) payroll_je = salary_slip.journal_entry if payroll_je: @@ -143,7 +142,7 @@ class TestPayrollEntry(unittest.TestCase): self.assertEqual(salary_slip.base_net_pay, payment_entry[0].total_debit) self.assertEqual(salary_slip.base_net_pay, payment_entry[0].total_credit) - def test_payroll_entry_with_employee_cost_center(self): # pylint: disable=no-self-use + def test_payroll_entry_with_employee_cost_center(self): for data in frappe.get_all("Salary Component", fields=["name"]): if not frappe.db.get_value( "Salary Component Account", {"parent": data.name, "company": "_Test Company"}, "name" @@ -356,8 +355,114 @@ class TestPayrollEntry(unittest.TestCase): if salary_slip.docstatus == 0: frappe.delete_doc("Salary Slip", name) + def test_salary_slip_operation_queueing(self): + # setup + company = erpnext.get_default_company() + company_doc = frappe.get_doc("Company", company) + employee = frappe.db.get_value("Employee", {"company": company}) + make_salary_structure( + "_Test Salary Structure", + "Monthly", + employee, + company=company, + currency=company_doc.default_currency, + ) -def make_payroll_entry(**args): + # enqueue salary slip creation via payroll entry + # Payroll Entry status should change to Queued + dates = get_start_end_dates("Monthly", nowdate()) + payroll_entry = get_payroll_entry_data( + start_date=dates.start_date, + end_date=dates.end_date, + payable_account=company_doc.default_payroll_payable_account, + currency=company_doc.default_currency, + ) + frappe.flags.enqueue_payroll_entry = True + payroll_entry.create_salary_slips() + payroll_entry.reload() + + self.assertEqual(payroll_entry.status, "Queued") + frappe.flags.enqueue_payroll_entry = False + + def test_salary_slip_operation_failure(self): + # setup + company = erpnext.get_default_company() + company_doc = frappe.get_doc("Company", company) + employee = frappe.db.get_value("Employee", {"company": company}) + salary_structure = make_salary_structure( + "_Test Salary Structure", + "Monthly", + employee, + company=company, + currency=company_doc.default_currency, + ) + + # reset account in component to test submission failure + component = frappe.get_doc("Salary Component", salary_structure.earnings[0].salary_component) + component.accounts = [] + component.save() + + # salary slip submission via payroll entry + # Payroll Entry status should change to Failed because of the missing account setup + dates = get_start_end_dates("Monthly", nowdate()) + payroll_entry = get_payroll_entry_data( + start_date=dates.start_date, + end_date=dates.end_date, + payable_account=company_doc.default_payroll_payable_account, + currency=company_doc.default_currency, + ) + payroll_entry.create_salary_slips() + payroll_entry.submit_salary_slips() + + payroll_entry.reload() + self.assertEqual(payroll_entry.status, "Failed") + self.assertIsNotNone(payroll_entry.error_message) + + # set accounts + for data in frappe.get_all("Salary Component", fields=["name"]): + if not frappe.db.get_value( + "Salary Component Account", {"parent": data.name, "company": company}, "name" + ): + set_salary_component_account(data.name, company_list=[company]) + + # Payroll Entry successful, status should change to Submitted + payroll_entry.submit_salary_slips() + payroll_entry.reload() + self.assertEqual(payroll_entry.status, "Submitted") + self.assertEqual(payroll_entry.error_message, "") + + def test_payroll_entry_status(self): + company = erpnext.get_default_company() + for data in frappe.get_all("Salary Component", fields=["name"]): + if not frappe.db.get_value( + "Salary Component Account", {"parent": data.name, "company": company}, "name" + ): + set_salary_component_account(data.name) + + employee = frappe.db.get_value("Employee", {"company": company}) + company_doc = frappe.get_doc("Company", company) + make_salary_structure( + "_Test Salary Structure", + "Monthly", + employee, + company=company, + currency=company_doc.default_currency, + ) + dates = get_start_end_dates("Monthly", nowdate()) + payroll_entry = get_payroll_entry_data( + start_date=dates.start_date, + end_date=dates.end_date, + payable_account=company_doc.default_payroll_payable_account, + currency=company_doc.default_currency, + ) + payroll_entry.submit() + self.assertEqual(payroll_entry.status, "Submitted") + + payroll_entry.cancel() + self.assertEqual(payroll_entry.status, "Cancelled") + + +def get_payroll_entry_data(**args): args = frappe._dict(args) payroll_entry = frappe.new_doc("Payroll Entry") @@ -381,7 +486,13 @@ def make_payroll_entry(**args): payroll_entry.fill_employee_details() payroll_entry.save() - payroll_entry.create_salary_slips() + + return payroll_entry + + +def make_payroll_entry(**args): + payroll_entry = get_payroll_entry_data(**args) + payroll_entry.submit() payroll_entry.submit_salary_slips() if payroll_entry.get_sal_slip_list(ss_status=1): payroll_entry.make_payment_entry() @@ -421,12 +532,3 @@ def make_holiday(holiday_list_name): ).insert() return holiday_list_name - - -def get_salary_slip(user, period, salary_structure): - salary_slip = make_employee_salary_slip(user, period, salary_structure) - salary_slip.exchange_rate = 70 - salary_slip.calculate_net_pay() - salary_slip.db_update() - - return salary_slip diff --git a/erpnext/payroll/doctype/salary_structure/test_salary_structure.py b/erpnext/payroll/doctype/salary_structure/test_salary_structure.py index e9b5ed2261d..9a0e8188f84 100644 --- a/erpnext/payroll/doctype/salary_structure/test_salary_structure.py +++ b/erpnext/payroll/doctype/salary_structure/test_salary_structure.py @@ -169,9 +169,6 @@ def make_salary_structure( payroll_period=None, include_flexi_benefits=False, ): - if test_tax: - frappe.db.sql("""delete from `tabSalary Structure` where name=%s""", (salary_structure)) - if frappe.db.exists("Salary Structure", salary_structure): frappe.db.delete("Salary Structure", salary_structure) From a0c412a0dd23aeb8181ec49cced5da4e1c908d81 Mon Sep 17 00:00:00 2001 From: Mitchy25 <42224026+Mitchy25@users.noreply.github.com> Date: Tue, 31 May 2022 15:25:09 +1200 Subject: [PATCH 022/105] Ignore Cancelled GL Entries Profitability Analysis includes 'is_cancelled' GL Entries which means that the profit numbers are incorrect. This change will ensure that the profit figures ignore cancelled GL Entries. --- .../report/profitability_analysis/profitability_analysis.py | 1 + 1 file changed, 1 insertion(+) diff --git a/erpnext/accounts/report/profitability_analysis/profitability_analysis.py b/erpnext/accounts/report/profitability_analysis/profitability_analysis.py index 3e7aa1e3680..183e279fe5d 100644 --- a/erpnext/accounts/report/profitability_analysis/profitability_analysis.py +++ b/erpnext/accounts/report/profitability_analysis/profitability_analysis.py @@ -211,6 +211,7 @@ def set_gl_entries_by_account( {additional_conditions} and posting_date <= %(to_date)s and {based_on} is not null + and is_cancelled = 0 order by {based_on}, posting_date""".format( additional_conditions="\n".join(additional_conditions), based_on=based_on ), From 34925a3a8c4306723f7e1ccc5af2763b6f3cf2e0 Mon Sep 17 00:00:00 2001 From: Rucha Mahabal Date: Wed, 4 May 2022 17:21:19 +0530 Subject: [PATCH 023/105] fix: HRA Exemption calculation in case of multiple salary structure assignments --- erpnext/hr/utils.py | 24 +++-- erpnext/regional/india/utils.py | 152 ++++++++++++++++++++++---------- 2 files changed, 115 insertions(+), 61 deletions(-) diff --git a/erpnext/hr/utils.py b/erpnext/hr/utils.py index 269e4aae310..c730b199247 100644 --- a/erpnext/hr/utils.py +++ b/erpnext/hr/utils.py @@ -439,20 +439,18 @@ def check_effective_date(from_date, to_date, frequency, based_on_date_of_joining return False -def get_salary_assignment(employee, date): - assignment = frappe.db.sql( - """ - select * from `tabSalary Structure Assignment` - where employee=%(employee)s - and docstatus = 1 - and %(on_date)s >= from_date order by from_date desc limit 1""", - { - "employee": employee, - "on_date": date, - }, - as_dict=1, +def get_salary_assignments(employee, payroll_period): + start_date, end_date = frappe.db.get_value( + "Payroll Period", payroll_period, ["start_date", "end_date"] ) - return assignment[0] if assignment else None + assignments = frappe.db.get_all( + "Salary Structure Assignment", + filters={"employee": employee, "docstatus": 1, "from_date": ["between", (start_date, end_date)]}, + fields=["*"], + order_by="from_date", + ) + + return assignments def get_sal_slip_total_benefit_given(employee, payroll_period, component=False): diff --git a/erpnext/regional/india/utils.py b/erpnext/regional/india/utils.py index 6a7e1133908..6aa1f1f277c 100644 --- a/erpnext/regional/india/utils.py +++ b/erpnext/regional/india/utils.py @@ -1,14 +1,24 @@ import json +import math import re import frappe from frappe import _ from frappe.model.utils import get_fetch_values -from frappe.utils import cint, cstr, date_diff, flt, getdate, nowdate +from frappe.utils import ( + add_days, + cint, + cstr, + date_diff, + flt, + get_link_to_form, + getdate, + month_diff, +) from erpnext.controllers.accounts_controller import get_taxes_and_charges from erpnext.controllers.taxes_and_totals import get_itemised_tax, get_itemised_taxable_amount -from erpnext.hr.utils import get_salary_assignment +from erpnext.hr.utils import get_salary_assignments from erpnext.payroll.doctype.salary_structure.salary_structure import make_salary_slip from erpnext.regional.india import number_state_mapping, state_numbers, states @@ -360,44 +370,55 @@ def calculate_annual_eligible_hra_exemption(doc): "Company", doc.company, ["basic_component", "hra_component"] ) if not (basic_component and hra_component): - frappe.throw(_("Please mention Basic and HRA component in Company")) - annual_exemption, monthly_exemption, hra_amount = 0, 0, 0 + frappe.throw( + _("Please set Basic and HRA component in Company {0}").format( + get_link_to_form("Company", doc.company) + ) + ) + + annual_exemption = monthly_exemption = hra_amount = basic_amount = 0 + if hra_component and basic_component: - assignment = get_salary_assignment(doc.employee, nowdate()) - if assignment: - hra_component_exists = frappe.db.exists( - "Salary Detail", - { - "parent": assignment.salary_structure, - "salary_component": hra_component, - "parentfield": "earnings", - "parenttype": "Salary Structure", - }, - ) + assignments = get_salary_assignments(doc.employee, doc.payroll_period) - if hra_component_exists: - basic_amount, hra_amount = get_component_amt_from_salary_slip( - doc.employee, assignment.salary_structure, basic_component, hra_component - ) - if hra_amount: - if doc.monthly_house_rent: - annual_exemption = calculate_hra_exemption( - assignment.salary_structure, - basic_amount, - hra_amount, - doc.monthly_house_rent, - doc.rented_in_metro_city, - ) - if annual_exemption > 0: - monthly_exemption = annual_exemption / 12 - else: - annual_exemption = 0 - - elif doc.docstatus == 1: + if not assignments and doc.docstatus == 1: frappe.throw( - _("Salary Structure must be submitted before submission of Tax Ememption Declaration") + _("Salary Structure must be submitted before submission of {0}").format(doc.doctype) ) + assignment_dates = [assignment.from_date for assignment in assignments] + + for idx, assignment in enumerate(assignments): + if has_hra_component(assignment.salary_structure, hra_component): + basic_salary_amt, hra_salary_amt = get_component_amt_from_salary_slip( + doc.employee, + assignment.salary_structure, + basic_component, + hra_component, + assignment.from_date, + ) + to_date = get_end_date_for_assignment(assignment_dates, idx, doc.payroll_period) + + frequency = frappe.get_value( + "Salary Structure", assignment.salary_structure, "payroll_frequency" + ) + basic_amount += get_component_pay(frequency, basic_salary_amt, assignment.from_date, to_date) + hra_amount += get_component_pay(frequency, hra_salary_amt, assignment.from_date, to_date) + + if hra_amount: + if doc.monthly_house_rent: + annual_exemption = calculate_hra_exemption( + assignment.salary_structure, + basic_amount, + hra_amount, + doc.monthly_house_rent, + doc.rented_in_metro_city, + ) + if annual_exemption > 0: + monthly_exemption = annual_exemption / 12 + else: + annual_exemption = 0 + return frappe._dict( { "hra_amount": hra_amount, @@ -407,10 +428,44 @@ def calculate_annual_eligible_hra_exemption(doc): ) -def get_component_amt_from_salary_slip(employee, salary_structure, basic_component, hra_component): +def has_hra_component(salary_structure, hra_component): + return frappe.db.exists( + "Salary Detail", + { + "parent": salary_structure, + "salary_component": hra_component, + "parentfield": "earnings", + "parenttype": "Salary Structure", + }, + ) + + +def get_end_date_for_assignment(assignment_dates, idx, payroll_period): + end_date = None + + try: + end_date = assignment_dates[idx + 1] + end_date = add_days(end_date, -1) + except IndexError: + pass + + if not end_date: + end_date = frappe.db.get_value("Payroll Period", payroll_period, "end_date") + + return end_date + + +def get_component_amt_from_salary_slip( + employee, salary_structure, basic_component, hra_component, from_date +): salary_slip = make_salary_slip( salary_structure, employee=employee, for_preview=1, ignore_permissions=True ) + # generate salary slip as per assignment on "from_date" + salary_slip.posting_date = from_date + salary_slip.start_date = salary_slip.end_date = None + salary_slip.run_method("process_salary_structure", for_preview=True) + basic_amt, hra_amt = 0, 0 for earning in salary_slip.earnings: if earning.salary_component == basic_component: @@ -423,36 +478,37 @@ def get_component_amt_from_salary_slip(employee, salary_structure, basic_compone def calculate_hra_exemption( - salary_structure, basic, monthly_hra, monthly_house_rent, rented_in_metro_city + salary_structure, annual_basic, annual_hra, monthly_house_rent, rented_in_metro_city ): # TODO make this configurable exemptions = [] - frequency = frappe.get_value("Salary Structure", salary_structure, "payroll_frequency") # case 1: The actual amount allotted by the employer as the HRA. - exemptions.append(get_annual_component_pay(frequency, monthly_hra)) - - actual_annual_rent = monthly_house_rent * 12 - annual_basic = get_annual_component_pay(frequency, basic) + exemptions.append(annual_hra) # case 2: Actual rent paid less 10% of the basic salary. + actual_annual_rent = monthly_house_rent * 12 exemptions.append(flt(actual_annual_rent) - flt(annual_basic * 0.1)) + # case 3: 50% of the basic salary, if the employee is staying in a metro city (40% for a non-metro city). exemptions.append(annual_basic * 0.5 if rented_in_metro_city else annual_basic * 0.4) + # return minimum of 3 cases return min(exemptions) -def get_annual_component_pay(frequency, amount): +def get_component_pay(frequency, amount, from_date, to_date): + days = date_diff(to_date, from_date) + 1 + if frequency == "Daily": - return amount * 365 + return amount * days elif frequency == "Weekly": - return amount * 52 + return amount * math.ceil(days / 7) elif frequency == "Fortnightly": - return amount * 26 + return amount * math.ceil(days / 15) elif frequency == "Monthly": - return amount * 12 + return amount * month_diff(to_date, from_date) elif frequency == "Bimonthly": - return amount * 6 + return amount * math.ceil(days / 60) def validate_house_rent_dates(doc): From 2b65c9616ff81ac8e8a1d6352965adbbd5b7a21e Mon Sep 17 00:00:00 2001 From: Rucha Mahabal Date: Wed, 11 May 2022 16:43:13 +0530 Subject: [PATCH 024/105] fix: component pay calculation --- .../salary_structure/salary_structure.py | 4 ++++ erpnext/regional/india/utils.py | 20 +++++++++---------- 2 files changed, 14 insertions(+), 10 deletions(-) diff --git a/erpnext/payroll/doctype/salary_structure/salary_structure.py b/erpnext/payroll/doctype/salary_structure/salary_structure.py index fa36b7ab2d5..edf17dbfb13 100644 --- a/erpnext/payroll/doctype/salary_structure/salary_structure.py +++ b/erpnext/payroll/doctype/salary_structure/salary_structure.py @@ -253,6 +253,7 @@ def make_salary_slip( source_name, target_doc=None, employee=None, + posting_date=None, as_print=False, print_format=None, for_preview=0, @@ -269,6 +270,9 @@ def make_salary_slip( target.designation = employee_details.designation target.department = employee_details.department + if posting_date: + target.posting_date = posting_date + target.run_method("process_salary_structure", for_preview=for_preview) doc = get_mapped_doc( diff --git a/erpnext/regional/india/utils.py b/erpnext/regional/india/utils.py index 6aa1f1f277c..2fc15653614 100644 --- a/erpnext/regional/india/utils.py +++ b/erpnext/regional/india/utils.py @@ -422,8 +422,8 @@ def calculate_annual_eligible_hra_exemption(doc): return frappe._dict( { "hra_amount": hra_amount, - "annual_exemption": annual_exemption, - "monthly_exemption": monthly_exemption, + "annual_exemption": flt(annual_exemption, doc.precision("annual_hra_exemption")), + "monthly_exemption": flt(monthly_exemption, doc.precision("monthly_hra_exemption")), } ) @@ -459,12 +459,12 @@ def get_component_amt_from_salary_slip( employee, salary_structure, basic_component, hra_component, from_date ): salary_slip = make_salary_slip( - salary_structure, employee=employee, for_preview=1, ignore_permissions=True + salary_structure, + employee=employee, + for_preview=1, + ignore_permissions=True, + posting_date=from_date, ) - # generate salary slip as per assignment on "from_date" - salary_slip.posting_date = from_date - salary_slip.start_date = salary_slip.end_date = None - salary_slip.run_method("process_salary_structure", for_preview=True) basic_amt, hra_amt = 0, 0 for earning in salary_slip.earnings: @@ -502,13 +502,13 @@ def get_component_pay(frequency, amount, from_date, to_date): if frequency == "Daily": return amount * days elif frequency == "Weekly": - return amount * math.ceil(days / 7) + return amount * math.floor(days / 7) elif frequency == "Fortnightly": - return amount * math.ceil(days / 15) + return amount * math.floor(days / 14) elif frequency == "Monthly": return amount * month_diff(to_date, from_date) elif frequency == "Bimonthly": - return amount * math.ceil(days / 60) + return amount * (month_diff(to_date, from_date) / 2) def validate_house_rent_dates(doc): From 5e96a46c87e8eb861a81d74e45910187758f9c57 Mon Sep 17 00:00:00 2001 From: Rucha Mahabal Date: Wed, 11 May 2022 16:45:20 +0530 Subject: [PATCH 025/105] test: HRA Exemption in Employee Tax Exemption Declaration --- ...test_employee_tax_exemption_declaration.py | 283 +++++++++++++++++- .../salary_structure/test_salary_structure.py | 9 +- 2 files changed, 288 insertions(+), 4 deletions(-) diff --git a/erpnext/payroll/doctype/employee_tax_exemption_declaration/test_employee_tax_exemption_declaration.py b/erpnext/payroll/doctype/employee_tax_exemption_declaration/test_employee_tax_exemption_declaration.py index 1d90e7383fe..6986bce6709 100644 --- a/erpnext/payroll/doctype/employee_tax_exemption_declaration/test_employee_tax_exemption_declaration.py +++ b/erpnext/payroll/doctype/employee_tax_exemption_declaration/test_employee_tax_exemption_declaration.py @@ -4,13 +4,15 @@ import unittest import frappe +from frappe.tests.utils import FrappeTestCase +from frappe.utils import add_months, getdate import erpnext from erpnext.hr.doctype.employee.test_employee import make_employee from erpnext.hr.utils import DuplicateDeclarationError -class TestEmployeeTaxExemptionDeclaration(unittest.TestCase): +class TestEmployeeTaxExemptionDeclaration(FrappeTestCase): def setUp(self): make_employee("employee@taxexepmtion.com") make_employee("employee1@taxexepmtion.com") @@ -112,6 +114,257 @@ class TestEmployeeTaxExemptionDeclaration(unittest.TestCase): self.assertEqual(declaration.total_exemption_amount, 100000) + def test_india_hra_exemption(self): + setup_hra_exemption_prerequisites("Monthly") + employee = frappe.get_value("Employee", {"user_id": "employee@taxexepmtion.com"}, "name") + + declaration = frappe.get_doc( + { + "doctype": "Employee Tax Exemption Declaration", + "employee": employee, + "company": "Test Company", + "payroll_period": "_Test Payroll Period 1", + "currency": "INR", + "monthly_house_rent": 50000, + "rented_in_metro_city": 1, + "declarations": [ + dict( + exemption_sub_category="_Test Sub Category", + exemption_category="_Test Category", + amount=80000, + ), + dict( + exemption_sub_category="_Test1 Sub Category", + exemption_category="_Test Category", + amount=60000, + ), + ], + } + ).insert() + + # Monthly HRA received = 3000 + # should set HRA exemption as per actual annual HRA because that's the minimum + self.assertEqual(declaration.monthly_hra_exemption, 3000) + self.assertEqual(declaration.annual_hra_exemption, 36000) + # 100000 Standard Exemption + 36000 HRA exemption + self.assertEqual(declaration.total_exemption_amount, 136000) + + def test_india_hra_exemption_with_daily_payroll_frequency(self): + setup_hra_exemption_prerequisites("Daily") + employee = frappe.get_value("Employee", {"user_id": "employee@taxexepmtion.com"}, "name") + + declaration = frappe.get_doc( + { + "doctype": "Employee Tax Exemption Declaration", + "employee": employee, + "company": "Test Company", + "payroll_period": "_Test Payroll Period 1", + "currency": "INR", + "monthly_house_rent": 170000, + "rented_in_metro_city": 1, + "declarations": [ + dict( + exemption_sub_category="_Test1 Sub Category", + exemption_category="_Test Category", + amount=60000, + ), + ], + } + ).insert() + + # Daily HRA received = 3000 + # should set HRA exemption as per (rent - 10% of Basic Salary), that's the minimum + self.assertEqual(declaration.monthly_hra_exemption, 17916.67) + self.assertEqual(declaration.annual_hra_exemption, 215000) + # 50000 Standard Exemption + 215000 HRA exemption + self.assertEqual(declaration.total_exemption_amount, 265000) + + def test_india_hra_exemption_with_weekly_payroll_frequency(self): + setup_hra_exemption_prerequisites("Weekly") + employee = frappe.get_value("Employee", {"user_id": "employee@taxexepmtion.com"}, "name") + + declaration = frappe.get_doc( + { + "doctype": "Employee Tax Exemption Declaration", + "employee": employee, + "company": "Test Company", + "payroll_period": "_Test Payroll Period 1", + "currency": "INR", + "monthly_house_rent": 170000, + "rented_in_metro_city": 1, + "declarations": [ + dict( + exemption_sub_category="_Test1 Sub Category", + exemption_category="_Test Category", + amount=60000, + ), + ], + } + ).insert() + + # Weekly HRA received = 3000 + # should set HRA exemption as per actual annual HRA because that's the minimum + self.assertEqual(declaration.monthly_hra_exemption, 13000) + self.assertEqual(declaration.annual_hra_exemption, 156000) + # 50000 Standard Exemption + 156000 HRA exemption + self.assertEqual(declaration.total_exemption_amount, 206000) + + def test_india_hra_exemption_with_fortnightly_payroll_frequency(self): + setup_hra_exemption_prerequisites("Fortnightly") + employee = frappe.get_value("Employee", {"user_id": "employee@taxexepmtion.com"}, "name") + + declaration = frappe.get_doc( + { + "doctype": "Employee Tax Exemption Declaration", + "employee": employee, + "company": "Test Company", + "payroll_period": "_Test Payroll Period 1", + "currency": "INR", + "monthly_house_rent": 170000, + "rented_in_metro_city": 1, + "declarations": [ + dict( + exemption_sub_category="_Test1 Sub Category", + exemption_category="_Test Category", + amount=60000, + ), + ], + } + ).insert() + + # Fortnightly HRA received = 3000 + # should set HRA exemption as per actual annual HRA because that's the minimum + self.assertEqual(declaration.monthly_hra_exemption, 6500) + self.assertEqual(declaration.annual_hra_exemption, 78000) + # 50000 Standard Exemption + 78000 HRA exemption + self.assertEqual(declaration.total_exemption_amount, 128000) + + def test_india_hra_exemption_with_bimonthly_payroll_frequency(self): + setup_hra_exemption_prerequisites("Bimonthly") + employee = frappe.get_value("Employee", {"user_id": "employee@taxexepmtion.com"}, "name") + + declaration = frappe.get_doc( + { + "doctype": "Employee Tax Exemption Declaration", + "employee": employee, + "company": "Test Company", + "payroll_period": "_Test Payroll Period 1", + "currency": "INR", + "monthly_house_rent": 50000, + "rented_in_metro_city": 1, + "declarations": [ + dict( + exemption_sub_category="_Test Sub Category", + exemption_category="_Test Category", + amount=80000, + ), + dict( + exemption_sub_category="_Test1 Sub Category", + exemption_category="_Test Category", + amount=60000, + ), + ], + } + ).insert() + + # Bimonthly HRA received = 3000 + # should set HRA exemption as per actual annual HRA because that's the minimum + self.assertEqual(declaration.monthly_hra_exemption, 1500) + self.assertEqual(declaration.annual_hra_exemption, 18000) + # 100000 Standard Exemption + 18000 HRA exemption + self.assertEqual(declaration.total_exemption_amount, 118000) + + def test_india_hra_exemption_with_multiple_salary_structure_assignments(self): + from erpnext.payroll.doctype.salary_slip.test_salary_slip import create_tax_slab + from erpnext.payroll.doctype.salary_structure.test_salary_structure import ( + create_salary_structure_assignment, + make_salary_structure, + ) + + payroll_period = create_payroll_period(name="_Test Payroll Period 1", company="_Test Company") + + create_tax_slab( + payroll_period, + allow_tax_exemption=True, + currency="INR", + effective_date=getdate("2019-04-01"), + company="_Test Company", + ) + + frappe.db.set_value( + "Company", "_Test Company", {"basic_component": "Basic Salary", "hra_component": "HRA"} + ) + + employee = frappe.get_value("Employee", {"user_id": "employee@taxexepmtion.com"}, "name") + + # salary structure with base 50000, HRA 3000 + make_salary_structure( + "Monthly Structure for HRA Exemption 1", + "Monthly", + employee=employee, + company="_Test Company", + currency="INR", + payroll_period=payroll_period.name, + from_date=payroll_period.start_date, + ) + + # salary structure with base 70000, HRA = base * 0.2 = 14000 + salary_structure = make_salary_structure( + "Monthly Structure for HRA Exemption 2", + "Monthly", + employee=employee, + company="_Test Company", + currency="INR", + payroll_period=payroll_period.name, + from_date=payroll_period.start_date, + dont_submit=True, + ) + for component_row in salary_structure.earnings: + if component_row.salary_component == "HRA": + component_row.amount = 0 + component_row.amount_based_on_formula = 1 + component_row.formula = "base * 0.2" + break + + salary_structure.submit() + + create_salary_structure_assignment( + employee, + salary_structure.name, + from_date=add_months(payroll_period.start_date, 6), + company="_Test Company", + currency="INR", + payroll_period=payroll_period.name, + base=70000, + allow_duplicate=True, + ) + + declaration = frappe.get_doc( + { + "doctype": "Employee Tax Exemption Declaration", + "employee": employee, + "company": "Test Company", + "payroll_period": "_Test Payroll Period 1", + "currency": "INR", + "monthly_house_rent": 50000, + "rented_in_metro_city": 1, + "declarations": [ + dict( + exemption_sub_category="_Test1 Sub Category", + exemption_category="_Test Category", + amount=60000, + ), + ], + } + ).insert() + + # Monthly HRA received = 50000 * 6 months + 70000 * 6 months + # should set HRA exemption as per actual annual HRA because that's the minimum + self.assertEqual(declaration.monthly_hra_exemption, 8500) + self.assertEqual(declaration.annual_hra_exemption, 102000) + # 50000 Standard Exemption + 102000 HRA exemption + self.assertEqual(declaration.total_exemption_amount, 152000) + def create_payroll_period(**args): args = frappe._dict(args) @@ -163,3 +416,31 @@ def create_exemption_category(): "is_active": 1, } ).insert() + + +def setup_hra_exemption_prerequisites(frequency): + from erpnext.payroll.doctype.salary_slip.test_salary_slip import create_tax_slab + from erpnext.payroll.doctype.salary_structure.test_salary_structure import make_salary_structure + + payroll_period = create_payroll_period(name="_Test Payroll Period 1", company="_Test Company") + + create_tax_slab( + payroll_period, + allow_tax_exemption=True, + currency="INR", + effective_date=getdate("2019-04-01"), + company="_Test Company", + ) + + make_salary_structure( + f"{frequency} Structure for HRA Exemption", + frequency, + employee=frappe.get_value("Employee", {"user_id": "employee@taxexepmtion.com"}, "name"), + company="_Test Company", + currency="INR", + payroll_period=payroll_period, + ) + + frappe.db.set_value( + "Company", "_Test Company", {"basic_component": "Basic Salary", "hra_component": "HRA"} + ) diff --git a/erpnext/payroll/doctype/salary_structure/test_salary_structure.py b/erpnext/payroll/doctype/salary_structure/test_salary_structure.py index e9b5ed2261d..5c78e8f0375 100644 --- a/erpnext/payroll/doctype/salary_structure/test_salary_structure.py +++ b/erpnext/payroll/doctype/salary_structure/test_salary_structure.py @@ -230,9 +230,12 @@ def create_salary_structure_assignment( company=None, currency=erpnext.get_default_currency(), payroll_period=None, + base=None, + allow_duplicate=False, ): - - if frappe.db.exists("Salary Structure Assignment", {"employee": employee}): + if not allow_duplicate and frappe.db.exists( + "Salary Structure Assignment", {"employee": employee} + ): frappe.db.sql("""delete from `tabSalary Structure Assignment` where employee=%s""", (employee)) if not payroll_period: @@ -245,7 +248,7 @@ def create_salary_structure_assignment( salary_structure_assignment = frappe.new_doc("Salary Structure Assignment") salary_structure_assignment.employee = employee - salary_structure_assignment.base = 50000 + salary_structure_assignment.base = base or 50000 salary_structure_assignment.variable = 5000 if not from_date: From 00adda7c8dab167e6679a65595f5bcb8a0f1f9d6 Mon Sep 17 00:00:00 2001 From: Rucha Mahabal Date: Wed, 11 May 2022 18:26:33 +0530 Subject: [PATCH 026/105] fix: Tax Declaration tests and amount precision --- .../employee_tax_exemption_declaration.py | 19 ++++++-- ...test_employee_tax_exemption_declaration.py | 48 +++++++++---------- erpnext/regional/india/utils.py | 4 +- 3 files changed, 41 insertions(+), 30 deletions(-) diff --git a/erpnext/payroll/doctype/employee_tax_exemption_declaration/employee_tax_exemption_declaration.py b/erpnext/payroll/doctype/employee_tax_exemption_declaration/employee_tax_exemption_declaration.py index c0ef2eee78c..3d1d96598fc 100644 --- a/erpnext/payroll/doctype/employee_tax_exemption_declaration/employee_tax_exemption_declaration.py +++ b/erpnext/payroll/doctype/employee_tax_exemption_declaration/employee_tax_exemption_declaration.py @@ -33,7 +33,9 @@ class EmployeeTaxExemptionDeclaration(Document): self.total_declared_amount += flt(d.amount) def set_total_exemption_amount(self): - self.total_exemption_amount = get_total_exemption_amount(self.declarations) + self.total_exemption_amount = flt( + get_total_exemption_amount(self.declarations), self.precision("total_exemption_amount") + ) def calculate_hra_exemption(self): self.salary_structure_hra, self.annual_hra_exemption, self.monthly_hra_exemption = 0, 0, 0 @@ -41,9 +43,18 @@ class EmployeeTaxExemptionDeclaration(Document): hra_exemption = calculate_annual_eligible_hra_exemption(self) if hra_exemption: self.total_exemption_amount += hra_exemption["annual_exemption"] - self.salary_structure_hra = hra_exemption["hra_amount"] - self.annual_hra_exemption = hra_exemption["annual_exemption"] - self.monthly_hra_exemption = hra_exemption["monthly_exemption"] + self.total_exemption_amount = flt( + self.total_exemption_amount, self.precision("total_exemption_amount") + ) + self.salary_structure_hra = flt( + hra_exemption["hra_amount"], self.precision("salary_structure_hra") + ) + self.annual_hra_exemption = flt( + hra_exemption["annual_exemption"], self.precision("annual_hra_exemption") + ) + self.monthly_hra_exemption = flt( + hra_exemption["monthly_exemption"], self.precision("monthly_hra_exemption") + ) @frappe.whitelist() diff --git a/erpnext/payroll/doctype/employee_tax_exemption_declaration/test_employee_tax_exemption_declaration.py b/erpnext/payroll/doctype/employee_tax_exemption_declaration/test_employee_tax_exemption_declaration.py index 6986bce6709..e158cc31bbc 100644 --- a/erpnext/payroll/doctype/employee_tax_exemption_declaration/test_employee_tax_exemption_declaration.py +++ b/erpnext/payroll/doctype/employee_tax_exemption_declaration/test_employee_tax_exemption_declaration.py @@ -14,17 +14,18 @@ from erpnext.hr.utils import DuplicateDeclarationError class TestEmployeeTaxExemptionDeclaration(FrappeTestCase): def setUp(self): - make_employee("employee@taxexepmtion.com") - make_employee("employee1@taxexepmtion.com") - create_payroll_period() + make_employee("employee@taxexemption.com", company="_Test Company") + make_employee("employee1@taxexemption.com", company="_Test Company") + create_payroll_period(company="_Test Company") create_exemption_category() - frappe.db.sql("""delete from `tabEmployee Tax Exemption Declaration`""") + frappe.db.delete("Employee Tax Exemption Declaration") + frappe.db.delete("Salary Structure Assignment") def test_duplicate_category_in_declaration(self): declaration = frappe.get_doc( { "doctype": "Employee Tax Exemption Declaration", - "employee": frappe.get_value("Employee", {"user_id": "employee@taxexepmtion.com"}, "name"), + "employee": frappe.get_value("Employee", {"user_id": "employee@taxexemption.com"}, "name"), "company": erpnext.get_default_company(), "payroll_period": "_Test Payroll Period", "currency": erpnext.get_default_currency(), @@ -48,7 +49,7 @@ class TestEmployeeTaxExemptionDeclaration(FrappeTestCase): declaration = frappe.get_doc( { "doctype": "Employee Tax Exemption Declaration", - "employee": frappe.get_value("Employee", {"user_id": "employee@taxexepmtion.com"}, "name"), + "employee": frappe.get_value("Employee", {"user_id": "employee@taxexemption.com"}, "name"), "company": erpnext.get_default_company(), "payroll_period": "_Test Payroll Period", "currency": erpnext.get_default_currency(), @@ -70,7 +71,7 @@ class TestEmployeeTaxExemptionDeclaration(FrappeTestCase): duplicate_declaration = frappe.get_doc( { "doctype": "Employee Tax Exemption Declaration", - "employee": frappe.get_value("Employee", {"user_id": "employee@taxexepmtion.com"}, "name"), + "employee": frappe.get_value("Employee", {"user_id": "employee@taxexemption.com"}, "name"), "company": erpnext.get_default_company(), "payroll_period": "_Test Payroll Period", "currency": erpnext.get_default_currency(), @@ -85,7 +86,7 @@ class TestEmployeeTaxExemptionDeclaration(FrappeTestCase): ) self.assertRaises(DuplicateDeclarationError, duplicate_declaration.insert) duplicate_declaration.employee = frappe.get_value( - "Employee", {"user_id": "employee1@taxexepmtion.com"}, "name" + "Employee", {"user_id": "employee1@taxexemption.com"}, "name" ) self.assertTrue(duplicate_declaration.insert) @@ -93,7 +94,7 @@ class TestEmployeeTaxExemptionDeclaration(FrappeTestCase): declaration = frappe.get_doc( { "doctype": "Employee Tax Exemption Declaration", - "employee": frappe.get_value("Employee", {"user_id": "employee@taxexepmtion.com"}, "name"), + "employee": frappe.get_value("Employee", {"user_id": "employee@taxexemption.com"}, "name"), "company": erpnext.get_default_company(), "payroll_period": "_Test Payroll Period", "currency": erpnext.get_default_currency(), @@ -116,13 +117,13 @@ class TestEmployeeTaxExemptionDeclaration(FrappeTestCase): def test_india_hra_exemption(self): setup_hra_exemption_prerequisites("Monthly") - employee = frappe.get_value("Employee", {"user_id": "employee@taxexepmtion.com"}, "name") + employee = frappe.get_value("Employee", {"user_id": "employee@taxexemption.com"}, "name") declaration = frappe.get_doc( { "doctype": "Employee Tax Exemption Declaration", "employee": employee, - "company": "Test Company", + "company": "_Test Company", "payroll_period": "_Test Payroll Period 1", "currency": "INR", "monthly_house_rent": 50000, @@ -151,13 +152,13 @@ class TestEmployeeTaxExemptionDeclaration(FrappeTestCase): def test_india_hra_exemption_with_daily_payroll_frequency(self): setup_hra_exemption_prerequisites("Daily") - employee = frappe.get_value("Employee", {"user_id": "employee@taxexepmtion.com"}, "name") + employee = frappe.get_value("Employee", {"user_id": "employee@taxexemption.com"}, "name") declaration = frappe.get_doc( { "doctype": "Employee Tax Exemption Declaration", "employee": employee, - "company": "Test Company", + "company": "_Test Company", "payroll_period": "_Test Payroll Period 1", "currency": "INR", "monthly_house_rent": 170000, @@ -181,13 +182,13 @@ class TestEmployeeTaxExemptionDeclaration(FrappeTestCase): def test_india_hra_exemption_with_weekly_payroll_frequency(self): setup_hra_exemption_prerequisites("Weekly") - employee = frappe.get_value("Employee", {"user_id": "employee@taxexepmtion.com"}, "name") + employee = frappe.get_value("Employee", {"user_id": "employee@taxexemption.com"}, "name") declaration = frappe.get_doc( { "doctype": "Employee Tax Exemption Declaration", "employee": employee, - "company": "Test Company", + "company": "_Test Company", "payroll_period": "_Test Payroll Period 1", "currency": "INR", "monthly_house_rent": 170000, @@ -211,13 +212,13 @@ class TestEmployeeTaxExemptionDeclaration(FrappeTestCase): def test_india_hra_exemption_with_fortnightly_payroll_frequency(self): setup_hra_exemption_prerequisites("Fortnightly") - employee = frappe.get_value("Employee", {"user_id": "employee@taxexepmtion.com"}, "name") + employee = frappe.get_value("Employee", {"user_id": "employee@taxexemption.com"}, "name") declaration = frappe.get_doc( { "doctype": "Employee Tax Exemption Declaration", "employee": employee, - "company": "Test Company", + "company": "_Test Company", "payroll_period": "_Test Payroll Period 1", "currency": "INR", "monthly_house_rent": 170000, @@ -241,13 +242,13 @@ class TestEmployeeTaxExemptionDeclaration(FrappeTestCase): def test_india_hra_exemption_with_bimonthly_payroll_frequency(self): setup_hra_exemption_prerequisites("Bimonthly") - employee = frappe.get_value("Employee", {"user_id": "employee@taxexepmtion.com"}, "name") + employee = frappe.get_value("Employee", {"user_id": "employee@taxexemption.com"}, "name") declaration = frappe.get_doc( { "doctype": "Employee Tax Exemption Declaration", "employee": employee, - "company": "Test Company", + "company": "_Test Company", "payroll_period": "_Test Payroll Period 1", "currency": "INR", "monthly_house_rent": 50000, @@ -281,6 +282,7 @@ class TestEmployeeTaxExemptionDeclaration(FrappeTestCase): make_salary_structure, ) + employee = make_employee("employee@taxexemption2.com", company="_Test Company") payroll_period = create_payroll_period(name="_Test Payroll Period 1", company="_Test Company") create_tax_slab( @@ -295,8 +297,6 @@ class TestEmployeeTaxExemptionDeclaration(FrappeTestCase): "Company", "_Test Company", {"basic_component": "Basic Salary", "hra_component": "HRA"} ) - employee = frappe.get_value("Employee", {"user_id": "employee@taxexepmtion.com"}, "name") - # salary structure with base 50000, HRA 3000 make_salary_structure( "Monthly Structure for HRA Exemption 1", @@ -343,8 +343,8 @@ class TestEmployeeTaxExemptionDeclaration(FrappeTestCase): { "doctype": "Employee Tax Exemption Declaration", "employee": employee, - "company": "Test Company", - "payroll_period": "_Test Payroll Period 1", + "company": "_Test Company", + "payroll_period": payroll_period.name, "currency": "INR", "monthly_house_rent": 50000, "rented_in_metro_city": 1, @@ -435,7 +435,7 @@ def setup_hra_exemption_prerequisites(frequency): make_salary_structure( f"{frequency} Structure for HRA Exemption", frequency, - employee=frappe.get_value("Employee", {"user_id": "employee@taxexepmtion.com"}, "name"), + employee=frappe.get_value("Employee", {"user_id": "employee@taxexemption.com"}, "name"), company="_Test Company", currency="INR", payroll_period=payroll_period, diff --git a/erpnext/regional/india/utils.py b/erpnext/regional/india/utils.py index 2fc15653614..5bbd6863d33 100644 --- a/erpnext/regional/india/utils.py +++ b/erpnext/regional/india/utils.py @@ -422,8 +422,8 @@ def calculate_annual_eligible_hra_exemption(doc): return frappe._dict( { "hra_amount": hra_amount, - "annual_exemption": flt(annual_exemption, doc.precision("annual_hra_exemption")), - "monthly_exemption": flt(monthly_exemption, doc.precision("monthly_hra_exemption")), + "annual_exemption": annual_exemption, + "monthly_exemption": monthly_exemption, } ) From 2e98e9e0b92c1be883419b314c0ba52888a2054c Mon Sep 17 00:00:00 2001 From: Rucha Mahabal Date: Mon, 23 May 2022 14:15:36 +0530 Subject: [PATCH 027/105] test: set country to India before running regional tests --- ...test_employee_tax_exemption_declaration.py | 56 ++++++++++++++++--- erpnext/regional/india/utils.py | 1 + 2 files changed, 50 insertions(+), 7 deletions(-) diff --git a/erpnext/payroll/doctype/employee_tax_exemption_declaration/test_employee_tax_exemption_declaration.py b/erpnext/payroll/doctype/employee_tax_exemption_declaration/test_employee_tax_exemption_declaration.py index e158cc31bbc..67418544585 100644 --- a/erpnext/payroll/doctype/employee_tax_exemption_declaration/test_employee_tax_exemption_declaration.py +++ b/erpnext/payroll/doctype/employee_tax_exemption_declaration/test_employee_tax_exemption_declaration.py @@ -116,6 +116,10 @@ class TestEmployeeTaxExemptionDeclaration(FrappeTestCase): self.assertEqual(declaration.total_exemption_amount, 100000) def test_india_hra_exemption(self): + # set country + current_country = frappe.flags.country + frappe.flags.country = "India" + setup_hra_exemption_prerequisites("Monthly") employee = frappe.get_value("Employee", {"user_id": "employee@taxexemption.com"}, "name") @@ -124,7 +128,7 @@ class TestEmployeeTaxExemptionDeclaration(FrappeTestCase): "doctype": "Employee Tax Exemption Declaration", "employee": employee, "company": "_Test Company", - "payroll_period": "_Test Payroll Period 1", + "payroll_period": "_Test Payroll Period", "currency": "INR", "monthly_house_rent": 50000, "rented_in_metro_city": 1, @@ -150,7 +154,14 @@ class TestEmployeeTaxExemptionDeclaration(FrappeTestCase): # 100000 Standard Exemption + 36000 HRA exemption self.assertEqual(declaration.total_exemption_amount, 136000) + # reset + frappe.flags.country = current_country + def test_india_hra_exemption_with_daily_payroll_frequency(self): + # set country + current_country = frappe.flags.country + frappe.flags.country = "India" + setup_hra_exemption_prerequisites("Daily") employee = frappe.get_value("Employee", {"user_id": "employee@taxexemption.com"}, "name") @@ -159,7 +170,7 @@ class TestEmployeeTaxExemptionDeclaration(FrappeTestCase): "doctype": "Employee Tax Exemption Declaration", "employee": employee, "company": "_Test Company", - "payroll_period": "_Test Payroll Period 1", + "payroll_period": "_Test Payroll Period", "currency": "INR", "monthly_house_rent": 170000, "rented_in_metro_city": 1, @@ -180,7 +191,14 @@ class TestEmployeeTaxExemptionDeclaration(FrappeTestCase): # 50000 Standard Exemption + 215000 HRA exemption self.assertEqual(declaration.total_exemption_amount, 265000) + # reset + frappe.flags.country = current_country + def test_india_hra_exemption_with_weekly_payroll_frequency(self): + # set country + current_country = frappe.flags.country + frappe.flags.country = "India" + setup_hra_exemption_prerequisites("Weekly") employee = frappe.get_value("Employee", {"user_id": "employee@taxexemption.com"}, "name") @@ -189,7 +207,7 @@ class TestEmployeeTaxExemptionDeclaration(FrappeTestCase): "doctype": "Employee Tax Exemption Declaration", "employee": employee, "company": "_Test Company", - "payroll_period": "_Test Payroll Period 1", + "payroll_period": "_Test Payroll Period", "currency": "INR", "monthly_house_rent": 170000, "rented_in_metro_city": 1, @@ -210,7 +228,14 @@ class TestEmployeeTaxExemptionDeclaration(FrappeTestCase): # 50000 Standard Exemption + 156000 HRA exemption self.assertEqual(declaration.total_exemption_amount, 206000) + # reset + frappe.flags.country = current_country + def test_india_hra_exemption_with_fortnightly_payroll_frequency(self): + # set country + current_country = frappe.flags.country + frappe.flags.country = "India" + setup_hra_exemption_prerequisites("Fortnightly") employee = frappe.get_value("Employee", {"user_id": "employee@taxexemption.com"}, "name") @@ -219,7 +244,7 @@ class TestEmployeeTaxExemptionDeclaration(FrappeTestCase): "doctype": "Employee Tax Exemption Declaration", "employee": employee, "company": "_Test Company", - "payroll_period": "_Test Payroll Period 1", + "payroll_period": "_Test Payroll Period", "currency": "INR", "monthly_house_rent": 170000, "rented_in_metro_city": 1, @@ -240,7 +265,14 @@ class TestEmployeeTaxExemptionDeclaration(FrappeTestCase): # 50000 Standard Exemption + 78000 HRA exemption self.assertEqual(declaration.total_exemption_amount, 128000) + # reset + frappe.flags.country = current_country + def test_india_hra_exemption_with_bimonthly_payroll_frequency(self): + # set country + current_country = frappe.flags.country + frappe.flags.country = "India" + setup_hra_exemption_prerequisites("Bimonthly") employee = frappe.get_value("Employee", {"user_id": "employee@taxexemption.com"}, "name") @@ -249,7 +281,7 @@ class TestEmployeeTaxExemptionDeclaration(FrappeTestCase): "doctype": "Employee Tax Exemption Declaration", "employee": employee, "company": "_Test Company", - "payroll_period": "_Test Payroll Period 1", + "payroll_period": "_Test Payroll Period", "currency": "INR", "monthly_house_rent": 50000, "rented_in_metro_city": 1, @@ -275,6 +307,9 @@ class TestEmployeeTaxExemptionDeclaration(FrappeTestCase): # 100000 Standard Exemption + 18000 HRA exemption self.assertEqual(declaration.total_exemption_amount, 118000) + # reset + frappe.flags.country = current_country + def test_india_hra_exemption_with_multiple_salary_structure_assignments(self): from erpnext.payroll.doctype.salary_slip.test_salary_slip import create_tax_slab from erpnext.payroll.doctype.salary_structure.test_salary_structure import ( @@ -282,8 +317,12 @@ class TestEmployeeTaxExemptionDeclaration(FrappeTestCase): make_salary_structure, ) + # set country + current_country = frappe.flags.country + frappe.flags.country = "India" + employee = make_employee("employee@taxexemption2.com", company="_Test Company") - payroll_period = create_payroll_period(name="_Test Payroll Period 1", company="_Test Company") + payroll_period = create_payroll_period(name="_Test Payroll Period", company="_Test Company") create_tax_slab( payroll_period, @@ -365,6 +404,9 @@ class TestEmployeeTaxExemptionDeclaration(FrappeTestCase): # 50000 Standard Exemption + 102000 HRA exemption self.assertEqual(declaration.total_exemption_amount, 152000) + # reset + frappe.flags.country = current_country + def create_payroll_period(**args): args = frappe._dict(args) @@ -422,7 +464,7 @@ def setup_hra_exemption_prerequisites(frequency): from erpnext.payroll.doctype.salary_slip.test_salary_slip import create_tax_slab from erpnext.payroll.doctype.salary_structure.test_salary_structure import make_salary_structure - payroll_period = create_payroll_period(name="_Test Payroll Period 1", company="_Test Company") + payroll_period = create_payroll_period(name="_Test Payroll Period", company="_Test Company") create_tax_slab( payroll_period, diff --git a/erpnext/regional/india/utils.py b/erpnext/regional/india/utils.py index 5bbd6863d33..ee48ccb24a4 100644 --- a/erpnext/regional/india/utils.py +++ b/erpnext/regional/india/utils.py @@ -369,6 +369,7 @@ def calculate_annual_eligible_hra_exemption(doc): basic_component, hra_component = frappe.db.get_value( "Company", doc.company, ["basic_component", "hra_component"] ) + if not (basic_component and hra_component): frappe.throw( _("Please set Basic and HRA component in Company {0}").format( From cfe2f8cac14c3f37ba8df8b3c24688306b99d917 Mon Sep 17 00:00:00 2001 From: Rucha Mahabal Date: Mon, 23 May 2022 16:17:39 +0530 Subject: [PATCH 028/105] fix: amount precision for Tax Exemption Proof Submission --- .../employee_tax_exemption_proof_submission.py | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/erpnext/payroll/doctype/employee_tax_exemption_proof_submission/employee_tax_exemption_proof_submission.py b/erpnext/payroll/doctype/employee_tax_exemption_proof_submission/employee_tax_exemption_proof_submission.py index c52efaba592..b3b66b9e7b1 100644 --- a/erpnext/payroll/doctype/employee_tax_exemption_proof_submission/employee_tax_exemption_proof_submission.py +++ b/erpnext/payroll/doctype/employee_tax_exemption_proof_submission/employee_tax_exemption_proof_submission.py @@ -31,7 +31,9 @@ class EmployeeTaxExemptionProofSubmission(Document): self.total_actual_amount += flt(d.amount) def set_total_exemption_amount(self): - self.exemption_amount = get_total_exemption_amount(self.tax_exemption_proofs) + self.exemption_amount = flt( + get_total_exemption_amount(self.tax_exemption_proofs), self.precision("exemption_amount") + ) def calculate_hra_exemption(self): self.monthly_hra_exemption, self.monthly_house_rent, self.total_eligible_hra_exemption = 0, 0, 0 @@ -39,6 +41,13 @@ class EmployeeTaxExemptionProofSubmission(Document): hra_exemption = calculate_hra_exemption_for_period(self) if hra_exemption: self.exemption_amount += hra_exemption["total_eligible_hra_exemption"] - self.monthly_hra_exemption = hra_exemption["monthly_exemption"] - self.monthly_house_rent = hra_exemption["monthly_house_rent"] - self.total_eligible_hra_exemption = hra_exemption["total_eligible_hra_exemption"] + self.exemption_amount = flt(self.exemption_amount, self.precision("exemption_amount")) + self.monthly_hra_exemption = flt( + hra_exemption["monthly_exemption"], self.precision("monthly_hra_exemption") + ) + self.monthly_house_rent = flt( + hra_exemption["monthly_house_rent"], self.precision("monthly_house_rent") + ) + self.total_eligible_hra_exemption = flt( + hra_exemption["total_eligible_hra_exemption"], self.precision("total_eligible_hra_exemption") + ) From ed1ba677d622bb078a9e6c71611bd7ccbcbd6c0c Mon Sep 17 00:00:00 2001 From: Rucha Mahabal Date: Mon, 23 May 2022 16:18:11 +0530 Subject: [PATCH 029/105] test: HRA Exemption in Proof Submission --- ...test_employee_tax_exemption_declaration.py | 6 +- ...employee_tax_exemption_proof_submission.py | 83 ++++++++++++++++--- 2 files changed, 75 insertions(+), 14 deletions(-) diff --git a/erpnext/payroll/doctype/employee_tax_exemption_declaration/test_employee_tax_exemption_declaration.py b/erpnext/payroll/doctype/employee_tax_exemption_declaration/test_employee_tax_exemption_declaration.py index 67418544585..2d8df350118 100644 --- a/erpnext/payroll/doctype/employee_tax_exemption_declaration/test_employee_tax_exemption_declaration.py +++ b/erpnext/payroll/doctype/employee_tax_exemption_declaration/test_employee_tax_exemption_declaration.py @@ -460,11 +460,13 @@ def create_exemption_category(): ).insert() -def setup_hra_exemption_prerequisites(frequency): +def setup_hra_exemption_prerequisites(frequency, employee=None): from erpnext.payroll.doctype.salary_slip.test_salary_slip import create_tax_slab from erpnext.payroll.doctype.salary_structure.test_salary_structure import make_salary_structure payroll_period = create_payroll_period(name="_Test Payroll Period", company="_Test Company") + if not employee: + employee = frappe.get_value("Employee", {"user_id": "employee@taxexemption.com"}, "name") create_tax_slab( payroll_period, @@ -477,7 +479,7 @@ def setup_hra_exemption_prerequisites(frequency): make_salary_structure( f"{frequency} Structure for HRA Exemption", frequency, - employee=frappe.get_value("Employee", {"user_id": "employee@taxexemption.com"}, "name"), + employee=employee, company="_Test Company", currency="INR", payroll_period=payroll_period, diff --git a/erpnext/payroll/doctype/employee_tax_exemption_proof_submission/test_employee_tax_exemption_proof_submission.py b/erpnext/payroll/doctype/employee_tax_exemption_proof_submission/test_employee_tax_exemption_proof_submission.py index 58b2c1af058..416cf316c97 100644 --- a/erpnext/payroll/doctype/employee_tax_exemption_proof_submission/test_employee_tax_exemption_proof_submission.py +++ b/erpnext/payroll/doctype/employee_tax_exemption_proof_submission/test_employee_tax_exemption_proof_submission.py @@ -4,22 +4,26 @@ import unittest import frappe +from frappe.tests.utils import FrappeTestCase +from erpnext.hr.doctype.employee.test_employee import make_employee from erpnext.payroll.doctype.employee_tax_exemption_declaration.test_employee_tax_exemption_declaration import ( create_exemption_category, create_payroll_period, + setup_hra_exemption_prerequisites, ) -class TestEmployeeTaxExemptionProofSubmission(unittest.TestCase): - def setup(self): - make_employee("employee@proofsubmission.com") - create_payroll_period() +class TestEmployeeTaxExemptionProofSubmission(FrappeTestCase): + def setUp(self): + make_employee("employee@proofsubmission.com", company="_Test Company") + create_payroll_period(company="_Test Company") create_exemption_category() - frappe.db.sql("""delete from `tabEmployee Tax Exemption Proof Submission`""") + frappe.db.delete("Employee Tax Exemption Proof Submission") + frappe.db.delete("Salary Structure Assignment") def test_exemption_amount_lesser_than_category_max(self): - declaration = frappe.get_doc( + proof = frappe.get_doc( { "doctype": "Employee Tax Exemption Proof Submission", "employee": frappe.get_value("Employee", {"user_id": "employee@proofsubmission.com"}, "name"), @@ -34,8 +38,8 @@ class TestEmployeeTaxExemptionProofSubmission(unittest.TestCase): ], } ) - self.assertRaises(frappe.ValidationError, declaration.save) - declaration = frappe.get_doc( + self.assertRaises(frappe.ValidationError, proof.save) + proof = frappe.get_doc( { "doctype": "Employee Tax Exemption Proof Submission", "payroll_period": "Test Payroll Period", @@ -50,11 +54,11 @@ class TestEmployeeTaxExemptionProofSubmission(unittest.TestCase): ], } ) - self.assertTrue(declaration.save) - self.assertTrue(declaration.submit) + self.assertTrue(proof.save) + self.assertTrue(proof.submit) def test_duplicate_category_in_proof_submission(self): - declaration = frappe.get_doc( + proof = frappe.get_doc( { "doctype": "Employee Tax Exemption Proof Submission", "employee": frappe.get_value("Employee", {"user_id": "employee@proofsubmission.com"}, "name"), @@ -74,4 +78,59 @@ class TestEmployeeTaxExemptionProofSubmission(unittest.TestCase): ], } ) - self.assertRaises(frappe.ValidationError, declaration.save) + self.assertRaises(frappe.ValidationError, proof.save) + + def test_india_hra_exemption(self): + # set country + current_country = frappe.flags.country + frappe.flags.country = "India" + + employee = frappe.get_value("Employee", {"user_id": "employee@proofsubmission.com"}, "name") + setup_hra_exemption_prerequisites("Monthly", employee) + payroll_period = frappe.db.get_value( + "Payroll Period", "_Test Payroll Period", ["start_date", "end_date"], as_dict=True + ) + + proof = frappe.get_doc( + { + "doctype": "Employee Tax Exemption Proof Submission", + "employee": employee, + "company": "_Test Company", + "payroll_period": "_Test Payroll Period", + "currency": "INR", + "house_rent_payment_amount": 600000, + "rented_in_metro_city": 1, + "rented_from_date": payroll_period.start_date, + "rented_to_date": payroll_period.end_date, + "tax_exemption_proofs": [ + dict( + exemption_sub_category="_Test Sub Category", + exemption_category="_Test Category", + type_of_proof="Test Proof", + amount=100000, + ), + dict( + exemption_sub_category="_Test1 Sub Category", + exemption_category="_Test Category", + type_of_proof="Test Proof", + amount=50000, + ), + ], + } + ).insert() + + self.assertEqual(proof.monthly_house_rent, 50000) + + # Monthly HRA received = 3000 + # should set HRA exemption as per actual annual HRA because that's the minimum + self.assertEqual(proof.monthly_hra_exemption, 3000) + self.assertEqual(proof.total_eligible_hra_exemption, 36000) + + # total exemptions + house rent payment amount + self.assertEqual(proof.total_actual_amount, 750000) + + # 100000 Standard Exemption + 36000 HRA exemption + self.assertEqual(proof.exemption_amount, 136000) + + # reset + frappe.flags.country = current_country From a8f98f3f9684afcf5675876f95d9896461291563 Mon Sep 17 00:00:00 2001 From: maharshivpatel <39730881+maharshivpatel@users.noreply.github.com> Date: Tue, 31 May 2022 12:14:39 +0530 Subject: [PATCH 030/105] feat(india): Improve E-way Bill Cancellation. (#31088) --- erpnext/regional/india/e_invoice/einvoice.js | 41 +++---- erpnext/regional/india/e_invoice/utils.py | 109 +++++++++++++++---- 2 files changed, 110 insertions(+), 40 deletions(-) diff --git a/erpnext/regional/india/e_invoice/einvoice.js b/erpnext/regional/india/e_invoice/einvoice.js index ef24ce791c0..580e6469e2c 100644 --- a/erpnext/regional/india/e_invoice/einvoice.js +++ b/erpnext/regional/india/e_invoice/einvoice.js @@ -150,26 +150,29 @@ erpnext.setup_einvoice_actions = (doctype) => { if (irn && ewaybill && !irn_cancelled && !eway_bill_cancelled) { const action = () => { - let message = __('Cancellation of e-way bill is currently not supported.') + ' '; - message += '

'; - message += __('You must first use the portal to cancel the e-way bill and then update the cancelled status in the ERPNext system.'); - - const dialog = frappe.msgprint({ - title: __('Update E-Way Bill Cancelled Status?'), - message: message, - indicator: 'orange', - primary_action: { - action: function() { - frappe.call({ - method: 'erpnext.regional.india.e_invoice.utils.cancel_eway_bill', - args: { doctype, docname: name }, - freeze: true, - callback: () => frm.reload_doc() && dialog.hide() - }); - } + // This confirm is added to just reduce unnecesory API calls. All required logic is implemented on server side. + frappe.confirm( + __("Have you cancelled e-way bill on the portal?"), + () => { + frappe.call({ + method: "erpnext.regional.india.e_invoice.utils.cancel_eway_bill", + args: { doctype, docname: name }, + freeze: true, + callback: () => frm.reload_doc(), + }); }, - primary_action_label: __('Yes') - }); + () => { + frappe.show_alert( + { + message: __( + "Please cancel e-way bill on the portal first." + ), + indicator: "orange", + }, + 5 + ); + } + ); }; add_custom_button(__("Cancel E-Way Bill"), action); } diff --git a/erpnext/regional/india/e_invoice/utils.py b/erpnext/regional/india/e_invoice/utils.py index e5a1a59e42f..9add09beaf7 100644 --- a/erpnext/regional/india/e_invoice/utils.py +++ b/erpnext/regional/india/e_invoice/utils.py @@ -803,6 +803,8 @@ class GSPConnector: self.gstin_details_url = self.base_url + "/enriched/ei/api/master/gstin" # cancel_ewaybill_url will only work if user have bought ewb api from adaequare. self.cancel_ewaybill_url = self.base_url + "/enriched/ewb/ewayapi?action=CANEWB" + # ewaybill_details_url + ?irn={irn_number} will provide eway bill number and details. + self.ewaybill_details_url = self.base_url + "/enriched/ei/api/ewaybill/irn" self.generate_ewaybill_url = self.base_url + "/enriched/ei/api/ewaybill" self.get_qrcode_url = self.base_url + "/enriched/ei/others/qr/image" @@ -1205,23 +1207,22 @@ class GSPConnector: log_error(data) self.raise_error(True) - def cancel_eway_bill(self, eway_bill, reason, remark): + def get_ewb_details(self): + """ + Get e-Waybill Details by IRN API documentaion for validation is not added yet. + https://einv-apisandbox.nic.in/version1.03/get-ewaybill-details-by-irn.html#validations + NOTE: if ewaybill Validity period lapsed or scanned by officer enroute (not tested yet) it will still return status as "ACT". + """ headers = self.get_headers() - data = json.dumps({"ewbNo": eway_bill, "cancelRsnCode": reason, "cancelRmrk": remark}, indent=4) - headers["username"] = headers["user_name"] - del headers["user_name"] - try: - res = self.make_request("post", self.cancel_ewaybill_url, headers, data) - if res.get("success"): - self.invoice.ewaybill = "" - self.invoice.eway_bill_cancelled = 1 - self.invoice.flags.updater_reference = { - "doctype": self.invoice.doctype, - "docname": self.invoice.name, - "label": _("E-Way Bill Cancelled - {}").format(remark), - } - self.update_invoice() + irn = self.invoice.irn + if not irn: + frappe.throw(_("IRN is mandatory to get E-Waybill Details. Please generate IRN first.")) + try: + params = "?irn={irn}".format(irn=irn) + res = self.make_request("get", self.ewaybill_details_url + params, headers) + if res.get("success"): + return res.get("result") else: raise RequestFailed @@ -1230,9 +1231,65 @@ class GSPConnector: self.raise_error(errors=errors) except Exception: - log_error(data) + log_error() self.raise_error(True) + def update_ewb_details(self, ewb_details=None): + # for any reason user chooses to generate eway bill using portal this will allow to update ewaybill details in the invoice. + if not self.invoice.irn: + frappe.throw(_("IRN is mandatory to update E-Waybill Details. Please generate IRN first.")) + if not ewb_details: + ewb_details = self.get_ewb_details() + if ewb_details: + self.invoice.ewaybill = ewb_details.get("EwbNo") + self.invoice.eway_bill_validity = ewb_details.get("EwbValidTill") + self.invoice.eway_bill_cancelled = 0 if ewb_details.get("Status") == "ACT" else 1 + self.update_invoice() + + def cancel_eway_bill(self): + ewb_details = self.get_ewb_details() + if ewb_details: + ewb_no = str(ewb_details.get("EwbNo")) + ewb_status = ewb_details.get("Status") + if ewb_status == "CNL": + self.invoice.ewaybill = "" + self.invoice.eway_bill_cancelled = 1 + self.invoice.flags.updater_reference = { + "doctype": self.invoice.doctype, + "docname": self.invoice.name, + "label": _("E-Way Bill Cancelled"), + } + self.update_invoice() + frappe.msgprint( + _("E-Way Bill Cancelled successfully"), + indicator="green", + alert=True, + ) + elif ewb_status == "ACT" and self.invoice.ewaybill == ewb_no: + msg = _("E-Way Bill {} is still active.").format(bold(ewb_no)) + msg += "

" + msg += _( + "You must first use the portal to cancel the e-way bill and then update the cancelled status in the ERPNext system." + ) + frappe.msgprint(msg) + elif ewb_status == "ACT" and self.invoice.ewaybill != ewb_no: + # if user cancelled the current eway bill and generated new eway bill using portal, then this will update new ewb number in sales invoice. + msg = _("E-Way Bill No. {0} doesn't match {1} saved in the invoice.").format( + bold(ewb_no), bold(self.invoice.ewaybill) + ) + msg += "
" + msg += _("E-Way Bill No. {} is updated in the invoice.").format(bold(ewb_no)) + frappe.msgprint(msg) + self.update_ewb_details(ewb_details=ewb_details) + else: + # this block should not be ever called but added incase there is any change in API. + msg = _("Unknown E-Way Status Code {}.").format(ewb_status) + msg += "

" + msg += _("Please contact your system administrator.") + frappe.throw(msg) + else: + frappe.msgprint(_("E-Way Bill Details not found for this IRN.")) + def sanitize_error_message(self, message): """ On validation errors, response message looks something like this: @@ -1383,12 +1440,22 @@ def generate_eway_bill(doctype, docname, **kwargs): @frappe.whitelist() def cancel_eway_bill(doctype, docname): - # NOTE: cancel_eway_bill api is disabled by Adequare. - # gsp_connector = GSPConnector(doctype, docname) - # gsp_connector.cancel_eway_bill(eway_bill, reason, remark) + # NOTE: cancel_eway_bill api is disabled by NIC for E-invoice so this will only check if eway bill is canceled or not and update accordingly. + # https://einv-apisandbox.nic.in/version1.03/cancel-eway-bill.html# + gsp_connector = GSPConnector(doctype, docname) + gsp_connector.cancel_eway_bill() - frappe.db.set_value(doctype, docname, "ewaybill", "") - frappe.db.set_value(doctype, docname, "eway_bill_cancelled", 1) + +@frappe.whitelist() +def get_ewb_details(doctype, docname): + gsp_connector = GSPConnector(doctype, docname) + gsp_connector.get_ewb_details() + + +@frappe.whitelist() +def update_ewb_details(doctype, docname): + gsp_connector = GSPConnector(doctype, docname) + gsp_connector.update_ewb_details() @frappe.whitelist() From ddb46c571133c92d4151b65350d896857d38edf1 Mon Sep 17 00:00:00 2001 From: Ankush Menat Date: Tue, 31 May 2022 14:14:27 +0530 Subject: [PATCH 031/105] fix: batch selector flag (#31191) This is broken again after serializing scan actions, which causes selector to trigger before batch_no is set. Solution: for duration of scan disable the selector --- erpnext/public/js/controllers/transaction.js | 6 ------ erpnext/public/js/utils/barcode_scanner.js | 7 ++++++- 2 files changed, 6 insertions(+), 7 deletions(-) diff --git a/erpnext/public/js/controllers/transaction.js b/erpnext/public/js/controllers/transaction.js index d11205a1ad3..edc4b06dca1 100644 --- a/erpnext/public/js/controllers/transaction.js +++ b/erpnext/public/js/controllers/transaction.js @@ -526,12 +526,6 @@ erpnext.TransactionController = class TransactionController extends erpnext.taxe if(!d[k]) d[k] = v; }); - if (d.__disable_batch_serial_selector) { - // reset for future use. - d.__disable_batch_serial_selector = false; - return; - } - if (d.has_batch_no && d.has_serial_no) { d.batch_no = undefined; } diff --git a/erpnext/public/js/utils/barcode_scanner.js b/erpnext/public/js/utils/barcode_scanner.js index eea91ef5fe1..0356fdcd056 100644 --- a/erpnext/public/js/utils/barcode_scanner.js +++ b/erpnext/public/js/utils/barcode_scanner.js @@ -98,6 +98,7 @@ erpnext.utils.BarcodeScanner = class BarcodeScanner { () => this.set_batch_no(row, batch_no), () => this.set_barcode(row, barcode), () => this.clean_up(), + () => this.revert_selector_flag(row, data), () => resolve(row) ]); }); @@ -112,10 +113,14 @@ erpnext.utils.BarcodeScanner = class BarcodeScanner { const require_selecting_serial = has_serial_no && !serial_no; if (!(require_selecting_batch || require_selecting_serial)) { - row.__disable_batch_serial_selector = true; + frappe.flags.hide_serial_batch_dialog = true; } } + revert_selector_flag() { + frappe.flags.hide_serial_batch_dialog = false; + } + set_item(row, item_code) { return new Promise(resolve => { const increment = async (value = 1) => { From 691b34a8eda3d415ebffc9da3baf1d8e9ca2adb4 Mon Sep 17 00:00:00 2001 From: Ankush Menat Date: Tue, 31 May 2022 14:15:13 +0530 Subject: [PATCH 032/105] chore: unnessary args --- erpnext/public/js/utils/barcode_scanner.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/erpnext/public/js/utils/barcode_scanner.js b/erpnext/public/js/utils/barcode_scanner.js index 0356fdcd056..943db07705e 100644 --- a/erpnext/public/js/utils/barcode_scanner.js +++ b/erpnext/public/js/utils/barcode_scanner.js @@ -98,7 +98,7 @@ erpnext.utils.BarcodeScanner = class BarcodeScanner { () => this.set_batch_no(row, batch_no), () => this.set_barcode(row, barcode), () => this.clean_up(), - () => this.revert_selector_flag(row, data), + () => this.revert_selector_flag(), () => resolve(row) ]); }); From a1b7a7983a92906d10c7b860f37167568ab279b6 Mon Sep 17 00:00:00 2001 From: Ankush Menat Date: Tue, 31 May 2022 15:35:40 +0530 Subject: [PATCH 033/105] refactor!: drop naming series tool (#31183) --- .../buying_settings/buying_settings.py | 2 +- .../doctype/website_item/website_item.py | 4 +- erpnext/hr/doctype/hr_settings/hr_settings.py | 2 +- erpnext/patches.txt | 1 + .../v13_0/item_naming_series_not_mandatory.py | 2 +- .../selling_settings/selling_settings.py | 2 +- erpnext/setup/doctype/naming_series/README.md | 1 - .../setup/doctype/naming_series/__init__.py | 0 .../doctype/naming_series/naming_series.js | 88 ----- .../doctype/naming_series/naming_series.json | 132 -------- .../doctype/naming_series/naming_series.py | 303 ------------------ .../naming_series/test_naming_series.py | 35 -- .../doctype/stock_settings/stock_settings.py | 2 +- erpnext/utilities/naming.py | 60 ++++ 14 files changed, 67 insertions(+), 567 deletions(-) delete mode 100644 erpnext/setup/doctype/naming_series/README.md delete mode 100644 erpnext/setup/doctype/naming_series/__init__.py delete mode 100644 erpnext/setup/doctype/naming_series/naming_series.js delete mode 100644 erpnext/setup/doctype/naming_series/naming_series.json delete mode 100644 erpnext/setup/doctype/naming_series/naming_series.py delete mode 100644 erpnext/setup/doctype/naming_series/test_naming_series.py create mode 100644 erpnext/utilities/naming.py diff --git a/erpnext/buying/doctype/buying_settings/buying_settings.py b/erpnext/buying/doctype/buying_settings/buying_settings.py index c52b59e4c0c..7b18cdbedcd 100644 --- a/erpnext/buying/doctype/buying_settings/buying_settings.py +++ b/erpnext/buying/doctype/buying_settings/buying_settings.py @@ -18,7 +18,7 @@ class BuyingSettings(Document): for key in ["supplier_group", "supp_master_name", "maintain_same_rate", "buying_price_list"]: frappe.db.set_default(key, self.get(key, "")) - from erpnext.setup.doctype.naming_series.naming_series import set_by_naming_series + from erpnext.utilities.naming import set_by_naming_series set_by_naming_series( "Supplier", diff --git a/erpnext/e_commerce/doctype/website_item/website_item.py b/erpnext/e_commerce/doctype/website_item/website_item.py index 02ec3bf1f3f..f6fea72f8a4 100644 --- a/erpnext/e_commerce/doctype/website_item/website_item.py +++ b/erpnext/e_commerce/doctype/website_item/website_item.py @@ -34,9 +34,7 @@ class WebsiteItem(WebsiteGenerator): def autoname(self): # use naming series to accomodate items with same name (different item code) - from frappe.model.naming import make_autoname - - from erpnext.setup.doctype.naming_series.naming_series import get_default_naming_series + from frappe.model.naming import get_default_naming_series, make_autoname naming_series = get_default_naming_series("Website Item") if not self.name and naming_series: diff --git a/erpnext/hr/doctype/hr_settings/hr_settings.py b/erpnext/hr/doctype/hr_settings/hr_settings.py index 72a49e285a0..b56f3dbe0d7 100644 --- a/erpnext/hr/doctype/hr_settings/hr_settings.py +++ b/erpnext/hr/doctype/hr_settings/hr_settings.py @@ -22,7 +22,7 @@ class HRSettings(Document): PROCEED_WITH_FREQUENCY_CHANGE = False def set_naming_series(self): - from erpnext.setup.doctype.naming_series.naming_series import set_by_naming_series + from erpnext.utilities.naming import set_by_naming_series set_by_naming_series( "Employee", diff --git a/erpnext/patches.txt b/erpnext/patches.txt index 8c0ebe7a904..785e2baa11b 100644 --- a/erpnext/patches.txt +++ b/erpnext/patches.txt @@ -372,3 +372,4 @@ erpnext.patches.v14_0.discount_accounting_separation erpnext.patches.v14_0.delete_employee_transfer_property_doctype erpnext.patches.v13_0.create_accounting_dimensions_in_orders erpnext.patches.v13_0.set_per_billed_in_return_delivery_note +execute:frappe.delete_doc("DocType", "Naming Series") diff --git a/erpnext/patches/v13_0/item_naming_series_not_mandatory.py b/erpnext/patches/v13_0/item_naming_series_not_mandatory.py index 33fb8f963c5..0235a621ce0 100644 --- a/erpnext/patches/v13_0/item_naming_series_not_mandatory.py +++ b/erpnext/patches/v13_0/item_naming_series_not_mandatory.py @@ -1,6 +1,6 @@ import frappe -from erpnext.setup.doctype.naming_series.naming_series import set_by_naming_series +from erpnext.utilities.naming import set_by_naming_series def execute(): diff --git a/erpnext/selling/doctype/selling_settings/selling_settings.py b/erpnext/selling/doctype/selling_settings/selling_settings.py index 6c098942510..d977807e7dc 100644 --- a/erpnext/selling/doctype/selling_settings/selling_settings.py +++ b/erpnext/selling/doctype/selling_settings/selling_settings.py @@ -27,7 +27,7 @@ class SellingSettings(Document): ]: frappe.db.set_default(key, self.get(key, "")) - from erpnext.setup.doctype.naming_series.naming_series import set_by_naming_series + from erpnext.utilities.naming import set_by_naming_series set_by_naming_series( "Customer", diff --git a/erpnext/setup/doctype/naming_series/README.md b/erpnext/setup/doctype/naming_series/README.md deleted file mode 100644 index 5a9b8ca8618..00000000000 --- a/erpnext/setup/doctype/naming_series/README.md +++ /dev/null @@ -1 +0,0 @@ -Tool to set numbering (naming) series for various DocTypes. \ No newline at end of file diff --git a/erpnext/setup/doctype/naming_series/__init__.py b/erpnext/setup/doctype/naming_series/__init__.py deleted file mode 100644 index e69de29bb2d..00000000000 diff --git a/erpnext/setup/doctype/naming_series/naming_series.js b/erpnext/setup/doctype/naming_series/naming_series.js deleted file mode 100644 index 0fb72abba69..00000000000 --- a/erpnext/setup/doctype/naming_series/naming_series.js +++ /dev/null @@ -1,88 +0,0 @@ -// Copyright (c) 2017, Frappe Technologies Pvt. Ltd. and contributors -// For license information, please see license.txt - - -frappe.ui.form.on("Naming Series", { - onload: function(frm) { - frm.events.get_doc_and_prefix(frm); - }, - - refresh: function(frm) { - frm.disable_save(); - }, - - get_doc_and_prefix: function(frm) { - frappe.call({ - method: "get_transactions", - doc: frm.doc, - callback: function(r) { - frm.set_df_property("select_doc_for_series", "options", r.message.transactions); - frm.set_df_property("prefix", "options", r.message.prefixes); - } - }); - }, - - select_doc_for_series: function(frm) { - frm.set_value("user_must_always_select", 0); - frappe.call({ - method: "get_options", - doc: frm.doc, - callback: function(r) { - frm.set_value("set_options", r.message); - if(r.message && r.message.split('\n')[0]=='') - frm.set_value('user_must_always_select', 1); - frm.refresh(); - } - }); - }, - - prefix: function(frm) { - frappe.call({ - method: "get_current", - doc: frm.doc, - callback: function(r) { - frm.refresh_field("current_value"); - } - }); - }, - - update: function(frm) { - frappe.call({ - method: "update_series", - doc: frm.doc, - callback: function(r) { - frm.events.get_doc_and_prefix(frm); - } - }); - }, - - naming_series_to_check(frm) { - frappe.call({ - method: "preview_series", - doc: frm.doc, - callback: function(r) { - if (!r.exc) { - frm.set_value("preview", r.message); - } else { - frm.set_value("preview", __("Failed to generate preview of series")); - } - } - }); - }, - - add_series(frm) { - const series = frm.doc.naming_series_to_check; - - if (!series) { - frappe.show_alert(__("Please type a valid series.")); - return; - } - - if (!frm.doc.set_options.includes(series)) { - const current_series = frm.doc.set_options; - frm.set_value("set_options", `${current_series}\n${series}`); - } else { - frappe.show_alert(__("Series already added to transaction.")); - } - }, -}); diff --git a/erpnext/setup/doctype/naming_series/naming_series.json b/erpnext/setup/doctype/naming_series/naming_series.json deleted file mode 100644 index c65a6f0ae44..00000000000 --- a/erpnext/setup/doctype/naming_series/naming_series.json +++ /dev/null @@ -1,132 +0,0 @@ -{ - "actions": [], - "creation": "2022-05-26 03:12:49.087648", - "description": "Set prefix for numbering series on your transactions", - "doctype": "DocType", - "engine": "InnoDB", - "field_order": [ - "setup_series", - "select_doc_for_series", - "help_html", - "naming_series_to_check", - "preview", - "add_series", - "set_options", - "user_must_always_select", - "update", - "column_break_13", - "update_series", - "prefix", - "current_value", - "update_series_start" - ], - "fields": [ - { - "description": "Set prefix for numbering series on your transactions", - "fieldname": "setup_series", - "fieldtype": "Section Break", - "label": "Setup Series" - }, - { - "fieldname": "select_doc_for_series", - "fieldtype": "Select", - "label": "Select Transaction" - }, - { - "depends_on": "select_doc_for_series", - "fieldname": "help_html", - "fieldtype": "HTML", - "label": "Help HTML", - "options": "
\n Edit list of Series in the box below. Rules:\n
    \n
  • Each Series Prefix on a new line.
  • \n
  • Allowed special characters are \"/\" and \"-\"
  • \n
  • \n Optionally, set the number of digits in the series using dot (.)\n followed by hashes (#). For example, \".####\" means that the series\n will have four digits. Default is five digits.\n
  • \n
  • \n You can also use variables in the series name by putting them\n between (.) dots\n
    \n Support Variables:\n
      \n
    • .YYYY. - Year in 4 digits
    • \n
    • .YY. - Year in 2 digits
    • \n
    • .MM. - Month
    • \n
    • .DD. - Day of month
    • \n
    • .WW. - Week of the year
    • \n
    • .FY. - Fiscal Year
    • \n
    • \n .{fieldname}. - fieldname on the document e.g.\n branch\n
    • \n
    \n
  • \n
\n Examples:\n
    \n
  • INV-
  • \n
  • INV-10-
  • \n
  • INVK-
  • \n
  • INV-.YYYY.-.{branch}.-.MM.-.####
  • \n
\n
\n
\n" - }, - { - "depends_on": "select_doc_for_series", - "fieldname": "set_options", - "fieldtype": "Text", - "label": "Series List for this Transaction" - }, - { - "default": "0", - "depends_on": "select_doc_for_series", - "description": "Check this if you want to force the user to select a series before saving. There will be no default if you check this.", - "fieldname": "user_must_always_select", - "fieldtype": "Check", - "label": "User must always select" - }, - { - "depends_on": "select_doc_for_series", - "fieldname": "update", - "fieldtype": "Button", - "label": "Update" - }, - { - "description": "Change the starting / current sequence number of an existing series.", - "fieldname": "update_series", - "fieldtype": "Section Break", - "label": "Update Series" - }, - { - "fieldname": "prefix", - "fieldtype": "Select", - "label": "Prefix" - }, - { - "description": "This is the number of the last created transaction with this prefix", - "fieldname": "current_value", - "fieldtype": "Int", - "label": "Current Value" - }, - { - "fieldname": "update_series_start", - "fieldtype": "Button", - "label": "Update Series Number", - "options": "update_series_start" - }, - { - "fieldname": "naming_series_to_check", - "fieldtype": "Data", - "label": "Try a naming Series" - }, - { - "default": " ", - "fieldname": "preview", - "fieldtype": "Text", - "label": "Preview of generated names", - "read_only": 1 - }, - { - "fieldname": "column_break_13", - "fieldtype": "Column Break" - }, - { - "fieldname": "add_series", - "fieldtype": "Button", - "label": "Add this Series" - } - ], - "hide_toolbar": 1, - "icon": "fa fa-sort-by-order", - "idx": 1, - "issingle": 1, - "links": [], - "modified": "2022-05-26 06:06:42.109504", - "modified_by": "Administrator", - "module": "Setup", - "name": "Naming Series", - "owner": "Administrator", - "permissions": [ - { - "create": 1, - "email": 1, - "print": 1, - "read": 1, - "role": "System Manager", - "share": 1, - "write": 1 - } - ], - "read_only": 1, - "sort_field": "modified", - "sort_order": "DESC", - "states": [] -} \ No newline at end of file diff --git a/erpnext/setup/doctype/naming_series/naming_series.py b/erpnext/setup/doctype/naming_series/naming_series.py deleted file mode 100644 index eafc264f308..00000000000 --- a/erpnext/setup/doctype/naming_series/naming_series.py +++ /dev/null @@ -1,303 +0,0 @@ -# Copyright (c) 2015, Frappe Technologies Pvt. Ltd. and Contributors -# License: GNU General Public License v3. See license.txt - - -import frappe -from frappe import _, msgprint, throw -from frappe.core.doctype.doctype.doctype import validate_series -from frappe.model.document import Document -from frappe.model.naming import make_autoname, parse_naming_series -from frappe.permissions import get_doctypes_with_read -from frappe.utils import cint, cstr - - -class NamingSeriesNotSetError(frappe.ValidationError): - pass - - -class NamingSeries(Document): - @frappe.whitelist() - def get_transactions(self, arg=None): - doctypes = list( - set( - frappe.db.sql_list( - """select parent - from `tabDocField` df where fieldname='naming_series'""" - ) - + frappe.db.sql_list( - """select dt from `tabCustom Field` - where fieldname='naming_series'""" - ) - ) - ) - - doctypes = list(set(get_doctypes_with_read()).intersection(set(doctypes))) - prefixes = "" - for d in doctypes: - options = "" - try: - options = self.get_options(d) - except frappe.DoesNotExistError: - frappe.msgprint(_("Unable to find DocType {0}").format(d)) - # frappe.pass_does_not_exist_error() - continue - - if options: - prefixes = prefixes + "\n" + options - prefixes.replace("\n\n", "\n") - prefixes = prefixes.split("\n") - - custom_prefixes = frappe.get_all( - "DocType", - fields=["autoname"], - filters={ - "name": ("not in", doctypes), - "autoname": ("like", "%.#%"), - "module": ("not in", ["Core"]), - }, - ) - if custom_prefixes: - prefixes = prefixes + [d.autoname.rsplit(".", 1)[0] for d in custom_prefixes] - - prefixes = "\n".join(sorted(prefixes)) - - return {"transactions": "\n".join([""] + sorted(doctypes)), "prefixes": prefixes} - - def scrub_options_list(self, ol): - options = list(filter(lambda x: x, [cstr(n).strip() for n in ol])) - return options - - @frappe.whitelist() - def update_series(self, arg=None): - """update series list""" - self.validate_series_set() - self.check_duplicate() - series_list = self.set_options.split("\n") - - # set in doctype - self.set_series_for(self.select_doc_for_series, series_list) - - # create series - map(self.insert_series, [d.split(".")[0] for d in series_list if d.strip()]) - - msgprint(_("Series Updated")) - - return self.get_transactions() - - def validate_series_set(self): - if self.select_doc_for_series and not self.set_options: - frappe.throw(_("Please set the series to be used.")) - - def set_series_for(self, doctype, ol): - options = self.scrub_options_list(ol) - - # validate names - for i in options: - self.validate_series_name(i) - - if options and self.user_must_always_select: - options = [""] + options - - default = options[0] if options else "" - - # update in property setter - prop_dict = {"options": "\n".join(options), "default": default} - - for prop in prop_dict: - ps_exists = frappe.db.get_value( - "Property Setter", {"field_name": "naming_series", "doc_type": doctype, "property": prop} - ) - - if ps_exists: - ps = frappe.get_doc("Property Setter", ps_exists) - ps.value = prop_dict[prop] - ps.save() - else: - ps = frappe.get_doc( - { - "doctype": "Property Setter", - "doctype_or_field": "DocField", - "doc_type": doctype, - "field_name": "naming_series", - "property": prop, - "value": prop_dict[prop], - "property_type": "Text", - "__islocal": 1, - } - ) - ps.save() - - self.set_options = "\n".join(options) - - frappe.clear_cache(doctype=doctype) - - def check_duplicate(self): - parent = list( - set( - frappe.db.sql_list( - """select dt.name - from `tabDocField` df, `tabDocType` dt - where dt.name = df.parent and df.fieldname='naming_series' and dt.name != %s""", - self.select_doc_for_series, - ) - + frappe.db.sql_list( - """select dt.name - from `tabCustom Field` df, `tabDocType` dt - where dt.name = df.dt and df.fieldname='naming_series' and dt.name != %s""", - self.select_doc_for_series, - ) - ) - ) - sr = [[frappe.get_meta(p).get_field("naming_series").options, p] for p in parent] - dt = frappe.get_doc("DocType", self.select_doc_for_series) - options = self.scrub_options_list(self.set_options.split("\n")) - for series in options: - validate_series(dt, series) - for i in sr: - if i[0]: - existing_series = [d.split(".")[0] for d in i[0].split("\n")] - if series.split(".")[0] in existing_series: - frappe.throw(_("Series {0} already used in {1}").format(series, i[1])) - - def validate_series_name(self, n): - import re - - if not re.match(r"^[\w\- \/.#{}]+$", n, re.UNICODE): - throw( - _('Special Characters except "-", "#", ".", "/", "{" and "}" not allowed in naming series') - ) - - @frappe.whitelist() - def get_options(self, arg=None): - if frappe.get_meta(arg or self.select_doc_for_series).get_field("naming_series"): - return frappe.get_meta(arg or self.select_doc_for_series).get_field("naming_series").options - - @frappe.whitelist() - def get_current(self, arg=None): - """get series current""" - if self.prefix: - prefix = self.parse_naming_series() - self.current_value = frappe.db.get_value("Series", prefix, "current", order_by="name") - - def insert_series(self, series): - """insert series if missing""" - if frappe.db.get_value("Series", series, "name", order_by="name") == None: - frappe.db.sql("insert into tabSeries (name, current) values (%s, 0)", (series)) - - @frappe.whitelist() - def update_series_start(self): - if self.prefix: - prefix = self.parse_naming_series() - self.insert_series(prefix) - frappe.db.sql( - "update `tabSeries` set current = %s where name = %s", (cint(self.current_value), prefix) - ) - msgprint(_("Series Updated Successfully")) - else: - msgprint(_("Please select prefix first")) - - def parse_naming_series(self): - parts = self.prefix.split(".") - - # Remove ### from the end of series - if parts[-1] == "#" * len(parts[-1]): - del parts[-1] - - prefix = parse_naming_series(parts) - return prefix - - @frappe.whitelist() - def preview_series(self) -> str: - """Preview what the naming series will generate.""" - - generated_names = [] - series = self.naming_series_to_check - if not series: - return "" - - try: - doc = self._fetch_last_doc_if_available() - for _count in range(3): - generated_names.append(make_autoname(series, doc=doc)) - except Exception as e: - if frappe.message_log: - frappe.message_log.pop() - return _("Failed to generate names from the series") + f"\n{str(e)}" - - # Explcitly rollback in case any changes were made to series table. - frappe.db.rollback() # nosemgrep - return "\n".join(generated_names) - - def _fetch_last_doc_if_available(self): - """Fetch last doc for evaluating naming series with fields.""" - try: - return frappe.get_last_doc(self.select_doc_for_series) - except Exception: - return None - - -def set_by_naming_series( - doctype, fieldname, naming_series, hide_name_field=True, make_mandatory=1 -): - from frappe.custom.doctype.property_setter.property_setter import make_property_setter - - if naming_series: - make_property_setter( - doctype, "naming_series", "hidden", 0, "Check", validate_fields_for_doctype=False - ) - make_property_setter( - doctype, "naming_series", "reqd", make_mandatory, "Check", validate_fields_for_doctype=False - ) - - # set values for mandatory - try: - frappe.db.sql( - """update `tab{doctype}` set naming_series={s} where - ifnull(naming_series, '')=''""".format( - doctype=doctype, s="%s" - ), - get_default_naming_series(doctype), - ) - except NamingSeriesNotSetError: - pass - - if hide_name_field: - make_property_setter(doctype, fieldname, "reqd", 0, "Check", validate_fields_for_doctype=False) - make_property_setter( - doctype, fieldname, "hidden", 1, "Check", validate_fields_for_doctype=False - ) - else: - make_property_setter( - doctype, "naming_series", "reqd", 0, "Check", validate_fields_for_doctype=False - ) - make_property_setter( - doctype, "naming_series", "hidden", 1, "Check", validate_fields_for_doctype=False - ) - - if hide_name_field: - make_property_setter( - doctype, fieldname, "hidden", 0, "Check", validate_fields_for_doctype=False - ) - make_property_setter(doctype, fieldname, "reqd", 1, "Check", validate_fields_for_doctype=False) - - # set values for mandatory - frappe.db.sql( - """update `tab{doctype}` set `{fieldname}`=`name` where - ifnull({fieldname}, '')=''""".format( - doctype=doctype, fieldname=fieldname - ) - ) - - -def get_default_naming_series(doctype): - naming_series = frappe.get_meta(doctype).get_field("naming_series").options or "" - naming_series = naming_series.split("\n") - out = naming_series[0] or (naming_series[1] if len(naming_series) > 1 else None) - - if not out: - frappe.throw( - _("Please set Naming Series for {0} via Setup > Settings > Naming Series").format(doctype), - NamingSeriesNotSetError, - ) - else: - return out diff --git a/erpnext/setup/doctype/naming_series/test_naming_series.py b/erpnext/setup/doctype/naming_series/test_naming_series.py deleted file mode 100644 index fce663e4c55..00000000000 --- a/erpnext/setup/doctype/naming_series/test_naming_series.py +++ /dev/null @@ -1,35 +0,0 @@ -# Copyright (c) 2022, Frappe Technologies Pvt. Ltd. and Contributors -# See license.txt - -import frappe -from frappe.tests.utils import FrappeTestCase - -from erpnext.setup.doctype.naming_series.naming_series import NamingSeries - - -class TestNamingSeries(FrappeTestCase): - def setUp(self): - self.ns: NamingSeries = frappe.get_doc("Naming Series") - - def tearDown(self): - frappe.db.rollback() - - def test_naming_preview(self): - self.ns.select_doc_for_series = "Sales Invoice" - - self.ns.naming_series_to_check = "AXBZ.####" - serieses = self.ns.preview_series().split("\n") - self.assertEqual(["AXBZ0001", "AXBZ0002", "AXBZ0003"], serieses) - - self.ns.naming_series_to_check = "AXBZ-.{currency}.-" - serieses = self.ns.preview_series().split("\n") - - def test_get_transactions(self): - - naming_info = self.ns.get_transactions() - self.assertIn("Sales Invoice", naming_info["transactions"]) - - existing_naming_series = frappe.get_meta("Sales Invoice").get_field("naming_series").options - - for series in existing_naming_series.split("\n"): - self.assertIn(series, naming_info["prefixes"]) diff --git a/erpnext/stock/doctype/stock_settings/stock_settings.py b/erpnext/stock/doctype/stock_settings/stock_settings.py index e592a4be3c6..50807a96abd 100644 --- a/erpnext/stock/doctype/stock_settings/stock_settings.py +++ b/erpnext/stock/doctype/stock_settings/stock_settings.py @@ -26,7 +26,7 @@ class StockSettings(Document): ]: frappe.db.set_default(key, self.get(key, "")) - from erpnext.setup.doctype.naming_series.naming_series import set_by_naming_series + from erpnext.utilities.naming import set_by_naming_series set_by_naming_series( "Item", diff --git a/erpnext/utilities/naming.py b/erpnext/utilities/naming.py new file mode 100644 index 00000000000..52bbadef148 --- /dev/null +++ b/erpnext/utilities/naming.py @@ -0,0 +1,60 @@ +import frappe +from frappe.model.naming import get_default_naming_series + + +class NamingSeriesNotSetError(frappe.ValidationError): + pass + + +def set_by_naming_series( + doctype, fieldname, naming_series, hide_name_field=True, make_mandatory=1 +): + """Change a doctype's naming to user naming series""" + from frappe.custom.doctype.property_setter.property_setter import make_property_setter + + if naming_series: + make_property_setter( + doctype, "naming_series", "hidden", 0, "Check", validate_fields_for_doctype=False + ) + make_property_setter( + doctype, "naming_series", "reqd", make_mandatory, "Check", validate_fields_for_doctype=False + ) + + # set values for mandatory + try: + frappe.db.sql( + """update `tab{doctype}` set naming_series={s} where + ifnull(naming_series, '')=''""".format( + doctype=doctype, s="%s" + ), + get_default_naming_series(doctype), + ) + except NamingSeriesNotSetError: + pass + + if hide_name_field: + make_property_setter(doctype, fieldname, "reqd", 0, "Check", validate_fields_for_doctype=False) + make_property_setter( + doctype, fieldname, "hidden", 1, "Check", validate_fields_for_doctype=False + ) + else: + make_property_setter( + doctype, "naming_series", "reqd", 0, "Check", validate_fields_for_doctype=False + ) + make_property_setter( + doctype, "naming_series", "hidden", 1, "Check", validate_fields_for_doctype=False + ) + + if hide_name_field: + make_property_setter( + doctype, fieldname, "hidden", 0, "Check", validate_fields_for_doctype=False + ) + make_property_setter(doctype, fieldname, "reqd", 1, "Check", validate_fields_for_doctype=False) + + # set values for mandatory + frappe.db.sql( + """update `tab{doctype}` set `{fieldname}`=`name` where + ifnull({fieldname}, '')=''""".format( + doctype=doctype, fieldname=fieldname + ) + ) From a62bc9b6c920b0d39a16b348a65962fa6a0d9992 Mon Sep 17 00:00:00 2001 From: marination Date: Tue, 31 May 2022 15:53:34 +0530 Subject: [PATCH 034/105] chore: Limit Update Cost jobs & `db_update` only if changed values - If `Update Cost` job is ongoing, then block creation of new ones since all BOMs are updated - `db_update` in `calculate_rm_cost` only if changed values to reduce redundant row updates - Misc: Use variable for batch size --- erpnext/manufacturing/doctype/bom/bom.py | 4 +++- .../doctype/bom_update_log/bom_update_log.py | 22 +++++++++++++++++-- .../bom_update_tool/bom_update_tool.py | 8 ++++++- 3 files changed, 30 insertions(+), 4 deletions(-) diff --git a/erpnext/manufacturing/doctype/bom/bom.py b/erpnext/manufacturing/doctype/bom/bom.py index 7055efac289..d4e0d4b8147 100644 --- a/erpnext/manufacturing/doctype/bom/bom.py +++ b/erpnext/manufacturing/doctype/bom/bom.py @@ -644,6 +644,7 @@ class BOM(WebsiteGenerator): base_total_rm_cost = 0 for d in self.get("items"): + old_rate = d.rate d.rate = self.get_rm_rate( { "company": self.company, @@ -656,6 +657,7 @@ class BOM(WebsiteGenerator): "sourced_by_supplier": d.sourced_by_supplier, } ) + d.base_rate = flt(d.rate) * flt(self.conversion_rate) d.amount = flt(d.rate, d.precision("rate")) * flt(d.qty, d.precision("qty")) d.base_amount = d.amount * flt(self.conversion_rate) @@ -665,7 +667,7 @@ class BOM(WebsiteGenerator): total_rm_cost += d.amount base_total_rm_cost += d.base_amount - if save: + if save and (old_rate != d.rate): d.db_update() self.raw_material_cost = total_rm_cost diff --git a/erpnext/manufacturing/doctype/bom_update_log/bom_update_log.py b/erpnext/manufacturing/doctype/bom_update_log/bom_update_log.py index 639628ac383..f61f863c10a 100644 --- a/erpnext/manufacturing/doctype/bom_update_log/bom_update_log.py +++ b/erpnext/manufacturing/doctype/bom_update_log/bom_update_log.py @@ -26,6 +26,8 @@ class BOMUpdateLog(Document): self.validate_boms_are_specified() self.validate_same_bom() self.validate_bom_items() + else: + self.validate_bom_cost_update_in_progress() self.status = "Queued" @@ -48,6 +50,21 @@ class BOMUpdateLog(Document): if current_bom_item != new_bom_item: frappe.throw(_("The selected BOMs are not for the same item")) + def validate_bom_cost_update_in_progress(self): + "If another Cost Updation Log is still in progress, dont make new ones." + + wip_log = frappe.get_all( + "BOM Update Log", + {"update_type": "Update Cost", "status": ["in", ["Queued", "In Progress", "Paused"]]}, + limit_page_length=1, + ) + if wip_log: + log_link = frappe.utils.get_link_to_form("BOM Update Log", wip_log[0].name) + frappe.throw( + _("BOM Updation already in progress. Please wait until {0} is complete.").format(log_link), + title=_("Note"), + ) + def on_submit(self): if frappe.flags.in_test: return @@ -124,10 +141,11 @@ def queue_bom_cost_jobs(current_boms: Dict, update_doc: "BOMUpdateLog") -> None: current_boms_list = [bom for bom in current_boms] while current_boms_list: - boms_to_process = current_boms_list[:20000] # slice out batch of 20k BOMs + batch_size = 20_000 + boms_to_process = current_boms_list[:batch_size] # slice out batch of 20k BOMs # update list to exclude 20K (queued) BOMs - current_boms_list = current_boms_list[20000:] if len(current_boms_list) > 20000 else [] + current_boms_list = current_boms_list[batch_size:] if len(current_boms_list) > batch_size else [] frappe.enqueue( method="erpnext.manufacturing.doctype.bom_update_log.bom_updation_utils.update_cost_in_level", doc=update_doc, diff --git a/erpnext/manufacturing/doctype/bom_update_tool/bom_update_tool.py b/erpnext/manufacturing/doctype/bom_update_tool/bom_update_tool.py index 4a2e03fb188..758d8ed0ef9 100644 --- a/erpnext/manufacturing/doctype/bom_update_tool/bom_update_tool.py +++ b/erpnext/manufacturing/doctype/bom_update_tool/bom_update_tool.py @@ -38,7 +38,13 @@ def enqueue_update_cost() -> "BOMUpdateLog": def auto_update_latest_price_in_all_boms() -> None: """Called via hooks.py.""" if frappe.db.get_single_value("Manufacturing Settings", "update_bom_costs_automatically"): - create_bom_update_log(update_type="Update Cost") + wip_log = frappe.get_all( + "BOM Update Log", + {"update_type": "Update Cost", "status": ["in", ["Queued", "In Progress", "Paused"]]}, + limit_page_length=1, + ) + if not wip_log: + create_bom_update_log(update_type="Update Cost") def create_bom_update_log( From 0331e37982b5513bc49ccfb8b840323bd9960041 Mon Sep 17 00:00:00 2001 From: ruthra kumar Date: Tue, 31 May 2022 15:38:08 +0530 Subject: [PATCH 035/105] fix: incorrect billed_qty when item has multiple Delivery note sales order analysis report returns incorrect billed_qty value for an SO item has multiple delivery notes --- .../sales_order_analysis.py | 68 ++++++++++++++++--- 1 file changed, 57 insertions(+), 11 deletions(-) diff --git a/erpnext/selling/report/sales_order_analysis/sales_order_analysis.py b/erpnext/selling/report/sales_order_analysis/sales_order_analysis.py index dcfb10a9d53..cc61594af4a 100644 --- a/erpnext/selling/report/sales_order_analysis/sales_order_analysis.py +++ b/erpnext/selling/report/sales_order_analysis/sales_order_analysis.py @@ -1,11 +1,13 @@ # Copyright (c) 2013, Frappe Technologies Pvt. Ltd. and contributors # For license information, please see license.txt - import copy +from collections import OrderedDict import frappe -from frappe import _ +from frappe import _, qb +from frappe.query_builder import CustomFunction +from frappe.query_builder.functions import Max from frappe.utils import date_diff, flt, getdate @@ -18,11 +20,12 @@ def execute(filters=None): columns = get_columns(filters) conditions = get_conditions(filters) data = get_data(conditions, filters) + so_elapsed_time = get_so_elapsed_time(data) if not data: return [], [], None, [] - data, chart_data = prepare_data(data, filters) + data, chart_data = prepare_data(data, so_elapsed_time, filters) return columns, data, None, chart_data @@ -65,7 +68,6 @@ def get_data(conditions, filters): IF(so.status in ('Completed','To Bill'), 0, (SELECT delay_days)) as delay, soi.qty, soi.delivered_qty, (soi.qty - soi.delivered_qty) AS pending_qty, - IF((SELECT pending_qty) = 0, (TO_SECONDS(Max(dn.posting_date))-TO_SECONDS(so.transaction_date)), 0) as time_taken_to_deliver, IFNULL(SUM(sii.qty), 0) as billed_qty, soi.base_amount as amount, (soi.delivered_qty * soi.base_rate) as delivered_qty_amount, @@ -76,13 +78,9 @@ def get_data(conditions, filters): soi.description as description FROM `tabSales Order` so, - (`tabSales Order Item` soi + `tabSales Order Item` soi LEFT JOIN `tabSales Invoice Item` sii - ON sii.so_detail = soi.name and sii.docstatus = 1) - LEFT JOIN `tabDelivery Note Item` dni - on dni.so_detail = soi.name - LEFT JOIN `tabDelivery Note` dn - on dni.parent = dn.name and dn.docstatus = 1 + ON sii.so_detail = soi.name and sii.docstatus = 1 WHERE soi.parent = so.name and so.status not in ('Stopped', 'Closed', 'On Hold') @@ -100,7 +98,48 @@ def get_data(conditions, filters): return data -def prepare_data(data, filters): +def get_so_elapsed_time(data): + """ + query SO's elapsed time till latest delivery note + """ + so_elapsed_time = OrderedDict() + if data: + sales_orders = [x.sales_order for x in data] + + so = qb.DocType("Sales Order") + soi = qb.DocType("Sales Order Item") + dn = qb.DocType("Delivery Note") + dni = qb.DocType("Delivery Note Item") + + to_seconds = CustomFunction("TO_SECONDS", ["date"]) + + query = ( + qb.from_(so) + .inner_join(soi) + .on(soi.parent == so.name) + .left_join(dni) + .on(dni.so_detail == soi.name) + .left_join(dn) + .on(dni.parent == dn.name) + .select( + so.name.as_("sales_order"), + soi.item_code.as_("so_item_code"), + (to_seconds(Max(dn.posting_date)) - to_seconds(so.transaction_date)).as_("elapsed_seconds"), + ) + .where((so.name.isin(sales_orders)) & (dn.docstatus == 1)) + .orderby(so.name, soi.name) + .groupby(soi.name) + ) + dn_elapsed_time = query.run(as_dict=True) + + for e in dn_elapsed_time: + key = (e.sales_order, e.so_item_code) + so_elapsed_time[key] = e.elapsed_seconds + + return so_elapsed_time + + +def prepare_data(data, so_elapsed_time, filters): completed, pending = 0, 0 if filters.get("group_by_so"): @@ -115,6 +154,13 @@ def prepare_data(data, filters): row["qty_to_bill"] = flt(row["qty"]) - flt(row["billed_qty"]) row["delay"] = 0 if row["delay"] and row["delay"] < 0 else row["delay"] + + row["time_taken_to_deliver"] = ( + so_elapsed_time.get((row.sales_order, row.item_code)) + if row["status"] in ("To Bill", "Completed") + else 0 + ) + if filters.get("group_by_so"): so_name = row["sales_order"] From 4f1bfbb93dc80f9f459d663cbe43433e47431ad5 Mon Sep 17 00:00:00 2001 From: ruthra kumar Date: Tue, 31 May 2022 12:10:04 +0530 Subject: [PATCH 036/105] test: multiple delivery notes and billed quantity --- .../test_sales_order_analysis.py | 106 ++++++++++++++++-- 1 file changed, 97 insertions(+), 9 deletions(-) diff --git a/erpnext/selling/report/sales_order_analysis/test_sales_order_analysis.py b/erpnext/selling/report/sales_order_analysis/test_sales_order_analysis.py index 25cbb734499..241f4358fba 100644 --- a/erpnext/selling/report/sales_order_analysis/test_sales_order_analysis.py +++ b/erpnext/selling/report/sales_order_analysis/test_sales_order_analysis.py @@ -11,7 +11,7 @@ test_dependencies = ["Sales Order", "Item", "Sales Invoice", "Delivery Note"] class TestSalesOrderAnalysis(FrappeTestCase): - def create_sales_order(self, transaction_date): + def create_sales_order(self, transaction_date, do_not_save=False, do_not_submit=False): item = create_item(item_code="_Test Excavator", is_stock_item=0) so = make_sales_order( transaction_date=transaction_date, @@ -24,25 +24,31 @@ class TestSalesOrderAnalysis(FrappeTestCase): so.taxes_and_charges = "" so.taxes = "" so.items[0].delivery_date = add_days(transaction_date, 15) - so.save() - so.submit() + if not do_not_save: + so.save() + if not do_not_submit: + so.submit() return item, so - def create_sales_invoice(self, so): + def create_sales_invoice(self, so, do_not_save=False, do_not_submit=False): sinv = make_sales_invoice(so.name) sinv.posting_date = so.transaction_date sinv.taxes_and_charges = "" sinv.taxes = "" - sinv.insert() - sinv.submit() + if not do_not_save: + sinv.save() + if not do_not_submit: + sinv.submit() return sinv - def create_delivery_note(self, so): + def create_delivery_note(self, so, do_not_save=False, do_not_submit=False): dn = make_delivery_note(so.name) dn.set_posting_time = True dn.posting_date = add_days(so.transaction_date, 1) - dn.save() - dn.submit() + if not do_not_save: + dn.save() + if not do_not_submit: + dn.submit() return dn def test_01_so_to_deliver_and_bill(self): @@ -164,3 +170,85 @@ class TestSalesOrderAnalysis(FrappeTestCase): ) # SO's from first 4 test cases should be in output self.assertEqual(len(data), 4) + + def test_06_so_pending_delivery_with_multiple_delivery_notes(self): + transaction_date = "2021-06-01" + item, so = self.create_sales_order(transaction_date) + + # bill 2 items + sinv1 = self.create_sales_invoice(so, do_not_save=True) + sinv1.items[0].qty = 2 + sinv1 = sinv1.save().submit() + # deliver 2 items + dn1 = self.create_delivery_note(so, do_not_save=True) + dn1.items[0].qty = 2 + dn1 = dn1.save().submit() + + # bill 2 items + sinv2 = self.create_sales_invoice(so, do_not_save=True) + sinv2.items[0].qty = 2 + sinv2 = sinv2.save().submit() + # deliver 1 item + dn2 = self.create_delivery_note(so, do_not_save=True) + dn2.items[0].qty = 1 + dn2 = dn2.save().submit() + + columns, data, message, chart = execute( + { + "company": "_Test Company", + "from_date": "2021-06-01", + "to_date": "2021-06-30", + "sales_order": [so.name], + } + ) + expected_value = { + "status": "To Deliver and Bill", + "sales_order": so.name, + "delay_days": frappe.utils.date_diff(frappe.utils.datetime.date.today(), so.delivery_date), + "qty": 10, + "delivered_qty": 3, + "pending_qty": 7, + "qty_to_bill": 6, + "billed_qty": 4, + "time_taken_to_deliver": 0, + } + self.assertEqual(len(data), 1) + for key, val in expected_value.items(): + with self.subTest(key=key, val=val): + self.assertEqual(data[0][key], val) + + def test_07_so_delivered_with_multiple_delivery_notes(self): + transaction_date = "2021-06-01" + item, so = self.create_sales_order(transaction_date) + + dn1 = self.create_delivery_note(so, do_not_save=True) + dn1.items[0].qty = 5 + dn1 = dn1.save().submit() + + dn2 = self.create_delivery_note(so, do_not_save=True) + dn2.items[0].qty = 5 + dn2 = dn2.save().submit() + + columns, data, message, chart = execute( + { + "company": "_Test Company", + "from_date": "2021-06-01", + "to_date": "2021-06-30", + "sales_order": [so.name], + } + ) + expected_value = { + "status": "To Bill", + "sales_order": so.name, + "delay_days": frappe.utils.date_diff(frappe.utils.datetime.date.today(), so.delivery_date), + "qty": 10, + "delivered_qty": 10, + "pending_qty": 0, + "qty_to_bill": 10, + "billed_qty": 0, + "time_taken_to_deliver": 86400, + } + self.assertEqual(len(data), 1) + for key, val in expected_value.items(): + with self.subTest(key=key, val=val): + self.assertEqual(data[0][key], val) From c4af63ad981649a2bcbc82c0fe79dd14e9cf2f47 Mon Sep 17 00:00:00 2001 From: hrzzz Date: Tue, 31 May 2022 10:46:56 -0300 Subject: [PATCH 037/105] feat: two new groupby mode: Monthly, Payment Term --- .../report/gross_profit/gross_profit.js | 2 +- .../report/gross_profit/gross_profit.py | 103 ++++++++++++++++-- 2 files changed, 92 insertions(+), 13 deletions(-) diff --git a/erpnext/accounts/report/gross_profit/gross_profit.js b/erpnext/accounts/report/gross_profit/gross_profit.js index 158ff4d3437..3d37b5898c1 100644 --- a/erpnext/accounts/report/gross_profit/gross_profit.js +++ b/erpnext/accounts/report/gross_profit/gross_profit.js @@ -35,7 +35,7 @@ frappe.query_reports["Gross Profit"] = { "fieldname":"group_by", "label": __("Group By"), "fieldtype": "Select", - "options": "Invoice\nItem Code\nItem Group\nBrand\nWarehouse\nCustomer\nCustomer Group\nTerritory\nSales Person\nProject", + "options": "Invoice\nItem Code\nItem Group\nBrand\nWarehouse\nCustomer\nCustomer Group\nTerritory\nSales Person\nProject\nMonthly\nPayment Term", "default": "Invoice" }, ], diff --git a/erpnext/accounts/report/gross_profit/gross_profit.py b/erpnext/accounts/report/gross_profit/gross_profit.py index 9668992e022..526ea9d6e2e 100644 --- a/erpnext/accounts/report/gross_profit/gross_profit.py +++ b/erpnext/accounts/report/gross_profit/gross_profit.py @@ -4,7 +4,7 @@ import frappe from frappe import _, scrub -from frappe.utils import cint, flt +from frappe.utils import cint, flt, formatdate from erpnext.controllers.queries import get_match_cond from erpnext.stock.utils import get_incoming_rate @@ -124,6 +124,23 @@ def execute(filters=None): "gross_profit", "gross_profit_percent", ], + "monthly": [ + "monthly", + "qty", + "base_rate", + "buying_rate", + "base_amount", + "buying_amount", + "gross_profit", + "gross_profit_percent", + ], + "payment_term": [ + "payment_term", + "base_amount", + "buying_amount", + "gross_profit", + "gross_profit_percent", + ], } ) @@ -317,6 +334,19 @@ def get_columns(group_wise_columns, filters): "options": "territory", "width": 100, }, + "monthly": { + "label": _("Monthly"), + "fieldname": "monthly", + "fieldtype": "Data", + "width": 100, + }, + "payment_term": { + "label": _("Payment Term"), + "fieldname": "payment_term", + "fieldtype": "Link", + "options": "Payment Term", + "width": 170, + }, } ) @@ -390,6 +420,9 @@ class GrossProfitGenerator(object): buying_amount = 0 for row in reversed(self.si_list): + if self.filters.get("group_by") == "Monthly": + row.monthly = formatdate(row.posting_date, "MMM YYYY") + if self.skip_row(row): continue @@ -445,17 +478,7 @@ class GrossProfitGenerator(object): def get_average_rate_based_on_group_by(self): for key in list(self.grouped): - if self.filters.get("group_by") != "Invoice": - for i, row in enumerate(self.grouped[key]): - if i == 0: - new_row = row - else: - new_row.qty += flt(row.qty) - new_row.buying_amount += flt(row.buying_amount, self.currency_precision) - new_row.base_amount += flt(row.base_amount, self.currency_precision) - new_row = self.set_average_rate(new_row) - self.grouped_data.append(new_row) - else: + if self.filters.get("group_by") == "Invoice": for i, row in enumerate(self.grouped[key]): if row.indent == 1.0: if ( @@ -469,6 +492,44 @@ class GrossProfitGenerator(object): if flt(row.qty) or row.base_amount: row = self.set_average_rate(row) self.grouped_data.append(row) + elif self.filters.get("group_by") == "Payment Term": + for i, row in enumerate(self.grouped[key]): + invoice_portion = 0 + + if row.is_return: + invoice_portion = 100 + elif row.invoice_portion: + invoice_portion = row.invoice_portion + else: + invoice_portion = row.payment_amount * 100 / row.base_net_amount + + if i == 0: + new_row = row + self.set_average_based_on_payment_term_portion(new_row, row, invoice_portion) + else: + new_row.qty += flt(row.qty) + self.set_average_based_on_payment_term_portion(new_row, row, invoice_portion, True) + + new_row = self.set_average_rate(new_row) + self.grouped_data.append(new_row) + else: + for i, row in enumerate(self.grouped[key]): + if i == 0: + new_row = row + else: + new_row.qty += flt(row.qty) + new_row.buying_amount += flt(row.buying_amount, self.currency_precision) + new_row.base_amount += flt(row.base_amount, self.currency_precision) + new_row = self.set_average_rate(new_row) + self.grouped_data.append(new_row) + + def set_average_based_on_payment_term_portion(self, new_row, row, invoice_portion, aggr=False): + cols = ["base_amount", "buying_amount", "gross_profit"] + for col in cols: + if aggr: + new_row[col] += row[col] * invoice_portion / 100 + else: + new_row[col] = row[col] * invoice_portion / 100 def is_not_invoice_row(self, row): return (self.filters.get("group_by") == "Invoice" and row.indent != 0.0) or self.filters.get( @@ -622,6 +683,20 @@ class GrossProfitGenerator(object): sales_person_cols = "" sales_team_table = "" + if self.filters.group_by == "Payment Term": + payment_term_cols = """,if(`tabSales Invoice`.is_return = 1, + '{0}', + coalesce(schedule.payment_term, '{1}')) as payment_term, + schedule.invoice_portion, + schedule.payment_amount """.format( + _("Sales Return"), _("No Terms") + ) + payment_term_table = """ left join `tabPayment Schedule` schedule on schedule.parent = `tabSales Invoice`.name and + `tabSales Invoice`.is_return = 0 """ + else: + payment_term_cols = "" + payment_term_table = "" + if self.filters.get("sales_invoice"): conditions += " and `tabSales Invoice`.name = %(sales_invoice)s" @@ -644,10 +719,12 @@ class GrossProfitGenerator(object): `tabSales Invoice Item`.name as "item_row", `tabSales Invoice`.is_return, `tabSales Invoice Item`.cost_center {sales_person_cols} + {payment_term_cols} from `tabSales Invoice` inner join `tabSales Invoice Item` on `tabSales Invoice Item`.parent = `tabSales Invoice`.name {sales_team_table} + {payment_term_table} where `tabSales Invoice`.docstatus=1 and `tabSales Invoice`.is_opening!='Yes' {conditions} {match_cond} order by @@ -655,6 +732,8 @@ class GrossProfitGenerator(object): conditions=conditions, sales_person_cols=sales_person_cols, sales_team_table=sales_team_table, + payment_term_cols=payment_term_cols, + payment_term_table=payment_term_table, match_cond=get_match_cond("Sales Invoice"), ), self.filters, From a6beafbc3c33643a1ba6c35ab52b5b9edc125d7e Mon Sep 17 00:00:00 2001 From: Deepesh Garg Date: Tue, 31 May 2022 19:41:46 +0530 Subject: [PATCH 038/105] fix: Permission for selling and buying settings --- .../doctype/buying_settings/buying_settings.json | 12 +++++++++++- .../doctype/selling_settings/selling_settings.json | 11 ++++++++++- 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/erpnext/buying/doctype/buying_settings/buying_settings.json b/erpnext/buying/doctype/buying_settings/buying_settings.json index 89a94487163..6c18a4650b7 100644 --- a/erpnext/buying/doctype/buying_settings/buying_settings.json +++ b/erpnext/buying/doctype/buying_settings/buying_settings.json @@ -148,7 +148,7 @@ "index_web_pages_for_search": 1, "issingle": 1, "links": [], - "modified": "2022-04-14 15:56:42.340223", + "modified": "2022-05-31 19:40:26.103909", "modified_by": "Administrator", "module": "Buying", "name": "Buying Settings", @@ -162,6 +162,16 @@ "role": "System Manager", "share": 1, "write": 1 + }, + { + "create": 1, + "email": 1, + "export": 1, + "print": 1, + "read": 1, + "role": "Purchase Manager", + "share": 1, + "write": 1 } ], "sort_field": "modified", diff --git a/erpnext/selling/doctype/selling_settings/selling_settings.json b/erpnext/selling/doctype/selling_settings/selling_settings.json index 005e24cfbe3..2abb169b8a0 100644 --- a/erpnext/selling/doctype/selling_settings/selling_settings.json +++ b/erpnext/selling/doctype/selling_settings/selling_settings.json @@ -179,7 +179,7 @@ "index_web_pages_for_search": 1, "issingle": 1, "links": [], - "modified": "2022-04-14 16:01:29.405642", + "modified": "2022-05-31 19:39:48.398738", "modified_by": "Administrator", "module": "Selling", "name": "Selling Settings", @@ -193,6 +193,15 @@ "role": "System Manager", "share": 1, "write": 1 + }, + { + "create": 1, + "email": 1, + "print": 1, + "read": 1, + "role": "Sales Manager", + "share": 1, + "write": 1 } ], "sort_field": "modified", From b3ccc4bfb953b90dc8301a6af953c1a2cd66d4b6 Mon Sep 17 00:00:00 2001 From: maharshivpatel <39730881+maharshivpatel@users.noreply.github.com> Date: Tue, 31 May 2022 19:48:30 +0530 Subject: [PATCH 039/105] fix: Auto Insert Item Price If Missing when discount & blank UOM (#31168) * fix: Auto Insert Item Price If Missing when discount and blank UOM fixes wrong item price insert when discount is used and adds uom=stock_uom instead of blank as price is converted to stock uom * unit tests added for item with discount I have added test for auto_insert_price where discount is used. * unit test issue fixed fixed make_sales_order as some of the test that depended on it were failing due to passing of incorrect parameters. Co-authored-by: Ankush Menat --- .../doctype/sales_order/test_sales_order.py | 24 ++++++++++++++++++- erpnext/stock/get_item_details.py | 8 +++++-- 2 files changed, 29 insertions(+), 3 deletions(-) diff --git a/erpnext/selling/doctype/sales_order/test_sales_order.py b/erpnext/selling/doctype/sales_order/test_sales_order.py index acae37f547f..96308f0bee6 100644 --- a/erpnext/selling/doctype/sales_order/test_sales_order.py +++ b/erpnext/selling/doctype/sales_order/test_sales_order.py @@ -783,6 +783,7 @@ class TestSalesOrder(FrappeTestCase): def test_auto_insert_price(self): make_item("_Test Item for Auto Price List", {"is_stock_item": 0}) + make_item("_Test Item for Auto Price List with Discount Percentage", {"is_stock_item": 0}) frappe.db.set_value("Stock Settings", None, "auto_insert_price_list_rate_if_missing", 1) item_price = frappe.db.get_value( @@ -804,6 +805,25 @@ class TestSalesOrder(FrappeTestCase): 100, ) + make_sales_order( + item_code="_Test Item for Auto Price List with Discount Percentage", + selling_price_list="_Test Price List", + price_list_rate=200, + discount_percentage=20, + ) + + self.assertEqual( + frappe.db.get_value( + "Item Price", + { + "price_list": "_Test Price List", + "item_code": "_Test Item for Auto Price List with Discount Percentage", + }, + "price_list_rate", + ), + 200, + ) + # do not update price list frappe.db.set_value("Stock Settings", None, "auto_insert_price_list_rate_if_missing", 0) @@ -1659,7 +1679,9 @@ def make_sales_order(**args): "warehouse": args.warehouse, "qty": args.qty or 10, "uom": args.uom or None, - "rate": args.rate or 100, + "price_list_rate": args.price_list_rate or None, + "discount_percentage": args.discount_percentage or None, + "rate": args.rate or (None if args.price_list_rate else 100), "against_blanket_order": args.against_blanket_order, }, ) diff --git a/erpnext/stock/get_item_details.py b/erpnext/stock/get_item_details.py index c6241f8df6d..c8d9f5404fb 100644 --- a/erpnext/stock/get_item_details.py +++ b/erpnext/stock/get_item_details.py @@ -353,6 +353,7 @@ def get_basic_details(args, item, overwrite_warehouse=True): "has_batch_no": item.has_batch_no, "batch_no": args.get("batch_no"), "uom": args.uom, + "stock_uom": item.stock_uom, "min_order_qty": flt(item.min_order_qty) if args.doctype == "Material Request" else "", "qty": flt(args.qty) or 1.0, "stock_qty": flt(args.qty) or 1.0, @@ -365,7 +366,7 @@ def get_basic_details(args, item, overwrite_warehouse=True): "net_rate": 0.0, "net_amount": 0.0, "discount_percentage": 0.0, - "discount_amount": 0.0, + "discount_amount": flt(args.discount_amount) or 0.0, "supplier": get_default_supplier(args, item_defaults, item_group_defaults, brand_defaults), "update_stock": args.get("update_stock") if args.get("doctype") in ["Sales Invoice", "Purchase Invoice"] @@ -823,7 +824,9 @@ def insert_item_price(args): ): if frappe.has_permission("Item Price", "write"): price_list_rate = ( - args.rate / args.get("conversion_factor") if args.get("conversion_factor") else args.rate + (args.rate + args.discount_amount) / args.get("conversion_factor") + if args.get("conversion_factor") + else (args.rate + args.discount_amount) ) item_price = frappe.db.get_value( @@ -849,6 +852,7 @@ def insert_item_price(args): "item_code": args.item_code, "currency": args.currency, "price_list_rate": price_list_rate, + "uom": args.stock_uom, } ) item_price.insert() From 167cf7b49d9b9b3b531459519cd73f53b8fcd742 Mon Sep 17 00:00:00 2001 From: Deepesh Garg Date: Wed, 1 Jun 2022 09:04:53 +0530 Subject: [PATCH 040/105] fix: Remove domain restrcition from Manufacturing Workspace --- .../manufacturing/workspace/manufacturing/manufacturing.json | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/erpnext/manufacturing/workspace/manufacturing/manufacturing.json b/erpnext/manufacturing/workspace/manufacturing/manufacturing.json index 05ca2a84523..9829a96e09e 100644 --- a/erpnext/manufacturing/workspace/manufacturing/manufacturing.json +++ b/erpnext/manufacturing/workspace/manufacturing/manufacturing.json @@ -402,14 +402,15 @@ "type": "Link" } ], - "modified": "2022-01-13 17:40:09.474747", + "modified": "2022-05-31 22:08:19.408223", "modified_by": "Administrator", "module": "Manufacturing", "name": "Manufacturing", "owner": "Administrator", "parent_page": "", "public": 1, - "restrict_to_domain": "Manufacturing", + "quick_lists": [], + "restrict_to_domain": "", "roles": [], "sequence_id": 17.0, "shortcuts": [ From 653d6341d46fcc6e2c49167a0ecfc7ccb1003d35 Mon Sep 17 00:00:00 2001 From: Rucha Mahabal Date: Wed, 1 Jun 2022 12:14:42 +0530 Subject: [PATCH 041/105] refactor: clean-up and commonify payroll entry test setups --- .../payroll_entry/test_payroll_entry.py | 295 ++++++++---------- 1 file changed, 126 insertions(+), 169 deletions(-) diff --git a/erpnext/payroll/doctype/payroll_entry/test_payroll_entry.py b/erpnext/payroll/doctype/payroll_entry/test_payroll_entry.py index 5c68bd35ef9..47b9962912f 100644 --- a/erpnext/payroll/doctype/payroll_entry/test_payroll_entry.py +++ b/erpnext/payroll/doctype/payroll_entry/test_payroll_entry.py @@ -25,7 +25,6 @@ from erpnext.payroll.doctype.salary_slip.test_salary_slip import ( create_account, make_deduction_salary_component, make_earning_salary_component, - make_employee_salary_slip, set_salary_component_account, ) from erpnext.payroll.doctype.salary_structure.test_salary_structure import ( @@ -38,10 +37,6 @@ test_dependencies = ["Holiday List"] class TestPayrollEntry(FrappeTestCase): def setUp(self): - frappe.db.set_value( - "Company", erpnext.get_default_company(), "default_holiday_list", "_Test Holiday List" - ) - for dt in [ "Salary Slip", "Salary Component", @@ -51,76 +46,79 @@ class TestPayrollEntry(FrappeTestCase): "Salary Structure Assignment", "Payroll Employee Detail", "Additional Salary", + "Loan", ]: - frappe.db.sql("delete from `tab%s`" % dt) + frappe.db.delete(dt) make_earning_salary_component(setup=True, company_list=["_Test Company"]) make_deduction_salary_component(setup=True, test_tax=False, company_list=["_Test Company"]) + frappe.db.set_value("Company", "_Test Company", "default_holiday_list", "_Test Holiday List") frappe.db.set_value("Payroll Settings", None, "email_salary_slip_to_employee", 0) - def test_payroll_entry(self): # pylint: disable=no-self-use + # set default payable account + default_account = frappe.db.get_value( + "Company", "_Test Company", "default_payroll_payable_account" + ) + if not default_account or default_account != "_Test Payroll Payable - _TC": + create_account( + account_name="_Test Payroll Payable", + company="_Test Company", + parent_account="Current Liabilities - _TC", + account_type="Payable", + ) + frappe.db.set_value( + "Company", "_Test Company", "default_payroll_payable_account", "_Test Payroll Payable - _TC" + ) + + def test_payroll_entry(self): + company = frappe.get_doc("Company", "_Test Company") + employee = frappe.db.get_value("Employee", {"company": "_Test Company"}) + setup_salary_structure(employee, company) + + dates = get_start_end_dates("Monthly", nowdate()) + make_payroll_entry( + start_date=dates.start_date, + end_date=dates.end_date, + payable_account=company.default_payroll_payable_account, + currency=company.default_currency, + company=company.name, + ) + + def test_multi_currency_payroll_entry(self): company = erpnext.get_default_company() + employee = make_employee("test_muti_currency_employee@payroll.com", company=company) for data in frappe.get_all("Salary Component", fields=["name"]): if not frappe.db.get_value( "Salary Component Account", {"parent": data.name, "company": company}, "name" ): - set_salary_component_account(data.name) + get_salary_component_account(data.name) - employee = frappe.db.get_value("Employee", {"company": company}) company_doc = frappe.get_doc("Company", company) - make_salary_structure( - "_Test Salary Structure", - "Monthly", - employee, - company=company, - currency=company_doc.default_currency, + salary_structure = make_salary_structure( + "_Test Multi Currency Salary Structure", "Monthly", company=company, currency="USD" ) - dates = get_start_end_dates("Monthly", nowdate()) - if not frappe.db.get_value( - "Salary Slip", {"start_date": dates.start_date, "end_date": dates.end_date} - ): - make_payroll_entry( - start_date=dates.start_date, - end_date=dates.end_date, - payable_account=company_doc.default_payroll_payable_account, - currency=company_doc.default_currency, - ) - - def test_multi_currency_payroll_entry(self): - company = frappe.get_doc("Company", "_Test Company") - employee = make_employee( - "test_muti_currency_employee@payroll.com", company=company.name, department="Accounts - _TC" + create_salary_structure_assignment( + employee, salary_structure.name, company=company, currency="USD" ) - - for data in frappe.get_all("Salary Component", fields=["name"]): - if not frappe.db.get_value( - "Salary Component Account", {"parent": data.name, "company": company.name}, "name" - ): - set_salary_component_account(data.name) - - salary_struct = make_salary_structure( - "_Test Multi Currency Salary Structure", - "Monthly", - employee, - currency="USD", - company=company.name, + frappe.db.sql( + """delete from `tabSalary Slip` where employee=%s""", + (frappe.db.get_value("Employee", {"user_id": "test_muti_currency_employee@payroll.com"})), + ) + salary_slip = get_salary_slip( + "test_muti_currency_employee@payroll.com", "Monthly", "_Test Multi Currency Salary Structure" ) - - frappe.db.delete("Salary Slip", {"employee": employee}) dates = get_start_end_dates("Monthly", nowdate()) payroll_entry = make_payroll_entry( start_date=dates.start_date, end_date=dates.end_date, - payable_account=company.default_payroll_payable_account, + payable_account=company_doc.default_payroll_payable_account, currency="USD", exchange_rate=70, - company=company.name, ) payroll_entry.make_payment_entry() - salary_slip = frappe.db.get_value("Salary Slip", {"payroll_entry": payroll_entry.name}) - salary_slip = frappe.get_doc("Salary Slip", salary_slip) + salary_slip.load_from_db() payroll_je = salary_slip.journal_entry if payroll_je: @@ -143,22 +141,11 @@ class TestPayrollEntry(FrappeTestCase): self.assertEqual(salary_slip.base_net_pay, payment_entry[0].total_credit) def test_payroll_entry_with_employee_cost_center(self): - for data in frappe.get_all("Salary Component", fields=["name"]): - if not frappe.db.get_value( - "Salary Component Account", {"parent": data.name, "company": "_Test Company"}, "name" - ): - set_salary_component_account(data.name) - if not frappe.db.exists("Department", "cc - _TC"): frappe.get_doc( {"doctype": "Department", "department_name": "cc", "company": "_Test Company"} ).insert() - frappe.db.sql("""delete from `tabEmployee` where employee_name='test_employee1@example.com' """) - frappe.db.sql("""delete from `tabEmployee` where employee_name='test_employee2@example.com' """) - frappe.db.sql("""delete from `tabSalary Structure` where name='_Test Salary Structure 1' """) - frappe.db.sql("""delete from `tabSalary Structure` where name='_Test Salary Structure 2' """) - employee1 = make_employee( "test_employee1@example.com", payroll_cost_center="_Test Cost Center - _TC", @@ -169,38 +156,15 @@ class TestPayrollEntry(FrappeTestCase): "test_employee2@example.com", department="cc - _TC", company="_Test Company" ) - if not frappe.db.exists("Account", "_Test Payroll Payable - _TC"): - create_account( - account_name="_Test Payroll Payable", - company="_Test Company", - parent_account="Current Liabilities - _TC", - account_type="Payable", - ) + company = frappe.get_doc("Company", "_Test Company") + setup_salary_structure(employee1, company) - if ( - not frappe.db.get_value("Company", "_Test Company", "default_payroll_payable_account") - or frappe.db.get_value("Company", "_Test Company", "default_payroll_payable_account") - != "_Test Payroll Payable - _TC" - ): - frappe.db.set_value( - "Company", "_Test Company", "default_payroll_payable_account", "_Test Payroll Payable - _TC" - ) - currency = frappe.db.get_value("Company", "_Test Company", "default_currency") - - make_salary_structure( - "_Test Salary Structure 1", - "Monthly", - employee1, - company="_Test Company", - currency=currency, - test_tax=False, - ) ss = make_salary_structure( "_Test Salary Structure 2", "Monthly", employee2, company="_Test Company", - currency=currency, + currency=company.default_currency, test_tax=False, ) @@ -219,42 +183,38 @@ class TestPayrollEntry(FrappeTestCase): ssa_doc.append( "payroll_cost_centers", {"cost_center": "_Test Cost Center 2 - _TC", "percentage": 40} ) - ssa_doc.save() dates = get_start_end_dates("Monthly", nowdate()) - if not frappe.db.get_value( - "Salary Slip", {"start_date": dates.start_date, "end_date": dates.end_date} - ): - pe = make_payroll_entry( - start_date=dates.start_date, - end_date=dates.end_date, - payable_account="_Test Payroll Payable - _TC", - currency=frappe.db.get_value("Company", "_Test Company", "default_currency"), - department="cc - _TC", - company="_Test Company", - payment_account="Cash - _TC", - cost_center="Main - _TC", - ) - je = frappe.db.get_value("Salary Slip", {"payroll_entry": pe.name}, "journal_entry") - je_entries = frappe.db.sql( - """ - select account, cost_center, debit, credit - from `tabJournal Entry Account` - where parent=%s - order by account, cost_center - """, - je, - ) - expected_je = ( - ("_Test Payroll Payable - _TC", "Main - _TC", 0.0, 155600.0), - ("Salary - _TC", "_Test Cost Center - _TC", 124800.0, 0.0), - ("Salary - _TC", "_Test Cost Center 2 - _TC", 31200.0, 0.0), - ("Salary Deductions - _TC", "_Test Cost Center - _TC", 0.0, 320.0), - ("Salary Deductions - _TC", "_Test Cost Center 2 - _TC", 0.0, 80.0), - ) + pe = make_payroll_entry( + start_date=dates.start_date, + end_date=dates.end_date, + payable_account="_Test Payroll Payable - _TC", + currency=frappe.db.get_value("Company", "_Test Company", "default_currency"), + department="cc - _TC", + company="_Test Company", + payment_account="Cash - _TC", + cost_center="Main - _TC", + ) + je = frappe.db.get_value("Salary Slip", {"payroll_entry": pe.name}, "journal_entry") + je_entries = frappe.db.sql( + """ + select account, cost_center, debit, credit + from `tabJournal Entry Account` + where parent=%s + order by account, cost_center + """, + je, + ) + expected_je = ( + ("_Test Payroll Payable - _TC", "Main - _TC", 0.0, 155600.0), + ("Salary - _TC", "_Test Cost Center - _TC", 124800.0, 0.0), + ("Salary - _TC", "_Test Cost Center 2 - _TC", 31200.0, 0.0), + ("Salary Deductions - _TC", "_Test Cost Center - _TC", 0.0, 320.0), + ("Salary Deductions - _TC", "_Test Cost Center 2 - _TC", 0.0, 80.0), + ) - self.assertEqual(je_entries, expected_je) + self.assertEqual(je_entries, expected_je) def test_get_end_date(self): self.assertEqual(get_end_date("2017-01-01", "monthly"), {"end_date": "2017-01-31"}) @@ -267,31 +227,22 @@ class TestPayrollEntry(FrappeTestCase): self.assertEqual(get_end_date("2017-02-15", "daily"), {"end_date": "2017-02-15"}) def test_loan(self): - branch = "Test Employee Branch" - applicant = make_employee("test_employee@loan.com", company="_Test Company") company = "_Test Company" - holiday_list = make_holiday("test holiday for loan") - - company_doc = frappe.get_doc("Company", company) - if not company_doc.default_payroll_payable_account: - company_doc.default_payroll_payable_account = frappe.db.get_value( - "Account", {"company": company, "root_type": "Liability", "account_type": ""}, "name" - ) - company_doc.save() + branch = "Test Employee Branch" if not frappe.db.exists("Branch", branch): frappe.get_doc({"doctype": "Branch", "branch": branch}).insert() + holiday_list = make_holiday("test holiday for loan") - employee_doc = frappe.get_doc("Employee", applicant) - employee_doc.branch = branch - employee_doc.holiday_list = holiday_list - employee_doc.save() + applicant = make_employee( + "test_employee@loan.com", company="_Test Company", branch=branch, holiday_list=holiday_list + ) + company_doc = frappe.get_doc("Company", company) - salary_structure = "Test Salary Structure for Loan" make_salary_structure( - salary_structure, + "Test Salary Structure for Loan", "Monthly", - employee=employee_doc.name, + employee=applicant, company="_Test Company", currency=company_doc.default_currency, ) @@ -352,21 +303,11 @@ class TestPayrollEntry(FrappeTestCase): self.assertEqual(row.principal_amount, principal_amount) self.assertEqual(row.total_payment, interest_amount + principal_amount) - if salary_slip.docstatus == 0: - frappe.delete_doc("Salary Slip", name) - def test_salary_slip_operation_queueing(self): - # setup - company = erpnext.get_default_company() + company = "_Test Company" company_doc = frappe.get_doc("Company", company) employee = frappe.db.get_value("Employee", {"company": company}) - make_salary_structure( - "_Test Salary Structure", - "Monthly", - employee, - company=company, - currency=company_doc.default_currency, - ) + setup_salary_structure(employee, company_doc) # enqueue salary slip creation via payroll entry # Payroll Entry status should change to Queued @@ -376,6 +317,7 @@ class TestPayrollEntry(FrappeTestCase): end_date=dates.end_date, payable_account=company_doc.default_payroll_payable_account, currency=company_doc.default_currency, + company=company_doc.name, ) frappe.flags.enqueue_payroll_entry = True payroll_entry.create_salary_slips() @@ -385,10 +327,10 @@ class TestPayrollEntry(FrappeTestCase): frappe.flags.enqueue_payroll_entry = False def test_salary_slip_operation_failure(self): - # setup - company = erpnext.get_default_company() + company = "_Test Company" company_doc = frappe.get_doc("Company", company) employee = frappe.db.get_value("Employee", {"company": company}) + salary_structure = make_salary_structure( "_Test Salary Structure", "Monthly", @@ -410,6 +352,7 @@ class TestPayrollEntry(FrappeTestCase): end_date=dates.end_date, payable_account=company_doc.default_payroll_payable_account, currency=company_doc.default_currency, + company=company_doc.name, ) payroll_entry.create_salary_slips() payroll_entry.submit_salary_slips() @@ -419,41 +362,30 @@ class TestPayrollEntry(FrappeTestCase): self.assertIsNotNone(payroll_entry.error_message) # set accounts - for data in frappe.get_all("Salary Component", fields=["name"]): - if not frappe.db.get_value( - "Salary Component Account", {"parent": data.name, "company": company}, "name" - ): - set_salary_component_account(data.name, company_list=[company]) + for data in frappe.get_all("Salary Component", pluck="name"): + set_salary_component_account(data, company_list=[company]) # Payroll Entry successful, status should change to Submitted payroll_entry.submit_salary_slips() payroll_entry.reload() + self.assertEqual(payroll_entry.status, "Submitted") self.assertEqual(payroll_entry.error_message, "") def test_payroll_entry_status(self): - company = erpnext.get_default_company() - for data in frappe.get_all("Salary Component", fields=["name"]): - if not frappe.db.get_value( - "Salary Component Account", {"parent": data.name, "company": company}, "name" - ): - set_salary_component_account(data.name) - - employee = frappe.db.get_value("Employee", {"company": company}) + company = "_Test Company" company_doc = frappe.get_doc("Company", company) - make_salary_structure( - "_Test Salary Structure", - "Monthly", - employee, - company=company, - currency=company_doc.default_currency, - ) + employee = frappe.db.get_value("Employee", {"company": company}) + + setup_salary_structure(employee, company_doc) + dates = get_start_end_dates("Monthly", nowdate()) payroll_entry = get_payroll_entry_data( start_date=dates.start_date, end_date=dates.end_date, payable_account=company_doc.default_payroll_payable_account, currency=company_doc.default_currency, + company=company_doc.name, ) payroll_entry.submit() self.assertEqual(payroll_entry.status, "Submitted") @@ -532,3 +464,28 @@ def make_holiday(holiday_list_name): ).insert() return holiday_list_name + + +def get_salary_slip(user, period, salary_structure): + salary_slip = make_employee_salary_slip(user, period, salary_structure) + salary_slip.exchange_rate = 70 + salary_slip.calculate_net_pay() + salary_slip.db_update() + + return salary_slip + + +def setup_salary_structure(employee, company_doc, currency=None, salary_structure=None): + for data in frappe.get_all("Salary Component", pluck="name"): + if not frappe.db.get_value( + "Salary Component Account", {"parent": data, "company": company_doc.name}, "name" + ): + set_salary_component_account(data) + + make_salary_structure( + salary_structure or "_Test Salary Structure", + "Monthly", + employee, + company=company_doc.name, + currency=(currency or company_doc.default_currency), + ) From d4b9cc02420fff8310f547b55ae158c717ec8fb0 Mon Sep 17 00:00:00 2001 From: Rucha Mahabal Date: Wed, 1 Jun 2022 12:18:14 +0530 Subject: [PATCH 042/105] fix: remove leave policy assignment creation patch (#31097) --- erpnext/patches.txt | 1 - ..._based_on_employee_current_leave_policy.py | 94 ------------------- 2 files changed, 95 deletions(-) delete mode 100644 erpnext/patches/v13_0/create_leave_policy_assignment_based_on_employee_current_leave_policy.py diff --git a/erpnext/patches.txt b/erpnext/patches.txt index 785e2baa11b..ad1ba2a1573 100644 --- a/erpnext/patches.txt +++ b/erpnext/patches.txt @@ -231,7 +231,6 @@ erpnext.patches.v13_0.updates_for_multi_currency_payroll erpnext.patches.v13_0.update_reason_for_resignation_in_employee execute:frappe.delete_doc("Report", "Quoted Item Comparison") erpnext.patches.v13_0.update_member_email_address -erpnext.patches.v13_0.create_leave_policy_assignment_based_on_employee_current_leave_policy erpnext.patches.v13_0.update_pos_closing_entry_in_merge_log erpnext.patches.v13_0.add_po_to_global_search erpnext.patches.v13_0.update_returned_qty_in_pr_dn diff --git a/erpnext/patches/v13_0/create_leave_policy_assignment_based_on_employee_current_leave_policy.py b/erpnext/patches/v13_0/create_leave_policy_assignment_based_on_employee_current_leave_policy.py deleted file mode 100644 index 59b17eea9fe..00000000000 --- a/erpnext/patches/v13_0/create_leave_policy_assignment_based_on_employee_current_leave_policy.py +++ /dev/null @@ -1,94 +0,0 @@ -# Copyright (c) 2019, Frappe and Contributors -# License: GNU General Public License v3. See license.txt - - -import frappe - - -def execute(): - frappe.reload_doc("hr", "doctype", "leave_policy_assignment") - frappe.reload_doc("hr", "doctype", "employee_grade") - employee_with_assignment = [] - leave_policy = [] - - if "leave_policy" in frappe.db.get_table_columns("Employee"): - employees_with_leave_policy = frappe.db.sql( - "SELECT name, leave_policy FROM `tabEmployee` WHERE leave_policy IS NOT NULL and leave_policy != ''", - as_dict=1, - ) - - for employee in employees_with_leave_policy: - alloc = frappe.db.exists( - "Leave Allocation", - {"employee": employee.name, "leave_policy": employee.leave_policy, "docstatus": 1}, - ) - if not alloc: - create_assignment(employee.name, employee.leave_policy) - - employee_with_assignment.append(employee.name) - leave_policy.append(employee.leave_policy) - - if "default_leave_policy" in frappe.db.get_table_columns("Employee Grade"): - employee_grade_with_leave_policy = frappe.db.sql( - "SELECT name, default_leave_policy FROM `tabEmployee Grade` WHERE default_leave_policy IS NOT NULL and default_leave_policy!=''", - as_dict=1, - ) - - # for whole employee Grade - for grade in employee_grade_with_leave_policy: - employees = get_employee_with_grade(grade.name) - for employee in employees: - - if employee not in employee_with_assignment: # Will ensure no duplicate - alloc = frappe.db.exists( - "Leave Allocation", - {"employee": employee.name, "leave_policy": grade.default_leave_policy, "docstatus": 1}, - ) - if not alloc: - create_assignment(employee.name, grade.default_leave_policy) - leave_policy.append(grade.default_leave_policy) - - # for old Leave allocation and leave policy from allocation, which may got updated in employee grade. - leave_allocations = frappe.db.sql( - "SELECT leave_policy, leave_period, employee FROM `tabLeave Allocation` WHERE leave_policy IS NOT NULL and leave_policy != '' and docstatus = 1 ", - as_dict=1, - ) - - for allocation in leave_allocations: - if allocation.leave_policy not in leave_policy: - create_assignment( - allocation.employee, - allocation.leave_policy, - leave_period=allocation.leave_period, - allocation_exists=True, - ) - - -def create_assignment(employee, leave_policy, leave_period=None, allocation_exists=False): - if frappe.db.get_value("Leave Policy", leave_policy, "docstatus") == 2: - return - - filters = {"employee": employee, "leave_policy": leave_policy} - if leave_period: - filters["leave_period"] = leave_period - - if not frappe.db.exists("Leave Policy Assignment", filters): - lpa = frappe.new_doc("Leave Policy Assignment") - lpa.employee = employee - lpa.leave_policy = leave_policy - - lpa.flags.ignore_mandatory = True - if allocation_exists: - lpa.assignment_based_on = "Leave Period" - lpa.leave_period = leave_period - lpa.leaves_allocated = 1 - - lpa.save() - if allocation_exists: - lpa.submit() - # Updating old Leave Allocation - frappe.db.sql("Update `tabLeave Allocation` set leave_policy_assignment = %s", lpa.name) - - -def get_employee_with_grade(grade): - return frappe.get_list("Employee", filters={"grade": grade}) From 03a24ce774ad79c928ba30944ad014c3b0617e8b Mon Sep 17 00:00:00 2001 From: Ankush Menat Date: Wed, 1 Jun 2022 12:33:23 +0530 Subject: [PATCH 043/105] chore: delete dead cypress code Moved to separate repo --- cypress.json | 11 - cypress/fixtures/example.json | 5 - .../test_bulk_transaction_processing.js | 44 ---- cypress/integration/test_customer.js | 13 -- cypress/integration/test_item.js | 44 ---- .../test_organizational_chart_desktop.js | 116 ----------- .../test_organizational_chart_mobile.js | 195 ------------------ cypress/plugins/index.js | 17 -- cypress/support/commands.js | 31 --- cypress/support/index.js | 26 --- cypress/tsconfig.json | 12 -- 11 files changed, 514 deletions(-) delete mode 100644 cypress.json delete mode 100644 cypress/fixtures/example.json delete mode 100644 cypress/integration/test_bulk_transaction_processing.js delete mode 100644 cypress/integration/test_customer.js delete mode 100644 cypress/integration/test_item.js delete mode 100644 cypress/integration/test_organizational_chart_desktop.js delete mode 100644 cypress/integration/test_organizational_chart_mobile.js delete mode 100644 cypress/plugins/index.js delete mode 100644 cypress/support/commands.js delete mode 100644 cypress/support/index.js delete mode 100644 cypress/tsconfig.json diff --git a/cypress.json b/cypress.json deleted file mode 100644 index 02b10d893f8..00000000000 --- a/cypress.json +++ /dev/null @@ -1,11 +0,0 @@ -{ - "baseUrl": "http://test_site:8000/", - "projectId": "da59y9", - "adminPassword": "admin", - "defaultCommandTimeout": 20000, - "pageLoadTimeout": 15000, - "retries": { - "runMode": 2, - "openMode": 2 - } -} diff --git a/cypress/fixtures/example.json b/cypress/fixtures/example.json deleted file mode 100644 index da18d9352a1..00000000000 --- a/cypress/fixtures/example.json +++ /dev/null @@ -1,5 +0,0 @@ -{ - "name": "Using fixtures to represent data", - "email": "hello@cypress.io", - "body": "Fixtures are a great way to mock data for responses to routes" -} \ No newline at end of file diff --git a/cypress/integration/test_bulk_transaction_processing.js b/cypress/integration/test_bulk_transaction_processing.js deleted file mode 100644 index 428ec5100b5..00000000000 --- a/cypress/integration/test_bulk_transaction_processing.js +++ /dev/null @@ -1,44 +0,0 @@ -describe("Bulk Transaction Processing", () => { - before(() => { - cy.login(); - cy.visit("/app/website"); - }); - - it("Creates To Sales Order", () => { - cy.visit("/app/sales-order"); - cy.url().should("include", "/sales-order"); - cy.window() - .its("frappe.csrf_token") - .then((csrf_token) => { - return cy - .request({ - url: "/api/method/erpnext.tests.ui_test_bulk_transaction_processing.create_records", - method: "POST", - headers: { - Accept: "application/json", - "Content-Type": "application/json", - "X-Frappe-CSRF-Token": csrf_token, - }, - timeout: 60000, - }) - .then((res) => { - expect(res.status).eq(200); - }); - }); - cy.wait(5000); - cy.get( - ".list-row-head > .list-header-subject > .list-row-col > .list-check-all" - ).check({ force: true }); - cy.wait(3000); - cy.get(".actions-btn-group > .btn-primary").click({ force: true }); - cy.wait(3000); - cy.get(".dropdown-menu-right > .user-action > .dropdown-item") - .contains("Sales Invoice") - .click({ force: true }); - cy.wait(3000); - cy.get(".modal-content > .modal-footer > .standard-actions") - .contains("Yes") - .click({ force: true }); - cy.contains("Creation of Sales Invoice successful"); - }); -}); diff --git a/cypress/integration/test_customer.js b/cypress/integration/test_customer.js deleted file mode 100644 index 3d6ed5d0d89..00000000000 --- a/cypress/integration/test_customer.js +++ /dev/null @@ -1,13 +0,0 @@ - -context('Customer', () => { - before(() => { - cy.login(); - }); - it('Check Customer Group', () => { - cy.visit(`app/customer/`); - cy.get('.primary-action').click(); - cy.wait(500); - cy.get('.custom-actions > .btn').click(); - cy.get_field('customer_group', 'Link').should('have.value', 'All Customer Groups'); - }); -}); diff --git a/cypress/integration/test_item.js b/cypress/integration/test_item.js deleted file mode 100644 index fcb7533a225..00000000000 --- a/cypress/integration/test_item.js +++ /dev/null @@ -1,44 +0,0 @@ -describe("Test Item Dashboard", () => { - before(() => { - cy.login(); - cy.visit("/app/item"); - cy.insert_doc( - "Item", - { - item_code: "e2e_test_item", - item_group: "All Item Groups", - opening_stock: 42, - valuation_rate: 100, - }, - true - ); - cy.go_to_doc("item", "e2e_test_item"); - }); - - it("should show dashboard with correct data on first load", () => { - cy.get(".stock-levels").contains("Stock Levels").should("be.visible"); - cy.get(".stock-levels").contains("e2e_test_item").should("exist"); - - // reserved and available qty - cy.get(".stock-levels .inline-graph-count") - .eq(0) - .contains("0") - .should("exist"); - cy.get(".stock-levels .inline-graph-count") - .eq(1) - .contains("42") - .should("exist"); - }); - - it("should persist on field change", () => { - cy.get('input[data-fieldname="disabled"]').check(); - cy.wait(500); - cy.get(".stock-levels").contains("Stock Levels").should("be.visible"); - cy.get(".stock-levels").should("have.length", 1); - }); - - it("should persist on reload", () => { - cy.reload(); - cy.get(".stock-levels").contains("Stock Levels").should("be.visible"); - }); -}); diff --git a/cypress/integration/test_organizational_chart_desktop.js b/cypress/integration/test_organizational_chart_desktop.js deleted file mode 100644 index 464cce48d03..00000000000 --- a/cypress/integration/test_organizational_chart_desktop.js +++ /dev/null @@ -1,116 +0,0 @@ -context('Organizational Chart', () => { - before(() => { - cy.login(); - cy.visit('/app/website'); - }); - - it('navigates to org chart', () => { - cy.visit('/app'); - cy.visit('/app/organizational-chart'); - cy.url().should('include', '/organizational-chart'); - - cy.window().its('frappe.csrf_token').then(csrf_token => { - return cy.request({ - url: `/api/method/erpnext.tests.ui_test_helpers.create_employee_records`, - method: 'POST', - headers: { - Accept: 'application/json', - 'Content-Type': 'application/json', - 'X-Frappe-CSRF-Token': csrf_token - }, - timeout: 60000 - }).then(res => { - expect(res.status).eq(200); - cy.get('.frappe-control[data-fieldname=company] input').focus().as('input'); - cy.get('@input') - .clear({ force: true }) - .type('Test Org Chart{downarrow}{enter}', { force: true }) - .blur({ force: true }); - }); - }); - }); - - it('renders root nodes and loads children for the first expandable node', () => { - // check rendered root nodes and the node name, title, connections - cy.get('.hierarchy').find('.root-level ul.node-children').children() - .should('have.length', 2) - .first() - .as('first-child'); - - cy.get('@first-child').get('.node-name').contains('Test Employee 1'); - cy.get('@first-child').get('.node-info').find('.node-title').contains('CEO'); - cy.get('@first-child').get('.node-info').find('.node-connections').contains('· 2 Connections'); - - cy.call('erpnext.tests.ui_test_helpers.get_employee_records').then(employee_records => { - // children of 1st root visible - cy.get(`div[data-parent="${employee_records.message[0]}"]`).as('child-node'); - cy.get('@child-node') - .should('have.length', 1) - .should('be.visible'); - cy.get('@child-node').get('.node-name').contains('Test Employee 3'); - - // connectors between first root node and immediate child - cy.get(`path[data-parent="${employee_records.message[0]}"]`) - .should('be.visible') - .invoke('attr', 'data-child') - .should('equal', employee_records.message[2]); - }); - }); - - it('hides active nodes children and connectors on expanding sibling node', () => { - cy.call('erpnext.tests.ui_test_helpers.get_employee_records').then(employee_records => { - // click sibling - cy.get(`#${employee_records.message[1]}`) - .click() - .should('have.class', 'active'); - - // child nodes and connectors hidden - cy.get(`[data-parent="${employee_records.message[0]}"]`).should('not.be.visible'); - cy.get(`path[data-parent="${employee_records.message[0]}"]`).should('not.be.visible'); - }); - }); - - it('collapses previous level nodes and refreshes connectors on expanding child node', () => { - cy.call('erpnext.tests.ui_test_helpers.get_employee_records').then(employee_records => { - // click child node - cy.get(`#${employee_records.message[3]}`) - .click() - .should('have.class', 'active'); - - // previous level nodes: parent should be on active-path; other nodes should be collapsed - cy.get(`#${employee_records.message[0]}`).should('have.class', 'collapsed'); - cy.get(`#${employee_records.message[1]}`).should('have.class', 'active-path'); - - // previous level connectors refreshed - cy.get(`path[data-parent="${employee_records.message[1]}"]`) - .should('have.class', 'collapsed-connector'); - - // child node's children and connectors rendered - cy.get(`[data-parent="${employee_records.message[3]}"]`).should('be.visible'); - cy.get(`path[data-parent="${employee_records.message[3]}"]`).should('be.visible'); - }); - }); - - it('expands previous level nodes', () => { - cy.call('erpnext.tests.ui_test_helpers.get_employee_records').then(employee_records => { - cy.get(`#${employee_records.message[0]}`) - .click() - .should('have.class', 'active'); - - cy.get(`[data-parent="${employee_records.message[0]}"]`) - .should('be.visible'); - - cy.get('ul.hierarchy').children().should('have.length', 2); - cy.get(`#connectors`).children().should('have.length', 1); - }); - }); - - it('edit node navigates to employee master', () => { - cy.call('erpnext.tests.ui_test_helpers.get_employee_records').then(employee_records => { - cy.get(`#${employee_records.message[0]}`).find('.btn-edit-node') - .click(); - - cy.url().should('include', `/employee/${employee_records.message[0]}`); - }); - }); -}); diff --git a/cypress/integration/test_organizational_chart_mobile.js b/cypress/integration/test_organizational_chart_mobile.js deleted file mode 100644 index 971ac6d3ef3..00000000000 --- a/cypress/integration/test_organizational_chart_mobile.js +++ /dev/null @@ -1,195 +0,0 @@ -context('Organizational Chart Mobile', () => { - before(() => { - cy.login(); - cy.visit('/app/website'); - }); - - it('navigates to org chart', () => { - cy.viewport(375, 667); - cy.visit('/app'); - cy.visit('/app/organizational-chart'); - cy.url().should('include', '/organizational-chart'); - - cy.window().its('frappe.csrf_token').then(csrf_token => { - return cy.request({ - url: `/api/method/erpnext.tests.ui_test_helpers.create_employee_records`, - method: 'POST', - headers: { - Accept: 'application/json', - 'Content-Type': 'application/json', - 'X-Frappe-CSRF-Token': csrf_token - }, - timeout: 60000 - }).then(res => { - expect(res.status).eq(200); - cy.get('.frappe-control[data-fieldname=company] input').focus().as('input'); - cy.get('@input') - .clear({ force: true }) - .type('Test Org Chart{downarrow}{enter}', { force: true }) - .blur({ force: true }); - }); - }); - }); - - it('renders root nodes', () => { - // check rendered root nodes and the node name, title, connections - cy.get('.hierarchy-mobile').find('.root-level').children() - .should('have.length', 2) - .first() - .as('first-child'); - - cy.get('@first-child').get('.node-name').contains('Test Employee 1'); - cy.get('@first-child').get('.node-info').find('.node-title').contains('CEO'); - cy.get('@first-child').get('.node-info').find('.node-connections').contains('· 2'); - }); - - it('expands root node', () => { - cy.call('erpnext.tests.ui_test_helpers.get_employee_records').then(employee_records => { - cy.get(`#${employee_records.message[1]}`) - .click() - .should('have.class', 'active'); - - // other root node removed - cy.get(`#${employee_records.message[0]}`).should('not.exist'); - - // children of active root node - cy.get('.hierarchy-mobile').find('.level').first().find('ul.node-children').children() - .should('have.length', 2); - - cy.get(`div[data-parent="${employee_records.message[1]}"]`).first().as('child-node'); - cy.get('@child-node').should('be.visible'); - - cy.get('@child-node') - .get('.node-name') - .contains('Test Employee 4'); - - // connectors between root node and immediate children - cy.get(`path[data-parent="${employee_records.message[1]}"]`).as('connectors'); - cy.get('@connectors') - .should('have.length', 2) - .should('be.visible'); - - cy.get('@connectors') - .first() - .invoke('attr', 'data-child') - .should('eq', employee_records.message[3]); - }); - }); - - it('expands child node', () => { - cy.call('erpnext.tests.ui_test_helpers.get_employee_records').then(employee_records => { - cy.get(`#${employee_records.message[3]}`) - .click() - .should('have.class', 'active') - .as('expanded_node'); - - // 2 levels on screen; 1 on active path; 1 collapsed - cy.get('.hierarchy-mobile').children().should('have.length', 2); - cy.get(`#${employee_records.message[1]}`).should('have.class', 'active-path'); - - // children of expanded node visible - cy.get('@expanded_node') - .next() - .should('have.class', 'node-children') - .as('node-children'); - - cy.get('@node-children').children().should('have.length', 1); - cy.get('@node-children') - .first() - .get('.node-card') - .should('have.class', 'active-child') - .contains('Test Employee 7'); - - // orphan connectors removed - cy.get(`#connectors`).children().should('have.length', 2); - }); - }); - - it('renders sibling group', () => { - cy.call('erpnext.tests.ui_test_helpers.get_employee_records').then(employee_records => { - // sibling group visible for parent - cy.get(`#${employee_records.message[1]}`) - .next() - .as('sibling_group'); - - cy.get('@sibling_group') - .should('have.attr', 'data-parent', 'undefined') - .should('have.class', 'node-group') - .and('have.class', 'collapsed'); - - cy.get('@sibling_group').get('.avatar-group').children().as('siblings'); - cy.get('@siblings').should('have.length', 1); - cy.get('@siblings') - .first() - .should('have.attr', 'title', 'Test Employee 1'); - - }); - }); - - it('expands previous level nodes', () => { - cy.call('erpnext.tests.ui_test_helpers.get_employee_records').then(employee_records => { - cy.get(`#${employee_records.message[6]}`) - .click() - .should('have.class', 'active'); - - // clicking on previous level node should remove all the nodes ahead - // and expand that node - cy.get(`#${employee_records.message[3]}`).click(); - cy.get(`#${employee_records.message[3]}`) - .should('have.class', 'active') - .should('not.have.class', 'active-path'); - - cy.get(`#${employee_records.message[6]}`).should('have.class', 'active-child'); - cy.get('.hierarchy-mobile').children().should('have.length', 2); - cy.get(`#connectors`).children().should('have.length', 2); - }); - }); - - it('expands sibling group', () => { - cy.call('erpnext.tests.ui_test_helpers.get_employee_records').then(employee_records => { - // sibling group visible for parent - cy.get(`#${employee_records.message[6]}`).click(); - - cy.get(`#${employee_records.message[3]}`) - .next() - .click(); - - // siblings of parent should be visible - cy.get('.hierarchy-mobile').prev().as('sibling_group'); - cy.get('@sibling_group') - .should('exist') - .should('have.class', 'sibling-group') - .should('not.have.class', 'collapsed'); - - cy.get(`#${employee_records.message[1]}`) - .should('be.visible') - .should('have.class', 'active'); - - cy.get(`[data-parent="${employee_records.message[1]}"]`) - .should('be.visible') - .should('have.length', 2) - .should('have.class', 'active-child'); - }); - }); - - it('goes to the respective level after clicking on non-collapsed sibling group', () => { - cy.call('erpnext.tests.ui_test_helpers.get_employee_records').then(() => { - // click on non-collapsed sibling group - cy.get('.hierarchy-mobile') - .prev() - .click(); - - // should take you to that level - cy.get('.hierarchy-mobile').find('li.level .node-card').should('have.length', 2); - }); - }); - - it('edit node navigates to employee master', () => { - cy.call('erpnext.tests.ui_test_helpers.get_employee_records').then(employee_records => { - cy.get(`#${employee_records.message[0]}`).find('.btn-edit-node') - .click(); - - cy.url().should('include', `/employee/${employee_records.message[0]}`); - }); - }); -}); diff --git a/cypress/plugins/index.js b/cypress/plugins/index.js deleted file mode 100644 index 07d9804a733..00000000000 --- a/cypress/plugins/index.js +++ /dev/null @@ -1,17 +0,0 @@ -// *********************************************************** -// This example plugins/index.js can be used to load plugins -// -// You can change the location of this file or turn off loading -// the plugins file with the 'pluginsFile' configuration option. -// -// You can read more here: -// https://on.cypress.io/plugins-guide -// *********************************************************** - -// This function is called when a project is opened or re-opened (e.g. due to -// the project's config changing) - -module.exports = () => { - // `on` is used to hook into various events Cypress emits - // `config` is the resolved Cypress config -}; diff --git a/cypress/support/commands.js b/cypress/support/commands.js deleted file mode 100644 index 7ddc80ab8dd..00000000000 --- a/cypress/support/commands.js +++ /dev/null @@ -1,31 +0,0 @@ -// *********************************************** -// This example commands.js shows you how to -// create various custom commands and overwrite -// existing commands. -// -// For more comprehensive examples of custom -// commands please read more here: -// https://on.cypress.io/custom-commands -// *********************************************** -// -// -// -- This is a parent command -- -// Cypress.Commands.add("login", (email, password) => { ... }); -// -// -// -- This is a child command -- -// Cypress.Commands.add("drag", { prevSubject: 'element'}, (subject, options) => { ... }); -// -// -// -- This is a dual command -- -// Cypress.Commands.add("dismiss", { prevSubject: 'optional'}, (subject, options) => { ... }); -// -// -// -- This is will overwrite an existing command -- -// Cypress.Commands.overwrite("visit", (originalFn, url, options) => { ... }); - -const slug = (name) => name.toLowerCase().replace(" ", "-"); - -Cypress.Commands.add("go_to_doc", (doctype, name) => { - cy.visit(`/app/${slug(doctype)}/${encodeURIComponent(name)}`); -}); diff --git a/cypress/support/index.js b/cypress/support/index.js deleted file mode 100644 index 72070cc81c4..00000000000 --- a/cypress/support/index.js +++ /dev/null @@ -1,26 +0,0 @@ -// *********************************************************** -// This example support/index.js is processed and -// loaded automatically before your test files. -// -// This is a great place to put global configuration and -// behavior that modifies Cypress. -// -// You can change the location of this file or turn off -// automatically serving support files with the -// 'supportFile' configuration option. -// -// You can read more here: -// https://on.cypress.io/configuration -// *********************************************************** - -// Import commands.js using ES2015 syntax: -import './commands'; -import '../../../frappe/cypress/support/commands' // eslint-disable-line - - -// Alternatively you can use CommonJS syntax: -// require('./commands') - -Cypress.Cookies.defaults({ - preserve: 'sid' -}); diff --git a/cypress/tsconfig.json b/cypress/tsconfig.json deleted file mode 100644 index d90ebf6856d..00000000000 --- a/cypress/tsconfig.json +++ /dev/null @@ -1,12 +0,0 @@ -{ - "compilerOptions": { - "allowJs": true, - "baseUrl": "../node_modules", - "types": [ - "cypress" - ] - }, - "include": [ - "**/*.*" - ] -} \ No newline at end of file From c84e11ac82d8fa2dd4d457a4ffd7ea1ca124e482 Mon Sep 17 00:00:00 2001 From: Ankush Menat Date: Wed, 1 Jun 2022 12:55:10 +0530 Subject: [PATCH 044/105] fix: re-validate warehouse after 'update items' (#31203) --- erpnext/controllers/accounts_controller.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/erpnext/controllers/accounts_controller.py b/erpnext/controllers/accounts_controller.py index 056084b7e81..0dd6a5c3334 100644 --- a/erpnext/controllers/accounts_controller.py +++ b/erpnext/controllers/accounts_controller.py @@ -2661,7 +2661,8 @@ def update_child_qty_rate(parent_doctype, trans_items, parent_doctype_name, chil parent.update_reserved_qty_for_subcontract() parent.create_raw_materials_supplied("supplied_items") parent.save() - else: + else: # Sales Order + parent.validate_warehouse() parent.update_reserved_qty() parent.update_project() parent.update_prevdoc_status("submit") From 536f1dfc4b4c7286bab41ded93c2d221023162d8 Mon Sep 17 00:00:00 2001 From: Rucha Mahabal Date: Wed, 1 Jun 2022 13:56:55 +0530 Subject: [PATCH 045/105] test: fix attendance tests for unmarked days (#31205) * test: fix attendance tests for unmarked days * chore: remove unused import --- .../hr/doctype/attendance/test_attendance.py | 61 +++++++++++-------- 1 file changed, 35 insertions(+), 26 deletions(-) diff --git a/erpnext/hr/doctype/attendance/test_attendance.py b/erpnext/hr/doctype/attendance/test_attendance.py index 762d0f7567e..c85ec6551a4 100644 --- a/erpnext/hr/doctype/attendance/test_attendance.py +++ b/erpnext/hr/doctype/attendance/test_attendance.py @@ -3,7 +3,15 @@ import frappe from frappe.tests.utils import FrappeTestCase -from frappe.utils import add_days, get_year_ending, get_year_start, getdate, now_datetime, nowdate +from frappe.utils import ( + add_days, + add_months, + get_last_day, + get_year_ending, + get_year_start, + getdate, + nowdate, +) from erpnext.hr.doctype.attendance.attendance import ( DuplicateAttendanceError, @@ -138,69 +146,70 @@ class TestAttendance(FrappeTestCase): self.assertEqual(attendance, fetch_attendance) def test_unmarked_days(self): - now = now_datetime() - previous_month = now.month - 1 - first_day = now.replace(day=1).replace(month=previous_month).date() + first_sunday = get_first_sunday( + self.holiday_list, for_date=get_last_day(add_months(getdate(), -1)) + ) + attendance_date = add_days(first_sunday, 1) employee = make_employee( - "test_unmarked_days@example.com", date_of_joining=add_days(first_day, -1) + "test_unmarked_days@example.com", date_of_joining=add_days(attendance_date, -1) ) frappe.db.set_value("Employee", employee, "holiday_list", self.holiday_list) - first_sunday = get_first_sunday(self.holiday_list, for_date=first_day) - mark_attendance(employee, first_day, "Present") - month_name = get_month_name(first_day) + mark_attendance(employee, attendance_date, "Present") + month_name = get_month_name(attendance_date) unmarked_days = get_unmarked_days(employee, month_name) unmarked_days = [getdate(date) for date in unmarked_days] # attendance already marked for the day - self.assertNotIn(first_day, unmarked_days) + self.assertNotIn(attendance_date, unmarked_days) # attendance unmarked - self.assertIn(getdate(add_days(first_day, 1)), unmarked_days) + self.assertIn(getdate(add_days(attendance_date, 1)), unmarked_days) # holiday considered in unmarked days self.assertIn(first_sunday, unmarked_days) def test_unmarked_days_excluding_holidays(self): - now = now_datetime() - previous_month = now.month - 1 - first_day = now.replace(day=1).replace(month=previous_month).date() + first_sunday = get_first_sunday( + self.holiday_list, for_date=get_last_day(add_months(getdate(), -1)) + ) + attendance_date = add_days(first_sunday, 1) employee = make_employee( - "test_unmarked_days@example.com", date_of_joining=add_days(first_day, -1) + "test_unmarked_days@example.com", date_of_joining=add_days(attendance_date, -1) ) frappe.db.set_value("Employee", employee, "holiday_list", self.holiday_list) - first_sunday = get_first_sunday(self.holiday_list, for_date=first_day) - mark_attendance(employee, first_day, "Present") - month_name = get_month_name(first_day) + mark_attendance(employee, attendance_date, "Present") + month_name = get_month_name(attendance_date) unmarked_days = get_unmarked_days(employee, month_name, exclude_holidays=True) unmarked_days = [getdate(date) for date in unmarked_days] # attendance already marked for the day - self.assertNotIn(first_day, unmarked_days) + self.assertNotIn(attendance_date, unmarked_days) # attendance unmarked - self.assertIn(getdate(add_days(first_day, 1)), unmarked_days) + self.assertIn(getdate(add_days(attendance_date, 1)), unmarked_days) # holidays not considered in unmarked days self.assertNotIn(first_sunday, unmarked_days) def test_unmarked_days_as_per_joining_and_relieving_dates(self): - now = now_datetime() - previous_month = now.month - 1 - first_day = now.replace(day=1).replace(month=previous_month).date() + first_sunday = get_first_sunday( + self.holiday_list, for_date=get_last_day(add_months(getdate(), -1)) + ) + date = add_days(first_sunday, 1) - doj = add_days(first_day, 1) - relieving_date = add_days(first_day, 5) + doj = add_days(date, 1) + relieving_date = add_days(date, 5) employee = make_employee( "test_unmarked_days_as_per_doj@example.com", date_of_joining=doj, relieving_date=relieving_date ) frappe.db.set_value("Employee", employee, "holiday_list", self.holiday_list) - attendance_date = add_days(first_day, 2) + attendance_date = add_days(date, 2) mark_attendance(employee, attendance_date, "Present") - month_name = get_month_name(first_day) + month_name = get_month_name(attendance_date) unmarked_days = get_unmarked_days(employee, month_name) unmarked_days = [getdate(date) for date in unmarked_days] From 37433aad48ce9a6b22fb3b20accef95eaea2c540 Mon Sep 17 00:00:00 2001 From: Mohammad Hussain Nagaria <34810212+NagariaHussain@users.noreply.github.com> Date: Wed, 1 Jun 2022 16:29:20 +0530 Subject: [PATCH 046/105] fix: Pluralize year text instead of optional bracket (#31210) Co-authored-by: Rucha Mahabal --- erpnext/hr/doctype/employee/employee_reminders.py | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/erpnext/hr/doctype/employee/employee_reminders.py b/erpnext/hr/doctype/employee/employee_reminders.py index 1829bc4f2fc..f09d7ff75a7 100644 --- a/erpnext/hr/doctype/employee/employee_reminders.py +++ b/erpnext/hr/doctype/employee/employee_reminders.py @@ -230,7 +230,7 @@ def get_work_anniversary_reminder_text_and_message(anniversary_persons): persons_name = anniversary_person # Number of years completed at the company completed_years = getdate().year - anniversary_persons[0]["date_of_joining"].year - anniversary_person += f" completed {completed_years} year(s)" + anniversary_person += f" completed {get_pluralized_years(completed_years)}" else: person_names_with_years = [] names = [] @@ -239,7 +239,7 @@ def get_work_anniversary_reminder_text_and_message(anniversary_persons): names.append(person_text) # Number of years completed at the company completed_years = getdate().year - person["date_of_joining"].year - person_text += f" completed {completed_years} year(s)" + person_text += f" completed {get_pluralized_years(completed_years)}" person_names_with_years.append(person_text) # converts ["Jim", "Rim", "Dim"] to Jim, Rim & Dim @@ -254,6 +254,12 @@ def get_work_anniversary_reminder_text_and_message(anniversary_persons): return reminder_text, message +def get_pluralized_years(years): + if years == 1: + return "1 year" + return f"{years} years" + + def send_work_anniversary_reminder(recipients, reminder_text, anniversary_persons, message): frappe.sendmail( recipients=recipients, From 3974fbbb6e0f4f41b292b870b80d6eab300f5e32 Mon Sep 17 00:00:00 2001 From: Ankush Menat Date: Wed, 1 Jun 2022 16:43:56 +0530 Subject: [PATCH 047/105] feat: UOM specific barcodes (#30988) --- erpnext/public/js/controllers/transaction.js | 3 ++- erpnext/public/js/utils/barcode_scanner.js | 23 ++++++++++++++----- .../doctype/item_barcode/item_barcode.json | 12 ++++++++-- erpnext/stock/utils.py | 2 +- 4 files changed, 30 insertions(+), 10 deletions(-) diff --git a/erpnext/public/js/controllers/transaction.js b/erpnext/public/js/controllers/transaction.js index edc4b06dca1..de93c82ef2c 100644 --- a/erpnext/public/js/controllers/transaction.js +++ b/erpnext/public/js/controllers/transaction.js @@ -423,7 +423,7 @@ erpnext.TransactionController = class TransactionController extends erpnext.taxe item.barcode = null; - if(item.item_code || item.barcode || item.serial_no) { + if(item.item_code || item.serial_no) { if(!this.validate_company_and_party()) { this.frm.fields_dict["items"].grid.grid_rows[item.idx - 1].remove(); } else { @@ -463,6 +463,7 @@ erpnext.TransactionController = class TransactionController extends erpnext.taxe stock_qty: item.stock_qty, conversion_factor: item.conversion_factor, weight_per_unit: item.weight_per_unit, + uom: item.uom, weight_uom: item.weight_uom, manufacturer: item.manufacturer, stock_uom: item.stock_uom, diff --git a/erpnext/public/js/utils/barcode_scanner.js b/erpnext/public/js/utils/barcode_scanner.js index 943db07705e..a6bff2c148d 100644 --- a/erpnext/public/js/utils/barcode_scanner.js +++ b/erpnext/public/js/utils/barcode_scanner.js @@ -9,6 +9,7 @@ erpnext.utils.BarcodeScanner = class BarcodeScanner { this.barcode_field = opts.barcode_field || "barcode"; this.serial_no_field = opts.serial_no_field || "serial_no"; this.batch_no_field = opts.batch_no_field || "batch_no"; + this.uom_field = opts.uom_field || "uom"; this.qty_field = opts.qty_field || "qty"; // field name on row which defines max quantity to be scanned e.g. picklist this.max_qty_field = opts.max_qty_field; @@ -26,6 +27,7 @@ erpnext.utils.BarcodeScanner = class BarcodeScanner { // bar_code: "123456", // present if barcode was scanned // batch_no: "LOT12", // present if batch was scanned // serial_no: "987XYZ", // present if serial no was scanned + // uom: "Kg", // present if barcode UOM is different from default // } this.scan_api = opts.scan_api || "erpnext.stock.utils.scan_barcode"; } @@ -67,9 +69,9 @@ erpnext.utils.BarcodeScanner = class BarcodeScanner { return new Promise(resolve => { let cur_grid = this.frm.fields_dict[this.items_table_name].grid; - const {item_code, barcode, batch_no, serial_no} = data; + const {item_code, barcode, batch_no, serial_no, uom} = data; - let row = this.get_row_to_modify_on_scan(item_code, batch_no); + let row = this.get_row_to_modify_on_scan(item_code, batch_no, uom); if (!row) { if (this.dont_allow_new_row) { @@ -90,10 +92,11 @@ erpnext.utils.BarcodeScanner = class BarcodeScanner { } frappe.run_serially([ - () => this.set_selector_trigger_flag(row, data), + () => this.set_selector_trigger_flag(data), () => this.set_item(row, item_code).then(qty => { this.show_scan_message(row.idx, row.item_code, qty); }), + () => this.set_barcode_uom(row, uom), () => this.set_serial_no(row, serial_no), () => this.set_batch_no(row, batch_no), () => this.set_barcode(row, barcode), @@ -106,7 +109,7 @@ erpnext.utils.BarcodeScanner = class BarcodeScanner { // batch and serial selector is reduandant when all info can be added by scan // this flag on item row is used by transaction.js to avoid triggering selector - set_selector_trigger_flag(row, data) { + set_selector_trigger_flag(data) { const {batch_no, serial_no, has_batch_no, has_serial_no} = data; const require_selecting_batch = has_batch_no && !batch_no; @@ -154,6 +157,12 @@ erpnext.utils.BarcodeScanner = class BarcodeScanner { } } + async set_barcode_uom(row, uom) { + if (uom && frappe.meta.has_field(row.doctype, this.uom_field)) { + await frappe.model.set_value(row.doctype, row.name, this.uom_field, uom); + } + } + async set_batch_no(row, batch_no) { if (batch_no && frappe.meta.has_field(row.doctype, this.batch_no_field)) { await frappe.model.set_value(row.doctype, row.name, this.batch_no_field, batch_no); @@ -184,7 +193,7 @@ erpnext.utils.BarcodeScanner = class BarcodeScanner { return is_duplicate; } - get_row_to_modify_on_scan(item_code, batch_no) { + get_row_to_modify_on_scan(item_code, batch_no, uom) { let cur_grid = this.frm.fields_dict[this.items_table_name].grid; // Check if batch is scanned and table has batch no field @@ -193,10 +202,12 @@ erpnext.utils.BarcodeScanner = class BarcodeScanner { const matching_row = (row) => { const item_match = row.item_code == item_code; - const batch_match = row.batch_no == batch_no; + const batch_match = row[this.batch_no_field] == batch_no; + const uom_match = !uom || row[this.uom_field] == uom; const qty_in_limit = flt(row[this.qty_field]) < flt(row[this.max_qty_field]); return item_match + && uom_match && (!is_batch_no_scan || batch_match) && (!check_max_qty || qty_in_limit) } diff --git a/erpnext/stock/doctype/item_barcode/item_barcode.json b/erpnext/stock/doctype/item_barcode/item_barcode.json index eef70c95d05..56832f32d30 100644 --- a/erpnext/stock/doctype/item_barcode/item_barcode.json +++ b/erpnext/stock/doctype/item_barcode/item_barcode.json @@ -6,7 +6,8 @@ "engine": "InnoDB", "field_order": [ "barcode", - "barcode_type" + "barcode_type", + "uom" ], "fields": [ { @@ -24,11 +25,18 @@ "in_list_view": 1, "label": "Barcode Type", "options": "\nEAN\nUPC-A" + }, + { + "fieldname": "uom", + "fieldtype": "Link", + "in_list_view": 1, + "label": "UOM", + "options": "UOM" } ], "istable": 1, "links": [], - "modified": "2022-04-01 05:54:27.314030", + "modified": "2022-06-01 06:24:33.969534", "modified_by": "Administrator", "module": "Stock", "name": "Item Barcode", diff --git a/erpnext/stock/utils.py b/erpnext/stock/utils.py index 2120304097b..6d8fdaa4042 100644 --- a/erpnext/stock/utils.py +++ b/erpnext/stock/utils.py @@ -558,7 +558,7 @@ def scan_barcode(search_value: str) -> Dict[str, Optional[str]]: barcode_data = frappe.db.get_value( "Item Barcode", {"barcode": search_value}, - ["barcode", "parent as item_code"], + ["barcode", "parent as item_code", "uom"], as_dict=True, ) if barcode_data: From 661e05e6937fe75d9acc331dc4b18be5b16ec638 Mon Sep 17 00:00:00 2001 From: Rucha Mahabal Date: Wed, 1 Jun 2022 17:28:42 +0530 Subject: [PATCH 048/105] fix(tests): account and company setups --- .../payroll_entry/test_payroll_entry.py | 55 ++++++------------- 1 file changed, 18 insertions(+), 37 deletions(-) diff --git a/erpnext/payroll/doctype/payroll_entry/test_payroll_entry.py b/erpnext/payroll/doctype/payroll_entry/test_payroll_entry.py index 47b9962912f..84f15753813 100644 --- a/erpnext/payroll/doctype/payroll_entry/test_payroll_entry.py +++ b/erpnext/payroll/doctype/payroll_entry/test_payroll_entry.py @@ -86,44 +86,32 @@ class TestPayrollEntry(FrappeTestCase): ) def test_multi_currency_payroll_entry(self): - company = erpnext.get_default_company() - employee = make_employee("test_muti_currency_employee@payroll.com", company=company) - for data in frappe.get_all("Salary Component", fields=["name"]): - if not frappe.db.get_value( - "Salary Component Account", {"parent": data.name, "company": company}, "name" - ): - get_salary_component_account(data.name) + company = frappe.get_doc("Company", "_Test Company") + employee = make_employee( + "test_muti_currency_employee@payroll.com", company=company.name, department="Accounts - _TC" + ) + salary_structure = "_Test Multi Currency Salary Structure" + setup_salary_structure(employee, company, "USD", salary_structure) - company_doc = frappe.get_doc("Company", company) - salary_structure = make_salary_structure( - "_Test Multi Currency Salary Structure", "Monthly", company=company, currency="USD" - ) - create_salary_structure_assignment( - employee, salary_structure.name, company=company, currency="USD" - ) - frappe.db.sql( - """delete from `tabSalary Slip` where employee=%s""", - (frappe.db.get_value("Employee", {"user_id": "test_muti_currency_employee@payroll.com"})), - ) - salary_slip = get_salary_slip( - "test_muti_currency_employee@payroll.com", "Monthly", "_Test Multi Currency Salary Structure" - ) dates = get_start_end_dates("Monthly", nowdate()) payroll_entry = make_payroll_entry( start_date=dates.start_date, end_date=dates.end_date, - payable_account=company_doc.default_payroll_payable_account, + payable_account=company.default_payroll_payable_account, currency="USD", exchange_rate=70, + company=company.name, + cost_center="Main - _TC", ) payroll_entry.make_payment_entry() - salary_slip.load_from_db() + salary_slip = frappe.db.get_value("Salary Slip", {"payroll_entry": payroll_entry.name}, "name") + salary_slip = frappe.get_doc("Salary Slip", salary_slip) + payroll_entry.reload() payroll_je = salary_slip.journal_entry if payroll_je: payroll_je_doc = frappe.get_doc("Journal Entry", payroll_je) - self.assertEqual(salary_slip.base_gross_pay, payroll_je_doc.total_debit) self.assertEqual(salary_slip.base_gross_pay, payroll_je_doc.total_credit) @@ -136,7 +124,6 @@ class TestPayrollEntry(FrappeTestCase): (payroll_entry.name), as_dict=1, ) - self.assertEqual(salary_slip.base_net_pay, payment_entry[0].total_debit) self.assertEqual(salary_slip.base_net_pay, payment_entry[0].total_credit) @@ -306,7 +293,7 @@ class TestPayrollEntry(FrappeTestCase): def test_salary_slip_operation_queueing(self): company = "_Test Company" company_doc = frappe.get_doc("Company", company) - employee = frappe.db.get_value("Employee", {"company": company}) + employee = make_employee("test_employee@payroll.com", company=company) setup_salary_structure(employee, company_doc) # enqueue salary slip creation via payroll entry @@ -318,6 +305,7 @@ class TestPayrollEntry(FrappeTestCase): payable_account=company_doc.default_payroll_payable_account, currency=company_doc.default_currency, company=company_doc.name, + cost_center="Main - _TC", ) frappe.flags.enqueue_payroll_entry = True payroll_entry.create_salary_slips() @@ -329,7 +317,7 @@ class TestPayrollEntry(FrappeTestCase): def test_salary_slip_operation_failure(self): company = "_Test Company" company_doc = frappe.get_doc("Company", company) - employee = frappe.db.get_value("Employee", {"company": company}) + employee = make_employee("test_employee@payroll.com", company=company) salary_structure = make_salary_structure( "_Test Salary Structure", @@ -353,6 +341,7 @@ class TestPayrollEntry(FrappeTestCase): payable_account=company_doc.default_payroll_payable_account, currency=company_doc.default_currency, company=company_doc.name, + cost_center="Main - _TC", ) payroll_entry.create_salary_slips() payroll_entry.submit_salary_slips() @@ -375,7 +364,7 @@ class TestPayrollEntry(FrappeTestCase): def test_payroll_entry_status(self): company = "_Test Company" company_doc = frappe.get_doc("Company", company) - employee = frappe.db.get_value("Employee", {"company": company}) + employee = make_employee("test_employee@payroll.com", company=company) setup_salary_structure(employee, company_doc) @@ -386,6 +375,7 @@ class TestPayrollEntry(FrappeTestCase): payable_account=company_doc.default_payroll_payable_account, currency=company_doc.default_currency, company=company_doc.name, + cost_center="Main - _TC", ) payroll_entry.submit() self.assertEqual(payroll_entry.status, "Submitted") @@ -466,15 +456,6 @@ def make_holiday(holiday_list_name): return holiday_list_name -def get_salary_slip(user, period, salary_structure): - salary_slip = make_employee_salary_slip(user, period, salary_structure) - salary_slip.exchange_rate = 70 - salary_slip.calculate_net_pay() - salary_slip.db_update() - - return salary_slip - - def setup_salary_structure(employee, company_doc, currency=None, salary_structure=None): for data in frappe.get_all("Salary Component", pluck="name"): if not frappe.db.get_value( From 48bde2de2a2280d788f7688f9cb523d76042ffcf Mon Sep 17 00:00:00 2001 From: Sun Howwrongbum Date: Wed, 1 Jun 2022 20:20:16 +0530 Subject: [PATCH 049/105] fix: Trial Balance failing to ignore Finance Book --- .../accounts/report/trial_balance/trial_balance.py | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/erpnext/accounts/report/trial_balance/trial_balance.py b/erpnext/accounts/report/trial_balance/trial_balance.py index e5a4ed2f347..af447df52a8 100644 --- a/erpnext/accounts/report/trial_balance/trial_balance.py +++ b/erpnext/accounts/report/trial_balance/trial_balance.py @@ -160,14 +160,10 @@ def get_rootwise_opening_balances(filters, report_type): if filters.project: additional_conditions += " and project = %(project)s" - if filters.finance_book: - fb_conditions = " AND finance_book = %(finance_book)s" - if filters.include_default_book_entries: - fb_conditions = ( - " AND (finance_book in (%(finance_book)s, %(company_fb)s, '') OR finance_book IS NULL)" - ) - - additional_conditions += fb_conditions + if filters.get("include_default_book_entries"): + additional_conditions += "AND (finance_book in (%(finance_book)s, %(company_fb)s, '') OR finance_book IS NULL)" + else: + additional_conditions += "AND (finance_book in (%(finance_book)s, '') OR finance_book IS NULL)" accounting_dimensions = get_accounting_dimensions(as_list=False) From 77dcdff0db39f3dfe06a41733039b52bbf8c4caa Mon Sep 17 00:00:00 2001 From: Ankush Menat Date: Wed, 1 Jun 2022 22:01:07 +0530 Subject: [PATCH 050/105] fix: unusable SO after clearing taxes (#31215) --- erpnext/controllers/accounts_controller.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/erpnext/controllers/accounts_controller.py b/erpnext/controllers/accounts_controller.py index 0dd6a5c3334..bebfa6c76f5 100644 --- a/erpnext/controllers/accounts_controller.py +++ b/erpnext/controllers/accounts_controller.py @@ -1866,7 +1866,7 @@ def get_default_taxes_and_charges(master_doctype, tax_template=None, company=Non def get_taxes_and_charges(master_doctype, master_name): if not master_name: return - from frappe.model import default_fields + from frappe.model import child_table_fields, default_fields tax_master = frappe.get_doc(master_doctype, master_name) @@ -1874,7 +1874,7 @@ def get_taxes_and_charges(master_doctype, master_name): for i, tax in enumerate(tax_master.get("taxes")): tax = tax.as_dict() - for fieldname in default_fields: + for fieldname in default_fields + child_table_fields: if fieldname in tax: del tax[fieldname] From c7efa3b44d033c5214fbf6453954b7c5de25e037 Mon Sep 17 00:00:00 2001 From: Ankush Menat Date: Thu, 2 Jun 2022 12:27:11 +0530 Subject: [PATCH 051/105] ci: stale apt cache (#31217) --- .github/helper/install.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/helper/install.sh b/.github/helper/install.sh index 69749c93aff..f0f83b061b9 100644 --- a/.github/helper/install.sh +++ b/.github/helper/install.sh @@ -11,7 +11,7 @@ fi cd ~ || exit -sudo apt-get install redis-server libcups2-dev +sudo apt update && sudo apt install redis-server libcups2-dev pip install frappe-bench From f0ac394d6e5d284a476eba62a6fcd6aaa01dd00d Mon Sep 17 00:00:00 2001 From: Deepesh Garg Date: Thu, 2 Jun 2022 12:59:55 +0530 Subject: [PATCH 052/105] fix(India): GSTIN filter in GSTR-1 report --- erpnext/regional/report/gstr_1/gstr_1.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/erpnext/regional/report/gstr_1/gstr_1.py b/erpnext/regional/report/gstr_1/gstr_1.py index 0bdbe56de60..6cbc12c7a12 100644 --- a/erpnext/regional/report/gstr_1/gstr_1.py +++ b/erpnext/regional/report/gstr_1/gstr_1.py @@ -1155,8 +1155,11 @@ def get_company_gstins(company): .inner_join(links) .on(address.name == links.parent) .select(address.gstin) + .distinct() .where(links.link_doctype == "Company") .where(links.link_name == company) + .where(address.gstin.isnotnull()) + .where(address.gstin != "") .run(as_dict=1) ) From 62857e3e080b3888f40a09112be63238974dd175 Mon Sep 17 00:00:00 2001 From: marination Date: Thu, 2 Jun 2022 13:35:30 +0530 Subject: [PATCH 053/105] feat: Track progress in Log Batch/Job wise - This was done due to stale reads while the background jobs tried updating status of the log - Added a table where all bom jobs within log will be tracked with what level they are processing - Cron job will check if table jobs are all processed every 5 mins - If yes, it will prepare parents and call `process_boms_cost_level_wise` to start next level - If pending jobs, do nothing - Current BOM Level is being tracked that helps adding rows to the table - Individual bom cost jobs (that are queued) will process and update boms > will update BOM Update Batch table row with list of updated BOMs --- .../doctype/bom_update_batch/__init__.py | 0 .../bom_update_batch/bom_update_batch.json | 45 ++++++++ .../bom_update_batch/bom_update_batch.py | 9 ++ .../bom_update_log/bom_update_log.json | 21 ++-- .../doctype/bom_update_log/bom_update_log.py | 106 +++++++++++++----- .../bom_update_log/bom_updation_utils.py | 55 +-------- 6 files changed, 154 insertions(+), 82 deletions(-) create mode 100644 erpnext/manufacturing/doctype/bom_update_batch/__init__.py create mode 100644 erpnext/manufacturing/doctype/bom_update_batch/bom_update_batch.json create mode 100644 erpnext/manufacturing/doctype/bom_update_batch/bom_update_batch.py diff --git a/erpnext/manufacturing/doctype/bom_update_batch/__init__.py b/erpnext/manufacturing/doctype/bom_update_batch/__init__.py new file mode 100644 index 00000000000..e69de29bb2d diff --git a/erpnext/manufacturing/doctype/bom_update_batch/bom_update_batch.json b/erpnext/manufacturing/doctype/bom_update_batch/bom_update_batch.json new file mode 100644 index 00000000000..9938454ce4e --- /dev/null +++ b/erpnext/manufacturing/doctype/bom_update_batch/bom_update_batch.json @@ -0,0 +1,45 @@ +{ + "actions": [], + "autoname": "autoincrement", + "creation": "2022-05-31 17:34:39.825537", + "doctype": "DocType", + "engine": "InnoDB", + "field_order": [ + "level", + "batch_no", + "boms_updated" + ], + "fields": [ + { + "fieldname": "level", + "fieldtype": "Int", + "in_list_view": 1, + "label": "Level" + }, + { + "fieldname": "batch_no", + "fieldtype": "Int", + "in_list_view": 1, + "label": "Batch No." + }, + { + "fieldname": "boms_updated", + "fieldtype": "Long Text", + "in_list_view": 1, + "label": "BOMs Updated" + } + ], + "index_web_pages_for_search": 1, + "istable": 1, + "links": [], + "modified": "2022-05-31 23:36:13.628391", + "modified_by": "Administrator", + "module": "Manufacturing", + "name": "BOM Update Batch", + "naming_rule": "Autoincrement", + "owner": "Administrator", + "permissions": [], + "sort_field": "modified", + "sort_order": "DESC", + "states": [] +} \ No newline at end of file diff --git a/erpnext/manufacturing/doctype/bom_update_batch/bom_update_batch.py b/erpnext/manufacturing/doctype/bom_update_batch/bom_update_batch.py new file mode 100644 index 00000000000..f952e435e67 --- /dev/null +++ b/erpnext/manufacturing/doctype/bom_update_batch/bom_update_batch.py @@ -0,0 +1,9 @@ +# Copyright (c) 2022, Frappe Technologies Pvt. Ltd. and contributors +# For license information, please see license.txt + +# import frappe +from frappe.model.document import Document + + +class BOMUpdateBatch(Document): + pass diff --git a/erpnext/manufacturing/doctype/bom_update_log/bom_update_log.json b/erpnext/manufacturing/doctype/bom_update_log/bom_update_log.json index bea3cf03733..b1c24ab9954 100644 --- a/erpnext/manufacturing/doctype/bom_update_log/bom_update_log.json +++ b/erpnext/manufacturing/doctype/bom_update_log/bom_update_log.json @@ -14,9 +14,10 @@ "status", "error_log", "progress_section", - "current_boms", + "current_level", "parent_boms", "processed_boms", + "bom_batches", "amended_from" ], "fields": [ @@ -70,15 +71,11 @@ }, { "collapsible": 1, + "depends_on": "eval: doc.update_type == \"Update Cost\"", "fieldname": "progress_section", "fieldtype": "Section Break", "label": "Progress" }, - { - "fieldname": "current_boms", - "fieldtype": "Long Text", - "label": "Current BOMs" - }, { "description": "Immediate parent BOMs", "fieldname": "parent_boms", @@ -89,13 +86,23 @@ "fieldname": "processed_boms", "fieldtype": "Long Text", "label": "Processed BOMs" + }, + { + "fieldname": "bom_batches", + "fieldtype": "Table", + "options": "BOM Update Batch" + }, + { + "fieldname": "current_level", + "fieldtype": "Int", + "label": "Current Level" } ], "in_create": 1, "index_web_pages_for_search": 1, "is_submittable": 1, "links": [], - "modified": "2022-05-27 17:03:34.712010", + "modified": "2022-05-31 20:20:06.370786", "modified_by": "Administrator", "module": "Manufacturing", "name": "BOM Update Log", diff --git a/erpnext/manufacturing/doctype/bom_update_log/bom_update_log.py b/erpnext/manufacturing/doctype/bom_update_log/bom_update_log.py index f61f863c10a..bfae76c2b2e 100644 --- a/erpnext/manufacturing/doctype/bom_update_log/bom_update_log.py +++ b/erpnext/manufacturing/doctype/bom_update_log/bom_update_log.py @@ -1,15 +1,16 @@ # Copyright (c) 2022, Frappe Technologies Pvt. Ltd. and contributors # For license information, please see license.txt import json -from typing import Dict, Optional +from typing import Any, Dict, List, Optional, Tuple import frappe from frappe import _ from frappe.model.document import Document -from frappe.utils import cstr +from frappe.utils import cint, cstr from erpnext.manufacturing.doctype.bom_update_log.bom_updation_utils import ( get_leaf_boms, + get_next_higher_level_boms, handle_exception, replace_bom, set_values_in_log, @@ -111,55 +112,110 @@ def process_boms_cost_level_wise(update_doc: "BOMUpdateLog") -> None: if update_doc.status == "Queued": # First level yet to process. On Submit. - current_boms = {bom: False for bom in get_leaf_boms()} + current_level = 0 + current_boms = get_leaf_boms() values = { - "current_boms": json.dumps(current_boms), "parent_boms": "[]", "processed_boms": json.dumps({}), "status": "In Progress", + "current_level": current_level, } else: - # status is Paused, resume. via Cron Job. - current_boms, parent_boms = json.loads(update_doc.current_boms), json.loads( - update_doc.parent_boms - ) - if not current_boms: - # Process the next level BOMs. Stage parents as current BOMs. - current_boms = {bom: False for bom in parent_boms} - values = { - "current_boms": json.dumps(current_boms), - "parent_boms": "[]", - "status": "In Progress", - } + # Resume next level. via Cron Job. + current_level = cint(update_doc.current_level) + 1 + parent_boms = json.loads(update_doc.parent_boms) + + # Process the next level BOMs. Stage parents as current BOMs. + current_boms = parent_boms.copy() + values = {"parent_boms": "[]", "current_level": current_level} set_values_in_log(update_doc.name, values, commit=True) - queue_bom_cost_jobs(current_boms, update_doc) + queue_bom_cost_jobs(current_boms, update_doc, current_level) -def queue_bom_cost_jobs(current_boms: Dict, update_doc: "BOMUpdateLog") -> None: +def queue_bom_cost_jobs( + current_boms_list: List, update_doc: "BOMUpdateLog", current_level: int +) -> None: "Queue batches of 20k BOMs of the same level to process parallelly" - current_boms_list = [bom for bom in current_boms] + batch_no = 0 while current_boms_list: + batch_no += 1 batch_size = 20_000 boms_to_process = current_boms_list[:batch_size] # slice out batch of 20k BOMs # update list to exclude 20K (queued) BOMs current_boms_list = current_boms_list[batch_size:] if len(current_boms_list) > batch_size else [] + + batch_row = update_doc.append("bom_batches", {"level": current_level, "batch_no": batch_no}) + batch_row.db_insert() + frappe.enqueue( method="erpnext.manufacturing.doctype.bom_update_log.bom_updation_utils.update_cost_in_level", doc=update_doc, bom_list=boms_to_process, + batch_name=batch_row.name, timeout=40000, ) def resume_bom_cost_update_jobs(): - "Called every 10 minutes via Cron job." - paused_jobs = frappe.db.get_all("BOM Update Log", {"status": "Paused"}) - if not paused_jobs: + """ + 1. Checks for In Progress BOM Update Log. + 2. Checks if this job has completed the _current level_. + 3. If current level is complete, get parent BOMs and start next level. + 4. If no parents, mark as Complete. + 5. If current level is WIP, skip the Log. + + Called every 5 minutes via Cron job. + """ + + in_progress_logs = frappe.db.get_all( + "BOM Update Log", + {"update_type": "Update Cost", "status": "In Progress"}, + ["name", "processed_boms", "current_level"], + ) + if not in_progress_logs: return - for job in paused_jobs: - # resume from next level - process_boms_cost_level_wise(update_doc=frappe.get_doc("BOM Update Log", job.name)) + for log in in_progress_logs: + # check if all log batches of current level are processed + bom_batches = frappe.db.get_all( + "BOM Update Batch", {"parent": log.name, "level": log.current_level}, ["name", "boms_updated"] + ) + incomplete_level = any(not row.get("boms_updated") for row in bom_batches) + if not bom_batches or incomplete_level: + continue + + # Prep parent BOMs & updated processed BOMs for next level + current_boms, processed_boms = get_processed_current_boms(log, bom_batches) + parent_boms = get_next_higher_level_boms(child_boms=current_boms, processed_boms=processed_boms) + + set_values_in_log( + log.name, + values={ + "processed_boms": json.dumps(processed_boms), + "parent_boms": json.dumps(parent_boms), + "status": "Completed" if not parent_boms else "In Progress", + }, + commit=True, + ) + + if parent_boms: # there is a next level to process + process_boms_cost_level_wise(update_doc=frappe.get_doc("BOM Update Log", log.name)) + + +def get_processed_current_boms( + log: Dict[str, Any], bom_batches: Dict[str, Any] +) -> Tuple[List[str], Dict[str, Any]]: + "Aggregate all BOMs from BOM Update Batch rows into 'processed_boms' field and into current boms list." + processed_boms = json.loads(log.processed_boms) if log.processed_boms else {} + current_boms = [] + + for row in bom_batches: + boms_updated = json.loads(row.boms_updated) + current_boms.extend(boms_updated) + boms_updated_dict = {bom: True for bom in boms_updated} + processed_boms.update(boms_updated_dict) + + return current_boms, processed_boms diff --git a/erpnext/manufacturing/doctype/bom_update_log/bom_updation_utils.py b/erpnext/manufacturing/doctype/bom_update_log/bom_updation_utils.py index 790a79b3333..2d6429b0504 100644 --- a/erpnext/manufacturing/doctype/bom_update_log/bom_updation_utils.py +++ b/erpnext/manufacturing/doctype/bom_update_log/bom_updation_utils.py @@ -38,7 +38,7 @@ def replace_bom(boms: Dict) -> None: bom_obj.save_version() -def update_cost_in_level(doc: "BOMUpdateLog", bom_list: List[str]) -> None: +def update_cost_in_level(doc: "BOMUpdateLog", bom_list: List[str], batch_name: int) -> None: "Updates Cost for BOMs within a given level. Runs via background jobs." try: @@ -47,19 +47,9 @@ def update_cost_in_level(doc: "BOMUpdateLog", bom_list: List[str]) -> None: return frappe.db.auto_commit_on_many_writes = 1 - # main updation logic - job_data = update_cost_in_boms(bom_list=bom_list, docname=doc.name) - set_values_in_log( - doc.name, - values={ - "current_boms": json.dumps(job_data.get("current_boms")), - "processed_boms": json.dumps(job_data.get("processed_boms")), - }, - commit=True, - ) - - process_if_level_is_complete(doc.name, job_data["current_boms"], job_data["processed_boms"]) + update_cost_in_boms(bom_list=bom_list) # main updation logic + frappe.db.set_value("BOM Update Batch", batch_name, "boms_updated", json.dumps(bom_list)) except Exception: handle_exception(doc) finally: @@ -112,48 +102,13 @@ def get_bom_unit_cost(bom_name: str) -> float: return frappe.utils.flt(new_bom_unitcost[0][0]) -def update_cost_in_boms(bom_list: List[str], docname: str) -> Dict[str, Dict]: +def update_cost_in_boms(bom_list: List[str]) -> None: "Updates cost in given BOMs. Returns current and total updated BOMs." - updated_boms = {} # current boms that have been updated - for bom in bom_list: bom_doc = frappe.get_cached_doc("BOM", bom) bom_doc.calculate_cost(save_updates=True, update_hour_rate=True) bom_doc.db_update() - updated_boms[bom] = True - - # Update processed BOMs in Log - log_data = frappe.db.get_values( - "BOM Update Log", docname, ["current_boms", "processed_boms"], as_dict=True - )[0] - - for field in ("current_boms", "processed_boms"): - log_data[field] = json.loads(log_data.get(field)) - log_data[field].update(updated_boms) - - return log_data - - -def process_if_level_is_complete( - docname: str, current_boms: Dict[str, bool], processed_boms: Dict[str, bool] -) -> None: - "Prepare and set higher level BOMs/dependants in Log if current level is complete." - - processing_complete = all(current_boms.get(bom) for bom in current_boms) - if not processing_complete: - return - - parent_boms = get_next_higher_level_boms(child_boms=current_boms, processed_boms=processed_boms) - set_values_in_log( - docname, - values={ - "current_boms": json.dumps({}), - "parent_boms": json.dumps(parent_boms), - "status": "Completed" if not parent_boms else "Paused", - }, - commit=True, - ) def get_next_higher_level_boms( @@ -244,7 +199,7 @@ def set_values_in_log(log_name: str, values: Dict[str, Any], commit: bool = Fals query.run() if commit: - frappe.db.commit() + frappe.db.commit() # nosemgrep def handle_exception(doc: "BOMUpdateLog") -> None: From 3f376cc3a5b7f6f28957e032976d31287f7f88cb Mon Sep 17 00:00:00 2001 From: Deepesh Garg Date: Thu, 2 Jun 2022 13:57:54 +0530 Subject: [PATCH 054/105] fix: Parent dimension filters in orders --- erpnext/accounts/doctype/purchase_invoice/purchase_invoice.js | 2 -- erpnext/accounts/doctype/sales_invoice/sales_invoice.js | 1 - erpnext/buying/doctype/purchase_order/purchase_order.js | 2 -- erpnext/public/js/controllers/buying.js | 1 + erpnext/selling/sales_common.js | 1 + erpnext/stock/doctype/delivery_note/delivery_note.js | 2 -- erpnext/stock/doctype/purchase_receipt/purchase_receipt.js | 2 -- 7 files changed, 2 insertions(+), 9 deletions(-) diff --git a/erpnext/accounts/doctype/purchase_invoice/purchase_invoice.js b/erpnext/accounts/doctype/purchase_invoice/purchase_invoice.js index 42917f811db..7e3597e491e 100644 --- a/erpnext/accounts/doctype/purchase_invoice/purchase_invoice.js +++ b/erpnext/accounts/doctype/purchase_invoice/purchase_invoice.js @@ -45,8 +45,6 @@ erpnext.accounts.PurchaseInvoice = class PurchaseInvoice extends erpnext.buying. if (this.frm.doc.supplier && this.frm.doc.__islocal) { this.frm.trigger('supplier'); } - - erpnext.accounts.dimensions.setup_dimension_filters(this.frm, this.frm.doctype); } refresh(doc) { diff --git a/erpnext/accounts/doctype/sales_invoice/sales_invoice.js b/erpnext/accounts/doctype/sales_invoice/sales_invoice.js index 9dde85fe12e..aefa9a59ddf 100644 --- a/erpnext/accounts/doctype/sales_invoice/sales_invoice.js +++ b/erpnext/accounts/doctype/sales_invoice/sales_invoice.js @@ -52,7 +52,6 @@ erpnext.accounts.SalesInvoiceController = class SalesInvoiceController extends e me.frm.refresh_fields(); } erpnext.queries.setup_warehouse_query(this.frm); - erpnext.accounts.dimensions.setup_dimension_filters(this.frm, this.frm.doctype); } refresh(doc, dt, dn) { diff --git a/erpnext/buying/doctype/purchase_order/purchase_order.js b/erpnext/buying/doctype/purchase_order/purchase_order.js index c9e67987c6b..da45610eaff 100644 --- a/erpnext/buying/doctype/purchase_order/purchase_order.js +++ b/erpnext/buying/doctype/purchase_order/purchase_order.js @@ -43,8 +43,6 @@ frappe.ui.form.on("Purchase Order", { erpnext.queries.setup_queries(frm, "Warehouse", function() { return erpnext.queries.warehouse(frm.doc); }); - - erpnext.accounts.dimensions.setup_dimension_filters(frm, frm.doctype); }, apply_tds: function(frm) { diff --git a/erpnext/public/js/controllers/buying.js b/erpnext/public/js/controllers/buying.js index 58eb8916002..a5b7699040b 100644 --- a/erpnext/public/js/controllers/buying.js +++ b/erpnext/public/js/controllers/buying.js @@ -74,6 +74,7 @@ erpnext.buying.BuyingController = class BuyingController extends erpnext.Transac me.frm.set_query('supplier_address', erpnext.queries.address_query); me.frm.set_query('billing_address', erpnext.queries.company_address_query); + erpnext.accounts.dimensions.setup_dimension_filters(me.frm, me.frm.doctype); if(this.frm.fields_dict.supplier) { this.frm.set_query("supplier", function() { diff --git a/erpnext/selling/sales_common.js b/erpnext/selling/sales_common.js index 0954de4e665..6cb53c3bbe3 100644 --- a/erpnext/selling/sales_common.js +++ b/erpnext/selling/sales_common.js @@ -43,6 +43,7 @@ erpnext.selling.SellingController = class SellingController extends erpnext.Tran me.frm.set_query('shipping_address_name', erpnext.queries.address_query); me.frm.set_query('dispatch_address_name', erpnext.queries.dispatch_address_query); + erpnext.accounts.dimensions.setup_dimension_filters(me.frm, me.frm.doctype); if(this.frm.fields_dict.selling_price_list) { this.frm.set_query("selling_price_list", function() { diff --git a/erpnext/stock/doctype/delivery_note/delivery_note.js b/erpnext/stock/doctype/delivery_note/delivery_note.js index 706ca365988..ea3cf1948b3 100644 --- a/erpnext/stock/doctype/delivery_note/delivery_note.js +++ b/erpnext/stock/doctype/delivery_note/delivery_note.js @@ -77,8 +77,6 @@ frappe.ui.form.on("Delivery Note", { } }); - erpnext.accounts.dimensions.setup_dimension_filters(frm, frm.doctype); - frm.set_df_property('packed_items', 'cannot_add_rows', true); frm.set_df_property('packed_items', 'cannot_delete_rows', true); }, diff --git a/erpnext/stock/doctype/purchase_receipt/purchase_receipt.js b/erpnext/stock/doctype/purchase_receipt/purchase_receipt.js index 51ec598f726..754404b068b 100644 --- a/erpnext/stock/doctype/purchase_receipt/purchase_receipt.js +++ b/erpnext/stock/doctype/purchase_receipt/purchase_receipt.js @@ -46,8 +46,6 @@ frappe.ui.form.on("Purchase Receipt", { erpnext.queries.setup_queries(frm, "Warehouse", function() { return erpnext.queries.warehouse(frm.doc); }); - - erpnext.accounts.dimensions.setup_dimension_filters(frm, frm.doctype); }, refresh: function(frm) { From d641f260352d5aff7ec77def19bb318dca4a5054 Mon Sep 17 00:00:00 2001 From: Rucha Mahabal Date: Thu, 2 Jun 2022 15:08:49 +0530 Subject: [PATCH 055/105] fix: error handling and messages - remove savepoints since submission should stop if any error occurs - refactor variable naming and msgprints - test Salary Slip creation failure - fix(test): explicitly commit after payroll entry creation so that the first salary slip creation failure does not rollback the Payroll Entry insert --- .../doctype/payroll_entry/payroll_entry.py | 38 +++++++++---------- .../payroll_entry/test_payroll_entry.py | 28 ++++++++++---- 2 files changed, 38 insertions(+), 28 deletions(-) diff --git a/erpnext/payroll/doctype/payroll_entry/payroll_entry.py b/erpnext/payroll/doctype/payroll_entry/payroll_entry.py index edcada14518..620fcadceb2 100644 --- a/erpnext/payroll/doctype/payroll_entry/payroll_entry.py +++ b/erpnext/payroll/doctype/payroll_entry/payroll_entry.py @@ -54,7 +54,7 @@ class PayrollEntry(Document): if self.validate_employee_attendance(): frappe.throw(_("Cannot Submit, Employees left to mark attendance")) - def set_status(self, status=None, update=True): + def set_status(self, status=None, update=False): if not status: status = {0: "Draft", 1: "Submitted", 2: "Cancelled"}[self.docstatus or 0] @@ -850,7 +850,6 @@ def log_payroll_failure(process, payroll_entry, error): def create_salary_slips_for_employees(employees, args, publish_progress=True): try: - frappe.db.savepoint("salary_slip_creation") payroll_entry = frappe.get_doc("Payroll Entry", args.payroll_entry) salary_slips_exist_for = get_existing_salary_slips(employees, args) count = 0 @@ -879,7 +878,7 @@ def create_salary_slips_for_employees(employees, args, publish_progress=True): ) except Exception as e: - frappe.db.rollback(save_point="salary_slip_creation") + frappe.db.rollback() log_payroll_failure("creation", payroll_entry, e) finally: @@ -887,24 +886,25 @@ def create_salary_slips_for_employees(employees, args, publish_progress=True): frappe.publish_realtime("completed_salary_slip_creation") -def show_payroll_submission_status(submitted, not_submitted, salary_slip): - if not submitted and not not_submitted: +def show_payroll_submission_status(submitted, unsubmitted, payroll_entry): + if not submitted and not unsubmitted: frappe.msgprint( _( "No salary slip found to submit for the above selected criteria OR salary slip already submitted" ) ) - return - - if submitted: + elif submitted and not unsubmitted: frappe.msgprint( - _("Salary Slip submitted for period from {0} to {1}").format( - salary_slip.start_date, salary_slip.end_date + _("Salary Slips submitted for period from {0} to {1}").format( + payroll_entry.start_date, payroll_entry.end_date + ) + ) + elif unsubmitted: + frappe.msgprint( + _("Could not submit some Salary Slips: {}").format( + ", ".join(get_link_to_form("Salary Slip", entry) for entry in unsubmitted) ) ) - - if not_submitted: - frappe.msgprint(_("Could not submit some Salary Slips")) def get_existing_salary_slips(employees, args): @@ -922,23 +922,21 @@ def get_existing_salary_slips(employees, args): def submit_salary_slips_for_employees(payroll_entry, salary_slips, publish_progress=True): try: - frappe.db.savepoint("salary_slip_submission") - submitted = [] - not_submitted = [] + unsubmitted = [] frappe.flags.via_payroll_entry = True count = 0 for entry in salary_slips: salary_slip = frappe.get_doc("Salary Slip", entry[0]) if salary_slip.net_pay < 0: - not_submitted.append(entry[0]) + unsubmitted.append(entry[0]) else: try: salary_slip.submit() submitted.append(salary_slip) except frappe.ValidationError: - not_submitted.append(entry[0]) + unsubmitted.append(entry[0]) count += 1 if publish_progress: @@ -949,10 +947,10 @@ def submit_salary_slips_for_employees(payroll_entry, salary_slips, publish_progr payroll_entry.email_salary_slip(submitted) payroll_entry.db_set({"salary_slips_submitted": 1, "status": "Submitted", "error_message": ""}) - show_payroll_submission_status(submitted, not_submitted, salary_slip) + show_payroll_submission_status(submitted, unsubmitted, payroll_entry) except Exception as e: - frappe.db.rollback(save_point="salary_slip_submission") + frappe.db.rollback() log_payroll_failure("submission", payroll_entry, e) finally: diff --git a/erpnext/payroll/doctype/payroll_entry/test_payroll_entry.py b/erpnext/payroll/doctype/payroll_entry/test_payroll_entry.py index 84f15753813..0363a0c3de6 100644 --- a/erpnext/payroll/doctype/payroll_entry/test_payroll_entry.py +++ b/erpnext/payroll/doctype/payroll_entry/test_payroll_entry.py @@ -299,7 +299,7 @@ class TestPayrollEntry(FrappeTestCase): # enqueue salary slip creation via payroll entry # Payroll Entry status should change to Queued dates = get_start_end_dates("Monthly", nowdate()) - payroll_entry = get_payroll_entry_data( + payroll_entry = get_payroll_entry( start_date=dates.start_date, end_date=dates.end_date, payable_account=company_doc.default_payroll_payable_account, @@ -308,7 +308,7 @@ class TestPayrollEntry(FrappeTestCase): cost_center="Main - _TC", ) frappe.flags.enqueue_payroll_entry = True - payroll_entry.create_salary_slips() + payroll_entry.submit() payroll_entry.reload() self.assertEqual(payroll_entry.status, "Queued") @@ -335,7 +335,7 @@ class TestPayrollEntry(FrappeTestCase): # salary slip submission via payroll entry # Payroll Entry status should change to Failed because of the missing account setup dates = get_start_end_dates("Monthly", nowdate()) - payroll_entry = get_payroll_entry_data( + payroll_entry = get_payroll_entry( start_date=dates.start_date, end_date=dates.end_date, payable_account=company_doc.default_payroll_payable_account, @@ -343,7 +343,16 @@ class TestPayrollEntry(FrappeTestCase): company=company_doc.name, cost_center="Main - _TC", ) - payroll_entry.create_salary_slips() + + # set employee as Inactive to check creation failure + frappe.db.set_value("Employee", employee, "status", "Inactive") + payroll_entry.submit() + payroll_entry.reload() + self.assertEqual(payroll_entry.status, "Failed") + self.assertIsNotNone(payroll_entry.error_message) + + frappe.db.set_value("Employee", employee, "status", "Active") + payroll_entry.submit() payroll_entry.submit_salary_slips() payroll_entry.reload() @@ -369,7 +378,7 @@ class TestPayrollEntry(FrappeTestCase): setup_salary_structure(employee, company_doc) dates = get_start_end_dates("Monthly", nowdate()) - payroll_entry = get_payroll_entry_data( + payroll_entry = get_payroll_entry( start_date=dates.start_date, end_date=dates.end_date, payable_account=company_doc.default_payroll_payable_account, @@ -384,7 +393,7 @@ class TestPayrollEntry(FrappeTestCase): self.assertEqual(payroll_entry.status, "Cancelled") -def get_payroll_entry_data(**args): +def get_payroll_entry(**args): args = frappe._dict(args) payroll_entry = frappe.new_doc("Payroll Entry") @@ -407,13 +416,16 @@ def get_payroll_entry_data(**args): payroll_entry.payment_account = args.payment_account payroll_entry.fill_employee_details() - payroll_entry.save() + payroll_entry.insert() + + # Commit so that the first salary slip creation failure does not rollback the Payroll Entry insert. + frappe.db.commit() # nosemgrep return payroll_entry def make_payroll_entry(**args): - payroll_entry = get_payroll_entry_data(**args) + payroll_entry = get_payroll_entry(**args) payroll_entry.submit() payroll_entry.submit_salary_slips() if payroll_entry.get_sal_slip_list(ss_status=1): From 1db4e623ab6d728ee71663a33a22023961f03ea0 Mon Sep 17 00:00:00 2001 From: Rucha Mahabal Date: Thu, 2 Jun 2022 17:26:08 +0530 Subject: [PATCH 056/105] fix: payroll operations button visibility --- .../doctype/payroll_entry/payroll_entry.js | 39 ++++++++++++------- 1 file changed, 26 insertions(+), 13 deletions(-) diff --git a/erpnext/payroll/doctype/payroll_entry/payroll_entry.js b/erpnext/payroll/doctype/payroll_entry/payroll_entry.js index a33f7665bda..b06f3502e26 100644 --- a/erpnext/payroll/doctype/payroll_entry/payroll_entry.js +++ b/erpnext/payroll/doctype/payroll_entry/payroll_entry.js @@ -40,27 +40,40 @@ frappe.ui.form.on('Payroll Entry', { }, refresh: function (frm) { - if (frm.doc.docstatus == 0) { - if (!frm.is_new()) { + if (frm.doc.docstatus === 0 && !frm.is_new()) { + frm.page.clear_primary_action(); + frm.add_custom_button(__("Get Employees"), + function () { + frm.events.get_employee_details(frm); + } + ).toggleClass("btn-primary", !(frm.doc.employees || []).length); + } + + if ( + (frm.doc.employees || []).length + && !frappe.model.has_workflow(frm.doctype) + && !cint(frm.doc.salary_slips_created) + && (frm.doc.docstatus != 2) + ) { + if (frm.doc.docstatus == 0) { frm.page.clear_primary_action(); - frm.add_custom_button(__("Get Employees"), - function () { - frm.events.get_employee_details(frm); - } - ).toggleClass('btn-primary', !(frm.doc.employees || []).length); - } - if ((frm.doc.employees || []).length && !frappe.model.has_workflow(frm.doctype)) { - frm.page.clear_primary_action(); - frm.page.set_primary_action(__('Create Salary Slips'), () => { - frm.save('Submit').then(() => { + frm.page.set_primary_action(__("Create Salary Slips"), () => { + frm.save("Submit").then(() => { frm.page.clear_primary_action(); frm.refresh(); frm.events.refresh(frm); }); }); + } else if (frm.doc.docstatus == 1 && frm.doc.status == "Failed") { + frm.add_custom_button(__("Create Salary Slip"), function () { + frm.call("create_salary_slips", {}, () => { + frm.reload_doc(); + }); + }).addClass("btn-primary"); } } - if (frm.doc.docstatus == 1) { + + if (frm.doc.docstatus == 1 && frm.doc.status == "Submitted") { if (frm.custom_buttons) frm.clear_custom_buttons(); frm.events.add_context_buttons(frm); } From 3a1c923e76c1f19e101e32d98f54f9ac7d9266bc Mon Sep 17 00:00:00 2001 From: Devin Slauenwhite Date: Thu, 2 Jun 2022 14:15:50 -0400 Subject: [PATCH 057/105] fix: display currencies in validation message. --- erpnext/controllers/accounts_controller.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/erpnext/controllers/accounts_controller.py b/erpnext/controllers/accounts_controller.py index bebfa6c76f5..24a34d63136 100644 --- a/erpnext/controllers/accounts_controller.py +++ b/erpnext/controllers/accounts_controller.py @@ -1465,8 +1465,10 @@ class AccountsController(TransactionBase): if not party_gle_currency and (party_account_currency != self.currency): frappe.throw( - _("Party Account {0} currency and document currency should be same").format( - frappe.bold(party_account) + _("Party Account {0} currency ({1}) and document currency ({2}) should be same").format( + frappe.bold(party_account), + party_account_currency, + self.currency ) ) From b061ea4cd2e66a9a0e2a96ef9174f7c0366c52e9 Mon Sep 17 00:00:00 2001 From: Devin Slauenwhite Date: Thu, 2 Jun 2022 14:23:54 -0400 Subject: [PATCH 058/105] chore: linter --- erpnext/controllers/accounts_controller.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/erpnext/controllers/accounts_controller.py b/erpnext/controllers/accounts_controller.py index 24a34d63136..854c0d00f54 100644 --- a/erpnext/controllers/accounts_controller.py +++ b/erpnext/controllers/accounts_controller.py @@ -1466,9 +1466,7 @@ class AccountsController(TransactionBase): if not party_gle_currency and (party_account_currency != self.currency): frappe.throw( _("Party Account {0} currency ({1}) and document currency ({2}) should be same").format( - frappe.bold(party_account), - party_account_currency, - self.currency + frappe.bold(party_account), party_account_currency, self.currency ) ) From a200e7e1fbb14baf547e47f9644c8b2819916e41 Mon Sep 17 00:00:00 2001 From: Deepesh Garg Date: Sun, 5 Jun 2022 11:16:12 +0530 Subject: [PATCH 059/105] fix: Remove redundant query --- .../item_wise_sales_register.py | 29 ++++++++++++------- 1 file changed, 18 insertions(+), 11 deletions(-) diff --git a/erpnext/accounts/report/item_wise_sales_register/item_wise_sales_register.py b/erpnext/accounts/report/item_wise_sales_register/item_wise_sales_register.py index 2e7213f42b1..ac706666547 100644 --- a/erpnext/accounts/report/item_wise_sales_register/item_wise_sales_register.py +++ b/erpnext/accounts/report/item_wise_sales_register/item_wise_sales_register.py @@ -443,12 +443,6 @@ def get_grand_total(filters, doctype): ] # nosec -def get_deducted_taxes(): - return frappe.db.sql_list( - "select name from `tabPurchase Taxes and Charges` where add_deduct_tax = 'Deduct'" - ) - - def get_tax_accounts( item_list, columns, @@ -462,6 +456,7 @@ def get_tax_accounts( tax_columns = [] invoice_item_row = {} itemised_tax = {} + add_deduct_tax = "charge_type" tax_amount_precision = ( get_field_precision( @@ -477,13 +472,13 @@ def get_tax_accounts( conditions = "" if doctype == "Purchase Invoice": conditions = " and category in ('Total', 'Valuation and Total') and base_tax_amount_after_discount_amount != 0" + add_deduct_tax = "add_deduct_tax" - deducted_tax = get_deducted_taxes() tax_details = frappe.db.sql( """ select name, parent, description, item_wise_tax_detail, - charge_type, base_tax_amount_after_discount_amount + charge_type, {add_deduct_tax}, base_tax_amount_after_discount_amount from `tab%s` where parenttype = %s and docstatus = 1 @@ -491,12 +486,22 @@ def get_tax_accounts( and parent in (%s) %s order by description - """ + """.format( + add_deduct_tax=add_deduct_tax + ) % (tax_doctype, "%s", ", ".join(["%s"] * len(invoice_item_row)), conditions), tuple([doctype] + list(invoice_item_row)), ) - for name, parent, description, item_wise_tax_detail, charge_type, tax_amount in tax_details: + for ( + name, + parent, + description, + item_wise_tax_detail, + charge_type, + add_deduct_tax, + tax_amount, + ) in tax_details: description = handle_html(description) if description not in tax_columns and tax_amount: # as description is text editor earlier and markup can break the column convention in reports @@ -529,7 +534,9 @@ def get_tax_accounts( if item_tax_amount: tax_value = flt(item_tax_amount, tax_amount_precision) tax_value = ( - tax_value * -1 if (doctype == "Purchase Invoice" and name in deducted_tax) else tax_value + tax_value * -1 + if (doctype == "Purchase Invoice" and add_deduct_tax == "Deduct") + else tax_value ) itemised_tax.setdefault(d.name, {})[description] = frappe._dict( From db07831db781b66a0070212ef5a06a600638aa27 Mon Sep 17 00:00:00 2001 From: Deepesh Garg Date: Sun, 5 Jun 2022 12:13:02 +0530 Subject: [PATCH 060/105] fix(India): Supplies from composite dealer not showing up --- erpnext/regional/doctype/gstr_3b_report/gstr_3b_report.py | 1 - 1 file changed, 1 deletion(-) diff --git a/erpnext/regional/doctype/gstr_3b_report/gstr_3b_report.py b/erpnext/regional/doctype/gstr_3b_report/gstr_3b_report.py index d6210abf80d..91fccfa6e8d 100644 --- a/erpnext/regional/doctype/gstr_3b_report/gstr_3b_report.py +++ b/erpnext/regional/doctype/gstr_3b_report/gstr_3b_report.py @@ -148,7 +148,6 @@ class GSTR3BReport(Document): FROM `tabPurchase Invoice` p , `tabPurchase Invoice Item` i WHERE p.docstatus = 1 and p.name = i.parent and p.is_opening = 'No' - and p.gst_category != 'Registered Composition' and (i.is_nil_exempt = 1 or i.is_non_gst = 1 or p.gst_category = 'Registered Composition') and month(p.posting_date) = %s and year(p.posting_date) = %s and p.company = %s and p.company_gstin = %s From 61fa4eb6c947525a948ec3212e3d7af10eed815f Mon Sep 17 00:00:00 2001 From: Deepesh Garg Date: Sun, 5 Jun 2022 18:23:24 +0530 Subject: [PATCH 061/105] fix: Reverse provisional entries on Purchase Invoice cancel --- .../doctype/purchase_invoice/purchase_invoice.py | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/erpnext/accounts/doctype/purchase_invoice/purchase_invoice.py b/erpnext/accounts/doctype/purchase_invoice/purchase_invoice.py index e6da6669ac2..deb905b0093 100644 --- a/erpnext/accounts/doctype/purchase_invoice/purchase_invoice.py +++ b/erpnext/accounts/doctype/purchase_invoice/purchase_invoice.py @@ -532,7 +532,10 @@ class PurchaseInvoice(BuyingController): def make_gl_entries(self, gl_entries=None, from_repost=False): if not gl_entries: - gl_entries = self.get_gl_entries() + if self.docstatus == 1: + gl_entries = self.get_gl_entries() + else: + gl_entries = self.get_gl_entries(cancel=1) if gl_entries: update_outstanding = "No" if (cint(self.is_paid) or self.write_off_account) else "Yes" @@ -545,7 +548,10 @@ class PurchaseInvoice(BuyingController): from_repost=from_repost, ) elif self.docstatus == 2: + provisional_entries = [a for a in gl_entries if a.voucher_type == "Purchase Receipt"] make_reverse_gl_entries(voucher_type=self.doctype, voucher_no=self.name) + if provisional_entries: + make_gl_entries(provisional_entries) if update_outstanding == "No": update_outstanding_amt( @@ -559,7 +565,7 @@ class PurchaseInvoice(BuyingController): elif self.docstatus == 2 and cint(self.update_stock) and self.auto_accounting_for_stock: make_reverse_gl_entries(voucher_type=self.doctype, voucher_no=self.name) - def get_gl_entries(self, warehouse_account=None): + def get_gl_entries(self, warehouse_account=None, cancel=0): self.auto_accounting_for_stock = erpnext.is_perpetual_inventory_enabled(self.company) if self.auto_accounting_for_stock: self.stock_received_but_not_billed = self.get_company_default("stock_received_but_not_billed") @@ -572,7 +578,7 @@ class PurchaseInvoice(BuyingController): gl_entries = [] self.make_supplier_gl_entry(gl_entries) - self.make_item_gl_entries(gl_entries) + self.make_item_gl_entries(gl_entries, cancel=cancel) self.make_discount_gl_entries(gl_entries) if self.check_asset_cwip_enabled(): @@ -639,7 +645,7 @@ class PurchaseInvoice(BuyingController): ) ) - def make_item_gl_entries(self, gl_entries): + def make_item_gl_entries(self, gl_entries, cancel=0): # item gl entries stock_items = self.get_stock_items() if self.update_stock and self.auto_accounting_for_stock: @@ -836,7 +842,7 @@ class PurchaseInvoice(BuyingController): if expense_booked_in_pr: # Intentionally passing purchase invoice item to handle partial billing purchase_receipt_doc.add_provisional_gl_entry( - item, gl_entries, self.posting_date, provisional_account, reverse=1 + item, gl_entries, self.posting_date, provisional_account, reverse=not cancel ) if not self.is_internal_transfer(): From 86a24f3d223c0ede3b8e9762bd166285b39a9b10 Mon Sep 17 00:00:00 2001 From: Deepesh Garg Date: Sun, 5 Jun 2022 19:12:24 +0530 Subject: [PATCH 062/105] fix: Simply cancel reverse entries --- .../purchase_invoice/purchase_invoice.py | 21 +++++++++++-------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/erpnext/accounts/doctype/purchase_invoice/purchase_invoice.py b/erpnext/accounts/doctype/purchase_invoice/purchase_invoice.py index deb905b0093..07173a31c2b 100644 --- a/erpnext/accounts/doctype/purchase_invoice/purchase_invoice.py +++ b/erpnext/accounts/doctype/purchase_invoice/purchase_invoice.py @@ -532,10 +532,7 @@ class PurchaseInvoice(BuyingController): def make_gl_entries(self, gl_entries=None, from_repost=False): if not gl_entries: - if self.docstatus == 1: - gl_entries = self.get_gl_entries() - else: - gl_entries = self.get_gl_entries(cancel=1) + gl_entries = self.get_gl_entries() if gl_entries: update_outstanding = "No" if (cint(self.is_paid) or self.write_off_account) else "Yes" @@ -551,7 +548,13 @@ class PurchaseInvoice(BuyingController): provisional_entries = [a for a in gl_entries if a.voucher_type == "Purchase Receipt"] make_reverse_gl_entries(voucher_type=self.doctype, voucher_no=self.name) if provisional_entries: - make_gl_entries(provisional_entries) + for entry in provisional_entries: + frappe.db.set_value( + "GL Entry", + {"voucher_type": "Purchase Receipt", "voucher_detail_no": entry.voucher_detail_no}, + "is_cancelled", + 1, + ) if update_outstanding == "No": update_outstanding_amt( @@ -565,7 +568,7 @@ class PurchaseInvoice(BuyingController): elif self.docstatus == 2 and cint(self.update_stock) and self.auto_accounting_for_stock: make_reverse_gl_entries(voucher_type=self.doctype, voucher_no=self.name) - def get_gl_entries(self, warehouse_account=None, cancel=0): + def get_gl_entries(self, warehouse_account=None): self.auto_accounting_for_stock = erpnext.is_perpetual_inventory_enabled(self.company) if self.auto_accounting_for_stock: self.stock_received_but_not_billed = self.get_company_default("stock_received_but_not_billed") @@ -578,7 +581,7 @@ class PurchaseInvoice(BuyingController): gl_entries = [] self.make_supplier_gl_entry(gl_entries) - self.make_item_gl_entries(gl_entries, cancel=cancel) + self.make_item_gl_entries(gl_entries) self.make_discount_gl_entries(gl_entries) if self.check_asset_cwip_enabled(): @@ -645,7 +648,7 @@ class PurchaseInvoice(BuyingController): ) ) - def make_item_gl_entries(self, gl_entries, cancel=0): + def make_item_gl_entries(self, gl_entries): # item gl entries stock_items = self.get_stock_items() if self.update_stock and self.auto_accounting_for_stock: @@ -842,7 +845,7 @@ class PurchaseInvoice(BuyingController): if expense_booked_in_pr: # Intentionally passing purchase invoice item to handle partial billing purchase_receipt_doc.add_provisional_gl_entry( - item, gl_entries, self.posting_date, provisional_account, reverse=not cancel + item, gl_entries, self.posting_date, provisional_account, reverse=1 ) if not self.is_internal_transfer(): From c3219ebad1cac35afc04cc051c9e215c70cd1e9b Mon Sep 17 00:00:00 2001 From: Ankush Menat Date: Mon, 6 Jun 2022 12:39:12 +0530 Subject: [PATCH 063/105] fix(Sales Register): incorrect query with dimensions If accounting dimension is also part of the default filters then same query is repeated with incorrect syntax. e.g. `item_group = (child1, child2)` instead of `in` query. fix: don't add default filter if they are part of dimensions to be added. --- .../report/sales_register/sales_register.py | 40 +++++++------------ 1 file changed, 15 insertions(+), 25 deletions(-) diff --git a/erpnext/accounts/report/sales_register/sales_register.py b/erpnext/accounts/report/sales_register/sales_register.py index 34b3f032068..777d96ced17 100644 --- a/erpnext/accounts/report/sales_register/sales_register.py +++ b/erpnext/accounts/report/sales_register/sales_register.py @@ -346,9 +346,13 @@ def get_columns(invoice_list, additional_table_columns): def get_conditions(filters): conditions = "" + accounting_dimensions = get_accounting_dimensions(as_list=False) or [] + accounting_dimensions_list = [d.fieldname for d in accounting_dimensions] + if filters.get("company"): conditions += " and company=%(company)s" - if filters.get("customer"): + + if filters.get("customer") and "customer" not in accounting_dimensions_list: conditions += " and customer = %(customer)s" if filters.get("from_date"): @@ -359,32 +363,18 @@ def get_conditions(filters): if filters.get("owner"): conditions += " and owner = %(owner)s" - if filters.get("mode_of_payment"): - conditions += """ and exists(select name from `tabSales Invoice Payment` + def get_sales_invoice_item_field_condition(field, table="Sales Invoice Item") -> str: + if not filters.get(field) or field in accounting_dimensions_list: + return "" + return f""" and exists(select name from `tab{table}` where parent=`tabSales Invoice`.name - and ifnull(`tabSales Invoice Payment`.mode_of_payment, '') = %(mode_of_payment)s)""" + and ifnull(`tab{table}`.{field}, '') = %({field})s)""" - if filters.get("cost_center"): - conditions += """ and exists(select name from `tabSales Invoice Item` - where parent=`tabSales Invoice`.name - and ifnull(`tabSales Invoice Item`.cost_center, '') = %(cost_center)s)""" - - if filters.get("warehouse"): - conditions += """ and exists(select name from `tabSales Invoice Item` - where parent=`tabSales Invoice`.name - and ifnull(`tabSales Invoice Item`.warehouse, '') = %(warehouse)s)""" - - if filters.get("brand"): - conditions += """ and exists(select name from `tabSales Invoice Item` - where parent=`tabSales Invoice`.name - and ifnull(`tabSales Invoice Item`.brand, '') = %(brand)s)""" - - if filters.get("item_group"): - conditions += """ and exists(select name from `tabSales Invoice Item` - where parent=`tabSales Invoice`.name - and ifnull(`tabSales Invoice Item`.item_group, '') = %(item_group)s)""" - - accounting_dimensions = get_accounting_dimensions(as_list=False) + conditions += get_sales_invoice_item_field_condition("mode_of_payments", "Sales Invoice Payment") + conditions += get_sales_invoice_item_field_condition("cost_center") + conditions += get_sales_invoice_item_field_condition("warehouse") + conditions += get_sales_invoice_item_field_condition("brand") + conditions += get_sales_invoice_item_field_condition("item_group") if accounting_dimensions: common_condition = """ From 4decb7a02be94a98ad8dea9d5d4db37f7410b1d2 Mon Sep 17 00:00:00 2001 From: Rucha Mahabal Date: Fri, 3 Jun 2022 11:03:12 +0530 Subject: [PATCH 064/105] fix: Consider only Approved leave applications in LWP, Employee Benefit calculations - do not allow submitting leave applications with 'Cancelled' status --- erpnext/hr/doctype/leave_application/leave_application.py | 5 +++-- .../employee_benefit_application.py | 1 + erpnext/payroll/doctype/salary_slip/salary_slip.py | 1 + 3 files changed, 5 insertions(+), 2 deletions(-) diff --git a/erpnext/hr/doctype/leave_application/leave_application.py b/erpnext/hr/doctype/leave_application/leave_application.py index cd6b1686675..5a338bdfe83 100755 --- a/erpnext/hr/doctype/leave_application/leave_application.py +++ b/erpnext/hr/doctype/leave_application/leave_application.py @@ -88,7 +88,7 @@ class LeaveApplication(Document): share_doc_with_approver(self, self.leave_approver) def on_submit(self): - if self.status == "Open": + if self.status in ["Open", "Cancelled"]: frappe.throw( _("Only Leave Applications with status 'Approved' and 'Rejected' can be submitted") ) @@ -1117,7 +1117,7 @@ def add_leaves(events, start, end, filter_conditions=None): WHERE from_date <= %(end)s AND to_date >= %(start)s <= to_date AND docstatus < 2 - AND status != 'Rejected' + AND status in ('Approved', 'Open') """ if conditions: @@ -1206,6 +1206,7 @@ def get_approved_leaves_for_period(employee, leave_type, from_date, to_date): from `tabLeave Application` where employee=%(employee)s and docstatus=1 + and status='Approved' 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)) diff --git a/erpnext/payroll/doctype/employee_benefit_application/employee_benefit_application.py b/erpnext/payroll/doctype/employee_benefit_application/employee_benefit_application.py index 0acd44711b0..592e7dd6f09 100644 --- a/erpnext/payroll/doctype/employee_benefit_application/employee_benefit_application.py +++ b/erpnext/payroll/doctype/employee_benefit_application/employee_benefit_application.py @@ -216,6 +216,7 @@ def calculate_lwp(employee, start_date, holidays, working_days): where t2.name = t1.leave_type and t2.is_lwp = 1 and t1.docstatus = 1 + and t1.status = 'Approved' and t1.employee = %(employee)s and CASE WHEN t2.include_holiday != 1 THEN %(dt)s not in ('{0}') and %(dt)s between from_date and to_date WHEN t2.include_holiday THEN %(dt)s between from_date and to_date diff --git a/erpnext/payroll/doctype/salary_slip/salary_slip.py b/erpnext/payroll/doctype/salary_slip/salary_slip.py index 4c5fea1e759..378227f0463 100644 --- a/erpnext/payroll/doctype/salary_slip/salary_slip.py +++ b/erpnext/payroll/doctype/salary_slip/salary_slip.py @@ -477,6 +477,7 @@ class SalarySlip(TransactionBase): WHERE t2.name = t1.leave_type AND (t2.is_lwp = 1 or t2.is_ppl = 1) AND t1.docstatus = 1 + AND t1.status = 'Approved' AND t1.employee = %(employee)s AND ifnull(t1.salary_slip, '') = '' AND CASE From 59de303b131c49f5ffacb3142f58b688e2d1c926 Mon Sep 17 00:00:00 2001 From: Rucha Mahabal Date: Fri, 3 Jun 2022 16:47:42 +0530 Subject: [PATCH 065/105] refactor: rewrite lwp queries using query builder --- .../leave_application/leave_application.py | 42 ++++++---- .../employee_benefit_application.py | 44 ++++++----- .../doctype/salary_slip/salary_slip.py | 77 ++++++++++++------- 3 files changed, 99 insertions(+), 64 deletions(-) diff --git a/erpnext/hr/doctype/leave_application/leave_application.py b/erpnext/hr/doctype/leave_application/leave_application.py index 5a338bdfe83..53c5df4210a 100755 --- a/erpnext/hr/doctype/leave_application/leave_application.py +++ b/erpnext/hr/doctype/leave_application/leave_application.py @@ -1201,25 +1201,33 @@ def get_mandatory_approval(doctype): def get_approved_leaves_for_period(employee, leave_type, from_date, to_date): - query = """ - select employee, leave_type, from_date, to_date, total_leave_days - from `tabLeave Application` - where employee=%(employee)s - and docstatus=1 - and status='Approved' - 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)) - """ - if leave_type: - query += "and leave_type=%(leave_type)s" - - leave_applications = frappe.db.sql( - query, - {"from_date": from_date, "to_date": to_date, "employee": employee, "leave_type": leave_type}, - as_dict=1, + LeaveApplication = frappe.qb.DocType("Leave Application") + query = ( + frappe.qb.from_(LeaveApplication) + .select( + LeaveApplication.employee, + LeaveApplication.leave_type, + LeaveApplication.from_date, + LeaveApplication.to_date, + LeaveApplication.total_leave_days, + ) + .where( + (LeaveApplication.employee == employee) + & (LeaveApplication.docstatus == 1) + & (LeaveApplication.status == "Approved") + & ( + (LeaveApplication.from_date.between(from_date, to_date)) + | (LeaveApplication.to_date.between(from_date, to_date)) + | ((LeaveApplication.from_date < from_date) & (LeaveApplication.to_date > to_date)) + ) + ) ) + if leave_type: + query = query.where(LeaveApplication.leave_type == leave_type) + + leave_applications = query.run(as_dict=True) + leave_days = 0 for leave_app in leave_applications: if leave_app.from_date >= getdate(from_date) and leave_app.to_date <= getdate(to_date): diff --git a/erpnext/payroll/doctype/employee_benefit_application/employee_benefit_application.py b/erpnext/payroll/doctype/employee_benefit_application/employee_benefit_application.py index 592e7dd6f09..8dad7cc8bc9 100644 --- a/erpnext/payroll/doctype/employee_benefit_application/employee_benefit_application.py +++ b/erpnext/payroll/doctype/employee_benefit_application/employee_benefit_application.py @@ -207,27 +207,35 @@ def get_max_benefits_remaining(employee, on_date, payroll_period): def calculate_lwp(employee, start_date, holidays, working_days): lwp = 0 holidays = "','".join(holidays) + for d in range(working_days): dt = add_days(cstr(getdate(start_date)), d) - leave = frappe.db.sql( - """ - select t1.name, t1.half_day - from `tabLeave Application` t1, `tabLeave Type` t2 - where t2.name = t1.leave_type - and t2.is_lwp = 1 - and t1.docstatus = 1 - and t1.status = 'Approved' - and t1.employee = %(employee)s - and CASE WHEN t2.include_holiday != 1 THEN %(dt)s not in ('{0}') and %(dt)s between from_date and to_date - WHEN t2.include_holiday THEN %(dt)s between from_date and to_date - END - """.format( - holidays - ), - {"employee": employee, "dt": dt}, + + LeaveApplication = frappe.qb.DocType("Leave Application") + LeaveType = frappe.qb.DocType("Leave Type") + + query = ( + frappe.qb.from_(LeaveApplication) + .inner_join(LeaveType) + .on((LeaveType.name == LeaveApplication.leave_type)) + .select(LeaveApplication.name, LeaveApplication.half_day) + .where( + (LeaveType.is_lwp == 1) + & (LeaveApplication.docstatus == 1) + & (LeaveApplication.status == "Approved") + & (LeaveApplication.employee == employee) + & ((LeaveApplication.from_date <= dt) & (dt <= LeaveApplication.to_date)) + ) ) - if leave: - lwp = cint(leave[0][1]) and (lwp + 0.5) or (lwp + 1) + + # if it's a holiday only include if leave type has "include holiday" enabled + if dt in holidays: + query = query.where((LeaveType.include_holiday == "1")) + leaves = query.run() + + if leaves: + lwp = cint(leaves[0][1]) and (lwp + 0.5) or (lwp + 1) + return lwp diff --git a/erpnext/payroll/doctype/salary_slip/salary_slip.py b/erpnext/payroll/doctype/salary_slip/salary_slip.py index 378227f0463..6a35985e64c 100644 --- a/erpnext/payroll/doctype/salary_slip/salary_slip.py +++ b/erpnext/payroll/doctype/salary_slip/salary_slip.py @@ -465,38 +465,14 @@ class SalarySlip(TransactionBase): ) for d in range(working_days): - dt = add_days(cstr(getdate(self.start_date)), d) - leave = frappe.db.sql( - """ - SELECT t1.name, - CASE WHEN (t1.half_day_date = %(dt)s or t1.to_date = t1.from_date) - THEN t1.half_day else 0 END, - t2.is_ppl, - t2.fraction_of_daily_salary_per_leave - FROM `tabLeave Application` t1, `tabLeave Type` t2 - WHERE t2.name = t1.leave_type - AND (t2.is_lwp = 1 or t2.is_ppl = 1) - AND t1.docstatus = 1 - AND t1.status = 'Approved' - AND t1.employee = %(employee)s - AND ifnull(t1.salary_slip, '') = '' - AND CASE - WHEN t2.include_holiday != 1 - THEN %(dt)s not in ('{0}') and %(dt)s between from_date and to_date - WHEN t2.include_holiday - THEN %(dt)s between from_date and to_date - END - """.format( - holidays - ), - {"employee": self.employee, "dt": dt}, - ) + date = add_days(cstr(getdate(self.start_date)), d) + leave = get_lwp_or_ppl_for_date(date, self.employee, holidays) if leave: equivalent_lwp_count = 0 - is_half_day_leave = cint(leave[0][1]) - is_partially_paid_leave = cint(leave[0][2]) - fraction_of_daily_salary_per_leave = flt(leave[0][3]) + is_half_day_leave = cint(leave[0].is_half_day) + is_partially_paid_leave = cint(leave[0].is_ppl) + fraction_of_daily_salary_per_leave = flt(leave[0].fraction_of_daily_salary_per_leave) equivalent_lwp_count = (1 - daily_wages_fraction_for_half_day) if is_half_day_leave else 1 @@ -1743,3 +1719,46 @@ def eval_tax_slab_condition(condition, eval_globals=None, eval_locals=None): except Exception as e: frappe.throw(_("Error in formula or condition: {0} in Income Tax Slab").format(e)) raise + + +def get_lwp_or_ppl_for_date(date, employee, holidays): + LeaveApplication = frappe.qb.DocType("Leave Application") + LeaveType = frappe.qb.DocType("Leave Type") + + is_half_day = ( + frappe.qb.terms.Case() + .when( + ( + (LeaveApplication.half_day_date == date) + | (LeaveApplication.from_date == LeaveApplication.to_date) + ), + LeaveApplication.half_day, + ) + .else_(0) + ).as_("is_half_day") + + query = ( + frappe.qb.from_(LeaveApplication) + .inner_join(LeaveType) + .on((LeaveType.name == LeaveApplication.leave_type)) + .select( + LeaveApplication.name, + LeaveType.is_ppl, + LeaveType.fraction_of_daily_salary_per_leave, + (is_half_day), + ) + .where( + (((LeaveType.is_lwp == 1) | (LeaveType.is_ppl == 1))) + & (LeaveApplication.docstatus == 1) + & (LeaveApplication.status == "Approved") + & (LeaveApplication.employee == employee) + & ((LeaveApplication.salary_slip.isnull()) | (LeaveApplication.salary_slip == "")) + & ((LeaveApplication.from_date <= date) & (date <= LeaveApplication.to_date)) + ) + ) + + # if it's a holiday only include if leave type has "include holiday" enabled + if date in holidays: + query = query.where((LeaveType.include_holiday == "1")) + + return query.run(as_dict=True) From cf04683ad333ca1d1b34d4bfb9a64b69e539657a Mon Sep 17 00:00:00 2001 From: Rucha Mahabal Date: Fri, 3 Jun 2022 17:00:01 +0530 Subject: [PATCH 066/105] fix(Leave Application): 'Cancelled' status shown as 'Open' in list view --- .../leave_application/leave_application_list.js | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/erpnext/hr/doctype/leave_application/leave_application_list.js b/erpnext/hr/doctype/leave_application/leave_application_list.js index a3c03b1bec7..157271a5a0e 100644 --- a/erpnext/hr/doctype/leave_application/leave_application_list.js +++ b/erpnext/hr/doctype/leave_application/leave_application_list.js @@ -1,13 +1,14 @@ -frappe.listview_settings['Leave Application'] = { +frappe.listview_settings["Leave Application"] = { add_fields: ["leave_type", "employee", "employee_name", "total_leave_days", "from_date", "to_date"], has_indicator_for_draft: 1, get_indicator: function (doc) { - if (doc.status === "Approved") { - return [__("Approved"), "green", "status,=,Approved"]; - } else if (doc.status === "Rejected") { - return [__("Rejected"), "red", "status,=,Rejected"]; - } else { - return [__("Open"), "red", "status,=,Open"]; - } + let status_color = { + "Approved": "green", + "Rejected": "red", + "Open": "orange", + "Cancelled": "red", + "Submitted": "blue" + }; + return [__(doc.status), status_color[doc.status], "status,=," + doc.status]; } }; From 3b8bc7d8e1446d8b48f7f95924842650cb3f74e3 Mon Sep 17 00:00:00 2001 From: Rucha Mahabal Date: Mon, 6 Jun 2022 13:03:05 +0530 Subject: [PATCH 067/105] fix: incorrect LWP calculation for half days in employee benefit application --- .../employee_benefit_application.py | 38 +++++++++++++------ 1 file changed, 27 insertions(+), 11 deletions(-) diff --git a/erpnext/payroll/doctype/employee_benefit_application/employee_benefit_application.py b/erpnext/payroll/doctype/employee_benefit_application/employee_benefit_application.py index 8dad7cc8bc9..8df1bb6e87e 100644 --- a/erpnext/payroll/doctype/employee_benefit_application/employee_benefit_application.py +++ b/erpnext/payroll/doctype/employee_benefit_application/employee_benefit_application.py @@ -5,7 +5,7 @@ import frappe from frappe import _ from frappe.model.document import Document -from frappe.utils import add_days, cint, cstr, date_diff, getdate, rounded +from frappe.utils import add_days, cstr, date_diff, flt, getdate, rounded from erpnext.hr.utils import ( get_holiday_dates_for_employee, @@ -27,11 +27,14 @@ class EmployeeBenefitApplication(Document): validate_active_employee(self.employee) self.validate_duplicate_on_payroll_period() if not self.max_benefits: - self.max_benefits = get_max_benefits_remaining(self.employee, self.date, self.payroll_period) + self.max_benefits = flt( + get_max_benefits_remaining(self.employee, self.date, self.payroll_period), + self.precision("max_benefits"), + ) if self.max_benefits and self.max_benefits > 0: self.validate_max_benefit_for_component() self.validate_prev_benefit_claim() - if self.remaining_benefit > 0: + if self.remaining_benefit and self.remaining_benefit > 0: self.validate_remaining_benefit_amount() else: frappe.throw( @@ -110,7 +113,7 @@ class EmployeeBenefitApplication(Document): max_benefit_amount = 0 for employee_benefit in self.employee_benefits: self.validate_max_benefit(employee_benefit.earning_component) - max_benefit_amount += employee_benefit.amount + max_benefit_amount += flt(employee_benefit.amount) if max_benefit_amount > self.max_benefits: frappe.throw( _("Maximum benefit amount of employee {0} exceeds {1}").format( @@ -125,7 +128,8 @@ class EmployeeBenefitApplication(Document): benefit_amount = 0 for employee_benefit in self.employee_benefits: if employee_benefit.earning_component == earning_component_name: - benefit_amount += employee_benefit.amount + benefit_amount += flt(employee_benefit.amount) + prev_sal_slip_flexi_amount = get_sal_slip_total_benefit_given( self.employee, frappe.get_doc("Payroll Period", self.payroll_period), earning_component_name ) @@ -209,32 +213,44 @@ def calculate_lwp(employee, start_date, holidays, working_days): holidays = "','".join(holidays) for d in range(working_days): - dt = add_days(cstr(getdate(start_date)), d) + date = add_days(cstr(getdate(start_date)), d) LeaveApplication = frappe.qb.DocType("Leave Application") LeaveType = frappe.qb.DocType("Leave Type") + is_half_day = ( + frappe.qb.terms.Case() + .when( + ( + (LeaveApplication.half_day_date == date) + | (LeaveApplication.from_date == LeaveApplication.to_date) + ), + LeaveApplication.half_day, + ) + .else_(0) + ).as_("is_half_day") + query = ( frappe.qb.from_(LeaveApplication) .inner_join(LeaveType) .on((LeaveType.name == LeaveApplication.leave_type)) - .select(LeaveApplication.name, LeaveApplication.half_day) + .select(LeaveApplication.name, is_half_day) .where( (LeaveType.is_lwp == 1) & (LeaveApplication.docstatus == 1) & (LeaveApplication.status == "Approved") & (LeaveApplication.employee == employee) - & ((LeaveApplication.from_date <= dt) & (dt <= LeaveApplication.to_date)) + & ((LeaveApplication.from_date <= date) & (date <= LeaveApplication.to_date)) ) ) # if it's a holiday only include if leave type has "include holiday" enabled - if dt in holidays: + if date in holidays: query = query.where((LeaveType.include_holiday == "1")) - leaves = query.run() + leaves = query.run(as_dict=True) if leaves: - lwp = cint(leaves[0][1]) and (lwp + 0.5) or (lwp + 1) + lwp += 0.5 if leaves[0].is_half_day else 1 return lwp From edb775c3814ec94537f790d92b745e9672a45f21 Mon Sep 17 00:00:00 2001 From: Rucha Mahabal Date: Mon, 6 Jun 2022 13:42:23 +0530 Subject: [PATCH 068/105] test: Employee Benefit Application - make `get_no_of_days` a function for reusability --- .../test_employee_benefit_application.py | 80 ++++++++++++++++++- .../doctype/salary_slip/test_salary_slip.py | 40 +++++----- 2 files changed, 99 insertions(+), 21 deletions(-) diff --git a/erpnext/payroll/doctype/employee_benefit_application/test_employee_benefit_application.py b/erpnext/payroll/doctype/employee_benefit_application/test_employee_benefit_application.py index 02149adfce5..de8f9b6a7ad 100644 --- a/erpnext/payroll/doctype/employee_benefit_application/test_employee_benefit_application.py +++ b/erpnext/payroll/doctype/employee_benefit_application/test_employee_benefit_application.py @@ -3,6 +3,82 @@ import unittest +import frappe +from frappe.tests.utils import FrappeTestCase +from frappe.utils import add_days, date_diff, get_year_ending, get_year_start, getdate -class TestEmployeeBenefitApplication(unittest.TestCase): - pass +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.hr.utils import get_holiday_dates_for_employee +from erpnext.payroll.doctype.employee_benefit_application.employee_benefit_application import ( + calculate_lwp, +) +from erpnext.payroll.doctype.employee_tax_exemption_declaration.test_employee_tax_exemption_declaration import ( + create_payroll_period, +) +from erpnext.payroll.doctype.salary_slip.test_salary_slip import ( + make_holiday_list, + make_leave_application, +) +from erpnext.payroll.doctype.salary_structure.salary_structure import make_salary_slip +from erpnext.payroll.doctype.salary_structure.test_salary_structure import make_salary_structure + + +class TestEmployeeBenefitApplication(FrappeTestCase): + def setUp(self): + date = getdate() + make_holiday_list(from_date=get_year_start(date), to_date=get_year_ending(date)) + + @set_holiday_list("Salary Slip Test Holiday List", "_Test Company") + def test_employee_benefit_application(self): + payroll_period = create_payroll_period(name="_Test Payroll Period 1", company="_Test Company") + employee = make_employee("test_employee_benefits@salary.com", company="_Test Company") + first_sunday = get_first_sunday("Salary Slip Test Holiday List") + + leave_application = make_leave_application( + employee, + add_days(first_sunday, 1), + add_days(first_sunday, 3), + "Leave Without Pay", + half_day=1, + half_day_date=add_days(first_sunday, 1), + submit=True, + ) + + frappe.db.set_value("Leave Type", "Leave Without Pay", "include_holiday", 0) + salary_structure = make_salary_structure( + "Test Employee Benefits", + "Monthly", + other_details={"max_benefits": 100000}, + include_flexi_benefits=True, + employee=employee, + payroll_period=payroll_period, + ) + salary_slip = make_salary_slip(salary_structure.name, employee=employee, posting_date=getdate()) + salary_slip.insert() + salary_slip.submit() + + application = make_employee_benefit_application( + employee, payroll_period.name, date=leave_application.to_date + ) + self.assertEqual(application.employee_benefits[0].max_benefit_amount, 15000) + + holidays = get_holiday_dates_for_employee(employee, payroll_period.start_date, application.date) + working_days = date_diff(application.date, payroll_period.start_date) + 1 + lwp = calculate_lwp(employee, payroll_period.start_date, holidays, working_days) + self.assertEqual(lwp, 2.5) + + +def make_employee_benefit_application(employee, payroll_period, date): + frappe.db.delete("Employee Benefit Application") + + return frappe.get_doc( + { + "doctype": "Employee Benefit Application", + "employee": employee, + "date": date, + "payroll_period": payroll_period, + "employee_benefits": [{"earning_component": "Medical Allowance", "amount": 1500}], + } + ).insert() diff --git a/erpnext/payroll/doctype/salary_slip/test_salary_slip.py b/erpnext/payroll/doctype/salary_slip/test_salary_slip.py index 5e3814b73cd..a8b6bb5714b 100644 --- a/erpnext/payroll/doctype/salary_slip/test_salary_slip.py +++ b/erpnext/payroll/doctype/salary_slip/test_salary_slip.py @@ -49,7 +49,7 @@ class TestSalarySlip(unittest.TestCase): "Payroll Settings", {"payroll_based_on": "Attendance", "daily_wages_fraction_for_half_day": 0.75} ) def test_payment_days_based_on_attendance(self): - no_of_days = self.get_no_of_days() + no_of_days = get_no_of_days() emp_id = make_employee("test_payment_days_based_on_attendance@salary.com") frappe.db.set_value("Employee", emp_id, {"relieving_date": None, "status": "Active"}) @@ -128,7 +128,7 @@ class TestSalarySlip(unittest.TestCase): }, ) def test_payment_days_for_mid_joinee_including_holidays(self): - no_of_days = self.get_no_of_days() + no_of_days = get_no_of_days() month_start_date, month_end_date = get_first_day(nowdate()), get_last_day(nowdate()) new_emp_id = make_employee("test_payment_days_based_on_joining_date@salary.com") @@ -196,7 +196,7 @@ class TestSalarySlip(unittest.TestCase): # tests mid month joining and relieving along with unmarked days from erpnext.hr.doctype.holiday_list.holiday_list import is_holiday - no_of_days = self.get_no_of_days() + no_of_days = get_no_of_days() month_start_date, month_end_date = get_first_day(nowdate()), get_last_day(nowdate()) new_emp_id = make_employee("test_payment_days_based_on_joining_date@salary.com") @@ -236,7 +236,7 @@ class TestSalarySlip(unittest.TestCase): def test_payment_days_for_mid_joinee_excluding_holidays(self): from erpnext.hr.doctype.holiday_list.holiday_list import is_holiday - no_of_days = self.get_no_of_days() + no_of_days = get_no_of_days() month_start_date, month_end_date = get_first_day(nowdate()), get_last_day(nowdate()) new_emp_id = make_employee("test_payment_days_based_on_joining_date@salary.com") @@ -267,7 +267,7 @@ class TestSalarySlip(unittest.TestCase): @change_settings("Payroll Settings", {"payroll_based_on": "Leave"}) def test_payment_days_based_on_leave_application(self): - no_of_days = self.get_no_of_days() + no_of_days = get_no_of_days() emp_id = make_employee("test_payment_days_based_on_leave_application@salary.com") frappe.db.set_value("Employee", emp_id, {"relieving_date": None, "status": "Active"}) @@ -366,7 +366,7 @@ class TestSalarySlip(unittest.TestCase): salary_slip.submit() salary_slip.reload() - no_of_days = self.get_no_of_days() + no_of_days = get_no_of_days() days_in_month = no_of_days[0] no_of_holidays = no_of_days[1] @@ -441,7 +441,7 @@ class TestSalarySlip(unittest.TestCase): @change_settings("Payroll Settings", {"include_holidays_in_total_working_days": 1}) def test_salary_slip_with_holidays_included(self): - no_of_days = self.get_no_of_days() + no_of_days = get_no_of_days() make_employee("test_salary_slip_with_holidays_included@salary.com") frappe.db.set_value( "Employee", @@ -473,7 +473,7 @@ class TestSalarySlip(unittest.TestCase): @change_settings("Payroll Settings", {"include_holidays_in_total_working_days": 0}) def test_salary_slip_with_holidays_excluded(self): - no_of_days = self.get_no_of_days() + no_of_days = get_no_of_days() make_employee("test_salary_slip_with_holidays_excluded@salary.com") frappe.db.set_value( "Employee", @@ -510,7 +510,7 @@ class TestSalarySlip(unittest.TestCase): create_salary_structure_assignment, ) - no_of_days = self.get_no_of_days() + no_of_days = get_no_of_days() # set joinng date in the same month employee = make_employee("test_payment_days@salary.com") @@ -984,17 +984,18 @@ class TestSalarySlip(unittest.TestCase): activity_type.wage_rate = 25 activity_type.save() - def get_no_of_days(self): - no_of_days_in_month = calendar.monthrange(getdate(nowdate()).year, getdate(nowdate()).month) - no_of_holidays_in_month = len( - [ - 1 - for i in calendar.monthcalendar(getdate(nowdate()).year, getdate(nowdate()).month) - if i[6] != 0 - ] - ) - return [no_of_days_in_month[1], no_of_holidays_in_month] +def get_no_of_days(): + no_of_days_in_month = calendar.monthrange(getdate(nowdate()).year, getdate(nowdate()).month) + no_of_holidays_in_month = len( + [ + 1 + for i in calendar.monthcalendar(getdate(nowdate()).year, getdate(nowdate()).month) + if i[6] != 0 + ] + ) + + return [no_of_days_in_month[1], no_of_holidays_in_month] def make_employee_salary_slip(user, payroll_frequency, salary_structure=None, posting_date=None): @@ -1136,6 +1137,7 @@ def make_earning_salary_component( "pay_against_benefit_claim": 0, "type": "Earning", "max_benefit_amount": 15000, + "depends_on_payment_days": 1, }, ] ) From c66c1e2215d251a904816436c8f99ebfbd83ff65 Mon Sep 17 00:00:00 2001 From: Ankush Menat Date: Mon, 6 Jun 2022 15:27:29 +0530 Subject: [PATCH 069/105] chore(meta): disable stale bot on issues --- .github/stale.yml | 12 ------------ 1 file changed, 12 deletions(-) diff --git a/.github/stale.yml b/.github/stale.yml index fbf64471567..cdce0aee082 100644 --- a/.github/stale.yml +++ b/.github/stale.yml @@ -23,15 +23,3 @@ pulls: activity occurs, but it only takes a comment to keep a contribution alive :) Also, even if it is closed, you can always reopen the PR when you're ready. Thank you for contributing. - -issues: - daysUntilStale: 90 - daysUntilClose: 7 - exemptLabels: - - valid - - to-validate - - QA - markComment: > - This issue has been automatically marked as inactive because it has not had - recent activity and it wasn't validated by maintainer team. It will be - closed within a week if no further activity occurs. From 3513d54c0a04887422caf932071754fa71e1f6bc Mon Sep 17 00:00:00 2001 From: vishdha Date: Fri, 3 Jun 2022 14:23:53 +0530 Subject: [PATCH 070/105] fix: Print/PDF for financial statement reports displays either wrong date range or wrong fiscal year --- .../consolidated_financial_statement.js | 20 +++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/erpnext/accounts/report/consolidated_financial_statement/consolidated_financial_statement.js b/erpnext/accounts/report/consolidated_financial_statement/consolidated_financial_statement.js index d3e836afd10..a5c5a67613e 100644 --- a/erpnext/accounts/report/consolidated_financial_statement/consolidated_financial_statement.js +++ b/erpnext/accounts/report/consolidated_financial_statement/consolidated_financial_statement.js @@ -50,7 +50,15 @@ frappe.require("assets/erpnext/js/financial_statements.js", function() { "fieldtype": "Link", "options": "Fiscal Year", "default": frappe.defaults.get_user_default("fiscal_year"), - "reqd": 1 + "reqd": 1, + on_change: () => { + frappe.model.with_doc("Fiscal Year", frappe.query_report.get_filter_value('from_fiscal_year'), function(r) { + let start_fy = frappe.model.get_doc("Fiscal Year", frappe.query_report.get_filter_value('from_fiscal_year')); + frappe.query_report.set_filter_value({ + period_start_date: start_fy.year_start_date + }); + }); + } }, { "fieldname":"to_fiscal_year", @@ -58,7 +66,15 @@ frappe.require("assets/erpnext/js/financial_statements.js", function() { "fieldtype": "Link", "options": "Fiscal Year", "default": frappe.defaults.get_user_default("fiscal_year"), - "reqd": 1 + "reqd": 1, + on_change: () => { + frappe.model.with_doc("Fiscal Year", frappe.query_report.get_filter_value('to_fiscal_year'), function(r) { + let to_fy = frappe.model.get_doc("Fiscal Year", frappe.query_report.get_filter_value('to_fiscal_year')); + frappe.query_report.set_filter_value({ + period_end_date: to_fy.year_end_date + }); + }); + } }, { "fieldname":"finance_book", From 53774e0f520defeb47d41d2fe3078ab26962f5a6 Mon Sep 17 00:00:00 2001 From: vishdha Date: Mon, 6 Jun 2022 13:07:39 +0530 Subject: [PATCH 071/105] chore: minor change in fetching start and end date --- .../consolidated_financial_statement.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/erpnext/accounts/report/consolidated_financial_statement/consolidated_financial_statement.js b/erpnext/accounts/report/consolidated_financial_statement/consolidated_financial_statement.js index a5c5a67613e..dd965a9813e 100644 --- a/erpnext/accounts/report/consolidated_financial_statement/consolidated_financial_statement.js +++ b/erpnext/accounts/report/consolidated_financial_statement/consolidated_financial_statement.js @@ -53,9 +53,9 @@ frappe.require("assets/erpnext/js/financial_statements.js", function() { "reqd": 1, on_change: () => { frappe.model.with_doc("Fiscal Year", frappe.query_report.get_filter_value('from_fiscal_year'), function(r) { - let start_fy = frappe.model.get_doc("Fiscal Year", frappe.query_report.get_filter_value('from_fiscal_year')); + let year_start_date = frappe.model.get_value("Fiscal Year", frappe.query_report.get_filter_value('from_fiscal_year'), "year_start_date"); frappe.query_report.set_filter_value({ - period_start_date: start_fy.year_start_date + period_start_date: year_start_date }); }); } @@ -69,9 +69,9 @@ frappe.require("assets/erpnext/js/financial_statements.js", function() { "reqd": 1, on_change: () => { frappe.model.with_doc("Fiscal Year", frappe.query_report.get_filter_value('to_fiscal_year'), function(r) { - let to_fy = frappe.model.get_doc("Fiscal Year", frappe.query_report.get_filter_value('to_fiscal_year')); + let year_end_date = frappe.model.get_value("Fiscal Year", frappe.query_report.get_filter_value('to_fiscal_year'), "year_end_date"); frappe.query_report.set_filter_value({ - period_end_date: to_fy.year_end_date + period_end_date: year_end_date }); }); } From ee5bc58e9ba8b4c4b4ab255101919974302068e6 Mon Sep 17 00:00:00 2001 From: Ankush Menat Date: Mon, 6 Jun 2022 16:27:25 +0530 Subject: [PATCH 072/105] fix(job card): only hold during draft state (#31243) --- .../doctype/job_card/job_card.py | 2 +- erpnext/patches.txt | 1 + .../patches/v13_0/job_card_status_on_hold.py | 19 +++++++++++++++++++ 3 files changed, 21 insertions(+), 1 deletion(-) create mode 100644 erpnext/patches/v13_0/job_card_status_on_hold.py diff --git a/erpnext/manufacturing/doctype/job_card/job_card.py b/erpnext/manufacturing/doctype/job_card/job_card.py index 0a9fd8a0996..0199a5c31ea 100644 --- a/erpnext/manufacturing/doctype/job_card/job_card.py +++ b/erpnext/manufacturing/doctype/job_card/job_card.py @@ -621,7 +621,7 @@ class JobCard(Document): self.set_status(update_status) def set_status(self, update_status=False): - if self.status == "On Hold": + if self.status == "On Hold" and self.docstatus == 0: return self.status = {0: "Open", 1: "Submitted", 2: "Cancelled"}[self.docstatus or 0] diff --git a/erpnext/patches.txt b/erpnext/patches.txt index 8594ebbe9d5..5a984635fdc 100644 --- a/erpnext/patches.txt +++ b/erpnext/patches.txt @@ -373,3 +373,4 @@ erpnext.patches.v13_0.create_accounting_dimensions_in_orders erpnext.patches.v13_0.set_per_billed_in_return_delivery_note execute:frappe.delete_doc("DocType", "Naming Series") erpnext.patches.v13_0.set_payroll_entry_status +erpnext.patches.v13_0.job_card_status_on_hold diff --git a/erpnext/patches/v13_0/job_card_status_on_hold.py b/erpnext/patches/v13_0/job_card_status_on_hold.py new file mode 100644 index 00000000000..8c67c3c858e --- /dev/null +++ b/erpnext/patches/v13_0/job_card_status_on_hold.py @@ -0,0 +1,19 @@ +import frappe + + +def execute(): + job_cards = frappe.get_all( + "Job Card", + {"status": "On Hold", "docstatus": ("!=", 0)}, + pluck="name", + ) + + for idx, job_card in enumerate(job_cards): + try: + doc = frappe.get_doc("Job Card", job_card) + doc.set_status() + doc.db_set("status", doc.status, update_modified=False) + if idx % 100 == 0: + frappe.db.commit() + except Exception: + continue From 934db57fdd5346dde982e9022f33d61780175d07 Mon Sep 17 00:00:00 2001 From: marination Date: Mon, 6 Jun 2022 17:01:51 +0530 Subject: [PATCH 073/105] chore: Miscellanous fixes/enhancements - `get_valuation_rate`: if no bins are found return 0, SLEs do not exist either - `get_valuation_rate`: Compute average valuation rate via query - `get_rm_rate_map`: set order_by as None to avoid creating sort index (modified) each time query runs (seen in process list) - BOM Update Batch: add status field and hide `boms_updated` so that users can see progress without loading all updated boms (too much data) - BOM Update Batch: set batch row status to completed after job runs - BOM Update Log: remove `parent_boms` field (just pass parent boms to processing function) & remove Paused state (not used) - Move job to long queue to avoid choking default queue - `update_cost_in_boms`: use `get_doc` as each BOM is accessed only once. Use `for_update` to lock BOM row - Commit after every 100 BOMs --- erpnext/manufacturing/doctype/bom/bom.py | 30 +++++++++------- .../bom_update_batch/bom_update_batch.json | 14 ++++++-- .../bom_update_log/bom_update_log.json | 12 ++----- .../doctype/bom_update_log/bom_update_log.py | 34 ++++++++++++------- .../bom_update_log/bom_updation_utils.py | 24 +++++++++---- .../bom_update_tool/bom_update_tool.py | 2 +- 6 files changed, 73 insertions(+), 43 deletions(-) diff --git a/erpnext/manufacturing/doctype/bom/bom.py b/erpnext/manufacturing/doctype/bom/bom.py index d4e0d4b8147..1fb00ef3fe2 100644 --- a/erpnext/manufacturing/doctype/bom/bom.py +++ b/erpnext/manufacturing/doctype/bom/bom.py @@ -714,8 +714,11 @@ class BOM(WebsiteGenerator): for item in self.get("items"): if item.bom_no: # Get Item-Rate from Subassembly BOM - explosion_items = frappe.db.get_all( - "BOM Explosion Item", filters={"parent": item.bom_no}, fields=["item_code", "rate"] + explosion_items = frappe.get_all( + "BOM Explosion Item", + filters={"parent": item.bom_no}, + fields=["item_code", "rate"], + order_by=None, # to avoid sort index creation at db level (granular change) ) explosion_item_rate = {item.item_code: flt(item.rate) for item in explosion_items} rm_rate_map.update(explosion_item_rate) @@ -935,13 +938,17 @@ def get_bom_item_rate(args, bom_doc): def get_valuation_rate(args): - """Get weighted average of valuation rate from all warehouses""" + """ + 1) Get average valuation rate from all warehouses + 2) If no value, get last valuation rate from SLE + 3) If no value, get valuation rate from Item + """ - total_qty, total_value, valuation_rate = 0.0, 0.0, 0.0 - item_bins = frappe.db.sql( + valuation_rate = 0.0 + item_valuation = frappe.db.sql( """ select - bin.actual_qty, bin.stock_value + (sum(bin.stock_value) / sum(bin.actual_qty)) as valuation_rate from `tabBin` bin, `tabWarehouse` warehouse where @@ -950,14 +957,13 @@ def get_valuation_rate(args): and warehouse.company=%(company)s""", {"item": args["item_code"], "company": args["company"]}, as_dict=1, - ) + )[0] - for d in item_bins: - total_qty += flt(d.actual_qty) - total_value += flt(d.stock_value) + valuation_rate = item_valuation.get("valuation_rate") - if total_qty: - valuation_rate = total_value / total_qty + if valuation_rate is None: + # Explicit null value check. If null, Bins don't exist, neither does SLE + return valuation_rate if valuation_rate <= 0: last_valuation_rate = frappe.db.sql( diff --git a/erpnext/manufacturing/doctype/bom_update_batch/bom_update_batch.json b/erpnext/manufacturing/doctype/bom_update_batch/bom_update_batch.json index 9938454ce4e..83b54d326cb 100644 --- a/erpnext/manufacturing/doctype/bom_update_batch/bom_update_batch.json +++ b/erpnext/manufacturing/doctype/bom_update_batch/bom_update_batch.json @@ -7,7 +7,8 @@ "field_order": [ "level", "batch_no", - "boms_updated" + "boms_updated", + "status" ], "fields": [ { @@ -25,14 +26,23 @@ { "fieldname": "boms_updated", "fieldtype": "Long Text", + "hidden": 1, "in_list_view": 1, "label": "BOMs Updated" + }, + { + "fieldname": "status", + "fieldtype": "Select", + "in_list_view": 1, + "label": "Status", + "options": "Pending\nCompleted", + "read_only": 1 } ], "index_web_pages_for_search": 1, "istable": 1, "links": [], - "modified": "2022-05-31 23:36:13.628391", + "modified": "2022-06-06 14:50:35.161062", "modified_by": "Administrator", "module": "Manufacturing", "name": "BOM Update Batch", diff --git a/erpnext/manufacturing/doctype/bom_update_log/bom_update_log.json b/erpnext/manufacturing/doctype/bom_update_log/bom_update_log.json index b1c24ab9954..c32e383b08a 100644 --- a/erpnext/manufacturing/doctype/bom_update_log/bom_update_log.json +++ b/erpnext/manufacturing/doctype/bom_update_log/bom_update_log.json @@ -15,7 +15,6 @@ "error_log", "progress_section", "current_level", - "parent_boms", "processed_boms", "bom_batches", "amended_from" @@ -52,7 +51,7 @@ "fieldname": "status", "fieldtype": "Select", "label": "Status", - "options": "Queued\nIn Progress\nPaused\nCompleted\nFailed" + "options": "Queued\nIn Progress\nCompleted\nFailed" }, { "fieldname": "amended_from", @@ -76,15 +75,10 @@ "fieldtype": "Section Break", "label": "Progress" }, - { - "description": "Immediate parent BOMs", - "fieldname": "parent_boms", - "fieldtype": "Long Text", - "label": "Parent BOMs" - }, { "fieldname": "processed_boms", "fieldtype": "Long Text", + "hidden": 1, "label": "Processed BOMs" }, { @@ -102,7 +96,7 @@ "index_web_pages_for_search": 1, "is_submittable": 1, "links": [], - "modified": "2022-05-31 20:20:06.370786", + "modified": "2022-06-06 15:15:23.883251", "modified_by": "Administrator", "module": "Manufacturing", "name": "BOM Update Log", diff --git a/erpnext/manufacturing/doctype/bom_update_log/bom_update_log.py b/erpnext/manufacturing/doctype/bom_update_log/bom_update_log.py index bfae76c2b2e..d714b9d5fd0 100644 --- a/erpnext/manufacturing/doctype/bom_update_log/bom_update_log.py +++ b/erpnext/manufacturing/doctype/bom_update_log/bom_update_log.py @@ -56,7 +56,7 @@ class BOMUpdateLog(Document): wip_log = frappe.get_all( "BOM Update Log", - {"update_type": "Update Cost", "status": ["in", ["Queued", "In Progress", "Paused"]]}, + {"update_type": "Update Cost", "status": ["in", ["Queued", "In Progress"]]}, limit_page_length=1, ) if wip_log: @@ -104,10 +104,12 @@ def run_replace_bom_job( frappe.db.commit() # nosemgrep -def process_boms_cost_level_wise(update_doc: "BOMUpdateLog") -> None: +def process_boms_cost_level_wise( + update_doc: "BOMUpdateLog", parent_boms: List[str] = None +) -> None: "Queue jobs at the start of new BOM Level in 'Update Cost' Jobs." - current_boms, parent_boms = {}, [] + current_boms = {} values = {} if update_doc.status == "Queued": @@ -115,26 +117,27 @@ def process_boms_cost_level_wise(update_doc: "BOMUpdateLog") -> None: current_level = 0 current_boms = get_leaf_boms() values = { - "parent_boms": "[]", "processed_boms": json.dumps({}), "status": "In Progress", "current_level": current_level, } else: # Resume next level. via Cron Job. + if not parent_boms: + return + current_level = cint(update_doc.current_level) + 1 - parent_boms = json.loads(update_doc.parent_boms) # Process the next level BOMs. Stage parents as current BOMs. current_boms = parent_boms.copy() - values = {"parent_boms": "[]", "current_level": current_level} + values = {"current_level": current_level} set_values_in_log(update_doc.name, values, commit=True) queue_bom_cost_jobs(current_boms, update_doc, current_level) def queue_bom_cost_jobs( - current_boms_list: List, update_doc: "BOMUpdateLog", current_level: int + current_boms_list: List[str], update_doc: "BOMUpdateLog", current_level: int ) -> None: "Queue batches of 20k BOMs of the same level to process parallelly" batch_no = 0 @@ -147,7 +150,9 @@ def queue_bom_cost_jobs( # update list to exclude 20K (queued) BOMs current_boms_list = current_boms_list[batch_size:] if len(current_boms_list) > batch_size else [] - batch_row = update_doc.append("bom_batches", {"level": current_level, "batch_no": batch_no}) + batch_row = update_doc.append( + "bom_batches", {"level": current_level, "batch_no": batch_no, "status": "Pending"} + ) batch_row.db_insert() frappe.enqueue( @@ -155,7 +160,7 @@ def queue_bom_cost_jobs( doc=update_doc, bom_list=boms_to_process, batch_name=batch_row.name, - timeout=40000, + queue="long", ) @@ -181,9 +186,11 @@ def resume_bom_cost_update_jobs(): for log in in_progress_logs: # check if all log batches of current level are processed bom_batches = frappe.db.get_all( - "BOM Update Batch", {"parent": log.name, "level": log.current_level}, ["name", "boms_updated"] + "BOM Update Batch", + {"parent": log.name, "level": log.current_level}, + ["name", "boms_updated", "status"], ) - incomplete_level = any(not row.get("boms_updated") for row in bom_batches) + incomplete_level = any(row.get("status") == "Pending" for row in bom_batches) if not bom_batches or incomplete_level: continue @@ -195,14 +202,15 @@ def resume_bom_cost_update_jobs(): log.name, values={ "processed_boms": json.dumps(processed_boms), - "parent_boms": json.dumps(parent_boms), "status": "Completed" if not parent_boms else "In Progress", }, commit=True, ) if parent_boms: # there is a next level to process - process_boms_cost_level_wise(update_doc=frappe.get_doc("BOM Update Log", log.name)) + process_boms_cost_level_wise( + update_doc=frappe.get_doc("BOM Update Log", log.name), parent_boms=parent_boms + ) def get_processed_current_boms( diff --git a/erpnext/manufacturing/doctype/bom_update_log/bom_updation_utils.py b/erpnext/manufacturing/doctype/bom_update_log/bom_updation_utils.py index 2d6429b0504..49e747c4bb9 100644 --- a/erpnext/manufacturing/doctype/bom_update_log/bom_updation_utils.py +++ b/erpnext/manufacturing/doctype/bom_update_log/bom_updation_utils.py @@ -3,7 +3,7 @@ import json from collections import defaultdict -from typing import TYPE_CHECKING, Any, Dict, List, Optional +from typing import TYPE_CHECKING, Any, Dict, List, Optional, Union if TYPE_CHECKING: from erpnext.manufacturing.doctype.bom_update_log.bom_update_log import BOMUpdateLog @@ -38,7 +38,9 @@ def replace_bom(boms: Dict) -> None: bom_obj.save_version() -def update_cost_in_level(doc: "BOMUpdateLog", bom_list: List[str], batch_name: int) -> None: +def update_cost_in_level( + doc: "BOMUpdateLog", bom_list: List[str], batch_name: Union[int, str] +) -> None: "Updates Cost for BOMs within a given level. Runs via background jobs." try: @@ -49,7 +51,14 @@ def update_cost_in_level(doc: "BOMUpdateLog", bom_list: List[str], batch_name: i frappe.db.auto_commit_on_many_writes = 1 update_cost_in_boms(bom_list=bom_list) # main updation logic - frappe.db.set_value("BOM Update Batch", batch_name, "boms_updated", json.dumps(bom_list)) + + bom_batch = frappe.qb.DocType("BOM Update Batch") + ( + frappe.qb.update(bom_batch) + .set(bom_batch.boms_updated, json.dumps(bom_list)) + .set(bom_batch.status, "Completed") + .where(bom_batch.name == batch_name) + ).run() except Exception: handle_exception(doc) finally: @@ -105,14 +114,17 @@ def get_bom_unit_cost(bom_name: str) -> float: def update_cost_in_boms(bom_list: List[str]) -> None: "Updates cost in given BOMs. Returns current and total updated BOMs." - for bom in bom_list: - bom_doc = frappe.get_cached_doc("BOM", bom) + for index, bom in enumerate(bom_list): + bom_doc = frappe.get_doc("BOM", bom, for_update=True) bom_doc.calculate_cost(save_updates=True, update_hour_rate=True) bom_doc.db_update() + if index % 100 == 0: + frappe.db.commit() + def get_next_higher_level_boms( - child_boms: Dict[str, bool], processed_boms: Dict[str, bool] + child_boms: List[str], processed_boms: Dict[str, bool] ) -> List[str]: "Generate immediate higher level dependants with no unresolved dependencies (children)." diff --git a/erpnext/manufacturing/doctype/bom_update_tool/bom_update_tool.py b/erpnext/manufacturing/doctype/bom_update_tool/bom_update_tool.py index 758d8ed0ef9..d16fcd08326 100644 --- a/erpnext/manufacturing/doctype/bom_update_tool/bom_update_tool.py +++ b/erpnext/manufacturing/doctype/bom_update_tool/bom_update_tool.py @@ -40,7 +40,7 @@ def auto_update_latest_price_in_all_boms() -> None: if frappe.db.get_single_value("Manufacturing Settings", "update_bom_costs_automatically"): wip_log = frappe.get_all( "BOM Update Log", - {"update_type": "Update Cost", "status": ["in", ["Queued", "In Progress", "Paused"]]}, + {"update_type": "Update Cost", "status": ["in", ["Queued", "In Progress"]]}, limit_page_length=1, ) if not wip_log: From 15101190a6efb4a596824a1c7cba88d8363e5d17 Mon Sep 17 00:00:00 2001 From: marination Date: Mon, 6 Jun 2022 17:12:43 +0530 Subject: [PATCH 074/105] chore: `get_valuation_rate` in bom.py must always return float & goto Item master if no bins --- erpnext/manufacturing/doctype/bom/bom.py | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/erpnext/manufacturing/doctype/bom/bom.py b/erpnext/manufacturing/doctype/bom/bom.py index 1fb00ef3fe2..8bf124e058f 100644 --- a/erpnext/manufacturing/doctype/bom/bom.py +++ b/erpnext/manufacturing/doctype/bom/bom.py @@ -961,11 +961,8 @@ def get_valuation_rate(args): valuation_rate = item_valuation.get("valuation_rate") - if valuation_rate is None: - # Explicit null value check. If null, Bins don't exist, neither does SLE - return valuation_rate - - if valuation_rate <= 0: + if (valuation_rate is not None) and valuation_rate <= 0: + # Explicit null value check. If None, Bins don't exist, neither does SLE last_valuation_rate = frappe.db.sql( """select valuation_rate from `tabStock Ledger Entry` From 91f9f37d64b54625888f2642edd1b11a8cde82a6 Mon Sep 17 00:00:00 2001 From: Rucha Mahabal Date: Tue, 7 Jun 2022 09:51:30 +0530 Subject: [PATCH 075/105] fix: leave balance for earned leaves in backdated Leave Application dashboard (#31253) --- .../leave_application/leave_application.js | 2 +- .../leave_application/leave_application.py | 23 +-- .../test_leave_application.py | 139 ++++++++++++------ 3 files changed, 101 insertions(+), 63 deletions(-) diff --git a/erpnext/hr/doctype/leave_application/leave_application.js b/erpnext/hr/doctype/leave_application/leave_application.js index 85997a4087f..ee00e6719c0 100755 --- a/erpnext/hr/doctype/leave_application/leave_application.js +++ b/erpnext/hr/doctype/leave_application/leave_application.js @@ -173,7 +173,7 @@ frappe.ui.form.on("Leave Application", { date: frm.doc.from_date, to_date: frm.doc.to_date, leave_type: frm.doc.leave_type, - consider_all_leaves_in_the_allocation_period: true + consider_all_leaves_in_the_allocation_period: 1 }, callback: function (r) { if (!r.exc && r.message) { diff --git a/erpnext/hr/doctype/leave_application/leave_application.py b/erpnext/hr/doctype/leave_application/leave_application.py index 53c5df4210a..43c2bb37b21 100755 --- a/erpnext/hr/doctype/leave_application/leave_application.py +++ b/erpnext/hr/doctype/leave_application/leave_application.py @@ -757,22 +757,6 @@ def get_leave_details(employee, date): leave_allocation = {} for d in allocation_records: allocation = allocation_records.get(d, frappe._dict()) - - total_allocated_leaves = ( - frappe.db.get_value( - "Leave Allocation", - { - "from_date": ("<=", date), - "to_date": (">=", date), - "employee": employee, - "leave_type": allocation.leave_type, - "docstatus": 1, - }, - "SUM(total_leaves_allocated)", - ) - or 0 - ) - remaining_leaves = get_leave_balance_on( employee, d, date, to_date=allocation.to_date, consider_all_leaves_in_the_allocation_period=True ) @@ -782,10 +766,11 @@ def get_leave_details(employee, date): leaves_pending = get_leaves_pending_approval_for_period( employee, d, allocation.from_date, end_date ) + expired_leaves = allocation.total_leaves_allocated - (remaining_leaves + leaves_taken) leave_allocation[d] = { - "total_leaves": total_allocated_leaves, - "expired_leaves": total_allocated_leaves - (remaining_leaves + leaves_taken), + "total_leaves": allocation.total_leaves_allocated, + "expired_leaves": expired_leaves if expired_leaves > 0 else 0, "leaves_taken": leaves_taken, "leaves_pending_approval": leaves_pending, "remaining_leaves": remaining_leaves, @@ -830,7 +815,7 @@ def get_leave_balance_on( allocation_records = get_leave_allocation_records(employee, date, leave_type) allocation = allocation_records.get(leave_type, frappe._dict()) - end_date = allocation.to_date if consider_all_leaves_in_the_allocation_period else date + end_date = allocation.to_date if cint(consider_all_leaves_in_the_allocation_period) else date cf_expiry = get_allocation_expiry_for_cf_leaves(employee, leave_type, to_date, date) leaves_taken = get_leaves_for_period(employee, leave_type, allocation.from_date, end_date) diff --git a/erpnext/hr/doctype/leave_application/test_leave_application.py b/erpnext/hr/doctype/leave_application/test_leave_application.py index 7506c611083..27c54109dea 100644 --- a/erpnext/hr/doctype/leave_application/test_leave_application.py +++ b/erpnext/hr/doctype/leave_application/test_leave_application.py @@ -76,7 +76,14 @@ _test_records = [ class TestLeaveApplication(unittest.TestCase): def setUp(self): - for dt in ["Leave Application", "Leave Allocation", "Salary Slip", "Leave Ledger Entry"]: + for dt in [ + "Leave Application", + "Leave Allocation", + "Salary Slip", + "Leave Ledger Entry", + "Leave Period", + "Leave Policy Assignment", + ]: frappe.db.delete(dt) frappe.set_user("Administrator") @@ -702,59 +709,24 @@ class TestLeaveApplication(unittest.TestCase): self.assertEqual(details.leave_balance, 30) def test_earned_leaves_creation(self): - - frappe.db.sql("""delete from `tabLeave Period`""") - frappe.db.sql("""delete from `tabLeave Policy Assignment`""") - frappe.db.sql("""delete from `tabLeave Allocation`""") - frappe.db.sql("""delete from `tabLeave Ledger Entry`""") + from erpnext.hr.utils import allocate_earned_leaves leave_period = get_leave_period() employee = get_employee() leave_type = "Test Earned Leave Type" - frappe.delete_doc_if_exists("Leave Type", "Test Earned Leave Type", force=1) - frappe.get_doc( - dict( - leave_type_name=leave_type, - doctype="Leave Type", - is_earned_leave=1, - earned_leave_frequency="Monthly", - rounding=0.5, - max_leaves_allowed=6, - ) - ).insert() + make_policy_assignment(employee, leave_type, leave_period) - leave_policy = frappe.get_doc( - { - "doctype": "Leave Policy", - "title": "Test Leave Policy", - "leave_policy_details": [{"leave_type": leave_type, "annual_allocation": 6}], - } - ).insert() - - data = { - "assignment_based_on": "Leave Period", - "leave_policy": leave_policy.name, - "leave_period": leave_period.name, - } - - leave_policy_assignments = create_assignment_for_multiple_employees( - [employee.name], frappe._dict(data) - ) - - from erpnext.hr.utils import allocate_earned_leaves - - i = 0 - while i < 14: + for i in range(0, 14): allocate_earned_leaves() - i += 1 + self.assertEqual(get_leave_balance_on(employee.name, leave_type, nowdate()), 6) # validate earned leaves creation without maximum leaves frappe.db.set_value("Leave Type", leave_type, "max_leaves_allowed", 0) - i = 0 - while i < 6: + + for i in range(0, 6): allocate_earned_leaves() - i += 1 + self.assertEqual(get_leave_balance_on(employee.name, leave_type, nowdate()), 9) # test to not consider current leave in leave balance while submitting @@ -970,6 +942,54 @@ class TestLeaveApplication(unittest.TestCase): self.assertEqual(leave_allocation["leaves_pending_approval"], 1) self.assertEqual(leave_allocation["remaining_leaves"], 26) + @set_holiday_list("Salary Slip Test Holiday List", "_Test Company") + def test_get_earned_leave_details_for_dashboard(self): + from erpnext.hr.utils import allocate_earned_leaves + + leave_period = get_leave_period() + employee = get_employee() + leave_type = "Test Earned Leave Type" + leave_policy_assignments = make_policy_assignment(employee, leave_type, leave_period) + allocation = frappe.db.get_value( + "Leave Allocation", + {"leave_policy_assignment": leave_policy_assignments[0]}, + "name", + ) + allocation = frappe.get_doc("Leave Allocation", allocation) + allocation.new_leaves_allocated = 2 + allocation.save() + + for i in range(0, 6): + allocate_earned_leaves() + + first_sunday = get_first_sunday(self.holiday_list) + make_leave_application( + employee.name, add_days(first_sunday, 1), add_days(first_sunday, 1), leave_type + ) + + details = get_leave_details(employee.name, allocation.from_date) + leave_allocation = details["leave_allocation"][leave_type] + expected = { + "total_leaves": 2.0, + "expired_leaves": 0.0, + "leaves_taken": 1.0, + "leaves_pending_approval": 0.0, + "remaining_leaves": 1.0, + } + self.assertEqual(leave_allocation, expected) + + details = get_leave_details(employee.name, getdate()) + leave_allocation = details["leave_allocation"][leave_type] + + expected = { + "total_leaves": 5.0, + "expired_leaves": 0.0, + "leaves_taken": 1.0, + "leaves_pending_approval": 0.0, + "remaining_leaves": 4.0, + } + self.assertEqual(leave_allocation, expected) + @set_holiday_list("Salary Slip Test Holiday List", "_Test Company") def test_get_leave_allocation_records(self): employee = get_employee() @@ -1100,3 +1120,36 @@ def get_first_sunday(holiday_list, for_date=None): )[0][0] return first_sunday + + +def make_policy_assignment(employee, leave_type, leave_period): + frappe.delete_doc_if_exists("Leave Type", leave_type, force=1) + frappe.get_doc( + dict( + leave_type_name=leave_type, + doctype="Leave Type", + is_earned_leave=1, + earned_leave_frequency="Monthly", + rounding=0.5, + max_leaves_allowed=6, + ) + ).insert() + + leave_policy = frappe.get_doc( + { + "doctype": "Leave Policy", + "title": "Test Leave Policy", + "leave_policy_details": [{"leave_type": leave_type, "annual_allocation": 6}], + } + ).insert() + + data = { + "assignment_based_on": "Leave Period", + "leave_policy": leave_policy.name, + "leave_period": leave_period.name, + } + + leave_policy_assignments = create_assignment_for_multiple_employees( + [employee.name], frappe._dict(data) + ) + return leave_policy_assignments From c3f2201c45fa2bdbffdd31ba506120cc73658bfd Mon Sep 17 00:00:00 2001 From: Ankush Menat Date: Tue, 7 Jun 2022 10:04:35 +0530 Subject: [PATCH 076/105] chore(meta): apply stale rules to pull only --- .github/stale.yml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.github/stale.yml b/.github/stale.yml index cdce0aee082..da15d32680f 100644 --- a/.github/stale.yml +++ b/.github/stale.yml @@ -23,3 +23,5 @@ pulls: activity occurs, but it only takes a comment to keep a contribution alive :) Also, even if it is closed, you can always reopen the PR when you're ready. Thank you for contributing. + +only: pulls From dc8e80ea815d5684b56376330500f8dccdd38816 Mon Sep 17 00:00:00 2001 From: Deepesh Garg Date: Tue, 7 Jun 2022 11:35:03 +0530 Subject: [PATCH 077/105] test: Add test coverage for cancellation --- .../purchase_invoice/test_purchase_invoice.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/erpnext/accounts/doctype/purchase_invoice/test_purchase_invoice.py b/erpnext/accounts/doctype/purchase_invoice/test_purchase_invoice.py index 30d26acf3a2..9b7b88973fa 100644 --- a/erpnext/accounts/doctype/purchase_invoice/test_purchase_invoice.py +++ b/erpnext/accounts/doctype/purchase_invoice/test_purchase_invoice.py @@ -1526,6 +1526,18 @@ class TestPurchaseInvoice(unittest.TestCase): check_gl_entries(self, pr.name, expected_gle_for_purchase_receipt, pr.posting_date) + # Cancel purchase invoice to check reverse provisional entry cancellation + pi.cancel() + + expected_gle_for_purchase_receipt_post_pi_cancel = [ + ["Provision Account - _TC", 0, 250, pi.posting_date], + ["_Test Account Cost for Goods Sold - _TC", 250, 0, pi.posting_date], + ] + + check_gl_entries( + self, pr.name, expected_gle_for_purchase_receipt_post_pi_cancel, pr.posting_date + ) + company.enable_provisional_accounting_for_non_stock_items = 0 company.save() From 293eb8d722c773864eef6ef45ce36a8bda25340e Mon Sep 17 00:00:00 2001 From: Ankush Menat Date: Tue, 10 May 2022 21:30:31 +0530 Subject: [PATCH 078/105] test: create stock test mixin for assertion/utils --- .../doctype/stock_entry/stock_entry_utils.py | 26 +++++++++++ .../test_stock_ledger_entry.py | 27 +----------- .../test_stock_reconciliation.py | 15 ++++--- erpnext/stock/tests/test_utils.py | 44 ++++++++++++++++--- 4 files changed, 75 insertions(+), 37 deletions(-) diff --git a/erpnext/stock/doctype/stock_entry/stock_entry_utils.py b/erpnext/stock/doctype/stock_entry/stock_entry_utils.py index c5c0cefe51c..41a3b8916de 100644 --- a/erpnext/stock/doctype/stock_entry/stock_entry_utils.py +++ b/erpnext/stock/doctype/stock_entry/stock_entry_utils.py @@ -2,11 +2,37 @@ # See license.txt +from typing import TYPE_CHECKING, Optional, overload + import frappe from frappe.utils import cint, flt import erpnext +if TYPE_CHECKING: + from erpnext.stock.doctype.stock_entry.stock_entry import StockEntry + + +@overload +def make_stock_entry( + *, + item_code: str, + qty: float, + company: Optional[str] = None, + from_warehouse: Optional[str] = None, + to_warehouse: Optional[str] = None, + rate: Optional[float] = None, + serial_no: Optional[str] = None, + batch_no: Optional[str] = None, + posting_date: Optional[str] = None, + posting_time: Optional[str] = None, + purpose: Optional[str] = None, + do_not_save: bool = False, + do_not_submit: bool = False, + inspection_required: bool = False, +) -> "StockEntry": + ... + @frappe.whitelist() def make_stock_entry(**args): diff --git a/erpnext/stock/doctype/stock_ledger_entry/test_stock_ledger_entry.py b/erpnext/stock/doctype/stock_ledger_entry/test_stock_ledger_entry.py index eb1e0fc25fd..55a213ccc3f 100644 --- a/erpnext/stock/doctype/stock_ledger_entry/test_stock_ledger_entry.py +++ b/erpnext/stock/doctype/stock_ledger_entry/test_stock_ledger_entry.py @@ -24,9 +24,10 @@ from erpnext.stock.doctype.stock_reconciliation.test_stock_reconciliation import create_stock_reconciliation, ) from erpnext.stock.stock_ledger import get_previous_sle +from erpnext.stock.tests.test_utils import StockTestMixin -class TestStockLedgerEntry(FrappeTestCase): +class TestStockLedgerEntry(FrappeTestCase, StockTestMixin): def setUp(self): items = create_items() reset("Stock Entry") @@ -541,30 +542,6 @@ class TestStockLedgerEntry(FrappeTestCase): "Incorrect 'Incoming Rate' values fetched for DN items", ) - def assertSLEs(self, doc, expected_sles, sle_filters=None): - """Compare sorted SLEs, useful for vouchers that create multiple SLEs for same line""" - - filters = {"voucher_no": doc.name, "voucher_type": doc.doctype, "is_cancelled": 0} - if sle_filters: - filters.update(sle_filters) - sles = frappe.get_all( - "Stock Ledger Entry", - fields=["*"], - filters=filters, - order_by="timestamp(posting_date, posting_time), creation", - ) - - for exp_sle, act_sle in zip(expected_sles, sles): - for k, v in exp_sle.items(): - act_value = act_sle[k] - if k == "stock_queue": - act_value = json.loads(act_value) - if act_value and act_value[0][0] == 0: - # ignore empty fifo bins - continue - - self.assertEqual(v, act_value, msg=f"{k} doesn't match \n{exp_sle}\n{act_sle}") - def test_batchwise_item_valuation_stock_reco(self): item, warehouses, batches = setup_item_valuation_test() state = {"stock_value": 0.0, "qty": 0.0} diff --git a/erpnext/stock/doctype/stock_reconciliation/test_stock_reconciliation.py b/erpnext/stock/doctype/stock_reconciliation/test_stock_reconciliation.py index 9088eb802b5..191c03f5f1c 100644 --- a/erpnext/stock/doctype/stock_reconciliation/test_stock_reconciliation.py +++ b/erpnext/stock/doctype/stock_reconciliation/test_stock_reconciliation.py @@ -10,7 +10,7 @@ from frappe.tests.utils import FrappeTestCase, change_settings from frappe.utils import add_days, cstr, flt, nowdate, nowtime, random_string from erpnext.accounts.utils import get_stock_and_account_balance -from erpnext.stock.doctype.item.test_item import create_item, make_item +from erpnext.stock.doctype.item.test_item import create_item from erpnext.stock.doctype.purchase_receipt.test_purchase_receipt import make_purchase_receipt from erpnext.stock.doctype.serial_no.serial_no import get_serial_nos from erpnext.stock.doctype.stock_reconciliation.stock_reconciliation import ( @@ -19,10 +19,11 @@ from erpnext.stock.doctype.stock_reconciliation.stock_reconciliation import ( ) from erpnext.stock.doctype.warehouse.test_warehouse import create_warehouse from erpnext.stock.stock_ledger import get_previous_sle, update_entries_after +from erpnext.stock.tests.test_utils import StockTestMixin from erpnext.stock.utils import get_incoming_rate, get_stock_value_on, get_valuation_method -class TestStockReconciliation(FrappeTestCase): +class TestStockReconciliation(FrappeTestCase, StockTestMixin): @classmethod def setUpClass(cls): create_batch_or_serial_no_items() @@ -40,7 +41,7 @@ class TestStockReconciliation(FrappeTestCase): self._test_reco_sle_gle("Moving Average") def _test_reco_sle_gle(self, valuation_method): - item_code = make_item(properties={"valuation_method": valuation_method}).name + item_code = self.make_item(properties={"valuation_method": valuation_method}).name se1, se2, se3 = insert_existing_sle(warehouse="Stores - TCP1", item_code=item_code) company = frappe.db.get_value("Warehouse", "Stores - TCP1", "company") @@ -392,7 +393,7 @@ class TestStockReconciliation(FrappeTestCase): SR4 | Reco | 0 | 6 (posting date: today-1) [backdated] PR3 | PR | 1 | 7 (posting date: today) # can't post future PR """ - item_code = make_item().name + item_code = self.make_item().name warehouse = "_Test Warehouse - _TC" frappe.flags.dont_execute_stock_reposts = True @@ -458,7 +459,7 @@ class TestStockReconciliation(FrappeTestCase): from erpnext.stock.doctype.delivery_note.test_delivery_note import create_delivery_note from erpnext.stock.stock_ledger import NegativeStockError - item_code = make_item().name + item_code = self.make_item().name warehouse = "_Test Warehouse - _TC" pr1 = make_purchase_receipt( @@ -506,7 +507,7 @@ class TestStockReconciliation(FrappeTestCase): from erpnext.stock.doctype.delivery_note.test_delivery_note import create_delivery_note from erpnext.stock.stock_ledger import NegativeStockError - item_code = make_item().name + item_code = self.make_item().name warehouse = "_Test Warehouse - _TC" sr = create_stock_reconciliation( @@ -549,7 +550,7 @@ class TestStockReconciliation(FrappeTestCase): # repost will make this test useless, qty should update in realtime without reposts frappe.flags.dont_execute_stock_reposts = True - item_code = make_item().name + item_code = self.make_item().name warehouse = "_Test Warehouse - _TC" sr = create_stock_reconciliation( diff --git a/erpnext/stock/tests/test_utils.py b/erpnext/stock/tests/test_utils.py index 9ee0c9f3b5a..e07e08c79e5 100644 --- a/erpnext/stock/tests/test_utils.py +++ b/erpnext/stock/tests/test_utils.py @@ -1,16 +1,50 @@ +import json + import frappe from frappe.tests.utils import FrappeTestCase -from erpnext.stock.doctype.item.test_item import make_item from erpnext.stock.utils import scan_barcode -class TestStockUtilities(FrappeTestCase): +class StockTestMixin: + """Mixin to simplfy stock ledger tests, useful for all stock transactions.""" + + def make_item(self, item_code=None, properties=None, *args, **kwargs): + from erpnext.stock.doctype.item.test_item import make_item + + return make_item(item_code, properties, *args, **kwargs) + + def assertSLEs(self, doc, expected_sles, sle_filters=None): + """Compare sorted SLEs, useful for vouchers that create multiple SLEs for same line""" + + filters = {"voucher_no": doc.name, "voucher_type": doc.doctype, "is_cancelled": 0} + if sle_filters: + filters.update(sle_filters) + sles = frappe.get_all( + "Stock Ledger Entry", + fields=["*"], + filters=filters, + order_by="timestamp(posting_date, posting_time), creation", + ) + + for exp_sle, act_sle in zip(expected_sles, sles): + for k, v in exp_sle.items(): + act_value = act_sle[k] + if k == "stock_queue": + act_value = json.loads(act_value) + if act_value and act_value[0][0] == 0: + # ignore empty fifo bins + continue + + self.assertEqual(v, act_value, msg=f"{k} doesn't match \n{exp_sle}\n{act_sle}") + + +class TestStockUtilities(FrappeTestCase, StockTestMixin): def test_barcode_scanning(self): - simple_item = make_item(properties={"barcodes": [{"barcode": "12399"}]}) + simple_item = self.make_item(properties={"barcodes": [{"barcode": "12399"}]}) self.assertEqual(scan_barcode("12399")["item_code"], simple_item.name) - batch_item = make_item(properties={"has_batch_no": 1, "create_new_batch": 1}) + batch_item = self.make_item(properties={"has_batch_no": 1, "create_new_batch": 1}) batch = frappe.get_doc(doctype="Batch", item=batch_item.name).insert() batch_scan = scan_barcode(batch.name) @@ -19,7 +53,7 @@ class TestStockUtilities(FrappeTestCase): self.assertEqual(batch_scan["has_batch_no"], 1) self.assertEqual(batch_scan["has_serial_no"], 0) - serial_item = make_item(properties={"has_serial_no": 1}) + serial_item = self.make_item(properties={"has_serial_no": 1}) serial = frappe.get_doc( doctype="Serial No", item_code=serial_item.name, serial_no=frappe.generate_hash() ).insert() From 7726271e2ac6776b29f795e6e54dd76aa6d581b8 Mon Sep 17 00:00:00 2001 From: Ankush Menat Date: Wed, 1 Jun 2022 14:17:06 +0530 Subject: [PATCH 079/105] fix: purchase invoice return GLe voucher_wise_stock_value contains tuples and the condition was looking for string, so it's never triggered. Caused by https://github.com/frappe/erpnext/pull/24200 --- .../purchase_invoice/purchase_invoice.py | 2 +- .../purchase_invoice/test_purchase_invoice.py | 77 ++++++++++++++++++- .../controllers/sales_and_purchase_return.py | 2 +- erpnext/stock/tests/test_utils.py | 17 ++++ 4 files changed, 95 insertions(+), 3 deletions(-) diff --git a/erpnext/accounts/doctype/purchase_invoice/purchase_invoice.py b/erpnext/accounts/doctype/purchase_invoice/purchase_invoice.py index e6da6669ac2..5d11dffdef8 100644 --- a/erpnext/accounts/doctype/purchase_invoice/purchase_invoice.py +++ b/erpnext/accounts/doctype/purchase_invoice/purchase_invoice.py @@ -1127,7 +1127,7 @@ class PurchaseInvoice(BuyingController): # Stock ledger value is not matching with the warehouse amount if ( self.update_stock - and voucher_wise_stock_value.get(item.name) + and voucher_wise_stock_value.get((item.name, item.warehouse)) and warehouse_debit_amount != flt(voucher_wise_stock_value.get((item.name, item.warehouse)), net_amt_precision) ): diff --git a/erpnext/accounts/doctype/purchase_invoice/test_purchase_invoice.py b/erpnext/accounts/doctype/purchase_invoice/test_purchase_invoice.py index 30d26acf3a2..9c8a6dd6b26 100644 --- a/erpnext/accounts/doctype/purchase_invoice/test_purchase_invoice.py +++ b/erpnext/accounts/doctype/purchase_invoice/test_purchase_invoice.py @@ -27,12 +27,13 @@ from erpnext.stock.doctype.purchase_receipt.test_purchase_receipt import ( make_purchase_receipt, ) from erpnext.stock.doctype.stock_entry.test_stock_entry import get_qty_after_transaction +from erpnext.stock.tests.test_utils import StockTestMixin test_dependencies = ["Item", "Cost Center", "Payment Term", "Payment Terms Template"] test_ignore = ["Serial No"] -class TestPurchaseInvoice(unittest.TestCase): +class TestPurchaseInvoice(unittest.TestCase, StockTestMixin): @classmethod def setUpClass(self): unlink_payment_on_cancel_of_invoice() @@ -693,6 +694,80 @@ class TestPurchaseInvoice(unittest.TestCase): self.assertEqual(expected_values[gle.account][0], gle.debit) self.assertEqual(expected_values[gle.account][1], gle.credit) + def test_standalone_return_using_pi(self): + from erpnext.stock.doctype.stock_entry.test_stock_entry import make_stock_entry + + item = self.make_item().name + company = "_Test Company with perpetual inventory" + warehouse = "Stores - TCP1" + + make_stock_entry(item_code=item, target=warehouse, qty=50, rate=120) + + return_pi = make_purchase_invoice( + is_return=1, + item=item, + qty=-10, + update_stock=1, + rate=100, + company=company, + warehouse=warehouse, + cost_center="Main - TCP1", + ) + + # assert that stock consumption is with actual rate + self.assertGLEs( + return_pi, + [{"credit": 1200, "debit": 0}], + gle_filters={"account": "Stock In Hand - TCP1"}, + ) + + # assert loss booked in COGS + self.assertGLEs( + return_pi, + [{"credit": 0, "debit": 200}], + gle_filters={"account": "Cost of Goods Sold - TCP1"}, + ) + + def test_return_with_lcv(self): + from erpnext.controllers.sales_and_purchase_return import make_return_doc + from erpnext.stock.doctype.landed_cost_voucher.test_landed_cost_voucher import ( + create_landed_cost_voucher, + ) + + item = self.make_item().name + company = "_Test Company with perpetual inventory" + warehouse = "Stores - TCP1" + cost_center = "Main - TCP1" + + pi = make_purchase_invoice( + item=item, + company=company, + warehouse=warehouse, + cost_center=cost_center, + update_stock=1, + qty=10, + rate=100, + ) + + # Create landed cost voucher - will increase valuation of received item by 10 + create_landed_cost_voucher("Purchase Invoice", pi.name, pi.company, charges=100) + return_pi = make_return_doc(pi.doctype, pi.name) + return_pi.save().submit() + + # assert that stock consumption is with actual in rate + self.assertGLEs( + return_pi, + [{"credit": 1100, "debit": 0}], + gle_filters={"account": "Stock In Hand - TCP1"}, + ) + + # assert loss booked in COGS + self.assertGLEs( + return_pi, + [{"credit": 0, "debit": 100}], + gle_filters={"account": "Cost of Goods Sold - TCP1"}, + ) + def test_multi_currency_gle(self): pi = make_purchase_invoice( supplier="_Test Supplier USD", diff --git a/erpnext/controllers/sales_and_purchase_return.py b/erpnext/controllers/sales_and_purchase_return.py index bd4b59b3855..d24ac3f2cf3 100644 --- a/erpnext/controllers/sales_and_purchase_return.py +++ b/erpnext/controllers/sales_and_purchase_return.py @@ -316,7 +316,7 @@ def get_returned_qty_map_for_row(return_against, party, row_name, doctype): return data[0] -def make_return_doc(doctype, source_name, target_doc=None): +def make_return_doc(doctype: str, source_name: str, target_doc=None): from frappe.model.mapper import get_mapped_doc from erpnext.stock.doctype.serial_no.serial_no import get_serial_nos diff --git a/erpnext/stock/tests/test_utils.py b/erpnext/stock/tests/test_utils.py index e07e08c79e5..b046dbda24f 100644 --- a/erpnext/stock/tests/test_utils.py +++ b/erpnext/stock/tests/test_utils.py @@ -38,6 +38,23 @@ class StockTestMixin: self.assertEqual(v, act_value, msg=f"{k} doesn't match \n{exp_sle}\n{act_sle}") + def assertGLEs(self, doc, expected_gles, gle_filters=None, order_by=None): + filters = {"voucher_no": doc.name, "voucher_type": doc.doctype, "is_cancelled": 0} + + if gle_filters: + filters.update(gle_filters) + actual_gles = frappe.get_all( + "GL Entry", + fields=["*"], + filters=filters, + order_by=order_by or "posting_date, creation", + ) + + for exp_gle, act_gle in zip(expected_gles, actual_gles): + for k, exp_value in exp_gle.items(): + act_value = act_gle[k] + self.assertEqual(exp_value, act_value, msg=f"{k} doesn't match \n{exp_gle}\n{act_gle}") + class TestStockUtilities(FrappeTestCase, StockTestMixin): def test_barcode_scanning(self): From 815141bf57b3f7710993c0d0d871ea2457d0488f Mon Sep 17 00:00:00 2001 From: Deepesh Garg Date: Tue, 7 Jun 2022 13:16:06 +0530 Subject: [PATCH 080/105] fix: Close unsecured terms loans --- erpnext/loan_management/doctype/loan/loan.js | 18 ++++++++++++++++ erpnext/loan_management/doctype/loan/loan.py | 22 +++++++++++++++++--- 2 files changed, 37 insertions(+), 3 deletions(-) diff --git a/erpnext/loan_management/doctype/loan/loan.js b/erpnext/loan_management/doctype/loan/loan.js index 940a1bbc000..38328e69674 100644 --- a/erpnext/loan_management/doctype/loan/loan.js +++ b/erpnext/loan_management/doctype/loan/loan.js @@ -93,6 +93,12 @@ frappe.ui.form.on('Loan', { frm.trigger("make_loan_refund"); },__('Create')); } + + if (frm.doc.status == "Loan Closure Requested" && frm.doc.is_term_loan && !frm.doc.is_secured_loan) { + frm.add_custom_button(__('Close Loan'), function() { + frm.trigger("close_unsecured_term_loan"); + },__('Status')); + } } frm.trigger("toggle_fields"); }, @@ -174,6 +180,18 @@ frappe.ui.form.on('Loan', { }) }, + close_unsecured_term_loan: function(frm) { + frappe.call({ + args: { + "loan": frm.doc.name + }, + method: "erpnext.loan_management.doctype.loan.loan.close_unsecured_term_loan", + callback: function () { + frm.refresh(); + } + }) + }, + request_loan_closure: function(frm) { frappe.confirm(__("Do you really want to close this loan"), function() { diff --git a/erpnext/loan_management/doctype/loan/loan.py b/erpnext/loan_management/doctype/loan/loan.py index 3b76ba4edb6..ac8b3629d97 100644 --- a/erpnext/loan_management/doctype/loan/loan.py +++ b/erpnext/loan_management/doctype/loan/loan.py @@ -60,11 +60,11 @@ class Loan(AccountsController): ) def validate_cost_center(self): - if not self.cost_center and self.rate_of_interest != 0: + if not self.cost_center and self.rate_of_interest != 0.0: self.cost_center = frappe.db.get_value("Company", self.company, "cost_center") - if not self.cost_center: - frappe.throw(_("Cost center is mandatory for loans having rate of interest greater than 0")) + if not self.cost_center: + frappe.throw(_("Cost center is mandatory for loans having rate of interest greater than 0")) def on_submit(self): self.link_loan_security_pledge() @@ -342,6 +342,22 @@ def get_loan_application(loan_application): return loan.as_dict() +@frappe.whitelist() +def close_unsecured_term_loan(loan): + loan_details = frappe.db.get_value( + "Loan", {"name": loan}, ["status", "is_term_loan", "is_secured_loan"], as_dict=1 + ) + + if ( + loan_details.status == "Loan Closure Requested" + and loan_details.is_term_loan + and not loan_details.is_secured_loan + ): + frappe.db.set_value("Loan", loan, "status", "Closed") + else: + frappe.throw(_("Cannot close this loan until full repayment")) + + def close_loan(loan, total_amount_paid): frappe.db.set_value("Loan", loan, "total_amount_paid", total_amount_paid) frappe.db.set_value("Loan", loan, "status", "Closed") From 6bde1bb5d2446c3ed08f566060c321664ad1d4e4 Mon Sep 17 00:00:00 2001 From: marination Date: Tue, 7 Jun 2022 14:44:00 +0530 Subject: [PATCH 081/105] test: Util to update cost in all BOMs - Utility to update cost in all BOMs without cron jobs or background jobs (run immediately) - Re-use util wherever all bom costs are to be updated - Skip explicit commits if in test - Specify company in test records (dirty data sometimes, company wh mismatch) - Skip background jobs queueing if in test --- erpnext/manufacturing/doctype/bom/test_bom.py | 6 +- .../doctype/bom/test_records.json | 1 + .../doctype/bom_update_log/bom_update_log.py | 21 +++- .../bom_update_log/bom_updation_utils.py | 10 +- .../bom_update_log/test_bom_update_log.py | 97 ++++++++++++------- .../bom_update_tool/test_bom_update_tool.py | 14 +-- 6 files changed, 97 insertions(+), 52 deletions(-) diff --git a/erpnext/manufacturing/doctype/bom/test_bom.py b/erpnext/manufacturing/doctype/bom/test_bom.py index 62fc0724e03..bc1bea7389e 100644 --- a/erpnext/manufacturing/doctype/bom/test_bom.py +++ b/erpnext/manufacturing/doctype/bom/test_bom.py @@ -11,7 +11,9 @@ from frappe.utils import cstr, flt from erpnext.buying.doctype.purchase_order.test_purchase_order import create_purchase_order from erpnext.manufacturing.doctype.bom.bom import item_query, make_variant_bom -from erpnext.manufacturing.doctype.bom_update_tool.bom_update_tool import update_cost +from erpnext.manufacturing.doctype.bom_update_log.test_bom_update_log import ( + update_cost_in_all_boms_in_test, +) from erpnext.stock.doctype.item.test_item import make_item from erpnext.stock.doctype.stock_reconciliation.test_stock_reconciliation import ( create_stock_reconciliation, @@ -80,7 +82,7 @@ class TestBOM(FrappeTestCase): reset_item_valuation_rate(item_code="_Test Item 2", qty=200, rate=rm_rate + 10) # update cost of all BOMs based on latest valuation rate - update_cost() + update_cost_in_all_boms_in_test() # check if new valuation rate updated in all BOMs for d in frappe.db.sql( diff --git a/erpnext/manufacturing/doctype/bom/test_records.json b/erpnext/manufacturing/doctype/bom/test_records.json index 25730f9b9f4..507d319b515 100644 --- a/erpnext/manufacturing/doctype/bom/test_records.json +++ b/erpnext/manufacturing/doctype/bom/test_records.json @@ -32,6 +32,7 @@ "is_active": 1, "is_default": 1, "item": "_Test Item Home Desktop Manufactured", + "company": "_Test Company", "quantity": 1.0 }, { diff --git a/erpnext/manufacturing/doctype/bom_update_log/bom_update_log.py b/erpnext/manufacturing/doctype/bom_update_log/bom_update_log.py index d714b9d5fd0..71430bd57e4 100644 --- a/erpnext/manufacturing/doctype/bom_update_log/bom_update_log.py +++ b/erpnext/manufacturing/doctype/bom_update_log/bom_update_log.py @@ -1,7 +1,7 @@ # Copyright (c) 2022, Frappe Technologies Pvt. Ltd. and contributors # For license information, please see license.txt import json -from typing import Any, Dict, List, Optional, Tuple +from typing import Any, Dict, List, Optional, Tuple, Union import frappe from frappe import _ @@ -101,12 +101,14 @@ def run_replace_bom_job( handle_exception(doc) finally: frappe.db.auto_commit_on_many_writes = 0 - frappe.db.commit() # nosemgrep + + if not frappe.flags.in_test: + frappe.db.commit() # nosemgrep def process_boms_cost_level_wise( update_doc: "BOMUpdateLog", parent_boms: List[str] = None -) -> None: +) -> Union[None, Tuple]: "Queue jobs at the start of new BOM Level in 'Update Cost' Jobs." current_boms = {} @@ -133,6 +135,10 @@ def process_boms_cost_level_wise( values = {"current_level": current_level} set_values_in_log(update_doc.name, values, commit=True) + + if frappe.flags.in_test: + return current_boms, current_level + queue_bom_cost_jobs(current_boms, update_doc, current_level) @@ -155,6 +161,10 @@ def queue_bom_cost_jobs( ) batch_row.db_insert() + if frappe.flags.in_test: + # skip background jobs in test + return boms_to_process, batch_row.name + frappe.enqueue( method="erpnext.manufacturing.doctype.bom_update_log.bom_updation_utils.update_cost_in_level", doc=update_doc, @@ -216,7 +226,10 @@ def resume_bom_cost_update_jobs(): def get_processed_current_boms( log: Dict[str, Any], bom_batches: Dict[str, Any] ) -> Tuple[List[str], Dict[str, Any]]: - "Aggregate all BOMs from BOM Update Batch rows into 'processed_boms' field and into current boms list." + """ + Aggregate all BOMs from BOM Update Batch rows into 'processed_boms' field + and into current boms list. + """ processed_boms = json.loads(log.processed_boms) if log.processed_boms else {} current_boms = [] diff --git a/erpnext/manufacturing/doctype/bom_update_log/bom_updation_utils.py b/erpnext/manufacturing/doctype/bom_update_log/bom_updation_utils.py index 49e747c4bb9..dde1e4ed759 100644 --- a/erpnext/manufacturing/doctype/bom_update_log/bom_updation_utils.py +++ b/erpnext/manufacturing/doctype/bom_update_log/bom_updation_utils.py @@ -63,7 +63,9 @@ def update_cost_in_level( handle_exception(doc) finally: frappe.db.auto_commit_on_many_writes = 0 - frappe.db.commit() # nosemgrep + + if not frappe.flags.in_test: + frappe.db.commit() # nosemgrep def get_ancestor_boms(new_bom: str, bom_list: Optional[List] = None) -> List: @@ -119,8 +121,8 @@ def update_cost_in_boms(bom_list: List[str]) -> None: bom_doc.calculate_cost(save_updates=True, update_hour_rate=True) bom_doc.db_update() - if index % 100 == 0: - frappe.db.commit() + if (index % 100 == 0) and not frappe.flags.in_test: + frappe.db.commit() # nosemgrep def get_next_higher_level_boms( @@ -210,7 +212,7 @@ def set_values_in_log(log_name: str, values: Dict[str, Any], commit: bool = Fals query = query.set(key, value) query.run() - if commit: + if commit and not frappe.flags.in_test: frappe.db.commit() # nosemgrep diff --git a/erpnext/manufacturing/doctype/bom_update_log/test_bom_update_log.py b/erpnext/manufacturing/doctype/bom_update_log/test_bom_update_log.py index 4f151334a2a..d770f6c56a5 100644 --- a/erpnext/manufacturing/doctype/bom_update_log/test_bom_update_log.py +++ b/erpnext/manufacturing/doctype/bom_update_log/test_bom_update_log.py @@ -1,14 +1,27 @@ # Copyright (c) 2022, Frappe Technologies Pvt. Ltd. and Contributors # See license.txt +import json + import frappe from frappe.tests.utils import FrappeTestCase from erpnext.manufacturing.doctype.bom_update_log.bom_update_log import ( BOMMissingError, + get_processed_current_boms, + process_boms_cost_level_wise, + queue_bom_cost_jobs, run_replace_bom_job, ) -from erpnext.manufacturing.doctype.bom_update_tool.bom_update_tool import enqueue_replace_bom +from erpnext.manufacturing.doctype.bom_update_log.bom_updation_utils import ( + get_next_higher_level_boms, + set_values_in_log, + update_cost_in_level, +) +from erpnext.manufacturing.doctype.bom_update_tool.bom_update_tool import ( + enqueue_replace_bom, + enqueue_update_cost, +) test_records = frappe.get_test_records("BOM") @@ -31,17 +44,12 @@ class TestBOMUpdateLog(FrappeTestCase): def tearDown(self): frappe.db.rollback() - if self._testMethodName == "test_bom_update_log_completion": - # clear logs and delete BOM created via setUp - frappe.db.delete("BOM Update Log") - self.new_bom_doc.cancel() - self.new_bom_doc.delete() - - # explicitly commit and restore to original state - frappe.db.commit() # nosemgrep - def test_bom_update_log_validate(self): - "Test if BOM presence is validated." + """ + 1) Test if BOM presence is validated. + 2) Test if same BOMs are validated. + 3) Test of non-existent BOM is validated. + """ with self.assertRaises(BOMMissingError): enqueue_replace_bom(boms={}) @@ -55,9 +63,7 @@ class TestBOMUpdateLog(FrappeTestCase): def test_bom_update_log_queueing(self): "Test if BOM Update Log is created and queued." - log = enqueue_replace_bom( - boms=self.boms, - ) + log = enqueue_replace_bom(boms=self.boms) self.assertEqual(log.docstatus, 1) self.assertEqual(log.status, "Queued") @@ -65,32 +71,51 @@ class TestBOMUpdateLog(FrappeTestCase): def test_bom_update_log_completion(self): "Test if BOM Update Log handles job completion correctly." - log = enqueue_replace_bom( - boms=self.boms, - ) + log = enqueue_replace_bom(boms=self.boms) - # Explicitly commits log, new bom (setUp) and replacement impact. - # Is run via background jobs IRL - run_replace_bom_job( - doc=log, - boms=self.boms, - update_type="Replace BOM", - ) + # Is run via background job IRL + run_replace_bom_job(doc=log, boms=self.boms) log.reload() self.assertEqual(log.status, "Completed") - # teardown (undo replace impact) due to commit - boms = frappe._dict( - current_bom=self.boms.new_bom, - new_bom=self.boms.current_bom, + +def update_cost_in_all_boms_in_test(): + """ + Utility to run 'Update Cost' job in tests immediately without Cron job. + Run job for all levels (manually) until fully complete. + """ + parent_boms = [] + log = enqueue_update_cost() # create BOM Update Log + + while log.status != "Completed": + level_boms, current_level = process_boms_cost_level_wise(log, parent_boms) + log.reload() + + boms, batch = queue_bom_cost_jobs( + level_boms, log, current_level + ) # adds rows in log for tracking + log.reload() + + update_cost_in_level(log, boms, batch) # business logic + log.reload() + + # current level done, get next level boms + bom_batches = frappe.db.get_all( + "BOM Update Batch", + {"parent": log.name, "level": log.current_level}, + ["name", "boms_updated", "status"], ) - log2 = enqueue_replace_bom( - boms=self.boms, + current_boms, processed_boms = get_processed_current_boms(log, bom_batches) + parent_boms = get_next_higher_level_boms(child_boms=current_boms, processed_boms=processed_boms) + + set_values_in_log( + log.name, + values={ + "processed_boms": json.dumps(processed_boms), + "status": "Completed" if not parent_boms else "In Progress", + }, ) - run_replace_bom_job( # Explicitly commits - doc=log2, - boms=boms, - update_type="Replace BOM", - ) - self.assertEqual(log2.status, "Completed") + log.reload() + + return log diff --git a/erpnext/manufacturing/doctype/bom_update_tool/test_bom_update_tool.py b/erpnext/manufacturing/doctype/bom_update_tool/test_bom_update_tool.py index fae72a0f6f7..d1882e56e95 100644 --- a/erpnext/manufacturing/doctype/bom_update_tool/test_bom_update_tool.py +++ b/erpnext/manufacturing/doctype/bom_update_tool/test_bom_update_tool.py @@ -1,11 +1,13 @@ -# Copyright (c) 2015, 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.tests.utils import FrappeTestCase from erpnext.manufacturing.doctype.bom_update_log.bom_update_log import replace_bom -from erpnext.manufacturing.doctype.bom_update_tool.bom_update_tool import update_cost +from erpnext.manufacturing.doctype.bom_update_log.test_bom_update_log import ( + update_cost_in_all_boms_in_test, +) from erpnext.manufacturing.doctype.production_plan.test_production_plan import make_bom from erpnext.stock.doctype.item.test_item import create_item @@ -25,8 +27,8 @@ class TestBOMUpdateTool(FrappeTestCase): boms = frappe._dict(current_bom=current_bom, new_bom=bom_doc.name) replace_bom(boms) - self.assertFalse(frappe.db.sql("select name from `tabBOM Item` where bom_no=%s", current_bom)) - self.assertTrue(frappe.db.sql("select name from `tabBOM Item` where bom_no=%s", bom_doc.name)) + self.assertFalse(frappe.db.exists("BOM Item", {"bom_no": current_bom, "docstatus": 1})) + self.assertTrue(frappe.db.exists("BOM Item", {"bom_no": bom_doc.name, "docstatus": 1})) # reverse, as it affects other testcases boms.current_bom = bom_doc.name @@ -52,13 +54,13 @@ class TestBOMUpdateTool(FrappeTestCase): self.assertEqual(doc.total_cost, 200) frappe.db.set_value("Item", "BOM Cost Test Item 2", "valuation_rate", 200) - update_cost() + update_cost_in_all_boms_in_test() doc.load_from_db() self.assertEqual(doc.total_cost, 300) frappe.db.set_value("Item", "BOM Cost Test Item 2", "valuation_rate", 100) - update_cost() + update_cost_in_all_boms_in_test() doc.load_from_db() self.assertEqual(doc.total_cost, 200) From f830b57fd481d46973b6874387529394fe659539 Mon Sep 17 00:00:00 2001 From: Ankush Menat Date: Tue, 7 Jun 2022 15:23:32 +0530 Subject: [PATCH 082/105] test: sales register report with conditions --- erpnext/accounts/report/sales_register/sales_register.py | 4 ++-- erpnext/accounts/test/test_reports.py | 1 + 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/erpnext/accounts/report/sales_register/sales_register.py b/erpnext/accounts/report/sales_register/sales_register.py index 777d96ced17..33bd3c74965 100644 --- a/erpnext/accounts/report/sales_register/sales_register.py +++ b/erpnext/accounts/report/sales_register/sales_register.py @@ -367,8 +367,8 @@ def get_conditions(filters): if not filters.get(field) or field in accounting_dimensions_list: return "" return f""" and exists(select name from `tab{table}` - where parent=`tabSales Invoice`.name - and ifnull(`tab{table}`.{field}, '') = %({field})s)""" + where parent=`tabSales Invoice`.name + and ifnull(`tab{table}`.{field}, '') = %({field})s)""" conditions += get_sales_invoice_item_field_condition("mode_of_payments", "Sales Invoice Payment") conditions += get_sales_invoice_item_field_condition("cost_center") diff --git a/erpnext/accounts/test/test_reports.py b/erpnext/accounts/test/test_reports.py index 19fe74fffc1..3f06c30adb6 100644 --- a/erpnext/accounts/test/test_reports.py +++ b/erpnext/accounts/test/test_reports.py @@ -28,6 +28,7 @@ REPORT_FILTER_TEST_CASES: List[Tuple[ReportName, ReportFilters]] = [ ("Item-wise Sales Register", {}), ("Item-wise Purchase Register", {}), ("Sales Register", {}), + ("Sales Register", {"item_group": "All Item Groups"}), ("Purchase Register", {}), ( "Tax Detail", From fb4f8d870be85037df1fd416be8fb93c1a85231f Mon Sep 17 00:00:00 2001 From: Saqib Ansari Date: Wed, 8 Jun 2022 09:36:33 +0530 Subject: [PATCH 083/105] fix(india): e-invoice eligibility if company gstin is not configured (#31247) --- erpnext/regional/india/e_invoice/utils.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/erpnext/regional/india/e_invoice/utils.py b/erpnext/regional/india/e_invoice/utils.py index 9add09beaf7..5eb14a5ddd3 100644 --- a/erpnext/regional/india/e_invoice/utils.py +++ b/erpnext/regional/india/e_invoice/utils.py @@ -55,6 +55,9 @@ def validate_eligibility(doc): return False invalid_company = not frappe.db.get_value("E Invoice User", {"company": doc.get("company")}) + invalid_company_gstin = not frappe.db.get_value( + "E Invoice User", {"gstin": doc.get("company_gstin")} + ) invalid_supply_type = doc.get("gst_category") not in [ "Registered Regular", "Registered Composition", @@ -71,6 +74,7 @@ def validate_eligibility(doc): if ( invalid_company + or invalid_company_gstin or invalid_supply_type or company_transaction or no_taxes_applied From 9f2793ccf1e185b28c0f6b480a304641923dbc30 Mon Sep 17 00:00:00 2001 From: marination Date: Tue, 7 Jun 2022 17:44:39 +0530 Subject: [PATCH 084/105] test: Fix `test_update_bom_cost_in_all_boms` - Use base_rate for assertions as rate is subject to change due to conversion factor (USD) --- erpnext/manufacturing/doctype/bom/test_bom.py | 22 ++++++++++++------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/erpnext/manufacturing/doctype/bom/test_bom.py b/erpnext/manufacturing/doctype/bom/test_bom.py index f2731ec5ef7..04e937a7e32 100644 --- a/erpnext/manufacturing/doctype/bom/test_bom.py +++ b/erpnext/manufacturing/doctype/bom/test_bom.py @@ -71,26 +71,32 @@ class TestBOM(FrappeTestCase): def test_update_bom_cost_in_all_boms(self): # get current rate for '_Test Item 2' - rm_rate = frappe.db.sql( - """select rate from `tabBOM Item` - where parent='BOM-_Test Item Home Desktop Manufactured-001' - and item_code='_Test Item 2' and docstatus=1 and parenttype='BOM'""" + bom_rates = frappe.db.get_values( + "BOM Item", + { + "parent": "BOM-_Test Item Home Desktop Manufactured-001", + "item_code": "_Test Item 2", + "docstatus": 1, + }, + fieldname=["rate", "base_rate"], + as_dict=True, ) - rm_rate = rm_rate[0][0] if rm_rate else 0 + rm_base_rate = bom_rates[0].get("base_rate") if bom_rates else 0 + rm_rate = bom_rates[0].get("rate") if bom_rates else 0 # Reset item valuation rate - reset_item_valuation_rate(item_code="_Test Item 2", qty=200, rate=rm_rate + 10) + reset_item_valuation_rate(item_code="_Test Item 2", qty=200, rate=rm_base_rate + 10) # update cost of all BOMs based on latest valuation rate update_cost_in_all_boms_in_test() # check if new valuation rate updated in all BOMs for d in frappe.db.sql( - """select rate from `tabBOM Item` + """select base_rate from `tabBOM Item` where item_code='_Test Item 2' and docstatus=1 and parenttype='BOM'""", as_dict=1, ): - self.assertEqual(d.rate, rm_rate + 10) + self.assertEqual(d.base_rate, rm_base_rate + 10) def test_bom_cost(self): bom = frappe.copy_doc(test_records[2]) From 018bc2af43160c4b25446bdba63c9fc5412b0be2 Mon Sep 17 00:00:00 2001 From: ruthra kumar Date: Wed, 8 Jun 2022 06:12:40 +0530 Subject: [PATCH 085/105] fix: ignore payment ledger on cancellation of loan --- erpnext/loan_management/doctype/loan/loan.py | 2 +- .../doctype/loan_disbursement/loan_disbursement.py | 2 +- .../doctype/loan_interest_accrual/loan_interest_accrual.py | 2 +- .../loan_management/doctype/loan_repayment/loan_repayment.py | 2 +- .../loan_management/doctype/loan_write_off/loan_write_off.py | 2 +- 5 files changed, 5 insertions(+), 5 deletions(-) diff --git a/erpnext/loan_management/doctype/loan/loan.py b/erpnext/loan_management/doctype/loan/loan.py index a0ef1b971cd..bb32c946f2d 100644 --- a/erpnext/loan_management/doctype/loan/loan.py +++ b/erpnext/loan_management/doctype/loan/loan.py @@ -71,7 +71,7 @@ class Loan(AccountsController): def on_cancel(self): self.unlink_loan_security_pledge() - self.ignore_linked_doctypes = ["GL Entry"] + self.ignore_linked_doctypes = ["GL Entry", "Payment Ledger Entry"] def set_missing_fields(self): if not self.company: diff --git a/erpnext/loan_management/doctype/loan_disbursement/loan_disbursement.py b/erpnext/loan_management/doctype/loan_disbursement/loan_disbursement.py index 10174e531a1..0c2042ba500 100644 --- a/erpnext/loan_management/doctype/loan_disbursement/loan_disbursement.py +++ b/erpnext/loan_management/doctype/loan_disbursement/loan_disbursement.py @@ -29,7 +29,7 @@ class LoanDisbursement(AccountsController): def on_cancel(self): self.set_status_and_amounts(cancel=1) self.make_gl_entries(cancel=1) - self.ignore_linked_doctypes = ["GL Entry"] + self.ignore_linked_doctypes = ["GL Entry", "Payment Ledger Entry"] def set_missing_values(self): if not self.disbursement_date: diff --git a/erpnext/loan_management/doctype/loan_interest_accrual/loan_interest_accrual.py b/erpnext/loan_management/doctype/loan_interest_accrual/loan_interest_accrual.py index 3a4c6513e45..0aeb4489184 100644 --- a/erpnext/loan_management/doctype/loan_interest_accrual/loan_interest_accrual.py +++ b/erpnext/loan_management/doctype/loan_interest_accrual/loan_interest_accrual.py @@ -32,7 +32,7 @@ class LoanInterestAccrual(AccountsController): self.update_is_accrued() self.make_gl_entries(cancel=1) - self.ignore_linked_doctypes = ["GL Entry"] + self.ignore_linked_doctypes = ["GL Entry", "Payment Ledger Entry"] def update_is_accrued(self): frappe.db.set_value("Repayment Schedule", self.repayment_schedule_name, "is_accrued", 0) diff --git a/erpnext/loan_management/doctype/loan_repayment/loan_repayment.py b/erpnext/loan_management/doctype/loan_repayment/loan_repayment.py index 8614fcb9cdc..35fbe3a38e3 100644 --- a/erpnext/loan_management/doctype/loan_repayment/loan_repayment.py +++ b/erpnext/loan_management/doctype/loan_repayment/loan_repayment.py @@ -41,7 +41,7 @@ class LoanRepayment(AccountsController): self.check_future_accruals() self.update_repayment_schedule(cancel=1) self.mark_as_unpaid() - self.ignore_linked_doctypes = ["GL Entry"] + self.ignore_linked_doctypes = ["GL Entry", "Payment Ledger Entry"] self.make_gl_entries(cancel=1) def set_missing_values(self, amounts): diff --git a/erpnext/loan_management/doctype/loan_write_off/loan_write_off.py b/erpnext/loan_management/doctype/loan_write_off/loan_write_off.py index e19fd15fc84..25aecf673bb 100644 --- a/erpnext/loan_management/doctype/loan_write_off/loan_write_off.py +++ b/erpnext/loan_management/doctype/loan_write_off/loan_write_off.py @@ -42,7 +42,7 @@ class LoanWriteOff(AccountsController): def on_cancel(self): self.update_outstanding_amount(cancel=1) - self.ignore_linked_doctypes = ["GL Entry"] + self.ignore_linked_doctypes = ["GL Entry", "Payment Ledger Entry"] self.make_gl_entries(cancel=1) def update_outstanding_amount(self, cancel=0): From 7e41d84a116f2acd03984c98ec4eaa8e50ddc1d3 Mon Sep 17 00:00:00 2001 From: marination Date: Wed, 8 Jun 2022 14:01:04 +0530 Subject: [PATCH 086/105] chore: `get_valuation_rate` sider fixes - Use qb instead of db.sql - Don't use `args` as argument for function - Cleaner variable names --- erpnext/manufacturing/doctype/bom/bom.py | 48 ++++++++++--------- erpnext/manufacturing/doctype/bom/test_bom.py | 1 - 2 files changed, 25 insertions(+), 24 deletions(-) diff --git a/erpnext/manufacturing/doctype/bom/bom.py b/erpnext/manufacturing/doctype/bom/bom.py index 3e2a2d13f18..631548b3099 100644 --- a/erpnext/manufacturing/doctype/bom/bom.py +++ b/erpnext/manufacturing/doctype/bom/bom.py @@ -932,44 +932,46 @@ def get_bom_item_rate(args, bom_doc): return flt(rate) -def get_valuation_rate(args): +def get_valuation_rate(data): """ 1) Get average valuation rate from all warehouses 2) If no value, get last valuation rate from SLE 3) If no value, get valuation rate from Item """ + from frappe.query_builder.functions import Sum + item_code, company = data.get("item_code"), data.get("company") valuation_rate = 0.0 - item_valuation = frappe.db.sql( - """ - select - (sum(bin.stock_value) / sum(bin.actual_qty)) as valuation_rate - from - `tabBin` bin, `tabWarehouse` warehouse - where - bin.item_code=%(item)s - and bin.warehouse = warehouse.name - and warehouse.company=%(company)s""", - {"item": args["item_code"], "company": args["company"]}, - as_dict=1, - )[0] + + bin_table = frappe.qb.DocType("Bin") + wh_table = frappe.qb.DocType("Warehouse") + item_valuation = ( + frappe.qb.from_(bin_table) + .join(wh_table) + .on(bin_table.warehouse == wh_table.name) + .select((Sum(bin_table.stock_value) / Sum(bin_table.actual_qty)).as_("valuation_rate")) + .where((bin_table.item_code == item_code) & (wh_table.company == company)) + ).run(as_dict=True)[0] valuation_rate = item_valuation.get("valuation_rate") if (valuation_rate is not None) and valuation_rate <= 0: # Explicit null value check. If None, Bins don't exist, neither does SLE - last_valuation_rate = frappe.db.sql( - """select valuation_rate - from `tabStock Ledger Entry` - where item_code = %s and valuation_rate > 0 and is_cancelled = 0 - order by posting_date desc, posting_time desc, creation desc limit 1""", - args["item_code"], - ) + sle = frappe.qb.DocType("Stock Ledger Entry") + last_val_rate = ( + frappe.qb.from_(sle) + .select(sle.valuation_rate) + .where((sle.item_code == item_code) & (sle.valuation_rate > 0) & (sle.is_cancelled == 0)) + .orderby(sle.posting_date, order=frappe.qb.desc) + .orderby(sle.posting_time, order=frappe.qb.desc) + .orderby(sle.creation, order=frappe.qb.desc) + .limit(1) + ).run(as_dict=True) - valuation_rate = flt(last_valuation_rate[0][0]) if last_valuation_rate else 0 + valuation_rate = flt(last_val_rate[0].get("valuation_rate")) if last_val_rate else 0 if not valuation_rate: - valuation_rate = frappe.db.get_value("Item", args["item_code"], "valuation_rate") + valuation_rate = frappe.db.get_value("Item", item_code, "valuation_rate") return flt(valuation_rate) diff --git a/erpnext/manufacturing/doctype/bom/test_bom.py b/erpnext/manufacturing/doctype/bom/test_bom.py index 04e937a7e32..182a20c6bb7 100644 --- a/erpnext/manufacturing/doctype/bom/test_bom.py +++ b/erpnext/manufacturing/doctype/bom/test_bom.py @@ -82,7 +82,6 @@ class TestBOM(FrappeTestCase): as_dict=True, ) rm_base_rate = bom_rates[0].get("base_rate") if bom_rates else 0 - rm_rate = bom_rates[0].get("rate") if bom_rates else 0 # Reset item valuation rate reset_item_valuation_rate(item_code="_Test Item 2", qty=200, rate=rm_base_rate + 10) From 2832731601920b07c7083a20c49868e866640add Mon Sep 17 00:00:00 2001 From: Marica Date: Wed, 8 Jun 2022 15:52:13 +0530 Subject: [PATCH 087/105] fix: Use `frappe.as_unicode` to decode output of redis module list (#31282) - As of redis 7, a list is added to the result of fetching the module list - This list cannot be "decoded",so use `frappe.as_unicode` that handles bytes as well as other types --- 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 61b4b9ee1f4..1f649c7b486 100644 --- a/erpnext/e_commerce/redisearch_utils.py +++ b/erpnext/e_commerce/redisearch_utils.py @@ -38,7 +38,7 @@ def is_search_module_loaded(): out = cache.execute_command("MODULE LIST") parsed_output = " ".join( - (" ".join([s.decode() for s in o if not isinstance(s, int)]) for o in out) + (" ".join([frappe.as_unicode(s) for s in o if not isinstance(s, int)]) for o in out) ) return "search" in parsed_output except Exception: From 5c6937865cf8e54b52012f3dbefdb536f07586d7 Mon Sep 17 00:00:00 2001 From: Rushabh Mehta Date: Wed, 8 Jun 2022 16:30:44 +0530 Subject: [PATCH 088/105] fix(ux): Add tabs in Item --- .../module_onboarding/accounts/accounts.json | 2 +- .../chart_of_accounts/chart_of_accounts.json | 6 +- .../setup_taxes/setup_taxes.json | 4 +- .../setup/module_onboarding/home/home.json | 2 +- .../data_import/data_import.json | 4 +- .../navigation_help/navigation_help.json | 2 +- erpnext/stock/doctype/item/item.json | 130 ++++++++---------- 7 files changed, 70 insertions(+), 80 deletions(-) diff --git a/erpnext/accounts/module_onboarding/accounts/accounts.json b/erpnext/accounts/module_onboarding/accounts/accounts.json index aa7cdf788b0..b9040e33097 100644 --- a/erpnext/accounts/module_onboarding/accounts/accounts.json +++ b/erpnext/accounts/module_onboarding/accounts/accounts.json @@ -13,7 +13,7 @@ "documentation_url": "https://docs.erpnext.com/docs/user/manual/en/accounts", "idx": 0, "is_complete": 0, - "modified": "2022-01-18 18:35:52.326688", + "modified": "2022-06-07 14:29:21.352132", "modified_by": "Administrator", "module": "Accounts", "name": "Accounts", diff --git a/erpnext/accounts/onboarding_step/chart_of_accounts/chart_of_accounts.json b/erpnext/accounts/onboarding_step/chart_of_accounts/chart_of_accounts.json index 67553baec74..0973ab39626 100644 --- a/erpnext/accounts/onboarding_step/chart_of_accounts/chart_of_accounts.json +++ b/erpnext/accounts/onboarding_step/chart_of_accounts/chart_of_accounts.json @@ -1,8 +1,8 @@ { - "action": "Watch Video", + "action": "Go to Page", "action_label": "Learn more about Chart of Accounts", "callback_message": "You can continue with the onboarding after exploring this page", - "callback_title": "Awesome Work", + "callback_title": "Explore Chart of Accounts", "creation": "2020-05-13 19:58:20.928127", "description": "# Chart Of Accounts\n\nERPNext sets up a simple chart of accounts for each Company you create, but you can modify it according to business and legal requirements.", "docstatus": 0, @@ -12,7 +12,7 @@ "is_complete": 0, "is_single": 0, "is_skipped": 0, - "modified": "2021-08-13 11:46:25.878506", + "modified": "2022-06-07 14:21:26.264769", "modified_by": "Administrator", "name": "Chart of Accounts", "owner": "Administrator", diff --git a/erpnext/accounts/onboarding_step/setup_taxes/setup_taxes.json b/erpnext/accounts/onboarding_step/setup_taxes/setup_taxes.json index 9f4c873e349..b6e9f5cd878 100644 --- a/erpnext/accounts/onboarding_step/setup_taxes/setup_taxes.json +++ b/erpnext/accounts/onboarding_step/setup_taxes/setup_taxes.json @@ -2,14 +2,14 @@ "action": "Create Entry", "action_label": "Manage Sales Tax Templates", "creation": "2020-05-13 19:29:43.844463", - "description": "# Setting up Taxes\n\nERPNext lets you configure your taxes so that they are automatically applied in your buying and selling transactions. You can configure them globally or even on Items. ERPNext taxes are pre-configured for most regions.\n", + "description": "# Setting up Taxes\n\nERPNext lets you configure your taxes so that they are automatically applied in your buying and selling transactions. You can configure them globally or even on Items. ERPNext taxes are pre-configured for most regions.\n\n[Checkout pre-configured taxes](/app/sales-taxes-and-charges-template)\n", "docstatus": 0, "doctype": "Onboarding Step", "idx": 0, "is_complete": 0, "is_single": 0, "is_skipped": 0, - "modified": "2021-08-13 11:48:37.238610", + "modified": "2022-06-07 14:27:15.906286", "modified_by": "Administrator", "name": "Setup Taxes", "owner": "Administrator", diff --git a/erpnext/setup/module_onboarding/home/home.json b/erpnext/setup/module_onboarding/home/home.json index 1b2dbc6fea7..f02fc454c00 100644 --- a/erpnext/setup/module_onboarding/home/home.json +++ b/erpnext/setup/module_onboarding/home/home.json @@ -25,7 +25,7 @@ "documentation_url": "https://docs.erpnext.com/docs/v13/user/manual/en/setting-up/company-setup", "idx": 0, "is_complete": 0, - "modified": "2021-12-15 14:23:52.460913", + "modified": "2022-06-07 14:31:00.575193", "modified_by": "Administrator", "module": "Setup", "name": "Home", diff --git a/erpnext/setup/onboarding_step/data_import/data_import.json b/erpnext/setup/onboarding_step/data_import/data_import.json index 48741dca01c..4999a368d37 100644 --- a/erpnext/setup/onboarding_step/data_import/data_import.json +++ b/erpnext/setup/onboarding_step/data_import/data_import.json @@ -2,14 +2,14 @@ "action": "Watch Video", "action_label": "Learn more about data migration", "creation": "2021-05-19 05:29:16.809610", - "description": "# Import Data from Spreadsheet\n\nIn ERPNext, you can easily migrate your historical data using spreadsheets. You can use it for migrating not just masters (like Customer, Supplier, Items), but also for transactions like (outstanding invoices, opening stock and accounting entries, etc). If you are migrating from [Tally](https://tallysolutions.com/) or [Quickbooks](https://quickbooks.intuit.com/in/), we got special migration tools for you.", + "description": "# Import Data from Spreadsheet\n\nIn ERPNext, you can easily migrate your historical data using spreadsheets. You can use it for migrating not just masters (like Customer, Supplier, Items), but also for transactions like (outstanding invoices, opening stock and accounting entries, etc).", "docstatus": 0, "doctype": "Onboarding Step", "idx": 0, "is_complete": 0, "is_single": 0, "is_skipped": 0, - "modified": "2021-12-15 13:10:57.346422", + "modified": "2022-06-07 14:28:51.390813", "modified_by": "Administrator", "name": "Data import", "owner": "Administrator", diff --git a/erpnext/setup/onboarding_step/navigation_help/navigation_help.json b/erpnext/setup/onboarding_step/navigation_help/navigation_help.json index 388853df79d..cf07968bc7f 100644 --- a/erpnext/setup/onboarding_step/navigation_help/navigation_help.json +++ b/erpnext/setup/onboarding_step/navigation_help/navigation_help.json @@ -9,7 +9,7 @@ "is_complete": 0, "is_single": 0, "is_skipped": 0, - "modified": "2021-12-15 14:20:55.441678", + "modified": "2022-06-07 14:28:00.901082", "modified_by": "Administrator", "name": "Navigation Help", "owner": "Administrator", diff --git a/erpnext/stock/doctype/item/item.json b/erpnext/stock/doctype/item/item.json index 4f3e8429957..2f6d4fb783d 100644 --- a/erpnext/stock/doctype/item/item.json +++ b/erpnext/stock/doctype/item/item.json @@ -11,7 +11,7 @@ "editable_grid": 1, "engine": "InnoDB", "field_order": [ - "name_and_description_section", + "details", "naming_series", "item_code", "variant_of", @@ -35,11 +35,11 @@ "over_billing_allowance", "image", "section_break_11", - "brand", "description", - "sb_barcodes", - "barcodes", + "brand", + "dashboard_tab", "inventory_section", + "inventory_settings_section", "shelf_life_in_days", "end_of_life", "default_material_request_type", @@ -49,6 +49,8 @@ "weight_per_unit", "weight_uom", "allow_negative_stock", + "sb_barcodes", + "barcodes", "reorder_section", "reorder_levels", "unit_of_measure_conversion", @@ -67,13 +69,13 @@ "has_variants", "variant_based_on", "attributes", - "defaults", + "accounting", "item_defaults", - "purchase_details", - "is_purchase_item", + "purchasing_tab", "purchase_uom", "min_order_qty", "safety_stock", + "is_purchase_item", "purchase_details_cb", "lead_time_days", "last_purchase_rate", @@ -83,33 +85,31 @@ "delivered_by_supplier", "column_break2", "supplier_items", + "deferred_expense_section", + "enable_deferred_expense", + "deferred_expense_account", + "no_of_months_exp", "foreign_trade_details", "country_of_origin", "column_break_59", "customs_tariff_number", "sales_details", "sales_uom", - "is_sales_item", "grant_commission", + "is_sales_item", "column_break3", "max_discount", "deferred_revenue", - "deferred_revenue_account", "enable_deferred_revenue", - "column_break_85", + "deferred_revenue_account", "no_of_months", - "deferred_expense_section", - "deferred_expense_account", - "enable_deferred_expense", - "column_break_88", - "no_of_months_exp", "customer_details", "customer_items", "item_tax_section_break", "taxes", - "inspection_criteria", - "quality_inspection_template", + "quality_tab", "inspection_required_before_purchase", + "quality_inspection_template", "inspection_required_before_delivery", "manufacturing", "default_bom", @@ -118,17 +118,10 @@ "customer_code", "default_item_manufacturer", "default_manufacturer_part_no", - "more_information_section", "published_in_website", "total_projected_qty" ], "fields": [ - { - "fieldname": "name_and_description_section", - "fieldtype": "Section Break", - "oldfieldtype": "Section Break", - "options": "fa fa-flag" - }, { "fieldname": "naming_series", "fieldtype": "Select", @@ -315,7 +308,7 @@ "collapsible_depends_on": "is_stock_item", "depends_on": "is_stock_item", "fieldname": "inventory_section", - "fieldtype": "Section Break", + "fieldtype": "Tab Break", "label": "Inventory", "oldfieldtype": "Section Break", "options": "fa fa-truck" @@ -514,31 +507,17 @@ "label": "Attributes", "options": "Item Variant Attribute" }, - { - "depends_on": "eval:!doc.is_fixed_asset", - "fieldname": "defaults", - "fieldtype": "Section Break", - "label": "Sales, Purchase, Accounting Defaults" - }, { "fieldname": "item_defaults", "fieldtype": "Table", "label": "Item Defaults", "options": "Item Default" }, - { - "collapsible": 1, - "fieldname": "purchase_details", - "fieldtype": "Section Break", - "label": "Purchase, Replenishment Details", - "oldfieldtype": "Section Break", - "options": "fa fa-shopping-cart" - }, { "default": "1", "fieldname": "is_purchase_item", "fieldtype": "Check", - "label": "Is Purchase Item" + "label": "Allow Purchase" }, { "fieldname": "purchase_uom", @@ -646,8 +625,8 @@ { "collapsible": 1, "fieldname": "sales_details", - "fieldtype": "Section Break", - "label": "Sales Details", + "fieldtype": "Tab Break", + "label": "Sales", "oldfieldtype": "Section Break", "options": "fa fa-tag" }, @@ -661,7 +640,7 @@ "default": "1", "fieldname": "is_sales_item", "fieldtype": "Check", - "label": "Is Sales Item" + "label": "Allow Sales" }, { "fieldname": "column_break3", @@ -696,10 +675,6 @@ "fieldtype": "Check", "label": "Enable Deferred Revenue" }, - { - "fieldname": "column_break_85", - "fieldtype": "Column Break" - }, { "depends_on": "enable_deferred_revenue", "fieldname": "no_of_months", @@ -726,10 +701,6 @@ "fieldtype": "Check", "label": "Enable Deferred Expense" }, - { - "fieldname": "column_break_88", - "fieldtype": "Column Break" - }, { "depends_on": "enable_deferred_expense", "fieldname": "no_of_months_exp", @@ -753,8 +724,8 @@ "collapsible": 1, "collapsible_depends_on": "taxes", "fieldname": "item_tax_section_break", - "fieldtype": "Section Break", - "label": "Item Tax", + "fieldtype": "Tab Break", + "label": "Tax", "oldfieldtype": "Section Break", "options": "fa fa-money" }, @@ -767,15 +738,6 @@ "oldfieldtype": "Table", "options": "Item Tax" }, - { - "collapsible": 1, - "depends_on": "eval:!doc.is_fixed_asset", - "fieldname": "inspection_criteria", - "fieldtype": "Section Break", - "label": "Inspection Criteria", - "oldfieldtype": "Section Break", - "options": "fa fa-search" - }, { "default": "0", "fieldname": "inspection_required_before_purchase", @@ -801,7 +763,7 @@ "collapsible": 1, "depends_on": "is_stock_item", "fieldname": "manufacturing", - "fieldtype": "Section Break", + "fieldtype": "Tab Break", "label": "Manufacturing", "oldfieldtype": "Section Break", "options": "fa fa-cogs" @@ -880,12 +842,6 @@ "label": "Default Manufacturer Part No", "read_only": 1 }, - { - "collapsible": 1, - "fieldname": "more_information_section", - "fieldtype": "Section Break", - "label": "More Information" - }, { "default": "0", "depends_on": "published_in_website", @@ -912,6 +868,40 @@ "fieldname": "allow_negative_stock", "fieldtype": "Check", "label": "Allow Negative Stock" + }, + { + "fieldname": "inventory_settings_section", + "fieldtype": "Section Break", + "label": "Inventory Settings" + }, + { + "fieldname": "purchasing_tab", + "fieldtype": "Tab Break", + "label": "Purchasing" + }, + { + "fieldname": "quality_tab", + "fieldtype": "Tab Break", + "label": "Quality" + }, + { + "fieldname": "details", + "fieldtype": "Tab Break", + "label": "Details", + "oldfieldtype": "Section Break", + "options": "fa fa-flag" + }, + { + "fieldname": "dashboard_tab", + "fieldtype": "Tab Break", + "label": "Dashboard", + "show_dashboard": 1 + }, + { + "depends_on": "eval:!doc.is_fixed_asset", + "fieldname": "accounting", + "fieldtype": "Tab Break", + "label": "Accounting" } ], "icon": "fa fa-tag", @@ -919,7 +909,7 @@ "image_field": "image", "index_web_pages_for_search": 1, "links": [], - "modified": "2022-04-28 04:52:10.272256", + "modified": "2022-06-08 11:35:20.094546", "modified_by": "Administrator", "module": "Stock", "name": "Item", From 67c26325eecb63a81f2839df1287ce8358340d1f Mon Sep 17 00:00:00 2001 From: Ankush Menat Date: Sat, 4 Jun 2022 14:46:35 +0530 Subject: [PATCH 089/105] fix: unnecessary GLE reposts In Sales/Purchase invoices credit/debit are flipped and negated while making GLE, this is unflipped while posting them but if we compare the flipped ones it will always result in comparison failure and repost it. --- erpnext/accounts/utils.py | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/erpnext/accounts/utils.py b/erpnext/accounts/utils.py index 1869cc7b290..df5e37d83d0 100644 --- a/erpnext/accounts/utils.py +++ b/erpnext/accounts/utils.py @@ -1124,6 +1124,9 @@ def update_gl_entries_after( def repost_gle_for_stock_vouchers( stock_vouchers, posting_date, company=None, warehouse_account=None ): + + from erpnext.accounts.general_ledger import toggle_debit_credit_if_negative + if not stock_vouchers: return @@ -1144,8 +1147,10 @@ def repost_gle_for_stock_vouchers( gle = get_voucherwise_gl_entries(stock_vouchers, posting_date) for voucher_type, voucher_no in stock_vouchers: existing_gle = gle.get((voucher_type, voucher_no), []) - voucher_obj = frappe.get_cached_doc(voucher_type, voucher_no) - expected_gle = voucher_obj.get_gl_entries(warehouse_account) + voucher_obj = frappe.get_doc(voucher_type, voucher_no) + # Some transactions post credit as negative debit, this is handled while posting GLE + # but while comparing we need to make sure it's flipped so comparisons are accurate + expected_gle = toggle_debit_credit_if_negative(voucher_obj.get_gl_entries(warehouse_account)) if expected_gle: if not existing_gle or not compare_existing_and_expected_gle( existing_gle, expected_gle, precision From eb53a9727d2e465f74597d987f3f82b9f0530d7c Mon Sep 17 00:00:00 2001 From: Ankush Menat Date: Sat, 4 Jun 2022 18:19:44 +0530 Subject: [PATCH 090/105] perf: commit GL reposting periodically If you have a huge list of docs to repost then maintaining transaction throughtout entire GL reposting is not only unnecessary but also creates performance issues. Periodically commiting the changes prevents lost progress and reduces memory usage. --- erpnext/accounts/utils.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/erpnext/accounts/utils.py b/erpnext/accounts/utils.py index df5e37d83d0..8711395d558 100644 --- a/erpnext/accounts/utils.py +++ b/erpnext/accounts/utils.py @@ -1145,7 +1145,7 @@ def repost_gle_for_stock_vouchers( precision = get_field_precision(frappe.get_meta("GL Entry").get_field("debit")) or 2 gle = get_voucherwise_gl_entries(stock_vouchers, posting_date) - for voucher_type, voucher_no in stock_vouchers: + for idx, (voucher_type, voucher_no) in enumerate(stock_vouchers): existing_gle = gle.get((voucher_type, voucher_no), []) voucher_obj = frappe.get_doc(voucher_type, voucher_no) # Some transactions post credit as negative debit, this is handled while posting GLE @@ -1160,6 +1160,11 @@ def repost_gle_for_stock_vouchers( else: _delete_gl_entries(voucher_type, voucher_no) + if idx % 20 == 0: + # Commit every 20 documents to avoid losing progress + # and reducing memory usage + frappe.db.commit() + def sort_stock_vouchers_by_posting_date( stock_vouchers: List[Tuple[str, str]] From 20f568c159424a728d8cf87b087efea069732579 Mon Sep 17 00:00:00 2001 From: Deepesh Garg Date: Thu, 9 Jun 2022 11:50:37 +0530 Subject: [PATCH 091/105] fix(India): Incorrect taxable in GSTR-3B report --- .../doctype/gstr_3b_report/gstr_3b_report.py | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/erpnext/regional/doctype/gstr_3b_report/gstr_3b_report.py b/erpnext/regional/doctype/gstr_3b_report/gstr_3b_report.py index 91fccfa6e8d..77b8a3fecd0 100644 --- a/erpnext/regional/doctype/gstr_3b_report/gstr_3b_report.py +++ b/erpnext/regional/doctype/gstr_3b_report/gstr_3b_report.py @@ -244,11 +244,10 @@ class GSTR3BReport(Document): ) for d in item_details: - if d.item_code not in self.invoice_items.get(d.parent, {}): - self.invoice_items.setdefault(d.parent, {}).setdefault(d.item_code, 0.0) - self.invoice_items[d.parent][d.item_code] += d.get("taxable_value", 0) or d.get( - "base_net_amount", 0 - ) + self.invoice_items.setdefault(d.parent, {}).setdefault(d.item_code, 0.0) + self.invoice_items[d.parent][d.item_code] += d.get("taxable_value", 0) or d.get( + "base_net_amount", 0 + ) if d.is_nil_exempt and d.item_code not in self.is_nil_exempt: self.is_nil_exempt.append(d.item_code) @@ -335,7 +334,7 @@ class GSTR3BReport(Document): def set_outward_taxable_supplies(self): inter_state_supply_details = {} - + invoice_list = {} for inv, items_based_on_rate in self.items_based_on_tax_rate.items(): gst_category = self.invoice_detail_map.get(inv, {}).get("gst_category") place_of_supply = ( @@ -343,6 +342,8 @@ class GSTR3BReport(Document): ) export_type = self.invoice_detail_map.get(inv, {}).get("export_type") + invoice_list.setdefault(inv, 0.0) + for rate, items in items_based_on_rate.items(): for item_code, taxable_value in self.invoice_items.get(inv).items(): if item_code in items: @@ -361,7 +362,6 @@ class GSTR3BReport(Document): else: self.report_dict["sup_details"]["osup_det"]["iamt"] += taxable_value * rate / 100 self.report_dict["sup_details"]["osup_det"]["txval"] += taxable_value - if ( gst_category in ["Unregistered", "Registered Composition", "UIN Holders"] and self.gst_details.get("gst_state") != place_of_supply.split("-")[1] @@ -374,10 +374,12 @@ class GSTR3BReport(Document): inter_state_supply_details[(gst_category, place_of_supply)]["iamt"] += ( taxable_value * rate / 100 ) + invoice_list[inv] += taxable_value if self.invoice_cess.get(inv): self.report_dict["sup_details"]["osup_det"]["csamt"] += flt(self.invoice_cess.get(inv), 2) + print({k: v for k, v in sorted(invoice_list.items(), key=lambda item: item[1])}) self.set_inter_state_supply(inter_state_supply_details) def set_supplies_liable_to_reverse_charge(self): From 50aafdbe99ff3f35b6816863768d0144c1e17e7b Mon Sep 17 00:00:00 2001 From: Deepesh Garg Date: Thu, 9 Jun 2022 11:52:46 +0530 Subject: [PATCH 092/105] chore: cleanup --- erpnext/regional/doctype/gstr_3b_report/gstr_3b_report.py | 5 ----- 1 file changed, 5 deletions(-) diff --git a/erpnext/regional/doctype/gstr_3b_report/gstr_3b_report.py b/erpnext/regional/doctype/gstr_3b_report/gstr_3b_report.py index 77b8a3fecd0..090697b0102 100644 --- a/erpnext/regional/doctype/gstr_3b_report/gstr_3b_report.py +++ b/erpnext/regional/doctype/gstr_3b_report/gstr_3b_report.py @@ -334,7 +334,6 @@ class GSTR3BReport(Document): def set_outward_taxable_supplies(self): inter_state_supply_details = {} - invoice_list = {} for inv, items_based_on_rate in self.items_based_on_tax_rate.items(): gst_category = self.invoice_detail_map.get(inv, {}).get("gst_category") place_of_supply = ( @@ -342,8 +341,6 @@ class GSTR3BReport(Document): ) export_type = self.invoice_detail_map.get(inv, {}).get("export_type") - invoice_list.setdefault(inv, 0.0) - for rate, items in items_based_on_rate.items(): for item_code, taxable_value in self.invoice_items.get(inv).items(): if item_code in items: @@ -374,12 +371,10 @@ class GSTR3BReport(Document): inter_state_supply_details[(gst_category, place_of_supply)]["iamt"] += ( taxable_value * rate / 100 ) - invoice_list[inv] += taxable_value if self.invoice_cess.get(inv): self.report_dict["sup_details"]["osup_det"]["csamt"] += flt(self.invoice_cess.get(inv), 2) - print({k: v for k, v in sorted(invoice_list.items(), key=lambda item: item[1])}) self.set_inter_state_supply(inter_state_supply_details) def set_supplies_liable_to_reverse_charge(self): From bbaa14af1615cedc88b9e5f4d19289c6be6510fe Mon Sep 17 00:00:00 2001 From: ruthra kumar Date: Thu, 9 Jun 2022 15:14:44 +0530 Subject: [PATCH 093/105] fix: misaligned columns in print format of AR/AP report --- .../report/accounts_receivable/accounts_receivable.html | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/erpnext/accounts/report/accounts_receivable/accounts_receivable.html b/erpnext/accounts/report/accounts_receivable/accounts_receivable.html index f4fd06ba037..f2bf9424f72 100644 --- a/erpnext/accounts/report/accounts_receivable/accounts_receivable.html +++ b/erpnext/accounts/report/accounts_receivable/accounts_receivable.html @@ -42,7 +42,7 @@ {% if(filters.show_future_payments) { %} {% var balance_row = data.slice(-1).pop(); - var start = filters.based_on_payment_terms ? 13 : 11; + var start = report.columns.findIndex((elem) => (elem.fieldname == 'age')); var range1 = report.columns[start].label; var range2 = report.columns[start+1].label; var range3 = report.columns[start+2].label; From d9a52139523cf095d3cc60cf61483a8d56468595 Mon Sep 17 00:00:00 2001 From: Ankush Menat Date: Thu, 9 Jun 2022 15:33:18 +0530 Subject: [PATCH 094/105] fix(ux): hide new version btn on unsaved BOM (#31297) --- erpnext/manufacturing/doctype/bom/bom.js | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/erpnext/manufacturing/doctype/bom/bom.js b/erpnext/manufacturing/doctype/bom/bom.js index d74379881ca..ecad41fe7b8 100644 --- a/erpnext/manufacturing/doctype/bom/bom.js +++ b/erpnext/manufacturing/doctype/bom/bom.js @@ -81,7 +81,7 @@ frappe.ui.form.on("BOM", { } ) - if (!frm.doc.__islocal && frm.doc.docstatus<2) { + if (!frm.is_new() && frm.doc.docstatus<2) { frm.add_custom_button(__("Update Cost"), function() { frm.events.update_cost(frm, true); }); @@ -93,10 +93,12 @@ frappe.ui.form.on("BOM", { }); } - frm.add_custom_button(__("New Version"), function() { - let new_bom = frappe.model.copy_doc(frm.doc); - frappe.set_route("Form", "BOM", new_bom.name); - }); + if (!frm.is_new() && !frm.doc.docstatus == 0) { + frm.add_custom_button(__("New Version"), function() { + let new_bom = frappe.model.copy_doc(frm.doc); + frappe.set_route("Form", "BOM", new_bom.name); + }); + } if(frm.doc.docstatus==1) { frm.add_custom_button(__("Work Order"), function() { From 3fa0a46f39f7024c5d0b235a7725eaa9ad0f3869 Mon Sep 17 00:00:00 2001 From: marination Date: Thu, 9 Jun 2022 16:22:00 +0530 Subject: [PATCH 095/105] chore: Less hacky tests, versioning (replace bom) and clearing log data (update cost) - Remove `auto_commit_on_many_writes` in `update_cost_in_level()` as commits happen every N BOMs - Auto commit every 50 BOMs - test: Remove hacky `frappe.flags.in_test` returns - test: Enqueue `now` if in tests (for update cost and replace bom) - Replace BOM: Copy bom object to `_doc_before_save` so that version.py finds a difference between the two - Replace BOM: Add reference to version - Update Cost: Unset `processed_boms` if Log is completed (useless after completion) - test: `update_cost_in_all_boms_in_test` works close to actual prod implementation (only call Cron job manually) - Test: use `enqueue_replace_bom` so that test works closest to production behaviour Co-authored-by: Ankush Menat --- .../doctype/bom_update_log/bom_update_log.py | 18 ++---- .../bom_update_log/bom_updation_utils.py | 19 ++++--- .../bom_update_log/test_bom_update_log.py | 56 +------------------ .../bom_update_tool/test_bom_update_tool.py | 12 ++-- 4 files changed, 23 insertions(+), 82 deletions(-) diff --git a/erpnext/manufacturing/doctype/bom_update_log/bom_update_log.py b/erpnext/manufacturing/doctype/bom_update_log/bom_update_log.py index 71430bd57e4..9c9c24044aa 100644 --- a/erpnext/manufacturing/doctype/bom_update_log/bom_update_log.py +++ b/erpnext/manufacturing/doctype/bom_update_log/bom_update_log.py @@ -67,9 +67,6 @@ class BOMUpdateLog(Document): ) def on_submit(self): - if frappe.flags.in_test: - return - if self.update_type == "Replace BOM": boms = {"current_bom": self.current_bom, "new_bom": self.new_bom} frappe.enqueue( @@ -77,6 +74,7 @@ class BOMUpdateLog(Document): doc=self, boms=boms, timeout=40000, + now=frappe.flags.in_test, ) else: process_boms_cost_level_wise(self) @@ -94,7 +92,7 @@ def run_replace_bom_job( frappe.db.auto_commit_on_many_writes = 1 boms = frappe._dict(boms or {}) - replace_bom(boms) + replace_bom(boms, doc.name) doc.db_set("status", "Completed") except Exception: @@ -135,10 +133,6 @@ def process_boms_cost_level_wise( values = {"current_level": current_level} set_values_in_log(update_doc.name, values, commit=True) - - if frappe.flags.in_test: - return current_boms, current_level - queue_bom_cost_jobs(current_boms, update_doc, current_level) @@ -161,16 +155,13 @@ def queue_bom_cost_jobs( ) batch_row.db_insert() - if frappe.flags.in_test: - # skip background jobs in test - return boms_to_process, batch_row.name - frappe.enqueue( method="erpnext.manufacturing.doctype.bom_update_log.bom_updation_utils.update_cost_in_level", doc=update_doc, bom_list=boms_to_process, batch_name=batch_row.name, queue="long", + now=frappe.flags.in_test, ) @@ -208,10 +199,11 @@ def resume_bom_cost_update_jobs(): current_boms, processed_boms = get_processed_current_boms(log, bom_batches) parent_boms = get_next_higher_level_boms(child_boms=current_boms, processed_boms=processed_boms) + # Unset processed BOMs if log is complete, it is used for next level BOMs set_values_in_log( log.name, values={ - "processed_boms": json.dumps(processed_boms), + "processed_boms": json.dumps([] if not parent_boms else processed_boms), "status": "Completed" if not parent_boms else "In Progress", }, commit=True, diff --git a/erpnext/manufacturing/doctype/bom_update_log/bom_updation_utils.py b/erpnext/manufacturing/doctype/bom_update_log/bom_updation_utils.py index dde1e4ed759..af115e3e421 100644 --- a/erpnext/manufacturing/doctype/bom_update_log/bom_updation_utils.py +++ b/erpnext/manufacturing/doctype/bom_update_log/bom_updation_utils.py @@ -1,6 +1,7 @@ # Copyright (c) 2022, Frappe Technologies Pvt. Ltd. and contributors # For license information, please see license.txt +import copy import json from collections import defaultdict from typing import TYPE_CHECKING, Any, Dict, List, Optional, Union @@ -12,7 +13,7 @@ import frappe from frappe import _ -def replace_bom(boms: Dict) -> None: +def replace_bom(boms: Dict, log_name: str) -> None: "Replace current BOM with new BOM in parent BOMs." current_bom = boms.get("current_bom") @@ -29,13 +30,17 @@ def replace_bom(boms: Dict) -> None: # this is only used for versioning and we do not want # to make separate db calls by using load_doc_before_save # which proves to be expensive while doing bulk replace - bom_obj._doc_before_save = bom_obj + bom_obj._doc_before_save = copy.deepcopy(bom_obj) bom_obj.update_exploded_items() bom_obj.calculate_cost() bom_obj.update_parent_cost() bom_obj.db_update() - if bom_obj.meta.get("track_changes") and not bom_obj.flags.ignore_version: - bom_obj.save_version() + bom_obj.flags.updater_reference = { + "doctype": "BOM Update Log", + "docname": log_name, + "label": _("via BOM Update Tool"), + } + bom_obj.save_version() def update_cost_in_level( @@ -48,8 +53,6 @@ def update_cost_in_level( if status == "Failed": return - frappe.db.auto_commit_on_many_writes = 1 - update_cost_in_boms(bom_list=bom_list) # main updation logic bom_batch = frappe.qb.DocType("BOM Update Batch") @@ -62,8 +65,6 @@ def update_cost_in_level( except Exception: handle_exception(doc) finally: - frappe.db.auto_commit_on_many_writes = 0 - if not frappe.flags.in_test: frappe.db.commit() # nosemgrep @@ -121,7 +122,7 @@ def update_cost_in_boms(bom_list: List[str]) -> None: bom_doc.calculate_cost(save_updates=True, update_hour_rate=True) bom_doc.db_update() - if (index % 100 == 0) and not frappe.flags.in_test: + if (index % 50 == 0) and not frappe.flags.in_test: frappe.db.commit() # nosemgrep diff --git a/erpnext/manufacturing/doctype/bom_update_log/test_bom_update_log.py b/erpnext/manufacturing/doctype/bom_update_log/test_bom_update_log.py index d770f6c56a5..b38fc8976b2 100644 --- a/erpnext/manufacturing/doctype/bom_update_log/test_bom_update_log.py +++ b/erpnext/manufacturing/doctype/bom_update_log/test_bom_update_log.py @@ -1,22 +1,12 @@ # Copyright (c) 2022, Frappe Technologies Pvt. Ltd. and Contributors # See license.txt -import json - import frappe from frappe.tests.utils import FrappeTestCase from erpnext.manufacturing.doctype.bom_update_log.bom_update_log import ( BOMMissingError, - get_processed_current_boms, - process_boms_cost_level_wise, - queue_bom_cost_jobs, - run_replace_bom_job, -) -from erpnext.manufacturing.doctype.bom_update_log.bom_updation_utils import ( - get_next_higher_level_boms, - set_values_in_log, - update_cost_in_level, + resume_bom_cost_update_jobs, ) from erpnext.manufacturing.doctype.bom_update_tool.bom_update_tool import ( enqueue_replace_bom, @@ -60,62 +50,22 @@ class TestBOMUpdateLog(FrappeTestCase): with self.assertRaises(frappe.ValidationError): enqueue_replace_bom(boms=frappe._dict(current_bom=self.boms.new_bom, new_bom="Dummy BOM")) - def test_bom_update_log_queueing(self): - "Test if BOM Update Log is created and queued." - - log = enqueue_replace_bom(boms=self.boms) - - self.assertEqual(log.docstatus, 1) - self.assertEqual(log.status, "Queued") - def test_bom_update_log_completion(self): "Test if BOM Update Log handles job completion correctly." log = enqueue_replace_bom(boms=self.boms) - - # Is run via background job IRL - run_replace_bom_job(doc=log, boms=self.boms) log.reload() - self.assertEqual(log.status, "Completed") def update_cost_in_all_boms_in_test(): """ - Utility to run 'Update Cost' job in tests immediately without Cron job. - Run job for all levels (manually) until fully complete. + Utility to run 'Update Cost' job in tests without Cron job until fully complete. """ - parent_boms = [] log = enqueue_update_cost() # create BOM Update Log while log.status != "Completed": - level_boms, current_level = process_boms_cost_level_wise(log, parent_boms) - log.reload() - - boms, batch = queue_bom_cost_jobs( - level_boms, log, current_level - ) # adds rows in log for tracking - log.reload() - - update_cost_in_level(log, boms, batch) # business logic - log.reload() - - # current level done, get next level boms - bom_batches = frappe.db.get_all( - "BOM Update Batch", - {"parent": log.name, "level": log.current_level}, - ["name", "boms_updated", "status"], - ) - current_boms, processed_boms = get_processed_current_boms(log, bom_batches) - parent_boms = get_next_higher_level_boms(child_boms=current_boms, processed_boms=processed_boms) - - set_values_in_log( - log.name, - values={ - "processed_boms": json.dumps(processed_boms), - "status": "Completed" if not parent_boms else "In Progress", - }, - ) + resume_bom_cost_update_jobs() # run cron job until complete log.reload() return log diff --git a/erpnext/manufacturing/doctype/bom_update_tool/test_bom_update_tool.py b/erpnext/manufacturing/doctype/bom_update_tool/test_bom_update_tool.py index d1882e56e95..5dd557f8ab1 100644 --- a/erpnext/manufacturing/doctype/bom_update_tool/test_bom_update_tool.py +++ b/erpnext/manufacturing/doctype/bom_update_tool/test_bom_update_tool.py @@ -4,10 +4,10 @@ import frappe from frappe.tests.utils import FrappeTestCase -from erpnext.manufacturing.doctype.bom_update_log.bom_update_log import replace_bom from erpnext.manufacturing.doctype.bom_update_log.test_bom_update_log import ( update_cost_in_all_boms_in_test, ) +from erpnext.manufacturing.doctype.bom_update_tool.bom_update_tool import enqueue_replace_bom from erpnext.manufacturing.doctype.production_plan.test_production_plan import make_bom from erpnext.stock.doctype.item.test_item import create_item @@ -17,6 +17,9 @@ test_records = frappe.get_test_records("BOM") class TestBOMUpdateTool(FrappeTestCase): "Test major functions run via BOM Update Tool." + def tearDown(self): + frappe.db.rollback() + def test_replace_bom(self): current_bom = "BOM-_Test Item Home Desktop Manufactured-001" @@ -25,16 +28,11 @@ class TestBOMUpdateTool(FrappeTestCase): bom_doc.insert() boms = frappe._dict(current_bom=current_bom, new_bom=bom_doc.name) - replace_bom(boms) + enqueue_replace_bom(boms=boms) self.assertFalse(frappe.db.exists("BOM Item", {"bom_no": current_bom, "docstatus": 1})) self.assertTrue(frappe.db.exists("BOM Item", {"bom_no": bom_doc.name, "docstatus": 1})) - # reverse, as it affects other testcases - boms.current_bom = bom_doc.name - boms.new_bom = current_bom - replace_bom(boms) - def test_bom_cost(self): for item in ["BOM Cost Test Item 1", "BOM Cost Test Item 2", "BOM Cost Test Item 3"]: item_doc = create_item(item, valuation_rate=100) From e6f65e1697dfac82688623184ba4b1fb6782da54 Mon Sep 17 00:00:00 2001 From: "mergify[bot]" <37929162+mergify[bot]@users.noreply.github.com> Date: Thu, 9 Jun 2022 17:47:15 +0530 Subject: [PATCH 096/105] chore: Asset Arabic translation Fix (backport #31221) (#31301) chore: Asset Arabic translation Fix (#31221) Update ar.csv Fix Translation arabic translation that caused an error when submitting an asset if user language was arabic (cherry picked from commit 9347cbbc9f7826116faf22db7bf9f3bf32e6e3c2) Co-authored-by: meaziz --- erpnext/translations/ar.csv | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/erpnext/translations/ar.csv b/erpnext/translations/ar.csv index 91a9da9f163..e62f61a4f55 100644 --- a/erpnext/translations/ar.csv +++ b/erpnext/translations/ar.csv @@ -4297,7 +4297,7 @@ Fetch Serial Numbers based on FIFO,إحضار الأرقام المسلسلة ب "To allow different rates, disable the {0} checkbox in {1}.",للسماح بمعدلات مختلفة ، قم بتعطيل مربع الاختيار {0} في {1}., Current Odometer Value should be greater than Last Odometer Value {0},يجب أن تكون قيمة عداد المسافات الحالية أكبر من قيمة آخر عداد المسافات {0}, No additional expenses has been added,لم يتم إضافة مصاريف إضافية, -Asset{} {assets_link} created for {},الأصل {} {asset_link} الذي تم إنشاؤه لـ {}, +Asset{} {assets_link} created for {},الأصل {} {assets_link} الذي تم إنشاؤه لـ {}, Row {}: Asset Naming Series is mandatory for the auto creation for item {},الصف {}: سلسلة تسمية الأصول إلزامية للإنشاء التلقائي للعنصر {}, Assets not created for {0}. You will have to create asset manually.,لم يتم إنشاء الأصول لـ {0}. سيكون عليك إنشاء الأصل يدويًا., {0} {1} has accounting entries in currency {2} for company {3}. Please select a receivable or payable account with currency {2}.,{0} يحتوي {1} على إدخالات محاسبية بالعملة {2} للشركة {3}. الرجاء تحديد حساب مستحق أو دائن بالعملة {2}., From 17887cde7122ff2332f92394cfa2c8d1e196339a Mon Sep 17 00:00:00 2001 From: RJPvT <48353029+RJPvT@users.noreply.github.com> Date: Wed, 8 Jun 2022 10:55:15 +0200 Subject: [PATCH 097/105] fix: locale Currency and Float setting in update_employee In fieldtypes locale settings (example NL) . and , changes whereby the field is inproperly filled --- erpnext/hr/utils.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/erpnext/hr/utils.py b/erpnext/hr/utils.py index c730b199247..3f4e31b1b2b 100644 --- a/erpnext/hr/utils.py +++ b/erpnext/hr/utils.py @@ -55,6 +55,8 @@ def update_employee_work_history(employee, details, date=None, cancel=False): new_data = getdate(new_data) elif fieldtype == "Datetime" and new_data: new_data = get_datetime(new_data) + elif fieldtype in ["Currency", "Float"] and new_data: + new_data = flt(new_data) setattr(employee, item.fieldname, new_data) if item.fieldname in ["department", "designation", "branch"]: internal_work_history[item.fieldname] = item.new From 2675751d6c2ce188b1df8be5f930869a97ebd520 Mon Sep 17 00:00:00 2001 From: Vladislav Date: Thu, 9 Jun 2022 16:16:08 +0300 Subject: [PATCH 098/105] fix: update ru translate (#31200) * Update ru.csv * Update ru.csv * Update ru.csv * Update ru.csv * Update ru.csv * Update ru.csv * Update ru.csv * Update ru.csv fix logic * Update ru.csv * Update ru.csv * Update ru.csv --- erpnext/translations/ru.csv | 141 +++++++++++++++++++----------------- 1 file changed, 74 insertions(+), 67 deletions(-) diff --git a/erpnext/translations/ru.csv b/erpnext/translations/ru.csv index 6b766e7dc0f..743b29493c2 100644 --- a/erpnext/translations/ru.csv +++ b/erpnext/translations/ru.csv @@ -288,7 +288,7 @@ Asset {0} must be submitted,Актив {0} должен быть проведе Assets,Активы, Assign,Назначить, Assign Salary Structure,Назначить структуру заработной платы, -Assign To,Назначить в, +Assign To,Назначить для, Assign to Employees,Назначить сотрудникам, Assigning Structures...,Назначение структур..., Associate,Помощник, @@ -421,7 +421,7 @@ Buildings,Здания, Bundle items at time of sale.,Собирать продукты в момент продажи., Business Development Manager,Менеджер по развитию бизнеса, Buy,Купить, -Buying,Покупки, +Buying,Закупки, Buying Amount,Сумма покупки, Buying Price List,Ценовой список покупок, Buying Rate,Частота покупки, @@ -490,7 +490,7 @@ Capital Equipments,Капитальные оборудование, Capital Stock,Капитал, Capital Work in Progress,Капитальная работа в процессе, Cart,Корзина, -Cart is Empty,Корзина Пусто, +Cart is Empty,Корзина пуста, Case No(s) already in use. Try from Case No {0},Случай Нет (ы) уже используется. Попробуйте из дела № {0}, Cash,Наличные, Cash Flow Statement,О движении денежных средств, @@ -578,7 +578,7 @@ Compensatory Off,Компенсационные Выкл, Compensatory leave request days not in valid holidays,Дни запроса на получение компенсационных отчислений не действительны, Complaint,Жалоба, Completion Date,Дата завершения, -Computer,компьютер, +Computer,Компьютер, Condition,Условия, Configure,Конфигурировать, Configure {0},Настроить {0}, @@ -643,7 +643,6 @@ Course Code: ,Код курса: , Course Enrollment {0} does not exists,Зачисление на курс {0} не существует, Course Schedule,Расписание курса, Course: ,Курс: , -Cr,Cr, Create,Создать, Create BOM,Создать спецификацию, Create Delivery Trip,Создать маршрут доставки, @@ -795,7 +794,6 @@ Defense,Оборона, Define Project type.,Установите тип проекта., Define budget for a financial year.,Определить бюджет на финансовый год., Define various loan types,Определение различных видов кредита, -Del,Del, Delay in payment (Days),Задержка в оплате (дни), Delete all the Transactions for this Company,Удалить все транзакции этой компании, Deletion is not permitted for country {0},Для страны не разрешено удаление {0}, @@ -1287,12 +1285,12 @@ Installing presets,Установка пресетов, Institute Abbreviation,институт Аббревиатура, Institute Name,Название института, Instructor,Инструктор, -Insufficient Stock,Недостаточный Stock, -Insurance Start date should be less than Insurance End date,"Дата страхование начала должна быть меньше, чем дата страхование End", +Insufficient Stock,Недостаточный запас, +Insurance Start date should be less than Insurance End date,"Дата начала страхования должна быть раньше, чем дата окончания", Integrated Tax,Интегрированный налог, Inter-State Supplies,Межгосударственные поставки, -Interest Amount,Проценты Сумма, -Interests,интересы, +Interest Amount,Сумма процентов, +Interests,Интересы, Intern,Стажер, Internet Publishing,Интернет издания, Intra-State Supplies,Внутригосударственные поставки, @@ -1397,7 +1395,7 @@ Job Card,Карточка работы, Job Description,Описание работы, Job Offer,Предложение работы, Job card {0} created,Карта работы {0} создана, -Jobs,Работы, +Jobs,Вакансии, Join,Присоединиться, Journal Entries {0} are un-linked,Записи в журнале {0} не-связаны, Journal Entry,Запись в журнале, @@ -1925,7 +1923,7 @@ Pending Amount,В ожидании Сумма, Pending Leaves,Ожидающие листья, Pending Qty,В ожидании кол-во, Pending Quantity,Количество в ожидании, -Pending Review,В ожидании отзыв, +Pending Review,В ожидании отзыва, Pending activities for today,В ожидании деятельность на сегодняшний день, Pension Funds,Пенсионные фонды, Percentage Allocation should be equal to 100%,Процент Распределение должно быть равно 100%, @@ -1949,7 +1947,7 @@ Planned Qty,Планируемое кол-во, Planning,Планирование, Plants and Machineries,Растения и Механизмов, Please Set Supplier Group in Buying Settings.,Установите группу поставщиков в разделе «Настройки покупок»., -Please add a Temporary Opening account in Chart of Accounts,"Пожалуйста, добавьте временный вступительный счет в План счетов", +Please add a Temporary Opening account in Chart of Accounts,"Пожалуйста, добавьте временный вступительный счет в план счетов", Please add the account to root level Company - ,"Пожалуйста, добавьте счет на корневой уровень компании -", Please add the remaining benefits {0} to any of the existing component,Добавьте оставшиеся преимущества {0} к любому из существующих компонентов, Please check Multi Currency option to allow accounts with other currency,"Пожалуйста, проверьте мультивалютный вариант, позволяющий счета другой валюте", @@ -2146,7 +2144,7 @@ Preview Salary Slip,Просмотр Зарплата скольжению, Previous Financial Year is not closed,Предыдущий финансовый год не закрыт, Price,Цена, Price List,Прайс-лист, -Price List Currency not selected,Прайс-лист Обмен не выбран, +Price List Currency not selected,Валюта прайс-листа не выбрана, Price List Rate,Прайс-лист Оценить, Price List master.,Мастер Прайс-лист., Price List must be applicable for Buying or Selling,Прайс-лист должен быть применим для покупки или продажи, @@ -2347,7 +2345,7 @@ Remaining,Осталось, Remaining Balance,Остаток средств, Remarks,Примечания, Reminder to update GSTIN Sent,Напоминание об обновлении отправленного GSTIN, -Remove item if charges is not applicable to that item,"Удалить продукт, если сборы не применимы к этому продукту", +Remove item if charges is not applicable to that item,"Удалить объект, если к нему не применяются сборы", Removed items with no change in quantity or value.,Удалены пункты без изменения в количестве или стоимости., Reopen,Возобновить, Reorder Level,Уровень переупорядочения, @@ -2509,7 +2507,7 @@ Salary Slip of employee {0} already created for this period,Зарплата С Salary Slip of employee {0} already created for time sheet {1},Зарплата Скольжение работника {0} уже создан для табеля {1}, Salary Slip submitted for period from {0} to {1},"Зарплатная ведомость отправлена за период с {0} по {1}", Salary Structure Assignment for Employee already exists,Присвоение структуры зарплаты сотруднику уже существует, -Salary Structure Missing,Структура заработной платы Отсутствующий, +Salary Structure Missing,Структура заработной платы отсутствует, Salary Structure must be submitted before submission of Tax Ememption Declaration,Структура заработной платы должна быть представлена до подачи декларации об освобождении от налогов, Salary Structure not found for employee {0} and date {1},Структура зарплаты не найдена для сотрудника {0} и даты {1}, Salary Structure should have flexible benefit component(s) to dispense benefit amount,Структура заработной платы должна иметь гибкий компонент (ы) выгоды для распределения суммы пособия, @@ -2701,10 +2699,10 @@ Setup default values for POS Invoices,Настройка значений по Setup mode of POS (Online / Offline),Режим настройки POS (Online / Offline), Setup your Institute in ERPNext,Установите свой институт в ERPNext, Share Balance,Баланс акций, -Share Ledger,Поделиться записями, +Share Ledger,Записи по акциям, Share Management,Управление долями, Share Transfer,Передача акций, -Share Type,Share Тип, +Share Type,Тип акций, Shareholder,Акционер, Ship To State,Корабль в штат, Shipments,Поставки, @@ -2796,8 +2794,8 @@ Stock Entry {0} is not submitted,Складской акт {0} не провед Stock Expenses,Расходы по Запасам, Stock In Hand,Запасы на руках, Stock Items,Позиции на складе, -Stock Ledger,Книга учета Запасов, -Stock Ledger Entries and GL Entries are reposted for the selected Purchase Receipts,Записи складской книги и записи GL запасов отправляются для выбранных покупок, +Stock Ledger,Книга учета запасов, +Stock Ledger Entries and GL Entries are reposted for the selected Purchase Receipts,Записи книги учета запасов и записи GL повторно публикуются для выбранных квитанций о покупках, Stock Levels,Уровень запасов, Stock Liabilities,Обязательства по запасам, Stock Options,Опционы, @@ -2829,9 +2827,9 @@ Student Email ID,Идентификация студента по электро Student Group,Учебная группа, Student Group Strength,Сила студенческой группы, Student Group is already updated.,Студенческая группа уже обновлена., -Student Group: ,Студенческая группа:, +Student Group: ,Студенческая группа: , Student ID,Студенческий билет, -Student ID: ,Студенческий билет:, +Student ID: ,Студенческий билет: , Student LMS Activity,Студенческая LMS Активность, Student Mobile No.,Мобильный номер студента, Student Name,Имя ученика, @@ -2864,9 +2862,9 @@ Successfully created payment entries,Успешно созданные плат Successfully deleted all transactions related to this company!,"Успешно удален все сделки, связанные с этой компанией!", Sum of Scores of Assessment Criteria needs to be {0}.,Сумма десятков критериев оценки должно быть {0}., Sum of points for all goals should be 100. It is {0},Сумма баллов за все цели должны быть 100. Это {0}, -Summary,Резюме, -Summary for this month and pending activities,Резюме для этого месяца и в ожидании деятельности, -Summary for this week and pending activities,Резюме на этой неделе и в ожидании деятельности, +Summary,Сводка, +Summary for this month and pending activities,Сводка за этот месяц и предстоящие мероприятия, +Summary for this week and pending activities,Сводка за эту неделю и предстоящие мероприятия, Sunday,Воскресенье, Suplier,Поставщик, Supplier,Поставщик, @@ -2880,7 +2878,7 @@ Supplier Name,наименование поставщика, Supplier Part No,Деталь поставщика №, Supplier Quotation,Предложение поставщика, Supplier Scorecard,Оценочная карта поставщика, -Supplier Warehouse mandatory for sub-contracted Purchase Receipt,Поставщик Склад обязательным для субподрядчиком ТОВАРНЫЙ ЧЕК, +Supplier Warehouse mandatory for sub-contracted Purchase Receipt,Наличие склада поставщика обязательно для субподрядной квитанции о покупке, Supplier database.,База данных поставщиков., Supplier {0} not found in {1},Поставщик {0} не найден в {1}, Supplier(s),Поставщик(и), @@ -3199,7 +3197,7 @@ Used Leaves,Используемые листы, User,Пользователь, User ID,ID пользователя, User ID not set for Employee {0},ID пользователя не установлен для сотрудника {0}, -User Remark,Примечание Пользователь, +User Remark,Примечание пользователя, User has not applied rule on the invoice {0},Пользователь не применил правило к счету {0}, User {0} already exists,Пользователь {0} уже существует, User {0} created,Пользователь {0} создан, @@ -3243,7 +3241,7 @@ View Fees Records,Посмотреть рекорды, View Form,Посмотреть форму, View Lab Tests,Просмотр лабораторных тестов, View Leads,Посмотреть лиды, -View Ledger,Посмотреть Леджер, +View Ledger,Посмотреть записи, View Now,Просмотр сейчас, View a list of all the help videos,Просмотреть список всех справочных видео, View in Cart,Смотрите в корзину, @@ -3314,7 +3312,7 @@ Work Orders Created: {0},Созданы рабочие задания: {0}, Work Summary for {0},Резюме работы для {0}, Work-in-Progress Warehouse is required before Submit,Работа-в-Прогресс Склад требуется перед Отправить, Workflow,Рабочий процесс, -Working,Работающий, +Working,В работе, Working Hours,Часы работы, Workstation,Рабочее место, Workstation is closed on the following dates as per Holiday List: {0},Рабочая место закрыто в следующие даты согласно списка праздников: {0}, @@ -3869,13 +3867,17 @@ Non stock items,Нет на складе, Not Allowed,Не разрешено, Not allowed to create accounting dimension for {0},Не разрешено создавать учетное измерение для {0}, Not permitted. Please disable the Lab Test Template,"Не разрешено Пожалуйста, отключите шаблон лабораторного теста", -Note,Заметки, +Note,Заметка, Notes: ,Заметки: , -On Converting Opportunity,О возможности конвертации, -On Purchase Order Submission,При подаче заказа на поставку, -On Sales Order Submission,На подаче заказа клиента, -On Task Completion,По завершении задачи, +On Converting Opportunity,Конвертацию возможности, +On Purchase Order Submission,Офомление заказа на закупку, +On Sales Order Submission,Оформление заказа на продажу, +On Task Completion,Завершении задачи, On {0} Creation,На {0} создании, +On Item Creation,Создание продукта, +On Lead Creation,Создание лида, +On Supplier Creation,Создание поставщика, +On Customer Creation,Создание клиента, Only .csv and .xlsx files are supported currently,В настоящее время поддерживаются только файлы .csv и .xlsx, Only expired allocation can be cancelled,Только истекшее распределение может быть отменено, Only users with the {0} role can create backdated leave applications,Только пользователи с ролью {0} могут создавать оставленные приложения с задним сроком действия, @@ -4217,7 +4219,7 @@ Mode Of Payment,Способ оплаты, No students Found,Студенты не найдены, Not in Stock,Нет в наличии, Please select a Customer,Выберите клиента, -Printed On,Отпечатано на, +Printed On,Напечатано на, Received From,Получено от, Sales Person,Продавец, To date cannot be before From date,На сегодняшний день не может быть раньше От даты, @@ -4945,14 +4947,14 @@ Max Qty,Макс. кол-во, Min Amt,Мин Amt, Max Amt,Макс Амт, Period Settings,Настройки периода, -Margin,Разница, +Margin,Маржа, Margin Type,Тип маржа, Margin Rate or Amount,Маржинальная ставка или сумма, Price Discount Scheme,Схема скидок, Rate or Discount,Стоимость или скидка, Discount Percentage,Скидка в процентах, Discount Amount,Сумма скидки, -For Price List,Для Прейскурантом, +For Price List,Для прайс-листа, Product Discount Scheme,Схема скидок на товары, Same Item,Тот же пункт, Free Item,Бесплатный товар, @@ -5385,18 +5387,18 @@ Insurance Start Date,Дата начала страхования, Insurance End Date,Дата окончания страхования, Comprehensive Insurance,Комплексное страхование, Maintenance Required,Требуется техническое обслуживание, -Check if Asset requires Preventive Maintenance or Calibration,"Проверьте, требуется ли Asset профилактическое обслуживание или калибровка", +Check if Asset requires Preventive Maintenance or Calibration,"Проверьте, требует ли актив профилактического обслуживания или калибровки", Booked Fixed Asset,Забронированные основные средства, Purchase Receipt Amount,Сумма покупки, Default Finance Book,Финансовая книга по умолчанию, Quality Manager,Менеджер по качеству, -Asset Category Name,Asset Категория Название, +Asset Category Name,Название категории активов, Depreciation Options,Варианты амортизации, Enable Capital Work in Progress Accounting,Включить капитальную работу в процессе учета, Finance Book Detail,Финансовая книга, Asset Category Account,Счет категории активов, Fixed Asset Account,Счет учета основных средств, -Accumulated Depreciation Account,Начисленной амортизации Счет, +Accumulated Depreciation Account,Счет накопленной амортизации, Depreciation Expense Account,Износ счет расходов, Capital Work In Progress Account,Счет капитальной работы, Asset Finance Book,Финансовая книга по активам, @@ -5441,7 +5443,7 @@ Failure Date,Дата отказа, Assign To Name,Назначить имя, Repair Status,Статус ремонта, Error Description,Описание ошибки, -Downtime,время простоя, +Downtime,Время простоя, Repair Cost,Стоимость ремонта, Manufacturing Manager,Менеджер производства, Current Asset Value,Текущая стоимость актива, @@ -6073,7 +6075,7 @@ Shopify Tax/Shipping Title,Изменить название налога / до ERPNext Account,Учетная запись ERPNext, Shopify Webhook Detail,Узнайте подробности веб-камеры, Webhook ID,Идентификатор Webhook, -Tally Migration,Tally Migration, +Tally Migration,Tally миграция, Master Data,Основные данные, "Data exported from Tally that consists of the Chart of Accounts, Customers, Suppliers, Addresses, Items and UOMs","Данные, экспортированные из Tally, которые состоят из плана счетов, клиентов, поставщиков, адресов, позиций и единиц измерения", Is Master Data Processed,Обработка основных данных, @@ -6082,7 +6084,7 @@ Tally Creditors Account,Счет Tally Creditors, Creditors Account set in Tally,Счет кредиторов установлен в Tally, Tally Debtors Account,Счет Tally должников, Debtors Account set in Tally,Счет дебитора установлен в Tally, -Tally Company,Талли Компания, +Tally Company,Tally Компания, Company Name as per Imported Tally Data,Название компании согласно импортированным данным подсчета, Default UOM,Единица измерения по умолчанию, UOM in case unspecified in imported data,"Единицы измерения, если они не указаны в импортированных данных", @@ -6108,7 +6110,7 @@ Freight and Forwarding Account,Фрахт и пересылка, Creation User,Создание пользователя, "The user that will be used to create Customers, Items and Sales Orders. This user should have the relevant permissions.","Пользователь, который будет использоваться для создания клиентов, товаров и заказов на продажу. Этот пользователь должен иметь соответствующие разрешения.", "This warehouse will be used to create Sales Orders. The fallback warehouse is ""Stores"".",Этот склад будет использоваться для создания заказов на продажу. Резервный склад "Магазины"., -"The fallback series is ""SO-WOO-"".",Аварийная серия "SO-WOO-"., +"The fallback series is ""SO-WOO-"".","Аварийная серия ""SO-WOO-"".", This company will be used to create Sales Orders.,Эта компания будет использоваться для создания заказов на продажу., Delivery After (Days),Доставка после (дней), This is the default offset (days) for the Delivery Date in Sales Orders. The fallback offset is 7 days from the order placement date.,Это смещение по умолчанию (дни) для даты поставки в заказах на продажу. Смещение отступления составляет 7 дней с даты размещения заказа., @@ -6441,7 +6443,7 @@ Job Applicant,Соискатель работы, Applicant Name,Имя заявителя, Appointment Date,Назначенная дата, Appointment Letter Template,Шаблон письма о назначении, -Body,Тело, +Body,Содержимое, Closing Notes,Заметки, Appointment Letter content,Письмо о назначении, Appraisal,Оценка, @@ -6455,7 +6457,7 @@ Appraisal Goal,Цель оценки, Key Responsibility Area,Основная зона ответственности, Weightage (%),Весовая нагрузка (%), Score (0-5),Оценка (0-5), -Score Earned,Оценка Заработано, +Score Earned,Оценка получена, Appraisal Template Title,Название шаблона оценки, Appraisal Template Goal,Цель шаблона оценки, KRA,КРА, @@ -6747,7 +6749,7 @@ Applicant Email Address,Адрес электронной почты заяви Awaiting Response,В ожидании ответа, Job Offer Terms,Условия работы, Select Terms and Conditions,Выберите Сроки и условия, -Printing Details,Печатать Подробности, +Printing Details,Подробности печати, Job Offer Term,Срок действия предложения, Offer Term,Условие предложения, Value / Description,Значение / Описание, @@ -7520,7 +7522,7 @@ Expected Time (in hours),Ожидаемое время (в часах), Is Milestone,Является этапом, Task Description,Описание задания, Dependencies,Зависимости, -Dependent Tasks,Зависимые задачи, +Dependent Tasks,Зависит от задач, Depends on Tasks,Зависит от задач, Actual Start Date (via Time Sheet),Фактическая дата начала (по табелю учета рабочего времени), Actual Time (in hours),Фактическое время (в часах), @@ -7645,7 +7647,7 @@ Campaign Schedules,Расписание кампаний, Buyer of Goods and Services.,Покупатель товаров и услуг., CUST-.YYYY.-,CUST-.YYYY.-, Default Company Bank Account,Стандартный банковский счет компании, -From Lead,Из Лида, +From Lead,Из лида, Account Manager,Менеджер по работе с клиентами, Allow Sales Invoice Creation Without Sales Order,Разрешить создание счета без заказа на продажу, Allow Sales Invoice Creation Without Delivery Note,Разрешить создание счета без накладной, @@ -7818,14 +7820,14 @@ Phone No,Номер телефона, Company Description,Описание компании, Registration Details,Регистрационные данные, Company registration numbers for your reference. Tax numbers etc.,Регистрационные номера компании для вашей справки. Налоговые числа и т.д., -Delete Company Transactions,Удалить Сделки Компания, +Delete Company Transactions,Удалить транзакции компании, Currency Exchange,Курс обмена валюты, Specify Exchange Rate to convert one currency into another,Укажите Курс конвертировать одну валюту в другую, From Currency,Из валюты, To Currency,В валюту, For Buying,Для покупки, For Selling,Для продажи, -Customer Group Name,Группа Имя клиента, +Customer Group Name,Название группы клиентов, Parent Customer Group,Родительская группа клиента, Only leaf nodes are allowed in transaction,Только листовые узлы допускаются в сделке, Mention if non-standard receivable account applicable,Упоминание если нестандартная задолженность счет применимо, @@ -7893,7 +7895,7 @@ This is the number of the last created transaction with this prefix,Это чи Update Series Number,Обновить Идентификаторы по Номеру, Quotation Lost Reason,Причина Отказа от Предложения, A third party distributor / dealer / commission agent / affiliate / reseller who sells the companies products for a commission.,"Сторонний дистрибьютер, дилер, агент, филиал или реселлер, который продаёт продукты компании за комиссионное вознаграждение.", -Sales Partner Name,Имя Партнера по продажам, +Sales Partner Name,Имя партнера по продажам, Partner Type,Тип партнера, Address & Contacts,Адрес и контакты, Address Desc,Адрес по убыванию, @@ -7914,7 +7916,7 @@ Sales Person Targets,Цели продавца, Set targets Item Group-wise for this Sales Person.,Задайте цели Продуктовых Групп для Продавца, Supplier Group Name,Название группы поставщиков, Parent Supplier Group,Родительская группа поставщиков, -Target Detail,Цель Подробности, +Target Detail,Подробности цели, Target Qty,Целевое количество, Target Amount,Целевая сумма, Target Distribution,Распределение цели, @@ -7973,13 +7975,13 @@ Is Return,Является Вернуться, Issue Credit Note,Кредитная кредитная карта, Return Against Delivery Note,Вернуться На накладной, Customer's Purchase Order No,Клиентам Заказ Нет, -Billing Address Name,Адрес для выставления счета Имя, +Billing Address Name,Название адреса для выставления счета, Required only for sample item.,Требуется только для образца пункта., "If you have created a standard template in Sales Taxes and Charges Template, select one and click on the button below.","Если вы создали стандартный шаблон в шаблонах Налоги с налогами и сбором платежей, выберите его и нажмите кнопку ниже.", In Words will be visible once you save the Delivery Note.,По словам будет виден только вы сохраните накладной., In Words (Export) will be visible once you save the Delivery Note.,В Слов (Экспорт) будут видны только вы сохраните накладной., Transporter Info,Информация для транспортировки, -Driver Name,Имя драйвера, +Driver Name,Имя водителя, Track this Delivery Note against any Project,Подписка на Delivery Note против любого проекта, Inter Company Reference,Справочник Интер, Print Without Amount,Распечатать без суммы, @@ -8079,7 +8081,7 @@ Delivered by Supplier (Drop Ship),Доставка поставщиком, Supplier Items,Продукты поставщика, Foreign Trade Details,Сведения о внешней торговле, Country of Origin,Страна происхождения, -Sales Details,Продажи Подробности, +Sales Details,Детали продажи, Default Sales Unit of Measure,Единица измерения продаж по умолчанию, Is Sales Item,Продаваемый продукт, Max Discount (%),Макс. скидка (%), @@ -8117,7 +8119,7 @@ Item Alternative,Альтернативный продукт, Alternative Item Code,Альтернативный код продукта, Two-way,Двусторонний, Alternative Item Name,Альтернативное название продукта, -Attribute Name,Имя атрибута, +Attribute Name,Название атрибута, Numeric Values,Числовые значения, From Range,От хребта, Increment,Приращение, @@ -8236,8 +8238,8 @@ Transporter Details,Детали транспорта, Vehicle Number,Номер транспортного средства, Vehicle Date,Дата транспортного средства, Received and Accepted,Получил и принял, -Accepted Quantity,Принято Количество, -Rejected Quantity,Отклонен Количество, +Accepted Quantity,Количество принятых, +Rejected Quantity,Количество отклоненных, Accepted Qty as per Stock UOM,Принятое количество в соответствии с единицами измерения запаса, Sample Quantity,Количество образцов, Rate and Amount,Ставку и сумму, @@ -8285,7 +8287,7 @@ Out of AMC,Из КУА, Warranty Period (Days),Гарантийный срок (дней), Serial No Details,Серийный номер подробнее, MAT-STE-.YYYY.-,MAT-STE-.YYYY.-, -Stock Entry Type,Тип входа, +Stock Entry Type,Тип складской записи, Stock Entry (Outward GIT),Вход в акции (внешний GIT), Material Consumption for Manufacture,Потребление материала для производства, Repack,Перепаковать, @@ -8447,7 +8449,7 @@ No of Sent SMS,Кол-во отправленных SMS, Sent To,Отправить, Absent Student Report,Отчет о пропуске занятия, Assessment Plan Status,Статус плана оценки, -Asset Depreciation Ledger,Износ Леджер активов, +Asset Depreciation Ledger,Книга амортизации основных средств, Asset Depreciations and Balances,Активов Амортизация и противовесов, Available Stock for Packing Items,Доступные Запасы для Комплектации Продуктов, Bank Clearance Summary,Банк уплата по счетам итого, @@ -8559,7 +8561,7 @@ Sales Order Trends,Динамика по сделкам, Sales Partner Commission Summary,Сводка комиссий партнеров по продажам, Sales Partner Target Variance based on Item Group,Целевое отклонение партнера по продажам на основе группы товаров, Sales Partner Transaction Summary,Сводка по сделкам с партнерами по продажам, -Sales Partners Commission,Комиссионные Партнеров по продажам, +Sales Partners Commission,Комиссия партнеров по продажам, Invoiced Amount (Exclusive Tax),Сумма счета (без учета налога), Average Commission Rate,Средний уровень комиссии, Sales Payment Summary,Сводка по продажам, @@ -8579,7 +8581,7 @@ Student Fee Collection,Сбор студенческой платы, Student Monthly Attendance Sheet,Ежемесячная посещаемость студентов, Subcontracted Item To Be Received,"Субподрядный предмет, подлежащий получению", Subcontracted Raw Materials To Be Transferred,Субподрядное сырье для передачи, -Supplier Ledger Summary,Список поставщиков, +Supplier Ledger Summary,Сводка книги поставщиков, Supplier-Wise Sales Analytics,Аналитика продаж в разрезе поставщиков, Support Hour Distribution,Распределение поддержки, TDS Computation Summary,Сводка расчетов TDS, @@ -9242,7 +9244,7 @@ Tasks Completed,Задачи выполнены, Tasks Overdue,Просроченные задачи, Completion,Завершение, Provident Fund Deductions,Отчисления в резервный фонд, -Purchase Order Analysis,Анализ заказа на закупку, +Purchase Order Analysis,Анализ заказов на закупку, From and To Dates are required.,Укажите даты от и до., To Date cannot be before From Date.,Дата не может быть раньше даты начала., Qty to Bill,Кол-во к счету, @@ -9267,7 +9269,7 @@ Sales Order Analysis,Анализ заказов на продажу, Amount Delivered,Сумма доставки, Delay (in Days),Задержка (в днях), Group by Sales Order,Группировать по заказу на продажу, - Sales Value,Объем продаж, + Sales Value, Объем продаж, Stock Qty vs Serial No Count,Кол-во на складе по сравнению с серийным номером, Serial No Count,Серийный номер, Work Order Summary,Сводка заказа на работу, @@ -9320,8 +9322,8 @@ Error creating membership entry for {0},Ошибка создания запис A customer is already linked to this Member,Клиент уже привязан к этому участнику, End Date must not be lesser than Start Date,Дата окончания не должна быть меньше даты начала., Employee {0} already has Active Shift {1}: {2},Сотрудник {0} уже имеет активную смену {1}: {2}, - from {0},от {0}, - to {0},в {0}, + from {0}, от {0}, + to {0}, в {0}, Please select Employee first.,"Пожалуйста, сначала выберите сотрудника.", Please set {0} for the Employee or for Department: {1},Установите {0} для сотрудника или отдела: {1}, To Date should be greater than From Date,"Дата до должна быть больше, чем Дата", @@ -9838,3 +9840,8 @@ Enable European Access,Включить европейский доступ, Creating Purchase Order ...,Создание заказа на поставку ..., "Select a Supplier from the Default Suppliers of the items below. On selection, a Purchase Order will be made against items belonging to the selected Supplier only.","Выберите поставщика из списка поставщиков по умолчанию для позиций ниже. При выборе Заказ на поставку будет сделан в отношении товаров, принадлежащих только выбранному Поставщику.", Row #{}: You must select {} serial numbers for item {}.,Строка № {}: необходимо выбрать {} серийных номеров для позиции {}., +Items & Pricing,Продукты и цены, +Overdue,Просрочено, +Completed,Завершенно, +Total Tasks,Всего задач, +Build,Конструктор, From b9dbb36d0e55eb4f12e067032f5e7e93875304e3 Mon Sep 17 00:00:00 2001 From: Deepesh Garg Date: Thu, 9 Jun 2022 18:58:04 +0530 Subject: [PATCH 099/105] chore: Linting Issues --- erpnext/accounts/report/trial_balance/trial_balance.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/erpnext/accounts/report/trial_balance/trial_balance.py b/erpnext/accounts/report/trial_balance/trial_balance.py index af447df52a8..26572130d26 100644 --- a/erpnext/accounts/report/trial_balance/trial_balance.py +++ b/erpnext/accounts/report/trial_balance/trial_balance.py @@ -161,7 +161,9 @@ def get_rootwise_opening_balances(filters, report_type): additional_conditions += " and project = %(project)s" if filters.get("include_default_book_entries"): - additional_conditions += "AND (finance_book in (%(finance_book)s, %(company_fb)s, '') OR finance_book IS NULL)" + additional_conditions += ( + "AND (finance_book in (%(finance_book)s, %(company_fb)s, '') OR finance_book IS NULL)" + ) else: additional_conditions += "AND (finance_book in (%(finance_book)s, '') OR finance_book IS NULL)" From c13e5ad741de68a51ae478727c35dea5fb6f2390 Mon Sep 17 00:00:00 2001 From: Deepesh Garg Date: Thu, 9 Jun 2022 19:18:52 +0530 Subject: [PATCH 100/105] fix: Reset represents company on disabling internal customer and supplier (#31302) --- erpnext/buying/doctype/supplier/supplier.py | 3 +++ erpnext/selling/doctype/customer/customer.py | 3 +++ 2 files changed, 6 insertions(+) diff --git a/erpnext/buying/doctype/supplier/supplier.py b/erpnext/buying/doctype/supplier/supplier.py index 97d0ba0b9c1..43152e89a83 100644 --- a/erpnext/buying/doctype/supplier/supplier.py +++ b/erpnext/buying/doctype/supplier/supplier.py @@ -84,6 +84,9 @@ class Supplier(TransactionBase): self.save() def validate_internal_supplier(self): + if not self.is_internal_supplier: + self.represents_company = "" + internal_supplier = frappe.db.get_value( "Supplier", { diff --git a/erpnext/selling/doctype/customer/customer.py b/erpnext/selling/doctype/customer/customer.py index 8889a5f939a..35e0b0de407 100644 --- a/erpnext/selling/doctype/customer/customer.py +++ b/erpnext/selling/doctype/customer/customer.py @@ -141,6 +141,9 @@ class Customer(TransactionBase): ) def validate_internal_customer(self): + if not self.is_internal_customer: + self.represents_company = "" + internal_customer = frappe.db.get_value( "Customer", { From ee2949aa3fa221877d7a16a02e1e3164894f8219 Mon Sep 17 00:00:00 2001 From: Sun Howwrongbum Date: Thu, 9 Jun 2022 19:28:59 +0530 Subject: [PATCH 101/105] fix: typo in sql condition --- erpnext/accounts/report/trial_balance/trial_balance.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/erpnext/accounts/report/trial_balance/trial_balance.py b/erpnext/accounts/report/trial_balance/trial_balance.py index 26572130d26..6bd08ad837a 100644 --- a/erpnext/accounts/report/trial_balance/trial_balance.py +++ b/erpnext/accounts/report/trial_balance/trial_balance.py @@ -162,10 +162,10 @@ def get_rootwise_opening_balances(filters, report_type): if filters.get("include_default_book_entries"): additional_conditions += ( - "AND (finance_book in (%(finance_book)s, %(company_fb)s, '') OR finance_book IS NULL)" + " AND (finance_book in (%(finance_book)s, %(company_fb)s, '') OR finance_book IS NULL)" ) else: - additional_conditions += "AND (finance_book in (%(finance_book)s, '') OR finance_book IS NULL)" + additional_conditions += " AND (finance_book in (%(finance_book)s, '') OR finance_book IS NULL)" accounting_dimensions = get_accounting_dimensions(as_list=False) From 6fc32b83c89eed916e108a6f2ad7e55c144d7fd7 Mon Sep 17 00:00:00 2001 From: Ankush Menat Date: Fri, 10 Jun 2022 11:03:51 +0530 Subject: [PATCH 102/105] fix: revert show title field on Employee doctype (#31312) --- erpnext/hr/doctype/employee/employee.json | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/erpnext/hr/doctype/employee/employee.json b/erpnext/hr/doctype/employee/employee.json index a3638e1a650..42479143e74 100644 --- a/erpnext/hr/doctype/employee/employee.json +++ b/erpnext/hr/doctype/employee/employee.json @@ -827,7 +827,7 @@ "idx": 24, "image_field": "image", "links": [], - "modified": "2022-04-22 16:21:55.811983", + "modified": "2022-06-10 01:29:32.952091", "modified_by": "Administrator", "module": "HR", "name": "Employee", @@ -872,7 +872,6 @@ ], "search_fields": "employee_name", "show_name_in_global_search": 1, - "show_title_field_in_link": 1, "sort_field": "modified", "sort_order": "DESC", "states": [], From 1646fbe478fefaa173c2c1e009d1a5d0dcb13326 Mon Sep 17 00:00:00 2001 From: Ankush Menat Date: Fri, 10 Jun 2022 13:52:17 +0530 Subject: [PATCH 103/105] refactor: remove add_fetch (#31315) - Sales Team already had fetch from set up - Set up fetch from on sales partner in sales transaction Reason for removal: the JS code applies arbitrarily to any field called "sales_person" --- erpnext/accounts/doctype/sales_invoice/sales_invoice.json | 4 +++- erpnext/selling/doctype/sales_order/sales_order.json | 4 +++- erpnext/selling/sales_common.js | 4 +--- erpnext/stock/doctype/delivery_note/delivery_note.json | 4 +++- 4 files changed, 10 insertions(+), 6 deletions(-) diff --git a/erpnext/accounts/doctype/sales_invoice/sales_invoice.json b/erpnext/accounts/doctype/sales_invoice/sales_invoice.json index 80b95db8868..327545aa54e 100644 --- a/erpnext/accounts/doctype/sales_invoice/sales_invoice.json +++ b/erpnext/accounts/doctype/sales_invoice/sales_invoice.json @@ -1790,6 +1790,8 @@ "width": "50%" }, { + "fetch_from": "sales_partner.commission_rate", + "fetch_if_empty": 1, "fieldname": "commission_rate", "fieldtype": "Float", "hide_days": 1, @@ -2038,7 +2040,7 @@ "link_fieldname": "consolidated_invoice" } ], - "modified": "2022-03-08 16:08:53.517903", + "modified": "2022-06-10 03:52:51.409913", "modified_by": "Administrator", "module": "Accounts", "name": "Sales Invoice", diff --git a/erpnext/selling/doctype/sales_order/sales_order.json b/erpnext/selling/doctype/sales_order/sales_order.json index ff921c721df..74c5c07e47b 100644 --- a/erpnext/selling/doctype/sales_order/sales_order.json +++ b/erpnext/selling/doctype/sales_order/sales_order.json @@ -1359,6 +1359,8 @@ "width": "50%" }, { + "fetch_from": "sales_partner.commission_rate", + "fetch_if_empty": 1, "fieldname": "commission_rate", "fieldtype": "Float", "hide_days": 1, @@ -1547,7 +1549,7 @@ "idx": 105, "is_submittable": 1, "links": [], - "modified": "2022-04-26 14:38:18.350207", + "modified": "2022-06-10 03:52:22.212953", "modified_by": "Administrator", "module": "Selling", "name": "Sales Order", diff --git a/erpnext/selling/sales_common.js b/erpnext/selling/sales_common.js index 6cb53c3bbe3..8ff01f5cb4c 100644 --- a/erpnext/selling/sales_common.js +++ b/erpnext/selling/sales_common.js @@ -12,8 +12,6 @@ frappe.provide("erpnext.selling"); erpnext.selling.SellingController = class SellingController extends erpnext.TransactionController { setup() { super.setup(); - this.frm.add_fetch("sales_partner", "commission_rate", "commission_rate"); - this.frm.add_fetch("sales_person", "commission_rate", "commission_rate"); } onload() { @@ -514,4 +512,4 @@ frappe.ui.form.on(cur_frm.doctype, { dialog.show(); } -}) \ No newline at end of file +}) diff --git a/erpnext/stock/doctype/delivery_note/delivery_note.json b/erpnext/stock/doctype/delivery_note/delivery_note.json index e3222bc8850..f9e934921d8 100644 --- a/erpnext/stock/doctype/delivery_note/delivery_note.json +++ b/erpnext/stock/doctype/delivery_note/delivery_note.json @@ -1192,6 +1192,8 @@ "width": "50%" }, { + "fetch_from": "sales_partner.commission_rate", + "fetch_if_empty": 1, "fieldname": "commission_rate", "fieldtype": "Float", "label": "Commission Rate (%)", @@ -1334,7 +1336,7 @@ "idx": 146, "is_submittable": 1, "links": [], - "modified": "2022-04-26 14:48:08.781837", + "modified": "2022-06-10 03:52:04.197415", "modified_by": "Administrator", "module": "Stock", "name": "Delivery Note", From 3a3d13622d9cdae52e11b728a196602da70d3ec3 Mon Sep 17 00:00:00 2001 From: Ankush Menat Date: Fri, 10 Jun 2022 14:01:41 +0530 Subject: [PATCH 104/105] refactor!: drop github connector from ERPNext (#31316) --- .../connectors/github_connection.py | 44 ------------------- .../data_migration_mapping/__init__.py | 0 .../issue_to_task/__init__.py | 12 ----- .../issue_to_task/issue_to_task.json | 36 --------------- .../milestone_to_project/__init__.py | 6 --- .../milestone_to_project.json | 36 --------------- .../github_sync/github_sync.json | 22 ---------- requirements.txt | 1 - 8 files changed, 157 deletions(-) delete mode 100644 erpnext/erpnext_integrations/connectors/github_connection.py delete mode 100644 erpnext/erpnext_integrations/data_migration_mapping/__init__.py delete mode 100644 erpnext/erpnext_integrations/data_migration_mapping/issue_to_task/__init__.py delete mode 100644 erpnext/erpnext_integrations/data_migration_mapping/issue_to_task/issue_to_task.json delete mode 100644 erpnext/erpnext_integrations/data_migration_mapping/milestone_to_project/__init__.py delete mode 100644 erpnext/erpnext_integrations/data_migration_mapping/milestone_to_project/milestone_to_project.json delete mode 100644 erpnext/erpnext_integrations/data_migration_plan/github_sync/github_sync.json diff --git a/erpnext/erpnext_integrations/connectors/github_connection.py b/erpnext/erpnext_integrations/connectors/github_connection.py deleted file mode 100644 index f28065e7248..00000000000 --- a/erpnext/erpnext_integrations/connectors/github_connection.py +++ /dev/null @@ -1,44 +0,0 @@ -import frappe -from frappe.data_migration.doctype.data_migration_connector.connectors.base import BaseConnection -from github import Github - -class GithubConnection(BaseConnection): - def __init__(self, connector): - self.connector = connector - - try: - password = self.get_password() - except frappe.AuthenticationError: - password = None - - if self.connector.username and password: - self.connection = Github(self.connector.username, self.get_password()) - else: - self.connection = Github() - - self.name_field = 'id' - - def insert(self, doctype, doc): - pass - - def update(self, doctype, doc, migration_id): - pass - - def delete(self, doctype, migration_id): - pass - - def get(self, remote_objectname, fields=None, filters=None, start=0, page_length=10): - repo = filters.get('repo') - - if remote_objectname == 'Milestone': - return self.get_milestones(repo, start, page_length) - if remote_objectname == 'Issue': - return self.get_issues(repo, start, page_length) - - def get_milestones(self, repo, start=0, page_length=10): - _repo = self.connection.get_repo(repo) - return list(_repo.get_milestones()[start:start+page_length]) - - def get_issues(self, repo, start=0, page_length=10): - _repo = self.connection.get_repo(repo) - return list(_repo.get_issues()[start:start+page_length]) diff --git a/erpnext/erpnext_integrations/data_migration_mapping/__init__.py b/erpnext/erpnext_integrations/data_migration_mapping/__init__.py deleted file mode 100644 index e69de29bb2d..00000000000 diff --git a/erpnext/erpnext_integrations/data_migration_mapping/issue_to_task/__init__.py b/erpnext/erpnext_integrations/data_migration_mapping/issue_to_task/__init__.py deleted file mode 100644 index 616ecfbac68..00000000000 --- a/erpnext/erpnext_integrations/data_migration_mapping/issue_to_task/__init__.py +++ /dev/null @@ -1,12 +0,0 @@ -import frappe - - -def pre_process(issue): - - project = frappe.db.get_value("Project", filters={"project_name": issue.milestone}) - return { - "title": issue.title, - "body": frappe.utils.md_to_html(issue.body or ""), - "state": issue.state.title(), - "project": project or "", - } diff --git a/erpnext/erpnext_integrations/data_migration_mapping/issue_to_task/issue_to_task.json b/erpnext/erpnext_integrations/data_migration_mapping/issue_to_task/issue_to_task.json deleted file mode 100644 index e945ba22617..00000000000 --- a/erpnext/erpnext_integrations/data_migration_mapping/issue_to_task/issue_to_task.json +++ /dev/null @@ -1,36 +0,0 @@ -{ - "condition": "{\"repo\":\"frappe/erpnext\"}", - "creation": "2017-10-16 16:03:32.772191", - "docstatus": 0, - "doctype": "Data Migration Mapping", - "fields": [ - { - "is_child_table": 0, - "local_fieldname": "subject", - "remote_fieldname": "title" - }, - { - "is_child_table": 0, - "local_fieldname": "description", - "remote_fieldname": "body" - }, - { - "is_child_table": 0, - "local_fieldname": "status", - "remote_fieldname": "state" - } - ], - "idx": 0, - "local_doctype": "Task", - "local_primary_key": "name", - "mapping_name": "Issue to Task", - "mapping_type": "Pull", - "migration_id_field": "github_sync_id", - "modified": "2017-10-20 11:48:54.575993", - "modified_by": "Administrator", - "name": "Issue to Task", - "owner": "Administrator", - "page_length": 10, - "remote_objectname": "Issue", - "remote_primary_key": "id" -} \ No newline at end of file diff --git a/erpnext/erpnext_integrations/data_migration_mapping/milestone_to_project/__init__.py b/erpnext/erpnext_integrations/data_migration_mapping/milestone_to_project/__init__.py deleted file mode 100644 index d44fc0454ca..00000000000 --- a/erpnext/erpnext_integrations/data_migration_mapping/milestone_to_project/__init__.py +++ /dev/null @@ -1,6 +0,0 @@ -def pre_process(milestone): - return { - "title": milestone.title, - "description": milestone.description, - "state": milestone.state.title(), - } diff --git a/erpnext/erpnext_integrations/data_migration_mapping/milestone_to_project/milestone_to_project.json b/erpnext/erpnext_integrations/data_migration_mapping/milestone_to_project/milestone_to_project.json deleted file mode 100644 index 5a3e07e37ea..00000000000 --- a/erpnext/erpnext_integrations/data_migration_mapping/milestone_to_project/milestone_to_project.json +++ /dev/null @@ -1,36 +0,0 @@ -{ - "condition": "{\"repo\": \"frappe/erpnext\"}", - "creation": "2017-10-13 11:16:49.664925", - "docstatus": 0, - "doctype": "Data Migration Mapping", - "fields": [ - { - "is_child_table": 0, - "local_fieldname": "project_name", - "remote_fieldname": "title" - }, - { - "is_child_table": 0, - "local_fieldname": "notes", - "remote_fieldname": "description" - }, - { - "is_child_table": 0, - "local_fieldname": "status", - "remote_fieldname": "state" - } - ], - "idx": 0, - "local_doctype": "Project", - "local_primary_key": "project_name", - "mapping_name": "Milestone to Project", - "mapping_type": "Pull", - "migration_id_field": "github_sync_id", - "modified": "2017-10-20 11:48:54.552305", - "modified_by": "Administrator", - "name": "Milestone to Project", - "owner": "Administrator", - "page_length": 10, - "remote_objectname": "Milestone", - "remote_primary_key": "id" -} \ No newline at end of file diff --git a/erpnext/erpnext_integrations/data_migration_plan/github_sync/github_sync.json b/erpnext/erpnext_integrations/data_migration_plan/github_sync/github_sync.json deleted file mode 100644 index 20eb387cd85..00000000000 --- a/erpnext/erpnext_integrations/data_migration_plan/github_sync/github_sync.json +++ /dev/null @@ -1,22 +0,0 @@ -{ - "creation": "2017-10-13 11:16:53.600026", - "docstatus": 0, - "doctype": "Data Migration Plan", - "idx": 0, - "mappings": [ - { - "enabled": 1, - "mapping": "Milestone to Project" - }, - { - "enabled": 1, - "mapping": "Issue to Task" - } - ], - "modified": "2017-10-20 11:48:54.496123", - "modified_by": "Administrator", - "module": "ERPNext Integrations", - "name": "GitHub Sync", - "owner": "Administrator", - "plan_name": "GitHub Sync" -} \ No newline at end of file diff --git a/requirements.txt b/requirements.txt index 85ff515772d..83e53758be2 100644 --- a/requirements.txt +++ b/requirements.txt @@ -3,7 +3,6 @@ gocardless-pro~=1.22.0 googlemaps plaid-python~=7.2.1 pycountry~=20.7.3 -PyGithub~=1.55 python-stdnum~=1.16 python-youtube~=0.8.0 taxjar~=1.9.2 From 74b274f555801356b1ef05577eb4cb2cfb79b8d3 Mon Sep 17 00:00:00 2001 From: hendrik Date: Fri, 10 Jun 2022 16:22:53 +0700 Subject: [PATCH 105/105] fix: update Period Closing Voucher per Company Validate period closing voucher company-wise --- .../doctype/period_closing_voucher/period_closing_voucher.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/erpnext/accounts/doctype/period_closing_voucher/period_closing_voucher.py b/erpnext/accounts/doctype/period_closing_voucher/period_closing_voucher.py index 53b1c64c460..5a86376199c 100644 --- a/erpnext/accounts/doctype/period_closing_voucher/period_closing_voucher.py +++ b/erpnext/accounts/doctype/period_closing_voucher/period_closing_voucher.py @@ -54,8 +54,8 @@ class PeriodClosingVoucher(AccountsController): pce = frappe.db.sql( """select name from `tabPeriod Closing Voucher` - where posting_date > %s and fiscal_year = %s and docstatus = 1""", - (self.posting_date, self.fiscal_year), + where posting_date > %s and fiscal_year = %s and docstatus = 1 and company = %s""", + (self.posting_date, self.fiscal_year, self.company), ) if pce and pce[0][0]: frappe.throw(