mirror of
https://github.com/frappe/erpnext.git
synced 2026-06-08 15:42:52 +00:00
On deletion of warehouse, delete bin and sle if no qty
This commit is contained in:
@@ -18,73 +18,88 @@ convert_to_lists = webnotes.conn.convert_to_lists
|
|||||||
|
|
||||||
|
|
||||||
class DocType:
|
class DocType:
|
||||||
def __init__(self, doc, doclist=[]):
|
def __init__(self, doc, doclist=[]):
|
||||||
self.doc = doc
|
self.doc = doc
|
||||||
self.doclist = doclist
|
self.doclist = doclist
|
||||||
|
|
||||||
def get_bin(self, item_code):
|
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 = sql("select name from tabBin where item_code = '%s' and warehouse = '%s'" % (item_code, self.doc.name))
|
||||||
bin = bin and bin[0][0] or ''
|
bin = bin and bin[0][0] or ''
|
||||||
if not bin:
|
if not bin:
|
||||||
if not self.doc.warehouse_type :
|
if not self.doc.warehouse_type :
|
||||||
msgprint("[Warehouse Type is Mandatory] Please Enter warehouse type in Warehouse " + self.doc.name)
|
msgprint("[Warehouse Type is Mandatory] Please Enter warehouse type in Warehouse " + self.doc.name)
|
||||||
raise Exception
|
raise Exception
|
||||||
bin = Document('Bin')
|
bin = Document('Bin')
|
||||||
bin.item_code = item_code
|
bin.item_code = item_code
|
||||||
bin.stock_uom = get_value('Item', item_code, 'stock_uom')
|
bin.stock_uom = get_value('Item', item_code, 'stock_uom')
|
||||||
bin.warehouse = self.doc.name
|
bin.warehouse = self.doc.name
|
||||||
bin.warehouse_type = self.doc.warehouse_type
|
bin.warehouse_type = self.doc.warehouse_type
|
||||||
bin_obj = get_obj(doc=bin)
|
bin_obj = get_obj(doc=bin)
|
||||||
bin_obj.validate()
|
bin_obj.validate()
|
||||||
bin.save(1)
|
bin.save(1)
|
||||||
bin = bin.name
|
bin = bin.name
|
||||||
else:
|
else:
|
||||||
bin_obj = get_obj('Bin',bin)
|
bin_obj = get_obj('Bin',bin)
|
||||||
|
|
||||||
return bin_obj
|
return bin_obj
|
||||||
|
|
||||||
|
|
||||||
def validate_asset(self, item_code):
|
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':
|
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)
|
msgprint("Fixed Asset Item %s can only be transacted in a Fixed Asset type Warehouse" % item_code)
|
||||||
raise Exception
|
raise Exception
|
||||||
|
|
||||||
|
|
||||||
# update bin
|
# 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'):
|
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)
|
self.validate_asset(item_code)
|
||||||
it_det = get_value('Item', item_code, 'is_stock_item')
|
it_det = get_value('Item', item_code, 'is_stock_item')
|
||||||
if it_det and it_det == 'Yes':
|
if it_det and it_det == 'Yes':
|
||||||
bin = self.get_bin(item_code)
|
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)
|
bin.update_stock(actual_qty, reserved_qty, ordered_qty, indented_qty, planned_qty, dt, sle_id, posting_time, serial_no, is_cancelled)
|
||||||
return bin
|
return bin
|
||||||
else:
|
else:
|
||||||
msgprint("[Stock Update] Ignored %s since it is not a stock item" % item_code)
|
msgprint("[Stock Update] Ignored %s since it is not a stock item" % item_code)
|
||||||
|
|
||||||
# repost stock
|
# repost stock
|
||||||
# ------------
|
# ------------
|
||||||
def repost_stock(self):
|
def repost_stock(self):
|
||||||
bl = sql("select name from tabBin where warehouse=%s", self.doc.name)
|
bl = sql("select name from tabBin where warehouse=%s", self.doc.name)
|
||||||
for b in bl:
|
for b in bl:
|
||||||
bobj = get_obj('Bin',b[0])
|
bobj = get_obj('Bin',b[0])
|
||||||
bobj.update_entries_after(posting_date = '0000-00-00', posting_time = '00:00')
|
bobj.update_entries_after(posting_date = '0000-00-00', posting_time = '00:00')
|
||||||
|
|
||||||
sql("COMMIT")
|
sql("COMMIT")
|
||||||
sql("START TRANSACTION")
|
sql("START TRANSACTION")
|
||||||
|
|
||||||
def check_state(self):
|
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)])
|
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):
|
def validate(self):
|
||||||
if self.doc.email_id:
|
if self.doc.email_id:
|
||||||
if not validate_email_add(self.doc.email_id):
|
if not validate_email_add(self.doc.email_id):
|
||||||
msgprint("Please enter valid Email Id.")
|
msgprint("Please enter valid Email Id.")
|
||||||
raise Exception
|
raise Exception
|
||||||
if not self.doc.warehouse_type:
|
if not self.doc.warehouse_type:
|
||||||
msgprint("[Warehouse Type is Mandatory] Please Enter Please Entry warehouse type in Warehouse " + self.doc.name)
|
msgprint("[Warehouse Type is Mandatory] Please Enter Please Entry warehouse type in Warehouse " + self.doc.name)
|
||||||
raise Exception
|
raise Exception
|
||||||
wt = sql("select warehouse_type from `tabWarehouse` where name ='%s'" % self.doc.name)
|
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 ''):
|
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))
|
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.")
|
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)
|
||||||
|
|||||||
Reference in New Issue
Block a user