From 99173639c9cb4e40bfff7bb5aa8c32218f31dd0f Mon Sep 17 00:00:00 2001 From: wushumin Date: Thu, 16 Apr 2026 13:04:15 +0800 Subject: [PATCH] add --- alter_invoices.php | 43 ++++++++++++ alter_orders_express.php | 36 ++++++++++ app/api/controller/InvoiceController.php | 86 ++++++++++++++++++++++++ app/api/controller/OrderController.php | 4 +- app/common/model/Invoice.php | 10 +++ app/common/service/OrderFlowService.php | 20 ++++-- config/route.php | 5 ++ 7 files changed, 199 insertions(+), 5 deletions(-) create mode 100644 alter_invoices.php create mode 100644 alter_orders_express.php create mode 100644 app/api/controller/InvoiceController.php create mode 100644 app/common/model/Invoice.php diff --git a/alter_invoices.php b/alter_invoices.php new file mode 100644 index 0000000..cfb0098 --- /dev/null +++ b/alter_invoices.php @@ -0,0 +1,43 @@ +load(); +} + +$capsule = new Capsule; +$capsule->addConnection([ + 'driver' => 'mysql', + 'host' => $_ENV['DB_HOST'] ?? '127.0.0.1', + 'port' => $_ENV['DB_PORT'] ?? '3306', + 'database' => $_ENV['DB_DATABASE'] ?? '', + 'username' => $_ENV['DB_USERNAME'] ?? '', + 'password' => $_ENV['DB_PASSWORD'] ?? '', + 'charset' => 'utf8mb4', + 'collation' => 'utf8mb4_unicode_ci', + 'prefix' => '', +]); +$capsule->setAsGlobal(); +$capsule->bootEloquent(); + +if (!Capsule::schema()->hasTable('invoices')) { + Capsule::schema()->create('invoices', function ($table) { + $table->id(); + $table->unsignedBigInteger('order_id')->unique(); + $table->unsignedBigInteger('user_id')->index(); + $table->string('type', 20)->default('company')->comment('company, personal'); + $table->string('title', 100); + $table->string('tax_no', 50)->nullable(); + $table->string('email', 100); + $table->decimal('amount', 10, 2)->default(0.00); + $table->string('status', 20)->default('pending')->comment('pending, issued, rejected'); + $table->string('file_url', 255)->nullable(); + $table->timestamps(); + }); + echo "Table 'invoices' created successfully.\n"; +} else { + echo "Table 'invoices' already exists.\n"; +} \ No newline at end of file diff --git a/alter_orders_express.php b/alter_orders_express.php new file mode 100644 index 0000000..7287785 --- /dev/null +++ b/alter_orders_express.php @@ -0,0 +1,36 @@ +load(); +} + +$capsule = new Capsule; +$capsule->addConnection([ + 'driver' => 'mysql', + 'host' => $_ENV['DB_HOST'] ?? '127.0.0.1', + 'port' => $_ENV['DB_PORT'] ?? '3306', + 'database' => $_ENV['DB_DATABASE'] ?? '', + 'username' => $_ENV['DB_USERNAME'] ?? '', + 'password' => $_ENV['DB_PASSWORD'] ?? '', + 'charset' => 'utf8mb4', + 'collation' => 'utf8mb4_unicode_ci', + 'prefix' => '', +]); +$capsule->setAsGlobal(); +$capsule->bootEloquent(); + +if (Capsule::schema()->hasTable('orders')) { + if (!Capsule::schema()->hasColumn('orders', 'express_modify_count')) { + Capsule::schema()->table('orders', function ($table) { + $table->tinyInteger('express_modify_count')->default(0)->after('express_no'); + }); + echo "Added 'express_modify_count' to orders.\n"; + } else { + echo "'express_modify_count' already exists.\n"; + } +} diff --git a/app/api/controller/InvoiceController.php b/app/api/controller/InvoiceController.php new file mode 100644 index 0000000..6098311 --- /dev/null +++ b/app/api/controller/InvoiceController.php @@ -0,0 +1,86 @@ +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') + ]); + } +} \ No newline at end of file diff --git a/app/api/controller/OrderController.php b/app/api/controller/OrderController.php index 5b6571f..f94c8b5 100644 --- a/app/api/controller/OrderController.php +++ b/app/api/controller/OrderController.php @@ -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); } diff --git a/app/common/model/Invoice.php b/app/common/model/Invoice.php new file mode 100644 index 0000000..559500f --- /dev/null +++ b/app/common/model/Invoice.php @@ -0,0 +1,10 @@ +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; } diff --git a/config/route.php b/config/route.php index a043d09..43b73d4 100644 --- a/config/route.php +++ b/config/route.php @@ -30,6 +30,11 @@ Route::group('/api', function () { Route::get('/detail', [app\api\controller\ReportController::class, 'detail']); }); + Route::group('/invoice', function () { + Route::post('/apply', [app\api\controller\InvoiceController::class, 'apply']); + Route::get('/detail', [app\api\controller\InvoiceController::class, 'detail']); + }); + Route::group('/user', function () { Route::get('/stat', [app\api\controller\UserController::class, 'stat']); Route::post('/update_info', [app\api\controller\UserController::class, 'updateInfo']);