Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Future<RefillSync> syncRefill(
- {@required String? kodeCustomer,
- @required String? noJadwal,
- @required String? noRealisasi,
- @required int? finishRefill,
- @required bool? syncInventory}) async {
- SharedPreferences prefs = await SharedPreferences.getInstance();
- String? kodeUser = prefs.getString('kodeUser');
- String? token = prefs.getString('token');
- RefillSync response = RefillSync();
- try {
- int offset = 0;
- int limit = 50;
- var dbClient = await db;
- String getAllDetailQuery =
- 'SELECT * FROM $detailRealisasiTable WHERE $dNoRealisasi = \'$noRealisasi\'';
- var getAllDetail = await dbClient!.rawQuery(getAllDetailQuery);
- // round up
- int loopTimes = (getAllDetail.length / limit).ceil() == 0
- ? 1
- : (getAllDetail.length / limit).ceil();
- print('SYNC REFILL CALLED - LOOPTIMES $loopTimes');
- var syncRefill;
- for (var i = 0; i < loopTimes; i++) {
- String getDetailQuery = '';
- print('OFFSET => $offset');
- if (syncInventory!) {
- getDetailQuery =
- 'SELECT * FROM $detailRealisasiTable WHERE $dNoRealisasi = \'$noRealisasi\' AND $dLock = \'0\' LIMIT $limit OFFSET $offset';
- } else {
- getDetailQuery =
- 'SELECT * FROM $detailRealisasiTable WHERE $dNoRealisasi = \'$noRealisasi\' AND $dConnectionMode = \'offline\' LIMIT $limit OFFSET $offset';
- }
- var detailGet = await dbClient.rawQuery(getDetailQuery);
- print('detailGet.length => ${detailGet.length}');
- List<Map> refillMap = [];
- if (detailGet.length > 0) {
- for (var j = 0; j < detailGet.length; j++) {
- refillMap.add({
- 'barcode': detailGet[j][dBarcode],
- 'type': detailGet[j][dType],
- 'brand': detailGet[j][dBrand],
- 'media': detailGet[j][dMedia],
- 'kapasitas': detailGet[j][dKapasitas],
- 'kadaluarsa': detailGet[j][dKadaluarsa],
- 'keterangan': detailGet[j][dKeterangan]
- });
- }
- }
- Map refillData = {'data': refillMap};
- var getDir = await getExternalStorageDirectory();
- var path = '${getDir!.path}/Refill';
- String jsonString = jsonEncode(refillData);
- print('jsonString => $jsonString');
- print('path => $path');
- var jsonPath =
- await createJson(jsonString, path, 'Refill $noRealisasi $i');
- var dio = Dio();
- FormData formData = FormData.fromMap({
- 'kode_user': kodeUser,
- 'token': token,
- 'kode_customer': kodeCustomer,
- 'no_jadwal': noJadwal,
- 'finish_refill': finishRefill,
- 'sync_inventory': syncInventory ? 1 : 0,
- "data": MultipartFile.fromBytes(File(jsonPath).readAsBytesSync(),
- filename: '${kodeCustomer!}_${DateTime.now()}'),
- });
- syncRefill = await dio.post(Modal.refillSyncExtinguisherV2,
- data: formData, onSendProgress: (send, rec) {
- print('send : $send / receive : $rec');
- }).timeout(Modal.timeout);
- print('syncRefill.msg => $syncRefill');
- RefillSync decodeSyncRefill = RefillSync();
- if (syncRefill != null) {
- decodeSyncRefill = RefillSync.fromJson(syncRefill.data);
- }
- if (decodeSyncRefill.msgCode == 200 && detailGet.isNotEmpty) {
- print('update status to online');
- print('detailGet[i][dBarcode => $detailGet');
- // only update header and detail status if only data sync success
- if (finishRefill == 1) {
- String updateHeaderQuery =
- 'UPDATE $headerRealisasiTable SET $hStatus = 1, $hTglSelesai = \'${DateTime.now()}\' WHERE $hNoRealisasi = \'$noRealisasi\'';
- await dbClient.rawQuery(updateHeaderQuery);
- }
- String headerRefillQuery =
- 'SELECT * FROM $headerRealisasiTable WHERE $hNoRealisasi = \'$noRealisasi\'';
- var headerRefill = await dbClient.rawQuery(headerRefillQuery);
- if (headerRefill.isNotEmpty) {
- if (headerRefill[0][hConnectionMode] == 'offline' &&
- headerRefill[0][noJadwal] == '' ||
- headerRefill[0][hConnectionMode] == 'offline' &&
- headerRefill[0][noJadwal] == null) {
- String updateHeaderRefillQuery =
- 'UPDATE $headerRealisasiTable SET $hConnectionMode = \'online\', $hNoJadwal = \'$noJadwal\' WHERE $hNoRealisasi = \'$noRealisasi\'';
- await dbClient.rawQuery(updateHeaderRefillQuery);
- }
- }
- if (detailGet.length > 0) {
- for (var k = 0; k < detailGet.length; k++) {
- var updateStatusQuery =
- 'UPDATE $detailRealisasiTable SET $dConnectionMode = \'online\' WHERE $dBarcode = \'${detailGet[k][dBarcode]}\' AND $dNoRealisasi = \'$noRealisasi\'';
- await dbClient.rawQuery(updateStatusQuery);
- }
- }
- }
- offset += limit;
- }
- if (syncRefill != null) {
- response = RefillSync.fromJson(syncRefill.data);
- }
- } catch (e) {
- print('syncRefill ERR => $e');
- }
- return response;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement