123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980 |
- <?php
- // +----------------------------------------------------------------------
- // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
- // +----------------------------------------------------------------------
- // | Copyright (c) 2016~2020 https://www.crmeb.com All rights reserved.
- // +----------------------------------------------------------------------
- // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
- // +----------------------------------------------------------------------
- // | Author: CRMEB Team <admin@crmeb.com>
- // +----------------------------------------------------------------------
- namespace app\services\order;
- use app\jobs\activity\StorePromotionsJob;
- use app\services\activity\discounts\StoreDiscountsServices;
- use app\services\activity\newcomer\StoreNewcomerServices;
- use app\services\agent\AgentLevelServices;
- use app\services\activity\coupon\StoreCouponUserServices;
- use app\services\other\CityAreaServices;
- use app\services\pay\PayServices;
- use app\services\product\branch\StoreBranchProductServices;
- use app\services\product\brand\StoreBrandServices;
- use app\services\product\category\StoreProductCategoryServices;
- use app\services\product\shipping\ShippingTemplatesFreeServices;
- use app\services\product\shipping\ShippingTemplatesRegionServices;
- use app\services\product\shipping\ShippingTemplatesServices;
- use app\services\user\AwardIntegralServices;
- use app\services\wechat\WechatUserServices;
- use app\services\BaseServices;
- use crmeb\services\CacheService;
- use app\dao\order\StoreOrderDao;
- use app\services\user\UserServices;
- use crmeb\traits\ServicesTrait;
- use think\exception\ValidateException;
- use app\services\user\UserBillServices;
- use app\services\user\UserAddressServices;
- use app\services\activity\bargain\StoreBargainServices;
- use app\services\activity\seckill\StoreSeckillServices;
- use app\services\store\SystemStoreServices;
- use app\services\activity\combination\StoreCombinationServices;
- use app\services\product\product\StoreProductServices;
- use app\services\activity\collage\UserCollageServices;
- use app\services\activity\collage\UserCollagePartakeServices;
- use think\facade\Cache;
- use think\facade\Log;
- /**
- * 订单创建
- * Class StoreOrderCreateServices
- * @package app\services\order
- * @mixin StoreOrderDao
- */
- class StoreOrderCreateServices extends BaseServices
- {
- use ServicesTrait;
- /**
- * StoreOrderCreateServices constructor.
- * @param StoreOrderDao $dao
- */
- public function __construct(StoreOrderDao $dao)
- {
- $this->dao = $dao;
- }
- /**
- * 使用雪花算法生成订单ID
- * @return string
- * @throws \Exception
- */
- public function getNewOrderId(string $prefix = 'wx')
- {
- $snowflake = new \Godruoyi\Snowflake\Snowflake();
- $is_callable = function ($currentTime) {
- // if (is_win()) {
- $redis = Cache::store('redis');
- $swooleSequenceResolver = new \Godruoyi\Snowflake\RedisSequenceResolver($redis->handler());
- return $swooleSequenceResolver->sequence($currentTime);
- // } else {
- // $swooleSequenceResolver = new \Godruoyi\Snowflake\SwooleSequenceResolver();
- // return $swooleSequenceResolver->sequence($currentTime);
- // }
- };
- //32位
- if (PHP_INT_SIZE == 4) {
- $id = abs($snowflake->setSequenceResolver($is_callable)->id());
- } else {
- $id = $snowflake->setStartTimeStamp(strtotime('2020-06-05') * 1000)->setSequenceResolver($is_callable)->id();
- }
- return $prefix . $id;
- }
- /**
- * 核销订单生成核销码
- * @return false|string
- */
- public function getStoreCode()
- {
- mt_srand();
- [$msec, $sec] = explode(' ', microtime());
- $num = time() + mt_rand(10, 999999) . '' . substr($msec, 2, 3);//生成随机数
- if (strlen($num) < 12)
- $num = str_pad((string)$num, 12, 0, STR_PAD_RIGHT);
- else
- $num = substr($num, 0, 12);
- if ($this->dao->count(['verify_code' => $num])) {
- return $this->getStoreCode();
- }
- return $num;
- }
- /**
- * 创建订单
- * @param int $uid
- * @param string $key
- * @param array $cartGroup
- * @param int $addressId
- * @param string $payType
- * @param array $addressInfo
- * @param array $userInfo
- * @param bool $useIntegral
- * @param int $couponId
- * @param string $mark
- * @param int $pinkId
- * @param int $isChannel
- * @param int $shippingType
- * @param int $storeId
- * @param false $news
- * @param array $customForm
- * @param int $invoice_id
- * @param string $from
- * @return mixed
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\DbException
- * @throws \think\db\exception\ModelNotFoundException
- */
- public function createOrder(int $uid, string $key, array $cartGroup, int $addressId, string $payType, array $addressInfo, array $userInfo = [], bool $useIntegral = false, $couponId = 0, $mark = '', $pinkId = 0, $isChannel = 0, $shippingType = 1, $storeId = 0, $news = false, $customForm = [], int $invoice_id = 0, string $from = '', int $collate_code_id = 0)
- {
- /** @var StoreOrderComputedServices $computedServices */
- $computedServices = app()->make(StoreOrderComputedServices::class);
- $priceData = $computedServices->computedOrder($uid, $userInfo, $cartGroup, $addressId, $payType, $useIntegral, $couponId, $shippingType);
- $cartInfo = $cartGroup['cartInfo'];
- $priceGroup = $cartGroup['priceGroup'];
- if ($priceGroup['awardPrice'] > 0 && $useIntegral) throw new ValidateException('增值商品不能使用消费分抵扣!');
- $cartIds = [];
- $totalNum = 0;
- $gainIntegral = 0;
- foreach ($cartInfo as $cart) {
- $cartIds[] = $cart['id'];
- $totalNum += $cart['cart_num'];
- $cartInfoGainIntegral = isset($cart['productInfo']['give_integral']) ? bcmul((string)$cart['cart_num'], (string)$cart['productInfo']['give_integral'], 0) : 0;
- $gainIntegral = bcadd((string)$gainIntegral, (string)$cartInfoGainIntegral, 0);
- }
- $deduction = $cartGroup['deduction'];
- $other = $cartGroup['other'];
- $promotions_give = [
- 'give_integral' => $other['give_integral'] ?? 0,
- 'give_coupon' => $other['give_coupon'] ?? [],
- 'give_product' => $other['give_product'] ?? [],
- 'promotions' => $other['promotions'] ?? []
- ];
- $type = (int)$deduction['type'] ?? 0;
- $activity_id = (int)$deduction['activity_id'] ?? 0;
- $collateCodeId = (int)$deduction['collate_code_id'] ?? 0;
- $product_type = (int)$deduction['product_type'] ?? 0;
- /** @var UserCollageServices $collageServices */
- $collageServices = app()->make(UserCollageServices::class);
- if (in_array($type, [1, 2, 3, 5])) {
- $couponId = 0;
- if ($type != 5) $useIntegral = false;
- $systemPayType = PayServices::PAY_TYPE;
- unset($systemPayType['offline']);
- if ($from != 'pc' && !array_key_exists($payType, $systemPayType)) {
- throw new ValidateException('营销商品不能使用线下支付!');
- }
- } else if ($type == 8) {
- $gainIntegral = 0;
- } else if ($type == 9 || $type == 10) {
- if ($collateCodeId != $collate_code_id) throw new ValidateException('拼单/桌码ID有误!');
- $status = $collageServices->value(['id' => $collate_code_id], 'status');
- if ($status >= 2) throw new ValidateException($type == 10 ? '桌码' : '拼单' . '已生成订单!');
- $activity_id = $collate_code_id;
- }
- //$shipping_type = 1 快递发货 $shipping_type = 2 门店自提
- if (!sys_config('store_func_status', 1) || !sys_config('store_self_mention', 1)) $shippingType = 1;
- $userAddress = $addressInfo['province'] . ' ' . $addressInfo['city'] . ' ' . $addressInfo['district'] . ' ' . $addressInfo['street'] . ' ' . $addressInfo['detail'];
- $userLocation = $addressInfo['longitude'] . ' ' . $addressInfo['latitude'];
- $orderInfo = [
- 'uid' => $uid,
- 'type' => $type,
- 'order_id' => $this->getNewOrderId(),
- 'real_name' => $addressInfo['real_name'],
- 'user_phone' => $addressInfo['phone'],
- 'user_address' => $userAddress,
- 'order_province' => $addressInfo['province'],
- 'order_city' => $addressInfo['city'],
- 'order_district' => $addressInfo['district'],
- 'send_area_award' => 0,
- 'user_location' => $userLocation,
- 'cart_id' => $cartIds,
- 'total_num' => $totalNum,
- 'total_price' => $priceGroup['sumPrice'] ?? $priceGroup['totalPrice'],
- 'total_postage' => $priceData['total_postage'] ?? $priceGroup['storePostage'],
- 'coupon_id' => $couponId,
- 'coupon_price' => $priceData['coupon_price'],
- 'first_order_price' => $priceData['first_order_price'],
- 'promotions_price' => $priceData['promotions_price'],
- 'pay_price' => $priceData['pay_price'],
- 'pay_postage' => $priceData['pay_postage'],
- 'deduction_price' => $priceData['deduction_price'],
- 'paid' => 0,
- 'pay_type' => $payType,
- 'use_integral' => $priceData['usedIntegral'],
- 'gain_integral' => $gainIntegral,
- 'mark' => htmlspecialchars($mark),
- 'product_type' => $product_type,
- 'activity_id' => $activity_id,
- 'pink_id' => $pinkId,
- 'cost' => $priceGroup['costPrice'],
- 'award_price' => $priceGroup['awardPrice'],
- 'is_channel' => $isChannel,
- 'add_time' => time(),
- 'unique' => $key,
- 'shipping_type' => $shippingType,
- 'channel_type' => $userInfo['user_type'],
- 'province' => '',
- 'spread_uid' => 0,
- 'spread_two_uid' => 0,
- 'custom_form' => json_encode($customForm),
- 'promotions_give' => json_encode($promotions_give),
- 'give_integral' => $promotions_give['give_integral'] ?? 0,
- 'give_coupon' => implode(',', $promotions_give['give_coupon'] ?? []),
- 'store_id' => $storeId
- ];
- if ($orderInfo['award_price'] > sys_config('max_price', 0) && sys_config('max_price', 0) > 0 && $orderInfo['award_price'] > 0) {
- throw new ValidateException('下单金额不超过' . sys_config('max_price'));
- }
- if ($orderInfo['award_price'] < sys_config('min_price', 0) && sys_config('min_price', 0) > 0 && $orderInfo['award_price'] > 0) {
- throw new ValidateException('下单金额不低于' . sys_config('min_price'));
- }
- if ($orderInfo['award_price'] > 0) {
- $awardService = app()->make(AwardIntegralServices::class);
- $main_orders = $awardService->search(['uid' => $uid, 'link_id' => 0, 'order_price_elt' => $orderInfo['award_price'], 'valid' => 2])->order('id asc')->column('id');//满足复投条件的主单
- $link_id = 0;
- foreach ($main_orders as $v) {
- $can_reorder = $this->dao->search(['uid' => $uid, 'valid' => 1, 'link_id' => $v])->find();
- if (!$can_reorder) {
- $link_id = $v;
- break;
- }
- }
- if ($link_id > 0) {
- if (!$userInfo['reorder']) throw new ValidateException('暂不支持复投');
- }
- $order_count = $awardService->search(['uid' => $uid])->count();
- if ($userInfo['max_order_count'] > 0 && $order_count >= $userInfo['max_order_count']) {
- throw new ValidateException('报单数量已达上限');
- }
- }
- $orderPre = $this->dao->search(['uid' => $uid, 'is_del' => 0, 'is_system_del' => 0])->order('add_time', 'desc')->find();
- if ($orderPre && $orderPre['add_time'] > (time() - (sys_config('order_time', 0) * 60))) {
- throw new ValidateException('请等待' . ((sys_config('order_time', 0) * 60) - (time() - $orderPre['add_time'])) . '秒后再下单');
- }
- if ($userInfo['user_type'] == 'wechat' || $userInfo['user_type'] == 'routine') {
- /** @var WechatUserServices $wechatServices */
- $wechatServices = app()->make(WechatUserServices::class);
- $orderInfo['province'] = $wechatServices->value(['uid' => $uid, 'user_type' => $userInfo['user_type']], 'province') ?: '';
- }
- if ($shippingType == 2) {
- $orderInfo['verify_code'] = $this->getStoreCode();
- /** @var SystemStoreServices $storeServices */
- $storeServices = app()->make(SystemStoreServices::class);
- $orderInfo['store_id'] = $storeServices->getStoreDisposeCache($storeId, 'id');
- if (!$orderInfo['store_id']) {
- throw new ValidateException('暂无门店无法选择门店自提');
- }
- }
- $priceData['coupon_id'] = $couponId;
- $order = $this->transaction(function () use ($cartIds, $couponId, $orderInfo, $cartInfo, $key, $userInfo, $useIntegral, $priceData, $type, $activity_id, $uid, $addressId, $promotions_give, $storeId) {
- //创建订单
- $order = $this->dao->save($orderInfo);
- if ($couponId) {
- /** @var StoreCouponUserServices $couponServices */
- $couponServices = app()->make(StoreCouponUserServices::class);
- $couponServices->useCoupon($couponId, (int)$userInfo['uid'], $cartInfo, [], $storeId);
- }
- //抵扣积分
- $this->deductIntegral($userInfo, $useIntegral, $priceData, (int)$userInfo['uid'], $key);
- //扣库存
- $this->decGoodsStock($cartInfo, $type, $activity_id, $orderInfo['store_id'] ?? 0);
- //保存购物车商品信息
- /** @var StoreOrderCartInfoServices $cartServices */
- $cartServices = app()->make(StoreOrderCartInfoServices::class);
- $cartServices->setCartInfo($order['id'], $cartInfo, (int)$uid, $promotions_give['promotions'] ?? []);
- return $order;
- });
- if (in_array($type, [9, 10]) && $collate_code_id > 0 && $order) {
- //关联订单和拼单、桌码
- $collageServices->update($collate_code_id, ['oid' => $order['id'], 'status' => 2]);
- /** @var UserCollagePartakeServices $partakeService */
- $partakeService = app()->make(UserCollagePartakeServices::class);
- $partakeService->update(['collate_code_id' => $collate_code_id, 'is_settle' => 0], ['status' => 0]);
- }
- //保存购物车商品信息
- // StoreCartJob::dispatch([$order['id'], $cartInfo, $uid, $promotions_give['promotions'] ?? []]);
- //扣除优惠活动赠品限量
- StorePromotionsJob::dispatchDo('changeGiveLimit', [$promotions_give]);
- //订单创建事件
- event('order.create', [$order, $userInfo, compact('cartInfo', 'priceData', 'addressId', 'cartIds', 'news'), compact('type', 'activity_id'), $invoice_id]);
- return $order;
- }
- /**
- * 抵扣积分
- * @param array $userInfo
- * @param bool $useIntegral
- * @param array $priceData
- * @param int $uid
- * @param string $key
- */
- public function deductIntegral(array $userInfo, bool $useIntegral, array $priceData, int $uid, string $key)
- {
- $res2 = true;
- if (sys_config('integral_ratio_status', 1) && $userInfo && $useIntegral && $userInfo['integral'] > 0) {
- /** @var UserServices $userServices */
- $userServices = app()->make(UserServices::class);
- if (!$priceData['SurplusIntegral']) {
- $integral = 0;
- } else {
- $integral = bcsub((string)$userInfo['integral'], (string)$priceData['usedIntegral']);
- }
- $res2 = false !== $userServices->update($uid, ['integral' => $integral]);
- /** @var UserBillServices $userBillServices */
- $userBillServices = app()->make(UserBillServices::class);
- $res3 = $userBillServices->income('deduction', $uid, [
- 'number' => (int)$priceData['usedIntegral'],
- 'deductionPrice' => $priceData['deduction_price']
- ], $integral, $key);
- $res2 = $res2 && false != $res3;
- }
- if (!$res2) {
- throw new ValidateException('使用积分抵扣失败!');
- }
- }
- /**
- * 扣库存
- * @param array $cartInfo
- * @param int $type
- * @param int $activity_id
- * @param int $store_id
- */
- public function decGoodsStock(array $cartInfo, int $type, int $activity_id, int $store_id = 0)
- {
- $res5 = true;
- /** @var StoreProductServices $services */
- $services = app()->make(StoreProductServices::class);
- /** @var StoreSeckillServices $seckillServices */
- $seckillServices = app()->make(StoreSeckillServices::class);
- /** @var StoreCombinationServices $pinkServices */
- $pinkServices = app()->make(StoreCombinationServices::class);
- /** @var StoreBargainServices $bargainServices */
- $bargainServices = app()->make(StoreBargainServices::class);
- /** @var StoreDiscountsServices $discountServices */
- $discountServices = app()->make(StoreDiscountsServices::class);
- /** @var StoreNewcomerServices $storeNewcomerServices */
- $storeNewcomerServices = app()->make(StoreNewcomerServices::class);
- try {
- foreach ($cartInfo as $cart) {
- $unique = isset($cart['productInfo']['attrInfo']) ? $cart['productInfo']['attrInfo']['unique'] : '';
- $cart_num = (int)$cart['cart_num'];
- //减库存加销量
- switch ($type) {
- case 0://普通
- case 6://预售
- case 8://抽奖
- case 9://拼单
- case 10://桌码
- $res5 = $res5 && $services->decProductStock($cart_num, (int)$cart['productInfo']['id'], $unique, $store_id);
- break;
- case 1://秒杀
- $res5 = $res5 && $seckillServices->decSeckillStock($cart_num, $activity_id, $unique, $store_id);
- break;
- case 2://砍价
- $res5 = $res5 && $bargainServices->decBargainStock($cart_num, $activity_id, $unique, $store_id);
- break;
- case 3://拼团
- $res5 = $res5 && $pinkServices->decCombinationStock($cart_num, $activity_id, $unique, $store_id);
- break;
- case 5://套餐
- $res5 = $res5 && $discountServices->decDiscountStock($cart_num, $activity_id, (int)($cart['discount_product_id'] ?? 0), (int)($cart['product_id'] ?? 0), $unique, $store_id);
- break;
- case 7://新人专享
- $res5 = $res5 && $storeNewcomerServices->decNewcomerStock($cart_num, $activity_id, $unique, $store_id);
- break;
- default:
- $res5 = $res5 && $services->decProductStock($cart_num, (int)$cart['productInfo']['id'], $unique, $store_id);
- break;
- }
- }
- if ($type == 5 && $activity_id) {
- //改变套餐限量
- $res5 = $res5 && $discountServices->changeDiscountLimit($activity_id);
- }
- if (!$res5) {
- throw new ValidateException('库存不足!');
- }
- } catch (\Throwable $e) {
- throw new ValidateException('库存不足!');
- }
- }
- /**
- * 订单创建后的后置事件
- * @param UserAddressServices $addressServices
- * @param $order
- * @param array $group
- */
- public function orderCreateAfter($order, array $group)
- {
- /** @var UserAddressServices $addressServices */
- $addressServices = app()->make(UserAddressServices::class);
- //设置用户默认地址
- if ($order['uid'] && isset($group['addressId']) && $group['addressId'] && !$addressServices->be(['is_default' => 1, 'uid' => $order['uid']])) {
- $addressServices->setDefaultAddress($group['addressId'], $order['uid']);
- }
- //删除购物车
- if (isset($group['news']) && $group['news']) {
- array_map(function ($key) {
- CacheService::redisHandler()->delete($key);
- }, $group['cartIds']);
- } else {
- if (!isset($group['delCart']) || (isset($group['delCart']) && $group['delCart'] !== false)) {
- /** @var StoreCartServices $cartServices */
- $cartServices = app()->make(StoreCartServices::class);
- $cartServices->deleteCartStatus($group['cartIds'] ?? []);
- }
- }
- }
- /**
- * 计算订单每个商品真实付款价格
- * @param array $orderInfo
- * @param array $cartInfo
- * @param array $priceData
- * @param $addressId
- * @param int $uid
- * @param $userInfo
- * @return array
- */
- public function computeOrderProductTruePrice($orderInfo, array $cartInfo, array $priceData, $addressId, int $uid, $userInfo)
- {
- //统一放入默认数据
- foreach ($cartInfo as &$cart) {
- $cart['use_integral'] = 0;
- $cart['integral_price'] = 0.00;
- if (!isset($cart['coupon_price'])) {
- $cart['coupon_price'] = 0.00;
- }
- $cart['first_order_price'] = 0.00;
- $cart['one_brokerage'] = 0.00;
- $cart['two_brokerage'] = 0.00;
- }
- try {
- $promotionsGice = isset($orderInfo['promotions_give']) ? (is_string($orderInfo['promotions_give']) ? json_decode($orderInfo['promotions_give'], true) : $orderInfo['promotions_give']) : [];
- $promotions = [];
- if (isset($promotionsGice['promotions']) && $promotionsGice['promotions']) {
- $promotions = $promotionsGice['promotions'];
- }
- //$cartInfo = $this->computeOrderProductCoupon($cartInfo, $priceData, $promotions);
- $cartInfo = $this->computeOrderProductIntegral($cartInfo, $priceData);
- // $cartInfo = $this->computeOrderProductPostage($cartInfo, $priceData);
- $cartInfo = $this->computeOrderProductFirstDiscount($cartInfo, $priceData);
- } catch (\Throwable $e) {
- Log::error('订单商品结算失败,File:' . $e->getFile() . ',Line:' . $e->getLine() . ',Message:' . $e->getMessage());
- throw new ValidateException('订单商品结算失败');
- }
- //truePice实际支付单价(存在)
- //几件商品总体优惠 以及积分抵扣金额
- foreach ($cartInfo as &$cart) {
- $coupon_price = $cart['coupon_price'] ?? 0;
- $integral_price = $cart['integral_price'] ?? 0;
- $first_order_price = $cart['first_order_price'] ?? 0;
- $cart['sum_true_price'] = bcmul((string)$cart['truePrice'], (string)$cart['cart_num'], 2);
- if ($coupon_price) {
- $cart['sum_true_price'] = bcsub((string)$cart['sum_true_price'], (string)$coupon_price, 2);
- $uni_coupon_price = (string)bcdiv((string)$coupon_price, (string)$cart['cart_num'], 4);
- $cart['truePrice'] = $cart['truePrice'] > $uni_coupon_price ? bcsub((string)$cart['truePrice'], $uni_coupon_price, 2) : 0;
- }
- if ($integral_price) {
- $cart['sum_true_price'] = bcsub((string)$cart['sum_true_price'], (string)$integral_price, 2);
- $uni_integral_price = (string)bcdiv((string)$integral_price, (string)$cart['cart_num'], 4);
- $cart['truePrice'] = $cart['truePrice'] > $uni_integral_price ? bcsub((string)$cart['truePrice'], $uni_integral_price, 2) : 0;
- }
- if ($first_order_price) {
- $cart['sum_true_price'] = bcsub((string)$cart['sum_true_price'], (string)$first_order_price, 2);
- $uni_first_order_price = (string)bcdiv((string)$first_order_price, (string)$cart['cart_num'], 4);
- $cart['truePrice'] = $cart['truePrice'] > $uni_first_order_price ? bcsub((string)$cart['truePrice'], $uni_first_order_price, 2) : 0;
- }
- }
- //计算佣金:1售价2实际支付金额
- [$cartInfo, $spread_ids] = $this->computeOrderProductBrokerage($uid, $cartInfo, $userInfo);
- return [$cartInfo, $spread_ids];
- }
- /**
- * 计算每个商品实际支付运费
- * @param array $cartInfo
- * @param array $priceData
- * @return array
- */
- public function computeOrderProductPostage(array $cartInfo, array $priceData, $addressId)
- {
- $storePostage = $priceData['pay_postage'] ?? 0;
- if ($storePostage) {
- /** @var UserAddressServices $addressServices */
- $addressServices = app()->make(UserAddressServices::class);
- $addr = $addressServices->getAdderssCache($addressId);
- if ($addr) {
- //按照运费模板计算每个运费模板下商品的件数/重量/体积以及总金额 按照首重倒序排列
- $cityId = $addr['city_id'] ?? 0;
- $ids = [];
- if ($cityId) {
- /** @var CityAreaServices $cityAreaServices */
- $cityAreaServices = app()->make(CityAreaServices::class);
- $ids = $cityAreaServices->getRelationCityIds($cityId);
- }
- $cityIds = array_merge([0], $ids);
- $tempIds[] = 1;
- foreach ($cartInfo as $key_c => $item_c) {
- $tempIds[] = $item_c['productInfo']['temp_id'];
- }
- $tempIds = array_unique($tempIds);
- /** @var ShippingTemplatesServices $shippServices */
- $shippServices = app()->make(ShippingTemplatesServices::class);
- $temp = $shippServices->getShippingColumn(['id' => $tempIds], 'type,appoint', 'id');
- /** @var ShippingTemplatesRegionServices $regionServices */
- $regionServices = app()->make(ShippingTemplatesRegionServices::class);
- $regions = $regionServices->getTempRegionList($tempIds, $cityIds, 'temp_id,first,first_price,continue,continue_price', 'temp_id');
- $temp_num = [];
- foreach ($cartInfo as $cart) {
- $tempId = $cart['productInfo']['temp_id'] ?? 1;
- $type = isset($temp[$tempId]['type']) ? $temp[$tempId]['type'] : $temp[1]['type'];
- if ($type == 1) {
- $num = $cart['cart_num'];
- } elseif ($type == 2) {
- $num = $cart['cart_num'] * $cart['productInfo']['attrInfo']['weight'];
- } else {
- $num = $cart['cart_num'] * $cart['productInfo']['attrInfo']['volume'];
- }
- $region = isset($regions[$tempId]) ? $regions[$tempId] : $regions[1];
- if (!isset($temp_num[$tempId])) {
- $temp_num[$tempId] = [
- 'cart_id' => [$cart['id']],
- 'number' => $num,
- 'type' => $type,
- 'price' => bcmul($cart['cart_num'], $cart['truePrice'], 2),
- 'first' => $region['first'],
- 'first_price' => $region['first_price'],
- 'continue' => $region['continue'],
- 'continue_price' => $region['continue_price'],
- 'temp_id' => $tempId
- ];
- } else {
- $temp_num[$tempId]['cart_id'][] = $cart['id'];
- $temp_num[$tempId]['number'] += $num;
- $temp_num[$tempId]['price'] += bcmul($cart['cart_num'], $cart['truePrice'], 2);
- }
- }
- $cartInfo = array_combine(array_column($cartInfo, 'id'), $cartInfo);
- /** @var ShippingTemplatesFreeServices $freeServices */
- $freeServices = app()->make(ShippingTemplatesFreeServices::class);
- $freeList = $freeServices->isFreeList($tempIds, $cityIds, 0, 'temp_id,number,price', 'temp_id');
- if ($freeList) {
- foreach ($temp_num as $k => $v) {
- if (isset($temp[$v['temp_id']]['appoint']) && $temp[$v['temp_id']]['appoint'] && isset($freeList[$v['temp_id']])) {
- $free = $freeList[$v['temp_id']];
- $condition = $free['number'] <= $v['number'];
- if ($free['price'] <= $v['price'] && $condition) {
- //免运费
- foreach ($v['cart_id'] as $c_id) {
- if (isset($cartInfo[$c_id])) $cartInfo[$c_id]['postage_price'] = 0.00;
- }
- }
- }
- }
- }
- $count = 0;
- $compute_price = 0.00;
- $total_price = 0;
- $postage_price = 0.00;
- foreach ($cartInfo as $cart) {
- if (isset($cart['postage_price'])) {//免运费
- continue;
- }
- if (isset($cart['is_gift']) && $cart['is_gift'] == 1) {
- continue;
- }
- $total_price = bcadd((string)$total_price, (string)bcmul((string)$cart['truePrice'], (string)$cart['cart_num'], 4), 2);
- $count++;
- }
- foreach ($cartInfo as &$cart) {
- if (isset($cart['postage_price'])) {//免运费
- continue;
- }
- if (isset($cart['is_gift']) && $cart['is_gift'] == 1) {
- continue;
- }
- if ($count > 1) {
- $postage_price = bcmul((string)bcdiv((string)bcmul((string)$cart['cart_num'], (string)$cart['truePrice'], 4), (string)$total_price, 4), (string)$storePostage, 2);
- $compute_price = bcadd((string)$compute_price, (string)$postage_price, 2);
- } else {
- $postage_price = bcsub((string)$storePostage, $compute_price, 2);
- }
- $cart['postage_price'] = $postage_price;
- $count--;
- }
- $cartInfo = array_merge($cartInfo);
- }
- }
- return $cartInfo;
- }
- /**
- * 计算订单商品积分实际抵扣金额
- * @param array $cartInfo
- * @param array $priceData
- * @return array
- */
- public function computeOrderProductIntegral(array $cartInfo, array $priceData)
- {
- $usedIntegral = $priceData['usedIntegral'] ?? 0;
- $deduction_price = $priceData['deduction_price'] ?? 0;
- if ($deduction_price) {
- $count = 0;
- $total_price = 0.00;
- $compute_price = 0.00;
- $integral_price = 0.00;
- $use_integral = 0;
- $compute_integral = 0;
- foreach ($cartInfo as $cart) {
- if (isset($cart['is_gift']) && $cart['is_gift'] == 1) {
- continue;
- }
- $total_price = bcadd((string)$total_price, (string)bcmul((string)$cart['truePrice'], (string)$cart['cart_num'], 4), 2);
- $count++;
- }
- if ($total_price == $deduction_price) {
- $ratio = 1;
- } else {
- $ratio = bcdiv((string)$deduction_price, (string)$total_price, 4);
- }
- foreach ($cartInfo as &$cart) {
- if (isset($cart['is_gift']) && $cart['is_gift'] == 1) {
- continue;
- }
- if ($count > 1) {
- $integral_price = bcmul((string)bcmul((string)$cart['cart_num'], (string)$cart['truePrice'], 4), (string)$ratio, 2);
- $compute_price = bcadd((string)$compute_price, (string)$integral_price, 2);
- $use_integral = bcmul((string)bcdiv((string)bcmul((string)$cart['cart_num'], (string)$cart['truePrice'], 4), (string)$total_price, 4), (string)$usedIntegral, 0);
- $compute_integral = bcadd((string)$compute_integral, $use_integral, 0);
- } else {
- $integral_price = bcsub((string)$deduction_price, $compute_price, 2);
- $use_integral = bcsub((string)$usedIntegral, $compute_integral, 0);
- }
- $count--;
- $cart['integral_price'] = $integral_price;
- $cart['use_integral'] = $use_integral;
- }
- }
- return $cartInfo;
- }
- /**
- * 计算订单商品优惠券实际抵扣金额
- * @param array $cartInfo
- * @param array $priceData
- * @return array
- */
- public function computeOrderProductCoupon(array $cartInfo, array $priceData, array $promotions = [], int $store_id = 0)
- {
- if ($priceData['coupon_id'] && $priceData['coupon_price'] ?? 0) {
- $count = 0;
- $total_price = 0.00;
- $compute_price = 0.00;
- $coupon_price = 0.00;
- /** @var StoreCouponUserServices $couponServices */
- $couponServices = app()->make(StoreCouponUserServices::class);
- $couponInfo = $couponServices->getOne(['id' => $priceData['coupon_id']], '*', ['issue']);
- if ($couponInfo) {
- //验证是否适用门店
- if ($store_id && isset($couponInfo['applicable_type']) && isset($couponInfo['applicable_store_id'])) {
- $applicable_store_id = is_array($couponInfo['applicable_store_id']) ? $couponInfo['applicable_store_id'] : explode(',', $couponInfo['applicable_store_id']);
- //活动不适用该门店
- if ($couponInfo['applicable_type'] == 0 || ($couponInfo['applicable_type'] == 2 && !in_array($store_id, $applicable_store_id))) {
- return [[], 0];
- }
- }
- $promotionsList = [];
- if ($promotions) {
- $promotionsList = array_combine(array_column($promotions, 'id'), $promotions);
- }
- $isOverlay = function ($cart) use ($promotionsList) {
- $productInfo = $cart['productInfo'] ?? [];
- if (!$productInfo) {
- return false;
- }
- //门店独立商品 不使用优惠券
- $isBranchProduct = isset($productInfo['type']) && isset($productInfo['pid']) && $productInfo['type'] == 1 && !$productInfo['pid'];
- if ($isBranchProduct) {
- return false;
- }
- if (isset($cart['promotions_id']) && $cart['promotions_id']) {
- foreach ($cart['promotions_id'] as $key => $promotions_id) {
- $promotions = $promotionsList[$promotions_id] ?? [];
- if ($promotions && $promotions['promotions_type'] != 4) {
- $overlay = is_string($promotions['overlay']) ? explode(',', $promotions['overlay']) : $promotions['overlay'];
- if (!in_array(5, $overlay)) {
- return false;
- }
- }
- }
- }
- return true;
- };
- $type = $couponInfo['applicable_type'] ?? 0;
- $counpon_id = $couponInfo['id'];
- switch ($type) {
- case 0:
- foreach ($cartInfo as $cart) {
- if (!$isOverlay($cart) || (isset($cart['is_gift']) && $cart['is_gift'] == 1)) continue;
- $total_price = bcadd((string)$total_price, (string)bcmul((string)$cart['truePrice'], (string)$cart['cart_num'], 4), 2);
- $count++;
- }
- foreach ($cartInfo as &$cart) {
- $cart['coupon_id'] = 0;
- $cart['coupon_price'] = 0;
- if (!$isOverlay($cart) || (isset($cart['is_gift']) && $cart['is_gift'] == 1)) continue;
- if ($count > 1) {
- $coupon_price = bcmul((string)bcdiv((string)bcmul((string)$cart['cart_num'], (string)$cart['truePrice'], 4), (string)$total_price, 4), (string)$priceData['coupon_price'], 2);
- $compute_price = bcadd((string)$compute_price, (string)$coupon_price, 2);
- } else {
- $coupon_price = bcsub((string)$priceData['coupon_price'], $compute_price, 2);
- }
- $cart['coupon_price'] = $coupon_price;
- $cart['coupon_id'] = $counpon_id;
- $count--;
- }
- break;
- case 1://品类券
- /** @var StoreProductCategoryServices $storeCategoryServices */
- $storeCategoryServices = app()->make(StoreProductCategoryServices::class);
- $cateGorys = $storeCategoryServices->getAllById((int)$couponInfo['category_id']);
- if ($cateGorys) {
- $cateIds = array_column($cateGorys, 'id');
- foreach ($cartInfo as $cart) {
- if (!$isOverlay($cart) || (isset($cart['is_gift']) && $cart['is_gift'] == 1)) continue;
- if (isset($cart['productInfo']['cate_id']) && array_intersect(explode(',', $cart['productInfo']['cate_id']), $cateIds)) {
- $total_price = bcadd((string)$total_price, (string)bcmul((string)$cart['truePrice'], (string)$cart['cart_num'], 4), 2);
- $count++;
- }
- }
- foreach ($cartInfo as &$cart) {
- $cart['coupon_id'] = 0;
- $cart['coupon_price'] = 0;
- if (!$isOverlay($cart) || (isset($cart['is_gift']) && $cart['is_gift'] == 1)) continue;
- if (isset($cart['productInfo']['cate_id']) && array_intersect(explode(',', $cart['productInfo']['cate_id']), $cateIds)) {
- if ($count > 1) {
- $coupon_price = bcmul((string)bcdiv((string)bcmul((string)$cart['cart_num'], (string)$cart['truePrice'], 4), (string)$total_price, 4), (string)$priceData['coupon_price'], 2);
- $compute_price = bcadd((string)$compute_price, (string)$coupon_price, 2);
- } else {
- $coupon_price = bcsub((string)$priceData['coupon_price'], $compute_price, 2);
- }
- $cart['coupon_id'] = $counpon_id;
- $cart['coupon_price'] = $coupon_price;
- $count--;
- }
- }
- }
- break;
- case 2://商品劵
- foreach ($cartInfo as $cart) {
- if (!$isOverlay($cart) || (isset($cart['is_gift']) && $cart['is_gift'] == 1)) continue;
- $product_id = isset($cart['productInfo']['pid']) && $cart['productInfo']['pid'] ? $cart['productInfo']['pid'] : ($cart['product_id'] ?? 0);
- if ($product_id && in_array($product_id, explode(',', $couponInfo['product_id']))) {
- $total_price = bcadd((string)$total_price, (string)bcmul((string)$cart['truePrice'], (string)$cart['cart_num'], 4), 2);
- $count++;
- }
- }
- foreach ($cartInfo as &$cart) {
- $cart['coupon_id'] = 0;
- $cart['coupon_price'] = 0;
- if (!$isOverlay($cart) || (isset($cart['is_gift']) && $cart['is_gift'] == 1)) continue;
- $product_id = isset($cart['productInfo']['pid']) && $cart['productInfo']['pid'] ? $cart['productInfo']['pid'] : ($cart['product_id'] ?? 0);
- if ($product_id && in_array($product_id, explode(',', $couponInfo['product_id']))) {
- if ($count > 1) {
- $coupon_price = bcmul((string)bcdiv((string)bcmul((string)$cart['cart_num'], (string)$cart['truePrice'], 4), (string)$total_price, 4), (string)$priceData['coupon_price'], 2);
- $compute_price = bcadd((string)$compute_price, (string)$coupon_price, 2);
- } else {
- $coupon_price = bcsub((string)$priceData['coupon_price'], $compute_price, 2);
- }
- $cart['coupon_id'] = $counpon_id;
- $cart['coupon_price'] = $coupon_price;
- $count--;
- }
- }
- break;
- case 3://品牌券
- /** @var StoreBrandServices $storeBrandServices */
- $storeBrandServices = app()->make(StoreBrandServices::class);
- $brands = $storeBrandServices->getAllById((int)$couponInfo['brand_id']);
- if ($brands) {
- $brandIds = array_column($brands, 'id');
- foreach ($cartInfo as $cart) {
- if (!$isOverlay($cart) || (isset($cart['is_gift']) && $cart['is_gift'] == 1)) continue;
- if (isset($cart['productInfo']['brand_id']) && in_array($cart['productInfo']['brand_id'], $brandIds)) {
- $total_price = bcadd((string)$total_price, (string)bcmul((string)$cart['truePrice'], (string)$cart['cart_num'], 4), 2);
- $count++;
- }
- }
- foreach ($cartInfo as &$cart) {
- $cart['coupon_id'] = 0;
- $cart['coupon_price'] = 0;
- if (!$isOverlay($cart) || (isset($cart['is_gift']) && $cart['is_gift'] == 1)) continue;
- if (isset($cart['productInfo']['brand_id']) && in_array($cart['productInfo']['brand_id'], $brandIds)) {
- if ($count > 1) {
- $coupon_price = bcmul((string)bcdiv((string)bcmul((string)$cart['cart_num'], (string)$cart['truePrice'], 4), (string)$total_price, 4), (string)$priceData['coupon_price'], 2);
- $compute_price = bcadd((string)$compute_price, (string)$coupon_price, 2);
- } else {
- $coupon_price = bcsub((string)$priceData['coupon_price'], $compute_price, 2);
- }
- $cart['coupon_id'] = $counpon_id;
- $cart['coupon_price'] = $coupon_price;
- $count--;
- }
- }
- }
- break;
- }
- }
- }
- return $cartInfo;
- }
- /**
- * 计算实际佣金
- * @param int $uid
- * @param array $cartInfo
- * @param $userInfo
- * @return array
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\DbException
- * @throws \think\db\exception\ModelNotFoundException
- */
- public function computeOrderProductBrokerage(int $uid, array $cartInfo, $userInfo)
- {
- //获取后台一级返佣比例
- $storeBrokerageRatio = sys_config('store_brokerage_ratio');
- //获取二级返佣比例
- $storeBrokerageTwo = sys_config('store_brokerage_two');
- //佣金计算方式
- $brokerageComputeType = sys_config('brokerage_compute_type', 1);
- /** @var AgentLevelServices $agentLevelServices */
- $agentLevelServices = app()->make(AgentLevelServices::class);
- [$one_brokerage_up, $two_brokerage_up, $spread_uid, $spread_two_uid] = $agentLevelServices->getAgentLevelBrokerage($uid, $userInfo);
- // 二级分销开关
- if (sys_config('brokerage_level', 2) == 1) {
- $storeBrokerageTwo = $spread_two_uid = 0;
- }
- foreach ($cartInfo as &$cart) {
- $oneBrokerage = '0';//一级返佣金额
- $twoBrokerage = '0';//二级返佣金额
- $cartNum = (string)$cart['cart_num'] ?? '0';
- if (isset($cart['productInfo']) && isset($cart['is_gift']) && $cart['is_gift'] == 0) {
- $productInfo = $cart['productInfo'];
- //指定返佣金额
- if (isset($productInfo['is_sub']) && $productInfo['is_sub'] == 1) {
- $oneBrokerage = bcmul((string)($productInfo['attrInfo']['brokerage'] ?? '0'), $cartNum, 2);
- $twoBrokerage = bcmul((string)($productInfo['attrInfo']['brokerage_two'] ?? '0'), $cartNum, 2);
- } else {//比例返佣
- $price = 0;
- switch ($brokerageComputeType) {
- case 1://售价
- if (isset($productInfo['attrInfo'])) {
- $price = bcmul((string)($productInfo['attrInfo']['price'] ?? '0'), $cartNum, 4);
- } else {
- $price = bcmul((string)($productInfo['price'] ?? '0'), $cartNum, 4);
- }
- break;
- case 2://实付金额
- $price = bcmul((string)($cart['truePrice'] ?? 0), $cartNum, 4);
- break;
- case 3://商品利润
- $price = bcmul(bcsub((string)($cart['truePrice'] ?? 0), (string)($cart['costPrice'] ?? 0), 2), $cartNum, 4);
- break;
- }
- if ($price > 0) {
- //一级返佣比例 小于等于零时直接返回 不返佣
- if ($storeBrokerageRatio > 0) {
- //计算获取一级返佣比例
- $brokerageRatio = bcdiv($storeBrokerageRatio, 100, 4);
- $oneBrokerage = bcmul((string)$price, (string)$brokerageRatio, 2);
- }
- //二级返佣比例小于等于0 直接返回
- if ($storeBrokerageTwo > 0) {
- //计算获取二级返佣比例
- $brokerageTwo = bcdiv($storeBrokerageTwo, 100, 4);
- $twoBrokerage = bcmul((string)$price, (string)$brokerageTwo, 2);
- }
- }
- }
- }
- //分销等级上浮佣金
- if ($one_brokerage_up) $oneBrokerage = bcadd((string)$oneBrokerage, (string)bcmul((string)$oneBrokerage, (string)bcdiv((string)$one_brokerage_up, '100', 2), 4), 2);
- if ($two_brokerage_up) $twoBrokerage = bcadd((string)$twoBrokerage, (string)bcmul((string)$twoBrokerage, (string)bcdiv((string)$two_brokerage_up, '100', 2), 4), 2);
- $cart['one_brokerage'] = $oneBrokerage;
- $cart['two_brokerage'] = $twoBrokerage;
- }
- return [$cartInfo, [$spread_uid, $spread_two_uid]];
- }
- /**
- * 计算订单商品信任首单优惠实际抵扣金额
- * @param array $cartInfo
- * @param array $priceData
- * @return array
- */
- public function computeOrderProductFirstDiscount(array $cartInfo, array $priceData)
- {
- $first_order_price = $priceData['first_order_price'] ?? 0;
- if ($first_order_price) {
- $count = 0;
- $total_price = 0.00;
- $compute_price = 0.00;
- $discount_price = 0.00;
- foreach ($cartInfo as $cart) {
- if (isset($cart['is_gift']) && $cart['is_gift'] == 1) {
- continue;
- }
- $total_price = bcadd((string)$total_price, (string)bcmul((string)$cart['truePrice'], (string)$cart['cart_num'], 4), 2);
- $count++;
- }
- if ($total_price == $first_order_price) {
- $ratio = 1;
- } else {
- $ratio = bcdiv((string)$first_order_price, (string)$total_price, 4);
- }
- foreach ($cartInfo as &$cart) {
- if (isset($cart['is_gift']) && $cart['is_gift'] == 1) {
- continue;
- }
- if ($count > 1) {
- $discount_price = bcmul((string)bcmul((string)$cart['cart_num'], (string)$cart['truePrice'], 4), (string)$ratio, 2);
- $compute_price = bcadd((string)$compute_price, (string)$discount_price, 2);
- } else {
- $discount_price = bcsub((string)$first_order_price, $compute_price, 2);
- }
- $count--;
- $cart['first_order_price'] = $discount_price;
- }
- }
- return $cartInfo;
- }
- }
|