summaryrefslogtreecommitdiffstats
path: root/kmymoney2/dialogs/ksplittransactiondlg.cpp
blob: a197b4cb58222573e81e822182fd24e77f94dc37 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
/***************************************************************************
                          ksplittransactiondlg.cpp  -  description
                             -------------------
    begin                : Thu Jan 10 2002
    copyright            : (C) 2000-2002 by Michael Edwardes
    email                : mte@users.sourceforge.net
                           Javier Campos Morales <javi_c@users.sourceforge.net>
                           Felix Rodriguez <frodriguez@users.sourceforge.net>
                           John C <thetacoturtle@users.sourceforge.net>
                           Thomas Baumgart <ipwizard@users.sourceforge.net>
                           Kevin Tambascio <ktambascio@users.sourceforge.net>
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   This program 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 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 ***************************************************************************/

// ----------------------------------------------------------------------------
// QT Includes

#include <qpushbutton.h>
#include <qlabel.h>
#include <qtable.h>
#include <qtimer.h>
#include <qptrlist.h>
#include <qbuttongroup.h>
#include <qradiobutton.h>
#include <qcursor.h>

// ----------------------------------------------------------------------------
// KDE Includes

#include <kglobal.h>
#include <kconfig.h>
#include <klocale.h>
#include <kmessagebox.h>
#include <kpushbutton.h>
#include <kactivelabel.h>
#include <kstdguiitem.h>
#include <kapplication.h>

// ----------------------------------------------------------------------------
// Project Includes

#include "ksplittransactiondlg.h"
#include <kmymoney/kmymoneyedit.h>
#include <kmymoney/kmymoneylineedit.h>
#include <kmymoney/mymoneyfile.h>

#include "kmymoneysplittable.h"
#include "../dialogs/ksplitcorrectiondlg.h"

KSplitTransactionDlg::KSplitTransactionDlg(const MyMoneyTransaction& t,
                                           const MyMoneySplit& s,
                                           const MyMoneyAccount& acc,
                                           const bool amountValid,
                                           const bool deposit,
                                           const MyMoneyMoney& calculatedValue,
                                           const QMap<QString, MyMoneyMoney>& priceInfo,
                                           QWidget* parent, const char* name) :
  KSplitTransactionDlgDecl(parent, name, true),
  m_account(acc),
  m_split(s),
  m_precision(2),
  m_amountValid(amountValid),
  m_isDeposit(deposit),
  m_calculatedValue(calculatedValue)
{
  // add icons to buttons
  KIconLoader *il = KGlobal::iconLoader();

  KGuiItem clearButtenItem( i18n( "Clear &All" ),
                    QIconSet(il->loadIcon("edittrash", KIcon::Small, KIcon::SizeSmall)),
                    i18n("Clear all splits"),
                    i18n("Use this to clear all splits of this transaction"));
  clearAllBtn->setGuiItem(clearButtenItem);


  KGuiItem mergeButtenItem( i18n( "&Merge" ),
                             QIconSet(il->loadIcon("math_sum", KIcon::Small, KIcon::SizeSmall)),
                                      "", "");
  mergeBtn->setGuiItem(mergeButtenItem);

  // make finish the default
  finishBtn->setDefault(true);

  // setup the focus
  cancelBtn->setFocusPolicy(QWidget::NoFocus);
  finishBtn->setFocusPolicy(QWidget::NoFocus);
  clearAllBtn->setFocusPolicy(QWidget::NoFocus);

  // connect signals with slots
  connect(transactionsTable, SIGNAL(transactionChanged(const MyMoneyTransaction&)),
          this, SLOT(slotSetTransaction(const MyMoneyTransaction&)));
  connect(transactionsTable, SIGNAL(createCategory(const QString&, QString&)), this, SLOT(slotCreateCategory(const QString&, QString&)));
  connect(transactionsTable, SIGNAL(objectCreation(bool)), this, SIGNAL(objectCreation(bool)));

  connect(transactionsTable, SIGNAL(returnPressed()), this, SLOT(accept()));
  connect(transactionsTable, SIGNAL(escapePressed()), this, SLOT(reject()));

  connect(cancelBtn, SIGNAL(clicked()), this, SLOT(reject()));
  connect(finishBtn, SIGNAL(clicked()), this, SLOT(accept()));
  connect(clearAllBtn, SIGNAL(clicked()), this, SLOT(slotClearAllSplits()));
  connect(mergeBtn, SIGNAL(clicked()), this, SLOT(slotMergeSplits()));
  connect(clearZeroBtn, SIGNAL(clicked()), this, SLOT(slotClearUnusedSplits()));

  // setup the precision
  try {
    MyMoneySecurity currency = MyMoneyFile::instance()->currency(t.commodity());
    m_precision = MyMoneyMoney::denomToPrec(m_account.fraction(currency));
  } catch(MyMoneyException *e) {
    delete e;
  }

  slotSetTransaction(t);

  // pass on those vars
  transactionsTable->setup(priceInfo);

  QSize size(width(), height());
  kapp->config()->setGroup("SplitTransactionEditor");
  size = kapp->config()->readSizeEntry("Geometry", &size);
  size.setHeight(size.height()-1);
  QDialog::resize( size.expandedTo(minimumSizeHint()) );

  // Trick: it seems, that the initial sizing of the dialog does
  // not work correctly. At least, the columns do not get displayed
  // correct. Reason: the return value of transactionsTable->visibleWidth()
  // is incorrect. If the widget is visible, resizing works correctly.
  // So, we let the dialog show up and resize it then. It's not really
  // clean, but the only way I got the damned thing working.
  QTimer::singleShot( 10, this, SLOT(initSize()) );
}

