diff --git a/app/common/repositories/store/order/StoreOrderCreateRepository.php b/app/common/repositories/store/order/StoreOrderCreateRepository.php index 15d09ac1..0f389309 100644 --- a/app/common/repositories/store/order/StoreOrderCreateRepository.php +++ b/app/common/repositories/store/order/StoreOrderCreateRepository.php @@ -1,1166 +1,1166 @@ -uid; - $this->balance = 0; - $userIntegral = $user->integral; - $key = md5(json_encode(compact('cartId', 'takes', 'useCoupon', 'useIntegral', 'addressId'))) . $uid; - app()->make(StoreCouponUserRepository::class)->failCoupon(); - $address = null; - //验证地址 - if ($addressId) { - $addressRepository = app()->make(UserAddressRepository::class); - $address = $addressRepository->getWhere(['uid' => $uid, 'address_id' => $addressId]); - } - $storeCartRepository = app()->make(StoreCartRepository::class); - $res = $storeCartRepository->checkCartList($storeCartRepository->cartIbByData($cartId, $uid, $address), 0, $user); - $merchantCartList = $res['list']; - $fail = $res['fail']; - - //检查购物车失效数据 - if (count($fail)) { - if ($fail[0]['is_fail']) - throw new ValidateException('[已失效]' . mb_substr($fail[0]['product']['store_name'], 0, 10) . '...'); - if (in_array($fail[0]['product_type'], [1, 2, 3]) && !$fail[0]['userPayCount']) { - throw new ValidateException('[超出限购数]' . mb_substr($fail[0]['product']['store_name'], 0, 10) . '...'); - } - throw new ValidateException('[已失效]' . mb_substr($fail[0]['product']['store_name'], 0, 10) . '...'); - } - - $svip_status = $user->is_svip > 0 && systemConfig('svip_switch_status') == '1'; - $svip_integral_rate = $svip_status ? app()->make(MemberinterestsRepository::class)->getSvipInterestVal(MemberinterestsRepository::HAS_TYPE_PAY) : 0; - //订单活动类型 - $order_type = 0; - $source = 2; - //虚拟订单 - $order_model = 0; - //虚拟订单自定义数据 - $order_extend = []; - //检查商品类型, 活动商品只能单独购买 - $allowDelivery = true; - foreach ($merchantCartList as $merchantCart) { - - if (($merchantCart['type_id'] != Merchant::TypeSupplyChain) && $address) { - if ($merchantCart['street_id'] != $address['street_code']) { - throw new ValidateException('不支持跨区域购买,请在【我的】-【地址管理】更改后重新购买'); - } - } - foreach ($merchantCart['list'] as $cart) { - if ($cart['product_type'] == 0) { - if ($cart['product']['once_min_count'] > 0 && $cart['product']['once_min_count'] > $cart['cart_num']) - throw new ValidateException('[低于起购数:' . $cart['product']['once_min_count'] . ']' . mb_substr($cart['product']['store_name'], 0, 10) . '...'); - if ($cart['product']['pay_limit'] == 1 && $cart['product']['once_max_count'] < $cart['cart_num']) - throw new ValidateException('[超出单次限购数:' . $cart['product']['once_max_count'] . ']' . mb_substr($cart['product']['store_name'], 0, 10) . '...'); - if ($cart['product']['pay_limit'] == 2) { - //如果长期限购 - //已购买数量 - $count = app()->make(StoreOrderRepository::class)->getMaxCountNumber($cart['uid'], $cart['product_id']); - if (($cart['cart_num'] + $count) > $cart['product']['once_max_count']) - throw new ValidateException('[超出限购总数:' . $cart['product']['once_max_count'] . ']' . mb_substr($cart['product']['store_name'], 0, 10) . '...'); - } - } - if ($cart['product_type'] > 0) { - $order_type = $cart['product_type']; - } - $source = $cart['source']; - if ($cart['product_type'] <= 97 && $cart['product_type'] > 0 && (($cart['product_type'] != 10 && count($merchantCart['list']) != 1) || count($merchantCartList) != 1)) { - throw new ValidateException('活动商品必须单独购买'); - } - if ($cart['product']['type'] && (count($merchantCart['list']) != 1 || count($merchantCartList) != 1)) { - throw new ValidateException('虚拟商品必须单独购买'); - } - $order_model = $cart['product']['type']; - if ($cart['product']['extend']) { - $order_extend = json_decode($cart['product']['extend'], true); - } - if ($address) { - if ($cart['source'] == 0 || $cart['source'] == 103) { - $userAddressCode = ($address['province_code'] ?? '') . ',' . ($address['city_code'] ?? '') . ',' . ($address['district_code'] ?? '') . ',' . ($address['street_code'] ?? '') . ',' . ($address['village_code'] ?? '') . ',' . ($address['brigade_id'] ?? 0); - $getUrl = env('LOGISTICS_HOST_URL') . '/api/hasCourier?user_address_code=' . $userAddressCode; - $curl = new Curl(); - $response = $curl->get($getUrl); - $courierData = json_decode($response, true); - if (empty($courierData['data']['user_id'])) { - $allowDelivery = false; - } - } - } - } - } - // if (($order_type == 98 || $order_type == 99) && count($merchantCartList) > 1) { - // throw new ValidateException('采购、委托商品不支持跨店购买'); - // } - $community = []; - unset($merchantCart, $cart); - $order_price = 0; - $total_true_price = 0; - $order_total_price = 0; - $order_coupon_price = 0; - $noDeliver = false; - $presellType = 0; - $fn = []; - if (in_array($source, [0, 2, 103])) { - $enabledPlatformCoupon = true; - } else { - $enabledPlatformCoupon = false; - } - $order_total_postage = 0; - $platformCoupon = []; - - - //套餐订单 - if ($order_type == 10) { - app()->make(StoreDiscountRepository::class) - ->check($merchantCartList[0]['list'][0]['source_id'], $merchantCartList[0]['list'], $user); - } - $orderDeliveryStatus = true; - $order_svip_discount = 0; - - if ($consumption_id > 0) { - $this->store_consumption_user = Db::name('store_consumption_user')->where('coupon_user_id', $consumption_id)->where('uid', $uid)->find(); - $this->balance=$this->store_consumption_user['balance']; - } - // 循环计算每个店铺的订单数据 委托商品是否设置收货方式 ? - foreach ($merchantCartList as &$merchantCart) { - if ($order_type == 98 && !empty($deliverMethodArray)) { - $merchantCart['delivery_way'] = $deliverMethodArray; - } - $postageRule = []; - $total_price = 0; - $total_num = 0; - $valid_total_price = 0; - $postage_price = 0; - $product_price = []; - $final_price = 0; - $down_price = 0; - $total_svip_discount = 0; - - //是否自提 - $isTake = in_array($merchantCart['mer_id'], $takes ?? []); - - // if (!$createOrder && !$isTake) { - // $isTake = count($merchantCart['delivery_way']) == 1 && $merchantCart['delivery_way'][0] == '1'; - // } - - $merTake = in_array('1', $merchantCart['delivery_way'], true); - $merDelivery = (!$merchantCart['delivery_way'] || !count($merchantCart['delivery_way']) || in_array('2', $merchantCart['delivery_way'], true)); - $_merTake = $merTake; - $_merDelivery = $merDelivery; - $deliveryStatus = true; - if ($createOrder && $isTake && !$merTake) { - $deliveryStatus = false; - // throw new ValidateException('[仅支持快递配送]' . $merchantCart['mer_name']); - } - $product_cart = []; - - foreach ($merchantCart['list'] as $k => $cart) { - //获取订单类型, 活动商品单次只能购买一个 - if ($cart['product']['delivery_way']) { - $delivery_way = explode(',', $cart['product']['delivery_way']); - $proTake = in_array('1', $delivery_way, true); - $merTake = $merTake && $proTake; - $proDelivery = (!count($delivery_way) || in_array('2', $delivery_way, true)); - $merDelivery = $merDelivery && $proDelivery; - $merchantCart['list'][$k]['allow_take'] = true; - $merchantCart['list'][$k]['allow_delivery'] = $allowDelivery; - } else { - $merchantCart['list'][$k]['allow_take'] = true; - $merchantCart['list'][$k]['allow_delivery'] = $allowDelivery; - } - if ($createOrder && $isTake && !$merTake) { - $deliveryStatus = false; - // throw new ValidateException('[仅支持快递配送]' . $cart['product']['store_name']); - } - } - if (!$merDelivery && !$merTake) { - $deliveryStatus = false; - // throw new ValidateException('部分商品配送方式不一致,请单独下单'); - } - if ($deliveryStatus && !$isTake && ($merDelivery || $merTake)) { - $isTake = $merDelivery ? 0 : 1; - } - - unset($cart); - - $enabledCoupon = !($order_type && $order_type != 2); - - //只有预售和普通商品可以用优惠券 - if (!$enabledCoupon) { - $merchantCart['coupon'] = []; - } - $svip_coupon_merge = merchantConfig($merchantCart['mer_id'], 'svip_coupon_merge'); - $use_svip = 0; - //获取运费规则和统计商品数据 - foreach ($merchantCart['list'] as &$cart) { - if ($cart['product_type'] == 10 && $cart['productDiscountAttr']) { - $cart['productAttr']['price'] = $cart['productDiscountAttr']['active_price']; - $cart['productAttr']['show_svip_price'] = false; - } - - if ($cart['cart_num'] <= 0) { - throw new ValidateException('购买商品数必须大于0'); - } - $svip_discount = 0; - $realPrice = $this->cartByPrice($cart); - if (in_array($source, [9, 10, 11, 12, 13])) { - if ($realPrice == 0) { - throw new ValidateException('价格必须大于0'); - } - $cart['product']['price'] = $realPrice; - $cart['productAttr']['price'] = $realPrice; - $cart['productAttr']['stock'] = $cart['cart_num']; - } - $price = bcmul($cart['cart_num'], $realPrice, 2); - $cart['total_price'] = $price; - $cart['postage_price'] = 0; - $cart['svip_discount'] = 0; - $total_price = bcadd($total_price, $price, 2); - $total_num += $cart['cart_num']; - $_price = bcmul($cart['cart_num'], $this->cartByCouponPrice($cart), 2); - $cart['svip_coupon_merge'] = 1; - if ($cart['productAttr']['show_svip_price'] && !$cart['product_type']) { - $svip_discount = max(bcmul($cart['cart_num'], bcsub($cart['productAttr']['org_price'] ?? 0, $cart['productAttr']['price'], 2), 2), 0); - if ($svip_coupon_merge != '1') { - $_price = 0; - $cart['svip_coupon_merge'] = 0; - } - $use_svip = 1; - } - $valid_total_price = bcadd($valid_total_price, $_price, 2); - $cart['allow_price'] = $_price; - $temp1 = $cart['product']['temp']; - $cart['temp_number'] = 0; - $total_svip_discount = bcadd($total_svip_discount, $svip_discount, 2); - $cart['svip_discount'] = $svip_discount; - - if (!isset($product_cart[$cart['product_id']])) - $product_cart[$cart['product_id']] = [$cart['cart_id']]; - else - $product_cart[$cart['product_id']][] = $cart['cart_id']; - - if ($_price > 0) { - $product_price[$cart['product_id']] = bcadd($product_price[$cart['product_id']] ?? 0, $_price, 2); - } - - if (!$temp1) continue; - - $number = $this->productByTempNumber($cart); - if ($number <= 0) continue; - $cart['temp_number'] = $number; - - if ($order_model || !$temp1 || ($cart['product_type'] == 10 && $cart['productDiscount']['free_shipping'])) { - continue; - } - - $free = $temp1['free'][0] ?? null; - $region = $temp1['region'][0] ?? null; - - if (!$cart['product']['delivery_free'] && !$isTake && (!$address || !$cart['product']['temp'] || ($temp1['undelivery'] == 2 && !$free && (!$region || !$region['city_id'])))) { - $cart['undelivered'] = true; - $noDeliver = true; - continue; - } - $cart['undelivered'] = (!$isTake) && $temp1['undelivery'] == 1 && isset($temp1['undelives']); - $fn[] = function () use ($cart) { - unset($cart['product']['temp']); - }; - - if ($cart['undelivered']) { - $noDeliver = true; - continue; - } - if ($cart['product']['delivery_free']) { - continue; - } - $tempId = $cart['product']['temp_id']; - if (!isset($postageRule[$tempId])) { - $postageRule[$tempId] = [ - 'free' => null, - 'region' => null, - 'cart' => [], - 'price' => 0, - ]; - } - - $freeRule = $postageRule[$tempId]['free']; - $regionRule = $postageRule[$tempId]['region']; - $postageRule[$tempId]['cart'][] = $cart['cart_id']; - $postageRule[$tempId]['price'] = bcadd($postageRule[$tempId]['price'], $cart['price'], 2); - - if ($temp1['appoint'] && $free) { - if (!isset($freeRule)) { - $freeRule = $free; - $freeRule['cart_price'] = 0; - $freeRule['cart_number'] = 0; - } - $freeRule['cart_number'] = bcadd($freeRule['cart_number'], $number, 2); - $freeRule['cart_price'] = bcadd($freeRule['cart_price'], $price, 2); - } - - if ($region) { - if (!isset($regionRule)) { - $regionRule = $region; - $regionRule['cart_price'] = 0; - $regionRule['cart_number'] = 0; - } - $regionRule['cart_number'] = bcadd($regionRule['cart_number'], $number, 2); - $regionRule['cart_price'] = bcadd($regionRule['cart_price'], $price, 2); - } - $postageRule[$tempId]['free'] = $freeRule; - $postageRule[$tempId]['region'] = $regionRule; - } - unset($cart); - - if (!$isTake) { - //计算运费 - foreach ($postageRule as $item) { - $freeRule = $item['free']; - if ($freeRule && $freeRule['cart_number'] >= $freeRule['number'] && $freeRule['cart_price'] >= $freeRule['price']) - continue; - if (!$item['region']) continue; - $regionRule = $item['region']; - $postage = $regionRule['first_price']; - if ($regionRule['first'] > 0 && $regionRule['cart_number'] > $regionRule['first']) { - $num = ceil(bcdiv(bcsub($regionRule['cart_number'], $regionRule['first'], 2), $regionRule['continue'], 2)); - $postage = bcadd($postage, bcmul($num, $regionRule['continue_price'], 2), 2); - } - $postage_price = bcadd($postage_price, $postage, 2); - $cartNum = count($item['cart']); - //计算每个商品的运费比例 - foreach ($merchantCart['list'] as &$cart) { - if (in_array($cart['cart_id'], $item['cart'])) { - if (--$cartNum) { - $cart['postage_price'] = bcmul($postage, bcdiv($cart['temp_number'], $regionRule['cart_number'], 3), 2); - $postage = bcsub($postage, $cart['postage_price'], 2); - } else { - $cart['postage_price'] = $postage; - } - } - } - unset($cart); - } - unset($item); - } - - $coupon_price = 0; - $use_coupon_product = []; - $use_store_coupon = 0; - - $useCouponFlag = isset($useCoupon[$merchantCart['mer_id']]); - $merCouponIds = (array)($useCoupon[$merchantCart['mer_id']] ?? []); - $merCouponIds = array_reverse($merCouponIds); - $sortIds = $merCouponIds; - // $all_coupon_product = []; - unset($defaultSort); - $defaultSort = []; - if (count($merCouponIds)) { - foreach ($merchantCart['coupon'] as &$item) { - $defaultSort[] = &$item; - if (!in_array($item['coupon_user_id'], $sortIds, true)) { - $sortIds[] = $item['coupon_user_id']; - } - } - unset($item); - usort($merchantCart['coupon'], function ($a, $b) use ($sortIds) { - return array_search($a['coupon_user_id'], $sortIds) > array_search($b['coupon_user_id'], $sortIds) ? 1 : -1; - }); - } - - $pay_price = max(bcsub($valid_total_price, $coupon_price, 2), 0); - $_pay_price = $pay_price; - - - $productCouponRate = []; - $storeCouponRate = null; - $useCouponIds = []; - - - if ($use_store_coupon) { - $storeCouponRate = [ - 'rate' => $pay_price > 0 ? bcdiv($use_store_coupon['coupon_price'], $pay_price, 4) : 1, - 'coupon_price' => $use_store_coupon['coupon_price'], - 'price' => $coupon_price - ]; - $useCouponIds[] = $use_store_coupon['coupon_user_id']; - } - - //计算单个商品实际支付金额 - foreach ($merchantCart['list'] as $_k => &$cart) { - $cartTotalPrice = bcmul($this->cartByPrice($cart), $cart['cart_num'], 2); - $_cartTotalPrice = $cartTotalPrice; - - //单个商品实际支付金额 - $cart['coupon_price'] = bcsub($_cartTotalPrice, $cartTotalPrice, 2); - $cart['true_price'] = $cartTotalPrice; - } - $procure = $this->cartByPrice($cart, 1); - $procure_price = bcmul($cart['cart_num'], $procure, 2); - unset($cart, $_k); - $total_true_price = bcadd($_pay_price, $total_true_price, 2); - if (count($merchantCartList) > 1 || count($merchantCart['list']) > 1) { - $orderDeliveryStatus = $orderDeliveryStatus && $deliveryStatus; - } - $merchantCart['order'] = [ - 'true_price' => $_pay_price, - 'platform_coupon_price' => 0, - 'valid_total_price' => $valid_total_price, - 'total_price' => $total_price, - 'final_price' => $final_price, - 'down_price' => $down_price, - 'coupon_price' => $coupon_price, - 'platformCoupon' => $platformCoupon, - 'svip_coupon_merge' => $svip_coupon_merge, - 'postage_price' => $postage_price, - 'isTake' => intval($allowDelivery == false || count($takes) > 0), - 'total_num' => $total_num, - 'enabledCoupon' => $enabledCoupon, - 'useCouponIds' => $useCouponIds, - 'allow_take' => true, - 'allow_delivery' => $allowDelivery, - 'delivery_status' => $deliveryStatus, - 'svip_discount' => $total_svip_discount, - 'use_svip' => $use_svip, - 'procure_price' => $procure_price - ]; - $order_total_postage = bcadd($order_total_postage, $postage_price, 2); - $order_svip_discount = bcadd($total_svip_discount, $order_svip_discount, 2); - if (count($defaultSort)) { - $merchantCart['coupon'] = &$defaultSort; - } - } - unset($merchantCart); - - $usePlatformCouponId = 0; - $total_platform_coupon_price = 0; - $usePlatformCouponFlag = isset($useCoupon[0]); - - foreach ($merchantCartList as &$merchantCart) { - if (!$merchantCart['order']['use_svip']) - continue; - $totalMergePrice = 0; - foreach ($merchantCart['list'] as &$cart) { - if (!$cart['svip_coupon_merge']) { - $totalMergePrice = bcadd($totalMergePrice, $cart['true_price'], 2); - $cart['allow_price'] = $cart['true_price']; - } - } - unset($cart); - if ($totalMergePrice > 0) { - $total_true_price = bcadd($total_true_price, $totalMergePrice, 2); - $merchantCart['order']['valid_total_price'] = bcadd($merchantCart['order']['valid_total_price'], $totalMergePrice, 2); - $merchantCart['order']['true_price'] = $merchantCart['order']['valid_total_price']; - } - } - unset($merchantCart); - //计算平台券优惠金额 - // if ($total_true_price > 0) { - $StoreCouponUser = app()->make(StoreCouponUserRepository::class); - $platformCoupon = $StoreCouponUser->validUserPlatformCoupon($uid); - $platformConsumption = []; - //消费金 - if ($source == 103) { - $ConsumptionWhere = [ - 'uid' => $uid, - 'status' => 0, - 'is_fail' => 0 - ]; - - $platformConsumption = Db::name('store_consumption_user')->where($ConsumptionWhere)->limit(100)->order('create_time', 'desc') - ->field('coupon_user_id,uid,coupon_title,coupon_price,balance,start_time,end_time') - ->select()->each(function ($item) { - $item['describe'] = '仅限平台指定商家商品可使用'; - return $item; - }); - } - // } - - $usePlatformCouponId = 0; - $total_platform_coupon_price = 0; - - //积分配置 - $sysIntegralConfig = systemConfig(['integral_money', 'integral_status', 'integral_order_rate']); - $merIntegralFlag = false; - $order_total_integral = 0; - $order_total_integral_price = 0; - $order_total_give_integral = 0; - $allow_no_address = true; - - foreach ($merchantCartList as &$merchantCart) { - $merchantCart['take'] = [ - 'mer_integral_rate' => 0, - 'mer_integral_status' => 0, - ]; - $allow_no_address = $allow_no_address && $merchantCart['order']['isTake']; - foreach ($merchantCart['config'] as $config) { - $merchantCart['take'][$config['config_key']] = $config['value']; - } - $merIntegralConfig = $merchantCart['take']; - unset($merchantCart['config']); - $merIntegralConfig['mer_integral_rate'] = min(1, $merIntegralConfig['mer_integral_rate'] > 0 ? bcdiv($merIntegralConfig['mer_integral_rate'], 100, 4) : $merIntegralConfig['mer_integral_rate']); - $total_integral = 0; - $total_integral_price = 0; - $merIntegralFlag = $merIntegralFlag || ((bool)$merIntegralConfig['mer_integral_status']); - $integralFlag = $useIntegral && $sysIntegralConfig['integral_status'] && $sysIntegralConfig['integral_money'] > 0 && $merIntegralConfig['mer_integral_status']; - - $order_total_integral = bcadd($order_total_integral, $total_integral, 0); - $order_total_integral_price = bcadd($order_total_integral_price, $total_integral_price, 2); - - $_pay_price = $merchantCart['order']['true_price']; - $valid_total_price = $merchantCart['order']['valid_total_price']; - $total_price = $merchantCart['order']['total_price']; - $final_price = $merchantCart['order']['final_price']; - $down_price = $merchantCart['order']['down_price']; - $coupon_price = $merchantCart['order']['coupon_price']; - $postage_price = $merchantCart['order']['postage_price']; - - //计算订单商品金额 - $org_price = bcadd(bcsub($total_price, $valid_total_price, 2), max($_pay_price, 0), 2); - if ($presellType == 2) { - $org_price = max(bcsub($org_price, $final_price, 2), $down_price); - } - - //获取可优惠金额 - $coupon_price = min($coupon_price, bcsub($total_price, $down_price, 2)); - $order_coupon_price = bcadd($coupon_price, $order_coupon_price, 2); - - //计算订单金额 - if ($order_type != 2 || $presellType != 2) { - $pay_price = bcadd($postage_price, $org_price, 2); - } else { - $pay_price = $org_price; - } - //计算总红包金额 - if ($consumption_id > 0 ) { - if ($this->store_consumption_user) { - if ($this->store_consumption_user['type'] == 2 && $pay_price>=6) { - $a = bcdiv($pay_price, 6); - if ($this->balance > $a) { - $pay_price = bcsub($pay_price, $a, 2); - $this->consumption_money = bcadd($this->consumption_money, $a, 2); - $this->balance = bcsub($this->balance, $a, 2); - } else { - $pay_price = bcsub($pay_price, $this->balance, 2); - $this->consumption_money = bcadd($this->consumption_money, $this->balance, 2); - $this->balance = 0; - } - } - if ($this->store_consumption_user['type'] == 1) { - if ($pay_price > $this->balance) { - $pay_price = bcsub($pay_price, $this->balance, 2); - $this->consumption_money = bcadd($this->consumption_money, $this->balance, 2); - $this->balance=0; - } else { - $pay_price = 0; - $this->consumption_money = bcadd($this->consumption_money, $pay_price, 2); - $this->balance = $pay_price; - - } - } - - } - } - - $giveIntegralFlag = $sysIntegralConfig['integral_status'] && $sysIntegralConfig['integral_order_rate'] > 0; - $total_give_integral = 0; - - foreach ($fn as $callback) { - $callback(); - } - $merchantCart['order']['order_type'] = $order_type; - $merchantCart['order']['total_give_integral'] = $total_give_integral; - $merchantCart['order']['total_integral_price'] = $total_integral_price; - $merchantCart['order']['total_integral'] = $total_integral; - $merchantCart['order']['org_price'] = $org_price; - $merchantCart['order']['pay_price'] = $pay_price; - $merchantCart['order']['coupon_price'] = $coupon_price; - $merchantCart['order']['postage_price'] = $merchantCart['order']['postage_price']; - $merchantCart['order']['procure_price'] = $merchantCart['order']['procure_price']; - $merchantCart['order']['consumption_id'] = $consumption_id; - $merchantCart['order']['consumption_money'] = $this->consumption_money; - - $order_price = bcadd($order_price, $pay_price, 2); - $order_total_price = bcadd($order_total_price, $total_price, 2); - } - unset($merchantCart); - - if ($order_model) { - $allow_no_address = false; - } - - foreach ($merchantCartList as &$merchantCart) { - foreach ($merchantCart['list'] as &$cart) { - $cart['total_price'] = bcadd($cart['total_price'], $cart['svip_discount'], 2); - } - unset($cart); - $merchantCart['order']['total_price'] = bcadd($merchantCart['order']['total_price'], $merchantCart['order']['svip_discount'], 2); - $order_total_price = bcadd($order_total_price, $merchantCart['order']['svip_discount'], 2); - } - unset($merchantCart); - $status = ($address || $order_model || $allow_no_address) ? ($noDeliver ? 'noDeliver' : 'finish') : 'noAddress'; - $order = $merchantCartList; - $consumption_money = $this->consumption_money; - $total_price = $order_total_price; - $openIntegral = $merIntegralFlag && !$order_type && $sysIntegralConfig['integral_status'] && $sysIntegralConfig['integral_money'] > 0; - $total_coupon = bcadd($order_svip_discount, bcadd(bcadd($total_platform_coupon_price, $order_coupon_price, 2), $order_total_integral_price, 2), 2); - $is_self_pickup = true; - if($order_type=='balance' &&$source!=103){ - throw new ValidateException('余额支付只能用于里海云仓'); - } - return compact( - 'is_self_pickup', - 'order_type', - 'source', - 'order_model', - 'order_extend', - 'order_total_postage', - 'order_price', - 'total_price', - 'community', - 'enabledPlatformCoupon', - 'platformCoupon', - 'usePlatformCouponId', - 'order_total_integral', - 'order_total_integral_price', - 'order_total_give_integral', - 'order_svip_discount', - 'total_platform_coupon_price', - 'total_coupon', - 'order_coupon_price', - 'order', - 'status', - 'address', - 'openIntegral', - 'useIntegral', - 'key', - 'platformConsumption', - 'consumption_money', - 'consumption_id' - ) + ['allow_address' => !$allow_no_address, 'order_delivery_status' => $orderDeliveryStatus]; - } - - public function v2CreateOrder(int $pay_type, $user, array $cartId, array $extend, array $mark, array $receipt_data, array $takes = null, array $useCoupon = null, bool $useIntegral = false, int $addressId = null, array $post, int $product_type = 0, $consumption_id = 0) - { - $uid = $user->uid; - $orderInfo = $this->v2CartIdByOrderInfo($user, $cartId, $takes, $useCoupon, $useIntegral, $addressId, true, $consumption_id); - $order_model = $orderInfo['order_model']; - $order_extend = $orderInfo['order_extend']; - if (!$orderInfo['order_delivery_status']) { - throw new ValidateException('部分商品配送方式不一致,请单独下单'); - } - if ($orderInfo['order_price'] > 1000000) { - throw new ValidateException('支付金额超出最大限制'); - } - if ($orderInfo['status'] == 'noDeliver') throw new ValidateException('商品不支持该区域'); - if ($orderInfo['status'] == 'noAddress') throw new ValidateException('请选择地址'); - if (!$order_model && $orderInfo['allow_address']) { - if (!$orderInfo['address']) throw new ValidateException('请选择正确的收货地址'); - if (!$orderInfo['address']['province_id']) throw new ValidateException('请完善收货地址信息'); - $extend = []; - } else if (count($order_extend)) { - $extend = app()->make(OrderVirtualFieldValidate::class)->load($order_extend, $extend); - } else { - $extend = []; - } - $orderType = $orderInfo['order_type']; - if ($orderType == 0 && $pay_type == StoreGroupOrder::PAY_TYPE_CREDIT_BUY) { - throw new ValidateException('该商品不支持先货后款'); - } - if (!in_array($orderType, [0, 98, 99]) && (count($orderInfo['order']) > 1 || ($orderType != 10 && count($orderInfo['order'][0]['list']) > 1))) { - throw new ValidateException('活动商品请单独购买'); - } - - $merchantCartList = $orderInfo['order']; - $cartSpread = 0; - $hasTake = false; - - foreach ($merchantCartList as $merchantCart) { - if ($merchantCart['order']['isTake']) { - $hasTake = true; - } - //检查发票状态 - if (isset($receipt_data[$merchantCart['mer_id']]) && !$merchantCart['openReceipt']) - throw new ValidateException('该店铺不支持开发票'); - if ($pay_type == StoreGroupOrder::PAY_TYPE_CREDIT_BUY && !$merchantCart['credit_buy']) { - throw new ValidateException("{$merchantCart['mer_name']}不支持先货后款"); - } - - foreach ($merchantCart['list'] as $cart) { - if (!$cartSpread && $cart['spread_id']) { - $cartSpread = $cart['spread_id']; - } - } - } - if (($orderType == 98 || $orderType == 99) && count($merchantCartList) > 1) { - throw new ValidateException('采购、委托商品不支持跨店购买'); - } - if ($hasTake) { - app()->make(UserAddressValidate::class)->scene('take')->check($post); - } - - if ($cartSpread) { - app()->make(UserRepository::class)->bindSpread($user, $cartSpread); - } - - $isSelfBuy = $user->is_promoter && systemConfig('extension_self') ? 1 : 0; - if ($isSelfBuy) { - $spreadUser = $user; - $topUser = $user->valid_spread; - } else { - $spreadUser = $user->valid_spread; - $topUser = $user->valid_top; - } - $spreadUid = $spreadUser->uid ?? 0; - $topUid = $topUser->uid ?? 0; - - $merchantRepository = app()->make(MerchantRepository::class); - $giveCouponIds = []; - $ex = systemConfig('extension_status'); //是否开启分销 - $address = $orderInfo['address']; - $allUseCoupon = $orderInfo['usePlatformCouponId'] ? [$orderInfo['usePlatformCouponId']] : []; - $totalNum = 0; - $totalPostage = 0; - $totalCost = 0; - $cartIds = []; - $orderList = []; - foreach ($merchantCartList as $k => $merchantCart) { - $cost = 0; - $total_extension_one = 0; - $total_extension_two = 0; - //计算佣金和赠送的优惠券 - foreach ($merchantCart['list'] as &$cart) { - $cartIds[] = $cart['cart_id']; - $giveCouponIds = array_merge($giveCouponIds, $cart['product']['give_coupon_ids'] ?: []); - $cart['cost'] = $cart['productAttr']['cost']; - $cost = bcadd(bcmul($cart['cost'], $cart['cart_num'], 2), $cost, 2); - $extension_one = 0; - $extension_two = 0; - $cart['extension_one'] = $extension_one; - $cart['extension_two'] = $extension_two; - $total_extension_one = bcadd($total_extension_one, bcmul($extension_one, $cart['cart_num'], 2), 2); - $total_extension_two = bcadd($total_extension_two, bcmul($extension_two, $cart['cart_num'], 2), 2); - } - unset($cart); - - $rate = 0; - if ($merchantCart['commission_rate'] > 0) { - $rate = $merchantCart['commission_rate']; - } else if (isset($merchantCart['merchantCategory']['commission_rate']) && $merchantCart['merchantCategory']['commission_rate'] > 0) { - $rate = bcmul($merchantCart['merchantCategory']['commission_rate'], 100, 4); - } - $user_address = isset($address) ? ($address['province'] . $address['city'] . $address['district'] . $address['street'] . $address['village'] . $address['brigade'] . $address['detail']) : ''; - $user_address_code = isset($address) ? ($address['province_code'] . ',' . $address['city_code'] . ',' . $address['district_code'] . ',' . $address['street_code'] . ',' . $address['village_code'] . ',' . $address['brigade_id']) : ''; - //整理订单数据 - $_order = [ - 'cartInfo' => $merchantCart, - 'activity_type' => $orderInfo['order_type'], - 'source' => $orderInfo['source'] ?? 2, - 'commission_rate' => (float)$rate, - 'order_type' => $merchantCart['order']['isTake'] ? 1 : 0, - 'is_virtual' => $order_model ? 1 : 0, - 'extension_one' => $total_extension_one, - 'extension_two' => $total_extension_two, - 'order_sn' => $this->getNewOrderId(StoreOrderRepository::TYPE_SN_ORDER) . ($k + 1), - 'uid' => $uid, - 'spread_uid' => $spreadUid, - 'top_uid' => $topUid, - 'is_selfbuy' => $isSelfBuy, - 'real_name' => $merchantCart['order']['isTake'] ? $post['real_name'] : ($address['real_name'] ?? ''), - 'user_phone' => $merchantCart['order']['isTake'] ? $post['phone'] : ($address['phone'] ?? ''), - 'user_address' => $user_address, - 'user_address_code' => $user_address_code, - 'cart_id' => implode(',', array_column($merchantCart['list'], 'cart_id')), - 'total_num' => $merchantCart['order']['total_num'], - 'total_price' => $merchantCart['order']['total_price'], - 'total_postage' => $merchantCart['order']['postage_price'], - 'pay_postage' => $merchantCart['order']['postage_price'], - 'svip_discount' => $merchantCart['order']['svip_discount'], - 'pay_price' => $merchantCart['order']['pay_price'], - 'integral' => $merchantCart['order']['total_integral'], - 'integral_price' => $merchantCart['order']['total_integral_price'], - 'give_integral' => $merchantCart['order']['total_give_integral'], - 'mer_id' => $merchantCart['mer_id'], - 'cost' => $cost, - 'order_extend' => count($extend) ? json_encode($extend, JSON_UNESCAPED_UNICODE) : '', - 'coupon_id' => implode(',', $merchantCart['order']['useCouponIds']), - 'mark' => $mark[$merchantCart['mer_id']] ?? '', - 'coupon_price' => bcadd($merchantCart['order']['coupon_price'], $merchantCart['order']['platform_coupon_price'], 2), - 'platform_coupon_price' => $merchantCart['order']['platform_coupon_price'], - 'pay_type' => $pay_type, - 'procure_price' => $merchantCart['order']['procure_price'], - 'consumption_id' => $merchantCart['order']['consumption_id'], - 'consumption_money' => $merchantCart['order']['consumption_money'], - ]; - $allUseCoupon = array_merge($allUseCoupon, $merchantCart['order']['useCouponIds']); - $orderList[] = $_order; - $totalPostage = bcadd($totalPostage, $_order['total_postage'], 2); - $totalCost = bcadd($totalCost, $cost, 2); - $totalNum += $merchantCart['order']['total_num']; - } - //总订单组 - $groupOrder = [ - 'uid' => $uid, - 'group_order_sn' => count($orderList) === 1 ? $orderList[0]['order_sn'] : ($this->getNewOrderId(StoreOrderRepository::TYPE_SN_ORDER) . '0'), - 'total_postage' => $totalPostage, - 'total_price' => $orderInfo['total_price'], - 'total_num' => $totalNum, - 'real_name' => $address['real_name'] ?? '', - 'user_phone' => $address['phone'] ?? '', - 'user_address' => $user_address, - 'pay_price' => $orderInfo['order_price'], - 'coupon_price' => bcadd($orderInfo['total_platform_coupon_price'], $orderInfo['order_coupon_price'], 2), - 'pay_postage' => $totalPostage, - 'cost' => $totalCost, - 'coupon_id' => $orderInfo['usePlatformCouponId'] > 0 ? $orderInfo['usePlatformCouponId'] : '', - 'pay_type' => $pay_type, - 'give_coupon_ids' => $giveCouponIds, - 'integral' => $orderInfo['order_total_integral'], - 'integral_price' => $orderInfo['order_total_integral_price'], - 'give_integral' => $orderInfo['order_total_give_integral'], - 'consumption_id' => $orderInfo['consumption_id'], - 'consumption_money' => $orderInfo['consumption_money'], - ]; - event('order.create.before', compact('groupOrder', 'orderList')); - $group = Db::transaction(function () use ($ex, $user, $topUid, $spreadUid, $uid, $receipt_data, $cartIds, $allUseCoupon, $groupOrder, $orderList, $orderInfo) { - $storeGroupOrderRepository = app()->make(StoreGroupOrderRepository::class); - $storeCartRepository = app()->make(StoreCartRepository::class); - /** @var ProductAttrValueRepository $attrValueRepository */ - $attrValueRepository = app()->make(ProductAttrValueRepository::class); - /** @var ProductRepository $productRepository */ - $productRepository = app()->make(ProductRepository::class); - $storeOrderProductRepository = app()->make(StoreOrderProductRepository::class); - $couponUserRepository = app()->make(StoreCouponUserRepository::class); - //订单记录 - $storeOrderStatusRepository = app()->make(StoreOrderStatusRepository::class); - $userMerchantRepository = app()->make(UserMerchantRepository::class); - - //减库存 - foreach ($orderList as $order) { - foreach ($order['cartInfo']['list'] as $cart) { - if (!isset($uniqueList[$cart['productAttr']['product_id'] . $cart['productAttr']['unique']])) - $uniqueList[$cart['productAttr']['product_id'] . $cart['productAttr']['unique']] = true; - else - throw new ValidateException('购物车商品信息重复'); - - try { - if ($cart['product_type'] == '1') { - $attrValueRepository->descSkuStock($cart['product']['old_product_id'], $cart['productAttr']['sku'], $cart['cart_num']); - $productRepository->descStock($cart['product']['old_product_id'], $cart['cart_num']); - } else if ($cart['product_type'] == '2') { - $productPresellSkuRepository = app()->make(ProductPresellSkuRepository::class); - $productPresellSkuRepository->descStock($cart['productPresellAttr']['product_presell_id'], $cart['productPresellAttr']['unique'], $cart['cart_num']); - $attrValueRepository->descStock($cart['productAttr']['product_id'], $cart['productAttr']['unique'], $cart['cart_num']); - $productRepository->descStock($cart['product']['product_id'], $cart['cart_num']); - } else if ($cart['product_type'] == '3') { - app()->make(ProductAssistSkuRepository::class)->descStock($cart['productAssistAttr']['product_assist_id'], $cart['productAssistAttr']['unique'], $cart['cart_num']); - $productRepository->descStock($cart['product']['old_product_id'], $cart['cart_num']); - $attrValueRepository->descStock($cart['product']['old_product_id'], $cart['productAttr']['unique'], $cart['cart_num']); - } else if ($cart['product_type'] == '4') { - app()->make(ProductGroupSkuRepository::class)->descStock($cart['activeSku']['product_group_id'], $cart['activeSku']['unique'], $cart['cart_num']); - $productRepository->descStock($cart['product']['old_product_id'], $cart['cart_num']); - $attrValueRepository->descStock($cart['product']['old_product_id'], $cart['productAttr']['unique'], $cart['cart_num']); - } else { - if ($cart['source'] == StoreCartDao::SOURCE_COMMUNITY_RESALE) { - /** @var CommunityRepository $communityRepository */ - $communityRepository = app()->make(CommunityRepository::class); - $communityRepository->saleOrCancel($cart['source_id']); - $attrValueRepository->incSales($cart['productAttr']['product_id'], $cart['productAttr']['unique'], $cart['cart_num']); - $productRepository->incSales($cart['product']['product_id'], $cart['cart_num']); - } else { - $attrValueRepository->descStock($cart['productAttr']['product_id'], $cart['productAttr']['unique'], $cart['cart_num']); - $productRepository->descStock($cart['product']['product_id'], $cart['cart_num']); - } - /** @var PurchaseRecordRepository $purchaseRecordRepo */ - $purchaseRecordRepo = app()->make(PurchaseRecordRepository::class); - $purchaseRecordRepo->incSalesVolume($cart['product']['product_id'], $cart['productAttr']['unique'], $cart['cart_num']); - if ($cart['integral'] && $cart['integral']['use'] > 0) { - $productRepository->incIntegral($cart['product']['product_id'], $cart['integral']['use'], $cart['integral']['price']); - } - } - } catch (\Exception $e) { - throw new ValidateException('库存不足'); - } - } - } - - if ($orderInfo['order_type'] == 10 && !app()->make(StoreDiscountRepository::class)->decStock($orderList[0]['cartInfo']['list'][0]['source_id'])) { - throw new ValidateException('套餐库不足'); - } - - //修改购物车状态 - $storeCartRepository->updates($cartIds, [ - 'is_pay' => 1 - ]); - - //使用优惠券 - if (count($allUseCoupon)) { - $couponUserRepository->updates($allUseCoupon, [ - 'use_time' => date('Y-m-d H:i:s'), - 'status' => 1 - ]); - } - - //创建订单 - $groupOrder = $storeGroupOrderRepository->create($groupOrder); - $bills = []; - $consumptionDetails=[]; - if ($groupOrder['integral'] > 0) { - $user->integral = bcsub($user->integral, $groupOrder['integral'], 0); - app()->make(UserBillRepository::class)->decBill($user['uid'], 'integral', 'deduction', [ - 'link_id' => $groupOrder['group_order_id'], - 'status' => 1, - 'title' => '购买商品', - 'number' => $groupOrder['integral'], - 'mark' => '购买商品使用积分抵扣' . floatval($groupOrder['integral_price']) . '元', - 'balance' => $user->integral - ]); - $user->save(); - } - - if ($groupOrder['consumption_money'] > 0) { - $balance = 0; - $store_consumption_user = Db::name('store_consumption_user')->where('uid',$uid)->where('coupon_user_id', $groupOrder['consumption_id'])->where('status', 0)->find(); - if ($store_consumption_user) { - if ($store_consumption_user['type'] == 1) { - if ($store_consumption_user['balance'] > $groupOrder['consumption_money']) { - $balance = bcsub($store_consumption_user['balance'], $groupOrder['consumption_money'], 2); - $balanceArr = ['balance' => $balance]; - Db::name('store_consumption_user')->where('coupon_user_id', $store_consumption_user['coupon_user_id'])->update($balanceArr); - } else { - $balanceArr = ['balance' => 0, 'status' => 1]; - Db::name('store_consumption_user')->where('coupon_user_id', $store_consumption_user['coupon_user_id'])->update($balanceArr); - } - } - } - app()->make(UserBillRepository::class)->decBill($user['uid'], 'consumption', 'deduction', [ - 'link_id' => $groupOrder['group_order_id'], - 'status' => 1, - 'title' => '购买商品', - 'number' => $groupOrder['consumption_money'], - 'mark' => '购买商品使用红包抵扣' . floatval($groupOrder['consumption_money']) . '元', - 'balance' => $balance - ]); - } - - foreach ($orderList as $k => $order) { - $orderList[$k]['group_order_id'] = $groupOrder->group_order_id; - } - - $orderProduct = []; - $orderStatus = []; - foreach ($orderList as $order) { - if ($order['pay_type'] == StoreGroupOrder::PAY_TYPE_CREDIT_BUY) { - $order['status'] = StoreOrder::STATUS_WAIT_CONFIRM; - } - $cartInfo = $order['cartInfo']; - unset($order['cartInfo']); - //创建子订单 - $_order = $this->dao->create($order); - - if ($order['integral'] > 0) { - $bills[] = [ - 'uid' => $uid, - 'link_id' => $_order->order_id, - 'pm' => 0, - 'title' => '积分抵扣', - 'category' => 'mer_integral', - 'type' => 'deduction', - 'number' => $order['integral'], - 'balance' => $user->integral, - 'mark' => '购买商品使用' . $order['integral'] . '积分抵扣' . floatval($order['integral_price']) . '元', - 'mer_id' => $order['mer_id'], - 'status' => 1 - ]; - } - if ($order['consumption_money'] > 0) { - $consumptionDetails[] = [ - 'user_id' => $uid, - 'order_id' => $_order->order_id, - 'group_order_id' => $groupOrder->group_order_id, - 'coupon_user_id' => $order['consumption_id'], - 'type' => 1, - 'amount' => $order['consumption_money'], - 'pay_price' => $order['pay_price'], - 'create_time' => time() - ]; - } - - //创建发票信息 - if (isset($receipt_data[$_order['mer_id']])) { - app()->make(StoreOrderReceiptRepository::class)->add($receipt_data[$_order['mer_id']], $_order); - } - - $orderStatus[] = [ - 'order_id' => $_order->order_id, - 'order_sn' => $_order->order_sn, - 'type' => $storeOrderStatusRepository::TYPE_ORDER, - 'change_message' => '订单生成', - 'change_type' => $storeOrderStatusRepository::ORDER_STATUS_CREATE, - 'uid' => $user->uid, - 'nickname' => $user->nickname, - 'user_type' => $storeOrderStatusRepository::U_TYPE_USER, - ]; - - foreach ($cartInfo['list'] as $cart) { - $productPrice = $cart['true_price']; - $extension_one = $cart['extension_one']; - $extension_two = $cart['extension_two']; - - //计算预售订单尾款 - if ($cartInfo['order']['order_type'] == 2) { - $finalPrice = max(bcsub($cartInfo['order']['final_price'], $cartInfo['order']['coupon_price'], 2), 0); - $allFinalPrice = $order['order_type'] ? $finalPrice : bcadd($finalPrice, $order['pay_postage'], 2); - if ($cart['productPresell']['presell_type'] == 1) { - $productPrice = bcsub($cartInfo['order']['pay_price'], $order['pay_postage'], 2); - } else { - $productPrice = bcadd($cartInfo['order']['pay_price'], $finalPrice, 2); - } - //生成尾款订单 - if ($cart['productPresell']['presell_type'] == 2) { - $presellOrderRepository = app()->make(PresellOrderRepository::class); - $presellOrderRepository->create([ - 'uid' => $uid, - 'order_id' => $_order->order_id, - 'mer_id' => $_order->mer_id, - 'final_start_time' => $cart['productPresell']['final_start_time'], - 'final_end_time' => $cart['productPresell']['final_end_time'], - 'pay_price' => $allFinalPrice, - 'presell_order_sn' => $this->getNewOrderId(StoreOrderRepository::TYPE_SN_PRESELL) - ]); - } - app()->make(ProductPresellSkuRepository::class)->incCount($cart['source_id'], $cart['productAttr']['unique'], 'one_take'); - } - - $order_cart = [ - 'product' => $cart['product'], - 'productAttr' => $cart['productAttr'], - 'product_type' => $cart['product_type'] - ]; - - if ($cart['product_type'] == '2') { - $order_cart['productPresell'] = $cart['productPresell']; - $order_cart['productPresellAttr'] = $cart['productPresellAttr']; - $order_cart['final_extension_one'] = $cart['final_extension_one'] ?? 0; - $order_cart['final_extension_two'] = $cart['final_extension_two'] ?? 0; - $order_cart['presell_extension_one'] = $cart['presell_extension_one'] ?? 0; - $order_cart['presell_extension_two'] = $cart['presell_extension_two'] ?? 0; - } else if ($cart['product_type'] == '3') { - $order_cart['productAssistAttr'] = $cart['productAssistAttr']; - $order_cart['productAssistSet'] = $cart['productAssistSet']; - } else if ($cart['product_type'] == '4') { - $order_cart['activeSku'] = $cart['activeSku']; - } else if ($cart['product_type'] == '10') { - $order_cart['active'] = $cart['productDiscount']; - $order_cart['activeSku'] = $cart['productDiscountAttr']; - } - $product_source_id = 0; - $product_mer_id = 0; - if ($cart['source'] == 103) { - $old_product_id = Db::name('store_product')->where('product_id', $cart['product_id'])->value('old_product_id'); - if ($old_product_id) { - $old_mer_id = Db::name('store_product')->where('product_id', $old_product_id)->value('mer_id'); - $product_mer_id = $old_mer_id; - $product_source_id = $old_product_id; - } - } - $orderProduct[] = [ - 'order_id' => $_order->order_id, - 'cart_id' => $cart['cart_id'], - 'uid' => $uid, - 'product_id' => $cart['product_id'], - 'activity_id' => $cart['source'] >= 2 ? $cart['source_id'] : $cart['product_id'], - 'total_price' => $cart['total_price'], - 'product_price' => $productPrice, - 'extension_one' => $extension_one, - 'extension_two' => $extension_two, - 'postage_price' => $cart['postage_price'], - 'svip_discount' => $cart['svip_discount'], - 'cost' => $cart['cost'], - 'coupon_price' => $cart['coupon_price'], - 'platform_coupon_price' => $cart['platform_coupon_price'], - 'product_sku' => $cart['productAttr']['unique'], - 'product_num' => $cart['cart_num'], - 'refund_num' => $cart['cart_num'], - 'integral_price' => $cart['integral']['price'] ?? 0, - 'integral' => $cart['integral'] ? bcdiv($cart['integral']['use'], $cart['cart_num'], 0) : 0, - 'integral_total' => $cart['integral'] ? $cart['integral']['use'] : 0, - 'product_type' => $cart['product_type'], - 'source' => $cart['source'], - 'source_id' => $cart['source_id'], - 'cart_info' => json_encode($order_cart), - 'product_source_id' => $product_source_id, - 'product_mer_id' => $product_mer_id, - ]; - } - - $userMerchantRepository->getInfo($uid, $order['mer_id']); - app()->make(MerchantRepository::class)->incSales($order['mer_id'], $order['total_num']); - } - - if (count($bills) > 0) { - app()->make(UserBillRepository::class)->insertAll($bills); - } - if (count($consumptionDetails) > 0) { - Db::name('store_consumption_detail')->insertAll($consumptionDetails); - } - $storeOrderStatusRepository->batchCreateLog($orderStatus); - $storeOrderProductRepository->insertAll($orderProduct); - event('order.create', compact('groupOrder')); - return $groupOrder; - }); - foreach ($merchantCartList as $merchantCart) { - foreach ($merchantCart['list'] as $cart) { - if (($cart['productAttr']['stock'] - $cart['cart_num']) < (int)merchantConfig($merchantCart['mer_id'], 'mer_store_stock')) { - SwooleTaskService::merchant('notice', [ - 'type' => 'min_stock', - 'data' => [ - 'title' => '库存不足', - 'message' => $cart['product']['store_name'] . '(' . $cart['productAttr']['sku'] . ')库存不足', - 'id' => $cart['product']['product_id'] - ] - ], $merchantCart['mer_id']); - } - } - } - // Queue::push(SendSmsJob::class, ['tempId' => 'ORDER_CREATE', 'id' => $group->group_order_id, - // 'activity_type'=>$group->activity_type,'order_id'=>$group->order_id]); - return $group; - } -} +uid; + $this->balance = 0; + $userIntegral = $user->integral; + $key = md5(json_encode(compact('cartId', 'takes', 'useCoupon', 'useIntegral', 'addressId'))) . $uid; + app()->make(StoreCouponUserRepository::class)->failCoupon(); + $address = null; + //验证地址 + if ($addressId) { + $addressRepository = app()->make(UserAddressRepository::class); + $address = $addressRepository->getWhere(['uid' => $uid, 'address_id' => $addressId]); + } + $storeCartRepository = app()->make(StoreCartRepository::class); + $res = $storeCartRepository->checkCartList($storeCartRepository->cartIbByData($cartId, $uid, $address), 0, $user); + $merchantCartList = $res['list']; + $fail = $res['fail']; + + //检查购物车失效数据 + if (count($fail)) { + if ($fail[0]['is_fail']) + throw new ValidateException('[已失效]' . mb_substr($fail[0]['product']['store_name'], 0, 10) . '...'); + if (in_array($fail[0]['product_type'], [1, 2, 3]) && !$fail[0]['userPayCount']) { + throw new ValidateException('[超出限购数]' . mb_substr($fail[0]['product']['store_name'], 0, 10) . '...'); + } + throw new ValidateException('[已失效]' . mb_substr($fail[0]['product']['store_name'], 0, 10) . '...'); + } + + $svip_status = $user->is_svip > 0 && systemConfig('svip_switch_status') == '1'; + $svip_integral_rate = $svip_status ? app()->make(MemberinterestsRepository::class)->getSvipInterestVal(MemberinterestsRepository::HAS_TYPE_PAY) : 0; + //订单活动类型 + $order_type = 0; + $source = 2; + //虚拟订单 + $order_model = 0; + //虚拟订单自定义数据 + $order_extend = []; + //检查商品类型, 活动商品只能单独购买 + $allowDelivery = true; + foreach ($merchantCartList as $merchantCart) { + + if (($merchantCart['type_id'] != Merchant::TypeSupplyChain) && $address) { + if ($merchantCart['street_id'] != $address['street_code']) { + throw new ValidateException('不支持跨区域购买,请在【我的】-【地址管理】更改后重新购买'); + } + } + foreach ($merchantCart['list'] as $cart) { + if ($cart['product_type'] == 0) { + if ($cart['product']['once_min_count'] > 0 && $cart['product']['once_min_count'] > $cart['cart_num']) + throw new ValidateException('[低于起购数:' . $cart['product']['once_min_count'] . ']' . mb_substr($cart['product']['store_name'], 0, 10) . '...'); + if ($cart['product']['pay_limit'] == 1 && $cart['product']['once_max_count'] < $cart['cart_num']) + throw new ValidateException('[超出单次限购数:' . $cart['product']['once_max_count'] . ']' . mb_substr($cart['product']['store_name'], 0, 10) . '...'); + if ($cart['product']['pay_limit'] == 2) { + //如果长期限购 + //已购买数量 + $count = app()->make(StoreOrderRepository::class)->getMaxCountNumber($cart['uid'], $cart['product_id']); + if (($cart['cart_num'] + $count) > $cart['product']['once_max_count']) + throw new ValidateException('[超出限购总数:' . $cart['product']['once_max_count'] . ']' . mb_substr($cart['product']['store_name'], 0, 10) . '...'); + } + } + if ($cart['product_type'] > 0) { + $order_type = $cart['product_type']; + } + $source = $cart['source']; + if ($cart['product_type'] <= 97 && $cart['product_type'] > 0 && (($cart['product_type'] != 10 && count($merchantCart['list']) != 1) || count($merchantCartList) != 1)) { + throw new ValidateException('活动商品必须单独购买'); + } + if ($cart['product']['type'] && (count($merchantCart['list']) != 1 || count($merchantCartList) != 1)) { + throw new ValidateException('虚拟商品必须单独购买'); + } + $order_model = $cart['product']['type']; + if ($cart['product']['extend']) { + $order_extend = json_decode($cart['product']['extend'], true); + } + if ($address) { + if ($cart['source'] == 0 || $cart['source'] == 103) { + $userAddressCode = ($address['province_code'] ?? '') . ',' . ($address['city_code'] ?? '') . ',' . ($address['district_code'] ?? '') . ',' . ($address['street_code'] ?? '') . ',' . ($address['village_code'] ?? '') . ',' . ($address['brigade_id'] ?? 0); + $getUrl = env('LOGISTICS_HOST_URL') . '/api/hasCourier?user_address_code=' . $userAddressCode; + $curl = new Curl(); + $response = $curl->get($getUrl); + $courierData = json_decode($response, true); + if (empty($courierData['data']['user_id'])) { + $allowDelivery = false; + } + } + } + } + } + // if (($order_type == 98 || $order_type == 99) && count($merchantCartList) > 1) { + // throw new ValidateException('采购、委托商品不支持跨店购买'); + // } + $community = []; + unset($merchantCart, $cart); + $order_price = 0; + $total_true_price = 0; + $order_total_price = 0; + $order_coupon_price = 0; + $noDeliver = false; + $presellType = 0; + $fn = []; + if (in_array($source, [0, 2, 103])) { + $enabledPlatformCoupon = true; + } else { + $enabledPlatformCoupon = false; + } + $order_total_postage = 0; + $platformCoupon = []; + + + //套餐订单 + if ($order_type == 10) { + app()->make(StoreDiscountRepository::class) + ->check($merchantCartList[0]['list'][0]['source_id'], $merchantCartList[0]['list'], $user); + } + $orderDeliveryStatus = true; + $order_svip_discount = 0; + + if ($consumption_id > 0) { + $this->store_consumption_user = Db::name('store_consumption_user')->where('coupon_user_id', $consumption_id)->where('uid', $uid)->find(); + $this->balance=$this->store_consumption_user['balance']; + } + // 循环计算每个店铺的订单数据 委托商品是否设置收货方式 ? + foreach ($merchantCartList as &$merchantCart) { + if ($order_type == 98 && !empty($deliverMethodArray)) { + $merchantCart['delivery_way'] = $deliverMethodArray; + } + $postageRule = []; + $total_price = 0; + $total_num = 0; + $valid_total_price = 0; + $postage_price = 0; + $product_price = []; + $final_price = 0; + $down_price = 0; + $total_svip_discount = 0; + + //是否自提 + $isTake = in_array($merchantCart['mer_id'], $takes ?? []); + + // if (!$createOrder && !$isTake) { + // $isTake = count($merchantCart['delivery_way']) == 1 && $merchantCart['delivery_way'][0] == '1'; + // } + + $merTake = in_array('1', $merchantCart['delivery_way'], true); + $merDelivery = (!$merchantCart['delivery_way'] || !count($merchantCart['delivery_way']) || in_array('2', $merchantCart['delivery_way'], true)); + $_merTake = $merTake; + $_merDelivery = $merDelivery; + $deliveryStatus = true; + if ($createOrder && $isTake && !$merTake) { + $deliveryStatus = false; + // throw new ValidateException('[仅支持快递配送]' . $merchantCart['mer_name']); + } + $product_cart = []; + + foreach ($merchantCart['list'] as $k => $cart) { + //获取订单类型, 活动商品单次只能购买一个 + if ($cart['product']['delivery_way']) { + $delivery_way = explode(',', $cart['product']['delivery_way']); + $proTake = in_array('1', $delivery_way, true); + $merTake = $merTake && $proTake; + $proDelivery = (!count($delivery_way) || in_array('2', $delivery_way, true)); + $merDelivery = $merDelivery && $proDelivery; + $merchantCart['list'][$k]['allow_take'] = true; + $merchantCart['list'][$k]['allow_delivery'] = $allowDelivery; + } else { + $merchantCart['list'][$k]['allow_take'] = true; + $merchantCart['list'][$k]['allow_delivery'] = $allowDelivery; + } + if ($createOrder && $isTake && !$merTake) { + $deliveryStatus = false; + // throw new ValidateException('[仅支持快递配送]' . $cart['product']['store_name']); + } + } + if (!$merDelivery && !$merTake) { + $deliveryStatus = false; + // throw new ValidateException('部分商品配送方式不一致,请单独下单'); + } + if ($deliveryStatus && !$isTake && ($merDelivery || $merTake)) { + $isTake = $merDelivery ? 0 : 1; + } + + unset($cart); + + $enabledCoupon = !($order_type && $order_type != 2); + + //只有预售和普通商品可以用优惠券 + if (!$enabledCoupon) { + $merchantCart['coupon'] = []; + } + $svip_coupon_merge = merchantConfig($merchantCart['mer_id'], 'svip_coupon_merge'); + $use_svip = 0; + //获取运费规则和统计商品数据 + foreach ($merchantCart['list'] as &$cart) { + if ($cart['product_type'] == 10 && $cart['productDiscountAttr']) { + $cart['productAttr']['price'] = $cart['productDiscountAttr']['active_price']; + $cart['productAttr']['show_svip_price'] = false; + } + + if ($cart['cart_num'] <= 0) { + throw new ValidateException('购买商品数必须大于0'); + } + $svip_discount = 0; + $realPrice = $this->cartByPrice($cart); + if (in_array($source, [9, 10, 11, 12, 13])) { + if ($realPrice == 0) { + throw new ValidateException('价格必须大于0'); + } + $cart['product']['price'] = $realPrice; + $cart['productAttr']['price'] = $realPrice; + $cart['productAttr']['stock'] = $cart['cart_num']; + } + $price = bcmul($cart['cart_num'], $realPrice, 2); + $cart['total_price'] = $price; + $cart['postage_price'] = 0; + $cart['svip_discount'] = 0; + $total_price = bcadd($total_price, $price, 2); + $total_num += $cart['cart_num']; + $_price = bcmul($cart['cart_num'], $this->cartByCouponPrice($cart), 2); + $cart['svip_coupon_merge'] = 1; + if ($cart['productAttr']['show_svip_price'] && !$cart['product_type']) { + $svip_discount = max(bcmul($cart['cart_num'], bcsub($cart['productAttr']['org_price'] ?? 0, $cart['productAttr']['price'], 2), 2), 0); + if ($svip_coupon_merge != '1') { + $_price = 0; + $cart['svip_coupon_merge'] = 0; + } + $use_svip = 1; + } + $valid_total_price = bcadd($valid_total_price, $_price, 2); + $cart['allow_price'] = $_price; + $temp1 = $cart['product']['temp']; + $cart['temp_number'] = 0; + $total_svip_discount = bcadd($total_svip_discount, $svip_discount, 2); + $cart['svip_discount'] = $svip_discount; + + if (!isset($product_cart[$cart['product_id']])) + $product_cart[$cart['product_id']] = [$cart['cart_id']]; + else + $product_cart[$cart['product_id']][] = $cart['cart_id']; + + if ($_price > 0) { + $product_price[$cart['product_id']] = bcadd($product_price[$cart['product_id']] ?? 0, $_price, 2); + } + + if (!$temp1) continue; + + $number = $this->productByTempNumber($cart); + if ($number <= 0) continue; + $cart['temp_number'] = $number; + + if ($order_model || !$temp1 || ($cart['product_type'] == 10 && $cart['productDiscount']['free_shipping'])) { + continue; + } + + $free = $temp1['free'][0] ?? null; + $region = $temp1['region'][0] ?? null; + + if (!$cart['product']['delivery_free'] && !$isTake && (!$address || !$cart['product']['temp'] || ($temp1['undelivery'] == 2 && !$free && (!$region || !$region['city_id'])))) { + $cart['undelivered'] = true; + $noDeliver = true; + continue; + } + $cart['undelivered'] = (!$isTake) && $temp1['undelivery'] == 1 && isset($temp1['undelives']); + $fn[] = function () use ($cart) { + unset($cart['product']['temp']); + }; + + if ($cart['undelivered']) { + $noDeliver = true; + continue; + } + if ($cart['product']['delivery_free']) { + continue; + } + $tempId = $cart['product']['temp_id']; + if (!isset($postageRule[$tempId])) { + $postageRule[$tempId] = [ + 'free' => null, + 'region' => null, + 'cart' => [], + 'price' => 0, + ]; + } + + $freeRule = $postageRule[$tempId]['free']; + $regionRule = $postageRule[$tempId]['region']; + $postageRule[$tempId]['cart'][] = $cart['cart_id']; + $postageRule[$tempId]['price'] = bcadd($postageRule[$tempId]['price'], $cart['price'], 2); + + if ($temp1['appoint'] && $free) { + if (!isset($freeRule)) { + $freeRule = $free; + $freeRule['cart_price'] = 0; + $freeRule['cart_number'] = 0; + } + $freeRule['cart_number'] = bcadd($freeRule['cart_number'], $number, 2); + $freeRule['cart_price'] = bcadd($freeRule['cart_price'], $price, 2); + } + + if ($region) { + if (!isset($regionRule)) { + $regionRule = $region; + $regionRule['cart_price'] = 0; + $regionRule['cart_number'] = 0; + } + $regionRule['cart_number'] = bcadd($regionRule['cart_number'], $number, 2); + $regionRule['cart_price'] = bcadd($regionRule['cart_price'], $price, 2); + } + $postageRule[$tempId]['free'] = $freeRule; + $postageRule[$tempId]['region'] = $regionRule; + } + unset($cart); + + if (!$isTake) { + //计算运费 + foreach ($postageRule as $item) { + $freeRule = $item['free']; + if ($freeRule && $freeRule['cart_number'] >= $freeRule['number'] && $freeRule['cart_price'] >= $freeRule['price']) + continue; + if (!$item['region']) continue; + $regionRule = $item['region']; + $postage = $regionRule['first_price']; + if ($regionRule['first'] > 0 && $regionRule['cart_number'] > $regionRule['first']) { + $num = ceil(bcdiv(bcsub($regionRule['cart_number'], $regionRule['first'], 2), $regionRule['continue'], 2)); + $postage = bcadd($postage, bcmul($num, $regionRule['continue_price'], 2), 2); + } + $postage_price = bcadd($postage_price, $postage, 2); + $cartNum = count($item['cart']); + //计算每个商品的运费比例 + foreach ($merchantCart['list'] as &$cart) { + if (in_array($cart['cart_id'], $item['cart'])) { + if (--$cartNum) { + $cart['postage_price'] = bcmul($postage, bcdiv($cart['temp_number'], $regionRule['cart_number'], 3), 2); + $postage = bcsub($postage, $cart['postage_price'], 2); + } else { + $cart['postage_price'] = $postage; + } + } + } + unset($cart); + } + unset($item); + } + + $coupon_price = 0; + $use_coupon_product = []; + $use_store_coupon = 0; + + $useCouponFlag = isset($useCoupon[$merchantCart['mer_id']]); + $merCouponIds = (array)($useCoupon[$merchantCart['mer_id']] ?? []); + $merCouponIds = array_reverse($merCouponIds); + $sortIds = $merCouponIds; + // $all_coupon_product = []; + unset($defaultSort); + $defaultSort = []; + if (count($merCouponIds)) { + foreach ($merchantCart['coupon'] as &$item) { + $defaultSort[] = &$item; + if (!in_array($item['coupon_user_id'], $sortIds, true)) { + $sortIds[] = $item['coupon_user_id']; + } + } + unset($item); + usort($merchantCart['coupon'], function ($a, $b) use ($sortIds) { + return array_search($a['coupon_user_id'], $sortIds) > array_search($b['coupon_user_id'], $sortIds) ? 1 : -1; + }); + } + + $pay_price = max(bcsub($valid_total_price, $coupon_price, 2), 0); + $_pay_price = $pay_price; + + + $productCouponRate = []; + $storeCouponRate = null; + $useCouponIds = []; + + + if ($use_store_coupon) { + $storeCouponRate = [ + 'rate' => $pay_price > 0 ? bcdiv($use_store_coupon['coupon_price'], $pay_price, 4) : 1, + 'coupon_price' => $use_store_coupon['coupon_price'], + 'price' => $coupon_price + ]; + $useCouponIds[] = $use_store_coupon['coupon_user_id']; + } + + //计算单个商品实际支付金额 + foreach ($merchantCart['list'] as $_k => &$cart) { + $cartTotalPrice = bcmul($this->cartByPrice($cart), $cart['cart_num'], 2); + $_cartTotalPrice = $cartTotalPrice; + + //单个商品实际支付金额 + $cart['coupon_price'] = bcsub($_cartTotalPrice, $cartTotalPrice, 2); + $cart['true_price'] = $cartTotalPrice; + } + $procure = $this->cartByPrice($cart, 1); + $procure_price = bcmul($cart['cart_num'], $procure, 2); + unset($cart, $_k); + $total_true_price = bcadd($_pay_price, $total_true_price, 2); + if (count($merchantCartList) > 1 || count($merchantCart['list']) > 1) { + $orderDeliveryStatus = $orderDeliveryStatus && $deliveryStatus; + } + $merchantCart['order'] = [ + 'true_price' => $_pay_price, + 'platform_coupon_price' => 0, + 'valid_total_price' => $valid_total_price, + 'total_price' => $total_price, + 'final_price' => $final_price, + 'down_price' => $down_price, + 'coupon_price' => $coupon_price, + 'platformCoupon' => $platformCoupon, + 'svip_coupon_merge' => $svip_coupon_merge, + 'postage_price' => $postage_price, + 'isTake' => intval($allowDelivery == false || count($takes) > 0), + 'total_num' => $total_num, + 'enabledCoupon' => $enabledCoupon, + 'useCouponIds' => $useCouponIds, + 'allow_take' => true, + 'allow_delivery' => $allowDelivery, + 'delivery_status' => $deliveryStatus, + 'svip_discount' => $total_svip_discount, + 'use_svip' => $use_svip, + 'procure_price' => $procure_price + ]; + $order_total_postage = bcadd($order_total_postage, $postage_price, 2); + $order_svip_discount = bcadd($total_svip_discount, $order_svip_discount, 2); + if (count($defaultSort)) { + $merchantCart['coupon'] = &$defaultSort; + } + } + unset($merchantCart); + + $usePlatformCouponId = 0; + $total_platform_coupon_price = 0; + $usePlatformCouponFlag = isset($useCoupon[0]); + + foreach ($merchantCartList as &$merchantCart) { + if (!$merchantCart['order']['use_svip']) + continue; + $totalMergePrice = 0; + foreach ($merchantCart['list'] as &$cart) { + if (!$cart['svip_coupon_merge']) { + $totalMergePrice = bcadd($totalMergePrice, $cart['true_price'], 2); + $cart['allow_price'] = $cart['true_price']; + } + } + unset($cart); + if ($totalMergePrice > 0) { + $total_true_price = bcadd($total_true_price, $totalMergePrice, 2); + $merchantCart['order']['valid_total_price'] = bcadd($merchantCart['order']['valid_total_price'], $totalMergePrice, 2); + $merchantCart['order']['true_price'] = $merchantCart['order']['valid_total_price']; + } + } + unset($merchantCart); + //计算平台券优惠金额 + // if ($total_true_price > 0) { + $StoreCouponUser = app()->make(StoreCouponUserRepository::class); + $platformCoupon = $StoreCouponUser->validUserPlatformCoupon($uid); + $platformConsumption = []; + //消费金 + if ($source == 103) { + $ConsumptionWhere = [ + 'uid' => $uid, + 'status' => 0, + 'is_fail' => 0 + ]; + + $platformConsumption = Db::name('store_consumption_user')->where($ConsumptionWhere)->limit(100)->order('create_time', 'desc') + ->field('coupon_user_id,uid,coupon_title,coupon_price,balance,start_time,end_time') + ->select()->each(function ($item) { + $item['describe'] = '仅限平台指定商家商品可使用'; + return $item; + }); + } + // } + + $usePlatformCouponId = 0; + $total_platform_coupon_price = 0; + + //积分配置 + $sysIntegralConfig = systemConfig(['integral_money', 'integral_status', 'integral_order_rate']); + $merIntegralFlag = false; + $order_total_integral = 0; + $order_total_integral_price = 0; + $order_total_give_integral = 0; + $allow_no_address = true; + + foreach ($merchantCartList as &$merchantCart) { + $merchantCart['take'] = [ + 'mer_integral_rate' => 0, + 'mer_integral_status' => 0, + ]; + $allow_no_address = $allow_no_address && $merchantCart['order']['isTake']; + foreach ($merchantCart['config'] as $config) { + $merchantCart['take'][$config['config_key']] = $config['value']; + } + $merIntegralConfig = $merchantCart['take']; + unset($merchantCart['config']); + $merIntegralConfig['mer_integral_rate'] = min(1, $merIntegralConfig['mer_integral_rate'] > 0 ? bcdiv($merIntegralConfig['mer_integral_rate'], 100, 4) : $merIntegralConfig['mer_integral_rate']); + $total_integral = 0; + $total_integral_price = 0; + $merIntegralFlag = $merIntegralFlag || ((bool)$merIntegralConfig['mer_integral_status']); + $integralFlag = $useIntegral && $sysIntegralConfig['integral_status'] && $sysIntegralConfig['integral_money'] > 0 && $merIntegralConfig['mer_integral_status']; + + $order_total_integral = bcadd($order_total_integral, $total_integral, 0); + $order_total_integral_price = bcadd($order_total_integral_price, $total_integral_price, 2); + + $_pay_price = $merchantCart['order']['true_price']; + $valid_total_price = $merchantCart['order']['valid_total_price']; + $total_price = $merchantCart['order']['total_price']; + $final_price = $merchantCart['order']['final_price']; + $down_price = $merchantCart['order']['down_price']; + $coupon_price = $merchantCart['order']['coupon_price']; + $postage_price = $merchantCart['order']['postage_price']; + + //计算订单商品金额 + $org_price = bcadd(bcsub($total_price, $valid_total_price, 2), max($_pay_price, 0), 2); + if ($presellType == 2) { + $org_price = max(bcsub($org_price, $final_price, 2), $down_price); + } + + //获取可优惠金额 + $coupon_price = min($coupon_price, bcsub($total_price, $down_price, 2)); + $order_coupon_price = bcadd($coupon_price, $order_coupon_price, 2); + + //计算订单金额 + if ($order_type != 2 || $presellType != 2) { + $pay_price = bcadd($postage_price, $org_price, 2); + } else { + $pay_price = $org_price; + } + //计算总红包金额 + if ($consumption_id > 0 ) { + if ($this->store_consumption_user) { + if ($this->store_consumption_user['type'] == 2 && $pay_price>=6) { + $a = bcdiv($pay_price, 6); + if ($this->balance > $a) { + $pay_price = bcsub($pay_price, $a, 2); + $this->consumption_money = bcadd($this->consumption_money, $a, 2); + $this->balance = bcsub($this->balance, $a, 2); + } else { + $pay_price = bcsub($pay_price, $this->balance, 2); + $this->consumption_money = bcadd($this->consumption_money, $this->balance, 2); + $this->balance = 0; + } + } + if ($this->store_consumption_user['type'] == 1) { + if ($pay_price > $this->balance) { + $pay_price = bcsub($pay_price, $this->balance, 2); + $this->consumption_money = bcadd($this->consumption_money, $this->balance, 2); + $this->balance=0; + } else { + $pay_price = 0; + $this->consumption_money = bcadd($this->consumption_money, $pay_price, 2); + $this->balance = $pay_price; + + } + } + + } + } + + $giveIntegralFlag = $sysIntegralConfig['integral_status'] && $sysIntegralConfig['integral_order_rate'] > 0; + $total_give_integral = 0; + + foreach ($fn as $callback) { + $callback(); + } + $merchantCart['order']['order_type'] = $order_type; + $merchantCart['order']['total_give_integral'] = $total_give_integral; + $merchantCart['order']['total_integral_price'] = $total_integral_price; + $merchantCart['order']['total_integral'] = $total_integral; + $merchantCart['order']['org_price'] = $org_price; + $merchantCart['order']['pay_price'] = $pay_price; + $merchantCart['order']['coupon_price'] = $coupon_price; + $merchantCart['order']['postage_price'] = $merchantCart['order']['postage_price']; + $merchantCart['order']['procure_price'] = $merchantCart['order']['procure_price']; + $merchantCart['order']['consumption_id'] = $consumption_id; + $merchantCart['order']['consumption_money'] = $this->consumption_money; + + $order_price = bcadd($order_price, $pay_price, 2); + $order_total_price = bcadd($order_total_price, $total_price, 2); + } + unset($merchantCart); + + if ($order_model) { + $allow_no_address = false; + } + + foreach ($merchantCartList as &$merchantCart) { + foreach ($merchantCart['list'] as &$cart) { + $cart['total_price'] = bcadd($cart['total_price'], $cart['svip_discount'], 2); + } + unset($cart); + $merchantCart['order']['total_price'] = bcadd($merchantCart['order']['total_price'], $merchantCart['order']['svip_discount'], 2); + $order_total_price = bcadd($order_total_price, $merchantCart['order']['svip_discount'], 2); + } + unset($merchantCart); + $status = ($address || $order_model || $allow_no_address) ? ($noDeliver ? 'noDeliver' : 'finish') : 'noAddress'; + $order = $merchantCartList; + $consumption_money = $this->consumption_money; + $total_price = $order_total_price; + $openIntegral = $merIntegralFlag && !$order_type && $sysIntegralConfig['integral_status'] && $sysIntegralConfig['integral_money'] > 0; + $total_coupon = bcadd($order_svip_discount, bcadd(bcadd($total_platform_coupon_price, $order_coupon_price, 2), $order_total_integral_price, 2), 2); + $is_self_pickup = true; + if($order_type=='balance' &&$source!=103 &&$createOrder==true){ + throw new ValidateException('余额支付只能用于里海云仓'); + } + return compact( + 'is_self_pickup', + 'order_type', + 'source', + 'order_model', + 'order_extend', + 'order_total_postage', + 'order_price', + 'total_price', + 'community', + 'enabledPlatformCoupon', + 'platformCoupon', + 'usePlatformCouponId', + 'order_total_integral', + 'order_total_integral_price', + 'order_total_give_integral', + 'order_svip_discount', + 'total_platform_coupon_price', + 'total_coupon', + 'order_coupon_price', + 'order', + 'status', + 'address', + 'openIntegral', + 'useIntegral', + 'key', + 'platformConsumption', + 'consumption_money', + 'consumption_id' + ) + ['allow_address' => !$allow_no_address, 'order_delivery_status' => $orderDeliveryStatus]; + } + + public function v2CreateOrder(int $pay_type, $user, array $cartId, array $extend, array $mark, array $receipt_data, array $takes = null, array $useCoupon = null, bool $useIntegral = false, int $addressId = null, array $post, int $product_type = 0, $consumption_id = 0) + { + $uid = $user->uid; + $orderInfo = $this->v2CartIdByOrderInfo($user, $cartId, $takes, $useCoupon, $useIntegral, $addressId, true, $consumption_id); + $order_model = $orderInfo['order_model']; + $order_extend = $orderInfo['order_extend']; + if (!$orderInfo['order_delivery_status']) { + throw new ValidateException('部分商品配送方式不一致,请单独下单'); + } + if ($orderInfo['order_price'] > 1000000) { + throw new ValidateException('支付金额超出最大限制'); + } + if ($orderInfo['status'] == 'noDeliver') throw new ValidateException('商品不支持该区域'); + if ($orderInfo['status'] == 'noAddress') throw new ValidateException('请选择地址'); + if (!$order_model && $orderInfo['allow_address']) { + if (!$orderInfo['address']) throw new ValidateException('请选择正确的收货地址'); + if (!$orderInfo['address']['province_id']) throw new ValidateException('请完善收货地址信息'); + $extend = []; + } else if (count($order_extend)) { + $extend = app()->make(OrderVirtualFieldValidate::class)->load($order_extend, $extend); + } else { + $extend = []; + } + $orderType = $orderInfo['order_type']; + if ($orderType == 0 && $pay_type == StoreGroupOrder::PAY_TYPE_CREDIT_BUY) { + throw new ValidateException('该商品不支持先货后款'); + } + if (!in_array($orderType, [0, 98, 99]) && (count($orderInfo['order']) > 1 || ($orderType != 10 && count($orderInfo['order'][0]['list']) > 1))) { + throw new ValidateException('活动商品请单独购买'); + } + + $merchantCartList = $orderInfo['order']; + $cartSpread = 0; + $hasTake = false; + + foreach ($merchantCartList as $merchantCart) { + if ($merchantCart['order']['isTake']) { + $hasTake = true; + } + //检查发票状态 + if (isset($receipt_data[$merchantCart['mer_id']]) && !$merchantCart['openReceipt']) + throw new ValidateException('该店铺不支持开发票'); + if ($pay_type == StoreGroupOrder::PAY_TYPE_CREDIT_BUY && !$merchantCart['credit_buy']) { + throw new ValidateException("{$merchantCart['mer_name']}不支持先货后款"); + } + + foreach ($merchantCart['list'] as $cart) { + if (!$cartSpread && $cart['spread_id']) { + $cartSpread = $cart['spread_id']; + } + } + } + if (($orderType == 98 || $orderType == 99) && count($merchantCartList) > 1) { + throw new ValidateException('采购、委托商品不支持跨店购买'); + } + if ($hasTake) { + app()->make(UserAddressValidate::class)->scene('take')->check($post); + } + + if ($cartSpread) { + app()->make(UserRepository::class)->bindSpread($user, $cartSpread); + } + + $isSelfBuy = $user->is_promoter && systemConfig('extension_self') ? 1 : 0; + if ($isSelfBuy) { + $spreadUser = $user; + $topUser = $user->valid_spread; + } else { + $spreadUser = $user->valid_spread; + $topUser = $user->valid_top; + } + $spreadUid = $spreadUser->uid ?? 0; + $topUid = $topUser->uid ?? 0; + + $merchantRepository = app()->make(MerchantRepository::class); + $giveCouponIds = []; + $ex = systemConfig('extension_status'); //是否开启分销 + $address = $orderInfo['address']; + $allUseCoupon = $orderInfo['usePlatformCouponId'] ? [$orderInfo['usePlatformCouponId']] : []; + $totalNum = 0; + $totalPostage = 0; + $totalCost = 0; + $cartIds = []; + $orderList = []; + foreach ($merchantCartList as $k => $merchantCart) { + $cost = 0; + $total_extension_one = 0; + $total_extension_two = 0; + //计算佣金和赠送的优惠券 + foreach ($merchantCart['list'] as &$cart) { + $cartIds[] = $cart['cart_id']; + $giveCouponIds = array_merge($giveCouponIds, $cart['product']['give_coupon_ids'] ?: []); + $cart['cost'] = $cart['productAttr']['cost']; + $cost = bcadd(bcmul($cart['cost'], $cart['cart_num'], 2), $cost, 2); + $extension_one = 0; + $extension_two = 0; + $cart['extension_one'] = $extension_one; + $cart['extension_two'] = $extension_two; + $total_extension_one = bcadd($total_extension_one, bcmul($extension_one, $cart['cart_num'], 2), 2); + $total_extension_two = bcadd($total_extension_two, bcmul($extension_two, $cart['cart_num'], 2), 2); + } + unset($cart); + + $rate = 0; + if ($merchantCart['commission_rate'] > 0) { + $rate = $merchantCart['commission_rate']; + } else if (isset($merchantCart['merchantCategory']['commission_rate']) && $merchantCart['merchantCategory']['commission_rate'] > 0) { + $rate = bcmul($merchantCart['merchantCategory']['commission_rate'], 100, 4); + } + $user_address = isset($address) ? ($address['province'] . $address['city'] . $address['district'] . $address['street'] . $address['village'] . $address['brigade'] . $address['detail']) : ''; + $user_address_code = isset($address) ? ($address['province_code'] . ',' . $address['city_code'] . ',' . $address['district_code'] . ',' . $address['street_code'] . ',' . $address['village_code'] . ',' . $address['brigade_id']) : ''; + //整理订单数据 + $_order = [ + 'cartInfo' => $merchantCart, + 'activity_type' => $orderInfo['order_type'], + 'source' => $orderInfo['source'] ?? 2, + 'commission_rate' => (float)$rate, + 'order_type' => $merchantCart['order']['isTake'] ? 1 : 0, + 'is_virtual' => $order_model ? 1 : 0, + 'extension_one' => $total_extension_one, + 'extension_two' => $total_extension_two, + 'order_sn' => $this->getNewOrderId(StoreOrderRepository::TYPE_SN_ORDER) . ($k + 1), + 'uid' => $uid, + 'spread_uid' => $spreadUid, + 'top_uid' => $topUid, + 'is_selfbuy' => $isSelfBuy, + 'real_name' => $merchantCart['order']['isTake'] ? $post['real_name'] : ($address['real_name'] ?? ''), + 'user_phone' => $merchantCart['order']['isTake'] ? $post['phone'] : ($address['phone'] ?? ''), + 'user_address' => $user_address, + 'user_address_code' => $user_address_code, + 'cart_id' => implode(',', array_column($merchantCart['list'], 'cart_id')), + 'total_num' => $merchantCart['order']['total_num'], + 'total_price' => $merchantCart['order']['total_price'], + 'total_postage' => $merchantCart['order']['postage_price'], + 'pay_postage' => $merchantCart['order']['postage_price'], + 'svip_discount' => $merchantCart['order']['svip_discount'], + 'pay_price' => $merchantCart['order']['pay_price'], + 'integral' => $merchantCart['order']['total_integral'], + 'integral_price' => $merchantCart['order']['total_integral_price'], + 'give_integral' => $merchantCart['order']['total_give_integral'], + 'mer_id' => $merchantCart['mer_id'], + 'cost' => $cost, + 'order_extend' => count($extend) ? json_encode($extend, JSON_UNESCAPED_UNICODE) : '', + 'coupon_id' => implode(',', $merchantCart['order']['useCouponIds']), + 'mark' => $mark[$merchantCart['mer_id']] ?? '', + 'coupon_price' => bcadd($merchantCart['order']['coupon_price'], $merchantCart['order']['platform_coupon_price'], 2), + 'platform_coupon_price' => $merchantCart['order']['platform_coupon_price'], + 'pay_type' => $pay_type, + 'procure_price' => $merchantCart['order']['procure_price'], + 'consumption_id' => $merchantCart['order']['consumption_id'], + 'consumption_money' => $merchantCart['order']['consumption_money'], + ]; + $allUseCoupon = array_merge($allUseCoupon, $merchantCart['order']['useCouponIds']); + $orderList[] = $_order; + $totalPostage = bcadd($totalPostage, $_order['total_postage'], 2); + $totalCost = bcadd($totalCost, $cost, 2); + $totalNum += $merchantCart['order']['total_num']; + } + //总订单组 + $groupOrder = [ + 'uid' => $uid, + 'group_order_sn' => count($orderList) === 1 ? $orderList[0]['order_sn'] : ($this->getNewOrderId(StoreOrderRepository::TYPE_SN_ORDER) . '0'), + 'total_postage' => $totalPostage, + 'total_price' => $orderInfo['total_price'], + 'total_num' => $totalNum, + 'real_name' => $address['real_name'] ?? '', + 'user_phone' => $address['phone'] ?? '', + 'user_address' => $user_address, + 'pay_price' => $orderInfo['order_price'], + 'coupon_price' => bcadd($orderInfo['total_platform_coupon_price'], $orderInfo['order_coupon_price'], 2), + 'pay_postage' => $totalPostage, + 'cost' => $totalCost, + 'coupon_id' => $orderInfo['usePlatformCouponId'] > 0 ? $orderInfo['usePlatformCouponId'] : '', + 'pay_type' => $pay_type, + 'give_coupon_ids' => $giveCouponIds, + 'integral' => $orderInfo['order_total_integral'], + 'integral_price' => $orderInfo['order_total_integral_price'], + 'give_integral' => $orderInfo['order_total_give_integral'], + 'consumption_id' => $orderInfo['consumption_id'], + 'consumption_money' => $orderInfo['consumption_money'], + ]; + event('order.create.before', compact('groupOrder', 'orderList')); + $group = Db::transaction(function () use ($ex, $user, $topUid, $spreadUid, $uid, $receipt_data, $cartIds, $allUseCoupon, $groupOrder, $orderList, $orderInfo) { + $storeGroupOrderRepository = app()->make(StoreGroupOrderRepository::class); + $storeCartRepository = app()->make(StoreCartRepository::class); + /** @var ProductAttrValueRepository $attrValueRepository */ + $attrValueRepository = app()->make(ProductAttrValueRepository::class); + /** @var ProductRepository $productRepository */ + $productRepository = app()->make(ProductRepository::class); + $storeOrderProductRepository = app()->make(StoreOrderProductRepository::class); + $couponUserRepository = app()->make(StoreCouponUserRepository::class); + //订单记录 + $storeOrderStatusRepository = app()->make(StoreOrderStatusRepository::class); + $userMerchantRepository = app()->make(UserMerchantRepository::class); + + //减库存 + foreach ($orderList as $order) { + foreach ($order['cartInfo']['list'] as $cart) { + if (!isset($uniqueList[$cart['productAttr']['product_id'] . $cart['productAttr']['unique']])) + $uniqueList[$cart['productAttr']['product_id'] . $cart['productAttr']['unique']] = true; + else + throw new ValidateException('购物车商品信息重复'); + + try { + if ($cart['product_type'] == '1') { + $attrValueRepository->descSkuStock($cart['product']['old_product_id'], $cart['productAttr']['sku'], $cart['cart_num']); + $productRepository->descStock($cart['product']['old_product_id'], $cart['cart_num']); + } else if ($cart['product_type'] == '2') { + $productPresellSkuRepository = app()->make(ProductPresellSkuRepository::class); + $productPresellSkuRepository->descStock($cart['productPresellAttr']['product_presell_id'], $cart['productPresellAttr']['unique'], $cart['cart_num']); + $attrValueRepository->descStock($cart['productAttr']['product_id'], $cart['productAttr']['unique'], $cart['cart_num']); + $productRepository->descStock($cart['product']['product_id'], $cart['cart_num']); + } else if ($cart['product_type'] == '3') { + app()->make(ProductAssistSkuRepository::class)->descStock($cart['productAssistAttr']['product_assist_id'], $cart['productAssistAttr']['unique'], $cart['cart_num']); + $productRepository->descStock($cart['product']['old_product_id'], $cart['cart_num']); + $attrValueRepository->descStock($cart['product']['old_product_id'], $cart['productAttr']['unique'], $cart['cart_num']); + } else if ($cart['product_type'] == '4') { + app()->make(ProductGroupSkuRepository::class)->descStock($cart['activeSku']['product_group_id'], $cart['activeSku']['unique'], $cart['cart_num']); + $productRepository->descStock($cart['product']['old_product_id'], $cart['cart_num']); + $attrValueRepository->descStock($cart['product']['old_product_id'], $cart['productAttr']['unique'], $cart['cart_num']); + } else { + if ($cart['source'] == StoreCartDao::SOURCE_COMMUNITY_RESALE) { + /** @var CommunityRepository $communityRepository */ + $communityRepository = app()->make(CommunityRepository::class); + $communityRepository->saleOrCancel($cart['source_id']); + $attrValueRepository->incSales($cart['productAttr']['product_id'], $cart['productAttr']['unique'], $cart['cart_num']); + $productRepository->incSales($cart['product']['product_id'], $cart['cart_num']); + } else { + $attrValueRepository->descStock($cart['productAttr']['product_id'], $cart['productAttr']['unique'], $cart['cart_num']); + $productRepository->descStock($cart['product']['product_id'], $cart['cart_num']); + } + /** @var PurchaseRecordRepository $purchaseRecordRepo */ + $purchaseRecordRepo = app()->make(PurchaseRecordRepository::class); + $purchaseRecordRepo->incSalesVolume($cart['product']['product_id'], $cart['productAttr']['unique'], $cart['cart_num']); + if ($cart['integral'] && $cart['integral']['use'] > 0) { + $productRepository->incIntegral($cart['product']['product_id'], $cart['integral']['use'], $cart['integral']['price']); + } + } + } catch (\Exception $e) { + throw new ValidateException('库存不足'); + } + } + } + + if ($orderInfo['order_type'] == 10 && !app()->make(StoreDiscountRepository::class)->decStock($orderList[0]['cartInfo']['list'][0]['source_id'])) { + throw new ValidateException('套餐库不足'); + } + + //修改购物车状态 + $storeCartRepository->updates($cartIds, [ + 'is_pay' => 1 + ]); + + //使用优惠券 + if (count($allUseCoupon)) { + $couponUserRepository->updates($allUseCoupon, [ + 'use_time' => date('Y-m-d H:i:s'), + 'status' => 1 + ]); + } + + //创建订单 + $groupOrder = $storeGroupOrderRepository->create($groupOrder); + $bills = []; + $consumptionDetails=[]; + if ($groupOrder['integral'] > 0) { + $user->integral = bcsub($user->integral, $groupOrder['integral'], 0); + app()->make(UserBillRepository::class)->decBill($user['uid'], 'integral', 'deduction', [ + 'link_id' => $groupOrder['group_order_id'], + 'status' => 1, + 'title' => '购买商品', + 'number' => $groupOrder['integral'], + 'mark' => '购买商品使用积分抵扣' . floatval($groupOrder['integral_price']) . '元', + 'balance' => $user->integral + ]); + $user->save(); + } + + if ($groupOrder['consumption_money'] > 0) { + $balance = 0; + $store_consumption_user = Db::name('store_consumption_user')->where('uid',$uid)->where('coupon_user_id', $groupOrder['consumption_id'])->where('status', 0)->find(); + if ($store_consumption_user) { + if ($store_consumption_user['type'] == 1) { + if ($store_consumption_user['balance'] > $groupOrder['consumption_money']) { + $balance = bcsub($store_consumption_user['balance'], $groupOrder['consumption_money'], 2); + $balanceArr = ['balance' => $balance]; + Db::name('store_consumption_user')->where('coupon_user_id', $store_consumption_user['coupon_user_id'])->update($balanceArr); + } else { + $balanceArr = ['balance' => 0, 'status' => 1]; + Db::name('store_consumption_user')->where('coupon_user_id', $store_consumption_user['coupon_user_id'])->update($balanceArr); + } + } + } + app()->make(UserBillRepository::class)->decBill($user['uid'], 'consumption', 'deduction', [ + 'link_id' => $groupOrder['group_order_id'], + 'status' => 1, + 'title' => '购买商品', + 'number' => $groupOrder['consumption_money'], + 'mark' => '购买商品使用红包抵扣' . floatval($groupOrder['consumption_money']) . '元', + 'balance' => $balance + ]); + } + + foreach ($orderList as $k => $order) { + $orderList[$k]['group_order_id'] = $groupOrder->group_order_id; + } + + $orderProduct = []; + $orderStatus = []; + foreach ($orderList as $order) { + if ($order['pay_type'] == StoreGroupOrder::PAY_TYPE_CREDIT_BUY) { + $order['status'] = StoreOrder::STATUS_WAIT_CONFIRM; + } + $cartInfo = $order['cartInfo']; + unset($order['cartInfo']); + //创建子订单 + $_order = $this->dao->create($order); + + if ($order['integral'] > 0) { + $bills[] = [ + 'uid' => $uid, + 'link_id' => $_order->order_id, + 'pm' => 0, + 'title' => '积分抵扣', + 'category' => 'mer_integral', + 'type' => 'deduction', + 'number' => $order['integral'], + 'balance' => $user->integral, + 'mark' => '购买商品使用' . $order['integral'] . '积分抵扣' . floatval($order['integral_price']) . '元', + 'mer_id' => $order['mer_id'], + 'status' => 1 + ]; + } + if ($order['consumption_money'] > 0) { + $consumptionDetails[] = [ + 'user_id' => $uid, + 'order_id' => $_order->order_id, + 'group_order_id' => $groupOrder->group_order_id, + 'coupon_user_id' => $order['consumption_id'], + 'type' => 1, + 'amount' => $order['consumption_money'], + 'pay_price' => $order['pay_price'], + 'create_time' => time() + ]; + } + + //创建发票信息 + if (isset($receipt_data[$_order['mer_id']])) { + app()->make(StoreOrderReceiptRepository::class)->add($receipt_data[$_order['mer_id']], $_order); + } + + $orderStatus[] = [ + 'order_id' => $_order->order_id, + 'order_sn' => $_order->order_sn, + 'type' => $storeOrderStatusRepository::TYPE_ORDER, + 'change_message' => '订单生成', + 'change_type' => $storeOrderStatusRepository::ORDER_STATUS_CREATE, + 'uid' => $user->uid, + 'nickname' => $user->nickname, + 'user_type' => $storeOrderStatusRepository::U_TYPE_USER, + ]; + + foreach ($cartInfo['list'] as $cart) { + $productPrice = $cart['true_price']; + $extension_one = $cart['extension_one']; + $extension_two = $cart['extension_two']; + + //计算预售订单尾款 + if ($cartInfo['order']['order_type'] == 2) { + $finalPrice = max(bcsub($cartInfo['order']['final_price'], $cartInfo['order']['coupon_price'], 2), 0); + $allFinalPrice = $order['order_type'] ? $finalPrice : bcadd($finalPrice, $order['pay_postage'], 2); + if ($cart['productPresell']['presell_type'] == 1) { + $productPrice = bcsub($cartInfo['order']['pay_price'], $order['pay_postage'], 2); + } else { + $productPrice = bcadd($cartInfo['order']['pay_price'], $finalPrice, 2); + } + //生成尾款订单 + if ($cart['productPresell']['presell_type'] == 2) { + $presellOrderRepository = app()->make(PresellOrderRepository::class); + $presellOrderRepository->create([ + 'uid' => $uid, + 'order_id' => $_order->order_id, + 'mer_id' => $_order->mer_id, + 'final_start_time' => $cart['productPresell']['final_start_time'], + 'final_end_time' => $cart['productPresell']['final_end_time'], + 'pay_price' => $allFinalPrice, + 'presell_order_sn' => $this->getNewOrderId(StoreOrderRepository::TYPE_SN_PRESELL) + ]); + } + app()->make(ProductPresellSkuRepository::class)->incCount($cart['source_id'], $cart['productAttr']['unique'], 'one_take'); + } + + $order_cart = [ + 'product' => $cart['product'], + 'productAttr' => $cart['productAttr'], + 'product_type' => $cart['product_type'] + ]; + + if ($cart['product_type'] == '2') { + $order_cart['productPresell'] = $cart['productPresell']; + $order_cart['productPresellAttr'] = $cart['productPresellAttr']; + $order_cart['final_extension_one'] = $cart['final_extension_one'] ?? 0; + $order_cart['final_extension_two'] = $cart['final_extension_two'] ?? 0; + $order_cart['presell_extension_one'] = $cart['presell_extension_one'] ?? 0; + $order_cart['presell_extension_two'] = $cart['presell_extension_two'] ?? 0; + } else if ($cart['product_type'] == '3') { + $order_cart['productAssistAttr'] = $cart['productAssistAttr']; + $order_cart['productAssistSet'] = $cart['productAssistSet']; + } else if ($cart['product_type'] == '4') { + $order_cart['activeSku'] = $cart['activeSku']; + } else if ($cart['product_type'] == '10') { + $order_cart['active'] = $cart['productDiscount']; + $order_cart['activeSku'] = $cart['productDiscountAttr']; + } + $product_source_id = 0; + $product_mer_id = 0; + if ($cart['source'] == 103) { + $old_product_id = Db::name('store_product')->where('product_id', $cart['product_id'])->value('old_product_id'); + if ($old_product_id) { + $old_mer_id = Db::name('store_product')->where('product_id', $old_product_id)->value('mer_id'); + $product_mer_id = $old_mer_id; + $product_source_id = $old_product_id; + } + } + $orderProduct[] = [ + 'order_id' => $_order->order_id, + 'cart_id' => $cart['cart_id'], + 'uid' => $uid, + 'product_id' => $cart['product_id'], + 'activity_id' => $cart['source'] >= 2 ? $cart['source_id'] : $cart['product_id'], + 'total_price' => $cart['total_price'], + 'product_price' => $productPrice, + 'extension_one' => $extension_one, + 'extension_two' => $extension_two, + 'postage_price' => $cart['postage_price'], + 'svip_discount' => $cart['svip_discount'], + 'cost' => $cart['cost'], + 'coupon_price' => $cart['coupon_price'], + 'platform_coupon_price' => $cart['platform_coupon_price'], + 'product_sku' => $cart['productAttr']['unique'], + 'product_num' => $cart['cart_num'], + 'refund_num' => $cart['cart_num'], + 'integral_price' => $cart['integral']['price'] ?? 0, + 'integral' => $cart['integral'] ? bcdiv($cart['integral']['use'], $cart['cart_num'], 0) : 0, + 'integral_total' => $cart['integral'] ? $cart['integral']['use'] : 0, + 'product_type' => $cart['product_type'], + 'source' => $cart['source'], + 'source_id' => $cart['source_id'], + 'cart_info' => json_encode($order_cart), + 'product_source_id' => $product_source_id, + 'product_mer_id' => $product_mer_id, + ]; + } + + $userMerchantRepository->getInfo($uid, $order['mer_id']); + app()->make(MerchantRepository::class)->incSales($order['mer_id'], $order['total_num']); + } + + if (count($bills) > 0) { + app()->make(UserBillRepository::class)->insertAll($bills); + } + if (count($consumptionDetails) > 0) { + Db::name('store_consumption_detail')->insertAll($consumptionDetails); + } + $storeOrderStatusRepository->batchCreateLog($orderStatus); + $storeOrderProductRepository->insertAll($orderProduct); + event('order.create', compact('groupOrder')); + return $groupOrder; + }); + foreach ($merchantCartList as $merchantCart) { + foreach ($merchantCart['list'] as $cart) { + if (($cart['productAttr']['stock'] - $cart['cart_num']) < (int)merchantConfig($merchantCart['mer_id'], 'mer_store_stock')) { + SwooleTaskService::merchant('notice', [ + 'type' => 'min_stock', + 'data' => [ + 'title' => '库存不足', + 'message' => $cart['product']['store_name'] . '(' . $cart['productAttr']['sku'] . ')库存不足', + 'id' => $cart['product']['product_id'] + ] + ], $merchantCart['mer_id']); + } + } + } + // Queue::push(SendSmsJob::class, ['tempId' => 'ORDER_CREATE', 'id' => $group->group_order_id, + // 'activity_type'=>$group->activity_type,'order_id'=>$group->order_id]); + return $group; + } +}