From 8396f24e70520eed6c6b73a90a7b92349cbb2139 Mon Sep 17 00:00:00 2001 From: GangaManoj Date: Mon, 20 Sep 2021 19:01:46 +0530 Subject: [PATCH 1/7] fix: Unlink PO on cancelling SO --- erpnext/controllers/accounts_controller.py | 49 ++++++++++++++++++- .../doctype/sales_order/sales_order.py | 6 +++ 2 files changed, 54 insertions(+), 1 deletion(-) diff --git a/erpnext/controllers/accounts_controller.py b/erpnext/controllers/accounts_controller.py index b90db054b5..4da7a72b45 100644 --- a/erpnext/controllers/accounts_controller.py +++ b/erpnext/controllers/accounts_controller.py @@ -19,7 +19,8 @@ from frappe.utils import ( get_link_to_form, getdate, nowdate, - today, + now, + today ) from six import text_type @@ -811,6 +812,52 @@ class AccountsController(TransactionBase): if frappe.db.get_single_value('Accounts Settings', 'unlink_advance_payment_on_cancelation_of_order'): unlink_ref_doc_from_payment_entries(self) + if self.doctype == "Sales Order": + self.unlink_ref_doc_from_po() + + def unlink_ref_doc_from_po(self): + print("\n"*5, "*"*50, "\n"*5) + + so_items = [] + for item in self.items: + so_items.append(item.name) + + print("SO Items: ", so_items) + + linked_po = frappe.get_all( + 'Purchase Order Item', + filters = { + 'sales_order': self.name, + 'sales_order_item': ['in', so_items], + 'docstatus': ['<', 2] + }, + pluck='parent' + ) + + print("Before unlinking: ", linked_po) + + if linked_po: + frappe.db.sql("""update `tabPurchase Order Item` + set sales_order = null, sales_order_item = null, + modified = %s, modified_by = %s + where sales_order = %s and sales_order_item in %s + and docstatus < 2""", (now(), frappe.session.user, self.name, so_items)) + + frappe.msgprint(_("Purchase Orders {0} are un-linked").format("\n".join(linked_po))) + + linked_po = frappe.get_all( + 'Purchase Order Item', + filters = { + 'sales_order': self.name, + 'sales_order_item': ['in', so_items], + 'docstatus': ['<', 2] + }, + pluck='parent' + ) + print("After unlinking: ", linked_po) + + print("\n"*5, "*"*50, "\n"*5) + def get_tax_map(self): tax_map = {} for tax in self.get('taxes'): diff --git a/erpnext/selling/doctype/sales_order/sales_order.py b/erpnext/selling/doctype/sales_order/sales_order.py index 9367609421..4397d19dca 100755 --- a/erpnext/selling/doctype/sales_order/sales_order.py +++ b/erpnext/selling/doctype/sales_order/sales_order.py @@ -209,6 +209,12 @@ class SalesOrder(SellingController): from erpnext.accounts.doctype.pricing_rule.utils import update_coupon_code_count update_coupon_code_count(self.coupon_code,'cancelled') + def cancel(self): + import pdb + + pdb.set_trace() + super(SalesOrder, self).cancel() + def update_project(self): if frappe.db.get_single_value('Selling Settings', 'sales_update_frequency') != "Each Transaction": return From e77534fe13a8384a066352f028eb26fb3520f26f Mon Sep 17 00:00:00 2001 From: GangaManoj Date: Mon, 20 Sep 2021 19:01:46 +0530 Subject: [PATCH 2/7] fix: Unlink PO on cancelling SO --- erpnext/controllers/accounts_controller.py | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/erpnext/controllers/accounts_controller.py b/erpnext/controllers/accounts_controller.py index 8289b8a080..075069c6e8 100644 --- a/erpnext/controllers/accounts_controller.py +++ b/erpnext/controllers/accounts_controller.py @@ -837,11 +837,16 @@ class AccountsController(TransactionBase): print("Before unlinking: ", linked_po) if linked_po: - frappe.db.sql("""update `tabPurchase Order Item` - set sales_order = null, sales_order_item = null, - modified = %s, modified_by = %s - where sales_order = %s and sales_order_item in %s - and docstatus < 2""", (now(), frappe.session.user, self.name, so_items)) + frappe.db.set_value( + 'Purchase Order Item', { + 'sales_order': self.name, + 'sales_order_item': ['in', so_items], + 'docstatus': ['<', 2] + },{ + 'sales_order': None, + 'sales_order_item': None + } + ) frappe.msgprint(_("Purchase Orders {0} are un-linked").format("\n".join(linked_po))) From d786855d94a8d60be34ddd34e4b7a013bc7d4480 Mon Sep 17 00:00:00 2001 From: Subin Tom Date: Thu, 28 Oct 2021 13:19:19 +0530 Subject: [PATCH 3/7] fix: Consolidated Financial Report throws error for empty equity data list --- .../consolidated_financial_statement.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/erpnext/accounts/report/consolidated_financial_statement/consolidated_financial_statement.py b/erpnext/accounts/report/consolidated_financial_statement/consolidated_financial_statement.py index 0de2a9854d..0475231a93 100644 --- a/erpnext/accounts/report/consolidated_financial_statement/consolidated_financial_statement.py +++ b/erpnext/accounts/report/consolidated_financial_statement/consolidated_financial_statement.py @@ -114,8 +114,9 @@ def prepare_companywise_opening_balance(asset_data, liability_data, equity_data, # opening_value = Aseet - liability - equity for data in [asset_data, liability_data, equity_data]: - account_name = get_root_account_name(data[0].root_type, company) - opening_value += (get_opening_balance(account_name, data, company) or 0.0) + if data: + account_name = get_root_account_name(data[0].root_type, company) + opening_value += (get_opening_balance(account_name, data, company) or 0.0) opening_balance[company] = opening_value From 8844fdbf230ef167c0051935a8f922c5c6bb13de Mon Sep 17 00:00:00 2001 From: GangaManoj Date: Thu, 28 Oct 2021 19:25:55 +0530 Subject: [PATCH 4/7] fix: Remove print statements --- erpnext/controllers/accounts_controller.py | 19 ------------------- 1 file changed, 19 deletions(-) diff --git a/erpnext/controllers/accounts_controller.py b/erpnext/controllers/accounts_controller.py index bfbbf4d140..9267eb5b8e 100644 --- a/erpnext/controllers/accounts_controller.py +++ b/erpnext/controllers/accounts_controller.py @@ -820,14 +820,10 @@ class AccountsController(TransactionBase): self.unlink_ref_doc_from_po() def unlink_ref_doc_from_po(self): - print("\n"*5, "*"*50, "\n"*5) - so_items = [] for item in self.items: so_items.append(item.name) - print("SO Items: ", so_items) - linked_po = frappe.get_all( 'Purchase Order Item', filters = { @@ -838,8 +834,6 @@ class AccountsController(TransactionBase): pluck='parent' ) - print("Before unlinking: ", linked_po) - if linked_po: frappe.db.set_value( 'Purchase Order Item', { @@ -854,19 +848,6 @@ class AccountsController(TransactionBase): frappe.msgprint(_("Purchase Orders {0} are un-linked").format("\n".join(linked_po))) - linked_po = frappe.get_all( - 'Purchase Order Item', - filters = { - 'sales_order': self.name, - 'sales_order_item': ['in', so_items], - 'docstatus': ['<', 2] - }, - pluck='parent' - ) - print("After unlinking: ", linked_po) - - print("\n"*5, "*"*50, "\n"*5) - def get_tax_map(self): tax_map = {} for tax in self.get('taxes'): From 1a9d2684e2c8e9f89ea82dbade5cbe9b72f9e364 Mon Sep 17 00:00:00 2001 From: GangaManoj Date: Thu, 28 Oct 2021 19:27:45 +0530 Subject: [PATCH 5/7] fix: Remove debugger --- erpnext/selling/doctype/sales_order/sales_order.py | 6 ------ 1 file changed, 6 deletions(-) diff --git a/erpnext/selling/doctype/sales_order/sales_order.py b/erpnext/selling/doctype/sales_order/sales_order.py index c9490fcae4..dcf478bda6 100755 --- a/erpnext/selling/doctype/sales_order/sales_order.py +++ b/erpnext/selling/doctype/sales_order/sales_order.py @@ -207,12 +207,6 @@ class SalesOrder(SellingController): from erpnext.accounts.doctype.pricing_rule.utils import update_coupon_code_count update_coupon_code_count(self.coupon_code,'cancelled') - def cancel(self): - import pdb - - pdb.set_trace() - super(SalesOrder, self).cancel() - def update_project(self): if frappe.db.get_single_value('Selling Settings', 'sales_update_frequency') != "Each Transaction": return From d24cfff3886b8502c8f98cb4421c5e37a46ec3f2 Mon Sep 17 00:00:00 2001 From: GangaManoj Date: Thu, 28 Oct 2021 20:06:48 +0530 Subject: [PATCH 6/7] fix: Remove unused imports --- erpnext/controllers/accounts_controller.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/erpnext/controllers/accounts_controller.py b/erpnext/controllers/accounts_controller.py index 9267eb5b8e..14ad4228c4 100644 --- a/erpnext/controllers/accounts_controller.py +++ b/erpnext/controllers/accounts_controller.py @@ -19,8 +19,7 @@ from frappe.utils import ( get_link_to_form, getdate, nowdate, - now, - today + today, ) from six import text_type From 051aaa708d7ab8a0019e19c9c8b8371afa03abc6 Mon Sep 17 00:00:00 2001 From: Deepesh Garg Date: Fri, 29 Oct 2021 11:35:34 +0530 Subject: [PATCH 7/7] fix: Ignore PO on So cancel --- erpnext/controllers/accounts_controller.py | 4 ++-- erpnext/selling/doctype/sales_order/sales_order.js | 2 ++ 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/erpnext/controllers/accounts_controller.py b/erpnext/controllers/accounts_controller.py index 14ad4228c4..37b8f9d6d3 100644 --- a/erpnext/controllers/accounts_controller.py +++ b/erpnext/controllers/accounts_controller.py @@ -823,7 +823,7 @@ class AccountsController(TransactionBase): for item in self.items: so_items.append(item.name) - linked_po = frappe.get_all( + linked_po = list(set(frappe.get_all( 'Purchase Order Item', filters = { 'sales_order': self.name, @@ -831,7 +831,7 @@ class AccountsController(TransactionBase): 'docstatus': ['<', 2] }, pluck='parent' - ) + ))) if linked_po: frappe.db.set_value( diff --git a/erpnext/selling/doctype/sales_order/sales_order.js b/erpnext/selling/doctype/sales_order/sales_order.js index f6926906b8..d46c46f90e 100644 --- a/erpnext/selling/doctype/sales_order/sales_order.js +++ b/erpnext/selling/doctype/sales_order/sales_order.js @@ -78,6 +78,8 @@ frappe.ui.form.on("Sales Order", { }); erpnext.queries.setup_warehouse_query(frm); + + frm.ignore_doctypes_on_cancel_all = ['Purchase Order']; }, delivery_date: function(frm) {