prepareCollectPaymentRequestSubmit method

Future<CollectPaymentRequest> prepareCollectPaymentRequestSubmit(
  1. PaymentData data,
  2. Map<String, String> selectedDates,
  3. Map<String, TextEditingController> controllers,
  4. Map<String, ValueList> dropdownValues,
  5. String currency,
  6. String selectFile,
)

Implementation

Future<CollectPaymentRequest> prepareCollectPaymentRequestSubmit(
    PaymentData data,
    Map<String, String> selectedDates,
    Map<String, TextEditingController> controllers,
    Map<String, ValueList> dropdownValues,
    String currency,
    String selectFile) async {
  List<CollectPayment> collection = [];
  var identifierMode = "";
  Map<String, dynamic> enteredData = {};
  identifierMode = data.id.toString();
  var amount = "";
  for (var attr in data.attributes!) {
    if (attr.fieldType == "DATE_PICKER") {
      enteredData[attr.name ?? ""] = selectedDates[attr.label] ?? "";
      CollectPayment paymentCollection = CollectPayment();
      paymentCollection.label = attr.label;
      paymentCollection.fieldType = attr.fieldType;
      paymentCollection.id = attr.id;
      paymentCollection.fieldMappingKey = attr.fieldMappingKey;
      paymentCollection.value = selectedDates[attr.label] ?? "";
      paymentCollection.formatAmount = false;
      paymentCollection.amount = false;
      collection.add(paymentCollection);
    } else if (attr.fieldType == "DROPDOWN") {
      enteredData[attr.name ?? ""] = dropdownValues[attr.label]!.label ?? "";
      CollectPayment paymentCollection = CollectPayment();
      paymentCollection.label = attr.label;
      paymentCollection.fieldType = attr.fieldType;
      paymentCollection.id = attr.id;
      paymentCollection.fieldMappingKey = attr.fieldMappingKey;
      paymentCollection.value = dropdownValues[attr.label]?.value ?? "";
      paymentCollection.formatAmount = false;
      paymentCollection.amount = false;
      collection.add(paymentCollection);
    } else {
      CollectPayment paymentCollection = CollectPayment();
      enteredData[attr.name ?? ""] = controllers[attr.label]?.text ?? "";

      if (attr.label != null &&
          attr.label!.toLowerCase().contains("document")) {
        // CollectPayment paymentCollection = CollectPayment();
        paymentCollection.label = attr.label;
        paymentCollection.fieldType = attr.fieldType;
        paymentCollection.value = selectFile;
        paymentCollection.id = attr.id;
        paymentCollection.fieldMappingKey = attr.fieldMappingKey;
        paymentCollection.formatAmount = false;
        paymentCollection.amount = false;
        collection.add(paymentCollection);
      } else {
        // CollectPayment paymentCollection = CollectPayment();
        paymentCollection.label = attr.label;
        paymentCollection.fieldType = attr.fieldType;
        paymentCollection.id = attr.id;
        paymentCollection.fieldMappingKey = attr.fieldMappingKey;
        paymentCollection.formatAmount = false;
        paymentCollection.amount = false;
        paymentCollection.value = controllers[attr.label]?.text ?? "";
        if (attr.label!.toLowerCase().contains("mode.amount")) {
          amount = controllers[attr.label]?.text ?? "";
          currency = attr.currency ?? "USD";
          paymentCollection.formatAmount = true;
          paymentCollection.amount = true;
        }
        collection.add(paymentCollection);
      }
    }
  }
  mode.removeWhere((element) => element.id == identifierMode);
  var amountKHR =
      await callConvertCurrencyApi("USD", "KHR", amount.toString(), "");
  mode.add(submit.CollectPaymentModeList(
      id: identifierMode,
      amountPaid: submit.AmountPaid(
          amount: double.tryParse(amount), amountKHR: amountKHR),
      collection: [collection]));
  // mode.sort((a, b) => a.id!.compareTo(b.id!));
  ConditionalLogs().customLog("mode ${mode.toString()}");
  ConditionalLogs().customLog("newMode: $enteredData");
  // newMode = <submit.CollectPaymentModeList>[];
  newMode.removeWhere((element) => element.id == identifierMode);
  newMode.add(
      CollectPaymentModeList(id: identifierMode, collection: [collection]));
  // newMode.sort((a, b) => a.id!.compareTo(b.id!));
  ConditionalLogs().customLog("newmode ${mode.toString()}");
  ConditionalLogs().customLog("Entered Data: $enteredData");
  currency = currency;
  paymentCollectionRequest = CollectPaymentRequest(
    modeList: newMode,
    currency: currency ?? "USD",
  );
  ConditionalLogs().customLog("Entered Data: $paymentCollectionRequest");
  return paymentCollectionRequest;
  // Send data to API or model
}