callAddToCartApi method

Future<void> callAddToCartApi(
  1. String? serialNum,
  2. String? serialStartRange,
  3. String? serialEndRange,
  4. String? productCode,
)

Implementation

Future<void> callAddToCartApi(String? serialNum, String? serialStartRange,
    String? serialEndRange, String? productCode) async {
  BuildContext context = Get.context!;
  Helper.progressDialog(context, "Please wait..");
  AddToCartRequest body = prepareCartRequest(
    checkValue(serialNum),
    checkValue(serialStartRange),
    checkValue(serialEndRange),
    productCode,
  );
  try {
    String requestTime = "";
    String responseTime = "";
    cart.CartModel model = await ApiClient(
      baseUrl: ApiConstant.posBL,
      onFetchRequestTime: (time) {
        requestTime = time;
      },
      onFetchResponseTime: (time) {
        responseTime = time;
      },
      screenName: "productsBottomsheet",
    ).addToCart(
      body: body,
      xRoles: SecureStorageService.readSecureData(
        SecureStorageService.xUserId,
      ),
      xUserId: SecureStorageService.readSecureData(
        SecureStorageService.xUserId,
      ),
    );

    if (model.status == "POS200") {
      cartId = model.data!.cartId!;
      Helper.logEvent(
        "RESPONSE_EVENT",
        success: true,
        endPoint: Api.addToCart,
        responseDate: responseTime,
        screenName: "productsBottomsheet",
        requestDate: requestTime,
        transactionId: cartId,
      );
      discountList.value = model.data!.cartDiscountsList!;
      Helper.close();
      Helper.showAssetSnackbar(
        "Asset added successfully",
        "Product ID: ${serialNum ?? serialStartRange}",
      );
      // await Helper.assetSuccessDialog(
      //   Get.context!,
      //   "Asset added successfully",
      //   "Product ID: ${serialNum ?? serialStartRange}",
      //   const AssetImage(
      //     "assets/images/success.png",
      //     package: Constants.packageName,
      //   ),
      // );
      if (currentScan == ScanType.spotSellThru) {
        await updateSpotSellThruOrder(model);
      } else if (currentScan == ScanType.sellThru) {
        await updateSellThruOrder(model);
      } else if (currentScan == ScanType.collectStock) {
        totalRetailPrice = model.data!.totalRetailPrice as double;
        await updateCollectStock(
          model,
          serialNum,
          serialStartRange,
          serialEndRange,
          productCode,
        );
      }
      isScanning.value = false;
    } else {
      Helper.close();
      if (model.errors != null) {
        Helper.messageDialog(
          Get.context!,
          model.errors![0].code ?? "Try again!!",
          model.errors![0].localeMessage ?? "Something went wrong",
          isDismissible: false,
          onTap: () {
            isScanning.value = false;
            Helper.close();
          },
        );
      }
    }
  } catch (err, stacktrace) {
    Helper.close();
    ConditionalLogs().customLog("$err");
    ConditionalLogs().customLog("$stacktrace");
    Helper.messageDialog(
      Get.context!,
      localization.translate("tryAgain"),
      localization.translate("technicalErrorMsg"),
      isDismissible: false,
      onTap: () {
        isScanning.value = false;
        Helper.close();
      },
    );
  }
}