feat: managing returns

This commit is contained in:
alzalia1 2025-08-18 18:20:33 +02:00
parent ca1eeafd8f
commit 6947bcfb01
11 changed files with 521 additions and 22 deletions

View file

@ -1,5 +1,8 @@
import 'package:seshat/data/services/api_client.dart';
import 'package:seshat/domain/models/accounting.dart';
import 'package:seshat/domain/models/bal.dart';
import 'package:seshat/domain/models/bal_stats.dart';
import 'package:seshat/domain/models/enums.dart';
import 'package:seshat/utils/result.dart';
class BalRepository {
@ -7,6 +10,7 @@ class BalRepository {
final ApiClient _apiClient;
List<Bal>? _bals;
Accounting? accounting;
Future<Result<List<Bal>>> getBals() async {
if (_bals != null) {
@ -95,4 +99,71 @@ class BalRepository {
await _getBalsNoCache();
return result;
}
Future<Result<BalStats>> getBalStats(int id) async {
return _apiClient.getBalStats(id);
}
Future<Result<Accounting>> getAccountingNoCache(int balId) async {
final result = await _apiClient.getAccounting(balId);
switch (result) {
case Ok():
accounting = result.value;
break;
default:
}
return result;
}
Future<Result<Accounting>> getAccounting(int balId) async {
if (accounting != null) {
return Result.ok(accounting!);
}
final result = await _apiClient.getAccounting(balId);
switch (result) {
case Ok():
accounting = result.value;
break;
default:
}
return result;
}
Future<Result<void>> returnToId(
int balId,
int ownerId,
ReturnType type,
) async {
final result = await _apiClient.returnToId(balId, ownerId, type.name);
switch (result) {
case Ok():
switch (type) {
case ReturnType.books:
final owner = accounting?.owners
.where((el) => el.ownerId == ownerId)
.firstOrNull;
if (owner?.owedMoney == 0) {
accounting?.owners.removeWhere((el) => el.ownerId == ownerId);
}
owner?.owed = [];
owner?.owedInstances = [];
break;
case ReturnType.money:
final owner = accounting?.owners
.where((el) => el.ownerId == ownerId)
.firstOrNull;
if (owner?.owed == null || owner!.owed.isEmpty) {
accounting?.owners.removeWhere((el) => el.ownerId == ownerId);
}
owner?.owedMoney = 0;
break;
case ReturnType.all:
accounting?.owners.removeWhere((el) => el.ownerId == ownerId);
break;
}
break;
default:
}
return result;
}
}