Changeset View
Changeset View
Standalone View
Standalone View
kmymoney/mymoney/mymoneyfile.cpp
Show First 20 Lines • Show All 899 Lines • ▼ Show 20 Line(s) | 882 | { | |||
---|---|---|---|---|---|
900 | 900 | | |||
901 | d->m_storage->removeInstitution(institution); | 901 | d->m_storage->removeInstitution(institution); | ||
902 | 902 | | |||
903 | d->m_changeSet += MyMoneyNotification(notifyRemove, institution); | 903 | d->m_changeSet += MyMoneyNotification(notifyRemove, institution); | ||
904 | 904 | | |||
905 | d->addCacheNotification(institution.id(), false); | 905 | d->addCacheNotification(institution.id(), false); | ||
906 | } | 906 | } | ||
907 | 907 | | |||
908 | void MyMoneyFile::createAccount(MyMoneyAccount& newAccount, MyMoneyAccount& parentAccount, MyMoneyAccount& brokerageAccount, MyMoneyMoney openingBal) | ||||
909 | { | ||||
910 | // make sure we have a currency. If none is assigned, we assume base currency | ||||
911 | if (newAccount.currencyId().isEmpty()) | ||||
912 | newAccount.setCurrencyId(baseCurrency().id()); | ||||
913 | | ||||
914 | MyMoneyFileTransaction ft; | ||||
915 | try { | ||||
916 | int pos; | ||||
917 | // check for ':' in the name and use it as separator for a hierarchy | ||||
918 | while ((pos = newAccount.name().indexOf(MyMoneyFile::AccountSeperator)) != -1) { | ||||
919 | QString part = newAccount.name().left(pos); | ||||
920 | QString remainder = newAccount.name().mid(pos + 1); | ||||
921 | const MyMoneyAccount& existingAccount = subAccountByName(parentAccount, part); | ||||
922 | if (existingAccount.id().isEmpty()) { | ||||
923 | newAccount.setName(part); | ||||
924 | | ||||
925 | addAccount(newAccount, parentAccount); | ||||
926 | parentAccount = newAccount; | ||||
927 | } else { | ||||
928 | parentAccount = existingAccount; | ||||
929 | } | ||||
930 | newAccount.setParentAccountId(QString()); // make sure, there's no parent | ||||
931 | newAccount.clearId(); // and no id set for adding | ||||
932 | newAccount.removeAccountIds(); // and no sub-account ids | ||||
933 | newAccount.setName(remainder); | ||||
934 | } | ||||
935 | | ||||
936 | addAccount(newAccount, parentAccount); | ||||
937 | | ||||
938 | // in case of a loan account, we add the initial payment | ||||
939 | if ((newAccount.accountType() == MyMoneyAccount::Loan | ||||
tbaumgart: Code from MyMoneyFile downwards to the storage should not have UI relevant code. Please extract… | |||||
940 | || newAccount.accountType() == MyMoneyAccount::AssetLoan) | ||||
941 | && !newAccount.value("kmm-loan-payment-acc").isEmpty() | ||||
942 | && !newAccount.value("kmm-loan-payment-date").isEmpty()) { | ||||
943 | MyMoneyAccountLoan acc(newAccount); | ||||
944 | MyMoneyTransaction t; | ||||
945 | MyMoneySplit a, b; | ||||
946 | a.setAccountId(acc.id()); | ||||
947 | b.setAccountId(acc.value("kmm-loan-payment-acc").toLatin1()); | ||||
948 | a.setValue(acc.loanAmount()); | ||||
949 | if (acc.accountType() == MyMoneyAccount::Loan) | ||||
950 | a.setValue(-a.value()); | ||||
951 | | ||||
952 | a.setShares(a.value()); | ||||
953 | b.setValue(-a.value()); | ||||
954 | b.setShares(b.value()); | ||||
955 | a.setMemo(i18n("Loan payout")); | ||||
956 | b.setMemo(i18n("Loan payout")); | ||||
957 | t.setPostDate(QDate::fromString(acc.value("kmm-loan-payment-date"), Qt::ISODate)); | ||||
958 | newAccount.deletePair("kmm-loan-payment-acc"); | ||||
959 | newAccount.deletePair("kmm-loan-payment-date"); | ||||
960 | MyMoneyFile::instance()->modifyAccount(newAccount); | ||||
961 | | ||||
962 | t.addSplit(a); | ||||
963 | t.addSplit(b); | ||||
964 | addTransaction(t); | ||||
965 | createOpeningBalanceTransaction(newAccount, openingBal); | ||||
966 | | ||||
967 | // in case of an investment account we check if we should create | ||||
968 | // a brokerage account | ||||
969 | } else if (newAccount.accountType() == MyMoneyAccount::Investment | ||||
970 | && !brokerageAccount.name().isEmpty()) { | ||||
971 | addAccount(brokerageAccount, parentAccount); | ||||
972 | | ||||
973 | // set a link from the investment account to the brokerage account | ||||
974 | modifyAccount(newAccount); | ||||
975 | createOpeningBalanceTransaction(brokerageAccount, openingBal); | ||||
976 | | ||||
977 | } else | ||||
978 | createOpeningBalanceTransaction(newAccount, openingBal); | ||||
979 | | ||||
980 | ft.commit(); | ||||
981 | } catch (const MyMoneyException &e) { | ||||
982 | qWarning("Unable to create account: %s", qPrintable(e.what())); | ||||
983 | throw MYMONEYEXCEPTION(e.what()); | ||||
984 | } | ||||
985 | } | ||||
986 | | ||||
908 | void MyMoneyFile::addAccount(MyMoneyAccount& account, MyMoneyAccount& parent) | 987 | void MyMoneyFile::addAccount(MyMoneyAccount& account, MyMoneyAccount& parent) | ||
909 | { | 988 | { | ||
910 | d->checkTransaction(Q_FUNC_INFO); | 989 | d->checkTransaction(Q_FUNC_INFO); | ||
911 | 990 | | |||
912 | MyMoneyInstitution institution; | 991 | MyMoneyInstitution institution; | ||
913 | 992 | | |||
914 | // perform some checks to see that the account stuff is OK. For | 993 | // perform some checks to see that the account stuff is OK. For | ||
915 | // now we assume that the account must have a name, has no | 994 | // now we assume that the account must have a name, has no | ||
▲ Show 20 Lines • Show All 1638 Lines • ▼ Show 20 Line(s) | 2608 | for (it = subAccounts.begin(); it != subAccounts.end(); ++it) { | |||
2554 | } | 2633 | } | ||
2555 | 2634 | | |||
2556 | parent = categoryAccount; | 2635 | parent = categoryAccount; | ||
2557 | } | 2636 | } | ||
2558 | 2637 | | |||
2559 | return categoryToAccount(name); | 2638 | return categoryToAccount(name); | ||
2560 | } | 2639 | } | ||
2561 | 2640 | | |||
2641 | QString MyMoneyFile::checkCategory(const QString& name, const MyMoneyMoney& value, const MyMoneyMoney& value2) | ||||
2642 | { | ||||
2643 | QString accountId; | ||||
2644 | MyMoneyAccount newAccount; | ||||
2645 | bool found = true; | ||||
2646 | | ||||
2647 | if (!name.isEmpty()) { | ||||
2648 | // The category might be constructed with an arbitraty depth (number of | ||||
2649 | // colon delimited fields). We try to find a parent account within this | ||||
2650 | // hierarchy by searching the following sequence: | ||||
2651 | // | ||||
2652 | // aaaa:bbbb:cccc:ddddd | ||||
2653 | // | ||||
2654 | // 1. search aaaa:bbbb:cccc:dddd, create nothing | ||||
2655 | // 2. search aaaa:bbbb:cccc , create dddd | ||||
2656 | // 3. search aaaa:bbbb , create cccc:dddd | ||||
2657 | // 4. search aaaa , create bbbb:cccc:dddd | ||||
2658 | // 5. don't search , create aaaa:bbbb:cccc:dddd | ||||
2659 | | ||||
2660 | newAccount.setName(name); | ||||
2661 | QString accName; // part to be created (right side in above list) | ||||
2662 | QString parent(name); // a possible parent part (left side in above list) | ||||
2663 | do { | ||||
2664 | accountId = categoryToAccount(parent); | ||||
2665 | if (accountId.isEmpty()) { | ||||
2666 | found = false; | ||||
2667 | // prepare next step | ||||
2668 | if (!accName.isEmpty()) | ||||
2669 | accName.prepend(':'); | ||||
2670 | accName.prepend(parent.section(':', -1)); | ||||
2671 | newAccount.setName(accName); | ||||
2672 | parent = parent.section(':', 0, -2); | ||||
2673 | } else if (!accName.isEmpty()) { | ||||
2674 | newAccount.setParentAccountId(accountId); | ||||
2675 | } | ||||
2676 | } while (!parent.isEmpty() && accountId.isEmpty()); | ||||
2677 | | ||||
2678 | // if we did not find the category, we create it | ||||
2679 | if (!found) { | ||||
2680 | MyMoneyAccount parent; | ||||
2681 | if (newAccount.parentAccountId().isEmpty()) { | ||||
2682 | if (!value.isNegative() && value2.isNegative()) | ||||
2683 | parent = income(); | ||||
2684 | else | ||||
2685 | parent = expense(); | ||||
2686 | } else { | ||||
2687 | parent = account(newAccount.parentAccountId()); | ||||
2688 | } | ||||
2689 | newAccount.setAccountType((!value.isNegative() && value2.isNegative()) ? MyMoneyAccount::Income : MyMoneyAccount::Expense); | ||||
2690 | MyMoneyAccount brokerage; | ||||
2691 | // clear out the parent id, because createAccount() does not like that | ||||
2692 | newAccount.setParentAccountId(QString()); | ||||
2693 | createAccount(newAccount, parent, brokerage, MyMoneyMoney()); | ||||
2694 | accountId = newAccount.id(); | ||||
2695 | } | ||||
2696 | } | ||||
2697 | | ||||
2698 | return accountId; | ||||
2699 | } | ||||
2700 | | ||||
2562 | const QList<MyMoneySchedule> MyMoneyFile::scheduleListEx(int scheduleTypes, | 2701 | const QList<MyMoneySchedule> MyMoneyFile::scheduleListEx(int scheduleTypes, | ||
2563 | int scheduleOcurrences, | 2702 | int scheduleOcurrences, | ||
2564 | int schedulePaymentTypes, | 2703 | int schedulePaymentTypes, | ||
2565 | QDate startDate, | 2704 | QDate startDate, | ||
2566 | const QStringList& accounts) const | 2705 | const QStringList& accounts) const | ||
2567 | { | 2706 | { | ||
2568 | d->checkStorage(); | 2707 | d->checkStorage(); | ||
2569 | 2708 | | |||
▲ Show 20 Lines • Show All 971 Lines • Show Last 20 Lines |
Code from MyMoneyFile downwards to the storage should not have UI relevant code. Please extract it here and provide external method to do that or check and modify in the calling method.