package group_head_package;

import java.io.Serializable;
import java.sql.Connection;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
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 main_package.DataBaseMethods;
import main_package.DataBaseConnection;

import org.primefaces.context.RequestContext;
@ManagedBean(name="viewEditDeleteExpense")
@ViewScoped
public class ViewEditDeleteExpenseBean implements Serializable
{
    private static final long serialVersionUID = 1L;
    ArrayList<IncomeList>iList;
    ArrayList<SelectItem> categoryList;
    IncomeList selectedIncome;
    String incomeId,selectedCategory,searchCategory="all",strIncomeDate,description,paymentMode,chequeNo,chequeDate,bankBranch,bankName,label,expenseCategory, searchType="Other_Dr";
    Date startDate=new Date(),endDate=new Date();
    double amount, totalAmount;
    int totalEntry;
    boolean showCheque;
    DataBaseIncomeExpense dbe=new DataBaseIncomeExpense();
    DataBaseMethods dbm=new DataBaseMethods();
    
    public ViewEditDeleteExpenseBean()
    {
    	HttpSession ss=(HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
		int branchId = (int) ss.getAttribute("branchId");
    	startDate.setDate(1);
    	Connection conn=DataBaseConnection.JAVAConnection();
    	iList=dbe.allExpenseAndAssetsList(branchId,startDate, endDate, searchType, searchCategory,conn);
    	totalAmount=totalEntry=0;
    	totalEntry=iList.size();
    	for(IncomeList ii: iList)
    	{
    		totalAmount+=ii.getAmount();
    	}
    	try {
			conn.close();
		} catch (Exception e) {
			// TODO: handle exception
		}
    }
    
    public void createCategoryList()
	{
		Connection conn=DataBaseConnection.JAVAConnection();
		if(searchType.equalsIgnoreCase("Other_Dr"))
		{
			categoryList=dbm.groupHeadSelectList(conn, "Expense");
		}
		else
		{
			categoryList=dbm.groupHeadSelectList(conn, "Assets");
		}
		try {
			conn.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
    
	public void searchIncome()
	{
		HttpSession ss=(HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
		int branchId = (int) ss.getAttribute("branchId");
		Connection conn=DataBaseConnection.JAVAConnection();
		totalAmount=totalEntry=0;
		iList=dbe.allExpenseAndAssetsList(branchId,startDate,endDate, searchType,searchCategory,conn);
    	totalEntry=iList.size();
    	for(IncomeList ii: iList)
    	{
    		totalAmount+=ii.getAmount();
    	}
		try {
			conn.close();
		} catch (Exception e) {
			// TODO: handle exception
		}
	}

	public void viewDetails()
	{
		selectedCategory=selectedIncome.getCategory();
		expenseCategory=selectedIncome.getCategory();
		incomeId=selectedIncome.getId();
		paymentMode=selectedIncome.getPaymentMode();
		amount=selectedIncome.getAmount();
		strIncomeDate=selectedIncome.getStrIncomeDate();
		if(paymentMode.equals("Cash"))
		{
			showCheque=false;
		}
		else if(paymentMode.equals("Cheque"))
		{
			showCheque=true;
			chequeNo=selectedIncome.getChequeNo();
			chequeDate=selectedIncome.getStrChqDate();
			bankName=selectedIncome.getBankName();
			bankBranch=selectedIncome.getBankBranch();
			description=selectedIncome.getDescription();
		}
	}
	
	public void printVoucherDetail()
	{
		HttpSession ss=(HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
		ss.setAttribute("voucherNo", selectedIncome.getVoucherNo());
		ss.setAttribute("expDate", selectedIncome.getIncomeDate());
		ss.setAttribute("amount", String.valueOf(selectedIncome.getAmount()));
		ss.setAttribute("vchtype", "Debit");
		ss.setAttribute("payTo", selectedIncome.getDescription());
		ss.setAttribute("being", selectedIncome.getItem_name());
		ss.setAttribute("payMode", selectedIncome.getPaymentMode());
		
		RequestContext.getCurrentInstance().execute("window.open('printVoucher.xhtml')");

	}
	
	public void update()
	{
		HttpSession ss=(HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
		int branchId = (int) ss.getAttribute("branchId");
		Connection conn=DataBaseConnection.JAVAConnection();
		int x=new DataBaseMethods().updateCategoryInExpenseTable(incomeId,expenseCategory);
		if(x>0)
		{
			iList=dbe.allExpenseAndAssetsList(branchId,startDate, endDate, searchType, searchCategory,conn);
			FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Category Updated!"));
			RequestContext.getCurrentInstance().update("form");
			RequestContext.getCurrentInstance().update("form3");
			try {
				conn.close();
			} catch (Exception e) {
				// TODO: handle exception
			}
			RequestContext.getCurrentInstance().execute("PF('editDialog').hide");
			expenseCategory="";
		}
	}
	
	public void print()
	{
		HttpSession ss=(HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
		ss.setAttribute("list", iList);
		ss.setAttribute("start", new SimpleDateFormat("dd/MM/yyyy").format(startDate));
		ss.setAttribute("end", new SimpleDateFormat("dd/MM/yyyy").format(endDate));
		
		RequestContext.getCurrentInstance().execute("window.open('printExtraExpense.xhtml')");
	}
	
	public void deleteSelectedIncome()
	{
		HttpSession ss=(HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
		int branchId = (int) ss.getAttribute("branchId");
		Connection conn=DataBaseConnection.JAVAConnection();
		double amount=selectedIncome.getAmount();
		String bank=selectedIncome.getBankId();
		int i=dbe.deleteExpense(selectedIncome);
		if(i>=1)
		{
			if(bank.equals("N/A"))
			{
				new DataBaseMethods().increaseCompanyCapitalAmount(branchId,amount,conn);
			}
			else
			{
				new DataBaseMethods().increaseBankBalance(bank, amount,conn);
			}
			iList=new  DataBaseIncomeExpense().allExpenseAndAssetsList(branchId,startDate, endDate, searchType, searchCategory,conn);
			FacesContext fc=FacesContext.getCurrentInstance();
			try {
				conn.close();
			} catch (Exception e) {
				// TODO: handle exception
			}
			fc.addMessage(null, new FacesMessage("Expense Deleted Successfully"));
		}
		else
		{
			FacesContext fc=FacesContext.getCurrentInstance();
			fc.addMessage(null, new FacesMessage("An Error Occured... Please Try Again"));
		}
	}

	public ArrayList<IncomeList> getiList() {
		return iList;
	}

	public void setiList(ArrayList<IncomeList> iList) {
		this.iList = iList;
	}

	public IncomeList getSelectedIncome() {
		return selectedIncome;
	}
	public void setSelectedIncome(IncomeList selectedIncome) {
		this.selectedIncome = selectedIncome;
	}

	public String getSelectedCategory() {
		return selectedCategory;
	}

	public void setSelectedCategory(String selectedCategory) {
		this.selectedCategory = selectedCategory;
	}

	public String getStrIncomeDate() {
		return strIncomeDate;
	}

	public void setStrIncomeDate(String strIncomeDate) {
		this.strIncomeDate = strIncomeDate;
	}

	

	public double getAmount() {
		return amount;
	}

	public void setAmount(double amount) {
		this.amount = amount;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public String getPaymentMode() {
		return paymentMode;
	}

	public void setPaymentMode(String paymentMode) {
		this.paymentMode = paymentMode;
	}

	public String getChequeNo() {
		return chequeNo;
	}

	public void setChequeNo(String chequeNo) {
		this.chequeNo = chequeNo;
	}

	public String getBankBranch() {
		return bankBranch;
	}

	public void setBankBranch(String bankBranch) {
		this.bankBranch = bankBranch;
	}

	public String getBankName() {
		return bankName;
	}

	public void setBankName(String bankName) {
		this.bankName = bankName;
	}

	public String getLabel() {
		return label;
	}

	public void setLabel(String label) {
		this.label = label;
	}

	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 boolean isShowCheque() {
		return showCheque;
	}

	public void setShowCheque(boolean showCheque) {
		this.showCheque = showCheque;
	}

	public String getChequeDate() {
		return chequeDate;
	}

	public void setChequeDate(String chequeDate) {
		this.chequeDate = chequeDate;
	}

	public String getExpenseCategory() {
		return expenseCategory;
	}

	public void setExpenseCategory(String expenseCategory) {
		this.expenseCategory = expenseCategory;
	}

	public String getSearchType() {
		return searchType;
	}

	public void setSearchType(String searchType) {
		this.searchType = searchType;
	}

	public double getTotalAmount() {
		return totalAmount;
	}

	public void setTotalAmount(double totalAmount) {
		this.totalAmount = totalAmount;
	}

	public int getTotalEntry() {
		return totalEntry;
	}

	public void setTotalEntry(int totalEntry) {
		this.totalEntry = totalEntry;
	}

	public ArrayList<SelectItem> getCategoryList() {
		return categoryList;
	}

	public void setCategoryList(ArrayList<SelectItem> categoryList) {
		this.categoryList = categoryList;
	}

	public String getSearchCategory() {
		return searchCategory;
	}

	public void setSearchCategory(String searchCategory) {
		this.searchCategory = searchCategory;
	}
	
}
