OrderService.php 20 KB

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