getStockHistory method

dynamic getStockHistory(
  1. BuildContext context,
  2. String? date,
  3. String partnerId, {
  4. String? state,
  5. String? fromDate,
  6. String? toDate,
  7. int offset = 0,
  8. String? orderId,
  9. bool isFromEmployee = false,
  10. String itemCode = "",
  11. String? type,
  12. String? dealerId,
})

Implementation

getStockHistory(
  BuildContext context,
  String? date,
  String partnerId, {
  String? state,
  String? fromDate,
  String? toDate,
  int offset = 0,
  String? orderId,
  bool isFromEmployee = false,
  String itemCode = "",
  String? type,
  String? dealerId,
}) async {
  Helper.hideKeyboard();
  if (offset == 0) {
    Helper.progressDialog(context, "Please wait..");
  }
  // printLogs(dio);
  String requestTime = DateTime.now().toString();
  try {
    var locationId = "";
    var locationValues = "";
    // String relatedParties = partnerId.join(',');
    locationId = storage.read("LOCATION_ID_TYPE") ?? "";
    String temp = storage.read("LOCATION_IDs") ?? "";
    var locationIds = [];
    if (temp.isNotEmpty) {
      locationIds = jsonDecode(temp);
      locationValues = locationIds.join(",");
    }
    var query = isFromEmployee
        ? [
            {
              "action": "or",
              "query": "category",
              "value": type ?? "sell-in-agent"
            },
            {
              "action": "or",
              "query": "relatedParty",
              "value": "ToDistributorId:$dealerId"
            },
            {
              "action": null,
              "query": "productOrderItem.productRefOrValue.itemCode",
              "value": "itemCode:$itemCode"
            },
            {
              "action": "or",
              "query": "state",
              "value": state ?? "COMPLETED,FAILED,CANCELLED"
            }
          ]
        : [
            {
              "action": "or",
              "query": "category",
              "value": type ?? "sell-in-agent,spot-sell-thru"
            },
            {
              "action": null,
              "query": "relatedParty",
              "value": "ToDistributorId:$partnerId"
            },
            {
              "action": "or",
              "query": "state",
              "value": state ?? "COMPLETED,FAILED,CANCELLED"
            },
          ];
    if (fromDate != null && toDate != null) {
      query
          .add({"query": "fromOrderDate", "value": fromDate, "action": null});
      query.add({"query": "toOrderDate", "value": toDate, "action": null});
    }

    if (orderId != null && orderId.isNotEmpty) {
      query.add({"query": "id", "value": orderId, "action": null});
    }
    dio
        .post(
      '${ApiConstant.romBaseUrl}rom-core-svc/tmf-api/productOrderingManagement/v4/elastic/productOrder',
      queryParameters: {
        'fieldName': 'orderDate',
        'sortBy': 'DESC',
        'offset': offset,
        'limit': 10,
      },
      data: json.encode({"hsQuery": query}),
      options: Options(
        headers: {
          'X-User-Id': SecureStorageService.readSecureData(
            SecureStorageService.xUserId,
          ),
          'X-User-Name': SecureStorageService.readSecureData(
            SecureStorageService.xUserName,
          ),
          'Accept-Language': storage.read("selected_language") ?? "en",
          'x-request-txn-id': UDID.uDID,
          'x-trace-id': UDID.uDIDTraceId,
          "Authorization": "Bearer ${SecureStorageService.readSecureData(
            SecureStorageService.accessToken,
          )}",
        },
      ),
    )
        .then((response) {
      UDID.setTraceId(
        response.headers.map[Constants.traceIdKey]?[0] ?? "",
      );
      Helper.logEvent(
        "RESPONSE_EVENT",
        success: true,
        endPoint:
            "rom-core-svc/tmf-api/productOrderingManagement/v4/elastic/productOrder",
        responseDate: DateTime.now().toString(),
        screenName: "stockOrderHistory",
        buttonName: "pastOrder",
        requestDate: requestTime,
      );
      isResponseReceived.value = true;
      if (response.data != null &&
          response.data.toString().trim().isNotEmpty) {
        // ConditionalLogs().customLog('getStockHistory${response.data}');
        List<dynamic> jsonList = response.data;
        List<SellInOrderHistory> myModels = jsonList
            .map((json) => SellInOrderHistory.fromJson(json))
            .toList();
        if (offset == 0) {
          getStockHistoryList.value = myModels;
        } else {
          getStockHistoryList.addAll(myModels);
        }
        if (myModels.length < 10) {
          isLoadingMore.value = true;
          isAllDataFetched.value = true;
        } else {
          isLoadingMore.value = false;
        }
        // ConditionalLogs().customLog(
        //     'getStockHistoryList1-->${getStockHistoryList.toString()}');
      }
      if (offset == 0) {
        Helper.close();
      }
    }).catchError((error) {
      ConditionalLogs().customLog("$error");
      if (offset == 0) {
        Helper.close();
      }
      isResponseReceived.value = false;
      if (error is DioException) {
        UDID.setTraceId(
          error.response?.headers.map[Constants.traceIdKey]?[0] ?? "",
        );
        Helper.logEvent(
          "ERROR_EVENT",
          failure: true,
          requestDate: requestTime,
          endPoint:
              "rom-core-svc/tmf-api/productOrderingManagement/v4/elastic/productOrder",
          responseDate: DateTime.now().toString(),
          screenName: "stockOrderHistory",
          buttonName: "pastOrder",
          error: error,
        );
      }
      MainController mainController = Get.put(MainController());
      mainController.showErrorPopup();
    });
  } catch (err, stacktrace) {
    ConditionalLogs().customLog("$stacktrace");
    Helper.close();
    if (err is DioException) {
      UDID.setTraceId(
        err.response?.headers.map[Constants.traceIdKey]?[0] ?? "",
      );
      Helper.logEvent(
        "ERROR_EVENT",
        failure: true,
        requestDate: requestTime,
        endPoint:
            "rom-core-svc/tmf-api/productOrderingManagement/v4/elastic/productOrder",
        responseDate: DateTime.now().toString(),
        screenName: "stockOrderHistory",
        buttonName: "pastOrder",
        error: err,
      );
    }
  }
}