Rushabh Mehta | ad45e31 | 2013-11-20 12:59:58 +0530 | [diff] [blame] | 1 | # Copyright (c) 2013, Web Notes Technologies Pvt. Ltd. and Contributors |
Rushabh Mehta | e67d1fb | 2013-08-05 14:59:54 +0530 | [diff] [blame] | 2 | # License: GNU General Public License v3. See license.txt |
Nabin Hait | 9d0f636 | 2013-01-07 18:51:11 +0530 | [diff] [blame] | 3 | |
Rushabh Mehta | 793ba6b | 2014-02-14 15:47:51 +0530 | [diff] [blame] | 4 | import frappe |
| 5 | from frappe import msgprint, _ |
Nabin Hait | 9d0f636 | 2013-01-07 18:51:11 +0530 | [diff] [blame] | 6 | import json |
Rushabh Mehta | 793ba6b | 2014-02-14 15:47:51 +0530 | [diff] [blame] | 7 | from frappe.utils import flt, cstr, nowdate, add_days, cint |
| 8 | from frappe.defaults import get_global_default |
| 9 | from frappe.utils.email_lib import sendmail |
Nabin Hait | 9d0f636 | 2013-01-07 18:51:11 +0530 | [diff] [blame] | 10 | |
Rushabh Mehta | 793ba6b | 2014-02-14 15:47:51 +0530 | [diff] [blame] | 11 | class InvalidWarehouseCompany(frappe.ValidationError): pass |
Nabin Hait | 0dd7be1 | 2013-08-02 11:45:43 +0530 | [diff] [blame] | 12 | |
Nabin Hait | 625da79 | 2013-09-25 10:32:51 +0530 | [diff] [blame] | 13 | def get_stock_balance_on(warehouse, posting_date=None): |
Nabin Hait | 0dd7be1 | 2013-08-02 11:45:43 +0530 | [diff] [blame] | 14 | if not posting_date: posting_date = nowdate() |
| 15 | |
Anand Doshi | e9baaa6 | 2014-02-26 12:35:33 +0530 | [diff] [blame] | 16 | stock_ledger_entries = frappe.db.sql(""" |
Nabin Hait | 0dd7be1 | 2013-08-02 11:45:43 +0530 | [diff] [blame] | 17 | SELECT |
Nabin Hait | 625da79 | 2013-09-25 10:32:51 +0530 | [diff] [blame] | 18 | item_code, stock_value |
Nabin Hait | 0dd7be1 | 2013-08-02 11:45:43 +0530 | [diff] [blame] | 19 | FROM |
| 20 | `tabStock Ledger Entry` |
| 21 | WHERE |
Nabin Hait | 625da79 | 2013-09-25 10:32:51 +0530 | [diff] [blame] | 22 | warehouse=%s AND posting_date <= %s |
Nabin Hait | 0dd7be1 | 2013-08-02 11:45:43 +0530 | [diff] [blame] | 23 | ORDER BY timestamp(posting_date, posting_time) DESC, name DESC |
Nabin Hait | 625da79 | 2013-09-25 10:32:51 +0530 | [diff] [blame] | 24 | """, (warehouse, posting_date), as_dict=1) |
Nabin Hait | 0dd7be1 | 2013-08-02 11:45:43 +0530 | [diff] [blame] | 25 | |
| 26 | sle_map = {} |
| 27 | for sle in stock_ledger_entries: |
Nabin Hait | 625da79 | 2013-09-25 10:32:51 +0530 | [diff] [blame] | 28 | sle_map.setdefault(sle.item_code, flt(sle.stock_value)) |
Nabin Hait | 0dd7be1 | 2013-08-02 11:45:43 +0530 | [diff] [blame] | 29 | |
Nabin Hait | 625da79 | 2013-09-25 10:32:51 +0530 | [diff] [blame] | 30 | return sum(sle_map.values()) |
Nabin Hait | 0dd7be1 | 2013-08-02 11:45:43 +0530 | [diff] [blame] | 31 | |
Nabin Hait | 47dc318 | 2013-08-06 15:58:16 +0530 | [diff] [blame] | 32 | def get_latest_stock_balance(): |
| 33 | bin_map = {} |
Anand Doshi | e9baaa6 | 2014-02-26 12:35:33 +0530 | [diff] [blame] | 34 | for d in frappe.db.sql("""SELECT item_code, warehouse, stock_value as stock_value |
Nabin Hait | 47dc318 | 2013-08-06 15:58:16 +0530 | [diff] [blame] | 35 | FROM tabBin""", as_dict=1): |
Nabin Hait | 469ee71 | 2013-08-07 12:33:37 +0530 | [diff] [blame] | 36 | bin_map.setdefault(d.warehouse, {}).setdefault(d.item_code, flt(d.stock_value)) |
Nabin Hait | 47dc318 | 2013-08-06 15:58:16 +0530 | [diff] [blame] | 37 | |
| 38 | return bin_map |
Nabin Hait | 74c281c | 2013-08-19 16:17:18 +0530 | [diff] [blame] | 39 | |
| 40 | def get_bin(item_code, warehouse): |
Anand Doshi | e9baaa6 | 2014-02-26 12:35:33 +0530 | [diff] [blame] | 41 | bin = frappe.db.get_value("Bin", {"item_code": item_code, "warehouse": warehouse}) |
Nabin Hait | 74c281c | 2013-08-19 16:17:18 +0530 | [diff] [blame] | 42 | if not bin: |
Rushabh Mehta | b385ecf | 2014-03-28 16:44:37 +0530 | [diff] [blame] | 43 | bin_wrapper = frappe.get_doc([{ |
Nabin Hait | 74c281c | 2013-08-19 16:17:18 +0530 | [diff] [blame] | 44 | "doctype": "Bin", |
| 45 | "item_code": item_code, |
| 46 | "warehouse": warehouse, |
| 47 | }]) |
| 48 | bin_wrapper.ignore_permissions = 1 |
| 49 | bin_wrapper.insert() |
| 50 | bin_obj = bin_wrapper.make_controller() |
| 51 | else: |
Rushabh Mehta | 0a0f249 | 2014-03-31 17:27:06 +0530 | [diff] [blame^] | 52 | bin_obj = frappe.get_doc('Bin', bin) |
Nabin Hait | 74c281c | 2013-08-19 16:17:18 +0530 | [diff] [blame] | 53 | return bin_obj |
| 54 | |
| 55 | def update_bin(args): |
Anand Doshi | e9baaa6 | 2014-02-26 12:35:33 +0530 | [diff] [blame] | 56 | is_stock_item = frappe.db.get_value('Item', args.get("item_code"), 'is_stock_item') |
Nabin Hait | 74c281c | 2013-08-19 16:17:18 +0530 | [diff] [blame] | 57 | if is_stock_item == 'Yes': |
| 58 | bin = get_bin(args.get("item_code"), args.get("warehouse")) |
| 59 | bin.update_stock(args) |
| 60 | return bin |
| 61 | else: |
| 62 | msgprint("[Stock Update] Ignored %s since it is not a stock item" |
| 63 | % args.get("item_code")) |
Nabin Hait | 0dd7be1 | 2013-08-02 11:45:43 +0530 | [diff] [blame] | 64 | |
Nabin Hait | 9d0f636 | 2013-01-07 18:51:11 +0530 | [diff] [blame] | 65 | def get_incoming_rate(args): |
| 66 | """Get Incoming Rate based on valuation method""" |
Rushabh Mehta | 1f84799 | 2013-12-12 19:12:19 +0530 | [diff] [blame] | 67 | from erpnext.stock.stock_ledger import get_previous_sle |
Nabin Hait | 9d0f636 | 2013-01-07 18:51:11 +0530 | [diff] [blame] | 68 | |
| 69 | in_rate = 0 |
| 70 | if args.get("serial_no"): |
| 71 | in_rate = get_avg_purchase_rate(args.get("serial_no")) |
| 72 | elif args.get("bom_no"): |
Anand Doshi | e9baaa6 | 2014-02-26 12:35:33 +0530 | [diff] [blame] | 73 | result = frappe.db.sql("""select ifnull(total_cost, 0) / ifnull(quantity, 1) |
Nabin Hait | 9d0f636 | 2013-01-07 18:51:11 +0530 | [diff] [blame] | 74 | from `tabBOM` where name = %s and docstatus=1 and is_active=1""", args.get("bom_no")) |
| 75 | in_rate = result and flt(result[0][0]) or 0 |
| 76 | else: |
| 77 | valuation_method = get_valuation_method(args.get("item_code")) |
| 78 | previous_sle = get_previous_sle(args) |
| 79 | if valuation_method == 'FIFO': |
Nabin Hait | 831207f | 2013-01-16 14:15:48 +0530 | [diff] [blame] | 80 | if not previous_sle: |
| 81 | return 0.0 |
Rushabh Mehta | 4c17f94 | 2013-08-12 14:18:09 +0530 | [diff] [blame] | 82 | previous_stock_queue = json.loads(previous_sle.get('stock_queue', '[]') or '[]') |
Nabin Hait | 831207f | 2013-01-16 14:15:48 +0530 | [diff] [blame] | 83 | in_rate = previous_stock_queue and \ |
Nabin Hait | 6b1f21d | 2013-01-16 17:17:17 +0530 | [diff] [blame] | 84 | get_fifo_rate(previous_stock_queue, args.get("qty") or 0) or 0 |
Nabin Hait | 9d0f636 | 2013-01-07 18:51:11 +0530 | [diff] [blame] | 85 | elif valuation_method == 'Moving Average': |
| 86 | in_rate = previous_sle.get('valuation_rate') or 0 |
| 87 | return in_rate |
| 88 | |
| 89 | def get_avg_purchase_rate(serial_nos): |
| 90 | """get average value of serial numbers""" |
| 91 | |
| 92 | serial_nos = get_valid_serial_nos(serial_nos) |
Anand Doshi | e9baaa6 | 2014-02-26 12:35:33 +0530 | [diff] [blame] | 93 | return flt(frappe.db.sql("""select avg(ifnull(purchase_rate, 0)) from `tabSerial No` |
Nabin Hait | 9d0f636 | 2013-01-07 18:51:11 +0530 | [diff] [blame] | 94 | where name in (%s)""" % ", ".join(["%s"] * len(serial_nos)), |
| 95 | tuple(serial_nos))[0][0]) |
| 96 | |
| 97 | def get_valuation_method(item_code): |
| 98 | """get valuation method from item or default""" |
Anand Doshi | e9baaa6 | 2014-02-26 12:35:33 +0530 | [diff] [blame] | 99 | val_method = frappe.db.get_value('Item', item_code, 'valuation_method') |
Nabin Hait | 9d0f636 | 2013-01-07 18:51:11 +0530 | [diff] [blame] | 100 | if not val_method: |
Rushabh Mehta | 5117d9c | 2013-02-19 15:27:31 +0530 | [diff] [blame] | 101 | val_method = get_global_default('valuation_method') or "FIFO" |
Nabin Hait | 9d0f636 | 2013-01-07 18:51:11 +0530 | [diff] [blame] | 102 | return val_method |
| 103 | |
Nabin Hait | 831207f | 2013-01-16 14:15:48 +0530 | [diff] [blame] | 104 | def get_fifo_rate(previous_stock_queue, qty): |
| 105 | """get FIFO (average) Rate from Queue""" |
| 106 | if qty >= 0: |
| 107 | total = sum(f[0] for f in previous_stock_queue) |
| 108 | return total and sum(f[0] * f[1] for f in previous_stock_queue) / flt(total) or 0.0 |
| 109 | else: |
| 110 | outgoing_cost = 0 |
| 111 | qty_to_pop = abs(qty) |
Nabin Hait | 64d7c4b | 2013-01-17 12:22:59 +0530 | [diff] [blame] | 112 | while qty_to_pop and previous_stock_queue: |
Nabin Hait | 831207f | 2013-01-16 14:15:48 +0530 | [diff] [blame] | 113 | batch = previous_stock_queue[0] |
| 114 | if 0 < batch[0] <= qty_to_pop: |
| 115 | # if batch qty > 0 |
| 116 | # not enough or exactly same qty in current batch, clear batch |
| 117 | outgoing_cost += flt(batch[0]) * flt(batch[1]) |
| 118 | qty_to_pop -= batch[0] |
| 119 | previous_stock_queue.pop(0) |
| 120 | else: |
| 121 | # all from current batch |
| 122 | outgoing_cost += flt(qty_to_pop) * flt(batch[1]) |
| 123 | batch[0] -= qty_to_pop |
| 124 | qty_to_pop = 0 |
Nabin Hait | 64d7c4b | 2013-01-17 12:22:59 +0530 | [diff] [blame] | 125 | # if queue gets blank and qty_to_pop remaining, get average rate of full queue |
| 126 | return outgoing_cost / abs(qty) - qty_to_pop |
Nabin Hait | 9d0f636 | 2013-01-07 18:51:11 +0530 | [diff] [blame] | 127 | |
| 128 | def get_valid_serial_nos(sr_nos, qty=0, item_code=''): |
| 129 | """split serial nos, validate and return list of valid serial nos""" |
| 130 | # TODO: remove duplicates in client side |
| 131 | serial_nos = cstr(sr_nos).strip().replace(',', '\n').split('\n') |
| 132 | |
| 133 | valid_serial_nos = [] |
| 134 | for val in serial_nos: |
| 135 | if val: |
| 136 | val = val.strip() |
| 137 | if val in valid_serial_nos: |
| 138 | msgprint("You have entered duplicate serial no: '%s'" % val, raise_exception=1) |
| 139 | else: |
| 140 | valid_serial_nos.append(val) |
| 141 | |
| 142 | if qty and len(valid_serial_nos) != abs(qty): |
| 143 | msgprint("Please enter serial nos for " |
| 144 | + cstr(abs(qty)) + " quantity against item code: " + item_code, |
| 145 | raise_exception=1) |
| 146 | |
Rushabh Mehta | 0dbe898 | 2013-02-04 13:56:50 +0530 | [diff] [blame] | 147 | return valid_serial_nos |
Nabin Hait | a72c512 | 2013-03-06 18:50:53 +0530 | [diff] [blame] | 148 | |
Anand Doshi | 373680b | 2013-10-10 16:04:40 +0530 | [diff] [blame] | 149 | def validate_warehouse_company(warehouse, company): |
Anand Doshi | e9baaa6 | 2014-02-26 12:35:33 +0530 | [diff] [blame] | 150 | warehouse_company = frappe.db.get_value("Warehouse", warehouse, "company") |
Anand Doshi | 373680b | 2013-10-10 16:04:40 +0530 | [diff] [blame] | 151 | if warehouse_company and warehouse_company != company: |
Rushabh Mehta | 793ba6b | 2014-02-14 15:47:51 +0530 | [diff] [blame] | 152 | frappe.msgprint(_("Warehouse does not belong to company.") + " (" + \ |
Anand Doshi | 373680b | 2013-10-10 16:04:40 +0530 | [diff] [blame] | 153 | warehouse + ", " + company +")", raise_exception=InvalidWarehouseCompany) |
Rushabh Mehta | a65253b | 2013-08-27 10:32:56 +0530 | [diff] [blame] | 154 | |
Nabin Hait | 94c90bd | 2013-08-30 22:48:19 +0530 | [diff] [blame] | 155 | def get_sales_bom_buying_amount(item_code, warehouse, voucher_type, voucher_no, voucher_detail_no, |
| 156 | stock_ledger_entries, item_sales_bom): |
| 157 | # sales bom item |
| 158 | buying_amount = 0.0 |
| 159 | for bom_item in item_sales_bom[item_code]: |
| 160 | if bom_item.get("parent_detail_docname")==voucher_detail_no: |
| 161 | buying_amount += get_buying_amount(voucher_type, voucher_no, voucher_detail_no, |
| 162 | stock_ledger_entries.get((bom_item.item_code, warehouse), [])) |
| 163 | |
| 164 | return buying_amount |
Nabin Hait | a72c512 | 2013-03-06 18:50:53 +0530 | [diff] [blame] | 165 | |
Nabin Hait | 94c90bd | 2013-08-30 22:48:19 +0530 | [diff] [blame] | 166 | def get_buying_amount(voucher_type, voucher_no, item_row, stock_ledger_entries): |
Anand Doshi | 5dd6b1d | 2013-08-07 19:27:30 +0530 | [diff] [blame] | 167 | # IMP NOTE |
| 168 | # stock_ledger_entries should already be filtered by item_code and warehouse and |
| 169 | # sorted by posting_date desc, posting_time desc |
| 170 | for i, sle in enumerate(stock_ledger_entries): |
Nabin Hait | 0cfbc5f | 2013-03-12 11:34:56 +0530 | [diff] [blame] | 171 | if sle.voucher_type == voucher_type and sle.voucher_no == voucher_no and \ |
Anand Doshi | 8c45420 | 2013-03-28 16:40:30 +0530 | [diff] [blame] | 172 | sle.voucher_detail_no == item_row: |
Anand Doshi | 5dd6b1d | 2013-08-07 19:27:30 +0530 | [diff] [blame] | 173 | previous_stock_value = len(stock_ledger_entries) > i+1 and \ |
| 174 | flt(stock_ledger_entries[i+1].stock_value) or 0.0 |
Nabin Hait | c3afb25 | 2013-03-19 12:01:24 +0530 | [diff] [blame] | 175 | buying_amount = previous_stock_value - flt(sle.stock_value) |
Anand Doshi | 6d8d3b4 | 2013-03-21 18:45:02 +0530 | [diff] [blame] | 176 | |
Nabin Hait | c3afb25 | 2013-03-19 12:01:24 +0530 | [diff] [blame] | 177 | return buying_amount |
Nabin Hait | 62d0629 | 2013-05-22 16:19:10 +0530 | [diff] [blame] | 178 | return 0.0 |
| 179 | |
| 180 | |
| 181 | def reorder_item(): |
| 182 | """ Reorder item if stock reaches reorder level""" |
Rushabh Mehta | 793ba6b | 2014-02-14 15:47:51 +0530 | [diff] [blame] | 183 | if getattr(frappe.local, "auto_indent", None) is None: |
Anand Doshi | e9baaa6 | 2014-02-26 12:35:33 +0530 | [diff] [blame] | 184 | frappe.local.auto_indent = cint(frappe.db.get_value('Stock Settings', None, 'auto_indent')) |
Nabin Hait | bf5d44c | 2013-08-12 16:30:48 +0530 | [diff] [blame] | 185 | |
Rushabh Mehta | 793ba6b | 2014-02-14 15:47:51 +0530 | [diff] [blame] | 186 | if frappe.local.auto_indent: |
Nabin Hait | 62d0629 | 2013-05-22 16:19:10 +0530 | [diff] [blame] | 187 | material_requests = {} |
Anand Doshi | e9baaa6 | 2014-02-26 12:35:33 +0530 | [diff] [blame] | 188 | bin_list = frappe.db.sql("""select item_code, warehouse, projected_qty |
Anand Doshi | ad6180e | 2013-06-17 11:57:04 +0530 | [diff] [blame] | 189 | from tabBin where ifnull(item_code, '') != '' and ifnull(warehouse, '') != '' |
| 190 | and exists (select name from `tabItem` |
| 191 | where `tabItem`.name = `tabBin`.item_code and |
| 192 | is_stock_item='Yes' and (is_purchase_item='Yes' or is_sub_contracted_item='Yes') and |
Anand Doshi | 4d47b00 | 2013-08-23 16:54:31 +0530 | [diff] [blame] | 193 | (ifnull(end_of_life, '')='' or end_of_life > now()))""", as_dict=True) |
Nabin Hait | 62d0629 | 2013-05-22 16:19:10 +0530 | [diff] [blame] | 194 | for bin in bin_list: |
| 195 | #check if re-order is required |
Anand Doshi | e9baaa6 | 2014-02-26 12:35:33 +0530 | [diff] [blame] | 196 | item_reorder = frappe.db.get("Item Reorder", |
Nabin Hait | 62d0629 | 2013-05-22 16:19:10 +0530 | [diff] [blame] | 197 | {"parent": bin.item_code, "warehouse": bin.warehouse}) |
| 198 | if item_reorder: |
| 199 | reorder_level = item_reorder.warehouse_reorder_level |
| 200 | reorder_qty = item_reorder.warehouse_reorder_qty |
| 201 | material_request_type = item_reorder.material_request_type or "Purchase" |
| 202 | else: |
Anand Doshi | e9baaa6 | 2014-02-26 12:35:33 +0530 | [diff] [blame] | 203 | reorder_level, reorder_qty = frappe.db.get_value("Item", bin.item_code, |
Nabin Hait | 62d0629 | 2013-05-22 16:19:10 +0530 | [diff] [blame] | 204 | ["re_order_level", "re_order_qty"]) |
| 205 | material_request_type = "Purchase" |
| 206 | |
Anand Doshi | ad6180e | 2013-06-17 11:57:04 +0530 | [diff] [blame] | 207 | if flt(reorder_level) and flt(bin.projected_qty) < flt(reorder_level): |
Nabin Hait | 62d0629 | 2013-05-22 16:19:10 +0530 | [diff] [blame] | 208 | if flt(reorder_level) - flt(bin.projected_qty) > flt(reorder_qty): |
| 209 | reorder_qty = flt(reorder_level) - flt(bin.projected_qty) |
| 210 | |
Anand Doshi | e9baaa6 | 2014-02-26 12:35:33 +0530 | [diff] [blame] | 211 | company = frappe.db.get_value("Warehouse", bin.warehouse, "company") or \ |
Rushabh Mehta | 793ba6b | 2014-02-14 15:47:51 +0530 | [diff] [blame] | 212 | frappe.defaults.get_defaults()["company"] or \ |
Anand Doshi | e9baaa6 | 2014-02-26 12:35:33 +0530 | [diff] [blame] | 213 | frappe.db.sql("""select name from tabCompany limit 1""")[0][0] |
Nabin Hait | 62d0629 | 2013-05-22 16:19:10 +0530 | [diff] [blame] | 214 | |
Rushabh Mehta | 793ba6b | 2014-02-14 15:47:51 +0530 | [diff] [blame] | 215 | material_requests.setdefault(material_request_type, frappe._dict()).setdefault( |
| 216 | company, []).append(frappe._dict({ |
Nabin Hait | 62d0629 | 2013-05-22 16:19:10 +0530 | [diff] [blame] | 217 | "item_code": bin.item_code, |
| 218 | "warehouse": bin.warehouse, |
| 219 | "reorder_qty": reorder_qty |
| 220 | }) |
| 221 | ) |
Anand Doshi | 4a67d36 | 2013-11-13 14:53:09 +0530 | [diff] [blame] | 222 | |
Nabin Hait | 62d0629 | 2013-05-22 16:19:10 +0530 | [diff] [blame] | 223 | create_material_request(material_requests) |
| 224 | |
| 225 | def create_material_request(material_requests): |
| 226 | """ Create indent on reaching reorder level """ |
| 227 | mr_list = [] |
Rushabh Mehta | 793ba6b | 2014-02-14 15:47:51 +0530 | [diff] [blame] | 228 | defaults = frappe.defaults.get_defaults() |
Anand Doshi | ad6180e | 2013-06-17 11:57:04 +0530 | [diff] [blame] | 229 | exceptions_list = [] |
Rushabh Mehta | 1f84799 | 2013-12-12 19:12:19 +0530 | [diff] [blame] | 230 | from erpnext.accounts.utils import get_fiscal_year |
Akhilesh Darjee | b0a1b3a | 2013-11-26 14:45:27 +0530 | [diff] [blame] | 231 | current_fiscal_year = get_fiscal_year(nowdate())[0] or defaults.fiscal_year |
Nabin Hait | 62d0629 | 2013-05-22 16:19:10 +0530 | [diff] [blame] | 232 | for request_type in material_requests: |
| 233 | for company in material_requests[request_type]: |
Anand Doshi | ad6180e | 2013-06-17 11:57:04 +0530 | [diff] [blame] | 234 | try: |
| 235 | items = material_requests[request_type][company] |
| 236 | if not items: |
| 237 | continue |
Anand Doshi | 6f6e91c | 2013-07-22 11:28:34 +0530 | [diff] [blame] | 238 | |
Nabin Hait | 62d0629 | 2013-05-22 16:19:10 +0530 | [diff] [blame] | 239 | mr = [{ |
| 240 | "doctype": "Material Request", |
| 241 | "company": company, |
Akhilesh Darjee | b0a1b3a | 2013-11-26 14:45:27 +0530 | [diff] [blame] | 242 | "fiscal_year": current_fiscal_year, |
Nabin Hait | 62d0629 | 2013-05-22 16:19:10 +0530 | [diff] [blame] | 243 | "transaction_date": nowdate(), |
Anand Doshi | 4a67d36 | 2013-11-13 14:53:09 +0530 | [diff] [blame] | 244 | "material_request_type": request_type |
Nabin Hait | 62d0629 | 2013-05-22 16:19:10 +0530 | [diff] [blame] | 245 | }] |
| 246 | |
Anand Doshi | ad6180e | 2013-06-17 11:57:04 +0530 | [diff] [blame] | 247 | for d in items: |
Rushabh Mehta | b385ecf | 2014-03-28 16:44:37 +0530 | [diff] [blame] | 248 | item = frappe.get_doc("Item", d.item_code) |
Anand Doshi | ad6180e | 2013-06-17 11:57:04 +0530 | [diff] [blame] | 249 | mr.append({ |
| 250 | "doctype": "Material Request Item", |
| 251 | "parenttype": "Material Request", |
| 252 | "parentfield": "indent_details", |
| 253 | "item_code": d.item_code, |
| 254 | "schedule_date": add_days(nowdate(),cint(item.lead_time_days)), |
| 255 | "uom": item.stock_uom, |
| 256 | "warehouse": d.warehouse, |
| 257 | "item_name": item.item_name, |
| 258 | "description": item.description, |
| 259 | "item_group": item.item_group, |
| 260 | "qty": d.reorder_qty, |
| 261 | "brand": item.brand, |
| 262 | }) |
Nabin Hait | 62d0629 | 2013-05-22 16:19:10 +0530 | [diff] [blame] | 263 | |
Rushabh Mehta | b385ecf | 2014-03-28 16:44:37 +0530 | [diff] [blame] | 264 | mr_bean = frappe.get_doc(mr) |
Anand Doshi | ad6180e | 2013-06-17 11:57:04 +0530 | [diff] [blame] | 265 | mr_bean.insert() |
| 266 | mr_bean.submit() |
| 267 | mr_list.append(mr_bean) |
Anand Doshi | 6f6e91c | 2013-07-22 11:28:34 +0530 | [diff] [blame] | 268 | |
Anand Doshi | ad6180e | 2013-06-17 11:57:04 +0530 | [diff] [blame] | 269 | except: |
Rushabh Mehta | 793ba6b | 2014-02-14 15:47:51 +0530 | [diff] [blame] | 270 | if frappe.local.message_log: |
| 271 | exceptions_list.append([] + frappe.local.message_log) |
| 272 | frappe.local.message_log = [] |
Anand Doshi | ad6180e | 2013-06-17 11:57:04 +0530 | [diff] [blame] | 273 | else: |
Rushabh Mehta | 793ba6b | 2014-02-14 15:47:51 +0530 | [diff] [blame] | 274 | exceptions_list.append(frappe.get_traceback()) |
Nabin Hait | 62d0629 | 2013-05-22 16:19:10 +0530 | [diff] [blame] | 275 | |
| 276 | if mr_list: |
Rushabh Mehta | 793ba6b | 2014-02-14 15:47:51 +0530 | [diff] [blame] | 277 | if getattr(frappe.local, "reorder_email_notify", None) is None: |
Anand Doshi | e9baaa6 | 2014-02-26 12:35:33 +0530 | [diff] [blame] | 278 | frappe.local.reorder_email_notify = cint(frappe.db.get_value('Stock Settings', None, |
Nabin Hait | bf5d44c | 2013-08-12 16:30:48 +0530 | [diff] [blame] | 279 | 'reorder_email_notify')) |
Nabin Hait | 62d0629 | 2013-05-22 16:19:10 +0530 | [diff] [blame] | 280 | |
Rushabh Mehta | 793ba6b | 2014-02-14 15:47:51 +0530 | [diff] [blame] | 281 | if(frappe.local.reorder_email_notify): |
Nabin Hait | 62d0629 | 2013-05-22 16:19:10 +0530 | [diff] [blame] | 282 | send_email_notification(mr_list) |
Anand Doshi | ad6180e | 2013-06-17 11:57:04 +0530 | [diff] [blame] | 283 | |
| 284 | if exceptions_list: |
| 285 | notify_errors(exceptions_list) |
Nabin Hait | 62d0629 | 2013-05-22 16:19:10 +0530 | [diff] [blame] | 286 | |
| 287 | def send_email_notification(mr_list): |
| 288 | """ Notify user about auto creation of indent""" |
| 289 | |
Anand Doshi | e9baaa6 | 2014-02-26 12:35:33 +0530 | [diff] [blame] | 290 | email_list = frappe.db.sql_list("""select distinct r.parent |
Rushabh Mehta | 7c93200 | 2014-03-11 16:15:05 +0530 | [diff] [blame] | 291 | from tabUserRole r, tabUser p |
Nabin Hait | 62d0629 | 2013-05-22 16:19:10 +0530 | [diff] [blame] | 292 | where p.name = r.parent and p.enabled = 1 and p.docstatus < 2 |
| 293 | and r.role in ('Purchase Manager','Material Manager') |
| 294 | and p.name not in ('Administrator', 'All', 'Guest')""") |
| 295 | |
| 296 | msg="""<h3>Following Material Requests has been raised automatically \ |
| 297 | based on item reorder level:</h3>""" |
| 298 | for mr in mr_list: |
Anand Doshi | f78d1ae | 2014-03-28 13:55:00 +0530 | [diff] [blame] | 299 | msg += "<p><b><u>" + mr.name + """</u></b></p><table class='table table-bordered'><tr> |
Nabin Hait | 62d0629 | 2013-05-22 16:19:10 +0530 | [diff] [blame] | 300 | <th>Item Code</th><th>Warehouse</th><th>Qty</th><th>UOM</th></tr>""" |
Rushabh Mehta | d2b34dc | 2014-03-27 16:12:56 +0530 | [diff] [blame] | 301 | for item in mr.get("indent_details"): |
Nabin Hait | 62d0629 | 2013-05-22 16:19:10 +0530 | [diff] [blame] | 302 | msg += "<tr><td>" + item.item_code + "</td><td>" + item.warehouse + "</td><td>" + \ |
| 303 | cstr(item.qty) + "</td><td>" + cstr(item.uom) + "</td></tr>" |
| 304 | msg += "</table>" |
Anand Doshi | ad6180e | 2013-06-17 11:57:04 +0530 | [diff] [blame] | 305 | sendmail(email_list, subject='Auto Material Request Generation Notification', msg = msg) |
| 306 | |
| 307 | def notify_errors(exceptions_list): |
| 308 | subject = "[Important] [ERPNext] Error(s) while creating Material Requests based on Re-order Levels" |
| 309 | msg = """Dear System Manager, |
| 310 | |
| 311 | An error occured for certain Items while creating Material Requests based on Re-order level. |
| 312 | |
| 313 | Please rectify these issues: |
| 314 | --- |
| 315 | |
| 316 | %s |
| 317 | |
| 318 | --- |
| 319 | Regards, |
| 320 | Administrator""" % ("\n\n".join(["\n".join(msg) for msg in exceptions_list]),) |
| 321 | |
Rushabh Mehta | 7c93200 | 2014-03-11 16:15:05 +0530 | [diff] [blame] | 322 | from frappe.utils.user import get_system_managers |
Anand Doshi | ad6180e | 2013-06-17 11:57:04 +0530 | [diff] [blame] | 323 | sendmail(get_system_managers(), subject=subject, msg=msg) |