import 'dart:convert'; import 'package:connectivity_plus/connectivity_plus.dart'; import 'package:siopas/connection/connection.dart'; import 'package:siopas/models/asset_status_model.dart'; import 'package:siopas/models/condition_peti_model.dart'; import 'package:siopas/models/customer_model.dart'; import 'package:siopas/models/transfer_peti_model.dart'; import 'package:siopas/models/type_peti_model.dart'; import 'package:siopas/models/warehouse_mode.dart'; import 'package:internet_connection_checker/internet_connection_checker.dart'; import '../migrations/databasehelper.dart'; import 'package:http/http.dart' as http; import '../models/disposal_model.dart'; import '../models/m_asset_status_model.dart'; class SyncronizationDataAPI { final conn = SqfliteDatabaseHelper.instance; Future> fetchAssetStatusFromApi() async { final apiURL = '${await getBaseUrl()}/asset-status'; final response = await http.get(Uri.parse(apiURL)); if (response.statusCode == 200) { List data = json.decode(response.body)['data']['asset_status']; print('Success Fetch Data API Asset Status'); List assetStatusApiList = data .map( (item) => AssetStatusModel.fromJson(item as Map)) .toList(); return assetStatusApiList; } else { throw Exception('Failed to fetch data from API Peti'); } } Future> fetchPetiFromApi() async { final apiURL = '${await getBaseUrl()}/peti-asset'; final response = await http.get(Uri.parse(apiURL)); if (response.statusCode == 200) { List data = json.decode(response.body)['data']['petis']; print('Success Fetch Data Peti'); List petiDBList = data .map((item) => PetiAssetModel.fromJson(item as Map)) .toList(); return petiDBList; } else { throw Exception('Failed to fetch data from API Peti'); } } // Future> fetchPetiFromApi() async { // final apiURL = 'http://192.168.0.18:8000/api/v1/peti-asset'; // final response = await http.get(Uri.parse(apiURL)); // if (response.statusCode == 200) { // List data = json.decode(response.body)['data']['petis']; // print('Success Fetch Data Peti'); // List petiList = data.map((item) { // // Extracting and transforming data from JSON // String id = item['id'].toString(); // String tipePetiId = item['tipe_peti_id'].toString(); // String warna = item['warna'].toString(); // int? packingNo = item['packing_no'] != null // ? int.parse(item['packing_no'].toString()) // : null; // String customerID = item['customer_id'].toString(); // String warehouseID = item['warehouse_id'].toString(); // String kondisiPetiID = item['kondisipeti_id'].toString(); // int? jumlah = item['jumlah'] != null // ? int.parse(item['jumlah'].toString()) // : null; // DateTime datePembuatan = DateTime.parse(item['date_pembuatan']); // // Creating an instance of PetiAssetModel // PetiAssetModel peti = PetiAssetModel( // id: id, // tipe_peti_id: tipePetiId, // warna: warna, // packing_no: packingNo, // customer_id: customerID, // warehouse_id: warehouseID, // kondisipeti_id: kondisiPetiID, // jumlah: jumlah, // date_pembuatan: datePembuatan, // created_by: // item['created_by'] != null ? item['created_by'].toString() : null, // updated_by: // item['updated_by'] != null ? item['updated_by'].toString() : null, // ); // return peti; // }).toList(); // return petiList; // } else { // throw Exception('Failed to fetch data from API Peti'); // } // } Future> fetchWarehouseFromApi() async { final apiURL = '${await getBaseUrl()}/m-warehouse'; final response = await http.get(Uri.parse(apiURL)); if (response.statusCode == 200) { List data = json.decode(response.body)['data']['warehouse']; print('Success Fetch Data Warehouse'); List warehouseDBList = data .map((item) => WarehouseModel.fromJson(item as Map)) .toList(); return warehouseDBList; } else { throw Exception('Failed to fetch data from API Warehouse'); } } Future> fetchCustomerFromApi() async { final apiURL = '${await getBaseUrl()}/m-customer'; final response = await http.get(Uri.parse(apiURL)); if (response.statusCode == 200) { List data = json.decode(response.body)['data']['customers']; print('Success Fetch Data Customer'); List customerDBList = data .map((item) => CustomerModel.fromJson(item as Map)) .toList(); return customerDBList; } else { throw Exception('Failed to fetch data from API Customer'); } } Future> fetchTipePetiFromApi() async { final apiURL = '${await getBaseUrl()}/m-type-peti'; final response = await http.get(Uri.parse(apiURL)); if (response.statusCode == 200) { List data = json.decode(response.body)['data']['tipe_peti']; print('Success Fetch Data Tipe Peti'); List tipePetiDBList = data .map((item) => TypePetiModel.fromJson(item as Map)) .toList(); return tipePetiDBList; } else { throw Exception('Failed to fetch data from API Tipe Peti'); } } Future> fetchKondisiPetiFromApi() async { final apiURL = '${await getBaseUrl()}/m-kondisi-peti'; final response = await http.get(Uri.parse(apiURL)); if (response.statusCode == 200) { List data = json.decode(response.body)['data']['kondisi_peti']; print('Success Fetch Data Master Kondisi Peti'); List kondisiPetiDBList = data .map((item) => ConditionPetiModel.fromJson(item as Map)) .toList(); return kondisiPetiDBList; } else { throw Exception('Failed to fetch data from API Master Kondisi Peti'); } } Future> fetchTransferPetiFromApi() async { final apiURL = '${await getBaseUrl()}/m-transfer-peti'; final response = await http.get(Uri.parse(apiURL)); if (response.statusCode == 200) { List data = json.decode(response.body)['data']['transfer_peti']; print('Success Fetch Data Transfer Peti'); List transferPetiDBList = data .map((item) => TransferPetiModel.fromJson(item as Map)) .toList(); return transferPetiDBList; } else { throw Exception('Failed to fetch data from API Transfer Peti'); } } Future> fetchDisposalFromApi() async { final apiURL = '${await getBaseUrl()}/m-disposal-peti'; final response = await http.get(Uri.parse(apiURL)); if (response.statusCode == 200) { List data = json.decode(response.body)['data']['disposals']; print('Success Fetch Data Disposal Peti'); List disposalPetiDBList = data .map((item) => DisposalPetiModel.fromJson(item as Map)) .toList(); return disposalPetiDBList; } else { throw Exception('Failed to fetch data from API Disposal Peti'); } } }