admin/addon/cashier/event/PrinterContent.php

319 lines
14 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
/**
* ThinkShop商城系统 - 团队十年电商经验汇集巨献!
* =========================================================
* Copy right 2019-2029 成都云之牛科技有限公司, 保留所有权利。
* ----------------------------------------------
* 官方网址: https://www.cdcloudshop.com
* =========================================================
*/
namespace addon\cashier\event;
use addon\memberrecharge\model\MemberrechargeOrder;
use addon\printer\model\Printer;
use addon\printer\model\PrinterOrder;
use addon\printer\model\PrinterTemplate;
use app\model\member\Member;
use app\model\order\OrderCommon as OrderCommonModel;
use app\model\shop\Shop;
use app\model\store\Store;
/**
* 应用卸载
*/
class PrinterContent
{
/**
* 执行卸载
*/
public function handle($params)
{
if ($params[ 'type' ] == 'cashierorder') {
$stage = $params[ 'printer_type' ];//打印类型,手动打印、支付、收货
$order_id = $params[ 'order_id' ];
$order_common_model = new OrderCommonModel();
$order_info = $order_common_model->getOrderDetail($order_id)[ 'data' ];
$site_id = $order_info[ 'site_id' ];
$store_id = $order_info[ 'store_id' ];
$params[ 'site_id' ] = $order_info[ 'site_id' ];
//获取店铺信息
$shop_model = new Shop();
$shop_info = $shop_model->getShopInfo([ [ 'site_id', '=', $params[ 'site_id' ] ] ], ' mobile,province_name,city_name,district_name,address')[ 'data' ];
$store_info = ( new Store() )->getStoreInfo([ [ 'store_id', '=', $store_id ] ], 'store_name,telphone,full_address,address')[ 'data' ];
//获取打印机列表
$print_model = new Printer();
if ($order_info[ 'cashier_order_type' ] == 'recharge') {
$recharge_order = ( new MemberrechargeOrder() )->getMemberRechargeOrderInfo([ [ 'relate_type', '=', 'order' ], [ 'relate_id', '=', $order_id ] ])[ 'data' ];
$order_info[ 'face_value' ] = $recharge_order[ 'face_value' ];
$printer_data = $print_model->getPrinterList([ [ 'site_id', '=', $params[ 'site_id' ] ], [ 'store_id', '=', $store_id ], [ 'recharge_open', '=', 1 ] ])[ 'data' ] ?? [];
} else {
$printer_data = $print_model->getPrinterList([ [ 'site_id', '=', $params[ 'site_id' ] ], [ 'store_id', '=', $store_id ], [ 'order_pay_open', '=', 1 ], [ 'order_pay_order_type', 'like', '%5%' ] ])[ 'data' ] ?? [];
}
if (empty($printer_data)) return error(-1, '未找到可用的打印机');
$res_data = [];
foreach ($printer_data as $k => $v) {
//此处应该根据打印机不同分别设置返回不同的数据。当前为易联云
$array = [];
$array[ 'printer_info' ] = $v;
$print_template_model = new PrinterTemplate();
$template_id = 0;
$print_num = 1;
if ($order_info[ 'cashier_order_type' ] == 'recharge') {
$template_id = $v[ 'recharge_template_id' ];
$print_num = $v[ 'recharge_print_num' ];
} else {
$template_id = $v[ 'order_pay_template_id' ];
$print_num = $v[ 'order_pay_print_num' ];
}
$print_template = $print_template_model->getPrinterTemplateInfo([ [ 'template_id', '=', $template_id ] ])[ 'data' ];
if (empty($print_template)) continue;
$array[ 'printer_code' ] = $v[ 'printer_code' ]; //商户授权机器码
$array[ 'origin_id' ] = $order_info[ 'order_no' ]; //内部订单号(32位以内)
/**文本接口开始**/
if ($order_info[ 'cashier_order_type' ] == 'recharge') {
$member_info = ( new Member() )->getMemberInfo([ [ 'member_id', '=', $order_info[ 'member_id' ] ] ], 'balance,balance_money')[ 'data' ];
$array[ 'content' ] = $this->handleRechargePrintContent($print_num, $print_template, $order_info, $shop_info, $member_info);
} else {
$array[ 'content' ] = $this->handleOrderPrintContent($print_num, $print_template, $order_info, $shop_info, $store_info);
}
$res_data[] = $array;
}
return $res_data;
}
}
/**
* 获取订单打印内容
* @param $print_num
* @param $print_template
* @param $order_info
* @param $shop_info
* @param $store_info
* @return string
*/
private function handleOrderPrintContent($print_num, $print_template, $order_info, $shop_info, $store_info)
{
$content = "<MN>" . $print_num . "</MN>";
//小票名称
if ($print_template[ 'title' ] != '') {
$content .= "<center>" . $print_template[ 'title' ] . "</center>";
$content .= str_repeat('.', 32);
}
//商城名称
if ($print_template[ 'head' ] == 1) {
$content .= "<FH2><FS><center>" . $print_template[ 'site_name' ] . "</center></FS></FH2>";
$content .= str_repeat('.', 32);
}
$content .= "订单时间:" . date("Y-m-d H:i", $order_info[ 'pay_time' ]) . "\n";
$content .= "订单编号:" . $order_info[ 'order_no' ] . "\n";
$content .= "支付方式:" . $order_info[ 'pay_type_name' ] . "\n";
$content .= str_repeat('.', 32);
$content .= "<table>";
$content .= "<tr>";
if ($print_template[ 'goods_price_show' ]) {
$content .= "<td>商品名称</td><td></td><td>数量</td><td>金额</td>";
} else {
$content .= "<td>商品名称</td><td></td><td></td><td>数量</td>";
}
$content .= "</tr>";
$content .= "</table>";
$content .= str_repeat('.', 32);
$content .= "<table>";
foreach ($order_info[ 'order_goods' ] as $goods) {
//显示售价或卖价
$price = $print_template[ 'goods_price_type' ] == 'price' ? $goods[ 'price' ] : $goods[ 'real_goods_money' ];
$sku_name_list = ( new PrinterOrder() )->r_str_pad_1($goods[ 'sku_name' ], 7);
foreach ($sku_name_list as $index => $value) {
if ($index == 0) {
if ($print_template[ 'goods_price_show' ]) {
$content .= "<tr><td>" . $value . "</td><td></td><td>x" . $goods[ 'num' ] . "</td><td>¥" . $price . "</td></tr>";
} else {
$content .= "<tr><td>" . $value . "</td><td></td><td></td><td>x" . $goods[ 'num' ] . "</td></tr>";
}
} else {
$content .= "<tr><td>" . $value . "</td></tr>";
}
}
//商品编码
if ($print_template[ 'goods_code_show' ] && !empty($goods[ 'sku_no' ])) {
$content .= "<tr><td>[" . $goods[ 'sku_no' ] . "]</td><td></td><td></td><td></td></tr>";
}
}
$content .= "</table>";
$content .= str_repeat('.', 32);
if ($order_info[ "goods_money" ] > 0) {
$content .= "商品总额:¥" . $order_info[ "goods_money" ] . "\n";
}
if ($order_info[ "coupon_money" ] > 0) {
$content .= "店铺优惠券:¥" . $order_info[ "coupon_money" ] . "\n";
}
if ($order_info[ "promotion_money" ] > 0) {
$content .= "店铺优惠:¥" . $order_info[ "promotion_money" ] . "\n";
}
if ($order_info[ "point_money" ] > 0) {
$content .= "积分抵扣:¥" . $order_info[ "point_money" ] . "\n";
}
if ($order_info[ "adjust_money" ] > 0) {
$content .= "订单调价:¥" . $order_info[ "adjust_money" ] . "\n";
}
if ($order_info[ "delivery_money" ] > 0) {
$content .= "配送费用:¥" . $order_info[ "delivery_money" ] . "\n";
}
if ($order_info[ "invoice_money" ] > 0) {
$content .= "发票费用:¥" . $order_info[ "invoice_money" ] . "\n";
}
if ($order_info[ "invoice_delivery_money" ] > 0) {
$content .= "发票邮寄费用:¥" . $order_info[ "invoice_delivery_money" ] . "\n";
}
if ($order_info[ "goods_num" ] > 0) {
$content .= "订单共" . $order_info[ 'goods_num' ] . "件商品,总计: ¥" . $order_info[ 'order_money' ] . " \n";
}
$content .= str_repeat('.', 32);
/******************** 备注信息 **************************/
//买家留言
if ($print_template[ 'buy_notes' ] == 1) {
$order_info[ "buyer_message" ] = $order_info[ "buyer_message" ] ? $order_info[ "buyer_message" ] : '无';
$content .= "<FH2>买家留言:" . $order_info[ "buyer_message" ] . "</FH2>\n";
$content .= str_repeat('.', 32);
}
//卖家留言
if ($print_template[ 'seller_notes' ] == 1) {
$order_info[ "remark" ] = $order_info[ "remark" ] ? $order_info[ "remark" ] : '无';
$content .= "<FH2>卖家留言:" . $order_info[ "remark" ] . "</FH2>\n";
$content .= str_repeat('.', 32);
}
//表单
if ($print_template[ 'form_show' ] == 1 && addon_is_exit('form')) {
$form_data = model('form_data')->getInfo([ [ 'site_id', '=', $order_info[ 'site_id' ] ], [ 'scene', '=', 'order' ], [ 'relation_id', '=', $order_info[ 'order_id' ] ] ]);
if ($form_data) {
$form_data = json_decode($form_data[ 'form_data' ], true);
foreach ($form_data as $item) {
$content .= "<FH2>" . $item[ 'title' ] . "" . $item[ "val" ] . "</FH2>\n";
$content .= str_repeat('.', 32);
}
}
}
/******************** 买家信息 **************************/
//买家姓名
if ($print_template[ 'buy_name' ] == 1) {
$content .= "" . $order_info[ "name" ] . "\n";
}
//联系方式
if ($print_template[ 'buy_mobile' ] == 1) {
$content .= "" . $order_info[ "mobile" ] . "\n";
}
//地址
if ($print_template[ 'buy_address' ] == 1) {
$content .= "" . $order_info[ 'full_address' ] . "-" . $order_info[ 'address' ] . "\n";
}
if ($print_template[ 'buy_name' ] == 1 || $print_template[ 'buy_mobile' ] == 1 || $print_template[ 'buy_address' ] == 1) {
$content .= str_repeat('.', 32);
}
/******************** 商城信息 **************************/
//联系方式
if ($print_template[ 'shop_mobile' ] == 1) {
$content .= "" . $shop_info[ "mobile" ] . "\n";
}
//地址
if ($print_template[ 'shop_address' ] == 1) {
$content .= "" . $shop_info[ 'province_name' ] . $shop_info[ 'city_name' ] . $shop_info[ 'district_name' ] . $shop_info[ 'address' ] . "\n";
}
if ($print_template[ 'shop_mobile' ] == 1 || $print_template[ 'shop_address' ] == 1) {
$content .= str_repeat('.', 32);
}
//二维码
if ($print_template[ 'shop_qrcode' ] == 1) {
$content .= "<QR>" . $print_template[ 'qrcode_url' ] . "</QR>";
$content .= str_repeat('.', 32);
}
/******************** 门店信息 **************************/
if ($order_info[ 'store_id' ] > 0 && !empty($store_info)) {
$content .= "" . $store_info[ "store_name" ] . "\n";//门店名称
$content .= "" . $store_info[ "telphone" ] . "\n";//门店电话
$content .= "" . $store_info[ "full_address" ] . "\n";//门店地址
$content .= str_repeat('.', 32);
}
//底部内容
if (!empty($print_template[ 'bottom' ])) {
$content .= "<center>" . $print_template[ 'bottom' ] . "</center>";
}
return $content;
}
public function handleRechargePrintContent($print_num, $print_template, $order_info, $shop_info, $member_info)
{
/**文本接口开始**/
$content = "<MN>" . $print_num . "</MN>";
//小票名称
if ($print_template[ 'title' ] != '') {
$content .= "<center>" . $print_template[ 'title' ] . "</center>";
$content .= str_repeat('.', 32);
}
//商城名称
if ($print_template[ 'head' ] == 1) {
$content .= "<FH2><FS><center>" . $print_template[ 'site_name' ] . "</center></FS></FH2>";
$content .= str_repeat('.', 32);
}
$content .= "订单时间:" . date("Y-m-d H:i", $order_info[ 'pay_time' ]) . "\n";
$content .= "订单编号:" . $order_info[ 'order_no' ] . "\n";
$content .= "支付方式:" . $order_info[ 'pay_type_name' ] . "\n";
$content .= str_repeat('.', 32);
$content .= "充值金额:¥" . $order_info[ "face_value" ] . "\n";
$content .= "实付金额:¥" . $order_info[ "pay_money" ] . "\n";
$content .= "会员余额:¥" . ( round($member_info[ 'balance' ] + $member_info[ 'balance_money' ], 2) ) . "\n";
/******************** 买家信息 **************************/
//买家姓名
if ($print_template[ 'buy_name' ] == 1) {
$content .= "会员:" . $order_info[ "nickname" ] . "\n";
$content .= str_repeat('.', 32);
}
/******************** 商城信息 **************************/
//联系方式
if ($print_template[ 'shop_mobile' ] == 1) {
$content .= "" . $shop_info[ "mobile" ] . "\n";
}
//地址
if ($print_template[ 'shop_address' ] == 1) {
$content .= "" . $shop_info[ 'province_name' ] . $shop_info[ 'city_name' ] . $shop_info[ 'district_name' ] . $shop_info[ 'address' ] . "\n";
}
if ($print_template[ 'shop_mobile' ] == 1 || $print_template[ 'shop_address' ] == 1) {
$content .= str_repeat('.', 32);
}
//二维码
if ($print_template[ 'shop_qrcode' ] == 1) {
$content .= "<QR>" . $print_template[ 'qrcode_url' ] . "</QR>";
$content .= str_repeat('.', 32);
}
//底部内容
if (!empty($print_template[ 'bottom' ])) {
$content .= "<center>" . $print_template[ 'bottom' ] . "</center>";
}
return $content;
}
}