@@ -1194,7 +1194,7 @@ private function handleStockAdjustmentsForGroupedProducts(
1194
1194
$ orderProductQuantity ,
1195
1195
Product $ product ,
1196
1196
Unit $ parentUnit ,
1197
- ? OrderProduct $ orderProduct = null ): EloquentCollection
1197
+ OrderProduct $ orderProduct = null ): EloquentCollection
1198
1198
{
1199
1199
$ product ->load ('sub_items ' );
1200
1200
@@ -1386,20 +1386,45 @@ private function handleStockAdjustmentRegularProducts($action, $quantity, $produ
1386
1386
} elseif (
1387
1387
in_array ($ action , [ ProductHistory::ACTION_SET ])
1388
1388
) {
1389
- $ this ->setQuantity ($ product_id , $ unit_id , $ quantity );
1389
+ $ currentQuantity = $ this ->getQuantity (
1390
+ product_id: $ product_id ,
1391
+ unit_id: $ unit_id
1392
+ );
1393
+
1394
+ if ($ currentQuantity < $ quantity ) {
1395
+ $ action = ProductHistory::ACTION_ADDED ;
1396
+ $ adjustQuantity = $ quantity - $ currentQuantity ;
1397
+
1398
+ $ this ->increaseUnitQuantities (
1399
+ product_id: $ product_id ,
1400
+ unit_id: $ unit_id ,
1401
+ quantity: $ adjustQuantity ,
1402
+ oldQuantity: $ currentQuantity
1403
+ );
1404
+ } elseif ($ currentQuantity > $ quantity ) {
1405
+ $ action = ProductHistory::ACTION_REMOVED ;
1406
+ $ adjustQuantity = $ currentQuantity - $ quantity ;
1407
+
1408
+ $ this ->reduceUnitQuantities (
1409
+ product_id: $ product_id ,
1410
+ unit_id: $ unit_id ,
1411
+ quantity: $ adjustQuantity ,
1412
+ oldQuantity: $ currentQuantity
1413
+ );
1414
+ }
1390
1415
1391
1416
return $ this ->recordStockHistory (
1392
1417
product_id: $ product_id ,
1393
1418
action: $ action ,
1394
1419
unit_id: $ unit_id ,
1395
1420
unit_price: $ unit_price ,
1396
- quantity: $ quantity ,
1421
+ quantity: $ adjustQuantity ,
1397
1422
total_price: $ total_price ,
1398
1423
procurement_product_id: $ procurementProduct ?->id ?: null ,
1399
1424
procurement_id: $ procurementProduct ->procurement_id ?? null ,
1400
1425
order_id: isset ($ orderProduct ) ? $ orderProduct ->order_id : null ,
1401
1426
order_product_id: isset ($ orderProduct ) ? $ orderProduct ->id : null ,
1402
- old_quantity: $ oldQuantity ,
1427
+ old_quantity: $ currentQuantity ,
1403
1428
new_quantity: $ quantity
1404
1429
);
1405
1430
}
@@ -1670,7 +1695,7 @@ public function deleteAllProducts()
1670
1695
* Will return the last purchase price
1671
1696
* defined for the provided product
1672
1697
*/
1673
- public function getLastPurchasePrice (?Product $ product , Unit $ unit , ? string $ before = null ): float |int
1698
+ public function getLastPurchasePrice (?Product $ product , Unit $ unit , string $ before = null ): float |int
1674
1699
{
1675
1700
if ($ product instanceof Product) {
1676
1701
$ request = ProcurementProduct::where ('product_id ' , $ product ->id )
@@ -1836,7 +1861,7 @@ public function generateProductBarcode(Product $product)
1836
1861
* Convert quantity from a source unit ($from) to a destination unit ($to)
1837
1862
* using the provided quantity and product.
1838
1863
*/
1839
- public function convertUnitQuantities (Product $ product , Unit $ from , float $ quantity , Unit $ to , ? ProcurementProduct $ procurementProduct = null ): array
1864
+ public function convertUnitQuantities (Product $ product , Unit $ from , float $ quantity , Unit $ to , ProcurementProduct $ procurementProduct = null ): array
1840
1865
{
1841
1866
if ($ product ->stock_management !== Product::STOCK_MANAGEMENT_ENABLED ) {
1842
1867
throw new NotAllowedException (__ ('You cannot convert unit on a product having stock management disabled. ' ));
0 commit comments