Changeset View
Changeset View
Standalone View
Standalone View
kmymoney/converter/mymoneyqifreader.cpp
Show First 20 Lines • Show All 897 Lines • ▼ Show 20 Line(s) | 882 | { | |||
---|---|---|---|---|---|
898 | } | 898 | } | ||
899 | 899 | | |||
900 | // check if we can find the account already in the file | 900 | // check if we can find the account already in the file | ||
901 | MyMoneyAccount acc = kmymoney->findAccount(account, MyMoneyAccount()); | 901 | MyMoneyAccount acc = kmymoney->findAccount(account, MyMoneyAccount()); | ||
902 | 902 | | |||
903 | // if not, we just create it | 903 | // if not, we just create it | ||
904 | if (acc.id().isEmpty()) { | 904 | if (acc.id().isEmpty()) { | ||
905 | MyMoneyAccount brokerage; | 905 | MyMoneyAccount brokerage; | ||
906 | MyMoneyMoney balance; | 906 | file->createAccount(account, parentAccount, brokerage, MyMoneyMoney()); | ||
907 | kmymoney->createAccount(account, parentAccount, brokerage, balance); | | |||
908 | } | 907 | } | ||
909 | } | 908 | } | ||
910 | 909 | | |||
911 | const QString MyMoneyQifReader::transferAccount(const QString& name, bool useBrokerage) | 910 | const QString MyMoneyQifReader::transferAccount(const QString& name, bool useBrokerage) | ||
912 | { | 911 | { | ||
913 | QString accountId; | 912 | QString accountId; | ||
914 | QStringList tmpEntry = m_qifEntry; // keep temp copies | 913 | QStringList tmpEntry = m_qifEntry; // keep temp copies | ||
915 | MyMoneyAccount tmpAccount = m_account; | 914 | MyMoneyAccount tmpAccount = m_account; | ||
▲ Show 20 Lines • Show All 260 Lines • ▼ Show 20 Line(s) | 1174 | } else { | |||
1176 | /* pos = tmp.findRev("--"); | 1175 | /* pos = tmp.findRev("--"); | ||
1177 | if(pos != -1) { | 1176 | if(pos != -1) { | ||
1178 | t.setValue("Dialog", tmp.mid(pos+2)); | 1177 | t.setValue("Dialog", tmp.mid(pos+2)); | ||
1179 | tmp = tmp.left(pos); | 1178 | tmp = tmp.left(pos); | ||
1180 | }*/ | 1179 | }*/ | ||
1181 | 1180 | | |||
1182 | // it's an expense / income | 1181 | // it's an expense / income | ||
1183 | tmp = tmp.trimmed(); | 1182 | tmp = tmp.trimmed(); | ||
1184 | accountId = checkCategory(tmp, s1.m_amount, s2.m_amount); | 1183 | accountId = file->checkCategory(tmp, s1.m_amount, s2.m_amount); | ||
1185 | } | 1184 | } | ||
1186 | 1185 | | |||
1187 | if (!accountId.isEmpty()) { | 1186 | if (!accountId.isEmpty()) { | ||
1188 | try { | 1187 | try { | ||
1189 | MyMoneyAccount account = file->account(accountId); | 1188 | MyMoneyAccount account = file->account(accountId); | ||
1190 | // FIXME: check that the type matches and ask if not | 1189 | // FIXME: check that the type matches and ask if not | ||
1191 | 1190 | | |||
1192 | if (account.accountType() == MyMoneyAccount::Investment) { | 1191 | if (account.accountType() == MyMoneyAccount::Investment) { | ||
Show All 29 Lines | 1220 | if (d->isTransfer(tmp, m_qifProfile.accountDelimiter().left(1), m_qifProfile.accountDelimiter().mid(1, 1))) { | |||
1222 | accountId = transferAccount(tmp, false); | 1221 | accountId = transferAccount(tmp, false); | ||
1223 | 1222 | | |||
1224 | } else { | 1223 | } else { | ||
1225 | pos = tmp.lastIndexOf("--"); | 1224 | pos = tmp.lastIndexOf("--"); | ||
1226 | if (pos != -1) { | 1225 | if (pos != -1) { | ||
1227 | tmp = tmp.left(pos); | 1226 | tmp = tmp.left(pos); | ||
1228 | } | 1227 | } | ||
1229 | tmp = tmp.trimmed(); | 1228 | tmp = tmp.trimmed(); | ||
1230 | accountId = checkCategory(tmp, s1.m_amount, s2.m_amount); | 1229 | accountId = file->checkCategory(tmp, s1.m_amount, s2.m_amount); | ||
1231 | } | 1230 | } | ||
1232 | 1231 | | |||
1233 | if (!accountId.isEmpty()) { | 1232 | if (!accountId.isEmpty()) { | ||
1234 | try { | 1233 | try { | ||
1235 | MyMoneyAccount account = file->account(accountId); | 1234 | MyMoneyAccount account = file->account(accountId); | ||
1236 | // FIXME: check that the type matches and ask if not | 1235 | // FIXME: check that the type matches and ask if not | ||
1237 | 1236 | | |||
1238 | if (account.accountType() == MyMoneyAccount::Investment) { | 1237 | if (account.accountType() == MyMoneyAccount::Investment) { | ||
▲ Show 20 Lines • Show All 648 Lines • ▼ Show 20 Line(s) | 1880 | if (result.isEmpty()) { | |||
1887 | file->addAccount(acc, expense); | 1886 | file->addAccount(acc, expense); | ||
1888 | ft.commit(); | 1887 | ft.commit(); | ||
1889 | result = acc.id(); | 1888 | result = acc.id(); | ||
1890 | } | 1889 | } | ||
1891 | 1890 | | |||
1892 | return result; | 1891 | return result; | ||
1893 | } | 1892 | } | ||
1894 | 1893 | | |||
1895 | const QString MyMoneyQifReader::checkCategory(const QString& name, const MyMoneyMoney& value, const MyMoneyMoney& value2) | | |||
1896 | { | | |||
1897 | QString accountId; | | |||
1898 | MyMoneyFile *file = MyMoneyFile::instance(); | | |||
1899 | MyMoneyAccount account; | | |||
1900 | bool found = true; | | |||
1901 | | ||||
1902 | if (!name.isEmpty()) { | | |||
1903 | // The category might be constructed with an arbitraty depth (number of | | |||
1904 | // colon delimited fields). We try to find a parent account within this | | |||
1905 | // hierarchy by searching the following sequence: | | |||
1906 | // | | |||
1907 | // aaaa:bbbb:cccc:ddddd | | |||
1908 | // | | |||
1909 | // 1. search aaaa:bbbb:cccc:dddd, create nothing | | |||
1910 | // 2. search aaaa:bbbb:cccc , create dddd | | |||
1911 | // 3. search aaaa:bbbb , create cccc:dddd | | |||
1912 | // 4. search aaaa , create bbbb:cccc:dddd | | |||
1913 | // 5. don't search , create aaaa:bbbb:cccc:dddd | | |||
1914 | | ||||
1915 | account.setName(name); | | |||
1916 | QString accName; // part to be created (right side in above list) | | |||
1917 | QString parent(name); // a possible parent part (left side in above list) | | |||
1918 | do { | | |||
1919 | accountId = file->categoryToAccount(parent); | | |||
1920 | if (accountId.isEmpty()) { | | |||
1921 | found = false; | | |||
1922 | // prepare next step | | |||
1923 | if (!accName.isEmpty()) | | |||
1924 | accName.prepend(':'); | | |||
1925 | accName.prepend(parent.section(':', -1)); | | |||
1926 | account.setName(accName); | | |||
1927 | parent = parent.section(':', 0, -2); | | |||
1928 | } else if (!accName.isEmpty()) { | | |||
1929 | account.setParentAccountId(accountId); | | |||
1930 | } | | |||
1931 | } while (!parent.isEmpty() && accountId.isEmpty()); | | |||
1932 | | ||||
1933 | // if we did not find the category, we create it | | |||
1934 | if (!found) { | | |||
1935 | MyMoneyAccount parent; | | |||
1936 | if (account.parentAccountId().isEmpty()) { | | |||
1937 | if (!value.isNegative() && value2.isNegative()) | | |||
1938 | parent = file->income(); | | |||
1939 | else | | |||
1940 | parent = file->expense(); | | |||
1941 | } else { | | |||
1942 | parent = file->account(account.parentAccountId()); | | |||
1943 | } | | |||
1944 | account.setAccountType((!value.isNegative() && value2.isNegative()) ? MyMoneyAccount::Income : MyMoneyAccount::Expense); | | |||
1945 | MyMoneyAccount brokerage; | | |||
1946 | // clear out the parent id, because createAccount() does not like that | | |||
1947 | account.setParentAccountId(QString()); | | |||
1948 | kmymoney->createAccount(account, parent, brokerage, MyMoneyMoney()); | | |||
1949 | accountId = account.id(); | | |||
1950 | } | | |||
1951 | } | | |||
1952 | | ||||
1953 | return accountId; | | |||
1954 | } | | |||
1955 | | ||||
1956 | const QString MyMoneyQifReader::processAccountEntry(bool resetAccountId) | 1894 | const QString MyMoneyQifReader::processAccountEntry(bool resetAccountId) | ||
1957 | { | 1895 | { | ||
1958 | MyMoneyFile* file = MyMoneyFile::instance(); | 1896 | MyMoneyFile* file = MyMoneyFile::instance(); | ||
1959 | 1897 | | |||
1960 | MyMoneyAccount account; | 1898 | MyMoneyAccount account; | ||
1961 | QString tmp; | 1899 | QString tmp; | ||
1962 | 1900 | | |||
1963 | account.setName(extractLine('N')); | 1901 | account.setName(extractLine('N')); | ||
Show All 40 Lines | 1941 | if (acc.id().isEmpty()) { | |||
2004 | // in case the account is not found by name and the type is | 1942 | // in case the account is not found by name and the type is | ||
2005 | // unknown, we have to assume something and create a checking account. | 1943 | // unknown, we have to assume something and create a checking account. | ||
2006 | // this might be wrong, but we have no choice at this point. | 1944 | // this might be wrong, but we have no choice at this point. | ||
2007 | if (account.accountType() == MyMoneyAccount::UnknownAccountType) | 1945 | if (account.accountType() == MyMoneyAccount::UnknownAccountType) | ||
2008 | account.setAccountType(MyMoneyAccount::Checkings); | 1946 | account.setAccountType(MyMoneyAccount::Checkings); | ||
2009 | 1947 | | |||
2010 | MyMoneyAccount parentAccount; | 1948 | MyMoneyAccount parentAccount; | ||
2011 | MyMoneyAccount brokerage; | 1949 | MyMoneyAccount brokerage; | ||
2012 | MyMoneyMoney balance; | | |||
2013 | // in case it's a stock account, we need to setup a fix investment account | 1950 | // in case it's a stock account, we need to setup a fix investment account | ||
2014 | if (account.isInvest()) { | 1951 | if (account.isInvest()) { | ||
2015 | acc.setName(i18n("%1 (Investment)", account.name())); // use the same name for the investment account | 1952 | acc.setName(i18n("%1 (Investment)", account.name())); // use the same name for the investment account | ||
2016 | acc.setDescription(i18n("Autogenerated by QIF importer from type Mutual account entry")); | 1953 | acc.setDescription(i18n("Autogenerated by QIF importer from type Mutual account entry")); | ||
2017 | acc.setAccountType(MyMoneyAccount::Investment); | 1954 | acc.setAccountType(MyMoneyAccount::Investment); | ||
2018 | parentAccount = file->asset(); | 1955 | parentAccount = file->asset(); | ||
2019 | kmymoney->createAccount(acc, parentAccount, brokerage, MyMoneyMoney()); | 1956 | file->createAccount(acc, parentAccount, brokerage, MyMoneyMoney()); | ||
2020 | parentAccount = acc; | 1957 | parentAccount = acc; | ||
2021 | qDebug("We still need to create the stock account in MyMoneyQifReader::processAccountEntry()"); | 1958 | qDebug("We still need to create the stock account in MyMoneyQifReader::processAccountEntry()"); | ||
2022 | } else { | 1959 | } else { | ||
2023 | // setup parent according the type of the account | 1960 | // setup parent according the type of the account | ||
2024 | switch (account.accountGroup()) { | 1961 | switch (account.accountGroup()) { | ||
2025 | case MyMoneyAccount::Asset: | 1962 | case MyMoneyAccount::Asset: | ||
2026 | default: | 1963 | default: | ||
2027 | parentAccount = file->asset(); | 1964 | parentAccount = file->asset(); | ||
Show All 10 Lines | |||||
2038 | // investment accounts will receive a brokerage account, as KMyMoney | 1975 | // investment accounts will receive a brokerage account, as KMyMoney | ||
2039 | // currently does not allow to store funds in the investment account directly | 1976 | // currently does not allow to store funds in the investment account directly | ||
2040 | // but only create it (not here, but later) if it is needed | 1977 | // but only create it (not here, but later) if it is needed | ||
2041 | if (account.accountType() == MyMoneyAccount::Investment) { | 1978 | if (account.accountType() == MyMoneyAccount::Investment) { | ||
2042 | brokerage.setName(QString()); // brokerage name empty so account not created yet | 1979 | brokerage.setName(QString()); // brokerage name empty so account not created yet | ||
2043 | brokerage.setAccountType(MyMoneyAccount::Checkings); | 1980 | brokerage.setAccountType(MyMoneyAccount::Checkings); | ||
2044 | brokerage.setCurrencyId(MyMoneyFile::instance()->baseCurrency().id()); | 1981 | brokerage.setCurrencyId(MyMoneyFile::instance()->baseCurrency().id()); | ||
2045 | } | 1982 | } | ||
2046 | kmymoney->createAccount(account, parentAccount, brokerage, balance); | 1983 | file->createAccount(account, parentAccount, brokerage, MyMoneyMoney()); | ||
2047 | acc = account; | 1984 | acc = account; | ||
2048 | // qDebug("Account created"); | 1985 | // qDebug("Account created"); | ||
2049 | } else { | 1986 | } else { | ||
2050 | // qDebug("Existing account found"); | 1987 | // qDebug("Existing account found"); | ||
2051 | } | 1988 | } | ||
2052 | 1989 | | |||
2053 | if (resetAccountId) { | 1990 | if (resetAccountId) { | ||
2054 | // possibly start a new statement | 1991 | // possibly start a new statement | ||
▲ Show 20 Lines • Show All 246 Lines • Show Last 20 Lines |