OrderService.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552
  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. $memberDiscount = $custom->discount ?? 1;
  230. if (is_numeric($memberDiscount) && $memberDiscount > 0 && $memberDiscount < 1.0 && $modifyPrice > 0) {
  231. $modifyPrice = bcmul($modifyPrice, $memberDiscount, 2);
  232. if (bccomp($modifyPrice, '0', 2) <= 0) {
  233. $modifyPrice = 0.01;
  234. }
  235. }
  236. //优惠
  237. if ($modifyPrice < $totalPrice) {
  238. $data['discountType'] = dict::getDict('discountType', 'discount');
  239. $discountAmount = bcsub($totalPrice, $modifyPrice, 2);
  240. $data['discountAmount'] = $discountAmount;
  241. }
  242. //保存组合
  243. $groupName = $data['groupName'] ?? '';
  244. if (empty($groupId) && !empty($groupName)) {
  245. util::fail('组合功能不可用');
  246. }
  247. $data['weight'] = $totalWeight;
  248. $data['goodsPrice'] = $goodsPrice;
  249. $data['prePrice'] = $totalPrice;
  250. $data['orderPrice'] = $modifyPrice;
  251. if (isset($data['cash']) && is_numeric($data['cash']) && $data['cash'] > 0) {
  252. $cashAmount = $data['cash'] ?? 0;
  253. //现金付款如果大于订单金额,则切换成现金付款模式
  254. if ($cashAmount >= $modifyPrice) {
  255. $payWay = dict::getDict('payWay', 'cash');
  256. $hasPay = dict::getDict('hasPay', 'payed');
  257. $data['cash'] = 0;
  258. $data['mainPay'] = $modifyPrice;
  259. } else {
  260. //微信+现金 支付宝+现金 组合付款
  261. $data['cash'] = $cashAmount;
  262. $data['mainPay'] = bcsub($modifyPrice, $cashAmount, 2);
  263. $hasPay = dict::getDict('hasPay', 'unPay');
  264. }
  265. } else {
  266. $data['cash'] = 0;
  267. $data['mainPay'] = $modifyPrice;
  268. }
  269. if ($hasPay == dict::getDict('hasPay', 'debt')) {
  270. $data['debtPrice'] = $modifyPrice;
  271. $data['remainDebtPrice'] = $modifyPrice;
  272. $data['debt'] = 1;
  273. $payWay = dict::getDict('payWay', 'debtPay');
  274. }
  275. //hasPay balance 转化成余额支付方式
  276. if ($hasPay == dict::getDict('hasPay', 'balance')) {
  277. $payWay = dict::getDict('payWay', 'balancePay');
  278. }
  279. $data['cover'] = $orderCover;
  280. $data['actPrice'] = $modifyPrice;
  281. $data['realPrice'] = $modifyPrice;
  282. $data['customId'] = $customId;
  283. $data['hdId'] = $hdId;
  284. $data['sjId'] = $sjId;
  285. $data['userId'] = $userId;
  286. $data['shopId'] = $shopId;
  287. $data['mainId'] = $mainId;
  288. $data['manyType'] = $manyType;
  289. $data['payWay'] = $payWay;
  290. $data['orderSn'] = $orderSn;
  291. $data['status'] = OrderClass::ORDER_STATUS_UN_PAY;
  292. if (in_array($hasPay, [dict::getDict('hasPay', 'payed'), dict::getDict('hasPay', 'debt')])) {
  293. $data['onlinePay'] = dict::getDict('onlinePay', 'not');
  294. }
  295. //保存订单
  296. $returnOrder = OrderClass::addOrder($data);
  297. //欠款、已付款、余额支付的直接完成,另外客服下的单要走配送流程,所以是待配送状态
  298. if (in_array($hasPay, [dict::getDict('hasPay', 'payed'), dict::getDict('hasPay', 'debt'), dict::getDict('hasPay', 'balance')])) {
  299. $params = [];
  300. if (!empty($data['historyDate'])) {
  301. //后台开单传账单日期
  302. $params['historyDate'] = $data['historyDate'];
  303. }
  304. OrderClass::payAfter($returnOrder, $payWay, $params);
  305. }
  306. return $returnOrder;
  307. }
  308. //添加订单
  309. public static function addOrder($data)
  310. {
  311. return OrderClass::addOrder($data);
  312. }
  313. //获取订单信息 ssh 2019.11.28
  314. public static function getOrderList($where)
  315. {
  316. $order = 'addTime DESC';
  317. if (isset($where['status']) && $where['status'] == 2) {
  318. $order = 'reachDate ASC,reachPeriod ASC,addTime ASC';
  319. }
  320. $data = self::getList('*', $where, $order);
  321. if (empty($data['list'])) {
  322. return $data;
  323. }
  324. $orderSns = array_column($data['list'], 'orderSn');
  325. //获取订单的商品信息
  326. $goodsList = OrderGoodsService::getGoodsListByOrderSns($orderSns);
  327. //获取订花人的用户信息
  328. $orderUserIds = array_column($data['list'], 'customId');
  329. $orderUserIds = array_unique(array_filter($orderUserIds));
  330. $customList = CustomClass::getCustomByIds($orderUserIds);
  331. $sjIdList = array_column($data['list'], 'sjId');
  332. $sjIdList = array_filter(array_unique($sjIdList));
  333. $merchantList = MerchantService::getByIds($sjIdList, null, 'id');
  334. $defaultImg = imgUtil::groupImg('');
  335. foreach ($data['list'] as $key => $val) {
  336. $id = $val['id'];
  337. $customId = $val['customId'];
  338. $currentActPrice = $val['actPrice'] ?? 0;
  339. $currentSendCost = $val['sendCost'] ?? 0;
  340. $currentPrice = bcsub($currentActPrice, $currentSendCost, 2);
  341. //没有商品显示的样式
  342. $goodsInfoList = [['title' => '', 'coverUrl' => $defaultImg, 'unitPrice' => $currentPrice, 'num' => 1]];
  343. if (isset($goodsList[$id])) {
  344. $goodsInfoList = $goodsList[$id];
  345. }
  346. $data['list'][$key]['goodsInfoList'] = $goodsInfoList;
  347. $data['list'][$key]['bookAvatar'] = $customList[$customId]['smallAvatarUrl'] ?? '';
  348. $sjId = $val['sjId'];
  349. $data['list'][$key]['name'] = $merchantList[$sjId]['name'] ?? '';
  350. $data['list'][$key]['reachTime'] = OrderClass::getReachTime($val);
  351. $shortCover = $val['cover'] ?? '';
  352. $data['list'][$key]['cover'] = imgUtil::groupImg($shortCover);
  353. //同个人同地址当天订单数
  354. $sameTimeIds = $val['sameTimeIds'] ?? '';
  355. $sameNum = 0;
  356. if (!empty($sameTimeIds)) {
  357. $sameIds = explode(',', $sameTimeIds);
  358. $sameNum = count($sameIds);
  359. }
  360. $data['list'][$key]['sameTimeIdsNum'] = $sameNum;
  361. }
  362. return $data;
  363. }
  364. //取客户最近若干条订单 ssh 2019.11.30
  365. public static function getUserOrder($customId, $limit = 10, $order = null, $with = null)
  366. {
  367. $where = ['adminId' => $customId, 'payStatus' => 1];
  368. return self::getLimitList('*', $where, $limit, $order, $with);
  369. }
  370. //支付前验证订单 ssh 2019.12.6
  371. public static function checkBeforePay($order)
  372. {
  373. $now = time();
  374. if (empty($order)) {
  375. util::fail('订单无效');
  376. }
  377. if ($order['status'] != 1) {
  378. util::fail('订单不是待付款状态');
  379. }
  380. if (($now - 60) > $order['deadline']) {
  381. util::fail('订单已过期');
  382. }
  383. if (ceil($order['actPrice']) <= 0) {
  384. util::fail('订单金额有问题');
  385. }
  386. if ($order['payStatus'] == 1) {
  387. util::fail('您已经付过了');
  388. }
  389. }
  390. //商家验证是否有效 ssh 2019.12.15
  391. public static function valid($order, $mainId)
  392. {
  393. if (empty($order)) {
  394. util::fail('订单无效');
  395. }
  396. if ($order['mainId'] != $mainId) {
  397. util::fail('不是您的订单');
  398. }
  399. }
  400. //根据编号查询 ssh 2019.12.14
  401. public static function getByOrderSn($orderSn)
  402. {
  403. return OrderClass::getByOrderSn($orderSn);
  404. }
  405. //点评 ssh 2019.12.16
  406. public static function comment($data)
  407. {
  408. return OrderClass::comment($data);
  409. }
  410. //订单归类
  411. public static function classify($id, $categoryId, $usageId)
  412. {
  413. $connection = Yii::$app->db;
  414. $transaction = $connection->beginTransaction();
  415. try {
  416. $order = OrderClass::getById($id, true);
  417. if ($order->payStatus != 1) {
  418. util::fail('订单还未付款');
  419. }
  420. if ($order->classify == 1) {
  421. util::fail('已经归类过了');
  422. }
  423. $order->categoryId = $categoryId;
  424. $order->usageId = $usageId;
  425. $order->classify = 1;
  426. $order->save();
  427. //设置流水的分类和用途并分配资金
  428. $setData = [
  429. 'sjId' => $order->sjId,
  430. 'capitalId' => $order->capitalId,
  431. 'time' => $order->payTime,
  432. 'usageId' => $order->usageId,
  433. 'categoryId' => $order->categoryId,
  434. 'amount' => $order->actPrice,
  435. 'payWay' => $order->payWay,
  436. ];
  437. SjCapitalService::orderClassify($setData);
  438. $transaction->commit();
  439. } catch (Exception $e) {
  440. $transaction->rollBack();
  441. util::fail('没有归类成功');
  442. }
  443. }
  444. //更新配送单 ssh 2019.12.17
  445. public static function updateSheet($order, $data)
  446. {
  447. $id = $order['id'];
  448. if ($order['status'] != OrderClass::ORDER_STATUS_UN_SEND) {
  449. util::fail('订单当前不是待配送状态');
  450. }
  451. $data['deliver'] = 1;
  452. OrderClass::updateById($id, $data);
  453. }
  454. //订单详情 ssh 2020.1.2
  455. public static function getOrderById($id)
  456. {
  457. return OrderClass::getOrderById($id);
  458. }
  459. //查看订单详情 ssh 2020.1.4
  460. public static function getOrderBySn($orderSn)
  461. {
  462. return OrderClass::getOrderBySn($orderSn);
  463. }
  464. //计算优惠 ssh 2020.3.9
  465. public static function getDiscountPrice($params)
  466. {
  467. $couponId = isset($params['couponId']) ? $params['couponId'] : 0;
  468. $customId = isset($params['userId']) ? $params['userId'] : 0;
  469. $price = isset($params['price']) ? $params['price'] : 0;
  470. $sourceType = isset($params['sourceType']) ? $params['sourceType'] : 0;
  471. $discountAmount = 0;
  472. $discountType = 0;
  473. $discountPrice = $price;
  474. //使用优惠券
  475. if (!empty($couponId)) {
  476. $discountAmount = CouponService::checkBeforeUse($couponId, $price, $customId);
  477. $discountPrice = stringUtil::calcSub($price, $discountAmount);
  478. $discountType = 1;
  479. } else {
  480. //使用会员
  481. $asset = UserAssetClass::getByUserId($customId);
  482. if (isset($asset['member']) && $asset['member'] > 0) {
  483. $currentDiscount = $asset['discount'];
  484. $currentPrice = $price * $currentDiscount;
  485. $newPrice = substr(sprintf("%.3f", $currentPrice), 0, -1);
  486. $discountType = 2;
  487. $discountAmount = stringUtil::calcSub($price, $newPrice);
  488. $discountPrice = $newPrice;
  489. }
  490. }
  491. //如果没有优惠券和会员优惠,并且是朋友圈收款,则进行随机优惠 ssh 2019.9.12
  492. if ($discountPrice == $price && $sourceType == 3) {
  493. $discountAmount = OrderClass::getRandDiscount($price);
  494. //开发和测试环境直接随机优惠
  495. $env = getenv('YII_ENV');
  496. if ($env == 'local' || $env == 'test') {
  497. $discountAmount = 0.01;
  498. }
  499. $discountPrice = stringUtil::calcSub($price, $discountAmount);
  500. $discountType = 3;
  501. }
  502. $discountType = $discountAmount <= 0 ? 0 : $discountType;
  503. $discountPrice = $discountPrice <= 0 ? 0.01 : $discountPrice;
  504. return ['price' => $discountPrice, 'discountType' => $discountType, 'discountAmount' => $discountAmount];
  505. }
  506. }