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