@@ -935,14 +935,16 @@ public function stockAdjustment( $action, $data )
935935 * @param id unit_id
936936 * @param float $unit_price
937937 * @param float total_price
938+ * @param int procurement_product_id
938939 * @param string $description
939940 * @param float quantity
940941 * @param string sku
941942 * @param string $unit_identifier
942943 */
943- $ product = isset ( $ product_id ) ? Product::findOrFail ( $ product_id ) : Product::usingSKU ( $ sku )->first ();
944- $ product_id = $ product ->id ;
945- $ unit_id = isset ( $ unit_id ) ? $ unit_id : Unit::identifier ( $ unit_identifier )->firstOrFail ()->id ;
944+ $ product = isset ( $ product_id ) ? Product::findOrFail ( $ product_id ) : Product::usingSKU ( $ sku )->first ();
945+ $ product_id = $ product ->id ;
946+ $ unit_id = isset ( $ unit_id ) ? $ unit_id : Unit::identifier ( $ unit_identifier )->firstOrFail ()->id ;
947+ $ procurementProduct = isset ( $ procurement_product_id ) ? ProcurementProduct::find ( $ procurement_product_id ) : false ;
946948
947949 /**
948950 * let's check the different
@@ -1011,6 +1013,15 @@ public function stockAdjustment( $action, $data )
10111013 * @var array [ 'oldQuantity', 'newQuantity' ]
10121014 */
10131015 $ result = $ this ->reduceUnitQuantities ( $ product_id , $ unit_id , abs ( $ quantity ), $ oldQuantity );
1016+
1017+ /**
1018+ * We should reduce the quantity if
1019+ * we're dealing with a product that has
1020+ * accurate stock tracking
1021+ */
1022+ if ( $ procurementProduct instanceof ProcurementProduct ) {
1023+ $ this ->updateProcurementProductQuantity ( $ procurementProduct , $ quantity , ProcurementProduct::STOCK_REDUCE );
1024+ }
10141025 } else {
10151026
10161027 /**
@@ -1019,6 +1030,15 @@ public function stockAdjustment( $action, $data )
10191030 * @var array [ 'oldQuantity', 'newQuantity' ]
10201031 */
10211032 $ result = $ this ->increaseUnitQuantities ( $ product_id , $ unit_id , abs ( $ quantity ), $ oldQuantity );
1033+
1034+ /**
1035+ * We should reduce the quantity if
1036+ * we're dealing with a product that has
1037+ * accurate stock tracking
1038+ */
1039+ if ( $ procurementProduct instanceof ProcurementProduct ) {
1040+ $ this ->updateProcurementProductQuantity ( $ procurementProduct , $ quantity , ProcurementProduct::STOCK_INCREASE );
1041+ }
10221042 }
10231043 }
10241044
@@ -1043,6 +1063,23 @@ public function stockAdjustment( $action, $data )
10431063 return $ history ;
10441064 }
10451065
1066+ /**
1067+ * Update procurement product quantity
1068+ * @param ProcurementProduct $procurementProduct
1069+ * @param int $quantity
1070+ * @param string $action
1071+ */
1072+ public function updateProcurementProductQuantity ( $ procurementProduct , $ quantity , $ action )
1073+ {
1074+ if ( $ action === ProcurementProduct::STOCK_INCREASE ) {
1075+ $ procurementProduct ->available_quantity += $ quantity ;
1076+ } else if ( $ action === ProcurementProduct::STOCK_REDUCE ) {
1077+ $ procurementProduct ->available_quantity -= $ quantity ;
1078+ }
1079+
1080+ $ procurementProduct ->save ();
1081+ }
1082+
10461083 /**
10471084 * reduce Product unit quantities and update
10481085 * the available quantity for the unit provided
0 commit comments