KSplitTransactionDlg::~KSplitTransactionDlg()
{
  kapp->config()->setGroup("SplitTransactionEditor");
  kapp->config()->writeEntry("Geometry", size());
}

int KSplitTransactionDlg::exec(void)
{
  // for deposits, we invert the sign of all splits.
  // don't forget to revert when we're done ;-)
  if(m_isDeposit) {
    for(unsigned i = 0; i < m_transaction.splits().count(); ++i) {
      MyMoneySplit split = m_transaction.splits()[i];
      split.setValue(-split.value());
      split.setShares(-split.shares());
      m_transaction.modifySplit(split);
    }
  }

  int rc;
  do {
    transactionsTable->setFocus();

    // initialize the display
    transactionsTable->setTransaction(m_transaction, m_split, m_account);
    updateSums();

    rc = KSplitTransactionDlgDecl::exec();

    if(rc == QDialog::Accepted) {
      if(!diffAmount().isZero()) {
        KSplitCorrectionDlgDecl* corrDlg = new KSplitCorrectionDlgDecl(this, 0, true);

        // add icons to buttons
        corrDlg->okBtn->setGuiItem(KStdGuiItem::ok());
        corrDlg->cancelBtn->setGuiItem(KStdGuiItem::cancel());

        MyMoneySplit split = m_transaction.splits()[0];
        QString total = (-split.value()).formatMoney("", m_precision);
        QString sums = splitsValue().formatMoney("", m_precision);
        QString diff = diffAmount().formatMoney("", m_precision);

        // now modify the text items of the dialog to contain the correct values
        QString q = i18n("The total amount of this transaction is %1 while "
                                "the sum of the splits is %2. The remaining %3 are "
                                "unassigned.")
                    .arg(total)
                    .arg(sums)
                    .arg(diff);
        corrDlg->explanation->setText(q);

        q = i18n("Change &total amount of transaction to %1.").arg(sums);
        corrDlg->changeBtn->setText(q);

        q = i18n("&Distribute difference of %1 among all splits.").arg(diff);
        corrDlg->distributeBtn->setText(q);
        // FIXME remove the following line once distribution among
        //       all splits is implemented
        corrDlg->distributeBtn->hide();


        // if we have only two splits left, we don't allow leaving sth. unassigned.
        if(m_transaction.splitCount() < 3) {
          q = i18n("&Leave total amount of transaction at %1.").arg(total);
        } else {
          q = i18n("&Leave %1 unassigned.").arg(diff);
        }
        corrDlg->leaveBtn->setText(q);

        if((rc = corrDlg->exec()) == QDialog::Accepted) {
          QButton* button = corrDlg->buttonGroup->selected();
          if(button != 0) {
            switch(corrDlg->buttonGroup->id(button)) {
              case 0:       // continue to edit
                rc = QDialog::Rejected;
                break;

              case 1:       // modify total
                split.setValue(-splitsValue());
                split.setShares(-splitsValue());
                m_transaction.modifySplit(split);
                break;

              case 2:       // distribute difference
                qDebug("distribution of difference not yet supported in KSplitTransactionDlg::slotFinishClicked()");
                break;

              case 3:       // leave unassigned
                break;
            }
          }
        }
        delete corrDlg;
      }
    } else
      break;

  } while(rc != QDialog::Accepted);

  // for deposits, we inverted the sign of all splits.
  // now we revert it back, so that things are left correct
  if(m_isDeposit) {
    for(unsigned i = 0; i < m_transaction.splits().count(); ++i) {
      MyMoneySplit split = m_transaction.splits()[i];
      split.setValue(-split.value());
      split.setShares(-split.shares());
      m_transaction.modifySplit(split);
    }
  }

  return rc;
}

void KSplitTransactionDlg::initSize(void)
{
  QDialog::resize(width(), height()+1);
}

void KSplitTransactionDlg::accept()
{
  transactionsTable->slotCancelEdit();
  KSplitTransactionDlgDecl::accept();
}

void KSplitTransactionDlg::reject()
{
  // cancel any edit activity in the split register
  transactionsTable->slotCancelEdit();
  KSplitTransactionDlgDecl::reject();
}

void KSplitTransactionDlg::slotClearAllSplits(void)
{
  transactionsTable->slotEndEdit();
  int answer;
  answer = KMessageBox::warningContinueCancel (this,
     i18n("You are about to delete all splits of this transaction. "
          "Do you really want to continue?"),
     i18n("KMyMoney"),
     i18n("Continue")
     );

  if(answer == KMessageBox::Continue) {
    transactionsTable->slotCancelEdit();
    QValueList<MyMoneySplit> list = transactionsTable->getSplits(m_transaction);
    QValueList<MyMoneySplit>::ConstIterator it;

    // clear all but the one referencing the account
    for(it = list.begin(); it != list.end(); ++it) {
      m_transaction.removeSplit(*it);
    }

    transactionsTable->setTransaction(m_transaction, m_split, m_account);
    slotSetTransaction(m_transaction);
  }
}

void KSplitTransactionDlg::slotClearUnusedSplits(void)
{
  transactionsTable->slotEndEdit();

  QValueList<MyMoneySplit> list = transactionsTable->getSplits(m_transaction);
  QValueList<MyMoneySplit>::ConstIterator it;

  try {
    // remove all splits that don't have a value assigned
    for(it = list.begin(); it != list.end(); ++it) {
      if((*it).shares().isZero()) {
        m_transaction.removeSplit(*it);
      }
    }

    transactionsTable->setTransaction(m_transaction, m_split, m_account);
    slotSetTransaction(m_transaction);
  } catch(MyMoneyException* e) {
    delete e;
  }
}

