|
|
|
import 'dart:async';
|
|
|
|
|
|
|
|
import 'package:connectivity_plus/connectivity_plus.dart';
|
|
|
|
import 'package:siopas/models/asset_status_model.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';
|
|
|
|
|
|
|
|
class Controller {
|
|
|
|
final conn = SqfliteDatabaseHelper.instance;
|
|
|
|
|
|
|
|
Future<List<AssetStatusModel>> fetchAllPeminjamanInfo() async {
|
|
|
|
final dbClient = await conn.db;
|
|
|
|
List<AssetStatusModel> assetStatusList = [];
|
|
|
|
try {
|
|
|
|
final maps = await dbClient!.query(SqfliteDatabaseHelper.peminjamanTable);
|
|
|
|
for (var item in maps) {
|
|
|
|
assetStatusList.add(AssetStatusModel.fromJson(item));
|
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
print(e.toString());
|
|
|
|
}
|
|
|
|
return assetStatusList;
|
|
|
|
}
|
|
|
|
|
|
|
|
static Future<bool> isInternet() 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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<int> addPeminjamanData(AssetStatusModel peminjamanAddModel) async {
|
|
|
|
var dbclient = await conn.db;
|
|
|
|
int result = 0; // Provide an initial value
|
|
|
|
try {
|
|
|
|
result = await dbclient!.insert(
|
|
|
|
SqfliteDatabaseHelper.peminjamanTable, peminjamanAddModel.toJson());
|
|
|
|
} catch (e) {
|
|
|
|
print(e.toString());
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<int> updatePeminjamanData(AssetStatusModel peminjamanAddModel) async {
|
|
|
|
var dbclient = await conn.db;
|
|
|
|
late int result;
|
|
|
|
try {
|
|
|
|
result = await dbclient!.update(
|
|
|
|
SqfliteDatabaseHelper.peminjamanTable,
|
|
|
|
peminjamanAddModel.toJson(),
|
|
|
|
where: 'id=?',
|
|
|
|
whereArgs: [peminjamanAddModel.id],
|
|
|
|
);
|
|
|
|
} catch (e) {
|
|
|
|
print(e.toString());
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<List> fetchAssetStatusLocalController() async {
|
|
|
|
var dbclient = await conn.db;
|
|
|
|
List peminjamanStatusList = [];
|
|
|
|
try {
|
|
|
|
List<Map<String, dynamic>> maps = await dbclient!
|
|
|
|
.query(SqfliteDatabaseHelper.peminjamanTable, orderBy: 'id DESC');
|
|
|
|
for (var item in maps) {
|
|
|
|
peminjamanStatusList.add(item);
|
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
print(e.toString());
|
|
|
|
}
|
|
|
|
return peminjamanStatusList;
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<List<AssetStatusModel>> fetchPeminjamanDataId() async {
|
|
|
|
var dbclient = await conn.db;
|
|
|
|
List<AssetStatusModel> peminjamanList = [];
|
|
|
|
try {
|
|
|
|
List<Map<String, dynamic>> maps = await dbclient!
|
|
|
|
.query(SqfliteDatabaseHelper.peminjamanTable, orderBy: 'id DESC');
|
|
|
|
for (var item in maps) {
|
|
|
|
peminjamanList.add(AssetStatusModel.fromJson(item));
|
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
print(e.toString());
|
|
|
|
}
|
|
|
|
return peminjamanList;
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<void> deletePeminjamanById(String id) async {
|
|
|
|
var dbclient = await conn.db;
|
|
|
|
try {
|
|
|
|
await dbclient!.delete(
|
|
|
|
SqfliteDatabaseHelper.peminjamanTable,
|
|
|
|
where: 'id = ?',
|
|
|
|
whereArgs: [id],
|
|
|
|
);
|
|
|
|
} catch (e) {
|
|
|
|
print('Gagal menghapus peminjaman dari database: $e');
|
|
|
|
throw Exception('Gagal menghapus peminjaman');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<void> deleteAllData() async {
|
|
|
|
var dbClient = await conn.db;
|
|
|
|
await dbClient!.delete(SqfliteDatabaseHelper.peminjamanTable);
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<void> addAllData(List<AssetStatusModel> peminjamanList) async {
|
|
|
|
var dbclient = await conn.db;
|
|
|
|
Batch batch = dbclient!.batch();
|
|
|
|
|
|
|
|
for (var contact in peminjamanList) {
|
|
|
|
batch.insert(
|
|
|
|
SqfliteDatabaseHelper.peminjamanTable,
|
|
|
|
contact.toJson(),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
await batch.commit();
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<List> fetchPetiData() async {
|
|
|
|
var dbclient = await conn.db;
|
|
|
|
List petiList = [];
|
|
|
|
try {
|
|
|
|
List<Map<String, dynamic>> maps = await dbclient!
|
|
|
|
.query(SqfliteDatabaseHelper.petiTable, orderBy: 'id DESC');
|
|
|
|
for (var item in maps) {
|
|
|
|
petiList.add(item);
|
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
print(e.toString());
|
|
|
|
}
|
|
|
|
return petiList;
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<List> fetchTipePetiData() async {
|
|
|
|
var dbclient = await conn.db;
|
|
|
|
List tipePetiList = [];
|
|
|
|
try {
|
|
|
|
List<Map<String, dynamic>> maps = await dbclient!
|
|
|
|
.query(SqfliteDatabaseHelper.typePetiTable, orderBy: 'id DESC');
|
|
|
|
for (var item in maps) {
|
|
|
|
tipePetiList.add(item);
|
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
print(e.toString());
|
|
|
|
}
|
|
|
|
return tipePetiList;
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<List> fetchCustomerData() async {
|
|
|
|
var dbclient = await conn.db;
|
|
|
|
List customerList = [];
|
|
|
|
try {
|
|
|
|
List<Map<String, dynamic>> maps = await dbclient!
|
|
|
|
.query(SqfliteDatabaseHelper.customerTable, orderBy: 'id DESC');
|
|
|
|
for (var item in maps) {
|
|
|
|
customerList.add(item);
|
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
print(e.toString());
|
|
|
|
}
|
|
|
|
return customerList;
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<List> fetchWarehouseData() async {
|
|
|
|
var dbclient = await conn.db;
|
|
|
|
List warehouseList = [];
|
|
|
|
try {
|
|
|
|
List<Map<String, dynamic>> maps = await dbclient!
|
|
|
|
.query(SqfliteDatabaseHelper.warehouseTable, orderBy: 'id DESC');
|
|
|
|
for (var item in maps) {
|
|
|
|
warehouseList.add(item);
|
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
print(e.toString());
|
|
|
|
}
|
|
|
|
return warehouseList;
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<List> fetchDisposalData() async {
|
|
|
|
var dbclient = await conn.db;
|
|
|
|
List disposalList = [];
|
|
|
|
try {
|
|
|
|
List<Map<String, dynamic>> maps = await dbclient!
|
|
|
|
.query(SqfliteDatabaseHelper.disposalTable, orderBy: 'id DESC');
|
|
|
|
for (var item in maps) {
|
|
|
|
disposalList.add(item);
|
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
print(e.toString());
|
|
|
|
}
|
|
|
|
return disposalList;
|
|
|
|
}
|
|
|
|
}
|