This commit is contained in:
wushumin
2026-04-16 13:04:15 +08:00
parent 11ff03d0ea
commit 99173639c9
7 changed files with 199 additions and 5 deletions

View File

@@ -0,0 +1,86 @@
<?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')
]);
}
}

View File

@@ -72,6 +72,7 @@ class OrderController
'is_fast' => (bool)$order->is_fast,
'express_company' => $order->express_company,
'express_no' => $order->express_no,
'express_modify_count' => (int)$order->express_modify_count,
'timeline' => $timeline
]);
}
@@ -123,8 +124,9 @@ class OrderController
}
try {
$isModify = ($order->status === 'wait_receive');
OrderFlowService::userShip($order, $expressCompany, $expressNo);
return jsonResponse(null, '发货信息已提交');
return jsonResponse(null, $isModify ? '物流信息修改成功' : '发货信息已提交');
} catch (\Exception $e) {
return jsonResponse(null, $e->getMessage(), 400);
}

View File

@@ -0,0 +1,10 @@
<?php
namespace app\common\model;
use Illuminate\Database\Eloquent\Model;
class Invoice extends Model
{
protected $table = 'invoices';
protected $guarded = [];
}

View File

@@ -58,16 +58,28 @@ class OrderFlowService
*/
public static function userShip(Order $order, string $expressCompany, string $expressNo)
{
if ($order->status !== 'shipping') {
throw new Exception("当前状态不允许发货");
if ($order->status !== 'shipping' && $order->status !== 'wait_receive') {
throw new Exception("当前状态不允许修改发货信息");
}
if ($order->status === 'wait_receive') {
if ($order->express_modify_count >= 1) {
throw new Exception("运单号仅允许修改1次");
}
$order->express_modify_count += 1;
$actionType = 'user_ship_modify';
$title = '修改物流信息';
} else {
$order->status = 'wait_receive'; // 等待平台收件
$actionType = 'user_ship';
$title = '物品已寄出';
}
$order->express_company = $expressCompany;
$order->express_no = $expressNo;
$order->status = 'wait_receive'; // 等待平台收件(可复用为在途)
$order->save();
self::addLog($order->id, 'user_ship', '物品已寄出', "物流公司: {$expressCompany}, 单号: {$expressNo}", 'user', $order->user_id);
self::addLog($order->id, $actionType, $title, "物流公司: {$expressCompany}, 单号: {$expressNo}", 'user', $order->user_id);
return $order;
}