def repost(only_actual=False, allow_negative_stock=False, allow_zero_rate=False, only_bin=False):
 frappe.db.auto_commit_on_many_writes = 1
 if allow_negative_stock:
  existing_allow_negative_stock = frappe.db.get_value(u'Stock Settings', None, u'allow_negative_stock')
  frappe.db.set_value(u'Stock Settings', None, u'allow_negative_stock', 1)
 for d in frappe.db.sql(u'select distinct item_code, warehouse from\n DCTB  DCTB (select item_code, warehouse from tabBin\n DCTB  DCTB union\n DCTB  DCTB select item_code, warehouse from `tabStock Ledger Entry`) a'):
  try:
   repost_stock(d[0], d[1], allow_zero_rate, only_actual, only_bin)
   frappe.db.commit()
  except:
   frappe.db.rollback()
 if allow_negative_stock:
  frappe.db.set_value(u'Stock Settings', None, u'allow_negative_stock', existing_allow_negative_stock)
 frappe.db.auto_commit_on_many_writes = 0

def repost ( only_actual = False , allow_negative_stock = False , allow_zero_rate = False , only_bin = False ) : frappe . db . auto_commit_on_many_writes = 1 if allow_negative_stock : existing_allow_negative_stock = frappe . db . get_value ( u'Stock Settings' , None , u'allow_negative_stock' ) frappe . db . set_value ( u'Stock Settings' , None , u'allow_negative_stock' , 1 ) for d in frappe . db . sql ( u'select distinct item_code, warehouse from\n DCTB  DCTB (select item_code, warehouse from tabBin\n DCTB  DCTB union\n DCTB  DCTB select item_code, warehouse from `tabStock Ledger Entry`) a' ) : try : repost_stock ( d [ 0 ] , d [ 1 ] , allow_zero_rate , only_actual , only_bin ) frappe . db . commit ( ) except : frappe . db . rollback ( ) if allow_negative_stock : frappe . db . set_value ( u'Stock Settings' , None , u'allow_negative_stock' , existing_allow_negative_stock ) frappe . db . auto_commit_on_many_writes = 0
repost everything! .