package report_package;

import java.io.Serializable;
import java.sql.Connection;
import java.sql.SQLException;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

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.UserInfo;

@ManagedBean(name="rdFdSavingReport")
@ViewScoped
public class RdFdSavingUrgentReportBean implements Serializable
{
	ArrayList<UserInfo> rdFdList, tdsList;
	Date startDate, endDate;
	ArrayList<SelectItem> yearList;
	String type, year, sDate, eDate;
	ArrayList<BorrowerLenderInfo> list;
	double totalDebit, totalCredit, totalOpening, totalClosing;
	boolean show, showSaving, showFd;
	
	public RdFdSavingUrgentReportBean() 
	{
		startDate=new Date();
		endDate=new Date();
		
		yearList=new ArrayList<>();
		for(int i=2015; i<=2025; i++)
		{
			SelectItem tt=new SelectItem();
			
			tt.setLabel(String.valueOf(i));
			tt.setValue(String.valueOf(i));
			
			yearList.add(tt);
		}
	}
	
	public void search()
	{
		HttpSession ss=(HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
		int branchId = (int) ss.getAttribute("branchId");
		Connection conn=DataBaseConnection.JAVAConnection();
		show=true; showFd=false; showSaving=true;
		totalDebit=totalCredit=totalOpening=totalClosing=0;
		startDate.setDate(1);
		startDate.setMonth(3);
		
		endDate.setDate(31);
		endDate.setMonth(2);
		
		startDate.setYear(Integer.parseInt(year)-1901);
		endDate.setYear(Integer.parseInt(year)-1900);
		
		sDate=new SimpleDateFormat("dd-MMM-yyyy").format(startDate);
		eDate=new SimpleDateFormat("dd-MMM-yyyy").format(endDate);
		
		if(type.equals("Saving"))
		{
			list=new DataBaseReportMethods().savingAccountReport(branchId,startDate, endDate);
			for(BorrowerLenderInfo bb: list)
			{
				totalOpening+=bb.getOpeningAmt();
				totalDebit+=bb.getDebitAmt();
				totalCredit+=bb.getCreditAmt();
				totalClosing+=bb.getClosingAmt();
			}
			
			totalOpening=Double.parseDouble(new DecimalFormat("##.##").format(totalOpening));
			totalDebit=Double.parseDouble(new DecimalFormat("##.##").format(totalDebit));
			totalCredit=Double.parseDouble(new DecimalFormat("##.##").format(totalCredit));
			totalClosing=Double.parseDouble(new DecimalFormat("##.##").format(totalClosing));
		}
		else if(type.equals("RD"))
		{
			FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Not Available"));
		}
		else
		{
			tdsList=new ArrayList<>();
			show=true; showFd=true; showSaving=false;
			rdFdList=new DataBaseMethods().showAllFDUser(branchId,startDate, endDate);
			int count=1;
			for(UserInfo uu: rdFdList)
			{
				double intrest=0, openIntrest=0, days=0, opDays=0;
				if(uu.getOffice_date().before(startDate))
				{
					if(uu.getMaturity_date().before(endDate))
					{
						days = (int) ((uu.getMaturity_date().getTime() - startDate.getTime()) / (1000 * 60 * 60 * 24));
					}
					else
					{
						days = (int) ((endDate.getTime() - startDate.getTime()) / (1000 * 60 * 60 * 24));
					}
				}
				else
				{
					if(uu.getMaturity_date().before(endDate))
					{
						days = (int) ((uu.getMaturity_date().getTime() - uu.getOffice_date().getTime()) / (1000 * 60 * 60 * 24));
					}
					else
					{
						days = (int) ((endDate.getTime() - uu.getOffice_date().getTime()) / (1000 * 60 * 60 * 24));
					}
				}
				days=days+1;
				intrest=(uu.getInstallment_amount()*uu.getInterest()*days)/36500;
				// System.out.println(" Fd No. : "+uu.getUser_no()+" Days: "+days+" Intrest: "+intrest);
				
				
				/// interest calculate for opening balance
				Date dt=uu.getOffice_date();
				if(uu.getOffice_date().before(startDate))
				{
					dt.setDate(1);
					opDays = (int) ((startDate.getTime() - dt.getTime()) / (1000 * 60 * 60 * 24));
					openIntrest=(uu.getInstallment_amount()*uu.getInterest()*opDays)/36500;
					// System.out.println("Start Date: "+startDate +"Maturity Date: "+dt+" Fd No. : "+uu.getUser_no()+" opening Days: "+opDays+" opening Intrest: "+openIntrest);
				}
				
				
				
				
				
				UserInfo uu1=new UserInfo();
				
				uu1.setSno(count++);
				uu1.setEntryDateStr(new SimpleDateFormat("dd/MM/yyyy").format(uu.getOffice_date()));
				uu1.setUser_no(uu.getUser_no());
				uu1.setShareHolderNo(new DataBaseMethods().shareHolderNoByRdFdNo(uu.getUser_no()));
				uu1.setFirst_applicantName(new DataBaseMethods().firstApplicantNameBySHNo(uu1.getShareHolderNo(), conn));
				uu1.setOpeningAmt(Double.parseDouble(new DecimalFormat("##.##").format(uu.getInstallment_amount()+openIntrest)));
				uu1.setInterest(Double.parseDouble(new DecimalFormat("##.##").format(intrest)));
				uu1.setMaturity_date(uu.getMaturity_date());
				uu1.setCreditAmt(Double.parseDouble(new DecimalFormat("##.##").format(intrest)));
				uu1.setDebitAmt(Double.parseDouble(new DecimalFormat("##.##").format(new DataBaseMethods().findMatureFdBetweenDate(uu.getUser_no(), startDate, endDate))));
				uu1.setTdsAmt(new DataBaseMethods().findTdsDeductionAmountOnFd(uu.getUser_no()));
				uu1.setClosingAmt(Double.parseDouble(new DecimalFormat("##.##").format((uu1.getOpeningAmt()+uu1.getCreditAmt())-(uu1.getDebitAmt()+uu1.getTdsAmt()))));
				
				if(uu.getMaturity_date().after(endDate))
				{
					uu1.setPayoutDate(endDate);
				}
				else
				{
					uu1.setPayoutDate(uu.getMaturity_date());
				}
				
				
				uu1.setStrPayOutDate(new SimpleDateFormat("dd/MM/yyyy").format(uu1.getPayoutDate()));
				
				tdsList.add(uu1);
			}
			
			for(UserInfo bb: tdsList)
			{
				totalOpening+=bb.getOpeningAmt();
				totalDebit+=bb.getDebitAmt();
				totalCredit+=bb.getCreditAmt();
				totalClosing+=bb.getClosingAmt();
			}
			
			totalOpening=Double.parseDouble(new DecimalFormat("##.##").format(totalOpening));
			totalDebit=Double.parseDouble(new DecimalFormat("##.##").format(totalDebit));
			totalCredit=Double.parseDouble(new DecimalFormat("##.##").format(totalCredit));
			totalClosing=Double.parseDouble(new DecimalFormat("##.##").format(totalClosing));
		}
		
		try {
			conn.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public Date getStartDate() {
		return startDate;
	}

	public void setStartDate(Date startDate) {
		this.startDate = startDate;
	}

	public Date getEndDate() {
		return endDate;
	}

	public boolean isShow() {
		return show;
	}

	public void setShow(boolean show) {
		this.show = show;
	}

	public void setEndDate(Date endDate) {
		this.endDate = endDate;
	}

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

	public ArrayList<SelectItem> getYearList() {
		return yearList;
	}

	public void setYearList(ArrayList<SelectItem> yearList) {
		this.yearList = yearList;
	}

	public String getYear() {
		return year;
	}

	public void setYear(String year) {
		this.year = year;
	}

	public ArrayList<BorrowerLenderInfo> getList() {
		return list;
	}

	public void setList(ArrayList<BorrowerLenderInfo> list) {
		this.list = list;
	}

	public double getTotalDebit() {
		return totalDebit;
	}

	public void setTotalDebit(double totalDebit) {
		this.totalDebit = totalDebit;
	}

	public double getTotalCredit() {
		return totalCredit;
	}

	public void setTotalCredit(double totalCredit) {
		this.totalCredit = totalCredit;
	}

	public double getTotalOpening() {
		return totalOpening;
	}

	public void setTotalOpening(double totalOpening) {
		this.totalOpening = totalOpening;
	}

	public double getTotalClosing() {
		return totalClosing;
	}

	public void setTotalClosing(double totalClosing) {
		this.totalClosing = totalClosing;
	}

	public String getsDate() {
		return sDate;
	}

	public void setsDate(String sDate) {
		this.sDate = sDate;
	}

	public String geteDate() {
		return eDate;
	}

	public void seteDate(String eDate) {
		this.eDate = eDate;
	}

	public ArrayList<UserInfo> getRdFdList() {
		return rdFdList;
	}

	public void setRdFdList(ArrayList<UserInfo> rdFdList) {
		this.rdFdList = rdFdList;
	}

	public ArrayList<UserInfo> getTdsList() {
		return tdsList;
	}

	public void setTdsList(ArrayList<UserInfo> tdsList) {
		this.tdsList = tdsList;
	}

	public boolean isShowSaving() {
		return showSaving;
	}

	public void setShowSaving(boolean showSaving) {
		this.showSaving = showSaving;
	}

	public boolean isShowFd() {
		return showFd;
	}

	public void setShowFd(boolean showFd) {
		this.showFd = showFd;
	}
	
	
	
}
