diff --git a/app/common/repositories/store/order/StoreOrderCreateRepository.php b/app/common/repositories/store/order/StoreOrderCreateRepository.php index 379a0baf..ec703a28 100644 --- a/app/common/repositories/store/order/StoreOrderCreateRepository.php +++ b/app/common/repositories/store/order/StoreOrderCreateRepository.php @@ -32,6 +32,8 @@ use think\facade\Queue; class StoreOrderCreateRepository extends StoreOrderRepository { + protected $store_consumption_user; + protected $consumption_money; public function v2CartIdByOrderInfo($user, array $cartId, array $takes = null, array $useCoupon = null, bool $useIntegral = false, int $addressId = null, $createOrder = false,$consumption_id=0) { @@ -147,6 +149,10 @@ class StoreOrderCreateRepository extends StoreOrderRepository } $orderDeliveryStatus = true; $order_svip_discount = 0; + + if($consumption_id>0){ + $this->store_consumption_user=Db::name('store_consumption_user')->where('coupon_user_id',$consumption_id)->where('uid',$uid)->find(); + } // 循环计算每个店铺的订单数据 委托商品是否设置收货方式 ? foreach ($merchantCartList as &$merchantCart) { if ($order_type == 98 && !empty($deliverMethodArray)) { @@ -559,24 +565,33 @@ class StoreOrderCreateRepository extends StoreOrderRepository } else { $pay_price = $org_price; } - - $consumption_money=0; + //计算总红包金额 + $a=0; if($consumption_id>0){ - $store_consumption_user=Db::name('store_consumption_user')->where('coupon_user_id',$consumption_id)->where('uid',$uid)->find(); - if($store_consumption_user){ - if($store_consumption_user['type']==2){ - $a=bcdiv($pay_price,4); - if($store_consumption_user['balance']>$a){ + if($this->store_consumption_user){ + if($this->store_consumption_user['type']==2){ + $a=bcdiv($pay_price,6); + if($this->store_consumption_user['balance']>$a){ $pay_price = bcsub($pay_price, $a, 2); - $consumption_money=$a; + $this->consumption_money= bcadd($this->consumption_money,$a); + $this->store_consumption_user['balance']= bcsub($this->store_consumption_user['balance'],$a); }else{ - $pay_price = bcsub($pay_price, $store_consumption_user['balance'], 2); - $consumption_money=$store_consumption_user['balance']; + $a= $this->store_consumption_user['balance']; + $pay_price = bcsub($pay_price, $this->store_consumption_user['balance'], 2); + $this->consumption_money= bcadd($this->consumption_money,$this->store_consumption_user['balance']); + $this->store_consumption_user['balance']= 0; } } - if($store_consumption_user['type']==1){ - $pay_price = bcsub($pay_price, $store_consumption_user['balance'], 2); - $consumption_money=$store_consumption_user['balance']; + if($this->store_consumption_user['type']==1){ + if($pay_price>$this->store_consumption_user['balance']){ + $pay_price = bcsub($pay_price, $this->store_consumption_user['balance'], 2); + $this->consumption_money=$this->store_consumption_user['balance']; + $a=$this->store_consumption_user['balance']; + }else{ + $pay_price=0; + $this->consumption_money=bcsub($this->store_consumption_user['balance'],$pay_price, 2); + $a=$this->consumption_money; + } } } } @@ -597,6 +612,8 @@ class StoreOrderCreateRepository extends StoreOrderRepository $merchantCart['order']['coupon_price'] = $coupon_price; $merchantCart['order']['postage_price'] = $merchantCart['order']['postage_price']; $merchantCart['order']['procure_price'] = $merchantCart['order']['procure_price']; + $merchantCart['order']['consumption_id'] = $consumption_id; + $merchantCart['order']['consumption_money'] = $a; $order_price = bcadd($order_price, $pay_price, 2); $order_total_price = bcadd($order_total_price, $total_price, 2); @@ -618,6 +635,7 @@ class StoreOrderCreateRepository extends StoreOrderRepository unset($merchantCart); $status = ($address || $order_model || $allow_no_address) ? ($noDeliver ? 'noDeliver' : 'finish') : 'noAddress'; $order = $merchantCartList; + $consumption_money = $this->consumption_money; $total_price = $order_total_price; $openIntegral = $merIntegralFlag && !$order_type && $sysIntegralConfig['integral_status'] && $sysIntegralConfig['integral_money'] > 0; $total_coupon = bcadd($order_svip_discount, bcadd(bcadd($total_platform_coupon_price, $order_coupon_price, 2), $order_total_integral_price, 2), 2); @@ -730,7 +748,7 @@ class StoreOrderCreateRepository extends StoreOrderRepository $merchantRepository = app()->make(MerchantRepository::class); $giveCouponIds = []; - $ex = systemConfig('extension_status'); + $ex = systemConfig('extension_status');//是否开启分销 $address = $orderInfo['address']; $allUseCoupon = $orderInfo['usePlatformCouponId'] ? [$orderInfo['usePlatformCouponId']] : []; $totalNum = 0; @@ -750,63 +768,6 @@ class StoreOrderCreateRepository extends StoreOrderRepository $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); @@ -860,6 +821,8 @@ class StoreOrderCreateRepository extends StoreOrderRepository 'platform_coupon_price' => $merchantCart['order']['platform_coupon_price'], 'pay_type' => $pay_type, 'procure_price' => $merchantCart['order']['procure_price'], + 'consumption_id' => $merchantCart['order']['consumption_id'], + 'consumption_money' => $merchantCart['order']['consumption_money'], ]; $allUseCoupon = array_merge($allUseCoupon, $merchantCart['order']['useCouponIds']); $orderList[] = $_order; @@ -867,6 +830,7 @@ class StoreOrderCreateRepository extends StoreOrderRepository $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'), @@ -886,6 +850,8 @@ class StoreOrderCreateRepository extends StoreOrderRepository 'integral' => $orderInfo['order_total_integral'], 'integral_price' => $orderInfo['order_total_integral_price'], 'give_integral' => $orderInfo['order_total_give_integral'], + 'consumption_id' => $orderInfo['consumption_id'], + 'consumption_money' => $orderInfo['consumption_money'], ]; event('order.create.before', compact('groupOrder', 'orderList')); $group = Db::transaction(function () use ($ex, $user, $topUid, $spreadUid, $uid, $receipt_data, $cartIds, $allUseCoupon, $groupOrder, $orderList, $orderInfo) { @@ -984,6 +950,30 @@ class StoreOrderCreateRepository extends StoreOrderRepository $user->save(); } + if ($groupOrder['consumption_money'] > 0) { + $balance=0; + $store_consumption_user= Db::name('store_consumption_user')->where('coupon_user_id',$groupOrder['coupon_user_id'])->where('status',0)->find(); + if($store_consumption_user){ + if($store_consumption_user['type']==1){ + if($store_consumption_user['balance']>$groupOrder['consumption_money']){ + $balance=['balance'=>bcsub($store_consumption_user['balance'],$groupOrder['consumption_money'],2)]; + Db::name('store_consumption_user')->where('coupon_user_id',$groupOrder['coupon_user_id'])->update(); + }else{ + $balance=['balance'=>0,'status'=>1]; + Db::name('store_consumption_user')->where('coupon_user_id',$groupOrder['coupon_user_id'])->update(); + } + + } + app()->make(UserBillRepository::class)->decBill($user['uid'], 'consumption', 'deduction', [ + 'link_id' => $groupOrder['group_order_id'], + 'status' => 1, + 'title' => '购买商品', + 'number' => $groupOrder['consumption_money'], + 'mark' => '购买商品使用红包抵扣' . floatval($groupOrder['consumption_money']) . '元', + 'balance' => $balance + ]); + } + foreach ($orderList as $k => $order) { $orderList[$k]['group_order_id'] = $groupOrder->group_order_id; } @@ -1015,6 +1005,22 @@ class StoreOrderCreateRepository extends StoreOrderRepository ]; } + if ($order['consumption_money'] > 0) { + $bills[] = [ + 'uid' => $uid, + 'link_id' => $_order->order_id, + 'pm' => 0, + 'title' => '红包抵扣', + 'category' => 'mer_integral', + 'type' => 'deduction', + 'number' => $order['consumption_money'], + 'balance' => 0, + 'mark' => '购买商品使用积分抵扣' .$order['consumption_money']. '元', + '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);