PrintOrder.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. <?php
  2. namespace common\helpers\printer;
  3. use common\models\order\Order;
  4. use common\models\mall\Mall;
  5. use common\enums\PayTypeEnum;
  6. use common\enums\ShippingTypeEnum;
  7. use common\enums\OrderStatusEnum;
  8. use common\helpers\printer\templates\DefaultTemplate;
  9. use common\helpers\printer\printer\KdtOrderPrinter;
  10. use common\helpers\printer\printer\FeieOrderPrinter;
  11. use common\helpers\printer\printer\YilianyunOrderPrinter;
  12. use common\helpers\printer\printer\GpOrderPrinter;
  13. use common\models\common\MallSetting;
  14. use common\models\common\Printer;
  15. use Exception;
  16. class PrintOrder
  17. {
  18. private $store_id; //门店ID
  19. private $print_type;//打印类型 order,pay.....
  20. private $order_id;
  21. private $user_id;
  22. private $mall_id;
  23. const P_360_KDT2 = '360-kdt2';
  24. const P_YILIANYUN_K4 = 'yilianyun-k4';
  25. const P_FEIE = 'feie';
  26. const P_GAINSCHA_GP = 'gainscha-gp';
  27. //订单类型
  28. const MALL_ORDER = '商城订单';
  29. //打印类型
  30. const PRINT_TYPE_ORDER = 'order'; //下单
  31. public function __construct($param) {
  32. $this->store_id = $param['store_id'];
  33. $this->print_type = $param['print_type'];
  34. $this->order_id = $param['order_id'];
  35. $this->user_id = $param['user_id'];
  36. $this->mall_id = $param['mall_id'];
  37. }
  38. /**
  39. * 是否打印,老系统写死不打印
  40. */
  41. public function is_print() {
  42. return true;
  43. }
  44. /**
  45. * 打印
  46. */
  47. public function print($print_order_type) {
  48. try{
  49. $printers = $this->getPrinter();
  50. if (!$printers) {
  51. return;
  52. }
  53. $printers = array_column($printers, null, 'id');
  54. $printer_ids = array_keys($printers);
  55. $printerSetting = $this->printSetting($printer_ids);
  56. if ($printerSetting === false) {
  57. return;
  58. }
  59. $printData = $this->setOrderPrintData($print_order_type);
  60. if ($printData == false) {
  61. return;
  62. }
  63. $count = 0;
  64. foreach ($printerSetting as $setting) {
  65. $printTypeList = json_decode($setting['type'], true);
  66. $printTypeList['reprint'] = 1;
  67. //打印开启,并且设置了打印设置
  68. if (isset($printTypeList[$this->print_type]) && $printTypeList[$this->print_type] == 1) {
  69. $printData->is_attr = $setting['is_attr'];
  70. $printer = $printers[$setting['printer_id']];
  71. $config = $printer['printer_config'];
  72. switch ($printer['type']) {
  73. case self::P_360_KDT2: //365kdt打印
  74. $printer = new KdtOrderPrinter($config);
  75. break;
  76. case self::P_FEIE: //飞俄打印
  77. $printer = new FeieOrderPrinter($config);
  78. break;
  79. case self::P_YILIANYUN_K4: //易联云打印
  80. $printer = new YilianyunOrderPrinter($config);
  81. break;
  82. case self::P_GAINSCHA_GP: //佳博打印
  83. $config['orderNo'] = $printData->order_no;
  84. $printer = new GpOrderPrinter($config);
  85. break;
  86. }
  87. $template = new DefaultTemplate();
  88. $template->data = $printData;
  89. $printer->print($template->getContentByArray());
  90. $count++;
  91. }
  92. }
  93. } catch(Exception $e) {
  94. \Yii::error('打印错误,msg=' . $e->getMessage() . ',行数:' . $e->getLine() . ', file: ' . $e->getFile());
  95. }
  96. }
  97. /**
  98. * 打印机设置
  99. */
  100. public function printSetting($printer_ids) {
  101. try {
  102. $model = Printer::find()->where(['is_delete' => 0, 'printer_id' => $printer_ids]);
  103. $result = $model->asArray()->all();
  104. if (empty($result)) return false;
  105. return $result;
  106. } catch (\Exception $e) {
  107. return $e->getMessage();
  108. }
  109. }
  110. /**
  111. * 获取打印机
  112. */
  113. public function getPrinter() {
  114. $printers = MallSetting::getConfByGroup($this->mall_id, 'printer', true);
  115. if (empty($printers)) return false;
  116. return json_decode($printers['printer']['value'], true);
  117. }
  118. /**
  119. * 设置打印数据
  120. */
  121. public function setOrderPrintData($print_order_type) {
  122. $mall = Mall::findOne($this->mall_id);
  123. if (empty($mall)) {
  124. return false;
  125. }
  126. $order = Order::find()->where(['id' => $this->order_id, 'user_id' => $this->user_id, 'mall_id' => $this->mall_id])->one();
  127. $data = new OrderContent();
  128. $data->mall_name = $mall->name;
  129. $data->order_type = $print_order_type;
  130. $data->attribute = $order;
  131. $data->created_at = date("Y-m-d H:i:s", $order->created_at);
  132. $paymentTypeEnum = PayTypeEnum::getMap();
  133. $data->payment_type = isset($paymentTypeEnum[$order->payment_type]) ? $paymentTypeEnum[$order->payment_type] : '未知方式';
  134. $ShippingTypeEnum = ShippingTypeEnum::getMap();
  135. $shippingTypText = '';
  136. if (isset($ShippingTypeEnum[$order->shipping_type])) {
  137. if ($order->shipping_type != ShippingTypeEnum::PICKUP && $order->shipping_type != ShippingTypeEnum::LOCAL_DISTRIBUTION) {
  138. if ($order->order_status == OrderStatusEnum::SHIPMENTS) {
  139. $shippingTypText = '快递发货';
  140. }
  141. }
  142. }
  143. $data->shippingTypText = $shippingTypText;
  144. $orderInfo = [];
  145. $orderDetail = $order->detail;
  146. foreach ($orderDetail as $item) {
  147. $orderInfo[] = (object)[
  148. 'attr' => $item->goods_attr_name ?? '',
  149. 'num' => $item->num ?? 0,
  150. 'total_price' => $item->original_goods_price ?? 0,
  151. 'unit_price' => $item->price ?? 0,
  152. 'name' => $item->goods_name ?? ''
  153. ];
  154. }
  155. $data->goods_list = $orderInfo;
  156. if ($order->shipping_type == 1 && $this->store_id != 0) {
  157. //查询门店
  158. $data->store_name = '';
  159. $data->store_mobile = '';
  160. $data->store_address = '';
  161. } else {
  162. $data->store_name = '';
  163. $data->store_mobile = '';
  164. $data->store_address = '';
  165. }
  166. return $data;
  167. }
  168. }