I tried modifying the item_reorder.py script to fulfill material requests based on the name of the day for a particular warehouse.
The following is a snippet of the script that I added.
def auto_reorder():
current_day = now_datetime().weekday()
current_time = now_datetime().time()
# Define your warehouse and day mapping here
warehouse_day_mapping = {
'STOCK GHW - PTGGS': [0, 2, 4], # Monday, Wednesday, Friday
'STOCK GHI - PTGGS': [1, 3, 5], # Tuesday, Thursday, Saturday
# Add more warehouses as needed
}
for warehouse, days in warehouse_day_mapping.items():
if current_day in days:
erp_auto_reorder(warehouse)
It looks like this
def auto_reorder():
current_day = now_datetime().weekday()
current_time = now_datetime().time()
# Define your warehouse and day mapping here
warehouse_day_mapping = {
'STOCK GHW - PTGGS': [0, 2, 4], # Monday, Wednesday, Friday
'STOCK GHI - PTGGS': [1, 3, 5], # Tuesday, Thursday, Saturday
# Add more warehouses as needed
}
for warehouse, days in warehouse_day_mapping.items():
if current_day in days:
erp_auto_reorder(warehouse)
def get_item_warehouse_projected_qty(items_to_consider):
item_warehouse_projected_qty = {}
for item_code, warehouse, projected_qty in frappe.db.sql(
"""select item_code, warehouse, projected_qty
from tabBin where item_code in ({0})
and (warehouse != '' and warehouse is not null)""".format(
", ".join(["%s"] * len(items_to_consider))
),
items_to_consider,
):
if item_code not in item_warehouse_projected_qty:
item_warehouse_projected_qty.setdefault(item_code, {})
if warehouse not in item_warehouse_projected_qty.get(item_code):
item_warehouse_projected_qty[item_code][warehouse] = flt(projected_qty)
warehouse_doc = frappe.get_doc("Warehouse", warehouse)
while warehouse_doc.parent_warehouse:
if not item_warehouse_projected_qty.get(item_code, {}).get(warehouse_doc.parent_warehouse):
item_warehouse_projected_qty.setdefault(item_code, {})[warehouse_doc.parent_warehouse] = flt(
projected_qty
)
else:
item_warehouse_projected_qty[item_code][warehouse_doc.parent_warehouse] += flt(projected_qty)
warehouse_doc = frappe.get_doc("Warehouse", warehouse_doc.parent_warehouse)
return item_warehouse_projected_qty
def create_material_request(material_requests):
mr_list = []
exceptions_list = []
def _log_exception(mr):
if frappe.local.message_log:
exceptions_list.extend(frappe.local.message_log)
frappe.local.message_log = []
else:
exceptions_list.append(frappe.get_traceback())
mr.log_error("Unable to create material request")
for request_type in material_requests:
for company in material_requests[request_type]:
try:
items = material_requests[request_type][company]
if not items:
continue
# Group items by warehouse
items_by_warehouse = {}
for d in items:
d = frappe._dict(d)
if d.warehouse not in items_by_warehouse:
items_by_warehouse[d.warehouse] = []
items_by_warehouse[d.warehouse].append(d)
# Create a Material Request for each warehouse
for warehouse, warehouse_items in items_by_warehouse.items():
mr = frappe.new_doc("Material Request")
mr.update(
{
"company": company,
"transaction_date": nowdate(),
"material_request_type": "Material Transfer" if request_type == "Transfer" else request_type,
}
)
for d in warehouse_items:
item = frappe.get_doc("Item", d.item_code)
uom = item.stock_uom
conversion_factor = 1.0
if request_type == "Purchase":
uom = item.purchase_uom or item.stock_uom
if uom != item.stock_uom:
conversion_factor = (
frappe.db.get_value(
"UOM Conversion Detail", {"parent": item.name, "uom": uom}, "conversion_factor"
)
or 1.0
)
must_be_whole_number = frappe.db.get_value("UOM", uom, "must_be_whole_number", cache=True)
qty = d.reorder_qty / conversion_factor
if must_be_whole_number:
qty = ceil(qty)
mr.append(
"items",
{
"doctype": "Material Request Item",
"item_code": d.item_code,
"schedule_date": add_days(nowdate(), cint(item.lead_time_days)),
"qty": qty,
"uom": uom,
"stock_uom": item.stock_uom,
"warehouse": warehouse,
"item_name": item.item_name,
"description": item.description,
"item_group": item.item_group,
"brand": item.brand,
},
)
schedule_dates = [d.schedule_date for d in mr.items]
mr.schedule_date = max(schedule_dates or [nowdate()])
mr.flags.ignore_mandatory = True
mr.insert()
mr.submit()
mr_list.append(mr)
except Exception:
_log_exception(mr)
if mr_list:
if getattr(frappe.local, "reorder_email_notify", None) is None:
frappe.local.reorder_email_notify = cint(
frappe.db.get_value("Stock Settings", None, "reorder_email_notify")
)
if frappe.local.reorder_email_notify:
send_email_notification(mr_list)
if exceptions_list:
notify_errors(exceptions_list)
return mr_list
when this is executed :
bench execute erpnext.stock.reorder_item.reorder_item
no error message appears, but the material request is not created.
Is there an error here? please help