OrderService.php 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585
  1. <?php
  2. namespace bizHd\order\services;
  3. use bizGhs\item\classes\ItemClass;
  4. use bizHd\base\services\BaseService;
  5. use biz\sj\services\SjCapitalService;
  6. use biz\sj\services\MerchantService;
  7. use bizHd\custom\classes\CustomClass;
  8. use bizHd\goods\classes\GoodsClass;
  9. use bizHd\hb\classes\HbClass;
  10. use bizHd\order\classes\OrderClass;
  11. use bizHd\order\classes\OrderGoodsClass;
  12. use bizHd\order\classes\OrderItemClass;
  13. use bizHd\promote\services\CouponService;
  14. use bizHd\user\classes\UserAssetClass;
  15. use common\components\dict;
  16. use common\components\imgUtil;
  17. use common\components\orderSn;
  18. use common\components\stringUtil;
  19. use common\components\util;
  20. use Yii;
  21. class OrderService extends BaseService
  22. {
  23. public static $baseFile = '\bizHd\order\classes\OrderClass';
  24. public static function createHdOrder($data, $custom, $hasPay = 1)
  25. {
  26. $payWay = $data['payWay'] ?? 0;
  27. $orderSn = orderSn::getOrderSn();
  28. $customId = $custom->id ?? 0;
  29. $hdId = $custom->hdId ?? 0;
  30. $userId = $custom->userId ?? 0;
  31. $sjId = $data['sjId'] ?? 0;
  32. $shopId = $data['shopId'] ?? 0;
  33. $mainId = $data['mainId'] ?? 0;
  34. $dj = $data['dj'] ?? 0;
  35. if ($dj > 0) {
  36. util::fail('请去掉订金,不再支持订金方式');
  37. }
  38. $product = $data['product'] ?? [];
  39. //组合
  40. $groupId = $data['groupId'] ?? 0;
  41. if (!empty($groupId)) {
  42. util::fail('暂不支持使用组合功能');
  43. }
  44. if (empty($product)) {
  45. util::fail('没有商品');
  46. }
  47. //如果开单没有给预订人和预订人手机号,则给设置
  48. if (empty($data['bookMobile'])) {
  49. $data['bookMobile'] = $custom->mobile;
  50. }
  51. if (empty($data['bookName'])) {
  52. $data['bookName'] = $custom->name;
  53. }
  54. //如果开单没有传收花人信息,则使用订花人的信息
  55. if (empty($data['receiveUserName'])) {
  56. $data['receiveUserName'] = $custom->name;
  57. }
  58. if (empty($data['receiveMobile'])) {
  59. $data['receiveMobile'] = $custom->mobile;
  60. }
  61. if (empty($data['address'])) {
  62. $data['address'] = $custom->address;
  63. $data['province'] = $custom->province;
  64. $data['city'] = $custom->city;
  65. $data['floor'] = $custom->floor;
  66. $data['fullAddress'] = $custom->fullAddress;
  67. $data['showAddress'] = $custom->showAddress;
  68. $data['long'] = $custom->long;
  69. $data['lat'] = $custom->lat;
  70. }
  71. $currentGoodsIds = [];
  72. $currentItemIds = [];
  73. foreach ($product as $key => $val) {
  74. $currentId = $val['productId'] ?? 0;
  75. $property = $val['property'];
  76. if ($property == dict::getDict('property', 'goods')) {
  77. $currentGoodsIds[] = $currentId;
  78. }
  79. if ($property == dict::getDict('property', 'item')) {
  80. $currentItemIds[] = $currentId;
  81. }
  82. }
  83. $goodsInfo = [];
  84. $currentGoodsIds = array_unique(array_filter($currentGoodsIds));
  85. if (!empty($currentGoodsIds)) {
  86. $goodsInfo = GoodsClass::getByIds($currentGoodsIds, null, 'id');
  87. }
  88. $itemInfo = [];
  89. $currentItemIds = array_unique(array_filter($currentItemIds));
  90. if (!empty($currentItemIds)) {
  91. $itemInfo = ItemClass::getByIds($currentItemIds, null, 'id');
  92. }
  93. $goodsPrice = 0;
  94. $manyType = count($product) > 1 ? 1 : 0;
  95. $totalWeight = 0;
  96. $orderCover = '';
  97. foreach ($product as $key => $val) {
  98. if (!isset($val['property'])) {
  99. util::fail('没有找到产品属性');
  100. }
  101. $property = $val['property'];
  102. $unitPrice = $val['unitPrice'] ?? 0;
  103. $unitType = $val['unitType'] ?? 0;
  104. $currentId = $val['productId'] ?? 0;
  105. $num = $val['num'] ?? 0;
  106. //修改订单会出现数量为0的情况
  107. if ($num <= 0) {
  108. continue;
  109. }
  110. $currentPrice = bcmul($unitPrice, $num, 2);
  111. $goodsPrice = bcadd($goodsPrice, $currentPrice, 2);
  112. if ($property == dict::getDict('property', 'goods')) {
  113. $currentGoodsInfo = $goodsInfo[$currentId] ?? [];
  114. if (empty($currentGoodsInfo)) {
  115. util::fail("花束商品没有找到,编号{$currentId}");
  116. }
  117. $name = $currentGoodsInfo['name'] ?? '';
  118. $cover = $currentGoodsInfo['cover'] ?? '';
  119. $flower = $currentGoodsInfo['flower'] ?? 0;
  120. $kindId = $currentGoodsInfo['kindId'] ?? 0;
  121. $kindName = $currentGoodsInfo['kindName'] ?? 0;
  122. $sn = $currentGoodsInfo['sn'] ?? '';
  123. if (empty($orderCover)) {
  124. $orderCover = $cover;
  125. }
  126. $currentMainId = $currentGoodsInfo['mainId'] ?? 0;
  127. if ($currentMainId != $mainId) {
  128. util::fail('不是您的商品');
  129. }
  130. $weight = $currentGoodsInfo['weight'] ?? 0;
  131. $currentWeight = bcmul($weight, $num, 2);
  132. $totalWeight = bcadd($totalWeight, $currentWeight, 2);
  133. $currentGoodsData = [
  134. 'sn' => $sn,
  135. 'orderSn' => $orderSn,
  136. 'goodsId' => $currentId,
  137. 'seckillGoodsId' => intval($val['seckillGoodsId'] ?? 0),
  138. 'flower' => $flower,
  139. 'customId' => $customId,
  140. 'hdId' => $hdId,
  141. 'sjId' => $sjId,
  142. 'shopId' => $shopId,
  143. 'name' => $name,
  144. 'cover' => $cover,
  145. 'unitPrice' => $unitPrice,
  146. 'num' => $num,
  147. 'price' => bcmul($unitPrice, $num, 2),
  148. 'mainId' => $mainId,
  149. 'kindId' => $kindId,
  150. 'kindName' => $kindName,
  151. ];
  152. //占用库存在这里
  153. OrderGoodsClass::addData($currentGoodsData, $custom, $goodsInfo);
  154. }
  155. if ($property == dict::getDict('property', 'item')) {
  156. $currentInfo = $itemInfo[$currentId] ?? [];
  157. $name = $currentInfo['name'] ?? '';
  158. $cover = $currentInfo['cover'] ?? '';
  159. if (empty($orderCover)) {
  160. $orderCover = $cover;
  161. }
  162. $ratio = $currentInfo['ratio'] ?? 20;
  163. $currentMainId = $currentInfo['mainId'] ?? 0;
  164. if ($currentMainId != $mainId) {
  165. util::fail('不是您的花材' . $mainId . ' ' . $currentMainId);
  166. }
  167. $weight = $currentInfo['weight'] ?? 0;
  168. $currentWeight = bcmul($weight, $num, 2);
  169. $totalWeight = bcadd($totalWeight, $currentWeight, 2);
  170. if ($unitType == dict::getDict('unitType', 'big')) {
  171. $unitName = $currentInfo['bigUnit'] ?? '';
  172. $unitId = $currentInfo['bigUnitId'] ?? 0;
  173. $totalNum = $num;
  174. } else {
  175. $unitName = $currentInfo['smallUnit'] ?? '';
  176. $unitId = $currentInfo['smallUnitId'] ?? 0;
  177. $totalNum = bcdiv($num, $ratio, 2);
  178. }
  179. $ptItemId = $currentInfo['itemId'] ?? 0;
  180. $classId = $currentInfo['classId'] ?? 0;
  181. $belongCost = $currentInfo['belongCost'] ?? 0;
  182. $currentItemData = [
  183. 'name' => $name,
  184. 'cover' => $cover,
  185. 'ratio' => $ratio,
  186. 'unitType' => $unitType,
  187. 'unitName' => $unitName,
  188. 'unitId' => $unitId,
  189. 'unitPrice' => $unitPrice,
  190. 'num' => $num,
  191. 'orderSn' => $orderSn,
  192. 'itemId' => $currentId,
  193. 'ptItemId' => $ptItemId,
  194. 'totalNum' => $totalNum,
  195. 'totalPrice' => 0,
  196. 'customId' => $customId,
  197. 'hdId' => $hdId,
  198. 'sjId' => $sjId,
  199. 'shopId' => $shopId,
  200. 'mainId' => $mainId,
  201. 'classId' => $classId,
  202. 'price' => bcmul($unitPrice, $num, 2),
  203. 'belongCost' => $belongCost,
  204. ];
  205. //库存变化在这里
  206. OrderItemClass::addData($currentItemData, $custom);
  207. }
  208. }
  209. //运费
  210. $data['sendCost'] = isset($data['sendCost']) && $data['sendCost'] > 0 ? $data['sendCost'] : 0;
  211. $totalPrice = bcadd($goodsPrice, $data['sendCost'], 2);
  212. //人工资材包装费
  213. $labourCost = isset($data['labourCost']) && $data['labourCost'] > 0 ? $data['labourCost'] : 0;
  214. $totalPrice = bcadd($totalPrice, $labourCost, 2);
  215. //打包费
  216. $packingFee = isset($data['packingFee']) && $data['packingFee'] > 0 ? $data['packingFee'] : 0;
  217. $totalPrice = bcadd($totalPrice, $packingFee, 2);
  218. //美团服务费和手续费
  219. $serviceFee = isset($data['serviceFee']) && $data['serviceFee'] > 0 ? $data['serviceFee'] : 0;
  220. $totalPrice = bcadd($totalPrice, $serviceFee, 2);
  221. //扣除红包的减免;拼团单不使用红包,避免团购价再被抵扣
  222. $isGroupBuyOrder = intval($data['groupBuyId'] ?? 0) > 0;
  223. $hbAmount = isset($data['hbAmount']) && $data['hbAmount'] > 0 ? $data['hbAmount'] : 0;
  224. if ($isGroupBuyOrder) {
  225. $hbAmount = 0;
  226. $data['hbAmount'] = 0;
  227. $data['hbId'] = 0;
  228. }
  229. $totalPrice = bcsub($totalPrice, $hbAmount, 2);
  230. $modifyPrice = isset($data['modifyPrice']) && $data['modifyPrice'] > 0 ? $data['modifyPrice'] : 0;
  231. // skipMemberDiscount:前端(mall 混合单 / hd 开单页)已写入折后 modifyPrice 时跳过,避免二次会员折
  232. // 拼团单只按团购价结算,不再叠加会员折扣
  233. $skipMemberDiscount = !empty($data['skipMemberDiscount']) || $isGroupBuyOrder;
  234. unset($data['skipMemberDiscount']);
  235. $memberDiscount = $custom->discount ?? 1;
  236. // 秒杀已是活动价,会员折只打在非秒杀应付上,折后再加回秒杀额
  237. $seckillGoodsPrice = OrderClass::capSeckillExcludePrice($data['seckillGoodsPrice'] ?? 0, $modifyPrice);
  238. // 跑腿费不参与会员折扣:从可折扣基数剔除,折后原额加回(mall 混合单传入,其他单为 0)
  239. $runnerSendCost = isset($data['runnerSendCost']) && $data['runnerSendCost'] > 0 ? $data['runnerSendCost'] : 0;
  240. if (!$skipMemberDiscount && is_numeric($memberDiscount) && $memberDiscount > 0 && $memberDiscount < 1.0 && $modifyPrice > 0) {
  241. $discountablePrice = bcsub(bcsub($modifyPrice, $seckillGoodsPrice, 2), $runnerSendCost, 2);
  242. if (bccomp($discountablePrice, '0', 2) > 0) {
  243. $discountedPrice = bcmul($discountablePrice, $memberDiscount, 2);
  244. if (bccomp($discountedPrice, '0', 2) <= 0) {
  245. $discountedPrice = 0.01;
  246. }
  247. $modifyPrice = bcadd(bcadd($discountedPrice, $seckillGoodsPrice, 2), $runnerSendCost, 2);
  248. }
  249. }
  250. //优惠
  251. if ($modifyPrice < $totalPrice) {
  252. $data['discountType'] = dict::getDict('discountType', 'discount');
  253. $discountAmount = bcsub($totalPrice, $modifyPrice, 2);
  254. $data['discountAmount'] = $discountAmount;
  255. }
  256. //保存组合
  257. $groupName = $data['groupName'] ?? '';
  258. if (empty($groupId) && !empty($groupName)) {
  259. util::fail('组合功能不可用');
  260. }
  261. $data['weight'] = $totalWeight;
  262. $data['goodsPrice'] = $goodsPrice;
  263. $data['prePrice'] = $totalPrice;
  264. $data['orderPrice'] = $modifyPrice;
  265. if (isset($data['cash']) && is_numeric($data['cash']) && $data['cash'] > 0) {
  266. $cashAmount = $data['cash'] ?? 0;
  267. //现金付款如果大于订单金额,则切换成现金付款模式
  268. if ($cashAmount >= $modifyPrice) {
  269. $payWay = dict::getDict('payWay', 'cash');
  270. $hasPay = dict::getDict('hasPay', 'payed');
  271. $data['cash'] = 0;
  272. $data['mainPay'] = $modifyPrice;
  273. } else {
  274. //微信+现金 支付宝+现金 组合付款
  275. $data['cash'] = $cashAmount;
  276. $data['mainPay'] = bcsub($modifyPrice, $cashAmount, 2);
  277. $hasPay = dict::getDict('hasPay', 'unPay');
  278. }
  279. } else {
  280. $data['cash'] = 0;
  281. $data['mainPay'] = $modifyPrice;
  282. }
  283. if ($hasPay == dict::getDict('hasPay', 'debt')) {
  284. $data['debtPrice'] = $modifyPrice;
  285. $data['remainDebtPrice'] = $modifyPrice;
  286. $data['debt'] = 1;
  287. $payWay = dict::getDict('payWay', 'debtPay');
  288. }
  289. //hasPay balance 转化成余额支付方式
  290. if ($hasPay == dict::getDict('hasPay', 'balance')) {
  291. $payWay = dict::getDict('payWay', 'balancePay');
  292. }
  293. $data['cover'] = $orderCover;
  294. $data['actPrice'] = $modifyPrice;
  295. $data['realPrice'] = $modifyPrice;
  296. $data['customId'] = $customId;
  297. $data['hdId'] = $hdId;
  298. $data['sjId'] = $sjId;
  299. $data['userId'] = $userId;
  300. $data['shopId'] = $shopId;
  301. $data['mainId'] = $mainId;
  302. $data['manyType'] = $manyType;
  303. $data['payWay'] = $payWay;
  304. $data['orderSn'] = $orderSn;
  305. $data['status'] = OrderClass::ORDER_STATUS_UN_PAY;
  306. if (in_array($hasPay, [dict::getDict('hasPay', 'payed'), dict::getDict('hasPay', 'debt')])) {
  307. $data['onlinePay'] = dict::getDict('onlinePay', 'not');
  308. }
  309. //保存订单
  310. $returnOrder = OrderClass::addOrder($data);
  311. //欠款、已付款、余额支付的直接完成,另外客服下的单要走配送流程,所以是待配送状态
  312. if (in_array($hasPay, [dict::getDict('hasPay', 'payed'), dict::getDict('hasPay', 'debt'), dict::getDict('hasPay', 'balance')])) {
  313. $params = [];
  314. if (!empty($data['historyDate'])) {
  315. //后台开单传账单日期
  316. $params['historyDate'] = $data['historyDate'];
  317. }
  318. OrderClass::payAfter($returnOrder, $payWay, $params);
  319. }
  320. return $returnOrder;
  321. }
  322. //添加订单
  323. public static function addOrder($data)
  324. {
  325. return OrderClass::addOrder($data);
  326. }
  327. //获取订单信息 ssh 2019.11.28
  328. public static function getOrderList($where)
  329. {
  330. $order = 'addTime DESC';
  331. if (isset($where['status']) && $where['status'] == 2) {
  332. $order = 'reachDate ASC,reachPeriod ASC,addTime ASC';
  333. }
  334. $data = self::getList('*', $where, $order);
  335. if (empty($data['list'])) {
  336. return $data;
  337. }
  338. $orderSns = array_column($data['list'], 'orderSn');
  339. //获取订单的商品信息
  340. $goodsList = OrderGoodsService::getGoodsListByOrderSns($orderSns);
  341. //获取订花人的用户信息
  342. $orderUserIds = array_column($data['list'], 'customId');
  343. $orderUserIds = array_unique(array_filter($orderUserIds));
  344. $customList = CustomClass::getCustomByIds($orderUserIds);
  345. $sjIdList = array_column($data['list'], 'sjId');
  346. $sjIdList = array_filter(array_unique($sjIdList));
  347. $merchantList = MerchantService::getByIds($sjIdList, null, 'id');
  348. $defaultImg = imgUtil::groupImg('');
  349. foreach ($data['list'] as $key => $val) {
  350. $id = $val['id'];
  351. $customId = $val['customId'];
  352. $currentActPrice = $val['actPrice'] ?? 0;
  353. $currentSendCost = $val['sendCost'] ?? 0;
  354. $currentPrice = bcsub($currentActPrice, $currentSendCost, 2);
  355. //没有商品显示的样式
  356. $goodsInfoList = [['title' => '', 'coverUrl' => $defaultImg, 'unitPrice' => $currentPrice, 'num' => 1]];
  357. if (isset($goodsList[$id])) {
  358. $goodsInfoList = $goodsList[$id];
  359. }
  360. $data['list'][$key]['goodsInfoList'] = $goodsInfoList;
  361. $data['list'][$key]['bookAvatar'] = $customList[$customId]['smallAvatarUrl'] ?? '';
  362. $sjId = $val['sjId'];
  363. $data['list'][$key]['name'] = $merchantList[$sjId]['name'] ?? '';
  364. $data['list'][$key]['reachTime'] = OrderClass::getReachTime($val);
  365. $shortCover = $val['cover'] ?? '';
  366. $data['list'][$key]['cover'] = imgUtil::groupImg($shortCover);
  367. //同个人同地址当天订单数
  368. $sameTimeIds = $val['sameTimeIds'] ?? '';
  369. $sameNum = 0;
  370. if (!empty($sameTimeIds)) {
  371. $sameIds = explode(',', $sameTimeIds);
  372. $sameNum = count($sameIds);
  373. }
  374. $data['list'][$key]['sameTimeIdsNum'] = $sameNum;
  375. // 拼团订单附带团状态,供商城订单列表展示「拼团中/成功/失败」标签
  376. $groupBuyId = intval($val['groupBuyId'] ?? 0);
  377. $data['list'][$key]['groupBuyId'] = $groupBuyId;
  378. $data['list'][$key]['groupBuyStatus'] = 0;
  379. if ($groupBuyId > 0) {
  380. $groupBuy = \bizHd\groupBuy\classes\GroupBuyClass::getById($groupBuyId);
  381. $data['list'][$key]['groupBuyStatus'] = intval($groupBuy['status'] ?? 0);
  382. }
  383. }
  384. return $data;
  385. }
  386. //取客户最近若干条订单 ssh 2019.11.30
  387. public static function getUserOrder($customId, $limit = 10, $order = null, $with = null)
  388. {
  389. $where = ['adminId' => $customId, 'payStatus' => 1];
  390. return self::getLimitList('*', $where, $limit, $order, $with);
  391. }
  392. //支付前验证订单 ssh 2019.12.6
  393. public static function checkBeforePay($order)
  394. {
  395. $now = time();
  396. if (empty($order)) {
  397. util::fail('订单无效');
  398. }
  399. if ($order['status'] != 1) {
  400. util::fail('订单不是待付款状态');
  401. }
  402. if (($now - 60) > $order['deadline']) {
  403. util::fail('订单已过期');
  404. }
  405. if (ceil($order['actPrice']) <= 0) {
  406. $hbId = isset($order['hbId']) ? intval($order['hbId']) : 0;
  407. if ($hbId <= 0) {
  408. $orderId = isset($order['id']) ? intval($order['id']) : 0;
  409. if ($orderId > 0) {
  410. $usedHb = HbClass::getByCondition(['orderId' => $orderId, 'status' => 1]);
  411. $hbId = intval($usedHb['id'] ?? 0);
  412. }
  413. }
  414. // 红包全额抵扣后实付可以为 0;无红包的 0 元或负数仍视为金额异常
  415. if ($hbId <= 0 || bccomp((string)$order['actPrice'], '0', 2) < 0) {
  416. util::fail('订单金额有问题');
  417. }
  418. }
  419. if ($order['payStatus'] == 1) {
  420. util::fail('您已经付过了');
  421. }
  422. }
  423. //商家验证是否有效 ssh 2019.12.15
  424. public static function valid($order, $mainId)
  425. {
  426. if (empty($order)) {
  427. util::fail('订单无效');
  428. }
  429. if ($order['mainId'] != $mainId) {
  430. util::fail('不是您的订单');
  431. }
  432. }
  433. //根据编号查询 ssh 2019.12.14
  434. public static function getByOrderSn($orderSn)
  435. {
  436. return OrderClass::getByOrderSn($orderSn);
  437. }
  438. //点评 ssh 2019.12.16
  439. public static function comment($data)
  440. {
  441. return OrderClass::comment($data);
  442. }
  443. //订单归类
  444. public static function classify($id, $categoryId, $usageId)
  445. {
  446. $connection = Yii::$app->db;
  447. $transaction = $connection->beginTransaction();
  448. try {
  449. $order = OrderClass::getById($id, true);
  450. if ($order->payStatus != 1) {
  451. util::fail('订单还未付款');
  452. }
  453. if ($order->classify == 1) {
  454. util::fail('已经归类过了');
  455. }
  456. $order->categoryId = $categoryId;
  457. $order->usageId = $usageId;
  458. $order->classify = 1;
  459. $order->save();
  460. //设置流水的分类和用途并分配资金
  461. $setData = [
  462. 'sjId' => $order->sjId,
  463. 'capitalId' => $order->capitalId,
  464. 'time' => $order->payTime,
  465. 'usageId' => $order->usageId,
  466. 'categoryId' => $order->categoryId,
  467. 'amount' => $order->actPrice,
  468. 'payWay' => $order->payWay,
  469. ];
  470. SjCapitalService::orderClassify($setData);
  471. $transaction->commit();
  472. } catch (Exception $e) {
  473. $transaction->rollBack();
  474. util::fail('没有归类成功');
  475. }
  476. }
  477. //更新配送单 ssh 2019.12.17
  478. public static function updateSheet($order, $data)
  479. {
  480. $id = $order['id'];
  481. if ($order['status'] != OrderClass::ORDER_STATUS_UN_SEND) {
  482. util::fail('订单当前不是待配送状态');
  483. }
  484. $data['deliver'] = 1;
  485. OrderClass::updateById($id, $data);
  486. }
  487. //订单详情 ssh 2020.1.2
  488. public static function getOrderById($id)
  489. {
  490. return OrderClass::getOrderById($id);
  491. }
  492. //查看订单详情 ssh 2020.1.4
  493. public static function getOrderBySn($orderSn)
  494. {
  495. return OrderClass::getOrderBySn($orderSn);
  496. }
  497. //计算优惠 ssh 2020.3.9
  498. public static function getDiscountPrice($params)
  499. {
  500. $couponId = isset($params['couponId']) ? $params['couponId'] : 0;
  501. $customId = isset($params['userId']) ? $params['userId'] : 0;
  502. $price = isset($params['price']) ? $params['price'] : 0;
  503. $sourceType = isset($params['sourceType']) ? $params['sourceType'] : 0;
  504. $discountAmount = 0;
  505. $discountType = 0;
  506. $discountPrice = $price;
  507. //使用优惠券
  508. if (!empty($couponId)) {
  509. $discountAmount = CouponService::checkBeforeUse($couponId, $price, $customId);
  510. $discountPrice = stringUtil::calcSub($price, $discountAmount);
  511. $discountType = 1;
  512. } else {
  513. //使用会员
  514. $asset = UserAssetClass::getByUserId($customId);
  515. if (isset($asset['member']) && $asset['member'] > 0) {
  516. $currentDiscount = $asset['discount'];
  517. $currentPrice = $price * $currentDiscount;
  518. $newPrice = substr(sprintf("%.3f", $currentPrice), 0, -1);
  519. $discountType = 2;
  520. $discountAmount = stringUtil::calcSub($price, $newPrice);
  521. $discountPrice = $newPrice;
  522. }
  523. }
  524. //如果没有优惠券和会员优惠,并且是朋友圈收款,则进行随机优惠 ssh 2019.9.12
  525. if ($discountPrice == $price && $sourceType == 3) {
  526. $discountAmount = OrderClass::getRandDiscount($price);
  527. //开发和测试环境直接随机优惠
  528. $env = getenv('YII_ENV');
  529. if ($env == 'local' || $env == 'test') {
  530. $discountAmount = 0.01;
  531. }
  532. $discountPrice = stringUtil::calcSub($price, $discountAmount);
  533. $discountType = 3;
  534. }
  535. $discountType = $discountAmount <= 0 ? 0 : $discountType;
  536. $discountPrice = $discountPrice <= 0 ? 0.01 : $discountPrice;
  537. return ['price' => $discountPrice, 'discountType' => $discountType, 'discountAmount' => $discountAmount];
  538. }
  539. }