addScannedAsset method

dynamic addScannedAsset(
  1. List<ScanContentModel> assets,
  2. int? serial,
  3. int? start,
  4. int? end,
)

Implementation

addScannedAsset(
    List<ScanContentModel> assets, int? serial, int? start, int? end) {
  var range = RangeScannedModel();
  var iccids = <SingleScannedModel>[];
  var scannedIccids = <int>[];
  if (selectedScanType.value == "Range") {
    range.boxRangeId = "${start!} - ${end!}";
    range.id = "$start - ${end.toString().substring(
          end.toString().length - 2,
          end.toString().length,
        )}";
  }
  assets.forEach((asset) {
    var scannedProduct = productDataScannedList.firstWhere(
      (element) => element.productCode == asset.productCode,
      orElse: () => ProductContentModel(
          iccidList: [], rangeList: [], scannedserialNums: []),
    );

    if (scannedProduct.productCode != null) {
      if (selectedScanType.value == "Single") {
        if (currentScan == ScanType.sellThru) {
          int index = productDataScannedList.indexOf(scannedProduct);
          if (productDataScannedList[index].scannedQty ==
              productDataScannedList[index].totalOrderedQuantity) {
            Helper.assetSuccessDialog(
                Get.context!,
                "Failed to add asset.",
                "Cannot add more than ordered",
                const AssetImage(
                  "assets/images/failed.png",
                  package: Constants.packageName,
                ));

            return;
          }
        }
        if (scannedProduct.scannedserialNums!.contains(asset.serialNumber)) {
          Helper.assetSuccessDialog(
              Get.context!,
              "Failed to add asset.",
              "Scanned serial number already present",
              const AssetImage(
                "assets/images/failed.png",
                package: Constants.packageName,
              ));

          return;
        } else {
          scannedIccidList.add(asset.serialNumber!.toString());
          scannedProduct.scannedserialNums!.add(asset.serialNumber!);
          scannedProduct.iccidList!
              .add(SingleScannedModel(iccid: asset.serialNumber!.toString()));
          ProductContentModel updatedProduct = scannedProduct.copyWith(
              scannedserialNums: scannedProduct.scannedserialNums,
              scannedQty: scannedProduct.scannedQty + 1,
              iccidList: scannedProduct.iccidList);
          var temp = productDataScannedList;
          int index = temp.indexOf(scannedProduct);
          temp[index] = updatedProduct;
          productDataScannedList = temp;
          Helper.assetSuccessDialog(
              Get.context!,
              "Asset added successfully",
              "Product ID: ${asset.serialNumber}",
              const AssetImage(
                "assets/images/success.png",
                package: Constants.packageName,
              ));
        }
      } else {
        var diff = end! - start!;
        bool someAssetsAlreadyScanned = false;
        for (final data in scannedProduct.scannedserialNums!) {
          if (start <= data && data <= 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;
        }

        if (currentScan == ScanType.sellThru) {
          int index = productDataScannedList.indexOf(scannedProduct);
          if (productDataScannedList[index].scannedQty + diff >
              productDataScannedList[index].totalOrderedQuantity!) {
            Helper.assetSuccessDialog(
                Get.context!,
                "Failed to add asset.",
                "Cannot add more than ordered",
                const AssetImage(
                  "assets/images/failed.png",
                  package: Constants.packageName,
                ));

            return;
          }
        }

        scannedIccids.add(asset.serialNumber!);
        iccids.add(SingleScannedModel(iccid: asset.serialNumber!.toString()));
        scannedIccidList.add(asset.serialNumber!.toString());
        if (asset.serialNumber == end) {
          scannedProduct.scannedserialNums!.addAll(scannedIccids);
          range.iccidList = iccids;
          scannedProduct.rangeList!.add(range);
          ProductContentModel updatedProduct = scannedProduct.copyWith(
              scannedserialNums: scannedProduct.scannedserialNums,
              scannedQty: scannedProduct.scannedserialNums!.length,
              rangeList: scannedProduct.rangeList);
          var temp = productDataScannedList;
          int index = temp.indexOf(scannedProduct);
          temp[index] = updatedProduct;
          productDataScannedList = temp;
          Helper.assetSuccessDialog(
              Get.context!,
              "Asset added successfully",
              "Serials: $start - $end",
              const AssetImage(
                "assets/images/success.png",
                package: Constants.packageName,
              ));
        }
      }
    } else {
      if (selectedScanType.value == "Single") {
        scannedIccidList.add(asset.serialNumber!.toString());
        scannedProduct.scannedserialNums!.add(asset.serialNumber!);
        scannedProduct.iccidList!
            .add(SingleScannedModel(iccid: asset.serialNumber!.toString()));
        ProductContentModel updatedProduct = scannedProduct.copyWith(
            itemAmount: asset.itemAmount,
            itemTax: asset.itemTax,
            productImage: asset.productImage,
            scannedserialNums: scannedProduct.scannedserialNums,
            scannedQty: 1,
            iccidList: scannedProduct.iccidList,
            totalOrderedQuantity: 0,
            id: asset.productId,
            productCode: asset.productCode,
            productName: asset.productName,
            rangeList: [],
            orderDetails: []);
        var temp = productDataScannedList;
        temp.add(updatedProduct);
        productDataScannedList = temp;
        Helper.assetSuccessDialog(
            Get.context!,
            "Asset added successfully",
            "Product ID: ${asset.serialNumber}",
            const AssetImage(
              "assets/images/success.png",
              package: Constants.packageName,
            ));
      } else {
        var diff = end! - start!;
        scannedIccids.add(asset.serialNumber!);
        iccids.add(SingleScannedModel(iccid: asset.serialNumber!.toString()));
        scannedIccidList.add(asset.serialNumber!.toString());
        if (asset.serialNumber == end) {
          scannedProduct.scannedserialNums!.addAll(scannedIccids);
          range.iccidList = iccids;
          scannedProduct.rangeList!.add(range);
          ProductContentModel updatedProduct = scannedProduct.copyWith(
              itemAmount: asset.itemAmount,
              itemTax: asset.itemTax,
              scannedserialNums: scannedProduct.scannedserialNums,
              scannedQty: scannedProduct.scannedserialNums!.length,
              rangeList: scannedProduct.rangeList,
              id: asset.productId,
              productCode: asset.productCode,
              productName: asset.productName,
              iccidList: [],
              orderDetails: [],
              totalOrderedQuantity: 0);
          var temp = productDataScannedList;
          temp.add(updatedProduct);
          productDataScannedList = temp;
          Helper.assetSuccessDialog(
              Get.context!,
              "Asset added successfully",
              "Serials: $start - $end",
              const AssetImage(
                "assets/images/success.png",
                package: Constants.packageName,
              ));
        }
      }
    }
  });
}