summaryrefslogtreecommitdiffstats
path: root/kmymoney2/dialogs/knewinvestmentwizard.cpp
blob: 3a963f2662f9c0bf46030d28aa5523573f3e373b (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
/***************************************************************************
                         knewinvestmentwizard  -  description
                            -------------------
   begin                : Sat Dec 4 2004
   copyright            : (C) 2004 by Thomas Baumgart
   email                : kmymoney2-developer@lists.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 <tqcheckbox.h>
#include <tqlabel.h>

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

#include <kpushbutton.h>
#include <kcombobox.h>
#include <kurlrequester.h>
#include <klocale.h>
#include <kapplication.h>
#include <kmessagebox.h>

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

#include "knewinvestmentwizard.h"

#include <kmymoney/kmymoneylineedit.h>
#include <kmymoney/kmymoneyedit.h>
#include <kmymoney/mymoneysecurity.h>
#include <kmymoney/mymoneyfile.h>
#include <kmymoney/kmymoneycombo.h>
#include "../widgets/kmymoneycurrencyselector.h"
#include "../converter/webpricequote.h"
#include "../kmymoneyutils.h"

KNewInvestmentWizard::KNewInvestmentWizard( TQWidget *tqparent, const char *name ) :
  KNewInvestmentWizardDecl( tqparent, name )
{
  init1();
  slotCheckPage(TQString());

  m_investmentSymbol->setFocus();
  connect(m_investmentSymbol, TQT_SIGNAL(lineChanged(const TQString&)), this, TQT_SLOT(slotCheckForExistingSymbol(const TQString&)));
}

KNewInvestmentWizard::KNewInvestmentWizard( const MyMoneyAccount& acc, TQWidget *tqparent, const char *name ) :
  KNewInvestmentWizardDecl( tqparent, name ),
  m_account(acc)
{
  setCaption(i18n("Investment detail wizard"));
  init1();

  // load the widgets with the data
  setName(m_account.name());
  m_security = MyMoneyFile::instance()->security(m_account.currencyId());

  init2();

  int priceMode = 0;
  if(!m_account.value("priceMode").isEmpty())
    priceMode = m_account.value("priceMode").toInt();
  m_priceMode->setCurrentItem(priceMode);

}

KNewInvestmentWizard::KNewInvestmentWizard( const MyMoneySecurity& security, TQWidget *tqparent, const char *name ) :
  KNewInvestmentWizardDecl( tqparent, name ),
  m_security(security)
{
  setCaption(i18n("Security detail wizard"));
  init1();
  m_createAccount = false;

  // load the widgets with the data
  setName(security.name());

  init2();

  // no chance to change the price mode here
  m_priceMode->setCurrentItem(0);
  m_priceMode->setEnabled(false);
}

void KNewInvestmentWizard::init1(void)
{
  m_onlineSourceCombo->insertStringList( WebPriceQuote::quoteSources() );

  m_onlineFactor->setValue(MyMoneyMoney(1,1));
  m_onlineFactor->setPrecision(4);

  m_fraction->setPrecision(0);
  m_fraction->setValue(MyMoneyMoney(100, 1));
  kMyMoneyMoneyValidator* fractionValidator = new kMyMoneyMoneyValidator(1, 100000, 0, TQT_TQOBJECT(this));
  m_fraction->setValidator(fractionValidator);

  // load the price mode combo
  m_priceMode->insertItem(i18n("default price mode", "<default>"), 0);
  m_priceMode->insertItem(i18n("Price per share"), 1);
  m_priceMode->insertItem(i18n("Total for all shares"), 2);

  // load the widget with the available currencies
  m_tradingCurrencyEdit->update(TQString());

  connect(helpButton(),TQT_SIGNAL(clicked()), this, TQT_SLOT(slotHelp(void)));
  connect(m_investmentName, TQT_SIGNAL(textChanged(const TQString&)), this, TQT_SLOT(slotCheckPage(void)));
  connect(m_investmentSymbol, TQT_SIGNAL(textChanged(const TQString&)), this, TQT_SLOT(slotCheckPage(void)));
  connect(m_fraction, TQT_SIGNAL(textChanged(const TQString&)), this, TQT_SLOT(slotCheckPage(void)));
  connect(m_investmentIdentification, TQT_SIGNAL(textChanged(const TQString&)), this, TQT_SLOT(slotCheckPage(void)));
  connect(m_onlineFactor, TQT_SIGNAL(textChanged(const TQString&)), this, TQT_SLOT(slotCheckPage(void)));
  connect(m_onlineSourceCombo, TQT_SIGNAL(activated(const TQString&)), this, TQT_SLOT(slotCheckPage(const TQString&)));
  connect(m_useFinanceQuote, TQT_SIGNAL(toggled(bool)), this, TQT_SLOT(slotSourceChanged(bool)));

  m_createAccount = true;

  // Update label in case of edit
  if(!m_account.id().isEmpty()) {
    m_introLabel->setText(i18n("This wizard allows you to modify the selected investment."));
  }
  if(!m_security.id().isEmpty()) {
    m_introLabel->setText(i18n("This wizard allows you to modify the selected security."));
  }


}

void KNewInvestmentWizard::init2(void)
{
  MyMoneySecurity tradingCurrency = MyMoneyFile::instance()->currency(m_security.tradingCurrency());
  m_investmentSymbol->setText(m_security.tradingSymbol());
  m_tradingMarket->setCurrentText(m_security.tradingMarket());
  m_fraction->setValue(MyMoneyMoney(m_security.smallestAccountFraction(), 1));
  m_tradingCurrencyEdit->setSecurity(tradingCurrency);
  if (m_security.value("kmm-online-quote-system") == "Finance::Quote") {
    FinanceQuoteProcess p;
    m_useFinanceQuote->setChecked(true);
    m_onlineSourceCombo->setCurrentText(p.niceName(m_security.value("kmm-online-source")));
  } else {
    m_onlineSourceCombo->setCurrentText(m_security.value("kmm-online-source"));
  }
  if(!m_security.value("kmm-online-factor").isEmpty())
    m_onlineFactor->setValue(MyMoneyMoney(m_security.value("kmm-online-factor")));
  m_investmentIdentification->setText(m_security.value("kmm-security-id"));
  m_securityType->setCurrentText(KMyMoneyUtils::securityTypeToString(m_security.securityType()));

  slotCheckPage(m_security.value("kmm-online-source"));
}

KNewInvestmentWizard::~KNewInvestmentWizard()
{
}

void KNewInvestmentWizard::setName(const TQString& name)
{
  m_investmentName->setText(name);
}

void KNewInvestmentWizard::next(void)
{
  KNewInvestmentWizardDecl::next();
  slotCheckPage();
}

void KNewInvestmentWizard::slotCheckForExistingSymbol(const TQString& symbol)
{
  if(m_investmentName->text().isEmpty()) {
    TQValueList<MyMoneySecurity> list = MyMoneyFile::instance()->securityList();
    TQValueList<MyMoneySecurity>::const_iterator it_s;
    MyMoneySecurity::eSECURITYTYPE type = KMyMoneyUtils::stringToSecurity(m_securityType->currentText());

    for(it_s = list.begin(); it_s != list.end(); ++it_s) {
      if((*it_s).securityType() == type
      && (*it_s).tradingSymbol() == m_investmentSymbol->text()) {
        m_security = MyMoneySecurity();
        if(KMessageBox::questionYesNo(this, i18n("The selected symbol is already on file. Do you want to reuse the existing security?"), i18n("Security found")) == KMessageBox::Yes) {
          m_security = *it_s;
          init2();
          m_investmentName->loadText(m_security.name());
        }
        break;
      }
    }
  }
}

void KNewInvestmentWizard::slotSourceChanged(bool useFQ)
{
  m_onlineSourceCombo->clear();
  m_onlineSourceCombo->insertItem(TQString(), 0);
  if (useFQ) {
    m_onlineSourceCombo->insertStringList( WebPriceQuote::quoteSources( WebPriceQuote::FinanceQuote ) );
  } else {
    m_onlineSourceCombo->insertStringList( WebPriceQuote::quoteSources() );
  }
}

void KNewInvestmentWizard::slotCheckPage(const TQString& txt)
{
  m_onlineFactor->setEnabled(!txt.isEmpty());
}

void KNewInvestmentWizard::slotCheckPage(void)
{
  if(currentPage() == m_investmentDetailsPage) {
    setNextEnabled(m_investmentDetailsPage, false);
    if(m_investmentName->text().length() > 0
    && m_investmentSymbol->text().length() > 0
    && !m_fraction->value().isZero()
    ) {
      setNextEnabled(m_investmentDetailsPage, true);
    }
  } else if(currentPage() == m_onlineUpdatePage) {
    setFinishEnabled(m_onlineUpdatePage, true);
    if(m_onlineFactor->isEnabled() && m_onlineFactor->value().isZero())
      setFinishEnabled(m_onlineUpdatePage, false);
  }
}

void KNewInvestmentWizard::slotHelp(void)
{
  kapp->invokeHelp("details.investments.newinvestmentwizard");
}

void KNewInvestmentWizard::createObjects(const TQString& parentId)
{
  MyMoneyFile* file = MyMoneyFile::instance();

  TQValueList<MyMoneySecurity> list = MyMoneyFile::instance()->securityList();
  TQValueList<MyMoneySecurity>::ConstIterator it;

  MyMoneySecurity::eSECURITYTYPE type = KMyMoneyUtils::stringToSecurity(m_securityType->currentText());
  MyMoneyFileTransaction ft;
  try {
    // update all relevant attributes only, if we create a stock
    // account and the security is unknown or we modifiy the security
    MyMoneySecurity newSecurity(m_security);
    newSecurity.setName(m_investmentName->text());
    newSecurity.setTradingSymbol(m_investmentSymbol->text());
    newSecurity.setTradingMarket(m_tradingMarket->currentText());
    newSecurity.setSmallestAccountFraction(m_fraction->value());
    newSecurity.setTradingCurrency(m_tradingCurrencyEdit->security().id());
    newSecurity.setSecurityType(type);
    newSecurity.deletePair("kmm-online-source");
    newSecurity.deletePair("kmm-online-quote-system");
    newSecurity.deletePair("kmm-online-factor");
    newSecurity.deletePair("kmm-security-id");

    if(!m_onlineSourceCombo->currentText().isEmpty()) {
      if (m_useFinanceQuote->isChecked()) {
        FinanceQuoteProcess p;
        newSecurity.setValue("kmm-online-quote-system", "Finance::Quote");
        newSecurity.setValue("kmm-online-source", p.crypticName(m_onlineSourceCombo->currentText()));
      }else{
        newSecurity.setValue("kmm-online-source", m_onlineSourceCombo->currentText());
      }
    }
    if(m_onlineFactor->isEnabled() && (m_onlineFactor->value() != MyMoneyMoney(1,1)))
      newSecurity.setValue("kmm-online-factor", m_onlineFactor->value().toString());
    if(!m_investmentIdentification->text().isEmpty())
      newSecurity.setValue("kmm-security-id", m_investmentIdentification->text());

    if(m_security.id().isEmpty() || newSecurity != m_security) {
      m_security = newSecurity;

      // add or update it
      if(m_security.id().isEmpty()) {
        file->addSecurity(m_security);
      } else {
        file->modifySecurity(m_security);
      }
    }

    if(m_createAccount) {
      // now that the security exists, we can add the account to store it
      m_account.setName(m_investmentName->text());
      if(m_account.accountType() == MyMoneyAccount::UnknownAccountType)
        m_account.setAccountType(MyMoneyAccount::Stock);

      m_account.setCurrencyId(m_security.id());
      switch(m_priceMode->currentItem()) {
        case 0:
          m_account.deletePair("priceMode");
          break;
        case 1:
        case 2:
          m_account.setValue("priceMode", TQString("%1").tqarg(m_priceMode->currentItem()));
          break;
      }

      if(m_account.id().isEmpty()) {
        MyMoneyAccount tqparent = file->account(parentId);
        file->addAccount(m_account, tqparent);
      } else
        file->modifyAccount(m_account);
    }
    ft.commit();
  } catch(MyMoneyException* e) {
    KMessageBox::detailedSorry(0, i18n("Unable to create all objects for the investment"), TQString("%1 caugt in %2:%3").tqarg(e->what()).tqarg(e->file()).tqarg(e->line()));
    delete e;
  }
}

#include "knewinvestmentwizard.moc"