semua fitur selesai

This commit is contained in:
Andreas Malvino
2025-06-30 15:22:38 +07:00
parent 8284c93aa5
commit 0423c2fdf9
54 changed files with 11844 additions and 3143 deletions

View File

@ -18,6 +18,75 @@ class AsetProvider extends GetxService {
client = Supabase.instance.client;
}
// Method to clear any cached data
void clearCache() {
debugPrint('Clearing AsetProvider cached data');
// Clear any cached asset data or state
// This is useful when logging out to ensure no user data remains in memory
// Note: Since this provider doesn't currently maintain any persistent cache variables,
// this method serves as a placeholder for future cache implementations
}
// Delete an asset and all related data
Future<bool> deleteAset(String asetId) async {
try {
debugPrint('🔄 Starting deletion process for asset ID: $asetId');
// 1. Get existing photo URLs to delete them from storage
debugPrint('📋 Fetching photos for asset ID: $asetId');
final existingPhotos = await client
.from('foto_aset')
.select('foto_aset')
.eq('id_aset', asetId);
// 2. Delete files from storage first
if (existingPhotos is List && existingPhotos.isNotEmpty) {
debugPrint('🗑️ Deleting ${existingPhotos.length} files from storage');
for (final photo in existingPhotos) {
final url = photo['foto_aset'] as String?;
if (url != null && url.isNotEmpty) {
await deleteFileFromStorage(url);
}
}
}
// 3. Delete records from related tables in the correct order to maintain referential integrity
// 3.1 Delete rental time units
debugPrint('🗑️ Deleting rental time units for asset ID: $asetId');
await deleteSatuanWaktuSewaByAsetId(asetId);
// 3.2 Delete photo records from database
debugPrint('🗑️ Deleting photo records for asset ID: $asetId');
await client.from('foto_aset').delete().eq('id_aset', asetId);
// 3.3 Delete bookings if any (optional, may want to keep for historical records)
debugPrint('🗑️ Checking for bookings related to asset ID: $asetId');
final bookings = await client
.from('booked_detail')
.select('id')
.eq('aset_id', asetId);
if (bookings is List && bookings.isNotEmpty) {
debugPrint('⚠️ Found ${bookings.length} bookings for this asset');
debugPrint('⚠️ Consider handling booking records appropriately');
// Uncomment to delete bookings:
// await client.from('booked_detail').delete().eq('aset_id', asetId);
}
// 4. Finally delete the asset itself
debugPrint('🗑️ Deleting asset record with ID: $asetId');
await client.from('aset').delete().eq('id', asetId);
debugPrint('✅ Asset deletion completed successfully');
return true;
} catch (e, stackTrace) {
debugPrint('❌ Error deleting asset: $e');
debugPrint('❌ Stack trace: $stackTrace');
return false;
}
}
// Mendapatkan semua aset dengan kategori "sewa"
Future<List<AsetModel>> getSewaAsets() async {
try {
@ -805,7 +874,8 @@ class AsetProvider extends GetxService {
// Fungsi untuk membuat pesanan lengkap (sewa_aset, booked_detail, dan tagihan_sewa) dalam satu operasi
Future<bool> createCompleteOrder({
required Map<String, dynamic> sewaAsetData,
required Map<String, dynamic> bookedDetailData,
required dynamic
bookedDetailData, // Changed to dynamic to accept List or Map
required Map<String, dynamic> tagihanSewaData,
}) async {
try {
@ -813,15 +883,39 @@ class AsetProvider extends GetxService {
debugPrint('📦 sewa_aset data:');
sewaAsetData.forEach((key, value) => debugPrint(' $key: $value'));
debugPrint('📦 booked_detail data:');
bookedDetailData.forEach((key, value) => debugPrint(' $key: $value'));
// Check if bookedDetailData is a list (for package orders) or a single map (for regular orders)
bool isPackageOrder = bookedDetailData is List;
// Ensure we don't try to insert a status field that no longer exists
if (bookedDetailData.containsKey('status')) {
if (isPackageOrder) {
debugPrint(
'⚠️ Removing status field from booked_detail data as it does not exist in the table',
'📦 Package order detected with ${bookedDetailData.length} booked_detail items',
);
bookedDetailData.remove('status');
for (int i = 0; i < bookedDetailData.length; i++) {
debugPrint('📦 booked_detail item $i:');
bookedDetailData[i].forEach(
(key, value) => debugPrint(' $key: $value'),
);
// Ensure we don't try to insert a status field that no longer exists
if (bookedDetailData[i].containsKey('status')) {
debugPrint(
'⚠️ Removing status field from booked_detail data as it does not exist in the table',
);
bookedDetailData[i].remove('status');
}
}
} else {
debugPrint('📦 Regular order with single booked_detail');
debugPrint('📦 booked_detail data:');
bookedDetailData.forEach((key, value) => debugPrint(' $key: $value'));
// Ensure we don't try to insert a status field that no longer exists
if (bookedDetailData.containsKey('status')) {
debugPrint(
'⚠️ Removing status field from booked_detail data as it does not exist in the table',
);
bookedDetailData.remove('status');
}
}
debugPrint('📦 tagihan_sewa data:');
@ -835,19 +929,36 @@ class AsetProvider extends GetxService {
tagihanSewaData.remove('nama_aset');
}
// Insert all three records
// Insert sewa_aset record
final sewaAsetResult =
await client.from('sewa_aset').insert(sewaAsetData).select().single();
debugPrint('✅ sewa_aset created: ${sewaAsetResult['id']}');
final bookedDetailResult =
await client
.from('booked_detail')
.insert(bookedDetailData)
.select()
.single();
debugPrint('✅ booked_detail created: ${bookedDetailResult['id']}');
// Insert booked_detail record(s)
if (isPackageOrder) {
// For package orders, insert multiple booked_detail records
for (int i = 0; i < bookedDetailData.length; i++) {
final bookedDetailItem = bookedDetailData[i];
final bookedDetailResult =
await client
.from('booked_detail')
.insert(bookedDetailItem)
.select()
.single();
debugPrint('✅ booked_detail $i created: ${bookedDetailResult['id']}');
}
} else {
// For regular orders, insert a single booked_detail record
final bookedDetailResult =
await client
.from('booked_detail')
.insert(bookedDetailData)
.select()
.single();
debugPrint('✅ booked_detail created: ${bookedDetailResult['id']}');
}
// Insert tagihan_sewa record
final tagihanSewaResult =
await client
.from('tagihan_sewa')
@ -875,9 +986,19 @@ class AsetProvider extends GetxService {
);
// Print the field names from each data object to help debug
debugPrint('❌ Fields in sewa_aset data: ${sewaAsetData.keys.toList()}');
debugPrint(
'❌ Fields in booked_detail data: ${bookedDetailData.keys.toList()}',
);
if (bookedDetailData is List) {
for (int i = 0; i < bookedDetailData.length; i++) {
debugPrint(
'❌ Fields in booked_detail item $i: ${bookedDetailData[i].keys.toList()}',
);
}
} else {
debugPrint(
'❌ Fields in booked_detail data: ${bookedDetailData.keys.toList()}',
);
}
debugPrint(
'❌ Fields in tagihan_sewa data: ${tagihanSewaData.keys.toList()}',
);
@ -1461,6 +1582,8 @@ class AsetProvider extends GetxService {
// Get photos for a package
Future<List<String>> getFotoPaket(String paketId) async {
try {
debugPrint('🔍 Fetching photos for package ID: $paketId');
final response = await client
.from('foto_aset')
.select('foto_aset')
@ -1468,13 +1591,27 @@ class AsetProvider extends GetxService {
.order('created_at');
if (response != null && response.isNotEmpty) {
return response
.map<String>((item) => item['foto_aset'] as String)
.toList();
// Extract photo URLs and filter out duplicates
final Set<String> uniqueUrls = {};
final List<String> uniquePhotos = [];
for (var item in response) {
final String url = item['foto_aset'] as String;
if (url.isNotEmpty && !uniqueUrls.contains(url)) {
uniqueUrls.add(url);
uniquePhotos.add(url);
}
}
debugPrint(
'📸 Found ${response.length} photos, ${uniquePhotos.length} unique for package $paketId',
);
return uniquePhotos;
}
debugPrint('⚠️ No photos found for package ID: $paketId');
return [];
} catch (e) {
debugPrint('Error getting package photos: $e');
debugPrint('Error getting package photos for ID $paketId: $e');
return [];
}
}
@ -1910,7 +2047,7 @@ class AsetProvider extends GetxService {
'tagihan_sewa_id': tagihanSewaId,
'metode_pembayaran': metodePembayaran,
'total_pembayaran': nominal,
'status': 'lunas',
'status': 'diterima',
'created_at': DateTime.now().toIso8601String(),
'id_petugas': idPetugas,
};
@ -1979,4 +2116,74 @@ class AsetProvider extends GetxService {
}
return 0;
}
/// Delete a package (paket) and all related data
/// This includes:
/// 1. Deleting photos from storage
/// 2. Removing records from foto_aset table
/// 3. Removing records from satuan_waktu_sewa table
/// 4. Removing records from paket_item table
/// 5. Finally deleting the package itself from the paket table
Future<bool> deletePaket(String paketId) async {
try {
debugPrint('🔄 Starting deletion process for package ID: $paketId');
// 1. Get all photo URLs for this package
debugPrint('📋 Fetching photos for package ID: $paketId');
final existingPhotos = await client
.from('foto_aset')
.select('foto_aset')
.eq('id_paket', paketId);
// 2. Delete files from storage first
if (existingPhotos is List && existingPhotos.isNotEmpty) {
debugPrint('🗑️ Deleting ${existingPhotos.length} files from storage');
for (final photo in existingPhotos) {
final url = photo['foto_aset'] as String?;
if (url != null && url.isNotEmpty) {
await deleteFileFromStorage(url);
}
}
}
// 3. Delete records from related tables in the correct order to maintain referential integrity
// 3.1 Delete rental time units related to this package
debugPrint('🗑️ Deleting rental time units for package ID: $paketId');
await client.from('satuan_waktu_sewa').delete().eq('paket_id', paketId);
// 3.2 Delete photo records from database
debugPrint('🗑️ Deleting photo records for package ID: $paketId');
await client.from('foto_aset').delete().eq('id_paket', paketId);
// 3.3 Delete package items
debugPrint('🗑️ Deleting package items for package ID: $paketId');
await client.from('paket_item').delete().eq('paket_id', paketId);
// 3.4 Check for bookings (optional)
debugPrint('🔍 Checking for bookings related to package ID: $paketId');
final bookings = await client
.from('sewa_aset')
.select('id')
.eq('paket_id', paketId)
.not('status', 'in', '(DIBATALKAN,SELESAI)');
if (bookings is List && bookings.isNotEmpty) {
debugPrint('⚠️ Found ${bookings.length} bookings for this package');
debugPrint(
'⚠️ These bookings will be orphaned. Consider updating them if needed',
);
}
// 4. Finally delete the package itself
debugPrint('🗑️ Deleting package record with ID: $paketId');
await client.from('paket').delete().eq('id', paketId);
debugPrint('✅ Package deletion completed successfully');
return true;
} catch (e, stackTrace) {
debugPrint('❌ Error deleting package: $e');
debugPrint('❌ Stack trace: $stackTrace');
return false;
}
}
}

View File

@ -75,6 +75,15 @@ class AuthProvider extends GetxService {
await client.auth.signOut();
}
// Method to clear any cached data in the AuthProvider
void clearAuthData() {
// Clear any cached user data or state
// This method is called during logout to ensure all user-related data is cleared
debugPrint('Clearing AuthProvider cached data');
// Currently, signOut() handles most of the cleanup, but this method can be extended
// if additional cleanup is needed in the future
}
User? get currentUser => client.auth.currentUser;
Stream<AuthState> get authChanges => client.auth.onAuthStateChange;
@ -415,28 +424,17 @@ class AuthProvider extends GetxService {
final userData =
await client
.from('warga_desa')
.select('nomor_telepon, no_telepon, phone')
.select('no_hp')
.eq('user_id', user.id)
.maybeSingle();
// Jika berhasil mendapatkan data, cek beberapa kemungkinan nama kolom
if (userData != null) {
if (userData.containsKey('nomor_telepon')) {
final phone = userData['nomor_telepon']?.toString();
if (phone != null && phone.isNotEmpty) return phone;
}
if (userData.containsKey('no_telepon')) {
final phone = userData['no_telepon']?.toString();
if (phone != null && phone.isNotEmpty) return phone;
}
if (userData.containsKey('phone')) {
final phone = userData['phone']?.toString();
if (userData.containsKey('no_hp')) {
final phone = userData['no_hp']?.toString();
if (phone != null && phone.isNotEmpty) return phone;
}
}
// Fallback ke data dari Supabase Auth
final userMetadata = user.userMetadata;
if (userMetadata != null) {
@ -496,6 +494,146 @@ class AuthProvider extends GetxService {
}
}
// Metode untuk mendapatkan tanggal lahir dari tabel warga_desa berdasarkan user_id
Future<String?> getUserTanggalLahir() async {
final user = currentUser;
if (user == null) {
debugPrint('No current user found when getting tanggal_lahir');
return null;
}
try {
debugPrint('Fetching tanggal_lahir for user_id: ${user.id}');
// Coba ambil tanggal lahir dari tabel warga_desa
final userData =
await client
.from('warga_desa')
.select('tanggal_lahir')
.eq('user_id', user.id)
.maybeSingle();
// Jika berhasil mendapatkan data
if (userData != null && userData.containsKey('tanggal_lahir')) {
final tanggalLahir = userData['tanggal_lahir']?.toString();
if (tanggalLahir != null && tanggalLahir.isNotEmpty) {
debugPrint('Found tanggal_lahir: $tanggalLahir');
return tanggalLahir;
}
}
return null;
} catch (e) {
debugPrint('Error fetching user tanggal_lahir: $e');
return null;
}
}
// Metode untuk mendapatkan RT/RW dari tabel warga_desa berdasarkan user_id
Future<String?> getUserRtRw() async {
final user = currentUser;
if (user == null) {
debugPrint('No current user found when getting rt_rw');
return null;
}
try {
debugPrint('Fetching rt_rw for user_id: ${user.id}');
// Coba ambil RT/RW dari tabel warga_desa
final userData =
await client
.from('warga_desa')
.select('rt_rw')
.eq('user_id', user.id)
.maybeSingle();
// Jika berhasil mendapatkan data
if (userData != null && userData.containsKey('rt_rw')) {
final rtRw = userData['rt_rw']?.toString();
if (rtRw != null && rtRw.isNotEmpty) {
debugPrint('Found rt_rw: $rtRw');
return rtRw;
}
}
return null;
} catch (e) {
debugPrint('Error fetching user rt_rw: $e');
return null;
}
}
// Metode untuk mendapatkan kelurahan/desa dari tabel warga_desa berdasarkan user_id
Future<String?> getUserKelurahanDesa() async {
final user = currentUser;
if (user == null) {
debugPrint('No current user found when getting kelurahan_desa');
return null;
}
try {
debugPrint('Fetching kelurahan_desa for user_id: ${user.id}');
// Coba ambil kelurahan/desa dari tabel warga_desa
final userData =
await client
.from('warga_desa')
.select('kelurahan_desa')
.eq('user_id', user.id)
.maybeSingle();
// Jika berhasil mendapatkan data
if (userData != null && userData.containsKey('kelurahan_desa')) {
final kelurahanDesa = userData['kelurahan_desa']?.toString();
if (kelurahanDesa != null && kelurahanDesa.isNotEmpty) {
debugPrint('Found kelurahan_desa: $kelurahanDesa');
return kelurahanDesa;
}
}
return null;
} catch (e) {
debugPrint('Error fetching user kelurahan_desa: $e');
return null;
}
}
// Metode untuk mendapatkan kecamatan dari tabel warga_desa berdasarkan user_id
Future<String?> getUserKecamatan() async {
final user = currentUser;
if (user == null) {
debugPrint('No current user found when getting kecamatan');
return null;
}
try {
debugPrint('Fetching kecamatan for user_id: ${user.id}');
// Coba ambil kecamatan dari tabel warga_desa
final userData =
await client
.from('warga_desa')
.select('kecamatan')
.eq('user_id', user.id)
.maybeSingle();
// Jika berhasil mendapatkan data
if (userData != null && userData.containsKey('kecamatan')) {
final kecamatan = userData['kecamatan']?.toString();
if (kecamatan != null && kecamatan.isNotEmpty) {
debugPrint('Found kecamatan: $kecamatan');
return kecamatan;
}
}
return null;
} catch (e) {
debugPrint('Error fetching user kecamatan: $e');
return null;
}
}
// Mendapatkan data sewa_aset berdasarkan status (misal: MENUNGGU PEMBAYARAN, PEMBAYARANAN DENDA)
Future<List<Map<String, dynamic>>> getSewaAsetByStatus(
List<String> statuses,
@ -507,28 +645,78 @@ class AuthProvider extends GetxService {
}
try {
debugPrint(
'Fetching sewa_aset for user_id: \\${user.id} with statuses: \\${statuses.join(', ')}',
'Fetching sewa_aset for user_id: ${user.id} with statuses: ${statuses.join(', ')}',
);
// Supabase expects the IN filter as a comma-separated string in parentheses
final statusString = '(${statuses.map((s) => '"$s"').join(',')})';
// Get sewa_aset records filtered by user_id and status
final response = await client
.from('sewa_aset')
.select('*')
.eq('user_id', user.id)
.filter('status', 'in', statusString);
debugPrint('Fetched sewa_aset count: \\${response.length}');
// Pastikan response adalah List
.filter('status', 'in', statusString)
.order('created_at', ascending: false);
debugPrint('Fetched sewa_aset count: ${response.length}');
// Process the response to handle package data
if (response is List) {
return response
.map<Map<String, dynamic>>(
(item) => Map<String, dynamic>.from(item),
)
.toList();
final List<Map<String, dynamic>> processedResponse = [];
for (var item in response) {
final Map<String, dynamic> processedItem = Map<String, dynamic>.from(
item,
);
// If aset_id is null and paket_id is not null, fetch package data
if (item['aset_id'] == null && item['paket_id'] != null) {
final String paketId = item['paket_id'];
debugPrint(
'Found rental with paket_id: $paketId, fetching package details',
);
try {
// Get package name from paket table
final paketResponse =
await client
.from('paket')
.select('nama')
.eq('id', paketId)
.maybeSingle();
if (paketResponse != null && paketResponse['nama'] != null) {
processedItem['nama_paket'] = paketResponse['nama'];
debugPrint('Found package name: ${paketResponse['nama']}');
}
// Get package photo from foto_aset table
final fotoResponse =
await client
.from('foto_aset')
.select('foto_aset')
.eq('id_paket', paketId)
.limit(1)
.maybeSingle();
if (fotoResponse != null && fotoResponse['foto_aset'] != null) {
processedItem['foto_paket'] = fotoResponse['foto_aset'];
debugPrint('Found package photo: ${fotoResponse['foto_aset']}');
}
} catch (e) {
debugPrint('Error fetching package details: $e');
}
}
processedResponse.add(processedItem);
}
return processedResponse;
} else {
return [];
}
} catch (e) {
debugPrint('Error fetching sewa_aset by status: \\${e.toString()}');
debugPrint('Error fetching sewa_aset by status: ${e.toString()}');
return [];
}
}

View File

@ -9,6 +9,16 @@ class PesananProvider {
final SupabaseClient _supabase = Supabase.instance.client;
final _tableName = 'pesanan';
// Method to clear any cached data
void clearCache() {
print('Clearing PesananProvider cached data');
// Clear any cached order data or state
// This is useful when logging out to ensure no user data remains in memory
// Note: Since this provider doesn't currently maintain any persistent cache variables,
// this method serves as a placeholder for future cache implementations
}
Future<List<PesananModel>> getPesananByUserId(String userId) async {
try {
final response = await _supabase