SystemTicketServices.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2026 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\services\system;
  12. use app\dao\system\SystemTicketDao;
  13. use app\services\BaseServices;
  14. use crmeb\exceptions\AdminException;
  15. use crmeb\services\FormBuilder as Form;
  16. use crmeb\services\printer\Printer;
  17. class SystemTicketServices extends BaseServices
  18. {
  19. public function __construct(SystemTicketDao $dao)
  20. {
  21. $this->dao = $dao;
  22. }
  23. public function ticketList($where)
  24. {
  25. [$page, $limit] = $this->getPageValue();
  26. $list = $this->dao->ticketList($where, $page, $limit);
  27. foreach ($list as &$item) {
  28. $item['add_time'] = date('Y-m-d H:i:s', $item['add_time']);
  29. }
  30. $count = $this->dao->ticketCount($where);
  31. return compact('list', 'count');
  32. }
  33. public function ticketForm($id)
  34. {
  35. $info = $this->dao->get($id) ?? [];
  36. if ($info) $info = $info->toArray();
  37. $field[] = Form::input('print_name', '打印机名称', $info['print_name'] ?? '')->required('请输入打印机名称')->placeholder('打印机名称');
  38. $field[] = Form::radio('type', '平台选择', $info['type'] ?? 1)
  39. ->options([['label' => '易联云', 'value' => 1], ['label' => '飞鹅云', 'value' => 2]])
  40. ->appendControl(1, [
  41. Form::input('yly_user_id', '用户ID:', $info['yly_user_id'] ?? '')->required('请输入用户ID')->placeholder('易联云开发者ID'),
  42. Form::input('yly_app_id', '应用ID:', $info['yly_app_id'] ?? '')->required('请输入应用ID')->placeholder('易联应用ID'),
  43. Form::input('yly_app_secret', '应用密钥:', $info['yly_app_secret'] ?? '')->required('请输入应用密钥')->placeholder('易联应用密钥'),
  44. Form::input('yly_sn', '终端号:', $info['yly_sn'] ?? '')->required('请输入终端号')->placeholder('易联云打印机终端号,打印机型号:易联云打印机 K4无线版'),
  45. ]
  46. )->appendControl(2, [
  47. Form::input('fey_user', '飞鹅云USER:', $info['fey_user'] ?? '')->required('请输入飞鹅云USER')->placeholder('飞鹅云后台注册账号'),
  48. Form::input('fey_ukey', '飞鹅云UYEK:', $info['fey_ukey'] ?? '')->required('请输入飞鹅云UYEK')->placeholder('飞鹅云后台注册账号后生成的UKEY 【备注:这不是填打印机的KEY】'),
  49. Form::input('fey_sn', '飞鹅云SN:', $info['fey_sn'] ?? '')->required('请输入飞鹅云SN')->placeholder('打印机标签上的编号,必须要在管理后台里添加打印机或调用API接口'),
  50. ]
  51. );
  52. $field[] = Form::number('times', '打印联数', $info['times'] ?? 1)->min(1)->required('请输入打印联数')->placeholder('打印机单次打印张数');
  53. $field[] = Form::radio('print_type', '打印时机', $info['print_type'] ?? 1)->options([['label' => '支付后打印', 'value' => 1], ['label' => '下单后打印', 'value' => 2]]);
  54. $field[] = Form::radio('status', '打印开关', $info['status'] ?? 1)->options([['label' => '开启', 'value' => 1], ['label' => '关闭', 'value' => 0]]);
  55. return create_form('小票打印', $field, $this->url('/system/ticket/save/' . $id), 'POST');
  56. }
  57. public function ticketSave($id, $data)
  58. {
  59. if ($id) {
  60. $this->dao->update($id, $data);
  61. } else {
  62. $data['add_time'] = time();
  63. $this->dao->save($data);
  64. }
  65. return true;
  66. }
  67. public function ticketSetStatus($id, $status)
  68. {
  69. $this->dao->update($id, ['status' => $status]);
  70. return true;
  71. }
  72. public function ticketDel($id)
  73. {
  74. $this->dao->update($id, ['is_del' => 1]);
  75. return true;
  76. }
  77. public function ticketContent($id)
  78. {
  79. $print_content = $this->dao->value(['id' => $id], 'print_content');
  80. return $print_content ? json_decode($print_content, true) : [];
  81. }
  82. public function ticketContentSave($id, $data)
  83. {
  84. $print_content = json_encode($data);
  85. $this->dao->update($id, ['print_content' => $print_content]);
  86. return true;
  87. }
  88. public function startPrint($order, $product, $print_type = 1)
  89. {
  90. $where = $print_type === true ? ['status' => 1] : ['status' => 1, 'print_type' => $print_type];
  91. $list = $this->dao->ticketList($where);
  92. foreach ($list as $item) {
  93. if ($item['type'] == 1) { //易联云
  94. $name = 'yi_lian_yun';
  95. $configData = [
  96. 'partner' => $item['yly_user_id'],
  97. 'clientId' => $item['yly_app_id'],
  98. 'apiKey' => $item['yly_app_secret'],
  99. 'terminal' => $item['yly_sn']
  100. ];
  101. $print_content = json_decode($item['print_content'], true);
  102. if (is_null($print_content) || !count($print_content)) throw new AdminException('请先配置打印内容');
  103. $content = $this->ylyContent($print_content, $order, $product, $item['times'], $print_type);
  104. } else { //飞鹅云
  105. $name = 'fei_e_yun';
  106. $configData = [
  107. 'feyUser' => $item['fey_user'],
  108. 'feyUkey' => $item['fey_ukey'],
  109. 'feySn' => $item['fey_sn']
  110. ];
  111. $print_content = json_decode($item['print_content'], true);
  112. if (is_null($print_content) || !count($print_content)) throw new AdminException('请先配置打印内容');
  113. $content = $this->feyContent($print_content, $order, $product, $print_type);
  114. }
  115. $printer = new Printer($name, $configData);
  116. $printer->setPrinterContent($content, $item['times'])->startPrinter();
  117. }
  118. }
  119. public function ylyContent($printContent, $orderInfo, $product, $times, $print_type)
  120. {
  121. $goodsStr = '<table><tr><td>名称</td><td>单价</td><td>数量</td><td>金额</td></tr>';
  122. foreach ($product as $item) {
  123. $goodsStr .= '<tr><td><FH2><FW2>----------------</FW2></FH2></td></tr>';
  124. $goodsStr .= '<tr>';
  125. $price = $item['sum_price'];
  126. $num = $item['cart_num'];
  127. $prices = bcmul((string)$item['cart_num'], (string)$item['sum_price'], 2);
  128. $goodsStr .= "<td>{$item['productInfo']['store_name']} | {$item['productInfo']['attrInfo']['suk']}</td><td>{$price}</td><td>{$num}</td><td>{$prices}</td>";
  129. $goodsStr .= '</tr>';
  130. if (in_array(1, $printContent['goods'])) {
  131. $goodsStr .= '<tr>';
  132. $goodsStr .= "<td>规格编码:{$item['productInfo']['attrInfo']['bar_code']}</td>";
  133. $goodsStr .= '</tr>';
  134. }
  135. unset($price, $num, $prices);
  136. }
  137. $goodsStr .= '</table>';
  138. $total_price = bcadd($orderInfo['total_price'], $orderInfo['pay_postage'], 2);
  139. $addTime = date('Y-m-d H:i:s', $orderInfo['add_time']);
  140. $payTime = isset($orderInfo['pay_time']) ? date('Y-m-d H:i:s', $orderInfo['pay_time']) : '';
  141. $printTime = date('Y-m-d H:i:s', time());
  142. $content = '';
  143. $content .= '<MN>' . $times . '</MN>';
  144. if ($printContent['header']) {
  145. $content .= '<FS2><center>' . sys_config('site_name') . '</center></FS2>';
  146. $content .= '<FH2><FW2>----------------</FW2></FH2>';
  147. }
  148. if ($printContent['delivery']) {
  149. if ($orderInfo['shipping_type'] == 1) {
  150. $content .= '配送方式:商家配送 \r';
  151. } else {
  152. $content .= '配送方式:门店自提 \r';
  153. }
  154. $content .= '客户姓名: ' . $orderInfo['real_name'] . ' \r';
  155. $content .= '客户电话: ' . $orderInfo['user_phone'] . ' \r';
  156. if ($orderInfo['shipping_type'] == 1) $content .= '收货地址: ' . $orderInfo['user_address'] . ' \r';
  157. $content .= '<FH2><FW2>----------------</FW2></FH2>';
  158. }
  159. if ($printContent['buyer_remarks']) {
  160. $content .= '买家备注: ' . $orderInfo['mark'] . ' \r';
  161. $content .= '<FH2><FW2>----------------</FW2></FH2>';
  162. }
  163. if (in_array(0, $printContent['goods'])) {
  164. $content .= '*************商品***************';
  165. $content .= ' \r';
  166. $content .= $goodsStr;
  167. $content .= '********************************\r';
  168. $content .= '<FH2><FW2>----------------</FW2></FH2>';
  169. $content .= '<RA>合计:' . $total_price . '元</RA>';
  170. $content .= '<FH2><FW2>----------------</FW2></FH2>';
  171. }
  172. if ($printContent['preferential'] || $printContent['freight']) {
  173. if ($printContent['freight']) {
  174. $content .= '<RA>邮费:' . $orderInfo['pay_postage'] . '元</RA>';
  175. }
  176. if ($printContent['preferential']) {
  177. $discount_price = bcsub(bcadd($orderInfo['total_price'], $orderInfo['pay_postage'], 2), bcadd($orderInfo['deduction_price'], $orderInfo['pay_price'], 2), 2);
  178. $content .= '<RA>优惠:-' . $discount_price . '元</RA>';
  179. $content .= '<RA>抵扣:-' . $orderInfo['deduction_price'] . '元</RA>';
  180. }
  181. $content .= '<FH2><FW2>----------------</FW2></FH2>';
  182. }
  183. if (in_array(0, $printContent['pay'])) {
  184. if ($print_type == 1) {
  185. switch ($orderInfo['pay_type']) {
  186. case 'weixin':
  187. $content .= '<RA>支付方式:微信支付</RA>';
  188. break;
  189. case 'alipay':
  190. $content .= '<RA>支付方式:支付宝支付</RA>';
  191. break;
  192. case 'yue':
  193. $content .= '<RA>支付方式:余额支付</RA>';
  194. break;
  195. case 'offline':
  196. $content .= '<RA>支付方式:线下支付</RA>';
  197. break;
  198. default:
  199. $content .= '<RA>支付方式:暂无</RA>';
  200. break;
  201. }
  202. } else {
  203. $content .= '<RA>支付方式:暂无</RA>';
  204. }
  205. }
  206. if (in_array(1, $printContent['pay'])) {
  207. $content .= '<RA>实际支付:' . $orderInfo['pay_price'] . '元</RA>';
  208. }
  209. if (count($printContent['pay'])) {
  210. $content .= '<FH2><FW2>----------------</FW2></FH2>';
  211. }
  212. if (in_array(0, $printContent['order'])) {
  213. $content .= '订单编号:' . $orderInfo['order_id'] . '\r';
  214. }
  215. if (in_array(1, $printContent['order'])) {
  216. $content .= '下单时间:' . $addTime . '\r';
  217. }
  218. if (in_array(2, $printContent['order'])) {
  219. $content .= '支付时间:' . $payTime . '\r';
  220. }
  221. if (in_array(3, $printContent['order'])) {
  222. $content .= '打印时间:' . $printTime . '\r';
  223. }
  224. if (count($printContent['order'])) {
  225. $content .= '<FH2><FW2>----------------</FW2></FH2>';
  226. }
  227. if ($printContent['code'] && $printContent['code_url']) {
  228. $content .= '<QR>' . sys_config('site_url') . $printContent['code_url'] . '</QR>';
  229. $content .= ' \r';
  230. }
  231. if ($printContent['show_notice']) {
  232. $content .= '<center>' . $printContent['notice_content'] . '</center>';
  233. $content .= ' \r';
  234. }
  235. return $content;
  236. }
  237. public function feyContent($printContent, $orderInfo, $product, $print_type)
  238. {
  239. $printTime = date('Y-m-d H:i:s', time());
  240. $addTime = date('Y-m-d H:i:s', $orderInfo['add_time']);
  241. $payTime = isset($orderInfo['pay_time']) ? date('Y-m-d H:i:s', $orderInfo['pay_time']) : '';
  242. $content = '';
  243. if ($printContent['header']) {
  244. $content .= '<CB>' . sys_config('site_name') . '</CB><BR>';
  245. $content .= '--------------------------------<BR>';
  246. }
  247. if ($printContent['delivery']) {
  248. if ($orderInfo['shipping_type'] == 1) {
  249. $content .= '配送方式:商家配送<BR>';
  250. } else {
  251. $content .= '配送方式:门店自提<BR>';
  252. }
  253. $content .= '客户姓名: ' . $orderInfo['real_name'] . '<BR>';
  254. $content .= '客户电话: ' . $orderInfo['user_phone'] . '<BR>';
  255. if ($orderInfo['shipping_type'] == 1) $content .= '收货地址:' . $orderInfo['user_address'] . '<BR>';
  256. $content .= '--------------------------------<BR>';
  257. }
  258. if ($printContent['buyer_remarks']) {
  259. $content .= '买家备注:' . $orderInfo['mark'] . '<BR>';
  260. $content .= '--------------------------------<BR>';
  261. }
  262. if (in_array(0, $printContent['goods'])) {
  263. $content .= '<BR>';
  264. $content .= '**************商品**************<BR>';
  265. $content .= '<BR>';
  266. $content .= '名称 单价 数量 金额<BR>';
  267. foreach ($product as $item) {
  268. $content .= '--------------------------------<BR>';
  269. $name = $item['productInfo']['store_name'] . " | " . $item['productInfo']['attrInfo']['suk'];
  270. $price = $item['sum_price'];
  271. $num = $item['cart_num'];
  272. $prices = bcmul((string)$item['cart_num'], (string)$item['sum_price'], 2);
  273. $kw3 = '';
  274. $kw1 = '';
  275. $kw2 = '';
  276. $kw4 = '';
  277. $str = $name;
  278. $blankNum = 14;//名称控制为14个字节
  279. $lan = mb_strlen($str, 'utf-8');
  280. $m = 0;
  281. $j = 1;
  282. $blankNum++;
  283. $result = array();
  284. if (strlen($price) < 6) {
  285. $k1 = 6 - strlen($price);
  286. for ($q = 0; $q < $k1; $q++) {
  287. $kw1 .= ' ';
  288. }
  289. $price = $price . $kw1;
  290. }
  291. if (strlen($num) < 3) {
  292. $k2 = 3 - strlen($num);
  293. for ($q = 0; $q < $k2; $q++) {
  294. $kw2 .= ' ';
  295. }
  296. $num = $num . $kw2;
  297. }
  298. if (strlen($prices) < 6) {
  299. $k3 = 6 - strlen($prices);
  300. for ($q = 0; $q < $k3; $q++) {
  301. $kw4 .= ' ';
  302. }
  303. $prices = $prices . $kw4;
  304. }
  305. for ($i = 0; $i < $lan; $i++) {
  306. $new = mb_substr($str, $m, $j, 'utf-8');
  307. $j++;
  308. if (mb_strwidth($new, 'utf-8') < $blankNum) {
  309. if ($m + $j > $lan) {
  310. $m = $m + $j;
  311. $tail = $new;
  312. $lenght = iconv("UTF-8", "GBK//IGNORE", $new);
  313. $k = 14 - strlen($lenght);
  314. for ($q = 0; $q < $k; $q++) {
  315. $kw3 .= ' ';
  316. }
  317. if ($m == $j) {
  318. $tail .= $kw3 . ' ' . $price . ' ' . $num . ' ' . $prices;
  319. } else {
  320. $tail .= $kw3 . '<BR>';
  321. }
  322. break;
  323. } else {
  324. $next_new = mb_substr($str, $m, $j, 'utf-8');
  325. if (mb_strwidth($next_new, 'utf-8') < $blankNum) {
  326. continue;
  327. } else {
  328. $m = $i + 1;
  329. $result[] = $new;
  330. $j = 1;
  331. }
  332. }
  333. }
  334. }
  335. $head = '';
  336. foreach ($result as $key => $value) {
  337. if ($key < 1) {
  338. $v_lenght = iconv("UTF-8", "GBK//IGNORE", $value);
  339. $v_lenght = strlen($v_lenght);
  340. if ($v_lenght == 13) $value = $value . " ";
  341. $head .= $value . ' ' . $price . ' ' . $num . ' ' . $prices;
  342. } else {
  343. $head .= $value . '<BR>';
  344. }
  345. }
  346. $content .= $head . $tail;
  347. if (in_array(1, $printContent['goods'])) {
  348. $content .= '规格编码:' . $item['productInfo']['attrInfo']['bar_code'] . '<BR>';
  349. }
  350. unset($price);
  351. }
  352. $content .= '<BR>';
  353. $content .= '********************************<BR>';
  354. $content .= '<BR>';
  355. $content .= '--------------------------------<BR>';
  356. $total_price = bcadd($orderInfo['total_price'], $orderInfo['pay_postage'], 2);
  357. $content .= '<RIGHT>合计:' . number_format($total_price, 2) . '元</RIGHT>';
  358. $content .= '--------------------------------<BR>';
  359. }
  360. if ($printContent['preferential'] || $printContent['freight']) {
  361. if ($printContent['freight']) {
  362. $content .= '<RIGHT>邮费:' . number_format($orderInfo['pay_postage'], 2) . '元</RIGHT><BR>';
  363. }
  364. if ($printContent['preferential']) {
  365. $discount_price = bcsub(bcadd($orderInfo['total_price'], $orderInfo['pay_postage'], 2), bcadd($orderInfo['deduction_price'], $orderInfo['pay_price'], 2), 2);
  366. $content .= '<RIGHT>优惠:-' . number_format($discount_price, 2) . '元</RIGHT><BR>';
  367. $content .= '<RIGHT>抵扣:-' . number_format($orderInfo['deduction_price'], 2) . '元</RIGHT>';
  368. }
  369. $content .= '--------------------------------<BR>';
  370. }
  371. if (in_array(0, $printContent['pay'])) {
  372. if ($print_type == 1) {
  373. switch ($orderInfo['pay_type']) {
  374. case 'weixin':
  375. $content .= '<RIGHT>支付方式:微信支付</RIGHT><BR>';
  376. break;
  377. case 'alipay':
  378. $content .= '<RIGHT>支付方式:支付宝支付</RIGHT><BR>';
  379. break;
  380. case 'yue':
  381. $content .= '<RIGHT>支付方式:余额支付</RIGHT><BR>';
  382. break;
  383. case 'offline':
  384. $content .= '<RIGHT>支付方式:线下支付</RIGHT><BR>';
  385. break;
  386. default:
  387. $content .= '<RIGHT>支付方式:暂无</RIGHT><BR>';
  388. break;
  389. }
  390. } else {
  391. $content .= '<RIGHT>支付方式:暂无</RIGHT><BR>';
  392. }
  393. }
  394. if (in_array(1, $printContent['pay'])) {
  395. $content .= '<RIGHT>实际支付:' . number_format($orderInfo['pay_price'], 2) . '元</RIGHT>';
  396. }
  397. if (count($printContent['pay'])) {
  398. $content .= '--------------------------------<BR>';
  399. }
  400. if (in_array(0, $printContent['order'])) {
  401. $content .= '订单编号:' . $orderInfo['order_id'] . '<BR>';
  402. }
  403. if (in_array(1, $printContent['order'])) {
  404. $content .= '下单时间: ' . $addTime . '<BR>';
  405. }
  406. if (in_array(2, $printContent['order'])) {
  407. $content .= '付款时间: ' . $payTime . '<BR>';
  408. }
  409. if (in_array(3, $printContent['order'])) {
  410. $content .= '打印时间: ' . $printTime . '<BR>';
  411. }
  412. if (count($printContent['order'])) {
  413. $content .= '--------------------------------<BR>';
  414. $content .= '<BR>';
  415. }
  416. if ($printContent['code'] && $printContent['code_url']) {
  417. $content .= '<QR>' . sys_config('site_url') . $printContent['code_url'] . '</QR>';
  418. }
  419. if ($printContent['show_notice']) {
  420. $content .= '<C>' . $printContent['notice_content'] . '</C>';
  421. }
  422. return $content;
  423. }
  424. }