package report_package;

import java.io.Serializable;
import java.sql.Connection;
import java.util.ArrayList;
import java.util.Collections;
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 org.primefaces.context.RequestContext;

import report_package.BorrowerLenderInfo;
import report_package.DataBaseReportMethods;

@ManagedBean(name="fixedAssetsReport")
@ViewScoped
public class FixedAssetsReportBean implements Serializable
{
	Date startDate=new Date(),endDate=new Date();
	ArrayList<BorrowerLenderInfo> list, saleList, pList;
	boolean show, showSale;
	BorrowerLenderInfo selected;
	String label;
	DataBaseReportMethods obj1=new DataBaseReportMethods();
	public FixedAssetsReportBean() 
	{
		startDate.setYear(startDate.getYear()-1);
	}
	
	
	public void view()
	{  
		HttpSession ss=(HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
		int branchId = (int) ss.getAttribute("branchId");
		Connection conn=DataBaseConnection.JAVAConnection();
		list=obj1.fixedAssetsReport(branchId,startDate, endDate,conn);
		
		// System.out.println(list.size());
		if(list.size()>0)
		{
			show=true;
		}
		else
		{
			show=false;
			FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("No Record Found"));
		}
		try {
			conn.close();
		} catch (Exception e) {
			// TODO: handle exception
		}
	}
	
	public void saleData()
	{ 
		HttpSession ss=(HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
		int branchId = (int) ss.getAttribute("branchId");
		Connection conn=DataBaseConnection.JAVAConnection();
		label="Sale";
		saleList=obj1.saleFixedAssetsDetail(selected.getItemName(), startDate, endDate,branchId);
		//// System.out.println("dsd "+saleList.size());

try {
			conn.close();
		} catch (Exception e) {
			// TODO: handle exception
		}
		showSale=true;
	}
	

	public void printPurchase()
	{
		HttpSession ss=(HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
		ss.setAttribute("startDate", startDate);
		ss.setAttribute("endDate", endDate);
		ss.setAttribute("list", list);
		RequestContext.getCurrentInstance().execute("window.open('printPurchaseFixedAsset.xhtml')");
	}
	
	public void printSale()
	{
		HttpSession ss=(HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
		ss.setAttribute("startDate", startDate);
		ss.setAttribute("endDate", endDate);
		RequestContext.getCurrentInstance().execute("window.open('printSaleFixedAsset.xhtml')");
	}
	

	
	
	
	public void pData()
	{  
		HttpSession ss=(HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
		int branchId = (int) ss.getAttribute("branchId");
		Connection conn=DataBaseConnection.JAVAConnection();
		label="Purchase";
		saleList=obj1.purchaseFixedAssetsDetail(branchId,selected.getItemName(), startDate, endDate,conn);
		//// System.out.println("pur "+saleList.size());
		try {
			conn.close();
		} catch (Exception e) {
			// TODO: handle exception
		}
		showSale=true;
	}
	
	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 ArrayList<BorrowerLenderInfo> getList() {
		return list;
	}


	public void setList(ArrayList<BorrowerLenderInfo> list) {
		this.list = list;
	}


	public boolean isShow() {
		return show;
	}


	public void setShow(boolean show) {
		this.show = show;
	}


	public BorrowerLenderInfo getSelected() {
		return selected;
	}


	public void setSelected(BorrowerLenderInfo selected) {
		this.selected = selected;
	}


	public ArrayList<BorrowerLenderInfo> getSaleList() {
		return saleList;
	}


	public void setSaleList(ArrayList<BorrowerLenderInfo> saleList) {
		this.saleList = saleList;
	}


	public ArrayList<BorrowerLenderInfo> getpList() {
		return pList;
	}


	public void setpList(ArrayList<BorrowerLenderInfo> pList) {
		this.pList = pList;
	}


	public boolean isShowSale() {
		return showSale;
	}


	public void setShowSale(boolean showSale) {
		this.showSale = showSale;
	}


	public String getLabel() {
		return label;
	}


	public void setLabel(String label) {
		this.label = label;
	}
	
	
	
	
}
