// +----------------------------------------------------------------------
namespace crmeb\services\printer\storage;
use app\services\activity\table\TableQrcodeServices;
use crmeb\basic\BasePrinter;
use crmeb\services\printer\AccessToken;
use app\services\activity\collage\UserCollageCodeServices;
/**
* Class YiLianYun
* @package crmeb\services\printer\storage
*/
class YiLianYun extends BasePrinter
{
/**
* 初始化
* @param array $config
* @return mixed|void
*/
protected function initialize(array $config)
{
}
/**
* 开始打印
* @return bool|mixed|string
* @throws \Exception
*/
public function startPrinter()
{
if (!$this->printerContent) {
return $this->setError('Missing print');
}
$time = time();
try {
$request = $this->accessToken->postRequest($this->accessToken->getApiUrl('print/index'), [
'client_id' => $this->accessToken->clientId,
'access_token' => $this->accessToken->getAccessToken(),
'machine_code' => $this->accessToken->machineCode,
'content' => $this->printerContent,
'origin_id' => 'crmeb' . $time,
'sign' => strtolower(md5($this->accessToken->clientId . $time . $this->accessToken->apiKey)),
'id' => $this->accessToken->createUuid(),
'timestamp' => $time
]);
} catch (\Exception $e) {
return $this->setError($e->getMessage());
}
$this->printerContent = null;
if ($request === false) {
return $this->setError('request was aborted');
}
$request = is_string($request) ? json_decode($request, true) : $request;
if (isset($request['error']) && $request['error'] != 0) {
return $this->setError(isset($request['error_description']) ? $request['error_description'] : 'Accesstoken has expired');
}
return $request;
}
/**
* 设置打印内容
* @param array $config
* @return YiLianYun
*/
public function setPrinterContent(array $config): self
{
$timeYmd = date('Y-m-d', time());
$timeHis = date('H:i:s', time());
$goodsStr = '
商品名称 | 数量 | 单价 | 金额 |
';
$product = $config['product'];
foreach ($product as $item) {
$goodsStr .= '';
if ($item['is_gift']) {
$unit_price = $price = 0;
} else {
if (isset($item['sum_price'])) {
$unit_price = $item['sum_price'];
$price = bcmul((string)$item['cart_num'], (string)$unit_price, 2);
} else {
$unit_price = $item['truePrice'];
$price = bcadd((string)$item['vip_truePrice'], (string)bcmul((string)$item['cart_num'], (string)$unit_price, 4), 2);
}
}
$goodsStr .= "{$item['productInfo']['store_name']} | {$item['productInfo']['attrInfo']['suk']} | {$item['cart_num']} | {$unit_price} | {$price} | ";
$goodsStr .= '
';
unset($price, $unit_price);
}
$goodsStr .= '
';
$orderInfo = $config['orderInfo'];
$tableInfo = isset($config['tableInfo'])&&!empty($config['tableInfo'])?$config['tableInfo']:[];
$name = $config['name'];
/** @var TableQrcodeServices $qrcodeService */
$qrcodeService = app()->make(TableQrcodeServices::class);
$discountPrice = (float)bcsub((string)bcadd((string)$orderInfo['total_price'], $orderInfo['pay_postage'], 2), (string)bcadd((string)$orderInfo['deduction_price'], $orderInfo['pay_price'], 2), 2);
if(!empty($tableInfo)){
$Info = $qrcodeService->getQrcodeyInfo((int)$tableInfo['qrcode_id'], ['category']);
$this->printerContent = <<{$this->accessToken->times}
** {$name} **
----------------
订单编号:{$orderInfo['order_id']}\r
桌码流水:{$tableInfo['serial_number']}\r
桌码分类: {$Info['category']['name']} \r
桌码编号: {$Info['table_number']} \r
日 期: {$timeYmd} \r
时 间: {$timeHis}\r
姓 名: {$orderInfo['real_name']}\r
电 话: {$orderInfo['user_phone']}\r
地 址: {$orderInfo['user_address']}\r
赠送积分: {$orderInfo['gain_integral']}\r
订单备注:{$orderInfo['mark']}\r
*************商品***************\r
{$goodsStr}
********************************\r
合计:¥{$orderInfo['total_price']},优惠: ¥{$discountPrice}
邮费:¥{$orderInfo['pay_postage']},抵扣:¥{$orderInfo['deduction_price']}
实际支付:¥{$orderInfo['pay_price']}
** 完 **
CONTENT;
}else{
$this->printerContent = <<{$this->accessToken->times}
** {$name} **
----------------
订单编号:{$orderInfo['order_id']}\r
日 期: {$timeYmd} \r
时 间: {$timeHis}\r
姓 名: {$orderInfo['real_name']}\r
电 话: {$orderInfo['user_phone']}\r
地 址: {$orderInfo['user_address']}\r
赠送积分: {$orderInfo['gain_integral']}\r
订单备注:{$orderInfo['mark']}\r
*************商品***************\r
{$goodsStr}
********************************\r
合计:¥{$orderInfo['total_price']},优惠: ¥{$discountPrice}
邮费:¥{$orderInfo['pay_postage']},抵扣:¥{$orderInfo['deduction_price']}
实际支付:¥{$orderInfo['pay_price']}
** 完 **
CONTENT;
}
return $this;
}
/**
* 设置桌码打印内容
* @param array $config
* @return YiLianYun
*/
public function setPrinterTableContent(array $config): self
{
$timeYmd = date('Y-m-d', time());
$timeHis = date('H:i:s', time());
$goodsStr = '商品名称 | 数量 | 金额 | 小计 |
';
$product = $config['product'];
$sumPrice = 0;
foreach ($product as $item) {
$goodsStr .= '';
$price = bcadd((string)$item['vip_truePrice'], (string)bcmul((string)$item['cart_num'], (string)$item['truePrice'], 4), 2);
$goodsStr .= "{$item['productInfo']['store_name']}|{$item['productInfo']['attrInfo']['suk']} | {$item['cart_num']} | {$item['truePrice']} | {$price} | ";
$goodsStr .= '
';
$sumPrice = bcadd((string)$sumPrice, (string)$price, 2);
unset($price);
}
$goodsStr .= '
';
$tableInfo = $config['tableInfo'];
$name = $config['name'];
/** @var TableQrcodeServices $qrcodeService */
$qrcodeService = app()->make(TableQrcodeServices::class);
$Info = $qrcodeService->getQrcodeyInfo((int)$tableInfo['qrcode_id'], ['category']);
$this->printerContent = << ** {$name} **
----------------
桌码流水:{$tableInfo['serial_number']}\r
桌码分类: {$Info['category']['name']} \r
桌码编号: {$Info['table_number']} \r
日 期: {$timeYmd}\r
时 间: {$timeHis}\r
*************商品***************\r
{$goodsStr}
********************************\r
商品金额:¥{$sumPrice}
** 完 **
CONTENT;
return $this;
}
/**
* 积分商城打印内容
* @param array $config
* @return YiLianYun
*/
public function setIntegralPrinterContent(array $config): self
{
$timeYmd = date('Y-m-d', time());
$timeHis = date('H:i:s', time());
$goodsStr = '商品名称 | 数量 | 单价 | 总积分 |
';
$goodsStr .= '';
$goodsStr .= "{$config['orderInfo']['store_name']} | {$config['orderInfo']['total_num']} | {$config['orderInfo']['price']} | {$config['orderInfo']['total_price']} | ";
$goodsStr .= '
';
$goodsStr .= '
';
$orderInfo = $config['orderInfo'];
$name = $config['name'];
$this->printerContent = << ** {$name} **
----------------
订单编号:{$orderInfo['order_id']}\r
日 期: {$timeYmd} \r
时 间: {$timeHis}\r
姓 名: {$orderInfo['real_name']}\r
电 话: {$orderInfo['user_phone']}\r
地 址: {$orderInfo['user_address']}\r
订单备注:{$orderInfo['mark']}\r
*************商品***************\r
{$goodsStr}
********************************\r
合计:{$orderInfo['total_price']}积分
** 完 **
CONTENT;
return $this;
}
public function setdrinkPrintContent(array $config): self
{
$timeYmd = date('Y-m-d', time());
$timeHis = date('H:i:s', time());
$orderInfo = $config['drinkdata'];
$this->printerContent = << ** {$config['name']} **
----------------
取 酒 码:{$orderInfo['code']}\r
桌 台 号: {$orderInfo['table_id'] } \r
酒品名称: {$orderInfo['name']}\r
酒水数量: {$orderInfo['num']}\r
到期时间: {$orderInfo['time']}\r
日 期: {$timeYmd}\r
时 间: {$timeHis}\r
** 完 **
CONTENT;
return $this;
}
}