package main_package;

import java.io.Serializable;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
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 org.primefaces.context.RequestContext;

import other_package.ReportInfo;
import report_package.BorrowerLenderInfo;
import report_package.DataBaseReportMethods;

@ManagedBean(name="profitAndLoss")
@ViewScoped

public class ProfitAndLossAccountBean implements Serializable
{
	double applicantFee=0, ddInterest, totalDepositPaidInterest, totalDepositAccruedInterest,loanInterestAmount=0,fdrInterestAmount=0,fdrAmount=0,otherIncome=0,rdInterest=0,fdInterest=0,savingsInterest=0,
			paidRdInterest,paidFdInterest,otherExpense=0,paidRdAmount=0,paidFdAmount=0,totalIncome=0,totalExpense=0,totalRdInterest=0
			,totalFdInterest=0,totalProfit=0,goodsAmount=0,goodsDepreciationAmount=0, salaryExp=0,paidDdInterest, preMatCharge, stationaryCharge, otherCharge,misIntrPaid;
	Date startDate,endDate=new Date();
	ArrayList<UserInfo> rdFdList,particularList;
	ArrayList<UserInfo> returnedRdList,returnedFdList,paidRdAmountList,paidFdAmountList;
	ArrayList<InstallmentInfo> paidInstallmentList,interestList;
	ArrayList<InstallmentInfo>finalList;
	Date tdyDate,standard,date1,date2,date3,date4,date5,date6,date7,date8;
	Timestamp tdydt,stndrd,dt1,dt2,dt3,dt4,dt5,dt6,dt7,dt8;
	boolean show,disEndDate, showSale, booleanAccuredDeposit,booleanAccuredLoan; 
	String strEndDate, strStartDate, label;
	DecimalFormat dc=new DecimalFormat("##.##");
	SimpleDateFormat df=new SimpleDateFormat("dd/MM/yyyy");
	DataBaseMethods db=new DataBaseMethods();
	DataBaseReportMethods obj=new DataBaseReportMethods();
	double totalAccruedLoanInterset, totalAmount, bookedExpense, bookedIncome,depAmount;
	ArrayList<String> loanList;
	ArrayList<LoanInfo> loanEMIList;
	ArrayList<InstallmentInfo> lastList;
	ArrayList<BorrowerLenderInfo>  saleList,dsaList,salaryLedgerList;
	ArrayList<ReportInfo> plList;
	
    int branchId;
    String branchUserId, userType;
    List<SelectItem> branchList = new ArrayList<SelectItem>();
		
	
	public ProfitAndLossAccountBean() throws ParseException 
	{	Connection conn=DataBaseConnection.JAVAConnection();
	
	//HttpSession ss=(HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
		HttpSession ss=(HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
		branchUserId = (String) ss.getAttribute("branchUserId");
		
		HeaderBean hh=new HeaderBean();
		
		userType= (String) ss.getAttribute("type"); //temp
		branchList = new DataBaseMethods().branchList(branchUserId,conn);

//		if(!db.previousFinancialYear(-1).equals(hh.getSelectedSession()))
		if(!db.previousFinancialYear(branchId).equals(hh.getSelectedSession()))
        {
            if(hh.getSelectedSession().contains("-"))
            {
                String aa[]=hh.getSelectedSession().split("-");
                Date dt=new Date();
                dt.setDate(31);
                dt.setMonth(2);
                dt.setYear(Integer.parseInt(aa[1])-1900);
                dt.setHours(0);
                dt.setMinutes(0);
                dt.setSeconds(0);
                endDate=dt;
            }
        }
		if(!hh.getSelectedSession().equals(hh.getCurrentSession()))
		{
			disEndDate=true;
			String eDate="31-03-"+hh.getEndFinYear();
			SimpleDateFormat f = new SimpleDateFormat("dd-MM-yyyy");
			endDate=f.parse(eDate);
		}
		else
		{
			disEndDate=false;
		}
	}
	
	public void income()
    {	
		//HttpSession ss=(HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
		Connection conn=DataBaseConnection.JAVAConnection();
        if(startDate!=null)
        {
            int i=1;
            
            totalAmount=0;
            label="Other Income";
            RequestContext context = RequestContext.getCurrentInstance(); context.execute("PF('dlg2').show();");
           // saleList=obj.saleFixedAssetsDetail("Gullak Security Money", branchId, startDate, endDate, conn);

            saleList=obj.saleFixedAssetsDetail("Gullak Security Money",  startDate, endDate, branchId);

        for(BorrowerLenderInfo ll : saleList)
        {
            totalAmount +=ll.getAmountIncomeExpense();
            ll.setSno(i++);
        }
        showSale=true;
        }
        else
        {
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("No Record Found"));
        }
    }
   
