diff --git a/src/model/Database.cpp b/src/model/Database.cpp index 3437e52..d411daa 100644 --- a/src/model/Database.cpp +++ b/src/model/Database.cpp @@ -1245,14 +1245,14 @@ std::vector* 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()); } diff --git a/src/model/export/CSVExportEngine.cpp b/src/model/export/CSVExportEngine.cpp new file mode 100644 index 0000000..54586e5 --- /dev/null +++ b/src/model/export/CSVExportEngine.cpp @@ -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 . +*/ + +#include +#include + +#include + +#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::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::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::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* operations) +{ + std::vector::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* 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; +} diff --git a/src/model/export/CSVExportEngine.hpp b/src/model/export/CSVExportEngine.hpp new file mode 100644 index 0000000..54dd88d --- /dev/null +++ b/src/model/export/CSVExportEngine.hpp @@ -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 . +*/ + +#ifndef CSVEXPORTENGINE_H +#define CSVEXPORTENGINE_H + +#include +#include +#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* operations); + +private: + QTextStream* _writer; + + bool SaveAccounts(); + bool SaveAccountAmounts(); + bool SaveCategories(); + bool SaveOperations(std::vector* operations); +}; + +#endif diff --git a/src/model/export/XMLExportEngine.cpp b/src/model/export/XMLExportEngine.cpp index 805363e..082f3f4 100644 --- a/src/model/export/XMLExportEngine.cpp +++ b/src/model/export/XMLExportEngine.cpp @@ -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* 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; diff --git a/src/view/SearchBanner.cpp b/src/view/SearchBanner.cpp index e178e86..a8772a6 100644 --- a/src/view/SearchBanner.cpp +++ b/src/view/SearchBanner.cpp @@ -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 * 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()) diff --git a/src/view/SearchBanner.hpp b/src/view/SearchBanner.hpp index 96455b3..0f3028e 100644 --- a/src/view/SearchBanner.hpp +++ b/src/view/SearchBanner.hpp @@ -1,5 +1,5 @@ /* - Copyright 2010-2012 Grégory Soutadé + Copyright 2010-2013 Grégory Soutadé This file is part of KissCount.