void makePrice(const QString& _currencyid, const QDate& _date, const MyMoneyMoney& _price )
{
  MyMoneyFileTransaction ft;
  MyMoneyFile* file = MyMoneyFile::instance();
  MyMoneySecurity curr = file->currency(_currencyid);
  MyMoneyPrice price(_currencyid, file->baseCurrency().id(), _date, _price, "test");
  file->addPrice(price);
  ft.commit();
}
void makeEquityPrice(const QString& _id, const QDate& _date, const MyMoneyMoney& _price )
{
  MyMoneyFile* file = MyMoneyFile::instance();
  MyMoneyFileTransaction ft;
  QString basecurrencyid = file->baseCurrency().id();
  MyMoneyPrice price = file->price( _id, basecurrencyid, _date, true );
  if ( !price.isValid() )
  {
    MyMoneyPrice newprice( _id, basecurrencyid, _date, _price, "test" );
    file->addPrice(newprice);
  }
  ft.commit();
}
void KEquityPriceUpdateDlg::storePrices()
{
  // update the new prices into the equities

  MyMoneyFile* file = MyMoneyFile::instance();
  QList<MyMoneySecurity> equities = file->securityList();

  QTreeWidgetItem* item = 0;
  MyMoneyFileTransaction ft;
  QString name;

  try {
    for (int i = 0; i < lvEquityList->invisibleRootItem()->childCount(); ++i) {
      item = lvEquityList->invisibleRootItem()->child(i);
      // turn on signals before we modify the last entry in the list
      MyMoneyFile::instance()->blockSignals(i < lvEquityList->invisibleRootItem()->childCount() - 1);

      MyMoneyMoney rate(item->text(PRICE_COL));
      if (!rate.isZero()) {
        QString id = item->text(ID_COL).toUtf8();

        // if the ID has a space, then this is TWO ID's, so it's a currency quote
        if (QString(id).contains(" ")) {
          QStringList ids = id.split(' ', QString::SkipEmptyParts);
          QString fromid = ids[0].toUtf8();
          QString toid = ids[1].toUtf8();
          name = QString("%1 --> %2").arg(fromid).arg(toid);
          MyMoneyPrice price(fromid, toid, QDate().fromString(item->text(DATE_COL), Qt::ISODate), rate, item->text(SOURCE_COL));
          file->addPrice(price);
        } else
          // otherwise, it's a security quote
        {
          MyMoneySecurity security = MyMoneyFile::instance()->security(id);
          name = security.name();
          MyMoneyPrice price(id, security.tradingCurrency(), QDate().fromString(item->text(DATE_COL), Qt::ISODate), rate, item->text(SOURCE_COL));

          // TODO (Ace) Better handling of the case where there is already a price
          // for this date.  Currently, it just overrides the old value.  Really it
          // should check to see if the price is the same and prompt the user.
          MyMoneyFile::instance()->addPrice(price);
        }

      }
    }
    ft.commit();

  } catch (const MyMoneyException &) {
    qDebug("Unable to add price information for %s", qPrintable(name));
  }
}
void InvTransactionHelper::init( const QDate& _date, const QString& _action, MyMoneyMoney _shares, MyMoneyMoney _price, const QString& _stockaccountid, const QString& _transferid, const QString& _categoryid )
{
  MyMoneyFile* file = MyMoneyFile::instance();
  MyMoneyAccount stockaccount = file->account(_stockaccountid);
  MyMoneyMoney value = _shares * _price;

  setPostDate(_date);

  setCommodity("USD");
  MyMoneySplit s1;
  s1.setValue(value);
  s1.setAccountId(_stockaccountid);

  if ( _action == MyMoneySplit::ActionReinvestDividend )
  {
    s1.setShares(_shares);
    s1.setAction(MyMoneySplit::ActionReinvestDividend);

    MyMoneySplit s2;
    s2.setAccountId(_categoryid);
    s2.setShares(-value);
    s2.setValue(-value);
    addSplit(s2);
  }
  else if ( _action == MyMoneySplit::ActionDividend || _action == MyMoneySplit::ActionYield )
  {
    s1.setAccountId(_categoryid);
    s1.setShares(-value);
    s1.setValue(-value);

    // Split 2 will be the zero-amount investment split that serves to
    // mark this transaction as a cash dividend and note which stock account
    // it belongs to.
    MyMoneySplit s2;
    s2.setValue(0);
    s2.setShares(0);
    s2.setAction(_action);
    s2.setAccountId(_stockaccountid);
    addSplit(s2);

    MyMoneySplit s3;
    s3.setAccountId(_transferid);
    s3.setShares(value);
    s3.setValue(value);
    addSplit(s3);
  }
  else if ( _action == MyMoneySplit::ActionBuyShares )
  {
    s1.setShares(_shares);
    s1.setAction(MyMoneySplit::ActionBuyShares);

    MyMoneySplit s3;
    s3.setAccountId(_transferid);
    s3.setShares(-value);
    s3.setValue(-value);
    addSplit(s3);
  }
  addSplit(s1);

  //kdDebug(2) << "created transaction, now adding..." << endl;

  MyMoneyFileTransaction ft;
  file->addTransaction(*this);

  //kdDebug(2) << "updating price..." << endl;

  // update the price, while we're here
  QString stockid = stockaccount.currencyId();
  QString basecurrencyid = file->baseCurrency().id();
  MyMoneyPrice price = file->price( stockid, basecurrencyid, _date, true );
  if ( !price.isValid() )
  {
    MyMoneyPrice newprice( stockid, basecurrencyid, _date, _price, "test" );
    file->addPrice(newprice);
  }
  ft.commit();
  //kdDebug(2) << "successfully added " << id() << endl;
}