<?php
namespace app\common\service\order;
use app\common\model\Setting as SettingModel;
use app\common\model\Printer as PrinterModel;
use app\common\enum\DeliveryType as DeliveryTypeEnum;
use app\common\library\printer\Driver as PrinterDriver;
class Printer
{
public function printTicket($order, $scene)
{
$printerConfig = SettingModel::getItem('printer', $order['wxapp_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);
}
private function getPrintContent($order)
{
$storeName = SettingModel::getItem('store', $order['wxapp_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 .= "收货人:{$address['name']}<BR>";
$content .= "联系电话:{$address['phone']}<BR>";
$content .= '收货地址:' . $address->getFullAddress() . '<BR>';
}
if ($order['delivery_type']['value'] == DeliveryTypeEnum::EXTRACT && !empty($order['extract'])) {
$content .= "--------------------------------<BR>";
$content .= "联系人:{$order['extract']['linkman']}<BR>";
$content .= "联系电话:{$order['extract']['phone']}<BR>";
$content .= "自提门店:{$order['extract_shop']['shop_name']}<BR>";
}
$content .= '=========== 商品信息 ===========<BR>';
foreach ($order['goods'] as $key => $goods) {
$content .= ($key + 1) . ".商品名称:{$goods['goods_name']}<BR>";
!empty($goods['goods_attr']) && $content .= " 商品规格:{$goods['goods_attr']}<BR>";
$content .= " 购买数量:{$goods['total_num']}<BR>";
$content .= " 商品总价:{$goods['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;
}
}