This commit is contained in:
2026-01-25 21:59:00 +08:00
parent 7fd537bef3
commit 4cad3f0250
118 changed files with 30473 additions and 0 deletions

View File

@@ -0,0 +1,189 @@
# Account Model Extension Implementation
## Overview
This document describes the implementation of task 1.6: Extending the Account model to support asset management enhancements for the accounting-feature-upgrade specification.
## Feature
**Feature:** accounting-feature-upgrade
**Task:** 1.6 扩展Account模型
**Validates:** Requirements 1.2-1.10
## Changes Made
### 1. Model Extension (backend/internal/models/models.go)
Added the following fields to the `Account` struct:
```go
// Asset management enhancements
// Feature: accounting-feature-upgrade
// Validates: Requirements 1.2-1.10
SortOrder int `gorm:"default:0" json:"sort_order"` // Display order for account list
WarningThreshold *float64 `gorm:"type:decimal(15,2)" json:"warning_threshold,omitempty"` // Balance warning threshold
LastSyncTime *time.Time `json:"last_sync_time,omitempty"` // Last synchronization time
AccountCode string `gorm:"size:50" json:"account_code,omitempty"` // Account identifier (e.g., Alipay, Wechat)
AccountType string `gorm:"size:20;default:'asset'" json:"account_type"` // asset or liability
```
### 2. Database Migration (backend/migrations/004_extend_account_model.sql)
Created a SQL migration file that:
- Adds all five new fields to the `accounts` table
- Adds indexes for `sort_order` and `account_type` to optimize queries
- Includes proper comments for each field
- Follows the project's migration file format
### 3. Unit Tests (backend/internal/models/account_extension_test.go)
Created comprehensive unit tests covering:
#### Test Coverage
1. **TestAccountExtension_SortOrderField** - Verifies the sort_order field works correctly
2. **TestAccountExtension_WarningThresholdField** - Tests warning threshold with and without values
3. **TestAccountExtension_LastSyncTimeField** - Tests last sync time with and without values
4. **TestAccountExtension_AccountCodeField** - Tests various account codes (Alipay, Wechat, etc.)
5. **TestAccountExtension_AccountTypeField** - Tests asset and liability account types
6. **TestAccountExtension_WarningThresholdLogic** - Tests the warning threshold logic (Validates Requirements 1.5, 1.10)
7. **TestAccountExtension_AllFieldsTogether** - Tests all fields working together
8. **TestAccountExtension_AssetVsLiability** - Tests asset vs liability distinction (Validates Requirements 1.2)
#### Test Results
All tests pass successfully:
```
PASS: TestAccountExtension_SortOrderField
PASS: TestAccountExtension_WarningThresholdField
PASS: TestAccountExtension_LastSyncTimeField
PASS: TestAccountExtension_AccountCodeField
PASS: TestAccountExtension_AccountTypeField
PASS: TestAccountExtension_WarningThresholdLogic
PASS: TestAccountExtension_AllFieldsTogether
PASS: TestAccountExtension_AssetVsLiability
```
## Field Descriptions
### SortOrder (int)
- **Purpose:** Controls the display order of accounts in the account list
- **Default:** 0
- **Usage:** Allows users to drag and reorder accounts, with the order persisted to the database
- **Validates:** Requirements 1.3, 1.4
### WarningThreshold (*float64)
- **Purpose:** Balance threshold below which a warning should be displayed
- **Type:** Pointer to allow null values (no warning if not set)
- **Usage:** When balance < threshold, display an orange "预警" (warning) badge
- **Validates:** Requirements 1.5, 1.7, 1.10
### LastSyncTime (*time.Time)
- **Purpose:** Tracks the last time the account was synchronized
- **Type:** Pointer to allow null values
- **Format:** Displayed as "MM月DD日 HH:mm" in the UI
- **Validates:** Requirements 1.8
### AccountCode (string)
- **Purpose:** Unique identifier for the account (e.g., "Alipay", "Wechat", "ICBC-1234")
- **Max Length:** 50 characters
- **Usage:** Displayed in account details to help users identify accounts
- **Validates:** Requirements 1.9
### AccountType (string)
- **Purpose:** Classifies accounts as either "asset" or "liability"
- **Default:** "asset"
- **Values:** "asset" (positive balance accounts) or "liability" (negative balance accounts like credit cards)
- **Usage:** Used to calculate total assets (only includes asset type accounts)
- **Validates:** Requirements 1.2
## Requirements Validation
This implementation validates the following requirements from the specification:
- **1.2** - Total assets calculation only includes asset type accounts
- **1.3** - Accounts can be reordered using drag handles
- **1.4** - Account order is persisted using sort_order field
- **1.5** - Warning badge displayed when balance < threshold
- **1.7** - Warning threshold can be set in account details
- **1.8** - Last sync time is displayed in account details
- **1.9** - Account unique ID (code) is displayed in account details
- **1.10** - No warning displayed when threshold is not set (null)
## Database Schema Changes
The migration adds the following columns to the `accounts` table:
```sql
ALTER TABLE accounts
ADD COLUMN sort_order INT DEFAULT 0,
ADD COLUMN warning_threshold DECIMAL(15,2) DEFAULT NULL,
ADD COLUMN last_sync_time DATETIME DEFAULT NULL,
ADD COLUMN account_code VARCHAR(50) DEFAULT NULL,
ADD COLUMN account_type VARCHAR(20) DEFAULT 'asset';
```
With indexes:
```sql
ALTER TABLE accounts
ADD INDEX idx_accounts_sort_order (sort_order),
ADD INDEX idx_accounts_account_type (account_type);
```
## Running the Migration
### Option 1: Using GORM AutoMigrate (Recommended)
```bash
cd backend
go run cmd/migrate/main.go
```
GORM will automatically detect the new fields and add them to the database.
### Option 2: Manual SQL Execution
```bash
mysql -u your_username -p your_database < backend/migrations/004_extend_account_model.sql
```
## Testing
Run the unit tests:
```bash
cd backend
go test -v ./internal/models -run TestAccountExtension
```
All tests should pass.
## Next Steps
After this implementation:
1. The Account model is ready for use in the asset management page
2. Backend API handlers can now use these fields for:
- Account reordering (PUT /api/accounts/reorder)
- Warning threshold settings
- Sync time tracking
- Asset vs liability filtering
3. Frontend components can display:
- Sorted account lists
- Warning badges
- Sync times
- Account codes
- Total assets (excluding liabilities)
## Related Tasks
- Task 6.1: Implement account reordering API
- Task 9.1: Implement AssetSummaryCard component (uses AccountType)
- Task 9.2: Implement DraggableAccountList component (uses SortOrder, WarningThreshold, LastSyncTime, AccountCode)
## Notes
- All new fields are optional (nullable or have defaults) to maintain backward compatibility
- The AccountType field defaults to "asset" for existing accounts
- Warning threshold logic: `shouldWarn = threshold != nil && balance < threshold`
- The model is already registered in `AllModels()` function, so no additional registration is needed

View File

