Skip to content

Commit

Permalink
fix: validate item defaults for item groups
Browse files Browse the repository at this point in the history
  • Loading branch information
ankush committed Sep 16, 2021
1 parent ad0a2cf commit a926584
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 19 deletions.
5 changes: 5 additions & 0 deletions erpnext/setup/doctype/item_group/item_group.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ def validate(self):
self.parent_item_group = _('All Item Groups')

self.make_route()
self.validate_item_group_defaults()

def on_update(self):
NestedSet.on_update(self)
Expand Down Expand Up @@ -134,6 +135,10 @@ def get_context(self, context):
def delete_child_item_groups_key(self):
frappe.cache().hdel("child_item_groups", self.name)

def validate_item_group_defaults(self):
from erpnext.stock.doctype.item.item import validate_item_default_company_links
validate_item_default_company_links(self.item_group_defaults)

@frappe.whitelist(allow_guest=True)
def get_product_list_for_group(product_group=None, start=0, limit=10, search=None):
if product_group:
Expand Down
45 changes: 26 additions & 19 deletions erpnext/stock/doctype/item/item.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import copy
import itertools
import json
from typing import List

import frappe
from frappe import _
Expand Down Expand Up @@ -36,6 +37,7 @@
get_parent_item_groups,
invalidate_cache_for,
)
from erpnext.stock.doctype.item_default.item_default import ItemDefault


class DuplicateReorderRows(frappe.ValidationError):
Expand Down Expand Up @@ -782,25 +784,8 @@ def validate_item_defaults(self):
if len(companies) != len(self.item_defaults):
frappe.throw(_("Cannot set multiple Item Defaults for a company."))

for item_default in self.item_defaults:
for doctype, field in [
['Warehouse', 'default_warehouse'],
['Cost Center', 'buying_cost_center'],
['Cost Center', 'selling_cost_center'],
['Account', 'expense_account'],
['Account', 'income_account']
]:
if item_default.get(field):
company = frappe.db.get_value(doctype, item_default.get(field), 'company', cache=True)
if company and company != item_default.company:
frappe.throw(_("Row #{}: {} {} doesn't belong to Company {}. Please select valid {}.")
.format(
item_default.idx,
doctype,
frappe.bold(item_default.get(field)),
frappe.bold(item_default.company),
frappe.bold(frappe.unscrub(field))
), title=_("Invalid Item Defaults"))
validate_item_default_company_links(self.item_defaults)


def update_defaults_from_item_group(self):
"""Get defaults from Item Group"""
Expand Down Expand Up @@ -1349,3 +1334,25 @@ def on_doctype_update():
@erpnext.allow_regional
def set_item_tax_from_hsn_code(item):
pass


def validate_item_default_company_links(item_defaults: List[ItemDefault]) -> None:
for item_default in item_defaults:
for doctype, field in [
['Warehouse', 'default_warehouse'],
['Cost Center', 'buying_cost_center'],
['Cost Center', 'selling_cost_center'],
['Account', 'expense_account'],
['Account', 'income_account']
]:
if item_default.get(field):
company = frappe.db.get_value(doctype, item_default.get(field), 'company', cache=True)
if company and company != item_default.company:
frappe.throw(_("Row #{}: {} {} doesn't belong to Company {}. Please select valid {}.")
.format(
item_default.idx,
doctype,
frappe.bold(item_default.get(field)),
frappe.bold(item_default.company),
frappe.bold(frappe.unscrub(field))
), title=_("Invalid Item Defaults"))

0 comments on commit a926584

Please sign in to comment.