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; } }