You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
225 lines
7.6 KiB
225 lines
7.6 KiB
12 months ago
|
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<List<AssetStatusModel>> fetchAssetStatusFromApi() async {
|
||
|
final apiURL = '${await getBaseUrl()}/asset-status';
|
||
|
|
||
|
final response = await http.get(Uri.parse(apiURL));
|
||
|
|
||
|
if (response.statusCode == 200) {
|
||
|
List<dynamic> data = json.decode(response.body)['data']['asset_status'];
|
||
|
print('Success Fetch Data API Asset Status');
|
||
|
List<AssetStatusModel> assetStatusApiList = data
|
||
|
.map(
|
||
|
(item) => AssetStatusModel.fromJson(item as Map<String, dynamic>))
|
||
|
.toList();
|
||
|
|
||
|
return assetStatusApiList;
|
||
|
} else {
|
||
|
throw Exception('Failed to fetch data from API Peti');
|
||
|
}
|
||
|
}
|
||
|
|
||
|
Future<List<PetiAssetModel>> fetchPetiFromApi() async {
|
||
|
final apiURL = '${await getBaseUrl()}/peti-asset';
|
||
|
|
||
|
final response = await http.get(Uri.parse(apiURL));
|
||
|
|
||
|
if (response.statusCode == 200) {
|
||
|
List<dynamic> data = json.decode(response.body)['data']['petis'];
|
||
|
print('Success Fetch Data Peti');
|
||
|
List<PetiAssetModel> petiDBList = data
|
||
|
.map((item) => PetiAssetModel.fromJson(item as Map<String, dynamic>))
|
||
|
.toList();
|
||
|
|
||
|
return petiDBList;
|
||
|
} else {
|
||
|
throw Exception('Failed to fetch data from API Peti');
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// Future<List<PetiAssetModel>> 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<dynamic> data = json.decode(response.body)['data']['petis'];
|
||
|
// print('Success Fetch Data Peti');
|
||
|
|
||
|
// List<PetiAssetModel> 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<List<WarehouseModel>> fetchWarehouseFromApi() async {
|
||
|
final apiURL = '${await getBaseUrl()}/m-warehouse';
|
||
|
|
||
|
final response = await http.get(Uri.parse(apiURL));
|
||
|
|
||
|
if (response.statusCode == 200) {
|
||
|
List<dynamic> data = json.decode(response.body)['data']['warehouse'];
|
||
|
print('Success Fetch Data Warehouse');
|
||
|
|
||
|
List<WarehouseModel> warehouseDBList = data
|
||
|
.map((item) => WarehouseModel.fromJson(item as Map<String, dynamic>))
|
||
|
.toList();
|
||
|
|
||
|
return warehouseDBList;
|
||
|
} else {
|
||
|
throw Exception('Failed to fetch data from API Warehouse');
|
||
|
}
|
||
|
}
|
||
|
|
||
|
Future<List<CustomerModel>> fetchCustomerFromApi() async {
|
||
|
final apiURL = '${await getBaseUrl()}/m-customer';
|
||
|
|
||
|
final response = await http.get(Uri.parse(apiURL));
|
||
|
|
||
|
if (response.statusCode == 200) {
|
||
|
List<dynamic> data = json.decode(response.body)['data']['customers'];
|
||
|
print('Success Fetch Data Customer');
|
||
|
|
||
|
List<CustomerModel> customerDBList = data
|
||
|
.map((item) => CustomerModel.fromJson(item as Map<String, dynamic>))
|
||
|
.toList();
|
||
|
|
||
|
return customerDBList;
|
||
|
} else {
|
||
|
throw Exception('Failed to fetch data from API Customer');
|
||
|
}
|
||
|
}
|
||
|
|
||
|
Future<List<TypePetiModel>> fetchTipePetiFromApi() async {
|
||
|
final apiURL = '${await getBaseUrl()}/m-type-peti';
|
||
|
|
||
|
final response = await http.get(Uri.parse(apiURL));
|
||
|
|
||
|
if (response.statusCode == 200) {
|
||
|
List<dynamic> data = json.decode(response.body)['data']['tipe_peti'];
|
||
|
print('Success Fetch Data Tipe Peti');
|
||
|
|
||
|
List<TypePetiModel> tipePetiDBList = data
|
||
|
.map((item) => TypePetiModel.fromJson(item as Map<String, dynamic>))
|
||
|
.toList();
|
||
|
|
||
|
return tipePetiDBList;
|
||
|
} else {
|
||
|
throw Exception('Failed to fetch data from API Tipe Peti');
|
||
|
}
|
||
|
}
|
||
|
|
||
|
Future<List<ConditionPetiModel>> fetchKondisiPetiFromApi() async {
|
||
|
final apiURL = '${await getBaseUrl()}/m-kondisi-peti';
|
||
|
|
||
|
final response = await http.get(Uri.parse(apiURL));
|
||
|
|
||
|
if (response.statusCode == 200) {
|
||
|
List<dynamic> data = json.decode(response.body)['data']['kondisi_peti'];
|
||
|
print('Success Fetch Data Master Kondisi Peti');
|
||
|
|
||
|
List<ConditionPetiModel> kondisiPetiDBList = data
|
||
|
.map((item) =>
|
||
|
ConditionPetiModel.fromJson(item as Map<String, dynamic>))
|
||
|
.toList();
|
||
|
|
||
|
return kondisiPetiDBList;
|
||
|
} else {
|
||
|
throw Exception('Failed to fetch data from API Master Kondisi Peti');
|
||
|
}
|
||
|
}
|
||
|
|
||
|
Future<List<TransferPetiModel>> fetchTransferPetiFromApi() async {
|
||
|
final apiURL = '${await getBaseUrl()}/m-transfer-peti';
|
||
|
|
||
|
final response = await http.get(Uri.parse(apiURL));
|
||
|
|
||
|
if (response.statusCode == 200) {
|
||
|
List<dynamic> data = json.decode(response.body)['data']['transfer_peti'];
|
||
|
print('Success Fetch Data Transfer Peti');
|
||
|
|
||
|
List<TransferPetiModel> transferPetiDBList = data
|
||
|
.map((item) =>
|
||
|
TransferPetiModel.fromJson(item as Map<String, dynamic>))
|
||
|
.toList();
|
||
|
|
||
|
return transferPetiDBList;
|
||
|
} else {
|
||
|
throw Exception('Failed to fetch data from API Transfer Peti');
|
||
|
}
|
||
|
}
|
||
|
|
||
|
Future<List<DisposalPetiModel>> fetchDisposalFromApi() async {
|
||
|
final apiURL = '${await getBaseUrl()}/m-disposal-peti';
|
||
|
|
||
|
final response = await http.get(Uri.parse(apiURL));
|
||
|
|
||
|
if (response.statusCode == 200) {
|
||
|
List<dynamic> data = json.decode(response.body)['data']['disposals'];
|
||
|
print('Success Fetch Data Disposal Peti');
|
||
|
|
||
|
List<DisposalPetiModel> disposalPetiDBList = data
|
||
|
.map((item) =>
|
||
|
DisposalPetiModel.fromJson(item as Map<String, dynamic>))
|
||
|
.toList();
|
||
|
|
||
|
return disposalPetiDBList;
|
||
|
} else {
|
||
|
throw Exception('Failed to fetch data from API Disposal Peti');
|
||
|
}
|
||
|
}
|
||
|
}
|