From bbd8508783a7264d3b8b5a5e31d685343f7aaa8e Mon Sep 17 00:00:00 2001 From: Manas Solanki Date: Mon, 7 May 2018 13:37:34 +0530 Subject: [PATCH] optimize the patch --- erpnext/controllers/selling_controller.py | 22 ++++-- ...efaults_to_child_table_for_multicompany.py | 79 +++++++++---------- .../doctype/quotation/test_quotation.py | 6 +- .../doctype/sales_order/test_sales_order.py | 9 +-- 4 files changed, 58 insertions(+), 58 deletions(-) diff --git a/erpnext/controllers/selling_controller.py b/erpnext/controllers/selling_controller.py index 2a22b32637..feee067496 100644 --- a/erpnext/controllers/selling_controller.py +++ b/erpnext/controllers/selling_controller.py @@ -340,10 +340,22 @@ class SellingController(StockController): def check_active_sales_items(obj): for d in obj.get("items"): if d.item_code: - item = frappe.db.sql("""select docstatus, - income_account from tabItem where name = %s""", - d.item_code, as_dict=True)[0] + item = frappe.db.sql("""select i.docstatus, id.income_account + from `tabItem` i, `tabItem Default` id + where i.name=%s and id.parent=i.name and id.company=%s""", + (d.item_code,obj.company), as_dict=True)[0] + income_account_set = False if getattr(d, "income_account", None) and not item.income_account: - frappe.db.set_value("Item", d.item_code, "income_account", - d.income_account) + doc = frappe.get_doc("Item", d.item_code) + for default in doc.item_defaults: + if default.company == obj.company: + default.income_account = d.income_account + income_account_set = True + else: + if not income_account_set: + doc.append("item_defaults", { + "company": obj.company, + "income_account": d.income_account + }) + doc.save() \ No newline at end of file diff --git a/erpnext/patches/v11_0/move_item_defaults_to_child_table_for_multicompany.py b/erpnext/patches/v11_0/move_item_defaults_to_child_table_for_multicompany.py index a78d2d6f34..8e17ea61dd 100644 --- a/erpnext/patches/v11_0/move_item_defaults_to_child_table_for_multicompany.py +++ b/erpnext/patches/v11_0/move_item_defaults_to_child_table_for_multicompany.py @@ -15,50 +15,45 @@ def execute(): frappe.reload_doc('stock', 'doctype', 'item_default') frappe.reload_doc('stock', 'doctype', 'item') - item_details = frappe.get_all("Item", fields=["name", "default_warehouse", "buying_cost_center", - "expense_account", "selling_cost_center", "income_account"], limit=100) + companies = frappe.get_all("Company") + if len(companies) == 1: + frappe.db.sql(''' + INSERT INTO `tabItem Default` + (name, parent, parenttype, parentfield, idx, company, default_warehouse, + buying_cost_center, selling_cost_center, expense_account, income_account, default_supplier) + SELECT + SUBSTRING(SHA2(name,224), 1, 10) as name, name as parent, 'Item' as parenttype, + 'item_defaults' as parentfield, 1 as idx, %s as company, default_warehouse, + buying_cost_center, selling_cost_center, expense_account, income_account, default_supplier + FROM `tabItem`; + ''', companies[0].name) + else: + item_details = frappe.get_all("Item", fields=["name", "default_warehouse", "buying_cost_center", + "expense_account", "selling_cost_center", "income_account"], limit=100) - for item in item_details: - item_defaults = [] + for item in item_details: + item_defaults = [] - def insert_into_item_defaults(doc_field_name, doc_field_value, company): - for d in item_defaults: - if d.get("company") == company: - d[doc_field_name] = doc_field_value - return - item_defaults.append({ - "company": company, - doc_field_name: doc_field_value - }) + def insert_into_item_defaults(doc_field_name, doc_field_value, company): + for d in item_defaults: + if d.get("company") == company: + d[doc_field_name] = doc_field_value + return + item_defaults.append({ + "company": company, + doc_field_name: doc_field_value + }) - if item.default_warehouse: - default_warehouse_company = frappe.get_value("Warehouse", item.default_warehouse, "company", cache=True) - insert_into_item_defaults("default_warehouse", item.default_warehouse, default_warehouse_company) + for d in [ + ["default_warehouse", "Warehouse"], ["expense_account", "Account"], ["expense_account", "Account"], + ["buying_cost_center", "Cost Center"], ["selling_cost_center", "Cost Center"] + ]: + if item.get(d[0]): + company = frappe.get_value(d[1], item.get(d[0]), "company", cache=True) + insert_into_item_defaults(d[0], item.get(d[0]), company) - if item.buying_cost_center: - buying_cost_center_company = get_cost_center_company(item.buying_cost_center) - insert_into_item_defaults("buying_cost_center", item.buying_cost_center, buying_cost_center_company) + doc = frappe.get_doc("Item", item.name) + doc.extend("item_defaults", item_defaults) - if item.selling_cost_center: - selling_cost_center_company = get_cost_center_company(item.buying_cost_center) - insert_into_item_defaults("selling_cost_center", item.selling_cost_center, selling_cost_center_company) - - if item.expense_account: - expense_account_company = get_account_company(item.expense_account) - insert_into_item_defaults("expense_account", item.expense_account, expense_account_company) - - if item.income_account: - income_account_company = get_account_company(item.income_account) - insert_into_item_defaults("income_account", item.income_account, income_account_company) - - doc = frappe.get_doc("Item", item.name) - doc.extend("item_defaults", item_defaults) - - for child_doc in doc.item_defaults: - child_doc.db_update() - -def get_account_company(account_name): - return frappe.get_value("Account", account_name, "company", cache=True) - -def get_cost_center_company(cost_center): - return frappe.get_value("Cost Center", cost_center, "company", cache=True) + for child_doc in doc.item_defaults: + child_doc.db_insert() \ No newline at end of file diff --git a/erpnext/selling/doctype/quotation/test_quotation.py b/erpnext/selling/doctype/quotation/test_quotation.py index ced5ebf4fb..3b36a2dca5 100644 --- a/erpnext/selling/doctype/quotation/test_quotation.py +++ b/erpnext/selling/doctype/quotation/test_quotation.py @@ -150,12 +150,10 @@ class TestQuotation(unittest.TestCase): from erpnext.stock.doctype.item.test_item import make_item first_item = make_item("_Test Laptop", - {"is_stock_item": 1, "expense_account": "_Test Account Cost for Goods Sold - _TC", - "cost_center": "_Test Cost Center - _TC"}) + {"is_stock_item": 1}) second_item = make_item("_Test CPU", - {"is_stock_item": 1, "expense_account": "_Test Account Cost for Goods Sold - _TC", - "cost_center": "_Test Cost Center - _TC"}) + {"is_stock_item": 1}) qo_item1 = [ { diff --git a/erpnext/selling/doctype/sales_order/test_sales_order.py b/erpnext/selling/doctype/sales_order/test_sales_order.py index c5f7ef22ec..83889412f4 100644 --- a/erpnext/selling/doctype/sales_order/test_sales_order.py +++ b/erpnext/selling/doctype/sales_order/test_sales_order.py @@ -359,14 +359,9 @@ class TestSalesOrder(unittest.TestCase): make_stock_entry(target="_Test Warehouse - _TC", qty=10, rate=100) - po_item = make_item("_Test Item for Drop Shipping", {"is_stock_item": 1, "delivered_by_supplier": 1, - 'default_supplier': '_Test Supplier', - "expense_account": "_Test Account Cost for Goods Sold - _TC", - "cost_center": "_Test Cost Center - _TC" - }) + po_item = make_item("_Test Item for Drop Shipping", {"is_stock_item": 1, "delivered_by_supplier": 1}) - dn_item = make_item("_Test Regular Item", {"is_stock_item": 1, "expense_account": "_Test Account Cost for Goods Sold - _TC", - "cost_center": "_Test Cost Center - _TC"}) + dn_item = make_item("_Test Regular Item", {"is_stock_item": 1}) so_items = [ {