/*
Copyright 2010-2012 Grégory Soutadé
This file is part of KissCount.
KissCount 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.
KissCount 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 KissCount. If not, see .
*/
#include "ExportEngine.hpp"
ExportEngine::ExportEngine()
{
}
ExportEngine::~ExportEngine()
{
}
bool ExportEngine::HandleFile(const QString& path, User* user, Database* db, KissCount* kiss)
{
_path = path;
_user = user;
_db = db;
_kiss = kiss;
return path.endsWith(_shortExt) || path.endsWith(_shortExt.toUpper());
}
bool ExportEngine::SaveFile(std::vector* operations)
{
int i;
int account, category;
AccountAmount accountAmount;
int minMonth = -1, minYear = -1;
unsigned int maxMonth = -1, maxYear = -1;
unsigned int month, year;
std::map::iterator it;
if (!operations) return 0;
_accounts.clear();
_categories.clear();
_accountAmounts.clear();
for(i=0; i<(int)operations->size(); i++)
{
account = (*operations)[i].account;
category = (*operations)[i].category;
if (minYear == -1 || (int)(*operations)[i].year < minYear)
maxYear = minYear = (*operations)[i].year;
if (minMonth == -1 || ((int)(*operations)[i].month < minMonth && (int)(*operations)[i].year == minYear))
maxMonth = minMonth = (*operations)[i].month;
if ((*operations)[i].year > maxYear)
{
maxYear = (*operations)[i].year;
maxMonth = (*operations)[i].month;
}
if ((*operations)[i].month > maxMonth && (*operations)[i].year == maxYear)
maxMonth = (*operations)[i].month;
if (account && !_accounts.count(account))
_accounts[account]++;
if (category && !_categories.count(category))
_categories[category]++;
}
for(it=_accounts.begin(); it!=_accounts.end(); it++)
{
month = minMonth;
for (year = minYear; year <= maxYear; year++)
{
for (; !(month > maxMonth && year == maxYear) && month < 12; month++)
{
accountAmount.account = it->first;
accountAmount.month = month;
accountAmount.year = year;
_accountAmounts[accountAmount] = _kiss->GetAccountAmount(accountAmount.account, accountAmount.month, accountAmount.year);
}
month = 0;
}
}
return true;
}
QString ExportEngine::GetFileExt()
{
return _(_longExt.toStdString().c_str());
}
QString ExportEngine::GetShortExt()
{
return _shortExt;
}