123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980 |
- <?php
- 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 extends BaseServices
- {
- use ServicesTrait;
-
- public function __construct(StoreOrderDao $dao)
- {
- $this->dao = $dao;
- }
-
- public function getNewOrderId(string $prefix = 'wx')
- {
- $snowflake = new \Godruoyi\Snowflake\Snowflake();
- $is_callable = function ($currentTime) {
- $redis = Cache::store('redis');
- $swooleSequenceResolver = new \Godruoyi\Snowflake\RedisSequenceResolver($redis->handler());
- return $swooleSequenceResolver->sequence($currentTime);
- };
-
- 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;
- }
-
- 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;
- }
-
- 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)
- {
-
- $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;
-
- $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;
- }
-
- 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') {
-
- $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();
-
- $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) {
-
- $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);
-
-
- $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]);
-
- $partakeService = app()->make(UserCollagePartakeServices::class);
- $partakeService->update(['collate_code_id' => $collate_code_id, 'is_settle' => 0], ['status' => 0]);
- }
-
-
- StorePromotionsJob::dispatchDo('changeGiveLimit', [$promotions_give]);
-
- event('order.create', [$order, $userInfo, compact('cartInfo', 'priceData', 'addressId', 'cartIds', 'news'), compact('type', 'activity_id'), $invoice_id]);
- return $order;
- }
-
- 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) {
-
- $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]);
-
- $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('使用积分抵扣失败!');
- }
- }
-
- public function decGoodsStock(array $cartInfo, int $type, int $activity_id, int $store_id = 0)
- {
- $res5 = true;
-
- $services = app()->make(StoreProductServices::class);
-
- $seckillServices = app()->make(StoreSeckillServices::class);
-
- $pinkServices = app()->make(StoreCombinationServices::class);
-
- $bargainServices = app()->make(StoreBargainServices::class);
-
- $discountServices = app()->make(StoreDiscountsServices::class);
-
- $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('库存不足!');
- }
- }
-
- public function orderCreateAfter($order, array $group)
- {
-
- $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)) {
-
- $cartServices = app()->make(StoreCartServices::class);
- $cartServices->deleteCartStatus($group['cartIds'] ?? []);
- }
- }
- }
-
- 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->computeOrderProductIntegral($cartInfo, $priceData);
- $cartInfo = $this->computeOrderProductFirstDiscount($cartInfo, $priceData);
- } catch (\Throwable $e) {
- Log::error('订单商品结算失败,File:' . $e->getFile() . ',Line:' . $e->getLine() . ',Message:' . $e->getMessage());
- throw new ValidateException('订单商品结算失败');
- }
-
-
- 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;
- }
- }
-
- [$cartInfo, $spread_ids] = $this->computeOrderProductBrokerage($uid, $cartInfo, $userInfo);
- return [$cartInfo, $spread_ids];
- }
-
- public function computeOrderProductPostage(array $cartInfo, array $priceData, $addressId)
- {
- $storePostage = $priceData['pay_postage'] ?? 0;
- if ($storePostage) {
-
- $addressServices = app()->make(UserAddressServices::class);
- $addr = $addressServices->getAdderssCache($addressId);
- if ($addr) {
-
- $cityId = $addr['city_id'] ?? 0;
- $ids = [];
- if ($cityId) {
-
- $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);
-
- $shippServices = app()->make(ShippingTemplatesServices::class);
- $temp = $shippServices->getShippingColumn(['id' => $tempIds], 'type,appoint', 'id');
-
- $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);
-
- $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;
- }
-
- 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;
- }
-
- 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;
-
- $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:
-
- $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:
-
- $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;
- }
-
- 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);
-
- $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);
- }
-
- 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]];
- }
-
- 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;
- }
- }
|