package report_package;

import group_head_package.IncomeList;

import java.io.Serializable;
import java.sql.Connection;
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.servlet.http.HttpSession;

import main_package.DataBaseConnection;
import main_package.UserInfo;

import org.primefaces.context.RequestContext;

@ManagedBean(name="incomeExpenseReport")
@ViewScoped

public class IncomeExpenseReportBean implements Serializable
{DataBaseReportMethods obj1=new DataBaseReportMethods();
	private static final long serialVersionUID = 1L;
	Date startDate,endDate;
	String reportType;
	boolean show,showAmount,showOtherAmount,showReportTable,showFixedDepositTable;
	ArrayList<UserInfo> finalList,incomeList,expenseList,assetList,depositList,loanPaymentList,shareAmntList,rdPayList,applicantFeeList;
	ArrayList<UserInfo> fileChargeGrpList,fileChargeVhclList,fileChargePrprtyList,fileChargePrsnlList,returnAmountList,extraExpenseList;
	ArrayList<UserInfo> withdrawlList,loanAmountGrpList,loanAmountVhclList,loanAmountPrprtyList,loanAmountPrsnlList,extraIncomeList;
	ArrayList<UserInfo> fixedAssetList,fixedDepositIncomeList,fixedDepositExpenseList;
	ArrayList<IncomeList> finalFixedDepositIncomeList;
	
	double totalAmount=0,penalty,totalIncome=0,totalExpense=0,totalAsset=0;
	int totalEntry;
	
