package main_package;

import java.io.IOException;
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 org.primefaces.context.RequestContext;



@ManagedBean(name="viewOdLoan")
@ViewScoped
public class ViewDeleteOfOdLoanBean implements Serializable
{
	Date currentDate=new Date();
	ArrayList<LoanInfo> odlist;
	LoanInfo selectedLoan;
	String type;
	int branchId;
	DataBaseMethods obj=new DataBaseMethods();
	public ViewDeleteOfOdLoanBean() 
	{
		HttpSession ss=(HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
		branchId = (int) ss.getAttribute("branchId");
		Connection conn=DataBaseConnection.JAVAConnection();
		odlist=obj.odLoanDetails(branchId,conn);
		try {
			conn.close();
		} catch (Exception e) {
			// TODO: handle exception
		}
	}
	
	public void viewOdLoan() throws IOException
	{
		HttpSession ss=(HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
		ss.setAttribute("selectedLoan", selectedLoan);
		RequestContext.getCurrentInstance().execute("window.open('viewOdLoan.xhtml')");
	}
	
	
	public void returnOdLoan() throws IOException
	{
		HttpSession ss=(HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
		ss.setAttribute("selectedLoan", selectedLoan);
		RequestContext.getCurrentInstance().execute("window.open('odLoanReturn.xhtml')");
	}
	

	public void printVoucherDetail()
	{
		String payMode=selectedLoan.getPaymentMode();
		String desc=selectedLoan.getShareHolderNo()+" - "+selectedLoan.getApplicantName();
		if(payMode.equalsIgnoreCase("Transfer"))
		{
			payMode=payMode+" In Saving Account No. "+selectedLoan.getSavingAccountNo();
		}
		HttpSession ss=(HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
		ss.setAttribute("voucherNo", "");
		ss.setAttribute("expDate", selectedLoan.getOdLoanDate());
		ss.setAttribute("amount", String.valueOf(selectedLoan.getOdAmount()));
		ss.setAttribute("vchtype", "Transfer");
		ss.setAttribute("payTo", desc);
		ss.setAttribute("being", "OD Loan No. "+selectedLoan.getOdLoanNo()+" Against Deposit No. "+selectedLoan.getDepositNo());
		ss.setAttribute("payMode", payMode);
		
		RequestContext.getCurrentInstance().execute("window.open('printVoucher.xhtml')");

	}
	
	public String deleteOdLoan()
	{
		HttpSession ss=(HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
		branchId = (int) ss.getAttribute("branchId");
		Connection conn=DataBaseConnection.JAVAConnection();
		String a=obj.deleteOdLoan(selectedLoan.getOdLoanNo());
		if(a.equals("i"))
		{
			
			if(selectedLoan.getSavingAccountNo()!=null && selectedLoan.getPaymentMode().equalsIgnoreCase("Transfer"))
			{
				obj.decreaseSavingAccountAmountForOdLoan(selectedLoan.getOdAmount(), selectedLoan.getSavingAccountNo());
				obj.userWithdrawalForOdLoan(selectedLoan.getSavingAccountNo(), String.valueOf(selectedLoan.getOdAmount()), currentDate, selectedLoan.getPaymentMode(), selectedLoan.getChqNo(), selectedLoan.chqDate, selectedLoan.getBankName(), "approved", "N/A",branchId);
			}
			
			else if(selectedLoan.getPaymentMode().equalsIgnoreCase("cash"))
			{
				obj.increaseCompanyCapitalAmount(branchId,selectedLoan.getOdAmount(), conn);
			}
			else if(selectedLoan.getPaymentMode().equalsIgnoreCase("cheque") || selectedLoan.getPaymentMode().equalsIgnoreCase("neft") || selectedLoan.getPaymentMode().equalsIgnoreCase("rtgs") )
			{
				obj.increaseBankBalance(selectedLoan.getBankId(),selectedLoan.getOdAmount(), conn);
			}
			odlist=obj.odLoanDetails(branchId,conn);
			FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Delete Successfully"));
			
		}
		else
		{
			FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Some Error Occured !!"));
		}
		try {
			conn.close();
		} catch (Exception e) {
			// TODO: handle exception
		}
		return "";
	}

	

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}





	public ArrayList<LoanInfo> getOdlist() {
		return odlist;
	}





	public void setOdlist(ArrayList<LoanInfo> odlist) {
		this.odlist = odlist;
	}

	public LoanInfo getSelectedLoan() {
		return selectedLoan;
	}

	public void setSelectedLoan(LoanInfo selectedLoan) {
		this.selectedLoan = selectedLoan;
	}

	public Date getCurrentDate() {
		return currentDate;
	}

	public void setCurrentDate(Date currentDate) {
		this.currentDate = currentDate;
	}
	
	
}
