QString CsvUtil::nameToId(const QString& name, MyMoneyAccount& parent) { // Adapted from KMyMoneyApp::createAccount(MyMoneyAccount& newAccount, MyMoneyAccount& parentAccount, MyMoneyAccount& brokerageAccount, MyMoneyMoney openingBal) // Needed to find/create category:sub-categories MyMoneyFile* file = MyMoneyFile::instance(); QString id = file->categoryToAccount(name, MyMoneyAccount::UnknownAccountType); // if it does not exist, we have to create it if (id.isEmpty()) { MyMoneyAccount newAccount; MyMoneyAccount parentAccount = parent; newAccount.setName(name) ; int pos; // check for ':' in the name and use it as separator for a hierarchy while ((pos = newAccount.name().indexOf(MyMoneyFile::AccountSeperator)) != -1) { QString part = newAccount.name().left(pos); QString remainder = newAccount.name().mid(pos + 1); const MyMoneyAccount& existingAccount = file->subAccountByName(parentAccount, part); if (existingAccount.id().isEmpty()) { newAccount.setName(part); newAccount.setAccountType(parentAccount.accountType()); file->addAccount(newAccount, parentAccount); parentAccount = newAccount; } else { parentAccount = existingAccount; } newAccount.setParentAccountId(QString()); // make sure, there's no parent newAccount.clearId(); // and no id set for adding newAccount.removeAccountIds(); // and no sub-account ids newAccount.setName(remainder); }//end while newAccount.setAccountType(parentAccount.accountType()); // make sure we have a currency. If none is assigned, we assume base currency if (newAccount.currencyId().isEmpty()) newAccount.setCurrencyId(file->baseCurrency().id()); file->addAccount(newAccount, parentAccount); id = newAccount.id(); } return id; }
const QString CsvUtil::checkCategory(const QString& name, const MyMoneyMoney& value, const MyMoneyMoney& value2) { // Borrowed from MyMoneyQifReader::checkCategory() QString accountId; MyMoneyFile *file = MyMoneyFile::instance(); MyMoneyAccount account; bool found = true; if (!name.isEmpty()) { // The category might be constructed with an arbitraty depth (number of // colon delimited fields). We try to find a parent account within this // hierarchy by searching the following sequence: // // aaaa:bbbb:cccc:ddddd // // 1. search aaaa:bbbb:cccc:dddd, create nothing // 2. search aaaa:bbbb:cccc , create dddd // 3. search aaaa:bbbb , create cccc:dddd // 4. search aaaa , create bbbb:cccc:dddd // 5. don't search , create aaaa:bbbb:cccc:dddd account.setName(name); QString accName; // part to be created (right side in above list) QString parent(name); // a possible parent part (left side in above list) do { accountId = file->categoryToAccount(parent); if (accountId.isEmpty()) { found = false; // prepare next step if (!accName.isEmpty()) accName.prepend(':'); accName.prepend(parent.section(':', -1)); account.setName(accName); parent = parent.section(':', 0, -2); } else if (!accName.isEmpty()) { account.setParentAccountId(accountId); } } while (!parent.isEmpty() && accountId.isEmpty()); // if we did not find the category, we create it if (!found) { MyMoneyAccount parent; if (account.parentAccountId().isEmpty()) { if (!value.isNegative() && value2.isNegative()) parent = file->income(); else parent = file->expense(); } else { parent = file->account(account.parentAccountId()); } account.setAccountType((!value.isNegative() && value2.isNegative()) ? MyMoneyAccount::Income : MyMoneyAccount::Expense); MyMoneyAccount brokerage; // clear out the parent id, because createAccount() does not like that account.setParentAccountId(QString()); createAccount(account, parent, brokerage, MyMoneyMoney()); accountId = account.id(); } } return accountId; }
void KNewAccountDlg::okClicked() { MyMoneyFile* file = MyMoneyFile::instance(); QString accountNameText = accountNameEdit->text(); if (accountNameText.isEmpty()) { KMessageBox::error(this, i18n("You have not specified a name.\nPlease fill in this field.")); accountNameEdit->setFocus(); return; } MyMoneyAccount parent = parentAccount(); if (parent.name().length() == 0) { KMessageBox::error(this, i18n("Please select a parent account.")); return; } if (!m_categoryEditor) { QString institutionNameText = m_qcomboboxInstitutions->currentText(); if (institutionNameText != i18n("<No Institution>")) { try { MyMoneyFile *file = MyMoneyFile::instance(); QValueList<MyMoneyInstitution> list = file->institutionList(); QValueList<MyMoneyInstitution>::ConstIterator institutionIterator; for (institutionIterator = list.begin(); institutionIterator != list.end(); ++institutionIterator) { if ((*institutionIterator).name() == institutionNameText) m_account.setInstitutionId((*institutionIterator).id()); } } catch (MyMoneyException *e) { qDebug("Exception in account institution set: %s", e->what().latin1()); delete e; } } else { m_account.setInstitutionId(QString()); } } m_account.setName(accountNameText); m_account.setNumber(accountNoEdit->text()); storeKVP("iban", ibanEdit); storeKVP("minBalanceAbsolute", m_minBalanceAbsoluteEdit); storeKVP("minBalanceEarly", m_minBalanceEarlyEdit); // the figures for credit line with reversed sign if(!m_maxCreditAbsoluteEdit->lineedit()->text().isEmpty()) m_maxCreditAbsoluteEdit->setValue(m_maxCreditAbsoluteEdit->value()*MyMoneyMoney(-1,1)); if(!m_maxCreditEarlyEdit->lineedit()->text().isEmpty()) m_maxCreditEarlyEdit->setValue(m_maxCreditEarlyEdit->value()*MyMoneyMoney(-1,1)); storeKVP("maxCreditAbsolute", m_maxCreditAbsoluteEdit); storeKVP("maxCreditEarly", m_maxCreditEarlyEdit); if(!m_maxCreditAbsoluteEdit->lineedit()->text().isEmpty()) m_maxCreditAbsoluteEdit->setValue(m_maxCreditAbsoluteEdit->value()*MyMoneyMoney(-1,1)); if(!m_maxCreditEarlyEdit->lineedit()->text().isEmpty()) m_maxCreditEarlyEdit->setValue(m_maxCreditEarlyEdit->value()*MyMoneyMoney(-1,1)); storeKVP("lastNumberUsed", m_lastCheckNumberUsed); // delete a previous version of the minimumbalance information storeKVP("minimumBalance", QString(), QString()); MyMoneyAccount::accountTypeE acctype; if (!m_categoryEditor) { acctype = KMyMoneyUtils::stringToAccountType(typeCombo->currentText()); // If it's a loan, check if the parent is asset or liability. In // case of asset, we change the account type to be AssetLoan if(acctype == MyMoneyAccount::Loan && parent.accountGroup() == MyMoneyAccount::Asset) acctype = MyMoneyAccount::AssetLoan; #if 0 // we do allow the same name for different accounts, so // we don't need this check anymore. if(!file->nameToAccount(accountNameText).isEmpty() && (file->nameToAccount(accountNameText) != m_account.id())) { KMessageBox::error(this, QString("<qt>")+i18n("An account named <b>%1</b> already exists. You cannot create a second account with the same name.").arg(accountNameText)+QString("</qt>")); return; } #endif } else { acctype = parent.accountGroup(); QString newName; if(!MyMoneyFile::instance()->isStandardAccount(parent.id())) { newName = MyMoneyFile::instance()->accountToCategory(parent.id()) + MyMoneyFile::AccountSeperator; } newName += accountNameText; if(!file->categoryToAccount(newName, acctype).isEmpty() && (file->categoryToAccount(newName, acctype) != m_account.id())) { KMessageBox::error(this, QString("<qt>")+i18n("A category named <b>%1</b> already exists. You cannot create a second category with the same name.").arg(newName)+QString("</qt>")); return; } } m_account.setAccountType(acctype); m_account.setDescription(descriptionEdit->text()); if (!m_categoryEditor) { m_account.setOpeningDate(startDateEdit->date()); m_account.setCurrencyId(m_currency->security().id()); if(m_qcheckboxPreferred->isChecked()) m_account.setValue("PreferredAccount", "Yes"); else m_account.deletePair("PreferredAccount"); if(m_qcheckboxNoVat->isChecked()) m_account.setValue("NoVat", "Yes"); else m_account.deletePair("NoVat"); if(m_minBalanceAbsoluteEdit->isVisible()) { m_account.setValue("minimumBalance", m_minBalanceAbsoluteEdit->value().toString()); } } else { if(KMyMoneyGlobalSettings::hideUnusedCategory() && !m_isEditing) { KMessageBox::information(this, i18n("You have selected to suppress the display of unused categories in the KMyMoney configuration dialog. The category you just created will therefore only be shown if it is used. Otherwise, it will be hidden in the accounts/categories view."), i18n("Hidden categories"), "NewHiddenCategory"); } } if ( m_qcheckboxTax->isChecked()) m_account.setValue("Tax", "Yes"); else m_account.deletePair("Tax"); m_account.deletePair("VatAccount"); m_account.deletePair("VatAmount"); m_account.deletePair("VatRate"); if(m_vatCategory->isChecked()) { m_account.setValue("VatRate", (m_vatRate->value().abs() / MyMoneyMoney(100,1)).toString()); } else { if(m_vatAssignment->isChecked()) { m_account.setValue("VatAccount", m_vatAccount->selectedItems().first()); if(m_netAmount->isChecked()) m_account.setValue("VatAmount", "Net"); } } accept(); }