searchOrderHistory method

Future<void> searchOrderHistory(
  1. BuildContext context,
  2. String partnerId,
  3. String orderId, {
  4. String? state,
  5. String? fromDate,
  6. String? toDate,
  7. int offset = 0,
  8. bool isFromEmployee = false,
  9. String itemCode = "",
  10. String? type,
  11. String? currentState,
})

Implementation

Future<void> searchOrderHistory(
  BuildContext context,
  String partnerId,
  String orderId, {
  String? state,
  String? fromDate,
  String? toDate,
  int offset = 0,
  bool isFromEmployee = false,
  String itemCode = "",
  String? type,
  String? currentState,
}) async {
  // Helper.hideKeyboard();
  if (offset == 0) {
    // Helper.progressDialog(context, "Please wait..");
    getSearchedList.clear();
    isSearchAllDataFetched.value = false;
  }

  isSearchingOrder.value = true;
  String requestTime = DateTime.now().toString();

  try {
    var query = isFromEmployee
        ? [
            {
              "action": "or",
              "query": "category",
              "value": type ?? "sell-in-agent,spot-sell-thru"
            },
            {
              "action": "or",
              "query": "relatedParty",
              "value": "ToDistributorId:$dealerId"
            },
            {
              "action": null,
              "query": "productOrderItem.productRefOrValue.itemCode",
              "value": "itemCode:$itemCode"
            },
            {
              "action": "or",
              "query": "state",
              "value": state ??
                  "INPROGRESS,ASSESSINGCANCELLATION,COMPLETED,FAILED,CANCELLED"
            },
          ]
        : currentState != null
            ? [
                {
                  "action": "or",
                  "query": "category",
                  "value": type ?? "sell-in-agent,spot-sell-thru"
                },
                {
                  "action": null,
                  "query": "relatedParty",
                  "value": "ToDistributorId:$partnerId"
                },
                {
                  "query": "currentState",
                  "value": currentState,
                  "action": null,
                }
              ]
            : state != null
                ? [
                    {
                      "action": "or",
                      "query": "category",
                      "value": "sell-in-agent,spot-sell-thru"
                    },
                    {
                      "action": null,
                      "query": "relatedParty",
                      "value": "ToDistributorId:$partnerId"
                    },
                    {"action": "or", "query": "state", "value": state},
                  ]
                : [
                    {
                      "action": "or",
                      "query": "category",
                      "value": "sell-in-agent,spot-sell-thru"
                    },
                    {
                      "action": null,
                      "query": "relatedParty",
                      "value": "ToDistributorId:$partnerId"
                    },
                    {
                      "action": "or",
                      "query": "state",
                      "value": state ??
                          "INPROGRESS,ASSESSINGCANCELLATION,COMPLETED,FAILED,CANCELLED"
                    },
                  ];

    if (fromDate != null &&
        toDate != null &&
        fromDate.isNotEmpty &&
        toDate.isNotEmpty) {
      query
          .add({"query": "fromOrderDate", "value": fromDate, "action": null});
      query.add({"query": "toOrderDate", "value": toDate, "action": null});
    }
    if (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(
        "SEARCH_RESPONSE_EVENT",
        success: true,
        endPoint:
            "rom-core-svc/tmf-api/productOrderingManagement/v4/elastic/productOrder",
        responseDate: DateTime.now().toString(),
        screenName: "stockOrderHistory",
        buttonName: "searchOrder",
        requestDate: requestTime,
      );

      isSearchResponseReceived.value = true;
      if (response.data != null &&
          response.data.toString().trim().isNotEmpty) {
        List<dynamic> jsonList = response.data;
        List<SellInOrderHistory> searchResults = jsonList
            .map((json) => SellInOrderHistory.fromJson(json))
            .toList();

        if (offset == 0) {
          getSearchedList.value = searchResults;
        } else {
          getSearchedList.addAll(searchResults);
        }

        if (searchResults.length < 10) {
          isSearchAllDataFetched.value = true;
        }
      }

      if (offset == 0) {
        // Helper.close();
      }
      isSearchingOrder.value = false;
      update();
    }).catchError((error) {
      if (offset == 0) {
        // Helper.close();
      }
      isSearchResponseReceived.value = false;
      isSearchingOrder.value = false;
      if (error is DioException) {
        UDID.setTraceId(
            error.response?.headers.map[Constants.traceIdKey]?[0] ?? "");
        Helper.logEvent(
          "SEARCH_ERROR_EVENT",
          failure: true,
          requestDate: requestTime,
          endPoint:
              "rom-core-svc/tmf-api/productOrderingManagement/v4/elastic/productOrder",
          responseDate: DateTime.now().toString(),
          screenName: "stockOrderHistory",
          buttonName: "searchOrder",
          error: error,
        );
      }
      MainController().showErrorPopup();
      update();
    });
  } catch (err, stacktrace) {
    // Helper.close();
    isSearchingOrder.value = false;
    if (err is DioException) {
      UDID.setTraceId(
          err.response?.headers.map[Constants.traceIdKey]?[0] ?? "");
      Helper.logEvent(
        "SEARCH_ERROR_EVENT",
        failure: true,
        requestDate: requestTime,
        endPoint:
            "rom-core-svc/tmf-api/productOrderingManagement/v4/elastic/productOrder",
        responseDate: DateTime.now().toString(),
        screenName: "stockOrderHistory",
        buttonName: "searchOrder",
        error: err,
      );
    }
    update();
  }
}