收银台下单库存校验

This commit is contained in:
liu 2024-07-02 16:33:09 +08:00
parent 23bf1ecee8
commit 77c14d6508
5 changed files with 80 additions and 7 deletions

View File

@ -394,5 +394,53 @@ class StoreOrderLogic extends BaseLogic
}
//检查缺失
public static function checkLeft($params, $uid, $type = 0)
{
$where = [];
if (empty($type)) {
$where = ['is_pay' => 0];
}
$cart_select = Cart::whereIn('id', $params['cart_id'])
->where($where)->field('id,product_id,cart_num,store_id')->select()->toArray();
if (empty($cart_select)) {
self::setError('购物车为空');
return false;
}
$newArr = [];
//检查购物车对比店铺得商品数量差异
foreach ($cart_select as $v) {
$store = StoreBranchProduct::where([
'store_id' => $params['store_id'],
'product_id' => $v['product_id'],
])->field('id,store_name,stock')->withTrashed()->find();
if (empty($store)) {
$store['stock'] = 0;
}
if ($store['stock'] < $v['cart_num']) {
//缺失
$newArr[] = [
'uid' => $uid,
'store_id' => $params['store_id'],
'product_id' => $v['product_id'],
'missing_quantity' => $v['cart_num'] - $store['stock']
];
}
}
if ($newArr) {
return [
'detail' => $newArr,
'reservation' => 1
];
}
return [
'detail' => [],
'reservation' => 0
];
}
}

View File

@ -42,13 +42,13 @@ class CartController extends BaseAdminController
return $this->fail('购物车商品不能大于100个请先结算');
}
//数量下单判断
$stock = StoreBranchProduct::where(
['product_id'=>$params['product_id'],
'store_id'=>$params['store_id']
])->value('stock')??0;
if ($params['cart_num'] >$stock) {
return $this->fail('库存数量不足');
}
// $stock = StoreBranchProduct::where(
// ['product_id'=>$params['product_id'],
// 'store_id'=>$params['store_id']
// ])->value('stock')??0;
// if ($params['cart_num'] >$stock) {
// return $this->fail('库存数量不足');
// }
if ($result) {
$res = CartLogic::edit($params);
} else {

View File

@ -132,6 +132,23 @@ class StoreOrderController extends BaseAdminController
}
}
//检查库存足够与否
public function checkInventory()
{
$params = (new StoreOrderValidate())->post()->goCheck('cart');
$params['store_id'] = $this->request->adminInfo['store_id']??22;
$res = StoreOrderLogic::checkLeft($params, $params['uid']);
if (!$res) {
$msg = StoreOrderLogic::getError();
if ($msg == '购物车为空') {
return $this->data([]);
}
return $this->fail(StoreOrderLogic::getError());
}
return $this->data($res);
}
/**
* 创建订单

View File

@ -3,6 +3,8 @@
namespace app\store\logic\store_order;
use app\common\model\order\Cart;
use app\common\model\store_branch_product\StoreBranchProduct;
use app\common\model\store_order\StoreOrder;
use app\common\logic\BaseLogic;
use think\facade\Db;

View File

@ -35,9 +35,15 @@ class StoreOrderValidate extends BaseValidate
'id' => 'id',
'verify_code' => '核销码',
'type' => '发送短信类型',
'cart_id' => '购物车id',
];
public function sceneCart()
{
return $this->only(['cart_id']);
}
/**
* @notes 添加场景
* @return StoreOrderValidate