OrderClass.php 42 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079
  1. <?php
  2. namespace bizHd\order\classes;
  3. use biz\goods\classes\GoodsClass;
  4. use biz\goods\models\Goods;
  5. use biz\shop\classes\ShopAdminClass;
  6. use biz\shop\classes\ShopCapitalClass;
  7. use biz\shop\classes\ShopClass;
  8. use biz\shop\classes\ShopExtClass;
  9. use biz\shop\models\Shop;
  10. use biz\stat\classes\StatKdClass;
  11. use biz\stat\classes\StatOrderCountClass;
  12. use biz\stat\classes\StatSaleClass;
  13. use biz\stock\classes\GoodsStockRecordClass;
  14. use biz\wx\classes\WxMessageClass;
  15. use bizGhs\order\classes\CheckOrderClass;
  16. use bizGhs\product\classes\ProductClass;
  17. use bizGhs\stat\classes\StatYjClass;
  18. use bizGhs\stock\classes\StockRecordClass;
  19. use bizGhs\ws\services\WsService;
  20. use bizHd\custom\classes\CustomClass;
  21. use bizHd\merchant\classes\SjClass;
  22. use bizHd\order\classes\OrderItemClass;
  23. use bizHd\refund\classes\HdRefundClass;
  24. use bizHd\shop\classes\MainClass;
  25. use bizHd\shop\classes\ShopMoneyChangeClass;
  26. use bizHd\stat\classes\StatIncomeClass;
  27. use bizHd\stat\classes\StatOrderClass;
  28. use bizHd\user\classes\UserClass;
  29. use bizHd\work\classes\WorkClass;
  30. use bizHd\wx\classes\WxOpenClass;
  31. use common\components\dict;
  32. use common\components\imgUtil;
  33. use common\components\miniUtil;
  34. use common\components\noticeUtil;
  35. use common\components\orderSn;
  36. use common\components\printUtil;
  37. use common\components\util;
  38. use Yii;
  39. use bizHd\base\classes\BaseClass;
  40. use common\components\lakala\Lakala;
  41. class OrderClass extends BaseClass
  42. {
  43. public static $baseFile = '\bizHd\order\models\Order';
  44. //已取消
  45. const ORDER_STATUS_CANCEL = 5;
  46. //完成
  47. const ORDER_STATUS_COMPLETE = 4;
  48. //配送中
  49. const ORDER_STATUS_SENDING = 3;
  50. //待配送
  51. const ORDER_STATUS_UN_SEND = 2;
  52. //待付款
  53. const ORDER_STATUS_UN_PAY = 1;
  54. //订单总流程数
  55. const ORDER_TOTAL_FLOW = 4;
  56. //未确认配送方式
  57. const SEND_TYPE_UNKNOWN = 0;
  58. //自取
  59. const SEND_TYPE_NO = 1;
  60. //自己送
  61. const SEND_TYPE_MYSELF = 2;
  62. //快递送
  63. const SEND_TYPE_THIRD = 3;
  64. //正常订单
  65. const REFUND_NO = 0;
  66. //已退款订单
  67. const REFUND_YES = 1;
  68. //送达时间段
  69. public static $reachPeriod = [0 => '上午', 1 => '下午', 2 => '晚上'];
  70. //优惠金额
  71. public static $randDiscount = [
  72. 50 => [1, 3],
  73. 100 => [5, 9],
  74. 300 => [10, 15],
  75. 500 => [16, 18],
  76. 1000 => [18, 30],
  77. 20000 => [90, 100],
  78. ];
  79. //因为开负数订单时引导库存变化 ssh 20250731
  80. public static function shPayChange($order, $shop, $staff, $originOrder = null)
  81. {
  82. $forward = $order['forward'] ?? 0;
  83. if ($forward == 0) {
  84. return false;
  85. }
  86. $forwardStock = $order['forwardStock'] ?? 0;
  87. $orderSn = $order['orderSn'] ?? '';
  88. $orderItemList = OrderItemClass::getAllByCondition(['orderSn' => $orderSn], null, '*');
  89. $orderGoodsList = OrderGoodsClass::getAllByCondition(['orderSn' => $orderSn], null, '*');
  90. if (empty($orderItemList) && empty($orderGoodsList)) {
  91. return false;
  92. }
  93. $shopId = $shop->id ?? 0;
  94. $sjId = $shop->sjId ?? 0;
  95. $adminId = $staff->adminId;
  96. $staffName = $staff->name ?? '';
  97. $mainId = $shop->mainId;
  98. if (!empty($orderItemList)) {
  99. $itemInfo = [];
  100. $ids = array_column($orderItemList, 'itemId');
  101. $infoList = ProductClass::getAllByCondition(['id' => ['in', $ids]], null, '*', 'id');
  102. foreach ($orderItemList as $orderItem) {
  103. $unitType = $orderItem['unitType'] ?? 0;
  104. $num = $orderItem['num'] ?? 0;
  105. $productId = $orderItem['itemId'] ?? 0;
  106. $info = $infoList[$productId] ?? [];
  107. $stock = $info['stock'] ?? 0;
  108. $ratio = $info['ratio'] ?? 20;
  109. $map = ProductClass::formatStock($stock, $ratio);
  110. $bigNum = $map['bigNum'] ?? 0;
  111. $smallNum = $map['smallNum'] ?? 0;
  112. $num = $forwardStock == 0 ? $num * 2 : $num;
  113. if ($unitType == 0) {
  114. $bigNum = $bigNum + $num;
  115. } else {
  116. $smallNum = $smallNum + $num;
  117. }
  118. $itemInfo[] = ['productId' => $productId, 'bigNum' => $bigNum, 'smallNum' => $smallNum];
  119. }
  120. $remark = $forwardStock == 0 ? '开售后付款单,退货并退款' : '开售后付款单,只退款';
  121. $itemData = [
  122. 'remark' => $remark,
  123. 'itemInfo' => $itemInfo,
  124. 'sjId' => $sjId,
  125. 'shopId' => $shopId,
  126. 'adminId' => $adminId,
  127. 'staffName' => $staffName,
  128. ];
  129. CheckOrderClass::opOrder($itemData);
  130. }
  131. if (!empty($orderGoodsList)) {
  132. foreach ($orderGoodsList as $orderGoods) {
  133. $goodsId = $orderGoods['goodsId'] ?? 0;
  134. $num = $orderGoods['num'] ?? 0;
  135. $num = $forwardStock == 0 ? $num * 2 : $num;
  136. $ret = \bizHd\goods\classes\GoodsClass::addStock($goodsId, $mainId, $num);
  137. $oldStock = $ret['oldStock'] ?? 0;
  138. $newStock = $ret['newStock'] ?? 0;
  139. $recordData = [];
  140. $recordData['sjId'] = $sjId;
  141. $recordData['shopId'] = $shopId;
  142. $recordData['mainId'] = $mainId;
  143. $recordData['orderSn'] = '';
  144. $recordData['goodsId'] = $goodsId;
  145. $recordData['goodsNum'] = $num;
  146. $recordData['oldStock'] = $oldStock;
  147. $recordData['newStock'] = $newStock;
  148. $recordData['relateName'] = $staffName;
  149. GoodsStockRecordClass::pdGoods($recordData);
  150. }
  151. }
  152. if (is_object($order) && !empty($originOrder)) {
  153. //建立关系
  154. $originOrder->hasForward = 1;
  155. $originOrder->save();
  156. $orderId = $originOrder->id ?? 0;
  157. $orderSn = $originOrder->orderSn ?? '';
  158. $shOrderId = $order->id ?? 0;
  159. $shOrderSn = $order->orderSn ?? '';
  160. $map = ['orderId' => $orderId, 'orderSn' => $orderSn, 'shOrderId' => $shOrderId, 'shOrderSn' => $shOrderSn];
  161. OrderForwardClass::add($map, true);
  162. }
  163. return true;
  164. }
  165. /**
  166. * @param $order
  167. * @param $mainId
  168. * @return array
  169. * $shAddPay 0走正常流程售后,1走售后付款单
  170. * $shAddPayReason 走售后付款单的原因:0、售后金额大于剩余欠款(剩余欠款可能没有了,可能还有);1、可售后时间到了
  171. */
  172. public static function ifShPay($order, $main, $refundAmount = 0)
  173. {
  174. $shAddPay = 0;
  175. $shAddPayReason = 0;
  176. $payStatus = $order['payStatus'] ?? 0;
  177. if ($payStatus == 1) {
  178. if ($order['payWay'] == 3) {
  179. if ($order['remainDebtPrice'] <= 0) {
  180. //不管是否要售后,没有剩余欠款了,只能走开负数订单模式
  181. $shAddPay = 1;
  182. } else {
  183. if ($refundAmount > 0 && $refundAmount > $order['remainDebtPrice']) {
  184. //如果需要售后,并且售后金额大于剩余欠款,走开负数订单模式
  185. $shAddPay = 1;
  186. }
  187. }
  188. }
  189. $cRet = OrderClass::couldRefund($order, $main);
  190. $could = $cRet['could'];
  191. if (!$could) {
  192. //可售后时间到了
  193. $shAddPay = 1;
  194. $shAddPayReason = 1;
  195. }
  196. }
  197. return ['shAddPay' => $shAddPay, 'shAddPayReason' => $shAddPayReason];
  198. }
  199. //订单是否过了可以售后的时间 ssh 20250730
  200. // $could false 过了售后时间,不能售后,true可以
  201. public static function couldRefund($order, $main)
  202. {
  203. $refundLimit = $main->refundLimit ?? 0;
  204. $payTime = $order['payTime'];
  205. $currentPayTime = strtotime($payTime);
  206. $addDateTime = $refundLimit * 86400;
  207. $couldRefundTime = $currentPayTime + $addDateTime;
  208. $now = time();
  209. $could = $couldRefundTime > $now;
  210. return ['could' => $could];
  211. }
  212. public static function valid($order, $mainId)
  213. {
  214. if (!isset($order->mainId) || $order->mainId != $mainId) {
  215. Yii::info(json_encode($order));
  216. Yii::info('mainId: ' . $mainId);
  217. util::fail('无法访问的订单');
  218. }
  219. return true;
  220. }
  221. //添加订单 ssh 2019.12.5
  222. public static function addOrder($data)
  223. {
  224. $data['orderSn'] = $data['orderSn'] ?? orderSn::getOrderSn();
  225. $data['payStatus'] = $data['payStatus'] ?? 0;
  226. $shopId = $data['shopId'] ?? 0;
  227. //累计订单增加
  228. $shop = ShopClass::getLockById($shopId);
  229. if (empty($shop)) {
  230. util::fail('没有找到门店信息54');
  231. }
  232. $mainId = $shop->mainId ?? 0;
  233. $main = MainClass::getLockById($mainId);
  234. if (empty($main)) {
  235. util::fail('没有找到main信息');
  236. }
  237. $main->unPayOrder += 1;
  238. $main->totalOrder += 1;
  239. $main->save();
  240. $address = $data['address'] ?? '';
  241. $floor = $data['floor'] ?? '';
  242. if (isset($data['fullAddress']) == false || empty($data['fullAddress'])) {
  243. $data['fullAddress'] = $address . $floor;
  244. }
  245. //非自取订单考虑配送时间
  246. $data['reachDate'] = isset($data['reachDate']) && !empty($data['reachDate']) ? $data['reachDate'] : date("Y-m-d");
  247. $sendType = $data['sendType'] ?? 0;
  248. if ($sendType != 1) {
  249. if (isset($data['reachPeriod']) == false || empty($data['reachPeriod'])) {
  250. util::fail('请选择配送时间');
  251. }
  252. $data['reachTime'] = strtotime($data['reachDate'] . ' ' . $data['reachPeriod']);
  253. }
  254. //将花店每月的总订单数作为编号
  255. $riseNum = StatOrderCountClass::addOrder($shop, $main);
  256. $data['sendNum'] = date("d") . $riseNum;
  257. $data['totalFlow'] = OrderClass::ORDER_TOTAL_FLOW;
  258. return self::add($data, true);
  259. }
  260. public static function getByOrderSn($orderSn)
  261. {
  262. return self::getByCondition(['orderSn' => $orderSn]);
  263. }
  264. //评价 ssh 2019.12.16
  265. public static function comment($data)
  266. {
  267. $id = $data['id'];
  268. unset($data['id']);
  269. return self::updateById($id, $data);
  270. }
  271. public static function getFullInfo($id)
  272. {
  273. $order = self::getById($id);
  274. if (empty($order)) {
  275. return [];
  276. }
  277. $sn = $order['orderSn'] ?? '';
  278. $order['goodsInfoList'] = OrderGoodsClass::getListBySn($sn);
  279. $order['itemInfoList'] = OrderItemClass::getListBySn($sn);
  280. $workList = WorkClass::getAllByCondition(['orderId' => $id], null, '*');
  281. if (!empty($workList)) {
  282. foreach ($workList as $key => $work) {
  283. $shortCover = $work['cover'] ?? '';
  284. $workList[$key]['shortCover'] = $shortCover;
  285. $workList[$key]['smallCover'] = imgUtil::groupImg($shortCover) . "?x-oss-process=image/resize,m_fill,h_130,w_130";
  286. }
  287. }
  288. $order['workList'] = $workList;
  289. $debtPrice = $order['debtPrice'] ?? 0;
  290. $remainDebtPrice = $order['remainDebtPrice'] ?? 0;
  291. $orderId = $order['id'] ?? 0;
  292. $osList = [];
  293. if ($debtPrice > $remainDebtPrice) {
  294. $osList = OrderSettleClass::getAllByCondition(['orderId' => $orderId, 'status' => 1], null, '*', null, true);
  295. }
  296. //结账记录关系
  297. $order['orderSettleList'] = $osList;
  298. $order['today'] = date("Y-m-d");
  299. $order['tomorrow'] = date("Y-m-d", strtotime("+1 day"));
  300. return $order;
  301. }
  302. //订单
  303. public static function getOrderById($id)
  304. {
  305. $order = self::getById($id);
  306. if (empty($order)) {
  307. return [];
  308. }
  309. $sn = $order['orderSn'] ?? '';
  310. $order['goodsInfoList'] = OrderGoodsClass::getListBySn($sn);
  311. return $order;
  312. }
  313. //根据订单查询 ssh 2020.1.4
  314. public static function getOrderBySn($orderSn)
  315. {
  316. $order = self::getByCondition(['orderSn' => $orderSn]);
  317. if (empty($order)) {
  318. return [];
  319. }
  320. $id = $order['id'];
  321. $order['goodsInfoList'] = OrderGoodsClass::getGoodsListById($id);
  322. return $order;
  323. }
  324. //返回随机优惠金额 ssh 2019.9.12
  325. public static function getRandDiscount($amount)
  326. {
  327. if ($amount <= 0) {
  328. return 0;
  329. }
  330. $randDiscount = self::$randDiscount;
  331. krsort($randDiscount);
  332. $rand = 0;
  333. foreach ($randDiscount as $currentAmount => $val) {
  334. if ($amount >= $currentAmount) {
  335. $rand = rand($val[0], $val[1]);
  336. break;
  337. }
  338. }
  339. return $rand / 10;
  340. }
  341. //第三方支付后回调处理流程 ssh 2021.4.30
  342. public static function thirdPay($payWay, $orderSn, $totalFee, $attach, $transactionId)
  343. {
  344. $order = self::getByCondition(['orderSn' => $orderSn], true);
  345. if (empty($order)) {
  346. noticeUtil::push('散客下单,付款成功,支付回调,单号:' . $orderSn . ' 金额:' . $totalFee . ' 回调id:' . $transactionId . ' 附件:' . $attach . ',没有找到订单', '15280215347');
  347. $msg = "没有找到零售订单 orderSn:{$orderSn}";
  348. Yii::info($msg);
  349. util::fail($msg);
  350. }
  351. if ($order->mainPay != $totalFee) {
  352. noticeUtil::push("散客下单,付款成功,支付回调错误,零售订单金额与回调通知金额不一致 orderSn:{$orderSn} {$order->mainPay} {$totalFee} 附件:{$attach}", '15280215347');
  353. $msg = "零售订单金额与回调通知金额不一致 orderSn:{$orderSn} {$order->mainPay} {$totalFee}";
  354. Yii::info($msg);
  355. util::fail($msg);
  356. }
  357. $id = $order->id;
  358. $order = self::getLockById($id);
  359. $date = date("Y-m-d H:i:s");
  360. $order->payTime = $date;
  361. $order->thirdNo = $transactionId;
  362. $order->onlinePay = dict::getDict('onlinePay', 'yes');
  363. $order->save();
  364. self::payAfter($order, $payWay);
  365. }
  366. //订单完成 ssh 2021.4.20
  367. public static function payAfter($order, $payWay)
  368. {
  369. if (empty($order)) {
  370. util::fail('没有找到订单');
  371. }
  372. if (!isset($order->status)) {
  373. util::fail('没有找到订单的状态');
  374. }
  375. $orderSn = $order->orderSn ?? '';
  376. if ($order->status == self::ORDER_STATUS_CANCEL) {
  377. $shopId = $order->shopId;
  378. $shop = ShopClass::getById($shopId, true);
  379. if (empty($shop)) {
  380. noticeUtil::push('注意,零售订单:' . $orderSn . ',付款成功但订单已取消,没有找到门店,关键词 cancel_get_money', '15280215347');
  381. util::stop("SUCCESS");
  382. }
  383. Yii::$app->params['errorReport'] = 1;
  384. try {
  385. //恢复取消的订单
  386. self::recoverOrderToPay($order);
  387. noticeUtil::push('注意,零售订单:' . $orderSn . ',付款成功,取消的订单,已恢复 SUCCESS', '15280215347');
  388. } catch (\Exception $exception) {
  389. $ret = HdRefundClass::onlyRefundAmount($shop, $order);
  390. if ($ret['status'] == 0) {
  391. $errMsg = $exception->getMessage();
  392. noticeUtil::push('注意,零售订单:' . $orderSn . ',付款成功但订单取消。订单没有恢复成功,原因' . $errMsg . ',也没有退款成功。请手动处理。关键词 cancel_get_money', '15280215347');
  393. }
  394. util::stop("SUCCESS");
  395. }
  396. }
  397. if ($order->payStatus == 1) {
  398. noticeUtil::push('注意,零售订单:' . $orderSn . ',已经付过款了,无需重复请求和操作', '15280215347');
  399. util::fail('订单已经付过款了');
  400. }
  401. $orderId = $order->id ?? 0;
  402. $customId = $order->customId ?? 0;
  403. if ($order->fromType == 3) {
  404. //客服下的单要配送流程
  405. $order->status = self::ORDER_STATUS_UN_SEND;
  406. } else {
  407. $order->status = self::ORDER_STATUS_COMPLETE;
  408. }
  409. $order->payWay = $payWay;
  410. $order->payStatus = 1;
  411. $order->payTime = date("Y-m-d H:i:s");
  412. $order->stockChange = 1;
  413. $order->save();
  414. $myCustom = CustomClass::getLockById($customId);
  415. if (!empty($myCustom)) {
  416. if ($order->forward == 0) {
  417. $myCustom->recentExpend = date("Y-m-d H:i:s");
  418. $myCustom->visitTime = date("Y-m-d H:i:s");
  419. $myCustom->save();
  420. }
  421. }
  422. \bizMall\order\classes\OrderGoodsClass::stockChange($order);
  423. $realPrice = $order->realPrice ?? 0;
  424. $shopId = $order->shopId;
  425. $shop = ShopClass::getLockById($shopId);
  426. if (empty($shop)) {
  427. util::fail('没有找到门店,编号5115');
  428. }
  429. $mainId = $shop->mainId ?? 0;
  430. $main = MainClass::getLockById($mainId);
  431. if (empty($main)) {
  432. util::fail('没有main信息,编号3096');
  433. }
  434. $main->unSendOrder += 1;
  435. $main->unPayOrder -= 1;
  436. $currentTotalIncome = bcadd($main->totalIncome, $realPrice, 2);
  437. $main->totalIncome = $currentTotalIncome;
  438. $main->save();
  439. $capitalType = dict::getDict('capitalType', 'xhOrder', 'id');
  440. $shopAdminId = $order->shopAdminId ?? 0;
  441. $shopAdminName = $order->shopAdminName ?? '';
  442. $sjId = $order->sjId;
  443. $fromType = $order->fromType ?? 1;
  444. $event = !empty($shopAdminId) ? '员工开单' : '客户下单';
  445. $capital = [
  446. 'capitalType' => $capitalType,
  447. 'relateId' => $orderId,
  448. 'io' => 1,
  449. 'totalIncome' => $currentTotalIncome,
  450. 'payWay' => $payWay,
  451. 'event' => $event,
  452. 'sjId' => $sjId,
  453. 'shopId' => $shopId,
  454. 'shopAdminId' => $shopAdminId,
  455. 'shopAdminName' => $shopAdminName,
  456. 'amount' => $realPrice,
  457. 'fromType' => $fromType,
  458. 'mainId' => $mainId,
  459. ];
  460. ShopCapitalClass::addCapital($capital);
  461. //每天和每月收入增加
  462. //StatIncomeClass::updateOrInsert($main, $shop, $realPrice);
  463. //今日订单+1
  464. //StatOrderClass::updateOrInsert($main, $shop);
  465. //销售收入增加
  466. //StatSaleClass::replace($main, $shop, $realPrice);
  467. //各渠道收入金额统计
  468. //StatKdClass::replace($shop, $realPrice, $payWay);
  469. //当天同个人同地址的订单汇总
  470. $sameDate = date("Y_m_d");
  471. $sameAddress = $order->showAddress ?? '';
  472. $addressMd5 = md5($sameAddress);
  473. $sameKey = $customId . '_' . $addressMd5 . '_' . $sameDate . '_retail';
  474. $sameString = Yii::$app->redis->executeCommand('GET', [$sameKey]);
  475. if (!empty($sameString)) {
  476. $newString = $sameString . ',' . $orderId;
  477. $newIdsArray = explode(',', $newString);
  478. if (count($newIdsArray) <= 90) {
  479. Yii::$app->redis->executeCommand('SETEX', [$sameKey, 186400, $newString]);
  480. foreach ($newIdsArray as $currentOrderId) {
  481. OrderClass::updateById($currentOrderId, ['sameTimeIds' => $newString]);
  482. }
  483. //noticeUtil::push($sameKey . '' . $sameAddress . " 花掌柜当天汇总ids:" . $newString, '15280215347');
  484. }
  485. } else {
  486. Yii::$app->redis->executeCommand('SETEX', [$sameKey, 186400, $orderId]);
  487. $order->sameTimeIds = $orderId;
  488. $order->save();
  489. //noticeUtil::push($sameKey . '' . $sameAddress . " 花掌柜当天汇总ids:" . $orderId, '15280215347');
  490. }
  491. //客户在线支付下单增加商家可提现余额
  492. if (isset($order->onlinePay) && $order->onlinePay == dict::getDict('onlinePay', 'yes')) {
  493. $amount = $order->mainPay;
  494. ShopClass::customKdAddBalance($main, $shop, $amount, $order, $capitalType);
  495. }
  496. //客户使用余额付款
  497. if ($payWay == dict::getDict('payWay', 'balancePay')) {
  498. CustomClass::payToChangeBalance($order);
  499. }
  500. //有欠款时
  501. if ($payWay == dict::getDict('payWay', 'debtPay')) {
  502. if (isset($order->remainDebtPrice) && $order->remainDebtPrice > 0) {
  503. CustomClass::cgDebtAmountAdd($myCustom, $order);
  504. }
  505. }
  506. //全部现金付款余额增加
  507. if ($payWay == dict::getDict('payWay', 'cash')) {
  508. $moneyBalance = bcadd($main->money, $order->mainPay, 2);
  509. $main->money = $moneyBalance;
  510. $main->save();
  511. $moneyEvent = "客户现金下单" . floatval($order->mainPay) . "元(单号 {$order->orderSn})";
  512. $moneyRemark = '';
  513. $change = [
  514. 'relateId' => $orderId,
  515. 'amount' => $order->mainPay,
  516. 'balance' => $moneyBalance,
  517. 'io' => 1,
  518. 'mainId' => $mainId,
  519. 'capitalType' => $capitalType,
  520. 'ptStyle' => dict::getDict('ptStyle', 'hd'),
  521. 'event' => $moneyEvent,
  522. 'remark' => $moneyRemark,
  523. ];
  524. ShopMoneyChangeClass::addData($change);
  525. } else {
  526. //部分现金付款
  527. $orderCash = $order->cash ?? 0;
  528. $orderCash = floatval($orderCash);
  529. if ($orderCash > 0) {
  530. $moneyBalance = bcadd($main->money, $orderCash, 2);
  531. $main->money = $moneyBalance;
  532. $main->save();
  533. $moneyEvent = "客户部分现金付款{$orderCash}元(单号 {$order->orderSn})";
  534. $moneyRemark = '';
  535. $change = [
  536. 'relateId' => $orderId,
  537. 'amount' => $orderCash,
  538. 'balance' => $moneyBalance,
  539. 'io' => 1,
  540. 'mainId' => $mainId,
  541. 'capitalType' => $capitalType,
  542. 'ptStyle' => dict::getDict('ptStyle', 'hd'),
  543. 'event' => $moneyEvent,
  544. 'remark' => $moneyRemark,
  545. ];
  546. ShopMoneyChangeClass::addData($change);
  547. }
  548. }
  549. }
  550. //转化出送到时间 ssh 2020.3.15
  551. public static function getReachTime($order)
  552. {
  553. $reachTime = '';
  554. if (isset($order['reachDate']) && !empty($order['reachDate'])) {
  555. $prev = date("n-j", strtotime($order['reachDate']));
  556. $periodData = self::$reachPeriod;
  557. $periodKey = $order['reachPeriod'];
  558. $period = isset($periodData[$periodKey]) ? $periodData[$periodKey] : '上午';
  559. $reachTime = $prev . ' ' . $period;
  560. }
  561. return $reachTime;
  562. }
  563. //支付成功获取unionId ssh 2020.5.12
  564. public static function payToUpdateUnionId($merchant, $merchantExtend, $orderSn, $user)
  565. {
  566. $mcId = $merchantExtend['wxPayMerchantId'];
  567. $miniOpenId = isset($user['miniOpenId']) ? $user['miniOpenId'] : '';
  568. $unionId = isset($user['unionId']) ? $user['unionId'] : '';
  569. $userId = $user['id'];
  570. if (empty($unionId)) {
  571. $unionId = miniUtil::getPaidUnionId($merchant, $miniOpenId, $mcId, (string)$orderSn, 0);
  572. if (!empty($unionId)) {
  573. $findUser = UserClass::getByUnionId($unionId);
  574. if (empty($findUser)) {
  575. UserClass::updateByCondition(['id' => $userId], ['unionId' => $unionId]);
  576. } else {
  577. UserClass::mergeUser($findUser, $user);
  578. }
  579. }
  580. }
  581. }
  582. //订单取消 ssh 20220516
  583. public static function setExpire($order, $force = false)
  584. {
  585. $now = time();
  586. if ($order->deadline > $now && $force == false) {
  587. return false;
  588. }
  589. if ($order->status != 1) {
  590. return false;
  591. }
  592. $order->status = 5;
  593. $order->cancelTime = date("Y-m-d H:i:s");
  594. $order->save();
  595. //暂时只考虑花材库存的回滚。商品只有下单成功了才会占用库存。
  596. $orderSn = $order->orderSn ?? '';
  597. $itemList = OrderItemClass::getAllByCondition(['orderSn' => $orderSn], null, '*', null, true);
  598. if (!empty($itemList)) {
  599. foreach ($itemList as $item) {
  600. $ptItemId = $item->ptItemId ?? 0;
  601. $sjId = $item->sjId ?? 0;
  602. $shopId = $item->shopId ?? 0;
  603. $mainId = $item->mainId ?? 0;
  604. $ratio = $item->ratio ?? 20;
  605. $unitType = $item->unitType ?? 0;
  606. if ($unitType == 0) {
  607. $bigNum = $item->num;
  608. $smallNum = 0;
  609. } else {
  610. $bigNum = 0;
  611. $smallNum = $item->num;
  612. }
  613. $itemId = $item->itemId ?? 0;
  614. $stockInfo = ProductClass::addStock($itemId, $bigNum, $smallNum);
  615. $recordData = [];
  616. $recordData['sjId'] = $sjId;
  617. $recordData['shopId'] = $shopId;
  618. $recordData['mainId'] = $mainId;
  619. $recordData['itemId'] = $ptItemId;
  620. $recordData['itemNum'] = ProductClass::mergeItemNum($bigNum, $smallNum, $ratio);
  621. $recordData['oldStock'] = $stockInfo['oldStock'];
  622. $recordData['newStock'] = $stockInfo['newStock'];
  623. $recordData['productId'] = $itemId;
  624. $recordData['orderSn'] = $orderSn;
  625. $recordData['relateName'] = '系统';
  626. StockRecordClass::addSellStockOrderCancelRecord($recordData);
  627. }
  628. }
  629. $shopId = $order->shopId ?? 0;
  630. $shop = ShopClass::getById($shopId, true);
  631. if (!empty($shop)) {
  632. $merchantPrivateKeyPath = Yii::getAlias("@vendor/lakala") . '/production/api_private_key.pem';
  633. $lklCertificatePath = Yii::getAlias("@vendor/lakala") . '/production/lkl-apigw-v1.cer';
  634. $params = [
  635. 'appid' => 'OP00002119',
  636. 'serial_no' => '018b08cfddbd',
  637. 'merchant_no' => $shop->lklSjNo,
  638. 'term_no' => $shop->lklScanTermNo,
  639. 'merchantPrivateKeyPath' => $merchantPrivateKeyPath,
  640. 'lklCertificatePath' => $lklCertificatePath,
  641. ];
  642. $laResource = new Lakala($params);
  643. $closeParams = [
  644. 'orderSn' => $orderSn,
  645. ];
  646. $response = $laResource->close($closeParams);
  647. if (isset($response['code']) == false || $response['code'] != 'BBS00000') {
  648. //noticeUtil::push('零售订单:' . $orderSn . ',关单没有成功。', '15280215347');
  649. } else {
  650. //noticeUtil::push('零售订单:' . $orderSn . ',关单成功。', '15280215347');
  651. }
  652. }
  653. }
  654. // 恢复订单 -- 即:把订单的状态从取消状态变成待付款状态
  655. public static function recoverOrderToPay($order)
  656. {
  657. $now = time();
  658. $order->status = 1;
  659. $order->deadline = $now + 86400;
  660. $order->save();
  661. // 重新占用库存(与取消订单时的库存回滚相反)
  662. $itemList = OrderItemClass::getAllByCondition(['orderSn' => $order->orderSn], null, '*', null, true);
  663. if (!empty($itemList)) {
  664. foreach ($itemList as $item) {
  665. $ptItemId = $item->ptItemId ?? 0;
  666. $sjId = $item->sjId ?? 0;
  667. $shopId = $item->shopId ?? 0;
  668. $mainId = $item->mainId ?? 0;
  669. $ratio = $item->ratio ?? 20;
  670. $unitType = $item->unitType ?? 0;
  671. if ($unitType == 0) {
  672. $bigNum = $item->num;
  673. $smallNum = 0;
  674. } else {
  675. $bigNum = 0;
  676. $smallNum = $item->num;
  677. }
  678. $itemId = $item->itemId ?? 0;
  679. // 重新扣减库存(占用库存)
  680. $stockInfo = ProductClass::decreaseStock($itemId, $bigNum, $smallNum, true);
  681. $recordData = [];
  682. $recordData['sjId'] = $sjId;
  683. $recordData['shopId'] = $shopId;
  684. $recordData['mainId'] = $mainId;
  685. $recordData['itemId'] = $ptItemId;
  686. $recordData['itemNum'] = ProductClass::mergeItemNum($bigNum, $smallNum, $ratio);
  687. $recordData['oldStock'] = $stockInfo['oldStock'];
  688. $recordData['newStock'] = $stockInfo['newStock'];
  689. $recordData['productId'] = $itemId;
  690. $recordData['orderSn'] = $order->orderSn;
  691. $recordData['relateName'] = $order->customName ?? '';
  692. StockRecordClass::hdKdAddRecord($recordData);
  693. }
  694. }
  695. return true;
  696. }
  697. // 扣库存
  698. protected static function decGoodsStock($orderGoods)
  699. {
  700. foreach ($orderGoods as $goods) {
  701. GoodsClass::counters([
  702. 'stock' => -$goods['num']
  703. ], [
  704. 'and',
  705. ['id' => $goods['goodsId']],
  706. ['<>', 'stock', Goods::FULL_STOCK]
  707. ]);
  708. }
  709. }
  710. //云打印 ssh 20210709
  711. public static function onlinePrint($order, $must = false, $port = 'front')
  712. {
  713. if (empty($order)) {
  714. return false;
  715. }
  716. if (!$must) {
  717. if ($order->needPrint != dict::getDict('needPrint', 'need')) {
  718. return false;
  719. }
  720. if ($order->payStatus != 1) {
  721. return false;
  722. }
  723. }
  724. if ($order->forward == 1) {
  725. //售后付款单暂时不能打印
  726. return false;
  727. }
  728. $shopId = $order->shopId ?? 0;
  729. $ext = ShopExtClass::getByCondition(['shopId' => $shopId]);
  730. $printSn = $ext['printSn'] ?? '';
  731. if ($port == 'make') {
  732. $printSn = $ext['makePrintSn'] ?? '';
  733. if ($order->fromType == 4) {
  734. $printSn = $ext['mtPrintSn'] ?? '';
  735. }
  736. $workList = WorkClass::getAllByCondition(['orderId' => $order->id], null, '*', null, true);
  737. $hasSh = 0;
  738. if (!empty($workList)) {
  739. foreach ($workList as $work) {
  740. $sh = $work->sh ?? 0;
  741. if ($sh == 1) {
  742. $hasSh = 1;
  743. }
  744. }
  745. }
  746. if ($hasSh == 1) {
  747. $printSn = $ext['printSn'] ?? '';
  748. }
  749. }
  750. if (empty($printSn)) {
  751. return false;
  752. }
  753. $order->printNum += 1;
  754. $order->save();
  755. $customId = $order->customId ?? 0;
  756. $custom = CustomClass::getById($customId);
  757. $debtAmount = $custom['debtAmount'] ?? 0;
  758. $respond = self::getPrintData($order, $debtAmount);
  759. $fromType = $order->fromType ?? dict::getDict('fromType', 'shop');
  760. $fromTypeMap = dict::getDict('fromTypeMap');
  761. $fromTypeName = $fromTypeMap[$fromType] ?? '门店';
  762. //客服下的单则显示客服号和客户名称
  763. if ($fromType == dict::getDict('fromType', 'friend')) {
  764. $staffName = $order->shopAdminName ?? '';
  765. $bookName = $order->bookName ?? '';
  766. $fromTypeName = $staffName . ' ' . $bookName;
  767. }
  768. $content = '';
  769. //$content .= "<CB>【零售】</CB><BR>";
  770. if ($fromType == dict::getDict('fromType', 'mt')) {
  771. $content .= '<B>美团 ' . $order->thirdSn . '</B><BR>';
  772. if (isset($order->goodsNum) && $order->goodsNum > 1) {
  773. $content .= '<B>美团 ' . $order->thirdSn . ' (' . $order->goodsNum . '份)</B><BR>';
  774. }
  775. $content .= '--------------------------------<BR>';
  776. $content .= date("Y-m-d", strtotime($order->addTime)) . '<BR>';
  777. } else {
  778. $content .= '<CB>' . $fromTypeName . ' ' . $respond['sendNum'] . '</CB><BR>';
  779. $content .= '--------------------------------<BR>';
  780. }
  781. $readPeriod = $order->reachPeriod ?? 0;
  782. if ($order->sendType == 1) {
  783. if (isset($order->reachDate) && !empty($order->reachDate) && $order->reachDate != '0000-00-00') {
  784. $currentReachDate = date("m-d", strtotime($order->reachDate));
  785. $content .= '<B>' . $currentReachDate . ' ' . $readPeriod . '前</B><BR>';
  786. $content .= '<B>到店自取</B><BR><BR>';
  787. } else {
  788. $content .= '<B>到店自取</B><BR><BR>';
  789. }
  790. } else {
  791. if ($order->sendType == 0) {
  792. $content .= '<B>送货上门</B><BR><BR>';
  793. } else if ($order->sendType == 2) {
  794. $content .= '<B>跑腿到付</B><BR><BR>';
  795. } else if ($order->sendType == 3) {
  796. $content .= '<B>物流到付</B><BR><BR>';
  797. } else if ($order->sendType == 4) {
  798. $content .= '<B>快递到付</B><BR><BR>';
  799. } else {
  800. $content .= '';
  801. }
  802. if (isset($order->reachDate) && !empty($order->reachDate) && $order->reachDate != '0000-00-00') {
  803. $currentReachDate = date("m-d", strtotime($order->reachDate));
  804. $content .= '<B>' . $currentReachDate . ' ' . $readPeriod . '前</B><BR><BR>';
  805. }
  806. }
  807. if (isset($order->receiveUserName) && !empty($order->receiveUserName)) {
  808. $content .= '收花人:<BR>';
  809. $content .= '<B>' . $order->receiveUserName . '</B><BR>';
  810. }
  811. if (isset($order->receiveMobile) && !empty($order->receiveMobile)) {
  812. $content .= '<B>' . $order->receiveMobile . '</B><BR><BR>';
  813. }
  814. if (isset($order->fullAddress) && !empty($order->fullAddress)) {
  815. $content .= '<B>' . $order->fullAddress . '(' . $order->showAddress . ')' . '</B><BR><BR>';
  816. }
  817. if (isset($order->cardInfo) && !empty($order->cardInfo)) {
  818. $content .= '<B>贺卡内容:' . $order->cardInfo . '</B><BR><BR>';
  819. }
  820. if ($order->anonymity == 1) {
  821. $content .= '<B>匿名派送!!</B><BR><BR>';
  822. } else {
  823. if (isset($order->bookMobile) && !empty($order->bookMobile)) {
  824. $bookName = $order->bookName ?? '';
  825. $bookMobile = $order->bookMobile ?? '';
  826. $content .= '订花人:<BR>';
  827. if (!empty($bookName)) {
  828. $content .= '<B>' . $bookName . '</B><BR>';
  829. }
  830. if (!empty($bookMobile)) {
  831. $content .= '<B>' . $bookMobile . '</B><BR>';
  832. }
  833. }
  834. }
  835. if (isset($respond['remark']) && !empty($respond['remark'])) {
  836. $content .= '<BR>';
  837. $content .= '备注:<BR>';
  838. $content .= '<B>' . $respond['remark'] . '</B><BR>';
  839. }
  840. $content .= '<BR>';
  841. $content .= '商品 数量/单价 金额 <BR>';
  842. $content .= '--------------------------------<BR>';
  843. if (isset($respond['product']) && !empty($respond['product'])) {
  844. foreach ($respond['product'] as $current) {
  845. $content = self::printGroup($current, $content);
  846. }
  847. $content .= '--------------------------------<BR>';
  848. }
  849. if (isset($respond['refund']) && !empty($respond['refund'])) {
  850. $content .= '<B>已退款:</B><BR>';
  851. foreach ($respond['refund'] as $current) {
  852. //58mm的机器,一行打印16个汉字,32个字母
  853. $name = $current['name'] ?? '';
  854. $content .= $name . '<BR>';
  855. $name = str_repeat(' ', 10);
  856. $productNum = $current['num'] ?? '';
  857. $blankNum = bcsub(12, strlen($productNum));
  858. if ($blankNum > 0) {
  859. $productNum = $productNum . str_repeat(' ', $blankNum);
  860. }
  861. $productPrice = $current['price'] ?? '';
  862. $blankNum = bcsub(7, strlen($productPrice));
  863. if ($blankNum > 0) {
  864. $productPrice = $productPrice . str_repeat(' ', $blankNum);
  865. }
  866. $content .= $name . " " . $productNum . ' ' . $productPrice . '<BR>';
  867. }
  868. $content .= '--------------------------------<BR>';
  869. }
  870. if (isset($order->goodsPrice) && $order->goodsPrice > 0) {
  871. $content .= '商品合计:' . floatval($order->goodsPrice) . '<BR>';
  872. }
  873. if (isset($respond['sendCost']) && $respond['sendCost'] > 0 && $order->sendType == 2) {
  874. $content .= '运费:' . floatval($respond['sendCost']) . '<BR>';
  875. }
  876. if (isset($order->serviceFee) && $order->serviceFee > 0) {
  877. $content .= '手续费:' . floatval($order->serviceFee) . '<BR>';
  878. }
  879. if (isset($order->labourCost) && $order->labourCost > 0) {
  880. $content .= '包装费:' . floatval($order->labourCost) . '<BR>';
  881. }
  882. if (isset($order->prePrice) && $order->prePrice > 0) {
  883. $content .= '总计:' . floatval($order->prePrice) . '<BR>';
  884. }
  885. if (isset($respond['discountAmount']) && !empty($respond['discountAmount']) && $respond['discountAmount'] > 0) {
  886. $content .= '优惠扣除:-' . floatval($respond['discountAmount']) . '<BR>';
  887. }
  888. $content .= '订单金额:' . floatval($respond['orderPrice']) . '<BR>';
  889. if (isset($respond['refundPrice']) && $respond['refundPrice'] > 0) {
  890. $content .= '退款金额:' . floatval($respond['refundPrice']) . '<BR>';
  891. }
  892. if (isset($respond['debt']) && $respond['debt'] == 1) {
  893. $content .= '实付金额:0<BR>';
  894. } else {
  895. $content .= '实付金额:' . floatval($respond['realPrice']) . '<BR>';
  896. }
  897. if (isset($order->remainDebtPrice) && $order->remainDebtPrice > 0) {
  898. $content .= '<B>待付尾款:' . floatval($order->remainDebtPrice) . '</B><BR>';
  899. }
  900. $shop = ShopClass::getById($shopId, true);
  901. $sjId = $shop->sjId ?? 0;
  902. $sj = SjClass::getById($sjId, true);
  903. $sjName = $sj->name ?? '';
  904. $shopName = $shop->shopName ?? '';
  905. $currentName = $shopName == '首店' ? $sjName : $sjName . ' ' . $shopName;
  906. $content .= '--------------------------------<BR>';
  907. $content .= '订单日期:' . $order->addTime . '<BR>';
  908. $content .= '订单编号:' . $respond['orderSn'] . '<BR>';
  909. $content .= '门店名称:' . $currentName . '<BR>';
  910. $content .= "<BR>";
  911. $p = new printUtil($printSn);
  912. $orderSn = $order->orderSn ?? '';
  913. $workList = WorkClass::getAllByCondition(['orderSn' => $orderSn], null, '*', null, true);
  914. if (!empty($workList)) {
  915. $content .= "<BR>";
  916. $content .= '--------------------------------<BR>';
  917. foreach ($workList as $work) {
  918. $goodsSn = $work->goodsSn ?? '';
  919. $sn = $work->sn ?? '';
  920. $content .= '<B>制作号:' . $sn . '<B>';
  921. // 1-523 有-特殊符号,使用飞鹅自带的函数处理
  922. $content .= $p->bar_code($goodsSn);
  923. $content .= "<BR>";
  924. }
  925. }
  926. $p->printMsg($content);
  927. }
  928. //打印数据 ssh 20210702
  929. public static function getPrintData($order, $debtAmount)
  930. {
  931. $orderSn = $order->orderSn ?? '';
  932. $printProduct = [];
  933. $orderItemList = OrderItemClass::getAllByCondition(['orderSn' => $orderSn], null, '*');
  934. if (!empty($orderItemList)) {
  935. foreach ($orderItemList as $itemKey => $itemVal) {
  936. $unitName = $itemVal['unitName'] ?? '';
  937. $unitPrice = $itemVal['unitPrice'] ? floatval($itemVal['unitPrice']) : 0;
  938. $printProduct[] = [
  939. 'name' => $itemVal['name'] ?? '',
  940. 'num' => $itemVal['num'] . $unitName . '*' . $unitPrice,
  941. 'price' => floatval($itemVal['price']),
  942. ];
  943. }
  944. }
  945. $orderGoodsList = OrderGoodsClass::getAllByCondition(['orderSn' => $orderSn], null, '*');
  946. if (!empty($orderGoodsList)) {
  947. foreach ($orderGoodsList as $itemKey => $itemVal) {
  948. $unitName = $itemVal['unitName'] ?? '份';
  949. $unitPrice = $itemVal['unitPrice'] ? floatval($itemVal['unitPrice']) : 0;
  950. $printProduct[] = [
  951. 'name' => $itemVal['name'] ?? '',
  952. 'num' => $itemVal['num'] . $unitName . '*' . $unitPrice,
  953. 'price' => floatval($itemVal['price']),
  954. ];
  955. }
  956. }
  957. $sendNum = $order['sendNum'] ?? '';
  958. //退款记录
  959. $refundList = [];
  960. $printData = [
  961. 'address' => '',
  962. 'sendCost' => $order->sendCost ?? '0.00',
  963. 'prePrice' => $order->prePrice ?? '0.00',
  964. 'orderPrice' => $order->orderPrice ?? '0',
  965. 'discountAmount' => $order->discountAmount ?? '0.00',
  966. 'realPrice' => $order->realPrice ?? '0.00',
  967. 'refundPrice' => $order->refundPrice ?? '0.00',
  968. 'debt' => $order->debt ?? 0,
  969. 'orderSn' => $order->orderSn ?? '',
  970. 'date' => $order->addTime ?? '',
  971. 'remark' => $order->remark ?? '',
  972. 'custom' => [
  973. 'customName' => $order->customName ?? '',
  974. 'customMobile' => $order->customMobile ?? '',
  975. 'fullAddress' => $order->fullAddress ?? '',
  976. ],
  977. 'product' => $printProduct,
  978. 'refund' => $refundList,
  979. 'sendNum' => $sendNum,
  980. 'debtAmount' => $debtAmount,
  981. ];
  982. return $printData;
  983. }
  984. public static function printGroup($current, $content)
  985. {
  986. //58mm的机器,一行打印16个汉字,32个字母
  987. $name = $current['name'] ?? '';
  988. $content .= $name . '<BR>';
  989. $name = str_repeat(' ', 10);
  990. $productNum = $current['num'] ?? '';
  991. $blankNum = bcsub(12, strlen($productNum));
  992. if ($blankNum > 0) {
  993. $productNum = $productNum . str_repeat(' ', $blankNum);
  994. }
  995. $productPrice = $current['price'] ?? '';
  996. $blankNum = bcsub(7, strlen($productPrice));
  997. if ($blankNum > 0) {
  998. $productPrice = $productPrice . str_repeat(' ', $blankNum);
  999. }
  1000. $content .= $name . " " . $productNum . ' ' . $productPrice . '<BR>';
  1001. return $content;
  1002. }
  1003. }