OrderClass.php 32 KB

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