feat: 前端分離+功能api化

1. 新增資產管理CRUD、googleOAuth登入
2. 更改原記帳controller
This commit is contained in:
2026-06-25 14:19:07 +08:00
parent 7bb931c97b
commit 85972f950c
70 changed files with 4754 additions and 1520 deletions

View File

@@ -14,6 +14,7 @@ return new class extends Migration
Schema::create('expenses', function (Blueprint $table) {
$table->id();
$table->foreignId('user_id')->constrained()->onDelete('cascade');
$table->unsignedBigInteger('account_id')->nullable()->after('user_id');
$table->foreignId('category_id')->nullable()->constrained()->onDelete('set null');
$table->decimal('amount', 10, 2);
$table->string('note')->nullable();

View File

@@ -0,0 +1,29 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::table('users', function (Blueprint $table) {
$table->string('google_id')->nullable()->unique()->after('id');
$table->string('avatar')->nullable()->after('google_id');
});
}
/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::table('users', function (Blueprint $table) {
//
});
}
};

View File

@@ -0,0 +1,33 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::create('personal_access_tokens', function (Blueprint $table) {
$table->id();
$table->morphs('tokenable');
$table->text('name');
$table->string('token', 64)->unique();
$table->text('abilities')->nullable();
$table->timestamp('last_used_at')->nullable();
$table->timestamp('expires_at')->nullable()->index();
$table->timestamps();
});
}
/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::dropIfExists('personal_access_tokens');
}
};

View File

@@ -0,0 +1,35 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::create('accounts', function (Blueprint $table) {
$table->id();
$table->foreignId('user_id')->constrained()->cascadeOnDelete();
$table->string('name');
$table->string('type', 20);
$table->string('currency', 10)->default('TWD');
$table->decimal('balance', 15, 2)->default(0);
$table->string('note')->nullable();
$table->integer('billing_day')->nullable();
$table->integer('payment_day')->nullable();
$table->timestamps();
});
}
/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::dropIfExists('accounts');
}
};

View File

@@ -0,0 +1,33 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::create('investment_holdings', function (Blueprint $table) {
$table->id();
$table->foreignId('account_id')->constrained()->cascadeOnDelete();
$table->enum('type', ['stock', 'fund']);
$table->string('symbol');
$table->string('name');
$table->decimal('shares', 10, 2)->default(0);
$table->decimal('avg_cost', 10, 2)->default(0);
$table->timestamps();
});
}
/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::dropIfExists('investment_holdings');
}
};

View File

@@ -0,0 +1,37 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::create('investment_transactions', function (Blueprint $table) {
$table->id();
$table->foreignId('holding_id')->constrained('investment_holdings')->cascadeOnDelete();
$table->enum('action', ['buy', 'sell']);
$table->decimal('shares', 10, 2)->nullable();
$table->decimal('price', 10, 2)->nullable();
$table->decimal('amount', 15, 2)->nullable();
$table->decimal('nav', 10, 4)->nullable();
$table->decimal('fee', 10, 2)->default(0);
$table->decimal('tax', 10, 2)->default(0);
$table->date('traded_at');
$table->string('note')->nullable();
$table->timestamps();
});
}
/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::dropIfExists('investment_transactions');
}
};

View File

@@ -12,14 +12,14 @@ return new class extends Migration
public function up(): void
{
Schema::table('expenses', function (Blueprint $table) {
$table->foreign('account_id')->references('id')->on('accounts')->nullOnDelete();
});
}
public function down(): void
{
Schema::table('expenses', function (Blueprint $table) {
$table->dropColumn('external_id');
$table->dropForeign(['account_id']);
});
}
};

View File

