
from __future__ import unicode_literals
import frappe
from erpnext.utilities.repost_stock import update_bin_qty, get_reserved_qty

def execute():
	repost_for = frappe.db.sql("""
		select 
			distinct item_code, warehouse 
		from 
			(
				(
					select distinct item_code, warehouse 
								from `tabSales Order Item` where docstatus=1
				) UNION (
					select distinct item_code, warehouse 
					from `tabPacked Item` where docstatus=1 and parenttype='Sales Order'
				)
			) so_item
		where
			exists(select name from tabItem where name=so_item.item_code and ifnull(is_stock_item, 0)=1)
	""")

	for item_code, warehouse in repost_for:
			update_bin_qty(item_code, warehouse, {
				"reserved_qty": get_reserved_qty(item_code, warehouse)
			})

from __future__ import unicode_literals
import frappe
from erpnext.utilities.repost_stock import update_bin_qty, get_reserved_qty

def execute():
	repost_for = frappe.db.sql("""
		select 
			distinct item_code, warehouse 
		from 
			(
				(
					select distinct item_code, warehouse 
								from `tabSales Order Item` where docstatus=1
				) UNION (
					select distinct item_code, warehouse 
					from `tabPacked Item` where docstatus=1 and parenttype='Sales Order'
				)
			) so_item
		where
			exists(select name from tabItem where name=so_item.item_code and ifnull(is_stock_item, 0)=1)
	""")

	for item_code, warehouse in repost_for:
			update_bin_qty(item_code, warehouse, {
				"reserved_qty": get_reserved_qty(item_code, warehouse)
			})
			
	frappe.db.sql("""delete from tabBin 
		where exists(
			select name from tabItem where name=tabBin.item_code and ifnull(is_stock_item, 0) = 0
		)
	""")
Delete Bin for non-stock item
[fix][patch] Delete Bin for non-stock item

  
  from __future__ import unicode_literals
  import frappe
  from erpnext.utilities.repost_stock import update_bin_qty, get_reserved_qty
  
  def execute():
  	repost_for = frappe.db.sql("""
  		select 
  			distinct item_code, warehouse 
  		from 
  			(
  				(
  					select distinct item_code, warehouse 
  								from `tabSales Order Item` where docstatus=1
  				) UNION (
  					select distinct item_code, warehouse 
  					from `tabPacked Item` where docstatus=1 and parenttype='Sales Order'
  				)
  			) so_item
  		where
  			exists(select name from tabItem where name=so_item.item_code and ifnull(is_stock_item, 0)=1)
  	""")
  
  	for item_code, warehouse in repost_for:
  			update_bin_qty(item_code, warehouse, {
  				"reserved_qty": get_reserved_qty(item_code, warehouse)
  			})
+ 			
+ 	frappe.db.sql("""delete from tabBin 
+ 		where exists(
+ 			select name from tabItem where name=tabBin.item_code and ifnull(is_stock_item, 0) = 0
+ 		)
+ 	""")
Delete Bin for non-stock item
## Code Before:

from __future__ import unicode_literals
import frappe
from erpnext.utilities.repost_stock import update_bin_qty, get_reserved_qty

def execute():
	repost_for = frappe.db.sql("""
		select 
			distinct item_code, warehouse 
		from 
			(
				(
					select distinct item_code, warehouse 
								from `tabSales Order Item` where docstatus=1
				) UNION (
					select distinct item_code, warehouse 
					from `tabPacked Item` where docstatus=1 and parenttype='Sales Order'
				)
			) so_item
		where
			exists(select name from tabItem where name=so_item.item_code and ifnull(is_stock_item, 0)=1)
	""")

	for item_code, warehouse in repost_for:
			update_bin_qty(item_code, warehouse, {
				"reserved_qty": get_reserved_qty(item_code, warehouse)
			})
## Instruction:
Delete Bin for non-stock item
## Code After:

from __future__ import unicode_literals
import frappe
from erpnext.utilities.repost_stock import update_bin_qty, get_reserved_qty

def execute():
	repost_for = frappe.db.sql("""
		select 
			distinct item_code, warehouse 
		from 
			(
				(
					select distinct item_code, warehouse 
								from `tabSales Order Item` where docstatus=1
				) UNION (
					select distinct item_code, warehouse 
					from `tabPacked Item` where docstatus=1 and parenttype='Sales Order'
				)
			) so_item
		where
			exists(select name from tabItem where name=so_item.item_code and ifnull(is_stock_item, 0)=1)
	""")

	for item_code, warehouse in repost_for:
			update_bin_qty(item_code, warehouse, {
				"reserved_qty": get_reserved_qty(item_code, warehouse)
			})
			
	frappe.db.sql("""delete from tabBin 
		where exists(
			select name from tabItem where name=tabBin.item_code and ifnull(is_stock_item, 0) = 0
		)
	""")