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