optimize the patch
This commit is contained in:
parent
b16a4ec79e
commit
bbd8508783
@ -340,10 +340,22 @@ class SellingController(StockController):
|
|||||||
def check_active_sales_items(obj):
|
def check_active_sales_items(obj):
|
||||||
for d in obj.get("items"):
|
for d in obj.get("items"):
|
||||||
if d.item_code:
|
if d.item_code:
|
||||||
item = frappe.db.sql("""select docstatus,
|
item = frappe.db.sql("""select i.docstatus, id.income_account
|
||||||
income_account from tabItem where name = %s""",
|
from `tabItem` i, `tabItem Default` id
|
||||||
d.item_code, as_dict=True)[0]
|
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:
|
if getattr(d, "income_account", None) and not item.income_account:
|
||||||
frappe.db.set_value("Item", d.item_code, "income_account",
|
doc = frappe.get_doc("Item", d.item_code)
|
||||||
d.income_account)
|
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()
|
@ -15,50 +15,45 @@ def execute():
|
|||||||
frappe.reload_doc('stock', 'doctype', 'item_default')
|
frappe.reload_doc('stock', 'doctype', 'item_default')
|
||||||
frappe.reload_doc('stock', 'doctype', 'item')
|
frappe.reload_doc('stock', 'doctype', 'item')
|
||||||
|
|
||||||
item_details = frappe.get_all("Item", fields=["name", "default_warehouse", "buying_cost_center",
|
companies = frappe.get_all("Company")
|
||||||
"expense_account", "selling_cost_center", "income_account"], limit=100)
|
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:
|
for item in item_details:
|
||||||
item_defaults = []
|
item_defaults = []
|
||||||
|
|
||||||
def insert_into_item_defaults(doc_field_name, doc_field_value, company):
|
def insert_into_item_defaults(doc_field_name, doc_field_value, company):
|
||||||
for d in item_defaults:
|
for d in item_defaults:
|
||||||
if d.get("company") == company:
|
if d.get("company") == company:
|
||||||
d[doc_field_name] = doc_field_value
|
d[doc_field_name] = doc_field_value
|
||||||
return
|
return
|
||||||
item_defaults.append({
|
item_defaults.append({
|
||||||
"company": company,
|
"company": company,
|
||||||
doc_field_name: doc_field_value
|
doc_field_name: doc_field_value
|
||||||
})
|
})
|
||||||
|
|
||||||
if item.default_warehouse:
|
for d in [
|
||||||
default_warehouse_company = frappe.get_value("Warehouse", item.default_warehouse, "company", cache=True)
|
["default_warehouse", "Warehouse"], ["expense_account", "Account"], ["expense_account", "Account"],
|
||||||
insert_into_item_defaults("default_warehouse", item.default_warehouse, default_warehouse_company)
|
["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:
|
doc = frappe.get_doc("Item", item.name)
|
||||||
buying_cost_center_company = get_cost_center_company(item.buying_cost_center)
|
doc.extend("item_defaults", item_defaults)
|
||||||
insert_into_item_defaults("buying_cost_center", item.buying_cost_center, buying_cost_center_company)
|
|
||||||
|
|
||||||
if item.selling_cost_center:
|
for child_doc in doc.item_defaults:
|
||||||
selling_cost_center_company = get_cost_center_company(item.buying_cost_center)
|
child_doc.db_insert()
|
||||||
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)
|
|
@ -150,12 +150,10 @@ class TestQuotation(unittest.TestCase):
|
|||||||
from erpnext.stock.doctype.item.test_item import make_item
|
from erpnext.stock.doctype.item.test_item import make_item
|
||||||
|
|
||||||
first_item = make_item("_Test Laptop",
|
first_item = make_item("_Test Laptop",
|
||||||
{"is_stock_item": 1, "expense_account": "_Test Account Cost for Goods Sold - _TC",
|
{"is_stock_item": 1})
|
||||||
"cost_center": "_Test Cost Center - _TC"})
|
|
||||||
|
|
||||||
second_item = make_item("_Test CPU",
|
second_item = make_item("_Test CPU",
|
||||||
{"is_stock_item": 1, "expense_account": "_Test Account Cost for Goods Sold - _TC",
|
{"is_stock_item": 1})
|
||||||
"cost_center": "_Test Cost Center - _TC"})
|
|
||||||
|
|
||||||
qo_item1 = [
|
qo_item1 = [
|
||||||
{
|
{
|
||||||
|
@ -359,14 +359,9 @@ class TestSalesOrder(unittest.TestCase):
|
|||||||
|
|
||||||
make_stock_entry(target="_Test Warehouse - _TC", qty=10, rate=100)
|
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,
|
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"
|
|
||||||
})
|
|
||||||
|
|
||||||
dn_item = make_item("_Test Regular Item", {"is_stock_item": 1, "expense_account": "_Test Account Cost for Goods Sold - _TC",
|
dn_item = make_item("_Test Regular Item", {"is_stock_item": 1})
|
||||||
"cost_center": "_Test Cost Center - _TC"})
|
|
||||||
|
|
||||||
so_items = [
|
so_items = [
|
||||||
{
|
{
|
||||||
|
Loading…
x
Reference in New Issue
Block a user