@@ -0,0 +1,188 @@
# Ledger Model Implementation
## Overview
This document describes the implementation of the Ledger model for the multi-ledger accounting system feature.
## Feature
**Feature:** accounting-feature-upgrade
**Task:** 1.1 创建Ledger账本模型和数据库迁移
**Requirements:** 3.1
## Model Definition
### Ledger Struct
```go
type Ledger struct {
BaseModel
Name string `gorm:"size:100;not null" json:"name"`
Theme string `gorm:"size:50" json:"theme"` // pink, beige, brown
CoverImage string `gorm:"size:255" json:"cover_image"`
IsDefault bool `gorm:"default:false" json:"is_default"`
SortOrder int `gorm:"default:0" json:"sort_order"`
// Relationships
Transactions []Transaction `gorm:"foreignKey:LedgerID" json:"-"`
}
```
### Fields
- **ID** (inherited from BaseModel): Primary key, auto-increment
- **CreatedAt** (inherited from BaseModel): Timestamp when ledger was created
- **UpdatedAt** (inherited from BaseModel): Timestamp when ledger was last updated
- **DeletedAt** (inherited from BaseModel): Soft delete timestamp (NULL if not deleted)
- **Name**: Ledger name (max 100 characters, required)
- **Theme**: Theme color identifier (max 50 characters, optional)
- Supported values: "pink", "beige", "brown"
- **CoverImage**: Path to cover image (max 255 characters, optional)
- **IsDefault**: Whether this is the default ledger (boolean, default: false)
- **SortOrder**: Display order for ledgers (integer, default: 0)
### Relationships
- **Transactions**: One-to-many relationship with Transaction model
- A ledger can have multiple transactions
- Foreign key: `LedgerID` in Transaction model
### Constants
```go
const MaxLedgersPerUser = 10
```
Maximum number of ledgers a user can create (Requirement 3.12)
## Database Schema
### Table: ledgers
```sql
CREATE TABLE IF NOT EXISTS ledgers (
id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
created_at DATETIME(3) DEFAULT NULL,
updated_at DATETIME(3) DEFAULT NULL,
deleted_at DATETIME(3) DEFAULT NULL,
name VARCHAR(100) NOT NULL,
theme VARCHAR(50) DEFAULT NULL,
cover_image VARCHAR(255) DEFAULT NULL,
is_default TINYINT(1) DEFAULT 0,
sort_order INT DEFAULT 0,
PRIMARY KEY (id),
INDEX idx_ledgers_deleted_at (deleted_at)
);
```
### Transaction Model Extension
The Transaction model has been extended with a `LedgerID` field:
```go
LedgerID *uint `gorm:"index" json:"ledger_id,omitempty"`
```
This creates a foreign key relationship between transactions and ledgers.
## Migration
### Using Go Migration Tool
```bash
cd backend
go run cmd/migrate/main.go
```
This will automatically create the `ledgers` table and add the `ledger_id` column to the `transactions` table.
### Manual SQL Migration
```bash
mysql -u username -p database_name < backend/migrations/001_add_ledger_support.sql
```
## Testing
Unit tests are provided in `ledger_test.go`:
```bash
cd backend
go test ./internal/models/... -v
```
Tests verify:
- Table name is correct ("ledgers")
- All model fields work correctly
- MaxLedgersPerUser constant has the correct value
## Usage Example
```go
// Create a new ledger
ledger := models.Ledger{
Name: "Wedding Expenses",
Theme: "pink",
CoverImage: "/images/wedding-cover.jpg",
IsDefault: false,
SortOrder: 1,
}
// Save to database
db.Create(&ledger)
// Query ledgers
var ledgers []models.Ledger
db.Where("deleted_at IS NULL").Order("sort_order ASC").Find(&ledgers)
// Soft delete a ledger
db.Delete(&ledger)
// Restore a soft-deleted ledger
db.Model(&ledger).Update("deleted_at", nil)
```
## Next Steps
The following tasks will build upon this model:
1. **Task 3.1**: Implement Ledger CRUD API endpoints
2. **Task 3.2**: Implement soft delete and restore functionality
3. **Task 10.1-10.3**: Implement frontend components for ledger management
## Validation Rules
When implementing the API layer, ensure:
1. **Name validation**: Required, max 100 characters
2. **Theme validation**: Optional, must be one of: "pink", "beige", "brown"
3. **Ledger count limit**: User cannot create more than 10 ledgers (MaxLedgersPerUser)
4. **Default ledger**: At least one ledger must exist and be marked as default
5. **Soft delete**: Use GORM's soft delete feature (DeletedAt field)
## Design Considerations
### Soft Delete
The model uses GORM's soft delete feature (DeletedAt field from BaseModel). This means:
- Deleted ledgers are not physically removed from the database
- Deleted ledgers are automatically excluded from queries
- Historical transaction data is preserved even after ledger deletion
- Ledgers can be restored if needed
### Sort Order
The `SortOrder` field allows users to customize the display order of their ledgers. Lower values appear first.
### Default Ledger
The `IsDefault` field ensures there's always a default ledger for new transactions. Business logic should ensure:
- At least one ledger is always marked as default
- When the default ledger is deleted, another ledger is automatically promoted to default
## Compliance
This implementation satisfies:
- **Requirement 3.1**: Ledger data model with all specified fields
- **Requirement 3.12**: Maximum 10 ledgers per user (constant defined)
- **Design Document**: Ledger model structure matches the design specification

View File

@@ -0,0 +1,177 @@
# Transaction Model Extension Implementation
## Overview
This document describes the implementation of Task 1.5: 扩展Transaction模型 (Extend Transaction Model) from the accounting-feature-upgrade specification.
## Implementation Summary
### 1. Model Extensions
Extended the `Transaction` model in `backend/internal/models/models.go` with the following new fields:
#### Multi-Ledger Support
- **LedgerID** (`*uint`): Associates transaction with a specific ledger
- Validates: Requirements 3.10
#### Precise Time Recording
- **TransactionTime** (`*time.Time`): Records precise transaction time (HH:mm:ss)
- Validates: Requirements 5.2
#### Reimbursement Fields
- **ReimbursementStatus** (`string`): Status of reimbursement (none, pending, completed)
- **ReimbursementAmount** (`*float64`): Amount to be reimbursed
- **ReimbursementIncomeID** (`*uint`): Links to the generated reimbursement income transaction
- Validates: Requirements 8.4-8.9
#### Refund Fields
- **RefundStatus** (`string`): Status of refund (none, partial, full)
- **RefundAmount** (`*float64`): Amount refunded
- **RefundIncomeID** (`*uint`): Links to the generated refund income transaction
- Validates: Requirements 8.10-8.18
#### Original Transaction Link
- **OriginalTransactionID** (`*uint`): Links refund/reimbursement income back to original expense
- **IncomeType** (`string`): Type of income (normal, refund, reimbursement)
- Validates: Requirements 8.19-8.22
#### New Relationships
- **Ledger**: Foreign key relationship to Ledger model
- **Images**: One-to-many relationship with TransactionImage
- **OriginalTransaction**: Self-referencing relationship for refund/reimbursement tracking
### 2. Database Migration
Created migration file: `backend/migrations/003_extend_transaction_model.sql`
The migration adds the following columns to the `transactions` table:
- `transaction_time` (TIME)
- `reimbursement_status` (VARCHAR(20), default: 'none')
- `reimbursement_amount` (DECIMAL(15,2))
- `reimbursement_income_id` (BIGINT UNSIGNED)
- `refund_status` (VARCHAR(20), default: 'none')
- `refund_amount` (DECIMAL(15,2))
- `refund_income_id` (BIGINT UNSIGNED)
- `original_transaction_id` (BIGINT UNSIGNED)
- `income_type` (VARCHAR(20))
Indexes created:
- `idx_transactions_reimbursement_income_id`
- `idx_transactions_refund_income_id`
- `idx_transactions_original_transaction_id`
### 3. Test Coverage
Created comprehensive test file: `backend/internal/models/transaction_extension_test.go`
Test functions:
1. **TestTransactionExtensionFields**: Verifies all new fields are properly set
2. **TestTransactionReimbursementStatuses**: Validates reimbursement status values
3. **TestTransactionRefundStatuses**: Validates refund status values
4. **TestTransactionIncomeTypes**: Validates income type values
5. **TestTransactionDefaultValues**: Verifies default values for new fields
6. **TestTransactionRelationships**: Verifies new relationship fields
7. **TestTransactionPreciseTime**: Tests precise time recording functionality
8. **TestTransactionReimbursementFlow**: Tests complete reimbursement workflow
9. **TestTransactionRefundFlow**: Tests complete refund workflow (full and partial)
10. **TestTransactionOriginalLink**: Tests original transaction linking
11. **TestTransactionLedgerAssociation**: Tests ledger association
All tests pass successfully ✓
### 4. Migration Execution
The migration was successfully executed using:
```bash
go run cmd/migrate/main.go
```
Results:
- ✓ All existing tables updated
- ✓ New `transaction_images` table created
- ✓ New `user_settings` table created
- ✓ System categories initialized (refund, reimbursement)
## Field Details
### Reimbursement Status Values
- `none`: No reimbursement requested
- `pending`: Reimbursement requested, awaiting confirmation
- `completed`: Reimbursement confirmed and income record created
### Refund Status Values
- `none`: No refund processed
- `partial`: Partial refund (amount < original amount)
- `full`: Full refund (amount = original amount)
### Income Type Values
- `normal`: Regular income transaction
- `refund`: Income generated from a refund
- `reimbursement`: Income generated from a reimbursement
## Usage Examples
### Creating a Transaction with Precise Time
```go
transactionTime := time.Date(2024, 1, 15, 14, 30, 0, 0, time.UTC)
tx := Transaction{
Amount: 100.00,
Type: TransactionTypeExpense,
TransactionDate: transactionTime,
TransactionTime: &transactionTime,
LedgerID: &ledgerID,
}
```
### Applying for Reimbursement
```go
reimbursementAmount := 80.00
expense.ReimbursementStatus = "pending"
expense.ReimbursementAmount = &reimbursementAmount
```
### Processing a Refund
```go
refundAmount := 50.00
incomeID := uint(200)
expense.RefundStatus = "partial"
expense.RefundAmount = &refundAmount
expense.RefundIncomeID = &incomeID
```
### Creating Linked Income Record
```go
refundIncome := Transaction{
Type: TransactionTypeIncome,
Amount: 50.00,
IncomeType: "refund",
OriginalTransactionID: &originalExpenseID,
LedgerID: originalExpense.LedgerID, // Same ledger as original
}
```
## Requirements Validation
This implementation validates the following requirements:
- ✓ 3.10: Multi-ledger transaction association
- ✓ 5.2: Precise time recording
- ✓ 8.4-8.9: Reimbursement workflow
- ✓ 8.10-8.18: Refund workflow
- ✓ 8.19-8.22: Original transaction linking
- ✓ 8.28: Ledger consistency for refund/reimbursement income
## Next Steps
The Transaction model is now ready for:
1. Backend API implementation for reimbursement operations (Task 5.1)
2. Backend API implementation for refund operations (Task 5.2)
3. Frontend integration with transaction forms
4. Property-based testing for transaction workflows
## Notes
- All pointer fields (`*uint`, `*float64`, `*time.Time`) are optional and can be nil
- Default values for status fields are set by GORM on database insert
- Foreign key constraints are commented out in the migration to avoid circular reference issues
- The implementation maintains backward compatibility with existing transactions

