OrderService.php 16 KB

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