parent
2907416e8d
commit
22155ec344
|
|
@ -4,6 +4,9 @@ namespace App\Admin\Actions\Store;
|
||||||
|
|
||||||
use Dcat\Admin\Show\AbstractTool;
|
use Dcat\Admin\Show\AbstractTool;
|
||||||
use App\Models\Store\Desk;
|
use App\Models\Store\Desk;
|
||||||
|
use App\Services\OrderService;
|
||||||
|
use App\Models\Order;
|
||||||
|
use App\Exceptions\BizException;
|
||||||
|
|
||||||
class ShowOrderPrint extends AbstractTool
|
class ShowOrderPrint extends AbstractTool
|
||||||
{
|
{
|
||||||
|
|
@ -13,8 +16,14 @@ class ShowOrderPrint extends AbstractTool
|
||||||
|
|
||||||
public function handle()
|
public function handle()
|
||||||
{
|
{
|
||||||
$id = $this->getKey();
|
$order = Order::findOrFail($this->getKey());
|
||||||
|
|
||||||
|
try {
|
||||||
|
(new OrderService())->print($order);
|
||||||
|
} catch (BizException $e) {
|
||||||
|
return $this->response()->error($e->getMessage());
|
||||||
|
}
|
||||||
|
|
||||||
return $this->response()->success('操作成功');
|
return $this->response()->success('操作成功');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -4,9 +4,8 @@ namespace App\Listeners;
|
||||||
|
|
||||||
use Illuminate\Contracts\Queue\ShouldQueue;
|
use Illuminate\Contracts\Queue\ShouldQueue;
|
||||||
use Illuminate\Queue\InteractsWithQueue;
|
use Illuminate\Queue\InteractsWithQueue;
|
||||||
use App\Models\Order;
|
use App\Exceptions\BizException;
|
||||||
use App\Models\Store\{Desk, DeviceRecord};
|
use App\Services\OrderService;
|
||||||
use App\Services\PrintService;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 打印订单小票
|
* 打印订单小票
|
||||||
|
|
@ -18,46 +17,10 @@ class OrderPrint
|
||||||
$order = $event->order;
|
$order = $event->order;
|
||||||
// 门店订单, 桌号信息
|
// 门店订单, 桌号信息
|
||||||
if ($order->store_id && $order->source_type == Desk::class) {
|
if ($order->store_id && $order->source_type == Desk::class) {
|
||||||
$store = $order->store;
|
try {
|
||||||
$desk = $order->source;
|
(new OrderService())->print($order);
|
||||||
|
} catch (BizException $e) {
|
||||||
// 门店设备
|
logger('订单打印失败: ' . $e->getMessage());
|
||||||
$devices = $store->devices()->where('status', 1)->get();
|
|
||||||
$templateId = data_get($store->extra, 'order_desk_print_template');
|
|
||||||
if ($devices->count() > 0 && $templateId) {
|
|
||||||
$service = PrintService::make();
|
|
||||||
$products = [];
|
|
||||||
foreach($order->products as $item) {
|
|
||||||
array_push($products, [
|
|
||||||
'name' => $item->name,
|
|
||||||
'price' => round($item->sell_price / 100, 2, PHP_ROUND_HALF_DOWN),
|
|
||||||
'amount' => $item->quantity,
|
|
||||||
'money' => round($item->total_amount / 100, 2, PHP_ROUND_HALF_DOWN),
|
|
||||||
]);
|
|
||||||
}
|
|
||||||
$data = [
|
|
||||||
'name' => $store->title,
|
|
||||||
'sn' => $order->sn,
|
|
||||||
'time' => $order->created_at->format('Y-m-d H:i:s'),
|
|
||||||
'desk' => $desk->name,
|
|
||||||
'products' => $products,
|
|
||||||
'total' => round($order->total_amount, 2, PHP_ROUND_HALF_DOWN),
|
|
||||||
'remarks' => $order->note ?: '',
|
|
||||||
];
|
|
||||||
foreach($devices as $item) {
|
|
||||||
$result = $service->template($item->device_no, $templateId, $data);
|
|
||||||
$status = data_get($result, 'code') == 0 ? DeviceRecord::STATUS_SUCCESS : DeviceRecord::STATUS_FAIL;
|
|
||||||
// 添加打印的日志记录
|
|
||||||
$item->records()->create([
|
|
||||||
'data' => $data,
|
|
||||||
'device_id' => $item->id,
|
|
||||||
'result' => $result,
|
|
||||||
'resource_id' => $order->id,
|
|
||||||
'resource_type' => $order->getMorphClass(),
|
|
||||||
'status' => $status,
|
|
||||||
'store_id' => $store->id
|
|
||||||
]);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -21,7 +21,7 @@ use App\Models\ProductSku;
|
||||||
use App\Models\ShippingAddress;
|
use App\Models\ShippingAddress;
|
||||||
use App\Models\SocialiteUser;
|
use App\Models\SocialiteUser;
|
||||||
use App\Models\{User, OrderPre, Tag};
|
use App\Models\{User, OrderPre, Tag};
|
||||||
use App\Models\Store\{Store, Desk};
|
use App\Models\Store\{Store, Desk, DeviceRecord};
|
||||||
use App\Models\UserCoupon;
|
use App\Models\UserCoupon;
|
||||||
use App\Services\Payment\WxpayService;
|
use App\Services\Payment\WxpayService;
|
||||||
use Illuminate\Database\Eloquent\Builder;
|
use Illuminate\Database\Eloquent\Builder;
|
||||||
|
|
@ -1358,4 +1358,63 @@ class OrderService
|
||||||
'status' => Order::STATUS_CANCELLED,
|
'status' => Order::STATUS_CANCELLED,
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 打印订单小票
|
||||||
|
*
|
||||||
|
* @param Order $order 订单
|
||||||
|
* @throws BizException
|
||||||
|
*/
|
||||||
|
public function print(Order $order)
|
||||||
|
{
|
||||||
|
if (!$order->store_id || $order->source_type !== Desk::class) {
|
||||||
|
throw new BizException('不是门店订单');
|
||||||
|
}
|
||||||
|
$store = $order->store;
|
||||||
|
if (!$store) {
|
||||||
|
throw new BizException('未找到门店');
|
||||||
|
}
|
||||||
|
$desk = $order->source;
|
||||||
|
if (!$desk) {
|
||||||
|
throw new BizException('未找到桌号');
|
||||||
|
}
|
||||||
|
$devices = $store->devices()->where('status', 1)->get();
|
||||||
|
|
||||||
|
$templateId = data_get($store->extra, 'order_desk_print_template');
|
||||||
|
if ($devices->count() > 0 && $templateId) {
|
||||||
|
$service = PrintService::make();
|
||||||
|
$products = [];
|
||||||
|
foreach($order->products as $item) {
|
||||||
|
array_push($products, [
|
||||||
|
'name' => $item->name,
|
||||||
|
'price' => round($item->sell_price / 100, 2, PHP_ROUND_HALF_DOWN),
|
||||||
|
'amount' => $item->quantity,
|
||||||
|
'money' => round($item->total_amount / 100, 2, PHP_ROUND_HALF_DOWN),
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
$data = [
|
||||||
|
'name' => $store->title,
|
||||||
|
'sn' => $order->sn,
|
||||||
|
'time' => $order->created_at->format('Y-m-d H:i:s'),
|
||||||
|
'desk' => $desk->name,
|
||||||
|
'products' => $products,
|
||||||
|
'total' => round($order->total_amount, 2, PHP_ROUND_HALF_DOWN),
|
||||||
|
'remarks' => $order->note ?: '',
|
||||||
|
];
|
||||||
|
foreach($devices as $item) {
|
||||||
|
$result = $service->template($item->device_no, $templateId, $data);
|
||||||
|
$status = data_get($result, 'code') == 0 ? DeviceRecord::STATUS_SUCCESS : DeviceRecord::STATUS_FAIL;
|
||||||
|
// 添加打印的日志记录
|
||||||
|
$item->records()->create([
|
||||||
|
'data' => $data,
|
||||||
|
'device_id' => $item->id,
|
||||||
|
'result' => $result,
|
||||||
|
'resource_id' => $order->id,
|
||||||
|
'resource_type' => $order->getMorphClass(),
|
||||||
|
'status' => $status,
|
||||||
|
'store_id' => $store->id
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -15,8 +15,8 @@ class PrintService
|
||||||
|
|
||||||
function __construct()
|
function __construct()
|
||||||
{
|
{
|
||||||
$this->memberCode = config('postcom.memberCode');
|
$this->memberCode = config('postcom.member_code');
|
||||||
$this->apiKey = config('postcom.apiKey');
|
$this->apiKey = config('postcom.api_key');
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -40,6 +40,7 @@ class PrintService
|
||||||
$params['templetID'] = $templetID;
|
$params['templetID'] = $templetID;
|
||||||
$params['tData'] = json_encode($data);
|
$params['tData'] = json_encode($data);
|
||||||
|
|
||||||
|
logger('postcom', $params);
|
||||||
$response = Http::asForm()->post($url, $params);
|
$response = Http::asForm()->post($url, $params);
|
||||||
if ($response->successful()) {
|
if ($response->successful()) {
|
||||||
$result = $response->json();
|
$result = $response->json();
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue