如果请求的可用性更大,则递减值数据库
decrement value database if availability greater of request
有一种方法可以通过查询来缩放产品的数量,该数量可以在 table 上加倍,因为它来自 2 个不同的订单,同时考虑到客户的要求和每种产品的可用数量
例如,我有 2 种相同 id_wine 的葡萄酒,但来自 2 个不同的订单,因此它们具有不同的 id_order,它们的可用性为:
id
id_wine
qty
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);
我不知道你是否可以这样做,但如果我是你,我会获取所有数据并遍历它并仅减少它的库存数量,假设你会在执行操作之前验证库存.
$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
}
如果以后有人需要,我已经解决了我的问题:
$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;
}
}
}
有一种方法可以通过查询来缩放产品的数量,该数量可以在 table 上加倍,因为它来自 2 个不同的订单,同时考虑到客户的要求和每种产品的可用数量
例如,我有 2 种相同 id_wine 的葡萄酒,但来自 2 个不同的订单,因此它们具有不同的 id_order,它们的可用性为:
id | id_wine | qty | 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);
我不知道你是否可以这样做,但如果我是你,我会获取所有数据并遍历它并仅减少它的库存数量,假设你会在执行操作之前验证库存.
$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
}
如果以后有人需要,我已经解决了我的问题:
$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;
}
}
}