OrderClass.php 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737
  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\stat\classes\StatIncomeClass;
  23. use bizHd\stat\classes\StatOrderClass;
  24. use bizHd\user\classes\UserClass;
  25. use bizHd\work\classes\WorkClass;
  26. use common\components\dict;
  27. use common\components\miniUtil;
  28. use common\components\orderSn;
  29. use common\components\printUtil;
  30. use common\components\util;
  31. use Yii;
  32. use bizHd\base\classes\BaseClass;
  33. class OrderClass extends BaseClass
  34. {
  35. public static $baseFile = '\bizHd\order\models\Order';
  36. //已取消
  37. const ORDER_STATUS_CANCEL = 5;
  38. //完成
  39. const ORDER_STATUS_COMPLETE = 4;
  40. //配送中
  41. const ORDER_STATUS_SENDING = 3;
  42. //待配送
  43. const ORDER_STATUS_UN_SEND = 2;
  44. //待付款
  45. const ORDER_STATUS_UN_PAY = 1;
  46. //订单总流程数
  47. const ORDER_TOTAL_FLOW = 4;
  48. //未确认配送方式
  49. const SEND_TYPE_UNKNOWN = 0;
  50. //自取
  51. const SEND_TYPE_NO = 1;
  52. //自己送
  53. const SEND_TYPE_MYSELF = 2;
  54. //快递送
  55. const SEND_TYPE_THIRD = 3;
  56. //正常订单
  57. const REFUND_NO = 0;
  58. //已退款订单
  59. const REFUND_YES = 1;
  60. //送达时间段
  61. public static $reachPeriod = [0 => '上午', 1 => '下午', 2 => '晚上'];
  62. //优惠金额
  63. public static $randDiscount = [
  64. 50 => [1, 3],
  65. 100 => [5, 9],
  66. 300 => [10, 15],
  67. 500 => [16, 18],
  68. 1000 => [18, 30],
  69. 20000 => [90, 100],
  70. ];
  71. public static function valid($order, $mainId)
  72. {
  73. if (isset($order->mainId) == false || $order->mainId != $mainId) {
  74. util::fail('没有权限访问的订单');
  75. }
  76. return true;
  77. }
  78. //添加订单 ssh 2019.12.5
  79. public static function addOrder($data)
  80. {
  81. $data['orderSn'] = $data['orderSn'] ?? orderSn::getOrderSn();
  82. $data['payStatus'] = $data['payStatus'] ?? 0;
  83. $shopId = $data['shopId'] ?? 0;
  84. //累计订单增加
  85. $shop = ShopClass::getLockById($shopId);
  86. if (empty($shop)) {
  87. util::fail('没有找到门店信息');
  88. }
  89. $mainId = $shop->mainId ?? 0;
  90. $main = MainClass::getLockById($mainId);
  91. if (empty($main)) {
  92. util::fail('没有找到main信息');
  93. }
  94. $main->unPayOrder += 1;
  95. $main->totalOrder += 1;
  96. $main->save();
  97. //将花店每月的总订单数作为编号
  98. $riseNum = StatOrderCountClass::addOrder($shop, $main);
  99. $data['sendNum'] = date("d") . $riseNum;
  100. $data['totalFlow'] = OrderClass::ORDER_TOTAL_FLOW;
  101. $return = self::add($data, true);
  102. return $return;
  103. }
  104. public static function getByOrderSn($orderSn)
  105. {
  106. return self::getByCondition(['orderSn' => $orderSn]);
  107. }
  108. //评价 ssh 2019.12.16
  109. public static function comment($data)
  110. {
  111. $id = $data['id'];
  112. unset($data['id']);
  113. return self::updateById($id, $data);
  114. }
  115. public static function getFullInfo($id)
  116. {
  117. $order = self::getById($id);
  118. if (empty($order)) {
  119. return [];
  120. }
  121. $sn = $order['orderSn'] ?? '';
  122. $order['goodsInfoList'] = OrderGoodsClass::getListBySn($sn);
  123. $order['itemInfoList'] = OrderItemClass::getListBySn($sn);
  124. return $order;
  125. }
  126. //订单
  127. public static function getOrderById($id)
  128. {
  129. $order = self::getById($id);
  130. if (empty($order)) {
  131. return [];
  132. }
  133. $sn = $order['orderSn'] ?? '';
  134. $order['goodsInfoList'] = OrderGoodsClass::getListBySn($sn);
  135. return $order;
  136. }
  137. //根据订单查询 ssh 2020.1.4
  138. public static function getOrderBySn($orderSn)
  139. {
  140. $order = self::getByCondition(['orderSn' => $orderSn]);
  141. if (empty($order)) {
  142. return [];
  143. }
  144. $id = $order['id'];
  145. $order['goodsInfoList'] = OrderGoodsClass::getGoodsListById($id);
  146. return $order;
  147. }
  148. //返回随机优惠金额 ssh 2019.9.12
  149. public static function getRandDiscount($amount)
  150. {
  151. if ($amount <= 0) {
  152. return 0;
  153. }
  154. $randDiscount = self::$randDiscount;
  155. krsort($randDiscount);
  156. $rand = 0;
  157. foreach ($randDiscount as $currentAmount => $val) {
  158. if ($amount >= $currentAmount) {
  159. $rand = rand($val[0], $val[1]);
  160. break;
  161. }
  162. }
  163. return $rand / 10;
  164. }
  165. //第三方支付后回调处理流程 ssh 2021.4.30
  166. public static function thirdPay($payWay, $orderSn, $totalFee, $attach, $transactionId)
  167. {
  168. $order = self::getByCondition(['orderSn' => $orderSn], true);
  169. if (empty($order)) {
  170. $msg = "没有找到零售订单 orderSn:{$orderSn}";
  171. Yii::info($msg);
  172. util::fail($msg);
  173. }
  174. if ($order->realPrice != $totalFee) {
  175. $msg = "零售订单金额与回调通知金额不一致 orderSn:{$orderSn} {$order->realPrice} {$totalFee}";
  176. Yii::info($msg);
  177. util::fail($msg);
  178. }
  179. $id = $order->id;
  180. $order = self::getLockById($id);
  181. $date = date("Y-m-d H:i:s");
  182. $order->payTime = $date;
  183. $order->thirdNo = $transactionId;
  184. $order->onlinePay = dict::getDict('onlinePay', 'yes');
  185. $order->save();
  186. self::payAfter($order, $payWay);
  187. }
  188. //订单完成 ssh 2021.4.20
  189. public static function payAfter($order, $payWay)
  190. {
  191. if (empty($order)) {
  192. util::fail('没有找到订单');
  193. }
  194. if (isset($order->status) == false || $order->status != self::ORDER_STATUS_UN_PAY) {
  195. util::fail('订单不是待付款状态');
  196. }
  197. $orderId = $order->id ?? 0;
  198. $order->status = self::ORDER_STATUS_COMPLETE;
  199. $order->payWay = $payWay;
  200. $order->payStatus = 1;
  201. $order->stockChange = 1;
  202. $order->save();
  203. \bizMall\order\classes\OrderGoodsClass::stockChange($order);
  204. $realPrice = $order->realPrice ?? 0;
  205. $shopId = $order->shopId;
  206. $shop = ShopClass::getLockById($shopId);
  207. if (empty($shop)) {
  208. util::fail('没有找到门店');
  209. }
  210. $mainId = $shop->mainId ?? 0;
  211. $main = MainClass::getLockById($mainId);
  212. if (empty($main)) {
  213. util::fail('没有main信息');
  214. }
  215. $main->unSendOrder += 1;
  216. $main->unPayOrder -= 1;
  217. $currentTotalIncome = bcadd($main->totalIncome, $realPrice, 2);
  218. $main->totalIncome = $currentTotalIncome;
  219. $main->save();
  220. $capitalType = dict::getDict('capitalType', 'xhOrder', 'id');
  221. $shopAdminId = $order->shopAdminId ?? 0;
  222. $shopAdminName = $order->shopAdminName ?? '';
  223. $sjId = $order->sjId;
  224. $fromType = $order->fromType ?? 1;
  225. $event = !empty($shopAdminId) ? '员工开单' : '客户下单';
  226. $capital = [
  227. 'capitalType' => $capitalType,
  228. 'relateId' => $orderId,
  229. 'io' => 1,
  230. 'totalIncome' => $currentTotalIncome,
  231. 'payWay' => $payWay,
  232. 'event' => $event,
  233. 'sjId' => $sjId,
  234. 'shopId' => $shopId,
  235. 'shopAdminId' => $shopAdminId,
  236. 'shopAdminName' => $shopAdminName,
  237. 'amount' => $realPrice,
  238. 'fromType' => $fromType,
  239. 'mainId' => $mainId,
  240. ];
  241. ShopCapitalClass::addCapital($capital);
  242. //每天和每月收入增加
  243. StatIncomeClass::updateOrInsert($main, $shop, $realPrice);
  244. //今日订单+1
  245. StatOrderClass::updateOrInsert($main, $shop);
  246. //销售收入增加
  247. StatSaleClass::replace($main, $shop, $realPrice);
  248. //各渠道收入金额统计
  249. StatKdClass::replace($shop, $realPrice, $payWay);
  250. //客户在线支付下单增加商家可提现余额
  251. if (isset($order->onlinePay) && $order->onlinePay == dict::getDict('onlinePay', 'yes')) {
  252. $amount = $order->actPrice;
  253. ShopClass::customKdAddBalance($main, $shop, $amount, $order, $capitalType);
  254. }
  255. }
  256. //转化出送到时间 ssh 2020.3.15
  257. public static function getReachTime($order)
  258. {
  259. $reachTime = '';
  260. if (isset($order['reachDate']) && !empty($order['reachDate'])) {
  261. $prev = date("n-j", strtotime($order['reachDate']));
  262. $periodData = self::$reachPeriod;
  263. $periodKey = $order['reachPeriod'];
  264. $period = isset($periodData[$periodKey]) ? $periodData[$periodKey] : '上午';
  265. $reachTime = $prev . ' ' . $period;
  266. }
  267. return $reachTime;
  268. }
  269. //支付成功获取unionId ssh 2020.5.12
  270. public static function payToUpdateUnionId($merchant, $merchantExtend, $orderSn, $user)
  271. {
  272. $mcId = $merchantExtend['wxPayMerchantId'];
  273. $miniOpenId = isset($user['miniOpenId']) ? $user['miniOpenId'] : '';
  274. $unionId = isset($user['unionId']) ? $user['unionId'] : '';
  275. $userId = $user['id'];
  276. if (empty($unionId)) {
  277. $unionId = miniUtil::getPaidUnionId($merchant, $miniOpenId, $mcId, (string)$orderSn, 0);
  278. if (!empty($unionId)) {
  279. $findUser = UserClass::getByUnionId($unionId);
  280. if (empty($findUser)) {
  281. UserClass::updateByCondition(['id' => $userId], ['unionId' => $unionId]);
  282. } else {
  283. UserClass::mergeUser($findUser, $user);
  284. }
  285. }
  286. }
  287. }
  288. //订单取消 ssh 20220516
  289. public static function setExpire($order)
  290. {
  291. $now = time();
  292. if ($order->deadline > $now) {
  293. return false;
  294. }
  295. if ($order->status != 1) {
  296. return false;
  297. }
  298. $order->status = 5;
  299. $order->save();
  300. //暂时只考虑花材库存的回滚
  301. $orderSn = $order->orderSn ?? '';
  302. $itemList = OrderItemClass::getAllByCondition(['orderSn' => $orderSn], null, '*', null, true);
  303. if (!empty($itemList)) {
  304. foreach ($itemList as $item) {
  305. $ptItemId = $item->ptItemId ?? 0;
  306. $sjId = $item->sjId ?? 0;
  307. $shopId = $item->shopId ?? 0;
  308. $mainId = $item->mainId ?? 0;
  309. $ratio = $item->ratio ?? 20;
  310. $unitType = $item->unitType ?? 0;
  311. $bigNum = 0;
  312. $smallNum = 0;
  313. if ($unitType == 0) {
  314. $bigNum = $item->num;
  315. } else {
  316. $smallNum = $item->num;
  317. }
  318. $itemId = $item->itemId ?? 0;
  319. $stockInfo = ProductClass::addStock($itemId, $bigNum, $smallNum);
  320. $recordData = [];
  321. $recordData['sjId'] = $sjId;
  322. $recordData['shopId'] = $shopId;
  323. $recordData['mainId'] = $mainId;
  324. $recordData['itemId'] = $ptItemId;
  325. $recordData['itemNum'] = ProductClass::mergeItemNum($bigNum, $smallNum, $ratio);
  326. $recordData['oldStock'] = $stockInfo['oldStock'];
  327. $recordData['newStock'] = $stockInfo['newStock'];
  328. $recordData['productId'] = $itemId;
  329. $recordData['orderSn'] = $orderSn;
  330. $recordData['relateName'] = '系统';
  331. StockRecordClass::addSellStockOrderCancelRecord($recordData);
  332. }
  333. }
  334. $goodsList = OrderGoodsClass::getAllByCondition(['orderSn' => $orderSn], null, '*', null, true);
  335. if (!empty($goodsList)) {
  336. foreach ($goodsList as $key => $info) {
  337. $sjId = $info->sjId ?? 0;
  338. $shopId = $info->shopId ?? 0;
  339. $mainId = $info->mainId ?? 0;
  340. $goodsId = $info->goodsId ?? 0;
  341. $num = $info->num ?? 0;
  342. $respond = \bizHd\goods\classes\GoodsClass::addStock($goodsId, $mainId, $num);
  343. $oldStock = $respond['oldStock'] ?? 0;
  344. $newStock = $respond['newStock'] ?? 0;
  345. $data = [];
  346. $data['sjId'] = $sjId;
  347. $data['shopId'] = $shopId;
  348. $data['mainId'] = $mainId;
  349. $data['orderSn'] = $orderSn;
  350. $data['goodsId'] = $goodsId;
  351. $data['goodsNum'] = $num;
  352. $data['oldStock'] = $oldStock;
  353. $data['newStock'] = $newStock;
  354. $data['relateName'] = '';
  355. GoodsStockRecordClass::cancelOrder($data);
  356. }
  357. }
  358. }
  359. // 扣库存
  360. protected static function decGoodsStock($orderGoods)
  361. {
  362. foreach ($orderGoods as $goods) {
  363. GoodsClass::counters([
  364. 'stock' => -$goods['num']
  365. ], [
  366. 'and',
  367. ['id' => $goods['goodsId']],
  368. ['<>', 'stock', Goods::FULL_STOCK]
  369. ]);
  370. }
  371. }
  372. // 更新商品销量
  373. protected static function updateGoodsActualSold($orderGoods)
  374. {
  375. foreach ($orderGoods as $goods) {
  376. GoodsClass::counters([
  377. 'actualSold' => $goods['num']
  378. ], [
  379. 'id' => $goods['goodsId']
  380. ]);
  381. }
  382. }
  383. //云打印 ssh 20210709
  384. public static function onlinePrint($order)
  385. {
  386. if (empty($order)) {
  387. return false;
  388. }
  389. if ($order->needPrint != dict::getDict('needPrint', 'need')) {
  390. return false;
  391. }
  392. if ($order->payStatus != 1) {
  393. return false;
  394. }
  395. $shopId = $order->shopId ?? 0;
  396. $ext = ShopExtClass::getByCondition(['shopId' => $shopId]);
  397. $printSn = $ext['printSn'] ?? '';
  398. if (empty($printSn)) {
  399. return false;
  400. }
  401. $order->printNum += 1;
  402. $order->save();
  403. $customId = $order->customId ?? 0;
  404. $custom = CustomClass::getById($customId);
  405. $debtAmount = $custom['debtAmount'] ?? 0;
  406. $respond = self::getPrintData($order, $debtAmount);
  407. $content = '<B>' . $respond['sendNum'] . '</B><BR>';
  408. $content .= '--------------------------------<BR>';
  409. $content .= '<B>' . $respond['custom']['fullAddress'] . '</B><BR>';
  410. $content .= '<B>' . $respond['custom']['customName'] . '</B><BR>';
  411. $content .= '<B>' . $respond['custom']['customMobile'] . '</B><BR>';
  412. $payDate = date("Y-m-d", strtotime($order->payTime));
  413. $sendTimeWant = $order->sendTimeWant ?? '';
  414. if (!empty($sendTimeWant) && $payDate != $sendTimeWant) {
  415. $content .= '<BR>';
  416. $content .= '送货时间:<BR>';
  417. $content .= '<B>' . $sendTimeWant . '</B><BR>';
  418. }
  419. if (isset($respond['remark']) && !empty($respond['remark'])) {
  420. $content .= '<BR>';
  421. $content .= '备注:<BR>';
  422. $content .= '<B>' . $respond['remark'] . '</B><BR>';
  423. }
  424. $content .= '<BR>';
  425. $content .= '花材 数量/单价 金额 <BR>';
  426. $content .= '--------------------------------<BR>';
  427. if (isset($respond['product']) && !empty($respond['product'])) {
  428. foreach ($respond['product'] as $current) {
  429. $content = self::printGroup($current, $content);
  430. }
  431. $content .= '--------------------------------<BR>';
  432. }
  433. if (isset($respond['refund']) && !empty($respond['refund'])) {
  434. $content .= '<B>已退款:</B><BR>';
  435. foreach ($respond['refund'] as $current) {
  436. //58mm的机器,一行打印16个汉字,32个字母
  437. $name = $current['name'] ?? '';
  438. $content .= $name . '<BR>';
  439. $name = str_repeat(' ', 10);
  440. $productNum = $current['num'] ?? '';
  441. $blankNum = bcsub(12, strlen($productNum));
  442. if ($blankNum > 0) {
  443. $productNum = $productNum . str_repeat(' ', $blankNum);
  444. }
  445. $productPrice = $current['price'] ?? '';
  446. $blankNum = bcsub(7, strlen($productPrice));
  447. if ($blankNum > 0) {
  448. $productPrice = $productPrice . str_repeat(' ', $blankNum);
  449. }
  450. $content .= $name . " " . $productNum . ' ' . $productPrice . '<BR>';
  451. }
  452. $content .= '--------------------------------<BR>';
  453. }
  454. if (isset($respond['sendCost']) && $respond['sendCost'] > 0) {
  455. $content .= ' 运费:' . floatval($respond['sendCost']) . '<BR>';
  456. }
  457. $content .= '订单金额:' . floatval($respond['orderPrice']) . '<BR>';
  458. if (isset($respond['discountAmount']) && !empty($respond['discountAmount']) && $respond['discountAmount'] > 0) {
  459. $content .= '优惠扣除:-' . floatval($respond['discountAmount']) . '<BR>';
  460. }
  461. if (isset($respond['refundPrice']) && $respond['refundPrice'] > 0) {
  462. $content .= '退款金额:' . floatval($respond['refundPrice']) . '<BR>';
  463. }
  464. $content .= '应付金额:' . floatval($respond['orderPrice']) . '<BR>';
  465. if (isset($respond['debt']) && $respond['debt'] == 1) {
  466. $content .= '实付金额:0<BR>';
  467. } else {
  468. $content .= '实付金额:' . floatval($respond['realPrice']) . '<BR>';
  469. }
  470. if ($debtAmount > 0) {
  471. $content .= '累计欠款:' . floatval($debtAmount) . '<BR>';
  472. }
  473. $shop = ShopClass::getById($shopId, true);
  474. $sjId = $shop->sjId ?? 0;
  475. $sj = SjClass::getById($sjId, true);
  476. $sjName = $sj->name ?? '';
  477. $currentName = $sjName;
  478. if (isset($shop->default) == false || $shop->default != 1) {
  479. $shopName = $shop->shopName ?? '';
  480. if (!empty($shopName)) {
  481. $currentName = $currentName . ' ' . $shopName;
  482. }
  483. }
  484. $content .= '--------------------------------<BR>';
  485. $content .= '订单日期:' . $order->addTime . '<BR>';
  486. $content .= '订单编号:' . $respond['orderSn'] . '<BR>';
  487. $content .= '门店名称:' . $currentName . '<BR>';
  488. $content .= "<BR>";
  489. $orderSn = $order->orderSn ?? '';
  490. $workList = WorkClass::getAllByCondition(['orderSn' => $orderSn], null, '*', null, true);
  491. if (!empty($workList)) {
  492. foreach ($workList as $work) {
  493. $goodsSn = $work->goodsSn ?? '';
  494. $sn = $work->sn ?? '';
  495. $content .= '<BOLD>制作单号:' . $sn.'<BOLD>';
  496. $content .= self::bar_code($goodsSn);
  497. }
  498. }
  499. $p = new printUtil($printSn);
  500. $p->printMsg($content);
  501. }
  502. //飞鹅自带函数 ssh 20220601
  503. public static function bar_code($strnum)
  504. {
  505. $chr = '';
  506. $codeB = array("\x30","\x31","\x32","\x33","\x34","\x35","\x36","\x37","\x38","\x39");//匹配字符集B
  507. $codeC = array("\x00","\x01","\x02","\x03","\x04","\x05","\x06","\x07","\x08","\x09","\x0A","\x0B","\x0C","\x0D","\x0E","\x0F","\x10","\x11","\x12","\x13","\x14","\x15","\x16","\x17","\x18","\x19","\x1A","\x1B","\x1C","\x1D","\x1E","\x1F","\x20","\x21","\x22","\x23","\x24","\x25","\x26","\x27","\x28","\x29","\x2A","\x2B","\x2C","\x2D","\x2E","\x2F","\x30","\x31","\x32","\x33","\x34","\x35","\x36","\x37","\x38","\x39","\x3A","\x3B","\x3C","\x3D","\x3E","\x3F","\x40","\x41","\x42","\x43","\x44","\x45","\x46","\x47","\x48","\x49","\x4A","\x4B","\x4C","\x4D","\x4E","\x4F","\x50","\x51","\x52","\x53","\x54","\x55","\x56","\x57","\x58","\x59","\x5A","\x5B","\x5C","\x5D","\x5E","\x5F","\x60","\x61","\x62","\x63");//匹配字符集C
  508. $length = strlen($strnum);
  509. $b=array();
  510. $b[0] = "\x1b";
  511. $b[1] = "\x64";
  512. $b[2] = "\x02";
  513. $b[3] = "\x1d";
  514. $b[4] = "\x48";
  515. $b[5] = "\x32";//条形码显示控制,\x32上图下字,\x31上字下图,\x30只显示条形码
  516. $b[6] = "\x1d";
  517. $b[7] = "\x68";
  518. $b[8] = "\x50";// \x30 设置条形码高度,7F是最大的高度
  519. $b[9] = "\x1d";
  520. $b[10] = "\x77";
  521. $b[11] = "\x02";// \x01 设置条形码宽度,1-6
  522. $b[12] = "\x1d";
  523. $b[13] = "\x6b";
  524. $b[14] = "\x49";//选择条形码类型code128,code39,codabar等等
  525. $b[15] = chr($length + 2);
  526. $b[16] = "\x7b";
  527. $b[17] = "\x42";//选择字符集
  528. if($length > 14 && is_numeric($strnum)){//大于14个字符,且为纯数字的进来这个区间
  529. $b[17] = "\x43";
  530. $j = 0;
  531. $key = 18;
  532. $ss = $length/2;//初始化数组长度
  533. if($length%2 == 1){//判断条形码为单数
  534. $ss = $ss-0.5;
  535. }
  536. for ($i = 0; $i < $ss; $i++){
  537. $temp = substr($strnum,$j,2);
  538. $iindex = intval($temp);
  539. $j = $j+2;
  540. if($iindex == 0){
  541. $chr = '';
  542. if($b[$key + $i-1] == '0' && $b[$key + $i-2] == '0'){//判断前面的为字符集B,此时不需要转换字符集
  543. $b[$key + $i] = $codeB[0];
  544. $b[$key + $i + 1] = $codeB[0];
  545. $key += 1;
  546. }else{
  547. if($b[$key + $i-1] == 'C' && $b[$key + $i-2] == '{'){//判断前面的为字符集C时转换字符集B
  548. $b[$key + $i - 2] = "\x7b";
  549. $b[$key + $i - 1] = "\x42";
  550. $b[$key + $i] = $codeB[0];
  551. $b[$key + $i + 1] = $codeB[0];
  552. $key += 1;
  553. }else{
  554. $b[$key + $i] = "\x7b";
  555. $b[$key + $i + 1] = "\x42";
  556. $b[$key + $i + 2] = $codeB[0];
  557. $b[$key + $i + 3] = $codeB[0];
  558. $key += 3;
  559. }
  560. }
  561. }else{
  562. if($b[$key + $i-1] == '0' && $b[$key + $i-2] == '0' && $chr != 'chr'){//判断前面的为字符集B,此时要转换字符集C
  563. $b[$key + $i] = "\x7b";
  564. $b[$key + $i + 1] = "\x43";
  565. $b[$key + $i + 2] = $codeC[$iindex];
  566. $key += 2;
  567. }else{
  568. $chr = '';
  569. $b[$key + $i] = $codeC[$iindex];
  570. if($iindex == 48) $chr = 'chr';//判断chr(48)等于0的情况
  571. }
  572. }
  573. }
  574. @$lastkey = end(array_keys($b));//取得数组的最后一个元素的键
  575. if($length % 2 > 0){
  576. $lastnum = substr($strnum,-1);//取得字符串的最后一个数字
  577. if($b[$lastkey] == '0' && $b[$lastkey-1] == '0'){//判断前面的为字符集B,此时不需要转换字符集
  578. $b[$lastkey + 1] = $codeB[$lastnum];
  579. }else{
  580. $b[$lastkey + 1] = "\x7b";
  581. $b[$lastkey + 2] = "\x42";
  582. $b[$lastkey + 3] = $codeB[$lastnum];
  583. }
  584. }
  585. @$b[15] = chr(end(array_keys($b)) - 15);//得出条形码长度
  586. $str = implode("",$b);
  587. }else{//1-14个字符的纯数字和非纯数字的条形码进来这个区间,支持数字,大小写字母,特殊字符例如: !@#$%^&*()-=+_
  588. $str = "\x1b\x64\x02\x1d\x48\x32\x1d\x68\x50\x1d\x77\x02\x1d\x6b\x49".chr($length + 2)."\x7b\x42".$strnum;
  589. }
  590. return $str;
  591. }
  592. //打印数据 ssh 20210702
  593. public static function getPrintData($order, $debtAmount)
  594. {
  595. $orderSn = $order->orderSn ?? '';
  596. $printProduct = [];
  597. $orderItemList = OrderItemClass::getAllByCondition(['orderSn' => $orderSn], null, '*');
  598. if (!empty($orderItemList)) {
  599. foreach ($orderItemList as $itemKey => $itemVal) {
  600. $unitName = $itemVal['unitName'] ?? '';
  601. $unitPrice = $itemVal['unitPrice'] ? floatval($itemVal['unitPrice']) : 0;
  602. $printProduct[] = [
  603. 'name' => $itemVal['name'] ?? '',
  604. 'num' => $itemVal['num'] . $unitName . '*' . $unitPrice,
  605. 'price' => floatval($itemVal['price']),
  606. ];
  607. }
  608. }
  609. $orderGoodsList = OrderGoodsClass::getAllByCondition(['orderSn' => $orderSn], null, '*');
  610. if (!empty($orderGoodsList)) {
  611. foreach ($orderGoodsList as $itemKey => $itemVal) {
  612. $unitName = $itemVal['unitName'] ?? '份';
  613. $unitPrice = $itemVal['unitPrice'] ? floatval($itemVal['unitPrice']) : 0;
  614. $printProduct[] = [
  615. 'name' => $itemVal['name'] ?? '',
  616. 'num' => $itemVal['num'] . $unitName . '*' . $unitPrice,
  617. 'price' => floatval($itemVal['price']),
  618. ];
  619. }
  620. }
  621. $sendNum = $order['sendNum'] ?? '';
  622. //退款记录
  623. $refundList = [];
  624. $printData = [
  625. 'address' => '',
  626. 'sendCost' => $order->sendCost ?? '0.00',
  627. 'prePrice' => $order->prePrice ?? '0.00',
  628. 'orderPrice' => $order->orderPrice ?? '0',
  629. 'discountAmount' => $order->discountAmount ?? '0.00',
  630. 'realPrice' => $order->realPrice ?? '0.00',
  631. 'refundPrice' => $order->refundPrice ?? '0.00',
  632. 'debt' => $order->debt ?? 0,
  633. 'orderSn' => $order->orderSn ?? '',
  634. 'date' => $order->addTime ?? '',
  635. 'remark' => $order->remark ?? '',
  636. 'custom' => [
  637. 'customName' => $order->customName ?? '',
  638. 'customMobile' => $order->customMobile ?? '',
  639. 'fullAddress' => $order->fullAddress ?? '',
  640. ],
  641. 'product' => $printProduct,
  642. 'refund' => $refundList,
  643. 'sendNum' => $sendNum,
  644. 'debtAmount' => $debtAmount,
  645. ];
  646. return $printData;
  647. }
  648. public static function printGroup($current, $content)
  649. {
  650. //58mm的机器,一行打印16个汉字,32个字母
  651. $name = $current['name'] ?? '';
  652. $content .= $name . '<BR>';
  653. $name = str_repeat(' ', 10);
  654. $productNum = $current['num'] ?? '';
  655. $blankNum = bcsub(12, strlen($productNum));
  656. if ($blankNum > 0) {
  657. $productNum = $productNum . str_repeat(' ', $blankNum);
  658. }
  659. $productPrice = $current['price'] ?? '';
  660. $blankNum = bcsub(7, strlen($productPrice));
  661. if ($blankNum > 0) {
  662. $productPrice = $productPrice . str_repeat(' ', $blankNum);
  663. }
  664. $content .= $name . " " . $productNum . ' ' . $productPrice . '<BR>';
  665. return $content;
  666. }
  667. }