updateCollectStock method

Future<void> updateCollectStock(
  1. CartModel data,
  2. String? serialNum,
  3. String? serialStartRange,
  4. String? serialEndRange,
  5. String? productCode,
)

Implementation

Future<void> updateCollectStock(
    cart.CartModel data,
    String? serialNum,
    String? serialStartRange,
    String? serialEndRange,
    String? productCode) async {
  // var totalAmount = data.data!.totalRetailPrice; // subTotal
  // var totalNetAmount = data.data!.totalNetPrice; // strike through
  // double totalTax = 0.0; //
  // double ctw = 0.0; // - value
  // var salesTax = "";
  // var ctwNegativeTax = ""; // red color
  // var localCurrency = data.data!.grandTotalInLocalCurrency!.price!.currency;
  // var currency = data.data!.currency;
  // var localGrandTotal = data.data!.grandTotalInLocalCurrency!.price!.amount;
  // data.data!.taxes!.forEach((tax) {
  //   if (tax.name == "SalesTax") {
  //     salesTax = tax.name!;
  //     totalTax = double.parse(tax.totalPrice!);
  //   }
  //   if (tax.name!.toLowerCase() == "ctw") {
  //     ctwNegativeTax = tax.name!;
  //     ctw = double.parse(tax.totalPrice!);
  //   }
  // });
  // var totalAmountPaid = data.data!.grandTotal;
  var products = data.data!.products ?? [];
  RxList<ProductContentModel> temp = <ProductContentModel>[].obs;
  var tempIccids = <String>[].obs;
  if (products.isNotEmpty) {
    temp.addAll(originalProductList); // assigning order prodcut to temp.
    for (var product in products) {
      for (var element in product.assetList!) {
        tempIccids.add(element.id!);
      }
      // tempIccids.addAll(product.assetList!);
      if (temp.isEmpty) {
        // new product
        ProductContentModel productModel = ProductContentModel();
        productModel.productCode =
            product.productCode ?? product.materialCode ?? "";
        productModel.productId = product.productId ?? "";
        productModel.productImage = product.productImage ?? "";
        productModel.productDescription = product.description ?? "";
        productModel.productName = product.name ?? "";
        productModel.totalOrderedQuantity = 0;
        productModel.itemAmount =
            product.unitPrice != null ? double.parse(product.unitPrice!) : 0;
        productModel.unitSellingPrice = product.sellingUnitPrice != null
            ? double.parse(product.sellingUnitPrice!)
            : 0;
        productModel.sellingPrice = product.sellingPrice != null
            ? double.parse(product.sellingPrice!)
            : 0;
        productModel.itemTax =
            product.unitTax != null ? double.parse(product.unitTax!) : 0;
        productModel.scannedQty = product.quantity ?? 0;
        List<SingleScannedModel> iccidList = [];
        for (var element in product.assetList!) {
          iccidList.add(SingleScannedModel(
              iccid: element.id,
              purchaseOrderDate: element.purchaseOrderDate));
        }
        productModel.iccidList = iccidList;
        temp.add(productModel);
      } else {
        var isProductExists = false;
        var existingProductIndex = 0;
        for (int i = 0; i < temp.length; i++) {
          if (product.productCode == temp[i].productCode ||
              product.materialCode == temp[i].productCode) {
            isProductExists = true;
            existingProductIndex = i;
            break;
          }
        }

        if (isProductExists) {
          List<SingleScannedModel> iccidList = [];
          for (var element in product.assetList!) {
            iccidList.add(SingleScannedModel(
                iccid: element.id,
                purchaseOrderDate: element.purchaseOrderDate));
          }
          ProductContentModel productModel = ProductContentModel();
          productModel = temp[existingProductIndex].copyWith(
              productImage: product.productImage ?? "",
              productDescription: product.description ?? "",
              productId: product.productId ?? "",
              itemAmount: product.unitPrice != null
                  ? double.parse(product.unitPrice!)
                  : 0,
              itemTax: product.unitTax != null
                  ? double.parse(product.unitTax!)
                  : 0,
              sellingPrice: product.sellingPrice != null
                  ? double.parse(product.sellingPrice!)
                  : 0,
              unitSellingPrice: product.sellingUnitPrice != null
                  ? double.parse(product.sellingUnitPrice!)
                  : 0,
              iccidList: iccidList,
              rangeList: [],
              scannedQty: product.quantity ?? 0);
          temp[existingProductIndex] =
              productModel; // updating existing product on temp
        } else {
          // new product
          ProductContentModel productModel = ProductContentModel();
          productModel.productCode =
              product.productCode ?? product.materialCode ?? "";
          productModel.productImage = product.productImage ?? "";
          productModel.productId = product.productId ?? "";
          productModel.productDescription = product.description ?? "";
          productModel.productName = product.name ?? "";
          productModel.totalOrderedQuantity = 0;
          productModel.itemAmount = product.unitPrice != null
              ? double.parse(product.unitPrice!)
              : 0;
          productModel.unitSellingPrice = product.sellingUnitPrice != null
              ? double.parse(product.sellingUnitPrice!)
              : 0;
          productModel.sellingPrice = product.sellingPrice != null
              ? double.parse(product.sellingPrice!)
              : 0;
          productModel.itemTax =
              product.unitTax != null ? double.parse(product.unitTax!) : 0;
          productModel.scannedQty = product.quantity ?? 0;
          List<SingleScannedModel> iccidList = [];
          for (var element in product.assetList!) {
            iccidList.add(SingleScannedModel(
                iccid: element.id,
                purchaseOrderDate: element.purchaseOrderDate));
          }
          productModel.iccidList = iccidList;
          temp.add(productModel); // adding new product on temp
        }
      }
    }
  } else {
    temp.addAll(originalProductList);
  }
  scannedIccidList = tempIccids; // assigning all the scanned iccids;
  productDataScannedList.value = [...temp]; // assigning product list
  searchedScannedList.value = [...temp];
  // RxList<ProductContentModel> onlyScannedProducts =
  //     <ProductContentModel>[].obs;
  // productDataScannedList.forEach((element) {
  //   // validating for scanQty>0
  //   if (element.scannedQty > 0) {
  //     onlyScannedProducts.add(element);
  //   }
  // });
  String gTotal = data.data!.grandTotal ?? "0.0";
  if (products.isNotEmpty && double.parse(gTotal) > 0) {
    await callCreditLimitValidation(
        serialNum, serialStartRange, serialEndRange, productCode);
  }
}