@@ -0,0 +1,39 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::create('securities', function (Blueprint $table) {
$table->id();
$table->string('code', 20);
$table->enum('type', ['stock', 'etf', 'fund']);
$table->string('source', 20); // twse, tpex, cnyes
$table->string('name'); // 中文名稱/簡稱
$table->decimal('latest_price', 15, 4)->nullable(); // 最新報價/淨值
$table->timestamp('latest_price_at')->nullable(); // 最新報價/淨值時間
$table->decimal('change_amount', 15, 4)->nullable(); // 漲跌
$table->decimal('change_rate', 8, 4)->nullable(); // 漲跌幅(%)
$table->jsonb('meta')->nullable(); // 基金專屬額外資訊(category、riskLevel、isinCode...)
$table->timestamps();
$table->unique(['code', 'type']);
});
}
public function down(): void
{
Schema::dropIfExists('securities');
}
};

View File

@@ -0,0 +1,36 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::create('security_price_histories', function (Blueprint $table) {
$table->id();
$table->foreignId('security_id')
->constrained('securities')
->cascadeOnDelete();
$table->date('price_date'); // 該筆報價/淨值所屬日期
$table->decimal('price', 15, 4); // 收盤價/淨值
$table->decimal('change_amount', 15, 4)->nullable(); // 漲跌
$table->decimal('change_rate', 8, 4)->nullable(); // 漲跌幅(%)
$table->timestamps();
$table->unique(['security_id', 'price_date']);
});
}
public function down(): void
{
Schema::dropIfExists('security_price_histories');
}
};

View File

@@ -0,0 +1,30 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::table('users', function (Blueprint $table) {
$table->text('google_access_token')->nullable();
$table->text('google_refresh_token')->nullable();
$table->timestamp('google_token_expires_at')->nullable();
});
}
/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::table('users', function (Blueprint $table) {
//
});
}
};

View File

@@ -12,15 +12,17 @@ return new class extends Migration
public function up(): void
{
Schema::table('expenses', function (Blueprint $table) {
$table->foreignId('subcategory_id')->nullable()->after('category_id')
->constrained('categories')->onDelete('set null');
// 🟢 新增分攤控制欄位
$table->boolean('is_amortized')->default(false); // 是否開啟分攤
$table->date('period_start')->nullable(); // 費用歸屬開始日期
$table->date('period_end')->nullable(); // 費用歸屬結束日期
});
}
public function down(): void
{
Schema::table('expenses', function (Blueprint $table) {
$table->dropConstrainedForeignId('subcategory_id');
$table->dropColumn(['is_amortized', 'period_start', 'period_end']);
});
}
};

View File

@@ -0,0 +1,31 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*/
public function up()
{
Schema::create('account_rules', function (Blueprint $table) {
$table->id();
$table->foreignId('user_id')->constrained()->onDelete('cascade');
$table->string('keyword'); // 🔍 關鍵字,例如: 'cathaybk' 或 '國泰' 或 '永豐'
$table->string('match_type'); // 💡 類型,例如: 'email_from' (比對寄件者) 或 'subject' (比對主旨)
$table->foreignId('account_id')->constrained('accounts')->onDelete('cascade'); // 🎯 要掛勾的資產帳戶ID
$table->timestamps();
});
}
/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::dropIfExists('account_rules');
}
};

View File

