[material request on re-order level] [fixes] if error occurs, send email to the System Manager

This commit is contained in:
Anand Doshi 2013-06-17 11:57:04 +05:30
parent 0865f60673
commit ad6180ef6d
4 changed files with 81 additions and 41 deletions

View File

@ -185,10 +185,13 @@ class DocType(BuyingController):
if d.fields.has_key(x): if d.fields.has_key(x):
d.fields[x] = f_lst[x] d.fields[x] = f_lst[x]
item = sql("select is_stock_item, is_purchase_item, is_sub_contracted_item from tabItem where name=%s and (ifnull(end_of_life,'')='' or end_of_life = '0000-00-00' or end_of_life > now())", d.item_code) item = sql("select is_stock_item, is_purchase_item, is_sub_contracted_item, end_of_life from tabItem where name=%s",
d.item_code)
if not item: if not item:
msgprint("Item %s does not exist in Item Master." % cstr(d.item_code)) msgprint("Item %s does not exist in Item Master." % cstr(d.item_code), raise_exception=True)
raise Exception
from stock.utils import validate_end_of_life
validate_end_of_life(d.item_code, item[0][3])
# validate stock item # validate stock item
if item[0][0]=='Yes' and d.qty and not d.warehouse: if item[0][0]=='Yes' and d.qty and not d.warehouse:
@ -197,9 +200,7 @@ class DocType(BuyingController):
# validate purchase item # validate purchase item
if item[0][1] != 'Yes' and item[0][2] != 'Yes': if item[0][1] != 'Yes' and item[0][2] != 'Yes':
msgprint("Item %s is not a purchase item or sub-contracted item. Please check" % (d.item_code)) msgprint("Item %s is not a purchase item or sub-contracted item. Please check" % (d.item_code), raise_exception=True)
raise Exception
if d.fields.has_key('prevdoc_docname') and d.prevdoc_docname: if d.fields.has_key('prevdoc_docname') and d.prevdoc_docname:
# check warehouse, uom in previous doc and in current doc are same. # check warehouse, uom in previous doc and in current doc are same.
@ -215,13 +216,13 @@ class DocType(BuyingController):
# Check if Warehouse has been modified. # Check if Warehouse has been modified.
if not cstr(data[0]['warehouse']) == cstr(d.warehouse): if not cstr(data[0]['warehouse']) == cstr(d.warehouse):
msgprint("Please check warehouse %s of Item %s which is not present in %s %s ." % (d.warehouse, d.item_code, d.prevdoc_doctype, d.prevdoc_docname)) msgprint("Please check warehouse %s of Item %s which is not present in %s %s ." % \
raise Exception (d.warehouse, d.item_code, d.prevdoc_doctype, d.prevdoc_docname), raise_exception=True)
# Check if UOM has been modified. # Check if UOM has been modified.
if not cstr(data[0]['uom']) == cstr(d.uom) and not cstr(d.prevdoc_doctype) == 'Material Request': if not cstr(data[0]['uom']) == cstr(d.uom) and not cstr(d.prevdoc_doctype) == 'Material Request':
msgprint("Please check UOM %s of Item %s which is not present in %s %s ." % (d.uom, d.item_code, d.prevdoc_doctype, d.prevdoc_docname)) msgprint("Please check UOM %s of Item %s which is not present in %s %s ." % \
raise Exception (d.uom, d.item_code, d.prevdoc_doctype, d.prevdoc_docname), raise_exception=True)
# list criteria that should not repeat if item is stock item # list criteria that should not repeat if item is stock item
e = [d.schedule_date, d.item_code, d.description, d.warehouse, d.uom, d.fields.has_key('prevdoc_docname') and d.prevdoc_docname or '', d.fields.has_key('prevdoc_detail_docname') and d.prevdoc_detail_docname or '', d.fields.has_key('batch_no') and d.batch_no or ''] e = [d.schedule_date, d.item_code, d.description, d.warehouse, d.uom, d.fields.has_key('prevdoc_docname') and d.prevdoc_docname or '', d.fields.has_key('prevdoc_detail_docname') and d.prevdoc_detail_docname or '', d.fields.has_key('batch_no') and d.batch_no or '']

View File

@ -53,7 +53,7 @@ def execute_daily():
# daily backup # daily backup
from setup.doctype.backup_manager.backup_manager import take_backups_daily from setup.doctype.backup_manager.backup_manager import take_backups_daily
take_backups_daily() run_fn(take_backups_daily)
# check reorder level # check reorder level
from stock.utils import reorder_item from stock.utils import reorder_item
@ -61,7 +61,7 @@ def execute_daily():
def execute_weekly(): def execute_weekly():
from setup.doctype.backup_manager.backup_manager import take_backups_weekly from setup.doctype.backup_manager.backup_manager import take_backups_weekly
take_backups_weekly() run_fn(take_backups_weekly)
def execute_monthly(): def execute_monthly():
pass pass

View File

@ -21,6 +21,9 @@ class TestStockEntry(unittest.TestCase):
st2.insert() st2.insert()
st2.submit() st2.submit()
from stock.utils import reorder_item
reorder_item()
mr_name = webnotes.conn.sql("""select parent from `tabMaterial Request Item` mr_name = webnotes.conn.sql("""select parent from `tabMaterial Request Item`
where item_code='_Test Item'""") where item_code='_Test Item'""")

View File

