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 | bb18ae8 | 2024-01-09 21:56:47 +0530 | [diff] [blame] | 21 | if cint(frappe.db.get_single_value("Stock Settings", "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 | |
Rohit Waghchaure | 951023f | 2024-01-31 11:32:17 +0530 | [diff] [blame] | 37 | items_to_consider = get_items_for_reorder() |
Anand Doshi | 7b2b0cd | 2015-10-15 18:07:51 +0530 | [diff] [blame] | 38 | |
| 39 | if not items_to_consider: |
| 40 | return |
| 41 | |
| 42 | item_warehouse_projected_qty = get_item_warehouse_projected_qty(items_to_consider) |
| 43 | |
Rohit Waghchaure | 951023f | 2024-01-31 11:32:17 +0530 | [diff] [blame] | 44 | def add_to_material_request(**kwargs): |
| 45 | if isinstance(kwargs, dict): |
| 46 | kwargs = frappe._dict(kwargs) |
| 47 | |
| 48 | if kwargs.warehouse not in warehouse_company: |
Anand Doshi | 7b2b0cd | 2015-10-15 18:07:51 +0530 | [diff] [blame] | 49 | # a disabled warehouse |
Rushabh Mehta | f850987 | 2014-10-08 12:03:19 +0530 | [diff] [blame] | 50 | return |
| 51 | |
Rohit Waghchaure | 951023f | 2024-01-31 11:32:17 +0530 | [diff] [blame] | 52 | reorder_level = flt(kwargs.reorder_level) |
| 53 | reorder_qty = flt(kwargs.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 |
Rohit Waghchaure | 951023f | 2024-01-31 11:32:17 +0530 | [diff] [blame] | 56 | if kwargs.warehouse_group: |
| 57 | projected_qty = flt( |
| 58 | item_warehouse_projected_qty.get(kwargs.item_code, {}).get(kwargs.warehouse_group) |
| 59 | ) |
Saurabh | 3d6aecd | 2016-06-20 17:25:45 +0530 | [diff] [blame] | 60 | else: |
Rohit Waghchaure | 951023f | 2024-01-31 11:32:17 +0530 | [diff] [blame] | 61 | projected_qty = flt( |
| 62 | item_warehouse_projected_qty.get(kwargs.item_code, {}).get(kwargs.warehouse) |
| 63 | ) |
Rushabh Mehta | f850987 | 2014-10-08 12:03:19 +0530 | [diff] [blame] | 64 | |
Devin Slauenwhite | f9ed8c1 | 2023-06-24 06:33:15 -0400 | [diff] [blame] | 65 | if (reorder_level or reorder_qty) and projected_qty <= reorder_level: |
Rushabh Mehta | f850987 | 2014-10-08 12:03:19 +0530 | [diff] [blame] | 66 | deficiency = reorder_level - projected_qty |
| 67 | if deficiency > reorder_qty: |
| 68 | reorder_qty = deficiency |
| 69 | |
Rohit Waghchaure | 951023f | 2024-01-31 11:32:17 +0530 | [diff] [blame] | 70 | company = warehouse_company.get(kwargs.warehouse) or default_company |
Rushabh Mehta | f850987 | 2014-10-08 12:03:19 +0530 | [diff] [blame] | 71 | |
Rohit Waghchaure | 951023f | 2024-01-31 11:32:17 +0530 | [diff] [blame] | 72 | material_requests[kwargs.material_request_type].setdefault(company, []).append( |
| 73 | { |
| 74 | "item_code": kwargs.item_code, |
| 75 | "warehouse": kwargs.warehouse, |
| 76 | "reorder_qty": reorder_qty, |
| 77 | "item_details": kwargs.item_details, |
| 78 | } |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 79 | ) |
Rushabh Mehta | f850987 | 2014-10-08 12:03:19 +0530 | [diff] [blame] | 80 | |
Rohit Waghchaure | 951023f | 2024-01-31 11:32:17 +0530 | [diff] [blame] | 81 | for item_code, reorder_levels in items_to_consider.items(): |
| 82 | for d in reorder_levels: |
| 83 | if d.has_variants: |
| 84 | continue |
Rushabh Mehta | f850987 | 2014-10-08 12:03:19 +0530 | [diff] [blame] | 85 | |
Rohit Waghchaure | 951023f | 2024-01-31 11:32:17 +0530 | [diff] [blame] | 86 | add_to_material_request( |
| 87 | item_code=item_code, |
| 88 | warehouse=d.warehouse, |
| 89 | reorder_level=d.warehouse_reorder_level, |
| 90 | reorder_qty=d.warehouse_reorder_qty, |
| 91 | material_request_type=d.material_request_type, |
| 92 | warehouse_group=d.warehouse_group, |
| 93 | item_details=frappe._dict( |
| 94 | { |
| 95 | "item_code": item_code, |
| 96 | "name": item_code, |
| 97 | "item_name": d.item_name, |
| 98 | "item_group": d.item_group, |
| 99 | "brand": d.brand, |
| 100 | "description": d.description, |
| 101 | "stock_uom": d.stock_uom, |
| 102 | "purchase_uom": d.purchase_uom, |
| 103 | } |
| 104 | ), |
| 105 | ) |
Rushabh Mehta | f850987 | 2014-10-08 12:03:19 +0530 | [diff] [blame] | 106 | |
Rushabh Mehta | f850987 | 2014-10-08 12:03:19 +0530 | [diff] [blame] | 107 | if material_requests: |
| 108 | return create_material_request(material_requests) |
| 109 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 110 | |
Rohit Waghchaure | 951023f | 2024-01-31 11:32:17 +0530 | [diff] [blame] | 111 | def get_items_for_reorder() -> dict[str, list]: |
| 112 | reorder_table = frappe.qb.DocType("Item Reorder") |
| 113 | item_table = frappe.qb.DocType("Item") |
| 114 | |
| 115 | query = ( |
| 116 | frappe.qb.from_(reorder_table) |
| 117 | .inner_join(item_table) |
| 118 | .on(reorder_table.parent == item_table.name) |
| 119 | .select( |
| 120 | reorder_table.warehouse, |
| 121 | reorder_table.warehouse_group, |
| 122 | reorder_table.material_request_type, |
| 123 | reorder_table.warehouse_reorder_level, |
| 124 | reorder_table.warehouse_reorder_qty, |
| 125 | item_table.name, |
| 126 | item_table.stock_uom, |
| 127 | item_table.purchase_uom, |
| 128 | item_table.description, |
| 129 | item_table.item_name, |
| 130 | item_table.item_group, |
| 131 | item_table.brand, |
| 132 | item_table.variant_of, |
| 133 | item_table.has_variants, |
| 134 | ) |
| 135 | .where( |
| 136 | (item_table.disabled == 0) |
| 137 | & (item_table.is_stock_item == 1) |
| 138 | & ( |
| 139 | (item_table.end_of_life.isnull()) |
| 140 | | (item_table.end_of_life > nowdate()) |
| 141 | | (item_table.end_of_life == "0000-00-00") |
| 142 | ) |
| 143 | ) |
| 144 | ) |
| 145 | |
| 146 | data = query.run(as_dict=True) |
| 147 | itemwise_reorder = frappe._dict({}) |
| 148 | for d in data: |
| 149 | itemwise_reorder.setdefault(d.name, []).append(d) |
| 150 | |
| 151 | itemwise_reorder = get_reorder_levels_for_variants(itemwise_reorder) |
| 152 | |
| 153 | return itemwise_reorder |
| 154 | |
| 155 | |
| 156 | def get_reorder_levels_for_variants(itemwise_reorder): |
| 157 | item_table = frappe.qb.DocType("Item") |
| 158 | |
| 159 | query = ( |
| 160 | frappe.qb.from_(item_table) |
| 161 | .select( |
| 162 | item_table.name, |
| 163 | item_table.variant_of, |
| 164 | ) |
| 165 | .where( |
| 166 | (item_table.disabled == 0) |
| 167 | & (item_table.is_stock_item == 1) |
| 168 | & ( |
| 169 | (item_table.end_of_life.isnull()) |
| 170 | | (item_table.end_of_life > nowdate()) |
| 171 | | (item_table.end_of_life == "0000-00-00") |
| 172 | ) |
| 173 | & (item_table.variant_of.notnull()) |
| 174 | ) |
| 175 | ) |
| 176 | |
| 177 | variants_item = query.run(as_dict=True) |
| 178 | for row in variants_item: |
| 179 | if not itemwise_reorder.get(row.name) and itemwise_reorder.get(row.variant_of): |
| 180 | itemwise_reorder.setdefault(row.name, []).extend(itemwise_reorder.get(row.variant_of, [])) |
| 181 | |
| 182 | return itemwise_reorder |
| 183 | |
| 184 | |
Anand Doshi | 7b2b0cd | 2015-10-15 18:07:51 +0530 | [diff] [blame] | 185 | def get_item_warehouse_projected_qty(items_to_consider): |
Rushabh Mehta | f850987 | 2014-10-08 12:03:19 +0530 | [diff] [blame] | 186 | item_warehouse_projected_qty = {} |
Rohit Waghchaure | 951023f | 2024-01-31 11:32:17 +0530 | [diff] [blame] | 187 | items_to_consider = list(items_to_consider.keys()) |
Rushabh Mehta | f850987 | 2014-10-08 12:03:19 +0530 | [diff] [blame] | 188 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 189 | for item_code, warehouse, projected_qty in frappe.db.sql( |
| 190 | """select item_code, warehouse, projected_qty |
Anand Doshi | 7b2b0cd | 2015-10-15 18:07:51 +0530 | [diff] [blame] | 191 | from tabBin where item_code in ({0}) |
Conor | 74a782d | 2022-06-17 06:31:27 -0500 | [diff] [blame] | 192 | and (warehouse != '' and warehouse is not null)""".format( |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 193 | ", ".join(["%s"] * len(items_to_consider)) |
| 194 | ), |
| 195 | items_to_consider, |
| 196 | ): |
Rohit Waghchaure | f83f6aa | 2018-01-20 15:44:38 +0530 | [diff] [blame] | 197 | |
| 198 | if item_code not in item_warehouse_projected_qty: |
| 199 | item_warehouse_projected_qty.setdefault(item_code, {}) |
| 200 | |
| 201 | if warehouse not in item_warehouse_projected_qty.get(item_code): |
| 202 | item_warehouse_projected_qty[item_code][warehouse] = flt(projected_qty) |
| 203 | |
Saurabh | 3d6aecd | 2016-06-20 17:25:45 +0530 | [diff] [blame] | 204 | warehouse_doc = frappe.get_doc("Warehouse", warehouse) |
Rohit Waghchaure | f83f6aa | 2018-01-20 15:44:38 +0530 | [diff] [blame] | 205 | |
ppd1990 | 9bd8427 | 2017-11-15 05:43:09 +0100 | [diff] [blame] | 206 | while warehouse_doc.parent_warehouse: |
Saurabh | 3d6aecd | 2016-06-20 17:25:45 +0530 | [diff] [blame] | 207 | 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] | 208 | item_warehouse_projected_qty.setdefault(item_code, {})[warehouse_doc.parent_warehouse] = flt( |
| 209 | projected_qty |
| 210 | ) |
Saurabh | 3d6aecd | 2016-06-20 17:25:45 +0530 | [diff] [blame] | 211 | else: |
| 212 | item_warehouse_projected_qty[item_code][warehouse_doc.parent_warehouse] += flt(projected_qty) |
ppd1990 | 9bd8427 | 2017-11-15 05:43:09 +0100 | [diff] [blame] | 213 | warehouse_doc = frappe.get_doc("Warehouse", warehouse_doc.parent_warehouse) |
Rohit Waghchaure | f83f6aa | 2018-01-20 15:44:38 +0530 | [diff] [blame] | 214 | |
Rushabh Mehta | f850987 | 2014-10-08 12:03:19 +0530 | [diff] [blame] | 215 | return item_warehouse_projected_qty |
| 216 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 217 | |
Rushabh Mehta | f850987 | 2014-10-08 12:03:19 +0530 | [diff] [blame] | 218 | def create_material_request(material_requests): |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 219 | """Create indent on reaching reorder level""" |
Rushabh Mehta | f850987 | 2014-10-08 12:03:19 +0530 | [diff] [blame] | 220 | mr_list = [] |
Rushabh Mehta | f850987 | 2014-10-08 12:03:19 +0530 | [diff] [blame] | 221 | exceptions_list = [] |
| 222 | |
Rushabh Mehta | 548afba | 2022-05-02 15:04:26 +0530 | [diff] [blame] | 223 | def _log_exception(mr): |
Rushabh Mehta | f850987 | 2014-10-08 12:03:19 +0530 | [diff] [blame] | 224 | if frappe.local.message_log: |
| 225 | exceptions_list.extend(frappe.local.message_log) |
| 226 | frappe.local.message_log = [] |
| 227 | else: |
Ankush Menat | 510fdf7 | 2024-01-01 13:10:03 +0530 | [diff] [blame] | 228 | exceptions_list.append(frappe.get_traceback(with_context=True)) |
Rushabh Mehta | f850987 | 2014-10-08 12:03:19 +0530 | [diff] [blame] | 229 | |
Rushabh Mehta | 548afba | 2022-05-02 15:04:26 +0530 | [diff] [blame] | 230 | mr.log_error("Unable to create material request") |
rohitwaghchaure | 1e6788b | 2020-02-26 11:25:34 +0530 | [diff] [blame] | 231 | |
Rohit Waghchaure | 764f342 | 2024-01-24 11:45:15 +0530 | [diff] [blame] | 232 | company_wise_mr = frappe._dict({}) |
Rushabh Mehta | f850987 | 2014-10-08 12:03:19 +0530 | [diff] [blame] | 233 | for request_type in material_requests: |
| 234 | for company in material_requests[request_type]: |
| 235 | try: |
| 236 | items = material_requests[request_type][company] |
| 237 | if not items: |
| 238 | continue |
| 239 | |
| 240 | mr = frappe.new_doc("Material Request") |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 241 | mr.update( |
| 242 | { |
| 243 | "company": company, |
| 244 | "transaction_date": nowdate(), |
| 245 | "material_request_type": "Material Transfer" if request_type == "Transfer" else request_type, |
| 246 | } |
| 247 | ) |
Rushabh Mehta | f850987 | 2014-10-08 12:03:19 +0530 | [diff] [blame] | 248 | |
| 249 | for d in items: |
| 250 | d = frappe._dict(d) |
Rohit Waghchaure | 951023f | 2024-01-31 11:32:17 +0530 | [diff] [blame] | 251 | item = d.get("item_details") |
rohitwaghchaure | e8ccc0e | 2017-11-21 16:17:22 +0530 | [diff] [blame] | 252 | uom = item.stock_uom |
| 253 | conversion_factor = 1.0 |
| 254 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 255 | if request_type == "Purchase": |
rohitwaghchaure | e8ccc0e | 2017-11-21 16:17:22 +0530 | [diff] [blame] | 256 | uom = item.purchase_uom or item.stock_uom |
| 257 | if uom != item.stock_uom: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 258 | conversion_factor = ( |
| 259 | frappe.db.get_value( |
| 260 | "UOM Conversion Detail", {"parent": item.name, "uom": uom}, "conversion_factor" |
| 261 | ) |
| 262 | or 1.0 |
| 263 | ) |
rohitwaghchaure | e8ccc0e | 2017-11-21 16:17:22 +0530 | [diff] [blame] | 264 | |
Alan | d4b2471 | 2021-10-06 18:16:33 +0530 | [diff] [blame] | 265 | must_be_whole_number = frappe.db.get_value("UOM", uom, "must_be_whole_number", cache=True) |
| 266 | qty = d.reorder_qty / conversion_factor |
| 267 | if must_be_whole_number: |
| 268 | qty = ceil(qty) |
| 269 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 270 | mr.append( |
| 271 | "items", |
| 272 | { |
| 273 | "doctype": "Material Request Item", |
| 274 | "item_code": d.item_code, |
| 275 | "schedule_date": add_days(nowdate(), cint(item.lead_time_days)), |
| 276 | "qty": qty, |
Rohit Waghchaure | 951023f | 2024-01-31 11:32:17 +0530 | [diff] [blame] | 277 | "conversion_factor": conversion_factor, |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 278 | "uom": uom, |
| 279 | "stock_uom": item.stock_uom, |
| 280 | "warehouse": d.warehouse, |
| 281 | "item_name": item.item_name, |
| 282 | "description": item.description, |
| 283 | "item_group": item.item_group, |
| 284 | "brand": item.brand, |
| 285 | }, |
| 286 | ) |
Rushabh Mehta | f850987 | 2014-10-08 12:03:19 +0530 | [diff] [blame] | 287 | |
rohitwaghchaure | 373b570 | 2017-11-27 11:27:28 +0530 | [diff] [blame] | 288 | schedule_dates = [d.schedule_date for d in mr.items] |
| 289 | mr.schedule_date = max(schedule_dates or [nowdate()]) |
rohitwaghchaure | 1e6788b | 2020-02-26 11:25:34 +0530 | [diff] [blame] | 290 | mr.flags.ignore_mandatory = True |
Rushabh Mehta | f850987 | 2014-10-08 12:03:19 +0530 | [diff] [blame] | 291 | mr.insert() |
| 292 | mr.submit() |
| 293 | mr_list.append(mr) |
| 294 | |
Rohit Waghchaure | 764f342 | 2024-01-24 11:45:15 +0530 | [diff] [blame] | 295 | company_wise_mr.setdefault(company, []).append(mr) |
| 296 | |
Ankush Menat | 694ae81 | 2021-09-01 14:40:56 +0530 | [diff] [blame] | 297 | except Exception: |
Rushabh Mehta | 548afba | 2022-05-02 15:04:26 +0530 | [diff] [blame] | 298 | _log_exception(mr) |
Rushabh Mehta | f850987 | 2014-10-08 12:03:19 +0530 | [diff] [blame] | 299 | |
Rohit Waghchaure | 764f342 | 2024-01-24 11:45:15 +0530 | [diff] [blame] | 300 | if company_wise_mr: |
Rushabh Mehta | f850987 | 2014-10-08 12:03:19 +0530 | [diff] [blame] | 301 | if getattr(frappe.local, "reorder_email_notify", None) is None: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 302 | frappe.local.reorder_email_notify = cint( |
Ankush Menat | bb18ae8 | 2024-01-09 21:56:47 +0530 | [diff] [blame] | 303 | frappe.db.get_single_value("Stock Settings", "reorder_email_notify") |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 304 | ) |
Rushabh Mehta | f850987 | 2014-10-08 12:03:19 +0530 | [diff] [blame] | 305 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 306 | if frappe.local.reorder_email_notify: |
Rohit Waghchaure | 764f342 | 2024-01-24 11:45:15 +0530 | [diff] [blame] | 307 | send_email_notification(company_wise_mr) |
Rushabh Mehta | f850987 | 2014-10-08 12:03:19 +0530 | [diff] [blame] | 308 | |
| 309 | if exceptions_list: |
| 310 | notify_errors(exceptions_list) |
| 311 | |
| 312 | return mr_list |
| 313 | |
Rushabh Mehta | f850987 | 2014-10-08 12:03:19 +0530 | [diff] [blame] | 314 | |
Rohit Waghchaure | 764f342 | 2024-01-24 11:45:15 +0530 | [diff] [blame] | 315 | def send_email_notification(company_wise_mr): |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 316 | """Notify user about auto creation of indent""" |
| 317 | |
Rohit Waghchaure | 764f342 | 2024-01-24 11:45:15 +0530 | [diff] [blame] | 318 | for company, mr_list in company_wise_mr.items(): |
| 319 | email_list = get_email_list(company) |
| 320 | |
| 321 | if not email_list: |
| 322 | continue |
| 323 | |
| 324 | msg = frappe.render_template("templates/emails/reorder_item.html", {"mr_list": mr_list}) |
| 325 | |
| 326 | frappe.sendmail( |
| 327 | recipients=email_list, subject=_("Auto Material Requests Generated"), message=msg |
| 328 | ) |
| 329 | |
| 330 | |
| 331 | def get_email_list(company): |
| 332 | users = get_comapny_wise_users(company) |
| 333 | user_table = frappe.qb.DocType("User") |
| 334 | role_table = frappe.qb.DocType("Has Role") |
| 335 | |
| 336 | query = ( |
| 337 | frappe.qb.from_(user_table) |
| 338 | .inner_join(role_table) |
| 339 | .on(user_table.name == role_table.parent) |
| 340 | .select(user_table.email) |
| 341 | .where( |
| 342 | (role_table.role.isin(["Purchase Manager", "Stock Manager"])) |
| 343 | & (user_table.name.notin(["Administrator", "All", "Guest"])) |
| 344 | & (user_table.enabled == 1) |
| 345 | & (user_table.docstatus < 2) |
| 346 | ) |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 347 | ) |
Rushabh Mehta | f850987 | 2014-10-08 12:03:19 +0530 | [diff] [blame] | 348 | |
Rohit Waghchaure | 764f342 | 2024-01-24 11:45:15 +0530 | [diff] [blame] | 349 | if users: |
| 350 | query = query.where(user_table.name.isin(users)) |
Anand Doshi | 5cf7a0b | 2015-11-26 15:11:18 +0530 | [diff] [blame] | 351 | |
Rohit Waghchaure | 764f342 | 2024-01-24 11:45:15 +0530 | [diff] [blame] | 352 | emails = query.run(as_dict=True) |
| 353 | |
| 354 | return list(set([email.email for email in emails])) |
| 355 | |
| 356 | |
| 357 | def get_comapny_wise_users(company): |
| 358 | users = frappe.get_all( |
| 359 | "User Permission", |
| 360 | filters={"allow": "Company", "for_value": company, "apply_to_all_doctypes": 1}, |
| 361 | fields=["user"], |
| 362 | ) |
| 363 | |
| 364 | return [user.user for user in users] |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 365 | |
Rushabh Mehta | f850987 | 2014-10-08 12:03:19 +0530 | [diff] [blame] | 366 | |
| 367 | def notify_errors(exceptions_list): |
Marica | 9ea1ad4 | 2020-04-21 12:52:29 +0530 | [diff] [blame] | 368 | subject = _("[Important] [ERPNext] Auto Reorder Errors") |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 369 | content = ( |
| 370 | _("Dear System Manager,") |
| 371 | + "<br>" |
| 372 | + _( |
barredterra | 806696a | 2024-01-24 02:59:52 +0100 | [diff] [blame] | 373 | "An error occurred 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] | 374 | ) |
| 375 | + "<br>" |
| 376 | ) |
Rushabh Mehta | f850987 | 2014-10-08 12:03:19 +0530 | [diff] [blame] | 377 | |
Marica | 9ea1ad4 | 2020-04-21 12:52:29 +0530 | [diff] [blame] | 378 | for exception in exceptions_list: |
Ankush Menat | ecb39d8 | 2022-05-23 20:06:24 +0530 | [diff] [blame] | 379 | try: |
| 380 | exception = json.loads(exception) |
| 381 | error_message = """<div class='small text-muted'>{0}</div><br>""".format( |
| 382 | _(exception.get("message")) |
| 383 | ) |
| 384 | content += error_message |
| 385 | except Exception: |
| 386 | pass |
Rushabh Mehta | f850987 | 2014-10-08 12:03:19 +0530 | [diff] [blame] | 387 | |
Marica | 9ea1ad4 | 2020-04-21 12:52:29 +0530 | [diff] [blame] | 388 | content += _("Regards,") + "<br>" + _("Administrator") |
Rushabh Mehta | f850987 | 2014-10-08 12:03:19 +0530 | [diff] [blame] | 389 | |
| 390 | from frappe.email import sendmail_to_system_managers |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 391 | |
Rushabh Mehta | f850987 | 2014-10-08 12:03:19 +0530 | [diff] [blame] | 392 | sendmail_to_system_managers(subject, content) |