Skip to content

Commit 523d89a

Browse files
fix: as limit 100 was set in the query therefore not all item's default value moved to the Item Default table
1 parent 4de1083 commit 523d89a

2 files changed

Lines changed: 58 additions & 28 deletions

File tree

erpnext/patches.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -533,7 +533,7 @@ erpnext.patches.v11_0.create_department_records_for_each_company
533533
erpnext.patches.v11_0.make_location_from_warehouse
534534
erpnext.patches.v11_0.make_asset_finance_book_against_old_entries
535535
erpnext.patches.v11_0.check_buying_selling_in_currency_exchange
536-
erpnext.patches.v11_0.move_item_defaults_to_child_table_for_multicompany #02-07-2018
536+
erpnext.patches.v11_0.move_item_defaults_to_child_table_for_multicompany #02-07-2018 #19-06-2019
537537
erpnext.patches.v11_0.refactor_erpnext_shopify #2018-09-07
538538
erpnext.patches.v11_0.rename_overproduction_percent_field
539539
erpnext.patches.v11_0.update_backflush_subcontract_rm_based_on_bom

erpnext/patches/v11_0/move_item_defaults_to_child_table_for_multicompany.py

Lines changed: 57 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,8 @@ def execute():
1717
frappe.reload_doc('stock', 'doctype', 'item_default')
1818
frappe.reload_doc('stock', 'doctype', 'item')
1919

20-
if frappe.db.a_row_exists('Item Default'): return
21-
2220
companies = frappe.get_all("Company")
23-
if len(companies) == 1:
21+
if len(companies) == 1 and not frappe.get_all("Item Default", limit=1):
2422
try:
2523
frappe.db.sql('''
2624
INSERT INTO `tabItem Default`
@@ -35,32 +33,64 @@ def execute():
3533
except:
3634
pass
3735
else:
38-
item_details = frappe.get_all("Item", fields=["name", "default_warehouse", "buying_cost_center",
39-
"expense_account", "selling_cost_center", "income_account"], limit=100)
36+
item_details = frappe.db.sql(""" SELECT name, default_warehouse,
37+
buying_cost_center, expense_account, selling_cost_center, income_account
38+
FROM tabItem
39+
WHERE
40+
name not in (select distinct parent from `tabItem Default`) and ifnull(disabled, 0) = 0"""
41+
, as_dict=1)
42+
43+
items_default_data = {}
44+
for item_data in item_details:
45+
for d in [["default_warehouse", "Warehouse"], ["expense_account", "Account"],
46+
["income_account", "Account"], ["buying_cost_center", "Cost Center"],
47+
["selling_cost_center", "Cost Center"]]:
48+
if item_data.get(d[0]):
49+
company = frappe.get_value(d[1], item_data.get(d[0]), "company", cache=True)
50+
51+
if item_data.name not in items_default_data:
52+
items_default_data[item_data.name] = {}
4053

41-
for item in item_details:
42-
item_defaults = []
54+
company_wise_data = items_default_data[item_data.name]
4355

44-
def insert_into_item_defaults(doc_field_name, doc_field_value, company):
45-
for d in item_defaults:
46-
if d.get("company") == company:
47-
d[doc_field_name] = doc_field_value
48-
return
49-
item_defaults.append({
50-
"company": company,
51-
doc_field_name: doc_field_value
52-
})
56+
if company not in company_wise_data:
57+
company_wise_data[company] = {}
5358

54-
for d in [
55-
["default_warehouse", "Warehouse"], ["expense_account", "Account"], ["income_account", "Account"],
56-
["buying_cost_center", "Cost Center"], ["selling_cost_center", "Cost Center"]
57-
]:
58-
if item.get(d[0]):
59-
company = frappe.get_value(d[1], item.get(d[0]), "company", cache=True)
60-
insert_into_item_defaults(d[0], item.get(d[0]), company)
59+
default_data = company_wise_data[company]
60+
default_data[d[0]] = item_data.get(d[0])
6161

62-
doc = frappe.get_doc("Item", item.name)
63-
doc.extend("item_defaults", item_defaults)
62+
to_insert_data = []
6463

65-
for child_doc in doc.item_defaults:
66-
child_doc.db_insert()
64+
# items_default_data data structure will be as follow
65+
# {
66+
# 'item_code 1': {'company 1': {'default_warehouse': 'Test Warehouse 1'}},
67+
# 'item_code 2': {
68+
# 'company 1': {'default_warehouse': 'Test Warehouse 1'},
69+
# 'company 2': {'default_warehouse': 'Test Warehouse 1'}
70+
# }
71+
# }
72+
73+
for item_code, companywise_item_data in items_default_data.items():
74+
for company, item_default_data in companywise_item_data.items():
75+
to_insert_data.append((
76+
frappe.generate_hash("", 10),
77+
item_code,
78+
'Item',
79+
'item_defaults',
80+
company,
81+
item_default_data.get('default_warehouse'),
82+
item_default_data.get('expense_account'),
83+
item_default_data.get('income_account'),
84+
item_default_data.get('buying_cost_center'),
85+
item_default_data.get('selling_cost_center'),
86+
))
87+
88+
if to_insert_data:
89+
frappe.db.sql('''
90+
INSERT INTO `tabItem Default`
91+
(
92+
`name`, `parent`, `parenttype`, `parentfield`, `company`, `default_warehouse`,
93+
`expense_account`, `income_account`, `buying_cost_center`, `selling_cost_center`
94+
)
95+
VALUES {}
96+
'''.format(', '.join(['%s'] * len(to_insert_data))), tuple(to_insert_data))

0 commit comments

Comments
 (0)