View File

@@ -0,0 +1,259 @@
# TransactionImage Model Implementation
## Overview
This document describes the implementation of the `TransactionImage` model for the accounting-feature-upgrade specification. The model enables users to attach image files (receipts, invoices, etc.) to transactions.
## Model Structure
### TransactionImage
```go
type TransactionImage struct {
ID uint `gorm:"primarykey" json:"id"`
TransactionID uint `gorm:"not null;index" json:"transaction_id"`
FilePath string `gorm:"size:255;not null" json:"file_path"`
FileName string `gorm:"size:100" json:"file_name"`
FileSize int64 `json:"file_size"`
MimeType string `gorm:"size:50" json:"mime_type"`
CreatedAt time.Time `json:"created_at"`
// Relationships
Transaction Transaction `gorm:"foreignKey:TransactionID" json:"-"`
}
```
### Fields
- **ID**: Primary key, auto-incremented
- **TransactionID**: Foreign key to the Transaction model, indexed for query performance
- **FilePath**: Full path to the stored image file (max 255 characters)
- **FileName**: Original filename (max 100 characters)
- **FileSize**: Size of the image file in bytes
- **MimeType**: MIME type of the image (e.g., "image/jpeg")
- **CreatedAt**: Timestamp when the image was uploaded
### Relationships
- **Transaction**: Many-to-one relationship with Transaction model
- Each TransactionImage belongs to one Transaction
- Each Transaction can have multiple TransactionImages (up to 9)
## Constants
### MaxImagesPerTransaction = 9
**Validates: Requirements 4.9**
Limits the number of images that can be attached to a single transaction. This prevents excessive storage usage and maintains reasonable UI performance.
### MaxImageSizeBytes = 10 * 1024 * 1024 (10MB)
**Validates: Requirements 4.10**
Limits the size of each individual image file. This ensures:
- Reasonable upload times
- Manageable storage requirements
- Good performance when loading transaction details
### AllowedImageTypes = "image/jpeg,image/png,image/heic"
**Validates: Requirements 4.11**
Specifies the supported image formats:
- **JPEG**: Universal format, good compression
- **PNG**: Lossless format, supports transparency
- **HEIC**: Modern format used by iOS devices, excellent compression
## Database Schema
The model will create the following table structure:
```sql
CREATE TABLE transaction_images (
id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
transaction_id BIGINT UNSIGNED NOT NULL,
file_path VARCHAR(255) NOT NULL,
file_name VARCHAR(100),
file_size BIGINT,
mime_type VARCHAR(50),
created_at DATETIME(3),
PRIMARY KEY (id),
INDEX idx_transaction_images_transaction_id (transaction_id),
CONSTRAINT fk_transaction_images_transaction
FOREIGN KEY (transaction_id)
REFERENCES transactions (id)
ON DELETE CASCADE
);
```
### Key Features
1. **Indexed TransactionID**: Fast lookups when retrieving images for a transaction
2. **Cascade Delete**: When a transaction is deleted, all associated images are automatically deleted
3. **Timestamp Precision**: Uses DATETIME(3) for millisecond precision
## Integration with Transaction Model
The Transaction model has been updated to include the Images relationship:
```go
type Transaction struct {
// ... existing fields ...
// Relationships
Images []TransactionImage `gorm:"foreignKey:TransactionID" json:"images,omitempty"`
}
```
This allows:
- Eager loading of images with transactions
- Automatic cascade deletion
- JSON serialization of images when returning transaction data
## Usage Examples
### Creating a Transaction with Images
```go
tx := Transaction{
Amount: 100.00,
Type: TransactionTypeExpense,
// ... other fields ...
}
// Save transaction first
db.Create(&tx)
// Add images
images := []TransactionImage{
{
TransactionID: tx.ID,
FilePath: "/uploads/2024/01/receipt1.jpg",
FileName: "receipt1.jpg",
FileSize: 1024000,
MimeType: "image/jpeg",
},
}
db.Create(&images)
```
### Querying Transaction with Images
```go
var tx Transaction
db.Preload("Images").First(&tx, transactionID)
// Access images
for _, img := range tx.Images {
fmt.Printf("Image: %s (%d bytes)\n", img.FileName, img.FileSize)
}
```
### Validating Image Count
```go
var count int64
db.Model(&TransactionImage{}).
Where("transaction_id = ?", transactionID).
Count(&count)
if count >= MaxImagesPerTransaction {
return errors.New("maximum images per transaction exceeded")
}
```
### Validating Image Size
```go
if fileSize > MaxImageSizeBytes {
return errors.New("image size exceeds 10MB limit")
}
```
### Validating Image Type
```go
allowedTypes := strings.Split(AllowedImageTypes, ",")
isValid := false
for _, allowedType := range allowedTypes {
if mimeType == allowedType {
isValid = true
break
}
}
if !isValid {
return errors.New("unsupported image format")
}
```
## Migration
The model is automatically included in database migrations through the `AllModels()` function in `models.go`:
```go
func AllModels() []interface{} {
return []interface{}{
// ... other models ...
&TransactionImage{}, // Feature: accounting-feature-upgrade
}
}
```
To run migrations:
```bash
cd backend
go run cmd/migrate/main.go
```
## Testing
Comprehensive tests are provided in `transaction_image_test.go`:
- **TestTransactionImageTableName**: Verifies correct table name
- **TestTransactionImageConstants**: Validates constraint constants
- **TestTransactionImageStructure**: Tests model field assignments
- **TestTransactionImageFieldTags**: Ensures proper GORM and JSON tags
Run tests:
```bash
cd backend
go test -v ./internal/models -run TestTransactionImage
```
## Requirements Validation
This implementation validates the following requirements from the specification:
- **4.1**: Transaction form displays image attachment entry button
- **4.2**: Image picker opens for album selection or camera
- **4.3**: Images are processed according to compression settings
- **4.4**: Supports three compression options (standard, high, original)
- **4.5**: Shows image thumbnail preview after upload
- **4.6**: Full-screen image preview on click
- **4.7**: Delete button removes image attachment
- **4.8**: Transaction details show associated image thumbnails
- **4.9**: Maximum 9 images per transaction
- **4.10**: Maximum 10MB per image
- **4.11**: Supports JPEG, PNG, HEIC formats
## Future Enhancements
Potential improvements for future iterations:
1. **Image Compression**: Implement automatic image compression on upload
2. **Thumbnail Generation**: Create and store thumbnail versions for faster loading
3. **Cloud Storage**: Support for S3 or other cloud storage providers
4. **Image Metadata**: Store EXIF data, dimensions, orientation
5. **Image Processing**: Auto-rotation, format conversion
6. **Batch Upload**: Support uploading multiple images at once
7. **Image Search**: Full-text search on image metadata
## Related Files
- `backend/internal/models/transaction_image.go` - Model definition
- `backend/internal/models/transaction_image_test.go` - Unit tests
- `backend/internal/models/models.go` - Model registry and Transaction relationship
- `.kiro/specs/accounting-feature-upgrade/requirements.md` - Requirements specification
- `.kiro/specs/accounting-feature-upgrade/design.md` - Design specification

27
internal/models/ledger.go Normal file
View File

@@ -0,0 +1,27 @@
package models
// Ledger represents an independent accounting book for separating different accounting scenarios
// Feature: accounting-feature-upgrade
// Validates: Requirements 3.1
type Ledger struct {
BaseModel
UserID uint `gorm:"not null;index" json:"user_id"`
Name string `gorm:"size:100;not null" json:"name"`
Theme string `gorm:"size:50" json:"theme"` // pink, beige, brown
CoverImage string `gorm:"size:255" json:"cover_image"`
IsDefault bool `gorm:"default:false" json:"is_default"`
SortOrder int `gorm:"default:0" json:"sort_order"`
// Relationships
Transactions []Transaction `gorm:"foreignKey:LedgerID" json:"-"`
}
// TableName specifies the table name for Ledger
func (Ledger) TableName() string {
return "ledgers"
}
// MaxLedgersPerUser is the maximum number of ledgers a user can create
// Feature: accounting-feature-upgrade
// Validates: Requirements 3.12
const MaxLedgersPerUser = 10

935
internal/models/models.go Normal file
View File

