package report_package;

import java.io.Serializable;
import java.sql.Connection;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;

import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.model.SelectItem;
import javax.servlet.http.HttpSession;

import org.primefaces.context.RequestContext;

import main_package.DataBaseConnection;
import main_package.DataBaseMethods;
import main_package.DataBaseMethodsHemant;
import main_package.LoanInfo;
import main_package.UserInfo;

@ManagedBean(name="dsaWiseCollectionReport")
@ViewScoped
public class DsaWiseCollectionReportBean implements Serializable
{
	int totalEntries;
	UserInfo selected;
	ArrayList<LoanInfo> debitLoanList;
	Date startDate=new Date(), endDate=new Date();
	ArrayList<UserInfo> list, ddlistDetail,dsaAllList;
	ArrayList<UserInfo>allVal, loanDetail;
	boolean show;
	double allDdTotal,allRdTotal,allFdTotal,allPlTotal, allSlTotal,allGlTotal,allPenaltyTotal, allSavingTotal, totalAmount, totalDebitLoanAmount;
	DecimalFormat dc=new DecimalFormat("##.##"); 
	SimpleDateFormat sdfForDb=new SimpleDateFormat("yyyy-MM-dd");
	int branchId;
	ArrayList<SelectItem> planList;
	String branchUserId,planId;
	List<SelectItem> branchList = new ArrayList<SelectItem>();

	DataBaseReportMethods obj1=new DataBaseReportMethods();
	DataBaseMethodsHemant dbmh = new DataBaseMethodsHemant();
    boolean disAll;
    String userType;
	
	
	public DsaWiseCollectionReportBean()
	{
		Connection con = DataBaseConnection.JAVAConnection();
		HttpSession ss=(HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
		branchUserId = (String) ss.getAttribute("branchUserId");
		branchList = new DataBaseMethods().branchList(branchUserId,con);
		
		String cc=(String) ss.getAttribute("companyName");

		userType= (String) ss.getAttribute("type");

	    disAll= dbmh.disAllByCompanyNameUserType(userType,cc);

	 
		try {
			con.close();
		} catch (Exception e) {
			// TODO: handle exception
		}
	}
	
	public void searchReport()
		{
		
		
		if(branchId != 0)
		{
			Connection conn=DataBaseConnection.JAVAConnection();
			
			allDdTotal=allFdTotal=allGlTotal=allPlTotal=allSavingTotal=allRdTotal=0.00;
			allVal= new ArrayList<>();
		     String sDate = new SimpleDateFormat("yyyy-MM-dd").format(startDate);
			String eDate = new SimpleDateFormat("yyyy-MM-dd").format(endDate);

			
			int a=1;
			
				if(startDate.equals(endDate) || startDate.before(endDate))
				{
					show=true;
					releaseLoanData(conn);   // Debit Loan List
					list=obj1.allDsaWiseCollection(branchId,conn, sDate, eDate);
				
					for(UserInfo ff:list)
					{
						allDdTotal=allDdTotal+ff.getDdAmount();
						allRdTotal=allRdTotal+ff.getRdAmount();
						allFdTotal=allFdTotal+ff.getFdAmount();
						allGlTotal=allGlTotal+ff.getGlAmount();
						allPlTotal=allPlTotal+ff.getPlAmount();
						allSlTotal=allSlTotal+ff.getSlAmount();
						allPenaltyTotal=allPenaltyTotal+ff.getPenaltyAmount();
						allSavingTotal=allSavingTotal+ff.getSavingDepositAmount();
					}
				
					allDdTotal=Double.parseDouble(dc.format(allDdTotal));
					allRdTotal=Double.parseDouble(dc.format(allRdTotal));
					allFdTotal=Double.parseDouble(dc.format(allFdTotal));
					allGlTotal=Double.parseDouble(dc.format(allGlTotal));
					allPlTotal=Double.parseDouble(dc.format(allPlTotal));
					allSlTotal=Double.parseDouble(dc.format(allSlTotal));
					allPenaltyTotal=Double.parseDouble(dc.format(allPenaltyTotal));
					allSavingTotal=Double.parseDouble(dc.format(allSavingTotal));
				}
				else
					{
						show=false;
					}
				try {
					conn.close();
				} catch (Exception e) {
					// TODO: handle exception
				}
		}
    	else
		{
			FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Select Branch"));
		}
		
		}

	
	public void releaseLoanData(Connection conn)
	{
		debitLoanList=new ArrayList<>();
		totalDebitLoanAmount=0;
		debitLoanList=obj1.debitLoanList(branchId,"all", startDate, endDate,"all",conn);
		for(LoanInfo ll : debitLoanList)
		{
			totalDebitLoanAmount+=Double.valueOf(ll.getLoanAmount());
		}
		
	}
	
	public void ddDetail()
	{
		FacesContext context = FacesContext.getCurrentInstance();
        String schemeType = (String) UIComponent.getCurrentComponent(context).getAttributes().get("type");
        loanDetail= new ArrayList<>();
		ddlistDetail= new ArrayList<>();
		String sDate = new SimpleDateFormat("yyyy-MM-dd").format(startDate);
		String eDate = new SimpleDateFormat("yyyy-MM-dd").format(endDate);
		if(schemeType.equals("RD")||schemeType.equals("DD")||schemeType.equals("FD"))
		{
			ddlistDetail= obj1.ddDetails(branchId,sDate,eDate,selected.getDsaCode(),schemeType);
		}
		else if(schemeType.equalsIgnoreCase("penalty"))
		{
			ddlistDetail= obj1.penaltyDetails(branchId,sDate,eDate,selected.getDsaCode());
		}
		else if(schemeType.equalsIgnoreCase("saving"))
		{
			//FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Under Working"));
			ddlistDetail= obj1.savingDepositDetails(branchId,sDate,eDate,selected.getDsaCode());
		}
		else
		{
		    ddlistDetail= obj1.loanDetails(branchId,sDate,eDate,selected.getDsaCode(),schemeType);
		}
		
		totalAmount=0;
		if(ddlistDetail.size()>0)
		{
			for(UserInfo uu: ddlistDetail)
			{
				totalAmount+=uu.getAmount();
			}
		}
		
	}
	
	public void viewDSAWiseDetail()
	{
		show=true;
		String sDate =sdfForDb.format(startDate);
		String eDate =sdfForDb.format(endDate);
		
		dsaAllList= new ArrayList<>();
		ArrayList<UserInfo> ddList, rdList, fdList, plList, glList, slList, penaltyList, svList;
		ddList=new ArrayList<>();
		rdList=new ArrayList<>();
		fdList=new ArrayList<>();
		plList=new ArrayList<>();
		glList=new ArrayList<>();
		slList=new ArrayList<>();
		penaltyList=new ArrayList<>();
		svList=new ArrayList<>();
		
		ddList= obj1.ddDetails(branchId,sDate,eDate,selected.getDsaCode(),"DD");
		rdList= obj1.ddDetails(branchId,sDate,eDate,selected.getDsaCode(),"RD");
		fdList= obj1.ddDetails(branchId,sDate,eDate,selected.getDsaCode(),"FD");
		plList= obj1.loanDetails(branchId,sDate,eDate,selected.getDsaCode(),"personalloan");
		glList= obj1.loanDetails(branchId,sDate,eDate,selected.getDsaCode(),"grouploan");
		slList= obj1.loanDetails(branchId,sDate,eDate,selected.getDsaCode(),"secure_loan");
		penaltyList=obj1.penaltyDetails(branchId,sDate, eDate, selected.getDsaCode());
		svList= obj1.savingDepositDetails(branchId,sDate,eDate,selected.getDsaCode());
		//System.out.println("DD:"+ddList.size());
		//System.out.println("hiiiiiiii"+dsaAllList.size());
		dsaAllList.addAll(ddList);
		dsaAllList.addAll(rdList);
		dsaAllList.addAll(fdList);
		dsaAllList.addAll(plList);
		dsaAllList.addAll(glList);
		dsaAllList.addAll(slList);
		dsaAllList.addAll(penaltyList);
		dsaAllList.addAll(svList);
		
		Collections.sort(dsaAllList);
		for(int i=1;i<=dsaAllList.size();i++)
		{
			dsaAllList.get(i-1).setSno(i);
		}
		
		totalAmount=0;
		if(dsaAllList.size()>0)
		{
			for(UserInfo uu: dsaAllList)
			{
				totalAmount+=uu.getAmount();
			}
		}
	}
	
	public void printDetail()
	{
		HttpSession ss=(HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
		ss.setAttribute("dsaTotalAmount", totalAmount);
		ss.setAttribute("dsaCodeName", selected.getDsaCode()+" "+selected.getDsaName());
		ss.setAttribute("dsaCollectionList", ddlistDetail);
		
		RequestContext.getCurrentInstance().execute("window.open('printDsaWiseCollection.xhtml')");
	}
	
	public void planListMethod()
	{
		planList=new DataBaseMethods().allPlans(branchId,"all");
	}
	
	
	public void printAllDetail()
	{
		HttpSession ss=(HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
		ss.setAttribute("dsaTotalAmount", totalAmount);
		ss.setAttribute("dsaCodeName", selected.getDsaCode()+" "+selected.getDsaName());
		ss.setAttribute("dsaCollectionList", dsaAllList);
		
		RequestContext.getCurrentInstance().execute("window.open('printDsaWiseCollection.xhtml')");
	}
	
	public void printSelectedPlanDetail()
	{
		ArrayList<UserInfo> tempList=new ArrayList<>();
		for(UserInfo uu:dsaAllList)
		{
			if(uu.getPlanId().equals(planId))
			{
				tempList.add(uu);
			}
		}
		HttpSession ss=(HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
		ss.setAttribute("dsaCodeName", selected.getDsaCode()+" "+selected.getDsaName());
		ss.setAttribute("dsaCollectionList", tempList);
		
		RequestContext.getCurrentInstance().execute("window.open('printDsaWiseCollection.xhtml')");
	}
	
	public void printDebitLoanReport()
	{
		HttpSession ss=(HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
		ss.setAttribute("list",debitLoanList);
		ss.setAttribute("amount", totalAmount);
		ss.setAttribute("size", list.size());
		
		RequestContext.getCurrentInstance().execute("window.open('printReleasedLoanReport.xhtml')");
	}
	
	
	
	public int getTotalEntries() {
		return totalEntries;
	}

	public void setTotalEntries(int totalEntries) {
		this.totalEntries = totalEntries;
	}

	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<UserInfo> getList() {
		return list;
	}

	public void setList(ArrayList<UserInfo> list) {
		this.list = list;
	}

	public boolean isShow() {
		return show;
	}

	public void setShow(boolean show) {
		this.show = show;
	}

	public ArrayList<UserInfo> getAllVal() {
		return allVal;
	}

	public void setAllVal(ArrayList<UserInfo> allVal) {
		this.allVal = allVal;
	}

	public double getAllDdTotal() {
		return allDdTotal;
	}

	public void setAllDdTotal(double allDdTotal) {
		this.allDdTotal = allDdTotal;
	}

	public double getAllRdTotal() {
		return allRdTotal;
	}

	public void setAllRdTotal(double allRdTotal) {
		this.allRdTotal = allRdTotal;
	}

	public double getAllFdTotal() {
		return allFdTotal;
	}

	public void setAllFdTotal(double allFdTotal) {
		this.allFdTotal = allFdTotal;
	}

	public double getAllPlTotal() {
		return allPlTotal;
	}

	public void setAllPlTotal(double allPlTotal) {
		this.allPlTotal = allPlTotal;
	}

	public double getAllGlTotal() {
		return allGlTotal;
	}

	public void setAllGlTotal(double allGlTotal) {
		this.allGlTotal = allGlTotal;
	}


	public UserInfo getSelected() {
		return selected;
	}


	public void setSelected(UserInfo selected) {
		this.selected = selected;
	}


	public ArrayList<UserInfo> getDdlistDetail() {
		return ddlistDetail;
	}


	public void setDdlistDetail(ArrayList<UserInfo> ddlistDetail) {
		this.ddlistDetail = ddlistDetail;
	}


	public double getTotalAmount() {
		return totalAmount;
	}


	public void setTotalAmount(double totalAmount) {
		this.totalAmount = totalAmount;
	}


	public int getBranchId() {
		return branchId;
	}

	public void setBranchId(int branchId) {
		this.branchId = branchId;
	}

	public List<SelectItem> getBranchList() {
		return branchList;
	}

	public void setBranchList(List<SelectItem> branchList) {
		this.branchList = branchList;
	}

	public double getAllPenaltyTotal() {
		return allPenaltyTotal;
	}

	public void setAllPenaltyTotal(double allPenaltyTotal) {
		this.allPenaltyTotal = allPenaltyTotal;
	}

	public double getAllSavingTotal() {
		return allSavingTotal;
	}

	public void setAllSavingTotal(double allSavingTotal) {
		this.allSavingTotal = allSavingTotal;
	}

	public ArrayList<UserInfo> getDsaAllList() {
		return dsaAllList;
	}

	public void setDsaAllList(ArrayList<UserInfo> dsaAllList) {
		this.dsaAllList = dsaAllList;
	}

	public String getPlanId() {
		return planId;
	}

	public void setPlanId(String planId) {
		this.planId = planId;
	}

	public ArrayList<SelectItem> getPlanList() {
		return planList;
	}

	public void setPlanList(ArrayList<SelectItem> planList) {
		this.planList = planList;
	}

	public ArrayList<LoanInfo> getDebitLoanList() {
		return debitLoanList;
	}

	public void setDebitLoanList(ArrayList<LoanInfo> debitLoanList) {
		this.debitLoanList = debitLoanList;
	}

	public double getTotalDebitLoanAmount() {
		return totalDebitLoanAmount;
	}

	public void setTotalDebitLoanAmount(double totalDebitLoanAmount) {
		this.totalDebitLoanAmount = totalDebitLoanAmount;
	}

	public double getAllSlTotal() {
		return allSlTotal;
	}

	public void setAllSlTotal(double allSlTotal) {
		this.allSlTotal = allSlTotal;
	}

	public boolean isDisAll() {
		return disAll;
	}

	public void setDisAll(boolean disAll) {
		this.disAll = disAll;
	}

	public String getUserType() {
		return userType;
	}

	public void setUserType(String userType) {
		this.userType = userType;
	}

	

}
