OrderClass.php 36 KB

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