1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
<?php
 
namespace app\common\service\order;
 
use app\common\model\settings\Setting as SettingModel;
use app\common\model\settings\Printer as PrinterModel;
use app\common\enum\settings\DeliveryTypeEnum;
use app\common\library\printer\Driver as PrinterDriver;
use app\supplier\model\store\Store as StoreModel;
 
/**
 * 订单打印服务类
 */
class OrderPrinterService
{
    /**
     * 执行订单打印
     */
    public function printTicket($order, $scene = 20)
    {
        /*// 打印机设置
        $printerConfig = SettingModel::getSupplierItem('printer', $order['shop_supplier_id'], $order['app_id']);
 
        // 判断是否开启打印设置
        if (!$printerConfig['is_open']
            || !$printerConfig['printer_id']
            || !in_array($scene, $printerConfig['order_status'])) {
            return false;
        }
        // 获取当前的打印机
        $printer = PrinterModel::detail($printerConfig['printer_id']);
        if (empty($printer) || $printer['is_delete']) {
            return false;
        }
        // 实例化打印机驱动
        $PrinterDriver = new PrinterDriver($printer);
        // 获取订单打印内容
        $content = $this->getPrintContent($order);
        // 执行打印请求
        return $PrinterDriver->printTicket($content);*/
        //商家打印
        $this->sellerPrint($order, $scene);
        //门店打印
        $this->storePrint($order);
    }
 
    /**
     * 商家打印  by yj
     */
    public function sellerPrint($order, $scene = 20)
    {
        // 打印机设置
        $printerConfig = SettingModel::getSupplierItem('printer', $order['shop_supplier_id'], $order['app_id']);
        // 判断是否开启打印设置
        if (!$printerConfig['is_open']
            || !$printerConfig['printer_id']
            || !in_array($scene, $printerConfig['order_status'])) {
            return false;
        }
        // 获取当前的打印机
        $printer = PrinterModel::detail($printerConfig['printer_id']);
        if (empty($printer) || $printer['is_delete']) {
            return false;
        }
        // 实例化打印机驱动
        $PrinterDriver = new PrinterDriver($printer);
        // 获取订单打印内容
        $content = $this->getPrintContent($order);
        // 执行打印请求
        return $PrinterDriver->printTicket($content);
    }
 
    /**
     * 门店打印 by yj
     */
    public function storePrint($order)
    {
        $model = new StoreModel;
        $store_id = empty($order["extract_store_id"]) ? $order["delivery_store"] : $order["extract_store_id"];
        //自提门店与配送门店都为空
        if(empty($store_id)){
            return false;
        }
        $store = $model::detail($store_id);
        //未开启或未选择打印机
        if(empty($store) || empty($store["is_printer_open"]) || empty($store["printer_id"])){
            return false;
        }
        // 获取该门店的打印机
        $printer = PrinterModel::detail($store["printer_id"]);
        if (empty($printer) || $printer['is_delete']) {
            return false;
        }
        // 实例化打印机驱动
        $PrinterDriver = new PrinterDriver($printer);
        // 获取订单打印内容
        $content = $this->getPrintContent($order);
        // 执行打印请求
        return $PrinterDriver->printTicket($content);
    }
 
    /**
     * 构建订单打印的内容
     */
    private function getPrintContent($order)
    {
        // 商城名称
        $storeName = SettingModel::getItem('store', $order['app_id'])['name'];
        // 收货地址
        $address = $order['address'];
        // 拼接模板内容
        $content = "<CB>{$storeName}</CB><BR>";
        $content .= '--------------------------------<BR>';
        $content .= "昵称:{$order['user']['nickName']} [{$order['user_id']}]<BR>";
        $content .= "订单号:{$order['order_no']}<BR>";
        $content .= '付款时间:' . date('Y-m-d H:i:s', $order['pay_time']) . '<BR>';
        // 收货人信息
        if ($order['delivery_type']['value'] == DeliveryTypeEnum::EXPRESS) {
            $content .= "--------------------------------<BR>";
            $content .= "配送方式:快递配送<BR>";
            $content .= "收货人:{$address['name']}<BR>";
            $content .= "联系电话:{$address['phone']}<BR>";
            $content .= '收货地址:' . $address->getFullAddress() . '<BR>';
        }
        // 自提信息
        if ($order['delivery_type']['value'] == DeliveryTypeEnum::EXTRACT && !empty($order['extract'])) {
            $content .= "--------------------------------<BR>";
            $content .= "配送方式:上门自提<BR>";
            $content .= "联系人:{$order['extract']['linkman']}<BR>";
            $content .= "联系电话:{$order['extract']['phone']}<BR>";
            $content .= "自提门店:{$order['extractStore']['store_name']}<BR>";
        }
        // 配送信息
        if ($order['delivery_type']['value'] == DeliveryTypeEnum::STORESS) {
            $content .= "--------------------------------<BR>";
            $content .= "配送方式:门店配送<BR>";
            $content .= "收货人:{$address['name']}<BR>";
            $content .= "联系电话:{$address['phone']}<BR>";
            $content .= '收货地址:' . $address->getFullAddress() . '<BR>';
        }
        // 商品信息
        $content .= '=========== 商品信息 ===========<BR>';
        foreach ($order['product'] as $key => $product) {
            $content .= ($key + 1) . ".商品名称:{$product['product_name']}<BR>";
            !empty($product['product_attr']) && $content .= " 商品规格:{$product['product_attr']}<BR>";
            $content .= " 购买数量:{$product['total_num']}<BR>";
            $content .= " 商品总价:{$product['total_price']}元<BR>";
            $content .= '--------------------------------<BR>';
        }
        // 买家备注
        if (!empty($order['buyer_remark'])) {
            $content .= '============ 买家备注 ============<BR>';
            $content .= "<B>{$order['buyer_remark']}</B><BR>";
            $content .= '--------------------------------<BR>';
        }
        // 订单金额
        if ($order['coupon_money'] > 0) {
            $content .= "优惠券:-{$order['coupon_money']}元<BR>";
        }
        if ($order['points_num'] > 0) {
            $content .= "积分抵扣:-{$order['points_money']}元<BR>";
        }
        if ($order['update_price']['value'] != '0.00') {
            $content .= "后台改价:{$order['update_price']['symbol']}{$order['update_price']['value']}元<BR>";
        }
        // 运费
        if ($order['delivery_type']['value'] == DeliveryTypeEnum::EXPRESS) {
            $content .= "运费:{$order['express_price']}元<BR>";
            $content .= '------------------------------<BR>';
        }
        // 实付款
        $content .= "<RIGHT>实付款:<BOLD><B>{$order['pay_price']}</B></BOLD>元</RIGHT><BR>";
        return $content;
    }
 
}