package report_package;

import java.io.Serializable;
import java.sql.Connection;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;

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.DataBaseConnection;
import main_package.DataBaseMethods;
import main_package.DataBaseMethodsHemant;

import org.primefaces.context.RequestContext;

@ManagedBean(name="incomeExpenseLedger")
@ViewScoped

public class IncomeExpenseLedgerBean implements Serializable
{	DataBaseReportMethods obj=new DataBaseReportMethods();
	Date startDate=new Date(), endDate=new Date();
	String mainCategory,subCategory,label;
	ArrayList<SelectItem> subCategoryList;
	ArrayList<MembersOdInfo> ledgerDataList,assetList,liabilityList;
	Boolean show,rndrValCr,rndrValDr;
	double totalCreditAmount,totalDebitAmount;
	int branchId;
	String branchUserId;
	List<SelectItem> branchList = new ArrayList<SelectItem>();
	DataBaseMethodsHemant dbmh = new DataBaseMethodsHemant();

	boolean disAll;
	String userType;
	
	public IncomeExpenseLedgerBean() 
	{
		Connection con = DataBaseConnection.JAVAConnection();
		startDate.setDate(1);
		HttpSession ss=(HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
		branchUserId = (String) ss.getAttribute("branchUserId");
		branchList = new DataBaseMethods().branchList(branchUserId,con);
		
		String cc=(String) ss.getAttribute("companyName");
        
		userType= (String) ss.getAttribute("type");
		
	    disAll= dbmh.disAllByCompanyNameUserType(userType,cc);
	    
		try {
			con.close();
		} catch (Exception e) {
			// TODO: handle exception
		}
	}
	
	public void searchSubcategory()
	{	
		HttpSession ss=(HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
		int branchId = (int) ss.getAttribute("branchId");
		Connection conn = DataBaseConnection.JAVAConnection();
		subCategoryList= new ArrayList<>();
		subCategoryList= obj.subcategoryList(branchId,mainCategory,conn);
		if(mainCategory.equalsIgnoreCase("liabilties"))
		{
			SelectItem obj4= new SelectItem();
			obj4.setValue("Share");
			obj4.setLabel("Share Account");
			subCategoryList.add(obj4);
			
			SelectItem obj= new SelectItem();
			obj.setValue("DD");
			obj.setLabel("DD Account");
			subCategoryList.add(obj);
			
			SelectItem obj1= new SelectItem();
			obj1.setValue("RD");
			obj1.setLabel("RD Account");
			subCategoryList.add(obj1);
			
			SelectItem obj2= new SelectItem();
			obj2.setValue("FD");
			obj2.setLabel("FD Account");
			subCategoryList.add(obj2);
			
			SelectItem obj3= new SelectItem();
			obj3.setValue("MIS");
			obj3.setLabel("MIS Account");
			subCategoryList.add(obj3);
			
			SelectItem obj5= new SelectItem();
			obj5.setValue("Saving");
			obj5.setLabel("Saving Account");
			subCategoryList.add(obj5);
			
			SelectItem obj6= new SelectItem();
			obj6.setValue("Promotor");
			obj6.setLabel("Promotor Account");
			subCategoryList.add(obj6);
		}
		
		else if(mainCategory.equalsIgnoreCase("income"))
		{
			SelectItem obj1= new SelectItem();
			obj1.setValue("Interest Account");
			obj1.setLabel("Interest Account");
			subCategoryList.add(obj1);
			
			SelectItem obj2= new SelectItem();
			obj2.setValue("Applicant Fee(RD/FD)");
			obj2.setLabel("Applicant Fee(RD/FD)");
			subCategoryList.add(obj2);
		}
		
		else if(mainCategory.equalsIgnoreCase("assets"))
		{
			SelectItem obj1= new SelectItem();
			obj1.setValue("Loan Account");
			obj1.setLabel("Loan Account");
			subCategoryList.add(obj1);
			
			SelectItem obj2= new SelectItem();
			obj2.setValue("FDR Account");
			obj2.setLabel("FDR Account");
			subCategoryList.add(obj2);
		}
		
		else if(mainCategory.equalsIgnoreCase("expense"))
		{
			SelectItem obj= new SelectItem();
			obj.setValue("Interest Paid");
			obj.setLabel("Interest Paid");
			subCategoryList.add(obj);

			SelectItem obj1= new SelectItem();
			obj1.setValue("Salary Account");
			obj1.setLabel("Salary Account");
			subCategoryList.add(obj1);
			
			SelectItem obj2= new SelectItem();
			obj2.setValue("MIS Interest Paid");
			obj2.setLabel("MIS Interest Paid");
			subCategoryList.add(obj2);
		}
		
		try {
			conn.close();
		} catch (Exception e) {
			// TODO: handle exception
		}
	}

	public void view()
	{		
		if(branchId != 0)
		{
			Connection conn=DataBaseConnection.JAVAConnection();
			show=true;
			int count=1;
			ledgerDataList= new ArrayList<>();
			
			// income and expense Ledger
			if(mainCategory.equalsIgnoreCase("income") || mainCategory.equalsIgnoreCase("expense"))
			{
				if(subCategory.equalsIgnoreCase("Salary Account"))
				{
					ledgerDataList=obj.salaryLedgerList(branchId,subCategory, startDate, endDate, conn);
				}
				else if(subCategory.equalsIgnoreCase("Interest Account"))
				{
					ledgerDataList=obj.loanInterestLedgerList(branchId,subCategory, startDate, endDate, conn);
				}
				else if(subCategory.equalsIgnoreCase("Interest Paid"))
				{
					ledgerDataList=obj.depositInterestPaidLedgerList(branchId,subCategory, startDate, endDate, conn);
				}
				else
				{
					ledgerDataList= obj.ledgerDataList(branchId,subCategory,mainCategory, startDate, endDate, conn);
				}
			}
			
			else if(mainCategory.equalsIgnoreCase("liabilties"))
			{
				if(subCategory.equalsIgnoreCase("DD") || subCategory.equalsIgnoreCase("RD") || subCategory.equalsIgnoreCase("FD"))
				{
					liabilityList=obj.ddRdFdLedgerList(branchId,subCategory, startDate, endDate, conn);
				}
				else if(subCategory.equalsIgnoreCase("Share"))
				{
					liabilityList=obj.shareLedgerList(branchId,subCategory, startDate, endDate, conn);
				}
				else if(subCategory.equalsIgnoreCase("Saving"))
				{
					liabilityList=obj.savingLedgerList(branchId,subCategory, startDate, endDate, conn);
				}
				else if(subCategory.equalsIgnoreCase("Promotor"))
				{
					liabilityList=obj.promotorLedgerList(branchId,subCategory, startDate, endDate, conn);
				}
				else
				{
					liabilityList=obj.otherLiabilityList(branchId,subCategory,mainCategory, startDate, endDate, conn);
				}
				ledgerDataList.addAll(liabilityList);
			}
			
			// Assets All Ledger Detail 
			else
			{
				if(subCategory.equalsIgnoreCase("Loan Account"))
				{
					assetList=obj.loanPrincipalLedgerList(branchId,subCategory, startDate, endDate, conn);
				}
				else if(subCategory.equalsIgnoreCase("FDR Account"))
				{
					assetList=obj.fdrLedgerList(branchId,subCategory, startDate, endDate, conn);
				}
				else
				{
					assetList=obj.assetList(branchId,subCategory, mainCategory, startDate, endDate, conn);
				}
			
				ledgerDataList.addAll(assetList);
			}
			
			Collections.sort(ledgerDataList);
			double balance=0;
			for(int i=0;i<ledgerDataList.size();i++)
			{
				ledgerDataList.get(i).setSno(count++);
				balance=(balance+ledgerDataList.get(i).getCredit())-ledgerDataList.get(i).getDebit();
				ledgerDataList.get(i).setBalance(Double.parseDouble(new DecimalFormat("##.##").format(balance)));
				ledgerDataList.get(i).setBalance(Math.abs(ledgerDataList.get(i).getBalance()));
			}
			
			totalCreditAmount=totalDebitAmount=0;
			for(MembersOdInfo obj:ledgerDataList)
			{
				totalCreditAmount=totalCreditAmount+obj.getCredit();
				totalDebitAmount=totalDebitAmount+obj.getDebit();
			}
			totalCreditAmount=Double.parseDouble(new DecimalFormat("##.##").format(totalCreditAmount));
			totalDebitAmount=Double.parseDouble(new DecimalFormat("##.##").format(totalDebitAmount));
			if(mainCategory.equals("income"))
			{
				label="Credit";
			}
			else if(mainCategory.equals("expense"))
			{
				label="Debit";
			}
			
			try {
				conn.close();
			} catch (Exception e) {
				// TODO: handle exception
			}
		}
		else
		{
			FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Select Branch"));
		}
		
	}
	
public void print()
{
	
	HttpSession ss=(HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
	ss.setAttribute("ledgerList",ledgerDataList);
	ss.setAttribute("ledgerCat",subCategory);
	RequestContext.getCurrentInstance().execute("window.open('printIncomeExpenseLedger.xhtml')");
	
}
	
	
	public String getMainCategory() {
		return mainCategory;
	}

	public void setMainCategory(String mainCategory) {
		this.mainCategory = mainCategory;
	}

	public ArrayList<SelectItem> getSubCategoryList() {
		return subCategoryList;
	}

	public void setSubCategoryList(ArrayList<SelectItem> subCategoryList) {
		this.subCategoryList = subCategoryList;
	}

	public String getSubCategory() {
		return subCategory;
	}

	public void setSubCategory(String subCategory) {
		this.subCategory = subCategory;
	}

	public ArrayList<MembersOdInfo> getLedgerDataList() {
		return ledgerDataList;
	}

	public void setLedgerDataList(ArrayList<MembersOdInfo> ledgerDataList) {
		this.ledgerDataList = ledgerDataList;
	}

	public Boolean getShow() {
		return show;
	}

	public void setShow(Boolean show) {
		this.show = show;
	}

	public double getTotalCreditAmount() {
		return totalCreditAmount;
	}

	public void setTotalCreditAmount(double totalCreditAmount) {
		this.totalCreditAmount = totalCreditAmount;
	}

	public String getLabel() {
		return label;
	}

	public void setLabel(String label) {
		this.label = label;
	}

	public double getTotalDebitAmount() {
		return totalDebitAmount;
	}

	public void setTotalDebitAmount(double totalDebitAmount) {
		this.totalDebitAmount = totalDebitAmount;
	}

	public Boolean getRndrValCr() {
		return rndrValCr;
	}

	public void setRndrValCr(Boolean rndrValCr) {
		this.rndrValCr = rndrValCr;
	}

	public Boolean getRndrValDr() {
		return rndrValDr;
	}

	public void setRndrValDr(Boolean rndrValDr) {
		this.rndrValDr = rndrValDr;
	}


	public ArrayList<MembersOdInfo> getLiabilityList() {
		return liabilityList;
	}

	public void setLiabilityList(ArrayList<MembersOdInfo> liabilityList) {
		this.liabilityList = liabilityList;
	}

	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<MembersOdInfo> getAssetList() {
		return assetList;
	}

	public void setAssetList(ArrayList<MembersOdInfo> assetList) {
		this.assetList = assetList;
	}

	public int getBranchId() {
		return branchId;
	}

	public void setBranchId(int branchId) {
		this.branchId = branchId;
	}

	public List<SelectItem> getBranchList() {
		return branchList;
	}

	public void setBranchList(List<SelectItem> branchList) {
		this.branchList = branchList;
	}

	public boolean isDisAll() {
		return disAll;
	}

	public void setDisAll(boolean disAll) {
		this.disAll = disAll;
	}

	public String getUserType() {
		return userType;
	}

	public void setUserType(String userType) {
		this.userType = userType;
	}

	

}




