Exemplo n.º 1
0
void BudgetWindow::RefreshBudgetGrid(void)
{
	fIncomeGrid.MakeEmpty();
	fSpendingGrid.MakeEmpty();

	CppSQLite3Query query = gDatabase.DBQuery("select category,amount,period from "
											"budgetlist order by category",
											"BudgetWindow::RefreshCategories");
	while(!query.eof())
	{
		BString cat = DeescapeIllegalCharacters(query.getStringField(0));
		Fixed amount;
		amount.SetPremultiplied(query.getInt64Field(1));
		BudgetPeriod period = (BudgetPeriod)query.getIntField(2);

		ReportGrid *grid = (amount.IsPositive()) ? &fIncomeGrid : &fSpendingGrid;

		int32 index = grid->CountItems();
		grid->AddItem();
		grid->SetRowTitle(index, cat.String());

		Fixed f(amount.AbsoluteValue());
		switch(period)
		{
			case BUDGET_QUARTERLY:
			{
				f /= 3;
				Fixed qamt(amount);
				qamt *= 4;
				grid->SetValue(12,index,qamt);
				break;
			}
			case BUDGET_ANNUALLY:
			{
				f /=12;
				grid->SetValue(12,index,amount);
				break;
			}
			default:
			{
				Fixed mamt(f);
				mamt *= 12;
				grid->SetValue(12,index,mamt);
				break;
			}
		}

		for(int32 i=0; i<12; i++)
			grid->SetValue(i,index,f);

		query.nextRow();
	}
}
Exemplo n.º 2
0
void BudgetWindow::RefreshCategories(void)
{
	fCategoryList->Clear();
	fIncomeRow = new BRow();
	fCategoryList->AddRow(fIncomeRow);
	fSpendingRow = new BRow();
	fCategoryList->AddRow(fSpendingRow);
	fIncomeRow->SetField(new BStringField(TRANSLATE("Income")),0);
	fSpendingRow->SetField(new BStringField(TRANSLATE("Spending")),0);

	CppSQLite3Query query = gDatabase.DBQuery("select category,amount,period,isexpense from "
											"budgetlist order by category",
											"BudgetWindow::RefreshCategories");
	float maxwidth=fCategoryList->StringWidth("Category");
	while(!query.eof())
	{
		BString cat = DeescapeIllegalCharacters(query.getStringField(0));
		Fixed amount;
		amount.SetPremultiplied(query.getInt64Field(1));
		BudgetPeriod period = (BudgetPeriod)query.getIntField(2);

		BRow *row = new BRow();

		if(query.getIntField(3)==0)
			fCategoryList->AddRow(row,fIncomeRow);
		else
			fCategoryList->AddRow(row,fSpendingRow);

		row->SetField(new BStringField(cat.String()),0);

		BString amountstr;
		gDefaultLocale.CurrencyToString(amount.AbsoluteValue(),amountstr);
		amountstr.Truncate(amountstr.FindFirst(gDefaultLocale.CurrencyDecimal()));
		amountstr.RemoveFirst(gDefaultLocale.CurrencySymbol());

		row->SetField(new BStringField(amountstr.String()),1);

		float tempwidth = fCategoryList->StringWidth(cat.String());
		maxwidth = MAX(tempwidth,maxwidth);

		row->SetField(new BStringField(BudgetPeriodToString(period).String()),2);

		query.nextRow();
	}
	fCategoryList->ColumnAt(0)->SetWidth(maxwidth+30);
	fCategoryList->ExpandOrCollapse(fIncomeRow,true);
	fCategoryList->ExpandOrCollapse(fSpendingRow,true);
}
Exemplo n.º 3
0
void BudgetWindow::CalcStats(const char *cat, Fixed &high, Fixed &low, Fixed &avg)
{
	if(!cat)
		return;

	BString querystring;
	Fixed cattotal;
	CppSQLite3Query query;

	// find the average amount
	for(int32 j=0; j<gDatabase.CountAccounts(); j++)
	{
		Account *acc = gDatabase.AccountAt(j);
		querystring = "select sum(amount) from account_" ;
		querystring << acc->GetID()	<< " where category = '"
					<< EscapeIllegalCharacters(cat)
					<< "';";
		query = gDatabase.DBQuery(querystring.String(),
								"BudgetWindow::CalcStats:get average");
		cattotal.AddPremultiplied(query.getInt64Field(0));
		query.finalize();
	}
	avg = cattotal;
	avg /= 12;

	// find the highest amount
	cattotal=0;
	for(int32 j=0; j<gDatabase.CountAccounts(); j++)
	{
		Account *acc = gDatabase.AccountAt(j);
		querystring = "select max(amount) from account_" ;
		querystring << acc->GetID()	<< " where category = '"
					<< EscapeIllegalCharacters(cat)
					<< "';";
		query = gDatabase.DBQuery(querystring.String(),
								"BudgetWindow::CalcStats:get highest");
		Fixed value;
		value.SetPremultiplied(query.getInt64Field(0));
		cattotal = MAX(cattotal,value);
		query.finalize();
	}
	high = cattotal;

	// find the lowest amount
	cattotal=0;
	for(int32 j=0; j<gDatabase.CountAccounts(); j++)
	{
		Account *acc = gDatabase.AccountAt(j);
		querystring = "select min(amount) from account_" ;
		querystring << acc->GetID()	<< " where category = '"
					<< EscapeIllegalCharacters(cat)
					<< "';";
		query = gDatabase.DBQuery(querystring.String(),
								"BudgetWindow::CalcStats:get highest");
		Fixed value;
		value.SetPremultiplied(query.getInt64Field(0));
		cattotal = MIN(cattotal,value);
		query.finalize();
	}
	low = cattotal;
}