Advertisement
Nurrohman_rex

Untitled

Mar 3rd, 2025
191
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Dart 5.30 KB | None | 0 0
  1. Future<RefillSync> syncRefill(
  2.       {@required String? kodeCustomer,
  3.       @required String? noJadwal,
  4.       @required String? noRealisasi,
  5.       @required int? finishRefill,
  6.       @required bool? syncInventory}) async {
  7.     SharedPreferences prefs = await SharedPreferences.getInstance();
  8.     String? kodeUser = prefs.getString('kodeUser');
  9.     String? token = prefs.getString('token');
  10.  
  11.     RefillSync response = RefillSync();
  12.  
  13.     try {
  14.       int offset = 0;
  15.       int limit = 50;
  16.  
  17.       var dbClient = await db;
  18.  
  19.       String getAllDetailQuery =
  20.           'SELECT * FROM $detailRealisasiTable WHERE $dNoRealisasi = \'$noRealisasi\'';
  21.       var getAllDetail = await dbClient!.rawQuery(getAllDetailQuery);
  22.  
  23.       // round up
  24.       int loopTimes = (getAllDetail.length / limit).ceil() == 0
  25.           ? 1
  26.           : (getAllDetail.length / limit).ceil();
  27.       print('SYNC REFILL CALLED - LOOPTIMES $loopTimes');
  28.  
  29.       var syncRefill;
  30.       for (var i = 0; i < loopTimes; i++) {
  31.         String getDetailQuery = '';
  32.         print('OFFSET => $offset');
  33.         if (syncInventory!) {
  34.           getDetailQuery =
  35.               'SELECT * FROM $detailRealisasiTable WHERE $dNoRealisasi = \'$noRealisasi\' AND $dLock = \'0\' LIMIT $limit OFFSET $offset';
  36.         } else {
  37.           getDetailQuery =
  38.               'SELECT * FROM $detailRealisasiTable WHERE $dNoRealisasi = \'$noRealisasi\' AND $dConnectionMode = \'offline\' LIMIT $limit OFFSET $offset';
  39.         }
  40.  
  41.         var detailGet = await dbClient.rawQuery(getDetailQuery);
  42.         print('detailGet.length => ${detailGet.length}');
  43.  
  44.         List<Map> refillMap = [];
  45.         if (detailGet.length > 0) {
  46.           for (var j = 0; j < detailGet.length; j++) {
  47.             refillMap.add({
  48.               'barcode': detailGet[j][dBarcode],
  49.               'type': detailGet[j][dType],
  50.               'brand': detailGet[j][dBrand],
  51.               'media': detailGet[j][dMedia],
  52.               'kapasitas': detailGet[j][dKapasitas],
  53.               'kadaluarsa': detailGet[j][dKadaluarsa],
  54.               'keterangan': detailGet[j][dKeterangan]
  55.             });
  56.           }
  57.         }
  58.  
  59.         Map refillData = {'data': refillMap};
  60.  
  61.         var getDir = await getExternalStorageDirectory();
  62.         var path = '${getDir!.path}/Refill';
  63.  
  64.         String jsonString = jsonEncode(refillData);
  65.         print('jsonString => $jsonString');
  66.         print('path => $path');
  67.         var jsonPath =
  68.             await createJson(jsonString, path, 'Refill $noRealisasi $i');
  69.  
  70.         var dio = Dio();
  71.  
  72.         FormData formData = FormData.fromMap({
  73.           'kode_user': kodeUser,
  74.           'token': token,
  75.           'kode_customer': kodeCustomer,
  76.           'no_jadwal': noJadwal,
  77.           'finish_refill': finishRefill,
  78.           'sync_inventory': syncInventory ? 1 : 0,
  79.           "data": MultipartFile.fromBytes(File(jsonPath).readAsBytesSync(),
  80.               filename: '${kodeCustomer!}_${DateTime.now()}'),
  81.         });
  82.  
  83.         syncRefill = await dio.post(Modal.refillSyncExtinguisherV2,
  84.             data: formData, onSendProgress: (send, rec) {
  85.           print('send : $send / receive : $rec');
  86.         }).timeout(Modal.timeout);
  87.  
  88.         print('syncRefill.msg => $syncRefill');
  89.         RefillSync decodeSyncRefill = RefillSync();
  90.         if (syncRefill != null) {
  91.           decodeSyncRefill = RefillSync.fromJson(syncRefill.data);
  92.         }
  93.  
  94.         if (decodeSyncRefill.msgCode == 200 && detailGet.isNotEmpty) {
  95.           print('update status to online');
  96.           print('detailGet[i][dBarcode => $detailGet');
  97.  
  98.           // only update header and detail status if only data sync success
  99.           if (finishRefill == 1) {
  100.             String updateHeaderQuery =
  101.                 'UPDATE $headerRealisasiTable SET $hStatus = 1, $hTglSelesai = \'${DateTime.now()}\' WHERE $hNoRealisasi = \'$noRealisasi\'';
  102.             await dbClient.rawQuery(updateHeaderQuery);
  103.           }
  104.  
  105.           String headerRefillQuery =
  106.               'SELECT * FROM $headerRealisasiTable WHERE $hNoRealisasi = \'$noRealisasi\'';
  107.           var headerRefill = await dbClient.rawQuery(headerRefillQuery);
  108.           if (headerRefill.isNotEmpty) {
  109.             if (headerRefill[0][hConnectionMode] == 'offline' &&
  110.                     headerRefill[0][noJadwal] == '' ||
  111.                 headerRefill[0][hConnectionMode] == 'offline' &&
  112.                     headerRefill[0][noJadwal] == null) {
  113.               String updateHeaderRefillQuery =
  114.                   'UPDATE $headerRealisasiTable SET $hConnectionMode = \'online\', $hNoJadwal = \'$noJadwal\' WHERE $hNoRealisasi = \'$noRealisasi\'';
  115.               await dbClient.rawQuery(updateHeaderRefillQuery);
  116.             }
  117.           }
  118.  
  119.           if (detailGet.length > 0) {
  120.             for (var k = 0; k < detailGet.length; k++) {
  121.               var updateStatusQuery =
  122.                   'UPDATE $detailRealisasiTable SET $dConnectionMode = \'online\' WHERE $dBarcode = \'${detailGet[k][dBarcode]}\' AND $dNoRealisasi = \'$noRealisasi\'';
  123.               await dbClient.rawQuery(updateStatusQuery);
  124.             }
  125.           }
  126.         }
  127.  
  128.         offset += limit;
  129.       }
  130.  
  131.       if (syncRefill != null) {
  132.         response = RefillSync.fromJson(syncRefill.data);
  133.       }
  134.     } catch (e) {
  135.       print('syncRefill ERR => $e');
  136.     }
  137.  
  138.     return response;
  139.   }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement