Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: auto unlink warehouse from item on delete (#26073) #26101

Merged
merged 1 commit into from
Jun 18, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions erpnext/stock/doctype/warehouse/test_warehouse.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import erpnext
from erpnext.stock.doctype.stock_entry.stock_entry_utils import make_stock_entry
from erpnext.accounts.doctype.account.test_account import get_inventory_account, create_account
from erpnext.stock.doctype.item.test_item import create_item

test_records = frappe.get_test_records('Warehouse')

Expand Down Expand Up @@ -92,6 +93,39 @@ def test_warehouse_merging(self):
self.assertTrue(frappe.db.get_value("Warehouse",
filters={"account": "Test Warehouse for Merging 2 - TCP1"}))

def test_unlinking_warehouse_from_item_defaults(self):
company = "_Test Company"

warehouse_names = [f'_Test Warehouse {i} for Unlinking' for i in range(2)]
warehouse_ids = []
for warehouse in warehouse_names:
warehouse_id = create_warehouse(warehouse, company=company)
warehouse_ids.append(warehouse_id)

item_names = [f'_Test Item {i} for Unlinking' for i in range(2)]
for item, warehouse in zip(item_names, warehouse_ids):
create_item(item, warehouse=warehouse, company=company)

# Delete warehouses
for warehouse in warehouse_ids:
frappe.delete_doc("Warehouse", warehouse)

# Check Item existance
for item in item_names:
self.assertTrue(
bool(frappe.db.exists("Item", item)),
f"{item} doesn't exist"
)

item_doc = frappe.get_doc("Item", item)
for item_default in item_doc.item_defaults:
self.assertNotIn(
item_default.default_warehouse,
warehouse_ids,
f"{item} linked to {item_default.default_warehouse} in {warehouse_ids}."
)


def create_warehouse(warehouse_name, properties=None, company=None):
if not company:
company = "_Test Company"
Expand Down
7 changes: 7 additions & 0 deletions erpnext/stock/doctype/warehouse/warehouse.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ def on_trash(self):
throw(_("Child warehouse exists for this warehouse. You can not delete this warehouse."))

self.update_nsm_model()
self.unlink_from_items()

def check_if_sle_exists(self):
return frappe.db.sql("""select name from `tabStock Ledger Entry`
Expand Down Expand Up @@ -138,6 +139,12 @@ def convert_to_group(self):
self.save()
return 1

def unlink_from_items(self):
frappe.db.sql("""
update `tabItem Default`
set default_warehouse=NULL
where default_warehouse=%s""", self.name)

@frappe.whitelist()
def get_children(doctype, parent=None, company=None, is_root=False):
if is_root:
Expand Down