OrderClass.php 12 KB

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