store_id = $store_id;
$this->mch_id = (int)$mch_id;
$this->store_name = Option::get('name', $store_id, 'store')['value'];
$md = Md::findOne($md_id);
if ($md) {
$this->md = $md;
} else {
$this->md = null;
}
$this->md_id = $md ? $md_id : -1;
$this->order_id = $order_id;
$this->print_type = $print_type;
$this->order_type = $order_type;
$this->type = $type;
$this->supplier_id = $supplier_id;
}
public function print_order()
{
try {
// 打印设置
if (in_array((int)$this->type, [1, 2])) {
$this->setting = PrinterSetting::findOne(['supplier_id' => $this->supplier_id ?: get_supplier_id()]);
} else {
$this->setting = PrinterSetting::findOne(['store_id' => $this->store_id, 'md_id' => $this->md_id]);
if (!$this->setting) {
$this->md_id = 0;
$this->setting = PrinterSetting::findOne(['store_id' => $this->store_id, 'md_id' => $this->md_id]);
}
}
if($this->mch_id){
$this->setting = PrinterSetting::findOne(['mch_id' => $this->mch_id]);
}
if (!$this->setting) {
\Yii::warning('未设置打印设置');
throw new \Exception('未设置打印设置');
}
$type = json_decode($this->setting->type, true);
if (!($type[$this->print_type] && $type[$this->print_type] == 1 || $this->print_type == 'reprint')) {
\Yii::warning('打印设置打印方式');
throw new \Exception('打印设置打印方式');
}
if (in_array((int)$this->type, [1, 2])) {
$this->printer = Printer::findAll(['id' => explode(',',$this->setting->printer_id), 'is_delete' => 0]);
} else {
$this->printer = Printer::findAll(['id' => explode(',',$this->setting->printer_id), 'is_delete' => 0, 'md_id' => $this->md_id]);
}
if (count($this->printer) == 0) { // 打印机参数
\Yii::warning('未找到设置的打印机');
throw new \Exception('未找到设置的打印机');
}
// 整理订单打印信息 商城订单打印
if (intval($this->type) === 0) {
$order_id = $this->order_id;
} else {
$order_data = [
'order_id' => $this->order_id,
'status' => $this->type//1转单订单 2采购订单
];
$domain = (new OptionSetting)->getCloudDomainName();
$order_url = "/user/getOrder";
$orderInfo = cloud_post($domain . $order_url,
$order_data);
$orderInfo = json_decode($orderInfo,true);
if($orderInfo['code'] !== 0){
return $orderInfo['msg'];
}
$orderInfo['data']['total_price'] = $orderInfo['data']['pay_price'];
$orderInfo['data']['pay_type'] = 0;
$goods_send_type = $orderInfo['data']['goods_send_type'];
switch (intval($goods_send_type)) {
case 1:
$send_type = "集采";
break;
case 2:
$send_type = "自主配送";
break;
case 3:
$send_type = "代理配送";
break;
case 4:
$send_type = "云仓库存";
break;
case 5:
$send_type = "仓库同城";
break;
case 6:
$send_type = "仓库快递";
break;
default:
$send_type = "快递";
break;
}
$orderInfo['data']['is_pay'] = intval($orderInfo['data']['status']) === 2 ? 0 : 1;
$orderInfo['data']['created_at'] = $orderInfo['data']['pay_time'];
$this->order = $orderInfo['data'];
// 这里要根据商城订单的配送状态
if($this->type){
$orderT = OrderTransit::findOne(['cloud_order_id' => $this->order_id]);
$storeOrder = Order::findOne($orderT->order_id);
$this->order['md_id'] = $storeOrder->md_id;
$this->order['is_offline'] = $storeOrder->is_offline;
}
$order_type = '供货商' . (intval($this->type) === 1 ? '转单订单' : '批发订单');
$this->goods_list = $orderInfo['data']['goods_list'];
}
if (isset($order_id)) {
$this->order = Order::find()->where(['id' => $order_id, 'is_delete' => 0])->asArray()->one();
$this->goods_list = $this->getOrderGoodsList($this->order['id']);
}
if (intval($this->type) === 0) {
if ($this->order['is_offline'] == 0) {
if ($this->order['is_delivery'] == 0) {
$send_type = '快递配送';
} else {
$send_type = '同城配送';
}
} else {
$send_type = '到店自提';
}
$order_type = '商城订单';
if (isset($this->order['order_origin']) && intval($this->order['order_origin']) === 4) {
$send_type = '收银台';
$order_type = '收银台订单';
}
}
$this->extra = [
'order_type' => $order_type,
'send_type' => $send_type,
];
switch ($this->order['pay_type']) {
case 1:
case 0:
$this->extra['pay_type'] = "线上支付";
break;
case 2:
$this->extra['pay_type'] = "货到付款";
break;
case 3:
$this->extra['pay_type'] = "余额支付";
break;
default:
$this->extra['pay_type'] = "";
}
foreach($this->printer as $printer){
$this->handlePrint($printer);
}
return [
'code' => 0,
'msg' => '打印成功',
'data' => [],
'cc' => [$order_id, $this->order_id, $this->type, [], $this->goods_list, $this->order],
];
} catch (\Exception $e) {
return [
'code' => 1,
'msg' => $e->getMessage() . $e->getFile() . $e->getLine(),
'cc' => [$order_id, $this->order_id, $this->type, [], $this->goods_list, $this->order],
'data' => $e
];
}
}
private function handlePrint($printer)
{
// 打印机配置
$this->printer_setting = json_decode($printer->printer_setting, true);
if ($printer->printer_type == 'kdt2') {
$res = $this->printer_1();
}
if ($printer->printer_type == 'yilianyun-k4') {
$res = $this->printer_2();
}
if ($printer->printer_type == 'feie') {
$res = $this->printer_3();
}
if ($printer->printer_type == 'gp') {
$res = $this->printer_4();
}
if ($printer->printer_type == 'xhy') {
$res = $this->printer_5($printer);
}
if ($printer->printer_type == 'dqzn') {
$res = $this->printer_6($printer);
}
if ($printer->printer_type == 'feie_label') {
$res = $this->printer_7();
}
}
/**
* 365打印机
*/
private function printer_1()
{
$big = [
1 => [
'start' => '',
'end' => ''
],
2 => [
'start' => "",
'end' => ""
],
3 => [
'start' => '',
'end' => ''
],
];
$start = $big[$this->setting->big]['start'];
$end = $big[$this->setting->big]['end'];
date_default_timezone_set("Asia/Shanghai");
$order = $this->order;
$addtime = date('Y-m-d H:i:s', $order['created_at']);
$goods_list = $this->goods_list;
header("Content-Type: text/html;charset=utf-8");
$DEVICE_NO = $this->printer_setting['name'];
$key = $this->printer_setting['key'];
$time = $this->printer_setting['time'];//打印联数
$content = "";
if ($this->mch && $this->mch->name){
$title_name = $this->mch->name;
}else{
$title_name = $this->store_name;
}
$content .= "{$title_name}
";
if (!empty($order['food_flag_id'])) {
$FoodFlag = FoodFlag::findOne($order['food_flag_id']);
$food_num = $FoodFlag->table_num;
$content .= "桌号:{$food_num}\n";
}
if ($this->is_refund) {
$content .= "订单类型:退款订单
";
} else {
if ($order['food_code']) {
$content .= "标识码:{$order['food_code']}
";
}
$content .= "订单类型:{$this->extra['order_type']}
";
}
$content .= "支付方式:{$this->extra['pay_type']}
";
$isPay = $order['is_pay'] == 1 ? '已支付' : '未支付';
$content .= "支付状态:{$isPay}
";
$content .= "发货方式:{$this->extra['send_type']}
";
$content .= "订单号:{$order['order_no']}
";
$content .= "下单时间:{$addtime}
";
if ($this->setting->is_attr == 0) {
$content .= "--------------------------------
";
$content .= "名称 单价 数量 金额
";
$content .= "--------------------------------
";
foreach ($goods_list as $k => $v) {
$price = round($v['total_price'] / $v['num'], 2);
$arr = $this->r_str_pad_1($v['name'], 7);
foreach ($arr as $index => $value) {
if ($index == 0) {
$content .= $value . " " . str_pad($price, 5) . " " . str_pad($v['num'], 6) . " " . round($v['total_price'], 2) . '
';
} else {
$content .= $value . '
';
}
}
}
} else {
/**
* $content .= "--------------------------------
";
* $content .= "名称 规格 单价 数量 金额
";
* $content .= "--------------------------------
";
* foreach ($goods_list as $k => $v) {
* $attr = json_decode($v['attr'], true);
* $attr_arr = [];
* foreach ($attr as $i => $j) {
* $attr_arr[] = $j['attr_group_name'] . $j['attr_name'];
* }
* $attr_str = implode(',', $attr_arr);
* $attr_arr_1 = $this->r_str_pad_1($attr_str, 4);
* $price = round($v['total_price'] / $v['num'], 2);
* $arr = $this->r_str_pad_1($v['name'], 4);
* $a = count($attr_arr_1) >= count($arr) ? $attr_arr_1 : $arr;
* foreach ($a as $index => $value) {
* $string = "";
* $string_1 = "";
* if (isset($arr[$index])) {
* $string = $arr[$index];
* }
* if (isset($attr_arr_1[$index])) {
* if (!$string) {
* $string = str_pad($string, 8);
* }
* $string_1 = $attr_arr_1[$index];
* }
* if ($index == 0) {
* $content .= $string . " " . $string_1 . str_pad($price, 5) . " " . str_pad($v['num'], 3) . " " . round($v['total_price'], 2) . '
';
* } else {
* $content .= $string . " " . $string_1 . '
';
* }
* }
* }
* */
$content .= "--------------------------------
";
$content .= "名称 数量 金额
";
$content .= "--------------------------------
";
foreach ($goods_list as $k => $v) {
$name = $v['name'];
if (!empty($v['attr'])) {
$attr = json_decode($v['attr'], true);
$attr_arr = [];
if ($attr['attr_list']) {
$attr = $attr['attr_list'];
}
foreach ($attr as $i => $j) {
$attr_arr[] = $j['attr_group_name'] . $j['attr_name'];
}
$attr_str = implode(',', $attr_arr);
$name = $v['name'] . '(' . $attr_str . ')';
}
$name_arr = $this->r_str_pad_1($name, 8);
foreach ($name_arr as $index => $value) {
if ($index == count($name_arr) - 1) {
$content .= $name_arr[$index] . " " . str_pad('×' . $v['num'], 7) . " " . round($v['total_price'], 2) . "
";
} else {
$content .= $name_arr[$index] . '
';
}
}
}
}
$content .= "--------------------------------
";
if ($order['express_price']) {
$content .= "运费:{$order['express_price']}元
";
}
$content .= "总计:{$order['total_price']}元
";
if (isset($order['user_coupon_id'])) {
$content .= "优惠券优惠:{$order['coupon_sub_price']}元
";
}
if (isset($order['integral'])) {
$integral = json_decode($order['integral'], true);
if ($integral['forehead'] != 0) {
$content .= "积分抵扣:{$integral['forehead']}元
";
}
}
if (isset($order['discount']) && $order['discount'] < 10) {
$content .= "会员折扣:{$order['discount']}折
";
}
if ($this->is_refund) {
$content .= "实退:{$order['pay_price']}元
";
} else {
$content .= $order['is_pay'] == 1 ? "实付:{$order['pay_price']}元
" : "实付:0元
";
}
if ($this->order_type != 3) {
if ((isset($order['is_offline']) && $order['is_offline'] != 1) || (isset($order['offline']) && $order['offline'] != 2)) {
$content .= "{$start}收货人:{$order['name']}
";
$content .= "收货地址:{$order['address']}
";
$content .= "收货人电话:{$order['mobile']}
{$end}";
$content .= "--------------------------------
";
} else {
$content .= "{$start}联系人:{$order['name']}
";
$content .= "联系人电话:{$order['mobile']}
{$end}";
$content .= "--------------------------------
";
$content .= "门店信息
";
// 门店信息要根据订单上的md_id查询门店 不能根据get_md_id获取 也不能根据构造函数中传入的md_id 因为供货商打印 或者 平台打印 或者 商城打印的时候 是不会传md_id的
$md = Md::findOne($order['md_id']);
$content .= "{$md->name}
";
$content .= "{$md->mobile}
";
$content .= "{$md->address}
";
$content .= "--------------------------------
";
}
}
if ($order['remark']) {
$content .= "备注:{$order['remark']}
";
} else {
$orderForm = OrderForm::findAll([
'order_id' => $this->order['id'], 'store_id' => $this->store_id, 'is_delete' => 0
]);
if ($orderForm) {
/* @var $orderForm OrderForm[] */
foreach ($orderForm as $item) {
$content .= $item->key . ':' . $item->value . "
";
}
}
}
if ($order['seller_comments']) {
$content .= "商家备注:{$order['seller_comments']}
";
}
$result = $this->sendSelfFormatOrderInfo($DEVICE_NO, $key, $time, $content);
\Yii::warning('==>>' . $result);
$result = Json::decode($result);
if ($result['responseCode'] != 0) {
throw new Exception($result['msg'], $result);
}
return $result;
}
/**
* 365打印机
*/
private function sendSelfFormatOrderInfo($device_no, $key, $times, $orderInfo)
{
// $times打印次数
$selfMessage = array(
'deviceNo' => $device_no,
'printContent' => $orderInfo,
'key' => $key,
'times' => $times
);
$url = "http://open.printcenter.cn:8080/addOrder";
$options = array(
'http' => array(
'header' => "Content-type: application/x-www-form-urlencoded ",
'method' => 'POST',
'content' => http_build_query($selfMessage),
),
);
$context = stream_context_create($options);
$result = file_get_contents($url, false, $context);
return $result;
}
/**
* @param $order_id
* @return mixed
* 订单商品详情
*/
public function getOrderGoodsList($order_id)
{
$order_detail_list = OrderDetail::find()->alias('od')
->leftJoin(['g' => Goods::tableName()], 'od.goods_id=g.id')
->where([
'od.is_delete' => 0,
'od.order_id' => $order_id,
])->select('od.*,g.name, g.product_type')->asArray()->all();
$new_arr = [];
foreach ($order_detail_list as $item) {
$open = true;
if (!empty($item['food_ext_goods'])) {
$food_ext_goods = json_decode($item['food_ext_goods'], true);
if (!empty($food_ext_goods)) {
$open = false;
foreach ($food_ext_goods as &$ext_item) {
$ext_item['is_food_ext_goods'] = 1;
$ext_item['total_price'] = $ext_item['price'];
}
$new_arr = array_merge($new_arr, [$item], $food_ext_goods);
}
}
if ($open) {
$orderGoodsCancel = OrderGoodsCancel::find()->where(['order_detail_id' => $item['id'], 'status' => [
OrderGoodsCancel::STATUS_PASS,
OrderGoodsCancel::STATUS_PAY,
OrderGoodsCancel::STATUS_PAY_FAIL,
]])->groupBy('order_detail_id')
->select('SUM(num) as num, SUM(refund_price) as refund_price')
->asArray()->one();
$this->order['total_price'] = bcsub($this->order['total_price'], $orderGoodsCancel['refund_price'], 2);
$this->order['pay_price'] = bcsub($this->order['pay_price'], $orderGoodsCancel['refund_price'], 2);
$item['total_price'] = bcsub($item['total_price'], $orderGoodsCancel['refund_price'], 2);
$item['num'] = bcsub($item['num'], $orderGoodsCancel['num']);
if ($item['num'] <= 0) {
continue;
}
if ($item['total_price'] <= 0) {
$item['total_price'] = 0;
}
$new_arr = array_merge($new_arr, [$item]);
}
}
return $new_arr;
}
/**
* 补齐空格
* 截取七个中文字符长度
*/
private function r_str_pad($input, $n = 7)
{
$string = "";
$count = 0;
$c_count = 0;
for ($i = 0; $i < mb_strlen($input, 'UTF-8'); $i++) {
$char = mb_substr($input, $i, 1, 'UTF-8');
$string .= $char;
if (strlen($char) == 3) {
$count += 2;
$c_count++;
} else {
$count += 1;
}
if ($count >= $n * 2) {
break;
}
}
if ($count < $n * 2) {
$string = str_pad($string, $n * 2 + $c_count);
}
return $string;
}
/**
* 补齐空格
* 每n个中文字符长度为一个数组元素
*/
private function r_str_pad_1($input, $n = 7)
{
$string = "";
$count = 0;
$c_count = 0;
$arr = array();
for ($i = 0; $i < mb_strlen($input, 'UTF-8'); $i++) {
$char = mb_substr($input, $i, 1, 'UTF-8');
$string .= $char;
if (strlen($char) == 3) {
$count += 2;
$c_count++;
} else {
$count += 1;
}
if ($count >= $n * 2) {
$arr[] = $string;
$string = '';
$count = 0;
$c_count = 0;
}
}
if ($count < $n * 2) {
$string = str_pad($string, $n * 2 + $c_count);
$arr[] = $string;
}
return $arr;
}
/**
* 易联云打印机-k4
*/
private function printer_2()
{
$big = [
1 => [
'start' => '',
'end' => ''
],
2 => [
'start' => '',
'end' => ''
],
3 => [
'start' => '',
'end' => ''
],
];
$start = $big[$this->setting->big]['start'];
$end = $big[$this->setting->big]['end'];
date_default_timezone_set("Asia/Shanghai");
$machine_code = $this->printer_setting['machine_code'];//终端编号
$key = $this->printer_setting['key'];//终端密钥
$client_id = $this->printer_setting['client_id'];//用户ID
$client_key = $this->printer_setting['client_key'];//用户密钥
$time = $this->printer_setting['time'];//打印联数
$order = $this->order;
$addtime = date('Y-m-d H:i:s', $order['created_at']);
$goods_list = $this->goods_list;
$print = new Yprint();
$content = "{$time}";
if ($this->mch && $this->mch->name) {
$title_name = $this->mch->name;
} else {
$title_name = $this->store_name;
}
$content .= "{$title_name}\n";
if (!empty($order['food_flag_id'])) {
$FoodFlag = FoodFlag::findOne($order['food_flag_id']);
$food_num = $FoodFlag->table_num;
$content .= "桌号:{$food_num}\n";
}
if ($this->is_refund) {
$content .= "订单类型:退款订单\n";
} else {
if ($order['food_code']) {
$content .= "标识码:{$order['food_code']}\n";
}
$content .= "订单类型:{$this->extra['order_type']}\n";
}
$content .= "支付方式:{$this->extra['pay_type']}\n";
$isPay = $order['is_pay'] == 1 ? '已支付' : '未支付';
$content .= "支付状态:{$isPay}\n";
$content .= "发货方式:{$this->extra['send_type']}\n";
$content .= "订单号:{$order['order_no']}\n";
$content .= "下单时间:{$addtime}\n";
$content .= "--------------------------------\n";
if ($this->setting->is_attr == 0) {
$content .= "
| 名称 | 单价 | 数量 | 金额 |
";
foreach ($goods_list as $k => $v) {
$price = round($v['total_price'] / $v['num'], 2);
$v['name'] = str_replace(',', ',', $v['name']);
$arr = $this->r_str_pad_1($v['name'], 8);
foreach ($arr as $index => $value) {
if ($index == 0) {
$content .= "| " . $value . " | " . $price . " | " . $v['num'] . " | " . round($v['total_price'], 2) . " |
";
} else {
$content .= "| " . $value . " |
";
}
}
}
$content .= "
";
} else {
/**
* $content .= "| 名称 | 规格 | 数量 | 单价 |
";
* foreach ($goods_list as $k => $v) {
* $attr = json_decode($v['attr'], true);
* $attr_arr = [];
* foreach ($attr as $i => $j) {
* $attr_arr[] = $j['attr_group_name'] . $j['attr_name'];
* }
* $attr_str = implode(',', $attr_arr);
* $attr_arr_1 = $this->r_str_pad_1($attr_str, 4);
* $price = round($v['total_price'] / $v['num'], 2);
* $v['name'] = str_replace(',', ',', $v['name']);
* $arr = $this->r_str_pad_1($v['name'], 4);
* $a = count($attr_arr_1) >= count($arr) ? $attr_arr_1 : $arr;
* foreach ($a as $index => $value) {
* $string = "";
* $string_1 = "";
* if (isset($arr[$index])) {
* $string = $arr[$index];
* }
* if (isset($attr_arr_1[$index])) {
* if (!$string) {
* $string = str_pad($string, 8);
* }
* $string_1 = $attr_arr_1[$index];
* }
* if ($index == 0) {
* $content .= "| " . $string . " | " . $string_1 . " | " . $v['num'] . " | " . $price . " |
";
* } else {
* $content .= "| " . $string . " | " . $string_1 . " |
";
* }
* }
* }
* $content .= "
";
* */
$content .= "| 名称 | 数量 | 总价 |
";
foreach ($goods_list as $k => $v) {
$name = $v['name'];
if (!empty($v['attr'])) {
if (!is_array($v['attr'])) {
$attr = json_decode($v['attr'], true);
} else {
$attr = $v['attr'];
}
if (intval($v['product_type']) === 0) {
$attr_arr = [];
if($attr['attr_list']){
$attr = $attr['attr_list'];
}
foreach ($attr as $i => $j) {
$attr_arr[] = ($j['attr_group_name'] ?? '') . ($j['attr_name'] ?? '');
}
$attr_str = implode(',', $attr_arr);
$name = $v['name'] . '(' . $attr_str . ')';
} elseif (intval($v['product_type']) === 1) {
$name = $v['name'] . '(预约日期:' . $attr[0]['date'] . '-' . $attr[count($attr) - 1]['date'] . ')';
} elseif (intval($v['product_type']) === 2) {
$name = $v['name'] . '(预约时间:' . $attr['data'] . ' ' . $attr['time'] . ')';
}
}
$name_arr = $this->r_str_pad_1($name, 6);
foreach ($name_arr as $index => $value) {
if ($index == count($name_arr) - 1) {
$content .= "| " . $name_arr[$index] . " | " . '×' . $v['num'] . " | " . round($v['total_price'], 2) . " |
";
} else {
$content .= "| " . $name_arr[$index] . " | | |
";
}
}
}
$content .= "
";
}
$content .= "--------------------------------\n";
if ($order['express_price']) {
$content .= "运费:{$order['express_price']}元\n";
}
$content .= "总计:{$order['total_price']}元\n";
if (isset($order['user_coupon_id'])) {
$content .= "优惠券优惠:{$order['coupon_sub_price']}元\n";
}
if (isset($order['integral'])) {
$integral = json_decode($order['integral'], true);
if ($integral['forehead'] != 0) {
$content .= "积分抵扣:{$integral['forehead']}元\n";
}
}
if (isset($order['discount']) && $order['discount'] < 10) {
$content .= "会员折扣:{$order['discount']}折\n";
}
if ($this->is_refund) {
$content .= "实退:{$order['pay_price']}元\n";
} else {
$content .= $order['is_pay'] == 1 ? "实付:{$order['pay_price']}元\n" : "实付:0元\n";
}
if ($this->order_type != 3) {
if ((isset($order['is_offline']) && $order['is_offline'] != 1) || (isset($order['offline']) && $order['offline'] != 2)) {
$content .= "{$start}收货人:{$order['name']}\n";
$content .= "收货地址:{$order['address']}\n";
$content .= "收货人电话:{$order['mobile']}\n{$end}";
$content .= "--------------------------------\n";
} else {
$content .= "{$start}联系人:{$order['name']}\n";
$content .= "联系人电话:{$order['mobile']}\n{$end}";
$content .= "--------------------------------\n";
$content .= "门店信息\n";
// 门店信息要根据订单上的md_id查询门店 不能根据get_md_id获取 也不能根据构造函数中传入的md_id 因为供货商打印 或者 平台打印 或者 商城打印的时候 是不会传md_id的
$md = Md::findOne($order['md_id']);
$content .= "{$md->name}\n";
$content .= "{$md->mobile}\n";
$content .= "{$md->address}\n";
$content .= "--------------------------------\n";
}
}
if ($order['remark']) {
$content .= "备注:{$order['remark']}\n";
} else {
$orderForm = OrderForm::findAll([
'order_id' => $this->order['id'], 'store_id' => $this->store_id, 'is_delete' => 0
]);
if ($orderForm) {
/* @var $orderForm OrderForm[] */
foreach ($orderForm as $item) {
$content .= $item->key . ':' . $item->value . "\n";
}
}
}
if ($order['seller_comments']) {
$content .= "商家备注:{$order['seller_comments']}
";
}
$result = $print->action_print($client_id, $machine_code, $content, $client_key, $key);
$result = Json::decode($result);
switch ($result['state']) {
case 1:
return $result;
break;
case 2:
throw new Exception('提交时间超时。验证你所提交的时间戳超过3分钟后拒绝接受', $result);
break;
case 3:
throw new Exception('参数有误', $result);
break;
case 4:
throw new Exception('sign加密验证失败', $result);
break;
default:
throw new Exception('打印机设置错误', $result);
break;
}
}
/**
* 补齐空格
* 截取$n个中文字符长度
*/
private function r_mb_str($input, $n)
{
$string = "";
$count = 0;
$c_count = 0;
for ($i = 0; $i < mb_strlen($input, 'UTF-8'); $i++) {
$char = mb_substr($input, $i, 1, 'UTF-8');
$string .= $char;
if (strlen($char) == 3) {
$count += 2;
$c_count++;
} else {
$count += 1;
}
if ($count >= 2 * $n) {
break;
}
}
if ($count < 2 * $n) {
$string = str_pad($string, 2 * $n + $c_count);
}
return $string;
}
// 飞鹅打印机
public function printer_3()
{
$big = [
1 => [
'start' => '',
'end' => ''
],
2 => [
'start' => '',
'end' => ''
],
3 => [
'start' => '',
'end' => ''
],
];
$start = $big[$this->setting->big]['start'];
$end = $big[$this->setting->big]['end'];
date_default_timezone_set("Asia/Shanghai");
// $order = Order::findOne(['id' => $this->order_id, 'is_delete' => 0]);
$order = $this->order;
$addtime = date('Y-m-d H:i:s', $order['created_at']);
// $goods_list = $this->getOrderGoodsList($order->id);
$goods_list = $this->goods_list;
$time = $this->printer_setting['time'];//打印联数
$content = "";
if ($this->mch && $this->mch->name){
$title_name = $this->mch->name;
}else{
$title_name = $this->store_name;
}
$content .= "{$title_name}
";
if (!empty($order['food_flag_id'])) {
$FoodFlag = FoodFlag::findOne($order['food_flag_id']);
$food_num = $FoodFlag->table_num;
$content .= "桌号:{$food_num}\n";
}
if ($this->is_refund) {
$content .= "订单类型:退款订单
";
} else {
if ($order['food_code']) {
$content .= "标识码:{$order['food_code']}
";
}
$content .= "订单类型:{$this->extra['order_type']}
";
}
$content .= "支付方式:{$this->extra['pay_type']}
";
$isPay = $order['is_pay'] == 1 ? '已支付' : '未支付';
$content .= "支付状态:{$isPay}
";
$content .= "发货方式:{$this->extra['send_type']}
";
$content .= "订单号:{$order['order_no']}
";
$content .= "下单时间:{$addtime}
";
if ($this->setting->is_attr == 0) {
$content .= "--------------------------------
";
$content .= "名称 单价 数量 金额
";
$content .= "--------------------------------
";
// foreach ($goods_list as $k => $v) {
// $price = round($v['total_price'] / $v['num'], 2);
// $content .= $this->r_str_pad($v['name']) . " " . str_pad($price, 5) . " " . str_pad($v['num'], 4) . " " . round($v['total_price'], 2) . '
';
// }
foreach ($goods_list as $k => $v) {
$price = round($v['total_price'] / $v['num'], 2);
$arr = $this->r_str_pad_1($v['name'], 7);
foreach ($arr as $index => $value) {
if ($index == 0) {
$content .= $value . " " . str_pad($price, 5) . " " . str_pad($v['num'], 6) . " " . round($v['total_price'], 2) . '
';
} else {
$content .= $value . '
';
}
}
}
} else {
/**
* $content .= "--------------------------------
";
* $content .= "名称 规格 单价 数量 金额
";
* $content .= "--------------------------------
";
* foreach ($goods_list as $k => $v) {
* $attr = json_decode($v['attr'], true);
* $attr_arr = [];
* foreach ($attr as $i => $j) {
* $attr_arr[] = $j['attr_group_name'] . $j['attr_name'];
* }
* $attr_str = implode(',', $attr_arr);
* $attr_arr_1 = $this->r_str_pad_1($attr_str, 4);
* $price = round($v['total_price'] / $v['num'], 2);
* $arr = $this->r_str_pad_1($v['name'], 4);
* $a = count($attr_arr_1) >= count($arr) ? $attr_arr_1 : $arr;
* foreach ($a as $index => $value) {
* $string = "";
* $string_1 = "";
* if (isset($arr[$index])) {
* $string = $arr[$index];
* }
* if (isset($attr_arr_1[$index])) {
* if (!$string) {
* $string = str_pad($string, 8);
* }
* $string_1 = $attr_arr_1[$index];
* }
* if ($index == 0) {
* $content .= $string . " " . $string_1 . str_pad($price, 5) . " " . str_pad($v['num'], 3) . " " . round($v['total_price'], 2) . '
';
* } else {
* $content .= $string . " " . $string_1 . '
';
* }
* }
* }
* */
$content .= "--------------------------------
";
$content .= "名称 数量 金额
";
$content .= "--------------------------------
";
foreach ($goods_list as $k => $v) {
$name = $v['name'];
if (!empty($v['attr'])) {
if (is_array($v['attr'])) {
$attr = $v['attr'];
} else {
$attr = json_decode($v['attr'], true);
}
$attr_arr = [];
if ($attr['attr_list']) {
$attr = $attr['attr_list'];
}
foreach ($attr as $i => $j) {
$attr_arr[] = ($j['attr_group_name'] ?? '') . $j['attr_name'];
}
$attr_str = implode(',', $attr_arr);
$name = $v['name'] . '(' . $attr_str . ')';
}
$name_arr = $this->r_str_pad_1($name, 8);
foreach ($name_arr as $index => $value) {
if ($index == count($name_arr) - 1) {
$content .= $name_arr[$index] . " " . str_pad('×' . $v['num'], 7) . " " . round($v['total_price'] ?? $v['price'], 2) . "
";
} else {
$content .= $name_arr[$index] . '
';
}
}
}
}
$content .= "--------------------------------
";
if ($order['express_price']) {
$content .= "运费:{$order['express_price']}元
";
}
$content .= "总计:{$order['total_price']}元
";
if (isset($order['user_coupon_id'])) {
$content .= "优惠券优惠:{$order['coupon_sub_price']}元
";
}
if (isset($order['integral'])) {
$integral = json_decode($order['integral'], true);
if ($integral['forehead'] != 0) {
$content .= "积分抵扣:{$integral['forehead']}元
";
}
}
// if (isset($order['discount']) && $order['discount'] < 10) {
// $content .= "会员折扣:{$order['discount']}折
";
// }
if ($this->is_refund) {
$content .= "实退:{$order['pay_price']}元
";
} else {
$content .= $order['is_pay'] == 1 ? "实付:{$order['pay_price']}元
" : "实付: 0元
";
}
// if ($this->order_type != 3) {
// if ((isset($order['is_offline']) && $order['is_offline'] != 1) || (isset($order['offline']) && $order['offline'] != 2)) {
//
// } else {
// $content .= "{$start}联系人:{$order['name']}
";
// $content .= "联系人电话:{$order['mobile']}
{$end}";
// $content .= "--------------------------------
";
// $content .= "门店信息
";
// $content .= "{$this->order->shop->name}
";
// $content .= "{$this->order->shop->mobile}
";
// $content .= "{$this->order->shop->address}
";
// $content .= "--------------------------------
";
// }
// }
if (isset($order['is_offline']) && $order['is_offline'] != 1) {
$content .= "{$start}收货人:{$order['name']}
";
$content .= "收货地址:{$order['address']}
";
$content .= "收货人电话:{$order['mobile']}
{$end}";
$content .= "--------------------------------
";
} else {
$content .= "{$start}联系人:{$order['name']}
";
$content .= "联系人电话:{$order['mobile']}
{$end}";
$content .= "--------------------------------
";
// 门店信息要根据订单上的md_id查询门店 不能根据get_md_id获取 也不能根据构造函数中传入的md_id 因为供货商打印 或者 平台打印 或者 商城打印的时候 是不会传md_id的
$md = Md::findOne($order['md_id']);
$content .= "门店信息
";
$content .= "门店名称:{$md->name}
";
$content .= "门店联系人:{$md->contact}
";
$content .= "门店电话:{$md->mobile}
";
$content .= "门店地址:{$md->address}
";
$content .= "--------------------------------
";
}
if ($order['remark']) {
$content .= "备注:{$order['remark']}
";
} else {
$orderForm = OrderForm::findAll([
'order_id' => $this->order['id'], 'store_id' => $this->store_id, 'is_delete' => 0
]);
if ($orderForm) {
/* @var $orderForm OrderForm[] */
foreach ($orderForm as $item) {
$content .= $item->key . ':' . $item->value . "
";
}
}
}
if ($order['seller_comments']) {
$content .= "商家备注:{$order['seller_comments']}
";
}
// var_dump($content);die;
$resule = $this->wp_print($this->printer_setting['sn'], $content, $time);
$resule = Json::decode($resule);
if ($resule['ret'] != 0) {
throw new Exception($resule['msg'], $resule);
}
return $resule;
}
// 飞鹅标签打印机
public function printer_7(){
date_default_timezone_set("Asia/Shanghai");
$order = $this->order;
$goods_list = $this->goods_list;
$time = $this->printer_setting['time'];//打印联数
//商品信息
$glist = array(
'orderid'=>'001',
'remark'=>'',
'name'=>'',
'tel'=>'',
'z_number'=>'',
'goods'=>array(
// '0'=>array('title'=>'可乐鸡翅'),
),
);
if (!empty($order['food_flag_id'])) {
$FoodFlag = FoodFlag::findOne($order['food_flag_id']);
$food_num = $FoodFlag->table_num;
$glist['z_number'] = $food_num . '号桌';
}else{
$glist['z_number'] = " ";
}
if ($order['remark']) {
$glist['remark'] = $order['remark'];
}
if ($this->is_refund) {
$glist['remark'] = "**退款订单 {$order['pay_price']}元";
}
if ($order['food_code']) {
$glist['orderid'] = $order['food_code'];
}else{
$glist['orderid'] = substr($order['order_no'], -4);
}
foreach ($goods_list as $k => $v) {
if($v['is_food_ext_goods']){
continue;
}
$food_ext_goods_names = [];
if (!empty($v['food_ext_goods'])) {
$food_ext_goods = json_decode($v['food_ext_goods'], true);
if (!empty($food_ext_goods)) {
$food_ext_goods_names = array_column($food_ext_goods, 'name');
}
}
$name = $v['name'];
if (!empty($v['attr'])) {
if (is_array($v['attr'])) {
$attr = $v['attr'];
} else {
$attr = json_decode($v['attr'], true);
}
$attr_arr = [];
if ($attr['attr_list']) {
$attr = $attr['attr_list'];
}
foreach ($attr as $i => $j) {
$attr_arr[] = $j['attr_name'];
}
$attr_str = implode(',', $attr_arr);
}
for($i=1; $i<=$v['num']; $i++){
$glist['goods'][] = [
'attr_str'=>$attr_str,
'title'=>$name,
'food_ext_goods_names'=>$food_ext_goods_names,
];
}
}
$glist['name'] = $order['name'];
$glist['tel'] = $order['mobile'];
$num = count($glist['goods']);
$itemI = 1;
$contents = [];
foreach ($glist['goods'] as $key => $value) {
$content = ''.$this->LR('#'.$glist['orderid'], $itemI.'/'.$num,23,$glist['z_number']) .'';
$glist['remark'] && $content .= ''.$glist['remark'].'';
$content .= ' 6 ? 1 : 2) .'" h="2" r="0">'.$this->LR('', '', (mb_strlen($value["title"]) > 6 ? 26 : 13), $value["title"]).'';
$value['attr_str'] && $content .= ''.$this->LR('', '', 26, mb_substr($value['attr_str'], 0, 12)).'';
mb_strlen($value['attr_str']) > 12 && $content .= ''.$this->LR('', '', 26, mb_substr($value['attr_str'], 12)).'';
if($value['food_ext_goods_names']){
$content .= ''.$this->LR('', '', 26, implode(',', $value['food_ext_goods_names'])).'';
}
$content .= ''. $this->LR(mb_substr($glist['name'], 0, 1) . '**', substr_replace($glist['tel'], '****', 3, 4), 23).'';//40mm宽度标签纸一行占用26个字符,50mm宽度标签纸请改成32个字符
$contents[] = [
'content' => $content,
'times' => $time,
];
$itemI++;
}
\Yii::error($contents);
// var_dump($contents);die;
$resule = $this->wp_print_label($this->printer_setting['sn'], json_encode($contents), 1);
$resule = Json::decode($resule);
debug_log($resule, __CLASS__);
\Yii::error($resule);
if ($resule['ret'] != 0) {
throw new Exception($resule['msg'], $resule);
}
return $resule;
}
/**
* [统计字符串字节数补空格,实现左右排版对齐]
* @param [string] $str_left [左边字符串]
* @param [string] $str_right [右边字符串]
* @param [int] $length [输入当前纸张规格一行所支持的最大字母数量]
* 58mm的机器,一行打印16个汉字,32个字母;76mm的机器,一行打印22个汉字,33个字母,80mm的机器,一行打印24个汉字,48个字母
* 标签机宽度50mm,一行32个字母,宽度40mm,一行26个字母
* @return [string] [返回处理结果字符串]
*/
public function LR($str_left,$str_right,$length,$str_center = ''){
if( empty($str_left) && empty($str_right) && empty($length) ) return '请输入正确的参数';
$kw = '';
$str_left_lenght = strlen(iconv("UTF-8", "GBK//IGNORE", $str_left));
$str_right_lenght = strlen(iconv("UTF-8", "GBK//IGNORE", $str_right));
$k = $length - ($str_left_lenght+$str_right_lenght);
for($q=0;$q<$k;$q++){
$kw .= ' ';
}
if($str_center){
$str_center_lenght = strlen(iconv("UTF-8", "GBK//IGNORE", $str_center));
if(strlen($kw) > $str_center_lenght){
$kk = (strlen($kw) - $str_center_lenght) / 2;
$kkw = '';
for($q=1;$q<$kk;$q++){
$kkw .= ' ';
}
$kw = $kkw . $str_center . $kkw;
}else{
$kw = $str_center;
}
}
return $str_left.$kw.$str_right;
}
/**
* 方法1
* 拼凑订单内容时可参考如下格式
* 根据打印纸张的宽度,自行调整内容的格式,可参考下面的样例格式
*/
private function wp_print($printer_sn, $orderInfo, $times)
{
$user = $this->printer_setting['user'];
$ukey = $this->printer_setting['ukey'];
$time = time();
$ip = 'api.feieyun.cn';
$path = '/Api/Open/';
$content = array(
'user' => $user,
'stime' => $time,
'sig' => sha1($user . $ukey . $time),
'apiname' => 'Open_printMsg',
'sn' => $printer_sn,
'content' => $orderInfo,
'times' => $times//打印次数
);
$client = new FeieYun($ip, 80);
if (!$client->post($path, $content)) {
return 'error';
} else {
//服务器返回的JSON字符串,建议要当做日志记录起来
return $client->getContent();
}
}
private function wp_print_label($printer_sn, $orderInfos, $times)
{
$user = $this->printer_setting['user'];
$ukey = $this->printer_setting['ukey'];
$time = time();
$ip = 'api.feieyun.cn';
$path = '/Api/Open/';
$content = array(
'user' => $user,
'stime' => $time,
'sig' => sha1($user . $ukey . $time),
'apiname' => 'Open_printLabelMsg',
'sn' => $printer_sn,
'contents' => $orderInfos,
'times' => $times//打印次数
);
$client = new FeieYun($ip, 80);
if (!$client->post($path, $content)) {
return 'error';
} else {
//服务器返回的JSON字符串,建议要当做日志记录起来
return $client->getContent();
}
}
// 秒杀商品详情
public function getMsOrderGoodsList()
{
$ms_goods = MsGoods::findOne(['id' => $this->order['goods_id']]);
$list['name'] = $ms_goods->name;
$list['total_price'] = $this->order['pay_price'];
$list['num'] = $this->order['num'];
$list['attr'] = $this->order['attr'];
$new_list = [];
array_push($new_list, $list);
return $new_list;
}
// 拼团商品详情
public function getPtOrderGoodsList()
{
$order_detail_list = PtOrderDetail::find()->alias('od')
->leftJoin(['g' => PtGoods::tableName()], 'od.goods_id=g.id')
->where(['od.is_delete' => 0, 'od.order_id' => $this->order['id']])
->select(['od.*', 'g.name'])->asArray()->all();
return $order_detail_list;
}
// 预约商品详情
public function getYyOrderGoodsList()
{
$order_detail_list = YyOrder::find()->alias('od')
->leftJoin(['g' => YyGoods::tableName()], 'od.goods_id = g.id')
->where(['od.is_delete' => 0, 'od.order_id' => $this->order['id']])
->select(['od.*', 'g.name'])->asArray()->one();
$order_detail_list['num'] = 1;
return [$order_detail_list];
}
public function printer_4()
{
$big = $this->setting->big;
$order = $this->order;
$addtime = date('Y-m-d H:i:s', $order['created_at']);
$goods_list = $this->goods_list;
header("Content-Type: text/html;charset=utf-8");
$content = "";
if ($this->mch && $this->mch->name) {
$title_name = $this->mch->name;
} else {
$title_name = $this->store_name;
}
$content .= "{$title_name}";
if (!empty($order['food_flag_id'])) {
$FoodFlag = FoodFlag::findOne($order['food_flag_id']);
$food_num = $FoodFlag->table_num;
$content .= "桌号:{$food_num}\n";
}
if ($this->is_refund) {
$content .= "订单类型:退款订单";
} else {
if ($order['food_code']) {
$content .= "标识码:{$order['food_code']}";
}
$content .= "订单类型:{$this->extra['order_type']}";
}
$content .= "支付方式:{$this->extra['pay_type']}";
$isPay = $order['is_pay'] == 1 ? '已支付' : '未支付';
$content .= "支付状态:{$isPay}";
$content .= "发货方式:{$this->extra['send_type']}";
$content .= "订单号:{$order['order_no']}";
$content .= "下单时间:{$addtime}";
if ($this->setting->is_attr == 0) {
$content .= "--------------------------------";
$content .= "名称 单价 数量 金额";
$content .= "--------------------------------";
foreach ($goods_list as $k => $v) {
$price = round($v['total_price'] / $v['num'], 2);
$arr = $this->r_str_pad_1($v['name'], 7);
foreach ($arr as $index => $value) {
if ($index == 0) {
$content .= $value . " " . str_pad($price, 5) . " " . str_pad($v['num'], 6) . " " . round($v['total_price'], 2) . '';
} else {
$content .= $value . '';
}
}
}
} else {
$content .= "--------------------------------";
$content .= "名称 数量 金额 ";
$content .= "--------------------------------";
foreach ($goods_list as $k => $v) {
$name = $v['name'];
if (!empty($v['attr'])) {
$attr = json_decode($v['attr'], true);
$attr_arr = [];
if($attr['attr_list']){
$attr = $attr['attr_list'];
}
foreach ($attr as $i => $j) {
$attr_arr[] = $j['attr_group_name'] . $j['attr_name'];
}
$attr_str = implode(',', $attr_arr);
$name = $v['name'] . '(' . $attr_str . ')';
}
$name_arr = $this->r_str_pad_1($name, 8);
foreach ($name_arr as $index => $value) {
if ($index == count($name_arr) - 1) {
$content .= $name_arr[$index] . " " . str_pad('×' . $v['num'], 7) . " " . round($v['total_price'], 2) . "";
} else {
$content .= $name_arr[$index] . '';
}
}
}
}
$content .= "--------------------------------";
if ($order['express_price']) {
$content .= "运费:{$order['express_price']}元";
}
$content .= "总计:{$order['total_price']}元";
if (isset($order['user_coupon_id'])) {
$content .= "优惠券优惠:{$order['coupon_sub_price']}元";
}
if (isset($order['integral'])) {
$integral = json_decode($order['integral'], true);
if ($integral['forehead'] != 0) {
$content .= "积分抵扣:{$integral['forehead']}元";
}
}
if (isset($order['discount']) && $order['discount'] < 10) {
$content .= "会员折扣:{$order['discount']}折";
}
if ($this->is_refund) {
$content .= "实退:{$order['pay_price']}元";
} else {
$content .= $order['is_pay'] == 1 ? "实付:{$order['pay_price']}元" : "实付:0元";
}
if ($this->order_type != 3) {
if ((isset($order['is_offline']) && $order['is_offline'] != 1) || (isset($order['offline']) && $order['offline'] != 2)) {
$content .= "收货人:{$order['name']}";
$content .= "收货地址:{$order['address']}";
$content .= "收货人电话:{$order['mobile']}";
$content .= "--------------------------------";
} else {
$content .= "联系人:{$order['name']}";
$content .= "联系人电话:{$order['mobile']}";
$content .= "--------------------------------";
$content .= "门店信息";
// 门店信息要根据订单上的md_id查询门店 不能根据get_md_id获取 也不能根据构造函数中传入的md_id 因为供货商打印 或者 平台打印 或者 商城打印的时候 是不会传md_id的
$md = Md::findOne($order['md_id']);
$content .= "{$md->name}";
$content .= "{$md->mobile}";
$content .= "{$md->address}";
$content .= "--------------------------------";
}
}
if ($order['remark']) {
$content .= "备注:{$order['remark']}";
} else {
$orderForm = OrderForm::findAll([
'order_id' => $this->order['id'], 'store_id' => $this->store_id, 'is_delete' => 0
]);
if ($orderForm) {
/* @var $orderForm OrderForm[] */
foreach ($orderForm as $item) {
$content .= $item->key . ':' . $item->value . "";
}
}
}
if ($order['seller_comments']) {
$content .= "商家备注:{$order['seller_comments']}";
}
$content .= "";
$res = $this->poscom($content);
$res = Json::decode($res);
if ($res['code'] != 0) {
throw new Exception($res['msg'], $res);
}
return $res;
}
public function printer_5($printer = null)
{
$big = [
1 => [
'start' => '',
'end' => ''
],
2 => [
'start' => "",
'end' => ""
],
3 => [
'start' => '',
'end' => ''
],
];
$start = $big[$this->setting->big]['start'];
$end = $big[$this->setting->big]['end'];
date_default_timezone_set("Asia/Shanghai");
$order = $this->order;
$addtime = date('Y-m-d H:i:s', $order['created_at']);
$goods_list = $this->goods_list;
header("Content-Type: text/html;charset=utf-8");
$time = $this->printer_setting['time'];//打印联数
$content = "";
if ($this->mch && $this->mch->name){
$title_name = $this->mch->name;
}else{
$title_name = $this->store_name;
}
$content .= "{$title_name}
";
if (!empty($order['food_flag_id'])) {
$FoodFlag = FoodFlag::findOne($order['food_flag_id']);
$food_num = $FoodFlag->table_num;
$content .= "桌号:{$food_num}\n";
}
if ($this->is_refund) {
$content .= "订单类型:退款订单
";
} else {
if ($order['food_code']) {
$content .= "标识码:{$order['food_code']}
";
}
$content .= "订单类型:{$this->extra['order_type']}
";
}
$content .= "支付方式:{$this->extra['pay_type']}
";
$isPay = $order['is_pay'] == 1 ? '已支付' : '未支付';
$content .= "支付状态:{$isPay}
";
$content .= "发货方式:{$this->extra['send_type']}
";
$content .= "订单号:{$order['order_no']}
";
$content .= "下单时间:{$addtime}
";
if ($this->setting->is_attr == 0) {
$content .= "--------------------------------
";
$content .= "名称 单价 数量 金额
";
$content .= "--------------------------------
";
foreach ($goods_list as $k => $v) {
$price = round($v['total_price'] / $v['num'], 2);
$arr = $this->r_str_pad_1($v['name'], 7);
foreach ($arr as $index => $value) {
if ($index == 0) {
$content .= $value . " " . str_pad($price, 5) . " " . str_pad($v['num'], 6) . " " . round($v['total_price'], 2) . '
';
} else {
$content .= $value . '
';
}
}
}
} else {
$content .= "--------------------------------
";
$content .= "名称 数量 金额
";
$content .= "--------------------------------
";
foreach ($goods_list as $k => $v) {
$name = $v['name'];
if (!empty($v['attr'])) {
$attr = json_decode($v['attr'], true);
$attr_arr = [];
if($attr['attr_list']){
$attr = $attr['attr_list'];
}
foreach ($attr as $i => $j) {
$attr_arr[] = $j['attr_group_name'] . $j['attr_name'];
}
$attr_str = implode(',', $attr_arr);
$name = $v['name'] . '(' . $attr_str . ')';
}
$name_arr = $this->r_str_pad_1($name, 8);
foreach ($name_arr as $index => $value) {
if ($index == count($name_arr) - 1) {
$content .= $name_arr[$index] . " " . str_pad('×' . $v['num'], 7) . " " . round($v['total_price'], 2) . "
";
} else {
$content .= $name_arr[$index] . '
';
}
}
}
}
$content .= "--------------------------------
";
if ($order['express_price']) {
if ($order['express_price'] == 0) {
$delivery_info = DeliveryInfo::findOne(['order_no' => $order['order_no']]);
if ($delivery_info && $delivery_info->fee > 0) {
$order['express_price'] = $delivery_info->fee;
}
}
$content .= "运费:{$order['express_price']}元
";
}
$content .= "总计:{$order['total_price']}元
";
if (isset($order['user_coupon_id'])) {
$content .= "优惠券优惠:{$order['coupon_sub_price']}元
";
}
if (isset($order['integral'])) {
$integral = json_decode($order['integral'], true);
if ($integral['forehead'] != 0) {
$content .= "积分抵扣:{$integral['forehead']}元
";
}
}
if (isset($order['discount']) && $order['discount'] < 10) {
$content .= "会员折扣:{$order['discount']}折
";
}
if ($this->is_refund) {
$content .= "实退:{$order['pay_price']}元
";
} else {
$content .= $order['is_pay'] == 1 ? "实付:{$order['pay_price']}元
" : "实付:0元
";
}
if ($this->order_type != 3) {
if ((isset($order['is_offline']) && $order['is_offline'] != 1) || (isset($order['offline']) && $order['offline'] != 2)) {
$content .= "{$start}收货人:{$order['name']}
";
$content .= "收货地址:{$order['address']}
";
$content .= "收货人电话:{$order['mobile']}
{$end}";
$content .= "--------------------------------
";
} else {
$content .= "{$start}联系人:{$order['name']}
";
$content .= "联系人电话:{$order['mobile']}
{$end}";
$content .= "--------------------------------
";
$content .= "门店信息
";
// 门店信息要根据订单上的md_id查询门店 不能根据get_md_id获取 也不能根据构造函数中传入的md_id 因为供货商打印 或者 平台打印 或者 商城打印的时候 是不会传md_id的
$md = Md::findOne($order['md_id']);
$content .= "{$md->name}
";
$content .= "{$md->mobile}
";
$content .= "{$md->address}
";
$content .= "--------------------------------
";
}
}
if ($order['content']) {
$content .= "备注:{$order['content']}
";
} else {
$orderForm = OrderForm::findAll([
'order_id' => $this->order['id'], 'store_id' => $this->store_id, 'is_delete' => 0
]);
if ($orderForm) {
/* @var $orderForm OrderForm[] */
foreach ($orderForm as $item) {
$content .= $item->key . ':' . $item->value . "
";
}
}
}
if ($order['seller_comments']) {
$content .= "商家备注:{$order['seller_comments']}
";
}
try {
$result = (new XhyPrint($printer->id))->print($this->printer_setting['sn'], $content, $time);
\Yii::warning('==>>' . $result);
} catch (\Exception $e) {
throw new Exception($e->getMessage(), $result);
}
return $result;
}
public function printer_6($printer = null)
{
$big = [
1 => [
'start' => '',
'end' => ''
],
2 => [
'start' => "",
'end' => ""
],
3 => [
'start' => '',
'end' => ''
],
];
$start = $big[$this->setting->big]['start'];
$end = $big[$this->setting->big]['end'];
date_default_timezone_set("Asia/Shanghai");
$order = $this->order;
$addtime = date('Y-m-d H:i:s', $order['created_at']);
$goods_list = $this->goods_list;
header("Content-Type: text/html;charset=utf-8");
$time = $this->printer_setting['time'];//打印联数
$content = "";
if ($this->mch && $this->mch->name){
$title_name = $this->mch->name;
}else{
$title_name = $this->store_name;
}
$content .= "{$title_name}
";
if (!empty($order['food_flag_id'])) {
$FoodFlag = FoodFlag::findOne($order['food_flag_id']);
$food_num = $FoodFlag->table_num;
$content .= "桌号:{$food_num}\n";
}
if ($this->is_refund) {
$content .= "订单类型:退款订单
";
} else {
if ($order['food_code']) {
$content .= "标识码:{$order['food_code']}
";
}
$content .= "订单类型:{$this->extra['order_type']}
";
}
$content .= "支付方式:{$this->extra['pay_type']}
";
$isPay = $order['is_pay'] == 1 ? '已支付' : '未支付';
$content .= "支付状态:{$isPay}
";
$content .= "发货方式:{$this->extra['send_type']}
";
$content .= "订单号:{$order['order_no']}
";
$content .= "下单时间:{$addtime}
";
if ($this->setting->is_attr == 0) {
$content .= "--------------------------------
";
$content .= "名称 单价 数量 金额
";
$content .= "--------------------------------
";
foreach ($goods_list as $k => $v) {
$price = round($v['total_price'] / $v['num'], 2);
$arr = $this->r_str_pad_1($v['name'], 7);
foreach ($arr as $index => $value) {
if ($index == 0) {
$content .= $value . " " . str_pad($price, 5) . " " . str_pad($v['num'], 6) . " " . round($v['total_price'], 2) . '
';
} else {
$content .= $value . '
';
}
}
}
} else {
$content .= "--------------------------------
";
$content .= "名称 数量 金额
";
$content .= "--------------------------------
";
foreach ($goods_list as $k => $v) {
$name = $v['name'];
if (!empty($v['attr'])) {
$attr = json_decode($v['attr'], true);
$attr_arr = [];
if($attr['attr_list']){
$attr = $attr['attr_list'];
}
foreach ($attr as $i => $j) {
$attr_arr[] = $j['attr_group_name'] . $j['attr_name'];
}
$attr_str = implode(',', $attr_arr);
$name = $v['name'] . '(' . $attr_str . ')';
}
$name_arr = $this->r_str_pad_1($name, 8);
foreach ($name_arr as $index => $value) {
if ($index == count($name_arr) - 1) {
$content .= $name_arr[$index] . " " . str_pad('×' . $v['num'], 7) . " " . round($v['total_price'], 2) . "
";
} else {
$content .= $name_arr[$index] . '
';
}
}
}
}
$content .= "--------------------------------
";
if ($order['express_price']) {
if ($order['express_price'] == 0) {
$delivery_info = DeliveryInfo::findOne(['order_no' => $order['order_no']]);
if ($delivery_info && $delivery_info->fee > 0) {
$order['express_price'] = $delivery_info->fee;
}
}
$content .= "运费:{$order['express_price']}元
";
}
$content .= "总计:{$order['total_price']}元
";
if (isset($order['user_coupon_id'])) {
$content .= "优惠券优惠:{$order['coupon_sub_price']}元
";
}
if (isset($order['integral'])) {
$integral = json_decode($order['integral'], true);
if ($integral['forehead'] != 0) {
$content .= "积分抵扣:{$integral['forehead']}元
";
}
}
if (isset($order['discount']) && $order['discount'] < 10) {
$content .= "会员折扣:{$order['discount']}折
";
}
if ($this->is_refund) {
$content .= "实退:{$order['pay_price']}元
";
} else {
$content .= $order['is_pay'] == 1 ? "实付:{$order['pay_price']}元
" : "实付:0元
";
}
if ($this->order_type != 3) {
if ((isset($order['is_offline']) && $order['is_offline'] != 1) || (isset($order['offline']) && $order['offline'] != 2)) {
$content .= "{$start}收货人:{$order['name']}{$end}
";
$content .= "{$start}收货地址:{$order['address']}{$end}
";
$content .= "{$start}收货人电话:{$order['mobile']}{$end}
";
$content .= "--------------------------------
";
} else {
$content .= "{$start}联系人:{$order['name']}{$end}
";
$content .= "{$start}联系人电话:{$order['mobile']}{$end}
";
$content .= "--------------------------------
";
$content .= "门店信息
";
// 门店信息要根据订单上的md_id查询门店 不能根据get_md_id获取 也不能根据构造函数中传入的md_id 因为供货商打印 或者 平台打印 或者 商城打印的时候 是不会传md_id的
$md = Md::findOne($order['md_id']);
$content .= "{$md->name}
";
$content .= "{$md->mobile}
";
$content .= "{$md->address}
";
$content .= "--------------------------------
";
}
}
if ($order['remark']) {
$content .= "备注:{$order['content']}
";
} else {
$orderForm = OrderForm::findAll([
'order_id' => $this->order['id'], 'store_id' => $this->store_id, 'is_delete' => 0
]);
if ($orderForm) {
/* @var $orderForm OrderForm[] */
foreach ($orderForm as $item) {
$content .= $item->key . ':' . $item->value . "
";
}
}
}
if ($order['seller_comments']) {
$content .= "商家备注:{$order['seller_comments']}
";
}
try {
$result = (new DqznPrint($printer->id))->print($this->printer_setting['sn'], $content, $time);
\Yii::warning('==>>' . $result);
} catch (\Exception $e) {
throw new Exception($e->getMessage(), $result);
}
return $result;
}
public function poscom($msgDetail, $times = 1)
{
$reqTime = $this->getMillisecond();
$memberCode = $this->printer_setting['memberCode'];
$deviceNo = $this->printer_setting['deviceNo'];
$apiKey = $this->printer_setting['apiKey'];
$orderNo = $this->order['order_no'] . '-' . $times . '-' . $this->print_type;
$securityCode = md5($memberCode . $deviceNo . $orderNo . $reqTime . $apiKey);
$url = 'http://api.poscom.cn/apisc/sendMsg';
$content['charset'] = 'UTF-8';
$content['reqTime'] = $reqTime;
$content['memberCode'] = $memberCode;
$content['deviceNo'] = $deviceNo;
$content['securityCode'] = $securityCode;
$content['msgDetail'] = $msgDetail;
$content['msgNo'] = $orderNo;
$content['mode'] = 2;
$content['times'] = $this->printer_setting['time'] > 5 ? 5 : $this->printer_setting['time'];
$res = $this->request_post($url, $content);
return $res;
}
public function getMillisecond()
{
list($t1, $t2) = explode(' ', microtime());
return (float)sprintf('%.0f', (floatval($t1) + floatval($t2)) * 1000);
}
public function request_post($url = '', $post_data = array())
{
if (empty($url) || empty($post_data)) {
return false;
}
$postUrl = $url;
$curlPost = $post_data;
$ch = curl_init();//初始化curl
curl_setopt($ch, CURLOPT_URL, $postUrl);//抓取指定网页
curl_setopt($ch, CURLOPT_HEADER, 0);//设置header
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//要求结果为字符串且输出到屏幕上
curl_setopt($ch, CURLOPT_POST, 1);//post提交方式
curl_setopt($ch, CURLOPT_POSTFIELDS, $curlPost);
$data = curl_exec($ch);//运行curl
curl_close($ch);
return $data;
}
/**
* @param $order_id
* @return mixed
* 砍价订单商品详情
*/
public function getBargainOrderGoodsList($order_id)
{
$order_detail_list = BargainOrderDetail::find()->alias('od')
->leftJoin(['g' => BargainGoods::tableName()], 'od.goods_id=g.id')
->where([
'od.is_delete' => 0,
'od.order_id' => $order_id,
])->select('od.*,g.name')->asArray()->all();
return $order_detail_list;
}
// /**
// * @param $order_id
// * @return mixed
// * 当面付订单商品详情
// */
// public function getScanOrderGoodsList($order_id)
// {
// $order_detail_list = \app\plugins\scanCodePay\models\OrderDetail::find()->alias('od')
// ->leftJoin(['g' => Goods::tableName()], 'od.goods_id=g.id')
// ->where([
// 'od.is_delete' => 0,
// 'od.order_id' => $order_id,
// 'g.type' => 6
// ])->select('od.*,g.name')->asArray()->all();
// return $order_detail_list;
// }
}