service = new PrintService();
}
public function buy($param)
{
$PrinterModel = D('RetailPrinter');
$BillModel = D('RetailBillOrder');
$BillOnlineModel = D('RetailBillOnline');
$BillDisModel = D('RetailBillDiscount');
$SellModel = D('RetailSell');
$printSet = $param['printSet'];
$bill_order = $param['bill_order'];
$bill_online = $param['bill_online'];
$bill_discount = $param['bill_discount'];
$goodsList = $param['goodsList'];
$pay_list = $param['pay_list'];
$address_info = $param['address_info'];
if ($bill_online['order_genre'] == 4) {
$advance_order = $param['advance_order'];
}
$content[] = "-- #" . $bill_order['print_number'] . " --";
if ($printSet[$PrinterModel::TITLE]) {
$content = array('title' => "{$printSet[$PrinterModel::TITLE]}");//title为自定义头部信息,如果没有显示门店名
}
$content[] = "{$param['store_name']}";//title为自定义头部信息,如果没有显示门店名
$content[] = '';
$content[] = '';
$content[] = "订单时间:" . date('Y-m-d H:i:s', $bill_order[$BillModel::ADD_TIME]);
if ($bill_online['order_genre'] == 4) {
if ($advance_order['advance_type'] == 2) {
$content[] = "定金支付时间:" . $advance_order['earnest_pay_time'];
if ($advance_order['state'] == 2) {
$content[] = "尾款支付时间:" . $advance_order['tail_time'];
}
} else {
$content[] = "支付时间:" . $advance_order['tail_time'];
}
}
$content[] = "订单号:{$bill_order[$BillModel::ORDER_NO]}";
if (!empty($param['cashier_name'])) {
$content[] = "收 银 员:{$param['cashier_name']}";
}
if (!empty($param['guide_name'])) {
$content[] = "导 购 员:{$param['guide_name']}";
}
$content[] = '名称 数量 单价 应收';
$content[] = '------------------------------';
foreach ($goodsList as $k => $val) {
$goods_name = $val[$SellModel::GOODS_NAME] ? $val[$SellModel::GOODS_NAME] : '无码商品';
if (!empty($val['skuinfo'])) {
$sku_name = json_decode($val['skuinfo'], true)[0]['sku'];
$goods_name = $goods_name . ":" . $sku_name;
} else if (!empty($val[$SellModel::SKU_JSON_INFO])) {
$sku_name = json_decode($val[$SellModel::SKU_JSON_INFO], true)['name'];
$goods_name = $goods_name . ":" . $sku_name;
}
$content[] = "{$goods_name}";//商品名
$str = '';
if ($val[$SellModel::PRICING_METHOD] == 1 || !empty($val[$SellModel::SKU_JSON_INFO])) {
$str .= ' x' . str_pad(round($val[$SellModel::NUM], 2), '8', ' ', STR_PAD_RIGHT);
} else if ($val[$SellModel::PRICING_METHOD] == 2) {
$str .= ' x' . str_pad(round($val[$SellModel::NUM], 2) . $val['company'], '8', ' ', STR_PAD_RIGHT);
}
$str .= '' . str_pad($val['unit_price'], '8', ' ', STR_PAD_RIGHT);
$str .= '' . str_pad($val['before_money'], '8', ' ', STR_PAD_RIGHT);
$content[] = $str;
}
$content[] = '------------------------------';
$content[] = "合 计:{$bill_order['before_money']}元";
$content[] = "应 收:{$bill_order['all_money']}元";
if ($bill_online['order_genre'] == 4 && $advance_order['advance_type'] == 2) {
$content[] = "实收定金:{$advance_order['all_earnest_price']}元";
if (!empty($advance_order['all_arrive_price'])) {
$content[] = "定金抵扣金额:{$advance_order['all_arrive_price']}元";
}
if ($advance_order['state'] == 2) {
$content[] = "实收尾款:{$advance_order['tail_price']}元";
} else if ($advance_order['state'] == 1) {
$content[] = "待付尾款:{$advance_order['tail_price']}元";
}
} else {
$content[] = "实收:{$bill_order['amount_received']}元";
}
if (count($pay_list) == 1) {
if ($bill_online['order_genre'] == 4) {
if ($advance_order['advance_type'] == 2) {
$content[] = "支付方式:";
$content[] = $pay_list[0]['pay_type_name'] . ":" . $pay_list[0]['name'] . "\n";
} else {
$content[] = "支付方式:" . "{$pay_list[0]['name']}";
}
} else {
$content[] = "支付方式:" . "{$pay_list[0]['name']}";
}
} else {
$content[] = "支付方式:" . "组合支付";
if ($bill_online['order_genre'] == 4) {
//预售
foreach ($pay_list as $v) {
$content[] = "{$v['pay_type_name']}-{$v['name']}";
}
} else {
foreach ($pay_list as $v) {
$content[] = "{$v['name']}:" . "{$v['money']}";
}
}
}
if ($bill_order[$BillModel::TYPE] == 2 && !empty($bill_order[$BillModel::GIVE_CHANGE]) && floatval($bill_order[$BillModel::GIVE_CHANGE]) > 0) {
$content[] = "找零:{$bill_order[$BillModel::GIVE_CHANGE]}元";
}
if ($bill_order[$BillModel::TYPE] == 2 && !empty($bill_order[$BillModel::ZERO_STORAGE]) && floatval($bill_order[$BillModel::ZERO_STORAGE]) > 0) {
$content[] = "存零:{$bill_order[$BillModel::ZERO_STORAGE]}元";
}
if (!empty($bill_order[$BillModel::VID]) && $bill_discount['vip_less_money'] > 0) {
$content[] = "会员价已优惠:{$bill_discount[$BillDisModel::VIP_LESS_MONEY]}元";
}
if (!empty($bill_order[$BillModel::VID]) && $bill_discount['vip_dis_money'] > 0) {
$content[] = "会员 折扣:{$bill_discount[$BillDisModel::VIP_DIS_MONEY]}元";
}
if ((!empty($bill_order[$BillModel::VID]) || $bill_discount['coupon_type'] == 3) && $bill_discount['coupon_money'] > 0) {
$content[] = "优 惠 券:{$bill_discount[$BillDisModel::COUPON_MONEY]}元";
}
if ($bill_discount['pay_coupon_fee'] > 0) {
$content[] = "代 金 券:{$bill_discount['pay_coupon_fee']}元";
}
if (!empty($bill_order[$BillModel::VID])) {
if ($bill_discount['integral_dis_money'] > 0) {
if ($bill_discount['integral_marketing'] == 2) {
$content[] = "购买 积分:{$bill_discount[$BillDisModel::INTEGRAL_DIS_MONEY]}元";
} else {
$content[] = "积分 抵扣:{$bill_discount[$BillDisModel::INTEGRAL_DIS_MONEY]}元";
}
}
if (!empty($bill_discount['use_integral'])) {
$content[] = "扣除 积分:{$bill_discount[$BillDisModel::USE_INTEGRAL]}";
}
}
if (!empty($bill_discount[$BillDisModel::CASHIER_DIS_MONEY])) {
$content[] = "整单折扣优惠:{$bill_discount[$BillDisModel::CASHIER_DIS_MONEY]}元";
}
if (!empty($bill_discount[$BillDisModel::CASHIER_MANUAL_MONEY])) {
$content[] = "单品打折优惠:{$bill_discount[$BillDisModel::CASHIER_MANUAL_MONEY]}元";
}
if (!empty($bill_discount[$BillDisModel::SMALL_CHANGES_MONEY])) {
$content[] = "收银员抹零:{$bill_discount[$BillDisModel::SMALL_CHANGES_MONEY]}元";
}
if ($bill_order[$BillModel::TYPE] == 1 && ($bill_online[$BillOnlineModel::PICKUP_TYPE] == 2 || $bill_online[$BillOnlineModel::PICKUP_TYPE] == 3 || $bill_online[$BillOnlineModel::PICKUP_TYPE] == 5)) {
$content[] = "配 送 费:{$bill_online[$BillOnlineModel::DELIVERY_FEE]}元";
}
if (!empty($param['vip_info'])) {
//会员买单
$VipModel = D('RetailVip');
$vip_info = $param['vip_info'];
$vipName = mb_substr($vip_info['name'], 0, 1, 'utf-8');
$name = '';
for ($i = 1; $i < mb_strlen($vip_info['name'], 'utf-8'); $i++) {
$name .= '*';
}
$vipName = $vipName . $name;
$vipName = $vipName ? $vipName : $vip_info[$VipModel::NICK_NAME];
// $vipPhone = str_replace(substr($vip_info[$VipModel::MOBILE_PHONE], 3, 4), '****', $vip_info[$VipModel::MOBILE_PHONE]);
$vipPhone = $param['vip_info']['mobile_phone'];
$content[] = '------------------------------';
$content[] = "会员姓名:{$vipName}";
$content[] = "会员手机:{$vipPhone}";
$content[] = "会员余额:{$vip_info[$VipModel::BALANCE]}";
}
if ($param['bill_order']['third_vip_code']) {
$content[] = "会员名称:{$param['bill_order']['third_vip_name']}";
$content[] = "会员标识:{$param['bill_order']['third_vip_code']}";
}
$content[] = '------------------------------';
if ($bill_order['type'] == 1) {
$content[] = "配送方式:" . "{$bill_online['pickup_name']}";
}
if ($bill_order[$BillModel::TYPE] == 1 && !empty($bill_online['user_select_time'])) {
$user_select_time = date('Y-m-d H:i:s', $bill_online['user_select_time']);
switch ($bill_online[$BillOnlineModel::PICKUP_TYPE]) {
case 1:
$content[] = "自提时间:" . "{$user_select_time}";
break;
case 2:
$content[] = "配送时间:" . "{$user_select_time}";
break;
}
}
if (!empty($address_info)) {
if (!($bill_online['order_genre'] == 4 && $bill_online['pickup_type'] == 1)) {
$content[] = "地 址:" . "{$address_info['address']}";
}
$consignee = $bill_online['pickup_type'] == 1 ? "取 货 人:" : "收 货 人";
$content[] = $consignee . "{$address_info['name']}先生/女士";
$content[] = "手 机 号:" . "{$address_info['phone']}";
}
if (!empty($bill_online[$BillOnlineModel::REMARK])) {
$content[] = '------------------------------';
$content[] = "买家留言:" . "{$bill_online[$BillOnlineModel::REMARK]}";
}
$content[] = '';
$content[] = "{$printSet[$PrinterModel::FOOTER]}";//title为自定义头部信息,如果没有显示门店名
return $this->printComplexReceiptVoiceSupport($param['printSet'], $content, $bill_order['amount_received']);
}
public function refund($param)
{
$PrinterModel = D('RetailPrinter');
$BillModel = D('RetailBillOrder');
$BillDisModel = D('RetailBillDiscount');
$SellModel = D('RetailSell');
$printSet = $param['printSet'];
$order = $param['bill_order'];
$billOnline = $param['bill_online'];
$billDiscount = $param['bill_discount'];
$goodsList = $param['goodsList'];
$payList = $param['pay_list'];
if ($printSet[$PrinterModel::TITLE]) {
$content = array('title' => "{$printSet[$PrinterModel::TITLE]}");//title为自定义头部信息,如果没有显示门店名
}
$content[] = "{$param['store_name']}";//title为自定义头部信息,如果没有显示门店名
$content[] = '';
$content[] = "订单编号:{$order[$BillModel::ORDER_NO]}";
$content[] = "订单类型:退款订单";
$content[] = "申请时间:" . date('Y-m-d H:i:s', $order[$BillModel::ADD_TIME]);
if (!empty($order["finish_time"])) {
$content[] = "退款时间:" . date('Y-m-d H:i:s', $order["finish_time"]);
}
$content[] = '名称 数量 单价';
$content[] = '------------------------------';
foreach ($goodsList as $k => $val) {
$goods_name = $val[$SellModel::GOODS_NAME] ? $val[$SellModel::GOODS_NAME] : '无码商品';
if (!empty($val['skuinfo'])) {
$sku_name = json_decode($val['skuinfo'], true)[0]['sku'];
$goods_name = $goods_name . ":" . $sku_name;
} else if (!empty($val[$SellModel::SKU_JSON_INFO])) {
$sku_name = json_decode($val[$SellModel::SKU_JSON_INFO], true)['name'];
$goods_name = $goods_name . ":" . $sku_name;
}
$content[] = "{$goods_name}";//商品名
$str = '';
if ($val[$SellModel::PRICING_METHOD] == 1 || !empty($val[$SellModel::SKU_JSON_INFO])) {
$str .= ' X' . str_pad(round($val[$SellModel::NUM], 2), '6', ' ', STR_PAD_RIGHT);
} else if ($val[$SellModel::PRICING_METHOD] == 2) {
$str .= ' X' . str_pad(round($val[$SellModel::NUM], 2) . $val['company'], '6', ' ', STR_PAD_RIGHT);
}
$str .= '' . str_pad($val[$SellModel::PRICE], '9', ' ', STR_PAD_RIGHT);
$content[] = $str;
}
$content[] = '------------------------------';
if (count($payList) == 1) {
$content[] = "退款方式:" . "{$payList[0]['name']}";
} else {
$content[] = "退款方式:" . "组合退款";
foreach ($payList as $v) {
$content[] = "{$v['name']}:" . "{$v['money']}";
}
}
if ($billDiscount['integral_dis_money'] > 0) {
if ($billDiscount['integral_marketing'] == 2) {
$content[] = "购买积分金额:{$billDiscount[$BillDisModel::INTEGRAL_DIS_MONEY]}元";
} else {
$content[] = "积分抵扣:{$billDiscount[$BillDisModel::INTEGRAL_DIS_MONEY]}元";
}
}
if ($billDiscount['use_integral'] > 0) {
$content[] = "退还积分:" . $billDiscount[$BillDisModel::USE_INTEGRAL];
}
$print_time = date('Y/m/d H:i:s', time());
if ($billOnline['pickup_type'] == 2) {
$content[] = "退还配送费:" . "{$billOnline["delivery_fee"]}元";
}
if ($billDiscount['pay_coupon_fee'] > 0) {
$content[] = "代金券:" . "{$billDiscount['pay_coupon_fee']}元";
}
$content[] = "实退金额:" . "{$order[$BillModel::ALL_MONEY]}元";
$content[] = "打印时间:" . "{$print_time}";
$content[] = '';
$content[] = "{$printSet[$PrinterModel::FOOTER]}";//title为自定义头部信息,如果没有显示门店名
return $this->printComplexReceiptVoiceSupport($param['printSet'], $content, -$order[$BillModel::ALL_MONEY]);
}
public function exchange($param)
{
$PrinterModel = D('RetailPrinter');
$BillModel = D('RetailBillOrder');
$SellModel = D('RetailSell');
$printSet = $param['printSet'];
$order = $param['bill_order'];
$goodsList = $param['goodsList'];
if ($printSet[$PrinterModel::TITLE]) {
$content = array('title' => "{$printSet[$PrinterModel::TITLE]}");//title为自定义头部信息,如果没有显示门店名
}
$content[] = "{$param['store_name']}";//title为自定义头部信息,如果没有显示门店名
$content[] = '';
$content[] = "订单编号:{$order[$BillModel::ORDER_NO]}";
$content[] = "订单类型:换货订单";
$content[] = "申请时间:" . date('Y-m-d H:i:s', $order[$BillModel::ADD_TIME]);
if (!empty($order["finish_time"])) {
$content[] = "换货时间:" . date('Y-m-d H:i:s', $order["finish_time"]);
}
$content[] = '名称 数量 单价';
$content[] = '------------------------------';
foreach ($goodsList as $k => $val) {
$goods_name = $val[$SellModel::GOODS_NAME] ? $val[$SellModel::GOODS_NAME] : '无码商品';
if (!empty($val['skuinfo'])) {
$sku_name = json_decode($val['skuinfo'], true)[0]['sku'];
$goods_name = $goods_name . ":" . $sku_name;
} else if (!empty($val[$SellModel::SKU_JSON_INFO])) {
$sku_name = json_decode($val[$SellModel::SKU_JSON_INFO], true)['name'];
$goods_name = $goods_name . ":" . $sku_name;
}
$content[] = "{$goods_name}";//商品名
$str = '';
if ($val[$SellModel::PRICING_METHOD] == 1 || !empty($val[$SellModel::SKU_JSON_INFO])) {
$str .= ' X' . str_pad(round($val[$SellModel::NUM], 2), '6', ' ', STR_PAD_RIGHT);
} else if ($val[$SellModel::PRICING_METHOD] == 2) {
$str .= ' X' . str_pad(round($val[$SellModel::NUM], 2) . $val['company'], '6', ' ', STR_PAD_RIGHT);
}
$str .= '' . str_pad($val[$SellModel::PRICE], '9', ' ', STR_PAD_RIGHT);
$content[] = $str;
}
$content[] = '------------------------------';
$content[] = '姓名:' . $param['addressInfo']['name'];
$content[] = '手机号:' . $param['addressInfo']['phone'];
if ($order['pickup_type'] != 1) {
$content[] = '收货地址:' . $param['addressInfo']['address'];
}
$print_time = date('Y/m/d H:i:s', time());
$content[] = "打印时间:" . "{$print_time}";
$content[] = '';
$content[] = "{$printSet[$PrinterModel::FOOTER]}";//title为自定义头部信息,如果没有显示门店名
return $this->printComplexReceiptVoiceSupport($param['printSet'], $content);
}
/**
* 打印订单
* @param array $settings 配置信息
* @param array $content 打印内容
* @return array
*/
private function printComplexReceiptVoiceSupport($settings, $content, $money = 0)
{
$request = new PrintRequest();
$request->user = $settings['api_name'];
$request->userKey = $settings['api_key'];
$request->generateSign();
//*必填*:打印机编号
$request->sn = $settings['printer_sn'];
//*必填*:打印内容,不能超过12K
$request->content = implode('
', $content);
//打印份数,默认为1
$request->copies = 1;
//声音播放模式,0 为取消订单模式,1 为静音模式,2 为来单播放模式,3为有用户申请退单了。默认为 2 来单播放模式
$request->voice = $money > 0 ? 2 : 3;
//打印模式:
//值为 0 或不指定则会检查打印机是否在线,如果不在线 则不生成打印订单,直接返回设备不在线状态码;如果在线则生成打印订单,并返回打印订单号。
//值为 1不检查打印机是否在线,直接生成打印订单,并返回打印订单号。如果打印机不在线,订单将缓存在打印队列中,打印机正常在线时会自动打印。
$request->mode = 1;
//支付方式:
//取值范围41~55:
//支付宝 41、微信 42、云支付 43、银联刷卡 44、银联支付 45、会员卡消费 46、会员卡充值 47、翼支付 48、成功收款 49、嘉联支付 50、壹钱包 51、京东支付 52、快钱支付 53、威支付 54、享钱支付 55
//仅用于支持金额播报的芯烨云打印机。
$request->payType = 42;
//支付与否:
//取值范围59~61:
//退款 59 到账 60 消费 61。
//仅用于支持金额播报的芯烨云打印机。
$request->payMode = $money > 0 ? 60 : 59;
//支付金额:
//最多允许保留2位小数。
//仅用于支持金额播报的芯烨云打印机。
$request->money = abs($money);
$result = $this->service->xpYunPrint($request);
return [
'code' => $result->content->code,
'msg' => $result->content->msg,
'data' => $result->content->data,
];
}
}