From 9ebdb24de7a74edff6bc39bff5a131ec9bc48705 Mon Sep 17 00:00:00 2001 From: mkm <727897186@qq.com> Date: Fri, 10 Nov 2023 11:07:59 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E5=BA=97=E9=93=BA=E6=9F=A5?= =?UTF-8?q?=E7=9C=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../order/StoreOrderCreateRepository.php.bak | 1360 ---------- .../store/order/StoreOrderRepository.php.bak | 2280 ----------------- .../system/merchant/MerchantRepository.php | 2 +- .../admin/system/merchant/Merchant.php.bak | 300 --- 4 files changed, 1 insertion(+), 3941 deletions(-) delete mode 100644 app/common/repositories/store/order/StoreOrderCreateRepository.php.bak delete mode 100644 app/common/repositories/store/order/StoreOrderRepository.php.bak delete mode 100644 app/controller/admin/system/merchant/Merchant.php.bak diff --git a/app/common/repositories/store/order/StoreOrderCreateRepository.php.bak b/app/common/repositories/store/order/StoreOrderCreateRepository.php.bak deleted file mode 100644 index d8ec8c4d..00000000 --- a/app/common/repositories/store/order/StoreOrderCreateRepository.php.bak +++ /dev/null @@ -1,1360 +0,0 @@ -uid; - $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; - //虚拟订单 - $order_model = 0; - //虚拟订单自定义数据 - $order_extend = []; - //检查商品类型, 活动商品只能单独购买 - foreach ($merchantCartList as $merchantCart) { - 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']; - if ($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); - } - } - } - unset($merchantCart, $cart); - - $order_price = 0; - $total_true_price = 0; - $order_total_price = 0; - $order_coupon_price = 0; - $noDeliver = false; - $presellType = 0; - $fn = []; - $enabledPlatformCoupon = !$order_type; - $order_total_postage = 0; - - - //套餐订单 - 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; - // 循环计算每个店铺的订单数据 - foreach ($merchantCartList as &$merchantCart) { - $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'] = $proTake; - $merchantCart['list'][$k]['allow_delivery'] = $proDelivery; - } else { - $merchantCart['list'][$k]['allow_take'] = $_merTake; - $merchantCart['list'][$k]['allow_delivery'] = $_merDelivery; - } - 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; - } - //加载商品数据 - foreach ($merchantCart['list'] as $cart) { - //预售订单 - if ($cart['product_type'] == 2) { - $cart->append(['productPresell', 'productPresellAttr']); - //助力订单 - } else if ($cart['product_type'] == 3) { - $cart->append(['productAssistAttr']); - //拼团订单 - } else if ($cart['product_type'] == 4) { - $cart->append(['activeSku']); - //套餐订单 - } else if ($cart['product_type'] == 10) { - $cart->append(['productDiscount', 'productDiscountAttr']); - } - - //如果是预售订单 获取预售的订单的首款,尾款预售类型 - if ($order_type == 2) { - $final_price = bcadd($final_price, bcmul($cart['cart_num'], $cart['productPresellAttr']['final_price'], 2), 2); - $presellType = $cart['productPresell']['presell_type']; - if ($presellType == 2) - $down_price = bcadd($down_price, bcmul($cart['cart_num'], $cart['productPresellAttr']['down_price'], 2), 2); - } - } - 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; - - $price = bcmul($cart['cart_num'], $this->cartByPrice($cart), 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; - }); - } - - //过滤不可用店铺优惠券 - foreach ($merchantCart['coupon'] as &$coupon) { - if (!$coupon['coupon']['type']) continue; - - $coupon['disabled'] = false; - $coupon['checked'] = false; - - if (count(array_intersect(array_column($coupon['product'], 'product_id'), array_keys($product_price))) == 0) { - $coupon['disabled'] = true; - continue; - } - if($svip_coupon_merge != '1' && $use_svip){ - $coupon['disabled'] = true; - continue; - } - $flag = false; - foreach ($coupon['product'] as $_product) { - if (isset($product_price[$_product['product_id']]) && $product_price[$_product['product_id']] >= $coupon['use_min_price']) { - $flag = true; - break; - } - } - if (!$flag) { - $coupon['disabled'] = true; - } -// if (!$coupon['disabled']) { -// $all_coupon_product[] = $coupon['coupon_user_id']; -// } - } - - unset($coupon); - - //if ($useCouponFlag && count(array_diff($all_coupon_product, $use_coupon_product))) { - // throw new ValidateException('请选择有效的商品券'); - //} - //计算商品券金额 - foreach ($merchantCart['coupon'] as &$coupon) { - if (!$coupon['coupon']['type']) continue; - if ($coupon['disabled']) continue; - - foreach ($coupon['product'] as $_product) { - if (isset($product_price[$_product['product_id']]) && $product_price[$_product['product_id']] >= $coupon['use_min_price']) { - if ($useCouponFlag) { - if (!in_array($coupon['coupon_user_id'], $merCouponIds) || isset($use_coupon_product[$_product['product_id']])) { - continue; - } - } else if (isset($use_coupon_product[$_product['product_id']])) { - continue; - } - $coupon_price = bcadd($coupon_price, $coupon['coupon_price'], 2); - $use_coupon_product[$_product['product_id']] = $coupon; - $coupon['checked'] = true; - break; - } - } - unset($_product); - } - unset($coupon); - $pay_price = max(bcsub($valid_total_price, $coupon_price, 2), 0); - $_pay_price = $pay_price; - //计算店铺券 - foreach ($merchantCart['coupon'] as &$coupon) { - if ($coupon['coupon']['type']) continue; - $coupon['checked'] = false; - $coupon['disabled'] = $pay_price <= 0; - if ($use_store_coupon || $pay_price <= 0) continue; - if($svip_coupon_merge != '1' && $use_svip){ - $coupon['disabled'] = true; - continue; - } - //店铺券 - if ($valid_total_price >= $coupon['use_min_price']) { - if ($useCouponFlag) { - if (!in_array($coupon['coupon_user_id'], $merCouponIds)) { - continue; - } - } - $use_store_coupon = $coupon; - $coupon_price = bcadd($coupon_price, $coupon['coupon_price'], 2); - $_pay_price = bcsub($_pay_price, $coupon['coupon_price'], 2); - $coupon['checked'] = true; - } else { - $coupon['disabled'] = true; - } - } - unset($coupon); - - $productCouponRate = []; - $storeCouponRate = null; - $useCouponIds = []; - //计算优惠占比 - foreach ($use_coupon_product as $productId => $coupon) { - $productCouponRate[$productId] = [ - 'rate' => $product_price[$productId] > 0 ? bcdiv($coupon['coupon_price'], $product_price[$productId], 4) : 1, - 'coupon_price' => $coupon['coupon_price'], - 'price' => $product_price[$productId] - ]; - $useCouponIds[] = $coupon['coupon_user_id']; - } - - 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; - if (!$cart['product_type'] && $cartTotalPrice > 0) { - if (isset($productCouponRate[$cart['product_id']])) { - //计算每个商品优惠金额(商品券) - if ($productCouponRate[$cart['product_id']]['rate'] >= 1) { - $cartTotalPrice = 0; - } else { - array_pop($product_cart); - if (!count($product_cart)) { - $cartTotalPrice = bcsub($cartTotalPrice, $productCouponRate[$cart['product_id']]['coupon_price'], 2); - $productCouponRate[$cart['product_id']]['coupon_price'] = 0; - } else { - $couponPrice = bcmul($cartTotalPrice, $productCouponRate[$cart['product_id']]['rate'], 2); - $cartTotalPrice = bcsub($cartTotalPrice, $couponPrice, 2); - $productCouponRate[$cart['product_id']]['coupon_price'] = bcsub($productCouponRate[$cart['product_id']]['coupon_price'], $couponPrice, 2); - } - } - } - - //(店铺券) - if ($storeCouponRate && $cartTotalPrice > 0) { - if ($storeCouponRate['rate'] >= 1) { - $cartTotalPrice = 0; - } else { - if (count($merchantCart['list']) == $_k + 1) { - $cartTotalPrice = bcsub($cartTotalPrice, $storeCouponRate['coupon_price'], 2); - } else { - $couponPrice = bcmul($cartTotalPrice, $storeCouponRate['rate'], 2); - $cartTotalPrice = bcsub($cartTotalPrice, $couponPrice, 2); - $storeCouponRate['coupon_price'] = bcsub($storeCouponRate['coupon_price'], $couponPrice, 2); - } - } - } - } - - //单个商品实际支付金额 - $cart['coupon_price'] = bcsub($_cartTotalPrice, $cartTotalPrice, 2); - $cart['true_price'] = $cartTotalPrice; - } - 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, - 'svip_coupon_merge' => $svip_coupon_merge, - 'postage_price' => $postage_price, - 'isTake' => $isTake, - 'total_num' => $total_num, - 'enabledCoupon' => $enabledCoupon, - 'useCouponIds' => $useCouponIds, - 'allow_take' => $merTake, - 'allow_delivery' => $merDelivery, - 'delivery_status' => $deliveryStatus, - 'svip_discount' => $total_svip_discount, - 'use_svip' => $use_svip, - ]; - $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 = $useCoupon[0] ?? 0; - $usePlatformCouponId = is_array($usePlatformCouponId) ? array_pop($usePlatformCouponId) : $usePlatformCouponId; - $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); - if ($enabledPlatformCoupon && count($platformCoupon)) { - - $catePriceLst = []; - $storePriceLst = []; - $_cartNum = 0; - - foreach ($merchantCartList as &$merchantCart) { - if ($merchantCart['order']['true_price'] <= 0) continue; - foreach ($merchantCart['list'] as &$cart) { - $_cartNum++; - if ($cart['product']['cate_id']) { - if (!isset($catePriceLst[$cart['product']['cate_id']])) { - $catePriceLst[$cart['product']['cate_id']] = ['price' => 0, 'cart' => []]; - } - $catePriceLst[$cart['product']['cate_id']]['price'] = bcadd($catePriceLst[$cart['product']['cate_id']]['price'], $cart['true_price']); - $catePriceLst[$cart['product']['cate_id']]['cart'][] = &$cart; - } - } - unset($cart); - $storePriceLst[$merchantCart['mer_id']] = [ - 'price' => $merchantCart['order']['true_price'], - 'num' => count($merchantCart['list']) - ]; - } - unset($merchantCart); - $flag = false; - $platformCouponRate = null; - - foreach ($platformCoupon as &$coupon) { - $coupon['checked'] = false; - //通用券 - if ($coupon['coupon']['type'] === StoreCouponRepository::TYPE_PLATFORM_ALL) { - $coupon['disabled'] = $total_true_price <= 0 || $coupon['use_min_price'] > $total_true_price; - if (!$platformCouponRate && !$coupon['disabled'] && !$flag && ((!$usePlatformCouponId && !$usePlatformCouponFlag) || $usePlatformCouponId == $coupon['coupon_user_id'])) { - $platformCouponRate = [ - 'id' => $coupon['coupon_user_id'], - 'type' => $coupon['coupon']['type'], - 'price' => $total_true_price, - 'coupon_price' => $coupon['coupon_price'], - 'use_count' => $_cartNum, - 'check' => function ($cart) { - return true; - } - ]; - $coupon['checked'] = true; - $flag = true; - } - //品类券 - } else if ($coupon['coupon']['type'] === StoreCouponRepository::TYPE_PLATFORM_CATE) { - $_price = 0; - $_use_count = 0; - $cateIds = $coupon['product']->column('product_id'); - $allCateIds = array_unique(array_merge(app()->make(StoreCategoryRepository::class)->allChildren($cateIds), $cateIds)); - $flag2 = true; - foreach ($allCateIds as $cateId) { - if (isset($catePriceLst[$cateId])) { - $_price = bcadd($catePriceLst[$cateId]['price'], $_price, 2); - $_use_count += count($catePriceLst[$cateId]['cart']); - $flag2 = false; - } - } - $coupon['disabled'] = $flag2 || $coupon['use_min_price'] > $_price; - //品类券可用 - if (!$platformCouponRate && !$coupon['disabled'] && !$flag && !$flag2 && ((!$usePlatformCouponId && !$usePlatformCouponFlag) || $usePlatformCouponId == $coupon['coupon_user_id'])) { - $platformCouponRate = [ - 'id' => $coupon['coupon_user_id'], - 'type' => $coupon['coupon']['type'], - 'price' => $_price, - 'use_cate' => $allCateIds, - 'coupon_price' => $coupon['coupon_price'], - 'use_count' => $_use_count, - 'check' => function ($cart) use ($allCateIds) { - return in_array($cart['product']['cate_id'], $allCateIds); - } - ]; - $coupon['checked'] = true; - $flag = true; - } - //跨店券 - } else if ($coupon['coupon']['type'] === StoreCouponRepository::TYPE_PLATFORM_STORE) { - $_price = 0; - $_use_count = 0; - $flag2 = true; - foreach ($coupon['product'] as $item) { - $merId = $item['product_id']; - if (isset($storePriceLst[$merId])) { - $_price = bcadd($storePriceLst[$merId]['price'], $_price, 2); - $_use_count += $storePriceLst[$merId]['num']; - $flag2 = false; - } - } - $coupon['disabled'] = $flag2 || $coupon['use_min_price'] > $_price; - //店铺券可用 - if (!$platformCouponRate && !$coupon['disabled'] && !$flag && !$flag2 && ((!$usePlatformCouponId && !$usePlatformCouponFlag) || $usePlatformCouponId == $coupon['coupon_user_id'])) { - $_merIds = $coupon['product']->column('product_id'); - $platformCouponRate = [ - 'id' => $coupon['coupon_user_id'], - 'type' => $coupon['coupon']['type'], - 'price' => $_price, - 'use_store' => $_merIds, - 'coupon_price' => $coupon['coupon_price'], - 'use_count' => $_use_count, - 'check' => function ($cart) use ($_merIds) { - return in_array($cart['mer_id'], $_merIds); - } - ]; - $coupon['checked'] = true; - $flag = true; - } - } - } - unset($coupon); - } -// } - - $usePlatformCouponId = 0; - $total_platform_coupon_price = 0; - //计算平台优惠券 - if (isset($platformCouponRate)) { - $_coupon_price = $platformCouponRate['coupon_price']; - foreach ($merchantCartList as &$merchantCart) { - $_price = 0; - foreach ($merchantCart['list'] as &$cart) { - if ($cart['true_price'] <= 0 || !$platformCouponRate['check']($cart)) continue; - - if ($platformCouponRate['use_count'] === 1) { - $couponPrice = min($platformCouponRate['coupon_price'], $cart['true_price']); - } else { - $couponPrice = min(bcmul($_coupon_price, bcdiv($cart['true_price'], $platformCouponRate['price'], 3), 2), $cart['true_price']); - } - $platformCouponRate['coupon_price'] = bcsub($platformCouponRate['coupon_price'], $couponPrice, 2); - $cart['true_price'] = bcsub($cart['true_price'], $couponPrice, 2); - $cart['platform_coupon_price'] = $couponPrice; - $platformCouponRate['use_count']--; - $_price = bcadd($couponPrice, $_price, 2); - } - unset($cart); - $merchantCart['order']['platform_coupon_price'] = $_price; - $merchantCart['order']['true_price'] = bcsub($merchantCart['order']['true_price'], $_price, 2); - $total_platform_coupon_price = bcadd($total_platform_coupon_price, $_price, 2); - } - $usePlatformCouponId = $platformCouponRate['id']; - unset($merchantCart); - } - - //积分配置 - $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']; - - //计算积分抵扣 - foreach ($merchantCart['list'] as &$cart) { - //只有普通商品可以抵扣 - if ($cart['product_type'] == 0 && $integralFlag && $userIntegral > 0 && $merchantCart['order']['true_price'] > 0) { - $integralRate = $cart['product']['integral_rate']; - if ($integralRate < 0) { - $integralRate = $merIntegralConfig['mer_integral_rate']; - } else if($integralRate > 0){ - $integralRate = min(bcdiv($integralRate, 100, 4), 1); - } - if ($integralRate > 0) { - $productIntegralPrice = min(bcmul(bcmul($this->cartByPrice($cart), $cart['cart_num'], 2), $integralRate, 2), $cart['true_price']); - if ($productIntegralPrice > 0) { - $productIntegral = ceil(bcdiv($productIntegralPrice, $sysIntegralConfig['integral_money'], 3)); - if ($productIntegral <= $userIntegral) { - $userIntegral = bcsub($userIntegral, $productIntegral, 0); - //使用多少积分抵扣了多少金额 - $cart['integral'] = [ - 'use' => $productIntegral, - 'price' => $productIntegralPrice - ]; - } else { - $productIntegralPrice = bcmul($userIntegral, $sysIntegralConfig['integral_money'], 2); - //使用多少积分抵扣了多少金额 - $cart['integral'] = [ - 'use' => $userIntegral, - 'price' => $productIntegralPrice - ]; - $userIntegral = 0; - } - - $cart['true_price'] = bcsub($cart['true_price'], $cart['integral']['price'], 2); - $merchantCart['order']['true_price'] = bcsub($merchantCart['order']['true_price'], $cart['integral']['price'], 2); - - $total_integral_price = bcadd($total_integral_price, $cart['integral']['price'], 2); - $total_integral = bcadd($total_integral, $cart['integral']['use'], 0); - continue; - } - } - } - $cart['integral'] = null; - } - unset($cart); - $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; - } - - $giveIntegralFlag = $sysIntegralConfig['integral_status'] && $sysIntegralConfig['integral_order_rate'] > 0; - $total_give_integral = 0; - //计算赠送积分, 只有普通商品赠送积分 - if ($giveIntegralFlag && !$order_type && $pay_price > 0) { - $total_give_integral = floor(bcmul($pay_price, $sysIntegralConfig['integral_order_rate'], 0)); - if ($total_give_integral > 0 && $svip_status && $svip_integral_rate > 0) { - $total_give_integral = bcmul($svip_integral_rate, $total_give_integral, 0); - } - } - $order_total_give_integral = bcadd($total_give_integral, $order_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; - - $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; - $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); - return compact( - 'order_type', - 'order_model', - 'order_extend', - 'order_total_postage', - 'order_price', - 'total_price', - 'platformCoupon', - 'enabledPlatformCoupon', - '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' - ) + ['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) - { - $uid = $user->uid; - $orderInfo = $this->v2CartIdByOrderInfo($user, $cartId, $takes, $useCoupon, $useIntegral, $addressId, true); - $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 && (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('该店铺不支持开发票'); - - foreach ($merchantCart['list'] as $cart) { - if (!$cartSpread && $cart['spread_id']) { - $cartSpread = $cart['spread_id']; - } - } - } - 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; - if ($ex) { - //预售订单 - if ($orderType == 2) { - $_payPrice = $merchantCart['order']['pay_price']; - $rate = $cart['productPresell']['presell_type'] == 2 ? bcdiv($cart['productPresellAttr']['down_price'], $cart['productPresellAttr']['presell_price'], 3) : 1; - $one_price = $_payPrice > 0 ? bcdiv($_payPrice, $cart['cart_num'], 2) : 0; - if ($spreadUid && $cart['productPresellAttr']['bc_extension_one'] > 0) { - $org_extension = $cart['productPresellAttr']['bc_extension_one']; - if ($spreadUser->brokerage_level > 0 && $spreadUser->brokerage && $spreadUser->brokerage->extension_one_rate > 0) { - $org_extension = bcmul($org_extension, 1 + $spreadUser->brokerage->extension_one_rate, 2); - } - $_extension_one = bcmul($rate, $org_extension, 3); - $presell_extension_one = 0; - if ($cart['true_price'] > 0) { - $extension_one = bcmul(bcdiv($one_price, $cart['productPresellAttr']['down_price'], 3), $_extension_one, 2); - } - if ($rate < 1) { - $presell_extension_one = bcmul(1 - $rate, $org_extension, 2); - } - $cart['final_extension_one'] = bcmul($extension_one, $cart['cart_num'], 2); - $extension_one = bcadd($extension_one, $presell_extension_one, 2); - $cart['presell_extension_one'] = bcmul($presell_extension_one, $cart['cart_num'], 2); - } - if ($topUid && $cart['productPresellAttr']['bc_extension_two'] > 0) { - $org_extension = $cart['productPresellAttr']['bc_extension_two']; - if ($topUser->brokerage_level > 0 && $topUser->brokerage && $topUser->brokerage->extension_two_rate > 0) { - $org_extension = bcmul($org_extension, 1 + $topUser->brokerage->extension_two_rate, 2); - } - $_extension_two = bcmul($rate, $org_extension, 2); - $presell_extension_two = 0; - if ($cart['true_price'] > 0) { - $extension_two = bcmul(bcdiv($one_price, $cart['productPresellAttr']['down_price'], 3), $_extension_two, 2); - } - if ($rate < 1) { - $presell_extension_two = bcmul(1 - $rate, $org_extension, 2); - } - $cart['final_extension_two'] = bcmul($extension_two, $cart['cart_num'], 2);; - $extension_two = bcadd($extension_two, $presell_extension_two, 2); - $cart['presell_extension_two'] = bcmul($presell_extension_two, $cart['cart_num'], 2); - } - } else if (!$orderType) { - if ($spreadUid && $cart['productAttr']['bc_extension_one'] > 0) { - $org_extension = $cart['productAttr']['bc_extension_one']; - if ($spreadUser->brokerage_level > 0 && $spreadUser->brokerage && $spreadUser->brokerage->extension_one_rate > 0) { - $org_extension = bcmul($org_extension, 1 + $spreadUser->brokerage->extension_one_rate, 2); - } - $extension_one = $cart['true_price'] > 0 ? bcmul(bcdiv($cart['true_price'], $cart['total_price'], 3), $org_extension, 2) : 0; - } - if ($topUid && $cart['productAttr']['bc_extension_two'] > 0) { - $org_extension = $cart['productAttr']['bc_extension_two']; - if ($topUser->brokerage_level > 0 && $topUser->brokerage && $topUser->brokerage->extension_two_rate > 0) { - $org_extension = bcmul($org_extension, 1 + $topUser->brokerage->extension_two_rate, 2); - } - $extension_two = $cart['true_price'] > 0 ? bcmul(bcdiv($cart['true_price'], $cart['total_price'], 3), $org_extension, 2) : 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['detail']) : ''; - //整理订单数据 - $_order = [ - 'cartInfo' => $merchantCart, - 'activity_type' => $orderInfo['order_type'], - '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, - '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 - ]; - $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'], - ]; - 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); - $attrValueRepository = app()->make(ProductAttrValueRepository::class); - $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 { - $attrValueRepository->descStock($cart['productAttr']['product_id'], $cart['productAttr']['unique'], $cart['cart_num']); - $productRepository->descStock($cart['product']['product_id'], $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 = []; - - 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(); - } - - foreach ($orderList as $k => $order) { - $orderList[$k]['group_order_id'] = $groupOrder->group_order_id; - } - - $orderProduct = []; - $orderStatus = []; - foreach ($orderList as $order) { - $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 (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']; - } - - $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'], - 'cart_info' => json_encode($order_cart) - ]; - } - - $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); - } - $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]); - return $group; - } -} diff --git a/app/common/repositories/store/order/StoreOrderRepository.php.bak b/app/common/repositories/store/order/StoreOrderRepository.php.bak deleted file mode 100644 index 6aae609e..00000000 --- a/app/common/repositories/store/order/StoreOrderRepository.php.bak +++ /dev/null @@ -1,2280 +0,0 @@ - -// +---------------------------------------------------------------------- -namespace app\common\repositories\store\order; - -use app\common\dao\store\order\StoreOrderDao; -use app\common\model\store\order\StoreGroupOrder; -use app\common\model\store\order\StoreOrder; -use app\common\model\user\User; -use app\common\repositories\BaseRepository; -use app\common\repositories\delivery\DeliveryOrderRepository; -use app\common\repositories\store\coupon\StoreCouponRepository; -use app\common\repositories\store\coupon\StoreCouponUserRepository; -use app\common\repositories\store\product\ProductAssistSetRepository; -use app\common\repositories\store\product\ProductCopyRepository; -use app\common\repositories\store\product\ProductGroupBuyingRepository; -use app\common\repositories\store\product\ProductPresellSkuRepository; -use app\common\repositories\store\product\ProductRepository; -use app\common\repositories\store\product\StoreDiscountRepository; -use app\common\repositories\store\shipping\ExpressRepository; -use app\common\repositories\store\StorePrinterRepository; -use app\common\repositories\store\StoreSeckillActiveRepository; -use app\common\repositories\system\attachment\AttachmentRepository; -use app\common\repositories\system\merchant\FinancialRecordRepository; -use app\common\repositories\system\merchant\MerchantRepository; -use app\common\repositories\system\serve\ServeDumpRepository; -use app\common\repositories\user\UserBillRepository; -use app\common\repositories\user\UserBrokerageRepository; -use app\common\repositories\user\UserMerchantRepository; -use app\common\repositories\user\UserRepository; -use crmeb\jobs\PayGiveCouponJob; -use crmeb\jobs\SendSmsJob; -use crmeb\jobs\UserBrokerageLevelJob; -use crmeb\services\CombinePayService; -use crmeb\services\CrmebServeServices; -use crmeb\services\ExpressService; -use crmeb\services\PayService; -use crmeb\services\printer\Printer; -use crmeb\services\QrcodeService; -use crmeb\services\SpreadsheetExcelService; -use crmeb\services\SwooleTaskService; -use Exception; -use FormBuilder\Factory\Elm; -use FormBuilder\Form; -use http\Exception\InvalidArgumentException; -use think\db\exception\DataNotFoundException; -use think\db\exception\DbException; -use think\db\exception\ModelNotFoundException; -use think\exception\ValidateException; -use think\facade\Db; -use think\facade\Log; -use think\facade\Queue; -use think\facade\Route; -use think\Model; - -/** - * Class StoreOrderRepository - * @package app\common\repositories\store\order - * @author xaboy - * @day 2020/6/9 - * @mixin StoreOrderDao - */ -class StoreOrderRepository extends BaseRepository -{ - /** - * 支付类型 - */ - const PAY_TYPE = ['balance', 'weixin', 'routine', 'h5', 'alipay', 'alipayQr', 'weixinQr']; - - const TYPE_SN_ORDER = 'wxo'; - const TYPE_SN_PRESELL = 'wxp'; - const TYPE_SN_USER_ORDER = 'wxs'; - const TYPE_SN_USER_RECHARGE = 'wxu'; - - const TYPE_SN_REFUND = 'rwx'; - /** - * StoreOrderRepository constructor. - * @param StoreOrderDao $dao - */ - public function __construct(StoreOrderDao $dao) - { - $this->dao = $dao; - } - - /** - * @param string $type - * @param User $user - * @param StoreGroupOrder $groupOrder - * @param string $return_url - * @return mixed - * @author xaboy - * @day 2020/10/22 - */ - public function pay(string $type, User $user, StoreGroupOrder $groupOrder, $return_url = '', $isApp = false) - { - - if ($type === 'balance') { - return $this->payBalance($user, $groupOrder); - } - - if (in_array($type, ['weixin', 'alipay'], true) && $isApp) { - $type .= 'App'; - } - event('order.pay.before', compact('groupOrder', 'type', 'isApp')); - if (in_array($type, ['weixin', 'weixinApp', 'routine', 'h5', 'weixinQr'], true) && systemConfig('open_wx_combine')) { - $service = new CombinePayService($type, $groupOrder->getCombinePayParams()); - } else { - $service = new PayService($type, $groupOrder->getPayParams($type === 'alipay' ? $return_url : '')); - } - $config = $service->pay($user); - return app('json')->status($type, $config + ['order_id' => $groupOrder['group_order_id']]); - } - - /** - * @param User $user - * @param StoreGroupOrder $groupOrder - * @return mixed - * @author xaboy - * @day 2020/6/9 - */ - public function payBalance(User $user, StoreGroupOrder $groupOrder) - { - if (!systemConfig('yue_pay_status')) - throw new ValidateException('未开启余额支付'); - if ($user['now_money'] < $groupOrder['pay_price']) - throw new ValidateException('余额不足,请更换支付方式'); - Db::transaction(function () use ($user, $groupOrder) { - $user->now_money = bcsub($user->now_money, $groupOrder['pay_price'], 2); - $user->save(); - $userBillRepository = app()->make(UserBillRepository::class); - $userBillRepository->decBill($user['uid'], 'now_money', 'pay_product', [ - 'link_id' => $groupOrder['group_order_id'], - 'status' => 1, - 'title' => '购买商品', - 'number' => $groupOrder['pay_price'], - 'mark' => '余额支付支付' . floatval($groupOrder['pay_price']) . '元购买商品', - 'balance' => $user->now_money - ]); - $this->paySuccess($groupOrder); - }); - return app('json')->status('success', '余额支付成功', ['order_id' => $groupOrder['group_order_id']]); - } - - public function changePayType(StoreGroupOrder $groupOrder, int $pay_type) - { - Db::transaction(function () use ($groupOrder, $pay_type) { - $groupOrder->pay_type = $pay_type; - foreach ($groupOrder->orderList as $order) { - $order->pay_type = $pay_type; - $order->save(); - } - $groupOrder->save(); - }); - } - - /** - * @return string - * @author xaboy - * @day 2020/8/3 - */ - public function verifyCode() - { - $code = substr(uniqid('', true), 15) . substr(microtime(), 2, 8); - if ($this->dao->existsWhere(['verify_code' => $code])) - return $this->verifyCode(); - else - return $code; - } - - /** - * //TODO 支付成功后 - * - * @param StoreGroupOrder $groupOrder - * @author xaboy - * @day 2020/6/9 - */ - public function paySuccess(StoreGroupOrder $groupOrder, $is_combine = 0, $subOrders = []) - { - $groupOrder->append(['user']); - //修改订单状态 - Db::transaction(function () use ($subOrders, $is_combine, $groupOrder) { - $time = date('Y-m-d H:i:s'); - $groupOrder->paid = 1; - $groupOrder->pay_time = $time; - $groupOrder->is_combine = $is_combine; - $orderStatus = []; - $groupOrder->append(['orderList.orderProduct']); - $flag = true; - $finance = []; - $profitsharing = []; - $financialRecordRepository = app()->make(FinancialRecordRepository::class); - $financeSn = $financialRecordRepository->getSn(); - $userMerchantRepository = app()->make(UserMerchantRepository::class); - $storeOrderProfitsharingRepository = app()->make(StoreOrderProfitsharingRepository::class); - $uid = $groupOrder->uid; - $i = 1; - $isVipCoupon = app()->make(StoreGroupOrderRepository::class)->isVipCoupon($groupOrder); - //订单记录 - $storeOrderStatusRepository = app()->make(StoreOrderStatusRepository::class); - $svipDiscount = 0; - foreach ($groupOrder->orderList as $_k => $order) { - $order->paid = 1; - $order->pay_time = $time; - $svipDiscount = bcadd($order->svip_discount, $svipDiscount, 2); - if (isset($subOrders[$order->order_sn])) { - $order->transaction_id = $subOrders[$order->order_sn]['transaction_id']; - } - $presell = false; - //todo 等待付尾款 - if ($order->activity_type == 2) { - $_make = app()->make(ProductPresellSkuRepository::class); - if ($order->orderProduct[0]['cart_info']['productPresell']['presell_type'] == 2) { - $order->status = 10; - $presell = true; - } else { - $_make->incCount($order->orderProduct[0]['activity_id'], $order->orderProduct[0]['product_sku'], 'two_pay'); - } - $_make->incCount($order->orderProduct[0]['activity_id'], $order->orderProduct[0]['product_sku'], 'one_pay'); - } else if ($order->activity_type == 4) { - $order->status = 9; - $order->save(); - $group_buying_id = app()->make(ProductGroupBuyingRepository::class)->create( - $groupOrder->user, - $order->orderProduct[0]['cart_info']['activeSku']['product_group_id'], - $order->orderProduct[0]['activity_id'], - $order->order_id - ); - $order->orderProduct[0]->activity_id = $group_buying_id; - $order->orderProduct[0]->save(); - } else if ($order->activity_type == 3) { - //更新助力状态 - app()->make(ProductAssistSetRepository::class)->changStatus($order->orderProduct[0]['activity_id']); - } - if ($order->order_type == 1 && $order->status != 10) - $order->verify_code = $this->verifyCode(); - $order->save(); - $orderStatus[] = [ - 'order_id' => $order->order_id, - 'order_sn' => $order->order_sn, - 'type' => $storeOrderStatusRepository::TYPE_ORDER, - 'change_message' => '订单支付成功', - 'change_type' => $storeOrderStatusRepository::ORDER_STATUS_PAY_SUCCCESS, - 'uid' => $order->uid, - 'nickname' => $order->user->nickname, - 'user_type' => $storeOrderStatusRepository::U_TYPE_USER, - ]; - - //TODO 成为推广员 - foreach ($order->orderProduct as $product) { - if ($flag && $product['cart_info']['product']['is_gift_bag']) { - app()->make(UserRepository::class)->promoter($order->uid); - $flag = false; - } - } - - $finance[] = [ - 'order_id' => $order->order_id, - 'order_sn' => $order->order_sn, - 'user_info' => $groupOrder->user->nickname, - 'user_id' => $uid, - 'financial_type' => $presell ? 'order_presell' : 'order', - 'financial_pm' => 1, - 'type' => $presell ? 2 : 1, - 'number' => $order->pay_price, - 'mer_id' => $order->mer_id, - 'financial_record_sn' => $financeSn . ($i++) - ]; - - $_payPrice = bcsub($order->pay_price, bcadd($order['extension_one'], $order['extension_two'], 3), 2); - if ($presell) { - if (isset($order->orderProduct[0]['cart_info']['presell_extension_one']) && $order->orderProduct[0]['cart_info']['presell_extension_one'] > 0) { - $_payPrice = bcadd($_payPrice, $order->orderProduct[0]['cart_info']['presell_extension_one'], 2); - } - if (isset($order->orderProduct[0]['cart_info']['presell_extension_two']) && $order->orderProduct[0]['cart_info']['presell_extension_two'] > 0) { - $_payPrice = bcadd($_payPrice, $order->orderProduct[0]['cart_info']['presell_extension_two'], 2); - } - } - - $_order_rate = 0; - - if ($order['commission_rate'] > 0) { - - $commission_rate = ($order['commission_rate'] / 100); - - $_order_rate = bcmul($_payPrice, $commission_rate, 2); - - $_payPrice = bcsub($_payPrice, $_order_rate, 2); - } - - if (!$presell) { - if ($order['extension_one'] > 0) { - $finance[] = [ - 'order_id' => $order->order_id, - 'order_sn' => $order->order_sn, - 'user_info' => $groupOrder->user->nickname, - 'user_id' => $uid, - 'financial_type' => 'brokerage_one', - 'financial_pm' => 0, - 'type' => 1, - 'number' => $order['extension_one'], - 'mer_id' => $order->mer_id, - 'financial_record_sn' => $financeSn . ($i++) - ]; - } - - if ($order['extension_two'] > 0) { - $finance[] = [ - 'order_id' => $order->order_id, - 'order_sn' => $order->order_sn, - 'user_info' => $groupOrder->user->nickname, - 'user_id' => $uid, - 'financial_type' => 'brokerage_two', - 'financial_pm' => 0, - 'type' => 1, - 'number' => $order['extension_two'], - 'mer_id' => $order->mer_id, - 'financial_record_sn' => $financeSn . ($i++) - ]; - } - - if ($order['commission_rate'] > 0) { - $finance[] = [ - 'order_id' => $order->order_id, - 'order_sn' => $order->order_sn, - 'user_info' => $groupOrder->user->nickname, - 'user_id' => $uid, - 'financial_type' => 'order_charge', - 'financial_pm' => 0, - 'type' => 1, - 'number' => $_order_rate, - 'mer_id' => $order->mer_id, - 'financial_record_sn' => $financeSn . ($i++) - ]; - } - $finance[] = [ - 'order_id' => $order->order_id, - 'order_sn' => $order->order_sn, - 'user_info' => $groupOrder->user->nickname, - 'user_id' => $uid, - 'financial_type' => 'order_true', - 'financial_pm' => 0, - 'type' => 2, - 'number' => $_payPrice, - 'mer_id' => $order->mer_id, - 'financial_record_sn' => $financeSn . ($i++) - ]; - - if ($order->platform_coupon_price > 0) { - $finance[] = [ - 'order_id' => $order->order_id, - 'order_sn' => $order->order_sn, - 'user_info' => $groupOrder->user->nickname, - 'user_id' => $uid, - 'financial_type' => $isVipCoupon ? 'order_svip_coupon' : 'order_platform_coupon', - 'financial_pm' => 0, - 'type' => 1, - 'number' => $order->platform_coupon_price, - 'mer_id' => $order->mer_id, - 'financial_record_sn' => $financeSn . ($i++) - ]; - $_payPrice = bcadd($_payPrice, $order->platform_coupon_price, 2); - } - - if (!$is_combine) { - app()->make(MerchantRepository::class)->addLockMoney($order->mer_id, 'order', $order->order_id, $_payPrice); - } - } - if ($is_combine) { - $profitsharing[] = [ - 'profitsharing_sn' => $storeOrderProfitsharingRepository->getOrderSn(), - 'order_id' => $order->order_id, - 'transaction_id' => $order->transaction_id ?? '', - 'mer_id' => $order->mer_id, - 'profitsharing_price' => $order->pay_price, - 'profitsharing_mer_price' => $_payPrice, - 'type' => $storeOrderProfitsharingRepository::PROFITSHARING_TYPE_ORDER, - ]; - } - $userMerchantRepository->updatePayTime($uid, $order->mer_id, $order->pay_price); - SwooleTaskService::merchant('notice', [ - 'type' => 'new_order', - 'data' => [ - 'title' => '新订单', - 'message' => '您有一个新的订单', - 'id' => $order->order_id - ] - ], $order->mer_id); - //自动打印订单 - $this->autoPrinter($order->order_id, $order->mer_id); - } - if ($groupOrder->user->spread_uid) { - Queue::push(UserBrokerageLevelJob::class, ['uid' => $groupOrder->user->spread_uid, 'type' => 'spread_pay_num', 'inc' => 1]); - Queue::push(UserBrokerageLevelJob::class, ['uid' => $groupOrder->user->spread_uid, 'type' => 'spread_money', 'inc' => $groupOrder->pay_price]); - } - app()->make(UserRepository::class)->update($groupOrder->uid, [ - 'pay_count' => Db::raw('pay_count+' . count($groupOrder->orderList)), - 'pay_price' => Db::raw('pay_price+' . $groupOrder->pay_price), - 'svip_save_money' => Db::raw('svip_save_money+' . $svipDiscount), - ]); - $this->giveIntegral($groupOrder); - if (count($profitsharing)) { - $storeOrderProfitsharingRepository->insertAll($profitsharing); - } - $financialRecordRepository->insertAll($finance); - $storeOrderStatusRepository->batchCreateLog($orderStatus); - if (count($groupOrder['give_coupon_ids']) > 0) - $groupOrder['give_coupon_ids'] = app()->make(StoreCouponRepository::class)->getGiveCoupon($groupOrder['give_coupon_ids'])->column('coupon_id'); - $groupOrder->save(); - }); - - if (count($groupOrder['give_coupon_ids']) > 0) { - try { - Queue::push(PayGiveCouponJob::class, ['ids' => $groupOrder['give_coupon_ids'], 'uid' => $groupOrder['uid']]); - } catch (Exception $e) { - } - } - - Queue::push(SendSmsJob::class, ['tempId' => 'ORDER_PAY_SUCCESS', 'id' => $groupOrder->group_order_id]); - Queue::push(SendSmsJob::class, ['tempId' => 'ADMIN_PAY_SUCCESS_CODE', 'id' => $groupOrder->group_order_id]); - Queue::push(UserBrokerageLevelJob::class, ['uid' => $groupOrder->uid, 'type' => 'pay_money', 'inc' => $groupOrder->pay_price]); - Queue::push(UserBrokerageLevelJob::class, ['uid' => $groupOrder->uid, 'type' => 'pay_num', 'inc' => 1]); - app()->make(UserBrokerageRepository::class)->incMemberValue($groupOrder->uid, 'member_pay_num', $groupOrder->group_order_id); - event('order.paySuccess', compact('groupOrder')); - } - - /** - * 自动打印 - * @Author:Qinii - * @Date: 2020/10/13 - * @param int $orderId - * @param int $merId - */ - public function autoPrinter(int $orderId, int $merId) - { - if (merchantConfig($merId, 'printing_auto_status')) { - try { - $this->batchPrinter($orderId, $merId); - } catch (Exception $exception) { - Log::info('自动打印小票报错'); - } - } else { - Log::info('自动打印小票验证:商户ID【' . $merId . '】,自动打印状态未开启'); - } - } - - /** - * @return string - * @author xaboy - * @day 2020/6/9 - */ - public function getNewOrderId($type) - { - list($msec, $sec) = explode(' ', microtime()); - $msectime = number_format((floatval($msec) + floatval($sec)) * 1000, 0, '', ''); - $orderId = $type . $msectime . mt_rand(10000, max(intval($msec * 10000) + 10000, 98369)); - return $orderId; - } - - /** - * @param $cart - * @return string - * @author xaboy - * @day 2020/6/9 - */ - public function productByTempNumber($cart) - { - $type = $cart['product']['temp']['type']; - $cartNum = $cart['cart_num']; - if (!$type) - return $cartNum; - else if ($type == 2) { - return bcmul($cartNum, $cart['productAttr']['volume'], 2); - } else { - return bcmul($cartNum, $cart['productAttr']['weight'], 2); - } - } - - public function cartByPrice($cart) - { - if ($cart['product_type'] == '2') { - return $cart['productPresellAttr']['presell_price']; - } else if ($cart['product_type'] == '3') { - return $cart['productAssistAttr']['assist_price']; - } else if ($cart['product_type'] == '4') { - return $cart['activeSku']['active_price']; - } else { - return $cart['productAttr']['price']; - } - } - - public function cartByCouponPrice($cart) - { - if ($cart['product_type'] == '2') { - return $cart['productPresellAttr']['final_price']; - } else if ($cart['product_type'] == '1') { - return 0; - } else if ($cart['product_type'] == '3') { - return 0; - } else if ($cart['product_type'] == '4') { - return 0; - } else { - return $cart['productAttr']['price']; - } - } - - public function cartByDownPrice($cart) - { - if ($cart['product_type'] == '2') { - return $cart['productPresellAttr']['down_price']; - } else { - return 0; - } - } - - - /** - * @param int $uid - * @return array - * @author xaboy - * @day 2020/6/10 - */ - public function userOrderNumber(int $uid) - { - $noPay = app()->make(StoreGroupOrderRepository::class)->orderNumber($uid); - $noPostage = $this->dao->search(['uid' => $uid, 'status' => 0, 'paid' => 1,'is_user' => 1])->where('StoreOrder.is_del', 0)->count(); - $all = $this->dao->search(['uid' => $uid, 'status' => -2,'is_user' => 1])->where('StoreOrder.is_del', 0)->count(); - $noDeliver = $this->dao->search(['uid' => $uid, 'status' => 1, 'paid' => 1])->where('StoreOrder.is_del', 0)->count(); - $noComment = $this->dao->search(['uid' => $uid, 'status' => 2, 'paid' => 1,'is_user' => 1])->where('StoreOrder.is_del', 0)->count(); - $done = $this->dao->search(['uid' => $uid, 'status' => 3, 'paid' => 1,'is_user' => 1])->where('StoreOrder.is_del', 0)->count(); - $refund = app()->make(StoreRefundOrderRepository::class)->getWhereCount(['uid' => $uid, 'status' => [0, 1, 2]]); - //$orderPrice = $this->dao->search(['uid' => $uid, 'paid' => 1])->sum('pay_price'); - $orderCount = $this->dao->search(['uid' => $uid, 'paid' => 1,'is_user' => 1])->count(); - return compact('noComment', 'done', 'refund', 'noDeliver', 'noPay', 'noPostage', 'orderCount', 'all'); - } - - /** - * @param $id - * @param null $uid - * @return array|Model|null - * @throws DataNotFoundException - * @throws DbException - * @throws ModelNotFoundException - * @author xaboy - * @day 2020/6/10 - */ - public function getDetail($id, $uid = null) - { - $where = []; - $with = [ - 'orderProduct', - 'merchant' => function ($query) { - return $query->field('mer_id,mer_name,service_phone')->append(['services_type']); - }, - 'receipt' => function ($query) { - return $query->field('order_id,order_receipt_id'); - }, - 'takeOrderList.orderProduct' - ]; - if ($uid) { - $where['uid'] = $uid; - } else if (!$uid) { - $with['user'] = function ($query) { - return $query->field('uid,nickname'); - }; - } - $order = $this->dao->search($where)->where('order_id', $id)->where('StoreOrder.is_del', 0)->with($with)->append(['refund_status'])->find(); - if (!$order) { - return null; - } - if ($order->activity_type == 2) { - if ($order->presellOrder) { - $order->presellOrder->append(['activeStatus']); - $order->presell_price = bcadd($order->pay_price, $order->presellOrder->pay_price, 2); - } else { - $order->presell_price = $order->pay_price; - } - } - return $order; - } - - public function codeByDetail($code, $uid = null) - { - $where = []; - if ($uid) $where['uid'] = $uid; - $data = $this->dao->search($where)->where('verify_code', $code) - ->where('StoreOrder.is_del', 0) - ->with([ - 'orderProduct', - 'merchant' => function ($query) { - return $query->field('mer_id,mer_name'); - } - ]) - ->find(); - if (!$data) - throw new ValidateException('数据不存在'); - if ($data['status']) - throw new ValidateException('该订单已全部核销'); - return $data; - } - - public function giveIntegral($groupOrder) - { - if ($groupOrder->give_integral > 0) { - app()->make(UserBillRepository::class)->incBill($groupOrder->uid, 'integral', 'lock', [ - 'link_id' => $groupOrder['group_order_id'], - 'status' => 0, - 'title' => '下单赠送积分', - 'number' => $groupOrder->give_integral, - 'mark' => '成功消费' . floatval($groupOrder['pay_price']) . '元,赠送积分' . floatval($groupOrder->give_integral), - 'balance' => $groupOrder->user->integral - ]); - } - } - - /** - * @param StoreOrder $order - * @param User $user - * @author xaboy - * @day 2020/8/3 - */ - public function computed(StoreOrder $order, User $user) - { - $userBillRepository = app()->make(UserBillRepository::class); - if ($order->spread_uid) { - $spreadUid = $order->spread_uid; - $topUid = $order->top_uid; - } else if ($order->is_selfbuy) { - $spreadUid = $user->uid; - $topUid = $user->spread_uid; - } else { - $spreadUid = $user->spread_uid; - $topUid = $user->top_uid; - } - //TODO 添加冻结佣金 - if ($order->extension_one > 0 && $spreadUid) { - $userBillRepository->incBill($spreadUid, 'brokerage', 'order_one', [ - 'link_id' => $order['order_id'], - 'status' => 0, - 'title' => '获得推广佣金', - 'number' => $order->extension_one, - 'mark' => $user['nickname'] . '成功消费' . floatval($order['pay_price']) . '元,奖励推广佣金' . floatval($order->extension_one), - 'balance' => 0 - ]); - $userRepository = app()->make(UserRepository::class); - $userRepository->incBrokerage($spreadUid, $order->extension_one); - // app()->make(FinancialRecordRepository::class)->dec([ - // 'order_id' => $order->order_id, - // 'order_sn' => $order->order_sn, - // 'user_info' => $userRepository->getUsername($spreadUid), - // 'user_id' => $spreadUid, - // 'financial_type' => 'brokerage_one', - // 'number' => $order->extension_one, - // ], $order->mer_id); - } - if ($order->extension_two > 0 && $topUid) { - $userBillRepository->incBill($topUid, 'brokerage', 'order_two', [ - 'link_id' => $order['order_id'], - 'status' => 0, - 'title' => '获得推广佣金', - 'number' => $order->extension_two, - 'mark' => $user['nickname'] . '成功消费' . floatval($order['pay_price']) . '元,奖励推广佣金' . floatval($order->extension_two), - 'balance' => 0 - ]); - $userRepository = app()->make(UserRepository::class); - $userRepository->incBrokerage($topUid, $order->extension_two); - // app()->make(FinancialRecordRepository::class)->dec([ - // 'order_id' => $order->order_id, - // 'order_sn' => $order->order_sn, - // 'user_info' => $userRepository->getUsername($topUid), - // 'user_id' => $topUid, - // 'financial_type' => 'brokerage_two', - // 'number' => $order->extension_two, - // ], $order->mer_id); - } - } - - /** - * @param StoreOrder $order - * @param User $user - * @param string $type - * @author xaboy - * @day 2020/8/3 - */ - public function takeAfter(StoreOrder $order, ?User $user) - { - Db::transaction(function () use ($user, $order) { - if ($user) $this->computed($order, $user); - Queue::push(SendSmsJob::class, ['tempId' => 'ORDER_TAKE_SUCCESS', 'id' => $order->order_id]); - Queue::push(SendSmsJob::class, ['tempId' => 'ADMIN_TAKE_DELIVERY_CODE', 'id' => $order->order_id]); - app()->make(MerchantRepository::class)->computedLockMoney($order); - $order->save(); - }); - } - - /** - * @param $id - * @param User $user - * @throws DataNotFoundException - * @throws DbException - * @throws ModelNotFoundException - * @author xaboy - * @day 2020/6/17 - */ - public function takeOrder($id, ?User $user = null) - { - $order = $this->dao->search(!$user ? [] : ['uid' => $user->uid], null)->where('order_id', $id)->where('StoreOrder.is_del', 0)->find(); - if (!$order) - throw new ValidateException('订单不存在'); - if ($order['status'] != 1 || $order['order_type']) - throw new ValidateException('订单状态有误'); - $func = 'createUserLog'; - if (!$user){ - $func = 'createSysLog'; - $user = $order->user; - } -// if (!$user) { -// -// throw new ValidateException('用户不存在'); -// } - $order->status = 2; - $order->verify_time = date('Y-m-d H:i:s'); - event('order.take.before', compact('order')); - //订单记录 - $storeOrderStatusRepository = app()->make(StoreOrderStatusRepository::class); - $orderStatus = [ - 'order_id' => $order->order_id, - 'order_sn' => $order->order_sn, - 'type' => $storeOrderStatusRepository::TYPE_ORDER, - 'change_message' => '已收货', - 'change_type' => $storeOrderStatusRepository::ORDER_STATUS_TAKE, - ]; - Db::transaction(function () use ($order, $user,$storeOrderStatusRepository,$orderStatus,$func) { - $this->takeAfter($order, $user); - $order->save(); - $storeOrderStatusRepository->{$func}($orderStatus); - }); - event('order.take', compact('order')); - } - - - /** - * 获取订单列表头部统计数据 - * @Author:Qinii - * @Date: 2020/9/12 - * @param int|null $merId - * @param int|null $orderType - * @return array - */ - public function OrderTitleNumber(?int $merId, ?int $orderType) - { - $where = []; - $sysDel = $merId ? 0 : null; //商户删除 - if ($merId) $where['mer_id'] = $merId; //商户订单 - if ($orderType === 0) $where['order_type'] = 0; //普通订单 - if ($orderType === 1) $where['take_order'] = 1; //已核销订单 - //1: 未支付 2: 未发货 3: 待收货 4: 待评价 5: 交易完成 6: 已退款 7: 已删除 - $all = $this->dao->search($where, $sysDel)->where($this->getOrderType(0))->count(); - $statusAll = $all; - $unpaid = $this->dao->search($where, $sysDel)->where($this->getOrderType(1))->count(); - $unshipped = $this->dao->search($where, $sysDel)->where($this->getOrderType(2))->count(); - $untake = $this->dao->search($where, $sysDel)->where($this->getOrderType(3))->count(); - $unevaluate = $this->dao->search($where, $sysDel)->where($this->getOrderType(4))->count(); - $complete = $this->dao->search($where, $sysDel)->where($this->getOrderType(5))->count(); - $refund = $this->dao->search($where, $sysDel)->where($this->getOrderType(6))->count(); - $del = $this->dao->search($where, $sysDel)->where($this->getOrderType(7))->count(); - - return compact('all', 'statusAll', 'unpaid', 'unshipped', 'untake', 'unevaluate', 'complete', 'refund', 'del'); - } - - public function orderType(array $where) - { - return [ - [ - 'count' => $this->dao->search($where)->count(), - 'title' => '全部', - 'order_type' => -1, - ], - [ - 'count' => $this->dao->search($where)->where('order_type', 0)->where('is_virtual', 0)->count(), - 'title' => '普通订单', - 'order_type' => 0, - ], - [ - 'count' => $this->dao->search($where)->where('order_type', 1)->count(), - 'title' => '核销订单', - 'order_type' => 1, - ], - [ - 'count' => $this->dao->search($where)->where('is_virtual', 1)->count(), - 'title' => '虚拟商品订单', - 'order_type' => 2, - ], - ]; - } - - /** - * @param $status - * @return mixed - * @author Qinii - */ - public function getOrderType($status) - { - $param['StoreOrder.is_del'] = 0; - switch ($status) { - case 1: - $param['paid'] = 0; - break; // 未支付 - case 2: - $param['paid'] = 1; - $param['StoreOrder.status'] = 0; - break; // 待发货 - case 3: - $param['StoreOrder.status'] = 1; - break; // 待收货 - case 4: - $param['StoreOrder.status'] = 2; - break; // 待评价 - case 5: - $param['StoreOrder.status'] = 3; - break; // 交易完成 - case 6: - $param['StoreOrder.status'] = -1; - break; // 已退款 - case 7: - $param['StoreOrder.is_del'] = 1; - break; // 待核销 - break; // 已删除 - default: - unset($param['StoreOrder.is_del']); - break; //全部 - } - return $param; - } - - /** - * @param int $id - * @param int|null $merId - * @return array|Model|null - * @author Qinii - */ - public function merDeliveryExists(int $id, ?int $merId, ?int $re = 0) - { - $where = ['order_id' => $id, 'is_del' => 0, 'paid' => 1]; - if ($re) $where['status'] = 0; - if ($merId) $where['mer_id'] = $merId; - return $this->dao->merFieldExists($where); - } - - /** - * TODO - * @param int $id - * @param int|null $merId - * @return bool - * @author Qinii - * @day 2020-06-11 - */ - public function merGetDeliveryExists(int $id, ?int $merId) - { - $where = ['order_id' => $id, 'is_del' => 0, 'paid' => 1, 'status' => 1]; - if ($merId) $where['mer_id'] = $merId; - return $this->dao->merFieldExists($where); - } - - /** - * @param int $id - * @param int|null $merId - * @return array|Model|null - * @author Qinii - */ - public function merStatusExists(int $id, ?int $merId) - { - $where = ['order_id' => $id, 'is_del' => 0, 'paid' => 0, 'status' => 0]; - if ($merId) $where['mer_id'] = $merId; - return $this->dao->merFieldExists($where); - } - - public function userDelExists(int $id, ?int $merId) - { - $where = ['order_id' => $id, 'is_del' => 1]; - if ($merId) $where['mer_id'] = $merId; - return $this->dao->merFieldExists($where); - } - - /** - * @param $id - * @return Form - * @author Qinii - */ - public function form($id) - { - $data = $this->dao->getWhere([$this->dao->getPk() => $id], 'total_price,pay_price,total_postage,pay_postage'); - $form = Elm::createForm(Route::buildUrl('merchantStoreOrderUpdate', ['id' => $id])->build()); - $form->setRule([ - Elm::number('total_price', '订单总价', $data['total_price'])->required(), - Elm::number('total_postage', '订单邮费', $data['total_postage'])->required(), - Elm::number('pay_price', '实际支付金额', $data['pay_price'])->required(), - ]); - return $form->setTitle('修改订单'); - } - - /** - * TODO 修改订单价格 - * @param int $id - * @param array $data - * @author Qinii - * @day 12/15/20 - */ - public function eidt(int $id, array $data, $service_id = 0) - { - - /** - * 1 计算出新的实际支付价格 - * 1.1 计算邮费 - * 1.2 计算商品总价 - * 2 修改订单信息 - * 3 计算总单数据 - * 4 修改总单数据 - * 5 修改订单商品单价 - * - * pay_price = total_price - coupon_price + pay_postage - */ - $order = $this->dao->get($id); - if ($order->activity_type == 2) { - throw new ValidateException('预售订单不支持改价'); - } - $extension_total = (float)bcadd($order->extension_one, $order->extension_two, 2); - $data['pay_price'] = $this->bcmathPrice($data['total_price'], $order['coupon_price'], $data['pay_postage']); - if ($data['pay_price'] < 0) { - throw new ValidateException('实际支付金额不能小于0'); - } else if ($data['pay_price'] < $extension_total) { - throw new ValidateException('实际支付金额不能小于佣金' . $extension_total); - } - $make = app()->make(StoreGroupOrderRepository::class); - $orderGroup = $make->dao->getWhere(['group_order_id' => $order['group_order_id']]); - - //总单总价格 - $_group['total_price'] = $this->bcmathPrice($orderGroup['total_price'], $order['total_price'], $data['total_price']); - //总单实际支付价格 - $_group['pay_price'] = $this->bcmathPrice($orderGroup['pay_price'], $order['pay_price'], $data['pay_price']); - //总单实际支付邮费 - $_group['pay_postage'] = $this->bcmathPrice($orderGroup['pay_postage'], $order['pay_postage'], $data['pay_postage']); - event('order.changePrice.before', compact('order', 'data')); - //订单记录 - $storeOrderStatusRepository = app()->make(StoreOrderStatusRepository::class); - - $orderStatus = [ - 'order_id' => $order->order_id, - 'order_sn' => $order->order_sn, - 'type' => $storeOrderStatusRepository::TYPE_ORDER, - 'change_message' => '订单价格修改', - 'change_type' => $storeOrderStatusRepository::ORDER_STATUS_CHANGE, - ]; - - Db::transaction(function () use ($id, $data, $orderGroup, $order, $_group,$storeOrderStatusRepository,$orderStatus,$service_id) { - $orderGroup->total_price = $_group['total_price']; - $orderGroup->pay_price = $_group['pay_price']; - $orderGroup->pay_postage = $_group['pay_postage']; - $orderGroup->group_order_sn = $this->getNewOrderId(StoreOrderRepository::TYPE_SN_ORDER) . '0'; - $orderGroup->save(); - - $this->dao->update($id, $data); - $this->changOrderProduct($id, $data); - - if ($service_id) { - $storeOrderStatusRepository->createServiceLog($service_id,$orderStatus); - } else { - $storeOrderStatusRepository->createAdminLog($orderStatus); - } - if ($data['pay_price'] != $order['pay_price']) Queue::push(SendSmsJob::class, ['tempId' => 'PRICE_REVISION_CODE', 'id' => $id]); - }); - event('order.changePrice', compact('order', 'data')); - } - - /** - * TODO 改价后重新计算每个商品的单价 - * @param int $orderId - * @param array $data - * @author Qinii - * @day 12/15/20 - */ - public function changOrderProduct(int $orderId, array $data) - { - $make = app()->make(StoreOrderProductRepository::class); - $ret = $make->getSearch(['order_id' => $orderId])->field('order_product_id,product_num,product_price')->select(); - $count = $make->getSearch(['order_id' => $orderId])->sum('product_price'); - $_count = (count($ret->toArray()) - 1); - $pay_price = $data['total_price']; - foreach ($ret as $k => $item) { - $_price = 0; - /** - * 比例 = 单个商品总价 / 订单原总价; - * - * 新的商品总价 = 比例 * 订单修改总价 - * - * 更新数据库 - */ - if ($k == $_count) { - $_price = $pay_price; - } else { - $_reta = bcdiv($item->product_price, $count, 3); - $_price = bcmul($_reta, $data['total_price'], 2); - } - - $item->product_price = $_price; - $item->save(); - - $pay_price = $this->bcmathPrice($pay_price, $_price, 0); - } - } - - /** - * TODO 计算的重复利用 - * @param $total - * @param $old - * @param $new - * @return int|string - * @author Qinii - * @day 12/15/20 - */ - public function bcmathPrice($total, $old, $new) - { - $_bcsub = bcsub($total, $old, 2); - $_count = (bccomp($_bcsub, 0, 2) == -1) ? 0 : $_bcsub; - $count = bcadd($_count, $new, 2); - return (bccomp($count, 0, 2) == -1) ? 0 : $count; - } - - /** - * @param $id - * @param $uid - * @return mixed - * @throws DataNotFoundException - * @throws DbException - * @throws ModelNotFoundException - * @author xaboy - * @day 2020/6/12 - */ - public function refundProduct($id, $uid) - { - $order = $this->dao->userOrder($id, $uid); - if (!$order) - throw new ValidateException('订单不存在'); - if (!count($order->refundProduct)) - throw new ValidateException('没有可退款商品'); - return $order->refundProduct->toArray(); - } - - /** - * TODO - * @param $id - * @param $data - * @return mixed - * @author Qinii - * @day 7/26/21 - */ - public function orderDumpInfo($id, $data, $merId) - { - $where = [ - 'order_id' => $id, - ]; - $ret = $this->dao->getWhere($where); - if ($ret['is_virtual']) throw new ValidateException('虚拟商品只能虚拟发货'); - $cargo = ''; - $count = 0; - foreach ($ret->orderProduct as $item) { - // $cargo .= $item['cart_info']['product']['store_name']. ' ' .$item['cart_info']['productAttr']['sku'] .' * ' .$item['product_num'].$item['cart_info']['product']['unit_name'].PHP_EOL; - $count += $item['product_num']; - } - - $data['to_name'] = $ret['real_name']; - $data['to_tel'] = $ret['user_phone']; - $data['to_addr'] = $ret['user_address']; - $data['cargo'] = $cargo; - $data['count'] = $count; - $data['order_sn'] = $ret['order_sn']; - return $data; - } - - /** - * TODO 批量发货 - * @param int $merId - * @param array $params - * @author Qinii - * @day 7/26/21 - */ - public function batchDelivery(int $merId, array $params) - { - $count = count($params['order_id']); - $import = app()->make(StoreImportRepository::class)->create($merId, 'delivery', $params['delivery_type']); - $make = app()->make(StoreImportDeliveryRepository::class); - $data = []; - $num = 0; - foreach ($params['order_id'] as $item) { - $ret = $this->dao->getWhere(['order_id' => $params['order_id']]); - $imp = [ - 'order_sn' => $ret['order_sn'] ?? $item, - 'delivery_id' => $params['delivery_id'], - 'delivery_type' => $params['delivery_type'], - 'delivery_name' => $params['delivery_name'], - 'import_id' => $import['import_id'], - 'mer_id' => $merId - ]; - - if (!$ret || $ret['status'] != 1 || $ret['mer_id'] != $merId || $ret['is_del'] != 0 || $ret['paid'] != 1 || $ret['delivery_type'] != 0 ) { - $imp['status'] = 0; - $imp['mark'] = '订单信息不存在或状态错误'; - } else { - try { - if ($params['delivery_type'] == 4) { - $dump = [ - 'temp_id' => $params['temp_id'], - 'from_tel' => $params['from_tel'], - 'from_addr' => $params['from_addr'], - 'from_name' => $params['from_name'], - 'delivery_name' => $params['delivery_name'], - ]; - $dump = $this->orderDumpInfo($item, $dump, $merId); - $ret = $this->dump($item, $merId, $dump); - $imp['delivery_id'] = $ret['delivery_id']; - $imp['delivery_name'] = $ret['delivery_name']; - } else { - $this->delivery($item, $merId,[ - 'delivery_id' => $params['delivery_id'], - 'delivery_type' => $params['delivery_type'], - 'delivery_name' => $params['delivery_name'], - ]); - } - $num++; - $imp['status'] = 1; - } catch (Exception $exception) { - $imp['status'] = 0; - $imp['mark'] = $exception->getMessage(); - } - } - $data[] = $imp; - } - - $_status = ($num == 0) ? -1 : (($count == $num) ? 1 : 10); - $make->insertAll($data); - $arr = ['count' => $count, 'success' => $num, 'status' => $_status]; - app()->make(StoreImportRepository::class)->update($import['import_id'], $arr); - } - - - /** - * TODO 打印电子面单,组合参数 - * @param int $id - * @param int $merId - * @param array $data - * @return mixed - * @author Qinii - * @day 7/26/21 - */ - public function dump(int $id, int $merId, array $data, $service_id = 0) - { - $make = app()->make(MerchantRepository::class); - $make->checkCrmebNum($merId, 'dump'); - - $data = $this->orderDumpInfo($id, $data, $merId); - - $data['com'] = $data['delivery_name']; - $result = app()->make(CrmebServeServices::class)->express()->dump($merId, $data); - if (!isset($result['kuaidinum'])) throw new ValidateException('打印失败'); - - $delivery = [ - 'delivery_type' => 4, - 'delivery_name' => $data['delivery_name'], - 'delivery_id' => $result['kuaidinum'], - 'remark' => $data['remark'] ?? '', - ]; - - $dump = [ - 'delivery_name' => $delivery['delivery_name'], - 'delivery_id' => $delivery['delivery_id'], - 'from_name' => $data['from_name'], - 'order_sn' => $data['order_sn'], - 'to_name' => $data['to_name'], - ]; - Db::transaction(function () use ($merId, $id, $delivery, $make, $dump, $service_id) { - $this->delivery($id, $merId, $delivery,$service_id); - $arr = [ - 'type' => 'mer_dump', - 'num' => -1, - 'message' => '电子面单', - 'info' => $dump - ]; - app()->make(ProductCopyRepository::class)->add($arr, $merId); - }); - return $delivery; - } - - public function runDelivery($id, $merId, $data, $split, $method,$service_id = 0) - { - return Db::transaction(function () use ($id, $merId, $data, $split, $method,$service_id) { - if ($split['is_split'] && !empty($split['split'])) { - foreach ($split['split'] as $v) { - $splitData[$v['id']] = $v['num']; - } - $order = $this->dao->get($id); - $newOrder = app()->make(StoreOrderSplitRepository::class)->splitOrder($order, $splitData,$service_id); - if ($newOrder){ - $id = $newOrder->order_id; - } else { - throw new ValidateException('商品不能全部拆单'); - } - } - return $this->{$method}($id, $merId, $data,$service_id); - }); - } - - /** - * TODO 发货订单操作 - * @param $id - * @param $data - * @return mixed - * @author Qinii - * @day 7/26/21 - */ - public function delivery($id, $merId, $data, $service_id = 0) - { - $data['status'] = 1; - $order = $this->dao->get($id); - if ($order['is_virtual'] && $data['delivery_type'] != 3) - throw new ValidateException('虚拟商品只能虚拟发货'); - //订单记录 - $statusRepository = app()->make(StoreOrderStatusRepository::class); - switch ($data['delivery_type']) { - case 1: - $exprss = app()->make(ExpressRepository::class)->getWhere(['code' => $data['delivery_name']]); - if (!$exprss) throw new ValidateException('快递公司不存在'); - $data['delivery_name'] = $exprss['name']; - $change_type = $statusRepository::ORDER_DELIVERY_COURIER; - $change_message = '订单已配送【快递名称】:' . $exprss['name'] . '; 【快递单号】:' . $data['delivery_id']; - $temp_code = 'DELIVER_GOODS_CODE'; - break; - case 2: - if (!preg_match("/^1[3456789]{1}\d{9}$/", $data['delivery_id'])) throw new ValidateException('手机号格式错误'); - $change_type = 'delivery_1'; - $change_message = '订单已配送【送货人姓名】:' . $data['delivery_name'] . '; 【手机号】:' . $data['delivery_id']; - $temp_code = 'ORDER_DELIVER_SUCCESS'; - break; - case 3: - $change_type = $statusRepository::ORDER_DELIVERY_NOTHING; - $change_message = '订单已配送【虚拟发货】'; - $data['status'] = 2; - break; - case 4: - $exprss = app()->make(ExpressRepository::class)->getWhere(['code' => $data['delivery_name']]); - if (!$exprss) throw new ValidateException('快递公司不存在'); - $data['delivery_name'] = $exprss['name']; - $change_type = $statusRepository::ORDER_DELIVERY_COURIER; - $change_message = '订单已配送【快递名称】:' . $exprss['name'] . '; 【快递单号】:' . $data['delivery_id']; - $temp_code = 'DELIVER_GOODS_CODE'; - break; - } - - event('order.delivery.before', compact('order', 'data')); - $this->dao->update($id, $data); - - $orderStatus = [ - 'order_id' => $order->order_id, - 'order_sn' => $order->order_sn, - 'type' => $statusRepository::TYPE_ORDER, - 'change_message' => $change_message, - 'change_type' => $change_type, - ]; - if ($service_id) { - $statusRepository->createServiceLog($service_id,$orderStatus); - } else { - $statusRepository->createAdminLog($orderStatus); - } - - - //虚拟发货后用户直接确认收获 - if($data['status'] == 2){ - $user = app()->make(UserRepository::class)->get($order['uid']); - //订单记录 - $this->takeAfter($order,$user); - $orderStatus = [ - 'order_id' => $order->order_id, - 'order_sn' => $order->order_sn, - 'type' => $statusRepository::TYPE_ORDER, - 'change_message' => '虚拟发货后', - 'change_type' => $statusRepository::ORDER_STATUS_TAKE, - ]; - $statusRepository->createSysLog($orderStatus); - - } - if (isset($temp_code)) Queue::push(SendSmsJob::class, ['tempId' => $temp_code, 'id' => $order->order_id]); - - event('order.delivery', compact('order', 'data')); - return $data; - } - - /** - * TODO 同城配送 - * @param int $id - * @param int $merId - * @param array $data - * @author Qinii - * @day 2/16/22 - */ - public function cityDelivery(int $id, int $merId, array $data, $service_id) - { - $make = app()->make(DeliveryOrderRepository::class); - $order = $this->dao->get($id); - if ($order['is_virtual']) - throw new ValidateException('虚拟商品只能虚拟发货'); - $make->create($id, $merId, $data, $order); - //订单记录 - $storeOrderStatusRepository = app()->make(StoreOrderStatusRepository::class); - $this->dao->update($id, ['delivery_type' => 5, 'status' => 1,'remark' => $data['remark']]); - - $orderStatus = [ - 'order_id' => $id, - 'order_sn' => $order->order_sn, - 'type' => $storeOrderStatusRepository::TYPE_ORDER, - 'change_message' => '订单配送【同城配送】', - 'change_type' => $storeOrderStatusRepository::ORDER_DELIVERY_SELF, - ]; - if ($service_id) { - $storeOrderStatusRepository->createServiceLog($service_id,$orderStatus); - } else { - $storeOrderStatusRepository->createAdminLog($orderStatus); - } - - Queue::push(SendSmsJob::class, ['tempId' => 'ORDER_DELIVER_SUCCESS', 'id' => $id]); - } - - - public function getOne($id, ?int $merId) - { - $where = [$this->getPk() => $id]; - if ($merId) { - $whre['mer_id'] = $merId; - $whre['is_system_del'] = 0; - } - $res = $this->dao->getWhere($where, '*', [ - 'orderProduct', - 'user' => function ($query) { - $query->field('uid,real_name,nickname,is_svip,svip_endtime,phone'); - }, - 'refundOrder' => function ($query) { - $query->field('order_id,extension_one,extension_two,refund_price,integral')->where('status', 3); - }, - 'finalOrder', - 'TopSpread' => function ($query) { - $query->field('uid,nickname,avatar'); - }, - 'spread' => function ($query) { - $query->field('uid,nickname,avatar'); - }, - ] - ); - if (!$res) throw new ValidateException('数据不存在'); - $res['integral'] = (int)$res['integral']; - return $res->append(['refund_extension_one', 'refund_extension_two']); - } - - public function getOrderStatus($where, $page, $limit) - { - $where['type'] = StoreOrderStatusRepository::TYPE_ORDER; - return app()->make(StoreOrderStatusRepository::class)->search($where, $page, $limit); - } - - public function remarkForm($id) - { - $data = $this->dao->get($id); - $form = Elm::createForm(Route::buildUrl('merchantStoreOrderRemark', ['id' => $id])->build()); - $form->setRule([ - Elm::text('remark', '备注', $data['remark'])->required(), - ]); - return $form->setTitle('订单备注'); - } - - public function adminMarkForm($id) - { - $data = $this->dao->get($id); - $form = Elm::createForm(Route::buildUrl('systemMerchantOrderMark', ['id' => $id])->build()); - $form->setRule([ - Elm::text('admin_mark', '备注', $data['admin_mark'])->required(), - ]); - return $form->setTitle('订单备注'); - } - - /** - * TODO 平台每个商户的订单列表 - * @param $where - * @param $page - * @param $limit - * @return array - * @author Qinii - * @day 2020-06-15 - */ - public function adminMerGetList($where, $page, $limit) - { - $where['paid'] = 1; - $query = $this->dao->search($where, null); - $count = $query->count(); - $list = $query->with([ - 'orderProduct', - 'merchant' => function ($query) { - $query->field('mer_id,mer_name,is_trader'); - }, - 'groupOrder' => function ($query) { - $query->field('group_order_id,group_order_sn'); - }, - 'finalOrder', - 'user' => function ($query) { - $query->field('uid,nickname,avatar'); - }, - ])->page($page, $limit)->select()->append(['refund_extension_one', 'refund_extension_two']); - - return compact('count', 'list'); - } - - public function reconList($where, $page, $limit) - { - $ids = app()->make(MerchantReconciliationOrderRepository::class)->getIds($where); - $query = $this->dao->search([], null)->whereIn('order_id', $ids); - $count = $query->count(); - $list = $query->with(['orderProduct'])->page($page, $limit)->select()->each(function ($item) { - //(实付金额 - 一级佣金 - 二级佣金) * 抽成 - $commission_rate = ($item['commission_rate'] / 100); - //佣金 - $_order_extension = bcadd($item['extension_one'], $item['extension_two'], 3); - //手续费 = (实付金额 - 一级佣金 - 二级佣金) * 比例 - $_order_rate = bcmul(bcsub($item['pay_price'], $_order_extension, 3), $commission_rate, 3); - $item['order_extension'] = round($_order_extension, 2); - $item['order_rate'] = round($_order_rate, 2); - return $item; - }); - - return compact('count', 'list'); - } - - /** - * @param array $where - * @param $page - * @param $limit - * @return array - * @author Qinii - */ - public function merchantGetList(array $where, $page, $limit) - { - $status = $where['status']; - unset($where['status']); - $query = $this->dao->search($where)->where($this->getOrderType($status)) - ->with([ - 'orderProduct', - 'merchant' => function ($query) { - $query->field('mer_id,mer_name'); - }, - 'verifyService' => function ($query) { - $query->field('service_id,nickname'); - }, - 'finalOrder', - 'groupUser.groupBuying', - 'TopSpread' => function ($query) { - $query->field('uid,nickname,avatar'); - }, - 'spread' => function ($query) { - $query->field('uid,nickname,avatar'); - }, - ]); - $count = $query->count(); - $list = $query->page($page, $limit)->select()->append(['refund_extension_one', 'refund_extension_two']) - ->each(function($item){ - // 1:退款中 2:部分退款 3 = 全退 - $refunding = 0; - if ($item['orderProduct']) { - $is_refund = array_column($item['orderProduct']->toArray(),'is_refund'); - $is_refund = array_unique($is_refund); - if (in_array(1,$is_refund)) { - $refunding = 1; - } else if (in_array(2,$is_refund)) { - $refunding = 2; - } else if (in_array(3,$is_refund)) { - $refunding = 3; - } - } - $item['refunding'] = $refunding; - }); - - - return compact('count', 'list'); - } - - /** - * TODO 平台总的订单列表 - * @param array $where - * @param $page - * @param $limit - * @return array - * @author Qinii - * @day 2020-06-15 - */ - public function adminGetList(array $where, $page, $limit) - { - $status = $where['status']; - unset($where['status']); - $query = $this->dao->search($where, null)->where($this->getOrderType($status)) - ->with([ - 'orderProduct', - 'merchant' => function ($query) { - return $query->field('mer_id,mer_name,is_trader'); - }, - 'verifyService' => function ($query) { - return $query->field('service_id,nickname'); - }, - 'groupOrder' => function ($query) { - $query->field('group_order_id,group_order_sn'); - }, - 'finalOrder', - 'groupUser.groupBuying', - 'TopSpread' => function ($query) { - $query->field('uid,nickname,avatar'); - }, - 'spread' => function ($query) { - $query->field('uid,nickname,avatar'); - }, - 'user' => function ($query) { - $query->field('uid,nickname,avatar'); - }, - ]); - $count = $query->count(); - $list = $query->page($page, $limit)->select()->append(['refund_extension_one', 'refund_extension_two']); - - return compact('count', 'list'); - } - - public function getStat(array $where, $status) - { - unset($where['status']); - $make = app()->make(StoreRefundOrderRepository::class); - $presellOrderRepository = app()->make(PresellOrderRepository::class); - - //退款订单id - $orderId = $this->dao->search($where)->where($this->getOrderType($status))->column('order_id'); - //退款金额 - $orderRefund = $make->refundPirceByOrder($orderId); - //实际支付订单数量 - $all = $this->dao->search($where)->where($this->getOrderType($status))->where('paid', 1)->count(); - //实际支付订单金额 - $countQuery = $this->dao->search($where)->where($this->getOrderType($status))->where('paid', 1); - $countOrderId = $countQuery->column('order_id'); - $countPay1 = $countQuery->sum('StoreOrder.pay_price'); - $countPay2 = $presellOrderRepository->search(['paid' => 1, 'order_ids' => $countOrderId])->sum('pay_price'); - $countPay = bcadd($countPay1, $countPay2, 2); - - //余额支付 - $banclQuery = $this->dao->search(array_merge($where, ['paid' => 1, 'pay_type' => 0]))->where($this->getOrderType($status)); - $banclOrderId = $banclQuery->column('order_id'); - $banclPay1 = $banclQuery->sum('StoreOrder.pay_price'); - $banclPay2 = $presellOrderRepository->search(['pay_type' => [0], 'paid' => 1, 'order_ids' => $banclOrderId])->sum('pay_price'); - $banclPay = bcadd($banclPay1, $banclPay2, 2); - - //微信金额 - $wechatQuery = $this->dao->search($where)->where($this->getOrderType($status))->where('paid', 1)->where('pay_type', 'in', [1, 2, 3, 6]); - $wechatOrderId = $wechatQuery->column('order_id'); - $wechatPay1 = $wechatQuery->sum('StoreOrder.pay_price'); - $wechatPay2 = $presellOrderRepository->search(['pay_type' => [1, 2, 3, 6], 'paid' => 1, 'order_ids' => $wechatOrderId])->sum('pay_price'); - $wechatPay = bcadd($wechatPay1, $wechatPay2, 2); - - //支付宝金额 - $aliQuery = $this->dao->search($where)->where($this->getOrderType($status))->where('paid', 1)->where('pay_type', 'in', [4, 5]); - $aliOrderId = $aliQuery->column('order_id'); - $aliPay1 = $aliQuery->sum('StoreOrder.pay_price'); - $aliPay2 = $presellOrderRepository->search(['pay_type' => [4, 5], 'paid' => 1, 'order_ids' => $aliOrderId])->sum('pay_price'); - $aliPay = bcadd($aliPay1, $aliPay2, 2); - - - $stat = [ - [ - 'className' => 'el-icon-s-goods', - 'count' => $all, - 'field' => '件', - 'name' => '已支付订单数量' - ], - [ - 'className' => 'el-icon-s-order', - 'count' => (float)$countPay, - 'field' => '元', - 'name' => '实际支付金额' - ], - [ - 'className' => 'el-icon-s-cooperation', - 'count' => (float)$orderRefund, - 'field' => '元', - 'name' => '已退款金额' - ], - [ - 'className' => 'el-icon-s-cooperation', - 'count' => (float)$wechatPay, - 'field' => '元', - 'name' => '微信支付金额' - ], - [ - 'className' => 'el-icon-s-finance', - 'count' => (float)$banclPay, - 'field' => '元', - 'name' => '余额支付金额' - ], - [ - 'className' => 'el-icon-s-cooperation', - 'count' => (float)$aliPay, - 'field' => '元', - 'name' => '支付宝支付金额' - ], - ]; - return $stat; - } - - /** - * @param array $where - * @param $page - * @param $limit - * @return array - * @throws DataNotFoundException - * @throws DbException - * @throws ModelNotFoundException - * @author xaboy - * @day 2020/6/10 - */ - public function getList(array $where, $page, $limit) - { - $query = $this->dao->search($where)->where('StoreOrder.is_del', 0); - $count = $query->count(); - $list = $query->with([ - 'orderProduct', - 'presellOrder', - 'merchant' => function ($query) { - return $query->field('mer_id,mer_name'); - }, - 'community', - 'receipt' => function ($query) { - return $query->field('order_id,order_receipt_id'); - }, - ])->page($page, $limit)->order('pay_time DESC')->append(['refund_status'])->select(); - - foreach ($list as $order) { - if ($order->activity_type == 2) { - if ($order->presellOrder) { - $order->presellOrder->append(['activeStatus']); - $order->presell_price = bcadd($order->pay_price, $order->presellOrder->pay_price, 2); - } else { - $order->presell_price = $order->pay_price; - } - } - $order->takeOrderCount = count($order['takeOrderList']); - unset($order['takeOrderList']); - } - - return compact( 'count','list'); - } - - public function userList($uid, $page, $limit) - { - $query = $this->dao->search([ - 'uid' => $uid, - 'paid' => 1 - ]); - $count = $query->count(); - $list = $query->page($page, $limit)->select(); - return compact('count', 'list'); - } - - - public function userMerList($uid, $merId, $page, $limit) - { - $query = $this->dao->search([ - 'uid' => $uid, - 'mer_id' => $merId, - 'paid' => 1 - ]); - $count = $query->count(); - $list = $query->with(['presellOrder'])->page($page, $limit)->select(); - foreach ($list as $order) { - if ($order->activity_type == 2 && $order->status >= 0 && $order->status < 10 && $order->presellOrder) { - $order->pay_price = bcadd($order->pay_price, $order->presellOrder->pay_price, 2); - } - } - return compact('count', 'list'); - } - - public function express(int $orderId, ?int $merId) - { - $order = $this->dao->get($orderId); - if ($merId && $order['mer_id'] != $merId) throw new ValidateException('订单信息不存在'); - if (!in_array($order['delivery_type'], [1, 4])) throw new ValidateException('订单状态错误'); - return ExpressService::express($order->delivery_id, $order->delivery_name, $order->user_phone); - } - - public function checkPrinterConfig(int $merId) - { - if (!merchantConfig($merId, 'printing_status')) - throw new ValidateException('打印功能未开启'); - $config = [ - 'clientId' => merchantConfig($merId, 'printing_client_id'), - 'apiKey' => merchantConfig($merId, 'printing_api_key'), - 'partner' => merchantConfig($merId, 'develop_id'), - 'terminal' => merchantConfig($merId, 'terminal_number') - ]; - if (!$config['clientId'] || !$config['apiKey'] || !$config['partner'] || !$config['terminal']) - throw new ValidateException('打印机配置错误'); - return $config; - } - - /** - * TODO 打印机 -- 暂无使用 - * @param int $id - * @param int $merId - * @return bool|mixed|string - * @author Qinii - * @day 2020-07-30 - */ - public function printer(int $id, int $merId) - { - $order = $this->dao->getWhere(['order_id' => $id], '*', ['orderProduct', 'merchant' => function ($query) { - $query->field('mer_id,mer_name'); - }]); - foreach ($order['orderProduct'] as $item) { - $product[] = [ - 'store_name' => $item['cart_info']['product']['store_name'] . '【' . $item['cart_info']['productAttr']['sku'] . '】', - 'product_num' => $item['product_num'], - 'price' => bcdiv($item['product_price'], $item['product_num'], 2), - 'product_price' => $item['product_price'], - ]; - } - $data = [ - 'order_sn' => $order['order_sn'], - 'pay_time' => $order['pay_time'], - 'real_name' => $order['real_name'], - 'user_phone' => $order['user_phone'], - 'user_address' => $order['user_address'], - 'total_price' => $order['total_price'], - 'coupon_price' => $order['coupon_price'], - 'pay_price' => $order['pay_price'], - 'total_postage' => $order['total_postage'], - 'pay_postage' => $order['pay_postage'], - 'mark' => $order['mark'], - ]; - $config = $this->checkPrinterConfig($merId); - $printer = new Printer('yi_lian_yun', $config); - event('order.print.before', compact('order')); - - $res = $printer->setPrinterContent([ - 'name' => $order['merchant']['mer_name'], - 'orderInfo' => $data, - 'product' => $product - ])->startPrinter(); - - event('order.print', compact('order', 'res')); - - return $res; - } - - public function batchPrinter(int $id, int $merId) - { - $order = $this->dao->getWhere(['order_id' => $id], '*', ['orderProduct', 'merchant' => function ($query) { - $query->field('mer_id,mer_name'); - }]); - - foreach ($order['orderProduct'] as $item) { - $product[] = [ - 'store_name' => $item['cart_info']['product']['store_name'] . '【' . $item['cart_info']['productAttr']['sku'] . '】', - 'product_num' => $item['product_num'], - 'price' => bcdiv($item['product_price'], $item['product_num'], 2), - 'product_price' => $item['product_price'], - ]; - } - - $data = [ - 'order_sn' => $order['order_sn'], - 'order_type' => $order['order_type'], - 'pay_time' => $order['pay_time'], - 'real_name' => $order['real_name'], - 'user_phone' => $order['user_phone'], - 'user_address' => $order['user_address'], - 'total_price' => $order['total_price'], - 'coupon_price' => $order['coupon_price'], - 'pay_price' => $order['pay_price'], - 'total_postage' => $order['total_postage'], - 'pay_postage' => $order['pay_postage'], - 'mark' => $order['mark'], - ]; - - $printer = app()->make(StorePrinterRepository::class)->getPrinter($merId); - event('order.print.before', compact('order')); - foreach ($printer as $config) { - $printer = new Printer('yi_lian_yun', $config); - $res = $printer->setPrinterContent([ - 'name' => $order['merchant']['mer_name'], - 'orderInfo' => $data, - 'product' => $product - ])->startPrinter(); - } - - event('order.print', compact('order', 'res')); - } - - - public function verifyOrder(int $id, int $merId, array $data, $serviceId = 0) - { - $order = $this->dao->getWhere(['order_id' => $id, 'mer_id' => $merId,'verify_code' => $data['verify_code'],'order_type' => 1],'*',['orderProduct']); - if (!$order) throw new ValidateException('订单不存在'); - if (!$order->paid) throw new ValidateException('订单未支付'); - if ($order['status']) throw new ValidateException('订单已全部核销,请勿重复操作'); - foreach ($data['data'] as $v) { - $splitData[$v['id']] = $v['num']; - } - $spl = app()->make(StoreOrderSplitRepository::class)->splitOrder($order, $splitData, $serviceId, 1); - if ($spl) $order = $spl; - $order->status = 2; - $order->verify_time = date('Y-m-d H:i:s'); - $order->verify_service_id = $serviceId; - event('order.verify.before', compact('order')); - //订单记录 - $storeOrderStatusRepository = app()->make(StoreOrderStatusRepository::class); - Db::transaction(function () use ($order,$storeOrderStatusRepository,$serviceId) { - $this->takeAfter($order, $order->user); - $order->save(); - $orderStatus = [ - 'order_id' => $order->order_id, - 'order_sn' => $order->order_sn, - 'type' => $storeOrderStatusRepository::TYPE_ORDER, - 'change_message' => '订单已核销', - 'change_type' => $storeOrderStatusRepository::ORDER_STATUS_TAKE, - ]; - if ($serviceId){ - $storeOrderStatusRepository->createServiceLog($serviceId,$orderStatus); - } else { - $storeOrderStatusRepository->createAdminLog($orderStatus); - } - - }); - event('order.verify', compact('order')); - } - - public function wxQrcode($orderId, $verify_code) - { - $siteUrl = systemConfig('site_url'); - $name = md5('owx' . $orderId . date('Ymd')) . '.jpg'; - $attachmentRepository = app()->make(AttachmentRepository::class); - $imageInfo = $attachmentRepository->getWhere(['attachment_name' => $name]); - - if (isset($imageInfo['attachment_src']) && strstr($imageInfo['attachment_src'], 'http') !== false && curl_file_exist($imageInfo['attachment_src']) === false) { - $imageInfo->delete(); - $imageInfo = null; - } - if (!$imageInfo) { - // $codeUrl = set_http_type(rtrim($siteUrl, '/') . '/pages/admin/order_cancellation/index?verify_code=' . $verify_code, request()->isSsl() ? 0 : 1);//二维码链接 - $imageInfo = app()->make(QrcodeService::class)->getQRCodePath($verify_code, $name); - if (is_string($imageInfo)) throw new ValidateException('二维码生成失败'); - - $imageInfo['dir'] = tidy_url($imageInfo['dir'], null, $siteUrl); - - $attachmentRepository->create(systemConfig('upload_type') ?: 1, -2, $orderId, [ - 'attachment_category_id' => 0, - 'attachment_name' => $imageInfo['name'], - 'attachment_src' => $imageInfo['dir'] - ]); - $urlCode = $imageInfo['dir']; - } else $urlCode = $imageInfo['attachment_src']; - return $urlCode; - } - - /** - * TODO 根据商品ID获取订单数 - * @param int $productId - * @return int - * @author Qinii - * @day 2020-08-05 - */ - public function seckillOrderCounut(int $productId) - { - $where = [ - 'activity_id' => $productId, - 'product_type' => 1, - 'day' => date('Y-m-d', time()) - ]; - $count = $this->dao->getTattendCount($where, null)->count(); - $count_ = $this->dao->getSeckillRefundCount($where, 2); - $count__ = $this->dao->getSeckillRefundCount($where, 1); - return $count - $count_ - $count__; - } - - /** - * TODO 根据商品sku获取订单数 - * @param int $productId - * @return int - * @author Qinii - * @day 2020-08-05 - */ - public function seckillSkuOrderCounut(string $sku) - { - $where = [ - 'product_sku' => $sku, - 'product_type' => 1, - 'day' => date('Y-m-d', time()) - ]; - $count = $this->dao->getTattendCount($where, null)->count(); - $count_ = $this->dao->getSeckillRefundCount($where, 2); - $count__ = $this->dao->getSeckillRefundCount($where, 1); - return $count - $count_ - $count__; - } - - /** - * TODO 获取sku的总销量 - * @param string $sku - * @return int|mixed - * @author Qinii - * @day 3/4/21 - */ - public function skuSalesCount(string $sku) - { - $where = [ - 'product_sku' => $sku, - 'product_type' => 1, - ]; - $count = $this->dao->getTattendSuccessCount($where, null)->count(); - $count_ = $this->dao->getSeckillRefundCount($where, 2); - $count__ = $this->dao->getSeckillRefundCount($where, 1); - return $count - $count_ - $count__; - } - - /** - * TODO 秒杀获取个人当天限购 - * @param int $uid - * @param int $productId - * @return int - * @author Qinii - * @day 2020-08-15 - */ - public function getDayPayCount(int $uid, int $productId) - { - $make = app()->make(StoreSeckillActiveRepository::class); - $active = $make->getWhere(['product_id' => $productId]); - if ($active['once_pay_count'] == 0) return true; - - $where = [ - 'activity_id' => $productId, - 'product_type' => 1, - 'day' => date('Y-m-d', time()) - ]; - - $count = $this->dao->getTattendCount($where, $uid)->count(); - return ($active['once_pay_count'] > $count); - } - - /** - * TODO 秒杀获取个人总限购 - * @param int $uid - * @param int $productId - * @return int - * @author Qinii - * @day 2020-08-15 - */ - public function getPayCount(int $uid, int $productId) - { - $make = app()->make(StoreSeckillActiveRepository::class); - $active = $make->getWhere(['product_id' => $productId]); - if ($active['all_pay_count'] == 0) return true; - $where = [ - 'activity_id' => $productId, - 'product_type' => 1, - 'day' => date('Y-m-d', time()) - ]; - $count = $this->dao->getTattendCount($where, $uid)->count(); - return ($active['all_pay_count'] > $count); - } - - /** - * 根据订单id查看是否全部退款 - * @Author:Qinii - * @Date: 2020/9/11 - * @param int $orderId - * @return bool - */ - public function checkRefundStatusById(int $orderId, int $refundId) - { - return Db::transaction(function () use ($orderId, $refundId) { - $res = $this->dao->search(['order_id' => $orderId])->with(['orderProduct'])->find(); - $refund = app()->make(StoreRefundOrderRepository::class)->getRefundCount($orderId, $refundId); - if ($refund) return false; - foreach ($res['orderProduct'] as $item) { - if ($item['refund_num'] !== 0) return false; - $item->is_refund = 3; - $item->save(); - } - $res->status = -1; - $res->save(); - $this->orderRefundAllAfter($res); - return true; - }); - } - - public function orderRefundAllAfter($order) - { - - if ($order->activity_type == 10) { - app()->make(StoreDiscountRepository::class)->incStock($order->orderProduct[0]['activity_id']); - } - $mainId = $order->main_id ?: $order->order_id; - $count = $this->query([])->where('status', '<>', -1)->where(function ($query) use ($mainId) { - $query->where('order_id', $mainId)->whereOr('main_id', $mainId); - })->count(); - //拆单后完全退完 - if (!$count) { - if ($order->main_id) { - $order = $this->query(['order_id' => $mainId])->find(); - } - $couponId = []; - if ($order->coupon_id) { - $couponId = explode(',', $order->coupon_id); - } - app()->make(MerchantRepository::class)->computedLockMoney($order); - //总单所有订单全部退完 - if (!$this->query([])->where('status', '<>', -1)->where('group_order_id', $order->group_order_id)->count()) { - if ($order->groupOrder->coupon_id) { - $couponId[] = $order->groupOrder->coupon_id; - } - } - if (count($couponId)) { - app()->make(StoreCouponUserRepository::class)->updates($couponId, ['status' => 0]); - } - - } - //订单记录 - $storeOrderStatusRepository = app()->make(StoreOrderStatusRepository::class); - $orderStatus = [ - 'order_id' => $order->order_id, - 'order_sn' => $order->order_sn, - 'type' => $storeOrderStatusRepository::TYPE_ORDER, - 'change_message' => '订单已全部退款', - 'change_type' => $storeOrderStatusRepository::ORDER_STATUS_REFUND_ALL, - ]; - $storeOrderStatusRepository->createSysLog($orderStatus); - - event('order.refundAll', compact('order')); - } - - /** - * @param $id - * @param $uid - * @throws DataNotFoundException - * @throws DbException - * @throws ModelNotFoundException - * @author xaboy - * @day 2020/9/17 - */ - public function userDel($id, $uid) - { - $order = $this->dao->getWhere([['status', 'in', [0, 3, -1, 11]], ['order_id', '=', $id], ['uid', '=', $uid], ['is_del', '=', 0]]); - if (!$order || ($order->status == 0 && $order->paid == 1)) - throw new ValidateException('订单状态有误'); - event('order.userDel.before', compact('order')); - $this->delOrder($order, '订单删除'); - event('order.userDel', compact('order')); - } - - public function delOrder($order, $info = '订单删除') - { - //订单记录 - $storeOrderStatusRepository = app()->make(StoreOrderStatusRepository::class); - $orderStatus = [ - 'order_id' => $order->order_id, - 'order_sn' => $order->order_sn, - 'type' => $storeOrderStatusRepository::TYPE_ORDER, - 'change_message' => $info, - 'change_type' => $storeOrderStatusRepository::ORDER_STATUS_DELETE, - ]; - $productRepository = app()->make(ProductRepository::class); - Db::transaction(function () use ($info, $order, $orderStatus, $storeOrderStatusRepository,$productRepository) { - $order->is_del = 1; - $order->save(); - $storeOrderStatusRepository->createUserLog($orderStatus); - foreach ($order->orderProduct as $cart) { - $productRepository->orderProductIncStock($order, $cart); - } - }); - } - - public function merDelete($id) - { - Db::transaction(function () use ($id) { - $data['is_system_del'] = 1; - $this->dao->update($id, $data); - app()->make(StoreOrderReceiptRepository::class)->deleteByOrderId($id); - }); - } - - /** - * @param $id - * @return \FormBuilder\Form - * @author Qinii - */ - public function sendProductForm($id, $data) - { - $express = app()->make(ExpressRepository::class)->options(); - $form = Elm::createForm(Route::buildUrl('merchantStoreOrderDelivery', ['id' => $id])->build()); - - if (in_array($data['delivery_type'], [1, 2])) { - if ($data['delivery_type'] == 1) { - $form->setRule([ - Elm::hidden('delivery_type', 1), - [ - 'type' => 'span', - 'title' => '原快递名称', - 'children' => [(string)$data['delivery_name']] - ], - [ - 'type' => 'span', - 'title' => '原快递单号', - 'children' => [(string)$data['delivery_id']] - ], - Elm::select('delivery_name', '快递名称')->options(function () use ($express) { - return $express; - }), - Elm::input('delivery_id', '快递单号')->required(), - ]); - } else { - $form->setRule([ - Elm::hidden('delivery_type', 2), - [ - 'type' => 'span', - 'title' => '原送货人姓名', - 'children' => [(string)$data['delivery_name']] - ], - [ - 'type' => 'span', - 'title' => '原手机号', - 'children' => [(string)$data['delivery_id']] - ], - Elm::input('delivery_name', '送货人姓名')->required(), - Elm::input('delivery_id', '手机号')->required(), - ]); - } - } - if ($data['delivery_type'] == 3) { - $form->setRule([ - Elm::hidden('delivery_type', 3), - [ - 'type' => 'span', - 'title' => '发货类型', - 'children' => ['无需配送'] - ] - ]); - } - if (!$data['delivery_type']) { - $form->setRule([ - Elm::radio('delivery_type', '发货类型', 1) - ->setOptions([ - ['value' => 1, 'label' => '发货'], - ['value' => 2, 'label' => '送货'], - ['value' => 3, 'label' => '无需配送'], - ])->control([ - [ - 'value' => 1, - 'rule' => [ - Elm::select('delivery_name', '快递名称')->options(function () use ($express) { - return $express; - }), - Elm::input('delivery_id', '快递单号')->required(), - ] - ], - [ - 'value' => 2, - 'rule' => [ - Elm::input('delivery_name', '送货人姓名')->required(), - Elm::input('delivery_id', '手机号')->required(), - ] - ], - [ - 'value' => 3, - 'rule' => [] - ], - - ]), - ]); - } - - return $form->setTitle('发货信息'); - } - - /** - * TODO 导入发货信息 - * @param array $data - * @param $merId - * @author Qinii - * @day 3/16/21 - */ - public function setWhereDeliveryStatus(array $arrary, $merId) - { - //读取excel - $data = SpreadsheetExcelService::instance()->_import($arrary['path'], $arrary['sql'], $arrary['where'], 4); - if (!$data) return; - $import_id = $arrary['import_id']; - Db::transaction(function () use ($data, $merId, $import_id) { - $result = []; - $num = 0; - $count = 0; - $status = 0; - foreach ($data as $datum) { - $value = []; - $ret = []; - if ($datum['where']) { - $count = $count + 1; - if (empty($datum['value']['delivery_id'])) { - $mark = '发货单号为空'; - } else { - $ret = $this->getSearch([]) - ->where('status', 0) - ->where('paid', 1) - ->where('order_type', 0) - ->where('mer_id', $merId) - ->where($datum['where']) - ->find(); - $mark = '数据有误或已发货'; - } - if ($ret) { - try { - $value = array_merge($datum['value'], ['status' => 1]); - $value['delivery_type'] = 1; - $this->delivery($ret['order_id'], $merId, $value); - - $status = 1; - $mark = ''; - - $num = $num + 1; - } catch (\Exception $exception) { - $mark = $exception->getMessage(); - } - } - $datum['where']['mark'] = $mark; - $datum['where']['mer_id'] = $merId; - $datum['where']['status'] = $status; - $datum['where']['import_id'] = $import_id; - $result[] = array_merge($datum['where'], $datum['value']); - } - } - // 记录入库操作 - if (!empty($result)) app()->make(StoreImportDeliveryRepository::class)->insertAll($result); - $_status = ($count == $num) ? 1 : (($num < 1) ? -1 : 10); - app()->make(StoreImportRepository::class)->update($import_id, ['count' => $count, 'success' => $num, 'status' => $_status]); - }); - if (file_exists($arrary['path'])) unlink($arrary['path']); - } - - /** - * TODO 根据订单查询相关联的自订单 - * @param $id - * @param $merId - * @return \think\Collection - * @author Qinii - * @day 2023/2/22 - */ - public function childrenList($id,$merId) - { - $data = $this->dao->get($id); - $query = $this->dao->getSearch([])->with(['orderProduct'])->where('order_id','<>',$id); - if ($merId) $query->where('mer_id',$merId); - if ($data['main_id']) { - $query->where(function($query) use($data,$id){ - $query->where('main_id',$data['main_id'])->whereOr('order_id',$data['main_id']); - }); - } else { - $query->where('main_id',$id); - } - return $query->select(); - } -} diff --git a/app/common/repositories/system/merchant/MerchantRepository.php b/app/common/repositories/system/merchant/MerchantRepository.php index a2a5f87f..fdadc123 100644 --- a/app/common/repositories/system/merchant/MerchantRepository.php +++ b/app/common/repositories/system/merchant/MerchantRepository.php @@ -223,7 +223,7 @@ class MerchantRepository extends BaseRepository */ public function updateForm($id) { - $data = $this->dao->get($id)->toArray(); + $data = $this->dao->get($id)->hidden(['coordinates'])->toArray(); /** @var MerchantAdminRepository $make */ $make = app()->make(MerchantAdminRepository::class); $data['mer_account'] = $make->merIdByAccount($id); diff --git a/app/controller/admin/system/merchant/Merchant.php.bak b/app/controller/admin/system/merchant/Merchant.php.bak deleted file mode 100644 index 2c901672..00000000 --- a/app/controller/admin/system/merchant/Merchant.php.bak +++ /dev/null @@ -1,300 +0,0 @@ - -// +---------------------------------------------------------------------- - - -namespace app\controller\admin\system\merchant; - - -use app\common\repositories\store\product\ProductCopyRepository; -use app\common\repositories\system\merchant\MerchantTypeRepository; -use app\common\repositories\user\UserBillRepository; -use crmeb\basic\BaseController; -use app\common\repositories\system\merchant\MerchantAdminRepository; -use app\common\repositories\system\merchant\MerchantCategoryRepository; -use app\common\repositories\system\merchant\MerchantRepository; -use app\validate\admin\MerchantValidate; -use crmeb\jobs\ChangeMerchantStatusJob; -use FormBuilder\Exception\FormBuilderException; -use think\App; -use think\db\exception\DataNotFoundException; -use think\db\exception\DbException; -use think\db\exception\ModelNotFoundException; -use think\facade\Queue; - -/** - * Class Merchant - * @package app\controller\admin\system\merchant - * @author xaboy - * @day 2020-04-16 - */ -class Merchant extends BaseController -{ - /** - * @var MerchantRepository - */ - protected $repository; - - /** - * Merchant constructor. - * @param App $app - * @param MerchantRepository $repository - */ - public function __construct(App $app, MerchantRepository $repository) - { - parent::__construct($app); - $this->repository = $repository; - } - - public function count() - { - $where = $this->request->params(['keyword', 'date', 'status', 'statusTag', 'is_trader', 'category_id', 'type_id']); - return app('json')->success($this->repository->count($where)); - } - - /** - * @return mixed - * @throws DataNotFoundException - * @throws DbException - * @throws ModelNotFoundException - * @author xaboy - * @day 2020-04-16 - */ - public function lst() - { - [$page, $limit] = $this->getPage(); - $where = $this->request->params(['keyword', 'date', 'status', 'statusTag', 'is_trader', 'category_id', 'type_id']); - return app('json')->success($this->repository->lst($where, $page, $limit)); - } - - - /** - * @return mixed - * @throws FormBuilderException - * @author xaboy - * @day 2020-04-16 - */ - public function createForm() - { - return app('json')->success(formToData($this->repository->form())); - } - - /** - * @param MerchantValidate $validate - * @param MerchantCategoryRepository $merchantCategoryRepository - * @param MerchantAdminRepository $adminRepository - * @return mixed - * @author xaboy - * @day 2020/7/2 - */ - public function create(MerchantValidate $validate) - { - $data = $this->checkParam($validate); - $this->repository->createMerchant($data); - return app('json')->success('添加成功'); - } - - - /** - * @param int $id - * @return mixed - * @throws FormBuilderException - * @throws DataNotFoundException - * @throws DbException - * @throws ModelNotFoundException - * @author xaboy - * @day 2020-04-16 - */ - public function updateForm($id) - { - if (!$this->repository->exists($id)) - return app('json')->fail('数据不存在'); - - return app('json')->success(formToData($this->repository->updateForm($id))); - } - - /** - * @param int $id - * @param MerchantValidate $validate - * @param MerchantCategoryRepository $merchantCategoryRepository - * @return mixed - * @throws DbException - * @author xaboy - * @day 2020-05-06 - */ - public function update($id, MerchantValidate $validate, MerchantCategoryRepository $merchantCategoryRepository) - { - $data = $this->checkParam($validate, true); - if (!$this->repository->exists($id)) - return app('json')->fail('数据不存在'); - if ($this->repository->fieldExists('mer_name', $data['mer_name'], $id)) - return app('json')->fail('商户名已存在'); - if ($data['mer_phone'] && isPhone($data['mer_phone'])) - return app('json')->fail('请输入正确的手机号'); - if (!$data['category_id'] || !$merchantCategoryRepository->exists($data['category_id'])) - return app('json')->fail('商户分类不存在'); - - unset($data['mer_account'], $data['mer_password']); - $margin = $this->repository->checkMargin($id, $data['type_id']); - $data['margin'] = $margin['margin']; - $data['is_margin'] = $margin['is_margin']; - $this->repository->update($id, $data); - return app('json')->success('编辑成功'); - } - - /** - * @param int $id - * @return mixed - * @throws DbException - * @author xaboy - * @day 2020-04-17 - */ - public function delete($id) - { - if (!$merchant = $this->repository->get(intval($id))) - return app('json')->fail('数据不存在'); - if ($merchant->status) - return app('json')->fail('请先关闭该商户'); - $this->repository->delete($id); - return app('json')->success('编辑成功'); - } - - /** - * @param MerchantValidate $validate - * @param bool $isUpdate - * @return array - * @author xaboy - * @day 2020-04-17 - */ - public function checkParam(MerchantValidate $validate, $isUpdate = false) - { - $data = $this->request->params([['category_id', 0], ['type_id', 0], 'mer_name', 'commission_rate', 'real_name', 'mer_phone', 'mer_keyword', 'mer_address', 'mark', ['sort', 0], ['status', 0], ['is_audit', 0], ['is_best', 0], ['is_bro_goods', 0], ['is_bro_room', 0], ['is_trader', 0],'sub_mchid']); - if (!$isUpdate) { - $data += $this->request->params(['mer_account', 'mer_password']); - }else { - $validate->isUpdate(); - unset($data['status']); - } - $validate->check($data); - return $data; - } - - /** - * @param int $id - * @return mixed - * @throws DbException - * @author xaboy - * @day 2020-03-31 - */ - public function switchStatus($id) - { - $is_best = $this->request->param('status', 0) == 1 ? 1 : 0; - if (!$this->repository->exists($id)) - return app('json')->fail('数据不存在'); - $this->repository->update($id, compact('is_best')); - return app('json')->success('修改成功'); - } - - /** - * @param int $id - * @return mixed - * @throws DbException - * @author xaboy - * @day 2020-03-31 - */ - public function switchClose($id) - { - $status = $this->request->param('status', 0) == 1 ? 1 : 0; - if (!$this->repository->exists($id)) - return app('json')->fail('数据不存在'); - $this->repository->update($id, compact('status')); - Queue::push(ChangeMerchantStatusJob::class, $id); - return app('json')->success('修改成功'); - } - - /** - * @param $id - * @param MerchantAdminRepository $adminRepository - * @return mixed - * @throws DataNotFoundException - * @throws DbException - * @throws ModelNotFoundException - * @author xaboy - * @day 2020/7/7 - */ - public function login($id, MerchantAdminRepository $adminRepository) - { - if (!$this->repository->exists($id)) - return app('json')->fail('数据不存在'); - $adminInfo = $adminRepository->merIdByAdmin($id); - $tokenInfo = $adminRepository->createToken($adminInfo); - $admin = $adminInfo->toArray(); - unset($admin['pwd']); - $data = [ - 'token' => $tokenInfo['token'], - 'exp' => $tokenInfo['out'], - 'admin' => $admin, - 'url' => '/' . config('admin.merchant_prefix') - ]; - - return app('json')->success($data); - } - - /** - * TODO 修改复制次数表单 - * @param $id - * @return mixed - * @author Qinii - * @day 2020-08-06 - */ - public function changeCopyNumForm($id) - { - return app('json')->success(formToData($this->repository->copyForm($id))); - } - - /** - * TODO 修改复制次数 - * @param $id - * @return mixed - * @author Qinii - * @day 2020-08-06 - */ - public function changeCopyNum($id) - { - $data = $this->request->params(['type', 'num']); - $num = $data['num']; - if ($num <= 0) return app('json')->fail('次数必须为正整数'); - if ($data['type'] == 2) { - $mer_num = $this->repository->getCopyNum($id); - if (($mer_num - $num) < 0) return app('json')->fail('剩余次数不足'); - $num = '-' . $data['num']; - } - $arr = [ - 'type' => 'sys', - 'num' => $num, - 'message' => '平台修改「' . $this->request->adminId() . '」', - ]; - app()->make(ProductCopyRepository::class)->add($arr, $id); - return app('json')->success('修改成功'); - } - - /** - * TODO 清理删除的商户内容 - * @return \think\response\Json - * @author Qinii - * @day 5/15/21 - */ - public function clearRedundancy() - { - $this->repository->clearRedundancy(); - return app('json')->success('清除完成'); - } -}