OrderService.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  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\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. //获取订单信息 ssh 2019.11.28
  218. public static function getOrderList($where)
  219. {
  220. $data = self::getList('*', $where, 'addTime DESC');
  221. if (empty($data['list'])) {
  222. return $data;
  223. }
  224. $list = $data['list'];
  225. $orderSns = array_column($list, 'orderSn');
  226. $goodsList = OrderGoodsService::getGoodsListByOrderSns($orderSns);
  227. $periodData = [0 => '上午', 1 => '下午', 2 => '晚上'];
  228. foreach ($data['list'] as $key => $val) {
  229. $id = $val['id'];
  230. $goodsInfoList = isset($goodsList[$id]) ? $goodsList[$id] : [];
  231. $data['list'][$key]['goodsInfoList'] = $goodsInfoList;
  232. $reachTime = OrderClass::getReachTime($val);
  233. $data['list'][$key]['reachTime'] = $reachTime;
  234. $requireTime = '';
  235. if ($val['reachDate'] != '0000-00-00') {
  236. $reachPeriodId = $val['reachPeriod'];
  237. $reachPeriod = isset($periodData[$reachPeriodId]) ? $periodData[$reachPeriodId] : '上午';
  238. $requireTime = date("m-d", strtotime($val['reachDate'])) . ' ' . $reachPeriod;
  239. }
  240. $data['list'][$key]['requireReachTime'] = $requireTime;
  241. $data['list'][$key]['smallHdAvatar'] = imgUtil::groupImg('') . "?x-oss-process=image/resize,m_fill,h_80,w_80";
  242. }
  243. return $data;
  244. }
  245. //取客户最近若干条订单 ssh 2019.11.30
  246. public static function getUserOrder($userId, $limit = 10, $order = null, $with = null)
  247. {
  248. $where = ['userId' => $userId, 'payStatus' => 1];
  249. return self::getLimitList('*', $where, $limit, $order, $with);
  250. }
  251. //支付前验证订单 ssh 2019.12.6
  252. public static function checkBeforePay($order)
  253. {
  254. $now = time();
  255. if (empty($order)) {
  256. util::fail('订单无效');
  257. }
  258. if ($order['status'] != 1) {
  259. util::fail('订单不是待付款状态');
  260. }
  261. if ($order['deadline'] <= ($now - 60)) {
  262. util::fail('订单已过期');
  263. }
  264. if (ceil($order['actPrice']) <= 0) {
  265. util::fail('订单金额有问题');
  266. }
  267. if ($order['payStatus'] == 1) {
  268. util::fail('您已经付过了');
  269. }
  270. }
  271. //根据编号查询 ssh 2019.12.14
  272. public static function getByOrderSn($orderSn)
  273. {
  274. return OrderClass::getByOrderSn($orderSn);
  275. }
  276. //点评 ssh 2019.12.16
  277. public static function comment($data)
  278. {
  279. return OrderClass::comment($data);
  280. }
  281. //订单归类
  282. public static function classify($id, $categoryId, $usageId)
  283. {
  284. $connection = Yii::$app->db;
  285. $transaction = $connection->beginTransaction();
  286. try {
  287. $order = OrderClass::getById($id, true);
  288. if ($order->payStatus != 1) {
  289. util::fail('订单还未付款');
  290. }
  291. if ($order->classify == 1) {
  292. util::fail('已经归类过了');
  293. }
  294. $order->categoryId = $categoryId;
  295. $order->usageId = $usageId;
  296. $order->classify = 1;
  297. $order->save();
  298. //设置流水的分类和用途并分配资金
  299. $setData = [
  300. 'sjId' => $order->sjId,
  301. 'capitalId' => $order->capitalId,
  302. 'time' => $order->payTime,
  303. 'usageId' => $order->usageId,
  304. 'categoryId' => $order->categoryId,
  305. 'amount' => $order->actPrice,
  306. 'payWay' => $order->payWay,
  307. ];
  308. MerchantCapitalService::orderClassify($setData);
  309. $transaction->commit();
  310. } catch (Exception $e) {
  311. $transaction->rollBack();
  312. util::fail('没有归类成功');
  313. }
  314. }
  315. //更新配送单 ssh 2019.12.17
  316. public static function updateSheet($order, $data)
  317. {
  318. $id = $order['id'];
  319. if ($order['status'] != OrderClass::ORDER_STATUS_UN_SEND) {
  320. util::fail('订单当前不是待配送状态');
  321. }
  322. $data['deliver'] = 1;
  323. OrderClass::updateById($id, $data);
  324. }
  325. //订单详情 ssh 2020.1.2
  326. public static function getOrderById($id)
  327. {
  328. return OrderClass::getOrderById($id);
  329. }
  330. //查看订单详情 ssh 2020.1.4
  331. public static function getOrderBySn($orderSn)
  332. {
  333. return OrderClass::getOrderBySn($orderSn);
  334. }
  335. //计算优惠 ssh 2020.3.9
  336. public static function getDiscountPrice($params)
  337. {
  338. $couponId = isset($params['couponId']) ? $params['couponId'] : 0;
  339. $userId = isset($params['userId']) ? $params['userId'] : 0;
  340. $price = isset($params['price']) ? $params['price'] : 0;
  341. $sourceType = $params['sourceType'] ?? 0;
  342. $discountAmount = 0;
  343. $discountType = 0;
  344. $discountPrice = $price;
  345. //使用优惠券
  346. if (!empty($couponId)) {
  347. $discountAmount = CouponService::checkBeforeUse($couponId, $price, $userId);
  348. $discountPrice = stringUtil::calcSub($price, $discountAmount);
  349. $discountType = 1;
  350. } else {
  351. //使用会员
  352. $asset = UserAssetClass::getByUserId($userId);
  353. if (isset($asset['member']) && $asset['member'] > 0) {
  354. $currentDiscount = $asset['discount'];
  355. $currentPrice = $price * $currentDiscount;
  356. $newPrice = substr(sprintf("%.3f", $currentPrice), 0, -1);
  357. $discountType = 2;
  358. $discountAmount = stringUtil::calcSub($price, $newPrice);
  359. $discountPrice = $newPrice;
  360. }
  361. }
  362. //如果没有优惠券和会员优惠,并且是朋友圈收款,则进行随机优惠 ssh 2019.9.12
  363. if ($discountPrice == $price && $sourceType == 3 && 1 == 2) {
  364. $discountAmount = OrderClass::getRandDiscount($price);
  365. //开发和测试环境直接随机优惠
  366. $env = getenv('YII_ENV');
  367. if ($env == 'local' || $env == 'test') {
  368. $discountAmount = 0.01;
  369. }
  370. $discountPrice = stringUtil::calcSub($price, $discountAmount);
  371. $discountType = 3;
  372. }
  373. $discountType = $discountAmount <= 0 ? 0 : $discountType;
  374. $discountPrice = $discountPrice <= 0 ? 0.01 : $discountPrice;
  375. return ['price' => $discountPrice, 'discountType' => $discountType, 'discountAmount' => $discountAmount];
  376. }
  377. }