| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185 |
- <?php
- namespace common\helpers\printer;
- use common\models\order\Order;
- use common\models\mall\Mall;
- use common\enums\PayTypeEnum;
- use common\enums\ShippingTypeEnum;
- use common\enums\OrderStatusEnum;
- use common\helpers\printer\templates\DefaultTemplate;
- use common\helpers\printer\printer\KdtOrderPrinter;
- use common\helpers\printer\printer\FeieOrderPrinter;
- use common\helpers\printer\printer\YilianyunOrderPrinter;
- use common\helpers\printer\printer\GpOrderPrinter;
- use common\models\common\MallSetting;
- use common\models\common\Printer;
- use Exception;
- class PrintOrder
- {
- private $store_id; //门店ID
- private $print_type;//打印类型 order,pay.....
- private $order_id;
- private $user_id;
- private $mall_id;
- const P_360_KDT2 = '360-kdt2';
- const P_YILIANYUN_K4 = 'yilianyun-k4';
- const P_FEIE = 'feie';
- const P_GAINSCHA_GP = 'gainscha-gp';
- //订单类型
- const MALL_ORDER = '商城订单';
- //打印类型
- const PRINT_TYPE_ORDER = 'order'; //下单
- public function __construct($param) {
- $this->store_id = $param['store_id'];
- $this->print_type = $param['print_type'];
- $this->order_id = $param['order_id'];
- $this->user_id = $param['user_id'];
- $this->mall_id = $param['mall_id'];
- }
- /**
- * 是否打印,老系统写死不打印
- */
- public function is_print() {
- return true;
- }
- /**
- * 打印
- */
- public function print($print_order_type) {
- try{
- $printers = $this->getPrinter();
- if (!$printers) {
- return;
- }
- $printers = array_column($printers, null, 'id');
- $printer_ids = array_keys($printers);
- $printerSetting = $this->printSetting($printer_ids);
- if ($printerSetting === false) {
- return;
- }
- $printData = $this->setOrderPrintData($print_order_type);
- if ($printData == false) {
- return;
- }
- $count = 0;
- foreach ($printerSetting as $setting) {
- $printTypeList = json_decode($setting['type'], true);
- $printTypeList['reprint'] = 1;
- //打印开启,并且设置了打印设置
- if (isset($printTypeList[$this->print_type]) && $printTypeList[$this->print_type] == 1) {
- $printData->is_attr = $setting['is_attr'];
- $printer = $printers[$setting['printer_id']];
- $config = $printer['printer_config'];
- switch ($printer['type']) {
- case self::P_360_KDT2: //365kdt打印
- $printer = new KdtOrderPrinter($config);
- break;
- case self::P_FEIE: //飞俄打印
- $printer = new FeieOrderPrinter($config);
- break;
- case self::P_YILIANYUN_K4: //易联云打印
- $printer = new YilianyunOrderPrinter($config);
- break;
- case self::P_GAINSCHA_GP: //佳博打印
- $config['orderNo'] = $printData->order_no;
- $printer = new GpOrderPrinter($config);
- break;
- }
- $template = new DefaultTemplate();
- $template->data = $printData;
- $printer->print($template->getContentByArray());
- $count++;
- }
-
- }
- } catch(Exception $e) {
- \Yii::error('打印错误,msg=' . $e->getMessage() . ',行数:' . $e->getLine() . ', file: ' . $e->getFile());
- }
- }
- /**
- * 打印机设置
- */
- public function printSetting($printer_ids) {
- try {
- $model = Printer::find()->where(['is_delete' => 0, 'printer_id' => $printer_ids]);
- $result = $model->asArray()->all();
- if (empty($result)) return false;
- return $result;
- } catch (\Exception $e) {
- return $e->getMessage();
- }
-
- }
- /**
- * 获取打印机
- */
- public function getPrinter() {
- $printers = MallSetting::getConfByGroup($this->mall_id, 'printer', true);
- if (empty($printers)) return false;
- return json_decode($printers['printer']['value'], true);
- }
- /**
- * 设置打印数据
- */
- public function setOrderPrintData($print_order_type) {
- $mall = Mall::findOne($this->mall_id);
- if (empty($mall)) {
- return false;
- }
- $order = Order::find()->where(['id' => $this->order_id, 'user_id' => $this->user_id, 'mall_id' => $this->mall_id])->one();
- $data = new OrderContent();
- $data->mall_name = $mall->name;
- $data->order_type = $print_order_type;
- $data->attribute = $order;
- $data->created_at = date("Y-m-d H:i:s", $order->created_at);
- $paymentTypeEnum = PayTypeEnum::getMap();
- $data->payment_type = isset($paymentTypeEnum[$order->payment_type]) ? $paymentTypeEnum[$order->payment_type] : '未知方式';
- $ShippingTypeEnum = ShippingTypeEnum::getMap();
- $shippingTypText = '';
- if (isset($ShippingTypeEnum[$order->shipping_type])) {
- if ($order->shipping_type != ShippingTypeEnum::PICKUP && $order->shipping_type != ShippingTypeEnum::LOCAL_DISTRIBUTION) {
- if ($order->order_status == OrderStatusEnum::SHIPMENTS) {
- $shippingTypText = '快递发货';
- }
- }
- }
- $data->shippingTypText = $shippingTypText;
- $orderInfo = [];
- $orderDetail = $order->detail;
- foreach ($orderDetail as $item) {
- $orderInfo[] = (object)[
- 'attr' => $item->goods_attr_name ?? '',
- 'num' => $item->num ?? 0,
- 'total_price' => $item->original_goods_price ?? 0,
- 'unit_price' => $item->price ?? 0,
- 'name' => $item->goods_name ?? ''
- ];
- }
- $data->goods_list = $orderInfo;
- if ($order->shipping_type == 1 && $this->store_id != 0) {
- //查询门店
- $data->store_name = '';
- $data->store_mobile = '';
- $data->store_address = '';
- } else {
- $data->store_name = '';
- $data->store_mobile = '';
- $data->store_address = '';
- }
- return $data;
- }
-
- }
|