@@ -0,0 +1,188 @@
<?php
namespace Database\Seeders;
use Illuminate\Database\Console\Seeds\WithoutModelEvents;
use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\DB;
class AccountSeeder extends Seeder
{
/**
* Run the database seeds.
*/
public function run(): void
{
DB::table('accounts')->insert([
[
'id' => 1,
'user_id' => 1,
'name' => '凱基銀行',
'type' => 'bank',
'currency' => 'TWD',
'balance' => 123028.00,
'note' => null,
'billing_day' => null,
'payment_day' => null,
'created_at' => now(),
'updated_at' => now()
],
[
'id' => 2,
'user_id' => 1,
'name' => '國泰證券',
'type' => 'stock',
'currency' => 'TWD',
'balance' => 0.00,
'note' => null,
'billing_day' => null,
'payment_day' => null,
'created_at' => now(),
'updated_at' => now()
],
[
'id' => 3,
'user_id' => 1,
'name' => '國泰銀行',
'type' => 'bank',
'currency' => 'TWD',
'balance' => 19067.00,
'note' => null,
'billing_day' => null,
'payment_day' => null,
'created_at' => now(),
'updated_at' => now()
],
[
'id' => 4,
'user_id' => 1,
'name' => '永豐銀行',
'type' => 'cash',
'currency' => 'TWD',
'balance' => 14138.00,
'note' => null,
'billing_day' => null,
'payment_day' => null,
'created_at' => now(),
'updated_at' => now()
],
[
'id' => 5,
'user_id' => 1,
'name' => '永豐證券',
'type' => 'stock',
'currency' => 'TWD',
'balance' => 0.00,
'note' => null,
'billing_day' => null,
'payment_day' => null,
'created_at' => now(),
'updated_at' => now()
],
[
'id' => 11,
'user_id' => 1,
'name' => '國泰信用卡',
'type' => 'credit_card',
'currency' => 'TWD',
'balance' => 0.00,
'note' => null,
'billing_day' => 17,
'payment_day' => 2,
'created_at' => now(),
'updated_at' => now()
],
[
'id' => 12,
'user_id' => 1,
'name' => '麥當勞點點卡',
'type' => 'wallet',
'currency' => 'TWD',
'balance' => 299.00,
'note' => null,
'billing_day' => null,
'payment_day' => null,
'created_at' => now(),
'updated_at' => now()
],
[
'id' => 13,
'user_id' => 1,
'name' => '國泰基金',
'type' => 'fund',
'currency' => 'TWD',
'balance' => 0.00,
'note' => null,
'billing_day' => null,
'payment_day' => null,
'created_at' => now(),
'updated_at' => now()
],
[
'id' => 14,
'user_id' => 1,
'name' => '手機悠遊卡',
'type' => 'wallet',
'currency' => 'TWD',
'balance' => 1.00,
'note' => null,
'billing_day' => null,
'payment_day' => null,
'created_at' => now(),
'updated_at' => now()
],
[
'id' => 15,
'user_id' => 1,
'name' => '玉山金融卡(悠遊卡)',
'type' => 'wallet',
'currency' => 'TWD',
'balance' => 222.00,
'note' => null,
'billing_day' => null,
'payment_day' => null,
'created_at' => now(),
'updated_at' => now()
],
[
'id' => 16,
'user_id' => 1,
'name' => '凱基信用卡(悠遊卡)',
'type' => 'wallet',
'currency' => 'TWD',
'balance' => 154.00,
'note' => null,
'billing_day' => null,
'payment_day' => null,
'created_at' => now(),
'updated_at' => now()
],
[
'id' => 17,
'user_id' => 1,
'name' => '東華學生證(悠遊卡)',
'type' => 'wallet',
'currency' => 'TWD',
'balance' => 85.00,
'note' => null,
'billing_day' => null,
'payment_day' => null,
'created_at' => now(),
'updated_at' => now()
],
[
'id' => 18,
'user_id' => 1,
'name' => '安聯基金',
'type' => 'fund',
'currency' => 'TWD',
'balance' => 0.00,
'note' => null,
'billing_day' => null,
'payment_day' => null,
'created_at' => now(),
'updated_at' => now()
],
]);
}
}

View File