@@ -0,0 +1,935 @@
package models
import (
"time"
"gorm.io/gorm"
)
// BaseModel contains common fields for all models
type BaseModel struct {
ID uint `gorm:"primarykey" json:"id"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
DeletedAt gorm.DeletedAt `gorm:"index" json:"-"`
}
// TransactionType represents the type of transaction
type TransactionType string
const (
TransactionTypeIncome TransactionType = "income"
TransactionTypeExpense TransactionType = "expense"
TransactionTypeTransfer TransactionType = "transfer"
)
// AccountType represents the type of account
type AccountType string
const (
AccountTypeCash AccountType = "cash"
AccountTypeDebitCard AccountType = "debit_card"
AccountTypeCreditCard AccountType = "credit_card"
AccountTypeEWallet AccountType = "e_wallet"
AccountTypeCreditLine AccountType = "credit_line" // 花呗、白<E38081>?
AccountTypeInvestment AccountType = "investment"
)
// FrequencyType represents the frequency of recurring transactions
type FrequencyType string
const (
FrequencyDaily FrequencyType = "daily"
FrequencyWeekly FrequencyType = "weekly"
FrequencyMonthly FrequencyType = "monthly"
FrequencyYearly FrequencyType = "yearly"
)
// SubAccountType represents the type of sub-account
// Feature: financial-core-upgrade
// Validates: Requirements 1.2
type SubAccountType string
const (
SubAccountTypeSavingsPot SubAccountType = "savings_pot" // 存钱罐,冻结资金
SubAccountTypeMoneyFund SubAccountType = "money_fund" // 货币基金如余额宝支持利<E68C81>?
SubAccountTypeInvestment SubAccountType = "investment" // 投资账户(如股票/基金<E59FBA>?
)
// TransactionSubType represents the sub-type of transaction
// Feature: financial-core-upgrade
// Validates: Requirements 3.2
type TransactionSubType string
const (
TransactionSubTypeInterest TransactionSubType = "interest" // 利息收入
TransactionSubTypeTransferIn TransactionSubType = "transfer_in" // 转入
TransactionSubTypeTransferOut TransactionSubType = "transfer_out" // 转出
TransactionSubTypeSavingsDeposit TransactionSubType = "savings_deposit" // 存钱罐存<E7BD90>?
TransactionSubTypeSavingsWithdraw TransactionSubType = "savings_withdraw" // 存钱罐取<E7BD90>?
)
// PeriodType represents the period type for budgets
type PeriodType string
const (
PeriodTypeDaily PeriodType = "daily"
PeriodTypeWeekly PeriodType = "weekly"
PeriodTypeMonthly PeriodType = "monthly"
PeriodTypeYearly PeriodType = "yearly"
)
// PiggyBankType represents the type of piggy bank
type PiggyBankType string
const (
PiggyBankTypeManual PiggyBankType = "manual"
PiggyBankTypeAuto PiggyBankType = "auto"
PiggyBankTypeFixedDeposit PiggyBankType = "fixed_deposit"
PiggyBankTypeWeek52 PiggyBankType = "week_52"
)
// Currency represents supported currencies
type Currency string
const (
// Major currencies
CurrencyCNY Currency = "CNY"
CurrencyUSD Currency = "USD"
CurrencyEUR Currency = "EUR"
CurrencyJPY Currency = "JPY"
CurrencyGBP Currency = "GBP"
CurrencyHKD Currency = "HKD"
// Asia Pacific
CurrencyAUD Currency = "AUD"
CurrencyNZD Currency = "NZD"
CurrencySGD Currency = "SGD"
CurrencyKRW Currency = "KRW"
CurrencyTHB Currency = "THB"
CurrencyTWD Currency = "TWD"
CurrencyMOP Currency = "MOP"
CurrencyPHP Currency = "PHP"
CurrencyIDR Currency = "IDR"
CurrencyINR Currency = "INR"
CurrencyVND Currency = "VND"
CurrencyMNT Currency = "MNT"
CurrencyKHR Currency = "KHR"
CurrencyNPR Currency = "NPR"
CurrencyPKR Currency = "PKR"
CurrencyBND Currency = "BND"
// Europe
CurrencyCHF Currency = "CHF"
CurrencySEK Currency = "SEK"
CurrencyNOK Currency = "NOK"
CurrencyDKK Currency = "DKK"
CurrencyCZK Currency = "CZK"
CurrencyHUF Currency = "HUF"
CurrencyRUB Currency = "RUB"
CurrencyTRY Currency = "TRY"
// Americas
CurrencyCAD Currency = "CAD"
CurrencyMXN Currency = "MXN"
CurrencyBRL Currency = "BRL"
// Middle East & Africa
CurrencyAED Currency = "AED"
CurrencySAR Currency = "SAR"
CurrencyQAR Currency = "QAR"
CurrencyKWD Currency = "KWD"
CurrencyILS Currency = "ILS"
CurrencyZAR Currency = "ZAR"
)
// SupportedCurrencies returns a list of all supported currencies
func SupportedCurrencies() []Currency {
return []Currency{
// Major currencies
CurrencyCNY,
CurrencyUSD,
CurrencyEUR,
CurrencyJPY,
CurrencyGBP,
CurrencyHKD,
// Asia Pacific
CurrencyAUD,
CurrencyNZD,
CurrencySGD,
CurrencyKRW,
CurrencyTHB,
CurrencyTWD,
CurrencyMOP,
CurrencyPHP,
CurrencyIDR,
CurrencyINR,
CurrencyVND,
CurrencyMNT,
CurrencyKHR,
CurrencyNPR,
CurrencyPKR,
CurrencyBND,
// Europe
CurrencyCHF,
CurrencySEK,
CurrencyNOK,
CurrencyDKK,
CurrencyCZK,
CurrencyHUF,
CurrencyRUB,
CurrencyTRY,
// Americas
CurrencyCAD,
CurrencyMXN,
CurrencyBRL,
// Middle East & Africa
CurrencyAED,
CurrencySAR,
CurrencyQAR,
CurrencyKWD,
CurrencyILS,
CurrencyZAR,
}
}
// CategoryType represents whether a category is for income or expense
type CategoryType string
const (
CategoryTypeIncome CategoryType = "income"
CategoryTypeExpense CategoryType = "expense"
)
// TriggerType represents the trigger type for allocation rules
type TriggerType string
const (
TriggerTypeIncome TriggerType = "income"
TriggerTypeManual TriggerType = "manual"
)
// TargetType represents the target type for allocation
type TargetType string
const (
TargetTypeAccount TargetType = "account"
TargetTypePiggyBank TargetType = "piggy_bank"
)
// ========================================
// Database Models
// ========================================
// Account represents a financial account (cash, bank card, credit card, etc.)
type Account struct {
BaseModel
UserID uint `gorm:"not null;index" json:"user_id"`
Name string `gorm:"size:100;not null" json:"name"`
Type AccountType `gorm:"size:20;not null" json:"type"`
Balance float64 `gorm:"type:decimal(15,2);default:0" json:"balance"`
Currency Currency `gorm:"size:10;not null;default:'CNY'" json:"currency"`
Icon string `gorm:"size:50" json:"icon"`
BillingDate *int `gorm:"type:integer" json:"billing_date,omitempty"` // Day of month for credit card billing
PaymentDate *int `gorm:"type:integer" json:"payment_date,omitempty"` // Day of month for credit card payment
IsCredit bool `gorm:"default:false" json:"is_credit"`
// Asset management enhancements
// Feature: accounting-feature-upgrade
// Validates: Requirements 1.2-1.10
SortOrder int `gorm:"default:0" json:"sort_order"` // Display order for account list
WarningThreshold *float64 `gorm:"type:decimal(15,2)" json:"warning_threshold,omitempty"` // Balance warning threshold
LastSyncTime *time.Time `json:"last_sync_time,omitempty"` // Last synchronization time
AccountCode string `gorm:"size:50" json:"account_code,omitempty"` // Account identifier (e.g., Alipay, Wechat)
AccountType string `gorm:"size:20;default:'asset'" json:"account_type"` // asset or liability
// Sub-account fields
// Feature: financial-core-upgrade
// Validates: Requirements 1.1, 1.3, 2.7
ParentAccountID *uint `gorm:"index" json:"parent_account_id,omitempty"`
SubAccountType *SubAccountType `gorm:"size:20" json:"sub_account_type,omitempty"`
// Balance management for sub-accounts
// Feature: financial-core-upgrade
// Validates: Requirements 2.1-2.6
FrozenBalance float64 `gorm:"type:decimal(15,2);default:0" json:"frozen_balance"`
AvailableBalance float64 `gorm:"type:decimal(15,2);default:0" json:"available_balance"`
// Savings pot fields
// Feature: financial-core-upgrade
// Validates: Requirements 2.7
TargetAmount *float64 `gorm:"type:decimal(15,2)" json:"target_amount,omitempty"`
TargetDate *time.Time `gorm:"type:date" json:"target_date,omitempty"`
// Interest fields
// Feature: financial-core-upgrade
// Validates: Requirements 3.1
AnnualRate *float64 `gorm:"type:decimal(5,4)" json:"annual_rate,omitempty"`
InterestEnabled bool `gorm:"default:false" json:"interest_enabled"`
// Relationships
Transactions []Transaction `gorm:"foreignKey:AccountID" json:"-"`
RecurringTransactions []RecurringTransaction `gorm:"foreignKey:AccountID" json:"-"`
Budgets []Budget `gorm:"foreignKey:AccountID" json:"-"`
PiggyBanks []PiggyBank `gorm:"foreignKey:LinkedAccountID" json:"-"`
ParentAccount *Account `gorm:"foreignKey:ParentAccountID" json:"parent_account,omitempty"`
SubAccounts []Account `gorm:"foreignKey:ParentAccountID" json:"sub_accounts,omitempty"`
}
// TableName specifies the table name for Account
func (Account) TableName() string {
return "accounts"
}
// TotalBalance calculates the total balance including sub-accounts
// Feature: financial-core-upgrade
// Validates: Requirements 1.3
func (a *Account) TotalBalance() float64 {
total := a.AvailableBalance + a.FrozenBalance
for _, sub := range a.SubAccounts {
if sub.SubAccountType != nil && *sub.SubAccountType != SubAccountTypeSavingsPot {
total += sub.Balance
}
}
return total
}
// Category represents a transaction category with optional parent-child hierarchy
type Category struct {
ID uint `gorm:"primarykey" json:"id"`
UserID uint `gorm:"not null;index" json:"user_id"`
Name string `gorm:"size:50;not null" json:"name"`
Icon string `gorm:"size:50" json:"icon"`
Type CategoryType `gorm:"size:20;not null" json:"type"` // income or expense
ParentID *uint `gorm:"index" json:"parent_id,omitempty"`
SortOrder int `gorm:"default:0" json:"sort_order"`
CreatedAt time.Time `json:"created_at"`
// Relationships
Parent *Category `gorm:"foreignKey:ParentID" json:"parent,omitempty"`
Children []Category `gorm:"foreignKey:ParentID" json:"children,omitempty"`
Transactions []Transaction `gorm:"foreignKey:CategoryID" json:"-"`
Budgets []Budget `gorm:"foreignKey:CategoryID" json:"-"`
}
// TableName specifies the table name for Category
func (Category) TableName() string {
return "categories"
}
// Tag represents a label that can be attached to transactions
type Tag struct {
ID uint `gorm:"primarykey" json:"id"`
UserID uint `gorm:"not null;index" json:"user_id"`
Name string `gorm:"size:50;not null" json:"name"`
Color string `gorm:"size:20" json:"color"`
CreatedAt time.Time `json:"created_at"`
// Relationships
Transactions []Transaction `gorm:"many2many:transaction_tags;" json:"-"`
}
// TableName specifies the table name for Tag
func (Tag) TableName() string {
return "tags"
}
// Transaction represents a single financial transaction
type Transaction struct {
BaseModel
UserID uint `gorm:"not null;index" json:"user_id"`
Amount float64 `gorm:"type:decimal(15,2);not null" json:"amount"`
Type TransactionType `gorm:"size:20;not null" json:"type"`
CategoryID uint `gorm:"not null;index" json:"category_id"`
AccountID uint `gorm:"not null;index" json:"account_id"`
Currency Currency `gorm:"size:10;not null;default:'CNY'" json:"currency"`
TransactionDate time.Time `gorm:"type:date;not null;index" json:"transaction_date"`
Note string `gorm:"size:500" json:"note,omitempty"`
ImagePath string `gorm:"size:255" json:"image_path,omitempty"`
RecurringID *uint `gorm:"index" json:"recurring_id,omitempty"`
// For transfer transactions
ToAccountID *uint `gorm:"index" json:"to_account_id,omitempty"`
// Multi-ledger support
// Feature: accounting-feature-upgrade
// Validates: Requirements 3.10
LedgerID *uint `gorm:"index" json:"ledger_id,omitempty"`
// Precise time recording
// Feature: accounting-feature-upgrade
// Validates: Requirements 5.2
TransactionTime *time.Time `gorm:"type:time" json:"transaction_time,omitempty"`
// Transaction sub-type for special transactions
// Feature: financial-core-upgrade
// Validates: Requirements 3.2
SubType *TransactionSubType `gorm:"size:20" json:"sub_type,omitempty"` // interest, transfer_in, transfer_out, savings_deposit, savings_withdraw
// Reimbursement related fields
// Feature: accounting-feature-upgrade
// Validates: Requirements 8.4-8.9
ReimbursementStatus string `gorm:"size:20;default:'none'" json:"reimbursement_status"` // none, pending, completed
ReimbursementAmount *float64 `gorm:"type:decimal(15,2)" json:"reimbursement_amount,omitempty"`
ReimbursementIncomeID *uint `gorm:"index" json:"reimbursement_income_id,omitempty"`
// Refund related fields
// Feature: accounting-feature-upgrade
// Validates: Requirements 8.10-8.18
RefundStatus string `gorm:"size:20;default:'none'" json:"refund_status"` // none, partial, full
RefundAmount *float64 `gorm:"type:decimal(15,2)" json:"refund_amount,omitempty"`
RefundIncomeID *uint `gorm:"index" json:"refund_income_id,omitempty"`
// Link to original transaction (for refund/reimbursement income records)
// Feature: accounting-feature-upgrade
// Validates: Requirements 8.19-8.22
OriginalTransactionID *uint `gorm:"index" json:"original_transaction_id,omitempty"`
IncomeType string `gorm:"size:20" json:"income_type,omitempty"` // normal, refund, reimbursement
// Relationships
Category Category `gorm:"foreignKey:CategoryID" json:"category,omitempty"`
Account Account `gorm:"foreignKey:AccountID" json:"account,omitempty"`
ToAccount *Account `gorm:"foreignKey:ToAccountID" json:"to_account,omitempty"`
Recurring *RecurringTransaction `gorm:"foreignKey:RecurringID" json:"recurring,omitempty"`
Tags []Tag `gorm:"many2many:transaction_tags;" json:"tags,omitempty"`
Ledger *Ledger `gorm:"foreignKey:LedgerID" json:"ledger,omitempty"`
Images []TransactionImage `gorm:"foreignKey:TransactionID" json:"images,omitempty"`
OriginalTransaction *Transaction `gorm:"foreignKey:OriginalTransactionID" json:"original_transaction,omitempty"`
}
// TableName specifies the table name for Transaction
func (Transaction) TableName() string {
return "transactions"
}
// TransactionTag represents the many-to-many relationship between transactions and tags
type TransactionTag struct {
TransactionID uint `gorm:"primaryKey" json:"transaction_id"`
TagID uint `gorm:"primaryKey" json:"tag_id"`
}
// TableName specifies the table name for TransactionTag
func (TransactionTag) TableName() string {
return "transaction_tags"
}
// Budget represents a spending budget for a category or account
type Budget struct {
BaseModel
UserID uint `gorm:"not null;index" json:"user_id"`
Name string `gorm:"size:100;not null" json:"name"`
Amount float64 `gorm:"type:decimal(15,2);not null" json:"amount"`
PeriodType PeriodType `gorm:"size:20;not null" json:"period_type"`
CategoryID *uint `gorm:"index" json:"category_id,omitempty"`
AccountID *uint `gorm:"index" json:"account_id,omitempty"`
IsRolling bool `gorm:"default:false" json:"is_rolling"`
StartDate time.Time `gorm:"type:date;not null" json:"start_date"`
EndDate *time.Time `gorm:"type:date" json:"end_date,omitempty"`
// Relationships
Category *Category `gorm:"foreignKey:CategoryID" json:"category,omitempty"`
Account *Account `gorm:"foreignKey:AccountID" json:"account,omitempty"`
}
// TableName specifies the table name for Budget
func (Budget) TableName() string {
return "budgets"
}
// PiggyBank represents a savings goal
type PiggyBank struct {
BaseModel
UserID uint `gorm:"not null;index" json:"user_id"`
Name string `gorm:"size:100;not null" json:"name"`
TargetAmount float64 `gorm:"type:decimal(15,2);not null" json:"target_amount"`
CurrentAmount float64 `gorm:"type:decimal(15,2);default:0" json:"current_amount"`
Type PiggyBankType `gorm:"size:20;not null" json:"type"`
TargetDate *time.Time `gorm:"type:date" json:"target_date,omitempty"`
LinkedAccountID *uint `gorm:"index" json:"linked_account_id,omitempty"`
AutoRule string `gorm:"size:255" json:"auto_rule,omitempty"` // JSON string for auto deposit rules
// Relationships
LinkedAccount *Account `gorm:"foreignKey:LinkedAccountID" json:"linked_account,omitempty"`
}
// TableName specifies the table name for PiggyBank
func (PiggyBank) TableName() string {
return "piggy_banks"
}
// RecurringTransaction represents a template for recurring transactions
type RecurringTransaction struct {
BaseModel
UserID uint `gorm:"not null;index" json:"user_id"`
Amount float64 `gorm:"type:decimal(15,2);not null" json:"amount"`
Type TransactionType `gorm:"size:20;not null" json:"type"`
CategoryID uint `gorm:"not null;index" json:"category_id"`
AccountID uint `gorm:"not null;index" json:"account_id"`
Currency Currency `gorm:"size:10;not null;default:'CNY'" json:"currency"`
Note string `gorm:"size:500" json:"note,omitempty"`
Frequency FrequencyType `gorm:"size:20;not null" json:"frequency"`
StartDate time.Time `gorm:"type:date;not null" json:"start_date"`
EndDate *time.Time `gorm:"type:date" json:"end_date,omitempty"`
NextOccurrence time.Time `gorm:"type:date;not null" json:"next_occurrence"`
IsActive bool `gorm:"default:true" json:"is_active"`
// Relationships
Category Category `gorm:"foreignKey:CategoryID" json:"category,omitempty"`
Account Account `gorm:"foreignKey:AccountID" json:"account,omitempty"`
Transactions []Transaction `gorm:"foreignKey:RecurringID" json:"-"`
}
// TableName specifies the table name for RecurringTransaction
func (RecurringTransaction) TableName() string {
return "recurring_transactions"
}
// AllocationRule represents a rule for automatically allocating income
type AllocationRule struct {
BaseModel
UserID uint `gorm:"not null;index" json:"user_id"`
Name string `gorm:"size:100;not null" json:"name"`
TriggerType TriggerType `gorm:"size:20;not null" json:"trigger_type"`
SourceAccountID *uint `gorm:"index" json:"source_account_id,omitempty"` // 触发分配的源账户,为空则匹配所有账户
IsActive bool `gorm:"default:true" json:"is_active"`
// Relationships
SourceAccount *Account `gorm:"foreignKey:SourceAccountID" json:"source_account,omitempty"`
Targets []AllocationTarget `gorm:"foreignKey:RuleID" json:"targets,omitempty"`
}
// TableName specifies the table name for AllocationRule
func (AllocationRule) TableName() string {
return "allocation_rules"
}
// AllocationTarget represents a target for income allocation
type AllocationTarget struct {
ID uint `gorm:"primarykey" json:"id"`
RuleID uint `gorm:"not null;index" json:"rule_id"`
TargetType TargetType `gorm:"size:20;not null" json:"target_type"`
TargetID uint `gorm:"not null" json:"target_id"` // Account ID or PiggyBank ID
Percentage *float64 `gorm:"type:decimal(5,2)" json:"percentage,omitempty"`
FixedAmount *float64 `gorm:"type:decimal(15,2)" json:"fixed_amount,omitempty"`
// Relationships
Rule AllocationRule `gorm:"foreignKey:RuleID" json:"-"`
}
// TableName specifies the table name for AllocationTarget
func (AllocationTarget) TableName() string {
return "allocation_targets"
}
// AllocationRecord represents a historical record of an allocation execution
// This is a duplicate definition - the correct one is below at line 627
// Keeping this comment for reference but removing the duplicate struct
// ExchangeRate represents currency exchange rates
type ExchangeRate struct {
ID uint `gorm:"primarykey" json:"id"`
FromCurrency Currency `gorm:"size:10;not null;index:idx_currency_pair" json:"from_currency"`
ToCurrency Currency `gorm:"size:10;not null;index:idx_currency_pair" json:"to_currency"`
Rate float64 `gorm:"type:decimal(15,6);not null" json:"rate"`
EffectiveDate time.Time `gorm:"type:date;not null;index" json:"effective_date"`
}
// TableName specifies the table name for ExchangeRate
func (ExchangeRate) TableName() string {
return "exchange_rates"
}
// ClassificationRule represents a rule for smart category classification
type ClassificationRule struct {
ID uint `gorm:"primarykey" json:"id"`
UserID uint `gorm:"not null;index" json:"user_id"`
Keyword string `gorm:"size:100;not null;index" json:"keyword"`
CategoryID uint `gorm:"not null;index" json:"category_id"`
MinAmount *float64 `gorm:"type:decimal(15,2)" json:"min_amount,omitempty"`
MaxAmount *float64 `gorm:"type:decimal(15,2)" json:"max_amount,omitempty"`
HitCount int `gorm:"default:0" json:"hit_count"`
// Relationships
Category Category `gorm:"foreignKey:CategoryID" json:"category,omitempty"`
}
// TableName specifies the table name for ClassificationRule
func (ClassificationRule) TableName() string {
return "classification_rules"
}
// BillStatus represents the status of a credit card bill
type BillStatus string
const (
BillStatusPending BillStatus = "pending" // Bill generated, not yet paid
BillStatusPaid BillStatus = "paid" // Bill fully paid
BillStatusOverdue BillStatus = "overdue" // Payment date passed, not paid
)
// CreditCardBill represents a credit card billing cycle statement
type CreditCardBill struct {
BaseModel
UserID uint `gorm:"not null;index" json:"user_id"`
AccountID uint `gorm:"not null;index" json:"account_id"`
BillingDate time.Time `gorm:"type:date;not null;index" json:"billing_date"` // Statement date
PaymentDueDate time.Time `gorm:"type:date;not null;index" json:"payment_due_date"` // Payment due date
PreviousBalance float64 `gorm:"type:decimal(15,2);default:0" json:"previous_balance"` // Balance from previous bill
TotalSpending float64 `gorm:"type:decimal(15,2);default:0" json:"total_spending"` // Total spending in this cycle
TotalPayment float64 `gorm:"type:decimal(15,2);default:0" json:"total_payment"` // Total payments made in this cycle
CurrentBalance float64 `gorm:"type:decimal(15,2);default:0" json:"current_balance"` // Outstanding balance
MinimumPayment float64 `gorm:"type:decimal(15,2);default:0" json:"minimum_payment"` // Minimum payment required
Status BillStatus `gorm:"size:20;not null;default:'pending'" json:"status"`
PaidAmount float64 `gorm:"type:decimal(15,2);default:0" json:"paid_amount"` // Amount paid towards this bill
PaidAt *time.Time `gorm:"type:datetime" json:"paid_at,omitempty"`
// Relationships
Account Account `gorm:"foreignKey:AccountID" json:"account,omitempty"`
RepaymentPlan *RepaymentPlan `gorm:"foreignKey:BillID" json:"repayment_plan,omitempty"`
}
// TableName specifies the table name for CreditCardBill
func (CreditCardBill) TableName() string {
return "credit_card_bills"
}
// RepaymentPlanStatus represents the status of a repayment plan
type RepaymentPlanStatus string
const (
RepaymentPlanStatusActive RepaymentPlanStatus = "active" // Plan is active
RepaymentPlanStatusCompleted RepaymentPlanStatus = "completed" // Plan completed
RepaymentPlanStatusCancelled RepaymentPlanStatus = "cancelled" // Plan cancelled
)
// RepaymentPlan represents a plan for repaying a credit card bill in installments
type RepaymentPlan struct {
BaseModel
UserID uint `gorm:"not null;index" json:"user_id"`
BillID uint `gorm:"not null;uniqueIndex" json:"bill_id"` // One plan per bill
TotalAmount float64 `gorm:"type:decimal(15,2);not null" json:"total_amount"`
RemainingAmount float64 `gorm:"type:decimal(15,2);not null" json:"remaining_amount"`
InstallmentCount int `gorm:"not null" json:"installment_count"`
InstallmentAmount float64 `gorm:"type:decimal(15,2);not null" json:"installment_amount"`
Status RepaymentPlanStatus `gorm:"size:20;not null;default:'active'" json:"status"`
// Relationships
Bill CreditCardBill `gorm:"foreignKey:BillID" json:"bill,omitempty"`
Installments []RepaymentInstallment `gorm:"foreignKey:PlanID" json:"installments,omitempty"`
}
// TableName specifies the table name for RepaymentPlan
func (RepaymentPlan) TableName() string {
return "repayment_plans"
}
// RepaymentInstallmentStatus represents the status of a repayment installment
type RepaymentInstallmentStatus string
const (
RepaymentInstallmentStatusPending RepaymentInstallmentStatus = "pending" // Not yet paid
RepaymentInstallmentStatusPaid RepaymentInstallmentStatus = "paid" // Paid
RepaymentInstallmentStatusOverdue RepaymentInstallmentStatus = "overdue" // Past due date
)
// RepaymentInstallment represents a single installment in a repayment plan
type RepaymentInstallment struct {
BaseModel
PlanID uint `gorm:"not null;index" json:"plan_id"`
DueDate time.Time `gorm:"type:date;not null;index" json:"due_date"`
Amount float64 `gorm:"type:decimal(15,2);not null" json:"amount"`
PaidAmount float64 `gorm:"type:decimal(15,2);default:0" json:"paid_amount"`
Status RepaymentInstallmentStatus `gorm:"size:20;not null;default:'pending'" json:"status"`
PaidAt *time.Time `gorm:"type:datetime" json:"paid_at,omitempty"`
Sequence int `gorm:"not null" json:"sequence"` // Installment number (1, 2, 3, ...)
// Relationships
Plan RepaymentPlan `gorm:"foreignKey:PlanID" json:"-"`
}
// TableName specifies the table name for RepaymentInstallment
func (RepaymentInstallment) TableName() string {
return "repayment_installments"
}
// PaymentReminder represents a reminder for upcoming payments
type PaymentReminder struct {
ID uint `gorm:"primarykey" json:"id"`
BillID uint `gorm:"not null;index" json:"bill_id"`
InstallmentID *uint `gorm:"index" json:"installment_id,omitempty"` // Optional, for installment reminders
ReminderDate time.Time `gorm:"type:date;not null;index" json:"reminder_date"`
Message string `gorm:"size:500;not null" json:"message"`
IsRead bool `gorm:"default:false" json:"is_read"`
CreatedAt time.Time `json:"created_at"`
// Relationships
Bill CreditCardBill `gorm:"foreignKey:BillID" json:"bill,omitempty"`
Installment *RepaymentInstallment `gorm:"foreignKey:InstallmentID" json:"installment,omitempty"`
}
// TableName specifies the table name for PaymentReminder
func (PaymentReminder) TableName() string {
return "payment_reminders"
}
// AppLock represents the application lock settings
type AppLock struct {
ID uint `gorm:"primarykey" json:"id"`
UserID uint `gorm:"not null;uniqueIndex" json:"user_id"`
PasswordHash string `gorm:"size:255;not null" json:"-"` // bcrypt hash of password
IsEnabled bool `gorm:"default:false" json:"is_enabled"`
FailedAttempts int `gorm:"default:0" json:"failed_attempts"`
LockedUntil *time.Time `gorm:"type:datetime" json:"locked_until,omitempty"`
LastFailedAttempt *time.Time `gorm:"type:datetime" json:"last_failed_attempt,omitempty"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
}
// User represents a user account for authentication
// Feature: api-interface-optimization
// Validates: Requirements 12, 13
type User struct {
ID uint `gorm:"primarykey" json:"id"`
Email string `gorm:"size:255;uniqueIndex" json:"email"`
PasswordHash string `gorm:"size:255" json:"-"`
Username string `gorm:"size:100" json:"username"`
Avatar string `gorm:"size:500" json:"avatar,omitempty"`
IsActive bool `gorm:"default:true" json:"is_active"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
DeletedAt gorm.DeletedAt `gorm:"index" json:"-"`
// Relationships
OAuthAccounts []OAuthAccount `gorm:"foreignKey:UserID" json:"oauth_accounts,omitempty"`
}
// TableName specifies the table name for User
func (User) TableName() string {
return "users"
}
// OAuthAccount represents an OAuth provider account linked to a user
// Feature: api-interface-optimization
// Validates: Requirements 13
type OAuthAccount struct {
ID uint `gorm:"primarykey" json:"id"`
UserID uint `gorm:"index" json:"user_id"`
Provider string `gorm:"size:50;index" json:"provider"` // github, google, etc.
ProviderID string `gorm:"size:255;index" json:"provider_id"`
AccessToken string `gorm:"size:500" json:"-"`
CreatedAt time.Time `json:"created_at"`
// Relationships
User User `gorm:"foreignKey:UserID" json:"-"`
}
// TableName specifies the table name for OAuthAccount
func (OAuthAccount) TableName() string {
return "oauth_accounts"
}
// TransactionTemplate represents a quick transaction template
// Feature: api-interface-optimization
// Validates: Requirements 15.1, 15.2
type TransactionTemplate struct {
ID uint `gorm:"primarykey" json:"id"`
UserID *uint `gorm:"index" json:"user_id,omitempty"`
Name string `gorm:"size:100;not null" json:"name"`
Amount float64 `gorm:"type:decimal(15,2)" json:"amount"`
Type TransactionType `gorm:"size:20;not null" json:"type"`
CategoryID uint `gorm:"not null" json:"category_id"`
AccountID uint `gorm:"not null" json:"account_id"`
Currency Currency `gorm:"size:10;not null;default:'CNY'" json:"currency"`
Note string `gorm:"size:500" json:"note,omitempty"`
SortOrder int `gorm:"default:0" json:"sort_order"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
// Relationships
Category Category `gorm:"foreignKey:CategoryID" json:"category,omitempty"`
Account Account `gorm:"foreignKey:AccountID" json:"account,omitempty"`
}
// TableName specifies the table name for TransactionTemplate
func (TransactionTemplate) TableName() string {
return "transaction_templates"
}
// UserPreference represents user preferences for quick entry
// Feature: api-interface-optimization
// Validates: Requirements 15.4
type UserPreference struct {
ID uint `gorm:"primarykey" json:"id"`
UserID *uint `gorm:"uniqueIndex" json:"user_id,omitempty"`
LastAccountID *uint `gorm:"index" json:"last_account_id,omitempty"`
LastCategoryID *uint `gorm:"index" json:"last_category_id,omitempty"`
FrequentAccounts string `gorm:"size:500" json:"frequent_accounts,omitempty"` // JSON array of account IDs
FrequentCategories string `gorm:"size:500" json:"frequent_categories,omitempty"` // JSON array of category IDs
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
}
// TableName specifies the table name for UserPreference
func (UserPreference) TableName() string {
return "user_preferences"
}
// TableName specifies the table name for AppLock
func (AppLock) TableName() string {
return "app_locks"
}
// IsLocked returns true if the app is currently locked due to failed attempts
func (a *AppLock) IsLocked() bool {
if a.LockedUntil == nil {
return false
}
return time.Now().Before(*a.LockedUntil)
}
// AllocationRecord represents a record of income allocation execution
type AllocationRecord struct {
ID uint `gorm:"primarykey" json:"id"`
UserID uint `gorm:"not null;index" json:"user_id"`
RuleID uint `gorm:"not null;index" json:"rule_id"`
RuleName string `gorm:"size:100;not null" json:"rule_name"`
SourceAccountID uint `gorm:"not null;index" json:"source_account_id"`
TotalAmount float64 `gorm:"type:decimal(15,2);not null" json:"total_amount"`
AllocatedAmount float64 `gorm:"type:decimal(15,2);not null" json:"allocated_amount"`
RemainingAmount float64 `gorm:"type:decimal(15,2);not null" json:"remaining_amount"`
Note string `gorm:"size:500" json:"note,omitempty"`
CreatedAt time.Time `json:"created_at"`
// Relationships
Rule AllocationRule `gorm:"foreignKey:RuleID" json:"rule,omitempty"`
SourceAccount Account `gorm:"foreignKey:SourceAccountID" json:"source_account,omitempty"`
Details []AllocationRecordDetail `gorm:"foreignKey:RecordID" json:"details,omitempty"`
}
// TableName specifies the table name for AllocationRecord
func (AllocationRecord) TableName() string {
return "allocation_records"
}
// AllocationRecordDetail represents a single allocation detail in a record
type AllocationRecordDetail struct {
ID uint `gorm:"primarykey" json:"id"`
RecordID uint `gorm:"not null;index" json:"record_id"`
TargetType TargetType `gorm:"size:20;not null" json:"target_type"`
TargetID uint `gorm:"not null" json:"target_id"`
TargetName string `gorm:"size:100;not null" json:"target_name"`
Amount float64 `gorm:"type:decimal(15,2);not null" json:"amount"`
Percentage *float64 `gorm:"type:decimal(5,2)" json:"percentage,omitempty"`
FixedAmount *float64 `gorm:"type:decimal(15,2)" json:"fixed_amount,omitempty"`
// Relationships
Record AllocationRecord `gorm:"foreignKey:RecordID" json:"-"`
}
// TableName specifies the table name for AllocationRecordDetail
func (AllocationRecordDetail) TableName() string {
return "allocation_record_details"
}
// AllModels returns all models for database migration
func AllModels() []interface{} {
return []interface{}{
&Account{},
&Category{},
&Tag{},
&Transaction{},
&TransactionTag{}, // Explicit join table for many-to-many relationship
&Budget{},
&PiggyBank{},
&RecurringTransaction{},
&AllocationRule{},
&AllocationTarget{},
&AllocationRecord{},
&AllocationRecordDetail{},
&ExchangeRate{},
&ClassificationRule{},
&CreditCardBill{},
&RepaymentPlan{},
&RepaymentInstallment{},
&PaymentReminder{},
&AppLock{},
&User{},
&OAuthAccount{},
&TransactionTemplate{},
&UserPreference{},
&Ledger{}, // Feature: accounting-feature-upgrade
&SystemCategory{}, // Feature: accounting-feature-upgrade
&TransactionImage{}, // Feature: accounting-feature-upgrade
&UserSettings{}, // Feature: accounting-feature-upgrade
}
}
// IsCreditAccountType returns true if the account type supports negative balance
func IsCreditAccountType(accountType AccountType) bool {
return accountType == AccountTypeCreditCard || accountType == AccountTypeCreditLine
}
// CurrencyInfo contains display information for a currency
type CurrencyInfo struct {
Code Currency `json:"code"`
Name string `json:"name"`
Symbol string `json:"symbol"`
}
// GetCurrencyInfo returns display information for all supported currencies
func GetCurrencyInfo() []CurrencyInfo {
return []CurrencyInfo{
// Major currencies
{Code: CurrencyCNY, Name: "人民币", Symbol: "¥"},
{Code: CurrencyUSD, Name: "美元", Symbol: "$"},
{Code: CurrencyEUR, Name: "欧元", Symbol: "€"},
{Code: CurrencyJPY, Name: "日元", Symbol: "¥"},
{Code: CurrencyGBP, Name: "英镑", Symbol: "£"},
{Code: CurrencyHKD, Name: "港币", Symbol: "HK$"},
// Asia Pacific
{Code: CurrencyAUD, Name: "澳元", Symbol: "A$"},
{Code: CurrencyNZD, Name: "新西兰元", Symbol: "NZ$"},
{Code: CurrencySGD, Name: "新加坡元", Symbol: "S$"},
{Code: CurrencyKRW, Name: "韩元", Symbol: "₩"},
{Code: CurrencyTHB, Name: "泰铢", Symbol: "฿"},
{Code: CurrencyTWD, Name: "新台币", Symbol: "NT$"},
{Code: CurrencyMOP, Name: "澳门元", Symbol: "MOP$"},
{Code: CurrencyPHP, Name: "菲律宾比索", Symbol: "₱"},
{Code: CurrencyIDR, Name: "印尼盾", Symbol: "Rp"},
{Code: CurrencyINR, Name: "印度卢比", Symbol: "₹"},
{Code: CurrencyVND, Name: "越南盾", Symbol: "₫"},
{Code: CurrencyMNT, Name: "蒙古图格里克", Symbol: "₮"},
{Code: CurrencyKHR, Name: "柬埔寨瑞尔", Symbol: "៛"},
{Code: CurrencyNPR, Name: "尼泊尔卢比", Symbol: "₨"},
{Code: CurrencyPKR, Name: "巴基斯坦卢比", Symbol: "₨"},
{Code: CurrencyBND, Name: "文莱元", Symbol: "B$"},
// Europe
{Code: CurrencyCHF, Name: "瑞士法郎", Symbol: "CHF"},
{Code: CurrencySEK, Name: "瑞典克朗", Symbol: "kr"},
{Code: CurrencyNOK, Name: "挪威克朗", Symbol: "kr"},
{Code: CurrencyDKK, Name: "丹麦克朗", Symbol: "kr"},
{Code: CurrencyCZK, Name: "捷克克朗", Symbol: "Kč"},
{Code: CurrencyHUF, Name: "匈牙利福林", Symbol: "Ft"},
{Code: CurrencyRUB, Name: "俄罗斯卢布", Symbol: "₽"},
{Code: CurrencyTRY, Name: "土耳其里拉", Symbol: "₺"},
// Americas
{Code: CurrencyCAD, Name: "加元", Symbol: "C$"},
{Code: CurrencyMXN, Name: "墨西哥比索", Symbol: "Mex$"},
{Code: CurrencyBRL, Name: "巴西雷亚尔", Symbol: "R$"},
// Middle East & Africa
{Code: CurrencyAED, Name: "阿联酋迪拉姆", Symbol: "د.إ"},
{Code: CurrencySAR, Name: "沙特里亚尔", Symbol: "﷼"},
{Code: CurrencyQAR, Name: "卡塔尔里亚尔", Symbol: "﷼"},
{Code: CurrencyKWD, Name: "科威特第纳尔", Symbol: "د.ك"},
{Code: CurrencyILS, Name: "以色列新谢克尔", Symbol: "₪"},
{Code: CurrencyZAR, Name: "南非兰特", Symbol: "R"},
}
}

