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 ControllerPengembalian { final conn = SqfliteDatabaseHelper.instance; Future> fetchAllPengembalianInfo() async { final dbClient = await conn.db; List pengembalianList = []; try { final maps = await dbClient!.query(SqfliteDatabaseHelper.pengembalianTable); for (var item in maps) { pengembalianList.add(AssetStatusModel.fromJson(item)); } } catch (e) { print(e.toString()); } return pengembalianList; } static Future 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 addPengembalianData(AssetStatusModel pengembalianAddModel) async { var dbclient = await conn.db; int result = 0; // Provide an initial value try { result = await dbclient!.insert(SqfliteDatabaseHelper.pengembalianTable, pengembalianAddModel.toJson()); } catch (e) { print(e.toString()); } return result; } Future updatePengembalianData( AssetStatusModel pengembalianAddModel) async { var dbclient = await conn.db; late int result; try { result = await dbclient!.update( SqfliteDatabaseHelper.pengembalianTable, pengembalianAddModel.toJson(), where: 'id=?', whereArgs: [pengembalianAddModel.id], ); } catch (e) { print(e.toString()); } return result; } Future fetchPengembalianLocalController() async { var dbclient = await conn.db; List pengembalianStatusList = []; try { List> maps = await dbclient! .query(SqfliteDatabaseHelper.pengembalianTable, orderBy: 'id DESC'); for (var item in maps) { pengembalianStatusList.add(item); } } catch (e) { print(e.toString()); } return pengembalianStatusList; } Future> fetchPengembalianDataId() async { var dbclient = await conn.db; List pengembalianList = []; try { List> maps = await dbclient! .query(SqfliteDatabaseHelper.pengembalianTable, orderBy: 'id DESC'); for (var item in maps) { pengembalianList.add(AssetStatusModel.fromJson(item)); } } catch (e) { print(e.toString()); } return pengembalianList; } Future deleteAllData() async { var dbClient = await conn.db; await dbClient!.delete(SqfliteDatabaseHelper.pengembalianTable); } Future addAllData(List pengembalianList) async { var dbclient = await conn.db; Batch batch = dbclient!.batch(); for (var pengembalian in pengembalianList) { batch.insert( SqfliteDatabaseHelper.pengembalianTable, pengembalian.toJson(), ); } await batch.commit(); } Future fetchPetiData() async { var dbclient = await conn.db; List petiList = []; try { List> 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 fetchTipePetiData() async { var dbclient = await conn.db; List tipePetiList = []; try { List> 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 fetchCustomerData() async { var dbclient = await conn.db; List customerList = []; try { List> 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 fetchWarehouseData() async { var dbclient = await conn.db; List warehouseList = []; try { List> 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 fetchConditionData() async { var dbclient = await conn.db; List conditionList = []; try { List> maps = await dbclient! .query(SqfliteDatabaseHelper.conditionPetiTable, orderBy: 'id DESC'); for (var item in maps) { conditionList.add(item); } } catch (e) { print(e.toString()); } return conditionList; } Future fetchDisposalData() async { var dbclient = await conn.db; List disposalList = []; try { List> maps = await dbclient! .query(SqfliteDatabaseHelper.disposalTable, orderBy: 'id DESC'); for (var item in maps) { disposalList.add(item); } } catch (e) { print(e.toString()); } return disposalList; } }