def validate_multiple_billing(self, ref_dt, item_ref_dn, based_on, parentfield): from erpnext.controllers.status_updater import get_tolerance_for item_tolerance = {} global_tolerance = None for item in self.get("items"): if item.get(item_ref_dn): ref_amt = flt(frappe.db.get_value(ref_dt + " Item", item.get(item_ref_dn), based_on), self.precision(based_on, item)) if not ref_amt: frappe.msgprint(_("Warning: System will not check overbilling since amount for Item {0} in {1} is zero").format(item.item_code, ref_dt)) else: already_billed = frappe.db.sql("""select sum(%s) from `tab%s` where %s=%s and docstatus=1 and parent != %s""" % (based_on, self.doctype + " Item", item_ref_dn, '%s', '%s'), (item.get(item_ref_dn), self.name))[0][0] total_billed_amt = flt(flt(already_billed) + flt(item.get(based_on)), self.precision(based_on, item)) tolerance, item_tolerance, global_tolerance = get_tolerance_for(item.item_code, item_tolerance, global_tolerance) max_allowed_amt = flt(ref_amt * (100 + tolerance) / 100) if total_billed_amt - max_allowed_amt > 0.01: frappe.throw(_("Cannot overbill for Item {0} in row {1} more than {2}. To allow overbilling, please set in Stock Settings").format(item.item_code, item.idx, max_allowed_amt))
def validate_multiple_billing(self, ref_dt, item_ref_dn, based_on, parentfield): from erpnext.controllers.status_updater import get_tolerance_for item_tolerance = {} global_tolerance = None for item in self.doclist.get({"parentfield": "entries"}): if item.fields.get(item_ref_dn): ref_amt = flt( frappe.db.get_value(ref_dt + " Item", item.fields[item_ref_dn], based_on), self.precision(based_on, item)) if not ref_amt: frappe.msgprint( _("As amount for item") + ": " + item.item_code + _(" in ") + ref_dt + _(" is zero, system will not check for over-billed")) else: already_billed = frappe.db.sql( """select sum(%s) from `tab%s` where %s=%s and docstatus=1 and parent != %s""" % (based_on, self.tname, item_ref_dn, '%s', '%s'), (item.fields[item_ref_dn], self.doc.name))[0][0] total_billed_amt = flt( flt(already_billed) + flt(item.fields[based_on]), self.precision(based_on, item)) tolerance, item_tolerance, global_tolerance = get_tolerance_for( item.item_code, item_tolerance, global_tolerance) max_allowed_amt = flt(ref_amt * (100 + tolerance) / 100) if total_billed_amt - max_allowed_amt > 0.01: reduce_by = total_billed_amt - max_allowed_amt frappe.throw( _("Row #") + cstr(item.idx) + ": " + _(" Max amount allowed for Item ") + cstr(item.item_code) + _(" against ") + ref_dt + " " + cstr(item.fields[ref_dt.lower().replace( " ", "_")]) + _(" is ") + cstr(max_allowed_amt) + ". \n" + _("""If you want to increase your overflow tolerance, please increase \ tolerance % in Global Defaults or Item master. Or, you must reduce the amount by """) + cstr(reduce_by) + "\n" + _("""Also, please check if the order item has already been billed \ in the Sales Order"""))