From b16a4ec79e45c693aa704989bfb4e05758ca9dd1 Mon Sep 17 00:00:00 2001 From: Manas Solanki Date: Fri, 4 May 2018 16:49:33 +0530 Subject: [PATCH] changes for the default cost center --- .../doctype/purchase_order/purchase_order.py | 3 ++- .../doctype/sales_order/sales_order.py | 6 +++-- erpnext/setup/doctype/company/company.py | 23 +++---------------- erpnext/stock/doctype/item/item.py | 8 +++---- erpnext/stock/doctype/item/test_item.py | 4 ++-- .../stock/doctype/stock_entry/stock_entry.py | 22 +++++++++--------- erpnext/stock/get_item_details.py | 15 ++++++------ 7 files changed, 33 insertions(+), 48 deletions(-) diff --git a/erpnext/buying/doctype/purchase_order/purchase_order.py b/erpnext/buying/doctype/purchase_order/purchase_order.py index 5a7573bc93..846c5b491b 100644 --- a/erpnext/buying/doctype/purchase_order/purchase_order.py +++ b/erpnext/buying/doctype/purchase_order/purchase_order.py @@ -14,6 +14,7 @@ from frappe.desk.notifications import clear_doctype_notifications from erpnext.buying.utils import validate_for_items, check_for_closed_status from erpnext.stock.utils import get_bin from six import string_types +from erpnext.stock.doctype.item.item import get_item_defaults form_grid_templates = { "items": "templates/form_grid/item_grid.html" @@ -374,7 +375,7 @@ def make_purchase_invoice(source_name, target_doc=None): target.base_amount = target.amount * flt(source_parent.conversion_rate) target.qty = target.amount / flt(obj.rate) if (flt(obj.rate) and flt(obj.billed_amt)) else flt(obj.qty) - item = frappe.db.get_value("Item", target.item_code, ["item_group", "buying_cost_center"], as_dict=1) + item = get_item_defaults(target.item_code, target.company) target.cost_center = frappe.db.get_value("Project", obj.project, "cost_center") \ or item.buying_cost_center \ or frappe.db.get_value("Item Group", item.item_group, "default_cost_center") diff --git a/erpnext/selling/doctype/sales_order/sales_order.py b/erpnext/selling/doctype/sales_order/sales_order.py index 7e6c3dcbae..5116725439 100755 --- a/erpnext/selling/doctype/sales_order/sales_order.py +++ b/erpnext/selling/doctype/sales_order/sales_order.py @@ -15,6 +15,8 @@ from frappe.contacts.doctype.address.address import get_company_address from erpnext.controllers.selling_controller import SellingController from frappe.desk.doctype.auto_repeat.auto_repeat import get_next_schedule_date from erpnext.selling.doctype.customer.customer import check_credit_limit +from erpnext.stock.doctype.item.item import get_item_defaults + form_grid_templates = { "items": "templates/form_grid/item_grid.html" @@ -493,7 +495,7 @@ def make_delivery_note(source_name, target_doc=None): target.amount = (flt(source.qty) - flt(source.delivered_qty)) * flt(source.rate) target.qty = flt(source.qty) - flt(source.delivered_qty) - item = frappe.db.get_value("Item", target.item_code, ["item_group", "selling_cost_center"], as_dict=1) + item = get_item_defaults(target.item_code, target.company) if item: target.cost_center = frappe.db.get_value("Project", source_parent.project, "cost_center") \ @@ -557,7 +559,7 @@ def make_sales_invoice(source_name, target_doc=None, ignore_permissions=False): if source_parent.project: target.cost_center = frappe.db.get_value("Project", source_parent.project, "cost_center") if not target.cost_center and target.item_code: - item = frappe.db.get_value("Item", target.item_code, ["item_group", "selling_cost_center"], as_dict=1) + item = get_item_defaults(target.item_code, target.company) target.cost_center = item.selling_cost_center \ or frappe.db.get_value("Item Group", item.item_group, "default_cost_center") diff --git a/erpnext/setup/doctype/company/company.py b/erpnext/setup/doctype/company/company.py index 2f177eed41..9e9ac5594f 100644 --- a/erpnext/setup/doctype/company/company.py +++ b/erpnext/setup/doctype/company/company.py @@ -291,9 +291,6 @@ class Company(NestedSet): Trash accounts and cost centers for this company if no gl entry exists """ self.update_nsm_model() - accounts = frappe.db.sql_list("select name from tabAccount where company=%s", self.name) - cost_centers = frappe.db.sql_list("select name from `tabCost Center` where company=%s", self.name) - warehouses = frappe.db.sql_list("select name from tabWarehouse where company=%s", self.name) rec = frappe.db.sql("SELECT name from `tabGL Entry` where company = %s", self.name) if not rec: @@ -308,33 +305,19 @@ class Company(NestedSet): frappe.db.sql("""delete from `tabWarehouse` where company=%s""", self.name) frappe.defaults.clear_default("company", value=self.name) - frappe.db.sql("delete from `tabMode of Payment Account` where company=%s", self.name) + for doctype in ["Mode of Payment Account", "Item Default"]: + frappe.db.sql("delete from `tab{0}` where company = %s".format(doctype), self.name) # clear default accounts, warehouses from item + warehouses = frappe.db.sql_list("select name from tabWarehouse where company=%s", self.name) if warehouses: - for f in ["default_warehouse", "website_warehouse"]: - frappe.db.sql("""update `tabItem Default` set %s=NULL where company=%s and %s in (%s)""" - % (f, self.name, f, ', '.join(['%s']*len(warehouses))), tuple(warehouses)) - frappe.db.sql("""delete from `tabItem Reorder` where warehouse in (%s)""" % ', '.join(['%s']*len(warehouses)), tuple(warehouses)) - if accounts: - for f in ["income_account", "expense_account"]: - frappe.db.sql("""update tabItem set %s=NULL where %s in (%s)""" - % (f, f, ', '.join(['%s']*len(accounts))), tuple(accounts)) - - if cost_centers: - for f in ["selling_cost_center", "buying_cost_center"]: - frappe.db.sql("""update tabItem set %s=NULL where %s in (%s)""" - % (f, f, ', '.join(['%s']*len(cost_centers))), tuple(cost_centers)) - # reset default company frappe.db.sql("""update `tabSingles` set value="" where doctype='Global Defaults' and field='default_company' and value=%s""", self.name) - # delete mode of payment account - frappe.db.sql("delete from `tabMode of Payment Account` where company=%s", self.name) # delete BOMs boms = frappe.db.sql_list("select name from tabBOM where company=%s", self.name) diff --git a/erpnext/stock/doctype/item/item.py b/erpnext/stock/doctype/item/item.py index 7dc9a89ce3..9487963394 100644 --- a/erpnext/stock/doctype/item/item.py +++ b/erpnext/stock/doctype/item/item.py @@ -883,13 +883,13 @@ def check_stock_uom_with_bin(item, stock_uom): frappe.throw( _("Default Unit of Measure for Item {0} cannot be changed directly because you have already made some transaction(s) with another UOM. You will need to create a new Item to use a different Default UOM.").format(item)) -def get_item_details(item, company): +def get_item_defaults(item, company): return frappe.db.sql(''' select - i.item_name, i.description, i.stock_uom, i.name, i.is_stock_item, i.item_code - id.expense_account, id.buying_cost_center, id.warehouse + i.item_name, i.description, i.stock_uom, i.name, i.is_stock_item, i.item_code, i.item_group, + id.expense_account, id.buying_cost_center, id.default_warehouse, id.selling_cost_center from `tabItem` i, `tabItem Default` id where i.name = id.parent and i.name = %s and id.company = %s - ''', (item, company), as_dict=1) \ No newline at end of file + ''', (item, company), as_dict=1)[0] \ No newline at end of file diff --git a/erpnext/stock/doctype/item/test_item.py b/erpnext/stock/doctype/item/test_item.py index ee2ca988e1..1c915ee8f2 100644 --- a/erpnext/stock/doctype/item/test_item.py +++ b/erpnext/stock/doctype/item/test_item.py @@ -36,7 +36,7 @@ def make_item(item_code, properties=None): if item.is_stock_item: - for item_default in [doc for doc in item.item_defaults if not doc.default_warehouse] + for item_default in [doc for doc in item.item_defaults if not doc.default_warehouse]: item_default.default_warehouse = "_Test Warehouse - _TC" item.insert() @@ -205,7 +205,7 @@ class TestItem(unittest.TestCase): "default_warehouse": "_Test Warehouse - _TC", "company": "_Test Company" } - ] + ], "has_variants": 1 }) diff --git a/erpnext/stock/doctype/stock_entry/stock_entry.py b/erpnext/stock/doctype/stock_entry/stock_entry.py index 49a278ca8a..5a6384aa73 100644 --- a/erpnext/stock/doctype/stock_entry/stock_entry.py +++ b/erpnext/stock/doctype/stock_entry/stock_entry.py @@ -10,7 +10,7 @@ from erpnext.stock.utils import get_incoming_rate from erpnext.stock.stock_ledger import get_previous_sle, NegativeStockError, get_valuation_rate from erpnext.stock.get_item_details import get_bin_details, get_default_cost_center, get_conversion_factor from erpnext.stock.doctype.batch.batch import get_batch_no, set_batch_nos, get_batch_qty -from erpnext.stock.doctype.item.item import get_item_details +from erpnext.stock.doctype.item.item import get_item_defaults from erpnext.manufacturing.doctype.bom.bom import validate_bom_no from erpnext.stock.utils import get_bin import json @@ -563,14 +563,14 @@ class StockEntry(StockController): pro_doc.run_method("update_planned_qty") def get_item_details(self, args=None, for_update=False): - item = frappe.db.sql("""select stock_uom, description, image, item_name, - expense_account, buying_cost_center, item_group, has_serial_no, - has_batch_no, sample_quantity - from `tabItem` - where name = %s - and disabled=0 - and (end_of_life is null or end_of_life='0000-00-00' or end_of_life > %s)""", - (args.get('item_code'), nowdate()), as_dict = 1) + item = frappe.db.sql("""select i.stock_uom, i.description, i.image, i.item_name, i.item_group, + i.has_batch_no, i.sample_quantity, i.has_serial_no, + id.expense_account, id.buying_cost_center + from `tabItem`, `tabItem Default` id + where i.name=%s and i.name=id.parent and id.company=%s + and i.disabled=0 + and (i.end_of_life is null or i.end_of_life='0000-00-00' or i.end_of_life > %s)""", + (args.get('item_code'), self.company, nowdate()), as_dict = 1) if not item: frappe.throw(_("Item {0} is not active or end of life has been reached").format(args.get("item_code"))) @@ -717,7 +717,7 @@ class StockEntry(StockController): item_code = frappe.db.get_value("BOM", self.bom_no, "item") to_warehouse = self.to_warehouse - item = get_item_details(item_code, self.company) + item = get_item_defaults(item_code, self.company) if not self.work_order and not to_warehouse: # in case of BOM @@ -783,7 +783,7 @@ class StockEntry(StockController): for item in wo_items: qty = item.required_qty - item_account_details = get_item_details(item.item_code, self.company) + item_account_details = get_item_defaults(item.item_code, self.company) # Take into account consumption if there are any. if self.purpose == 'Manufacture': req_qty_each = flt(item.required_qty / wo.qty) diff --git a/erpnext/stock/get_item_details.py b/erpnext/stock/get_item_details.py index 0351364d83..bbbe860396 100644 --- a/erpnext/stock/get_item_details.py +++ b/erpnext/stock/get_item_details.py @@ -11,7 +11,7 @@ from erpnext.setup.utils import get_exchange_rate from frappe.model.meta import get_field_precision from erpnext.stock.doctype.batch.batch import get_batch_no from erpnext import get_company_currency -from erpnext.stock.doctype.item.item import get_item_details +from erpnext.stock.doctype.item.item import get_item_defaults from six import string_types, iteritems @@ -206,9 +206,8 @@ def get_basic_details(args, item): user_default_warehouse = user_default_warehouse_list[0] \ if len(user_default_warehouse_list) == 1 else "" - item_default_warehouse = [default.default_warehouse for default in item.item_defaults if default.company == args.company] - item_default_warehouse = item_default_warehouse[0] if item_default_warehouse else None - warehouse = user_default_warehouse or item_default_warehouse or args.warehouse + item_defaults = get_item_defaults(item.name, args.company) + warehouse = user_default_warehouse or item_defaults.default_warehouse or args.warehouse material_request_type = '' if args.get('doctype') == "Material Request": @@ -231,9 +230,9 @@ def get_basic_details(args, item): "description": cstr(item.description).strip(), "image": cstr(item.image).strip(), "warehouse": warehouse, - "income_account": get_default_income_account(args, item), - "expense_account": get_default_expense_account(args, item), - "cost_center": get_default_cost_center(args, item), + "income_account": get_default_income_account(args, item_defaults), + "expense_account": get_default_expense_account(args, item_defaults), + "cost_center": get_default_cost_center(args, item_defaults), 'has_serial_no': item.has_serial_no, 'has_batch_no': item.has_batch_no, "batch_no": None, @@ -682,7 +681,7 @@ def get_default_bom(item_code=None): return bom def get_valuation_rate(item_code, company, warehouse=None): - item = get_item_details(item_code, company) + item = get_item_defaults(item_code, company) # item = frappe.get_doc("Item", item_code) if item.is_stock_item: if not warehouse: