KissCount/src/view/ImportPanel.cpp

441 lines
12 KiB
C++
Raw Normal View History

/*
Copyright 2010-2011 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 <QMessageBox>
#include "ImportPanel.hpp"
#include "grid/ChoiceDelegate.hpp"
ImportPanel::ImportPanel(KissCount* kiss, wxUI *parent) : KissPanel(kiss, parent)
{
QVBoxLayout *vbox = new QVBoxLayout;
QVBoxLayout *vbox2 = new QVBoxLayout;
QHBoxLayout *hbox = new QHBoxLayout;
_hbox = new QHBoxLayout;
QPushButton* buttonOpen;
QGroupBox* staticAccount = new QGroupBox(_("Unresolved accounts"));
QGroupBox* staticCategory = new QGroupBox(_("Unresolved categories"));
setLayout(vbox);
_fileTxt = new QLineEdit;
buttonOpen = new QPushButton("...");
connect(buttonOpen, SIGNAL(clicked()), this, SLOT(OnFile()));
connect(_fileTxt, SIGNAL(returnPressed()), this, SLOT(OnFileEnter()));
_buttonLoadOperations = new QPushButton(_("Load operations"));
_buttonLoadOperations->setEnabled(false);
connect(_buttonLoadOperations, SIGNAL(clicked()), this, SLOT(OnLoadOperations()));
_buttonIntegrate = new QPushButton(_("Integrate operations"));
_buttonIntegrate->setEnabled(false);
connect(_buttonIntegrate, SIGNAL(clicked()), this, SLOT(OnIntegrate()));
_checkSaveImportPatterns = new QCheckBox(_("Save import patterns"));
hbox->addWidget(_fileTxt);
hbox->addWidget(buttonOpen);
hbox->addWidget(_buttonLoadOperations);
hbox->addWidget(_buttonIntegrate);
hbox->addWidget(_checkSaveImportPatterns);
vbox->addLayout(hbox);
_accountsGrid = new QTableWidget(0, 3);
_accountsGrid->verticalHeader()->setHidden(true);
QStringList labels;
labels << _("File account") << _("Account name") << _("Internal account");
_accountsGrid->setHorizontalHeaderLabels(labels);
_accountsGrid->resizeColumnsToContents();
_categoriesGrid = new QTableWidget(0, 3);
_categoriesGrid->verticalHeader()->setHidden(true);
labels.clear();
labels << _("File category") << _("Category name") << _("Internal category");
_categoriesGrid->setHorizontalHeaderLabels(labels);
_categoriesGrid->resizeColumnsToContents();
{
QVBoxLayout *vbox = new QVBoxLayout;
vbox->addWidget(_accountsGrid);
staticAccount->setLayout(vbox);
}
vbox2->addWidget(staticAccount);
{
QVBoxLayout *vbox = new QVBoxLayout;
vbox->addWidget(_categoriesGrid);
staticCategory->setLayout(vbox);
}
vbox2->addWidget(staticCategory);
_operationsGrid = new GridAccount(kiss, this, false, false, false);
connect(_operationsGrid, SIGNAL(cellChanged(int, int)), this, SLOT(OnOperationModified(int, int)));
_hbox->addLayout(vbox2);
_hbox->addWidget(_operationsGrid);
vbox->addLayout(_hbox);
layout();
}
KissPanel* ImportPanel::CreatePanel()
{
return new ImportPanel(_kiss, _wxUI);
}
QPushButton* ImportPanel::GetButton()
{
if (!_KissButton)
{
_KissButton = new QPushButton(QIcon(IMPORT_ICON), "", this);
_KissButton->setFixedSize(128, 128);
_KissButton->setIconSize(QSize(128, 128));
}
return _KissButton;
}
QString ImportPanel::GetToolTip()
{
return _("Import");
}
void ImportPanel::OnShow()
{
_wxUI->setWindowTitle(_("KissCount - Import"));
}
void ImportPanel::OnFile()
{
QString path;
path = QFileDialog::getOpenFileName(0, _("Choose a database to open"), "", _kiss->GetImportEngineExtensions());
if (!path.size())
return;
_fileTxt->setText(path);
ProcessFile();
}
void ImportPanel::OnFileEnter()
{
ProcessFile();
}
void ImportPanel::ProcessFile()
{
User* user = _kiss->GetUser();
int i;
QString* userAccounts;
std::map<int, int> resolvedAccounts;
QString* userCategories;
std::map<int, int> resolvedCategories;
QTableWidgetItem* item;
QString path = _fileTxt->text();
_buttonLoadOperations->setEnabled(false);
_buttonIntegrate->setEnabled(false);
_accountsGrid->setRowCount(0);
_categoriesGrid->setRowCount(0);
_operationsGrid->setRowCount(0);
_importEngine = _kiss->GetImportEngine(path);
if (!_importEngine)
{
QMessageBox::critical(0, _("Error"), _("Any engine can process this file !"));
return ;
}
_importEngine->ParseFile(_unresolvedAccounts, _unresolvedCategories);
if (_unresolvedAccounts.size())
{
int nb_accounts = user->GetAccountsNumber();
userAccounts = new QString[nb_accounts+1];
userAccounts[0] = _("Create one");
for(i=0; i<nb_accounts; i++)
userAccounts[i+1] = user->_accounts[i].name;
ChoiceDelegate* accountEditor = new ChoiceDelegate(this, userAccounts, nb_accounts+1);
_accountsGrid->setItemDelegateForColumn(2, accountEditor);
_buttonLoadOperations->setEnabled(true);
_accountsGrid->setRowCount(_unresolvedAccounts.size());
for (i=0; i<(int)_unresolvedAccounts.size(); i++)
{
item = new QTableWidgetItem(_unresolvedAccounts[i].number);
item->setFlags(item->flags() & ~Qt::ItemIsEditable);
_accountsGrid->setItem(i, 0, item);
_accountsGrid->setItem(i, 1, new QTableWidgetItem(_unresolvedAccounts[i].name));
_accountsGrid->setItem(i, 2, new QTableWidgetItem(userAccounts[0]));
}
_accountsGrid->resizeColumnsToContents();
_accountsGrid->layout();
}
if (_unresolvedCategories.size())
{
int nb_categories = user->GetCategoriesNumber();
userCategories = new QString[nb_categories+1];
userCategories[0] = _("Create one");
for(i=0; i<nb_categories; i++)
userCategories[i+1] = user->_categories[i].name;
ChoiceDelegate* categoryEditor = new ChoiceDelegate(this, userCategories, nb_categories+1);
_categoriesGrid->setItemDelegateForColumn(2, categoryEditor);
_buttonLoadOperations->setEnabled(true);
_categoriesGrid->setRowCount(_unresolvedCategories.size());
for (i=0; i<(int)_unresolvedCategories.size(); i++)
{
item = new QTableWidgetItem(_unresolvedCategories[i].name);
item->setFlags(item->flags() & ~Qt::ItemIsEditable);
_categoriesGrid->setItem(i, 0, item);
_categoriesGrid->setItem(i, 1, new QTableWidgetItem(""));
_categoriesGrid->setItem(i, 2, new QTableWidgetItem(userCategories[0]));
}
_categoriesGrid->resizeColumnsToContents();
_categoriesGrid->layout();
}
if (!_unresolvedAccounts.size() && !_unresolvedCategories.size())
{
OnLoadOperations();
}
layout();
}
void ImportPanel::OnLoadOperations()
{
int i, nbAccounts=0, nbCategories=0;
User* user = _kiss->GetUser();
Account account;
Category category;
std::map<int, int> accounts;
std::map<int, int> categories;
int oldid;
for(i=0; i<_accountsGrid->rowCount(); i++)
{
if (_accountsGrid->item(i, 2)->text() == _("Create one"))
nbAccounts++;
else
accounts[_accountsGrid->item(i, 0)->text().toInt()] =
user->GetAccountId(_accountsGrid->item(i, 1)->text());
}
for(i=0; i<_categoriesGrid->rowCount(); i++)
{
if (_categoriesGrid->item(i, 2)->text() == _("Create one"))
nbCategories++;
else
categories[_categoriesGrid->item(i, 0)->text().toInt()] =
user->GetAccountId(_categoriesGrid->item(i, 1)->text());
}
if (nbAccounts || nbCategories)
{
QString message, v;
if (nbAccounts)
{
message += v.sprintf(_("%d accounts").toStdString().c_str(), nbAccounts);
if (nbCategories) message += _(" and ");
}
if (nbCategories)
message += v.sprintf(_("%d categories").toStdString().c_str(), nbCategories);
message += _(" will be created, is it ok ?");
if (QMessageBox::question(0, "KissCount", message, QMessageBox::Yes|QMessageBox::No) == QMessageBox::No)
return;
for(i=0; i<_accountsGrid->rowCount(); i++)
{
if (_accountsGrid->item(i, 2)->text() == _("Create one"))
{
account = _unresolvedAccounts[i] ;
if (_accountsGrid->item(i, 1)->text().length())
account.name = _accountsGrid->item(i, 1)->text();
else
account.name = _accountsGrid->item(i, 0)->text();
account.number = _accountsGrid->item(i, 0)->text();
oldid = account.id;
_resolvedAccounts[oldid] = accounts[_accountsGrid->item(i, 0)->text().toInt()] = _kiss->AddAccount(account);
}
}
_accountsGrid->setRowCount(0);
for(i=0; i<_categoriesGrid->rowCount(); i++)
{
if (_categoriesGrid->item(i, 2)->text() == _("Create one"))
{
category = _unresolvedCategories[i] ;
if (_categoriesGrid->item(i, 1)->text().length())
category.name = _categoriesGrid->item(i, 1)->text();
else
category.name = _categoriesGrid->item(i, 0)->text();
oldid = category.id;
_resolvedCategories[oldid] = categories[_categoriesGrid->item(i, 0)->text().toInt()] = category.id = _kiss->AddCategory(category);
}
}
_categoriesGrid->setRowCount(0);
_wxUI->NeedReload();
}
_operations = _importEngine->GetOperations(accounts, categories);
if (_operations->size())
{
_hbox->removeWidget(_operationsGrid);
delete _operationsGrid;
_operationsGrid = new GridAccount(_kiss, this, false, false, false);
_hbox->addWidget(_operationsGrid);
_operationsGrid->LoadOperations(_operations, 0, 0);
_buttonIntegrate->setEnabled(true);
_buttonLoadOperations->setEnabled(false);
layout();
}
else
{
QMessageBox::information(0, "KissCount", _("No operation found into this file"));
}
}
void ImportPanel::OnIntegrate()
{
int i;
std::map<int, int> mapid;
int oldid, account;
bool update;
2012-04-30 21:15:51 +02:00
std::map<AccountAmount, int, AccountAmount> accountAmounts;
std::map<AccountAmount, int, AccountAmount>::iterator it;
int amount;
if (!_operations->size()) return;
if (QMessageBox::question(0, "KissCount", _("Are you sure want to integrate these operations ?"), QMessageBox::Yes|QMessageBox::No) == QMessageBox::No)
return;
_buttonIntegrate->setEnabled(false);
for(i=0; i<(int)_operations->size(); i++)
{
oldid = (*_operations)[i].id;
_kiss->AddOperation((*_operations)[i], false);
mapid[oldid] = (*_operations)[i].id;
}
for(i=0; i<(int)_operations->size(); i++)
{
update = false;
if ((*_operations)[i].parent)
{
(*_operations)[i].parent = mapid[(*_operations)[i].parent];
update = true;
}
if ((*_operations)[i].transfert)
{
(*_operations)[i].transfert = mapid[(*_operations)[i].transfert];
update = true;
}
if (update)
_kiss->UpdateOperation((*_operations)[i], false);
}
accountAmounts = _importEngine->GetAccountAmounts();
for(it=accountAmounts.begin(); it!=accountAmounts.end(); it++)
{
account = it->first.account;
if (_resolvedAccounts.count(account))
account = _resolvedAccounts[account];
amount = _kiss->GetAccountAmount(account, it->first.month, it->first.year);
if (!amount)
_kiss->SetAccountAmount(account, it->first.month, it->first.year, it->second);
}
if (_checkSaveImportPatterns->checkState() == Qt::Checked)
_kiss->UpdateImportPattern();
_operations->clear();
_operationsGrid->ClearGrid();
QMessageBox::information(0, "KissCount", _("Operations successfully imported"));
_wxUI->NeedReload();
}
void ImportPanel::OnOperationModified(int row, int col)
{
static bool update = false;
if (col != GridAccount::DESCRIPTION &&
col != GridAccount::CATEGORY &&
col != GridAccount::ACCOUNT)
return ;
if (update) return;
update = true;
_operationsGrid->ClearGrid();
if (_importEngine->UpdatePattern(row-1) > 1)
_operationsGrid->LoadOperations(_operations, 0, 0);
layout();
update = false;
}