OrderService.php 22 KB

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