package main_package;

import group_head_package.DataBaseIncomeExpense;

import java.io.Serializable;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.Timestamp;
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;

@ManagedBean(name = "viewDeletePromotorsCapital")
@ViewScoped
public class ViewDeletePromotorsCapitalBean implements Serializable {
	UserInfo selectedRow;
	ArrayList<UserInfo> pList, rlist;;
	String basis;
	int branchId;
	boolean showView, showReturn, showViewReturn, showExtraAmt;
	ArrayList<SelectItem> bankList;
	double cashBalance;

	String selectedPromotor, paymentMode, chequeNo, bankBranch, bankName,
			submittedBankName, transfer, label, description, voucherNo;
	boolean showCheque, showFields, showMode, showFixedDeposit, renderReturn,
			renderItem, showTransfer, showTransferSelect, showButtonFdr,
			showButtonCommon;
	Date incomeDate = new Date(), chequeDate;
	double amount, extraAmount;

	DataBaseMethods obj = new DataBaseMethods();

	public ViewDeletePromotorsCapitalBean() {
		Connection conn = DataBaseConnection.JAVAConnection();
		bankList = new DataBaseIncomeExpense().allBankList(conn);
		try {
			conn.close();
		} catch (Exception e) {
			// TODO: handle exception
		}
	}