@ -19,13 +19,14 @@ from webnotes import msgprint, _
import json import json
from webnotes.utils import flt, cstr, nowdate, add_days, cint from webnotes.utils import flt, cstr, nowdate, add_days, cint
from webnotes.defaults import get_global_default from webnotes.defaults import get_global_default
from webnotes.utils.email_lib import sendmail
def validate_end_of_life(item_code, end_of_life=None, verbose=1): def validate_end_of_life(item_code, end_of_life=None, verbose=1):
if not end_of_life: if not end_of_life:
end_of_life = webnotes.conn.get_value("Item", item_code, "end_of_life") end_of_life = webnotes.conn.get_value("Item", item_code, "end_of_life")
from webnotes.utils import getdate, now_datetime, formatdate from webnotes.utils import getdate, now_datetime, formatdate
if end_of_life and getdate(end_of_life) > now_datetime().date(): if end_of_life and getdate(end_of_life) <= now_datetime().date():
msg = (_("Item") + " %(item_code)s: " + _("reached its end of life on") + \ msg = (_("Item") + " %(item_code)s: " + _("reached its end of life on") + \
" %(date)s. " + _("Please check") + ": %(end_of_life_label)s " + \ " %(date)s. " + _("Please check") + ": %(end_of_life_label)s " + \
"in Item master") % { "in Item master") % {
@ -205,7 +206,11 @@ def reorder_item():
if webnotes.auto_indent: if webnotes.auto_indent:
material_requests = {} material_requests = {}
bin_list = webnotes.conn.sql("""select item_code, warehouse, projected_qty bin_list = webnotes.conn.sql("""select item_code, warehouse, projected_qty
from tabBin where ifnull(item_code, '') != '' and ifnull(warehouse, '') != ''""", from tabBin where ifnull(item_code, '') != '' and ifnull(warehouse, '') != ''
and exists (select name from `tabItem`
where `tabItem`.name = `tabBin`.item_code and
is_stock_item='Yes' and (is_purchase_item='Yes' or is_sub_contracted_item='Yes') and
(ifnull(end_of_life, '')='') or end_of_life > now())""",
as_dict=True) as_dict=True)
for bin in bin_list: for bin in bin_list:
#check if re-order is required #check if re-order is required
@ -220,7 +225,7 @@ def reorder_item():
["re_order_level", "re_order_qty"]) ["re_order_level", "re_order_qty"])
material_request_type = "Purchase" material_request_type = "Purchase"
if reorder_level and flt(bin.projected_qty) < flt(reorder_level): if flt(reorder_level) and flt(bin.projected_qty) < flt(reorder_level):
if flt(reorder_level) - flt(bin.projected_qty) > flt(reorder_qty): if flt(reorder_level) - flt(bin.projected_qty) > flt(reorder_qty):
reorder_qty = flt(reorder_level) - flt(bin.projected_qty) reorder_qty = flt(reorder_level) - flt(bin.projected_qty)
@ -242,10 +247,14 @@ def create_material_request(material_requests):
""" Create indent on reaching reorder level """ """ Create indent on reaching reorder level """
mr_list = [] mr_list = []
defaults = webnotes.defaults.get_defaults() defaults = webnotes.defaults.get_defaults()
exceptions_list = []
for request_type in material_requests: for request_type in material_requests:
for company in material_requests[request_type]: for company in material_requests[request_type]:
try:
items = material_requests[request_type][company] items = material_requests[request_type][company]
if items: if not items:
continue
mr = [{ mr = [{
"doctype": "Material Request", "doctype": "Material Request",
"company": company, "company": company,
@ -279,6 +288,13 @@ def create_material_request(material_requests):
mr_bean.submit() mr_bean.submit()
mr_list.append(mr_bean) mr_list.append(mr_bean)
except:
if webnotes.message_log:
exceptions_list.append([] + webnotes.message_log)
webnotes.message_log = []
else:
exceptions_list.append(webnotes.getTraceback())
if mr_list: if mr_list:
if not hasattr(webnotes, "reorder_email_notify"): if not hasattr(webnotes, "reorder_email_notify"):
webnotes.reorder_email_notify = webnotes.conn.get_value('Global Defaults', None, webnotes.reorder_email_notify = webnotes.conn.get_value('Global Defaults', None,
@ -287,10 +303,12 @@ def create_material_request(material_requests):
if(webnotes.reorder_email_notify): if(webnotes.reorder_email_notify):
send_email_notification(mr_list) send_email_notification(mr_list)
if exceptions_list:
notify_errors(exceptions_list)
def send_email_notification(mr_list): def send_email_notification(mr_list):
""" Notify user about auto creation of indent""" """ Notify user about auto creation of indent"""
from webnotes.utils.email_lib import sendmail
email_list = webnotes.conn.sql_list("""select distinct r.parent email_list = webnotes.conn.sql_list("""select distinct r.parent
from tabUserRole r, tabProfile p from tabUserRole r, tabProfile p
where p.name = r.parent and p.enabled = 1 and p.docstatus < 2 where p.name = r.parent and p.enabled = 1 and p.docstatus < 2
@ -308,3 +326,21 @@ def send_email_notification(mr_list):
msg += "</table>" msg += "</table>"
sendmail(email_list, subject='Auto Material Request Generation Notification', msg = msg) sendmail(email_list, subject='Auto Material Request Generation Notification', msg = msg)
def notify_errors(exceptions_list):
subject = "[Important] [ERPNext] Error(s) while creating Material Requests based on Re-order Levels"
msg = """Dear System Manager,
An error occured for certain Items while creating Material Requests based on Re-order level.
Please rectify these issues:
---
%s
---
Regards,
Administrator""" % ("\n\n".join(["\n".join(msg) for msg in exceptions_list]),)
from webnotes.profile import get_system_managers
sendmail(get_system_managers(), subject=subject, msg=msg)