/*
 * Copyright (c) 2005-2011 Grameen Foundation USA
 * All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
 * implied. See the License for the specific language governing
 * permissions and limitations under the License.
 *
 * See also http://www.apache.org/licenses/LICENSE-2.0.html for an
 * explanation of the license and how it is applied.
 */

package org.mifos.ui.loan.controller;

import java.math.BigDecimal;
import java.util.Arrays;

import org.joda.time.LocalDate;
import org.mifos.application.servicefacade.LoanAccountServiceFacade;
import org.mifos.dto.screen.ExpectedPaymentDto;
import org.mifos.service.BusinessRuleException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.binding.message.MessageBuilder;
import org.springframework.binding.message.MessageContext;
import org.springframework.stereotype.Controller;

@Controller(value="loanRepaymentController")
public class LoanRepaymentController {

	private final LoanAccountServiceFacade loanAccountServiceFacade;

	@Autowired
    public LoanRepaymentController(LoanAccountServiceFacade loanAccountServiceFacade) {
		this.loanAccountServiceFacade = loanAccountServiceFacade;
    }
	
	// called by spring webflow
	public ExpectedPaymentDto retrieveExpectedPayment(final String loanGlobalAccountNumber, LoanRepaymentFormBean loanRepaymentFormBean) {
	
		LocalDate paymentDueAsOf = new LocalDate();
		
		ExpectedPaymentDto result = this.loanAccountServiceFacade.retrieveExpectedPayment(loanGlobalAccountNumber, paymentDueAsOf);
		
		loanRepaymentFormBean.setGlobalAccountNumber(result.getGlobalAccountNumber());
		
		Double amount = Double.valueOf(result.getAmount().doubleValue());
		loanRepaymentFormBean.setPaymentAmount(BigDecimal.valueOf(amount));
		loanRepaymentFormBean.setPaymentDate(paymentDueAsOf);
		
		return result;
	}
	
	// called by spring webflow
	public String applyPayment(final String loanGlobalAccountNumber, LoanRepaymentFormBean loanRepaymentFormBean, MessageContext messageContext) {
		
		try {
			BigDecimal repaymentAmount = BigDecimal.valueOf(loanRepaymentFormBean.getPaymentAmount().doubleValue());
			
			this.loanAccountServiceFacade.applyLoanRepayment(loanGlobalAccountNumber, loanRepaymentFormBean.getPaymentDate(), repaymentAmount);
			return "success";
		} catch (BusinessRuleException e) {
			MessageBuilder builder = new MessageBuilder()
					.error()
					.source("paymentAmount")
					.codes(Arrays.asList(e.getMessageKey()).toArray(
							new String[1])).defaultText(e.getMessage())
					.args(e.getMessageValues());

			messageContext.addMessage(builder.build());
			
			return "error";
		}
	}
}

@@ -2423,16 +2423,42 @@
 t) %7B%0A%09%09%0A
+%09%09String returnCode = %22%22;%0A
 %09%09try %7B%0A
@@ -2713,16 +2713,22 @@
 %09%09return
+Code =
  %22succes
@@ -3062,16 +3062,22 @@
 %09%09return
+Code =
  %22error%22
@@ -3081,14 +3081,39 @@
 or%22;%0A%09%09%7D
+ %0A%09%09%0A%09%09return returnCode;
 %0A%09%7D%0A%7D%0A

fix warning