Skip to content

Commit

Permalink
Added: Stock Adjustment Security
Browse files Browse the repository at this point in the history
  • Loading branch information
Blair2004 committed Jan 26, 2022
1 parent 6444a31 commit bdc63fc
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions app/Http/Controllers/Dashboard/ProductsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
);
}
}
}

/**
Expand Down

0 comments on commit bdc63fc

Please sign in to comment.