package report_package;

import java.io.Serializable;
import java.sql.Connection;
import java.util.ArrayList;
import java.util.Date;

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.DataBaseMethods;
import main_package.DataBaseConnection;
import main_package.LoanInfo;

@ManagedBean(name="centerLoanReport")
@ViewScoped
public class CenterWiseLoanReport implements Serializable
{
	private static final long serialVersionUID = 1L;
	Date startdate=new Date(),enddate=new Date();
	ArrayList<LoanInfo> list;
	int totalHolders,sno;
	String center="0",centerName;
	ArrayList<SelectItem> centerList;
	double totalamount,filecharge,maturityValue;
	DataBaseReportMethods obj1=new DataBaseReportMethods();
	public CenterWiseLoanReport()
	{
		HttpSession ss=(HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
		int branchId = (int) ss.getAttribute("branchId");
		Connection conn=DataBaseConnection.JAVAConnection();
		centerList=new DataBaseMethods().allcenter(branchId,"all",conn);
		list = new ArrayList<>();
		double d=0.0;
		list = obj1.groupLoanListOfCenter(branchId,startdate, enddate, center);
		sno=1;
		totalHolders = list.size();
		
		for(LoanInfo info:list)
		{
			if(info.getFileCharge().isEmpty())
			{
				d = 0.0;
			}
			else
			{
				d= Double.valueOf(info.getFileCharge());
			}
			filecharge = filecharge + d;
			info.setSno(sno++);
			totalamount = totalamount + Double.valueOf(info.getLoanAmount());
		}
		try {
			conn.close();
		} catch (Exception e) {
			// TODO: handle exception
		}
		centerName();
	}
	
	public void centerName()
	{
		Connection conn =DataBaseConnection.JAVAConnection();
		if(center.equals("0"))
		{
			centerName="All";
		}
		else
		{
			centerName=new DataBaseMethods().centerNameCodeById(center,conn);
		}
		try {
			conn.close();
		} catch (Exception e) {
			// TODO: handle exception
		}
	}
	
	public void view()
	{
		HttpSession ss=(HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
		int branchId = (int) ss.getAttribute("branchId");
		list = new ArrayList<>();
		list = obj1.groupLoanListOfCenter(branchId,startdate, enddate, center);
		totalHolders = list.size();
		double d=0.0;
		sno=1;
		for(LoanInfo info:list)
		{
			if(info.getFileCharge().isEmpty())
			{
				d = 0.0;
			}
			else
			{
				d= Double.valueOf(info.getFileCharge());
			}
			filecharge = filecharge + d;
			info.setSno(sno++);
			totalamount = totalamount + Double.valueOf(info.getLoanAmount());
		}
		centerName();
	}
	
	public String print()
	{
		HttpSession ss=(HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
		ss.setAttribute("report", "loan");
		ss.setAttribute("list", list);
		ss.setAttribute("centerName", centerName);
		return "printCenterWiseLoanReport";
	}
	
	
	public double getFilecharge() {
		return filecharge;
	}
	public void setFilecharge(double filecharge) {
		this.filecharge = filecharge;
	}
	public double getMaturityValue() {
		return maturityValue;
	}
	public void setMaturityValue(double maturityValue) {
		this.maturityValue = maturityValue;
	}
	public int getTotalHolders() {
		return totalHolders;
	}
	public void setTotalHolders(int totalHolders) {
		this.totalHolders = totalHolders;
	}
	public double getTotalamount() {
		return totalamount;
	}
	public void setTotalamount(double totalamount) {
		this.totalamount = totalamount;
	}
	public Date getStartdate() {
		return startdate;
	}
	public void setStartdate(Date startdate) {
		this.startdate = startdate;
	}
	public Date getEnddate() {
		return enddate;
	}
	public void setEnddate(Date enddate) {
		this.enddate = enddate;
	}
	public ArrayList<LoanInfo> getList() {
		return list;
	}
	public void setList(ArrayList<LoanInfo> list) {
		this.list = list;
	}
	public int getSno() {
		return sno;
	}
	public void setSno(int sno) {
		this.sno = sno;
	}
	public String getCenter() {
		return center;
	}
	public void setCenter(String center) {
		this.center = center;
	}
	public ArrayList<SelectItem> getCenterList() {
		return centerList;
	}
	public void setCenterList(ArrayList<SelectItem> centerList) {
		this.centerList = centerList;
	}
	public String getCenterName() {
		return centerName;
	}
	public void setCenterName(String centerName) {
		this.centerName = centerName;
	}
}
