/**********************************************************************
Virtual Cache is a program which helps to manage personal finances
Copyright (C) 2013 by Rovinskiy Nikolay
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
***********************************************************************/
package Controllers.ReportViewer;
import Controllers.Main.MainWindowController;
import Controllers.Transfer.TransferController;
import Controls.CircularDiagram;
import Controls.MultipleChart;
//import Controls.MyScrollPane;
import Controls.SpentTable;
import Interfaces.ReportViewer;
import Utils.Card;
import Utils.DateTool;
import Utils.MonthIterator;
import Utils.Transaction;
import java.util.Date;
import java.util.Enumeration;
import java.util.Vector;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
/**
*
* @author nrovinskiy
*/
public class ReportViewerController extends ReportViewer
{
private Date dtBegin, dtEnd;
private MainWindowController mwcMain;
public ReportViewerController(MainWindowController main, Date begin, Date end)
{
super((JFrame)main);
mwcMain=main;
dtBegin=begin;
dtEnd=end;
btnSave.addActionListener(new SavePictureListener(mwcMain, this));
jtpAnalisys.addChangeListener(new TabChangeListener(this));
this.setLocation(main.getLocation().x, main.getLocation().y);
CalculateAndShow();
}
private void CalculateAndShow()
{
double dblTotal=0;
Enumeration<String> enReasons=mwcMain.getProfile().getReasons().keys();
Vector<String> vctGroups= new Vector<String>();
while(enReasons.hasMoreElements())
{
String strReson=enReasons.nextElement();
if(!vctGroups.contains(mwcMain.getProfile().getReasons().get(strReson)))
vctGroups.add(mwcMain.getProfile().getReasons().get(strReson));
}
String[] strGroups=new String[vctGroups.size()];
String[] strCardNames=new String[mwcMain.getProfile().getCardNumber()];
vctGroups.toArray(strGroups);
MonthIterator mitTimer=new MonthIterator(dtBegin, dtEnd);
double[][] dblCardRemainings=new double[mwcMain.getProfile().getCardNumber()][mitTimer.getIterationCount()];
initArray(dblCardRemainings);
double[][] dblMonthSpent=new double[vctGroups.size()][mitTimer.getIterationCount()];
initArray(dblMonthSpent);
double[] dblValues=new double[vctGroups.size()];
vctGroups=null;
for(int i=0; i<dblValues.length; i++){dblValues[i]=0;}
Enumeration<Card> enCards=mwcMain.getProfile().getCards();
while(enCards.hasMoreElements())
{
Card crdTemp=enCards.nextElement();
Enumeration<Transaction> enTrans =crdTemp.getAllTransactions();
while(enTrans.hasMoreElements())
{
Transaction tmpTrans=enTrans.nextElement();
if((tmpTrans.getDate().before(dtBegin))||((tmpTrans.getDate().after(dtEnd))))
continue;
for(int i=mitTimer.getIterationNumber(); mitTimer.hasNext(); mitTimer.next())
{
i=mitTimer.getIterationNumber();
if(mitTimer.isInCurrentMonth(tmpTrans.getDate()))
{
for(int k=0; k<strGroups.length; k++)
{
if(strGroups[k].equals(mwcMain.getProfile().getReasons().get(tmpTrans.strReason.trim().toLowerCase())))
dblMonthSpent[k][i]-=tmpTrans.amount;
}
}
}
mitTimer.reset();
for(int i=0; i<strGroups.length; i++)
{
if(tmpTrans.amount>0)
continue;
if((strGroups[i].equals(mwcMain.getProfile().getReasons().get(tmpTrans.strReason.trim().toLowerCase())))&&(!tmpTrans.strReason.equals(TransferController.TransferMinus)))
dblValues[i]+=tmpTrans.amount;
}
if(tmpTrans.amount<0)
dblTotal-=tmpTrans.amount;
}
}
//circular diagram 0 tab
jtpAnalisys.addTab("Resourse distribution", new CircularDiagram(strGroups, dblValues));
//jtpAnalisys.setToolTipTextAt(0, "Resourse distribution");
//fill the dates
String[] strDates=new String[mitTimer.getIterationCount()];
for(Date t=mitTimer.getDateOfIteration(); mitTimer.hasNext(); mitTimer.next())
strDates[mitTimer.getIterationNumber()]=DateTool.getDateStr(mitTimer.getDateOfIteration(), "MM.yyyy");
mitTimer.reset();
//Calculade all data
enCards=mwcMain.getProfile().getCards();
int intCardPointer=mwcMain.getProfile().getCardNumber()-1;
double dblMonthBalance;
while(enCards.hasMoreElements())
{
Card crdTemp=enCards.nextElement();
strCardNames[intCardPointer]=crdTemp.getName();
dblMonthBalance=crdTemp.getInitialBalance();
Enumeration<Transaction> enInitTrans=crdTemp.getAllTransactions();
while(enInitTrans.hasMoreElements())
{
Transaction trTemp=enInitTrans.nextElement();
if(trTemp.getDate().before(dtBegin))
dblMonthBalance+=trTemp.amount;
}
while(mitTimer.hasNext())
{
Enumeration<Transaction> enTranCr=crdTemp.getAllTransactions();
while(enTranCr.hasMoreElements())
{
Transaction trans=enTranCr.nextElement();
if(mitTimer.isInCurrentMonth(trans.getDate()))
{
dblMonthBalance+=trans.amount;
}
}
dblCardRemainings[intCardPointer][mitTimer.getIterationNumber()]=dblMonthBalance;
mitTimer.next();
}
mitTimer.reset();
intCardPointer--;
}
//invert arrays for cards
//amount of money spent to different groups in months tab 1
//add this aount first as a table with data
SpentTable stNew=new SpentTable();
JScrollPane jspSpent=new JScrollPane();
jspSpent.getViewport().add(stNew);
jtpAnalisys.addTab("Account review", jspSpent);
stNew.setData(strGroups, dblMonthSpent, strDates);
//then as a plot
JScrollPane jspChart=new JScrollPane(new MultipleChart(strGroups, dblMonthSpent, strDates, "Spent"));
jtpAnalisys.addTab("Expenses by months", jspChart);
//jtpAnalisys.setToolTipTextAt(1, "Outcome by months");
//percent of money spent to different groups in months tab 3
//dblMonthSpent[group number][month number]
double[] dblMonthTotal=new double[dblMonthSpent[0].length]; //the total amount of spend money per month
for(int x=0; x<dblMonthTotal.length; x++)
{
dblMonthTotal[x]=0;
for(int y=0; y<dblMonthSpent.length; y++)
{
if(dblMonthSpent[y][x]>0)
dblMonthTotal[x]+=dblMonthSpent[y][x];
}
}
for(int i=0; i<dblMonthSpent.length; i++)
for(int k=0; k<dblMonthSpent[i].length; k++)
{
if(dblMonthSpent[i][k]<0)
dblMonthSpent[i][k]=0;
if(dblMonthTotal[k]!=0)
dblMonthSpent[i][k]=dblMonthSpent[i][k]*100/dblMonthTotal[k];
}
dblMonthTotal=null;
MultipleChart mtcPercents=new MultipleChart(strGroups, dblMonthSpent, strDates, "Spent %");
mtcPercents.setMaxValue(100);
jspChart=new JScrollPane(mtcPercents);
jtpAnalisys.addTab("Expenses by months in percent", jspChart);
//jtpAnalisys.setToolTipTextAt(2, "Outcome by monthsin percent");
//amount of money on different credit/debit cards at different months tab 3
jspChart=new JScrollPane(new MultipleChart(strCardNames, dblCardRemainings, strDates, "Balance"));
jtpAnalisys.addTab("Card balances", jspChart);
//jtpAnalisys.setToolTipTextAt(3, "Card balances");
}
private void initArray(double[][] array)
{
for(int i=0; i<array.length; i++)
for(int k=0; k<array[i].length; k++)
array[i][k]=0;
}
// @Override
// public void repaint()
// {
// super.repaint();
// CastDraws();
// }
// @Override
// public void paint(Graphics g)
// {
// super.paint(g);
// CastDraws();
// }
//private void CastDraws()
//{
// this.setVisible(true);
// if((jtpAnalisys.getSelectedComponent()!=null)&&(jtpAnalisys.getSelectedComponent().getClass().equals(JScrollPane.class))&&((((JScrollPane)jtpAnalisys.getSelectedComponent()).getViewport().getComponent(0)).getClass().equals(MultipleChart.class)))
// ((MultipleChart)(((JScrollPane)jtpAnalisys.getSelectedComponent()).getViewport().getComponent(0))).Draw();
// else if ((jtpAnalisys.getSelectedComponent()!=null)&&(jtpAnalisys.getSelectedComponent().getClass().equals(CircularDiagram.class)))
// ((CircularDiagram)jtpAnalisys.getComponentAt(0)).Draw();
// if(jtpAnalisys.getSelectedIndex()==0)
// ((CircularDiagram)jtpAnalisys.getComponentAt(0)).Draw();
// else
// ((MultipleChart)(((JScrollPane)jtpAnalisys.getSelectedComponent()).getViewport().getComponent(0))).Draw();
//else if(jtpAnalisys.getSelectedIndex()==2)
//((MultipleChart)((JViewport)((JScrollPane)jtpAnalisys.getComponentAt(2)).getComponent(0)).getComponent(0)).Draw();
//((MultipleChart)((JViewport)((JScrollPane)jtpAnalisys.getComponentAt(3)).getComponent(0)).getComponent(0)).Draw();
// }
}