package main_package;

import group_head_package.DataBaseIncomeExpense;

import java.io.IOException;
import java.io.Serializable;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
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;
import org.primefaces.model.UploadedFile;

@ManagedBean(name="manageMember")
@ViewScoped
public class ManageMemberBean implements Serializable
{
	DataBaseMethods obj=new DataBaseMethods();
	private static final long serialVersionUID = 1L;
	UserInfo user_info,selecteduser;
	ArrayList<UserInfo> list,multiSelectList, centerAllotList;
	String mmbrno,name,img,dsaType;
	boolean value, value1, value2, disDsa, renShareFiled, autoPl, autoGl, autoSl, autoDeposit, autoSaving;
	Date chequeDate, currentDate=new Date(),shareReturnDate=new Date(),office_date=new Date();
	double amount,penalty;
	String memberNo[];
	ArrayList<UserInfo> multipleRows =new ArrayList<UserInfo>();
	ArrayList<UserInfo> list1= new ArrayList<>();
	String id,user_no,receipt_no, chequeNo, bankName, paymentMode,label,submittedBankName="",bankNameCheque="", newDsaCode, center, newCenter;
	int total,branchId;
	UserInfo info=new UserInfo();
	String doc1Path,doc2Path,doc3Path,doc4Path,doc5Path,doc6Path,doc7Path,doc8Path,doc9Path,doc10Path,doc11Path,shareNo, applicant_photoPath,perAddress,signPath,formPath;
	transient UploadedFile applicant_photo,signPhoto,formPhoto, doc1photo, doc2photo, doc3photo, doc4photo, doc5photo,doc6photo,doc7photo,doc8photo,doc9photo,doc10photo,doc11photo;
	double shareAmount,shareTotalAmount;
	double noOfShare,totalAmount,appFee,totalShareAmount,totalShareNo=0;
	ArrayList<SelectItem> bankList, dsaList, memberNoList, centerList;
	String companyType;
	boolean showLoginRe;
	DbYashwant dby= new DbYashwant();
	DataBaseMethodRahul dbr = new DataBaseMethodRahul();
	DataBaseMethodsHemant dbh= new DataBaseMethodsHemant();
	String onlineFileViewPath;
	public ManageMemberBean() 
	{
		HttpSession ss=(HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
		branchId =(int) ss.getAttribute("branchId");
		String companyName = (String) ss.getAttribute("companyName");
		companyType=(String) ss.getAttribute("companyType");
		String username = (String) ss.getAttribute("username");
		String designation = DataBaseMethodsHemant.getDesignationByUname(username);
//		if(companyType.equalsIgnoreCase("NBFC"))
//		{
//			showLoginRe=true;
//		}
		
		if("NBFC".equalsIgnoreCase(companyType))
		{
			showLoginRe=true;
		}

		Connection conn=DataBaseConnection.JAVAConnection();
		centerList=obj.allcenter(branchId,"all",conn);
		bankList=new DataBaseIncomeExpense().allBankList(conn);    // Company Banks List
		String userId=obj.userId();
		dsaList=obj.allDsaList(branchId,"Loan",conn);
		String empDataStatus=obj.checkSMessageApiDetailFromTable("employee_wise_data", "",conn);
//		if(obj.userType().equals("employee") && empDataStatus.equalsIgnoreCase("active"))
//		{
//			dsaType=obj.dsaCodeByEmpUserId(userId, conn);
//			disDsa=true;
//		}
		if ("SUDERSHANA FINANCE".equalsIgnoreCase(companyName)) 
		{
			if (obj.userType().equals("employee"))
			{
				if ("BRANCH MANAGER".equalsIgnoreCase(designation))
				{
					dsaType=obj.dsaCodeByEmpUserId(userId, conn);
					disDsa=false;
				}
				else 
				{
					dsaType=obj.dsaCodeByEmpUserId(userId, conn);
					disDsa=true;
				}
			}
			else 
			{
				dsaType=obj.dsaCodeByEmpUserId(userId, conn);
				disDsa=false;
			}
		}
		else if (obj.userType().equals("employee") && empDataStatus.equalsIgnoreCase("active"))
		{
			dsaType=obj.dsaCodeByEmpUserId(userId, conn);
			disDsa=true;
		}
		
		memberNoListMethod();
		
		renShareFiled=obj.checkShareAllotmentFromMember();
		
		filePathDetail(conn);
		
		try {
			conn.close();
		} catch (Exception e) {
			// TODO: handle exception
		}
	}
	
	public void memberNoListMethod()
	{
		Connection conn=DataBaseConnection.JAVAConnection();
		memberNoList=obj.memberListDsaEmpWise(branchId,dsaType,conn);
		try {
			conn.close();
		} catch (Exception e) {
			// TODO: handle exception
		}
	}
	
	public void memberNoListByCenter()
	{
		Connection conn=DataBaseConnection.JAVAConnection();
		memberNoList=obj.memberListCenterWise(branchId,center,conn);
		centerAllotList=obj.centerAllotListMethod(branchId,center,conn);
		try {
			conn.close();
		} catch (Exception e) {
			// TODO: handle exception
		}
	}

	public void updateDsa()
	{
		if(multiSelectList.size()>0)
		{
			int count=0;
			Connection conn=DataBaseConnection.JAVAConnection();
			for(UserInfo uu: multiSelectList)
			{
				if(uu.getDsaCode()==null || !uu.getDsaCode().equals(newDsaCode))
				{
					count+=obj.updateDsaInLoanAccount(uu.getId(), newDsaCode, "shareholder_info", conn);
					
					if(autoPl==true)
					{
						obj.updateDsaInCommanTable(uu.getShareHolderNo(), newDsaCode, "personalloan", conn);
					}
					
					if(autoSl==true)
					{
						obj.updateDsaInCommanTable(uu.getShareHolderNo(), newDsaCode, "secure_loan", conn);
					}
					
					if(autoGl==true)
					{
						obj.updateDsaInCommanTable(uu.getShareHolderNo(), newDsaCode, "grouploan", conn);
					}
					
					if(autoSaving==true)
					{
						obj.updateDsaInCommanTable(uu.getShareHolderNo(), newDsaCode, "savingaccount", conn);
					}
					
					if(autoDeposit==true)
					{
						obj.updateDsaInCommanTable(uu.getShareHolderNo(), newDsaCode, "customer_info", conn);
					}
				}
			}
			
			FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(count+" Account Has been Successfully Tranfer in the Code of "+newDsaCode));
		  searchData();
			try {
				conn.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		else
		{
			FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Dear User, Please Select at least one Loan Account for Transfer"));
		}
	}
	
	public void searchData()
	{
		HttpSession ss=(HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
		int branchId =(int) ss.getAttribute("branchId");
		Connection conn=DataBaseConnection.JAVAConnection();
		
		if(memberNo==null || memberNo.length==0)
		{
			list = dbh.allShareHolders(branchId,dsaType, conn,center);
		}
		else if(!dsaType.equalsIgnoreCase("all")) 
		{
			
			list=dbh.multipleShareHolders(memberNo, conn,center);
			
		}
		else
		{
			list=dbh.multipleShareHolders(memberNo, conn,center);
		}
		total=list.size();	
		
		int k=0;
		for(int j=total; j>0;j--)
		{
			list.get(k).setSno(j);
			k++;
		}
		
		try {
			conn.close();
		} catch (Exception e) {
			// TODO: handle exception
		}
	}
	
	public void viewDetails() throws IOException
	{
		HttpSession ss=(HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
		ss.setAttribute("viewselecteduser", selecteduser);
		
		RequestContext.getCurrentInstance().execute("window.open('viewShareHolder.xhtml')");
	}
	
	public void printRegFormDetails()
	{
		HttpSession ss=(HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
		selecteduser=new DataBaseMethods().allShareHoldersForView(selecteduser.getShareHolderNo(),selecteduser);
		System.out.println("selected user religion name is:"+selecteduser.getReligionName());
		ss.setAttribute("selecteduserForPrintRegForm",selecteduser);
		
		RequestContext.getCurrentInstance().execute("window.open('printMemberRegForm.xhtml')");
		RequestContext.getCurrentInstance().execute("window.open('printMemberAddRecipt.xhtml')");
	}
	
	public void updateCenter()
	{
		if(multiSelectList.size()>0)
		{
			Connection conn=DataBaseConnection.JAVAConnection();
			for(UserInfo uu: multiSelectList)
			{
				if(uu.getCenterId()==null || !uu.getCenterId().equals(newCenter))
				{
					obj.updateCenterInCommanTable(uu.getShareHolderNo(), newCenter, "shareholder_info", conn);
					obj.updateCenterInCommanTable(uu.getShareHolderNo(), newCenter, "personalloan", conn);
					obj.updateCenterInCommanTable(uu.getShareHolderNo(), newCenter, "secure_loan", conn);
					obj.updateCenterInCommanTable(uu.getShareHolderNo(), newCenter, "grouploan", conn);
					obj.updateCenterInCommanTable(uu.getShareHolderNo(), newCenter, "savingaccount", conn);
				}
			}
			
			FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Member Has been Successfully Tranfer in this Center"));
		  searchData();
			try {
				conn.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		else
		{
			FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Dear User, Please Select at least one Loan Account for Transfer"));
		}
	}
	
	public void viewDocument()
	{
		 info=new DataBaseMethods().allShareHoldersForView(selecteduser.getShareHolderNo(),info);
		 shareNo = info.getShareHolderNo();
		 applicant_photoPath=info.getApplicant_photoForEdit();
		 signPath=info.getSignPhotoForEdit();
		 formPath=info.getFormPhotoForEdit();
		 doc1Path=info.getDoc1PathForEdit();
		 doc2Path=info.getDoc2PathForEdit();
		 doc3Path=info.getDoc3PathForEdit();
		 doc4Path=info.getDoc4PathForEdit();
		 doc5Path=info.getDoc5PathForEdit();
		 doc6Path=info.getDoc6PathForEdit();
		 doc7Path=info.getDoc7PathForEdit();
		 doc8Path=info.getDoc8PathForEdit();
		 doc9Path=info.getDoc9PathForEdit();
		 doc10Path=info.getDoc10PathForEdit();
		 doc11Path=info.getDoc11PathForEdit();
	}
	
	public void updateDocument() throws IOException
	{
		if (applicant_photo != null && applicant_photo.getSize() > 0) 
		{
			applicant_photoPath = applicant_photo.getFileName();
			String exten[]=applicant_photoPath.split("\\.");
			applicant_photoPath=shareNo+"_1"+"."+exten[1];
			obj.makeProfile(applicant_photo, applicant_photoPath);
		}
		
		if (signPhoto != null && signPhoto.getSize() > 0) 
		{
			signPath = signPhoto.getFileName();
			String exten[]=signPath.split("\\.");
			signPath=shareNo+"_2"+"."+exten[1];
			obj.makeProfile(signPhoto, signPath);
		}
		
		if (formPhoto != null && formPhoto.getSize() > 0) 
		{
			formPath = formPhoto.getFileName();
			String exten[]=formPath.split("\\.");
			formPath=shareNo+"_3"+"."+exten[1];
			obj.makeProfile(formPhoto, formPath);
		}
		
		if (doc1photo != null && doc1photo.getSize() > 0) 
		{
			doc1Path = doc1photo.getFileName();
			String exten[]=doc1Path.split("\\.");
			doc1Path=shareNo+"_4"+"."+exten[1];
			obj.makeProfile(doc1photo, doc1Path);
		}
		
		if (doc2photo != null && doc2photo.getSize() > 0) 
		{
			doc2Path = doc2photo.getFileName();
			String exten[]=doc2Path.split("\\.");
			doc2Path=shareNo+"_5"+"."+exten[1];
			obj.makeProfile(doc2photo, doc2Path);
		}
		
		
		if (doc3photo != null && doc3photo.getSize() > 0) 
		{
			doc3Path = doc3photo.getFileName();
			String exten[]=doc3Path.split("\\.");
			doc3Path=shareNo+"_6"+"."+exten[1];
			obj.makeProfile(doc3photo, doc3Path);
		}
		
		if (doc4photo != null && doc4photo.getSize() > 0) 
		{
			doc4Path = doc4photo.getFileName();
			String exten[]=doc4Path.split("\\.");
			doc4Path=shareNo+"_7"+"."+exten[1];
			obj.makeProfile(doc4photo, doc4Path);
		}
		
		
		if (doc5photo != null && doc5photo.getSize() > 0) 
		{
			doc5Path = doc5photo.getFileName();
			String exten[]=doc5Path.split("\\.");
			doc5Path=shareNo+"_8"+"."+exten[1];
			obj.makeProfile(doc5photo, doc5Path);
		}
		

		if (doc6photo != null && doc6photo.getSize() > 0) 
		{
			doc6Path = doc6photo.getFileName();
			String exten[]=doc6Path.split("\\.");
			doc6Path=shareNo+"_9"+"."+exten[1];
			obj.makeProfile(doc6photo, doc6Path);
		}
		
		if (doc7photo != null && doc7photo.getSize() > 0) 
		{
			doc7Path = doc7photo.getFileName();
			String exten[]=doc7Path.split("\\.");
			doc7Path=shareNo+"_10"+"."+exten[1];
			obj.makeProfile(doc7photo, doc7Path);
		}
		
		
		if (doc8photo != null && doc8photo.getSize() > 0) 
		{
			doc8Path = doc8photo.getFileName();
			String exten[]=doc8Path.split("\\.");
			doc8Path=shareNo+"_11"+"."+exten[1];
			obj.makeProfile(doc8photo, doc8Path);
		}
		
		
		
		
		
		if (doc9photo != null && doc9photo.getSize() > 0) 
		{
			doc9Path = doc9photo.getFileName();
			String exten[]=doc9Path.split("\\.");
			doc9Path=shareNo+"_12"+"."+exten[1];
			obj.makeProfile(doc9photo, doc9Path);
		}
		
		if (doc10photo != null && doc10photo.getSize() > 0) 
		{
			doc10Path = doc10photo.getFileName();
			String exten[]=doc10Path.split("\\.");
			doc10Path=shareNo+"_13"+"."+exten[1];
			obj.makeProfile(doc10photo, doc10Path);
		}
		
		
		if (doc11photo != null && doc11photo.getSize() > 0) 
		{
			doc11Path = doc11photo.getFileName();
			String exten[]=doc11Path.split("\\.");
			doc11Path=shareNo+"_14"+"."+exten[1];
			obj.makeProfile(doc11photo, doc11Path);
		}
		
		
		
		
		
		int i=obj.updateMemberDocumentDetail(info.getId(), applicant_photoPath,signPath,formPath,doc1Path, doc2Path, doc3Path, doc4Path, doc5Path, doc6Path, 
				doc7Path, doc8Path , doc9Path, doc10Path,doc11Path);
		if(i >= 1)
		{
			searchData();
			FacesContext fc=FacesContext.getCurrentInstance();
			fc.addMessage(null, new FacesMessage("Share Holder Updated Successfully"));
		}
		else
		{
			FacesContext fc=FacesContext.getCurrentInstance();
			fc.addMessage(null, new FacesMessage("Error in Documents Upload"));
		}
	}
	
	
	public void sub()
	{
		if(multipleRows.size()>0)
		{
			for(UserInfo ll: multipleRows)
			{
				UserInfo cate=  new UserInfo();
				cate.setShareHolderNo(ll.getShareHolderNo());
				cate.setApplicant_photo(ll.getApplicant_photo());
				cate.setFirst_applicantName(ll.getFirst_applicantName()+" / "+ll.getFather_husband_name());
				cate.setContactNo(ll.getContactNo());
				cate.setOffice_application_no(ll.getOffice_application_no());
				list1.add(cate);
			}
			HttpSession ss1=(HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(true);
	        ss1.setAttribute("list", list1);
	        ss1.setAttribute("center",center);
		}
		RequestContext.getCurrentInstance().execute("window.open('selectedAcccount.xhtml')");
	}	
    
	public String editDetails()
	{
		HttpSession ss=(HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
		ss.setAttribute("selecteduser", selecteduser);
		return "editShareHolder.xhtml";
	}
	
	public String returnShare() 
	{
		HttpSession ss=(HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
		int branchId =(int) ss.getAttribute("branchId");
		String svAccntNo=obj.savingAccountByShareHolderNo(selecteduser.getShareHolderNo());
		String totalAmount=String.valueOf(selecteduser.getTotalAmount());
		String crNo=selecteduser.getCrNo();
		String transferFrom=obj.savingAccountByShareHolderNo(selecteduser.getTransferFrom());
		double noOfShare=selecteduser.getNoOfShare();
		double preAmount=obj.acconuntHolderAmountByAccountNo(transferFrom);
		double noOfShareOftranferFrom=obj.noOfShareofTranfershareHolder(selecteduser.getTransferFrom());
		double totalnoofshare=noOfShareOftranferFrom+noOfShare;
		double checkAmountInsavingAccount=obj.savingAccountByAmount(transferFrom);
		boolean chcekShareAmountMember=obj.checkShareAmountFromMember();


		if(checkAmountInsavingAccount>Double.valueOf(totalAmount) && chcekShareAmountMember==true)
		{
			int returnShare = obj.shareReturnFromShareHolder(selecteduser);
			obj.updateTotalNoOfShare(selecteduser.getTransferFrom(), totalnoofshare);          // Update No. Of Share 
			obj.updateTotalAmountOfShare(selecteduser.getTransferFrom(), selecteduser.getTotalAmount());  // Update Total Amount  Of Share 
			if (returnShare > 0) 
			{
				int k = obj.userWithdrwal(branchId,transferFrom, totalAmount,
						shareReturnDate, "Cash", "", null, transferFrom, "","cleared", "Share Return amount to:" +svAccntNo, "");
				if (k > 0) 
				{
					double newAmount = preAmount - Double.valueOf(totalAmount);
					obj.updateSavingAmountByAccountNo(transferFrom, newAmount);
					obj.decreaseCapitalCompanyAmount(branchId,Double.valueOf(totalAmount));
					obj.enterValuesInShareReturnTable(branchId,transferFrom, crNo,shareReturnDate, noOfShare,selecteduser.getShareHolderNo());
					FacesContext.getCurrentInstance().addMessage(null,new FacesMessage("Share Returned Successfully"));
					
					return "manageMember";
				}
				else
				{
					FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("An Error Occured... Please Try Again"));
				}
			}
		}
		else 
		{
			int returnShare = obj.shareReturnFromShareHolder(selecteduser);
		
			if (returnShare > 0) 
			{
				obj.updateTotalNoOfShare(selecteduser.getTransferFrom(), totalnoofshare);
				obj.updateTotalAmountOfShare(selecteduser.getTransferFrom(), selecteduser.getTotalAmount());
				obj.enterValuesInShareReturnTable(branchId,selecteduser.getTransferFrom(), crNo,shareReturnDate, noOfShare,selecteduser.getShareHolderNo());
				FacesContext.getCurrentInstance().addMessage(null,new FacesMessage("Share Returned Successfully"));
				
				return "manageMember";
			}
		}
			return null;
	}
	
	public String closePlan()
	{
		HttpSession ss=(HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
		int branchId =(int) ss.getAttribute("branchId");
		String detail=obj.checkShareHolderAllAccount(selecteduser);
		if(!detail.trim().equalsIgnoreCase(""))
		{
			FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Account Can Not Be Closed.. Please Close The "+detail));
		}
		else
		{
			int k=obj.closeAccountOfShareHolder(selecteduser);
			if(k==1)
			{
				FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Account Closed SucessFully"));
				return "editViewDeleteShareHolder";
			}
			else
			{
				FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("An Error Occured... Please Try Again"));
			}
		}
		return null;
	}
	
	public void printDetails()
	{
		int id=Integer.valueOf(selecteduser.getId());
		HttpSession ss=(HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
		selecteduser=new DataBaseMethods().allShareHoldersForView(selecteduser.getShareHolderNo(),selecteduser);
		ss.setAttribute("shareHolderNo",selecteduser.getShareHolderNo());
		ss.setAttribute("office_date",selecteduser.getOffice_date());
		ss.setAttribute("first_applicantName",selecteduser.getFirst_applicantName());
		ss.setAttribute("dob",selecteduser.getDob());
		ss.setAttribute("address",selecteduser.getAddress());
		ss.setAttribute("nominee_address",selecteduser.getNominee_address());
		ss.setAttribute("nominee_name",selecteduser.getNominee_name());
		ss.setAttribute("nominee_dob",selecteduser.getNominee_dob());
		ss.setAttribute("nominee_relation",selecteduser.getNominee_relation());
		ss.setAttribute("noOfShare",selecteduser.getNoOfShare());
		ss.setAttribute("shareAmount",selecteduser.getShareAmount());
		ss.setAttribute("id", id);
		ss.setAttribute("crNo", selecteduser.getCrNo());
		
		RequestContext.getCurrentInstance().execute("window.open('shareHolderInfoPrint.xhtml')");
	}
	
	public void printForm4Details()
	{
		Connection conn=DataBaseConnection.JAVAConnection();
		String transferShareHolderName=obj.shareHolderNameByShareHolderNo(selecteduser.transferFrom, conn);
		int id=Integer.valueOf(selecteduser.getId());
		HttpSession ss=(HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
		selecteduser=new DataBaseMethods().allShareHoldersForView(selecteduser.getShareHolderNo(),selecteduser);
		ss.setAttribute("shareHolderNo",selecteduser.getShareHolderNo());
		ss.setAttribute("office_date",selecteduser.getOffice_date());
		ss.setAttribute("first_applicantName",selecteduser.getFirst_applicantName());
		ss.setAttribute("dob",selecteduser.getDob());
		ss.setAttribute("address",selecteduser.getAddress());
		ss.setAttribute("nominee_address",selecteduser.getNominee_address());
		ss.setAttribute("nominee_name",selecteduser.getNominee_name());
		ss.setAttribute("nominee_dob",selecteduser.getNominee_dob());
		ss.setAttribute("nominee_relation",selecteduser.getNominee_relation());
		ss.setAttribute("noOfShare",selecteduser.getNoOfShare());
		ss.setAttribute("shareAmount",selecteduser.getShareAmount());
		ss.setAttribute("id", id);
		ss.setAttribute("crNo", selecteduser.getCrNo());
		ss.setAttribute("transferNo", selecteduser.getTransferFrom());
		ss.setAttribute("transferName", transferShareHolderName);
		ss.setAttribute("fatherName", selecteduser.getFather_husband_name());
		
		
		try {
			conn.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		RequestContext.getCurrentInstance().execute("window.open('printFormNo4ShareHolder.xhtml')");
	}
	
	public void update()
	{
		if(paymentMode.equalsIgnoreCase("cash"))
		{
			bankNameCheque="";
			chequeNo="";
			submittedBankName="";
			addShare();
		}
		else
		{
			if(chequeNo.equalsIgnoreCase("") && submittedBankName.equalsIgnoreCase(""))
			{
				FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Please fill all the fields."));
			}
			else
			{
				addShare();
			}
		}
	}
	
	
	public void totalShareAmount()
	{
		Connection conn=DataBaseConnection.JAVAConnection();
	   shareTotalAmount=0.00;
	   shareAmount=obj.extractCompanyShareAmount(conn);
	   shareTotalAmount=shareAmount*noOfShare;
	   
	   try {
		conn.close();
	} catch (SQLException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
	}
	
	public String addShare()
	{
		HttpSession ss=(HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
		int branchId =(int) ss.getAttribute("branchId");
		Connection conn=DataBaseConnection.JAVAConnection();
		double companyShares=obj.checkRemainingCompanyShares();
		if(companyShares>=noOfShare)
		{
			totalAmount=noOfShare*shareAmount;
			if(paymentMode.equalsIgnoreCase("Cash"))
			{
				submittedBankName="N/A";
			}
			info=obj.allShareHoldersForView(selecteduser.getShareHolderNo(),info);
			
			int id=obj.addMoreShareOfExistingMember(branchId,info.getOffice_application_no(),office_date,info.getShareHolderNo(),info.getFirst_applicantName(),info.getDob(),
					info.getFather_husband_name(),info.getGuardian_name(),info.getGuardian_relation(),info.getStatus(),info.getPAN(),info.getBank_accountNo(),info.getBank_name()
					,info.getBranch(),info.getAddress(),info.getMobile(),
					info.getNominee_name(),info.getNominee_relation(),info.getNominee_dob(),info.getNominee_address(),applicant_photoPath,noOfShare,shareAmount,totalAmount,
					perAddress,signPath,formPath,info.getCategory(),info.getCrNo(), info.getVoterId(), info.getUniqueId(), info.getRationNo(), info.getDlNo(), info.getElectBill(),
					info.getResCert(), info.getAppCheque(), info.getgCheque(),submittedBankName,
					info.gettBill(),paymentMode,bankNameCheque,chequeNo,doc1Path, doc2Path, doc3Path, doc4Path, doc5Path, (int)noOfShare,conn);
			if(id>=1)
			{
				FacesContext fc=FacesContext.getCurrentInstance();
				fc.addMessage(null, new FacesMessage("Share Amount Added Successfully"));
				String crNo=obj.shareHolderCertificationNo(noOfShare,id, "share_info");
				obj.insertCertificationNoOfShareHolderNewShareInfoTable(crNo,id);       ////////// Update share certificate in new table
				//detailsToSet();
				//RequestContext.getCurrentInstance().execute("window.open('shareHolderInfoPrint.xhtml')");
				
				if(paymentMode.equalsIgnoreCase("Cash"))
			    {
			    	obj.increaseCompanyCapitalAmount(branchId,totalAmount, conn);
			    }
			    else
			    {
					fc.addMessage(null, new FacesMessage("Please Approve your Payment from Bank Clearance Module"));
			    }
				
				noOfShare=0;paymentMode="";bankNameCheque="";chequeNo="";submittedBankName="";
				
				return "manageMember.xhtml";
			}
			else
			{
				FacesContext fc=FacesContext.getCurrentInstance();
				fc.addMessage(null, new FacesMessage("An Error Occured... Please Try Again"));
			}
		}
		else
		{
			FacesContext fc=FacesContext.getCurrentInstance();
			fc.addMessage(null, new FacesMessage("Sorry! Company Do Not Have Sufficient Shares. Only '"+companyShares+"' Shares Are Remaining."));
		}
	
		return "";
	}
	
	
	public void setCreditLimit()
	{
		int i=dby.setLimit(selecteduser.getId(),selecteduser.getCreditLimit());
		
		if(i>0)
		{
			FacesContext fc=FacesContext.getCurrentInstance();
			fc.addMessage(null, new FacesMessage("Credit Limit Update Sucessfully"));
		}
	}
	public void loginfeeReceipt()
	{
		HttpSession ss=(HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
        ss.setAttribute("shareHolderNo",selecteduser.getShareHolderNo());
        RequestContext.getCurrentInstance().execute("window.open('loginReceipt.xhtml')");

	}
	
	public void filePathDetail(Connection conn)  
	{
		try
		{
			Statement st = conn.createStatement();
			String qq = "select * from firm_detail limit 1";
			ResultSet rs = st.executeQuery(qq);
			if(rs.next())
			{
				onlineFileViewPath = rs.getString("file_view_path");
			}
		}
		catch (Exception e)
		{
			 e.printStackTrace();
		} 
	}
	
	public UserInfo getUser_info() {
		return user_info;
	}
	public void setUser_info(UserInfo user_info) {
		this.user_info = user_info;
	}
	public UserInfo getSelecteduser() {
		return selecteduser;
	}
	public void setSelecteduser(UserInfo selecteduser) {
		this.selecteduser = selecteduser;
	}
	public ArrayList<UserInfo> getList() {
		return list;
	}
	public void setList(ArrayList<UserInfo> list) {
		this.list = list;
	}
	public boolean isValue() {
		return value;
	}
	public void setValue(boolean value) {
		this.value = value;
	}
	public boolean isValue1() {
		return value1;
	}
	public void setValue1(boolean value1) {
		this.value1 = value1;
	}
	public boolean isValue2() {
		return value2;
	}
	public void setValue2(boolean value2) {
		this.value2 = value2;
	}
	public Date getChequeDate() {
		return chequeDate;
	}
	public void setChequeDate(Date chequeDate) {
		this.chequeDate = chequeDate;
	}
	public Date getCurrentDate() {
		return currentDate;
	}
	public void setCurrentDate(Date currentDate) {
		this.currentDate = currentDate;
	}
	public double getAmount() {
		return amount;
	}
	public void setAmount(double amount) {
		this.amount = amount;
	}
	public double getPenalty() {
		return penalty;
	}
	public void setPenalty(double penalty) {
		this.penalty = penalty;
	}
	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	public String getUser_no() {
		return user_no;
	}
	public void setUser_no(String user_no) {
		this.user_no = user_no;
	}
	public String getReceipt_no() {
		return receipt_no;
	}
	public void setReceipt_no(String receipt_no) {
		this.receipt_no = receipt_no;
	}
	public String getChequeNo() {
		return chequeNo;
	}
	public void setChequeNo(String chequeNo) {
		this.chequeNo = chequeNo;
	}
	public String getBankName() {
		return bankName;
	}
	public void setBankName(String bankName) {
		this.bankName = bankName;
	}
	public String getPaymentMode() {
		return paymentMode;
	}
	public void setPaymentMode(String paymentMode) {
		this.paymentMode = paymentMode;
	}

	public ArrayList<UserInfo> getMultipleRows() {
		return multipleRows;
	}

	public String getMmbrno() {
		return mmbrno;
	}

	public void setMmbrno(String mmbrno) {
		this.mmbrno = mmbrno;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getImg() {
		return img;
	}

	public void setImg(String img) {
		this.img = img;
	}

	public void setMultipleRows(ArrayList<UserInfo> multipleRows) {
		this.multipleRows = multipleRows;
	}

	public ArrayList<UserInfo> getList1() {
		return list1;
	}

	public void setList1(ArrayList<UserInfo> list1) {
		this.list1 = list1;
	}

	public Date getShareReturnDate() {
		return shareReturnDate;
	}

	public void setShareReturnDate(Date shareReturnDate) {
		this.shareReturnDate = shareReturnDate;
	}

	public int getTotal() {
		return total;
	}

	public void setTotal(int total) {
		this.total = total;
	}

	public UserInfo getInfo() {
		return info;
	}

	public void setInfo(UserInfo info) {
		this.info = info;
	}

	public String getDoc1Path() {
		return doc1Path;
	}

	public void setDoc1Path(String doc1Path) {
		this.doc1Path = doc1Path;
	}

	public String getDoc2Path() {
		return doc2Path;
	}

	public void setDoc2Path(String doc2Path) {
		this.doc2Path = doc2Path;
	}

	public String getDoc3Path() {
		return doc3Path;
	}

	public void setDoc3Path(String doc3Path) {
		this.doc3Path = doc3Path;
	}

	public String getDoc4Path() {
		return doc4Path;
	}

	public void setDoc4Path(String doc4Path) {
		this.doc4Path = doc4Path;
	}

	public String getDoc5Path() {
		return doc5Path;
	}

	public void setDoc5Path(String doc5Path) {
		this.doc5Path = doc5Path;
	}

	public String getDoc6Path() {
		return doc6Path;
	}

	public void setDoc6Path(String doc6Path) {
		this.doc6Path = doc6Path;
	}

	public String getDoc7Path() {
		return doc7Path;
	}

	public void setDoc7Path(String doc7Path) {
		this.doc7Path = doc7Path;
	}

	public String getDoc8Path() {
		return doc8Path;
	}

	public void setDoc8Path(String doc8Path) {
		this.doc8Path = doc8Path;
	}

	public UploadedFile getApplicant_photo() {
		return applicant_photo;
	}

	public void setApplicant_photo(UploadedFile applicant_photo) {
		this.applicant_photo = applicant_photo;
	}

	public UploadedFile getSignPhoto() {
		return signPhoto;
	}

	public void setSignPhoto(UploadedFile signPhoto) {
		this.signPhoto = signPhoto;
	}

	public UploadedFile getFormPhoto() {
		return formPhoto;
	}

	public void setFormPhoto(UploadedFile formPhoto) {
		this.formPhoto = formPhoto;
	}

	public UploadedFile getDoc1photo() {
		return doc1photo;
	}

	public void setDoc1photo(UploadedFile doc1photo) {
		this.doc1photo = doc1photo;
	}

	public UploadedFile getDoc2photo() {
		return doc2photo;
	}

	public void setDoc2photo(UploadedFile doc2photo) {
		this.doc2photo = doc2photo;
	}

	public UploadedFile getDoc3photo() {
		return doc3photo;
	}

	public void setDoc3photo(UploadedFile doc3photo) {
		this.doc3photo = doc3photo;
	}

	public UploadedFile getDoc4photo() {
		return doc4photo;
	}

	public void setDoc4photo(UploadedFile doc4photo) {
		this.doc4photo = doc4photo;
	}

	public UploadedFile getDoc5photo() {
		return doc5photo;
	}

	public void setDoc5photo(UploadedFile doc5photo) {
		this.doc5photo = doc5photo;
	}

	public UploadedFile getDoc6photo() {
		return doc6photo;
	}

	public void setDoc6photo(UploadedFile doc6photo) {
		this.doc6photo = doc6photo;
	}

	public UploadedFile getDoc7photo() {
		return doc7photo;
	}

	public void setDoc7photo(UploadedFile doc7photo) {
		this.doc7photo = doc7photo;
	}

	public UploadedFile getDoc8photo() {
		return doc8photo;
	}

	public void setDoc8photo(UploadedFile doc8photo) {
		this.doc8photo = doc8photo;
	}

	public String getShareNo() {
		return shareNo;
	}

	public void setShareNo(String shareNo) {
		this.shareNo = shareNo;
	}

	public String getApplicant_photoPath() {
		return applicant_photoPath;
	}

	public void setApplicant_photoPath(String applicant_photoPath) {
		this.applicant_photoPath = applicant_photoPath;
	}

	public String getSignPath() {
		return signPath;
	}

	public void setSignPath(String signPath) {
		this.signPath = signPath;
	}

	public String getFormPath() {
		return formPath;
	}

	public void setFormPath(String formPath) {
		this.formPath = formPath;
	}

	public DataBaseMethods getObj() {
		return obj;
	}

	public void setObj(DataBaseMethods obj) {
		this.obj = obj;
	}

	public String getLabel() {
		return label;
	}

	public void setLabel(String label) {
		this.label = label;
	}

	public String getSubmittedBankName() {
		return submittedBankName;
	}

	public void setSubmittedBankName(String submittedBankName) {
		this.submittedBankName = submittedBankName;
	}

	public String getBankNameCheque() {
		return bankNameCheque;
	}

	public void setBankNameCheque(String bankNameCheque) {
		this.bankNameCheque = bankNameCheque;
	}

	public String getPerAddress() {
		return perAddress;
	}

	public void setPerAddress(String perAddress) {
		this.perAddress = perAddress;
	}

	public double getShareAmount() {
		return shareAmount;
	}

	public void setShareAmount(double shareAmount) {
		this.shareAmount = shareAmount;
	}

	public double getShareTotalAmount() {
		return shareTotalAmount;
	}

	public void setShareTotalAmount(double shareTotalAmount) {
		this.shareTotalAmount = shareTotalAmount;
	}

	public double getNoOfShare() {
		return noOfShare;
	}

	public void setNoOfShare(double noOfShare) {
		this.noOfShare = noOfShare;
	}

	public double getTotalAmount() {
		return totalAmount;
	}

	public void setTotalAmount(double totalAmount) {
		this.totalAmount = totalAmount;
	}
    
	public Date getOffice_date() {
		return office_date;
	}

	public void setOffice_date(Date office_date) {
		this.office_date = office_date;
	}

	public double getTotalShareAmount() {
		return totalShareAmount;
	}

	public void setTotalShareAmount(double totalShareAmount) {
		this.totalShareAmount = totalShareAmount;
	}

	public ArrayList<SelectItem> getBankList() {
		return bankList;
	}

	public void setBankList(ArrayList<SelectItem> bankList) {
		this.bankList = bankList;
	}

	public double getTotalShareNo() {
		return totalShareNo;
	}

	public void setTotalShareNo(double totalShareNo) {
		this.totalShareNo = totalShareNo;
	}

	public ArrayList<UserInfo> getMultiSelectList() {
		return multiSelectList;
	}

	public void setMultiSelectList(ArrayList<UserInfo> multiSelectList) {
		this.multiSelectList = multiSelectList;
	}

	public String getNewDsaCode() {
		return newDsaCode;
	}

	public void setNewDsaCode(String newDsaCode) {
		this.newDsaCode = newDsaCode;
	}

	public ArrayList<SelectItem> getDsaList() {
		return dsaList;
	}

	public void setDsaList(ArrayList<SelectItem> dsaList) {
		this.dsaList = dsaList;
	}

	public boolean isDisDsa() {
		return disDsa;
	}

	public void setDisDsa(boolean disDsa) {
		this.disDsa = disDsa;
	}

	public String getDsaType() {
		return dsaType;
	}

	public void setDsaType(String dsaType) {
		this.dsaType = dsaType;
	}

	public boolean isRenShareFiled() {
		return renShareFiled;
	}

	public void setRenShareFiled(boolean renShareFiled) {
		this.renShareFiled = renShareFiled;
	}

	public boolean isAutoPl() {
		return autoPl;
	}

	public void setAutoPl(boolean autoPl) {
		this.autoPl = autoPl;
	}

	public boolean isAutoGl() {
		return autoGl;
	}

	public void setAutoGl(boolean autoGl) {
		this.autoGl = autoGl;
	}

	public boolean isAutoSl() {
		return autoSl;
	}

	public void setAutoSl(boolean autoSl) {
		this.autoSl = autoSl;
	}

	public boolean isAutoDeposit() {
		return autoDeposit;
	}

	public void setAutoDeposit(boolean autoDeposit) {
		this.autoDeposit = autoDeposit;
	}

	public boolean isAutoSaving() {
		return autoSaving;
	}

	public void setAutoSaving(boolean autoSaving) {
		this.autoSaving = autoSaving;
	}

	public double getAppFee() {
		return appFee;
	}

	public void setAppFee(double appFee) {
		this.appFee = appFee;
	}

	
	public String[] getMemberNo() {
		return memberNo;
	}

	public void setMemberNo(String[] memberNo) {
		this.memberNo = memberNo;
	}

	public ArrayList<SelectItem> getMemberNoList() {
		return memberNoList;
	}

	public void setMemberNoList(ArrayList<SelectItem> memberNoList) {
		this.memberNoList = memberNoList;
	}

	public String getCenter() {
		return center;
	}

	public void setCenter(String center) {
		this.center = center;
	}

	public ArrayList<SelectItem> getCenterList() {
		return centerList;
	}

	public void setCenterList(ArrayList<SelectItem> centerList) {
		this.centerList = centerList;
	}

	public String getNewCenter() {
		return newCenter;
	}

	public void setNewCenter(String newCenter) {
		this.newCenter = newCenter;
	}

	public ArrayList<UserInfo> getCenterAllotList() {
		return centerAllotList;
	}

	public void setCenterAllotList(ArrayList<UserInfo> centerAllotList) {
		this.centerAllotList = centerAllotList;
	}

	public int getBranchId() {
		return branchId;
	}

	public void setBranchId(int branchId) {
		this.branchId = branchId;
	}

	public String getDoc9Path() {
		return doc9Path;
	}

	public void setDoc9Path(String doc9Path) {
		this.doc9Path = doc9Path;
	}

	public String getDoc10Path() {
		return doc10Path;
	}

	public void setDoc10Path(String doc10Path) {
		this.doc10Path = doc10Path;
	}

	public String getDoc11Path() {
		return doc11Path;
	}

	public void setDoc11Path(String doc11Path) {
		this.doc11Path = doc11Path;
	}

	public UploadedFile getDoc9photo() {
		return doc9photo;
	}

	public void setDoc9photo(UploadedFile doc9photo) {
		this.doc9photo = doc9photo;
	}

	public UploadedFile getDoc10photo() {
		return doc10photo;
	}

	public void setDoc10photo(UploadedFile doc10photo) {
		this.doc10photo = doc10photo;
	}

	public UploadedFile getDoc11photo() {
		return doc11photo;
	}

	public void setDoc11photo(UploadedFile doc11photo) {
		this.doc11photo = doc11photo;
	}

	public boolean isShowLoginRe() {
		return showLoginRe;
	}

	public void setShowLoginRe(boolean showLoginRe) {
		this.showLoginRe = showLoginRe;
	}

	public String getCompanyType() {
		return companyType;
	}

	public void setCompanyType(String companyType) {
		this.companyType = companyType;
	}

	public String getOnlineFileViewPath() {
		return onlineFileViewPath;
	}

	public void setOnlineFileViewPath(String onlineFileViewPath) {
		this.onlineFileViewPath = onlineFileViewPath;
	}
}
