diff --git a/buying/doctype/purchase_common/purchase_common.py b/buying/doctype/purchase_common/purchase_common.py index 6d2baf6c14..4b5fe020bd 100644 --- a/buying/doctype/purchase_common/purchase_common.py +++ b/buying/doctype/purchase_common/purchase_common.py @@ -167,21 +167,22 @@ class DocType(BuyingController): if d.fields.has_key(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: - msgprint("Item %s does not exist in Item Master." % cstr(d.item_code)) - raise Exception + msgprint("Item %s does not exist in Item Master." % cstr(d.item_code), raise_exception=True) + + from stock.utils import validate_end_of_life + validate_end_of_life(d.item_code, item[0][3]) # validate stock item if item[0][0]=='Yes' and d.qty and not d.warehouse: - msgprint("Warehouse is mandatory for %s, since it is a stock item" % - d.item_code, raise_exception=1) + msgprint("Warehouse is mandatory for %s, since it is a stock item" % + d.item_code, raise_exception=1) # validate purchase item 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)) - raise Exception - + msgprint("Item %s is not a purchase item or sub-contracted item. Please check" % (d.item_code), raise_exception=True) if d.fields.has_key('prevdoc_docname') and d.prevdoc_docname: # check warehouse, uom in previous doc and in current doc are same. @@ -203,8 +204,8 @@ class DocType(BuyingController): # Check if UOM has been modified. 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)) - raise Exception + 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), raise_exception=True) # 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 ''] diff --git a/patches/june_2013/p04_fix_event_for_lead_oppty_project.py b/patches/june_2013/p04_fix_event_for_lead_oppty_project.py index 3f66d8bfbb..971e4c7459 100644 --- a/patches/june_2013/p04_fix_event_for_lead_oppty_project.py +++ b/patches/june_2013/p04_fix_event_for_lead_oppty_project.py @@ -1,6 +1,8 @@ import webnotes def execute(): + from utilities.transaction_base import delete_events + # delete orphaned Event User webnotes.conn.sql("""delete from `tabEvent User` where not exists(select name from `tabEvent` where `tabEvent`.name = `tabEvent User`.parent)""") @@ -15,5 +17,4 @@ def execute(): webnotes.get_obj(dt, ref_name).add_calendar_event() else: # remove events where ref doc doesn't exist - webnotes.delete_doc("Event", webnotes.conn.sql_list("""select name from `tabEvent` - where ref_type=%s and ref_name=%s""", (dt, ref_name))) \ No newline at end of file + delete_events(dt, ref_name) \ No newline at end of file diff --git a/projects/doctype/project/project.py b/projects/doctype/project/project.py index 62714b84c2..d9bb7c02f4 100644 --- a/projects/doctype/project/project.py +++ b/projects/doctype/project/project.py @@ -19,6 +19,7 @@ import webnotes from webnotes.utils import flt, getdate from webnotes import msgprint +from utilities.transaction_base import delete_events class DocType: def __init__(self, doc, doclist=None): @@ -54,7 +55,7 @@ class DocType: def add_calendar_event(self): # delete any earlier event for this project - self.delete_events() + delete_events(self.doc.doctype, self.doc.name) # add events for milestone in self.doclist.get({"parentfield": "project_milestones"}): @@ -72,8 +73,4 @@ class DocType: }).insert() def on_trash(self): - self.delete_events() - - def delete_events(self): - webnotes.delete_doc("Event", webnotes.conn.sql_list("""select name from `tabEvent` - where ref_type=%s and ref_name=%s""", (self.doc.doctype, self.doc.name))) + delete_events(self.doc.doctype, self.doc.name) diff --git a/selling/utils.py b/selling/utils.py index 8947cb51f6..61eab59dfc 100644 --- a/selling/utils.py +++ b/selling/utils.py @@ -111,7 +111,7 @@ def _get_basic_details(args, item_bean): out = webnotes._dict({ "item_code": item.name, "description": item.description_html or item.description, - "reserved_warehouse": item.default_warehouse, + "reserved_warehouse": item.default_warehouse or args.warehouse or args.reserved_warehouse, "warehouse": item.default_warehouse or args.warehouse, "income_account": item.default_income_account or args.income_account, "expense_account": item.purchase_account or args.expense_account, diff --git a/startup/schedule_handlers.py b/startup/schedule_handlers.py index cc0d1f4fea..99cc05b041 100644 --- a/startup/schedule_handlers.py +++ b/startup/schedule_handlers.py @@ -53,7 +53,7 @@ def execute_daily(): # daily backup from setup.doctype.backup_manager.backup_manager import take_backups_daily - take_backups_daily() + run_fn(take_backups_daily) # check reorder level from stock.utils import reorder_item @@ -61,7 +61,7 @@ def execute_daily(): def execute_weekly(): from setup.doctype.backup_manager.backup_manager import take_backups_weekly - take_backups_weekly() + run_fn(take_backups_weekly) def execute_monthly(): pass diff --git a/stock/doctype/item/test_item.py b/stock/doctype/item/test_item.py index e4f1144302..2145631024 100644 --- a/stock/doctype/item/test_item.py +++ b/stock/doctype/item/test_item.py @@ -54,7 +54,8 @@ test_records = [ "inspection_required": "No", "is_pro_applicable": "No", "is_sub_contracted_item": "No", - "stock_uom": "_Test UOM" + "stock_uom": "_Test UOM", + "default_warehouse": "_Test Warehouse" }, { "doctype": "Item Reorder", "parentfield": "item_reorder", diff --git a/stock/doctype/stock_entry/test_stock_entry.py b/stock/doctype/stock_entry/test_stock_entry.py index ff2e479bb6..11811ba17f 100644 --- a/stock/doctype/stock_entry/test_stock_entry.py +++ b/stock/doctype/stock_entry/test_stock_entry.py @@ -21,6 +21,9 @@ class TestStockEntry(unittest.TestCase): st2.insert() st2.submit() + from stock.utils import reorder_item + reorder_item() + mr_name = webnotes.conn.sql("""select parent from `tabMaterial Request Item` where item_code='_Test Item'""") diff --git a/stock/utils.py b/stock/utils.py index 5e7e53bb01..53ad69b157 100644 --- a/stock/utils.py +++ b/stock/utils.py @@ -19,13 +19,14 @@ from webnotes import msgprint, _ import json from webnotes.utils import flt, cstr, nowdate, add_days, cint 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): if not end_of_life: end_of_life = webnotes.conn.get_value("Item", item_code, "end_of_life") 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") + \ " %(date)s. " + _("Please check") + ": %(end_of_life_label)s " + \ "in Item master") % { @@ -205,7 +206,11 @@ def reorder_item(): if webnotes.auto_indent: material_requests = {} 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) for bin in bin_list: #check if re-order is required @@ -220,7 +225,7 @@ def reorder_item(): ["re_order_level", "re_order_qty"]) 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): 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 """ mr_list = [] defaults = webnotes.defaults.get_defaults() + exceptions_list = [] for request_type in material_requests: for company in material_requests[request_type]: - items = material_requests[request_type][company] - if items: + try: + items = material_requests[request_type][company] + if not items: + continue + mr = [{ "doctype": "Material Request", "company": company, @@ -257,27 +266,34 @@ def create_material_request(material_requests): quantity reaches re-order level when the following record was created""") }] - for d in items: - item = webnotes.doc("Item", d.item_code) - mr.append({ - "doctype": "Material Request Item", - "parenttype": "Material Request", - "parentfield": "indent_details", - "item_code": d.item_code, - "schedule_date": add_days(nowdate(),cint(item.lead_time_days)), - "uom": item.stock_uom, - "warehouse": d.warehouse, - "item_name": item.item_name, - "description": item.description, - "item_group": item.item_group, - "qty": d.reorder_qty, - "brand": item.brand, - }) + for d in items: + item = webnotes.doc("Item", d.item_code) + mr.append({ + "doctype": "Material Request Item", + "parenttype": "Material Request", + "parentfield": "indent_details", + "item_code": d.item_code, + "schedule_date": add_days(nowdate(),cint(item.lead_time_days)), + "uom": item.stock_uom, + "warehouse": d.warehouse, + "item_name": item.item_name, + "description": item.description, + "item_group": item.item_group, + "qty": d.reorder_qty, + "brand": item.brand, + }) - mr_bean = webnotes.bean(mr) - mr_bean.insert() - mr_bean.submit() - mr_list.append(mr_bean) + mr_bean = webnotes.bean(mr) + mr_bean.insert() + mr_bean.submit() + 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 not hasattr(webnotes, "reorder_email_notify"): @@ -286,11 +302,13 @@ def create_material_request(material_requests): if(webnotes.reorder_email_notify): send_email_notification(mr_list) + + if exceptions_list: + notify_errors(exceptions_list) def send_email_notification(mr_list): """ Notify user about auto creation of indent""" - from webnotes.utils.email_lib import sendmail email_list = webnotes.conn.sql_list("""select distinct r.parent from tabUserRole r, tabProfile p where p.name = r.parent and p.enabled = 1 and p.docstatus < 2 @@ -307,4 +325,22 @@ def send_email_notification(mr_list): cstr(item.qty) + "" + cstr(item.uom) + "" msg += "" - sendmail(email_list, subject='Auto Material Request Generation Notification', msg = msg) \ No newline at end of file + 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) diff --git a/support/doctype/maintenance_schedule/maintenance_schedule.py b/support/doctype/maintenance_schedule/maintenance_schedule.py index 06c5a47aff..baed6a9dbd 100644 --- a/support/doctype/maintenance_schedule/maintenance_schedule.py +++ b/support/doctype/maintenance_schedule/maintenance_schedule.py @@ -26,7 +26,7 @@ from webnotes import msgprint sql = webnotes.conn.sql -from utilities.transaction_base import TransactionBase +from utilities.transaction_base import TransactionBase, delete_events class DocType(TransactionBase): def __init__(self, doc, doclist=[]): @@ -327,13 +327,7 @@ class DocType(TransactionBase): if d.serial_no: self.update_amc_date(d.serial_no, '') webnotes.conn.set(self.doc, 'status', 'Cancelled') - self.delete_events() + delete_events(self.doc.doctype, self.doc.name) def on_trash(self): - self.delete_events() - - def delete_events(self): - webnotes.delete_doc("Event", webnotes.conn.sql_list("""select name from `tabEvent` - where ref_type=%s and ref_name=%s""", (self.doc.doctype, self.doc.name))) - - + delete_events(self.doc.doctype, self.doc.name) diff --git a/utilities/transaction_base.py b/utilities/transaction_base.py index 041c4cb55b..c86a50ec19 100644 --- a/utilities/transaction_base.py +++ b/utilities/transaction_base.py @@ -357,3 +357,6 @@ def validate_currency(args, item, meta=None): get_field_precision(meta.get_field("plc_conversion_rate"), webnotes._dict({"fields": args}))) +def delete_events(ref_type, ref_name): + webnotes.delete_doc("Event", webnotes.conn.sql_list("""select name from `tabEvent` + where ref_type=%s and ref_name=%s""", (ref_type, ref_name)), for_reload=True) diff --git a/website/helpers/cart.py b/website/helpers/cart.py index bcea3c2152..efeb9759a2 100644 --- a/website/helpers/cart.py +++ b/website/helpers/cart.py @@ -6,76 +6,39 @@ import webnotes from webnotes import _, msgprint import webnotes.defaults from webnotes.utils import today, get_fullname -import json - -@webnotes.whitelist() -def checkout(cart): - # webnotes.msgprint(cart); - if isinstance(cart, basestring): - cart = json.loads(cart) or {} - - if webnotes.session.user == "Guest": - msgprint(_("Please login before you checkout!"), raise_exception=True) - elif webnotes.conn.get_value("Profile", webnotes.session.user, "user_type") != "Partner": - msgprint(_("Illegal User"), raise_exception=True) - - # make_quotation(cart) - -def make_quotation(cart): - from accounts.utils import get_fiscal_year - - quotation_defaults = webnotes._dict({ - "doctype": "Quotation", - "naming_series": "QTN-13-14-", - "quotation_to": "Customer", - "company": webnotes.defaults.get_user_default("company"), - "order_type": "Sales", - "status": "Draft", - }) - - quotation = webnotes.bean(quotation_defaults) - quotation.doc.fields.update({ - "transaction_date": today(), - "fiscal_year": get_fiscal_year(today()), - - # TODO - "price_list_name": "fetch", - "price_list_currency": "fetch", - "plc_conversion_rate": "something", - "currency": "same as price_list_currency", - "conversion_rate": "same as plc_converion_rate", - "territory": "fetch", - - - }) - - # TODO add items - for item_code, item in cart.items(): - pass - - # TODO apply taxes - - # save and submit @webnotes.whitelist() def add_to_cart(item_code): + update_qty(item_code, 1) + +@webnotes.whitelist() +def remove_from_cart(item_code): + update_qty(item_code, 0) + +@webnotes.whitelist() +def update_qty(item_code, qty_to_set): party = get_lead_or_customer() quotation = get_shopping_cart_quotation(party) - quotation_items = quotation.doclist.get({"parentfield": "quotation_details", "item_code": item_code}) - if not quotation_items: - quotation.doclist.append({ - "doctype": "Quotation Item", - "parentfield": "quotation_details", - "item_code": item_code, - "qty": 1 - }) + if qty_to_set == 0: + quotation.set_doclist(quotation.doclist.get({"item_code": ["!=", item_code]})) + else: + quotation_items = quotation.doclist.get({"item_code": item_code}) + if not quotation_items: + quotation.doclist.append({ + "doctype": "Quotation Item", + "parentfield": "quotation_details", + "item_code": item_code, + "qty": qty_to_set + }) + else: + quotation_items[0].qty = qty_to_set quotation.ignore_permissions = True quotation.save() return quotation.doc.name - + def get_lead_or_customer(): customer = webnotes.conn.get_value("Contact", {"email_id": webnotes.session.user}, "customer") if customer: @@ -116,14 +79,6 @@ def get_shopping_cart_quotation(party): }) return qbean - -@webnotes.whitelist() -def remove_from_cart(item_code): - pass - -@webnotes.whitelist() -def update_qty(item_code, qty): - pass def get_price_list(party): if not party.default_price_list: @@ -153,21 +108,75 @@ def get_price_list_using_geoip(): return price_list_name -import unittest -test_dependencies = ["Item", "Price List"] +@webnotes.whitelist() +def checkout(): + party = get_lead_or_customer() + quotation = get_shopping_cart_quotation(party) + + quotation.ignore_permissions = True + quotation.submit() + + sales_order = webnotes.bean(webnotes.map_doclist([["Quotation", "Sales Order"], ["Quotation Item", "Sales Order Item"], + ["Sales Taxes and Charges", "Sales Taxes and Charges"]], quotation.doc.name)) + + sales_order.ignore_permissions = True + sales_order.insert() + sales_order.submit() + + return sales_order + +import unittest +test_dependencies = ["Item", "Price List", "Contact"] class TestCart(unittest.TestCase): + def test_get_lead_or_customer(self): + webnotes.session.user = "test@example.com" + party1 = get_lead_or_customer() + party2 = get_lead_or_customer() + self.assertEquals(party1.name, party2.name) + self.assertEquals(party1.doctype, "Lead") + + webnotes.session.user = "test_contact_customer@example.com" + party = get_lead_or_customer() + self.assertEquals(party.name, "_Test Customer") + def test_add_to_cart(self): webnotes.session.user = "test@example.com" add_to_cart("_Test Item") - def test_change_qty(self): - pass + quotation = get_shopping_cart_quotation(get_lead_or_customer()) + quotation_items = quotation.doclist.get({"parentfield": "quotation_details", "item_code": "_Test Item"}) + self.assertTrue(quotation_items) + self.assertEquals(quotation_items[0].qty, 1) + + return quotation + + def test_update_qty(self): + self.test_add_to_cart() + + update_qty("_Test Item", 5) + + quotation = get_shopping_cart_quotation(get_lead_or_customer()) + quotation_items = quotation.doclist.get({"parentfield": "quotation_details", "item_code": "_Test Item"}) + self.assertTrue(quotation_items) + self.assertEquals(quotation_items[0].qty, 5) + + return quotation def test_remove_from_cart(self): - pass + quotation0 = self.test_add_to_cart() + + remove_from_cart("_Test Item") + + quotation = get_shopping_cart_quotation(get_lead_or_customer()) + self.assertEquals(quotation0.doc.name, quotation.doc.name) + + quotation_items = quotation.doclist.get({"parentfield": "quotation_details", "item_code": "_Test Item"}) + self.assertEquals(quotation_items, []) def test_checkout(self): - pass + quotation = self.test_update_qty() + sales_order = checkout() + self.assertEquals(sales_order.doclist.getone({"item_code": "_Test Item"}).prevdoc_docname, quotation.doc.name) \ No newline at end of file