changes for the default cost center
diff --git a/erpnext/buying/doctype/purchase_order/purchase_order.py b/erpnext/buying/doctype/purchase_order/purchase_order.py
index 5a7573b..846c5b4 100644
--- a/erpnext/buying/doctype/purchase_order/purchase_order.py
+++ b/erpnext/buying/doctype/purchase_order/purchase_order.py
@@ -14,6 +14,7 @@
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 @@
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 7e6c3dc..5116725 100755
--- a/erpnext/selling/doctype/sales_order/sales_order.py
+++ b/erpnext/selling/doctype/sales_order/sales_order.py
@@ -15,6 +15,8 @@
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 @@
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 @@
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 2f177ee..9e9ac55 100644
--- a/erpnext/setup/doctype/company/company.py
+++ b/erpnext/setup/doctype/company/company.py
@@ -291,9 +291,6 @@
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 @@
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 7dc9a89..9487963 100644
--- a/erpnext/stock/doctype/item/item.py
+++ b/erpnext/stock/doctype/item/item.py
@@ -883,13 +883,13 @@
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 ee2ca98..1c915ee 100644
--- a/erpnext/stock/doctype/item/test_item.py
+++ b/erpnext/stock/doctype/item/test_item.py
@@ -36,7 +36,7 @@
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 @@
"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 49a278c..5a6384a 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.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 @@
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 @@
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 @@
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 0351364..bbbe860 100644
--- a/erpnext/stock/get_item_details.py
+++ b/erpnext/stock/get_item_details.py
@@ -11,7 +11,7 @@
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 @@
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 @@
"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 @@
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: