Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def resolve_warehouse(doc, method=None):
- customer_doc = frappe.get_doc('Customer', doc.customer)
- territory_warehouse = frappe.get_doc('Territory', customer_doc.territory).warehouse
- for item in doc.items:
- default_warehouse = item.warehouse
- qty = item.qty
- # If the sales order is under any company other than XXXXXXXXXX
- # then skip warehouse resolution. This is because for the time being all inventory
- # will remain in the ownership of XXXXXXXXXX until sold by the other company.
- if doc.company != 'XXXXXXXXXX':
- break
- # Get warehouse in relation with customer territory.
- lft, rgt = frappe.db.get_value('Warehouse', territory_warehouse, ['lft', 'rgt'])
- sql = """SELECT warehouse, actual_qty qty FROM tabBin WHERE item_code=%s AND
- exists(SELECT name FROM tabWarehouse wh WHERE wh.name = tabBin.warehouse AND wh.lft >= %s AND wh.rgt <= %s)
- AND actual_qty > 0
- ORDER BY warehouse ASC;"""
- values = [item.item_code, lft, rgt]
- warehouse_list = frappe.db.sql(sql, values, as_dict=True)
- warehouse_names = []
- for w in warehouse_list:
- warehouse_names.append(w.warehouse)
- backup_warehouse = None
- for result in warehouse_list:
- if qty <= result.qty:
- backup_warehouse = result.warehouse
- break
- if default_warehouse in warehouse_names:
- if qty <= get_latest_stock_qty(warehouse=default_warehouse, item_code=item.item_code):
- continue
- if backup_warehouse:
- item.warehouse = backup_warehouse
- continue
- item.warehouse = 'XXXXXXX - LAST RESORT WAREHOUSE - XXXXXX'
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement