private double getTotalUnuseCreditAmount(List<ClientCreditsAndPayments> records) {
   double totalUnusedCreditAmount = 0.0;
   for (ClientCreditsAndPayments crd : records) {
     totalUnusedCreditAmount += crd.getRemaoningBalance() + crd.getAmtTouse();
   }
   return totalUnusedCreditAmount;
 }
 public void setTransactionCreditsAndPayments(
     List<ClientTransactionCreditsAndPayments> creditsAndPayments) {
   for (ClientCreditsAndPayments ccap : this.totalCredits) {
     double balance = ccap.getBalance();
     double usedAmount = 0.0;
     for (ClientTransactionCreditsAndPayments ctcap : creditsAndPayments) {
       if (ctcap.getCreditsAndPayments() == ccap.getID()) {
         usedAmount += ctcap.getAmountToUse();
       }
     }
     ccap.setRemaoningBalance(balance);
     ccap.setAmtTouse(usedAmount);
   }
   this.initialCredits = creditsAndPayments;
   initialAmountUse(totalCredits);
 }
 private void checkTotalAmount(Double totalAmount, List<ClientCreditsAndPayments> credits) {
   // if (totalAmount > amountDue) {
   // return getMessages().amountToUseMustLessthanTotal();
   // }
   for (ClientCreditsAndPayments credit : credits) {
     credit.setRemaoningBalance(credit.getRemaoningBalance() + credit.getAmtTouse());
   }
   double amountNeeded = totalAmount;
   for (ClientCreditsAndPayments credit : credits) {
     double balance = credit.getRemaoningBalance();
     double amountToUse = Math.min(amountNeeded, balance);
     credit.setAmtTouse(amountToUse);
     credit.setRemaoningBalance(credit.getRemaoningBalance() - amountToUse);
     amountNeeded -= amountToUse;
   }
   fullyApplied = amountNeeded == 0;
 }
  public void addCreditsAndPayments() {
    List<ClientCreditsAndPayments> list = getTotalCredits();
    this.totalCredits = list;
    if (getTransactionPayments() != null) {
      for (ClientTransactionReceivePayment ctrp : getTransactionPayments()) {
        for (ClientCreditsAndPayments ccap : totalCredits) {
          double balance = ccap.getBalance();
          double usedAmount = 0.0;
          for (ClientTransactionCreditsAndPayments ctcap :
              ctrp.getTransactionCreditsAndPayments()) {
            if (ctcap.getCreditsAndPayments() == ccap.getID()) {
              usedAmount += ctcap.getAmountToUse();
            }
          }
          ccap.setBalance(balance + usedAmount);
          ccap.setRemaoningBalance(balance);
          ccap.setAmtTouse(usedAmount);
        }
        ctrp.getTransactionCreditsAndPayments().clear();
      }
    }

    if (getPayBillPayments() != null) {
      for (ClientTransactionPayBill ctpb : getPayBillPayments()) {
        for (ClientCreditsAndPayments ccap : totalCredits) {
          double balance = ccap.getBalance();
          double usedAmount = 0.0;
          for (ClientTransactionCreditsAndPayments ctcap :
              ctpb.getTransactionCreditsAndPayments()) {
            if (ctcap.getCreditsAndPayments() == ccap.getID()) {
              usedAmount += ctcap.getAmountToUse();
            }
          }
          ccap.setBalance(balance + usedAmount);
        }
        ctpb.getTransactionCreditsAndPayments().clear();
      }
    }
  }
 @Override
 protected Result onFinish(Context context) {
   Double amount = getRequirement(AMOUNT_TO_USE).getValue();
   this.initialCredits.clear();
   for (ClientCreditsAndPayments ccap : totalCredits) {
     if (ccap.getAmtTouse() > 0) {
       ClientTransactionCreditsAndPayments ctcap = new ClientTransactionCreditsAndPayments();
       ctcap.setAmountToUse(ccap.getAmtTouse());
       ctcap.setCreditsAndPayments(ccap.getID());
       initialCredits.add(ctcap);
     }
     ccap.setBalance(ccap.getRemaoningBalance());
   }
   setValue(amount);
   return null;
 }