On deletion of warehouse, delete bin and sle if no qty
diff --git a/erpnext/stock/doctype/warehouse/warehouse.py b/erpnext/stock/doctype/warehouse/warehouse.py
index b59ce75..0b099fd 100644
--- a/erpnext/stock/doctype/warehouse/warehouse.py
+++ b/erpnext/stock/doctype/warehouse/warehouse.py
@@ -18,73 +18,88 @@
class DocType:
- def __init__(self, doc, doclist=[]):
- self.doc = doc
- self.doclist = doclist
-
- def get_bin(self, item_code):
- bin = sql("select name from tabBin where item_code = '%s' and warehouse = '%s'" % (item_code, self.doc.name))
- bin = bin and bin[0][0] or ''
- if not bin:
- if not self.doc.warehouse_type :
- msgprint("[Warehouse Type is Mandatory] Please Enter warehouse type in Warehouse " + self.doc.name)
- raise Exception
- bin = Document('Bin')
- bin.item_code = item_code
- bin.stock_uom = get_value('Item', item_code, 'stock_uom')
- bin.warehouse = self.doc.name
- bin.warehouse_type = self.doc.warehouse_type
- bin_obj = get_obj(doc=bin)
- bin_obj.validate()
- bin.save(1)
- bin = bin.name
- else:
- bin_obj = get_obj('Bin',bin)
+ def __init__(self, doc, doclist=[]):
+ self.doc = doc
+ self.doclist = doclist
+
+ def get_bin(self, item_code):
+ bin = sql("select name from tabBin where item_code = '%s' and warehouse = '%s'" % (item_code, self.doc.name))
+ bin = bin and bin[0][0] or ''
+ if not bin:
+ if not self.doc.warehouse_type :
+ msgprint("[Warehouse Type is Mandatory] Please Enter warehouse type in Warehouse " + self.doc.name)
+ raise Exception
+ bin = Document('Bin')
+ bin.item_code = item_code
+ bin.stock_uom = get_value('Item', item_code, 'stock_uom')
+ bin.warehouse = self.doc.name
+ bin.warehouse_type = self.doc.warehouse_type
+ bin_obj = get_obj(doc=bin)
+ bin_obj.validate()
+ bin.save(1)
+ bin = bin.name
+ else:
+ bin_obj = get_obj('Bin',bin)
- return bin_obj
-
+ return bin_obj
+
- def validate_asset(self, item_code):
- if sql("select is_asset_item from tabItem where name=%s", item_code)[0][0] == 'Yes' and self.doc.warehouse_type != 'Fixed Asset':
- msgprint("Fixed Asset Item %s can only be transacted in a Fixed Asset type Warehouse" % item_code)
- raise Exception
+ def validate_asset(self, item_code):
+ if sql("select is_asset_item from tabItem where name=%s", item_code)[0][0] == 'Yes' and self.doc.warehouse_type != 'Fixed Asset':
+ msgprint("Fixed Asset Item %s can only be transacted in a Fixed Asset type Warehouse" % item_code)
+ raise Exception
- # update bin
- # ----------
- def update_bin(self, actual_qty, reserved_qty, ordered_qty, indented_qty, planned_qty, item_code, dt, sle_id = '',posting_time = '', serial_no = '', is_cancelled = 'No'):
- self.validate_asset(item_code)
- it_det = get_value('Item', item_code, 'is_stock_item')
- if it_det and it_det == 'Yes':
- bin = self.get_bin(item_code)
- bin.update_stock(actual_qty, reserved_qty, ordered_qty, indented_qty, planned_qty, dt, sle_id, posting_time, serial_no, is_cancelled)
- return bin
- else:
- msgprint("[Stock Update] Ignored %s since it is not a stock item" % item_code)
+ # update bin
+ # ----------
+ def update_bin(self, actual_qty, reserved_qty, ordered_qty, indented_qty, planned_qty, item_code, dt, sle_id = '',posting_time = '', serial_no = '', is_cancelled = 'No'):
+ self.validate_asset(item_code)
+ it_det = get_value('Item', item_code, 'is_stock_item')
+ if it_det and it_det == 'Yes':
+ bin = self.get_bin(item_code)
+ bin.update_stock(actual_qty, reserved_qty, ordered_qty, indented_qty, planned_qty, dt, sle_id, posting_time, serial_no, is_cancelled)
+ return bin
+ else:
+ msgprint("[Stock Update] Ignored %s since it is not a stock item" % item_code)
- # repost stock
- # ------------
- def repost_stock(self):
- bl = sql("select name from tabBin where warehouse=%s", self.doc.name)
- for b in bl:
- bobj = get_obj('Bin',b[0])
- bobj.update_entries_after(posting_date = '0000-00-00', posting_time = '00:00')
+ # repost stock
+ # ------------
+ def repost_stock(self):
+ bl = sql("select name from tabBin where warehouse=%s", self.doc.name)
+ for b in bl:
+ bobj = get_obj('Bin',b[0])
+ bobj.update_entries_after(posting_date = '0000-00-00', posting_time = '00:00')
- sql("COMMIT")
- sql("START TRANSACTION")
+ sql("COMMIT")
+ sql("START TRANSACTION")
- def check_state(self):
- return "\n" + "\n".join([i[0] for i in sql("select state_name from `tabState` where `tabState`.country='%s' " % self.doc.country)])
+ def check_state(self):
+ return "\n" + "\n".join([i[0] for i in sql("select state_name from `tabState` where `tabState`.country='%s' " % self.doc.country)])
- def validate(self):
- if self.doc.email_id:
- if not validate_email_add(self.doc.email_id):
- msgprint("Please enter valid Email Id.")
- raise Exception
- if not self.doc.warehouse_type:
- msgprint("[Warehouse Type is Mandatory] Please Enter Please Entry warehouse type in Warehouse " + self.doc.name)
- raise Exception
- wt = sql("select warehouse_type from `tabWarehouse` where name ='%s'" % self.doc.name)
- if cstr(self.doc.warehouse_type) != cstr(wt and wt[0][0] or ''):
- sql("update `tabStock Ledger Entry` set warehouse_type = '%s' where warehouse = '%s'" % (self.doc.warehouse_type, self.doc.name))
- msgprint("All Stock Ledger Entries Updated.")
+ def validate(self):
+ if self.doc.email_id:
+ if not validate_email_add(self.doc.email_id):
+ msgprint("Please enter valid Email Id.")
+ raise Exception
+ if not self.doc.warehouse_type:
+ msgprint("[Warehouse Type is Mandatory] Please Enter Please Entry warehouse type in Warehouse " + self.doc.name)
+ raise Exception
+ wt = sql("select warehouse_type from `tabWarehouse` where name ='%s'" % self.doc.name)
+ if cstr(self.doc.warehouse_type) != cstr(wt and wt[0][0] or ''):
+ sql("update `tabStock Ledger Entry` set warehouse_type = '%s' where warehouse = '%s'" % (self.doc.warehouse_type, self.doc.name))
+ msgprint("All Stock Ledger Entries Updated.")
+
+ def on_trash(self):
+ # delete bin
+ bins = sql("select * from `tabBin` where warehouse = %s", self.doc.name, as_dict=1)
+ for d in bins:
+ if d['actual_qty'] or d['reserved_qty'] or d['ordered_qty'] or d['indented_qty'] or d['projected_qty'] or d['planned_qty']:
+ msgprint("Warehouse: %s can not be deleted as qty exists for item: %s" % (self.doc.name, d['item_code']), raise_exception=1)
+ else:
+ sql("delete from `tabBin` where name = %s", d['name'])
+
+ # delete cancelled sle
+ if sql("select name from `tabStock Ledger Entry` where warehouse = %s and ifnull('is_cancelled', '') = 'No'", self.doc.name):
+ mdgprint("Warehosue can not be deleted as stock ledger entry exists for this warehosue.", raise_exception=1)
+ else:
+ sql("delete from `tabStock Ledger Entry` where warehouse = %s", self.doc.name)