OrderCreate.php 57 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367
  1. <?php
  2. namespace addons\shopro\service\order;
  3. use think\Db;
  4. use addons\shopro\service\goods\GoodsService;
  5. use addons\shopro\service\pay\PayOper;
  6. use addons\shopro\exception\ShoproException;
  7. use addons\shopro\facade\Activity as ActivityFacade;
  8. use app\admin\model\shopro\goods\Goods;
  9. use app\admin\model\shopro\order\Order;
  10. use app\admin\model\shopro\order\OrderItem;
  11. use app\admin\model\shopro\order\Address as OrderAddress;
  12. use app\admin\model\shopro\order\Invoice as OrderInvoice;
  13. use app\admin\model\shopro\dispatch\Dispatch;
  14. use app\admin\model\shopro\user\Coupon;
  15. use app\admin\model\shopro\user\Address as UserAddress;
  16. use app\admin\model\shopro\user\Invoice as UserInvoice;
  17. use addons\shopro\service\StockSale;
  18. class OrderCreate
  19. {
  20. protected $user = null;
  21. /**
  22. * 订单类型
  23. */
  24. protected $order_type = 'goods';
  25. /**
  26. * 是否需要收货地址,自动发货商品不需要选
  27. */
  28. protected $need_address = 1;
  29. /**
  30. * 用户选择的收货地址 id
  31. */
  32. protected $address_id = 0;
  33. /**
  34. * 选择的收货地址信息
  35. */
  36. protected $userAddress = null;
  37. /**
  38. * 货到付款的状态
  39. */
  40. protected $offline_status = 'none';
  41. /**
  42. * 当前参与的活动
  43. */
  44. protected $activity_id = 0;
  45. /**
  46. * 用户选择的开票信息
  47. */
  48. protected $invoice_id = 0;
  49. /**
  50. * 用户选择的优惠券 id
  51. */
  52. protected $coupon_id = 0;
  53. /**
  54. * 用户备注
  55. */
  56. protected $remark = 0;
  57. /**
  58. * 余额抵扣(余额和 微信|支付宝,混合支付时使用了)
  59. */
  60. protected $money = 0;
  61. /**
  62. * 发票配置
  63. */
  64. protected $invoiceConfig = [];
  65. /**
  66. * 类型,create:创建订单,calc:计算费用
  67. */
  68. protected $calc_type = 'create';
  69. protected $goodsList = [];
  70. protected $msg = null;
  71. protected $activity = [
  72. 'activity' => null, // 当前订单参与的活动
  73. 'promos' => [], // 当前订单可能参与的促销(每个商品所涉及的促销的集合)
  74. 'promo_infos' => [], // 当前订单实际参与的促销
  75. ];
  76. /**
  77. * 订单相关费用
  78. */
  79. protected $orderData = [
  80. 'goods_original_amount' => '0', // 商品原始总价
  81. 'goods_old_amount' => '0', // 商品不参与活动时的总价
  82. 'goods_amount' => '0', // 商品总价
  83. 'coupon_discount_fee' => '0', // 优惠券优惠金额
  84. 'promo_discount_fee' => 0, // 当前促销优惠总金额 (包含满包邮的邮费)
  85. 'total_discount_fee' => 0, // 当前订单,总优惠金额(优惠券 + 活动优惠)
  86. 'coupon' => null, // 所使用的优惠券
  87. 'coupon_goods_ids' => [],
  88. 'dispatch_amount' => '0', // 运费总价
  89. 'real_dispatch_amount' => '0', // 免邮减免之后的运费总价,不做计算使用
  90. 'score_amount' => 0, // 订单总积分
  91. 'free_shipping_goods_ids' => [], // 满额包邮商品 ids
  92. 'dispatch_infos' => [], // 当前订单商品按照配送方式分组
  93. 'order_amount' => 0, // 订单总费用
  94. 'pay_fee' => 0 // 应支付总金额 (减去优惠之后的)
  95. ];
  96. /**
  97. * 活动(拼团,秒杀) 和 促销(满减,满折),是否可以同时存在
  98. */
  99. protected $activity_promos = false;
  100. public function __construct($params)
  101. {
  102. $this->user = auth_user();
  103. $this->calcParams($params);
  104. }
  105. /**
  106. * 获取请求参数,初始化,并设置默认值
  107. *
  108. * @param array $params
  109. * @return array
  110. */
  111. public function calcParams($params)
  112. {
  113. $this->order_type = $params['order_type'] ?? 'goods';
  114. $this->activity_id = $params['activity_id'] ?? 0;
  115. // $groupon_id = $groupon_id ?? 0; // 拼团的 团 id
  116. // $buy_type = $buy_type ?? 'alone'; // 拼团的 购买方式: alone=单独购买,groupon=开团
  117. // $this->goodsList = $params['goods_list'] ?? [];
  118. $this->goodsList = $params['goods_list'] ? json_decode(htmlspecialchars_decode($params['goods_list']),true) : [];
  119. $this->address_id = $params['address_id'] ?? 0;
  120. $this->invoice_id = $params['invoice_id'] ?? 0;//发票
  121. $this->coupon_id = $params['coupon_id'] ?? 0;//单张优惠券,优惠券只能与活动叠加
  122. $this->remark = $params['remark'] ?? '';
  123. $this->money = (isset($params['money']) && $params['money'] > 0) ? $params['money'] : 0;
  124. // 获取商品信息
  125. $this->goodsListInit();
  126. }
  127. public function goodsListInit()
  128. {
  129. foreach ($this->goodsList as $key => &$buyInfo) {
  130. $goods_sku_price_id = $buyInfo['goods_sku_price_id'];
  131. if ($this->order_type == 'score') {
  132. $goods = $this->getGoodsService()->score()->show()->where('id', $buyInfo['goods_id'])->findOrFail();
  133. } else {
  134. // 暂时保存当前商品要获取的活动的 id
  135. session('goods-activity_id:' . $buyInfo['goods_id'], $this->activity_id); //设置商品活动id的session
  136. $goods = $this->getGoodsService()->activity($this->activity_id)->show()->where('id', $buyInfo['goods_id'])->findOrFail();
  137. if ($this->activity_id) {
  138. if (!$this->activity['activity'] = $goods->activity) {
  139. $this->exception('活动不存在', true);
  140. }
  141. if ($this->activity['activity']['status'] != 'ing') {
  142. $this->exception('活动' . $this->activity['activity']['status_text']);
  143. }
  144. }
  145. if ($this->isCalcPromos()&& isset($goods['promos']) && $goods['promos']) {
  146. $this->activity['promos'] = array_merge($this->activity['promos'], $goods['promos']);
  147. }
  148. }
  149. $skuPrices = $goods['new_sku_prices'];
  150. foreach ($skuPrices as $key => $skuPrice) {
  151. if ($skuPrice['id'] == $goods_sku_price_id && $skuPrice['status'] == 'up') {
  152. $buyInfo['current_sku_price'] = $skuPrice; // 当前购买规格
  153. break;
  154. }
  155. }
  156. if (!isset($buyInfo['current_sku_price']) || !$buyInfo['current_sku_price']) {
  157. $this->exception('商品规格不存在', true);
  158. }
  159. $buyInfo['dispatch_type'] = $goods['dispatch_type'] ?: 'express';
  160. $buyInfo['dispatch_type_text'] = $goods['dispatch_type_text'] ?? '';
  161. $buyInfo['goods'] = $goods;
  162. }
  163. }
  164. /**
  165. * 下单前检测,商品状态,秒杀,拼团活动状态,必要的选择项(比如下单收货地址),收货地址等
  166. *
  167. * @param array $params,请求参数
  168. * @return array
  169. */
  170. public function calcCheck()
  171. {
  172. $offline_num = 0; // 货到付款商品数量
  173. foreach ($this->goodsList as $key => &$buyInfo) {
  174. $goods = $buyInfo['goods'];
  175. $goods_sku_price_id = $buyInfo['goods_sku_price_id'];
  176. $goods_num = $buyInfo['goods_num'] ?? 1;
  177. // 最少购买一件
  178. $buyInfo['goods_num'] = intval($goods_num) < 1 ? 1 : intval($goods_num);
  179. if ($buyInfo['current_sku_price']['stock'] < $buyInfo['goods_num']) {
  180. $this->exception('商品 ' . $goods['title'] . ' 库存不足');
  181. }
  182. // 校验活动规则
  183. if ($this->activity['activity']) {
  184. try {
  185. $buyInfo = ActivityFacade::buyCheck($buyInfo, $this->activity['activity']);
  186. } catch (ShoproException $e) {
  187. $this->exception($e->getMessage());
  188. }
  189. }
  190. if ($buyInfo['goods']['is_offline']) {
  191. $offline_num ++;
  192. }
  193. $buyInfo['activity_type'] = $this->activity['activity']['type'] ?? null;
  194. }
  195. if (!count($this->goodsList)) {
  196. $this->exception('请选择要购买的商品');
  197. }
  198. if ($this->activity_id && count($this->goodsList) > 1) {
  199. $this->exception($this->activity['activity']['type_text'] . '必须单独购买');
  200. }
  201. if ($this->order_type == 'score' && count($this->goodsList) > 1) {
  202. $this->exception('积分商品必须单独购买');
  203. }
  204. // 判断是否支持货到付款
  205. if ($offline_num < count($this->goodsList)) {
  206. $this->offline_status = $offline_num == 0 ? 'none' : 'disabled';
  207. } else {
  208. $this->offline_status = 'enable';
  209. }
  210. // 判断是否有需要收货地址的商品,新版每个商品需要选择配送方式
  211. $dispatchTypes = array_column($this->goodsList, 'dispatch_type');
  212. // 配送方式为 快递 必须填写收货地址
  213. if (in_array('express', $dispatchTypes)) {
  214. // 用户收货地址
  215. if ($this->address_id) {
  216. $this->userAddress = UserAddress::where("user_id", $this->user->id)->find($this->address_id);
  217. }
  218. if (is_null($this->userAddress) && $this->calc_type == 'create') {
  219. $this->exception("请选择正确的收货地址");
  220. }
  221. } else {
  222. // 不需要收货地址
  223. $this->need_address = 0;
  224. }
  225. }
  226. /**
  227. * 计算订单各种费用
  228. *
  229. * @return array
  230. */
  231. public function calcAmount()
  232. {
  233. // 计算商品金额
  234. foreach ($this->goodsList as $key => &$buyInfo) {
  235. $goods = $buyInfo['goods'];
  236. // 当前商品原始总价
  237. $current_goods_original_amount = bcmul($goods->original_price, $buyInfo['goods_num'], 2);
  238. $this->orderData['goods_original_amount'] = bcadd($this->orderData['goods_original_amount'], $current_goods_original_amount, 2);
  239. // 当前商品现在总价
  240. $current_goods_amount = bcmul($buyInfo['current_sku_price']->price, $buyInfo['goods_num'], 2);
  241. $this->orderData['goods_amount'] = bcadd($this->orderData['goods_amount'], $current_goods_amount, 2);
  242. // 当有活动时,计算作为普通商品时的商品总金额
  243. $current_goods_old_amount = $current_goods_amount;
  244. if ($this->activity['activity']) {
  245. $current_goods_old_amount = bcmul($buyInfo['current_sku_price']->old_price, $buyInfo['goods_num'], 2);
  246. }
  247. $this->orderData['goods_old_amount'] = bcadd($this->orderData['goods_old_amount'], $current_goods_old_amount, 2);
  248. // 当前商品所需积分
  249. $current_score_amount = 0;
  250. if ($this->order_type == 'score') { // 积分商城规格
  251. $current_score_amount = $buyInfo['current_sku_price']->score * $buyInfo['goods_num'];
  252. $this->orderData['score_amount'] = $this->orderData['score_amount'] + $current_score_amount;
  253. }
  254. // 当前商品总重量
  255. $current_weight = bcmul($buyInfo['current_sku_price']->weight, $buyInfo['goods_num'], 2);
  256. // 将计算好的属性记录下来,插入订单 item 表使用
  257. $buyInfo['goods_original_amount'] = $current_goods_original_amount; // 当前商品原始总金额(原价 * 数量)
  258. $buyInfo['goods_amount'] = $current_goods_amount; // 当前商品总金额(价格 * 数量)
  259. $buyInfo['score_amount'] = $current_score_amount; // 商品所需积分(积分商城)
  260. $buyInfo['weight'] = $current_weight; // 当前商品总重量
  261. $buyInfo['original_dispatch_amount'] = 0; // 当前商品运费(未判断活动的,并且也未合并相同运费模板商品的原始运费)
  262. $buyInfo['dispatch_amount'] = 0; // 当前商品运费,按照运费模板合并相同商品的真实运费)
  263. $buyInfo['dispatch_id'] = 0; // 商品运费模板,如果有活动,并且活动包邮,这里是 0
  264. $buyInfo['promo_types'] = []; // 商品参与的营销类型
  265. $buyInfo['dispatch_discount_fee'] = 0; // 初始化每个商品运费优惠金额
  266. $buyInfo['promo_discount_fee'] = 0; // 初始化每个商品的促销优惠金额(这里不包含运费优惠,实际 order_item 上是包含的)
  267. $buyInfo['coupon_discount_fee'] = 0; // 初始化每个商品的优惠券优惠金额
  268. // 商品的配送模板 id
  269. $current_dispatch_id = $goods['dispatch_id'];
  270. // 获取快递配送数据
  271. if ($buyInfo['dispatch_type'] == 'express'
  272. && (!$this->activity['activity'] // 没有活动
  273. || (
  274. $this->activity['activity'] // 有活动,并且(没有是否包邮字段,或者是不包邮)
  275. && (!isset($this->activity['activity']['rules']['is_free_shipping']) || !$this->activity['activity']['rules']['is_free_shipping'])
  276. )
  277. )
  278. ) {
  279. if ($this->userAddress) {
  280. if (!isset($this->orderData['dispatch_infos'][$goods['dispatch_id']]['finalExpress'])) {
  281. $finalExpress = $this->getDispatchExpress($buyInfo['dispatch_type'], $goods['dispatch_id']);
  282. } else {
  283. $finalExpress = $this->orderData['dispatch_infos'][$goods['dispatch_id']]['finalExpress'];
  284. }
  285. $current_original_dispatch_amount = $this->calcDispatch($finalExpress, [
  286. 'goods_num' => $buyInfo['goods_num'],
  287. 'weight' => $current_weight
  288. ]);
  289. $this->orderData['dispatch_infos'][$goods['dispatch_id']]['finalExpress'] = $finalExpress;
  290. $this->orderData['dispatch_infos'][$goods['dispatch_id']]['buyInfos'][] = $buyInfo;
  291. }
  292. $buyInfo['original_dispatch_amount'] = $current_original_dispatch_amount ?? 0; // 没选收货地址时默认为 0
  293. $buyInfo['dispatch_id'] = $current_dispatch_id;
  294. } else if ($buyInfo['dispatch_type'] == 'autosend') {
  295. $this->getDispatchAutosend($buyInfo['dispatch_type'], $goods['dispatch_id']);
  296. $buyInfo['dispatch_id'] = $current_dispatch_id;
  297. }
  298. }
  299. // 计算应付总运费,商品中同一种运费模板聚合进行计算运费,然后再按照运费模板规则,将运费加权平分到每个商品
  300. foreach ($this->orderData['dispatch_infos'] as $key => &$info) {
  301. $finalExpress = $info['finalExpress'];
  302. $buy_num = 0;
  303. $weight = 0;
  304. foreach ($info['buyInfos'] as $k => $infoInfo) {
  305. $buy_num += $infoInfo['goods_num'];
  306. $weight = bcadd($weight, $infoInfo['weight'], 2);
  307. }
  308. // 聚合原始运费
  309. $current_dispatch_amount = $this->calcDispatch($finalExpress, [
  310. 'goods_num' => $buy_num,
  311. 'weight' => $weight
  312. ]);
  313. $info['current_dispatch_amount'] = $current_dispatch_amount; // 记录当前运费模板下商品的运费
  314. // 将运费加权平分到当前运费模板中的每个商品
  315. if ($current_dispatch_amount) {
  316. $this->equalDispatch($current_dispatch_amount, $info['buyInfos'], [
  317. 'goods_num' => $buy_num,
  318. 'weight' => $weight,
  319. 'final_express' => $finalExpress
  320. ]);
  321. }
  322. $this->orderData['dispatch_amount'] = bcadd($this->orderData['dispatch_amount'], $current_dispatch_amount, 2);
  323. }
  324. }
  325. /**
  326. * 计算商品的优惠促销
  327. *
  328. * @return void
  329. */
  330. public function calcDiscount() {
  331. // 过滤重复促销
  332. $promos = [];
  333. foreach ($this->activity['promos'] as $promo) {
  334. if (!isset($promos[$promo['id']])) {
  335. $promos[$promo['id']] = $promo;
  336. }
  337. }
  338. // 将购买的商品,按照促销分类
  339. foreach ($this->goodsList as $buyInfo) {
  340. $goods = $buyInfo['goods'];
  341. unset($buyInfo['goods']);
  342. if (isset($goods['promos']) && $goods['promos']) {
  343. foreach ($goods['promos'] as $promo) {
  344. $promos[$promo['id']]['goods'][] = $buyInfo;
  345. }
  346. }
  347. }
  348. // 计算各个促销是否满足
  349. foreach ($promos as $key => $promo) {
  350. if (!isset($promo['goods'])) {
  351. // 促销没有商品,直接 next
  352. continue;
  353. }
  354. $promoInfo = ActivityFacade::getPromoInfo($promo, [
  355. 'userAddress' => $this->userAddress
  356. ]);
  357. if ($promoInfo) {
  358. if (in_array($promo['type'], ['full_reduce', 'full_discount'])) {
  359. // 这里目前只计算,满减,满折
  360. $this->orderData['promo_discount_fee'] = bcadd($this->orderData['promo_discount_fee'], $promoInfo['promo_discount_money'], 2);
  361. // 将当前优惠平分到参与该促销的商品上
  362. foreach ($this->goodsList as &$buyInfo) {
  363. if (in_array($buyInfo['goods_id'], $promoInfo['goods_ids'])) {
  364. $scale = bcdiv($buyInfo['goods_amount'], $promoInfo['promo_goods_amount'], 6);
  365. // 当前商品,当前促销分配的优惠金额
  366. $current_promo_discount_fee = round(bcmul($promoInfo['promo_discount_money'], $scale, 3), 2);
  367. $buyInfo['promo_discount_fee'] = bcadd($buyInfo['promo_discount_fee'], $current_promo_discount_fee, 2); // 当前商品参与所有活动累计分配的优惠金额
  368. }
  369. }
  370. }
  371. if ($promo['type'] == 'free_shipping') {
  372. // 满包邮涉及到的商品
  373. $this->orderData['free_shipping_goods_ids'] = array_merge($this->orderData['free_shipping_goods_ids'], $promoInfo['goods_ids']);
  374. }
  375. $this->activity['promo_infos'][] = $promoInfo;
  376. }
  377. }
  378. // 计算实际应付总运费
  379. if ($this->orderData['free_shipping_goods_ids']) {
  380. // first:简单计算方式,谁包邮,就把 buyinfo 上的 dispatch_amount 计算为运费优惠
  381. $promo_dispatch_discount_fee = 0;
  382. foreach ($this->goodsList as $key => &$buyInfo) {
  383. if (in_array($buyInfo['goods_id'], $this->orderData['free_shipping_goods_ids'])) {
  384. // 商品运费优惠金额就是 当前商品的运费
  385. $buyInfo['dispatch_discount_fee'] = $buyInfo['dispatch_amount'];
  386. $promo_dispatch_discount_fee = bcadd($promo_dispatch_discount_fee, $buyInfo['dispatch_amount'], 2);
  387. }
  388. }
  389. // 真实运费 = 总运费,减去总包邮优惠
  390. $this->orderData['real_dispatch_amount'] = bcsub($this->orderData['dispatch_amount'], $promo_dispatch_discount_fee, 2);
  391. $this->orderData['promo_discount_fee'] = bcadd($this->orderData['promo_discount_fee'], $promo_dispatch_discount_fee, 2);
  392. // second:复杂计算方式,一个运费模板中多个商品只有一个包邮时,包邮优惠重新计算,而不是直接使用 dispatch_amount
  393. // foreach ($this->orderData['dispatch_infos'] as $key => $info) {
  394. // $finalExpress = $info['finalExpress'];
  395. // $total_buy_num = 0; // 总购买数量
  396. // $total_weight = 0; // 总购买重量
  397. // $buy_num = 0; // 不包邮购买数量
  398. // $weight = 0; // 不包邮购买重量
  399. // $currentGoodsIds = array_column($info['buyInfos'], 'goods_id');
  400. // $currentFreeGoodsIds = array_intersect($this->orderData['free_shipping_goods_ids'], $currentGoodsIds); // 当前配送模板中的包邮商品
  401. // $noFreeGoodsIds = array_diff($currentGoodsIds, $currentFreeGoodsIds); // 当前配送模板中的不包邮商品
  402. // foreach ($info['buyInfos'] as $k => $dispatchBuyInfo) {
  403. // $total_buy_num += $dispatchBuyInfo['goods_num'];
  404. // $total_weight = bcadd($total_weight, $dispatchBuyInfo['weight'], 2);
  405. // if (!in_array($dispatchBuyInfo['goods_id'], $this->orderData['free_shipping_goods_ids'])) {
  406. // // 不是包邮商品 累加
  407. // $buy_num += $dispatchBuyInfo['goods_num'];
  408. // $weight = bcadd($weight, $dispatchBuyInfo['weight'], 2);
  409. // }
  410. // }
  411. // if ($currentFreeGoodsIds && $noFreeGoodsIds) {
  412. // // 有免邮商品,也有非免邮商品,重新计算除包邮优惠后的运费
  413. // $current_dispatch_amount = $this->calcDispatch($finalExpress, [
  414. // 'goods_num' => $buy_num,
  415. // 'weight' => $weight
  416. // ]);
  417. // // 模板总运费优惠金额
  418. // $current_discount_dispatch_amount = bcsub($info['current_dispatch_amount'], $current_dispatch_amount, 2);
  419. // if ($current_discount_dispatch_amount > 0) {
  420. // // 将优惠金额按照运费模板平均分配
  421. // $this->equalDispatchDiscount($currentFreeGoodsIds, $current_discount_dispatch_amount, [
  422. // 'goods_num' => bcsub($total_buy_num, $buy_num), // 包邮购买数量
  423. // 'weight' => bcsub($total_weight, $weight, 2), // 包邮购买重量
  424. // 'final_express' => $finalExpress
  425. // ]);
  426. // // 将新的运费加权平均到每个 item 上
  427. // $this->equalDispatch($current_dispatch_amount, $noFreeGoodsIds, [
  428. // 'goods_num' => $buy_num,
  429. // 'weight' => $weight,
  430. // 'final_express' => $finalExpress
  431. // ]);
  432. // }
  433. // } elseif ($currentFreeGoodsIds) {
  434. // // 全部包邮了,将邮费优惠按件数或者重量分配了
  435. // $current_discount_dispatch_amount = $info['current_dispatch_amount'];
  436. // if ($current_discount_dispatch_amount > 0) {
  437. // // 每个商品运费优惠,就是 dispatch_amount
  438. // $this->allDispatchDiscount($currentFreeGoodsIds);
  439. // }
  440. // $current_dispatch_amount = 0;
  441. // } else {
  442. // // 全部不包邮,每个商品的 dispatch_discount_fee 为初始值,不需要处理每个商品分配到的运费优惠
  443. // $current_dispatch_amount = $info['current_dispatch_amount'];
  444. // }
  445. // $this->orderData['real_dispatch_amount'] = bcadd($this->orderData['real_dispatch_amount'], $current_dispatch_amount, 2);
  446. // }
  447. // // 包邮活动优惠的总费用
  448. // $promo_dispatch_discount_fee = bcsub($this->orderData['dispatch_amount'], $this->orderData['real_dispatch_amount'], 2);
  449. // $this->orderData['promo_discount_fee'] = bcadd($this->orderData['promo_discount_fee'], $promo_dispatch_discount_fee, 2);
  450. // second:复杂计算方式结束
  451. }
  452. // 将每个商品对应的 activity_type 放入 goods_list 中的 promo_types 中,重新计算真实的包邮优惠
  453. foreach ($this->activity['promo_infos'] as &$info) {
  454. // 寻找商品id 等于 $goods_id 的所有商品,存在同一个商品,购买多个规格的情况
  455. foreach ($this->goodsList as &$buyInfo) {
  456. if (in_array($buyInfo['goods_id'], $info['goods_ids'])) {
  457. if (!in_array($info['activity_type'], $buyInfo['promo_types'])) {
  458. $buyInfo['promo_types'][] = $info['activity_type'];
  459. }
  460. if ($info['activity_type'] == 'free_shipping') {
  461. // 重新计算运费真实优惠
  462. $info['promo_discount_money'] = bcadd($info['promo_discount_money'], $buyInfo['dispatch_discount_fee'], 2);
  463. }
  464. }
  465. }
  466. }
  467. }
  468. /**
  469. * 计算优惠券的优惠
  470. *
  471. * @return void
  472. */
  473. public function calcCoupon()
  474. {
  475. // 获取优惠券
  476. $this->orderData['coupon'] = $this->getCoupon($this->coupon_id);
  477. if ($this->orderData['coupon']) {
  478. $this->orderData['coupon_discount_fee'] = $this->orderData['coupon']['coupon_money']; // 获取优惠券时计算的金额
  479. $this->orderData['coupon_goods_ids'] = $this->orderData['coupon']['goods_ids']; // 获取优惠券时参与的商品
  480. // 将当前优惠券优惠金额平分到使用优惠券的商品上
  481. foreach ($this->goodsList as &$buyInfo) {
  482. if (in_array($buyInfo['goods_id'], $this->orderData['coupon_goods_ids'])) {
  483. $scale = bcdiv($buyInfo['goods_amount'], $this->orderData['coupon']['goods_amount'], 6);
  484. // 当前商品,当前活动分配的优惠金额
  485. $current_coupon_discount_fee = round(bcmul($this->orderData['coupon_discount_fee'], $scale, 3), 2);
  486. $buyInfo['coupon_discount_fee'] = bcadd($buyInfo['coupon_discount_fee'], $current_coupon_discount_fee, 2); // 当前商品可用优惠券的累计优惠金额
  487. }
  488. }
  489. } else {
  490. $this->exception('优惠券不可用');
  491. }
  492. }
  493. /**
  494. * 获取用户的优惠券列表,可用和不可用的做区分
  495. *
  496. * @return array
  497. */
  498. public function getCoupons($calc_type = 'coupons')
  499. {
  500. $this->calc_type = $calc_type;
  501. // 检查是否可下单
  502. $this->calcCheck();
  503. // 计算订单各种费用
  504. $this->calcAmount();
  505. // 用户可用优惠券列表
  506. // 这里使用的with,没法用 coupon表的减免金额做排序,可能需要重新写
  507. $coupons = Coupon::with('coupon')->where('user_id', $this->user->id)->canUse()->select();
  508. $cannot_use = [];
  509. $can_use = [];
  510. foreach ($coupons as $key => $coupon) {
  511. $result = $this->checkCoupon($coupon);
  512. if (isset($result['can_use']) && $result['can_use']) {
  513. $can_use[] = $coupon;
  514. } else {
  515. $coupon->cannot_use_msg = $result['cannot_use_msg'];
  516. $cannot_use[] = $coupon;
  517. }
  518. }
  519. return compact('can_use', 'cannot_use');
  520. }
  521. public function getCoupon($coupon_id)
  522. {
  523. // 选用的优惠券
  524. $coupon = Coupon::with('coupon')->where('user_id', $this->user->id)->canUse()->find($coupon_id);
  525. if ($coupon) {
  526. $result = $this->checkCoupon($coupon);
  527. return (isset($result['can_use']) && $result['can_use']) ? $coupon : null;
  528. }
  529. return null;
  530. }
  531. /**
  532. * 检测用户优惠券是否可用
  533. *
  534. * @param array|object $coupon
  535. * @param string $type
  536. * @return array|object
  537. */
  538. private function checkCoupon($coupon)
  539. {
  540. $can_use = true;
  541. $cannot_use_msg = '';
  542. // (积分或者活动 并且 优惠券不支持优惠叠加)
  543. if (($this->order_type == 'score' || $this->activity_id) && !$coupon->is_double_discount) {
  544. $can_use = false;
  545. $cannot_use_msg = '优惠券不可与活动叠加';
  546. return compact('can_use', 'cannot_use_msg');
  547. }
  548. $goods_amount = 0;
  549. $goodsIds = []; // 符合优惠券规则的 商品 ids
  550. if ($coupon->use_scope == 'all_use') {
  551. // 计算商品总金额
  552. foreach ($this->goodsList as $buyInfo) {
  553. $goods_amount = bcadd($goods_amount, $buyInfo['goods_amount'], 2);
  554. $goodsIds[] = $buyInfo['goods_id'];
  555. }
  556. } elseif ($coupon->use_scope == 'goods') {
  557. // 计算指定商品的总金额是否满足
  558. foreach ($this->goodsList as $buyInfo) {
  559. if (in_array($buyInfo['goods_id'], explode(',', $coupon->items))) {
  560. $goods_amount = bcadd($goods_amount, $buyInfo['goods_amount'], 2);
  561. $goodsIds[] = $buyInfo['goods_id'];
  562. }
  563. }
  564. } elseif ($coupon->use_scope == 'disabled_goods') {
  565. // 计算非指定的商品的总金额是否满足
  566. foreach ($this->goodsList as $buyInfo) {
  567. if (!in_array($buyInfo['goods_id'], explode(',', $coupon->items))) {
  568. $goods_amount = bcadd($goods_amount, $buyInfo['goods_amount'], 2);
  569. $goodsIds[] = $buyInfo['goods_id'];
  570. }
  571. }
  572. } elseif ($coupon->use_scope == 'category') {
  573. // 计算指定分类的商品的总金额
  574. foreach ($this->goodsList as $buyInfo) {
  575. // 取分类交集
  576. if (array_intersect(explode(',', $buyInfo['goods']['category_ids']), explode(',', $coupon->items))) {
  577. $goods_amount = bcadd($goods_amount, $buyInfo['goods_amount'], 2);
  578. $goodsIds[] = $buyInfo['goods_id'];
  579. }
  580. }
  581. }
  582. if ($coupon->enough <= $goods_amount) {
  583. $coupon['coupon_money'] = $coupon->amount;
  584. $coupon['goods_amount'] = $goods_amount; // 参与优惠券的商品的总金额
  585. $coupon['goods_ids'] = $goodsIds;
  586. if ($coupon->type == 'discount') {
  587. $scale = bcmul($coupon->amount, 0.1, 3);
  588. $scale = $scale > 1 ? 1 : ($scale < 0 ? 0 : $scale);
  589. $coupon_money = bcmul(bcsub(1, $scale, 3), $goods_amount, 2);
  590. // 优惠金额最大为 coupon->max_amount;
  591. $coupon['coupon_money'] = $coupon->max_amount && $coupon_money > $coupon->max_amount ? $coupon->max_amount : $coupon_money;
  592. }
  593. $coupons[] = $coupon;
  594. } else {
  595. $can_use = false;
  596. if ($goods_amount > 0) {
  597. $cannot_use_msg = '商品金额不满足优惠券门槛';
  598. } else {
  599. $cannot_use_msg = '优惠券不支持该商品';
  600. }
  601. }
  602. return compact('can_use', 'cannot_use_msg');
  603. }
  604. /**
  605. * 获取匹配的配送规则
  606. *
  607. * @param string $dispatch_type
  608. * @param integer $dispatch_id
  609. * @return array
  610. */
  611. public function getDispatchExpress($dispatch_type, $dispatch_id)
  612. {
  613. // 物流快递
  614. $dispatch = Dispatch::with('express')->show()->where('type', $dispatch_type)->where('id', $dispatch_id)->find();
  615. if (!$dispatch) {
  616. $this->exception('配送方式不存在', true);
  617. }
  618. $finalExpress = null;
  619. foreach ($dispatch->express as $key => $express) {
  620. if (strpos($express->district_ids, strval($this->userAddress->district_id)) !== false) {
  621. $finalExpress = $express;
  622. break;
  623. }
  624. if (strpos($express->city_ids, strval($this->userAddress->city_id)) !== false) {
  625. $finalExpress = $express;
  626. break;
  627. }
  628. if (strpos($express->province_ids, strval($this->userAddress->province_id)) !== false) {
  629. $finalExpress = $express;
  630. break;
  631. }
  632. }
  633. if (empty($finalExpress)) {
  634. $this->exception('当前地区不在配送范围');
  635. }
  636. return $finalExpress;
  637. }
  638. /**
  639. * 获取匹配的配送规则
  640. *
  641. * @param string $dispatch_type
  642. * @param integer $dispatch_id
  643. * @return array
  644. */
  645. public function getDispatchAutosend($dispatch_type, $dispatch_id)
  646. {
  647. // 物流快递
  648. $dispatch = Dispatch::with(['autosend'])->show()->where('type', $dispatch_type)->where('id', $dispatch_id)->find();
  649. if (!$dispatch) {
  650. $this->exception('配送方式不存在', true);
  651. }
  652. return $dispatch;
  653. }
  654. public function calcDispatch($finalExpress, $data)
  655. {
  656. if (empty($finalExpress)) {
  657. // 没有找到 finalExpress,比如商品不在配送范围,直接返回 0
  658. return 0;
  659. }
  660. // 初始费用
  661. $dispatch_amount = $finalExpress->first_price;
  662. if ($finalExpress['type'] == 'number') {
  663. // 按件计算
  664. if ($finalExpress->additional_num && $finalExpress->additional_price) {
  665. // 首件之后剩余件数
  666. $surplus_num = $data['goods_num'] - $finalExpress->first_num;
  667. // 多出的计量
  668. $additional_mul = ceil(($surplus_num / $finalExpress->additional_num));
  669. if ($additional_mul > 0) {
  670. $additional_dispatch_amount = bcmul($additional_mul, $finalExpress->additional_price, 2);
  671. $dispatch_amount = bcadd($dispatch_amount, $additional_dispatch_amount, 2);
  672. }
  673. }
  674. } else {
  675. // 按重量计算
  676. if ($finalExpress->additional_num && $finalExpress->additional_price) {
  677. // 首重之后剩余重量
  678. $surplus_num = $data['weight'] - $finalExpress->first_num;
  679. // 多出的计量
  680. $additional_mul = ceil(($surplus_num / $finalExpress->additional_num));
  681. if ($additional_mul > 0) {
  682. $additional_dispatch_amount = bcmul($additional_mul, $finalExpress->additional_price, 2);
  683. $dispatch_amount = bcadd($dispatch_amount, $additional_dispatch_amount, 2);
  684. }
  685. }
  686. }
  687. return $dispatch_amount;
  688. }
  689. /**
  690. * 加权平均每个包邮商品,应该分配的优惠(一个运费模板中的商品,部分包邮,部分不包邮,要重新计算)
  691. *
  692. * @param [type] $currentFreeGoodsIds
  693. * @param [type] $current_dispatch_amount
  694. * @param array $data
  695. * @return void
  696. */
  697. public function equalDispatchDiscount($currentFreeGoodsIds, $dispatch_discount_amount, $data = [])
  698. {
  699. $goods_num = $data['goods_num'];
  700. $weight = $data['weight'];
  701. $finalExpress = $data['final_express']; // 这里肯定有,否则走不到这个方法
  702. foreach ($this->goodsList as $key => &$buyInfo) {
  703. if (in_array($buyInfo['goods_id'], $currentFreeGoodsIds)) {
  704. $scale = 0; // 重量或者数量计算比例
  705. if ($finalExpress['type'] == 'number') {
  706. // 按件
  707. if (floatval($goods_num)) { // 字符串 0.00 是 true, 这里转下类型在判断
  708. $scale = bcdiv($buyInfo['goods_num'], $goods_num, 6);
  709. }
  710. $current_dispatch_discount_fee = round(bcmul($dispatch_discount_amount, $scale, 3), 2);
  711. } else {
  712. // 按重量
  713. if (floatval($weight)) {
  714. $current_weight = bcmul($buyInfo['current_sku_price']->weight, $buyInfo['goods_num'], 2);
  715. $scale = bcdiv($current_weight, $weight, 6);
  716. }
  717. $current_dispatch_discount_fee = round(bcmul($dispatch_discount_amount, $scale, 3), 2);
  718. }
  719. // 每个商品分配到的包邮优惠金额, 和订单 item 上的dispatch_fee 不一样,因为 剩余运费又重新计算了,这个优惠没有 dispatch_fee 大
  720. $buyInfo['dispatch_discount_fee'] = $current_dispatch_discount_fee;
  721. }
  722. }
  723. }
  724. /**
  725. * 同一运费模板额所有商品都包邮了,不需要重新计算,优惠金额就是 dispatch_amount
  726. *
  727. * @param [type] $currentFreeGoodsIds
  728. * @param [type] $current_dispatch_amount
  729. * @param array $data
  730. * @return void
  731. */
  732. public function allDispatchDiscount($currentFreeGoodsIds)
  733. {
  734. foreach ($this->goodsList as $key => &$buyInfo) {
  735. if (in_array($buyInfo['goods_id'], $currentFreeGoodsIds)) {
  736. // 每个商品分配到的包邮优惠金额, 和订单 item 上的dispatch_fee 不一样,因为 剩余运费又重新计算了,这个优惠没有 dispatch_fee 大
  737. $buyInfo['dispatch_discount_fee'] = $buyInfo['dispatch_amount'];
  738. }
  739. }
  740. }
  741. /**
  742. * 加权平分同一运费模板下商品实际应付运费
  743. *
  744. * @param float $dispatch_amount
  745. * @param array $currentBuyInfos
  746. * @param array $data
  747. * @return void
  748. */
  749. private function equalDispatch($dispatch_amount, $currentBuyInfos, $data)
  750. {
  751. $goods_num = $data['goods_num'];
  752. $weight = $data['weight'];
  753. $finalExpress = $data['final_express'];
  754. // 当前运费模板中的商品 Ids
  755. $goodsIds = array_column($currentBuyInfos, 'goods_id');
  756. foreach ($this->goodsList as $key => &$buyInfo) {
  757. if (in_array($buyInfo['goods_id'], $goodsIds)) {
  758. $scale = 0; // 重量或者数量计算比例
  759. if ($finalExpress['type'] == 'number') {
  760. // 按件
  761. if ($goods_num) { // 字符串 0.00 是 true, 这里转下类型在判断
  762. $scale = bcdiv($buyInfo['goods_num'], $goods_num, 6);
  763. }
  764. $current_dispatch_amount = round(bcmul($dispatch_amount, $scale, 3), 2);
  765. } else {
  766. // 按重量
  767. if (floatval($weight)) {
  768. $scale = bcdiv($buyInfo['weight'], $weight, 6);
  769. }
  770. $current_dispatch_amount = round(bcmul($dispatch_amount, $scale, 3), 2);
  771. }
  772. $buyInfo['dispatch_amount'] = $current_dispatch_amount; // 每个商品分配到的实际应支付运费
  773. }
  774. }
  775. }
  776. public function calcReturn() {
  777. $invoiceConfig = sheep_config('shop.order.invoice');
  778. $this->invoiceConfig = [
  779. 'status' => $invoiceConfig['status'] && $this->orderData['pay_fee'] ? 1 : 0, // 可开具发票状态,
  780. 'amount_type' => $invoiceConfig['amount_type'] ?? 'pay_fee',
  781. 'user_invoice' => null
  782. ];
  783. if ($this->invoiceConfig['status'] && $this->invoice_id) {
  784. // 获取用户发票信息
  785. $this->invoiceConfig['user_invoice'] = UserInvoice::where('user_id', $this->user->id)->find($this->invoice_id);
  786. if (!$this->invoiceConfig['user_invoice']) {
  787. $this->exception('请选择正确的发票信息');
  788. }
  789. }
  790. $temp_remain_pay_fee = bcsub($this->orderData['pay_fee'], $this->money, 2);
  791. $result = [
  792. 'goods_original_amount' => $this->orderData['goods_original_amount'],
  793. 'goods_old_amount' => $this->orderData['goods_old_amount'],
  794. 'goods_amount' => $this->orderData['goods_amount'],
  795. 'dispatch_amount' => $this->orderData['dispatch_amount'],
  796. 'real_dispatch_amount' => $this->orderData['real_dispatch_amount'],
  797. 'order_amount' => $this->orderData['order_amount'],
  798. 'pay_fee' => $this->orderData['pay_fee'],
  799. 'temp_remain_pay_fee' => $temp_remain_pay_fee >= 0 ? $temp_remain_pay_fee : 0, // 临时剩余应支付金额,订单确认页面显示
  800. 'total_discount_fee' => $this->orderData['total_discount_fee'],
  801. 'coupon_discount_fee' => $this->orderData['coupon_discount_fee'],
  802. 'promo_discount_fee' => $this->orderData['promo_discount_fee'], // 包含包邮的运费优惠
  803. 'money' => $this->money, // 余额支付部分
  804. "invoice_amount" => $this->orderData[$this->invoiceConfig['amount_type']] // 可开票金额
  805. ];
  806. // 处理小数点保留两位小数
  807. foreach ($result as $key => $amount) {
  808. $result[$key] = number_format($amount, 2, '.', '');
  809. }
  810. // 合并不需要处理小数点的
  811. $result = array_merge($result, [
  812. 'activity_id' => $this->activity['activity']['id'] ?? 0,
  813. 'activity_type' => $this->activity['activity']['type'] ?? null,
  814. 'activity_title' => $this->activity['activity']['title'] ?? null,
  815. 'promo_types' => array_column($this->activity['promo_infos'], 'activity_type'),
  816. 'score_amount' => $this->orderData['score_amount'],
  817. 'goods_list' => $this->goodsList,
  818. 'promo_infos' => $this->activity['promo_infos'],
  819. "invoice_status" => $this->invoiceConfig['status'], // 发票可开状态
  820. "invoice_amount_type" => $this->invoiceConfig['amount_type'], // 发票金额类型
  821. "need_address" => $this->need_address,
  822. "offline_status" => $this->offline_status,
  823. ]);
  824. // 如果是下单,合并 优惠券, 收货地址
  825. if ($this->calc_type == 'create') {
  826. $result = array_merge($result, [
  827. "coupon" => $this->orderData['coupon'],
  828. "user_address" => $this->userAddress
  829. ]);
  830. } else {
  831. $result = array_merge($result, ['msg' => $this->msg]);
  832. }
  833. return $result;
  834. }
  835. /**
  836. * 计算订单
  837. *
  838. * @return void
  839. */
  840. public function calc($calc_type = 'calc')
  841. {
  842. $this->calc_type = $calc_type;
  843. // 检查系统必要条件
  844. check_env(['bcmath', 'queue']);
  845. // 检查是否可下单
  846. $this->calcCheck();
  847. // 计算订单各种费用
  848. $this->calcAmount();
  849. // 计算订单各种优惠(promo 等)
  850. if ($this->isCalcPromos()) { // 判断是否参与优惠
  851. $this->calcDiscount();
  852. }
  853. // 计算优惠券费用
  854. if ($this->coupon_id) {
  855. $this->calcCoupon();
  856. }
  857. // 订单应付金额
  858. $this->orderData['order_amount'] = bcadd($this->orderData['goods_amount'], $this->orderData['dispatch_amount'], 2);
  859. $this->orderData['total_discount_fee'] = bcadd($this->orderData['coupon_discount_fee'], $this->orderData['promo_discount_fee'], 2);
  860. $this->orderData['pay_fee'] = bcsub($this->orderData['order_amount'], $this->orderData['total_discount_fee'], 2);
  861. $this->orderData['pay_fee'] = $this->orderData['pay_fee'] < 0 ? 0 : $this->orderData['pay_fee'];
  862. return $this->calcReturn();
  863. }
  864. public function create($result)
  865. {
  866. $stockSale = new StockSale();
  867. try {
  868. // 如果失败,被扣除的库存,退回
  869. $order = $this->createOrder($result);
  870. $stockSale->successDelHashKey();
  871. } catch (\Exception $e) {
  872. $message = $e->getMessage();
  873. if ($e instanceof \think\exception\HttpResponseException) {
  874. $data = $e->getResponse()->getData();
  875. $message = $data ? ($data['msg'] ?? '') : $e->getMessage();
  876. }
  877. // 下单失败,检测并返还锁定的库存
  878. $stockSale->faildStockUnLock();
  879. error_stop($message);
  880. }
  881. // 重新获取订单
  882. $order = Order::with('items')->where('id', $order->id)->find();
  883. $pay_money = floatval($result['money']);
  884. if ($order->status == Order::STATUS_UNPAID && $pay_money > 0) {
  885. // 如果订单未支付,并且使用了余额混合支付 (支付失败,不影响订单状态)
  886. $order = Db::transaction(function () use ($order, $pay_money) {
  887. $payOper = new PayOper();
  888. // 余额支付,并判断是否直接支付成功了
  889. $order = $payOper->money($order, $pay_money, 'order');
  890. return $order;
  891. });
  892. }
  893. return $order;
  894. }
  895. private function createOrder($result)
  896. {
  897. $order = Db::transaction(function () use ($result) {
  898. $orderData['type'] = $this->order_type;
  899. $orderData['order_sn'] = get_sn($this->user->id);
  900. $orderData['user_id'] = $this->user->id;
  901. $orderData['activity_id'] = $result['activity_id'];
  902. $orderData['activity_type'] = $result['activity_type'];
  903. $orderData['promo_types'] = join(',', $result['promo_types']);
  904. $orderData['goods_original_amount'] = $result['goods_original_amount'];
  905. $orderData['goods_amount'] = $result['goods_amount'];
  906. $orderData['dispatch_amount'] = $result['dispatch_amount'];
  907. $orderData['remark'] = $this->remark;
  908. $orderData['order_amount'] = $result['order_amount'];
  909. $orderData['score_amount'] = $result['score_amount'];
  910. $orderData['pay_fee'] = $result['pay_fee'];
  911. $orderData['original_pay_fee'] = $result['pay_fee']; // 记录支付总金额,后面可能会订单改价
  912. $orderData['remain_pay_fee'] = $result['pay_fee']; // 这里还是显示应支付总金额,扣除动作放到实际支付时
  913. $orderData['total_discount_fee'] = $result['total_discount_fee'];
  914. $orderData['coupon_discount_fee'] = $result['coupon_discount_fee'];
  915. $orderData['promo_discount_fee'] = $result['promo_discount_fee'];
  916. $orderData['coupon_id'] = $result['coupon'] ? $result['coupon']['id'] : 0;
  917. $orderData['status'] = Order::STATUS_UNPAID;
  918. $orderData['platform'] = request()->header('platform');
  919. //直播间id
  920. $orderData['room_id'] = 0;
  921. $orderData['room_log_id'] = 0;
  922. $room_log_id = request()->param('room_log_id',0);
  923. if($room_log_id){
  924. $room_id = Db::name('live_room_log')->where('id',$room_log_id)->value('room_id');
  925. if($room_id){
  926. $orderData['room_id'] = $room_id;
  927. $orderData['room_log_id'] = $room_log_id;
  928. }
  929. }
  930. $ext = $result['promo_infos'] ? ['promo_infos' => $result['promo_infos']] : []; // 促销活动信息
  931. if ($this->activity['activity']) {
  932. $rules = $this->activity['activity']['rules'];
  933. $ext['refund_type'] = $rules['refund_type'] ?? 'back'; // 退款方式,(目前主要针对拼团失败自动退款)
  934. }
  935. $ext['real_dispatch_amount'] = $result['real_dispatch_amount']; // 真实运费,剪掉运费优惠金额
  936. $goods_discount_amount = bcsub($result['goods_old_amount'], $result['goods_amount'], 2); // 如果参与活动时商品总价的优惠费用
  937. $ext['activity_discount_amount'] = $goods_discount_amount > 0 ? $goods_discount_amount : 0; // 如果参与活动时的优惠费用
  938. if ($result['activity_id']) {
  939. $ext['activity_type'] = $result['activity_type'];
  940. $ext['activity_title'] = $result['activity_title'];
  941. }
  942. $ext['offline_status'] = $result['offline_status'];
  943. $ext['need_address'] = $result['need_address']; // 后台判断是否有 编辑收货地址按钮
  944. $orderData['ext'] = $ext;
  945. // 发票开具状态
  946. $invoice_status = 0; // 没有申请
  947. if ($result['invoice_status'] && $this->invoice_id) {
  948. // 可开具,并且申请了
  949. $invoice_status = 1;
  950. } else if ($result['invoice_status'] == 0) {
  951. // 不可开具
  952. $invoice_status = -1;
  953. }
  954. $orderData['invoice_status'] = $invoice_status; // 可开具发票,并且申请了
  955. // 订单创建前
  956. $hookData = [
  957. 'params' => $result,
  958. 'order_data' => $orderData
  959. ];
  960. \think\Hook::listen('order_create_before', $hookData);
  961. $order = new Order();
  962. $order->save($orderData);
  963. // 添加收货地址信息
  964. if ($result['user_address']) {
  965. $this->createOrderAddress($order, $result);
  966. }
  967. // 添加发票信息
  968. if ($invoice_status == 1) {
  969. $this->createOrderInvoice($order, $result);
  970. }
  971. // 将优惠券使用掉
  972. if ($result['coupon']) {
  973. $result['coupon']->use_order_id = $order->id;
  974. $result['coupon']->use_time = time();
  975. $result['coupon']->allowField(true)->save();
  976. }
  977. // 添加 订单 item
  978. foreach ($result['goods_list'] as $key => $buyInfo) {
  979. $goods = $buyInfo['goods'];
  980. $current_sku_price = $buyInfo['current_sku_price'];
  981. $orderItem = new OrderItem();
  982. $orderItem->order_id = $order->id;
  983. $orderItem->user_id = $this->user->id;
  984. $orderItem->goods_id = $buyInfo['goods_id'];
  985. $orderItem->goods_type = $goods['type'];
  986. $orderItem->goods_sku_price_id = $buyInfo['goods_sku_price_id'];
  987. $orderItem->activity_id = $result['activity_id']; // 商品当前参与的活动 id
  988. $orderItem->activity_type = $result['activity_type']; // 商品当前的活动类型
  989. $orderItem->promo_types = join(',', $buyInfo['promo_types']); // 商品当前的活动类型
  990. // 当前商品规格对应的 活动下对应商品规格的 id
  991. $orderItem->item_goods_sku_price_id = isset($current_sku_price['item_goods_sku_price']) ?
  992. $current_sku_price['item_goods_sku_price']['id'] : 0;
  993. $orderItem->goods_sku_text = is_array($current_sku_price->goods_sku_text) ? join(',', $current_sku_price->goods_sku_text) : '';
  994. $orderItem->goods_title = $goods->title;
  995. $orderItem->goods_image = empty($current_sku_price->image) ? $goods->image : $current_sku_price->image;
  996. $orderItem->goods_original_price = $goods->original_price;
  997. $orderItem->goods_price = $current_sku_price->price;
  998. $orderItem->goods_num = $buyInfo['goods_num'] ?? 1;
  999. $orderItem->goods_weight = $current_sku_price->weight;
  1000. $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)
  1001. $pay_fee = bcsub($buyInfo['goods_amount'], bcadd($buyInfo['promo_discount_fee'], $buyInfo['coupon_discount_fee'], 2), 2); // 商品总金额(不算运费),减去(活动优惠(不包含包邮优惠) + 优惠券优惠)
  1002. $orderItem->pay_fee = $pay_fee; // 平均计算单件商品不算运费,算折扣时候的金额
  1003. $orderItem->dispatch_status = 0;
  1004. $orderItem->dispatch_fee = $buyInfo['dispatch_amount']; // 运费模板中商品自动合并后的加权平均运费,没有扣除包邮优惠
  1005. $orderItem->dispatch_type = $buyInfo['dispatch_type'];
  1006. $orderItem->dispatch_id = $buyInfo['dispatch_id'] ? $buyInfo['dispatch_id'] : 0;
  1007. $orderItem->aftersale_status = 0;
  1008. $orderItem->comment_status = 0;
  1009. $orderItem->refund_status = 0;
  1010. $orderItem->room_id = $orderData['room_id'];
  1011. $orderItem->room_log_id = $orderData['room_log_id'];
  1012. $ext = [
  1013. 'original_dispatch_amount' => $buyInfo['original_dispatch_amount'], // 原始运费总金额(未判断活动的,并且也未合并相同运费模板商品的原始运费)
  1014. 'promo_discount_fee' => bcadd($buyInfo['promo_discount_fee'], $buyInfo['dispatch_discount_fee'], 2), // 促销优惠,包含满包邮
  1015. 'dispatch_discount_fee' => $buyInfo['dispatch_discount_fee'], // 包邮优惠,已经包含在 promo_discount_fee
  1016. 'coupon_discount_fee' => $buyInfo['coupon_discount_fee'], // 优惠券优惠
  1017. 'activity_sku_price_ext' => $current_sku_price['ext'] ?? [], // 活动规格 ext,保存备用
  1018. 'ladder' => $buyInfo['ladder'] ?? [], // (阶梯拼团仅有)阶梯拼团,当前购买的阶梯
  1019. ];
  1020. if (isset($buyInfo['is_commission'])) {
  1021. $ext['is_commission'] = $buyInfo['is_commission'];
  1022. }
  1023. $orderItem->ext = $ext;
  1024. $orderItem->save();
  1025. }
  1026. // 订单创建后
  1027. $hookData = [
  1028. 'order' => $order,
  1029. 'activity' => $this->activity['activity'],
  1030. ];
  1031. \think\Hook::listen('order_create_after', $hookData);
  1032. return $order;
  1033. });
  1034. return $order;
  1035. }
  1036. /**
  1037. * 添加收货地址信息
  1038. *
  1039. * @param \think\Model $order
  1040. * @param array $result
  1041. * @return void
  1042. */
  1043. private function createOrderAddress($order, $result)
  1044. {
  1045. // 保存收货地址
  1046. $orderAddress = new OrderAddress();
  1047. $orderAddress->order_id = $order->id;
  1048. $orderAddress->user_id = $this->user->id;
  1049. $orderAddress->consignee = $result['user_address']->consignee;
  1050. $orderAddress->mobile = $result['user_address']->mobile;
  1051. $orderAddress->province_name = $result['user_address']->province_name;
  1052. $orderAddress->city_name = $result['user_address']->city_name;
  1053. $orderAddress->district_name = $result['user_address']->district_name;
  1054. $orderAddress->address = $result['user_address']->address;
  1055. $orderAddress->province_id = $result['user_address']->province_id;
  1056. $orderAddress->city_id = $result['user_address']->city_id;
  1057. $orderAddress->district_id = $result['user_address']->district_id;
  1058. $orderAddress->save();
  1059. }
  1060. /**
  1061. * 添加发票信息
  1062. *
  1063. * @param \think\Model $order
  1064. * @param array $result
  1065. * @return void
  1066. */
  1067. private function createOrderInvoice($order, $result)
  1068. {
  1069. $userInvoice = $this->invoiceConfig['user_invoice'];
  1070. // 保存收货地址
  1071. $orderInvoice = new OrderInvoice();
  1072. $orderInvoice->type = $userInvoice->type;
  1073. $orderInvoice->order_id = $order->id;
  1074. $orderInvoice->user_id = $userInvoice->user_id;
  1075. $orderInvoice->name = $userInvoice->name;
  1076. $orderInvoice->tax_no = $userInvoice->tax_no;
  1077. $orderInvoice->address = $userInvoice->address;
  1078. $orderInvoice->mobile = $userInvoice->mobile;
  1079. $orderInvoice->bank_name = $userInvoice->bank_name;
  1080. $orderInvoice->bank_no = $userInvoice->bank_no;
  1081. $orderInvoice->amount = $result['invoice_amount'];
  1082. $orderInvoice->status = 'unpaid';
  1083. $orderInvoice->save();
  1084. }
  1085. /**
  1086. * 判断并处理异常
  1087. *
  1088. * @param string $msg 处理结果
  1089. * @param boolean $force 是否强制抛出异常
  1090. * @return boolean
  1091. */
  1092. private function exception($msg, $force = false)
  1093. {
  1094. if ($this->calc_type == 'create' || $force) {
  1095. // 如果是创建订单,或者强制抛出异常
  1096. error_stop($msg);
  1097. } else {
  1098. // 预下单,记录第一条错误信息
  1099. if (!$this->msg) {
  1100. $this->msg = $msg;
  1101. }
  1102. }
  1103. return false;
  1104. }
  1105. /**
  1106. * 获取商品服务实例
  1107. *
  1108. * @return GoodsService
  1109. */
  1110. private function getGoodsService()
  1111. {
  1112. // 实例化商品服务
  1113. return new GoodsService(function ($goods, $service) {
  1114. // $goods->sku_prices;
  1115. // 这个写法没用,只要判断 promos 还是会获取 promos
  1116. // if (!$this->activity_id) {
  1117. // // 可能要参与的营销活动
  1118. // $goods->promos = $goods->promos;
  1119. // }
  1120. return $goods;
  1121. });
  1122. }
  1123. /**
  1124. * 是否计算促销
  1125. *
  1126. * @return bool
  1127. */
  1128. private function isCalcPromos()
  1129. {
  1130. if ($this->order_type == 'score') {
  1131. return false; // 积分商品不能参与促销
  1132. } else if ($this->activity_id) {
  1133. if ($this->activity_promos) {
  1134. // 活动与促销共存
  1135. return true;
  1136. } else {
  1137. // 参与活动,不能在参与促销
  1138. return false;
  1139. }
  1140. } else {
  1141. return true;
  1142. }
  1143. }
  1144. }