    public void expense()
    {
    //	HttpSession ss=(HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
	
        Connection conn=DataBaseConnection.JAVAConnection();
    if(startDate!=null)
    {
    	
        int i=1;
       
        RequestContext context = RequestContext.getCurrentInstance();
        context.execute("PF('dlg2').show();");
        totalAmount = 0;
        label = "Other Expense";
        saleList = obj.purchaseFixedAssetsDetail("", startDate, endDate, branchId);
        BorrowerLenderInfo mm = new BorrowerLenderInfo();
        mm.setItemName("Salary Pay");
        mm.setAmountIncomeExpense(Double.parseDouble(dc.format(db.salaryExpenseAmount(branchId,startDate,endDate,conn))));
        saleList.add(mm);
        BorrowerLenderInfo kk=new BorrowerLenderInfo();
        kk.setItemName("Return DSA Commission Amount");
        kk.setAmountIncomeExpense(Double.parseDouble(dc.format(db.dsaCommissionExpenseAmount(branchId,startDate,endDate,conn))));
        saleList.add(kk);
         
       
        /*salaryLedgerList=obj.salaryLedgerList("", startDate, endDate);
        dsaList=obj.dsaCommisionList("", startDate, endDate);
        saleList.addAll(salaryLedgerList);
        saleList.addAll(dsaList);*/
        System.out.println(kk.getAmountIncomeExpense());
        for(BorrowerLenderInfo ll : saleList)
        {
            totalAmount +=ll.getAmountIncomeExpense();
            ll.setSno(i++);
        }
        //// System.out.println("pur "+saleList.size());
        showSale=true;
    }
    else
    {
        FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("No Record Found"));
    }
       
        try {
            conn.close();
        } catch (Exception e) {
            // TODO: handle exception
        }
    }


	
	
	public void showPLAccount() throws ParseException
	{
		HttpSession ss=(HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
	
		DataBaseMethods obj=db;
		Connection conn=DataBaseConnection.JAVAConnection();
		
		HeaderBean hh=new HeaderBean();
		String sDate="01-04-"+hh.getStartFinYear();
		
		SimpleDateFormat f = new SimpleDateFormat("dd-MM-yyyy");
		startDate=f.parse(sDate);
		
		preMatCharge=stationaryCharge=otherCharge=applicantFee=loanInterestAmount=fdrInterestAmount=fdrAmount=otherIncome=rdInterest=fdInterest=savingsInterest=
					paidRdInterest=paidFdInterest=otherExpense=paidRdAmount=paidFdAmount=totalIncome=totalExpense=totalRdInterest=
					totalFdInterest=totalProfit=goodsAmount=goodsDepreciationAmount=salaryExp=ddInterest=totalDepositPaidInterest=paidDdInterest=totalDepositAccruedInterest=0;
		bookedExpense=bookedIncome=0;
		
		applicantFee=Double.parseDouble(dc.format(obj.totalApplicantFee(branchId,startDate,endDate,conn)));
		loanInterestAmount=Double.parseDouble(dc.format(obj.totalLoanInterest(branchId,startDate,endDate,conn)));
		
		fdrInterestAmount=Double.parseDouble(dc.format(obj.totalFdrInterestAmount(branchId,startDate, endDate,conn)));
		fdrAmount=Double.parseDouble(dc.format(obj.totalFdrInterestAmountFromFdrIncomeTable(branchId,startDate,endDate,conn)));
		fdrInterestAmount=Double.parseDouble(dc.format(fdrInterestAmount+fdrAmount));
		otherIncome=Double.parseDouble(dc.format(obj.totalOtherIncomeAmount(branchId,startDate,endDate,conn))); 
		
		if(booleanAccuredLoan==true)
		{
			totalAccruedLoanInterset=viewAccruedLoanReport();
		}
		else
		{
			totalAccruedLoanInterset=0;
		}
	
		
		
		// Charges on Deposit Pre-Maturity from Return Amount Info table
		/*preMatCharge=Double.parseDouble(dc.format(obj.totalPreMaturityChargesOnDeposit(startDate,endDate,conn, "pre_maturity_charge"))); 
		stationaryCharge=Double.parseDouble(dc.format(obj.totalPreMaturityChargesOnDeposit(startDate,endDate,conn, "stationary_charge"))); 
		otherCharge=Double.parseDouble(dc.format(obj.totalPreMaturityChargesOnDeposit(startDate,endDate,conn, "other_charge")));*/ 
		//bookedIncome=db.bookedIncomeExpenseAmount("Credit",-1,startDate,endDate,conn);
		bookedIncome=db.bookedIncomeExpenseAmount("Credit",branchId,startDate,endDate,conn);
		
		totalIncome=Double.parseDouble(dc.format(applicantFee+loanInterestAmount+fdrInterestAmount+otherIncome+totalAccruedLoanInterset+bookedIncome));
		
		//***********************************************************************************
		savingsInterest=Double.parseDouble(dc.format(obj.totalSavingsInterestAmount(branchId,startDate,endDate,conn)));
		
		if(!hh.getSelectedSession().equals(hh.getCurrentSession()))
		{
			if(booleanAccuredDeposit==true)
			{
				ddInterest=Double.parseDouble(dc.format(view("DD")));
				rdInterest=Double.parseDouble(dc.format(view("RD")));
				fdInterest=Double.parseDouble(dc.format(view("FD")));
			}
			else
			{
				ddInterest=rdInterest=fdInterest=0;
			}
		}
		//Accrued Interest on Deposit 
		
		paidDdInterest=Double.parseDouble(dc.format(paidDD(startDate,endDate)));
		paidRdInterest=Double.parseDouble(dc.format(paidRD(startDate,endDate)));
		//// System.out.println(fdInterest);
		paidFdInterest=Double.parseDouble(dc.format(paidFD(startDate,endDate)));
		//// System.out.println(paidFdInterest);
		//Paid RD/DD/FD Interest
			
		otherExpense=Double.parseDouble(dc.format(obj.totalOtherExpenseAmount(branchId,startDate,endDate,conn)));
		salaryExp=Double.parseDouble(dc.format(obj.salaryExpenseAmount(branchId,startDate,endDate,conn)));
		System.out.println("salary ecpanse is "+salaryExp);
		//misIntrPaid = obj.paidMISInterestBetweenDates(-1,startDate,endDate, conn);
		double dsaCommExp=Double.parseDouble(dc.format(obj.dsaCommissionExpenseAmount(branchId,startDate,endDate,conn)));
		otherExpense=otherExpense+salaryExp+dsaCommExp;
		//goodsAmount=Double.parseDouble(dc.format(obj.totalGoodsExpenseAmount(startDate,endDate)));
		
		
		totalDepositAccruedInterest=ddInterest+rdInterest+fdInterest;
		totalDepositAccruedInterest=Double.parseDouble(dc.format(totalDepositAccruedInterest));
		totalDepositPaidInterest=paidDdInterest+paidFdInterest+paidRdInterest;
		
		// System.out.println(paidDdInterest+" == "+paidFdInterest+" ==  "+paidRdInterest);
		totalDepositPaidInterest=Math.abs(totalDepositPaidInterest);
		
		//totalRdInterest=Double.parseDouble(dc.format(rdInterest+paidRdInterest));
		//totalFdInterest=Double.parseDouble(dc.format(fdInterest+paidFdInterest));
		//totalFdInterest=Double.parseDouble(dc.format(totalFdInterest));
		
		bookedExpense=db.bookedEntryAmountByTransTypeForProfitLoss("Debit", conn);
		depAmount=db.depreciationAmount(hh.getEndFinYear(), conn);
		
		totalExpense=Double.parseDouble(dc.format(savingsInterest+otherExpense+totalDepositAccruedInterest+totalDepositPaidInterest+misIntrPaid+bookedExpense+depAmount));
		
		strStartDate=new SimpleDateFormat("dd/MM/yyyy").format(startDate);
		strEndDate=new SimpleDateFormat("dd/MM/yyyy").format(endDate);
		totalProfit=Double.parseDouble(dc.format(totalIncome-totalExpense));
		
		// Profit and Losss Dynamic list work start as on 02/August/2020
		
		/*ReportInfo rr1=new ReportInfo();
		
		rr1.setExpenseHead("Savings Acc. Interest");
		rr1.setDebitAmount(savingsInterest);
		rr1.setIncomeHead("Applicant Fee(RD/FD)");
		rr1.setCreditAmount(applicantFee);
		
		plList.add(rr1);
		
		ReportInfo rr2=new ReportInfo();
		
		rr2.setExpenseHead("Paid Interest On Deposit's");
		rr2.setDebitAmount(totalDepositPaidInterest);
		rr2.setIncomeHead("Loan Interest");
		rr2.setCreditAmount(loanInterestAmount);
		
		plList.add(rr2);
		
		ReportInfo rr3=new ReportInfo();
		
		rr3.setExpenseHead("Accrued Interest On Deposit's");
		rr3.setDebitAmount(totalDepositAccruedInterest);
		rr3.setIncomeHead("Accrued Interest On Loan");
		rr3.setCreditAmount(totalAccruedLoanInterset);
		
		plList.add(rr3);
		
		ReportInfo rr4=new ReportInfo();
		
		rr4.setExpenseHead("Fixed Assets Goods");
		rr4.setDebitAmount(goodsAmount);
		rr4.setIncomeHead("Fixed Asset Interest(Bank)");
		rr4.setCreditAmount(fdrInterestAmount);
		
		plList.add(rr4);
		
		ReportInfo rr5=new ReportInfo();
		
		rr5.setExpenseHead("Booked Expense");
		rr5.setDebitAmount(bookedExpense);
		rr5.setIncomeHead("Booked Income");
		rr5.setCreditAmount(bookedIncome);
		
		plList.add(rr5);
		
		ReportInfo rr6=new ReportInfo();
		
		rr6.setExpenseHead("MIS Interest Paid");
		rr6.setDebitAmount(misIntrPaid);
		rr6.setIncomeHead("");
		rr6.setCreditAmount(0);
		
		plList.add(rr6);*/
		
		
		
		
		//System.out.println("Exp 1."+savingsInterest+" 2. "+totalDepositPaidInterest+" 3. "+total);
		
		try {
			conn.close();
		} catch (Exception e) {
			// TODO: handle exception
		}
	}
	
	public void print()
    {
        HttpSession ss=(HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
        ss.setAttribute("savingsInterest",savingsInterest);
        ss.setAttribute("totalDepositPaidInterest",totalDepositPaidInterest);
        ss.setAttribute("loanInterestAmount",loanInterestAmount);
        ss.setAttribute("totalDepositAccruedInterest", totalDepositAccruedInterest);
        ss.setAttribute("totalAccruedLoanInterset",totalAccruedLoanInterset);
        ss.setAttribute("otherExpense",otherExpense);
        ss.setAttribute("otherIncome",otherIncome);
        ss.setAttribute("goodsAmount", goodsAmount);
        ss.setAttribute("fdrInterestAmount", fdrInterestAmount);
        ss.setAttribute("strStartDate", strStartDate);
        ss.setAttribute("strEndDate", strEndDate);
        ss.setAttribute("totalIncome", totalIncome);
        ss.setAttribute("totalExpense", totalExpense);
        ss.setAttribute("totalProfit", totalProfit);
        RequestContext.getCurrentInstance().execute("window.open('printProfitlossAccount.xhtml')");
    }
	
	
	private double viewAccruedLoanReport() 
	{
		HeaderBean hh=new HeaderBean();
	//	if(hh.getName().equals(""))
		{
			Connection conn=DataBaseConnection.JAVAConnection();
			lastList=new ArrayList<>();
			double totIntr=0,totalCrIntr=0;
			ArrayList<SelectItem> loanTypeList=db.loanTypeList1();
			for(SelectItem ss: loanTypeList)
			{
				loanList=db.allActiveLoanForAccruedLoan(String.valueOf(ss.getValue()),startDate,endDate,branchId);
				for(String vv: loanList)
				{
					loanEMIList=db.loanInstallment(String.valueOf(ss.getValue()), vv,conn);
					for(int l=0;l<loanEMIList.size();l++)
					{
						//if(loanEMIList.get(l).getStatus().equalsIgnoreCase("due"))
						{
							if((loanEMIList.get(l).getInstallDate().after(startDate) || loanEMIList.get(l).getInstallDate().equals(startDate)) && (loanEMIList.get(l).getInstallDate().before(endDate) || loanEMIList.get(l).getInstallDate().equals(endDate)))
							{
								//if(loanEMIList.get(l).getPaidAmount()<Double.parseDouble(loanEMIList.get(l).getInterestAmount()))
								{
									totIntr+=(Double.parseDouble(loanEMIList.get(l).getInterestAmount()));
								}
							}
						}
					}
					totalCrIntr+=db.totalLoanInterestByLoanNo(startDate, endDate, conn, vv);
					//System.out.println("=========="+totIntr+"=========="+totalCrIntr+"=="+vv);
				}
			}
			totalAccruedLoanInterset=totIntr-totalCrIntr;
			totalAccruedLoanInterset=Double.parseDouble(dc.format(totalAccruedLoanInterset));
			
			try {
				conn.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
			return totalAccruedLoanInterset;
		}
		/*else
		{
			totalAccruedLoanInterset=0;
			return totalAccruedLoanInterset;
		}*/
	}
	
	
	double total=0;
	public double view(String type) throws ParseException
	{
		Connection conn=DataBaseConnection.JAVAConnection();
		total=0;
		setAllDates(type);
		interestList=new ArrayList<>();
		particularList=new ArrayList<>();
		paidInstallmentList=new ArrayList<>();
		finalList=new ArrayList<>();
		boolean intfirst=false,intsecond=false,intthird=false,intfourth=false;
		for(UserInfo info : rdFdList)
		{
			UserInfo ii=new UserInfo();
			if(info.getScheme_type().equals(type))
			{
				ii.setScheme_type(info.getScheme_type());
				ii.setUser_no(info.getUser_no());
				ii.setOffice_date(info.getOffice_date());
				ii.setFirst_applicantName(info.getFirst_applicantName());
				ii.setInstallment_amount(info.getInstallment_amount());
				ii.setStrMatDate(info.getStrMatDate());
				
				particularList.add(ii);
				
			}
		}
		
		for(UserInfo uu : particularList)
		{
			
			paidInstallmentList=db.showInstallment(uu.getUser_no(),conn);
			if(paidInstallmentList!=null)
			{
				for(InstallmentInfo ll : paidInstallmentList)
				{
					InstallmentInfo oo=new InstallmentInfo();
					if(ll.getStatus().equals("Paid"))
					{
						oo.setUser_no(ll.getUser_no());
						oo.setCalc_interest(Double.parseDouble(dc.format(ll.getCalc_interest())));
						oo.setOffice_date(uu.getOffice_date());
						oo.setStatus(ll.getStatus());
						oo.setInstlmntDate(ll.getInstlmntDate());
						oo.setIns_date(ll.getIns_date()); 
						interestList.add(oo);
						
					}
					
				}
			}
			
		}
		
		int i=0;
		int j=0;
		int year=0;
		for(InstallmentInfo ff : interestList)
		{
			String usercode="";
			
			
			double interest1=0,interest2=0,interest3=0,interest4=0;
			InstallmentInfo ss=new InstallmentInfo();
			Timestamp insdt=new Timestamp(ff.getInstlmntDate().getTime());

			if(i>0)
			{
				usercode=interestList.get(i-1).getUser_no();
			}
			
			if(!(usercode.equals(ff.getUser_no())))
			{
				j=0;
			}
			
			if(j==0)
			{
				year=ff.getOffice_date().getYear();
			}
			
			dt1.setYear(year);dt2.setYear(year);dt3.setYear(year);
			dt4.setYear(year);dt5.setYear(year);dt6.setYear(year);
			dt7.setYear(year);dt8.setYear(year);
			
			
			if((insdt.equals(dt1) || insdt.after(dt1)) && (insdt.equals(dt2) || insdt.before(dt2)))
			{
				
				if(intfirst==false || !(usercode.equals(ff.getUser_no())))
				{
				
					for(InstallmentInfo rr : interestList)
					{
				
						Timestamp insdt1=new Timestamp(rr.getInstlmntDate().getTime());
						if((ff.getUser_no().equals(rr.getUser_no()) && (insdt1.equals(dt1) || insdt1.after(dt1)) && (insdt1.equals(dt2) || insdt1.before(dt2)) ))
						{
							interest1=interest1+rr.getCalc_interest();
						}
					}

					intsecond=false;
					intfirst=true;
					intthird=false;
					intfourth=false;
					
					ss.setUser_no(ff.getUser_no());
					ss.setPayoutDate(dt2);
					ss.setInterest(Double.parseDouble(dc.format(interest1)));
					ss.setStrPayOutDate(new SimpleDateFormat("dd/MM/yyyy").format(ss.getPayoutDate()));
				}
				
				
			}
			else if((insdt.equals(dt3) || insdt.after(dt3)) && (insdt.equals(dt4) || insdt.before(dt4)))
			{
				if(intsecond==false || !(usercode.equals(ff.getUser_no())))
				{
					for(InstallmentInfo rr : interestList)
					{
				
						Timestamp insdt1=new Timestamp(rr.getInstlmntDate().getTime());
						if((ff.getUser_no().equals(rr.getUser_no()) && (insdt1.equals(dt3) || insdt1.after(dt3)) && (insdt1.equals(dt4) || insdt1.before(dt4)) ))
						{
							interest2=interest2+rr.getCalc_interest();
						}
					}
					
					intsecond=true;
					intfirst=false;
					intthird=false;
					intfourth=false;
					
					ss.setUser_no(ff.getUser_no());
					ss.setPayoutDate(dt4);
					ss.setInterest(Double.parseDouble(dc.format(interest2)));
					ss.setStrPayOutDate(new SimpleDateFormat("dd/MM/yyyy").format(ss.getPayoutDate()));

				}

			}
			else if((insdt.equals(dt5) || insdt.after(dt5)) && (insdt.equals(dt6) || insdt.before(dt6)))
			{	
				if(intthird==false || !(usercode.equals(ff.getUser_no())))
				{
					for(InstallmentInfo rr : interestList)
					{
				
						Timestamp insdt1=new Timestamp(rr.getInstlmntDate().getTime());
						if((ff.getUser_no().equals(rr.getUser_no()) && (insdt1.equals(dt5) || insdt1.after(dt5)) && (insdt1.equals(dt6) || insdt1.before(dt6)) ))
						{
							interest3=interest3+rr.getCalc_interest();
						}
					}
					
					intsecond=false;
					intfirst=false;
					intthird=true;
					intfourth=false;
					
					ss.setUser_no(ff.getUser_no());
					ss.setPayoutDate(dt6);
					ss.setInterest(Double.parseDouble(dc.format(interest3)));
					ss.setStrPayOutDate(new SimpleDateFormat("dd/MM/yyyy").format(ss.getPayoutDate()));

				}
				year++;

			}
			else if((insdt.equals(dt7) || insdt.after(dt7)) && (insdt.equals(dt8) || insdt.before(dt8)))
			{
				if(intfourth==false || !(usercode.equals(ff.getUser_no())))
				{
					for(InstallmentInfo rr : interestList)
					{
				
						Timestamp insdt1=new Timestamp(rr.getInstlmntDate().getTime());
						if((ff.getUser_no().equals(rr.getUser_no()) && (insdt1.equals(dt7) || insdt1.after(dt7)) && (insdt1.equals(dt8) || insdt1.before(dt8)) ))
						{
							interest4=interest4+rr.getCalc_interest();
						}
					}
					
					intsecond=false;
					intfirst=false;
					intthird=false;
					intfourth=true;
					
					ss.setUser_no(ff.getUser_no());
					ss.setPayoutDate(dt8);
					ss.setInterest(Double.parseDouble(dc.format(interest4)));
					ss.setStrPayOutDate(new SimpleDateFormat("dd/MM/yyyy").format(ss.getPayoutDate()));

				}

			}
				
			//****
			i++;j++;
			if(ss.getInterest()>0 && (ss.getPayoutDate().before(tdydt) || ss.getPayoutDate().equals(tdydt)))
			{
				finalList.add(ss);
						
			}
			
		}
		int k=0;
		for(InstallmentInfo tt : finalList)
		{
			tt.setSno(++k);
			total+=Double.parseDouble(dc.format(tt.getInterest()));
			//// System.out.println(Double.parseDouble(dc.format(total)));
		}
		total=Double.parseDouble(dc.format(total));
		try {
			conn.close();
		} catch (Exception e) {
			// TODO: handle exception
		}
		return total;
		
	}
	
	public double paidDD(Date startDate2, Date endDate2)
	{
		HttpSession ss=(HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
		
		paidRdInterest=0;
		Connection conn=DataBaseConnection.JAVAConnection();
		returnedRdList=db.returnedRdAmountList(branchId,startDate2,endDate2,"DD",conn);
		for(UserInfo uu : returnedRdList)
		{
			
			paidRdAmount=db.paidAmountForRdList1(startDate,endDate,uu.getUser_no(), branchId ,conn);
		//	System.out.println("DD: "+uu.getUser_no()+" Maturity Amount: "+uu.getAmount()+" Paid Amount: "+paidRdAmount+" Interest Amount: "+(uu.getAmount()-paidRdAmount));
			paidRdInterest=paidRdInterest+(uu.getAmount()-paidRdAmount);
		}
		try {
			conn.close();
		} catch (Exception e) {
			// TODO: handle exception
		}
		
		return paidRdInterest;
	}
	
	public double paidRD(Date startDate2, Date endDate2)
	{
		HttpSession ss=(HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
		paidRdInterest=0;
		Connection conn=DataBaseConnection.JAVAConnection();
		returnedRdList=db.returnedRdAmountList(branchId,startDate2,endDate2,"RD",conn);
		for(UserInfo uu : returnedRdList)
		{
			paidRdAmount=db.paidAmountForRdList1(startDate,endDate,uu.getUser_no(), branchId, conn);
		//	System.out.println("RD: "+uu.getUser_no()+" Maturity Amount: "+uu.getAmount()+" Paid Amount: "+paidRdAmount+" Interest Amount: "+(uu.getAmount()-paidRdAmount));
			paidRdInterest=paidRdInterest+(uu.getAmount()-paidRdAmount);
		}
		try {
			conn.close();
		} catch (Exception e) {
			// TODO: handle exception
		}
		return paidRdInterest;
	}
	
	public double paidFD(Date startDate3, Date endDate3)
	{
		HttpSession ss=(HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
		paidFdInterest=0;
		Connection conn=DataBaseConnection.JAVAConnection();
		returnedFdList=db.returnedRdAmountList(branchId,startDate3,endDate3,"FD",conn);
		for(UserInfo uu : returnedFdList)
		{
			paidFdAmount=Double.parseDouble(dc.format(db.paidAmountForFdList1(startDate,endDate,uu.getUser_no(), branchId)));
		//	System.out.println("FD: "+uu.getUser_no()+" Maturity Amount: "+uu.getAmount()+" Paid Amount: "+paidFdAmount+" Interest Amount: "+(uu.getAmount()-paidFdAmount));
			paidFdInterest=Double.parseDouble(dc.format(paidFdInterest+(uu.getAmount()-paidFdAmount)));
		}
		try {
			conn.close();
		} catch (Exception e) {
			// TODO: handle exception
		}
		return paidFdInterest;
	}
	
	
	public void setAllDates(String type) throws ParseException
	{

		HeaderBean hh=new HeaderBean();
		
		Date currDate=new Date();
		
		String eDate="31-03-"+hh.getStartFinYear();
		
		
		
		if(!hh.getSelectedSession().equals(hh.getCurrentSession()))
		{
			SimpleDateFormat f = new SimpleDateFormat("dd-MM-yyyy");
			
			Date finLastDate;
			
			finLastDate=f.parse(eDate);
			standard=tdyDate=date1=date2=date3=date4=date5=date6=date7=date8=finLastDate;
		}
		else
		{
			
			date1=date2=date3=date4=date5=date6=date7=date8=standard=tdyDate=currDate;			
		}
		
		
		dt1=new Timestamp(date1.getTime());dt2=new Timestamp(date2.getTime());
		dt3=new Timestamp(date3.getTime());dt4=new Timestamp(date4.getTime());
		dt5=new Timestamp(date5.getTime());dt6=new Timestamp(date6.getTime());
		dt7=new Timestamp(date7.getTime());dt8=new Timestamp(date8.getTime());
		

		
		tdydt=new Timestamp(endDate.getTime());
		tdydt.setHours(0);tdydt.setMinutes(0);tdydt.setSeconds(0);tdydt.setNanos(0);
		
		stndrd=new Timestamp(endDate.getTime());
		stndrd.setDate(30);
		stndrd.setMonth(5);
		stndrd.setYear(endDate.getYear());
		stndrd.setHours(0);stndrd.setMinutes(0);stndrd.setSeconds(0);stndrd.setNanos(0);
		
		dt1.setDate(1);
		dt1.setMonth(3);
		dt1.setHours(0);dt1.setMinutes(0);dt1.setSeconds(0);dt1.setNanos(0);
		
		dt2.setDate(30);
		dt2.setMonth(5);
		dt2.setHours(0);dt2.setMinutes(0);dt2.setSeconds(0);dt2.setNanos(0);

		dt3.setDate(1);
		dt3.setMonth(6);
		dt3.setHours(0);dt3.setMinutes(0);dt3.setSeconds(0);dt3.setNanos(0);

		dt4.setDate(30);
		dt4.setMonth(8);
		dt4.setHours(0);dt4.setMinutes(0);dt4.setSeconds(0);dt4.setNanos(0);

		dt5.setDate(1);
		dt5.setMonth(9);
		dt5.setHours(0);dt5.setMinutes(0);dt5.setSeconds(0);dt5.setNanos(0);

		dt6.setDate(31);
		dt6.setMonth(11);
		dt6.setHours(0);dt6.setMinutes(0);dt6.setSeconds(0);dt6.setNanos(0);

		dt7.setDate(1);
		dt7.setMonth(0);
		dt7.setHours(0);dt7.setMinutes(0);dt7.setSeconds(0);dt7.setNanos(0);

		dt8.setDate(31);
		dt8.setMonth(2);
		dt8.setHours(0);dt8.setMinutes(0);dt8.setSeconds(0);dt8.setNanos(0);

		
		rdFdList=db.showUser(branchId,type);
	}

	public double getApplicantFee() {
		return applicantFee;
	}

	public void setApplicantFee(double applicantFee) {
		this.applicantFee = applicantFee;
	}

	public double getGoodsAmount() {
		return goodsAmount;
	}

	public void setGoodsAmount(double goodsAmount) {
		this.goodsAmount = goodsAmount;
	}

	public double getLoanInterestAmount() {
		return loanInterestAmount;
	}

	public void setLoanInterestAmount(double loanInterestAmount) {
		this.loanInterestAmount = loanInterestAmount;
	}

	public double getFdrInterestAmount() {
		return fdrInterestAmount;
	}

	public void setFdrInterestAmount(double fdrInterestAmount) {
		this.fdrInterestAmount = fdrInterestAmount;
	}

	public double getOtherIncome() {
		return otherIncome;
	}

	public void setOtherIncome(double otherIncome) {
		this.otherIncome = otherIncome;
	}

	public double getSavingsInterest() {
		return savingsInterest;
	}

	public void setSavingsInterest(double savingsInterest) {
		this.savingsInterest = savingsInterest;
	}

	public double getOtherExpense() {
		return otherExpense;
	}

	public void setOtherExpense(double otherExpense) {
		this.otherExpense = otherExpense;
	}

	public double getTotalIncome() {
		return totalIncome;
	}

	public void setTotalIncome(double totalIncome) {
		this.totalIncome = totalIncome;
	}

	public double getTotalExpense() {
		return totalExpense;
	}

	public void setTotalExpense(double totalExpense) {
		this.totalExpense = totalExpense;
	}

	public double getTotalRdInterest() {
		return totalRdInterest;
	}

	public void setTotalRdInterest(double totalRdInterest) {
		this.totalRdInterest = totalRdInterest;
	}

	public double getTotalFdInterest() {
		return totalFdInterest;
	}

	public void setTotalFdInterest(double totalFdInterest) {
		this.totalFdInterest = totalFdInterest;
	}

	public Date getEndDate() {
		return endDate;
	}

	public void setEndDate(Date endDate) {
		this.endDate = endDate;
	}

	public String getStrEndDate() {
		return strEndDate;
	}

	public void setStrEndDate(String strEndDate) {
		this.strEndDate = strEndDate;
	}

	public double getTotalProfit() {
		return totalProfit;
	}

	public void setTotalProfit(double totalProfit) {
		this.totalProfit = totalProfit;
	}

	public double getDdInterest() {
		return ddInterest;
	}

	public void setDdInterest(double ddInterest) {
		this.ddInterest = ddInterest;
	}

	public double getTotalDepositPaidInterest() {
		return totalDepositPaidInterest;
	}

	public void setTotalDepositPaidInterest(double totalDepositPaidInterest) {
		this.totalDepositPaidInterest = totalDepositPaidInterest;
	}

	public double getTotalDepositAccruedInterest() {
		return totalDepositAccruedInterest;
	}

	public void setTotalDepositAccruedInterest(double totalDepositAccruedInterest) {
		this.totalDepositAccruedInterest = totalDepositAccruedInterest;
	}


	public boolean isDisEndDate() {
		return disEndDate;
	}


	public void setDisEndDate(boolean disEndDate) {
		this.disEndDate = disEndDate;
	}


	public double getTotalAccruedLoanInterset() {
		return totalAccruedLoanInterset;
	}


	public void setTotalAccruedLoanInterset(double totalAccruedLoanInterset) {
		this.totalAccruedLoanInterset = totalAccruedLoanInterset;
	}


	


	public double getStationaryCharge() {
		return stationaryCharge;
	}


	public void setStationaryCharge(double stationaryCharge) {
		this.stationaryCharge = stationaryCharge;
	}


	public double getOtherCharge() {
		return otherCharge;
	}


	public void setOtherCharge(double otherCharge) {
		this.otherCharge = otherCharge;
	}


	public double getPreMatCharge() {
		return preMatCharge;
	}


	public void setPreMatCharge(double preMatCharge) {
		this.preMatCharge = preMatCharge;
	}

	public String getLabel() {
		return label;
	}

	public void setLabel(String label) {
		this.label = label;
	}

	public double getTotalAmount() {
		return totalAmount;
	}

	public void setTotalAmount(double totalAmount) {
		this.totalAmount = totalAmount;
	}

	public ArrayList<BorrowerLenderInfo> getSaleList() {
		return saleList;
	}

	public void setSaleList(ArrayList<BorrowerLenderInfo> saleList) {
		this.saleList = saleList;
	}

	public boolean isShowSale() {
		return showSale;
	}

	public void setShowSale(boolean showSale) {
		this.showSale = showSale;
	}

	public String getStrStartDate() {
		return strStartDate;
	}

	public void setStrStartDate(String strStartDate) {
		this.strStartDate = strStartDate;
	}

	public double getMisIntrPaid() {
		return misIntrPaid;
	}

	public void setMisIntrPaid(double misIntrPaid) {
		this.misIntrPaid = misIntrPaid;
	}

	public double getBookedExpense() {
		return bookedExpense;
	}

	public void setBookedExpense(double bookedExpense) {
		this.bookedExpense = bookedExpense;
	}

	public double getBookedIncome() {
		return bookedIncome;
	}

	public void setBookedIncome(double bookedIncome) {
		this.bookedIncome = bookedIncome;
	}

	public boolean isBooleanAccuredDeposit() {
		return booleanAccuredDeposit;
	}

	public void setBooleanAccuredDeposit(boolean booleanAccuredDeposit) {
		this.booleanAccuredDeposit = booleanAccuredDeposit;
	}

	public boolean isBooleanAccuredLoan() {
		return booleanAccuredLoan;
	}

	public void setBooleanAccuredLoan(boolean booleanAccuredLoan) {
		this.booleanAccuredLoan = booleanAccuredLoan;
	}

	public ArrayList<ReportInfo> getPlList() {
		return plList;
	}

	public void setPlList(ArrayList<ReportInfo> plList) {
		this.plList = plList;
	}

	public double getDepAmount() {
		return depAmount;
	}

	public void setDepAmount(double depAmount) {
		this.depAmount = depAmount;
	}

	public int getBranchId() {
		return branchId;
	}

	public void setBranchId(int branchId) {
		this.branchId = branchId;
	}

	public String getBranchUserId() {
		return branchUserId;
	}

	public void setBranchUserId(String branchUserId) {
		this.branchUserId = branchUserId;
	}

	public String getUserType() {
		return userType;
	}

	public void setUserType(String userType) {
		this.userType = userType;
	}

	public List<SelectItem> getBranchList() {
		return branchList;
	}

	public void setBranchList(List<SelectItem> branchList) {
		this.branchList = branchList;
	}

	
}
