diff --git a/app/common/logic/store_order/StoreOrderLogic.php b/app/common/logic/store_order/StoreOrderLogic.php index 94f657b5..1ae87783 100644 --- a/app/common/logic/store_order/StoreOrderLogic.php +++ b/app/common/logic/store_order/StoreOrderLogic.php @@ -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 + ]; + } + + + } diff --git a/app/store/controller/cart/CartController.php b/app/store/controller/cart/CartController.php index ae4587ee..97666dd8 100644 --- a/app/store/controller/cart/CartController.php +++ b/app/store/controller/cart/CartController.php @@ -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 { diff --git a/app/store/controller/store_order/StoreOrderController.php b/app/store/controller/store_order/StoreOrderController.php index 87f41a51..03f2eaad 100644 --- a/app/store/controller/store_order/StoreOrderController.php +++ b/app/store/controller/store_order/StoreOrderController.php @@ -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); + } + + /** * 创建订单 diff --git a/app/store/logic/store_order/StoreOrderLogic.php b/app/store/logic/store_order/StoreOrderLogic.php index f8808002..80df42e0 100644 --- a/app/store/logic/store_order/StoreOrderLogic.php +++ b/app/store/logic/store_order/StoreOrderLogic.php @@ -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; diff --git a/app/store/validate/store_order/StoreOrderValidate.php b/app/store/validate/store_order/StoreOrderValidate.php index adf8b8f9..df8c18a7 100644 --- a/app/store/validate/store_order/StoreOrderValidate.php +++ b/app/store/validate/store_order/StoreOrderValidate.php @@ -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