Commit a85761da authored by Thomas Baumgart's avatar Thomas Baumgart
Browse files

Convert GnuCash account type to valid top-level account type

The GnuCash template may use any account type for top-level account
groups. KMyMoney only supports certain types. The current conversion did
not take that into account and some templates failed during loading into
KMyMoney.

This change adjusts the account type of the top-level accounts to those
supported by KMyMoney and makes the templates loadable.

CCBUG: 452863
(cherry picked from commit 182c2267)
parent 416a91c2
......@@ -238,10 +238,9 @@ bool MyMoneyAccount::operator == (const MyMoneyAccount& right) const
// clang-format on
}
Account::Type MyMoneyAccount::accountGroup() const
Account::Type MyMoneyAccount::accountGroup(Account::Type type)
{
Q_D(const MyMoneyAccount);
switch (d->m_accountType) {
switch (type) {
case Account::Type::Checkings:
case Account::Type::Savings:
case Account::Type::Cash:
......@@ -258,10 +257,16 @@ Account::Type MyMoneyAccount::accountGroup() const
return Account::Type::Liability;
default:
return d->m_accountType;
return type;
}
}
Account::Type MyMoneyAccount::accountGroup() const
{
Q_D(const MyMoneyAccount);
return accountGroup(d->m_accountType);
}
QString MyMoneyAccount::currencyId() const
{
Q_D(const MyMoneyAccount);
......
......@@ -147,6 +147,11 @@ public:
*/
eMyMoney::Account::Type accountGroup() const;
/**
* Return the account group for a given account @a type.
*/
static eMyMoney::Account::Type accountGroup(eMyMoney::Account::Type type);
/**
* This method returns the id of the MyMoneyInstitution object this account
* belongs to.
......
......@@ -63,8 +63,20 @@ DirNameMapType &getDirNameMap()
return dirNameMap;
}
int toKMyMoneyAccountType(const QString &type)
int toKMyMoneyAccountType(const QString& type, int index)
{
// in case index is 1 we process the level that represents the
// KMyMoney top-level accounts. They can only have distinct values
// and we have to make sure to return one of them by converting
// the Account::Type into the respective accountGroup value.
if (index == 1) {
const auto accountType = toKMyMoneyAccountType(type, 0);
if (accountType != (int)Account::Type::Unknown) {
return (int)MyMoneyAccount::accountGroup(static_cast<eMyMoney::Account::Type>(accountType));
}
return 99; // unknown
}
if(type == "ROOT") return (int)Account::Type::Unknown;
else if (type == "BANK") return (int)Account::Type::Checkings;
else if (type == "CASH") return (int)Account::Type::Cash;
......@@ -112,11 +124,11 @@ public:
void clear()
{
id = "";
m_type = "";
m_name = "";
code = "";
parent = "";
id.clear();
m_type.clear();
m_name.clear();
code.clear();
parent.clear();
slotList.clear();
}
......@@ -282,7 +294,7 @@ public:
if (account->m_type != "ROOT")
{
xml.writeStartElement("","account");
xml.writeAttribute("type", QString::number(toKMyMoneyAccountType(account->m_type)));
xml.writeAttribute("type", QString::number(toKMyMoneyAccountType(account->m_type, index)));
xml.writeAttribute("name", noLevel1Names && index < 2 ? "" : account->m_name);
if (withID)
xml.writeAttribute("id", account->id);
......@@ -373,7 +385,7 @@ public:
{
QString a;
a.fill(' ', index);
qDebug() << a << account->m_name << toKMyMoneyAccountType(account->m_type);
qDebug() << a << account->m_name << toKMyMoneyAccountType(account->m_type, index);
index++;
dumpTemplates(account->id, index);
index--;
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment