有一種方法可以通過查詢來縮放產品的數量,該產品的數量可以在桌子上加倍,因為它來自 2 個不同的訂單,考慮到客戶的要求和每種產品的可用數量
例如,我有 2 種具有相同 id_wine 但來自 2 個不同訂單的葡萄酒,因此它們具有不同的 id_order,它們具有可用性:
| ID | id_wine | 數量 | id_order |
|---|---|---|---|
| 1 | 1 | 4 | 1 |
| 2 | 1 | 1 | 2 |
總數量 5
如果我點了 5 種酒,有沒有辦法可以將第一種酒的柜臺縮放 4 個單位,最后一個從另一種酒中縮放?
如果我下單個訂單,我可以擴展,所以只要可用性> 0 我從第一個擴展,如果 <切換到 2 種葡萄酒,但如果我將 5 種酒一起下訂單,我只擴展第一個,使單位為-1。
我現在正在這樣做:
$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);
uj5u.com熱心網友回復:
我不知道你是否可以這樣做,但如果我是你,我會獲取所有資料并回圈遍歷它,并且只減少它的庫存數量,假設你會在執行操作之前驗證庫存。
$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;
}
}
這只是針對訂單數量大于資料庫中最大庫存數量的部分,您可以通過按數量降序排列資料并首先檢查,如果大于請求的訂單數量,您可以從該股票中減去,因此您不必進行此計算。
$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
}
uj5u.com熱心網友回復:
如果將來有人需要它,我已經解決了我的問題:
$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;
}
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/428849.html
