Anand Doshi | 885e074 | 2015-03-03 14:55:30 +0530 | [diff] [blame] | 1 | # Copyright (c) 2015, Frappe Technologies Pvt. Ltd. and Contributors |
Rushabh Mehta | f850987 | 2014-10-08 12:03:19 +0530 | [diff] [blame] | 2 | # License: GNU General Public License v3. See license.txt |
| 3 | |
Chillar Anand | 915b343 | 2021-09-02 16:44:59 +0530 | [diff] [blame] | 4 | |
Marica | 9ea1ad4 | 2020-04-21 12:52:29 +0530 | [diff] [blame] | 5 | import json |
Alan | d4b2471 | 2021-10-06 18:16:33 +0530 | [diff] [blame] | 6 | from math import ceil |
Chillar Anand | 915b343 | 2021-09-02 16:44:59 +0530 | [diff] [blame] | 7 | |
| 8 | import frappe |
Anand Doshi | 5cf7a0b | 2015-11-26 15:11:18 +0530 | [diff] [blame] | 9 | from frappe import _ |
Chillar Anand | 915b343 | 2021-09-02 16:44:59 +0530 | [diff] [blame] | 10 | from frappe.utils import add_days, cint, flt, nowdate |
| 11 | |
| 12 | import erpnext |
| 13 | |
Rushabh Mehta | f850987 | 2014-10-08 12:03:19 +0530 | [diff] [blame] | 14 | |
| 15 | def reorder_item(): |
| 16 | """ Reorder item if stock reaches reorder level""" |
| 17 | # if initial setup not completed, return |
Anand Doshi | 8860b9d | 2015-05-28 01:09:21 -0400 | [diff] [blame] | 18 | if not (frappe.db.a_row_exists("Company") and frappe.db.a_row_exists("Fiscal Year")): |
Rushabh Mehta | f850987 | 2014-10-08 12:03:19 +0530 | [diff] [blame] | 19 | return |
| 20 | |
Nabin Hait | fd4bcd8 | 2015-05-22 16:55:40 +0530 | [diff] [blame] | 21 | if cint(frappe.db.get_value('Stock Settings', None, 'auto_indent')): |
Rushabh Mehta | f850987 | 2014-10-08 12:03:19 +0530 | [diff] [blame] | 22 | return _reorder_item() |
| 23 | |
| 24 | def _reorder_item(): |
Rohit Waghchaure | 08aadb8 | 2016-07-29 13:23:30 +0530 | [diff] [blame] | 25 | material_requests = {"Purchase": {}, "Transfer": {}, "Material Issue": {}, "Manufacture": {}} |
Anand Doshi | 7b2b0cd | 2015-10-15 18:07:51 +0530 | [diff] [blame] | 26 | warehouse_company = frappe._dict(frappe.db.sql("""select name, company from `tabWarehouse` |
| 27 | where disabled=0""")) |
Rushabh Mehta | 86a824c | 2016-04-15 16:44:44 +0530 | [diff] [blame] | 28 | default_company = (erpnext.get_default_company() or |
Rushabh Mehta | f850987 | 2014-10-08 12:03:19 +0530 | [diff] [blame] | 29 | frappe.db.sql("""select name from tabCompany limit 1""")[0][0]) |
| 30 | |
Anand Doshi | 7b2b0cd | 2015-10-15 18:07:51 +0530 | [diff] [blame] | 31 | items_to_consider = frappe.db.sql_list("""select name from `tabItem` item |
| 32 | where is_stock_item=1 and has_variants=0 |
Anand Doshi | 21e09a2 | 2015-10-29 12:21:41 +0530 | [diff] [blame] | 33 | and disabled=0 |
Anand Doshi | 7b2b0cd | 2015-10-15 18:07:51 +0530 | [diff] [blame] | 34 | and (end_of_life is null or end_of_life='0000-00-00' or end_of_life > %(today)s) |
Rushabh Mehta | 3cdf354 | 2016-02-08 11:52:22 +0530 | [diff] [blame] | 35 | and (exists (select name from `tabItem Reorder` ir where ir.parent=item.name) |
Anand Doshi | 7b2b0cd | 2015-10-15 18:07:51 +0530 | [diff] [blame] | 36 | or (variant_of is not null and variant_of != '' |
Rushabh Mehta | 3cdf354 | 2016-02-08 11:52:22 +0530 | [diff] [blame] | 37 | and exists (select name from `tabItem Reorder` ir where ir.parent=item.variant_of)) |
Anand Doshi | 7b2b0cd | 2015-10-15 18:07:51 +0530 | [diff] [blame] | 38 | )""", |
| 39 | {"today": nowdate()}) |
| 40 | |
| 41 | if not items_to_consider: |
| 42 | return |
| 43 | |
| 44 | item_warehouse_projected_qty = get_item_warehouse_projected_qty(items_to_consider) |
| 45 | |
Saurabh | 3d6aecd | 2016-06-20 17:25:45 +0530 | [diff] [blame] | 46 | def add_to_material_request(item_code, warehouse, reorder_level, reorder_qty, material_request_type, warehouse_group=None): |
Anand Doshi | 7b2b0cd | 2015-10-15 18:07:51 +0530 | [diff] [blame] | 47 | if warehouse not in warehouse_company: |
| 48 | # a disabled warehouse |
Rushabh Mehta | f850987 | 2014-10-08 12:03:19 +0530 | [diff] [blame] | 49 | return |
| 50 | |
| 51 | reorder_level = flt(reorder_level) |
| 52 | reorder_qty = flt(reorder_qty) |
Anand Doshi | 7b2b0cd | 2015-10-15 18:07:51 +0530 | [diff] [blame] | 53 | |
| 54 | # projected_qty will be 0 if Bin does not exist |
Saurabh | 3d6aecd | 2016-06-20 17:25:45 +0530 | [diff] [blame] | 55 | if warehouse_group: |
| 56 | projected_qty = flt(item_warehouse_projected_qty.get(item_code, {}).get(warehouse_group)) |
| 57 | else: |
| 58 | projected_qty = flt(item_warehouse_projected_qty.get(item_code, {}).get(warehouse)) |
Rushabh Mehta | f850987 | 2014-10-08 12:03:19 +0530 | [diff] [blame] | 59 | |
Nabin Hait | e32f9c3 | 2016-02-22 16:50:13 +0530 | [diff] [blame] | 60 | if (reorder_level or reorder_qty) and projected_qty < reorder_level: |
Rushabh Mehta | f850987 | 2014-10-08 12:03:19 +0530 | [diff] [blame] | 61 | deficiency = reorder_level - projected_qty |
| 62 | if deficiency > reorder_qty: |
| 63 | reorder_qty = deficiency |
| 64 | |
| 65 | company = warehouse_company.get(warehouse) or default_company |
| 66 | |
| 67 | material_requests[material_request_type].setdefault(company, []).append({ |
| 68 | "item_code": item_code, |
| 69 | "warehouse": warehouse, |
| 70 | "reorder_qty": reorder_qty |
| 71 | }) |
| 72 | |
Anand Doshi | 7b2b0cd | 2015-10-15 18:07:51 +0530 | [diff] [blame] | 73 | for item_code in items_to_consider: |
Rushabh Mehta | f850987 | 2014-10-08 12:03:19 +0530 | [diff] [blame] | 74 | item = frappe.get_doc("Item", item_code) |
| 75 | |
Nabin Hait | e7d1536 | 2014-12-25 16:01:55 +0530 | [diff] [blame] | 76 | if item.variant_of and not item.get("reorder_levels"): |
Rushabh Mehta | f850987 | 2014-10-08 12:03:19 +0530 | [diff] [blame] | 77 | item.update_template_tables() |
| 78 | |
Nabin Hait | e7d1536 | 2014-12-25 16:01:55 +0530 | [diff] [blame] | 79 | if item.get("reorder_levels"): |
| 80 | for d in item.get("reorder_levels"): |
Rushabh Mehta | f850987 | 2014-10-08 12:03:19 +0530 | [diff] [blame] | 81 | add_to_material_request(item_code, d.warehouse, d.warehouse_reorder_level, |
Saurabh | 3d6aecd | 2016-06-20 17:25:45 +0530 | [diff] [blame] | 82 | d.warehouse_reorder_qty, d.material_request_type, warehouse_group=d.warehouse_group) |
Rushabh Mehta | f850987 | 2014-10-08 12:03:19 +0530 | [diff] [blame] | 83 | |
Rushabh Mehta | f850987 | 2014-10-08 12:03:19 +0530 | [diff] [blame] | 84 | if material_requests: |
| 85 | return create_material_request(material_requests) |
| 86 | |
Anand Doshi | 7b2b0cd | 2015-10-15 18:07:51 +0530 | [diff] [blame] | 87 | def get_item_warehouse_projected_qty(items_to_consider): |
Rushabh Mehta | f850987 | 2014-10-08 12:03:19 +0530 | [diff] [blame] | 88 | item_warehouse_projected_qty = {} |
| 89 | |
| 90 | for item_code, warehouse, projected_qty in frappe.db.sql("""select item_code, warehouse, projected_qty |
Anand Doshi | 7b2b0cd | 2015-10-15 18:07:51 +0530 | [diff] [blame] | 91 | from tabBin where item_code in ({0}) |
| 92 | and (warehouse != "" and warehouse is not null)"""\ |
| 93 | .format(", ".join(["%s"] * len(items_to_consider))), items_to_consider): |
Rohit Waghchaure | f83f6aa | 2018-01-20 15:44:38 +0530 | [diff] [blame] | 94 | |
| 95 | if item_code not in item_warehouse_projected_qty: |
| 96 | item_warehouse_projected_qty.setdefault(item_code, {}) |
| 97 | |
| 98 | if warehouse not in item_warehouse_projected_qty.get(item_code): |
| 99 | item_warehouse_projected_qty[item_code][warehouse] = flt(projected_qty) |
| 100 | |
Saurabh | 3d6aecd | 2016-06-20 17:25:45 +0530 | [diff] [blame] | 101 | warehouse_doc = frappe.get_doc("Warehouse", warehouse) |
Rohit Waghchaure | f83f6aa | 2018-01-20 15:44:38 +0530 | [diff] [blame] | 102 | |
ppd1990 | 9bd8427 | 2017-11-15 05:43:09 +0100 | [diff] [blame] | 103 | while warehouse_doc.parent_warehouse: |
Saurabh | 3d6aecd | 2016-06-20 17:25:45 +0530 | [diff] [blame] | 104 | if not item_warehouse_projected_qty.get(item_code, {}).get(warehouse_doc.parent_warehouse): |
| 105 | item_warehouse_projected_qty.setdefault(item_code, {})[warehouse_doc.parent_warehouse] = flt(projected_qty) |
| 106 | else: |
| 107 | item_warehouse_projected_qty[item_code][warehouse_doc.parent_warehouse] += flt(projected_qty) |
ppd1990 | 9bd8427 | 2017-11-15 05:43:09 +0100 | [diff] [blame] | 108 | warehouse_doc = frappe.get_doc("Warehouse", warehouse_doc.parent_warehouse) |
Rohit Waghchaure | f83f6aa | 2018-01-20 15:44:38 +0530 | [diff] [blame] | 109 | |
Rushabh Mehta | f850987 | 2014-10-08 12:03:19 +0530 | [diff] [blame] | 110 | return item_warehouse_projected_qty |
| 111 | |
| 112 | def create_material_request(material_requests): |
| 113 | """ Create indent on reaching reorder level """ |
| 114 | mr_list = [] |
Rushabh Mehta | f850987 | 2014-10-08 12:03:19 +0530 | [diff] [blame] | 115 | exceptions_list = [] |
| 116 | |
| 117 | def _log_exception(): |
| 118 | if frappe.local.message_log: |
| 119 | exceptions_list.extend(frappe.local.message_log) |
| 120 | frappe.local.message_log = [] |
| 121 | else: |
| 122 | exceptions_list.append(frappe.get_traceback()) |
| 123 | |
rohitwaghchaure | 1e6788b | 2020-02-26 11:25:34 +0530 | [diff] [blame] | 124 | frappe.log_error(frappe.get_traceback()) |
| 125 | |
Rushabh Mehta | f850987 | 2014-10-08 12:03:19 +0530 | [diff] [blame] | 126 | for request_type in material_requests: |
| 127 | for company in material_requests[request_type]: |
| 128 | try: |
| 129 | items = material_requests[request_type][company] |
| 130 | if not items: |
| 131 | continue |
| 132 | |
| 133 | mr = frappe.new_doc("Material Request") |
| 134 | mr.update({ |
| 135 | "company": company, |
Rushabh Mehta | f850987 | 2014-10-08 12:03:19 +0530 | [diff] [blame] | 136 | "transaction_date": nowdate(), |
rohitwaghchaure | 373b570 | 2017-11-27 11:27:28 +0530 | [diff] [blame] | 137 | "material_request_type": "Material Transfer" if request_type=="Transfer" else request_type |
Rushabh Mehta | f850987 | 2014-10-08 12:03:19 +0530 | [diff] [blame] | 138 | }) |
| 139 | |
| 140 | for d in items: |
| 141 | d = frappe._dict(d) |
| 142 | item = frappe.get_doc("Item", d.item_code) |
rohitwaghchaure | e8ccc0e | 2017-11-21 16:17:22 +0530 | [diff] [blame] | 143 | uom = item.stock_uom |
| 144 | conversion_factor = 1.0 |
| 145 | |
| 146 | if request_type == 'Purchase': |
| 147 | uom = item.purchase_uom or item.stock_uom |
| 148 | if uom != item.stock_uom: |
Aditya Hase | f3c22f3 | 2019-01-22 18:22:20 +0530 | [diff] [blame] | 149 | conversion_factor = frappe.db.get_value("UOM Conversion Detail", |
rohitwaghchaure | e8ccc0e | 2017-11-21 16:17:22 +0530 | [diff] [blame] | 150 | {'parent': item.name, 'uom': uom}, 'conversion_factor') or 1.0 |
| 151 | |
Alan | d4b2471 | 2021-10-06 18:16:33 +0530 | [diff] [blame] | 152 | must_be_whole_number = frappe.db.get_value("UOM", uom, "must_be_whole_number", cache=True) |
| 153 | qty = d.reorder_qty / conversion_factor |
| 154 | if must_be_whole_number: |
| 155 | qty = ceil(qty) |
| 156 | |
Nabin Hait | e7d1536 | 2014-12-25 16:01:55 +0530 | [diff] [blame] | 157 | mr.append("items", { |
Rushabh Mehta | f850987 | 2014-10-08 12:03:19 +0530 | [diff] [blame] | 158 | "doctype": "Material Request Item", |
| 159 | "item_code": d.item_code, |
| 160 | "schedule_date": add_days(nowdate(),cint(item.lead_time_days)), |
Alan | d4b2471 | 2021-10-06 18:16:33 +0530 | [diff] [blame] | 161 | "qty": qty, |
rohitwaghchaure | e8ccc0e | 2017-11-21 16:17:22 +0530 | [diff] [blame] | 162 | "uom": uom, |
| 163 | "stock_uom": item.stock_uom, |
Rushabh Mehta | f850987 | 2014-10-08 12:03:19 +0530 | [diff] [blame] | 164 | "warehouse": d.warehouse, |
| 165 | "item_name": item.item_name, |
| 166 | "description": item.description, |
| 167 | "item_group": item.item_group, |
Rushabh Mehta | f850987 | 2014-10-08 12:03:19 +0530 | [diff] [blame] | 168 | "brand": item.brand, |
| 169 | }) |
| 170 | |
rohitwaghchaure | 373b570 | 2017-11-27 11:27:28 +0530 | [diff] [blame] | 171 | schedule_dates = [d.schedule_date for d in mr.items] |
| 172 | mr.schedule_date = max(schedule_dates or [nowdate()]) |
rohitwaghchaure | 1e6788b | 2020-02-26 11:25:34 +0530 | [diff] [blame] | 173 | mr.flags.ignore_mandatory = True |
Rushabh Mehta | f850987 | 2014-10-08 12:03:19 +0530 | [diff] [blame] | 174 | mr.insert() |
| 175 | mr.submit() |
| 176 | mr_list.append(mr) |
| 177 | |
Ankush Menat | 694ae81 | 2021-09-01 14:40:56 +0530 | [diff] [blame] | 178 | except Exception: |
Rushabh Mehta | f850987 | 2014-10-08 12:03:19 +0530 | [diff] [blame] | 179 | _log_exception() |
| 180 | |
| 181 | if mr_list: |
| 182 | if getattr(frappe.local, "reorder_email_notify", None) is None: |
| 183 | frappe.local.reorder_email_notify = cint(frappe.db.get_value('Stock Settings', None, |
| 184 | 'reorder_email_notify')) |
| 185 | |
| 186 | if(frappe.local.reorder_email_notify): |
| 187 | send_email_notification(mr_list) |
| 188 | |
| 189 | if exceptions_list: |
| 190 | notify_errors(exceptions_list) |
| 191 | |
| 192 | return mr_list |
| 193 | |
| 194 | def send_email_notification(mr_list): |
| 195 | """ Notify user about auto creation of indent""" |
| 196 | |
| 197 | email_list = frappe.db.sql_list("""select distinct r.parent |
Rohit Waghchaure | 3124980 | 2017-02-20 15:11:56 +0530 | [diff] [blame] | 198 | from `tabHas Role` r, tabUser p |
Rushabh Mehta | f850987 | 2014-10-08 12:03:19 +0530 | [diff] [blame] | 199 | where p.name = r.parent and p.enabled = 1 and p.docstatus < 2 |
Rushabh Mehta | 9415733 | 2015-07-13 15:06:12 +0530 | [diff] [blame] | 200 | and r.role in ('Purchase Manager','Stock Manager') |
Rushabh Mehta | f850987 | 2014-10-08 12:03:19 +0530 | [diff] [blame] | 201 | and p.name not in ('Administrator', 'All', 'Guest')""") |
| 202 | |
Anand Doshi | 5cf7a0b | 2015-11-26 15:11:18 +0530 | [diff] [blame] | 203 | msg = frappe.render_template("templates/emails/reorder_item.html", { |
| 204 | "mr_list": mr_list |
| 205 | }) |
| 206 | |
Anand Doshi | 8860b9d | 2015-05-28 01:09:21 -0400 | [diff] [blame] | 207 | frappe.sendmail(recipients=email_list, |
Anand Doshi | 5cf7a0b | 2015-11-26 15:11:18 +0530 | [diff] [blame] | 208 | subject=_('Auto Material Requests Generated'), message = msg) |
Rushabh Mehta | f850987 | 2014-10-08 12:03:19 +0530 | [diff] [blame] | 209 | |
| 210 | def notify_errors(exceptions_list): |
Marica | 9ea1ad4 | 2020-04-21 12:52:29 +0530 | [diff] [blame] | 211 | subject = _("[Important] [ERPNext] Auto Reorder Errors") |
| 212 | content = _("Dear System Manager,") + "<br>" + _("An error occured for certain Items while creating Material Requests based on Re-order level. \ |
| 213 | Please rectify these issues :") + "<br>" |
Rushabh Mehta | f850987 | 2014-10-08 12:03:19 +0530 | [diff] [blame] | 214 | |
Marica | 9ea1ad4 | 2020-04-21 12:52:29 +0530 | [diff] [blame] | 215 | for exception in exceptions_list: |
| 216 | exception = json.loads(exception) |
| 217 | error_message = """<div class='small text-muted'>{0}</div><br>""".format(_(exception.get("message"))) |
| 218 | content += error_message |
Rushabh Mehta | f850987 | 2014-10-08 12:03:19 +0530 | [diff] [blame] | 219 | |
Marica | 9ea1ad4 | 2020-04-21 12:52:29 +0530 | [diff] [blame] | 220 | content += _("Regards,") + "<br>" + _("Administrator") |
Rushabh Mehta | f850987 | 2014-10-08 12:03:19 +0530 | [diff] [blame] | 221 | |
| 222 | from frappe.email import sendmail_to_system_managers |
| 223 | sendmail_to_system_managers(subject, content) |