package main_package;

import java.io.Serializable;
import java.text.DecimalFormat;
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 org.primefaces.context.RequestContext;

@ManagedBean(name="viewSalary")
@ViewScoped

public class ViewSalaryBean implements Serializable
{
	String selectedYear,selectedMonth,type,monthName,selectedEmployee;
	ArrayList<SelectItem> monthList,yearList,employeeList;
	ArrayList<EmpInfo> elist;
	EmployeeInfo employeeInfo;
	SalaryInfo salInfo;
	double totalAmount, totalEntry;
	boolean showDescription, allEmp;
	
	public ViewSalaryBean() 
	{
		HttpSession ss=(HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
		 int branchId = (int) ss.getAttribute("branchId");
		yearList=new DataBaseMethodsSalary().allYearList();
		monthList=new DataBaseMethodsSalary().allMonths();
		
		Date date=new DataBaseMethods().uniqueDate();
		StringBuffer temp=new StringBuffer(String.valueOf(date));
		selectedYear=temp.substring(temp.length()-4);
		selectedMonth=String.valueOf(date.getMonth()+1);
		
		int dayMonth=new DataBaseMethodsSalary().calculateDayInMonth(Integer.parseInt(selectedMonth),Integer.parseInt(selectedYear));
		String dd=selectedYear+"-"+selectedMonth+"-"+dayMonth;
		employeeList=new DataBaseMethodsSalary().employeeListForSalary(branchId,dd);
		//System.out.println(date+"....."+selectedMonth);
	}
	
	public void showReport()
	{
		HttpSession ss=(HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
		 int branchId = (int) ss.getAttribute("branchId");
		totalAmount=totalEntry=0;
		monthName=new DataBaseMethods().monthList(Integer.valueOf(selectedMonth));
		if(selectedEmployee.equals("-1"))
		{
			showDescription=false;
			elist=new DataBaseMethods().allEmlSalaryFormationReport(branchId,selectedYear,selectedMonth);
			if(elist.size()==0)
			{
				allEmp=false;
				FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("NO RECORD FOUND."));
			}
			else
			{
				totalEntry=elist.size();
				for(EmpInfo ee: elist)
				{
					totalAmount+=ee.getSalary();
				}
				totalAmount=Double.parseDouble(new DecimalFormat("##.##").format(totalAmount));
				allEmp=true;
			}
		}
		else
		{
			allEmp=false;
			employeeInfo=new DataBaseMethodsSalary().employeeInfoByUserName(selectedEmployee);
			salInfo=new DataBaseMethodsSalary().viewPreparedSalary(selectedEmployee,selectedMonth,selectedYear);
			if(salInfo==null)
			{
				showDescription=false;
				FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("NO RECORD FOUND."));
			}
			else
			{
				showDescription=true;
			}
		}
		
	}
	
	
	public void printReport()
	{
		HttpSession ss=(HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(true);
		ss.setAttribute("salInfo", salInfo);
		ss.setAttribute("employeeInfo", employeeInfo);
		ss.setAttribute("selectedEmployee", selectedEmployee);
		ss.setAttribute("monthName", monthName);
		ss.setAttribute("selectedYear", selectedYear);

		RequestContext.getCurrentInstance().execute("window.open('printSalarySlip.xhtml')");
		RequestContext.getCurrentInstance().execute("window.open('printSalarySlip2.xhtml')");
	}

	public String getSelectedYear() {
		return selectedYear;
	}

	public void setSelectedYear(String selectedYear) {
		this.selectedYear = selectedYear;
	}

	public String getSelectedMonth() {
		return selectedMonth;
	}

	public void setSelectedMonth(String selectedMonth) {
		this.selectedMonth = selectedMonth;
	}

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

	public String getMonthName() {
		return monthName;
	}

	public void setMonthName(String monthName) {
		this.monthName = monthName;
	}

	public String getSelectedEmployee() {
		return selectedEmployee;
	}

	public void setSelectedEmployee(String selectedEmployee) {
		this.selectedEmployee = selectedEmployee;
	}

	public ArrayList<SelectItem> getMonthList() {
		return monthList;
	}

	public void setMonthList(ArrayList<SelectItem> monthList) {
		this.monthList = monthList;
	}

	public ArrayList<SelectItem> getYearList() {
		return yearList;
	}

	public void setYearList(ArrayList<SelectItem> yearList) {
		this.yearList = yearList;
	}

	public ArrayList<SelectItem> getEmployeeList() {
		return employeeList;
	}

	public void setEmployeeList(ArrayList<SelectItem> employeeList) {
		this.employeeList = employeeList;
	}

	public EmployeeInfo getEmployeeInfo() {
		return employeeInfo;
	}

	public void setEmployeeInfo(EmployeeInfo employeeInfo) {
		this.employeeInfo = employeeInfo;
	}

	public SalaryInfo getSalInfo() {
		return salInfo;
	}

	public void setSalInfo(SalaryInfo salInfo) {
		this.salInfo = salInfo;
	}

	public boolean isShowDescription() {
		return showDescription;
	}

	public void setShowDescription(boolean showDescription) {
		this.showDescription = showDescription;
	}

	public ArrayList<EmpInfo> getElist() {
		return elist;
	}

	public void setElist(ArrayList<EmpInfo> elist) {
		this.elist = elist;
	}

	public boolean isAllEmp() {
		return allEmp;
	}

	public void setAllEmp(boolean allEmp) {
		this.allEmp = allEmp;
	}

	public double getTotalAmount() {
		return totalAmount;
	}

	public void setTotalAmount(double totalAmount) {
		this.totalAmount = totalAmount;
	}

	public double getTotalEntry() {
		return totalEntry;
	}

	public void setTotalEntry(double totalEntry) {
		this.totalEntry = totalEntry;
	}
	
	
}
