Home > OS >  decrement value database if availability greater of request
decrement value database if availability greater of request

Time:02-19

there is a way through query to scale the quantity of a product that can be double on the table because it comes from 2 different orders taking into account the customer's request and the quantity available for each product

For example, I have 2 wines with the same id_wine but coming from 2 different orders, so they have a different id_order, where they have as availability:

id id_wine qty id_order
1 1 4 1
2 1 1 2

Total qty 5

If I order 5 wines, is there a way to be able to scale the counter of the first wine by 4 units and the last one to scale it from the other wine?

I can scale if I place single orders, so as long as the availability is> 0 I scale from the first, if <switch to 2 wine, but if I place an order with 5 wines all together, I scale only the first one, bringing the units to -1.

I'm doing this at the moment:

$scaleqty=warehouse::where('id_restaurant',Auth::user()->id_restaurant)
->where('id_wine',$wine_id)
->whereRaw('quantita_restante > 0')
->orderBy('id_order')
->first()
->decrement('quantita_restante',$request->quantita);

CodePudding user response:

I don't know if you can do it this way but if i were you i would get all the data and loop over it and decrement only the quantity it has in stock assuming you would validate the stock before performing operation.

$quantity = $request->quantita;

$stocks = warehouse::where('id_restaurant',Auth::user()->id_restaurant)
->where('id_wine',$wine_id)
->whereRaw('quantita_restante > 0')
->orderBy('id_order')
->get();

foreach($stocks as $stock){
 if($stock->qty < $quantity){
  $stock->decrement($stock->qty);
  $quantity = $quantity-$stock->qty;
 }
}

This is just for the part where the quantity of order is greater than the greatest quantity of stock in your database which you can check by ordering the data in descending order of quantity and check first and if it is greater than the requested order quantity you can subtract from that stock so you don't have to do this calculation.

$stock = warehouse::where('id_restaurant',Auth::user()->id_restaurant)
->where('id_wine',$wine_id)
->orderBy('qty','desc')
->first();

if($stock->qty > $request->quantita){
$stock->decrement($request->quantita)
}else{
  //code from above make sure to check you stop the loop once the request varaible becomes zero
}
  

CodePudding user response:

if someone needs it in the future, I have solved my problem:

$selectwine=warehouse::where('id_restaurant',Auth::user()->id_restaurant)
->where('id_wine',$wine_id)->whereRaw('quantita_restante > 0')
->orderBy('id_order')
->get();
$qta=$request->quantita;
foreach ($selectwine as $key => $value) {
   if ($value->quantita_restante <= $qta) {
       $qtarest=$value->quantita_restante;                
       $qta=$qta-$value->quantita_restante;
       $value->decrement('quantita_restante',$qtarest);                
   }elseif($value->quantita_restante > $qta){
       $value->decrement('quantita_restante',$qta);
       if($qta=0){
          break;
       }
   }
}
  • Related