OrderService.php 15 KB

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