OrderService.php 21 KB

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