From e1a51926042a07c2f438af6390371c9e382131b3 Mon Sep 17 00:00:00 2001
From: Oliver <oliver.henry.walters@gmail.com>
Date: Sat, 11 Dec 2021 00:45:53 +1100
Subject: [PATCH] Calculate how many items to assign to sales order

- Do not over-allocate by default
---
 InvenTree/templates/js/translated/order.js | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/InvenTree/templates/js/translated/order.js b/InvenTree/templates/js/translated/order.js
index fefa82475c..008091bf15 100644
--- a/InvenTree/templates/js/translated/order.js
+++ b/InvenTree/templates/js/translated/order.js
@@ -1658,7 +1658,7 @@ function allocateStockToSalesOrder(order_id, line_items, options={}) {
                             var available = Math.max((data.quantity || 0) - (data.allocated || 0), 0);
 
                             // Remaining quantity to be allocated?
-                            var remaining = opts.quantity || available;
+                            var remaining = Math.max(line_item.quantity - line_item.shipped - line_item.allocated, 0);
 
                             // Maximum amount that we need
                             var desired = Math.min(available, remaining);