From bdc63fc4e4953db48ad4663d1803fdbd9543e5cf Mon Sep 17 00:00:00 2001 From: blair2004 Date: Wed, 26 Jan 2022 18:06:41 +0000 Subject: [PATCH] Added: Stock Adjustment Security --- .../Dashboard/ProductsController.php | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/app/Http/Controllers/Dashboard/ProductsController.php b/app/Http/Controllers/Dashboard/ProductsController.php index ba4420d02..bf5971a19 100755 --- a/app/Http/Controllers/Dashboard/ProductsController.php +++ b/app/Http/Controllers/Dashboard/ProductsController.php @@ -493,6 +493,27 @@ public function createAdjustment( Request $request ) ) { throw new Exception( sprintf( __( 'Unsupported action for the product %s.' ), $unit[ 'name' ] ) ); } + + /** + * let's check for every operation if there is enough inventory + */ + $productUnitQuantity = ProductUnitQuantity::where( 'product_id', $unit[ 'id' ]) + ->where( 'unit_id', $unit[ 'adjust_unit' ][ 'unit_id' ] ) + ->first(); + + if ( $productUnitQuantity instanceof ProductUnitQuantity && in_array( $unit[ 'adjust_action' ], ProductHistory::STOCK_REDUCE ) ) { + $remaining = $productUnitQuantity->quantity - ( float ) $unit[ 'adjust_quantity' ]; + + if ( $remaining < 0 ) { + throw new NotAllowedException( + sprintf( + __( 'The operation will cause a negative stock for the product "%s" (%s).' ), + $productUnitQuantity->product->name, + $remaining + ) + ); + } + } } /**