Ad

Decrement Value Database If Availability Greater Of Request

- 1 answer

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:

idid_wineqtyid_order
1141
2112

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);

Ad

Answer

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;
       }
   }
}
Ad
source: stackoverflow.com
Ad