2015-03-03 09:25:30 +00:00
|
|
|
# Copyright (c) 2015, Frappe Technologies Pvt. Ltd. and Contributors
|
2014-10-08 06:33:19 +00:00
|
|
|
# License: GNU General Public License v3. See license.txt
|
|
|
|
|
2021-09-02 11:14:59 +00:00
|
|
|
|
2020-04-21 07:22:29 +00:00
|
|
|
import json
|
2021-10-06 12:46:33 +00:00
|
|
|
from math import ceil
|
2021-09-02 11:14:59 +00:00
|
|
|
|
2016-02-08 06:22:22 +00:00
|
|
|
import frappe
|
2015-11-26 09:41:18 +00:00
|
|
|
from frappe import _
|
2016-02-08 06:22:22 +00:00
|
|
|
from frappe.utils import add_days, cint, flt, nowdate
|
2021-09-02 11:14:59 +00:00
|
|
|
|
2016-04-15 11:14:44 +00:00
|
|
|
import erpnext
|
2021-09-02 11:14:59 +00:00
|
|
|
|
2014-10-08 06:33:19 +00:00
|
|
|
|
|
|
|
def reorder_item():
|
|
|
|
"""Reorder item if stock reaches reorder level"""
|
|
|
|
# if initial setup not completed, return
|
2015-05-28 05:09:21 +00:00
|
|
|
if not (frappe.db.a_row_exists("Company") and frappe.db.a_row_exists("Fiscal Year")):
|
2014-10-08 06:33:19 +00:00
|
|
|
return
|
|
|
|
|
2015-05-22 11:25:40 +00:00
|
|
|
if cint(frappe.db.get_value("Stock Settings", None, "auto_indent")):
|
2014-10-08 06:33:19 +00:00
|
|
|
return _reorder_item()
|
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
|
2014-10-08 06:33:19 +00:00
|
|
|
def _reorder_item():
|
2016-07-29 07:53:30 +00:00
|
|
|
material_requests = {"Purchase": {}, "Transfer": {}, "Material Issue": {}, "Manufacture": {}}
|
2015-10-15 12:37:51 +00:00
|
|
|
warehouse_company = frappe._dict(
|
|
|
|
frappe.db.sql(
|
|
|
|
"""select name, company from `tabWarehouse`
|
|
|
|
where disabled=0"""
|
2016-04-15 11:14:44 +00:00
|
|
|
)
|
2022-03-28 13:22:46 +00:00
|
|
|
)
|
2016-04-15 11:14:44 +00:00
|
|
|
default_company = (
|
2014-10-08 06:33:19 +00:00
|
|
|
erpnext.get_default_company() or frappe.db.sql("""select name from tabCompany limit 1""")[0][0]
|
2022-03-28 13:22:46 +00:00
|
|
|
)
|
|
|
|
|
2015-10-15 12:37:51 +00:00
|
|
|
items_to_consider = frappe.db.sql_list(
|
|
|
|
"""select name from `tabItem` item
|
|
|
|
where is_stock_item=1 and has_variants=0
|
2015-10-29 06:51:41 +00:00
|
|
|
and disabled=0
|
2015-10-15 12:37:51 +00:00
|
|
|
and (end_of_life is null or end_of_life='0000-00-00' or end_of_life > %(today)s)
|
2016-02-08 06:22:22 +00:00
|
|
|
and (exists (select name from `tabItem Reorder` ir where ir.parent=item.name)
|
2015-10-15 12:37:51 +00:00
|
|
|
or (variant_of is not null and variant_of != ''
|
2016-02-08 06:22:22 +00:00
|
|
|
and exists (select name from `tabItem Reorder` ir where ir.parent=item.variant_of))
|
2015-10-15 12:37:51 +00:00
|
|
|
)""",
|
|
|
|
{"today": nowdate()},
|
|
|
|
)
|
|
|
|
|
|
|
|
if not items_to_consider:
|
|
|
|
return
|
|
|
|
|
|
|
|
item_warehouse_projected_qty = get_item_warehouse_projected_qty(items_to_consider)
|
|
|
|
|
2016-06-20 11:55:45 +00:00
|
|
|
def add_to_material_request(
|
|
|
|
item_code, warehouse, reorder_level, reorder_qty, material_request_type, warehouse_group=None
|
|
|
|
):
|
2015-10-15 12:37:51 +00:00
|
|
|
if warehouse not in warehouse_company:
|
|
|
|
# a disabled warehouse
|
2014-10-08 06:33:19 +00:00
|
|
|
return
|
|
|
|
|
|
|
|
reorder_level = flt(reorder_level)
|
|
|
|
reorder_qty = flt(reorder_qty)
|
2015-10-15 12:37:51 +00:00
|
|
|
|
|
|
|
# projected_qty will be 0 if Bin does not exist
|
2016-06-20 11:55:45 +00:00
|
|
|
if warehouse_group:
|
|
|
|
projected_qty = flt(item_warehouse_projected_qty.get(item_code, {}).get(warehouse_group))
|
|
|
|
else:
|
|
|
|
projected_qty = flt(item_warehouse_projected_qty.get(item_code, {}).get(warehouse))
|
2014-10-08 06:33:19 +00:00
|
|
|
|
2016-02-22 11:20:13 +00:00
|
|
|
if (reorder_level or reorder_qty) and projected_qty < reorder_level:
|
2014-10-08 06:33:19 +00:00
|
|
|
deficiency = reorder_level - projected_qty
|
|
|
|
if deficiency > reorder_qty:
|
|
|
|
reorder_qty = deficiency
|
|
|
|
|
|
|
|
company = warehouse_company.get(warehouse) or default_company
|
|
|
|
|
|
|
|
material_requests[material_request_type].setdefault(company, []).append(
|
|
|
|
{"item_code": item_code, "warehouse": warehouse, "reorder_qty": reorder_qty}
|
|
|
|
)
|
|
|
|
|
2015-10-15 12:37:51 +00:00
|
|
|
for item_code in items_to_consider:
|
2014-10-08 06:33:19 +00:00
|
|
|
item = frappe.get_doc("Item", item_code)
|
|
|
|
|
2014-12-25 10:31:55 +00:00
|
|
|
if item.variant_of and not item.get("reorder_levels"):
|
2014-10-08 06:33:19 +00:00
|
|
|
item.update_template_tables()
|
|
|
|
|
2014-12-25 10:31:55 +00:00
|
|
|
if item.get("reorder_levels"):
|
|
|
|
for d in item.get("reorder_levels"):
|
2014-10-08 06:33:19 +00:00
|
|
|
add_to_material_request(
|
|
|
|
item_code,
|
|
|
|
d.warehouse,
|
|
|
|
d.warehouse_reorder_level,
|
2016-06-20 11:55:45 +00:00
|
|
|
d.warehouse_reorder_qty,
|
|
|
|
d.material_request_type,
|
|
|
|
warehouse_group=d.warehouse_group,
|
|
|
|
)
|
2014-10-08 06:33:19 +00:00
|
|
|
|
|
|
|
if material_requests:
|
|
|
|
return create_material_request(material_requests)
|
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
|
2015-10-15 12:37:51 +00:00
|
|
|
def get_item_warehouse_projected_qty(items_to_consider):
|
2014-10-08 06:33:19 +00:00
|
|
|
item_warehouse_projected_qty = {}
|
|
|
|
|
|
|
|
for item_code, warehouse, projected_qty in frappe.db.sql(
|
|
|
|
"""select item_code, warehouse, projected_qty
|
2015-10-15 12:37:51 +00:00
|
|
|
from tabBin where item_code in ({0})
|
|
|
|
and (warehouse != "" and warehouse is not null)""".format(
|
|
|
|
", ".join(["%s"] * len(items_to_consider))
|
|
|
|
),
|
|
|
|
items_to_consider,
|
|
|
|
):
|
2018-01-20 10:14:38 +00:00
|
|
|
|
|
|
|
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)
|
|
|
|
|
2016-06-20 11:55:45 +00:00
|
|
|
warehouse_doc = frappe.get_doc("Warehouse", warehouse)
|
2018-01-20 10:14:38 +00:00
|
|
|
|
2017-11-15 04:43:09 +00:00
|
|
|
while warehouse_doc.parent_warehouse:
|
2016-06-20 11:55:45 +00:00
|
|
|
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)
|
2017-11-15 04:43:09 +00:00
|
|
|
warehouse_doc = frappe.get_doc("Warehouse", warehouse_doc.parent_warehouse)
|
2018-01-20 10:14:38 +00:00
|
|
|
|
2014-10-08 06:33:19 +00:00
|
|
|
return item_warehouse_projected_qty
|
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
|
2014-10-08 06:33:19 +00:00
|
|
|
def create_material_request(material_requests):
|
|
|
|
"""Create indent on reaching reorder level"""
|
|
|
|
mr_list = []
|
|
|
|
exceptions_list = []
|
|
|
|
|
2022-05-02 09:34:26 +00:00
|
|
|
def _log_exception(mr):
|
2014-10-08 06:33:19 +00:00
|
|
|
if frappe.local.message_log:
|
|
|
|
exceptions_list.extend(frappe.local.message_log)
|
|
|
|
frappe.local.message_log = []
|
|
|
|
else:
|
|
|
|
exceptions_list.append(frappe.get_traceback())
|
|
|
|
|
2022-05-02 09:34:26 +00:00
|
|
|
mr.log_error("Unable to create material request")
|
2020-02-26 05:55:34 +00:00
|
|
|
|
2014-10-08 06:33:19 +00:00
|
|
|
for request_type in material_requests:
|
|
|
|
for company in material_requests[request_type]:
|
|
|
|
try:
|
|
|
|
items = material_requests[request_type][company]
|
|
|
|
if not items:
|
|
|
|
continue
|
|
|
|
|
|
|
|
mr = frappe.new_doc("Material Request")
|
|
|
|
mr.update(
|
|
|
|
{
|
|
|
|
"company": company,
|
|
|
|
"transaction_date": nowdate(),
|
2017-11-27 05:57:28 +00:00
|
|
|
"material_request_type": "Material Transfer" if request_type == "Transfer" else request_type,
|
2014-10-08 06:33:19 +00:00
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
for d in items:
|
|
|
|
d = frappe._dict(d)
|
|
|
|
item = frappe.get_doc("Item", d.item_code)
|
2017-11-21 10:47:22 +00:00
|
|
|
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:
|
2019-01-22 12:52:20 +00:00
|
|
|
conversion_factor = (
|
|
|
|
frappe.db.get_value(
|
2017-11-21 10:47:22 +00:00
|
|
|
"UOM Conversion Detail", {"parent": item.name, "uom": uom}, "conversion_factor"
|
|
|
|
)
|
|
|
|
or 1.0
|
2022-03-28 13:22:46 +00:00
|
|
|
)
|
2017-11-21 10:47:22 +00:00
|
|
|
|
2021-10-06 12:46:33 +00:00
|
|
|
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)
|
|
|
|
|
2014-12-25 10:31:55 +00:00
|
|
|
mr.append(
|
|
|
|
"items",
|
|
|
|
{
|
2014-10-08 06:33:19 +00:00
|
|
|
"doctype": "Material Request Item",
|
|
|
|
"item_code": d.item_code,
|
|
|
|
"schedule_date": add_days(nowdate(), cint(item.lead_time_days)),
|
2021-10-06 12:46:33 +00:00
|
|
|
"qty": qty,
|
2017-11-21 10:47:22 +00:00
|
|
|
"uom": uom,
|
|
|
|
"stock_uom": item.stock_uom,
|
2014-10-08 06:33:19 +00:00
|
|
|
"warehouse": d.warehouse,
|
|
|
|
"item_name": item.item_name,
|
|
|
|
"description": item.description,
|
|
|
|
"item_group": item.item_group,
|
|
|
|
"brand": item.brand,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
2017-11-27 05:57:28 +00:00
|
|
|
schedule_dates = [d.schedule_date for d in mr.items]
|
|
|
|
mr.schedule_date = max(schedule_dates or [nowdate()])
|
2020-02-26 05:55:34 +00:00
|
|
|
mr.flags.ignore_mandatory = True
|
2014-10-08 06:33:19 +00:00
|
|
|
mr.insert()
|
|
|
|
mr.submit()
|
|
|
|
mr_list.append(mr)
|
|
|
|
|
2021-09-01 09:10:56 +00:00
|
|
|
except Exception:
|
2022-05-02 09:34:26 +00:00
|
|
|
_log_exception(mr)
|
2014-10-08 06:33:19 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
|
2014-10-08 06:33:19 +00:00
|
|
|
def send_email_notification(mr_list):
|
|
|
|
"""Notify user about auto creation of indent"""
|
|
|
|
|
|
|
|
email_list = frappe.db.sql_list(
|
|
|
|
"""select distinct r.parent
|
2017-02-20 09:41:56 +00:00
|
|
|
from `tabHas Role` r, tabUser p
|
2014-10-08 06:33:19 +00:00
|
|
|
where p.name = r.parent and p.enabled = 1 and p.docstatus < 2
|
2015-07-13 09:36:12 +00:00
|
|
|
and r.role in ('Purchase Manager','Stock Manager')
|
2014-10-08 06:33:19 +00:00
|
|
|
and p.name not in ('Administrator', 'All', 'Guest')"""
|
|
|
|
)
|
2022-03-28 13:22:46 +00:00
|
|
|
|
2015-11-26 09:41:18 +00:00
|
|
|
msg = frappe.render_template("templates/emails/reorder_item.html", {"mr_list": mr_list})
|
2014-10-08 06:33:19 +00:00
|
|
|
|
2015-11-26 09:41:18 +00:00
|
|
|
frappe.sendmail(recipients=email_list, subject=_("Auto Material Requests Generated"), message=msg)
|
|
|
|
|
2014-10-08 06:33:19 +00:00
|
|
|
|
|
|
|
def notify_errors(exceptions_list):
|
2020-04-21 07:22:29 +00:00
|
|
|
subject = _("[Important] [ERPNext] Auto Reorder Errors")
|
|
|
|
content = (
|
|
|
|
_("Dear System Manager,")
|
|
|
|
+ "<br>"
|
|
|
|
+ _(
|
2022-04-02 11:26:59 +00:00
|
|
|
"An error occured for certain Items while creating Material Requests based on Re-order level. Please rectify these issues :"
|
2020-04-21 07:22:29 +00:00
|
|
|
)
|
|
|
|
+ "<br>"
|
2022-03-28 13:22:46 +00:00
|
|
|
)
|
2020-04-21 07:22:29 +00:00
|
|
|
|
|
|
|
for exception in exceptions_list:
|
2022-05-23 14:36:24 +00:00
|
|
|
try:
|
|
|
|
exception = json.loads(exception)
|
|
|
|
error_message = """<div class='small text-muted'>{0}</div><br>""".format(
|
|
|
|
_(exception.get("message"))
|
|
|
|
)
|
|
|
|
content += error_message
|
|
|
|
except Exception:
|
|
|
|
pass
|
2020-04-21 07:22:29 +00:00
|
|
|
|
|
|
|
content += _("Regards,") + "<br>" + _("Administrator")
|
2014-10-08 06:33:19 +00:00
|
|
|
|
|
|
|
from frappe.email import sendmail_to_system_managers
|
2022-03-28 13:22:46 +00:00
|
|
|
|
2014-10-08 06:33:19 +00:00
|
|
|
sendmail_to_system_managers(subject, content)
|