OrderClass.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  1. <?php
  2. namespace bizMall\order\classes;
  3. use biz\stat\classes\StatOrderCountClass;
  4. use bizMall\merchant\classes\MerchantAssetClass;
  5. use bizMall\shop\classes\MainClass;
  6. use bizMall\shop\classes\ShopClass;
  7. use bizMall\stat\classes\StatOrderClass;
  8. use bizMall\user\classes\UserClass;
  9. use common\components\imgUtil;
  10. use common\components\mapUtil;
  11. use common\components\miniUtil;
  12. use common\components\dict;
  13. use common\components\orderSn;
  14. use bizMall\base\classes\BaseClass;
  15. use common\components\util;
  16. class OrderClass extends BaseClass
  17. {
  18. public static $baseFile = '\bizMall\order\models\Order';
  19. //完成
  20. const ORDER_STATUS_COMPLETE = 4;
  21. //已取消
  22. const ORDER_STATUS_CANCEL = 5;
  23. //配送中
  24. const ORDER_STATUS_SENDING = 3;
  25. //待配送
  26. const ORDER_STATUS_UN_SEND = 2;
  27. //待付款
  28. const ORDER_STATUS_UN_PAY = 1;
  29. //订单总流程数
  30. const ORDER_TOTAL_FLOW = 4;
  31. //未确认配送方式
  32. const SEND_TYPE_UNKNOWN = 0;
  33. //自取
  34. const SEND_TYPE_NO = 1;
  35. //自己送
  36. const SEND_TYPE_MYSELF = 2;
  37. //代送
  38. const SEND_TYPE_OTHER = 3;
  39. //送达时间段
  40. public static $reachPeriod = [0 => '上午', 1 => '下午', 2 => '晚上'];
  41. //优惠金额
  42. public static $randDiscount = [
  43. 50 => [1, 3],
  44. 100 => [5, 9],
  45. 300 => [10, 15],
  46. 500 => [16, 18],
  47. 1000 => [18, 30],
  48. 20000 => [90, 100],
  49. ];
  50. //所有的欠款采购单 ssh 2021.1.26
  51. public static function getDebtList($where)
  52. {
  53. $list = self::getAllList('*', $where, 'addTime DESC');
  54. if (!empty($list)) {
  55. foreach ($list as $key => $value) {
  56. $debtPrice = $value['debtPrice'] ?? 0;
  57. $remainDebtPrice = $value['remainDebtPrice'] ?? 0;
  58. $hasPayDebt = 0;
  59. if ($debtPrice > $remainDebtPrice) {
  60. $hasPayDebt = bcsub($debtPrice, $remainDebtPrice, 2);
  61. }
  62. $list[$key]['hasPayDebt'] = floatval($hasPayDebt);
  63. }
  64. }
  65. $count = self::getCount($where);
  66. return ['list' => $list, 'debtNum' => $count];
  67. }
  68. //计算运费,请搜索关键词calc_freight,二个方法要合一起 ssh 20221014
  69. public static function getDistanceFee($userLat, $userLong, $shopLat, $shopLong, $weight)
  70. {
  71. //基础配送费8元,0-5公里,每公里+1元,5-100公里,6元起,每公里+2元
  72. //5-10公斤,2元起,每公斤+2元,10-15公斤,+12元,超过15公斤,+20元
  73. //00:00 - 06:00 +6元,22:00-24:00 +4元
  74. //恶劣天气,下单高峰期会临时调整价格
  75. //起步价
  76. $baseFee = 10;
  77. $distance = mapUtil::calculateDistance($userLong, $userLat, $shopLong, $shopLat);
  78. //0-5公里,每公里+1元,5-100公里,6元起,每公里+2元
  79. $formatDistance = ceil($distance / 1000);
  80. $startKiloFee = 6;
  81. if ($formatDistance < 5) {
  82. $addDistanceFee = bcmul($formatDistance, 2);
  83. } elseif ($formatDistance == 5) {
  84. $addDistanceFee = $startKiloFee;
  85. } else {
  86. $overKilo = bcsub($formatDistance, 5);
  87. $overFee = bcmul($overKilo, 3);
  88. $addDistanceFee = bcadd($startKiloFee, $overFee);
  89. }
  90. //5-10公斤,2元起,每公斤+2元,10-15公斤,+12元,超过15公斤,+20元
  91. $formatWeight = ceil($weight);
  92. if ($formatWeight > 15) {
  93. $addWeightFee = 20;
  94. } elseif ($formatWeight >= 10 && $formatWeight <= 15) {
  95. $addWeightFee = 12;
  96. } elseif ($formatWeight >= 5 && $formatWeight < 10) {
  97. $overWeight = bcsub($formatWeight, 4);
  98. $addWeightFee = bcmul($overWeight, 2);
  99. } else {
  100. $addWeightFee = 0;
  101. }
  102. $addFee = bcadd($addDistanceFee, $addWeightFee, 2);
  103. $totalFee = bcadd($baseFee, $addFee, 2);
  104. //特殊时段费
  105. $timeFee = 0;
  106. $hour = date("G");
  107. //22:00 - 24:00 +4元
  108. if (in_array($hour, [22, 23])) {
  109. $timeFee = 4;
  110. }
  111. //00:00 - 06:00 +6元
  112. if (in_array($hour, [0, 1, 2, 3, 4, 5])) {
  113. $timeFee = 6;
  114. }
  115. $totalFee = bcadd($totalFee, $timeFee, 2);
  116. //恶劣天气和下单高峰费用
  117. $weatherFee = 0;
  118. $totalFee = bcadd($totalFee, $weatherFee, 2);
  119. $showDistance = sprintf("%.1f", ($distance / 1000));
  120. //noticeUtil::push("零售商城 距离:{$distance} {$showDistance} 重量:{$weight} 费用:{$totalFee}", '15280215347');
  121. //return ['distance' => 0, 'showDistance' => 0, 'fee' => 0];
  122. return ['distance' => $distance, 'showDistance' => $showDistance, 'fee' => $totalFee];
  123. }
  124. //添加订单 ssh 2019.12.5
  125. public static function addOrder($data)
  126. {
  127. $data['orderSn'] = isset($data['orderSn']) ? $data['orderSn'] : orderSn::getOrderSn();
  128. $data['payStatus'] = 0;
  129. $shopId = $data['shopId'] ?? 0;
  130. $shop = ShopClass::getLockById($shopId);
  131. if (empty($shop)) {
  132. util::fail('没有找到门店85');
  133. }
  134. $mainId = $shop->mainId ?? 0;
  135. $main = MainClass::getLockById($mainId);
  136. if (empty($main)) {
  137. util::fail('没有main信息呢!');
  138. }
  139. $riseNum = StatOrderCountClass::addOrder($shop, $main);
  140. $address = $data['address'] ?? '';
  141. $floor = $data['floor'] ?? '';
  142. $fullAddress = $address . $floor;
  143. $data['fullAddress'] = $fullAddress;
  144. //将花店每月的总订单数作为编号
  145. $data['sendNum'] = date('j') . $riseNum;
  146. $return = self::add($data, true);
  147. $main->unPayOrder += 1;
  148. $main->totalOrder += 1;
  149. $main->save();
  150. return $return;
  151. }
  152. public static function getByOrderSn($orderSn)
  153. {
  154. return self::getByCondition(['orderSn' => $orderSn]);
  155. }
  156. //评价 ssh 2019.12.16
  157. public static function comment($data)
  158. {
  159. $id = $data['id'];
  160. unset($data['id']);
  161. return self::updateById($id, $data);
  162. }
  163. //订单
  164. public static function getOrderById($id)
  165. {
  166. $order = self::getById($id);
  167. if (empty($order)) {
  168. return [];
  169. }
  170. $orderSn = $order['orderSn'] ?? '';
  171. if (empty($orderSn)) {
  172. return [];
  173. }
  174. $goodsList = OrderGoodsClass::getAllByCondition(['orderSn' => $orderSn], null, '*');
  175. if (!empty($goodsList)) {
  176. foreach ($goodsList as $key => $val) {
  177. $shortCover = $val['cover'] ?? '';
  178. $cover = imgUtil::groupImg($shortCover);
  179. $goodsList[$key]['cover'] = $cover;
  180. $goodsList[$key]['shortCover'] = $shortCover;
  181. }
  182. }
  183. $order['goodsInfoList'] = $goodsList;
  184. $itemList = OrderItemClass::getAllByCondition(['orderSn' => $orderSn], null, '*');
  185. if (!empty($itemList)) {
  186. foreach ($itemList as $key => $item) {
  187. $shortCover = $item['cover'] ?? '';
  188. $cover = imgUtil::groupImg($shortCover);
  189. $itemList[$key]['cover'] = $cover;
  190. $itemList[$key]['shortCover'] = $shortCover;
  191. }
  192. }
  193. $order['itemList'] = $itemList;
  194. return $order;
  195. }
  196. //根据订单查询 ssh 2020.1.4
  197. public static function getOrderBySn($orderSn)
  198. {
  199. $order = self::getByCondition(['orderSn' => $orderSn]);
  200. if (empty($order)) {
  201. return [];
  202. }
  203. $goodsList = OrderGoodsClass::getAllByCondition(['orderSn' => $orderSn], null, '*');
  204. if (!empty($goodsList)) {
  205. foreach ($goodsList as $key => $val) {
  206. $shortCover = $val['cover'] ?? '';
  207. $cover = imgUtil::groupImg($shortCover);
  208. $goodsList[$key]['cover'] = $cover;
  209. }
  210. }
  211. $order['goodsInfoList'] = $goodsList;
  212. $itemList = OrderItemClass::getAllByCondition(['orderSn' => $orderSn], null, '*');
  213. if (!empty($itemList)) {
  214. foreach ($itemList as $key => $item) {
  215. $shortCover = $item['cover'] ?? '';
  216. $cover = imgUtil::groupImg($shortCover);
  217. $itemList[$key]['cover'] = $cover;
  218. }
  219. }
  220. $order['itemList'] = $itemList;
  221. return $order;
  222. }
  223. //返回随机优惠金额 ssh 2019.9.12
  224. public static function getRandDiscount($amount)
  225. {
  226. if ($amount <= 0) {
  227. return 0;
  228. }
  229. $randDiscount = self::$randDiscount;
  230. krsort($randDiscount);
  231. $rand = 0;
  232. foreach ($randDiscount as $currentAmount => $val) {
  233. if ($amount >= $currentAmount) {
  234. $rand = rand($val[0], $val[1]);
  235. break;
  236. }
  237. }
  238. return $rand / 10;
  239. }
  240. //订单完成 ssh 2020.3.12
  241. public static function complete($order, $currentFlow)
  242. {
  243. $id = $order['id'];
  244. self::updateById($id, ['status' => OrderClass::ORDER_STATUS_COMPLETE, 'currentFlow' => $currentFlow]);
  245. }
  246. //转化出送到时间 ssh 2020.3.15
  247. public static function getReachTime($order)
  248. {
  249. $reachTime = '';
  250. if (isset($order['reachDate']) && !empty($order['reachDate'])) {
  251. $prev = date("n-j", strtotime($order['reachDate']));
  252. $periodData = self::$reachPeriod;
  253. $periodKey = $order['reachPeriod'];
  254. $period = isset($periodData[$periodKey]) ? $periodData[$periodKey] : '上午';
  255. $reachTime = $prev . ' ' . $period;
  256. }
  257. return $reachTime;
  258. }
  259. //支付成功获取unionId ssh 2020.5.12
  260. public static function payToUpdateUnionId($merchant, $merchantExtend, $orderSn, $user)
  261. {
  262. $mcId = $merchantExtend['wxPayMerchantId'];
  263. $miniOpenId = isset($user['miniOpenId']) ? $user['miniOpenId'] : '';
  264. $unionId = isset($user['unionId']) ? $user['unionId'] : '';
  265. $userId = $user['id'];
  266. if (empty($unionId)) {
  267. $unionId = miniUtil::getPaidUnionId($merchant, $miniOpenId, $mcId, (string)$orderSn, 0);
  268. if (!empty($unionId)) {
  269. $findUser = UserClass::getByUnionId($unionId);
  270. if (empty($findUser)) {
  271. UserClass::updateByCondition(['id' => $userId], ['unionId' => $unionId]);
  272. } else {
  273. UserClass::mergeUser($findUser, $user);
  274. }
  275. }
  276. }
  277. }
  278. //订单到期未付款的订单置成取消状态 ssh 2020.5.25
  279. public static function setExpire($order)
  280. {
  281. $now = time();
  282. if (!empty($order['deadline']) && $now > $order['deadline']) {
  283. if ($order['status'] == 0) {
  284. $id = $order['id'];
  285. OrderClass::updateById($id, ['status' => 4]);
  286. }
  287. }
  288. }
  289. /**
  290. * 应付金额为0:走零售订单完整支付成功流程(状态、流水、库存标记、待付款计数等)
  291. */
  292. public static function completeZeroAmountPay($orderId)
  293. {
  294. $order = \bizHd\order\classes\OrderClass::getLockById($orderId);
  295. if (empty($order)) {
  296. util::fail('没有找到订单');
  297. }
  298. if ((int)$order->payStatus === 1) {
  299. return $order;
  300. }
  301. if ((int)$order->status !== self::ORDER_STATUS_UN_PAY) {
  302. util::fail('订单不是待付款状态');
  303. }
  304. $payWay = dict::getDict('payWay', 'wxPay');
  305. \bizHd\order\classes\OrderClass::payAfter($order, $payWay);
  306. return $order;
  307. }
  308. //支付成功需要处理的流程 ssh 2020.5.25
  309. public static function payAfter($order, $updateData)
  310. {
  311. $orderId = $order['id'];
  312. $orderSn = $order['orderSn'];
  313. $now = time();
  314. self::updateById($orderId, $updateData);
  315. //发货流程的完成下单
  316. OrderSendClass::placeOrder(['orderId' => $orderId, 'payTime' => $now, 'orderSn' => $orderSn]);
  317. $sjId = $order['sjId'];
  318. $asset = MerchantAssetClass::getBySjId($sjId);
  319. //增加订单数
  320. StatOrderClass::addOrder($sjId, $asset);
  321. //如果门店和朋友圈订单并且没有填写收花人的直接将订单置为自取并且是完成状态
  322. if (isset($order['fromType']) && in_array($order['fromType'], [0, 2])) {
  323. if (isset($order['receiveMobile']) && isset($order['address'])) {
  324. if (empty($order['receiveMobile']) && empty($order['address'])) {
  325. OrderSendClass::withoutSend($order);
  326. }
  327. }
  328. }
  329. }
  330. }