OrderCreate.php 62 KB

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