86 lines
2.7 KiB
PHP
86 lines
2.7 KiB
PHP
<?php
|
|
namespace app\api\controller;
|
|
|
|
use support\Request;
|
|
use app\common\model\Order;
|
|
use app\common\model\Invoice;
|
|
use app\common\service\OrderFlowService;
|
|
|
|
class InvoiceController
|
|
{
|
|
public function apply(Request $request)
|
|
{
|
|
$userId = $request->user->id;
|
|
$orderId = (int)$request->post('order_id');
|
|
$type = trim((string)$request->post('type', 'company'));
|
|
$title = trim((string)$request->post('title', ''));
|
|
$taxNo = trim((string)$request->post('tax_no', ''));
|
|
$email = trim((string)$request->post('email', ''));
|
|
|
|
if (!$orderId || !$title || !$email) {
|
|
return jsonResponse(null, '开票信息不完整', 400);
|
|
}
|
|
|
|
if ($type === 'company' && !$taxNo) {
|
|
return jsonResponse(null, '企业发票需填写税号', 400);
|
|
}
|
|
|
|
$order = Order::where('id', $orderId)->where('user_id', $userId)->first();
|
|
if (!$order) {
|
|
return jsonResponse(null, '订单不存在', 404);
|
|
}
|
|
|
|
if ($order->status === 'wait_pay') {
|
|
return jsonResponse(null, '未支付订单不能开票', 400);
|
|
}
|
|
|
|
$invoice = Invoice::where('order_id', $orderId)->first();
|
|
if ($invoice) {
|
|
return jsonResponse(null, '该订单已申请开票', 400);
|
|
}
|
|
|
|
$invoice = Invoice::create([
|
|
'order_id' => $orderId,
|
|
'user_id' => $userId,
|
|
'type' => $type,
|
|
'title' => $title,
|
|
'tax_no' => $taxNo,
|
|
'email' => $email,
|
|
'amount' => $order->total_price,
|
|
'status' => 'pending'
|
|
]);
|
|
|
|
OrderFlowService::addLog($orderId, 'invoice_apply', '已提交开票申请', '开票金额: ¥' . $invoice->amount, 'user', $userId);
|
|
|
|
return jsonResponse(['id' => $invoice->id], '开票申请已提交');
|
|
}
|
|
|
|
public function detail(Request $request)
|
|
{
|
|
$userId = $request->user->id;
|
|
$orderId = (int)$request->get('order_id');
|
|
|
|
if (!$orderId) {
|
|
return jsonResponse(null, '参数错误', 400);
|
|
}
|
|
|
|
$invoice = Invoice::where('order_id', $orderId)->where('user_id', $userId)->first();
|
|
|
|
if (!$invoice) {
|
|
return jsonResponse(null, '未找到开票记录', 404);
|
|
}
|
|
|
|
return jsonResponse([
|
|
'id' => $invoice->id,
|
|
'order_id' => $invoice->order_id,
|
|
'type' => $invoice->type,
|
|
'title' => $invoice->title,
|
|
'tax_no' => $invoice->tax_no,
|
|
'email' => $invoice->email,
|
|
'amount' => $invoice->amount,
|
|
'status' => $invoice->status,
|
|
'file_url' => $invoice->file_url,
|
|
'created_at' => $invoice->created_at->format('Y-m-d H:i:s')
|
|
]);
|
|
}
|
|
} |