	public void printReport()
	{
		showReportTable=true;
		showFixedDepositTable=false;
		if(totalEntry>0)
		{
			String st="All";
			HttpSession ss=(HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
			ss.setAttribute("finalList", finalList);
			ss.setAttribute("reportType",reportType);
			ss.setAttribute("identity", st);
			RequestContext.getCurrentInstance().execute("window.open('printIncomeExpenseReport.xhtml')");
		}
	}
	
	public void showReport()
	{  Connection conn=DataBaseConnection.JAVAConnection();
		finalList=new ArrayList<>();
		penalty=totalIncome=totalExpense=totalAsset=totalEntry=0;
		if((startDate==null && endDate==null) || ((startDate!=null && endDate!=null) && (endDate.after(startDate))))
		{
			show=true;
			showOtherAmount=false;showAmount=true;
			if(reportType.equals("Income"))
			{
				finalList=income(startDate,endDate);
				showReportTable=true;
				showFixedDepositTable=false;
			}
			else if(reportType.equals("Expense"))
			{
				finalList=expense(startDate,endDate);
				showReportTable=true;
				showFixedDepositTable=false;
			}
			else if(reportType.equals("Current Asset"))
			{
				finalList=currentAsset(startDate,endDate);
				showReportTable=true;
				showFixedDepositTable=false;
			}
			/*else if(reportType.equals("Fixed Asset"))
			{
				finalList=fixedAsset(startDate,endDate);
				showReportTable=true;
				showFixedDepositTable=false;
			}
			else if(reportType.equals("Fixed Deposit"))
			{
				finalFixedDepositIncomeList=fixedDeposit(startDate,endDate);
				showReportTable=false;
				showFixedDepositTable=true;
			}
			*/
			if(reportType.equals("Income_Expense"))
			{
				showReportTable=true;
				showOtherAmount=true;showAmount=false;
				finalList=income(startDate,endDate);
				finalList.addAll(expense(startDate,endDate));
				finalList.addAll(currentAsset(startDate,endDate));
				totalEntry=finalList.size();
				int i=1;
				penalty=0;
				for(UserInfo ll:finalList)
				{
					if(ll.getReportType().equals("Income"))
					{
						totalIncome+=ll.getAmount();
					}
					else if(ll.getReportType().equals("Expense"))
					{
						ll.setTotalAmount(ll.getAmount());
						ll.setAmount(0);
						totalExpense+=ll.getTotalAmount();
						/*ll.setTotalAmount(ll.getAmount());
						totalExpense+=ll.getAmount();*/
					}
					else if(ll.getReportType().equals("Current Asset"))
					{
						ll.setTotalAmount(ll.getAmount());
						ll.setAmount(0);
						totalAsset+=ll.getTotalAmount();
						/*ll.setTotalAmount(ll.getAmount());
						totalAsset+=ll.getAmount();*/
					}
				
					ll.setSno(i++);
					ll.setShowReportType(true);
					penalty=penalty+(ll.getPenalty());
				}
			}
		}
		else
		{
			show=false;
			FacesContext fc=FacesContext.getCurrentInstance();
			fc.addMessage(null, new FacesMessage("Please enter the dates properly!"));
		}
	}
	
	private ArrayList<UserInfo> expense(Date strtdt, Date enddt)
	{  
		HttpSession ss=(HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
		int branchId = (int) ss.getAttribute("branchId");
		Connection conn=DataBaseConnection.JAVAConnection();
		expenseList=new ArrayList<>();
		extraExpenseList=new ArrayList<>();
		withdrawlList=obj1.withdrawlListForExpenseReport(branchId,strtdt,enddt,conn,"-1");
		returnAmountList=obj1.returnAmountListForExpenseReport(branchId,strtdt,enddt,conn);
		extraExpenseList=obj1.allExpenseList(branchId,strtdt,enddt,conn);
		fixedDepositExpenseList=obj1.allFixedDepositExpenseList(branchId,strtdt,enddt,conn);
		
		expenseList.addAll(withdrawlList);
		expenseList.addAll(returnAmountList);
		expenseList.addAll(extraExpenseList);
		expenseList.addAll(fixedDepositExpenseList);
		
		totalEntry=0;
		totalEntry=expenseList.size();
		
		int i=1;
		totalAmount=0;
		penalty=0;
		for(UserInfo ll:expenseList)
		{
			ll.setReportType("Expense");
			ll.setSno(i++);
			totalAmount=totalAmount+(ll.getAmount());
		}  try {
			conn.close();
		} catch (Exception e) {
			// TODO: handle exception
		}
		return expenseList;
	}
	
	private ArrayList<UserInfo> currentAsset(Date strtdt, Date enddt)
	{  
		HttpSession ss=(HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
		int branchId = (int) ss.getAttribute("branchId");
		Connection conn=DataBaseConnection.JAVAConnection();
		assetList=new ArrayList<>();
		loanAmountPrsnlList=obj1.loanAmountPrsnlLoanListForExpenseReport(branchId,strtdt,enddt,conn);
		loanAmountPrprtyList=obj1.loanAmountPrprtyLoanListForExpenseReport(branchId,strtdt,enddt,conn);
		loanAmountVhclList=obj1.loanAmountVhclLoanListForExpenseReport(branchId,strtdt,enddt,conn);
		loanAmountGrpList=obj1.loanAmountGrpLoanListForExpenseReport(branchId,strtdt,enddt,conn);
		
		assetList.addAll(loanAmountPrsnlList);
		assetList.addAll(loanAmountPrprtyList);
		assetList.addAll(loanAmountVhclList);
		assetList.addAll(loanAmountGrpList);
		
		totalEntry=0;
		totalEntry=assetList.size();
		
		int i=1;
		totalAmount=0;
		penalty=0;
		for(UserInfo ll:assetList)
		{
			ll.setReportType("Current Asset");
			ll.setSno(i++);
			totalAmount=totalAmount+(ll.getAmount());
		}  try {
			conn.close();
		} catch (Exception e) {
			// TODO: handle exception
		}
		return assetList;
	}
	
	private ArrayList<UserInfo> income(Date strtdt,Date enddt)
	{  
		HttpSession ss=(HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
		int branchId = (int) ss.getAttribute("branchId");
		Connection conn=DataBaseConnection.JAVAConnection();
		incomeList=new ArrayList<>();
		extraIncomeList=new ArrayList<>();
		depositList=obj1.depositPaymentListForIncomeReport(branchId,strtdt,enddt,conn,"-1");
		//shareAmntList=obj1.shareAmountListForIncomeReport(strtdt,enddt);
		rdPayList=obj1.allPayReceiptForIncomeReport(branchId,strtdt,enddt,conn);
		applicantFeeList=obj1.applicantFeeListForIncomeReport(branchId,strtdt,enddt,conn);
		/*fileChargePrsnlList=obj1.fileChargePrsnlLoanListForIncomeReport(strtdt,enddt);
		fileChargePrprtyList=obj1.fileChargePrprtyLoanListForIncomeReport(strtdt,enddt);
		fileChargeVhclList=obj1.fileChargeVhclLoanListForIncomeReport(strtdt,enddt);
		fileChargeGrpList=obj1.fileChargeGrpLoanListForIncomeReport(strtdt,enddt);*/
		loanPaymentList=obj1.loanEmiPaymentListForIncomeReport(branchId,strtdt,enddt,conn);
		extraIncomeList=obj1.allIncomeList(branchId,strtdt,enddt,conn);
		fixedDepositIncomeList=obj1.allFixedDepositIncomeList(branchId,strtdt,enddt,conn);
		
		//incomeList.addAll(shareAmntList);
		incomeList.addAll(applicantFeeList);
		incomeList.addAll(rdPayList);
		incomeList.addAll(depositList);
		/*incomeList.addAll(fileChargePrsnlList);
		incomeList.addAll(fileChargePrprtyList);
		incomeList.addAll(fileChargeVhclList);
		incomeList.addAll(fileChargeGrpList);*/
		incomeList.addAll(loanPaymentList);
		incomeList.addAll(extraIncomeList);
		incomeList.addAll(fixedDepositIncomeList);
		
		totalEntry=0;
		totalEntry=incomeList.size();
		int i=1;
		totalAmount=0;
		penalty=0;
		for(UserInfo ll:incomeList)
		{
			ll.setSno(i++);
			ll.setReportType("Income");
			totalAmount=totalAmount+(ll.getAmount());
			penalty=penalty+(ll.getPenalty());
		} 
try {
			conn.close();
		} catch (Exception e) {
			// TODO: handle exception
		}
		return incomeList;
	}
	
	private ArrayList<UserInfo> fixedAsset(Date strtdt, Date enddt)
	{
		HttpSession ss=(HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
		int branchId = (int) ss.getAttribute("branchId");
		Connection conn=DataBaseConnection.JAVAConnection();
		fixedAssetList=new ArrayList<>();
		extraExpenseList=obj1.allFixedAssetList(branchId,strtdt,enddt,conn);
		
		fixedAssetList.addAll(extraExpenseList);
		
		totalEntry=0;
		totalEntry=fixedAssetList.size();
		
		int i=1;
		totalAmount=0;
		penalty=0;
		for(UserInfo ll:fixedAssetList)
		{
			ll.setReportType("Fixed Asset");
			ll.setSno(i++);
			totalAmount=totalAmount+(ll.getAmount());
		} 
try {
			conn.close();
		} catch (Exception e) {
			// TODO: handle exception
		}
		return fixedAssetList;
	}
	
	/*private ArrayList<IncomeList> fixedDeposit(Date strtdt, Date enddt)
	{
		fixedDepositList=new ArrayList<>();
		fixedDepositList=obj1.allFixedDepositList(strtdt,enddt);
		
		totalEntry=0;
		totalEntry=fixedDepositList.size();
		
		int i=1;
		totalAmount=0;
		penalty=0;
		for(IncomeList ll:fixedDepositList)
		{
			ll.setsNo(i++);
			totalAmount=totalAmount+(ll.getAmount());
		}
		return fixedDepositList;
	}*/

	
	public ArrayList<UserInfo> getFinalList() {
		return finalList;
	}
	public void setFinalList(ArrayList<UserInfo> finalList) {
		this.finalList = finalList;
	}
	public double getTotalAmount() {
		return totalAmount;
	}
	public void setTotalAmount(double totalAmount) {
		this.totalAmount = totalAmount;
	}
	public double getPenalty() {
		return penalty;
	}
	public void setPenalty(double penalty) {
		this.penalty = penalty;
	}
	public int getTotalEntry() {
		return totalEntry;
	}
	public void setTotalEntry(int totalEntry) {
		this.totalEntry = totalEntry;
	}
	public boolean isShow() {
		return show;
	}
	public void setShow(boolean show) {
		this.show = show;
	}
	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 String getReportType() {
		return reportType;
	}
	public double getTotalIncome() {
		return totalIncome;
	}
	public void setTotalIncome(double totalIncome) {
		this.totalIncome = totalIncome;
	}
	public double getTotalExpense() {
		return totalExpense;
	}
	public void setTotalExpense(double totalExpense) {
		this.totalExpense = totalExpense;
	}
	public void setReportType(String reportType) {
		this.reportType = reportType;
	}
	public boolean isShowAmount() {
		return showAmount;
	}
	public void setShowAmount(boolean showAmount) {
		this.showAmount = showAmount;
	}
	public boolean isShowOtherAmount() {
		return showOtherAmount;
	}
	public void setShowOtherAmount(boolean showOtherAmount) {
		this.showOtherAmount = showOtherAmount;
	}

	public double getTotalAsset() {
		return totalAsset;
	}

	public void setTotalAsset(double totalAsset) {
		this.totalAsset = totalAsset;
	}

	public boolean isShowReportTable() {
		return showReportTable;
	}

	public void setShowReportTable(boolean showReportTable) {
		this.showReportTable = showReportTable;
	}

	public boolean isShowFixedDepositTable() {
		return showFixedDepositTable;
	}

	public void setShowFixedDepositTable(boolean showFixedDepositTable) {
		this.showFixedDepositTable = showFixedDepositTable;
	}

	public ArrayList<IncomeList> getFinalFixedDepositIncomeList() {
		return finalFixedDepositIncomeList;
	}

	public void setFinalFixedDepositIncomeList(
			ArrayList<IncomeList> finalFixedDepositIncomeList) {
		this.finalFixedDepositIncomeList = finalFixedDepositIncomeList;
	}
	
}
