111 lines
2.9 KiB
Dart
111 lines
2.9 KiB
Dart
import 'package:flutter_bloc/flutter_bloc.dart';
|
|
import '../../data/models/super_admin_models.dart';
|
|
import '../../data/repositories/super_admin_repository.dart';
|
|
|
|
abstract class TreasuryState {
|
|
const TreasuryState();
|
|
}
|
|
|
|
class TreasuryInitial extends TreasuryState {}
|
|
|
|
class TreasuryLoading extends TreasuryState {}
|
|
|
|
class TreasuryLoaded extends TreasuryState {
|
|
final List<PayoutQueueItemModel> queue;
|
|
final double totalTreasuryBalanceJod;
|
|
final double totalApprovedTodayJod;
|
|
|
|
const TreasuryLoaded({
|
|
required this.queue,
|
|
required this.totalTreasuryBalanceJod,
|
|
required this.totalApprovedTodayJod,
|
|
});
|
|
|
|
TreasuryLoaded copyWith({
|
|
List<PayoutQueueItemModel>? queue,
|
|
double? totalTreasuryBalanceJod,
|
|
double? totalApprovedTodayJod,
|
|
}) {
|
|
return TreasuryLoaded(
|
|
queue: queue ?? this.queue,
|
|
totalTreasuryBalanceJod: totalTreasuryBalanceJod ?? this.totalTreasuryBalanceJod,
|
|
totalApprovedTodayJod: totalApprovedTodayJod ?? this.totalApprovedTodayJod,
|
|
);
|
|
}
|
|
}
|
|
|
|
class TreasuryCubit extends Cubit<TreasuryState> {
|
|
final SuperAdminRepository repository;
|
|
|
|
TreasuryCubit({required this.repository}) : super(TreasuryInitial());
|
|
|
|
Future<void> loadTreasury() async {
|
|
emit(TreasuryLoading());
|
|
try {
|
|
final queue = await repository.getPayoutQueue();
|
|
emit(TreasuryLoaded(
|
|
queue: queue,
|
|
totalTreasuryBalanceJod: 54200.0,
|
|
totalApprovedTodayJod: 0.0,
|
|
));
|
|
} catch (_) {
|
|
emit(const TreasuryLoaded(
|
|
queue: [],
|
|
totalTreasuryBalanceJod: 54200.0,
|
|
totalApprovedTodayJod: 0.0,
|
|
));
|
|
}
|
|
}
|
|
|
|
void approvePayout(int payoutId) {
|
|
if (state is TreasuryLoaded) {
|
|
final cur = state as TreasuryLoaded;
|
|
double approvedAmt = 0.0;
|
|
|
|
final updated = cur.queue.map((item) {
|
|
if (item.id == payoutId) {
|
|
approvedAmt = item.amountJod;
|
|
return PayoutQueueItemModel(
|
|
id: item.id,
|
|
teacherName: item.teacherName,
|
|
cliqAlias: item.cliqAlias,
|
|
amountJod: item.amountJod,
|
|
requestedAt: item.requestedAt,
|
|
status: 'completed',
|
|
);
|
|
}
|
|
return item;
|
|
}).toList();
|
|
|
|
emit(cur.copyWith(
|
|
queue: updated,
|
|
totalApprovedTodayJod: cur.totalApprovedTodayJod + approvedAmt,
|
|
));
|
|
}
|
|
}
|
|
|
|
void approveAllPayouts() {
|
|
if (state is TreasuryLoaded) {
|
|
final cur = state as TreasuryLoaded;
|
|
double sum = 0.0;
|
|
|
|
final updated = cur.queue.map((item) {
|
|
if (item.status == 'queued') sum += item.amountJod;
|
|
return PayoutQueueItemModel(
|
|
id: item.id,
|
|
teacherName: item.teacherName,
|
|
cliqAlias: item.cliqAlias,
|
|
amountJod: item.amountJod,
|
|
requestedAt: item.requestedAt,
|
|
status: 'completed',
|
|
);
|
|
}).toList();
|
|
|
|
emit(cur.copyWith(
|
|
queue: updated,
|
|
totalApprovedTodayJod: cur.totalApprovedTodayJod + sum,
|
|
));
|
|
}
|
|
}
|
|
}
|