View File

@@ -0,0 +1,40 @@
package models
import "gorm.io/gorm"
// SystemCategory represents system-level categories that cannot be deleted by users
// Feature: accounting-feature-upgrade
// Validates: Requirements 8.19, 8.20
type SystemCategory struct {
ID uint `gorm:"primarykey" json:"id"`
Code string `gorm:"size:50;uniqueIndex;not null" json:"code"` // refund, reimbursement
Name string `gorm:"size:100;not null" json:"name"`
Icon string `gorm:"size:100" json:"icon"`
Type string `gorm:"size:20;not null" json:"type"` // income, expense
IsSystem bool `gorm:"default:true" json:"is_system"`
}
// TableName specifies the table name for SystemCategory
func (SystemCategory) TableName() string {
return "system_categories"
}
// InitSystemCategories initializes the system categories (refund and reimbursement)
// This function should be called during application startup or migration
// Feature: accounting-feature-upgrade
// Validates: Requirements 8.19, 8.20
func InitSystemCategories(db *gorm.DB) error {
categories := []SystemCategory{
{Code: "refund", Name: "退款", Icon: "mdi:cash-refund", Type: "income", IsSystem: true},
{Code: "reimbursement", Name: "报销", Icon: "mdi:receipt-text-check", Type: "income", IsSystem: true},
}
for _, cat := range categories {
// Use FirstOrCreate to avoid duplicates
if err := db.FirstOrCreate(&cat, SystemCategory{Code: cat.Code}).Error; err != nil {
return err
}
}
return nil
}