void KSplitTransactionDlg::slotMergeSplits(void)
{
  transactionsTable->slotEndEdit();

  QValueList<MyMoneySplit> list = transactionsTable->getSplits(m_transaction);
  QValueList<MyMoneySplit>::ConstIterator it;

  try {
    // collect all splits, merge them if needed and remove from transaction
    QValueList<MyMoneySplit> splits;
    for(it = list.begin(); it != list.end(); ++it) {
      QValueList<MyMoneySplit>::iterator it_s;
      for(it_s = splits.begin(); it_s != splits.end(); ++it_s) {
        if((*it_s).accountId() == (*it).accountId()
        && (*it_s).memo().isEmpty() && (*it).memo().isEmpty())
          break;
      }
      if(it_s != splits.end()) {
        (*it_s).setShares((*it).shares() + (*it_s).shares());
        (*it_s).setValue((*it).value() + (*it_s).value());
      } else {
        splits << *it;
      }
      m_transaction.removeSplit(*it);
    }

    // now add them back to the transaction
    QValueList<MyMoneySplit>::iterator it_s;
    for(it_s = splits.begin(); it_s != splits.end(); ++it_s) {
      (*it_s).clearId();
      m_transaction.addSplit(*it_s);
    }

    transactionsTable->setTransaction(m_transaction, m_split, m_account);
    slotSetTransaction(m_transaction);
  } catch(MyMoneyException* e) {
    delete e;
  }
}

void KSplitTransactionDlg::slotSetTransaction(const MyMoneyTransaction& t)
{
  transactionsTable->slotCancelEdit();

  m_transaction = t;
  QValueList<MyMoneySplit> list = transactionsTable->getSplits(m_transaction);
  QValueList<MyMoneySplit>::ConstIterator it;

  // check if we can merge splits or not, have zero splits or not
  QMap<QString, int> splits;
  bool haveZeroSplit = false;
  for(it = list.begin(); it != list.end(); ++it) {
    splits[(*it).accountId()]++;
    if(((*it).id() != m_split.id()) && ((*it).shares().isZero()))
      haveZeroSplit = true;
  }
  QMap<QString, int>::const_iterator it_s;
  for(it_s = splits.begin(); it_s != splits.end(); ++it_s) {
    if((*it_s) > 1)
      break;
  }
  mergeBtn->setDisabled(it_s == splits.end());
  clearZeroBtn->setEnabled(haveZeroSplit);

  updateSums();
}

void KSplitTransactionDlg::updateSums(void)
{
  MyMoneyMoney splits(splitsValue());

  if(m_amountValid == false) {
    m_split.setValue(-splits);
    m_transaction.modifySplit(m_split);
  }

  splitSum->setText("<b>" + splits.formatMoney("", m_precision) + " ");
  splitUnassigned->setText("<b>" + diffAmount().formatMoney("", m_precision) + " ");
  transactionAmount->setText("<b>" + (-m_split.value()).formatMoney("", m_precision) + " ");
}

MyMoneyMoney KSplitTransactionDlg::splitsValue(void)
{
  MyMoneyMoney splitsValue(m_calculatedValue);
  QValueList<MyMoneySplit> list = transactionsTable->getSplits(m_transaction);
  QValueList<MyMoneySplit>::ConstIterator it;

  // calculate the current sum of all split parts
  for(it = list.begin(); it != list.end(); ++it) {
    if((*it).value() != MyMoneyMoney::autoCalc)
      splitsValue += (*it).value();
  }

  return splitsValue;
}

MyMoneyMoney KSplitTransactionDlg::diffAmount(void)
{
  MyMoneyMoney diff(0);

  // if there is an amount specified in the transaction, we need to calculate the
  // difference, otherwise we display the difference as 0 and display the same sum.
  if(m_amountValid) {
    MyMoneySplit split = m_transaction.splits()[0];

    diff = -(splitsValue() + split.value());
  }
  return diff;
}

void KSplitTransactionDlg::slotCreateCategory(const QString& name, QString& id)
{
  MyMoneyAccount acc, parent;
  acc.setName(name);

  if(m_isDeposit)
    parent = MyMoneyFile::instance()->income();
  else
    parent = MyMoneyFile::instance()->expense();

  // TODO extract possible first part of a hierarchy and check if it is one
  // of our top categories. If so, remove it and select the parent
  // according to this information.

  emit createCategory(acc, parent);

  // return id
  id = acc.id();
}

#include "ksplittransactiondlg.moc"