Merge branch 'staging-fixes' of https://github.com/frappe/erpnext into update_items_bug
diff --git a/erpnext/controllers/accounts_controller.py b/erpnext/controllers/accounts_controller.py
index 86ceb2e..fe6b39d 100644
--- a/erpnext/controllers/accounts_controller.py
+++ b/erpnext/controllers/accounts_controller.py
@@ -1073,6 +1073,13 @@
 	data = json.loads(trans_items)
 	for d in data:
 		child_item = frappe.get_doc(parent_doctype + ' Item', d.get("docname"))
+
+		if parent_doctype == "Sales Order" and flt(d.get("qty")) < child_item.delivered_qty:
+			frappe.throw(_("Cannot set quantity less than delivered quantity"))
+
+		if parent_doctype == "Purchase Order" and flt(d.get("qty")) < child_item.received_qty:
+			frappe.throw(_("Cannot set quantity less than received quantity"))
+
 		child_item.qty = flt(d.get("qty"))
 
 		if child_item.billed_amt > (flt(d.get("rate")) * flt(d.get("qty"))):