	public void searchReport() 
	{
		HttpSession ss=(HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
		branchId = (int) ss.getAttribute("branchId");
		if (basis.equals("view")) 
		{
			pList = obj.allPromotersCapital(branchId);
			showView = true;
			showReturn = false;
			showViewReturn=false;
		} 
		else if (basis.equals("return_view")) 
		{
			pList = obj.allReturnPromotersCapital(branchId);
			showView = false;
			showReturn = false;
			showViewReturn=true;
		} 
		else 
		{
			rlist = obj.allPromotersCapitalForReturn(branchId);
			showView = false;
			showViewReturn=false;
			showReturn = true;
		}
	}

	public void checkPaymentMode() {
		if (paymentMode.equals("Cash")) 
		{
			showCheque = false;
			showTransfer = false;
			showTransferSelect = true;
			transfer = bankBranch = bankName = chequeNo = "";
			chequeDate = null;
			showExtraAmt=true;
		} else if (paymentMode.equals("Cheque")
				|| paymentMode.equals("Demand Draft")
				|| paymentMode.equals("rtgs")) {
			if (paymentMode.equals("Cheque")) {
				label = "Cheque";
			} else if (paymentMode.equals("rtgs")) {
				label = "RTGS/NEFT/IMPS";
			} else {
				label = "Demand Draft";
			}
			showCheque = true;
			showTransferSelect = false;
			showTransfer = true;
			transfer = bankBranch = bankName = chequeNo = "";
			chequeDate = null;
			showExtraAmt=false;
		}
	}

	public void checkTransferMode() {
		if (transfer.equals("Yes")) {
			showTransfer = true;
		} else {
			showTransfer = false;
		}
	}

	public String returnPromotor() 
	{
		HttpSession ss=(HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
		branchId = (int) ss.getAttribute("branchId");
		
		if (amount <= selectedRow.getBalance()) {
			if (paymentMode.equals("Cash") && transfer.equals("No")) {
				submittedBankName = "N/A";
			}

			selectedPromotor = selectedRow.getPromotorId();
			int i = new DataBaseIncomeExpense().returnPromotorCapital(branchId,
					incomeDate, "Return Promotor Capital", paymentMode, amount,
					chequeNo, bankBranch, bankName, chequeDate, description,
					submittedBankName, selectedPromotor);
			if (i >= 1) {
				FacesContext
						.getCurrentInstance()
						.addMessage(
								null,
								new FacesMessage(
										"Promotor Capital Return Successfully"));
				Connection conn = DataBaseConnection.JAVAConnection();

				if (paymentMode.equals("Cash") && transfer.equals("No")) {
					obj.decreaseCapitalCompanyAmount(branchId,amount);
				} else if (paymentMode.equals("Cash") && transfer.equals("Yes")) {
					obj.decreaseBankBalance(submittedBankName, amount, conn);
				}

				if (extraAmount > 0 && paymentMode.equals("Cash") && transfer.equals("No")) 
				{
					updateExpense();
				}

				amount = 0;
				description = paymentMode = chequeNo = bankBranch = bankName = label = selectedPromotor = "";
				showCheque = false;
				showFields = true;
				showFixedDeposit = false;
				incomeDate = new Date();
				renderItem = false;
				try {
					conn.close();
				} catch (Exception e) {
					// TODO: handle exception
				}

				return "promotorsDashboard.xhtml";

			} else {
				FacesContext
						.getCurrentInstance()
						.addMessage(
								null,
								new FacesMessage(
										"An Error Occured... Please Try Again"));
			}
		} else {
			FacesContext.getCurrentInstance().addMessage(
					null,
					new FacesMessage(
							"Can't Return more than left Promotor Balance "
									+ selectedRow.getBalance()));
		}

		return "";
	}

	public void deletePromoter() 
	{
		HttpSession ss=(HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
		branchId = (int) ss.getAttribute("branchId");
		Connection conn = DataBaseConnection.JAVAConnection();
		cashBalance = 0;
		cashBalance = obj.remainingCompanyCapitalAmount(branchId,conn);
		cashBalance = Double.parseDouble(new DecimalFormat("##.##")
				.format(cashBalance));
		if (cashBalance >= selectedRow.getBalance()) 
		{
			int a = obj.deletePromotorsCapital(selectedRow.getId(),conn);
			if (a > 0) 
			{
				if (selectedRow.payMode.equals("Cash")) 
				{
					obj.decreaseCapitalCompanyAmount(branchId,selectedRow.getBalance());
				} 
				else 
				{
					obj.decreaseBankBalance(selectedRow.getSubmittedToBank(), selectedRow.getAmount(),conn);
				}
				pList = obj.allPromotersCapital(branchId);

				FacesContext.getCurrentInstance().addMessage(null,
						new FacesMessage("delete successfully"));
			} else {
				FacesContext.getCurrentInstance().addMessage(null,
						new FacesMessage("Some Error Occured !!"));
			}
		} else {
			FacesContext.getCurrentInstance().addMessage(null,
					new FacesMessage("insufficent cash balance "));
		}
		try {
			conn.close();
		} catch (Exception e) {
			// TODO: handle exception
		}
	}
	
	public void deleteReturnPromoter() 
	{
		HttpSession ss=(HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
		branchId = (int) ss.getAttribute("branchId");
		Connection conn = DataBaseConnection.JAVAConnection();
		int a = obj.deleteReturnPromotorsCapital(selectedRow.getId(),conn);
		if (a > 0) 
		{
			if (selectedRow.payMode.equals("Cash")) 
			{
				obj.increaseCompanyCapitalAmount(branchId,selectedRow.getAmount(), conn);
			} 
			else 
			{
				obj.increaseBankBalance(selectedRow.getSubmittedToBank(), selectedRow.getAmount(),conn);
			}
			pList = obj.allReturnPromotersCapital(branchId);

			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
		}
	}

	private void updateExpense() 
	{
		HttpSession ss=(HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
		branchId = (int) ss.getAttribute("branchId");
		Connection conn = DataBaseConnection.JAVAConnection();
		voucherNo = obj.generateVoucherNo(branchId,"expense_table", conn);
		int x = new DataBaseIncomeExpense().addExpense(branchId,incomeDate,
				"Other_Dr", paymentMode, String.valueOf(extraAmount), chequeNo,
				bankBranch, submittedBankName, chequeDate, description,
				"Promoter Interest", "1", voucherNo, 0,"0","","");
		if (x >= 1) {
			// FacesContext.getCurrentInstance().addMessage(null, new
			// FacesMessage("Expense Added Successfully"));
			if (paymentMode.equals("Cash")) {
				obj.increaseSharedCompanyAmount(branchId,Double.valueOf(extraAmount),
						conn);
			} else if (paymentMode.equals("Cash") && transfer.equals("Yes")) {
				obj.decreaseBankBalance(submittedBankName, amount, conn);
			}
			try {
				conn.close();
			} catch (Exception e) {
				// TODO: handle exception
			}
		}
	}

	/*
	 * public void delete() { String status=selectedBank.getStatus(); double
	 * amount=selectedBank.getAmount(); String bank=selectedBank.getBankId();
	 * String transferBank=selectedBank.getTransferBankId(); int
	 * i=obj.deleteBankTransactionEntry(selectedBank.getId()); if(i>=1) {
	 * bank_info = obj.allBankTransactionInfo(); FacesContext
	 * fc=FacesContext.getCurrentInstance(); fc.addMessage(null, new
	 * FacesMessage("Entry Deleted successfully"));
	 * if(status.equals("deposited")) { obj.decreaseSharedCompanyAmount(amount);
	 * obj.decreaseBankBalance(bank, amount); } else {
	 * if(transferBank.equals("N/A")) {
	 * obj.decreaseCapitalCompanyAmount(amount); obj.increaseBankBalance(bank,
	 * amount); } else { obj.increaseBankBalance(bank, amount);
	 * obj.decreaseBankBalance(transferBank, amount); } } } }
	 */

	public ArrayList<UserInfo> getpList() {
		return pList;
	}

	public void setpList(ArrayList<UserInfo> pList) {
		this.pList = pList;
	}

	public String getBasis() {
		return basis;
	}

	public void setBasis(String basis) {
		this.basis = basis;
	}

	public ArrayList<UserInfo> getRlist() {
		return rlist;
	}

	public void setRlist(ArrayList<UserInfo> rlist) {
		this.rlist = rlist;
	}

	public boolean isShowView() {
		return showView;
	}

	public void setShowView(boolean showView) {
		this.showView = showView;
	}

	public boolean isShowReturn() {
		return showReturn;
	}

	public void setShowReturn(boolean showReturn) {
		this.showReturn = showReturn;
	}

	public UserInfo getSelectedRow() {
		return selectedRow;
	}

	public void setSelectedRow(UserInfo selectedRow) {
		this.selectedRow = selectedRow;
	}

	public ArrayList<SelectItem> getBankList() {
		return bankList;
	}

	public void setBankList(ArrayList<SelectItem> bankList) {
		this.bankList = bankList;
	}

	public String getSelectedPromotor() {
		return selectedPromotor;
	}

	public void setSelectedPromotor(String selectedPromotor) {
		this.selectedPromotor = selectedPromotor;
	}

	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 getSubmittedBankName() {
		return submittedBankName;
	}

	public void setSubmittedBankName(String submittedBankName) {
		this.submittedBankName = submittedBankName;
	}

	public String getTransfer() {
		return transfer;
	}

	public void setTransfer(String transfer) {
		this.transfer = transfer;
	}

	public String getLabel() {
		return label;
	}

	public void setLabel(String label) {
		this.label = label;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public boolean isShowCheque() {
		return showCheque;
	}

	public void setShowCheque(boolean showCheque) {
		this.showCheque = showCheque;
	}

	public boolean isShowFields() {
		return showFields;
	}

	public void setShowFields(boolean showFields) {
		this.showFields = showFields;
	}

	public boolean isShowMode() {
		return showMode;
	}

	public void setShowMode(boolean showMode) {
		this.showMode = showMode;
	}

	public boolean isShowFixedDeposit() {
		return showFixedDeposit;
	}

	public void setShowFixedDeposit(boolean showFixedDeposit) {
		this.showFixedDeposit = showFixedDeposit;
	}

	public boolean isRenderReturn() {
		return renderReturn;
	}

	public void setRenderReturn(boolean renderReturn) {
		this.renderReturn = renderReturn;
	}

	public boolean isRenderItem() {
		return renderItem;
	}

	public void setRenderItem(boolean renderItem) {
		this.renderItem = renderItem;
	}

	public boolean isShowTransfer() {
		return showTransfer;
	}

	public void setShowTransfer(boolean showTransfer) {
		this.showTransfer = showTransfer;
	}

	public boolean isShowTransferSelect() {
		return showTransferSelect;
	}

	public void setShowTransferSelect(boolean showTransferSelect) {
		this.showTransferSelect = showTransferSelect;
	}

	public boolean isShowButtonFdr() {
		return showButtonFdr;
	}

	public void setShowButtonFdr(boolean showButtonFdr) {
		this.showButtonFdr = showButtonFdr;
	}

	public boolean isShowButtonCommon() {
		return showButtonCommon;
	}

	public void setShowButtonCommon(boolean showButtonCommon) {
		this.showButtonCommon = showButtonCommon;
	}

	public Date getIncomeDate() {
		return incomeDate;
	}

	public void setIncomeDate(Date incomeDate) {
		this.incomeDate = incomeDate;
	}

	public Date getChequeDate() {
		return chequeDate;
	}

	public void setChequeDate(Date chequeDate) {
		this.chequeDate = chequeDate;
	}

	public double getAmount() {
		return amount;
	}

	public void setAmount(double amount) {
		this.amount = amount;
	}

	public double getExtraAmount() {
		return extraAmount;
	}

	public void setExtraAmount(double extraAmount) {
		this.extraAmount = extraAmount;
	}

	public boolean isShowViewReturn() {
		return showViewReturn;
	}

	public void setShowViewReturn(boolean showViewReturn) {
		this.showViewReturn = showViewReturn;
	}

	public boolean isShowExtraAmt() {
		return showExtraAmt;
	}

	public void setShowExtraAmt(boolean showExtraAmt) {
		this.showExtraAmt = showExtraAmt;
	}

}
