def validate_for_items(self, obj): check_list, chk_dupl_itm=[],[] for d in getlist( obj.doclist, obj.fname): # validation for valid qty if flt(d.qty) < 0 or (d.parenttype != 'Purchase Receipt' and not flt(d.qty)): webnotes.throw("Please enter valid qty for item %s" % cstr(d.item_code)) # udpate with latest quantities bin = webnotes.conn.sql("""select projected_qty from `tabBin` where item_code = %s and warehouse = %s""", (d.item_code, d.warehouse), as_dict=1) f_lst ={'projected_qty': bin and flt(bin[0]['projected_qty']) or 0, 'ordered_qty': 0, 'received_qty' : 0} if d.doctype == 'Purchase Receipt Item': f_lst.pop('received_qty') for x in f_lst : if d.fields.has_key(x): d.fields[x] = f_lst[x] item = webnotes.conn.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: webnotes.throw("Item %s does not exist in Item Master." % cstr(d.item_code)) 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: webnotes.throw("Warehouse is mandatory for %s, since it is a stock item" % d.item_code) # validate purchase item if item[0][1] != 'Yes' and item[0][2] != 'Yes': webnotes.throw("Item %s is not a purchase item or sub-contracted item. Please check" % (d.item_code)) # 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('sales_order_no') and d.sales_order_no 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 ''] # if is not stock item f = [d.schedule_date, d.item_code, d.description] ch = webnotes.conn.sql("""select is_stock_item from `tabItem` where name = %s""", d.item_code) if ch and ch[0][0] == 'Yes': # check for same items if e in check_list: webnotes.throw("""Item %s has been entered more than once with same description, schedule date, warehouse and uom.\n Please change any of the field value to enter the item twice""" % d.item_code) else: check_list.append(e) elif ch and ch[0][0] == 'No': # check for same items if f in chk_dupl_itm: webnotes.throw("""Item %s has been entered more than once with same description, schedule date.\n Please change any of the field value to enter the item twice.""" % d.item_code) else: chk_dupl_itm.append(f)
def check_duplicate(self, finder=0): il = getlist(self.doclist, "sales_bom_items") if not il: webnotes.msgprint("Add atleast one item") return # get all Sales BOM that have the first item sbl = webnotes.conn.sql( """select distinct parent from `tabSales BOM Item` where item_code=%s and parent != %s and docstatus != 2""", (il[0].item_code, self.doc.name)) # check all siblings sub_items = [[d.item_code, flt(d.qty)] for d in il] for s in sbl: t = webnotes.conn.sql( """select item_code, qty from `tabSales BOM Item` where parent=%s and docstatus != 2""", s[0]) t = [[d[0], flt(d[1])] for d in t] if self.has_same_items(sub_items, t): webnotes.msgprint("%s has the same Sales BOM details" % s[0]) raise Exception if finder: webnotes.msgprint( "There is no Sales BOM present with the following Combination." )
def validate_qty_against_so(self): so_items = { } # Format --> {'SO/00001': {'Item/001': 120, 'Item/002': 24}} for d in getlist(self.doclist, 'indent_details'): if d.sales_order_no: if not so_items.has_key(d.sales_order_no): so_items[d.sales_order_no] = {d.item_code: flt(d.qty)} else: if not so_items[d.sales_order_no].has_key(d.item_code): so_items[d.sales_order_no][d.item_code] = flt(d.qty) else: so_items[d.sales_order_no][d.item_code] += flt(d.qty) for so_no in so_items.keys(): for item in so_items[so_no].keys(): already_indented = webnotes.conn.sql( "select sum(qty) from `tabMaterial Request Item` where item_code = '%s' and sales_order_no = '%s' and docstatus = 1 and parent != '%s'" % (item, so_no, self.doc.name)) already_indented = already_indented and flt( already_indented[0][0]) or 0 actual_so_qty = webnotes.conn.sql( "select sum(qty) from `tabSales Order Item` where parent = '%s' and item_code = '%s' and docstatus = 1 group by parent" % (so_no, item)) actual_so_qty = actual_so_qty and flt(actual_so_qty[0][0]) or 0 if flt(so_items[so_no] [item]) + already_indented > actual_so_qty: msgprint( "You can raise indent of maximum qty: %s for item: %s against sales order: %s\n Anyway, you can add more qty in new row for the same item." % (actual_so_qty - already_indented, item, so_no), raise_exception=1)
def clear_table(self, doclist, tablefield, save=0): """ Clears the child records from the given `doclist` for a particular `tablefield` """ from webnotes.model.utils import getlist table_list = getlist(doclist, tablefield) delete_list = [d.name for d in table_list] if delete_list: # filter doclist doclist = filter(lambda d: d.name not in delete_list, doclist) # delete from db webnotes.conn.sql( """\ delete from `tab%s` where parent=%s and parenttype=%s""" % (table_list[0].doctype, "%s", "%s"), (self.name, self.doctype), ) self.fields["__unsaved"] = 1 return doclist
def validate_qty_against_so(self): so_items = {} # Format --> {'SO/00001': {'Item/001': 120, 'Item/002': 24}} for d in getlist(self.doclist, 'indent_details'): if d.sales_order_no: if not so_items.has_key(d.sales_order_no): so_items[d.sales_order_no] = {d.item_code: flt(d.qty)} else: if not so_items[d.sales_order_no].has_key(d.item_code): so_items[d.sales_order_no][d.item_code] = flt(d.qty) else: so_items[d.sales_order_no][d.item_code] += flt(d.qty) for so_no in so_items.keys(): for item in so_items[so_no].keys(): already_indented = webnotes.conn.sql("""select sum(qty) from `tabMaterial Request Item` where item_code = %s and sales_order_no = %s and docstatus = 1 and parent != %s""", (item, so_no, self.doc.name)) already_indented = already_indented and flt(already_indented[0][0]) or 0 actual_so_qty = webnotes.conn.sql("""select sum(qty) from `tabSales Order Item` where parent = %s and item_code = %s and docstatus = 1 group by parent""", (so_no, item)) actual_so_qty = actual_so_qty and flt(actual_so_qty[0][0]) or 0 if actual_so_qty and (flt(so_items[so_no][item]) + already_indented > actual_so_qty): webnotes.throw("You can raise indent of maximum qty: %s for item: %s against sales order: %s\ \n Anyway, you can add more qty in new row for the same item." % (actual_so_qty - already_indented, item, so_no))
def get_last_purchase_rate(self, obj): """get last purchase rates for all items""" doc_name = obj.doc.name conversion_rate = flt(obj.doc.fields.get('conversion_rate')) or 1.0 for d in getlist(obj.doclist, obj.fname): if d.item_code: last_purchase_details = get_last_purchase_details( d.item_code, doc_name) if last_purchase_details: d.purchase_ref_rate = last_purchase_details[ 'purchase_ref_rate'] * (flt(d.conversion_factor) or 1.0) d.discount_rate = last_purchase_details['discount_rate'] d.purchase_rate = last_purchase_details[ 'purchase_rate'] * (flt(d.conversion_factor) or 1.0) d.import_ref_rate = d.purchase_ref_rate / conversion_rate d.import_rate = d.purchase_rate / conversion_rate else: # if no last purchase found, reset all values to 0 d.purchase_ref_rate = d.purchase_rate = d.import_ref_rate = d.import_rate = d.discount_rate = 0 item_last_purchase_rate = webnotes.conn.get_value( "Item", d.item_code, "last_purchase_rate") if item_last_purchase_rate: d.purchase_ref_rate = d.purchase_rate = d.import_ref_rate \ = d.import_rate = item_last_purchase_rate
def update_last_purchase_rate(self, obj, is_submit): """updates last_purchase_rate in item table for each item""" import webnotes.utils this_purchase_date = webnotes.utils.getdate(obj.doc.fields.get('posting_date') or obj.doc.fields.get('transaction_date')) for d in getlist(obj.doclist,obj.fname): # get last purchase details last_purchase_details = get_last_purchase_details(d.item_code, obj.doc.name) # compare last purchase date and this transaction's date last_purchase_rate = None if last_purchase_details and \ (last_purchase_details.purchase_date > this_purchase_date): last_purchase_rate = last_purchase_details['purchase_rate'] elif is_submit == 1: # even if this transaction is the latest one, it should be submitted # for it to be considered for latest purchase rate if flt(d.conversion_factor): last_purchase_rate = flt(d.purchase_rate) / flt(d.conversion_factor) else: msgprint(_("Row ") + cstr(d.idx) + ": " + _("UOM Conversion Factor is mandatory"), raise_exception=1) # update last purchsae rate if last_purchase_rate: webnotes.conn.sql("update `tabItem` set last_purchase_rate = %s where name = %s", (flt(last_purchase_rate),d.item_code))
def update_bin(self, is_submit, is_stopped): """ Update Quantity Requested for Purchase in Bin for Material Request of type 'Purchase'""" from stock.utils import update_bin for d in getlist(self.doclist, 'indent_details'): if webnotes.conn.get_value("Item", d.item_code, "is_stock_item") == "Yes": if not d.warehouse: msgprint( "Please Enter Warehouse for Item %s as it is stock item" % cstr(d.item_code), raise_exception=1) qty = flt(d.qty) if is_stopped: qty = (d.qty > d.ordered_qty ) and flt(flt(d.qty) - flt(d.ordered_qty)) or 0 args = { "item_code": d.item_code, "warehouse": d.warehouse, "indented_qty": (is_submit and 1 or -1) * flt(qty), "posting_date": self.doc.transaction_date } update_bin(args)
def validate_for_items(self, obj): check_list, chk_dupl_itm=[],[] for d in getlist( obj.doclist, obj.fname): # validation for valid qty if flt(d.qty) < 0 or (d.parenttype != 'Purchase Receipt' and not flt(d.qty)): msgprint("Please enter valid qty for item %s" % cstr(d.item_code)) raise Exception # udpate with latest quantities bin = webnotes.conn.sql("select projected_qty from `tabBin` where item_code = %s and warehouse = %s", (d.item_code, d.warehouse), as_dict = 1) f_lst ={'projected_qty': bin and flt(bin[0]['projected_qty']) or 0, 'ordered_qty': 0, 'received_qty' : 0} if d.doctype == 'Purchase Receipt Item': f_lst.pop('received_qty') for x in f_lst : if d.fields.has_key(x): d.fields[x] = f_lst[x] item = webnotes.conn.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=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) # 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=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 ''] # if is not stock item f = [d.schedule_date, d.item_code, d.description] ch = webnotes.conn.sql("select is_stock_item from `tabItem` where name = '%s'"%d.item_code) if ch and ch[0][0] == 'Yes': # check for same items if e in check_list: msgprint("""Item %s has been entered more than once with same description, schedule date, warehouse and uom.\n Please change any of the field value to enter the item twice""" % d.item_code, raise_exception = 1) else: check_list.append(e) elif ch and ch[0][0] == 'No': # check for same items if f in chk_dupl_itm: msgprint("""Item %s has been entered more than once with same description, schedule date.\n Please change any of the field value to enter the item twice.""" % d.item_code, raise_exception = 1) else: chk_dupl_itm.append(f)
def clear_table(self, doclist, tablefield, save=0): """ Clears the child records from the given `doclist` for a particular `tablefield` """ from webnotes.model.utils import getlist for d in getlist(doclist, tablefield): d.fields['__oldparent'] = d.parent d.parent = 'old_parent:' + d.parent # for client to send it back while saving d.docstatus = 2 if save and not d.fields.get('__islocal'): d.save() self.fields['__unsaved'] = 1
def update_bin(self, is_submit, is_stopped): """ Update Quantity Requested for Purchase in Bin for Material Request of type 'Purchase'""" from stock.utils import update_bin for d in getlist(self.doclist, 'indent_details'): if webnotes.conn.get_value("Item", d.item_code, "is_stock_item") == "Yes": if not d.warehouse: msgprint("Please Enter Warehouse for Item %s as it is stock item" % cstr(d.item_code), raise_exception=1) qty =flt(d.qty) if is_stopped: qty = (d.qty > d.ordered_qty) and flt(flt(d.qty) - flt(d.ordered_qty)) or 0 args = { "item_code": d.item_code, "warehouse": d.warehouse, "indented_qty": (is_submit and 1 or -1) * flt(qty), "posting_date": self.doc.transaction_date } update_bin(args)
def check_duplicate(self, finder=0): il = getlist(self.doclist, "sales_bom_items") if not il: webnotes.msgprint("Add atleast one item") return # get all Sales BOM that have the first item sbl = webnotes.conn.sql("""select distinct parent from `tabSales BOM Item` where item_code=%s and parent != %s and docstatus != 2""", (il[0].item_code, self.doc.name)) # check all siblings sub_items = [[d.item_code, flt(d.qty)] for d in il] for s in sbl: t = webnotes.conn.sql("""select item_code, qty from `tabSales BOM Item` where parent=%s and docstatus != 2""", s[0]) t = [[d[0], flt(d[1])] for d in t] if self.has_same_items(sub_items, t): webnotes.msgprint("%s has the same Sales BOM details" % s[0]) raise Exception if finder: webnotes.msgprint("There is no Sales BOM present with the following Combination.")
def clear_table(self, doclist, tablefield, save=0): """ Clears the child records from the given `doclist` for a particular `tablefield` """ from webnotes.model.utils import getlist table_list = getlist(doclist, tablefield) delete_list = [d.name for d in table_list] if delete_list: #filter doclist doclist = filter(lambda d: d.name not in delete_list, doclist) # delete from db webnotes.conn.sql( """\ delete from `tab%s` where parent=%s and parenttype=%s""" % (table_list[0].doctype, '%s', '%s'), (self.name, self.doctype)) self.fields['__unsaved'] = 1 return doclist
def get_last_purchase_rate(self, obj): """get last purchase rates for all items""" doc_name = obj.doc.name conversion_rate = flt(obj.doc.fields.get('conversion_rate')) or 1.0 for d in getlist(obj.doclist, obj.fname): if d.item_code: last_purchase_details = get_last_purchase_details(d.item_code, doc_name) if last_purchase_details: d.purchase_ref_rate = last_purchase_details['purchase_ref_rate'] * (flt(d.conversion_factor) or 1.0) d.discount_rate = last_purchase_details['discount_rate'] d.purchase_rate = last_purchase_details['purchase_rate'] * (flt(d.conversion_factor) or 1.0) d.import_ref_rate = d.purchase_ref_rate / conversion_rate d.import_rate = d.purchase_rate / conversion_rate else: # if no last purchase found, reset all values to 0 d.purchase_ref_rate = d.purchase_rate = d.import_ref_rate = d.import_rate = d.discount_rate = 0 item_last_purchase_rate = webnotes.conn.get_value("Item", d.item_code, "last_purchase_rate") if item_last_purchase_rate: d.purchase_ref_rate = d.purchase_rate = d.import_ref_rate \ = d.import_rate = item_last_purchase_rate
def validate_for_items(self, obj): check_list, chk_dupl_itm=[],[] for d in getlist( obj.doclist, obj.fname): # validation for valid qty if flt(d.qty) < 0 or (d.parenttype != 'Purchase Receipt' and not flt(d.qty)): msgprint("Please enter valid qty for item %s" % cstr(d.item_code)) raise Exception # udpate with latest quantities bin = webnotes.conn.sql("select projected_qty from `tabBin` where item_code = %s and warehouse = %s", (d.item_code, d.warehouse), as_dict = 1) f_lst ={'projected_qty': bin and flt(bin[0]['projected_qty']) or 0, 'ordered_qty': 0, 'received_qty' : 0} if d.doctype == 'Purchase Receipt Item': f_lst.pop('received_qty') for x in f_lst : if d.fields.has_key(x): d.fields[x] = f_lst[x] item = webnotes.conn.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=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) # 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=True) if d.fields.has_key('prevdoc_docname') and d.prevdoc_docname: # check warehouse, uom in previous doc and in current doc are same. data = sql("select item_code, warehouse, uom from `tab%s` where name = '%s'" % ( self.doctype_dict[d.prevdoc_doctype], d.prevdoc_detail_docname), as_dict = 1) if not data: msgprint("Please fetch data in Row " + cstr(d.idx) + " once again or please contact Administrator.") raise Exception # Check if Item Code has been modified. if not cstr(data[0]['item_code']) == cstr(d.item_code): msgprint("Please check Item %s is not present in %s %s ." % (d.item_code, d.prevdoc_doctype, d.prevdoc_docname)) raise Exception if cstr(data[0]['warehouse']) and \ 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), raise_exception=1) # 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=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 ''] # if is not stock item f = [d.schedule_date, d.item_code, d.description] ch = webnotes.conn.sql("select is_stock_item from `tabItem` where name = '%s'"%d.item_code) if ch and ch[0][0] == 'Yes': # check for same items if e in check_list: msgprint("""Item %s has been entered more than once with same description, schedule date, warehouse and uom.\n Please change any of the field value to enter the item twice""" % d.item_code, raise_exception = 1) else: check_list.append(e) elif ch and ch[0][0] == 'No': # check for same items if f in chk_dupl_itm: msgprint("""Item %s has been entered more than once with same description, schedule date.\n Please change any of the field value to enter the item twice.""" % d.item_code, raise_exception = 1) else: chk_dupl_itm.append(f)
def validate_schedule_date(self): for d in getlist(self.doclist, 'indent_details'): if d.schedule_date < self.doc.transaction_date: webnotes.throw(_("Expected Date cannot be before Material Request Date"))
def validate_schedule_date(self): for d in getlist(self.doclist, 'indent_details'): if d.schedule_date < self.doc.transaction_date: msgprint( "Expected Date cannot be before Material Request Date") raise Exception
def validate_schedule_date(self): for d in getlist(self.doclist, 'indent_details'): if d.schedule_date < self.doc.transaction_date: msgprint("Expected Date cannot be before Material Request Date") raise Exception