Add CSV Export

Set DateFrom to first of month for SearchBanner
Fix a bug : for dates in Search
Fix a bug : file was not truncated during export
This commit is contained in:
Grégory Soutadé 2013-01-17 20:22:42 +01:00
parent e9c677f5b2
commit 62a3712ff7
6 changed files with 267 additions and 10 deletions

View File

@ -1245,14 +1245,14 @@ std::vector<Operation>* Database::Search(User* user, QString* description, QDate
if (dateFrom)
{
dayFrom = QString::number(dateFrom->day()-1);
monthFrom = QString::number(dateFrom->month());
monthFrom = QString::number(dateFrom->month()-1);
yearFrom = QString::number(dateFrom->year());
}
if (dateTo)
{
dayTo = QString::number(dateTo->day()-1);
monthTo = QString::number(dateTo->month());
monthTo = QString::number(dateTo->month()-1);
yearTo = QString::number(dateTo->year());
}

View File

@ -0,0 +1,213 @@
/*
Copyright 2010-2013 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 <http://www.gnu.org/licenses/>.
*/
#include <QTextStream>
#include <QFile>
#include <iostream>
#include "CSVExportEngine.hpp"
static CSVExportEngine csvExportEngine;
CSVExportEngine::CSVExportEngine()
{
KissCount::RegisterExportEngine(this);
_shortExt = ".csv";
_longExt = _("CSV files (*.csv)");
}
CSVExportEngine::~CSVExportEngine()
{
}
bool CSVExportEngine::HandleFile(const QString& path, User* user, Database* db, KissCount* kiss)
{
return ExportEngine::HandleFile(path, user, db, kiss);
}
bool CSVExportEngine::SaveAccounts()
{
Account account;
std::map<int, int>::iterator it;
*_writer << "Accounts" << endl << endl;
*_writer << "id;name;number;blocked;virtual;hidden" << endl;
for(it=_accounts.begin(); it!=_accounts.end(); it++)
{
try
{
account = _user->GetAccount(it->first);
}
catch(User::AccountNotFound)
{
account.id = it->first;
account.name = "Unknown";
account.blocked = false;
account._virtual = false;
account.hidden = false;
}
ESCAPE_CHARS(account.name);
ESCAPE_CHARS(account.number);
*_writer << QString::number(account.id) << ";";
*_writer << "\"" << account.name << "\"" << ";";
*_writer << "\"" << account.number << "\"" << ";";
// *_writer << (account.shared ? "1" : "0") << ";";
*_writer << (account.blocked ? "1" : "0") << ";";
// *_writer << (account._default ? "1" : "0") << ";" ;
// *_writer << (account.is_owner ? "1" : "0") << ";";
*_writer << (account._virtual ? "1" : "0") << ";";
*_writer << (account.hidden ? "1" : "0");
*_writer << endl;
}
*_writer << endl << endl;
return true;
}
bool CSVExportEngine::SaveAccountAmounts()
{
std::map<AccountAmount, int, AccountAmount>::iterator it;
QString v;
*_writer << "Account Amounts" << endl << endl;
*_writer << "id;month;year;amount" << endl;
for(it=_accountAmounts.begin(); it!=_accountAmounts.end(); it++)
{
*_writer << QString::number(it->first.account) << ";";
*_writer << QString::number(it->first.month+1) << ";";
*_writer << QString::number(it->first.year) << ";";
*_writer << v.sprintf("%d", it->second);
*_writer << endl;
}
*_writer << endl << endl;
return true;
}
bool CSVExportEngine::SaveCategories()
{
Category category;
std::map<int, int>::iterator it;
int rgb;
QString v;
*_writer << "Categories" << endl << endl;
*_writer << "id;parent;name;font;backcolor;forecolor;fix_cost" << endl;
for(it=_categories.begin(); it!=_categories.end(); it++)
{
category = _user->GetCategory(it->first);
ESCAPE_CHARS(category.name);
*_writer << QString::number(category.id) << ";";
*_writer << QString::number(category.parent) << ";";
*_writer << "\"" << category.name << "\"" << ";";
*_writer << "\"" << category.font << "\"" << ";";
rgb = category.backcolor.blue();
rgb |= category.backcolor.green() << 8;
rgb |= category.backcolor.red() << 16;
*_writer << v.sprintf("0x%08X", rgb) << ";";
rgb = category.forecolor.blue();
rgb |= category.forecolor.green() << 8;
rgb |= category.forecolor.red() << 16;
*_writer << v.sprintf("0x%08X", rgb) << ";";
*_writer << (category.fix_cost ? "1" : "0");
*_writer << endl;
}
*_writer << endl << endl;
return true;
}
bool CSVExportEngine::SaveOperations(std::vector<Operation>* operations)
{
std::vector<Operation>::iterator it;
QString v;
*_writer << "Operations" << endl << endl;
*_writer << "id;parent;day;month;year;amount;description;category;fix_cost;account;checked;transfert;formula;meta;virtual" << endl;
for(it=operations->begin(); it!=operations->end(); it++)
{
ESCAPE_CHARS(it->description);
*_writer << QString::number(it->id) << ";" ;
*_writer << QString::number(it->parent) << ";";
*_writer << QString::number(it->day+1) << ";";
*_writer << QString::number(it->month+1) << ";";
*_writer << QString::number(it->year) << ";";
*_writer << v.sprintf("%d", it->amount) << ";";
*_writer << "\"" << it->description << "\"" << ";";
*_writer << QString::number(it->category) << ";";
*_writer << (it->fix_cost ? "1" : "0") << ";";
*_writer << QString::number(it->account) << ";";
*_writer << (it->checked ? "1" : "0") << ";";
*_writer << QString::number(it->transfert) << ";";
*_writer << it->formula << ";";
*_writer << (it->meta ? "1" : "0") << ";";
*_writer << (it->_virtual ? "1" : "0");
*_writer << endl;
}
*_writer << endl << endl;
return true;
}
bool CSVExportEngine::SaveFile(std::vector<Operation>* operations)
{
QFile file(_path);
int rc;
rc = ExportEngine::SaveFile(operations);
if (!rc) return false;
if (!file.open(QIODevice::ReadWrite|QIODevice::Truncate))
{
std::cout << "Error can't open the file " << _path.toStdString() << std::endl;
return false;
}
_writer = new QTextStream(&file);
_writer->setCodec("UTF-8");
SaveAccounts();
SaveAccountAmounts();
SaveCategories();
SaveOperations(operations);
file.flush();
file.close();
delete _writer;
return true;
}

View File

@ -0,0 +1,44 @@
/*
Copyright 2010-2013 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 <http://www.gnu.org/licenses/>.
*/
#ifndef CSVEXPORTENGINE_H
#define CSVEXPORTENGINE_H
#include <model/model.hpp>
#include <controller/KissCount.hpp>
#include "ExportEngine.hpp"
class CSVExportEngine : public ExportEngine {
public:
CSVExportEngine();
~CSVExportEngine();
bool HandleFile(const QString& path, User* user, Database* db, KissCount* kiss);
bool SaveFile(std::vector<Operation>* operations);
private:
QTextStream* _writer;
bool SaveAccounts();
bool SaveAccountAmounts();
bool SaveCategories();
bool SaveOperations(std::vector<Operation>* operations);
};
#endif

View File

@ -1,5 +1,5 @@
/*
Copyright 2010-2012 Grégory Soutadé
Copyright 2010-2013 Grégory Soutadé
This file is part of KissCount.
@ -173,7 +173,7 @@ bool XMLExportEngine::SaveFile(std::vector<Operation>* operations)
if (!rc) return false;
if (!file.open(QIODevice::ReadWrite))
if (!file.open(QIODevice::ReadWrite|QIODevice::Truncate))
{
std::cout << "Error can't open the file " << _path.toStdString() << std::endl;
return false;

View File

@ -1,5 +1,5 @@
/*
Copyright 2010-2012 Grégory Soutadé
Copyright 2010-2013 Grégory Soutadé
This file is part of KissCount.
@ -42,7 +42,7 @@ SearchBanner::SearchBanner(KissCount* kiss, QFrame *parent, void* caller, OnButt
// _calendarFrom->setNavigationBarVisible(false);
_calendarFrom->setVerticalHeaderFormat(QCalendarWidget::NoVerticalHeader);
_calendarFrom->setMaximumSize(_calendarFrom->sizeHint());
_calendarFrom->setSelectedDate(QDate::currentDate());
_calendarFrom->setSelectedDate(firstOfMonth);
connect(_calendarFrom, SIGNAL(currentPageChanged(int, int)), this, SLOT(OnCalendarFromPageChanged(int, int)));
@ -157,19 +157,19 @@ std::vector<Operation> * SearchBanner::Search()
if (_checkDateFrom->checkState() == Qt::Checked)
{
dateFrom = new QDate();
*dateFrom = _calendarFrom->selectedDate().addMonths(-1);
*dateFrom = _calendarFrom->selectedDate();
}
if (_checkDateTo->checkState() == Qt::Checked)
{
dateTo = new QDate();
*dateTo = _calendarTo->selectedDate().addMonths(-1);
*dateTo = _calendarTo->selectedDate();
}
if (dateFrom && dateTo && *dateFrom > *dateTo)
{
QMessageBox::critical(0, _("Error"), _("Invalid date range"));
goto end;
goto end;
}
if (_amountFrom->text().length())

View File

@ -1,5 +1,5 @@
/*
Copyright 2010-2012 Grégory Soutadé
Copyright 2010-2013 Grégory Soutadé
This file is part of KissCount.