Siopas Inventory PETI for ISTW Mobile
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.
 
 
 
 
 
 

474 lines
15 KiB

import 'dart:async';
import 'package:connectivity_plus/connectivity_plus.dart';
import 'package:siopas/models/asset_status_model.dart';
import 'package:siopas/models/customer_model.dart';
import 'package:siopas/models/disposal_model.dart';
import 'package:siopas/models/m_asset_status_model.dart';
import 'package:siopas/models/warehouse_mode.dart';
import 'package:http/http.dart' as http;
import 'package:internet_connection_checker/internet_connection_checker.dart';
import 'package:sqflite/sqflite.dart';
import '../migrations/databasehelper.dart';
import '../models/condition_peti_model.dart';
import '../models/type_peti_model.dart';
class ControllerApi {
final conn = SqfliteDatabaseHelper.instance;
static Future<bool> isInternetApi() async {
var connectivityResult = await (Connectivity().checkConnectivity());
if (connectivityResult == ConnectivityResult.mobile) {
if (await InternetConnectionChecker().hasConnection) {
print("Mobile data detected & internet connection confirmed.");
return true;
} else {
print('No internet :( Reason:');
return false;
}
} else if (connectivityResult == ConnectivityResult.wifi) {
if (await InternetConnectionChecker().hasConnection) {
print("wifi data detected & internet connection confirmed.");
return true;
} else {
print('No internet :( Reason:');
return false;
}
} else {
print(
"Neither mobile data or WIFI detected, not internet connection found.");
return false;
}
}
// Asset Status ------------------------------------------------------------------------------------------------------------------
Future<List> fetchAssetStatusLocalControllerApi() async {
var dbclient = await conn.db;
List assetStatusApiList = [];
try {
// Ensure that the table name is correct
List<Map<String, dynamic>> maps = await dbclient!
.query(SqfliteDatabaseHelper.asset_statusesTable, orderBy: 'id DESC');
for (var item in maps) {
assetStatusApiList.add(item);
}
} catch (e) {
print('Error fetching data from SQLite: $e');
}
return assetStatusApiList;
}
Future<void> deleteAllAssetStatusDataAPI() async {
var dbClient = await conn.db;
await dbClient!.delete(SqfliteDatabaseHelper.asset_statusesTable);
}
Future<void> addAllAssetStatusDataAPI(
List<AssetStatusModel> assetStatusListApi) async {
var dbclient = await conn.db;
Batch batch = dbclient!.batch();
for (var assetStatus in assetStatusListApi) {
// Ensure that toJson() correctly converts the model to a map
batch.insert(
SqfliteDatabaseHelper.asset_statusesTable,
assetStatus.toJson(),
);
}
await batch.commit();
}
// End Asset Status ------------------------------------------------------------------------------------------------------------------
// Peti ------------------------------------------------------------------------------------------------------------------
Future<List> fetchPetiDataAPI() async {
var dbclient = await conn.db;
List petiApiList = [];
try {
// Ensure that the table name is correct
List<Map<String, dynamic>> maps = await dbclient!
.query(SqfliteDatabaseHelper.petiTable, orderBy: 'id DESC');
for (var item in maps) {
petiApiList.add(item);
}
} catch (e) {
print('Error fetching data from SQLite: $e');
}
return petiApiList;
}
Future<void> deleteAllPetiDataAPI() async {
var dbClient = await conn.db;
await dbClient!.delete(SqfliteDatabaseHelper.petiTable);
}
// Future<void> addAllPetiDataAPI(List<PetiAssetModel> petiListApi) async {
// var dbclient = await conn.db;
// Batch batch = dbclient!.batch();
// for (var peti in petiListApi) {
// // Ensure that toJson() correctly converts the model to a map
// batch.insert(
// SqfliteDatabaseHelper.petiTable,
// peti.toJson(),
// );
// }
// await batch.commit();
// }
// ini sudah benar
// Future<void> addAllPetiDataAPI(
// List<PetiAssetModel> petiListApi, int chunkSize) async {
// var dbclient = await conn.db;
// // Mulai batch
// Batch batch = dbclient!.batch();
// // Iterasi melalui data dalam chunk
// for (int i = 0; i < petiListApi.length; i += chunkSize) {
// // Ambil chunk dari data
// List<PetiAssetModel> chunk = petiListApi.skip(i).take(chunkSize).toList();
// // Insert setiap item dalam chunk ke dalam batch
// for (var peti in chunk) {
// batch.insert(
// SqfliteDatabaseHelper.petiTable,
// peti.toJson(),
// );
// }
// }
// // Commit batch
// await batch.commit();
// }
// Future<void> addAllPetiDataAPI(List<PetiAssetModel> petiListApi) async {
// var dbclient = await conn.db;
// Batch batch = dbclient!.batch();
// // Tentukan ukuran chunk yang diinginkan
// int chunkSize = 100;
// // Iterasi melalui data dalam chunk
// for (int i = 0; i < petiListApi.length; i += chunkSize) {
// // Ambil chunk dari data
// List<PetiAssetModel> chunk = petiListApi.skip(i).take(chunkSize).toList();
// // Insert setiap item dalam chunk ke dalam batch
// for (var peti in chunk) {
// batch.insert(
// SqfliteDatabaseHelper.petiTable,
// peti.toJson(),
// );
// }
// // Commit batch setelah menambahkan semua item dalam chunk
// await batch.commit();
// }
// }
Future<void> addAllPetiDataAPI(
List<PetiAssetModel> petiListApi, int chunkSize) async {
try {
String? formatDateTime(DateTime? dateTime) {
return dateTime
?.toIso8601String(); // Ubah DateTime ke dalam format ISO 8601
}
var dbclient = await conn?.db;
if (dbclient == null) {
print("Error: Database connection is null");
return;
}
await dbclient.transaction((txn) async {
Batch batch = txn.batch();
// Iterasi melalui data dalam chunk
for (int i = 0; i < petiListApi.length; i += chunkSize) {
List<PetiAssetModel> chunk =
petiListApi.skip(i).take(chunkSize).toList();
for (var peti in chunk) {
// Hanya ambil kolom yang diinginkan
var petiData = {
'id': peti.id,
'fix_lot': peti.fix_lot,
'status': peti.status,
'customer_id': peti.customer_id,
'warehouse_id': peti.warehouse_id,
'kondisipeti_id': peti.kondisipeti_id,
'deleted_at': formatDateTime(peti.deleted_at),
};
batch.insert(
SqfliteDatabaseHelper.petiTable,
petiData,
);
}
}
await batch.commit();
});
} catch (e) {
print("Error during addAllPetiDataAPI: $e");
// Handle error sesuai kebutuhan Anda
}
}
// End Peti ------------------------------------------------------------------------------------------------------------------
// Warehouse ------------------------------------------------------------------------------------------------------------------
Future<List> fetchWarehouseDataAPI() async {
var dbclient = await conn.db;
List warehouseApiList = [];
try {
// Ensure that the table name is correct
List<Map<String, dynamic>> maps = await dbclient!
.query(SqfliteDatabaseHelper.warehouseTable, orderBy: 'id DESC');
for (var item in maps) {
warehouseApiList.add(item);
}
} catch (e) {
print('Error fetching data from SQLite: $e');
}
return warehouseApiList;
}
Future<void> deleteAllWarehouseDataAPI() async {
var dbClient = await conn.db;
await dbClient!.delete(SqfliteDatabaseHelper.warehouseTable);
}
Future<void> addAllWarehouseDataAPI(
List<WarehouseModel> warehouseListApi) async {
var dbclient = await conn.db;
Batch batch = dbclient!.batch();
for (var warehouse in warehouseListApi) {
// Ensure that toJson() correctly converts the model to a map
batch.insert(
SqfliteDatabaseHelper.warehouseTable,
warehouse.toJson(),
);
}
await batch.commit();
}
// End Warehouse ------------------------------------------------------------------------------------------------------------------
// Customer ------------------------------------------------------------------------------------------------------------------
Future<List> fetchCustomerDataAPI() async {
var dbclient = await conn.db;
List customerApiList = [];
try {
// Ensure that the table name is correct
List<Map<String, dynamic>> maps = await dbclient!
.query(SqfliteDatabaseHelper.customerTable, orderBy: 'id DESC');
for (var item in maps) {
customerApiList.add(item);
}
} catch (e) {
print('Error fetching data from SQLite: $e');
}
return customerApiList;
}
Future<void> deleteAllCustomerDataAPI() async {
var dbClient = await conn.db;
await dbClient!.delete(SqfliteDatabaseHelper.customerTable);
}
Future<void> addAllCustomerDataAPI(
List<CustomerModel> customerApiList) async {
var dbclient = await conn.db;
Batch batch = dbclient!.batch();
for (var customer in customerApiList) {
// Ensure that toJson() correctly converts the model to a map
batch.insert(
SqfliteDatabaseHelper.customerTable,
customer.toJson(),
);
}
await batch.commit();
}
// End Customer ------------------------------------------------------------------------------------------------------------------
// Tipe Peti ------------------------------------------------------------------------------------------------------------------
Future<List> fetchTipePetiDataAPI() async {
var dbclient = await conn.db;
List tipePetiApiList = [];
try {
// Ensure that the table name is correct
List<Map<String, dynamic>> maps = await dbclient!
.query(SqfliteDatabaseHelper.typePetiTable, orderBy: 'id DESC');
for (var item in maps) {
tipePetiApiList.add(item);
}
} catch (e) {
print('Error fetching data from SQLite: $e');
}
return tipePetiApiList;
}
Future<void> deleteAllTipePetiDataAPI() async {
var dbClient = await conn.db;
await dbClient!.delete(SqfliteDatabaseHelper.typePetiTable);
}
Future<void> addAllTipePetiDataAPI(
List<TypePetiModel> tipePetiApiList) async {
var dbclient = await conn.db;
Batch batch = dbclient!.batch();
for (var tipePeti in tipePetiApiList) {
// Ensure that toJson() correctly converts the model to a map
batch.insert(
SqfliteDatabaseHelper.typePetiTable,
tipePeti.toJson(),
);
}
await batch.commit();
}
// End Tipe Peti ------------------------------------------------------------------------------------------------------------------
// Tipe Kondisi ------------------------------------------------------------------------------------------------------------------
// Future<List> fetchKondisiPetiDataAPI() async {
// var dbclient = await conn.db;
// List kondisiPetiApiList = [];
// try {
// // Ensure that the table name is correct
// List<Map<String, dynamic>> maps = await dbclient!
// .query(SqfliteDatabaseHelper.conditionPetiTable, orderBy: 'id DESC');
// for (var item in maps) {
// kondisiPetiApiList.add(item);
// }
// } catch (e) {
// print('Error fetching data from SQLite: $e');
// }
// return kondisiPetiApiList;
// }
Future<List> fetchKondisiPetiDataAPI() async {
var dbclient = await conn.db;
List conditionApiList = [];
try {
// Ensure that the table name is correct
List<Map<String, dynamic>> maps = await dbclient!
.query(SqfliteDatabaseHelper.conditionPetiTable, orderBy: 'id DESC');
for (var item in maps) {
conditionApiList.add(item);
}
} catch (e) {
print('Error fetching data from SQLite: $e');
}
return conditionApiList;
}
Future<void> deleteAllKondisiPetiDataAPI() async {
var dbClient = await conn.db;
await dbClient!.delete(SqfliteDatabaseHelper.conditionPetiTable);
}
Future<void> addAllKondisiPetiDataAPI(
List<ConditionPetiModel> kondisiPetiApiList) async {
var dbclient = await conn.db;
Batch batch = dbclient!.batch();
for (var kondisiPeti in kondisiPetiApiList) {
// Ensure that toJson() correctly converts the model to a map
batch.insert(
SqfliteDatabaseHelper.conditionPetiTable,
kondisiPeti.toJson(),
);
}
await batch.commit();
}
// End Kondisi Peti ------------------------------------------------------------------------------------------------------------------
// Transfer Kondisi ------------------------------------------------------------------------------------------------------------------
Future<List> fetchTransferPetiDataAPI() async {
var dbclient = await conn.db;
List transferPetiApiList = [];
try {
// Ensure that the table name is correct
List<Map<String, dynamic>> maps = await dbclient!
.query(SqfliteDatabaseHelper.transferPetiTable, orderBy: 'id DESC');
for (var item in maps) {
transferPetiApiList.add(item);
}
} catch (e) {
print('Error fetching data from SQLite: $e');
}
return transferPetiApiList;
}
Future<void> deleteAllTransferPetiDataAPI() async {
var dbClient = await conn.db;
await dbClient!.delete(SqfliteDatabaseHelper.transferPetiTable);
}
Future<void> addAllTransferPetiDataAPI(
List<ConditionPetiModel> transferPetiApiList) async {
var dbclient = await conn.db;
Batch batch = dbclient!.batch();
for (var transferPeti in transferPetiApiList) {
// Ensure that toJson() correctly converts the model to a map
batch.insert(
SqfliteDatabaseHelper.transferPetiTable,
transferPeti.toJson(),
);
}
await batch.commit();
}
// End Transfer Peti ------------------------------------------------------------------------------------------------------------------
// Disposal ------------------------------------------------------------------------------------------------------------------
Future<List> fetchDisposalDataAPI() async {
var dbclient = await conn.db;
List disposalApiList = [];
try {
// Ensure that the table name is correct
List<Map<String, dynamic>> maps = await dbclient!
.query(SqfliteDatabaseHelper.disposalTable, orderBy: 'id DESC');
for (var item in maps) {
disposalApiList.add(item);
}
} catch (e) {
print('Error fetching data from SQLite: $e');
}
return disposalApiList;
}
Future<void> deleteAllDisposalDataAPI() async {
var dbClient = await conn.db;
await dbClient!.delete(SqfliteDatabaseHelper.disposalTable);
}
Future<void> addAllDisposalDataAPI(
List<DisposalPetiModel> disposalApiList) async {
var dbclient = await conn.db;
Batch batch = dbclient!.batch();
for (var disposal in disposalApiList) {
// Ensure that toJson() correctly converts the model to a map
batch.insert(
SqfliteDatabaseHelper.disposalTable,
disposal.toJson(),
);
}
await batch.commit();
}
// End Disposal ------------------------------------------------------------------------------------------------------------------
}