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

43
alter_invoices.php Normal file
View File

@@ -0,0 +1,43 @@
<?php
require __DIR__ . '/vendor/autoload.php';
use Illuminate\Database\Capsule\Manager as Capsule;
if (file_exists(__DIR__ . '/.env')) {
$dotenv = Dotenv\Dotenv::createUnsafeImmutable(__DIR__);
$dotenv->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";
}

36
alter_orders_express.php Normal file
View File

@@ -0,0 +1,36 @@
<?php
require __DIR__ . '/vendor/autoload.php';
use Illuminate\Database\Capsule\Manager as Capsule;
if (file_exists(__DIR__ . '/.env')) {
$dotenv = Dotenv\Dotenv::createUnsafeImmutable(__DIR__);
$dotenv->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";
}
}

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, 'is_fast' => (bool)$order->is_fast,
'express_company' => $order->express_company, 'express_company' => $order->express_company,
'express_no' => $order->express_no, 'express_no' => $order->express_no,
'express_modify_count' => (int)$order->express_modify_count,
'timeline' => $timeline 'timeline' => $timeline
]); ]);
} }
@@ -123,8 +124,9 @@ class OrderController
} }
try { try {
$isModify = ($order->status === 'wait_receive');
OrderFlowService::userShip($order, $expressCompany, $expressNo); OrderFlowService::userShip($order, $expressCompany, $expressNo);
return jsonResponse(null, '发货信息已提交'); return jsonResponse(null, $isModify ? '物流信息修改成功' : '发货信息已提交');
} catch (\Exception $e) { } catch (\Exception $e) {
return jsonResponse(null, $e->getMessage(), 400); 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) public static function userShip(Order $order, string $expressCompany, string $expressNo)
{ {
if ($order->status !== 'shipping') { if ($order->status !== 'shipping' && $order->status !== 'wait_receive') {
throw new Exception("当前状态不允许发货"); 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_company = $expressCompany;
$order->express_no = $expressNo; $order->express_no = $expressNo;
$order->status = 'wait_receive'; // 等待平台收件(可复用为在途)
$order->save(); $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; return $order;
} }

View File

@@ -30,6 +30,11 @@ Route::group('/api', function () {
Route::get('/detail', [app\api\controller\ReportController::class, 'detail']); 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::group('/user', function () {
Route::get('/stat', [app\api\controller\UserController::class, 'stat']); Route::get('/stat', [app\api\controller\UserController::class, 'stat']);
Route::post('/update_info', [app\api\controller\UserController::class, 'updateInfo']); Route::post('/update_info', [app\api\controller\UserController::class, 'updateInfo']);