package report_package;

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="rdFdIncomeExpenseReport")
@ViewScoped
public class RdFdIncomeExpenseBean implements Serializable
{
	private static final long serialVersionUID = 1L;
	Date startDate,endDate;
	String reportType;
	boolean show,showAmount,showOtherAmount;
	ArrayList<UserInfo> finalList,expenseList,incomeList;
	double totalAmount,penalty,totalIncome,totalExpense;
	int totalEntry;
	DataBaseReportMethods obj1=new DataBaseReportMethods();
	public void printReport()
	{
		if(totalEntry>0)
		{
			String st="RD/FD";
			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()
	{
		showOtherAmount=false;showAmount=true;
		if((startDate==null && endDate==null) || ((startDate!=null && endDate!=null) && (endDate.after(startDate))))
		{
			show=true;totalExpense=totalIncome=0;
			if(reportType.equals("Income"))
			{
				finalList=income(startDate,endDate);
			}
			else if(reportType.equals("Expense"))
			{
				finalList=expense(startDate,endDate);
			}
			if(reportType.equals("Income_Expense"))
			{
				showOtherAmount=true;showAmount=false;
				finalList=income(startDate,endDate);
				finalList.addAll(expense(startDate,endDate));
				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.setSno(i++);
					ll.setShowReportType(true);
					penalty=penalty+(ll.getPenalty());
				}
			}
			totalEntry=finalList.size();
		}
		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");
		expenseList=new ArrayList<>();
		expenseList=obj1.ExpenseReportOfRdFd(branchId,strtdt,enddt);
		int i=1;
		totalAmount=0;penalty=0;
		for(UserInfo ll:expenseList)
		{
			ll.setSno(i++);
			ll.setReportType("Expense");
			totalAmount=totalAmount+(ll.getAmount());
		}
		return expenseList;
	}
	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<>();
		incomeList=obj1.allPayReceiptForIncomeReport(branchId,strtdt,enddt,conn);
		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;
	}
	
	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 void setReportType(String reportType) {
		this.reportType = reportType;
	}
	public boolean isShow() {
		return show;
	}
	public void setShow(boolean show) {
		this.show = show;
	}
	public ArrayList<UserInfo> getFinalList() {
		return finalList;
	}
	public void setFinalList(ArrayList<UserInfo> finalList) {
		this.finalList = finalList;
	}
	public ArrayList<UserInfo> getExpenseList() {
		return expenseList;
	}
	public void setExpenseList(ArrayList<UserInfo> expenseList) {
		this.expenseList = expenseList;
	}
	public ArrayList<UserInfo> getIncomeList() {
		return incomeList;
	}
	public void setIncomeList(ArrayList<UserInfo> incomeList) {
		this.incomeList = incomeList;
	}
	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 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 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 int getTotalEntry() {
		return totalEntry;
	}
	public void setTotalEntry(int totalEntry) {
		this.totalEntry = totalEntry;
	}
}
