package other_package;

import java.io.Serializable;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpSession;

import main_package.UserInfo;

@ManagedBean(name="printParticularSavingAccountReport")
@ViewScoped

public class PrintParticularSavingAccountReportBean implements Serializable
{	
	UserInfo selectedUser;
	double totalDebit,totalCredit,closingBalance,opBalance;
	ArrayList<ReportInfo> list;
	Date startDate=new Date(),endDate=new Date();
	DecimalFormat df=new DecimalFormat("##.##");
	DataBaseMethodsOther dbmp=new DataBaseMethodsOther();
	
	public PrintParticularSavingAccountReportBean()
	{
		HttpSession ss=(HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
		selectedUser=(UserInfo) ss.getAttribute("selectedUser");
		startDate=(Date) ss.getAttribute("svStartDate");
		endDate=(Date) ss.getAttribute("svEndDate");
		
		list=dbmp.savingAccountReport(selectedUser.getAccountNo(),startDate,endDate);
		opBalance=dbmp.openingBalance(selectedUser.getAccountNo(),startDate);
		Collections.sort(list);
		calculate();
	}
	
	public void searchReport()
	{
		list=dbmp.savingAccountReport(selectedUser.getAccountNo(),startDate,endDate);
		opBalance=dbmp.openingBalance(selectedUser.getAccountNo(),startDate);
		Collections.sort(list);
		calculate();
	}
	
	public void calculate()
	{
		totalCredit=totalDebit=closingBalance=0;
		double balance=0;
		if(list.size()>0)
		{
			balance=opBalance;
			for(int i=0;i<list.size();i++)
			{
				list.get(i).setsNo(i+1);
				totalCredit+=list.get(i).getCreditAmount();
				totalDebit+=list.get(i).getDebitAmount();
				balance=(balance+list.get(i).getDebitAmount())-list.get(i).getCreditAmount();
				list.get(i).setBalance(balance);
			}
		}
		
		totalDebit+=opBalance;
		closingBalance=totalDebit-totalCredit;
		totalCredit=Double.parseDouble(df.format(totalCredit));
		totalDebit=Double.parseDouble(df.format(totalDebit));
		closingBalance=Double.parseDouble(df.format(closingBalance));
	}
	
	public String backButton()
	{
		HttpSession ss=(HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
		String pageName=(String) ss.getAttribute("pageName");
		return pageName;
	}

	public UserInfo getSelectedUser() {
		return selectedUser;
	}
	public void setSelectedUser(UserInfo selectedUser) {
		this.selectedUser = selectedUser;
	}
	public ArrayList<ReportInfo> getList() {
		return list;
	}
	public void setList(ArrayList<ReportInfo> list) {
		this.list = list;
	}
	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 double getTotalDebit() {
		return totalDebit;
	}
	public void setTotalDebit(double totalDebit) {
		this.totalDebit = totalDebit;
	}
	public double getTotalCredit() {
		return totalCredit;
	}
	public void setTotalCredit(double totalCredit) {
		this.totalCredit = totalCredit;
	}
	public double getClosingBalance() {
		return closingBalance;
	}
	public void setClosingBalance(double closingBalance) {
		this.closingBalance = closingBalance;
	}
	public double getOpBalance() {
		return opBalance;
	}
	public void setOpBalance(double opBalance) {
		this.opBalance = opBalance;
	}
}
