package main_package;

import group_head_package.IncomeList;

import java.io.Serializable;
import java.sql.Connection;
import java.util.ArrayList;
import java.util.Collections;

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 report_package.DataBaseReportMethods;

@ManagedBean(name="empSalaryLedger")
@ViewScoped

public class EmployeeSalaryLedgerReportBean implements Serializable 
{
	private static final long serialVersionUID = 1L;
	
	double totalCredit, totalDebit, closeBalance, totalEntry;
	
	
	String selectedEmp,empName;
	
	
	boolean show,showPanel, showAll;
	
	
	ArrayList<SelectItem> empList;
	ArrayList<EmpInfo> elist;
	ArrayList<IncomeList> salaryLedgerList;
	
	
	DataBaseMethodTaheer dbmt = new DataBaseMethodTaheer(); 
	
	
	
	public EmployeeSalaryLedgerReportBean() 
	{
		HttpSession ss = (HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
		int branchId = (int) ss.getAttribute("branchId");
		Connection con = DataBaseConnection.JAVAConnection();
		empList = new DataBaseMethodsSalary().allEmployeeList(branchId,con);
		try 
		{
			con.close();
		} 
		catch (Exception e) 	
		{
			e.printStackTrace();
		}
		
		showPanel = true;
	}
	
	public void createLedger()
	{
		HttpSession ss = (HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
		int branchId = (int) ss.getAttribute("branchId");
		Connection conn = DataBaseConnection.JAVAConnection();
		if(selectedEmp.equals("-1"))
		{
			show = false;
			elist = new DataBaseReportMethods().allEmpSalaryLedger(branchId);
			if(elist.size() > 0)
			{
				showAll = true;
				totalCredit = 0; 
				totalDebit = closeBalance = 0;
				
				for(EmpInfo ll : elist)
				{
					totalCredit += ll.getCredit();
					totalDebit += ll.getDebit();
				}
				
				closeBalance = totalCredit - totalDebit;
			}
			else
			{
				showAll = false;
				FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("No Records Found...."));
			}
			
			try 
			{
				conn.close();
			} 
			catch (Exception e) 
			{
				e.printStackTrace();
			}
		}
		else
		{
			showAll = false;
			empName = new DataBaseMethodsSalary().employeeNameByuserName(selectedEmp);
			salaryLedgerList = dbmt.salaryLedgerOfEmp(selectedEmp);
			
			if(salaryLedgerList.size() > 0)
			{
				show = true;
				totalCredit = 0;
				totalDebit = 0;
				
				for(IncomeList ll : salaryLedgerList)
				{
					totalCredit += ll.getCredit();
					totalDebit += ll.getDebit();
				}
				
				closeBalance = totalCredit - totalDebit;
			}
			else
			{
				show = false;
				FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("No Records Found...."));
			}
			
			Collections.sort(salaryLedgerList); 
		}
	}
	
	
	
	public boolean isShow() {
		return show;
	}
	public void setShow(boolean show) {
		this.show = show;
	}
	public double getTotalCredit() {
		return totalCredit;
	}
	public void setTotalCredit(double totalCredit) {
		this.totalCredit = totalCredit;
	}
	public double getTotalDebit() {
		return totalDebit;
	}
	public void setTotalDebit(double totalDebit) {
		this.totalDebit = totalDebit;
	}
	public double getCloseBalance() {
		return closeBalance;
	}
	public void setCloseBalance(double closeBalance) {
		this.closeBalance = closeBalance;
	}
	public ArrayList<SelectItem> getEmpList() {
		return empList;
	}
	public void setEmpList(ArrayList<SelectItem> empList) {
		this.empList = empList;
	}
	public String getSelectedEmp() {
		return selectedEmp;
	}
	public void setSelectedEmp(String selectedEmp) {
		this.selectedEmp = selectedEmp;
	}
	public String getEmpName() {
		return empName;
	}
	public void setEmpName(String empName) {
		this.empName = empName;
	}
	public ArrayList<IncomeList> getSalaryLedgerList() {
		return salaryLedgerList;
	}
	public void setSalaryLedgerList(ArrayList<IncomeList> salaryLedgerList) {
		this.salaryLedgerList = salaryLedgerList;
	}
	public boolean isShowPanel() {
		return showPanel;
	}
	public void setShowPanel(boolean showPanel) {
		this.showPanel = showPanel;
	}

	public boolean isShowAll() {
		return showAll;
	}

	public void setShowAll(boolean showAll) {
		this.showAll = showAll;
	}

	public ArrayList<EmpInfo> getElist() {
		return elist;
	}

	public void setElist(ArrayList<EmpInfo> elist) {
		this.elist = elist;
	}

	public double getTotalEntry() {
		return totalEntry;
	}

	public void setTotalEntry(double totalEntry) {
		this.totalEntry = totalEntry;
	}
	
}