PrinterContent.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. <?php
  2. /**
  3. * Niushop商城系统 - 团队十年电商经验汇集巨献!
  4. * =========================================================
  5. * Copy right 2019-2029 杭州牛之云科技有限公司, 保留所有权利。
  6. * ----------------------------------------------
  7. * 官方网址: https://www.niushop.com
  8. * =========================================================
  9. */
  10. namespace addon\printer\event;
  11. use addon\printer\model\Printer;
  12. use addon\printer\model\PrinterOrder;
  13. use addon\printer\model\PrinterTemplate;
  14. use app\model\order\OrderCommon as OrderCommonModel;
  15. use app\model\shop\Shop;
  16. use app\Controller;
  17. use app\model\store\Store;
  18. /**
  19. * 打印内容
  20. */
  21. class PrinterContent extends Controller
  22. {
  23. public function handle($params)
  24. {
  25. if ($params[ 'type' ] == 'goodsorder') {
  26. $stage = $params[ 'printer_type' ];//打印类型,手动打印、支付、收货
  27. $order_id = $params[ 'order_id' ];
  28. $order_common_model = new OrderCommonModel();
  29. $order_detail_result = $order_common_model->getOrderDetail($order_id);
  30. $order_info = $order_detail_result[ 'data' ];
  31. $site_id = $order_info[ 'site_id' ];
  32. $store_id = $order_info[ 'store_id' ];
  33. $params[ 'site_id' ] = $order_info[ 'site_id' ];
  34. //获取店铺信息
  35. $shop_model = new Shop();
  36. $shop = $shop_model->getShopInfo([ [ 'site_id', '=', $params[ 'site_id' ] ] ]);
  37. $shop_info = $shop[ 'data' ];
  38. //获取打印机列表
  39. $print_model = new Printer();
  40. $printer_condition = [
  41. [ 'site_id', '=', $params[ 'site_id' ] ]
  42. ];
  43. if (addon_is_exit('store', $site_id)) $printer_condition[] = [ 'store_id', '=', $store_id ];
  44. switch ( $stage ) {
  45. case 'order_pay':
  46. $printer_condition[] = [ 'order_pay_open', '=', 1 ];
  47. $printer_condition[] = [ 'order_pay_order_type', 'like', '%,' . $order_info[ 'order_type' ] . ',%' ];
  48. break;
  49. case 'take_delivery':
  50. $printer_condition[] = [ 'take_delivery_open', '=', 1 ];
  51. $printer_condition[] = [ 'take_delivery_order_type', 'like', '%,' . $order_info[ 'order_type' ] . ',%' ];
  52. break;
  53. case 'manual':
  54. $printer_condition[] = [ 'manual_open', '=', 1 ];
  55. break;
  56. }
  57. $printer_data = $print_model->getPrinterList($printer_condition)[ 'data' ] ?? [];
  58. $res_data = [];
  59. foreach ($printer_data as $k => $v) {
  60. //此处应该根据打印机不同分别设置返回不同的数据。当前为易联云
  61. $array = [];
  62. $array[ 'printer_info' ] = $v;
  63. $print_template_model = new PrinterTemplate();
  64. $template_id = 0;
  65. $print_num = 1;
  66. switch ( $stage ) {
  67. case 'order_pay':
  68. //支付
  69. $template_id = $v[ 'order_pay_template_id' ];
  70. $print_num = $v[ 'order_pay_print_num' ];
  71. break;
  72. case 'take_delivery':
  73. //收货
  74. $template_id = $v[ 'take_delivery_template_id' ];
  75. $print_num = $v[ 'take_delivery_print_num' ];
  76. break;
  77. case 'manual':
  78. //手动打印
  79. $template_id = $v[ 'template_id' ];
  80. $print_num = $v[ 'print_num' ];
  81. break;
  82. }
  83. $print_template_data = $print_template_model->getPrinterTemplateInfo([ [ 'template_id', '=', $template_id ] ]);
  84. $print_template = $print_template_data[ 'data' ];
  85. $array[ 'printer_code' ] = $v[ 'printer_code' ]; //商户授权机器码
  86. $array[ 'origin_id' ] = $order_info[ 'order_no' ]; //内部订单号(32位以内)
  87. /**文本接口开始**/
  88. $content = "<MN>" . $print_num . "</MN>";
  89. //小票名称
  90. if ($print_template[ 'title' ] != '') {
  91. $content .= "<center>" . $print_template[ 'title' ] . "</center>";
  92. $content .= str_repeat('.', 32);
  93. }
  94. //商城名称
  95. if ($print_template[ 'head' ] == 1) {
  96. $content .= "<FH2><FS><center>" . $print_template[ 'site_name' ] . "</center></FS></FH2>";
  97. $content .= str_repeat('.', 32);
  98. }
  99. if(!empty($order_info[ 'pay_time' ])) {
  100. $content .= "订单时间:" . date("Y-m-d H:i", $order_info[ 'pay_time' ]) . "\n";
  101. }else{
  102. $content .= "订单时间:" . date("Y-m-d H:i", time()) . "\n";
  103. }
  104. $content .= "订单编号:" . $order_info[ 'order_no' ] . "\n";
  105. $content .= "支付方式:" . $order_info[ 'pay_type_name' ] . "\n";
  106. // 2:自提提货,3:本地配送外卖
  107. if ($order_info[ 'order_type' ] == 2 || $order_info[ 'order_type' ] == 3) {
  108. if ($order_info[ 'buyer_ask_delivery_time' ] == 0) {
  109. $buyer_ask_delivery_time_str = '立即送达';
  110. } elseif (strpos($order_info[ 'buyer_ask_delivery_time' ], '-') !== false) {
  111. $buyer_ask_delivery_time_str = $order_info[ 'buyer_ask_delivery_time' ];
  112. } else {
  113. $buyer_ask_delivery_time_str = date("H:i:s", $order_info[ 'buyer_ask_delivery_time' ]);
  114. }
  115. $content .= "配送时间:" . $buyer_ask_delivery_time_str . "\n";
  116. }
  117. $content .= str_repeat('.', 32);
  118. $content .= "<table>";
  119. $content .= "<tr>";
  120. if ($print_template[ 'goods_price_show' ]) {
  121. $content .= "<td>商品名称</td><td></td><td>数量</td><td>金额</td>";
  122. } else {
  123. $content .= "<td>商品名称</td><td></td><td></td><td>数量</td>";
  124. }
  125. $content .= "</tr>";
  126. $content .= "</table>";
  127. $content .= str_repeat('.', 32);
  128. $content .= "<table>";
  129. foreach ($order_info[ 'order_goods' ] as $goods) {
  130. //显示售价或卖价
  131. $price = $print_template[ 'goods_price_type' ] == 'price' ? $goods[ 'price' ] : $goods[ 'real_goods_money' ];
  132. $sku_name_list = ( new PrinterOrder() )->r_str_pad_1($goods[ 'sku_name' ], 7);
  133. foreach ($sku_name_list as $index => $value) {
  134. if ($index == 0) {
  135. if ($print_template[ 'goods_price_show' ]) {
  136. $content .= "<tr><td>" . $value . "</td><td></td><td>x" . $goods[ 'num' ] . "</td><td>¥" . $price . "</td></tr>";
  137. } else {
  138. $content .= "<tr><td>" . $value . "</td><td></td><td></td><td>x" . $goods[ 'num' ] . "</td></tr>";
  139. }
  140. } else {
  141. $content .= "<tr><td>" . $value . "</td></tr>";
  142. }
  143. }
  144. //商品编码
  145. if ($print_template[ 'goods_code_show' ] && !empty($goods[ 'sku_no' ])) {
  146. $content .= "<tr><td>[" . $goods[ 'sku_no' ] . "]</td><td></td><td></td><td></td></tr>";
  147. }
  148. }
  149. $content .= "</table>";
  150. $content .= str_repeat('.', 32);
  151. if ($order_info[ "goods_money" ] > 0) {
  152. $content .= "商品总额:¥" . $order_info[ "goods_money" ] . "\n";
  153. }
  154. if ($order_info[ "coupon_money" ] > 0) {
  155. $content .= "店铺优惠券:¥" . $order_info[ "coupon_money" ] . "\n";
  156. }
  157. if ($order_info[ "promotion_money" ] > 0) {
  158. $content .= "店铺优惠:¥" . $order_info[ "promotion_money" ] . "\n";
  159. }
  160. if ($order_info[ "point_money" ] > 0) {
  161. $content .= "积分抵扣:¥" . $order_info[ "point_money" ] . "\n";
  162. }
  163. if ($order_info[ "adjust_money" ] > 0) {
  164. $content .= "订单调价:¥" . $order_info[ "adjust_money" ] . "\n";
  165. }
  166. if ($order_info[ "delivery_money" ] > 0) {
  167. $content .= "配送费用:¥" . $order_info[ "delivery_money" ] . "\n";
  168. }
  169. if ($order_info[ "invoice_money" ] > 0) {
  170. $content .= "发票费用:¥" . $order_info[ "invoice_money" ] . "\n";
  171. }
  172. if ($order_info[ "invoice_delivery_money" ] > 0) {
  173. $content .= "发票邮寄费用:¥" . $order_info[ "invoice_delivery_money" ] . "\n";
  174. }
  175. if ($order_info[ "goods_num" ] > 0) {
  176. $content .= "订单共" . $order_info[ 'goods_num' ] . "件商品,总计: ¥" . $order_info[ 'order_money' ] . " \n";
  177. }
  178. $content .= str_repeat('.', 32);
  179. /******************** 备注信息 **************************/
  180. //买家留言
  181. if ($print_template[ 'buy_notes' ] == 1) {
  182. $order_info[ "buyer_message" ] = $order_info[ "buyer_message" ] ? $order_info[ "buyer_message" ] : '无';
  183. $content .= "<FH2>买家留言:" . $order_info[ "buyer_message" ] . "</FH2>\n";
  184. $content .= str_repeat('.', 32);
  185. }
  186. //卖家留言
  187. if ($print_template[ 'seller_notes' ] == 1) {
  188. $order_info[ "remark" ] = $order_info[ "remark" ] ? $order_info[ "remark" ] : '无';
  189. $content .= "<FH2>卖家留言:" . $order_info[ "remark" ] . "</FH2>\n";
  190. $content .= str_repeat('.', 32);
  191. }
  192. //表单
  193. if ($print_template[ 'form_show' ] == 1 && addon_is_exit('form')) {
  194. $form_data = model('form_data')->getInfo([ [ 'site_id', '=', $params[ 'site_id' ] ], [ 'scene', '=', 'order' ], [ 'relation_id', '=', $order_id ] ]);
  195. if ($form_data) {
  196. $form_data = json_decode($form_data[ 'form_data' ], true);
  197. foreach ($form_data as $item) {
  198. $content .= "<FH2>" . $item[ 'title' ] . ":" . $item[ "val" ] . "</FH2>\n";
  199. $content .= str_repeat('.', 32);
  200. }
  201. }
  202. }
  203. /******************** 买家信息 **************************/
  204. if ($order_info[ 'member_id' ]) {
  205. //买家姓名
  206. if ($print_template[ 'buy_name' ] == 1) {
  207. $content .= "" . $order_info[ "name" ] . "\n";
  208. }
  209. //联系方式
  210. if ($print_template[ 'buy_mobile' ] == 1) {
  211. $content .= "" . $order_info[ "mobile" ] . "\n";
  212. }
  213. //地址
  214. if ($print_template[ 'buy_address' ] == 1) {
  215. $content .= "" . $order_info[ 'full_address' ] . "-" . $order_info[ 'address' ] . "\n";
  216. }
  217. if ($print_template[ 'buy_name' ] == 1 || $print_template[ 'buy_mobile' ] == 1 || $print_template[ 'buy_address' ] == 1) {
  218. $content .= str_repeat('.', 32);
  219. }
  220. }
  221. /******************** 商城信息 **************************/
  222. //联系方式
  223. if ($print_template[ 'shop_mobile' ] == 1) {
  224. $content .= "" . $shop_info[ "mobile" ] . "\n";
  225. }
  226. //地址
  227. if ($print_template[ 'shop_address' ] == 1) {
  228. $content .= "" . $shop_info[ 'province_name' ] . $shop_info[ 'city_name' ] . $shop_info[ 'district_name' ] . $shop_info[ 'address' ] . "\n";
  229. }
  230. if ($print_template[ 'shop_mobile' ] == 1 || $print_template[ 'shop_address' ] == 1) {
  231. $content .= str_repeat('.', 32);
  232. }
  233. //二维码
  234. if ($print_template[ 'shop_qrcode' ] == 1) {
  235. $content .= "<QR>" . $print_template[ 'qrcode_url' ] . "</QR>";
  236. $content .= str_repeat('.', 32);
  237. }
  238. /******************** 门店信息 **************************/
  239. if ($order_info[ 'store_id' ] > 0) {
  240. $store_info = ( new Store() )->getStoreInfo([ [ 'store_id', '=', $order_info[ 'store_id' ] ] ], 'store_name,telphone,full_address')[ 'data' ];
  241. $content .= "" . $order_info[ "store_name" ] . "\n";//门店名称
  242. $content .= "" . $store_info[ "telphone" ] . "\n";//门店电话
  243. $content .= "" . $store_info[ "full_address" ] . "\n";//门店地址
  244. $content .= str_repeat('.', 32);
  245. }
  246. //底部内容
  247. if (!empty($print_template[ 'bottom' ])) {
  248. $content .= "<center>" . $print_template[ 'bottom' ] . "</center>";
  249. }
  250. $array[ 'content' ] = $content;
  251. $res_data[] = $array;
  252. }
  253. return $res_data;
  254. }
  255. }
  256. }