onCollectStockScan method

Future onCollectStockScan()

Implementation

Future onCollectStockScan() async {
  if (selectedScanType.value == "Single") {
    String iccid = singleScanController.value.text;
    if (iccid.isEmpty) {
      Helper.assetSuccessDialog(
          Get.context!,
          "Failed to add asset.",
          "Enter valid iccid",
          const AssetImage(
            "assets/images/failed.png",
            package: Constants.packageName,
          ));

      return;
    }
    singleScanController.value.clear();
    if (scannedIccidList.contains(iccid) ||
        prevScannedIccidList.contains(iccid)) {
      Helper.assetSuccessDialog(
          Get.context!,
          "Failed to add asset.",
          "Id already added!",
          const AssetImage(
            "assets/images/failed.png",
            package: Constants.packageName,
          ));

      return;
    }
    SingleScannedModel? singleData;
    for (final data in availableSingleList) {
      if (data.iccid == iccid) {
        singleData = data;
        break;
      }
    }
    if (singleData == null) {
      ConditionalLogs().customLog("scanned iccid is not available");
      Helper.assetSuccessDialog(
          Get.context!,
          "Failed to add asset.",
          "Id not found",
          const AssetImage(
            "assets/images/failed.png",
            package: Constants.packageName,
          ));
    } else {
      addAssetToProduct(singleId: iccid, singleData: singleData);
    }
  } else if (selectedScanType.value == "Range") {
    String start = rangeStartScanController.value.text;
    String end = rangeEndScanController.value.text;
    if (start.isEmpty || end.isEmpty) {
      Helper.assetSuccessDialog(
          Get.context!,
          "Failed to add asset.",
          "Please enter start and end range",
          const AssetImage(
            "assets/images/failed.png",
            package: Constants.packageName,
          ));

      return;
    }
    rangeStartScanController.value.clear();
    rangeEndScanController.value.clear();
    if (int.parse(start) > int.parse(end)) {
      Helper.assetSuccessDialog(
          Get.context!,
          "Failed to add asset.",
          "End range should be greater than start range",
          const AssetImage(
            "assets/images/failed.png",
            package: Constants.packageName,
          ));

      return;
    }
    bool someAssetsAlreadyScanned = false;
    for (final data in scannedIccidList) {
      if (int.parse(start) <= int.parse(data) &&
          int.parse(data) <= int.parse(end)) {
        someAssetsAlreadyScanned = true;
        break;
      }
    }
    if (someAssetsAlreadyScanned) {
      Helper.assetSuccessDialog(
          Get.context!,
          "Failed to add asset.",
          "Please use different range, some assets are already scanned",
          const AssetImage(
            "assets/images/failed.png",
            package: Constants.packageName,
          ));
      return;
    }
    bool isStartAvailable = false;
    bool isEndAvailable = false;
    for (final data in availableSingleList) {
      if (data.iccid == start) {
        isStartAvailable = true;
        break;
      }
    }
    for (final data in availableSingleList) {
      if (data.iccid == end) {
        isEndAvailable = true;
        break;
      }
    }
    if (isStartAvailable && isEndAvailable) {
      List<SingleScannedModel> rangeItems = [];
      for (final data in availableSingleList) {
        if (int.parse(start) <= int.parse(data.iccid!) &&
            int.parse(data.iccid!) <= int.parse(end)) {
          rangeItems.add(data);
        }
      }
      addAssetToProduct(
        startRange: start,
        endRange: end,
        rangeAssets: rangeItems,
      );
    } else {
      ConditionalLogs().customLog("scanned range is not available");
      Helper.assetSuccessDialog(
          Get.context!,
          "Failed to add asset.",
          "Range not found",
          const AssetImage(
            "assets/images/failed.png",
            package: Constants.packageName,
          ));
    }
  }
  ConditionalLogs().customLog("scannedAssets : $scannedIccidList");
}