@@ -15,11 +15,19 @@ class DatabaseSeeder extends Seeder
*/
public function run(): void
{
// User::factory(10)->create();
\App\Models\User::updateOrCreate(
['email' => 'henry194557@gmail.com'], // 換成你的 Google 登入 email
[
'name' => 'Henry',
'google_id' => '', // 可以先隨便填
'password' => bcrypt('password'),
]
);
User::factory()->create([
'name' => 'Test User',
'email' => 'test@example.com',
$this->call([
AccountSeeder::class,
InvestmentHoldingSeeder::class,
InvestmentTransactionSeeder::class,
]);
}
}

View File

@@ -0,0 +1,52 @@
<?php
namespace Database\Seeders;
use Illuminate\Database\Console\Seeds\WithoutModelEvents;
use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\DB;
class InvestmentHoldingSeeder extends Seeder
{
/**
* Run the database seeds.
*/
public function run(): void
{
DB::table('investment_holdings')->insert([
[
'id' => 1,
'account_id' => 2,
'type' => 'stock',
'symbol' => '0050',
'name' => '元大台灣50',
'shares' => 304.00,
'avg_cost' => 76.27,
'created_at' => now(),
'updated_at' => now()
],
[
'id' => 2,
'account_id' => 13,
'type' => 'fund',
'symbol' => '10350113',
'name' => '國泰台灣高股息台幣月配',
'shares' => 229.70,
'avg_cost' => 43.53,
'created_at' => now(),
'updated_at' => now()
],
[
'id' => 3,
'account_id' => 18,
'type' => 'fund',
'symbol' => 'A36004',
'name' => '安聯台灣科技基金',
'shares' => 51.11,
'avg_cost' => 782.69,
'created_at' => now(),
'updated_at' => now()
],
]);
}
}

View File

@@ -0,0 +1,301 @@
<?php
namespace Database\Seeders;
use Illuminate\Database\Console\Seeds\WithoutModelEvents;
use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\DB;
class InvestmentTransactionSeeder extends Seeder
{
/**
* Run the database seeds.
*/
public function run(): void
{
DB::table('investment_transactions')->insert([
// 國泰基金 holding_id=2
[
'holding_id' => 2,
'action' => 'buy',
'shares' => 35.70,
'price' => 56.11,
'amount' => null,
'nav' => null,
'fee' => 0,
'tax' => 0,
'traded_at' => '2026-06-05',
'note' => null,
'created_at' => now(),
'updated_at' => now()
],
[
'holding_id' => 2,
'action' => 'buy',
'shares' => 20.10,
'price' => 49.74,
'amount' => null,
'nav' => null,
'fee' => 0,
'tax' => 0,
'traded_at' => '2026-05-20',
'note' => null,
'created_at' => now(),
'updated_at' => now()
],
[
'holding_id' => 2,
'action' => 'buy',
'shares' => 29.00,
'price' => 34.48,
'amount' => null,
'nav' => null,
'fee' => 0,
'tax' => 0,
'traded_at' => '2026-02-05',
'note' => null,
'created_at' => now(),
'updated_at' => now()
],
[
'holding_id' => 2,
'action' => 'buy',
'shares' => 27.30,
'price' => 36.59,
'amount' => null,
'nav' => null,
'fee' => 0,
'tax' => 0,
'traded_at' => '2026-02-23',
'note' => null,
'created_at' => now(),
'updated_at' => now()
],
[
'holding_id' => 2,
'action' => 'buy',
'shares' => 27.10,
'price' => 36.89,
'amount' => null,
'nav' => null,
'fee' => 0,
'tax' => 0,
'traded_at' => '2026-03-05',
'note' => null,
'created_at' => now(),
'updated_at' => now()
],
[
'holding_id' => 2,
'action' => 'buy',
'shares' => 25.10,
'price' => 39.79,
'amount' => null,
'nav' => null,
'fee' => 0,
'tax' => 0,
'traded_at' => '2026-03-20',
'note' => null,
'created_at' => now(),
'updated_at' => now()
],
[
'holding_id' => 2,
'action' => 'buy',
'shares' => 25.40,
'price' => 39.39,
'amount' => null,
'nav' => null,
'fee' => 0,
'tax' => 0,
'traded_at' => '2026-04-07',
'note' => null,
'created_at' => now(),
'updated_at' => now()
],
[
'holding_id' => 2,
'action' => 'buy',
'shares' => 21.10,
'price' => 47.36,
'amount' => null,
'nav' => null,
'fee' => 0,
'tax' => 0,
'traded_at' => '2026-04-20',
'note' => null,
'created_at' => now(),
'updated_at' => now()
],
[
'holding_id' => 2,
'action' => 'buy',
'shares' => 18.90,
'price' => 52.89,
'amount' => null,
'nav' => null,
'fee' => 0,
'tax' => 0,
'traded_at' => '2026-05-05',
'note' => null,
'created_at' => now(),
'updated_at' => now()
],
// 0050 holding_id=1
[
'holding_id' => 1,
'action' => 'buy',
'shares' => 1.00,
'price' => 70.05,
'amount' => null,
'nav' => null,
'fee' => 1,
'tax' => 0,
'traded_at' => '2026-01-09',
'note' => null,
'created_at' => now(),
'updated_at' => now()
],
[
'holding_id' => 1,
'action' => 'buy',
'shares' => 50.00,
'price' => 70.05,
'amount' => null,
'nav' => null,
'fee' => 1,
'tax' => 0,
'traded_at' => '2026-01-09',
'note' => null,
'created_at' => now(),
'updated_at' => now()
],
[
'holding_id' => 1,
'action' => 'buy',
'shares' => 14.00,
'price' => 104.49,
'amount' => null,
'nav' => null,
'fee' => 1,
'tax' => 0,
'traded_at' => '2026-06-05',
'note' => null,
'created_at' => now(),
'updated_at' => now()
],
[
'holding_id' => 1,
'action' => 'buy',
'shares' => 14.00,
'price' => 100.54,
'amount' => null,
'nav' => null,
'fee' => 1,
'tax' => 0,
'traded_at' => '2026-05-25',
'note' => null,
'created_at' => now(),
'updated_at' => now()
],
[
'holding_id' => 1,
'action' => 'buy',
'shares' => 15.00,
'price' => 94.39,
'amount' => null,
'nav' => null,
'fee' => 1,
'tax' => 0,
'traded_at' => '2026-05-05',
'note' => null,
'created_at' => now(),
'updated_at' => now()
],
[
'holding_id' => 1,
'action' => 'buy',
'shares' => 17.00,
'price' => 86.59,
'amount' => null,
'nav' => null,
'fee' => 1,
'tax' => 0,
'traded_at' => '2026-04-23',
'note' => null,
'created_at' => now(),
'updated_at' => now()
],
[
'holding_id' => 1,
'action' => 'buy',
'shares' => 33.00,
'price' => 75.14,
'amount' => null,
'nav' => null,
'fee' => 1,
'tax' => 0,
'traded_at' => '2026-04-07',
'note' => null,
'created_at' => now(),
'updated_at' => now()
],
[
'holding_id' => 1,
'action' => 'buy',
'shares' => 20.00,
'price' => 74.02,
'amount' => null,
'nav' => null,
'fee' => 1,
'tax' => 0,
'traded_at' => '2026-03-23',
'note' => null,
'created_at' => now(),
'updated_at' => now()
],
[
'holding_id' => 1,
'action' => 'buy',
'shares' => 45.00,
'price' => 77.53,
'amount' => null,
'nav' => null,
'fee' => 1,
'tax' => 0,
'traded_at' => '2026-03-05',
'note' => null,
'created_at' => now(),
'updated_at' => now()
],
[
'holding_id' => 1,
'action' => 'buy',
'shares' => 48.00,
'price' => 72.02,
'amount' => null,
'nav' => null,
'fee' => 1,
'tax' => 0,
'traded_at' => '2026-02-05',
'note' => '定期定額',
'created_at' => now(),
'updated_at' => now()
],
[
'holding_id' => 1,
'action' => 'buy',
'shares' => 47.00,
'price' => 62.54,
'amount' => null,
'nav' => null,
'fee' => 1,
'tax' => 0,
'traded_at' => '2025-12-05',
'note' => null,
'created_at' => now(),
'updated_at' => now()
],
]);
}
}