Advertisement
Guest User

Untitled

a guest
Apr 21st, 2021
262
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.52 KB | None | 0 0
  1. def resolve_warehouse(doc, method=None):
  2.     customer_doc = frappe.get_doc('Customer', doc.customer)
  3.     territory_warehouse = frappe.get_doc('Territory', customer_doc.territory).warehouse
  4.  
  5.     for item in doc.items:
  6.         default_warehouse = item.warehouse
  7.         qty = item.qty
  8.  
  9.         # If the sales order is under any company other than XXXXXXXXXX
  10.         # then skip warehouse resolution. This is because for the time being all inventory
  11.         # will remain in the ownership of XXXXXXXXXX until sold by the other company.
  12.         if doc.company != 'XXXXXXXXXX':
  13.             break
  14.  
  15.         # Get warehouse in relation with customer territory.
  16.         lft, rgt = frappe.db.get_value('Warehouse', territory_warehouse, ['lft', 'rgt'])
  17.         sql = """SELECT warehouse, actual_qty qty FROM tabBin WHERE item_code=%s AND
  18.         exists(SELECT name FROM tabWarehouse wh WHERE wh.name = tabBin.warehouse AND wh.lft >= %s AND wh.rgt <= %s)
  19.         AND actual_qty > 0
  20.         ORDER BY warehouse ASC;"""
  21.         values = [item.item_code, lft, rgt]
  22.         warehouse_list = frappe.db.sql(sql, values, as_dict=True)
  23.         warehouse_names = []
  24.         for w in warehouse_list:
  25.             warehouse_names.append(w.warehouse)
  26.  
  27.         backup_warehouse = None
  28.         for result in warehouse_list:
  29.             if qty <= result.qty:
  30.                 backup_warehouse = result.warehouse
  31.                 break
  32.  
  33.         if default_warehouse in warehouse_names:
  34.             if qty <= get_latest_stock_qty(warehouse=default_warehouse, item_code=item.item_code):
  35.                 continue
  36.  
  37.         if backup_warehouse:
  38.             item.warehouse = backup_warehouse
  39.             continue
  40.  
  41.         item.warehouse = 'XXXXXXX - LAST RESORT WAREHOUSE - XXXXXX'
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement