OrderService.php 21 KB

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