blob: 0a4be40db2179338f99ad43698f0739ccd0d42f1 [file] [log] [blame]
Rushabh Mehtaad45e312013-11-20 12:59:58 +05301# Copyright (c) 2013, Web Notes Technologies Pvt. Ltd. and Contributors
Rushabh Mehtae67d1fb2013-08-05 14:59:54 +05302# License: GNU General Public License v3. See license.txt
Nabin Hait9d0f6362013-01-07 18:51:11 +05303
Rushabh Mehta793ba6b2014-02-14 15:47:51 +05304import frappe
Rushabh Mehta9f0d6252014-04-14 19:20:45 +05305from frappe import _
Nabin Hait9d0f6362013-01-07 18:51:11 +05306import json
Rushabh Mehta793ba6b2014-02-14 15:47:51 +05307from frappe.utils import flt, cstr, nowdate, add_days, cint
8from frappe.defaults import get_global_default
Anand Doshi4f0a4282014-07-07 15:21:18 +05309from erpnext.accounts.utils import get_fiscal_year, FiscalYearError
Nabin Hait9d0f6362013-01-07 18:51:11 +053010
Rushabh Mehta793ba6b2014-02-14 15:47:51 +053011class InvalidWarehouseCompany(frappe.ValidationError): pass
Anand Doshi2ce39cf2014-04-07 18:51:58 +053012
Nabin Hait625da792013-09-25 10:32:51 +053013def get_stock_balance_on(warehouse, posting_date=None):
Nabin Hait0dd7be12013-08-02 11:45:43 +053014 if not posting_date: posting_date = nowdate()
Anand Doshi2ce39cf2014-04-07 18:51:58 +053015
Anand Doshie9baaa62014-02-26 12:35:33 +053016 stock_ledger_entries = frappe.db.sql("""
Anand Doshi2ce39cf2014-04-07 18:51:58 +053017 SELECT
Nabin Hait625da792013-09-25 10:32:51 +053018 item_code, stock_value
Anand Doshi2ce39cf2014-04-07 18:51:58 +053019 FROM
Nabin Hait0dd7be12013-08-02 11:45:43 +053020 `tabStock Ledger Entry`
Anand Doshi2ce39cf2014-04-07 18:51:58 +053021 WHERE
Nabin Hait625da792013-09-25 10:32:51 +053022 warehouse=%s AND posting_date <= %s
Nabin Hait0dd7be12013-08-02 11:45:43 +053023 ORDER BY timestamp(posting_date, posting_time) DESC, name DESC
Nabin Hait625da792013-09-25 10:32:51 +053024 """, (warehouse, posting_date), as_dict=1)
Anand Doshi2ce39cf2014-04-07 18:51:58 +053025
Nabin Hait0dd7be12013-08-02 11:45:43 +053026 sle_map = {}
27 for sle in stock_ledger_entries:
Nabin Hait625da792013-09-25 10:32:51 +053028 sle_map.setdefault(sle.item_code, flt(sle.stock_value))
Anand Doshi2ce39cf2014-04-07 18:51:58 +053029
Nabin Hait625da792013-09-25 10:32:51 +053030 return sum(sle_map.values())
Anand Doshi2ce39cf2014-04-07 18:51:58 +053031
Nabin Hait47dc3182013-08-06 15:58:16 +053032def get_latest_stock_balance():
33 bin_map = {}
Anand Doshi2ce39cf2014-04-07 18:51:58 +053034 for d in frappe.db.sql("""SELECT item_code, warehouse, stock_value as stock_value
Nabin Hait47dc3182013-08-06 15:58:16 +053035 FROM tabBin""", as_dict=1):
Nabin Hait469ee712013-08-07 12:33:37 +053036 bin_map.setdefault(d.warehouse, {}).setdefault(d.item_code, flt(d.stock_value))
Anand Doshi2ce39cf2014-04-07 18:51:58 +053037
Nabin Hait47dc3182013-08-06 15:58:16 +053038 return bin_map
Anand Doshi2ce39cf2014-04-07 18:51:58 +053039
Nabin Hait74c281c2013-08-19 16:17:18 +053040def get_bin(item_code, warehouse):
Anand Doshie9baaa62014-02-26 12:35:33 +053041 bin = frappe.db.get_value("Bin", {"item_code": item_code, "warehouse": warehouse})
Nabin Hait74c281c2013-08-19 16:17:18 +053042 if not bin:
Rushabh Mehtaa504f062014-04-04 12:16:26 +053043 bin_obj = frappe.get_doc({
Nabin Hait74c281c2013-08-19 16:17:18 +053044 "doctype": "Bin",
45 "item_code": item_code,
46 "warehouse": warehouse,
Rushabh Mehtaa504f062014-04-04 12:16:26 +053047 })
48 bin_obj.ignore_permissions = 1
49 bin_obj.insert()
Nabin Hait74c281c2013-08-19 16:17:18 +053050 else:
Rushabh Mehtaa504f062014-04-04 12:16:26 +053051 bin_obj = frappe.get_doc('Bin', bin)
Anand Doshi094610d2014-04-16 19:56:53 +053052 bin_obj.ignore_permissions = True
Nabin Hait74c281c2013-08-19 16:17:18 +053053 return bin_obj
54
55def update_bin(args):
Anand Doshie9baaa62014-02-26 12:35:33 +053056 is_stock_item = frappe.db.get_value('Item', args.get("item_code"), 'is_stock_item')
Nabin Hait74c281c2013-08-19 16:17:18 +053057 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:
Rushabh Mehta9f0d6252014-04-14 19:20:45 +053062 frappe.msgprint(_("Item {0} ignored since it is not a stock item").format(args.get("item_code")))
Nabin Hait0dd7be12013-08-02 11:45:43 +053063
Nabin Hait9d0f6362013-01-07 18:51:11 +053064def get_incoming_rate(args):
65 """Get Incoming Rate based on valuation method"""
Rushabh Mehta1f847992013-12-12 19:12:19 +053066 from erpnext.stock.stock_ledger import get_previous_sle
Anand Doshi2ce39cf2014-04-07 18:51:58 +053067
Nabin Hait9d0f6362013-01-07 18:51:11 +053068 in_rate = 0
Anand Doshi40a8ae22014-08-29 16:28:31 +053069 if (args.get("serial_no") or "").strip():
Nabin Hait9d0f6362013-01-07 18:51:11 +053070 in_rate = get_avg_purchase_rate(args.get("serial_no"))
Nabin Hait9d0f6362013-01-07 18:51:11 +053071 else:
72 valuation_method = get_valuation_method(args.get("item_code"))
73 previous_sle = get_previous_sle(args)
74 if valuation_method == 'FIFO':
Nabin Hait831207f2013-01-16 14:15:48 +053075 if not previous_sle:
76 return 0.0
Rushabh Mehta4c17f942013-08-12 14:18:09 +053077 previous_stock_queue = json.loads(previous_sle.get('stock_queue', '[]') or '[]')
Nabin Hait227db762014-05-08 19:06:01 +053078 in_rate = get_fifo_rate(previous_stock_queue, args.get("qty") or 0) if previous_stock_queue else 0
Nabin Hait9d0f6362013-01-07 18:51:11 +053079 elif valuation_method == 'Moving Average':
80 in_rate = previous_sle.get('valuation_rate') or 0
Anand Doshi094610d2014-04-16 19:56:53 +053081
Nabin Hait9d0f6362013-01-07 18:51:11 +053082 return in_rate
Anand Doshi2ce39cf2014-04-07 18:51:58 +053083
Nabin Hait9d0f6362013-01-07 18:51:11 +053084def get_avg_purchase_rate(serial_nos):
85 """get average value of serial numbers"""
Anand Doshi2ce39cf2014-04-07 18:51:58 +053086
Nabin Hait9d0f6362013-01-07 18:51:11 +053087 serial_nos = get_valid_serial_nos(serial_nos)
Anand Doshi2ce39cf2014-04-07 18:51:58 +053088 return flt(frappe.db.sql("""select avg(ifnull(purchase_rate, 0)) from `tabSerial No`
Nabin Hait9d0f6362013-01-07 18:51:11 +053089 where name in (%s)""" % ", ".join(["%s"] * len(serial_nos)),
90 tuple(serial_nos))[0][0])
91
92def get_valuation_method(item_code):
93 """get valuation method from item or default"""
Anand Doshie9baaa62014-02-26 12:35:33 +053094 val_method = frappe.db.get_value('Item', item_code, 'valuation_method')
Nabin Hait9d0f6362013-01-07 18:51:11 +053095 if not val_method:
Rushabh Mehta5117d9c2013-02-19 15:27:31 +053096 val_method = get_global_default('valuation_method') or "FIFO"
Nabin Hait9d0f6362013-01-07 18:51:11 +053097 return val_method
Anand Doshi2ce39cf2014-04-07 18:51:58 +053098
Nabin Hait831207f2013-01-16 14:15:48 +053099def get_fifo_rate(previous_stock_queue, qty):
100 """get FIFO (average) Rate from Queue"""
101 if qty >= 0:
Anand Doshi2ce39cf2014-04-07 18:51:58 +0530102 total = sum(f[0] for f in previous_stock_queue)
Nabin Hait831207f2013-01-16 14:15:48 +0530103 return total and sum(f[0] * f[1] for f in previous_stock_queue) / flt(total) or 0.0
104 else:
Nabin Hait227db762014-05-08 19:06:01 +0530105 available_qty_for_outgoing, outgoing_cost = 0, 0
Nabin Hait831207f2013-01-16 14:15:48 +0530106 qty_to_pop = abs(qty)
Nabin Hait64d7c4b2013-01-17 12:22:59 +0530107 while qty_to_pop and previous_stock_queue:
Nabin Hait831207f2013-01-16 14:15:48 +0530108 batch = previous_stock_queue[0]
109 if 0 < batch[0] <= qty_to_pop:
110 # if batch qty > 0
111 # not enough or exactly same qty in current batch, clear batch
Nabin Hait227db762014-05-08 19:06:01 +0530112 available_qty_for_outgoing += flt(batch[0])
Nabin Hait831207f2013-01-16 14:15:48 +0530113 outgoing_cost += flt(batch[0]) * flt(batch[1])
114 qty_to_pop -= batch[0]
115 previous_stock_queue.pop(0)
116 else:
117 # all from current batch
Nabin Hait227db762014-05-08 19:06:01 +0530118 available_qty_for_outgoing += flt(qty_to_pop)
Nabin Hait831207f2013-01-16 14:15:48 +0530119 outgoing_cost += flt(qty_to_pop) * flt(batch[1])
120 batch[0] -= qty_to_pop
121 qty_to_pop = 0
Anand Doshi094610d2014-04-16 19:56:53 +0530122
Nabin Hait227db762014-05-08 19:06:01 +0530123 return outgoing_cost / available_qty_for_outgoing
Anand Doshi2ce39cf2014-04-07 18:51:58 +0530124
Nabin Hait9d0f6362013-01-07 18:51:11 +0530125def get_valid_serial_nos(sr_nos, qty=0, item_code=''):
126 """split serial nos, validate and return list of valid serial nos"""
127 # TODO: remove duplicates in client side
128 serial_nos = cstr(sr_nos).strip().replace(',', '\n').split('\n')
Anand Doshi2ce39cf2014-04-07 18:51:58 +0530129
Nabin Hait9d0f6362013-01-07 18:51:11 +0530130 valid_serial_nos = []
131 for val in serial_nos:
132 if val:
133 val = val.strip()
134 if val in valid_serial_nos:
Rushabh Mehta9f0d6252014-04-14 19:20:45 +0530135 frappe.throw(_("Serial number {0} entered more than once").format(val))
Nabin Hait9d0f6362013-01-07 18:51:11 +0530136 else:
137 valid_serial_nos.append(val)
Anand Doshi2ce39cf2014-04-07 18:51:58 +0530138
Nabin Hait9d0f6362013-01-07 18:51:11 +0530139 if qty and len(valid_serial_nos) != abs(qty):
Rushabh Mehta9f0d6252014-04-14 19:20:45 +0530140 frappe.throw(_("{0} valid serial nos for Item {1}").format(abs(qty), item_code))
Anand Doshi2ce39cf2014-04-07 18:51:58 +0530141
Rushabh Mehta0dbe8982013-02-04 13:56:50 +0530142 return valid_serial_nos
Nabin Haita72c5122013-03-06 18:50:53 +0530143
Anand Doshi373680b2013-10-10 16:04:40 +0530144def validate_warehouse_company(warehouse, company):
Anand Doshie9baaa62014-02-26 12:35:33 +0530145 warehouse_company = frappe.db.get_value("Warehouse", warehouse, "company")
Anand Doshi373680b2013-10-10 16:04:40 +0530146 if warehouse_company and warehouse_company != company:
Rushabh Mehta9f0d6252014-04-14 19:20:45 +0530147 frappe.throw(_("Warehouse {0} does not belong to company {1}").format(warehouse, company),
148 InvalidWarehouseCompany)
Rushabh Mehtaa65253b2013-08-27 10:32:56 +0530149
Anand Doshi2ce39cf2014-04-07 18:51:58 +0530150def get_sales_bom_buying_amount(item_code, warehouse, voucher_type, voucher_no, voucher_detail_no,
Nabin Hait94c90bd2013-08-30 22:48:19 +0530151 stock_ledger_entries, item_sales_bom):
152 # sales bom item
153 buying_amount = 0.0
154 for bom_item in item_sales_bom[item_code]:
155 if bom_item.get("parent_detail_docname")==voucher_detail_no:
Anand Doshi2ce39cf2014-04-07 18:51:58 +0530156 buying_amount += get_buying_amount(voucher_type, voucher_no, voucher_detail_no,
Nabin Hait94c90bd2013-08-30 22:48:19 +0530157 stock_ledger_entries.get((bom_item.item_code, warehouse), []))
158
159 return buying_amount
Anand Doshi2ce39cf2014-04-07 18:51:58 +0530160
ankitjavalkarwork9aa11d22014-09-18 19:06:11 +0530161def get_buying_amount(item_code, item_qty, voucher_type, voucher_no, item_row, stock_ledger_entries):
Anand Doshi5dd6b1d2013-08-07 19:27:30 +0530162 # IMP NOTE
Anand Doshi2ce39cf2014-04-07 18:51:58 +0530163 # stock_ledger_entries should already be filtered by item_code and warehouse and
Anand Doshi5dd6b1d2013-08-07 19:27:30 +0530164 # sorted by posting_date desc, posting_time desc
ankitjavalkarwork9aa11d22014-09-18 19:06:11 +0530165 if frappe.db.get_value("Item", item_code, "is_stock_item") == "Yes":
166 for i, sle in enumerate(stock_ledger_entries):
167 if sle.voucher_type == voucher_type and sle.voucher_no == voucher_no and \
168 sle.voucher_detail_no == item_row:
169 previous_stock_value = len(stock_ledger_entries) > i+1 and \
170 flt(stock_ledger_entries[i+1].stock_value) or 0.0
171 buying_amount = previous_stock_value - flt(sle.stock_value)
Anand Doshi2ce39cf2014-04-07 18:51:58 +0530172
ankitjavalkarwork9aa11d22014-09-18 19:06:11 +0530173 return buying_amount
174 else:
175 item_rate = frappe.db.sql("""select sum(base_amount) / sum(qty)
176 from `tabPurchase Invoice Item`
177 where item_code = %s and docstatus=1""" % ('%s'), item_code)
Nabin Hait5e080f92014-09-26 15:05:30 +0530178 buying_amount = flt(item_qty) * flt(item_rate[0][0]) if item_rate else 0
ankitjavalkarwork9aa11d22014-09-18 19:06:11 +0530179
180 return buying_amount
181
Nabin Hait62d06292013-05-22 16:19:10 +0530182 return 0.0
Anand Doshi2ce39cf2014-04-07 18:51:58 +0530183
Nabin Hait62d06292013-05-22 16:19:10 +0530184
185def reorder_item():
186 """ Reorder item if stock reaches reorder level"""
Nabin Haite6ddd282014-05-30 14:44:37 +0530187 # if initial setup not completed, return
188 if not frappe.db.sql("select name from `tabFiscal Year` limit 1"):
189 return
190
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530191 if getattr(frappe.local, "auto_indent", None) is None:
Anand Doshie9baaa62014-02-26 12:35:33 +0530192 frappe.local.auto_indent = cint(frappe.db.get_value('Stock Settings', None, 'auto_indent'))
Anand Doshi2ce39cf2014-04-07 18:51:58 +0530193
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530194 if frappe.local.auto_indent:
Rushabh Mehta724f9e52014-10-07 15:29:58 +0530195 return _reorder_item()
Anand Doshie673a662014-07-02 18:12:02 +0530196
Anand Doshib8189d72014-07-16 19:24:53 +0530197def _reorder_item():
Anand Doshib8189d72014-07-16 19:24:53 +0530198 material_requests = {"Purchase": {}, "Transfer": {}}
Anand Doshi2ce39cf2014-04-07 18:51:58 +0530199
Anand Doshib8189d72014-07-16 19:24:53 +0530200 item_warehouse_projected_qty = get_item_warehouse_projected_qty()
Rushabh Mehta724f9e52014-10-07 15:29:58 +0530201
202 warehouse_company = frappe._dict(frappe.db.sql("""select name, company
203 from `tabWarehouse`"""))
Anand Doshid141cf72014-07-18 10:31:34 +0530204 default_company = (frappe.defaults.get_defaults().get("company") or
Anand Doshib8189d72014-07-16 19:24:53 +0530205 frappe.db.sql("""select name from tabCompany limit 1""")[0][0])
Anand Doshi2ce39cf2014-04-07 18:51:58 +0530206
Anand Doshib8189d72014-07-16 19:24:53 +0530207 def add_to_material_request(item_code, warehouse, reorder_level, reorder_qty, material_request_type):
208 if warehouse not in item_warehouse_projected_qty[item_code]:
209 # likely a disabled warehouse or a warehouse where BIN does not exist
210 return
Anand Doshi2ce39cf2014-04-07 18:51:58 +0530211
Anand Doshib8189d72014-07-16 19:24:53 +0530212 reorder_level = flt(reorder_level)
213 reorder_qty = flt(reorder_qty)
214 projected_qty = item_warehouse_projected_qty[item_code][warehouse]
Anand Doshi2ce39cf2014-04-07 18:51:58 +0530215
Anand Doshib8189d72014-07-16 19:24:53 +0530216 if reorder_level and projected_qty < reorder_level:
217 deficiency = reorder_level - projected_qty
218 if deficiency > reorder_qty:
219 reorder_qty = deficiency
220
221 company = warehouse_company.get(warehouse) or default_company
222
223 material_requests[material_request_type].setdefault(company, []).append({
224 "item_code": item_code,
225 "warehouse": warehouse,
226 "reorder_qty": reorder_qty
227 })
228
229 for item_code in item_warehouse_projected_qty:
230 item = frappe.get_doc("Item", item_code)
Rushabh Mehta724f9e52014-10-07 15:29:58 +0530231
232 if item.variant_of and not item.get("item_reorder"):
233 item.update_template_tables()
234
Anand Doshib8189d72014-07-16 19:24:53 +0530235 if item.get("item_reorder"):
236 for d in item.get("item_reorder"):
237 add_to_material_request(item_code, d.warehouse, d.warehouse_reorder_level,
238 d.warehouse_reorder_qty, d.material_request_type)
239
240 else:
241 # raise for default warehouse
242 add_to_material_request(item_code, item.default_warehouse, item.re_order_level, item.re_order_qty, "Purchase")
243
244 if material_requests:
Rushabh Mehta724f9e52014-10-07 15:29:58 +0530245 return create_material_request(material_requests)
Anand Doshib8189d72014-07-16 19:24:53 +0530246
247def get_item_warehouse_projected_qty():
248 item_warehouse_projected_qty = {}
249
250 for item_code, warehouse, projected_qty in frappe.db.sql("""select item_code, warehouse, projected_qty
251 from tabBin where ifnull(item_code, '') != '' and ifnull(warehouse, '') != ''
252 and exists (select name from `tabItem`
253 where `tabItem`.name = `tabBin`.item_code and
254 is_stock_item='Yes' and (is_purchase_item='Yes' or is_sub_contracted_item='Yes') and
255 (ifnull(end_of_life, '0000-00-00')='0000-00-00' or end_of_life > %s))
256 and exists (select name from `tabWarehouse`
257 where `tabWarehouse`.name = `tabBin`.warehouse
258 and ifnull(disabled, 0)=0)""", nowdate()):
259
260 item_warehouse_projected_qty.setdefault(item_code, {})[warehouse] = flt(projected_qty)
261
262 return item_warehouse_projected_qty
Nabin Hait62d06292013-05-22 16:19:10 +0530263
264def create_material_request(material_requests):
265 """ Create indent on reaching reorder level """
266 mr_list = []
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530267 defaults = frappe.defaults.get_defaults()
Anand Doshiad6180e2013-06-17 11:57:04 +0530268 exceptions_list = []
Anand Doshi4f0a4282014-07-07 15:21:18 +0530269
270 def _log_exception():
271 if frappe.local.message_log:
272 exceptions_list.extend(frappe.local.message_log)
273 frappe.local.message_log = []
274 else:
275 exceptions_list.append(frappe.get_traceback())
276
277 try:
278 current_fiscal_year = get_fiscal_year(nowdate())[0] or defaults.fiscal_year
279
280 except FiscalYearError:
281 _log_exception()
282 notify_errors(exceptions_list)
283 return
284
Nabin Hait62d06292013-05-22 16:19:10 +0530285 for request_type in material_requests:
286 for company in material_requests[request_type]:
Anand Doshiad6180e2013-06-17 11:57:04 +0530287 try:
288 items = material_requests[request_type][company]
289 if not items:
290 continue
Anand Doshi2ce39cf2014-04-07 18:51:58 +0530291
292 mr = frappe.new_doc("Material Request")
293 mr.update({
Nabin Hait62d06292013-05-22 16:19:10 +0530294 "company": company,
Akhilesh Darjeeb0a1b3a2013-11-26 14:45:27 +0530295 "fiscal_year": current_fiscal_year,
Nabin Hait62d06292013-05-22 16:19:10 +0530296 "transaction_date": nowdate(),
Anand Doshi4a67d362013-11-13 14:53:09 +0530297 "material_request_type": request_type
Anand Doshi2ce39cf2014-04-07 18:51:58 +0530298 })
299
Anand Doshiad6180e2013-06-17 11:57:04 +0530300 for d in items:
Anand Doshib8189d72014-07-16 19:24:53 +0530301 d = frappe._dict(d)
Rushabh Mehtab385ecf2014-03-28 16:44:37 +0530302 item = frappe.get_doc("Item", d.item_code)
Anand Doshi2ce39cf2014-04-07 18:51:58 +0530303 mr.append("indent_details", {
Anand Doshiad6180e2013-06-17 11:57:04 +0530304 "doctype": "Material Request Item",
Anand Doshiad6180e2013-06-17 11:57:04 +0530305 "item_code": d.item_code,
306 "schedule_date": add_days(nowdate(),cint(item.lead_time_days)),
307 "uom": item.stock_uom,
308 "warehouse": d.warehouse,
309 "item_name": item.item_name,
310 "description": item.description,
311 "item_group": item.item_group,
312 "qty": d.reorder_qty,
313 "brand": item.brand,
314 })
Anand Doshi2ce39cf2014-04-07 18:51:58 +0530315
316 mr.insert()
317 mr.submit()
318 mr_list.append(mr)
Anand Doshi6f6e91c2013-07-22 11:28:34 +0530319
Anand Doshiad6180e2013-06-17 11:57:04 +0530320 except:
Anand Doshi4f0a4282014-07-07 15:21:18 +0530321 _log_exception()
Nabin Hait62d06292013-05-22 16:19:10 +0530322
323 if mr_list:
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530324 if getattr(frappe.local, "reorder_email_notify", None) is None:
Anand Doshi2ce39cf2014-04-07 18:51:58 +0530325 frappe.local.reorder_email_notify = cint(frappe.db.get_value('Stock Settings', None,
Nabin Haitbf5d44c2013-08-12 16:30:48 +0530326 'reorder_email_notify'))
Anand Doshi2ce39cf2014-04-07 18:51:58 +0530327
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530328 if(frappe.local.reorder_email_notify):
Nabin Hait62d06292013-05-22 16:19:10 +0530329 send_email_notification(mr_list)
Anand Doshiad6180e2013-06-17 11:57:04 +0530330
331 if exceptions_list:
332 notify_errors(exceptions_list)
Anand Doshi2ce39cf2014-04-07 18:51:58 +0530333
Rushabh Mehta724f9e52014-10-07 15:29:58 +0530334 return mr_list
335
Nabin Hait62d06292013-05-22 16:19:10 +0530336def send_email_notification(mr_list):
337 """ Notify user about auto creation of indent"""
Anand Doshi2ce39cf2014-04-07 18:51:58 +0530338
339 email_list = frappe.db.sql_list("""select distinct r.parent
Rushabh Mehta7c932002014-03-11 16:15:05 +0530340 from tabUserRole r, tabUser p
Nabin Hait62d06292013-05-22 16:19:10 +0530341 where p.name = r.parent and p.enabled = 1 and p.docstatus < 2
Anand Doshi2ce39cf2014-04-07 18:51:58 +0530342 and r.role in ('Purchase Manager','Material Manager')
Nabin Hait62d06292013-05-22 16:19:10 +0530343 and p.name not in ('Administrator', 'All', 'Guest')""")
Anand Doshi2ce39cf2014-04-07 18:51:58 +0530344
Nabin Hait62d06292013-05-22 16:19:10 +0530345 msg="""<h3>Following Material Requests has been raised automatically \
346 based on item reorder level:</h3>"""
347 for mr in mr_list:
Anand Doshif78d1ae2014-03-28 13:55:00 +0530348 msg += "<p><b><u>" + mr.name + """</u></b></p><table class='table table-bordered'><tr>
Nabin Hait62d06292013-05-22 16:19:10 +0530349 <th>Item Code</th><th>Warehouse</th><th>Qty</th><th>UOM</th></tr>"""
Rushabh Mehtad2b34dc2014-03-27 16:12:56 +0530350 for item in mr.get("indent_details"):
Nabin Hait62d06292013-05-22 16:19:10 +0530351 msg += "<tr><td>" + item.item_code + "</td><td>" + item.warehouse + "</td><td>" + \
352 cstr(item.qty) + "</td><td>" + cstr(item.uom) + "</td></tr>"
353 msg += "</table>"
Rushabh Mehtabf8715d2014-09-16 14:57:31 +0530354 frappe.sendmail(recipients=email_list, subject='Auto Material Request Generation Notification', msg = msg)
Anand Doshi2ce39cf2014-04-07 18:51:58 +0530355
Anand Doshiad6180e2013-06-17 11:57:04 +0530356def notify_errors(exceptions_list):
357 subject = "[Important] [ERPNext] Error(s) while creating Material Requests based on Re-order Levels"
Rushabh Mehtabf8715d2014-09-16 14:57:31 +0530358 content = """Dear System Manager,
Anand Doshiad6180e2013-06-17 11:57:04 +0530359
Anand Doshi4f0a4282014-07-07 15:21:18 +0530360An error occured for certain Items while creating Material Requests based on Re-order level.
Anand Doshi2ce39cf2014-04-07 18:51:58 +0530361
Anand Doshi4f0a4282014-07-07 15:21:18 +0530362Please rectify these issues:
363---
364<pre>
365%s
366</pre>
367---
368Regards,
369Administrator""" % ("\n\n".join(exceptions_list),)
Anand Doshiad6180e2013-06-17 11:57:04 +0530370
Rushabh Mehtabf8715d2014-09-16 14:57:31 +0530371 from frappe.email import sendmail_to_system_managers
372 sendmail_to_system_managers(subject, content)