OrderService.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  1. <?php
  2. namespace bizMall\order\services;
  3. use bizMall\base\services\BaseService;
  4. use bizMall\goods\classes\GoodsClass;
  5. use bizMall\hd\classes\HdClass;
  6. use bizMall\item\classes\ItemClass;
  7. use bizMall\merchant\services\MerchantCapitalService;
  8. use bizMall\order\classes\OrderClass;
  9. use bizMall\order\classes\OrderGoodsClass;
  10. use bizMall\order\classes\OrderItemClass;
  11. use bizMall\promote\services\CouponService;
  12. use bizMall\shop\classes\ShopClass;
  13. use bizMall\user\classes\UserAssetClass;
  14. use common\components\dict;
  15. use common\components\delivery\platform\fengniao\OrderStatusUtil;
  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 = '\bizMall\order\classes\OrderClass';
  24. public static function createOrder($data, $custom)
  25. {
  26. $customId = $data['customId'] ?? 0;
  27. $hdId = $data['hdId'] ?? 0;
  28. $orderSn = orderSn::getOrderSn();
  29. $sjId = $data['sjId'] ?? 0;
  30. $shopId = $data['shopId'] ?? 0;
  31. $mainId = $data['mainId'] ?? 0;
  32. $shop = ShopClass::getById($shopId, true);
  33. if (empty($shop)) {
  34. util::fail('没有找到门店86');
  35. }
  36. $product = $data['product'] ?? [];
  37. if (empty($product)) {
  38. util::fail('没有花材');
  39. }
  40. $gIds = [];
  41. $itIds = [];
  42. foreach ($product as $val) {
  43. $currentId = $val['productId'] ?? 0;
  44. $property = $val['property'];
  45. if ($property == dict::getDict('property', 'goods')) {
  46. $gIds[] = $currentId;
  47. }
  48. if ($property == dict::getDict('property', 'item')) {
  49. $itIds[] = $currentId;
  50. }
  51. }
  52. $goodsInfo = [];
  53. $gIds = array_unique(array_filter($gIds));
  54. if (!empty($gIds)) {
  55. $goodsInfo = GoodsClass::getByIds($gIds, null, 'id');
  56. }
  57. $itemInfo = [];
  58. $itIds = array_unique(array_filter($itIds));
  59. if (!empty($itIds)) {
  60. $itemInfo = ItemClass::getByIds($itIds, null, 'id');
  61. }
  62. $weight = 0;
  63. $goodsPrice = 0;
  64. $priceMap = \bizGhs\custom\classes\CustomClass::$levelPriceKeyMap;
  65. $addPriceMap = \bizGhs\custom\classes\CustomClass::$levelAddPriceKeyMap;
  66. foreach ($product as $key => $val) {
  67. if (!isset($val['property'])) {
  68. util::fail('没有找到产品属性');
  69. }
  70. $property = $val['property'];
  71. $unitType = $val['unitType'] ?? 0;
  72. $currentId = $val['productId'] ?? 0;
  73. $num = $val['num'] ?? 0;
  74. $unitPrice = 0;
  75. $currentWeight = 0;
  76. if ($property == dict::getDict('property', 'goods')) {
  77. $name = $goodsInfo[$currentId]['name'] ?? '';
  78. $cover = $goodsInfo[$currentId]['cover'] ?? '';
  79. $flower = $goodsInfo[$currentId]['flower'] ?? 0;
  80. $currentWeight = $goodsInfo[$currentId]['weight'] ?? 0;
  81. if (!empty($groupCover)) {
  82. $groupCover = $cover;
  83. }
  84. $currentPrice = $goodsInfo[$currentId]['price'] ?? 0;
  85. $currentMainId = $goodsInfo[$currentId]['mainId'] ?? 0;
  86. $unitPrice = $currentPrice;
  87. if ($currentMainId != $mainId) {
  88. util::fail('不是您的商品');
  89. }
  90. $currentGoodsData = [
  91. 'orderSn' => $orderSn,
  92. 'goodsId' => $currentId,
  93. 'flower' => $flower,
  94. 'customId' => $customId,
  95. 'hdId' => $hdId,
  96. 'sjId' => $sjId,
  97. 'shopId' => $shopId,
  98. 'name' => $name,
  99. 'cover' => $cover,
  100. 'unitPrice' => $unitPrice,
  101. 'num' => $num,
  102. 'price' => bcmul($unitPrice, $num, 2),
  103. 'mainId' => $mainId,
  104. ];
  105. //库存变化在这里
  106. OrderGoodsClass::addData($currentGoodsData);
  107. }
  108. if ($property == dict::getDict('property', 'item')) {
  109. $currentItemInfo = $itemInfo[$currentId] ?? [];
  110. $name = $itemInfo[$currentId]['name'] ?? '';
  111. $cover = $itemInfo[$currentId]['cover'] ?? '';
  112. if (!empty($groupCover)) {
  113. $groupCover = $cover;
  114. }
  115. $ratio = $itemInfo[$currentId]['ratio'] ?? 20;
  116. $currentMainId = $itemInfo[$currentId]['mainId'] ?? 0;
  117. $currentPrice = \bizGhs\product\classes\ProductClass::getFinalPrice($currentItemInfo, 0, $priceMap, $addPriceMap);
  118. $currentWeight = $itemInfo[$currentId]['weight'] ?? 0;
  119. if ($currentMainId != $mainId) {
  120. util::fail('不是您的花材' . $mainId . ' ' . $currentMainId);
  121. }
  122. if ($unitType == dict::getDict('unitType', 'big')) {
  123. $unitName = $itemInfo[$currentId]['bigUnit'] ?? '';
  124. $unitId = $itemInfo[$currentId]['bigUnitId'] ?? 0;
  125. $totalNum = $num;
  126. $unitPrice = $currentPrice;
  127. } else {
  128. $unitName = $itemInfo[$currentId]['smallUnit'] ?? '';
  129. $unitId = $itemInfo[$currentId]['smallUnitId'] ?? 0;
  130. $totalNum = bcdiv($num, $ratio, 2);
  131. $div = bcdiv($currentPrice, $ratio, 2);
  132. $smallRatio = $itemInfo[$currentId]['smallRatio'] ?? 0;
  133. $unitPrice = bcmul($div, $smallRatio, 2);
  134. }
  135. $ptItemId = $itemInfo[$currentId]['itemId'] ?? 0;
  136. $classId = $itemInfo[$currentId]['classId'] ?? 0;
  137. $belongCost = $itemInfo[$currentId]['belongCost'] ?? 0;
  138. $currentItemData = [
  139. 'name' => $name,
  140. 'cover' => $cover,
  141. 'ratio' => $ratio,
  142. 'unitType' => $unitType,
  143. 'unitName' => $unitName,
  144. 'unitId' => $unitId,
  145. 'unitPrice' => $unitPrice,
  146. 'num' => $num,
  147. 'orderSn' => $orderSn,
  148. 'itemId' => $currentId,
  149. 'ptItemId' => $ptItemId,
  150. 'totalNum' => $totalNum,
  151. 'totalPrice' => 0,
  152. 'customId' => $customId,
  153. 'hdId' => $hdId,
  154. 'sjId' => $sjId,
  155. 'shopId' => $shopId,
  156. 'mainId' => $mainId,
  157. 'price' => bcmul($unitPrice, $num, 2),
  158. 'classId' => $classId,
  159. 'belongCost' => $belongCost,
  160. ];
  161. //库存变化在这里
  162. OrderItemClass::addData($currentItemData, $custom);
  163. }
  164. $weight = bcadd($weight, $currentWeight, 2);
  165. $goodsPrice = bcadd($goodsPrice, bcmul($unitPrice, $num, 2), 2);
  166. }
  167. //计算运费
  168. $sendDistance = 0;
  169. $sendCost = 0;
  170. $sendType = $data['sendType'] ?? dict::getDict('sendType', 'thirdSend');
  171. if ($sendType == dict::getDict('sendType', 'thirdSend')) {
  172. $lat = $data['lat'] ?? '';
  173. $lng = $data['long'] ?? '';
  174. if (empty($lat) || empty($lng)) {
  175. //util::fail('请填写收花地址...');
  176. }
  177. $shopLat = $shop->lat ?? '';
  178. $shopLong = $shop->long ?? '';
  179. if (empty($shopLat) || empty($shopLong)) {
  180. //util::fail('请完成门店地址');
  181. }
  182. $disRespond = OrderClass::getDistanceFee($lat, $lng, $shopLat, $shopLong, $weight);
  183. $sendCost = $disRespond['fee'] ?? 0;
  184. $sendDistance = $disRespond['distance'] ?? 0;
  185. }
  186. $data['sendDistance'] = $sendDistance;
  187. $data['sendCost'] = $sendCost;
  188. $totalPrice = bcadd($goodsPrice, $sendCost, 2);
  189. $modifyPrice = $totalPrice;
  190. if ($modifyPrice > $totalPrice) {
  191. //人工资材包装费
  192. $data['labourCost'] = bcsub($modifyPrice, $totalPrice, 2);
  193. }
  194. if ($modifyPrice < $totalPrice) {
  195. //优惠
  196. $data['discountType'] = dict::getDict('discountType', 'discount');
  197. $discountAmount = bcsub($totalPrice, $modifyPrice, 2);
  198. $data['discountAmount'] = $discountAmount;
  199. }
  200. $data['goodsPrice'] = $goodsPrice;
  201. $data['prePrice'] = $totalPrice;
  202. $data['orderPrice'] = $modifyPrice;
  203. $data['actPrice'] = $modifyPrice;
  204. $data['realPrice'] = $modifyPrice;
  205. $data['mainPay'] = $modifyPrice;
  206. $data['cash'] = 0;
  207. $data['customId'] = $customId;
  208. $data['hdId'] = $hdId;
  209. $data['sjId'] = $sjId;
  210. $data['shopId'] = $shopId;
  211. $data['mainId'] = $mainId;
  212. $data['payWay'] = dict::getDict('payWay', 'unPay');
  213. $data['orderSn'] = $orderSn;
  214. $data['status'] = OrderClass::ORDER_STATUS_UN_PAY;
  215. return OrderClass::addOrder($data);
  216. }
  217. //获取订单信息 TODO 重新实现,要限制查询字段与返回字段,性能第一???? -- 商品项(商品规格)、商品数量
  218. public static function getOrderList($where)
  219. {
  220. $fields = 'id, shopId, hdId, orderSn, groupBuyId, orderType, actPrice, goodsNum, reachDate, reachPeriod, status, addTime';
  221. $data = self::getList($fields, $where, 'addTime DESC');
  222. if (empty($data['list'])) {
  223. return $data;
  224. }
  225. $list = $data['list'];
  226. $hdIds = array_values(array_filter(array_unique(array_column($list, 'hdId'))));
  227. $hdMap = [];
  228. if (!empty($hdIds)) {
  229. $hdMap = HdClass::getByIds($hdIds, null, 'id');
  230. }
  231. $orderSns = array_values(array_filter(array_unique(array_column($list, 'orderSn'))));
  232. // 花束订单明细在 xhOrderGoods,花材订单明细在 xhOrderItem,orderType=3 时两者都有
  233. $goodsListMap = OrderGoodsService::getGoodsListByOrderSns($orderSns);
  234. $itemListMap = OrderItemService::getItemListByOrderSns($orderSns);
  235. $periodData = [0 => '上午', 1 => '下午', 2 => '晚上'];
  236. foreach ($data['list'] as $key => $val) {
  237. $orderSn = $val['orderSn'] ?? '';
  238. $orderType = intval($val['orderType'] ?? 1);
  239. $hdId = intval($val['hdId'] ?? 0);
  240. $orderStatus = intval($val['status'] ?? 0);
  241. $data['list'][$key]['hdName'] = $hdMap[$hdId]['name'] ?? '';
  242. $data['list'][$key]['statusDesc'] = OrderStatusUtil::getSystemStatusDesc($orderStatus);
  243. $goodsInfoList = [];
  244. if (!empty($orderSn)) {
  245. if ($orderType == 1) {
  246. $goodsInfoList = $goodsListMap[$orderSn] ?? [];
  247. } elseif ($orderType == 2) {
  248. $goodsInfoList = $itemListMap[$orderSn] ?? [];
  249. } elseif ($orderType == 3) {
  250. $goodsInfoList = array_merge($goodsListMap[$orderSn] ?? [], $itemListMap[$orderSn] ?? []);
  251. }
  252. }
  253. if(isset($val['goodsNum']) && $val['goodsNum'] == 0) {
  254. $data['list'][$key]['goodsNum'] = count($goodsInfoList);
  255. }
  256. $data['list'][$key]['goodsInfoList'] = $goodsInfoList;
  257. $reachTime = OrderClass::getReachTime($val);
  258. $data['list'][$key]['reachTime'] = $reachTime;
  259. $requireTime = '';
  260. if ($val['reachDate'] != '0000-00-00') {
  261. $reachPeriodId = $val['reachPeriod'];
  262. $reachPeriod = isset($periodData[$reachPeriodId]) ? $periodData[$reachPeriodId] : '上午';
  263. $requireTime = date("m-d", strtotime($val['reachDate'])) . ' ' . $reachPeriod;
  264. }
  265. $data['list'][$key]['requireReachTime'] = $requireTime;
  266. $data['list'][$key]['smallHdAvatar'] = imgUtil::groupImg('') . "?x-oss-process=image/resize,m_fill,h_80,w_80";
  267. }
  268. return $data;
  269. }
  270. //取客户最近若干条订单 ssh 2019.11.30
  271. public static function getUserOrder($userId, $limit = 10, $order = null, $with = null)
  272. {
  273. $where = ['userId' => $userId, 'payStatus' => 1];
  274. return self::getLimitList('*', $where, $limit, $order, $with);
  275. }
  276. //支付前验证订单 ssh 2019.12.6
  277. public static function checkBeforePay($order)
  278. {
  279. $now = time();
  280. if (empty($order)) {
  281. util::fail('订单无效');
  282. }
  283. if ($order['status'] != 1) {
  284. util::fail('订单不是待付款状态');
  285. }
  286. if ($order['deadline'] <= ($now - 60)) {
  287. util::fail('订单已过期');
  288. }
  289. if (ceil($order['actPrice']) <= 0) {
  290. util::fail('订单金额有问题');
  291. }
  292. if ($order['payStatus'] == 1) {
  293. util::fail('您已经付过了');
  294. }
  295. }
  296. //根据编号查询 ssh 2019.12.14
  297. public static function getByOrderSn($orderSn)
  298. {
  299. return OrderClass::getByOrderSn($orderSn);
  300. }
  301. //点评 ssh 2019.12.16
  302. public static function comment($data)
  303. {
  304. return OrderClass::comment($data);
  305. }
  306. //订单归类
  307. public static function classify($id, $categoryId, $usageId)
  308. {
  309. $connection = Yii::$app->db;
  310. $transaction = $connection->beginTransaction();
  311. try {
  312. $order = OrderClass::getById($id, true);
  313. if ($order->payStatus != 1) {
  314. util::fail('订单还未付款');
  315. }
  316. if ($order->classify == 1) {
  317. util::fail('已经归类过了');
  318. }
  319. $order->categoryId = $categoryId;
  320. $order->usageId = $usageId;
  321. $order->classify = 1;
  322. $order->save();
  323. //设置流水的分类和用途并分配资金
  324. $setData = [
  325. 'sjId' => $order->sjId,
  326. 'capitalId' => $order->capitalId,
  327. 'time' => $order->payTime,
  328. 'usageId' => $order->usageId,
  329. 'categoryId' => $order->categoryId,
  330. 'amount' => $order->actPrice,
  331. 'payWay' => $order->payWay,
  332. ];
  333. MerchantCapitalService::orderClassify($setData);
  334. $transaction->commit();
  335. } catch (Exception $e) {
  336. $transaction->rollBack();
  337. util::fail('没有归类成功');
  338. }
  339. }
  340. //更新配送单 ssh 2019.12.17
  341. public static function updateSheet($order, $data)
  342. {
  343. $id = $order['id'];
  344. if ($order['status'] != OrderClass::ORDER_STATUS_UN_SEND) {
  345. util::fail('订单当前不是待配送状态');
  346. }
  347. $data['deliver'] = 1;
  348. OrderClass::updateById($id, $data);
  349. }
  350. //订单详情 ssh 2020.1.2
  351. public static function getOrderById($id)
  352. {
  353. return OrderClass::getOrderById($id);
  354. }
  355. //查看订单详情 ssh 2020.1.4
  356. public static function getOrderBySn($orderSn)
  357. {
  358. return OrderClass::getOrderBySn($orderSn);
  359. }
  360. //计算优惠 ssh 2020.3.9
  361. public static function getDiscountPrice($params)
  362. {
  363. $couponId = isset($params['couponId']) ? $params['couponId'] : 0;
  364. $userId = isset($params['userId']) ? $params['userId'] : 0;
  365. $price = isset($params['price']) ? $params['price'] : 0;
  366. $sourceType = $params['sourceType'] ?? 0;
  367. $discountAmount = 0;
  368. $discountType = 0;
  369. $discountPrice = $price;
  370. //使用优惠券
  371. if (!empty($couponId)) {
  372. $discountAmount = CouponService::checkBeforeUse($couponId, $price, $userId);
  373. $discountPrice = stringUtil::calcSub($price, $discountAmount);
  374. $discountType = 1;
  375. } else {
  376. //使用会员
  377. $asset = UserAssetClass::getByUserId($userId);
  378. if (isset($asset['member']) && $asset['member'] > 0) {
  379. $currentDiscount = $asset['discount'];
  380. $currentPrice = $price * $currentDiscount;
  381. $newPrice = substr(sprintf("%.3f", $currentPrice), 0, -1);
  382. $discountType = 2;
  383. $discountAmount = stringUtil::calcSub($price, $newPrice);
  384. $discountPrice = $newPrice;
  385. }
  386. }
  387. //如果没有优惠券和会员优惠,并且是朋友圈收款,则进行随机优惠 ssh 2019.9.12
  388. if ($discountPrice == $price && $sourceType == 3 && 1 == 2) {
  389. $discountAmount = OrderClass::getRandDiscount($price);
  390. //开发和测试环境直接随机优惠
  391. $env = getenv('YII_ENV');
  392. if ($env == 'local' || $env == 'test') {
  393. $discountAmount = 0.01;
  394. }
  395. $discountPrice = stringUtil::calcSub($price, $discountAmount);
  396. $discountType = 3;
  397. }
  398. $discountType = $discountAmount <= 0 ? 0 : $discountType;
  399. $discountPrice = $discountPrice <= 0 ? 0.01 : $discountPrice;
  400. return ['price' => $discountPrice, 'discountType' => $discountType, 'discountAmount' => $discountAmount];
  401. }
  402. }