prepareCollectPaymentRequest method

Future<CollectPaymentRequest> prepareCollectPaymentRequest(
  1. double amount,
  2. double localCurrencyGrandTotal,
  3. String? localCurrency,
  4. String? currency,
)

Implementation

Future<CollectPaymentRequest> prepareCollectPaymentRequest(
    double amount,
    double localCurrencyGrandTotal,
    String? localCurrency,
    String? currency) async {
  List<CollectPayment> collection = [];
  var identifierMode = "";
  if (selectedPaymentLabel == "pos.common.payment.mode.bank.transfer") {
    paymentDataList.forEach((element) {
      if (element.label == "pos.common.payment.mode.bankTransfers.label") {
        identifierMode = element.id.toString();
        element.attributes!.forEach((attribute) {
          if (attribute.label ==
              "pos.common.payment.mode.bankTransfer.transactionamount") {
            CollectPayment paymentCollection = CollectPayment();
            // paymentCollection.amount = true;
            // paymentCollection.formatAmount = true;
            paymentCollection.label = attribute.label;
            paymentCollection.fieldMappingKey = attribute.fieldMappingKey;
            paymentCollection.fieldType = attribute.fieldType;
            paymentCollection.id = attribute.id.toString();
            paymentCollection.value = amount.toString();
            collection.add(paymentCollection);
          }
          if (attribute.label ==
              "pos.common.payment.mode.bankTransfer.transactionReferenceNumber") {
            CollectPayment paymentCollection = CollectPayment();
            // paymentCollection.amount = false;
            // paymentCollection.formatAmount = false;
            paymentCollection.label = attribute.label;
            paymentCollection.fieldMappingKey = attribute.fieldMappingKey;
            paymentCollection.fieldType = attribute.fieldType;
            // paymentCollection.id = attribute.id.toString();
            paymentCollection.value = bankAccno.value;
            collection.add(paymentCollection);
          }
          if (attribute.label ==
              "pos.common.payment.mode.bankTransfer.bankName") {
            CollectPayment paymentCollection = CollectPayment();
            // paymentCollection.amount = false;
            // paymentCollection.formatAmount = false;
            paymentCollection.label = attribute.label;
            paymentCollection.fieldMappingKey = attribute.fieldMappingKey;
            paymentCollection.fieldType = attribute.fieldType;
            paymentCollection.id = attribute.id.toString();
            paymentCollection.value = selectedBank.value;
            collection.add(paymentCollection);
          }
          // if (attribute.label ==
          //     "pos.common.payment.mode.bank.seller.account") {
          //   CollectPayment paymentCollection = CollectPayment();
          //   // paymentCollection.amount = false;
          //   // paymentCollection.formatAmount = false;
          //   paymentCollection.label = attribute.label;
          //   //  paymentCollection.fieldMappingKey = attribute.fieldSubType;
          //   paymentCollection.fieldType = attribute.fieldType;
          //   // paymentCollection.id = attribute.id.toString();
          //   paymentCollection.value = accountNo.text;
          //   collection.add(paymentCollection);
          // }
          if (attribute.label ==
              "pos.common.payment.mode.bankTransfer.bankCode") {
            CollectPayment paymentCollection = CollectPayment();
            // paymentCollection.amount = false;
            // paymentCollection.formatAmount = false;
            paymentCollection.label = attribute.label;
            paymentCollection.fieldMappingKey = attribute.fieldMappingKey;
            paymentCollection.fieldType = attribute.fieldType;
            paymentCollection.id = attribute.id.toString();
            paymentCollection.value = selectedBranch.value;
            collection.add(paymentCollection);
          }
          if (attribute.label ==
              "pos.common.payment.mode.bankTransfer.transactionReferenceNumber") {
            CollectPayment paymentCollection = CollectPayment();
            // paymentCollection.amount = false;
            // paymentCollection.formatAmount = false;
            paymentCollection.label = attribute.label;
            paymentCollection.fieldMappingKey = attribute.fieldMappingKey;
            paymentCollection.fieldType = attribute.fieldType;
            //paymentCollection.id = attribute.id.toString();
            paymentCollection.value = bankRefName.value;
            collection.add(paymentCollection);
          }

          if (attribute.label ==
              "pos.common.payment.mode.bankTransfer.transactionDateTime") {
            CollectPayment paymentCollection = CollectPayment();
            // paymentCollection.amount = false;
            // paymentCollection.formatAmount = false;
            paymentCollection.label = attribute.label;
            paymentCollection.fieldMappingKey = attribute.fieldMappingKey;
            paymentCollection.fieldType = attribute.fieldType;
            paymentCollection.id = attribute.id.toString();
            paymentCollection.value = bankDate.value;
            collection.add(paymentCollection);
          }
          if (attribute.label ==
              "pos.common.payment.mode.bankTransfer.uploadFile") {
            CollectPayment paymentCollection = CollectPayment();
            // paymentCollection.amount = false;
            // paymentCollection.formatAmount = true;
            paymentCollection.label = attribute.label;
            paymentCollection.fieldMappingKey = attribute.fieldMappingKey;
            paymentCollection.fieldType = attribute.fieldType;
            paymentCollection.id = attribute.id.toString();
            paymentCollection.value = file1Id;
            // TODO: paymentCollection.value = cashAmount.text;    files need to send
            collection.add(paymentCollection);
          }
        });
      }
    });
  }
  if (selectedPaymentLabel == "pos.common.payment.mode.cash.label") {
    paymentDataList.forEach((element) {
      if (element.label == "pos.common.payment.mode.cash.label") {
        identifierMode = element.id.toString();
        element.attributes!.forEach((attribute) {
          if (attribute.label == "pos.common.payment.mode.amount") {
            CollectPayment paymentCollection = CollectPayment();
            paymentCollection.label = attribute.label;
            paymentCollection.fieldMappingKey = attribute.fieldMappingKey;
            paymentCollection.fieldType = attribute.fieldType;
            paymentCollection.id = attribute.id.toString();
            //   paymentCollection.unique = attribute.unique ?? false;
            paymentCollection.formatAmount = true;
            paymentCollection.value = amount.toString();
            //    paymentCollection.currency = currency ?? "USD";
            paymentCollection.amount = true;
            collection.add(paymentCollection);
          }

          if (attribute.label == "pos.common.payment.mode.currencyType") {
            CollectPayment paymentCollection = CollectPayment();
            paymentCollection.amount = attribute.isAmount ?? false;
            paymentCollection.formatAmount = attribute.formatAmount ?? false;
            paymentCollection.label = attribute.label;
            paymentCollection.fieldMappingKey = attribute.fieldMappingKey;
            paymentCollection.fieldType = attribute.fieldType;
            paymentCollection.id = attribute.id;
            paymentCollection.value = currency ?? "USD";
            collection.add(paymentCollection);
          }
          if (attribute.label ==
              "pos.common.payment.mode.microPay.placeholder.exchangeRate") {
            CollectPayment paymentCollection = CollectPayment();
            paymentCollection.amount = attribute.isAmount ?? false;
            paymentCollection.formatAmount = attribute.formatAmount ?? false;
            paymentCollection.label = attribute.label;
            paymentCollection.fieldMappingKey = attribute.fieldMappingKey;
            paymentCollection.fieldType = attribute.fieldType;
            paymentCollection.id = attribute.id;
            paymentCollection.value = "";
            collection.add(paymentCollection);
          }
          if (attribute.label ==
              "pos.common.payment.mode.localCurrencyAmount") {
            CollectPayment paymentCollection = CollectPayment();
            paymentCollection.amount = attribute.isAmount ?? false;
            paymentCollection.formatAmount = attribute.formatAmount ?? false;
            paymentCollection.label = attribute.label;
            paymentCollection.fieldMappingKey = attribute.fieldMappingKey;
            paymentCollection.fieldType = attribute.fieldType;
            paymentCollection.id = attribute.id;
            paymentCollection.value = currency ?? "USD";
            collection.add(paymentCollection);
          }
        });
      }
    });
  }

  if (selectedPaymentLabel == "pos.common.payment.mode.cheque.label") {
    paymentDataList.forEach((element) {
      if (element.label == "pos.common.payment.mode.cheque.label") {
        identifierMode = element.id.toString();
        element.attributes!.forEach((attribute) {
          if (attribute.label == "pos.common.payment.mode.cheque.amount") {
            CollectPayment paymentCollection = CollectPayment();
            paymentCollection.label = attribute.label;
            paymentCollection.fieldType = attribute.fieldType;
            paymentCollection.value = amount.toString();
            collection.add(paymentCollection);
          }
          if (attribute.label == "pos.common.payment.mode.cheque.date") {
            CollectPayment paymentCollection = CollectPayment();
            paymentCollection.label = attribute.label;
            paymentCollection.fieldType = attribute.fieldType;
            paymentCollection.value = chequeDate.value.toString();
            collection.add(paymentCollection);
          }
          if (attribute.label == "pos.common.payment.mode.cheque.bank.name") {
            CollectPayment paymentCollection = CollectPayment();
            paymentCollection.label = attribute.label;
            paymentCollection.fieldType = attribute.fieldType;
            paymentCollection.value = chequebank.value.toString();
            collection.add(paymentCollection);
          }
          if (attribute.label == "pos.common.payment.mode.cheque.bank.code") {
            CollectPayment paymentCollection = CollectPayment();
            paymentCollection.label = attribute.label;
            paymentCollection.fieldType = attribute.fieldType;
            paymentCollection.value = chequeBranch.value.toString();
            collection.add(paymentCollection);
          }
          if (attribute.label ==
              "pos.common.payment.mode.cheque.payee,name") {
            CollectPayment paymentCollection = CollectPayment();
            paymentCollection.label = attribute.label;
            paymentCollection.fieldType = attribute.fieldType;
            paymentCollection.value = chequeName.value.toString();
            collection.add(paymentCollection);
          }
          if (attribute.label == "pos.common.payment.mode.cheque.number") {
            CollectPayment paymentCollection = CollectPayment();
            paymentCollection.label = attribute.label;
            paymentCollection.fieldType = attribute.fieldType;
            paymentCollection.value = chequeNumber.value.toString();
            collection.add(paymentCollection);
          }
          if (attribute.label ==
              "pos.common.payment.mode.cheque.upload.file") {
            CollectPayment paymentCollection = CollectPayment();
            paymentCollection.label = attribute.label;
            paymentCollection.fieldType = attribute.fieldType;
            paymentCollection.value = chequefile.value.toString();
            collection.add(paymentCollection);
          }
        });
      }
    });
  }

  if (selectedPaymentLabel == "pos.common.payment.mode.creditCard.label") {
    paymentDataList.forEach((element) {
      if (element.label == "pos.common.payment.mode.creditCard.label") {
        identifierMode = element.id.toString();
        element.attributes!.forEach((attribute) {
          if (attribute.label ==
              "pos.common.payment.mode.credit.card.transaction.amount") {
            CollectPayment paymentCollection = CollectPayment();
            paymentCollection.label = attribute.label;
            paymentCollection.fieldType = attribute.fieldType;
            paymentCollection.value = creditName.toString();
            collection.add(paymentCollection);
          }
          if (attribute.label ==
              "pos.common.payment.mode.credit.card.transaction.id") {
            CollectPayment paymentCollection = CollectPayment();
            paymentCollection.label = attribute.label;
            paymentCollection.fieldType = attribute.fieldType;
            paymentCollection.value = creditId.toString();
            collection.add(paymentCollection);
          }
          if (attribute.label ==
              "pos.common.payment.mode.credit.card.transaction.date") {
            CollectPayment paymentCollection = CollectPayment();
            paymentCollection.label = attribute.label;
            paymentCollection.fieldType = attribute.fieldType;
            paymentCollection.value = creditDate.toString();
            collection.add(paymentCollection);
          }
          if (attribute.label ==
              "pos.common.payment.mode.credit.card.upload.file") {
            CollectPayment paymentCollection = CollectPayment();
            paymentCollection.label = attribute.label;
            paymentCollection.fieldType = attribute.fieldType;
            paymentCollection.value = creditfile.toString();
            collection.add(paymentCollection);
          }
          if (attribute.label ==
              "pos.common.payment.mode.credit.card.transaction.amount") {
            CollectPayment paymentCollection = CollectPayment();
            paymentCollection.label = attribute.label;
            paymentCollection.fieldType = attribute.fieldType;
            paymentCollection.value = amount.toString();
            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: amount, amountKHR: amountKHR),
      collection: [collection]));
  mode.sort((a, b) => a.id!.compareTo(b.id!));
  // 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("mode cash $mode");
  ConditionalLogs().customLog("new mode cash $newMode");
  currency = currency ?? "USD";
  paymentCollectionRequest = CollectPaymentRequest(
    modeList: newMode,
    currency: currency ?? "USD",
  );
  return paymentCollectionRequest;
}