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.
 
 
 
 
 
 

372 lines
12 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();
}
// 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 ------------------------------------------------------------------------------------------------------------------
}