Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions lib/data/repositories/account_repository.dart
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,18 @@ abstract class AccountRepository {
/// 检查账户是否有交易记录
Future<bool> hasTransactions(int accountId);

/// 创建账户平账交易
/// [accountId] 账户ID
/// [ledgerId] 账本ID
/// [amount] 平账金额(正数表示增加余额,负数表示减少余额)
/// [note] 备注信息
Future<int> createAdjustmentTransaction({
required int accountId,
required int ledgerId,
required double amount,
String? note,
});

/// 响应式监听账户信息变化
Stream<Account?> watchAccount(int accountId);

Expand Down
18 changes: 18 additions & 0 deletions lib/data/repositories/cloud/cloud_account_repository.dart
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,11 @@ class CloudAccountRepository implements AccountRepository {
if (toAccountId == accountId) {
balance += amount;
}
} else if (type == 'adjustment') {
// 平账交易:根据金额的正负来决定增加或减少余额
if (tx['account_id'] == accountId) {
balance += amount;
}
}
}

Expand Down Expand Up @@ -298,6 +303,9 @@ class CloudAccountRepository implements AccountRepository {
balance += amount;
} else if (type == 'expense') {
balance -= amount;
} else if (type == 'adjustment') {
// 平账交易:根据金额的正负来决定增加或减少余额
balance += amount;
}
}

Expand Down Expand Up @@ -592,4 +600,14 @@ class CloudAccountRepository implements AccountRepository {

return results.map((row) => _accountFromJson(row)).toList();
}

@override
Future<int> createAdjustmentTransaction({
required int accountId,
required int ledgerId,
required double amount,
String? note,
}) async {
throw UnimplementedError('云端平账交易创建暂不支持');
}
}
3 changes: 3 additions & 0 deletions lib/data/repositories/cloud/cloud_ledger_repository.dart
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,9 @@ class CloudLedgerRepository implements LedgerRepository {
balance += amount;
} else if (type == 'expense') {
balance -= amount;
} else if (type == 'adjustment') {
// 平账交易:根据金额的正负来决定增加或减少余额
balance += amount;
}
// transfer 不影响总余额
}
Expand Down
14 changes: 14 additions & 0 deletions lib/data/repositories/cloud/cloud_repository.dart
Original file line number Diff line number Diff line change
Expand Up @@ -606,6 +606,20 @@ class CloudRepository extends BaseRepository {
Stream<List<Transaction>> watchAccountTransactions(int accountId) =>
_account.watchAccountTransactions(accountId);

@override
Future<int> createAdjustmentTransaction({
required int accountId,
required int ledgerId,
required double amount,
String? note,
}) =>
_account.createAdjustmentTransaction(
accountId: accountId,
ledgerId: ledgerId,
amount: amount,
note: note,
);

// Statistics Repository 方法
@override
Future<List<({int? id, String name, String? icon, double total})>>
Expand Down
39 changes: 39 additions & 0 deletions lib/data/repositories/local/local_account_repository.dart
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,9 @@ class LocalAccountRepository implements AccountRepository {
} else if (t.type == 'transfer') {
// 作为转出账户
balance -= t.amount;
} else if (t.type == 'adjustment') {
// 平账交易:根据金额的正负来决定增加或减少余额
balance += t.amount;
}
}

Expand Down Expand Up @@ -181,6 +184,9 @@ class LocalAccountRepository implements AccountRepository {
balance -= tx.amount;
} else if (tx.type == 'transfer') {
balance -= tx.amount;
} else if (tx.type == 'adjustment') {
// 平账交易:根据金额的正负来决定增加或减少余额
balance += tx.amount;
}
} else if (tx.toAccountId == accountId) {
// 作为转入账户(转账)
Expand Down Expand Up @@ -210,6 +216,9 @@ class LocalAccountRepository implements AccountRepository {
balance -= tx.amount;
} else if (tx.type == 'transfer') {
balance -= tx.amount;
} else if (tx.type == 'adjustment') {
// 平账交易:根据金额的正负来决定增加或减少余额
balance += tx.amount;
}
} else if (tx.toAccountId == accountId) {
// 作为转入账户(转账)
Expand Down Expand Up @@ -463,4 +472,34 @@ class LocalAccountRepository implements AccountRepository {
..where((a) => a.id.isIn(accountIds)))
.get();
}

