PrintOrder.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. <?php
  2. namespace addons\Printer\common\logic;
  3. use common\enums\StatusEnum;
  4. use common\models\order\Order;
  5. use common\models\mall\Mall;
  6. use common\enums\PayTypeEnum;
  7. use common\enums\ShippingTypeEnum;
  8. use addons\Printer\common\logic\templates\DefaultTemplate;
  9. use addons\Printer\common\logic\printer\YilianyunOrderPrinter;
  10. use common\models\common\MallSetting;
  11. use addons\Printer\common\models\Printer;
  12. use addons\Printer\common\models\PrinterTemplate;
  13. use Exception;
  14. class PrintOrder
  15. {
  16. private $order;
  17. private $order_detail;
  18. private $mall_id;
  19. private $event;
  20. private $config;
  21. private $template_list;
  22. private $mall_info;
  23. //打印类型
  24. const PRINT_TYPE_ORDER = 'order_printer_config'; //下单
  25. const PRINT_TYPE_PAY = 'pay_printer_config'; //支付
  26. const PRINT_TYPE_CONFIRM = 'confirm_printer_config'; //确认收货
  27. const PRINT_TYPE_SYSTEM = 'system_printer_config'; //后台
  28. public function setPrinter($param){
  29. $this->event = $param['event'];
  30. $this->order = Order::find()->where(['id' => $param['order_id']])->one();
  31. $this->mall_id = $this->order['mall_id'];
  32. $this->order_detail = $this->order->detail;
  33. $params = [
  34. 'where'=>[['=','mall_id',$this->mall_id],['in','name',['contact_tel','web_url','quick_map_address']]],
  35. 'select'=>['value','name']
  36. ];
  37. $this->mall_info = array_column(MallSetting::lists($params),'value','name');
  38. return $this;
  39. }
  40. /**
  41. * 获取模板
  42. * @return array
  43. */
  44. public function getOpenPrinterConfig(){
  45. $params = [
  46. 'select' => [$this->event,'terminal','secret_key','api_key','user_id','printer_type','store_id'],
  47. 'where' => [['=', 'mall_id', $this->mall_id],['<>','status',StatusEnum::DELETE]]
  48. ];
  49. $list = Printer::lists($params);
  50. if(empty($list)){
  51. return [];
  52. }
  53. $template_id = [];
  54. foreach($list as $k => $val){
  55. $v = json_decode($val[$this->event], true);
  56. if($v['is_printer']==0){
  57. unset($list[$k]);
  58. }else{
  59. $this->config[] = [
  60. 'client_id'=>$val['user_id'],
  61. 'machine_code'=>$val['terminal'],
  62. 'key'=>$val['secret_key'],
  63. 'client_key'=>$val['api_key'],
  64. 'time' => $v['num'],
  65. 'printer_type' => $val['printer_type'],
  66. 'store_id' => $val['store_id'],
  67. 'template_id'=>$v['template_id'],
  68. ];
  69. }
  70. }
  71. }
  72. public function getPrinterTemplate(){
  73. $template_id = array_column($this->config, 'template_id');
  74. $params = [
  75. 'where' => [['=', 'mall_id', $this->mall_id],['<>','status',StatusEnum::DELETE],['in', 'id', $template_id]]
  76. ];
  77. $this->template_list = array_column(PrinterTemplate::lists($params),null,'id');
  78. }
  79. /**
  80. * 打印
  81. */
  82. public function print() {
  83. try{
  84. //判断是商场打印机还是门店打印机
  85. $this->getOpenPrinterConfig();
  86. $this->getPrinterTemplate();
  87. if (empty($this->template_list) || empty($this->config)) {
  88. return;
  89. }
  90. $template = new DefaultTemplate();
  91. foreach($this->config as $config){
  92. if($config['printer_type'] == 1 || ($config['printer_type'] == 2 && $this->order['store_id'] == $config['store_id'])|| ($config['printer_type'] == 3 && $this->order['mch_id'] == $config['mch_id'])){
  93. //var_dump($config['printer_type'],$config['store_id']);
  94. $printer = new YilianyunOrderPrinter($config);
  95. $template_info = $this->template_list[$config['template_id']];
  96. $printData = $this->setOrderPrintData($template_info);;
  97. $template->data = $printData;
  98. $printer->print($template->getContentByArray());
  99. }
  100. }
  101. } catch(Exception $e) {
  102. \Yii::error('打印错误,msg=' . $e->getMessage() . ',行数:' . $e->getLine());
  103. }
  104. }
  105. /**
  106. * 设置打印数据
  107. */
  108. public function setOrderPrintData($template) {
  109. $mall = Mall::findOne($this->mall_id);
  110. if (empty($mall)) {
  111. return false;
  112. }
  113. $data = new OrderContent();
  114. $data->mall_name = empty($template['head_info']) ? '' :$mall->name;
  115. $data->ticket_nam = $template['ticket_nam'];
  116. $data->order_type = '商城订单';
  117. $data->attribute = $this->order;
  118. $data->created_at = date("Y-m-d H:i:s", $this->order->created_at);
  119. $data->contact_tel = $this->mall_info['contact_tel'];
  120. $data->quick_map_address = $this->mall_info['quick_map_address'];
  121. $data->qrcode_url = $template['qrcode_url'];
  122. $data->mall_qrcode_url = $this->mall_info['web_url']; //var_dump($this->mall_info['quick_map_address']);
  123. $data->bottom_info = $template['bottom_info'];
  124. $paymentTypeEnum = PayTypeEnum::getMap();
  125. $data->payment_type = isset($paymentTypeEnum[$this->order->payment_type]) ? $paymentTypeEnum[$this->order->payment_type] : '未知方式';
  126. $ShippingTypeEnum = ShippingTypeEnum::getMap();
  127. $orderInfo = [];
  128. foreach ($this->order_detail as $item) {
  129. $orderInfo[] = (object)[
  130. 'attr' => $item->goods_attr_name ?? '',
  131. 'num' => $item->num ?? 0,
  132. 'total_price' => $item->original_goods_price ?? 0,
  133. 'unit_price' => $item->price ?? 0,
  134. 'name' => $item->goods_name ?? ''
  135. ];
  136. }
  137. $data->goods_list = $orderInfo;
  138. $data->is_show_price = false;
  139. $data->is_show_barcode = false;
  140. if(!empty($template['goods_info'])){
  141. $goods_info = json_decode($template['goods_info'],true);
  142. if(in_array(1,$goods_info)){
  143. $data->is_show_price = true;
  144. }
  145. if(in_array(2,$goods_info)){
  146. $data->is_show_barcode = true;
  147. }
  148. }
  149. $data->price = $template['price_info']==1 ? $data->original_goods_price : $data->pay_money;
  150. if(!empty($template['remarks_info'])){
  151. $remarks_info = json_decode($template['remarks_info'], true);
  152. if(in_array(1,$remarks_info) && $data->remark){
  153. $data->is_show_remark = true;
  154. }
  155. if(in_array(2,$remarks_info) && $data->seller_remark){
  156. $data->is_show_seller_remark = true;
  157. }
  158. }
  159. if(!empty($template['buyer_info'])){
  160. $buyer_info = json_decode($template['buyer_info'], true);
  161. if(in_array(1,$buyer_info)){
  162. $data->is_show_name = true;
  163. }
  164. if(in_array(2,$buyer_info)){
  165. $data->is_show_phone = true;
  166. }
  167. if(in_array(3,$buyer_info)){
  168. $data->is_show_addr = true;
  169. }
  170. }
  171. if(!empty($template['mall_info'])){
  172. $mall_info = json_decode($template['mall_info'], true);
  173. if(in_array(1,$mall_info)){
  174. $data->is_show_mall_phone = true;
  175. }
  176. if(in_array(2,$mall_info)){
  177. $data->is_show_mall_addr = true;
  178. }
  179. if(in_array(3,$mall_info)){
  180. $data->is_show_mall_code = true;
  181. }
  182. }
  183. if(!empty($template['qrcode_url'])){
  184. $data->qrcode_url = $template['qrcode_url'];
  185. $data->is_show_qrcode_url = true;
  186. }
  187. $data->bottom_info = $template['bottom_info'];
  188. return $data;
  189. }
  190. }