package report_package;

import java.io.Serializable;
import java.sql.Connection;
import java.sql.SQLException;
import java.text.DecimalFormat;
import java.util.ArrayList;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.faces.model.SelectItem;
import javax.servlet.http.HttpSession;
import main_package.DataBaseConnection;
import main_package.DataBaseMethods;
import main_package.LoanInfo;

@ManagedBean(name="penaltyReport")
@ViewScoped
public class PenaltyReportBean implements Serializable
{
	boolean show, showLoan;
	double totalPenalty, totalPaid, totalDue, totalDiscount;
	String loanType, searchType;
	ArrayList<LoanInfo> list;
	ArrayList<SelectItem> loanList;
	double penaltyAmount,penaltyPaid, penaltyDue, penaltyDiscount;
	int discount; 
	DataBaseReportMethods obj1=new DataBaseReportMethods();
	DecimalFormat df=new DecimalFormat("##.##");
	
	public void checkLoan()
	{
		HttpSession ss=(HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
		int branchId = (int) ss.getAttribute("branchId");
		loanList=new DataBaseMethods().loanNoList(loanType,branchId);
		// System.out.println(loanList.size()+" row");
	}
	
	
	
	
	public void search()
	{
		HttpSession ss=(HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
		int branchId = (int) ss.getAttribute("branchId");
		Connection conn=DataBaseConnection.JAVAConnection();
		totalPenalty=totalPaid=totalDiscount=totalDue=0;
		if(searchType.equals("all"))
		{
			showLoan=false;
			list=new DataBaseMethods().penaltyOfallApproveLoanNo(branchId,loanType);
			if(list.size()>0)
			{
				show=true;
				for(LoanInfo ll: list)
				{
					totalPenalty+=ll.getPenalty();
					totalPaid+=ll.getPenaltyPaid();
					totalDue+=ll.getPenaltyDue();
					totalDiscount+=ll.getPenaltyDiscount();
				}
				
				totalPenalty=Double.parseDouble(df.format(totalPenalty));
				totalDue=Double.parseDouble(df.format(totalDue));
				totalPaid=Double.parseDouble(df.format(totalPaid));
				totalDiscount=Double.parseDouble(df.format(totalDiscount));
			}
			else
			{
				show=false;
				FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("No Record Found"));
			}
		}
		else
		{
			String loanNo=searchType;
			show=false;
			showLoan=true;
			penaltyAmount=new DataBaseMethods().calculatePenaltyAndChequeBounceCharges(loanNo, "Penalty", loanType, conn);
			penaltyPaid=new DataBaseMethods().totalPaidPenaltyAndChequeBounceCharges(loanNo, "Penalty", conn);
			
			discount=0;
			list=obj1.paidPenaltyListByLoanNo(loanNo);
			for(LoanInfo ll: list)
			{
				discount+=ll.getDiscount();
			}
			
			penaltyDue=penaltyAmount-(penaltyPaid+discount);
			
			penaltyAmount=Double.parseDouble(df.format(penaltyAmount));
			penaltyPaid=Double.parseDouble(df.format(penaltyPaid));
			penaltyDue=Double.parseDouble(df.format(penaltyDue));
		}
		
		try {
			conn.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public boolean isShow() {
		return show;
	}

	public void setShow(boolean show) {
		this.show = show;
	}

	public String getLoanType() {
		return loanType;
	}

	public void setLoanType(String loanType) {
		this.loanType = loanType;
	}

	public ArrayList<LoanInfo> getList() {
		return list;
	}

	public void setList(ArrayList<LoanInfo> list) {
		this.list = list;
	}

	public double getTotalPenalty() {
		return totalPenalty;
	}

	public void setTotalPenalty(double totalPenalty) {
		this.totalPenalty = totalPenalty;
	}

	public double getTotalPaid() {
		return totalPaid;
	}

	public void setTotalPaid(double totalPaid) {
		this.totalPaid = totalPaid;
	}

	public double getTotalDue() {
		return totalDue;
	}

	public void setTotalDue(double totalDue) {
		this.totalDue = totalDue;
	}




	public boolean isShowLoan() {
		return showLoan;
	}




	public void setShowLoan(boolean showLoan) {
		this.showLoan = showLoan;
	}




	public String getSearchType() {
		return searchType;
	}




	public void setSearchType(String searchType) {
		this.searchType = searchType;
	}




	




	public ArrayList<SelectItem> getLoanList() {
		return loanList;
	}




	public void setLoanList(ArrayList<SelectItem> loanList) {
		this.loanList = loanList;
	}




	public double getPenaltyAmount() {
		return penaltyAmount;
	}




	public void setPenaltyAmount(double penaltyAmount) {
		this.penaltyAmount = penaltyAmount;
	}




	public double getPenaltyPaid() {
		return penaltyPaid;
	}




	public void setPenaltyPaid(double penaltyPaid) {
		this.penaltyPaid = penaltyPaid;
	}




	public double getPenaltyDue() {
		return penaltyDue;
	}




	public void setPenaltyDue(double penaltyDue) {
		this.penaltyDue = penaltyDue;
	}




	public int getDiscount() {
		return discount;
	}




	public void setDiscount(int discount) {
		this.discount = discount;
	}




	public double getTotalDiscount() {
		return totalDiscount;
	}




	public void setTotalDiscount(double totalDiscount) {
		this.totalDiscount = totalDiscount;
	}




	public double getPenaltyDiscount() {
		return penaltyDiscount;
	}




	public void setPenaltyDiscount(double penaltyDiscount) {
		this.penaltyDiscount = penaltyDiscount;
	}
	
	
}