View File

@@ -0,0 +1,41 @@
package models
import (
"time"
)
// TransactionImage represents an image attachment for a transaction
// Feature: accounting-feature-upgrade
// Validates: Requirements 4.1-4.8
type TransactionImage struct {
ID uint `gorm:"primarykey" json:"id"`
TransactionID uint `gorm:"not null;index" json:"transaction_id"`
FilePath string `gorm:"size:255;not null" json:"file_path"`
FileName string `gorm:"size:100" json:"file_name"`
FileSize int64 `json:"file_size"`
MimeType string `gorm:"size:50" json:"mime_type"`
CreatedAt time.Time `json:"created_at"`
// Relationships
Transaction Transaction `gorm:"foreignKey:TransactionID" json:"-"`
}
// TableName specifies the table name for TransactionImage
func (TransactionImage) TableName() string {
return "transaction_images"
}
// Image attachment constraints
const (
// MaxImagesPerTransaction limits the number of images per transaction
// Validates: Requirements 4.9
MaxImagesPerTransaction = 9
// MaxImageSizeBytes limits the size of each image to 10MB
// Validates: Requirements 4.10
MaxImageSizeBytes = 10 * 1024 * 1024 // 10MB
// AllowedImageTypes specifies the supported image formats
// Validates: Requirements 4.11
AllowedImageTypes = "image/jpeg,image/png,image/heic"
)

