OrderClass.php 12 KB

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