12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367 |
- <?php
- namespace addons\shopro\service\order;
- use think\Db;
- use addons\shopro\service\goods\GoodsService;
- use addons\shopro\service\pay\PayOper;
- use addons\shopro\exception\ShoproException;
- use addons\shopro\facade\Activity as ActivityFacade;
- use app\admin\model\shopro\goods\Goods;
- use app\admin\model\shopro\order\Order;
- use app\admin\model\shopro\order\OrderItem;
- use app\admin\model\shopro\order\Address as OrderAddress;
- use app\admin\model\shopro\order\Invoice as OrderInvoice;
- use app\admin\model\shopro\dispatch\Dispatch;
- use app\admin\model\shopro\user\Coupon;
- use app\admin\model\shopro\user\Address as UserAddress;
- use app\admin\model\shopro\user\Invoice as UserInvoice;
- use addons\shopro\service\StockSale;
- class OrderCreate
- {
- protected $user = null;
- /**
- * 订单类型
- */
- protected $order_type = 'goods';
- /**
- * 是否需要收货地址,自动发货商品不需要选
- */
- protected $need_address = 1;
- /**
- * 用户选择的收货地址 id
- */
- protected $address_id = 0;
- /**
- * 选择的收货地址信息
- */
- protected $userAddress = null;
- /**
- * 货到付款的状态
- */
- protected $offline_status = 'none';
- /**
- * 当前参与的活动
- */
- protected $activity_id = 0;
- /**
- * 用户选择的开票信息
- */
- protected $invoice_id = 0;
- /**
- * 用户选择的优惠券 id
- */
- protected $coupon_id = 0;
- /**
- * 用户备注
- */
- protected $remark = 0;
- /**
- * 余额抵扣(余额和 微信|支付宝,混合支付时使用了)
- */
- protected $money = 0;
- /**
- * 发票配置
- */
- protected $invoiceConfig = [];
- /**
- * 类型,create:创建订单,calc:计算费用
- */
- protected $calc_type = 'create';
- protected $goodsList = [];
- protected $msg = null;
- protected $activity = [
- 'activity' => null, // 当前订单参与的活动
- 'promos' => [], // 当前订单可能参与的促销(每个商品所涉及的促销的集合)
- 'promo_infos' => [], // 当前订单实际参与的促销
- ];
-
-
- /**
- * 订单相关费用
- */
- protected $orderData = [
- 'goods_original_amount' => '0', // 商品原始总价
- 'goods_old_amount' => '0', // 商品不参与活动时的总价
- 'goods_amount' => '0', // 商品总价
- 'coupon_discount_fee' => '0', // 优惠券优惠金额
- 'promo_discount_fee' => 0, // 当前促销优惠总金额 (包含满包邮的邮费)
- 'total_discount_fee' => 0, // 当前订单,总优惠金额(优惠券 + 活动优惠)
- 'coupon' => null, // 所使用的优惠券
- 'coupon_goods_ids' => [],
- 'dispatch_amount' => '0', // 运费总价
- 'real_dispatch_amount' => '0', // 免邮减免之后的运费总价,不做计算使用
- 'score_amount' => 0, // 订单总积分
- 'free_shipping_goods_ids' => [], // 满额包邮商品 ids
- 'dispatch_infos' => [], // 当前订单商品按照配送方式分组
- 'order_amount' => 0, // 订单总费用
- 'pay_fee' => 0 // 应支付总金额 (减去优惠之后的)
- ];
- /**
- * 活动(拼团,秒杀) 和 促销(满减,满折),是否可以同时存在
- */
- protected $activity_promos = false;
- public function __construct($params)
- {
- $this->user = auth_user();
- $this->calcParams($params);
- }
- /**
- * 获取请求参数,初始化,并设置默认值
- *
- * @param array $params
- * @return array
- */
- public function calcParams($params)
- {
- $this->order_type = $params['order_type'] ?? 'goods';
- $this->activity_id = $params['activity_id'] ?? 0;
- // $groupon_id = $groupon_id ?? 0; // 拼团的 团 id
- // $buy_type = $buy_type ?? 'alone'; // 拼团的 购买方式: alone=单独购买,groupon=开团
-
- // $this->goodsList = $params['goods_list'] ?? [];
- $this->goodsList = $params['goods_list'] ? json_decode(htmlspecialchars_decode($params['goods_list']),true) : [];
-
- $this->address_id = $params['address_id'] ?? 0;
- $this->invoice_id = $params['invoice_id'] ?? 0;//发票
- $this->coupon_id = $params['coupon_id'] ?? 0;//单张优惠券,优惠券只能与活动叠加
- $this->remark = $params['remark'] ?? '';
- $this->money = (isset($params['money']) && $params['money'] > 0) ? $params['money'] : 0;
- // 获取商品信息
- $this->goodsListInit();
- }
- public function goodsListInit()
- {
- foreach ($this->goodsList as $key => &$buyInfo) {
- $goods_sku_price_id = $buyInfo['goods_sku_price_id'];
- if ($this->order_type == 'score') {
- $goods = $this->getGoodsService()->score()->show()->where('id', $buyInfo['goods_id'])->findOrFail();
- } else {
- // 暂时保存当前商品要获取的活动的 id
- session('goods-activity_id:' . $buyInfo['goods_id'], $this->activity_id); //设置商品活动id的session
- $goods = $this->getGoodsService()->activity($this->activity_id)->show()->where('id', $buyInfo['goods_id'])->findOrFail();
- if ($this->activity_id) {
- if (!$this->activity['activity'] = $goods->activity) {
- $this->exception('活动不存在', true);
- }
- if ($this->activity['activity']['status'] != 'ing') {
- $this->exception('活动' . $this->activity['activity']['status_text']);
- }
- }
- if ($this->isCalcPromos()&& isset($goods['promos']) && $goods['promos']) {
- $this->activity['promos'] = array_merge($this->activity['promos'], $goods['promos']);
- }
- }
- $skuPrices = $goods['new_sku_prices'];
- foreach ($skuPrices as $key => $skuPrice) {
- if ($skuPrice['id'] == $goods_sku_price_id && $skuPrice['status'] == 'up') {
- $buyInfo['current_sku_price'] = $skuPrice; // 当前购买规格
- break;
- }
- }
- if (!isset($buyInfo['current_sku_price']) || !$buyInfo['current_sku_price']) {
- $this->exception('商品规格不存在', true);
- }
- $buyInfo['dispatch_type'] = $goods['dispatch_type'] ?: 'express';
- $buyInfo['dispatch_type_text'] = $goods['dispatch_type_text'] ?? '';
- $buyInfo['goods'] = $goods;
- }
- }
- /**
- * 下单前检测,商品状态,秒杀,拼团活动状态,必要的选择项(比如下单收货地址),收货地址等
- *
- * @param array $params,请求参数
- * @return array
- */
- public function calcCheck()
- {
- $offline_num = 0; // 货到付款商品数量
- foreach ($this->goodsList as $key => &$buyInfo) {
- $goods = $buyInfo['goods'];
- $goods_sku_price_id = $buyInfo['goods_sku_price_id'];
- $goods_num = $buyInfo['goods_num'] ?? 1;
- // 最少购买一件
- $buyInfo['goods_num'] = intval($goods_num) < 1 ? 1 : intval($goods_num);
- if ($buyInfo['current_sku_price']['stock'] < $buyInfo['goods_num']) {
- $this->exception('商品 ' . $goods['title'] . ' 库存不足');
- }
- // 校验活动规则
- if ($this->activity['activity']) {
- try {
- $buyInfo = ActivityFacade::buyCheck($buyInfo, $this->activity['activity']);
- } catch (ShoproException $e) {
- $this->exception($e->getMessage());
- }
- }
- if ($buyInfo['goods']['is_offline']) {
- $offline_num ++;
- }
- $buyInfo['activity_type'] = $this->activity['activity']['type'] ?? null;
- }
- if (!count($this->goodsList)) {
- $this->exception('请选择要购买的商品');
- }
- if ($this->activity_id && count($this->goodsList) > 1) {
- $this->exception($this->activity['activity']['type_text'] . '必须单独购买');
- }
- if ($this->order_type == 'score' && count($this->goodsList) > 1) {
- $this->exception('积分商品必须单独购买');
- }
- // 判断是否支持货到付款
- if ($offline_num < count($this->goodsList)) {
- $this->offline_status = $offline_num == 0 ? 'none' : 'disabled';
- } else {
- $this->offline_status = 'enable';
- }
- // 判断是否有需要收货地址的商品,新版每个商品需要选择配送方式
- $dispatchTypes = array_column($this->goodsList, 'dispatch_type');
- // 配送方式为 快递 必须填写收货地址
- if (in_array('express', $dispatchTypes)) {
- // 用户收货地址
- if ($this->address_id) {
- $this->userAddress = UserAddress::where("user_id", $this->user->id)->find($this->address_id);
- }
- if (is_null($this->userAddress) && $this->calc_type == 'create') {
- $this->exception("请选择正确的收货地址");
- }
- } else {
- // 不需要收货地址
- $this->need_address = 0;
- }
- }
- /**
- * 计算订单各种费用
- *
- * @return array
- */
- public function calcAmount()
- {
- // 计算商品金额
- foreach ($this->goodsList as $key => &$buyInfo) {
- $goods = $buyInfo['goods'];
- // 当前商品原始总价
- $current_goods_original_amount = bcmul($goods->original_price, $buyInfo['goods_num'], 2);
- $this->orderData['goods_original_amount'] = bcadd($this->orderData['goods_original_amount'], $current_goods_original_amount, 2);
- // 当前商品现在总价
- $current_goods_amount = bcmul($buyInfo['current_sku_price']->price, $buyInfo['goods_num'], 2);
- $this->orderData['goods_amount'] = bcadd($this->orderData['goods_amount'], $current_goods_amount, 2);
- // 当有活动时,计算作为普通商品时的商品总金额
- $current_goods_old_amount = $current_goods_amount;
- if ($this->activity['activity']) {
- $current_goods_old_amount = bcmul($buyInfo['current_sku_price']->old_price, $buyInfo['goods_num'], 2);
- }
- $this->orderData['goods_old_amount'] = bcadd($this->orderData['goods_old_amount'], $current_goods_old_amount, 2);
- // 当前商品所需积分
- $current_score_amount = 0;
- if ($this->order_type == 'score') { // 积分商城规格
- $current_score_amount = $buyInfo['current_sku_price']->score * $buyInfo['goods_num'];
- $this->orderData['score_amount'] = $this->orderData['score_amount'] + $current_score_amount;
- }
- // 当前商品总重量
- $current_weight = bcmul($buyInfo['current_sku_price']->weight, $buyInfo['goods_num'], 2);
- // 将计算好的属性记录下来,插入订单 item 表使用
- $buyInfo['goods_original_amount'] = $current_goods_original_amount; // 当前商品原始总金额(原价 * 数量)
- $buyInfo['goods_amount'] = $current_goods_amount; // 当前商品总金额(价格 * 数量)
- $buyInfo['score_amount'] = $current_score_amount; // 商品所需积分(积分商城)
- $buyInfo['weight'] = $current_weight; // 当前商品总重量
- $buyInfo['original_dispatch_amount'] = 0; // 当前商品运费(未判断活动的,并且也未合并相同运费模板商品的原始运费)
- $buyInfo['dispatch_amount'] = 0; // 当前商品运费,按照运费模板合并相同商品的真实运费)
- $buyInfo['dispatch_id'] = 0; // 商品运费模板,如果有活动,并且活动包邮,这里是 0
- $buyInfo['promo_types'] = []; // 商品参与的营销类型
- $buyInfo['dispatch_discount_fee'] = 0; // 初始化每个商品运费优惠金额
- $buyInfo['promo_discount_fee'] = 0; // 初始化每个商品的促销优惠金额(这里不包含运费优惠,实际 order_item 上是包含的)
- $buyInfo['coupon_discount_fee'] = 0; // 初始化每个商品的优惠券优惠金额
- // 商品的配送模板 id
- $current_dispatch_id = $goods['dispatch_id'];
- // 获取快递配送数据
- if ($buyInfo['dispatch_type'] == 'express'
- && (!$this->activity['activity'] // 没有活动
- || (
- $this->activity['activity'] // 有活动,并且(没有是否包邮字段,或者是不包邮)
- && (!isset($this->activity['activity']['rules']['is_free_shipping']) || !$this->activity['activity']['rules']['is_free_shipping'])
- )
- )
- ) {
- if ($this->userAddress) {
- if (!isset($this->orderData['dispatch_infos'][$goods['dispatch_id']]['finalExpress'])) {
- $finalExpress = $this->getDispatchExpress($buyInfo['dispatch_type'], $goods['dispatch_id']);
- } else {
- $finalExpress = $this->orderData['dispatch_infos'][$goods['dispatch_id']]['finalExpress'];
- }
- $current_original_dispatch_amount = $this->calcDispatch($finalExpress, [
- 'goods_num' => $buyInfo['goods_num'],
- 'weight' => $current_weight
- ]);
- $this->orderData['dispatch_infos'][$goods['dispatch_id']]['finalExpress'] = $finalExpress;
- $this->orderData['dispatch_infos'][$goods['dispatch_id']]['buyInfos'][] = $buyInfo;
- }
-
- $buyInfo['original_dispatch_amount'] = $current_original_dispatch_amount ?? 0; // 没选收货地址时默认为 0
- $buyInfo['dispatch_id'] = $current_dispatch_id;
- } else if ($buyInfo['dispatch_type'] == 'autosend') {
- $this->getDispatchAutosend($buyInfo['dispatch_type'], $goods['dispatch_id']);
- $buyInfo['dispatch_id'] = $current_dispatch_id;
- }
- }
- // 计算应付总运费,商品中同一种运费模板聚合进行计算运费,然后再按照运费模板规则,将运费加权平分到每个商品
- foreach ($this->orderData['dispatch_infos'] as $key => &$info) {
- $finalExpress = $info['finalExpress'];
- $buy_num = 0;
- $weight = 0;
- foreach ($info['buyInfos'] as $k => $infoInfo) {
- $buy_num += $infoInfo['goods_num'];
- $weight = bcadd($weight, $infoInfo['weight'], 2);
- }
- // 聚合原始运费
- $current_dispatch_amount = $this->calcDispatch($finalExpress, [
- 'goods_num' => $buy_num,
- 'weight' => $weight
- ]);
- $info['current_dispatch_amount'] = $current_dispatch_amount; // 记录当前运费模板下商品的运费
- // 将运费加权平分到当前运费模板中的每个商品
- if ($current_dispatch_amount) {
- $this->equalDispatch($current_dispatch_amount, $info['buyInfos'], [
- 'goods_num' => $buy_num,
- 'weight' => $weight,
- 'final_express' => $finalExpress
- ]);
- }
- $this->orderData['dispatch_amount'] = bcadd($this->orderData['dispatch_amount'], $current_dispatch_amount, 2);
- }
- }
- /**
- * 计算商品的优惠促销
- *
- * @return void
- */
- public function calcDiscount() {
- // 过滤重复促销
- $promos = [];
- foreach ($this->activity['promos'] as $promo) {
- if (!isset($promos[$promo['id']])) {
- $promos[$promo['id']] = $promo;
- }
- }
- // 将购买的商品,按照促销分类
- foreach ($this->goodsList as $buyInfo) {
- $goods = $buyInfo['goods'];
- unset($buyInfo['goods']);
- if (isset($goods['promos']) && $goods['promos']) {
- foreach ($goods['promos'] as $promo) {
- $promos[$promo['id']]['goods'][] = $buyInfo;
- }
- }
- }
- // 计算各个促销是否满足
- foreach ($promos as $key => $promo) {
- if (!isset($promo['goods'])) {
- // 促销没有商品,直接 next
- continue;
- }
- $promoInfo = ActivityFacade::getPromoInfo($promo, [
- 'userAddress' => $this->userAddress
- ]);
- if ($promoInfo) {
- if (in_array($promo['type'], ['full_reduce', 'full_discount'])) {
- // 这里目前只计算,满减,满折
- $this->orderData['promo_discount_fee'] = bcadd($this->orderData['promo_discount_fee'], $promoInfo['promo_discount_money'], 2);
-
- // 将当前优惠平分到参与该促销的商品上
- foreach ($this->goodsList as &$buyInfo) {
- if (in_array($buyInfo['goods_id'], $promoInfo['goods_ids'])) {
- $scale = bcdiv($buyInfo['goods_amount'], $promoInfo['promo_goods_amount'], 6);
-
- // 当前商品,当前促销分配的优惠金额
- $current_promo_discount_fee = round(bcmul($promoInfo['promo_discount_money'], $scale, 3), 2);
- $buyInfo['promo_discount_fee'] = bcadd($buyInfo['promo_discount_fee'], $current_promo_discount_fee, 2); // 当前商品参与所有活动累计分配的优惠金额
- }
- }
- }
- if ($promo['type'] == 'free_shipping') {
- // 满包邮涉及到的商品
- $this->orderData['free_shipping_goods_ids'] = array_merge($this->orderData['free_shipping_goods_ids'], $promoInfo['goods_ids']);
- }
- $this->activity['promo_infos'][] = $promoInfo;
- }
- }
- // 计算实际应付总运费
- if ($this->orderData['free_shipping_goods_ids']) {
- // first:简单计算方式,谁包邮,就把 buyinfo 上的 dispatch_amount 计算为运费优惠
- $promo_dispatch_discount_fee = 0;
- foreach ($this->goodsList as $key => &$buyInfo) {
- if (in_array($buyInfo['goods_id'], $this->orderData['free_shipping_goods_ids'])) {
- // 商品运费优惠金额就是 当前商品的运费
- $buyInfo['dispatch_discount_fee'] = $buyInfo['dispatch_amount'];
- $promo_dispatch_discount_fee = bcadd($promo_dispatch_discount_fee, $buyInfo['dispatch_amount'], 2);
- }
- }
- // 真实运费 = 总运费,减去总包邮优惠
- $this->orderData['real_dispatch_amount'] = bcsub($this->orderData['dispatch_amount'], $promo_dispatch_discount_fee, 2);
- $this->orderData['promo_discount_fee'] = bcadd($this->orderData['promo_discount_fee'], $promo_dispatch_discount_fee, 2);
- // second:复杂计算方式,一个运费模板中多个商品只有一个包邮时,包邮优惠重新计算,而不是直接使用 dispatch_amount
- // foreach ($this->orderData['dispatch_infos'] as $key => $info) {
- // $finalExpress = $info['finalExpress'];
- // $total_buy_num = 0; // 总购买数量
- // $total_weight = 0; // 总购买重量
- // $buy_num = 0; // 不包邮购买数量
- // $weight = 0; // 不包邮购买重量
- // $currentGoodsIds = array_column($info['buyInfos'], 'goods_id');
- // $currentFreeGoodsIds = array_intersect($this->orderData['free_shipping_goods_ids'], $currentGoodsIds); // 当前配送模板中的包邮商品
- // $noFreeGoodsIds = array_diff($currentGoodsIds, $currentFreeGoodsIds); // 当前配送模板中的不包邮商品
- // foreach ($info['buyInfos'] as $k => $dispatchBuyInfo) {
- // $total_buy_num += $dispatchBuyInfo['goods_num'];
- // $total_weight = bcadd($total_weight, $dispatchBuyInfo['weight'], 2);
- // if (!in_array($dispatchBuyInfo['goods_id'], $this->orderData['free_shipping_goods_ids'])) {
- // // 不是包邮商品 累加
- // $buy_num += $dispatchBuyInfo['goods_num'];
- // $weight = bcadd($weight, $dispatchBuyInfo['weight'], 2);
- // }
- // }
- // if ($currentFreeGoodsIds && $noFreeGoodsIds) {
- // // 有免邮商品,也有非免邮商品,重新计算除包邮优惠后的运费
- // $current_dispatch_amount = $this->calcDispatch($finalExpress, [
- // 'goods_num' => $buy_num,
- // 'weight' => $weight
- // ]);
- // // 模板总运费优惠金额
- // $current_discount_dispatch_amount = bcsub($info['current_dispatch_amount'], $current_dispatch_amount, 2);
- // if ($current_discount_dispatch_amount > 0) {
- // // 将优惠金额按照运费模板平均分配
- // $this->equalDispatchDiscount($currentFreeGoodsIds, $current_discount_dispatch_amount, [
- // 'goods_num' => bcsub($total_buy_num, $buy_num), // 包邮购买数量
- // 'weight' => bcsub($total_weight, $weight, 2), // 包邮购买重量
- // 'final_express' => $finalExpress
- // ]);
- // // 将新的运费加权平均到每个 item 上
- // $this->equalDispatch($current_dispatch_amount, $noFreeGoodsIds, [
- // 'goods_num' => $buy_num,
- // 'weight' => $weight,
- // 'final_express' => $finalExpress
- // ]);
- // }
- // } elseif ($currentFreeGoodsIds) {
- // // 全部包邮了,将邮费优惠按件数或者重量分配了
- // $current_discount_dispatch_amount = $info['current_dispatch_amount'];
- // if ($current_discount_dispatch_amount > 0) {
- // // 每个商品运费优惠,就是 dispatch_amount
- // $this->allDispatchDiscount($currentFreeGoodsIds);
- // }
- // $current_dispatch_amount = 0;
- // } else {
- // // 全部不包邮,每个商品的 dispatch_discount_fee 为初始值,不需要处理每个商品分配到的运费优惠
- // $current_dispatch_amount = $info['current_dispatch_amount'];
- // }
- // $this->orderData['real_dispatch_amount'] = bcadd($this->orderData['real_dispatch_amount'], $current_dispatch_amount, 2);
- // }
- // // 包邮活动优惠的总费用
- // $promo_dispatch_discount_fee = bcsub($this->orderData['dispatch_amount'], $this->orderData['real_dispatch_amount'], 2);
- // $this->orderData['promo_discount_fee'] = bcadd($this->orderData['promo_discount_fee'], $promo_dispatch_discount_fee, 2);
- // second:复杂计算方式结束
- }
- // 将每个商品对应的 activity_type 放入 goods_list 中的 promo_types 中,重新计算真实的包邮优惠
- foreach ($this->activity['promo_infos'] as &$info) {
- // 寻找商品id 等于 $goods_id 的所有商品,存在同一个商品,购买多个规格的情况
- foreach ($this->goodsList as &$buyInfo) {
- if (in_array($buyInfo['goods_id'], $info['goods_ids'])) {
- if (!in_array($info['activity_type'], $buyInfo['promo_types'])) {
- $buyInfo['promo_types'][] = $info['activity_type'];
- }
- if ($info['activity_type'] == 'free_shipping') {
- // 重新计算运费真实优惠
- $info['promo_discount_money'] = bcadd($info['promo_discount_money'], $buyInfo['dispatch_discount_fee'], 2);
- }
- }
- }
- }
- }
- /**
- * 计算优惠券的优惠
- *
- * @return void
- */
- public function calcCoupon()
- {
- // 获取优惠券
- $this->orderData['coupon'] = $this->getCoupon($this->coupon_id);
- if ($this->orderData['coupon']) {
- $this->orderData['coupon_discount_fee'] = $this->orderData['coupon']['coupon_money']; // 获取优惠券时计算的金额
- $this->orderData['coupon_goods_ids'] = $this->orderData['coupon']['goods_ids']; // 获取优惠券时参与的商品
- // 将当前优惠券优惠金额平分到使用优惠券的商品上
- foreach ($this->goodsList as &$buyInfo) {
- if (in_array($buyInfo['goods_id'], $this->orderData['coupon_goods_ids'])) {
- $scale = bcdiv($buyInfo['goods_amount'], $this->orderData['coupon']['goods_amount'], 6);
- // 当前商品,当前活动分配的优惠金额
- $current_coupon_discount_fee = round(bcmul($this->orderData['coupon_discount_fee'], $scale, 3), 2);
- $buyInfo['coupon_discount_fee'] = bcadd($buyInfo['coupon_discount_fee'], $current_coupon_discount_fee, 2); // 当前商品可用优惠券的累计优惠金额
- }
- }
- } else {
- $this->exception('优惠券不可用');
- }
- }
- /**
- * 获取用户的优惠券列表,可用和不可用的做区分
- *
- * @return array
- */
- public function getCoupons($calc_type = 'coupons')
- {
- $this->calc_type = $calc_type;
- // 检查是否可下单
- $this->calcCheck();
- // 计算订单各种费用
- $this->calcAmount();
- // 用户可用优惠券列表
- // 这里使用的with,没法用 coupon表的减免金额做排序,可能需要重新写
- $coupons = Coupon::with('coupon')->where('user_id', $this->user->id)->canUse()->select();
- $cannot_use = [];
- $can_use = [];
- foreach ($coupons as $key => $coupon) {
- $result = $this->checkCoupon($coupon);
- if (isset($result['can_use']) && $result['can_use']) {
- $can_use[] = $coupon;
- } else {
- $coupon->cannot_use_msg = $result['cannot_use_msg'];
- $cannot_use[] = $coupon;
- }
- }
- return compact('can_use', 'cannot_use');
- }
- public function getCoupon($coupon_id)
- {
- // 选用的优惠券
- $coupon = Coupon::with('coupon')->where('user_id', $this->user->id)->canUse()->find($coupon_id);
- if ($coupon) {
- $result = $this->checkCoupon($coupon);
- return (isset($result['can_use']) && $result['can_use']) ? $coupon : null;
- }
- return null;
- }
- /**
- * 检测用户优惠券是否可用
- *
- * @param array|object $coupon
- * @param string $type
- * @return array|object
- */
- private function checkCoupon($coupon)
- {
- $can_use = true;
- $cannot_use_msg = '';
- // (积分或者活动 并且 优惠券不支持优惠叠加)
- if (($this->order_type == 'score' || $this->activity_id) && !$coupon->is_double_discount) {
- $can_use = false;
- $cannot_use_msg = '优惠券不可与活动叠加';
- return compact('can_use', 'cannot_use_msg');
- }
- $goods_amount = 0;
- $goodsIds = []; // 符合优惠券规则的 商品 ids
- if ($coupon->use_scope == 'all_use') {
- // 计算商品总金额
- foreach ($this->goodsList as $buyInfo) {
- $goods_amount = bcadd($goods_amount, $buyInfo['goods_amount'], 2);
- $goodsIds[] = $buyInfo['goods_id'];
- }
- } elseif ($coupon->use_scope == 'goods') {
- // 计算指定商品的总金额是否满足
- foreach ($this->goodsList as $buyInfo) {
- if (in_array($buyInfo['goods_id'], explode(',', $coupon->items))) {
- $goods_amount = bcadd($goods_amount, $buyInfo['goods_amount'], 2);
- $goodsIds[] = $buyInfo['goods_id'];
- }
- }
- } elseif ($coupon->use_scope == 'disabled_goods') {
- // 计算非指定的商品的总金额是否满足
- foreach ($this->goodsList as $buyInfo) {
- if (!in_array($buyInfo['goods_id'], explode(',', $coupon->items))) {
- $goods_amount = bcadd($goods_amount, $buyInfo['goods_amount'], 2);
- $goodsIds[] = $buyInfo['goods_id'];
- }
- }
- } elseif ($coupon->use_scope == 'category') {
- // 计算指定分类的商品的总金额
- foreach ($this->goodsList as $buyInfo) {
- // 取分类交集
- if (array_intersect(explode(',', $buyInfo['goods']['category_ids']), explode(',', $coupon->items))) {
- $goods_amount = bcadd($goods_amount, $buyInfo['goods_amount'], 2);
- $goodsIds[] = $buyInfo['goods_id'];
- }
- }
- }
- if ($coupon->enough <= $goods_amount) {
- $coupon['coupon_money'] = $coupon->amount;
- $coupon['goods_amount'] = $goods_amount; // 参与优惠券的商品的总金额
- $coupon['goods_ids'] = $goodsIds;
- if ($coupon->type == 'discount') {
- $scale = bcmul($coupon->amount, 0.1, 3);
- $scale = $scale > 1 ? 1 : ($scale < 0 ? 0 : $scale);
- $coupon_money = bcmul(bcsub(1, $scale, 3), $goods_amount, 2);
- // 优惠金额最大为 coupon->max_amount;
- $coupon['coupon_money'] = $coupon->max_amount && $coupon_money > $coupon->max_amount ? $coupon->max_amount : $coupon_money;
- }
- $coupons[] = $coupon;
- } else {
- $can_use = false;
- if ($goods_amount > 0) {
- $cannot_use_msg = '商品金额不满足优惠券门槛';
- } else {
- $cannot_use_msg = '优惠券不支持该商品';
- }
- }
- return compact('can_use', 'cannot_use_msg');
- }
- /**
- * 获取匹配的配送规则
- *
- * @param string $dispatch_type
- * @param integer $dispatch_id
- * @return array
- */
- public function getDispatchExpress($dispatch_type, $dispatch_id)
- {
- // 物流快递
- $dispatch = Dispatch::with('express')->show()->where('type', $dispatch_type)->where('id', $dispatch_id)->find();
- if (!$dispatch) {
- $this->exception('配送方式不存在', true);
- }
- $finalExpress = null;
- foreach ($dispatch->express as $key => $express) {
- if (strpos($express->district_ids, strval($this->userAddress->district_id)) !== false) {
- $finalExpress = $express;
- break;
- }
- if (strpos($express->city_ids, strval($this->userAddress->city_id)) !== false) {
- $finalExpress = $express;
- break;
- }
- if (strpos($express->province_ids, strval($this->userAddress->province_id)) !== false) {
- $finalExpress = $express;
- break;
- }
- }
- if (empty($finalExpress)) {
- $this->exception('当前地区不在配送范围');
- }
- return $finalExpress;
- }
- /**
- * 获取匹配的配送规则
- *
- * @param string $dispatch_type
- * @param integer $dispatch_id
- * @return array
- */
- public function getDispatchAutosend($dispatch_type, $dispatch_id)
- {
- // 物流快递
- $dispatch = Dispatch::with(['autosend'])->show()->where('type', $dispatch_type)->where('id', $dispatch_id)->find();
- if (!$dispatch) {
- $this->exception('配送方式不存在', true);
- }
- return $dispatch;
- }
- public function calcDispatch($finalExpress, $data)
- {
- if (empty($finalExpress)) {
- // 没有找到 finalExpress,比如商品不在配送范围,直接返回 0
- return 0;
- }
-
- // 初始费用
- $dispatch_amount = $finalExpress->first_price;
- if ($finalExpress['type'] == 'number') {
- // 按件计算
- if ($finalExpress->additional_num && $finalExpress->additional_price) {
- // 首件之后剩余件数
- $surplus_num = $data['goods_num'] - $finalExpress->first_num;
- // 多出的计量
- $additional_mul = ceil(($surplus_num / $finalExpress->additional_num));
- if ($additional_mul > 0) {
- $additional_dispatch_amount = bcmul($additional_mul, $finalExpress->additional_price, 2);
- $dispatch_amount = bcadd($dispatch_amount, $additional_dispatch_amount, 2);
- }
- }
- } else {
- // 按重量计算
- if ($finalExpress->additional_num && $finalExpress->additional_price) {
- // 首重之后剩余重量
- $surplus_num = $data['weight'] - $finalExpress->first_num;
- // 多出的计量
- $additional_mul = ceil(($surplus_num / $finalExpress->additional_num));
- if ($additional_mul > 0) {
- $additional_dispatch_amount = bcmul($additional_mul, $finalExpress->additional_price, 2);
- $dispatch_amount = bcadd($dispatch_amount, $additional_dispatch_amount, 2);
- }
- }
- }
-
- return $dispatch_amount;
- }
- /**
- * 加权平均每个包邮商品,应该分配的优惠(一个运费模板中的商品,部分包邮,部分不包邮,要重新计算)
- *
- * @param [type] $currentFreeGoodsIds
- * @param [type] $current_dispatch_amount
- * @param array $data
- * @return void
- */
- public function equalDispatchDiscount($currentFreeGoodsIds, $dispatch_discount_amount, $data = [])
- {
- $goods_num = $data['goods_num'];
- $weight = $data['weight'];
- $finalExpress = $data['final_express']; // 这里肯定有,否则走不到这个方法
- foreach ($this->goodsList as $key => &$buyInfo) {
- if (in_array($buyInfo['goods_id'], $currentFreeGoodsIds)) {
- $scale = 0; // 重量或者数量计算比例
- if ($finalExpress['type'] == 'number') {
- // 按件
- if (floatval($goods_num)) { // 字符串 0.00 是 true, 这里转下类型在判断
- $scale = bcdiv($buyInfo['goods_num'], $goods_num, 6);
- }
- $current_dispatch_discount_fee = round(bcmul($dispatch_discount_amount, $scale, 3), 2);
- } else {
- // 按重量
- if (floatval($weight)) {
- $current_weight = bcmul($buyInfo['current_sku_price']->weight, $buyInfo['goods_num'], 2);
- $scale = bcdiv($current_weight, $weight, 6);
- }
- $current_dispatch_discount_fee = round(bcmul($dispatch_discount_amount, $scale, 3), 2);
- }
- // 每个商品分配到的包邮优惠金额, 和订单 item 上的dispatch_fee 不一样,因为 剩余运费又重新计算了,这个优惠没有 dispatch_fee 大
- $buyInfo['dispatch_discount_fee'] = $current_dispatch_discount_fee;
- }
- }
- }
- /**
- * 同一运费模板额所有商品都包邮了,不需要重新计算,优惠金额就是 dispatch_amount
- *
- * @param [type] $currentFreeGoodsIds
- * @param [type] $current_dispatch_amount
- * @param array $data
- * @return void
- */
- public function allDispatchDiscount($currentFreeGoodsIds)
- {
- foreach ($this->goodsList as $key => &$buyInfo) {
- if (in_array($buyInfo['goods_id'], $currentFreeGoodsIds)) {
- // 每个商品分配到的包邮优惠金额, 和订单 item 上的dispatch_fee 不一样,因为 剩余运费又重新计算了,这个优惠没有 dispatch_fee 大
- $buyInfo['dispatch_discount_fee'] = $buyInfo['dispatch_amount'];
- }
- }
- }
- /**
- * 加权平分同一运费模板下商品实际应付运费
- *
- * @param float $dispatch_amount
- * @param array $currentBuyInfos
- * @param array $data
- * @return void
- */
- private function equalDispatch($dispatch_amount, $currentBuyInfos, $data)
- {
- $goods_num = $data['goods_num'];
- $weight = $data['weight'];
- $finalExpress = $data['final_express'];
- // 当前运费模板中的商品 Ids
- $goodsIds = array_column($currentBuyInfos, 'goods_id');
- foreach ($this->goodsList as $key => &$buyInfo) {
- if (in_array($buyInfo['goods_id'], $goodsIds)) {
- $scale = 0; // 重量或者数量计算比例
- if ($finalExpress['type'] == 'number') {
- // 按件
- if ($goods_num) { // 字符串 0.00 是 true, 这里转下类型在判断
- $scale = bcdiv($buyInfo['goods_num'], $goods_num, 6);
- }
- $current_dispatch_amount = round(bcmul($dispatch_amount, $scale, 3), 2);
- } else {
- // 按重量
- if (floatval($weight)) {
- $scale = bcdiv($buyInfo['weight'], $weight, 6);
- }
- $current_dispatch_amount = round(bcmul($dispatch_amount, $scale, 3), 2);
- }
- $buyInfo['dispatch_amount'] = $current_dispatch_amount; // 每个商品分配到的实际应支付运费
- }
- }
- }
- public function calcReturn() {
- $invoiceConfig = sheep_config('shop.order.invoice');
- $this->invoiceConfig = [
- 'status' => $invoiceConfig['status'] && $this->orderData['pay_fee'] ? 1 : 0, // 可开具发票状态,
- 'amount_type' => $invoiceConfig['amount_type'] ?? 'pay_fee',
- 'user_invoice' => null
- ];
- if ($this->invoiceConfig['status'] && $this->invoice_id) {
- // 获取用户发票信息
- $this->invoiceConfig['user_invoice'] = UserInvoice::where('user_id', $this->user->id)->find($this->invoice_id);
- if (!$this->invoiceConfig['user_invoice']) {
- $this->exception('请选择正确的发票信息');
- }
- }
- $temp_remain_pay_fee = bcsub($this->orderData['pay_fee'], $this->money, 2);
- $result = [
- 'goods_original_amount' => $this->orderData['goods_original_amount'],
- 'goods_old_amount' => $this->orderData['goods_old_amount'],
- 'goods_amount' => $this->orderData['goods_amount'],
- 'dispatch_amount' => $this->orderData['dispatch_amount'],
- 'real_dispatch_amount' => $this->orderData['real_dispatch_amount'],
- 'order_amount' => $this->orderData['order_amount'],
- 'pay_fee' => $this->orderData['pay_fee'],
- 'temp_remain_pay_fee' => $temp_remain_pay_fee >= 0 ? $temp_remain_pay_fee : 0, // 临时剩余应支付金额,订单确认页面显示
- 'total_discount_fee' => $this->orderData['total_discount_fee'],
- 'coupon_discount_fee' => $this->orderData['coupon_discount_fee'],
- 'promo_discount_fee' => $this->orderData['promo_discount_fee'], // 包含包邮的运费优惠
- 'money' => $this->money, // 余额支付部分
- "invoice_amount" => $this->orderData[$this->invoiceConfig['amount_type']] // 可开票金额
- ];
- // 处理小数点保留两位小数
- foreach ($result as $key => $amount) {
- $result[$key] = number_format($amount, 2, '.', '');
- }
- // 合并不需要处理小数点的
- $result = array_merge($result, [
- 'activity_id' => $this->activity['activity']['id'] ?? 0,
- 'activity_type' => $this->activity['activity']['type'] ?? null,
- 'activity_title' => $this->activity['activity']['title'] ?? null,
- 'promo_types' => array_column($this->activity['promo_infos'], 'activity_type'),
- 'score_amount' => $this->orderData['score_amount'],
- 'goods_list' => $this->goodsList,
- 'promo_infos' => $this->activity['promo_infos'],
- "invoice_status" => $this->invoiceConfig['status'], // 发票可开状态
- "invoice_amount_type" => $this->invoiceConfig['amount_type'], // 发票金额类型
- "need_address" => $this->need_address,
- "offline_status" => $this->offline_status,
- ]);
- // 如果是下单,合并 优惠券, 收货地址
- if ($this->calc_type == 'create') {
- $result = array_merge($result, [
- "coupon" => $this->orderData['coupon'],
- "user_address" => $this->userAddress
- ]);
- } else {
- $result = array_merge($result, ['msg' => $this->msg]);
- }
- return $result;
- }
- /**
- * 计算订单
- *
- * @return void
- */
- public function calc($calc_type = 'calc')
- {
- $this->calc_type = $calc_type;
- // 检查系统必要条件
- check_env(['bcmath', 'queue']);
- // 检查是否可下单
- $this->calcCheck();
- // 计算订单各种费用
- $this->calcAmount();
- // 计算订单各种优惠(promo 等)
- if ($this->isCalcPromos()) { // 判断是否参与优惠
- $this->calcDiscount();
- }
- // 计算优惠券费用
- if ($this->coupon_id) {
- $this->calcCoupon();
- }
- // 订单应付金额
- $this->orderData['order_amount'] = bcadd($this->orderData['goods_amount'], $this->orderData['dispatch_amount'], 2);
- $this->orderData['total_discount_fee'] = bcadd($this->orderData['coupon_discount_fee'], $this->orderData['promo_discount_fee'], 2);
- $this->orderData['pay_fee'] = bcsub($this->orderData['order_amount'], $this->orderData['total_discount_fee'], 2);
- $this->orderData['pay_fee'] = $this->orderData['pay_fee'] < 0 ? 0 : $this->orderData['pay_fee'];
- return $this->calcReturn();
- }
- public function create($result)
- {
- $stockSale = new StockSale();
- try {
- // 如果失败,被扣除的库存,退回
- $order = $this->createOrder($result);
- $stockSale->successDelHashKey();
- } catch (\Exception $e) {
- $message = $e->getMessage();
- if ($e instanceof \think\exception\HttpResponseException) {
- $data = $e->getResponse()->getData();
- $message = $data ? ($data['msg'] ?? '') : $e->getMessage();
- }
- // 下单失败,检测并返还锁定的库存
- $stockSale->faildStockUnLock();
- error_stop($message);
- }
- // 重新获取订单
- $order = Order::with('items')->where('id', $order->id)->find();
- $pay_money = floatval($result['money']);
- if ($order->status == Order::STATUS_UNPAID && $pay_money > 0) {
- // 如果订单未支付,并且使用了余额混合支付 (支付失败,不影响订单状态)
- $order = Db::transaction(function () use ($order, $pay_money) {
- $payOper = new PayOper();
- // 余额支付,并判断是否直接支付成功了
- $order = $payOper->money($order, $pay_money, 'order');
- return $order;
- });
- }
- return $order;
- }
- private function createOrder($result)
- {
- $order = Db::transaction(function () use ($result) {
- $orderData['type'] = $this->order_type;
- $orderData['order_sn'] = get_sn($this->user->id);
- $orderData['user_id'] = $this->user->id;
- $orderData['activity_id'] = $result['activity_id'];
- $orderData['activity_type'] = $result['activity_type'];
- $orderData['promo_types'] = join(',', $result['promo_types']);
- $orderData['goods_original_amount'] = $result['goods_original_amount'];
- $orderData['goods_amount'] = $result['goods_amount'];
- $orderData['dispatch_amount'] = $result['dispatch_amount'];
- $orderData['remark'] = $this->remark;
- $orderData['order_amount'] = $result['order_amount'];
- $orderData['score_amount'] = $result['score_amount'];
- $orderData['pay_fee'] = $result['pay_fee'];
- $orderData['original_pay_fee'] = $result['pay_fee']; // 记录支付总金额,后面可能会订单改价
- $orderData['remain_pay_fee'] = $result['pay_fee']; // 这里还是显示应支付总金额,扣除动作放到实际支付时
- $orderData['total_discount_fee'] = $result['total_discount_fee'];
- $orderData['coupon_discount_fee'] = $result['coupon_discount_fee'];
- $orderData['promo_discount_fee'] = $result['promo_discount_fee'];
- $orderData['coupon_id'] = $result['coupon'] ? $result['coupon']['id'] : 0;
- $orderData['status'] = Order::STATUS_UNPAID;
- $orderData['platform'] = request()->header('platform');
- //直播间id
- $orderData['room_id'] = 0;
- $orderData['room_log_id'] = 0;
- $room_log_id = request()->param('room_log_id',0);
- if($room_log_id){
- $room_id = Db::name('live_room_log')->where('id',$room_log_id)->value('room_id');
- if($room_id){
- $orderData['room_id'] = $room_id;
- $orderData['room_log_id'] = $room_log_id;
- }
- }
- $ext = $result['promo_infos'] ? ['promo_infos' => $result['promo_infos']] : []; // 促销活动信息
- if ($this->activity['activity']) {
- $rules = $this->activity['activity']['rules'];
- $ext['refund_type'] = $rules['refund_type'] ?? 'back'; // 退款方式,(目前主要针对拼团失败自动退款)
- }
- $ext['real_dispatch_amount'] = $result['real_dispatch_amount']; // 真实运费,剪掉运费优惠金额
- $goods_discount_amount = bcsub($result['goods_old_amount'], $result['goods_amount'], 2); // 如果参与活动时商品总价的优惠费用
- $ext['activity_discount_amount'] = $goods_discount_amount > 0 ? $goods_discount_amount : 0; // 如果参与活动时的优惠费用
- if ($result['activity_id']) {
- $ext['activity_type'] = $result['activity_type'];
- $ext['activity_title'] = $result['activity_title'];
- }
- $ext['offline_status'] = $result['offline_status'];
- $ext['need_address'] = $result['need_address']; // 后台判断是否有 编辑收货地址按钮
- $orderData['ext'] = $ext;
- // 发票开具状态
- $invoice_status = 0; // 没有申请
- if ($result['invoice_status'] && $this->invoice_id) {
- // 可开具,并且申请了
- $invoice_status = 1;
- } else if ($result['invoice_status'] == 0) {
- // 不可开具
- $invoice_status = -1;
- }
- $orderData['invoice_status'] = $invoice_status; // 可开具发票,并且申请了
- // 订单创建前
- $hookData = [
- 'params' => $result,
- 'order_data' => $orderData
- ];
- \think\Hook::listen('order_create_before', $hookData);
- $order = new Order();
- $order->save($orderData);
- // 添加收货地址信息
- if ($result['user_address']) {
- $this->createOrderAddress($order, $result);
- }
- // 添加发票信息
- if ($invoice_status == 1) {
- $this->createOrderInvoice($order, $result);
- }
- // 将优惠券使用掉
- if ($result['coupon']) {
- $result['coupon']->use_order_id = $order->id;
- $result['coupon']->use_time = time();
- $result['coupon']->allowField(true)->save();
- }
- // 添加 订单 item
- foreach ($result['goods_list'] as $key => $buyInfo) {
- $goods = $buyInfo['goods'];
- $current_sku_price = $buyInfo['current_sku_price'];
- $orderItem = new OrderItem();
- $orderItem->order_id = $order->id;
- $orderItem->user_id = $this->user->id;
- $orderItem->goods_id = $buyInfo['goods_id'];
- $orderItem->goods_type = $goods['type'];
- $orderItem->goods_sku_price_id = $buyInfo['goods_sku_price_id'];
- $orderItem->activity_id = $result['activity_id']; // 商品当前参与的活动 id
- $orderItem->activity_type = $result['activity_type']; // 商品当前的活动类型
- $orderItem->promo_types = join(',', $buyInfo['promo_types']); // 商品当前的活动类型
- // 当前商品规格对应的 活动下对应商品规格的 id
- $orderItem->item_goods_sku_price_id = isset($current_sku_price['item_goods_sku_price']) ?
- $current_sku_price['item_goods_sku_price']['id'] : 0;
- $orderItem->goods_sku_text = is_array($current_sku_price->goods_sku_text) ? join(',', $current_sku_price->goods_sku_text) : '';
- $orderItem->goods_title = $goods->title;
- $orderItem->goods_image = empty($current_sku_price->image) ? $goods->image : $current_sku_price->image;
- $orderItem->goods_original_price = $goods->original_price;
- $orderItem->goods_price = $current_sku_price->price;
- $orderItem->goods_num = $buyInfo['goods_num'] ?? 1;
- $orderItem->goods_weight = $current_sku_price->weight;
- $orderItem->discount_fee = bcadd(bcadd($buyInfo['promo_discount_fee'], $buyInfo['coupon_discount_fee'], 2), $buyInfo['dispatch_discount_fee'], 2); // 当前商品所享受的折扣 (promo_discount_fee + coupon_discount_fee + dispatch_discount_fee)
- $pay_fee = bcsub($buyInfo['goods_amount'], bcadd($buyInfo['promo_discount_fee'], $buyInfo['coupon_discount_fee'], 2), 2); // 商品总金额(不算运费),减去(活动优惠(不包含包邮优惠) + 优惠券优惠)
- $orderItem->pay_fee = $pay_fee; // 平均计算单件商品不算运费,算折扣时候的金额
- $orderItem->dispatch_status = 0;
- $orderItem->dispatch_fee = $buyInfo['dispatch_amount']; // 运费模板中商品自动合并后的加权平均运费,没有扣除包邮优惠
- $orderItem->dispatch_type = $buyInfo['dispatch_type'];
- $orderItem->dispatch_id = $buyInfo['dispatch_id'] ? $buyInfo['dispatch_id'] : 0;
- $orderItem->aftersale_status = 0;
- $orderItem->comment_status = 0;
- $orderItem->refund_status = 0;
- $orderItem->room_id = $orderData['room_id'];
- $orderItem->room_log_id = $orderData['room_log_id'];
- $ext = [
- 'original_dispatch_amount' => $buyInfo['original_dispatch_amount'], // 原始运费总金额(未判断活动的,并且也未合并相同运费模板商品的原始运费)
- 'promo_discount_fee' => bcadd($buyInfo['promo_discount_fee'], $buyInfo['dispatch_discount_fee'], 2), // 促销优惠,包含满包邮
- 'dispatch_discount_fee' => $buyInfo['dispatch_discount_fee'], // 包邮优惠,已经包含在 promo_discount_fee
- 'coupon_discount_fee' => $buyInfo['coupon_discount_fee'], // 优惠券优惠
- 'activity_sku_price_ext' => $current_sku_price['ext'] ?? [], // 活动规格 ext,保存备用
- 'ladder' => $buyInfo['ladder'] ?? [], // (阶梯拼团仅有)阶梯拼团,当前购买的阶梯
- ];
- if (isset($buyInfo['is_commission'])) {
- $ext['is_commission'] = $buyInfo['is_commission'];
- }
- $orderItem->ext = $ext;
- $orderItem->save();
- }
- // 订单创建后
- $hookData = [
- 'order' => $order,
- 'activity' => $this->activity['activity'],
- ];
- \think\Hook::listen('order_create_after', $hookData);
- return $order;
- });
- return $order;
- }
- /**
- * 添加收货地址信息
- *
- * @param \think\Model $order
- * @param array $result
- * @return void
- */
- private function createOrderAddress($order, $result)
- {
- // 保存收货地址
- $orderAddress = new OrderAddress();
- $orderAddress->order_id = $order->id;
- $orderAddress->user_id = $this->user->id;
- $orderAddress->consignee = $result['user_address']->consignee;
- $orderAddress->mobile = $result['user_address']->mobile;
- $orderAddress->province_name = $result['user_address']->province_name;
- $orderAddress->city_name = $result['user_address']->city_name;
- $orderAddress->district_name = $result['user_address']->district_name;
- $orderAddress->address = $result['user_address']->address;
- $orderAddress->province_id = $result['user_address']->province_id;
- $orderAddress->city_id = $result['user_address']->city_id;
- $orderAddress->district_id = $result['user_address']->district_id;
- $orderAddress->save();
- }
- /**
- * 添加发票信息
- *
- * @param \think\Model $order
- * @param array $result
- * @return void
- */
- private function createOrderInvoice($order, $result)
- {
- $userInvoice = $this->invoiceConfig['user_invoice'];
- // 保存收货地址
- $orderInvoice = new OrderInvoice();
- $orderInvoice->type = $userInvoice->type;
- $orderInvoice->order_id = $order->id;
- $orderInvoice->user_id = $userInvoice->user_id;
- $orderInvoice->name = $userInvoice->name;
- $orderInvoice->tax_no = $userInvoice->tax_no;
- $orderInvoice->address = $userInvoice->address;
- $orderInvoice->mobile = $userInvoice->mobile;
- $orderInvoice->bank_name = $userInvoice->bank_name;
- $orderInvoice->bank_no = $userInvoice->bank_no;
- $orderInvoice->amount = $result['invoice_amount'];
- $orderInvoice->status = 'unpaid';
- $orderInvoice->save();
- }
- /**
- * 判断并处理异常
- *
- * @param string $msg 处理结果
- * @param boolean $force 是否强制抛出异常
- * @return boolean
- */
- private function exception($msg, $force = false)
- {
- if ($this->calc_type == 'create' || $force) {
- // 如果是创建订单,或者强制抛出异常
- error_stop($msg);
- } else {
- // 预下单,记录第一条错误信息
- if (!$this->msg) {
- $this->msg = $msg;
- }
- }
- return false;
- }
- /**
- * 获取商品服务实例
- *
- * @return GoodsService
- */
- private function getGoodsService()
- {
- // 实例化商品服务
- return new GoodsService(function ($goods, $service) {
- // $goods->sku_prices;
- // 这个写法没用,只要判断 promos 还是会获取 promos
- // if (!$this->activity_id) {
- // // 可能要参与的营销活动
- // $goods->promos = $goods->promos;
- // }
- return $goods;
- });
- }
- /**
- * 是否计算促销
- *
- * @return bool
- */
- private function isCalcPromos()
- {
- if ($this->order_type == 'score') {
- return false; // 积分商品不能参与促销
- } else if ($this->activity_id) {
- if ($this->activity_promos) {
- // 活动与促销共存
- return true;
- } else {
- // 参与活动,不能在参与促销
- return false;
- }
- } else {
- return true;
- }
- }
- }
|