private Account GetAccount(Database database, MmexDatabase mmexDatabase, int accountid)
        {
            if (accountid <= 0)
            {
                return(null);
            }

            var account = mmexDatabase.Accounts.Single(a => a.ACCOUNTID == accountid);

            return(database.Accounts.Single(a => a.Name == account.ACCOUNTNAME));
        }
        private MmexDatabase LoadMmxDatabase(IDbConnection connection)
        {
            var result = new MmexDatabase();

            result.Accounts      = connection.Query <ACCOUNTLIST>("SELECT * FROM ACCOUNTLIST_V1").ToList();
            result.Transactions  = connection.Query <CHECKINGACCOUNT>("SELECT * FROM CHECKINGACCOUNT_V1").ToList();
            result.Subcategories = connection.Query <SUBCATEGORY>("SELECT * FROM SUBCATEGORY_V1").ToList();
            result.Categories    = connection.Query <CATEGORY>("SELECT * FROM CATEGORY_V1").ToList();
            result.Currencies    = connection.Query <CURRENCYFORMATS>("SELECT * FROM CURRENCYFORMATS_V1").ToList();

            return(result);
        }
        private Category GetCategory(Database database, MmexDatabase mmexDatabase, int categid, int subcategid)
        {
            if (categid <= 0)
            {
                return(null);
            }

            var category = mmexDatabase.Categories.Single(c => c.CATEGID == categid);

            if (subcategid <= 0)
            {
                return(database.Categories.Single(c => c.Name == category.CATEGNAME && c.Parent == null));
            }

            var parent      = database.Categories.Single(c => c.Name == category.CATEGNAME && c.Parent == null);
            var subcategory = mmexDatabase.Subcategories.Single(c => c.SUBCATEGID == subcategid);

            return(database.Categories.Single(c => c.Name == subcategory.SUBCATEGNAME && c.Parent == parent));
        }
        private Transaction GetTransaction(Database database, MmexDatabase mmexDatabase, CHECKINGACCOUNT t)
        {
            if (t.TRANSCODE == "Deposit")
            {
                return(new Transaction
                {
                    Category = GetCategory(database, mmexDatabase, t.CATEGID, t.SUBCATEGID),
                    ToAccount = GetAccount(database, mmexDatabase, t.ACCOUNTID),
                    ToAmount = t.TRANSAMOUNT,
                    Notes = t.NOTES,
                    Date = t.TRANSDATE
                });
            }

            if (t.TRANSCODE == "Withdrawal")
            {
                return(new Transaction
                {
                    Category = GetCategory(database, mmexDatabase, t.CATEGID, t.SUBCATEGID),
                    FromAccount = GetAccount(database, mmexDatabase, t.ACCOUNTID),
                    FromAmount = t.TRANSAMOUNT,
                    Notes = t.NOTES,
                    Date = t.TRANSDATE
                });
            }

            if (t.TRANSCODE == "Transfer")
            {
                return(new Transaction
                {
                    Category = GetCategory(database, mmexDatabase, t.CATEGID, t.SUBCATEGID),
                    FromAccount = GetAccount(database, mmexDatabase, t.ACCOUNTID),
                    ToAccount = GetAccount(database, mmexDatabase, t.TOACCOUNTID),
                    FromAmount = t.TRANSAMOUNT,
                    ToAmount = t.TOTRANSAMOUNT,
                    Notes = t.NOTES,
                    Date = t.TRANSDATE
                });
            }

            throw new Exception($"Unknown TRANSCODE {t.TRANSCODE}");
        }