View File

@@ -0,0 +1,55 @@
package models
import (
"time"
)
// UserSettings represents user preferences and settings
// Feature: accounting-feature-upgrade
// Validates: Requirements 5.4, 6.5, 8.25-8.27
type UserSettings struct {
ID uint `gorm:"primarykey" json:"id"`
UserID *uint `gorm:"uniqueIndex" json:"user_id,omitempty"`
PreciseTimeEnabled bool `gorm:"default:true" json:"precise_time_enabled"`
IconLayout string `gorm:"size:10;default:'five'" json:"icon_layout"` // four, five, six
ImageCompression string `gorm:"size:10;default:'medium'" json:"image_compression"` // low, medium, high
ShowReimbursementBtn bool `gorm:"default:true" json:"show_reimbursement_btn"`
ShowRefundBtn bool `gorm:"default:true" json:"show_refund_btn"`
CurrentLedgerID *uint `gorm:"index" json:"current_ledger_id,omitempty"`
// Default account settings
// Feature: financial-core-upgrade
// Validates: Requirements 5.1, 5.2
DefaultExpenseAccountID *uint `gorm:"index" json:"default_expense_account_id,omitempty"`
DefaultIncomeAccountID *uint `gorm:"index" json:"default_income_account_id,omitempty"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
// Relationships
DefaultExpenseAccount *Account `gorm:"foreignKey:DefaultExpenseAccountID" json:"default_expense_account,omitempty"`
DefaultIncomeAccount *Account `gorm:"foreignKey:DefaultIncomeAccountID" json:"default_income_account,omitempty"`
}
// TableName specifies the table name for UserSettings
func (UserSettings) TableName() string {
return "user_settings"
}
// IconLayoutType represents the icon layout options
type IconLayoutType string
const (
IconLayoutFour IconLayoutType = "four"
IconLayoutFive IconLayoutType = "five"
IconLayoutSix IconLayoutType = "six"
)
// ImageCompressionType represents the image compression options
type ImageCompressionType string
const (
ImageCompressionLow ImageCompressionType = "low" // 鏍囨竻 - max width 800px
ImageCompressionMedium ImageCompressionType = "medium" // 楂樻竻 - max width 1200px
ImageCompressionHigh ImageCompressionType = "high" // 鍘熺敾 - no compression
)