addAssetToProduct method

void addAssetToProduct({
  1. String? singleId,
  2. SingleScannedModel? singleData,
  3. String? startRange,
  4. String? endRange,
  5. List<SingleScannedModel>? rangeAssets,
  6. String? boxId,
  7. RangeScannedModel? boxData,
})

Implementation

void addAssetToProduct({
  String? singleId,
  SingleScannedModel? singleData,
  String? startRange,
  String? endRange,
  List<SingleScannedModel>? rangeAssets,
  String? boxId,
  RangeScannedModel? boxData,
}) {
  if (singleId != null && singleData != null) {
    CollectStockCardModel? product;
    for (final data in availableProducts) {
      if (data.singleList != null) {
        for (final asset in data.singleList!) {
          if (asset.iccid == singleId) {
            product = data;
            break;
          }
        }
      }
    }
    if (product == null) {
      debugPrint("No product available for this iccid");
      Helper.assetSuccessDialog(
          Get.context!,
          "Failed to add asset.",
          "Product not found",
          const AssetImage(
            "assets/images/failed.png",
          ));

      return;
    } else {
      CollectStockCardModel currentData = CollectStockCardModel(
        productName: product.productName,
        materialCode: product.materialCode,
        singleList: [singleData],
        rangeList: [],
        orderedQty: product.orderedQty,
      );
      // product.singleList?.add(singleData);

      CollectStockCardModel? findProduct = checkProductIsAlreadyAdded(
        product.materialCode,
      );

      if (findProduct != null &&
          findProduct.scannedQty + 1 > findProduct.orderedQty!) {
        Get.showSnackbar(
          const GetSnackBar(
            title: "Error",
            message: "Product limit reached",
            duration: Duration(seconds: 1),
          ),
        );
        return;
      }

      bool status = addProductToOrderList(
        product: currentData,
        isSingle: true,
        isBox: false,
        isRange: false,
      );
      if (!status) {
        return;
      }
      scannedIccidList.add(singleId);

      if (findProduct == null) {
        scannedProducts.add(currentData);

        debugPrint("$singleId added to the ${currentData.toJson()}");
        Helper.assetSuccessDialog(
            Get.context!,
            "Asset added successfully",
            "Product ID: $singleId",
            const AssetImage(
              "assets/images/success.png",
            ));
      } else {
        // findProduct.singleList!.add(singleData);
        debugPrint("$singleId updated to ${findProduct.toJson()}");
        Helper.assetSuccessDialog(
            Get.context!,
            "Asset added successfully",
            "Product ID: $singleId",
            const AssetImage(
              "assets/images/success.png",
            ));
      }
    }
  } else if (startRange != null && endRange != null && rangeAssets != null) {
    CollectStockCardModel? product;
    for (final data in availableProducts) {
      if (data.singleList != null) {
        for (final asset in data.singleList!) {
          if (asset.iccid == startRange || asset.iccid == endRange) {
            product = data;
            break;
          }
        }
      }
    }
    if (product == null) {
      debugPrint("No product available for this iccid");
      Helper.assetSuccessDialog(
          Get.context!,
          "Failed to add asset.",
          "Product not found",
          const AssetImage(
            "assets/images/failed.png",
          ));
    } else {
      CollectStockCardModel currentData = CollectStockCardModel(
        amount: product.amount,
        productName: product.productName,
        materialCode: product.materialCode,
        scannedQty: product.scannedQty,
        deliveredQty: product.deliveredQty,
        highlightMsg: product.highlightMsg,
        isRecommended: product.isRecommended,
        isTradeMark: product.isTradeMark,
        previousScannedCount: product.previousScannedCount,
        productImage: product.productImage,
        rangeList: [
          RangeScannedModel(
            iccidList: rangeAssets,
            boxRangeId: startRange,
            id: "$startRange - ${endRange.substring(
              endRange.length - 2,
              endRange.length,
            )}",
          ),
        ],
        singleList: [],
        orderedQty: product.orderedQty,
      );
      CollectStockCardModel? findProduct = checkProductIsAlreadyAdded(
        product.materialCode,
      );
      // need to credit limit check
      // if (findProduct != null &&
      //     findProduct.scannedQty + rangeAssets.length >
      //         findProduct.orderedQty!) {
      //   Helper.assetSuccessDialog(
      //       Get.context!,
      //       "Failed to add asset.",
      //       "Product limit reached",
      //       const AssetImage(
      //         "assets/images/failed.png",
      //       ));

      //   return;
      // }
      bool status = addProductToOrderList(
        product: currentData,
        isSingle: false,
        isBox: false,
        isRange: true,
      );
      if (!status) {
        return;
      }
      for (final data in rangeAssets) {
        scannedIccidList.add(data.iccid!);
      }

      if (findProduct == null) {
        scannedProducts.add(currentData);
        debugPrint(
            "$startRange - $endRange added to the ${currentData.toJson()}");
        Helper.assetSuccessDialog(
            Get.context!,
            "Assets added successfully",
            "$startRange - $endRange",
            const AssetImage(
              "assets/images/success.png",
            ));
      } else {
        findProduct.rangeList!.add(RangeScannedModel(
          iccidList: rangeAssets,
          boxRangeId: startRange,
          id: startRange,
        ));
        int index = 0;
        for (int i = 0; i < scannedProducts.length; i++) {
          if (scannedProducts[i].materialCode == currentData.materialCode) {
            index = i;
          }
        }
        scannedProducts.removeAt(index);
        scannedProducts.insert(index, findProduct);
        Helper.assetSuccessDialog(
            Get.context!,
            "Assets added successfully",
            "$startRange - $endRange",
            const AssetImage(
              "assets/images/success.png",
            ));

        debugPrint(
            "$startRange - $endRange updated to ${findProduct.toJson()}");
      }
    }
  }
}