| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209 |
- <?php
- namespace addons\Printer\common\logic;
- use common\enums\StatusEnum;
- use common\models\order\Order;
- use common\models\mall\Mall;
- use common\enums\PayTypeEnum;
- use common\enums\ShippingTypeEnum;
- use addons\Printer\common\logic\templates\DefaultTemplate;
- use addons\Printer\common\logic\printer\YilianyunOrderPrinter;
- use common\models\common\MallSetting;
- use addons\Printer\common\models\Printer;
- use addons\Printer\common\models\PrinterTemplate;
- use Exception;
- class PrintOrder
- {
- private $order;
- private $order_detail;
- private $mall_id;
- private $event;
- private $config;
- private $template_list;
- private $mall_info;
- //打印类型
- const PRINT_TYPE_ORDER = 'order_printer_config'; //下单
- const PRINT_TYPE_PAY = 'pay_printer_config'; //支付
- const PRINT_TYPE_CONFIRM = 'confirm_printer_config'; //确认收货
- const PRINT_TYPE_SYSTEM = 'system_printer_config'; //后台
- public function setPrinter($param){
- $this->event = $param['event'];
- $this->order = Order::find()->where(['id' => $param['order_id']])->one();
- $this->mall_id = $this->order['mall_id'];
- $this->order_detail = $this->order->detail;
- $params = [
- 'where'=>[['=','mall_id',$this->mall_id],['in','name',['contact_tel','web_url','quick_map_address']]],
- 'select'=>['value','name']
- ];
- $this->mall_info = array_column(MallSetting::lists($params),'value','name');
- return $this;
- }
- /**
- * 获取模板
- * @return array
- */
- public function getOpenPrinterConfig(){
- $params = [
- 'select' => [$this->event,'terminal','secret_key','api_key','user_id','printer_type','store_id'],
- 'where' => [['=', 'mall_id', $this->mall_id],['<>','status',StatusEnum::DELETE]]
- ];
- $list = Printer::lists($params);
- if(empty($list)){
- return [];
- }
- $template_id = [];
- foreach($list as $k => $val){
- $v = json_decode($val[$this->event], true);
- if($v['is_printer']==0){
- unset($list[$k]);
- }else{
- $this->config[] = [
- 'client_id'=>$val['user_id'],
- 'machine_code'=>$val['terminal'],
- 'key'=>$val['secret_key'],
- 'client_key'=>$val['api_key'],
- 'time' => $v['num'],
- 'printer_type' => $val['printer_type'],
- 'store_id' => $val['store_id'],
- 'template_id'=>$v['template_id'],
- ];
- }
- }
- }
- public function getPrinterTemplate(){
- $template_id = array_column($this->config, 'template_id');
- $params = [
- 'where' => [['=', 'mall_id', $this->mall_id],['<>','status',StatusEnum::DELETE],['in', 'id', $template_id]]
- ];
- $this->template_list = array_column(PrinterTemplate::lists($params),null,'id');
- }
- /**
- * 打印
- */
- public function print() {
- try{
- //判断是商场打印机还是门店打印机
- $this->getOpenPrinterConfig();
- $this->getPrinterTemplate();
- if (empty($this->template_list) || empty($this->config)) {
- return;
- }
- $template = new DefaultTemplate();
- foreach($this->config as $config){
- 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'])){
- //var_dump($config['printer_type'],$config['store_id']);
- $printer = new YilianyunOrderPrinter($config);
- $template_info = $this->template_list[$config['template_id']];
- $printData = $this->setOrderPrintData($template_info);;
- $template->data = $printData;
- $printer->print($template->getContentByArray());
- }
- }
- } catch(Exception $e) {
- \Yii::error('打印错误,msg=' . $e->getMessage() . ',行数:' . $e->getLine());
- }
- }
- /**
- * 设置打印数据
- */
- public function setOrderPrintData($template) {
- $mall = Mall::findOne($this->mall_id);
- if (empty($mall)) {
- return false;
- }
- $data = new OrderContent();
- $data->mall_name = empty($template['head_info']) ? '' :$mall->name;
- $data->ticket_nam = $template['ticket_nam'];
- $data->order_type = '商城订单';
- $data->attribute = $this->order;
- $data->created_at = date("Y-m-d H:i:s", $this->order->created_at);
- $data->contact_tel = $this->mall_info['contact_tel'];
- $data->quick_map_address = $this->mall_info['quick_map_address'];
- $data->qrcode_url = $template['qrcode_url'];
- $data->mall_qrcode_url = $this->mall_info['web_url']; //var_dump($this->mall_info['quick_map_address']);
- $data->bottom_info = $template['bottom_info'];
- $paymentTypeEnum = PayTypeEnum::getMap();
- $data->payment_type = isset($paymentTypeEnum[$this->order->payment_type]) ? $paymentTypeEnum[$this->order->payment_type] : '未知方式';
- $ShippingTypeEnum = ShippingTypeEnum::getMap();
- $orderInfo = [];
- foreach ($this->order_detail 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;
- $data->is_show_price = false;
- $data->is_show_barcode = false;
- if(!empty($template['goods_info'])){
- $goods_info = json_decode($template['goods_info'],true);
- if(in_array(1,$goods_info)){
- $data->is_show_price = true;
- }
- if(in_array(2,$goods_info)){
- $data->is_show_barcode = true;
- }
- }
- $data->price = $template['price_info']==1 ? $data->original_goods_price : $data->pay_money;
- if(!empty($template['remarks_info'])){
- $remarks_info = json_decode($template['remarks_info'], true);
- if(in_array(1,$remarks_info) && $data->remark){
- $data->is_show_remark = true;
- }
- if(in_array(2,$remarks_info) && $data->seller_remark){
- $data->is_show_seller_remark = true;
- }
- }
- if(!empty($template['buyer_info'])){
- $buyer_info = json_decode($template['buyer_info'], true);
- if(in_array(1,$buyer_info)){
- $data->is_show_name = true;
- }
- if(in_array(2,$buyer_info)){
- $data->is_show_phone = true;
- }
- if(in_array(3,$buyer_info)){
- $data->is_show_addr = true;
- }
- }
- if(!empty($template['mall_info'])){
- $mall_info = json_decode($template['mall_info'], true);
- if(in_array(1,$mall_info)){
- $data->is_show_mall_phone = true;
- }
- if(in_array(2,$mall_info)){
- $data->is_show_mall_addr = true;
- }
- if(in_array(3,$mall_info)){
- $data->is_show_mall_code = true;
- }
- }
- if(!empty($template['qrcode_url'])){
- $data->qrcode_url = $template['qrcode_url'];
- $data->is_show_qrcode_url = true;
- }
- $data->bottom_info = $template['bottom_info'];
- return $data;
- }
- }
|