package com.fitbank.installment;

import java.math.BigDecimal;

import com.fitbank.common.helper.Constant;

public class InterestInstallmentUltimaCuota extends AbstractQuota {

    /*
     * (non-Javadoc)
     * 
     * @see com.fitbank.installment.AbstractQuota#calculate(com.fitbank.installment .InstallmentTable)
     */
    public void calculate(InstallmentTable pQuotaTable) throws Exception {
        this.adjustLastQuota = true;
        this.quotaTable = pQuotaTable;
        this.quotanumber = pQuotaTable.getBegincalculationperiod();
        super.reducedcapital = quotaTable.getAmount();
        super.end = false;
        super.calculateTotalperiod(pQuotaTable);
        int size = quotaTable.getTotalperiod();
        size = size + quotaTable.getBegincalculationperiod() - 1;
        for (int i = this.quotaTable.getBegincalculationperiod(); i <= size; i++) {
            
            if (i == size) {
                super.end = true;
            }
            super.calculatePayDate(this.quotaTable);
            super.processByCategory(false);
            this.calculateCuota(super.end);
            this.quotanumber++;
            if (super.stop) {
                break;
            }
        }// end for
    }

    /**
     * Calcula tabla de pago dado una cuota fija.
     * 
     * @throws Exception
     */
    protected void calculateCuota(boolean pEnd) throws Exception {

        BigDecimal capital = Constant.BD_ZERO;
        // Obtiene el valor de interes y comision de la cuota
        // Si se trata de la ultima cuota
        if (pEnd) {
            // Carga todo el capital reducido al capital de la cuota
            capital = this.quotaTable.getAmount();
        }
        super.addQuota(this.quotanumber, this.quotaTable.getAmount(), capital, true);
    }

}
