cashumit/lib/providers/receipt_provider.dart

172 lines
5.1 KiB
Dart

// lib/providers/receipt_provider.dart
import 'package:flutter/material.dart';
import 'package:cashumit/providers/receipt_state.dart';
import 'package:cashumit/models/receipt_item.dart';
import 'package:cashumit/services/receipt_service.dart'; // Pastikan ReceiptService sudah dibuat
import 'package:cashumit/models/firefly_account.dart'; // Untuk tipe akun
class ReceiptProvider with ChangeNotifier {
late ReceiptState _state;
ReceiptState get state => _state;
ReceiptProvider() {
_state = ReceiptState();
}
/// Inisialisasi state awal
Future<void> initialize() async {
await loadCredentialsAndAccounts();
// Bisa menambahkan inisialisasi lain jika diperlukan
}
/// Memuat kredensial dan akun
Future<void> loadCredentialsAndAccounts() async {
final credentials = await ReceiptService.loadCredentials();
if (credentials == null) {
// Jika tidak ada kredensial, kita tetap perlu memberitahu listener bahwa state berubah
// (misalnya untuk menampilkan pesan error di UI)
_state = _state.copyWith(
fireflyUrl: null,
accessToken: null,
accounts: [], // Kosongkan akun juga
);
notifyListeners();
return;
}
// Periksa apakah kredensial berubah
final credentialsChanged = _state.fireflyUrl != credentials['url'] || _state.accessToken != credentials['token'];
_state = _state.copyWith(
fireflyUrl: credentials['url'],
accessToken: credentials['token'],
);
notifyListeners();
// Jika kredensial ada dan berubah, lanjutkan untuk memuat akun
if (credentialsChanged) {
await loadAccounts();
} else if (_state.accounts.isEmpty) {
// Jika akun belum pernah dimuat, muat sekarang
await loadAccounts();
}
}
/// Memuat daftar akun
Future<void> loadAccounts() async {
if (_state.fireflyUrl == null || _state.accessToken == null) {
return;
}
try {
final allAccounts = await ReceiptService.loadAccounts(
baseUrl: _state.fireflyUrl!,
accessToken: _state.accessToken!,
);
_state = _state.copyWith(accounts: allAccounts);
notifyListeners();
} catch (error) {
// Error handling bisa dilakukan di sini atau dibiarkan untuk ditangani oleh UI
print('Error in ReceiptProvider.loadAccounts: $error');
// Bisa memicu state error jika diperlukan
notifyListeners(); // Tetap notify untuk memperbarui UI (misalnya menampilkan pesan error)
}
}
/// Menambahkan item ke receipt
void addItem(ReceiptItem item) {
_state = _state.copyWith(
items: [..._state.items, item],
);
notifyListeners();
}
/// Mengedit item di receipt
void editItem(int index, ReceiptItem newItem) {
if (index < 0 || index >= _state.items.length) return;
final updatedItems = List<ReceiptItem>.from(_state.items);
updatedItems[index] = newItem;
_state = _state.copyWith(
items: updatedItems,
);
notifyListeners();
}
/// Menghapus item dari receipt
void removeItem(int index) {
if (index < 0 || index >= _state.items.length) return;
final updatedItems = List<ReceiptItem>.from(_state.items)..removeAt(index);
_state = _state.copyWith(
items: updatedItems,
);
notifyListeners();
}
/// Memilih akun sumber
void selectSourceAccount(String id, String name) {
_state = _state.copyWith(
sourceAccountId: id,
sourceAccountName: name,
);
notifyListeners();
}
/// Memilih akun tujuan
void selectDestinationAccount(String id, String name) {
_state = _state.copyWith(
destinationAccountId: id,
destinationAccountName: name,
);
notifyListeners();
}
/// Mencari ID akun berdasarkan nama dan tipe
String? findAccountIdByName(String name, String expectedType) {
return ReceiptService.findAccountIdByName(
name: name,
expectedType: expectedType,
accounts: _state.accounts,
);
}
/// Mengirim transaksi (ini akan memanggil ReceiptService dan mungkin memperbarui state setelahnya)
Future<String?> submitTransaction() async {
if (_state.items.isEmpty) {
throw Exception('Tidak ada item untuk dikirim');
}
if (_state.fireflyUrl == null || _state.accessToken == null) {
throw Exception('Kredensial Firefly III tidak ditemukan');
}
// Pastikan akun sumber dan tujuan dipilih
// Logika untuk mencari ID berdasarkan nama jika diperlukan bisa ditambahkan di sini
// atau diharapkan UI sudah menangani ini sebelum memanggil submitTransaction
if (_state.sourceAccountId == null || _state.destinationAccountId == null) {
throw Exception('Akun sumber dan tujuan harus dipilih');
}
final transactionId = await ReceiptService.submitTransaction(
items: _state.items,
transactionDate: _state.transactionDate,
sourceAccountId: _state.sourceAccountId!,
destinationAccountId: _state.destinationAccountId!,
accounts: _state.accounts,
baseUrl: _state.fireflyUrl!,
accessToken: _state.accessToken!,
);
return transactionId;
}
// Tambahkan metode lain sesuai kebutuhan, seperti untuk memperbarui transactionDate jika diperlukan
}