@override
Future<int> createAdjustmentTransaction({
required int accountId,
required int ledgerId,
required double amount,
String? note,
}) async {
logger.info('AccountAdjustment',
'📝 创建平账交易: accountId=$accountId, ledgerId=$ledgerId, amount=$amount, note=$note');

try {
final transactionId =
await db.into(db.transactions).insert(TransactionsCompanion.insert(
ledgerId: ledgerId,
type: 'adjustment',
amount: amount.abs(),
accountId: d.Value(accountId),
categoryId: const d.Value(null),
happenedAt: d.Value(DateTime.now()),
note: d.Value(note),
));

logger.info('AccountAdjustment', '✅ 平账交易创建成功!ID=$transactionId');
return transactionId;
} catch (e, stack) {
logger.error('AccountAdjustment', '❌ 创建平账交易失败', e, stack);
rethrow;
}
}
}
3 changes: 3 additions & 0 deletions lib/data/repositories/local/local_ledger_repository.dart
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,9 @@ class LocalLedgerRepository implements LedgerRepository {
balance += t.amount;
} else if (t.type == 'expense') {
balance -= t.amount;
} else if (t.type == 'adjustment') {
// 平账交易:根据金额的正负来决定增加或减少余额
balance += t.amount;
}
// transfer 不影响总余额
}
Expand Down
14 changes: 14 additions & 0 deletions lib/data/repositories/local/local_repository.dart
Original file line number Diff line number Diff line change
Expand Up @@ -635,6 +635,20 @@ class LocalRepository extends BaseRepository {
Stream<List<Transaction>> watchAccountTransactions(int accountId) =>
_accountRepo.watchAccountTransactions(accountId);

@override
Future<int> createAdjustmentTransaction({
required int accountId,
required int ledgerId,
required double amount,
String? note,
}) =>
_accountRepo.createAdjustmentTransaction(
accountId: accountId,
ledgerId: ledgerId,
amount: amount,
note: note,
);

@override
Future<void> batchInsertAccounts(List<AccountsCompanion> accounts) =>
_accountRepo.batchInsertAccounts(accounts);
Expand Down
3 changes: 3 additions & 0 deletions lib/l10n/app_en.arb
Original file line number Diff line number Diff line change
Expand Up @@ -1499,6 +1499,9 @@
"accountTypeOther": "Other",
"accountInitialBalance": "Initial Balance",
"accountInitialBalanceHint": "Enter initial balance (optional)",
"accountBalanceCurrent": "Current",
"accountBalanceTarget": "Enter target balance",
"accountAdjustmentHint": "Enter actual balance, system will auto-create adjustment record",
"accountDeleteWarningTitle": "Confirm Delete",
"accountDeleteWarningMessage": "This account has {count} related transactions. After deletion, account information in transaction records will be cleared. Confirm deletion?",
"@accountDeleteWarningMessage": {
Expand Down
18 changes: 18 additions & 0 deletions lib/l10n/app_localizations.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6570,6 +6570,24 @@ abstract class AppLocalizations {
/// **'Enter initial balance (optional)'**
String get accountInitialBalanceHint;

/// No description provided for @accountBalanceCurrent.
///
/// In en, this message translates to:
/// **'Current'**
String get accountBalanceCurrent;

/// No description provided for @accountBalanceTarget.
///
/// In en, this message translates to:
/// **'Enter target balance'**
String get accountBalanceTarget;

/// No description provided for @accountAdjustmentHint.
///
/// In en, this message translates to:
/// **'Enter actual balance, system will auto-create adjustment record'**
String get accountAdjustmentHint;

/// No description provided for @accountDeleteWarningTitle.
///
/// In en, this message translates to:
Expand Down
10 changes: 10 additions & 0 deletions lib/l10n/app_localizations_en.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3459,6 +3459,16 @@ class AppLocalizationsEn extends AppLocalizations {
@override
String get accountInitialBalanceHint => 'Enter initial balance (optional)';

@override
String get accountBalanceCurrent => 'Current';

@override
String get accountBalanceTarget => 'Enter target balance';

@override
String get accountAdjustmentHint =>
'Enter actual balance, system will auto-create adjustment record';

@override
String get accountDeleteWarningTitle => 'Confirm Delete';

Expand Down
9 changes: 9 additions & 0 deletions lib/l10n/app_localizations_zh.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3459,6 +3459,15 @@ class AppLocalizationsZh extends AppLocalizations {
@override
String get accountInitialBalanceHint => '请输入初始资金(可选)';

@override
String get accountBalanceCurrent => '当前';

@override
String get accountBalanceTarget => '输入目标余额';

@override
String get accountAdjustmentHint => '输入实际余额,系统将自动创建平账记录';

@override
String get accountDeleteWarningTitle => '确认删除';

Expand Down
3 changes: 3 additions & 0 deletions lib/l10n/app_zh.arb
Original file line number Diff line number Diff line change
Expand Up @@ -1395,6 +1395,9 @@
"accountTypeOther": "其他",
"accountInitialBalance": "初始资金",
"accountInitialBalanceHint": "请输入初始资金(可选)",
"accountBalanceCurrent": "当前",
"accountBalanceTarget": "输入目标余额",
"accountAdjustmentHint": "输入实际余额,系统将自动创建平账记录",
"accountDeleteWarningTitle": "确认删除",
"accountDeleteWarningMessage": "该账户有 {count} 笔关联交易,删除后交易记录中的账户信息将被清空。确认删除吗?",
"@accountDeleteWarningMessage": {
Expand Down
Loading