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.4 KiB
225 lines
7.4 KiB
1 year ago
|
import 'dart:convert';
|
||
|
|
||
|
import 'package:connectivity_plus/connectivity_plus.dart';
|
||
|
import 'package:siopas/models/transfer_peti_model.dart';
|
||
|
import 'package:internet_connection_checker/internet_connection_checker.dart';
|
||
|
import 'package:intl/intl.dart';
|
||
|
import '../../../connection/connection.dart';
|
||
|
import '../../../migrations/databasehelper.dart';
|
||
|
import '../../../models/asset_status_model.dart';
|
||
|
import 'package:http/http.dart' as http;
|
||
|
|
||
|
class SyncronizationTransferPetiData {
|
||
|
Future<int> addData(TransferPetiModel transferPetiModel) async {
|
||
|
final dbClient = await conn.db;
|
||
|
late int result;
|
||
|
try {
|
||
|
result = await dbClient!.insert(
|
||
|
SqfliteDatabaseHelper.transferPetiTable, transferPetiModel.toJson());
|
||
|
} catch (e) {
|
||
|
print('Error adding data to local SQLite: $e');
|
||
|
result = 0; // Handle the error appropriately
|
||
|
}
|
||
|
return result;
|
||
|
}
|
||
|
|
||
|
Future<List> fetchData() async {
|
||
|
var dbclient = await conn.db;
|
||
|
List transferPetiList = [];
|
||
|
try {
|
||
|
List<Map<String, dynamic>> maps = await dbclient!
|
||
|
.query(SqfliteDatabaseHelper.transferPetiTable, orderBy: 'id DESC');
|
||
|
for (var item in maps) {
|
||
|
transferPetiList.add(item);
|
||
|
}
|
||
|
} catch (e) {
|
||
|
print(e.toString());
|
||
|
}
|
||
|
return transferPetiList;
|
||
|
}
|
||
|
|
||
|
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;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
final conn = SqfliteDatabaseHelper.instance;
|
||
|
|
||
|
Future<List<TransferPetiModel>> fetchAllInfoTransferPeti() async {
|
||
|
final dbClient = await conn.db;
|
||
|
List<TransferPetiModel> transferPetiList = [];
|
||
|
try {
|
||
|
final maps =
|
||
|
await dbClient!.query(SqfliteDatabaseHelper.transferPetiTable);
|
||
|
for (var item in maps) {
|
||
|
transferPetiList.add(TransferPetiModel.fromJson(item));
|
||
|
}
|
||
|
} catch (e) {
|
||
|
print(e.toString());
|
||
|
}
|
||
|
return transferPetiList;
|
||
|
}
|
||
|
|
||
|
Future<void> deleteAllTransferPetiData() async {
|
||
|
var dbClient = await conn.db;
|
||
|
await dbClient!.delete(SqfliteDatabaseHelper.transferPetiTable);
|
||
|
}
|
||
|
|
||
|
Future saveTransferPetiServerWith(
|
||
|
List<TransferPetiModel> transferPetiLocalList) async {
|
||
|
for (var i = 0; i < transferPetiLocalList.length; i++) {
|
||
|
DateTime? parseDateTime(String? dateTimeString) {
|
||
|
if (dateTimeString == null || dateTimeString.isEmpty) {
|
||
|
return null;
|
||
|
}
|
||
|
|
||
|
try {
|
||
|
return DateTime.parse(dateTimeString);
|
||
|
} catch (e) {
|
||
|
print('Error parsing DateTime: $e');
|
||
|
return null;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// Format tanggal sesuai kebutuhan
|
||
|
String formattedCreatedAt = transferPetiLocalList[i].created_at != null
|
||
|
? DateFormat('yyyy-MM-dd HH:mm:ss.SSS')
|
||
|
.format(transferPetiLocalList[i].created_at!)
|
||
|
: DateFormat('yyyy-MM-dd HH:mm:ss.SSS').format(DateTime.now());
|
||
|
|
||
|
Map<String, dynamic> data = {
|
||
|
"mobile_id": transferPetiLocalList[i].mobile_id.toString(), // "id": "1
|
||
|
"peti_id": transferPetiLocalList[i].peti_id.toString(),
|
||
|
"name_customer": transferPetiLocalList[i].name_customer.toString(),
|
||
|
"source_warehouse":
|
||
|
transferPetiLocalList[i].source_warehouse.toString(),
|
||
|
"destination_warehouse":
|
||
|
transferPetiLocalList[i].destination_warehouse.toString(),
|
||
|
"date": transferPetiLocalList[i].date.toString(),
|
||
|
"created_by": transferPetiLocalList[i].created_by.toString(),
|
||
|
"created_at": formattedCreatedAt,
|
||
|
};
|
||
|
|
||
|
final response = await http.post(
|
||
|
Uri.parse(await getBaseUrl() + '/m-transfer-peti/store'),
|
||
|
body: data,
|
||
|
);
|
||
|
|
||
|
if (response.statusCode == 200) {
|
||
|
// print("Data uploaded successfully for index $i:");
|
||
|
// print("Response body: ${response.body}");
|
||
|
print("Saving Data saveToTransferPetiWith");
|
||
|
} else {
|
||
|
print(
|
||
|
"Failed to upload data for index $i. Status code: ${response.statusCode}");
|
||
|
print("Response body: ${response.body}");
|
||
|
}
|
||
|
}
|
||
|
|
||
|
return true; // Pengunggahan berhasil
|
||
|
}
|
||
|
|
||
|
Future<List> fetchAllPetiTransferInfo() async {
|
||
|
final dbClient = await conn.db;
|
||
|
List transferPetiList = [];
|
||
|
try {
|
||
|
final maps =
|
||
|
await dbClient!.query(SqfliteDatabaseHelper.transferPetiTable);
|
||
|
for (var item in maps) {
|
||
|
transferPetiList.add(item);
|
||
|
}
|
||
|
} catch (e) {
|
||
|
print(e.toString());
|
||
|
}
|
||
|
return transferPetiList;
|
||
|
}
|
||
|
|
||
|
Future saveToMysql(List transferPetiLocalList) async {
|
||
|
DateTime? parseDateTime(String? dateTimeString) {
|
||
|
if (dateTimeString == null || dateTimeString.isEmpty) {
|
||
|
return null;
|
||
|
}
|
||
|
|
||
|
try {
|
||
|
return DateTime.parse(dateTimeString);
|
||
|
} catch (e) {
|
||
|
print('Error parsing DateTime: $e');
|
||
|
return null;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
for (var i = 0; i < transferPetiLocalList.length; i++) {
|
||
|
// Format tanggal sesuai kebutuhan
|
||
|
String formattedCreatedAt = transferPetiLocalList[i]['created_at'] != null
|
||
|
? DateFormat('yyyy-MM-dd HH:mm:ss.SSS')
|
||
|
.format(transferPetiLocalList[i]['created_at'])
|
||
|
: DateFormat('yyyy-MM-dd HH:mm:ss.SSS').format(DateTime.now());
|
||
|
|
||
|
Map<String, dynamic> data = {
|
||
|
"mobile_id":
|
||
|
transferPetiLocalList[i]['mobile_id'].toString(), // "id": "1
|
||
|
"peti_id": transferPetiLocalList[i]['peti_id'].toString(),
|
||
|
"name_customer": transferPetiLocalList[i]['name_customer'].toString(),
|
||
|
"source_warehouse":
|
||
|
transferPetiLocalList[i]['source_warehouse'].toString(),
|
||
|
"destination_warehouse":
|
||
|
transferPetiLocalList[i]['destination_warehouse'].toString(),
|
||
|
"date": transferPetiLocalList[i]['date'].toString(),
|
||
|
"created_by":
|
||
|
transferPetiLocalList[i]['created_by'].toString(), // "id": "1
|
||
|
"created_at": formattedCreatedAt,
|
||
|
};
|
||
|
|
||
|
final response = await http.post(
|
||
|
Uri.parse(await getBaseUrl() + '/m-transfer-peti/store'),
|
||
|
body: data);
|
||
|
if (response.statusCode == 200) {
|
||
|
print(response.body);
|
||
|
print("Saving Data Transfer Peti");
|
||
|
} else {
|
||
|
print(response.statusCode);
|
||
|
print(
|
||
|
"Failed to upload data for index $i. Status code: ${response.statusCode}");
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
Future<List<TransferPetiModel>> fetchTransferPetiFromApi() async {
|
||
|
final response = await http.get(
|
||
|
Uri.parse(await getBaseUrl() + '/m-transfer-peti'),
|
||
|
);
|
||
|
|
||
|
if (response.statusCode == 200) {
|
||
|
List<dynamic> data = json.decode(response.body)['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');
|
||
|
}
|
||
|
}
|
||
|
}
|