OrderService.php 19 KB

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