// create (generate, load, calculate) RegularsVM for the current global regular incomes/expenses public RegularsVM GetRegulars() { try { using (SQLite.Net.SQLiteConnection conn = new SQLite.Net.SQLiteConnection(new SQLite.Net.Platform.WinRT.SQLitePlatformWinRT(), App.DB_PATH)) { // 1) request SQL table data // RegularItem = model var regulars = conn.Table <RegularItem>().OrderBy(o => o.Amount); // of type TableQuery<T>, BaseTableQuery, IEnumerable<T>, IEnumerable // 2) process each record float totalIncome = 0; float totalExpense = 0; RegularsVM regularsVM = new RegularsVM(); foreach (RegularItem r in regulars) { int amount = r.Amount; if (amount > 0) { // positive = income RegularItemVM regularItemVM = new RegularItemVM(r.Id, r.Name, amount, r.Period); totalIncome += regularItemVM.GetDaily(); regularsVM.AddIncome(regularItemVM); } else { // negative = expense amount = -amount; // use absolute value RegularItemVM regularItemVM = new RegularItemVM(r.Id, r.Name, amount, r.Period); totalExpense += regularItemVM.GetDaily(); regularsVM.AddExpense(regularItemVM); } } // 3) fill total values regularsVM.SetTotalIncomeAndExpense((int)Math.Truncate(totalIncome * 100.0f), (int)Math.Truncate(totalExpense * 100.0f)); // 4) return output VM return(regularsVM); } } catch { return(null); } }
public RegularStorage() { // calculate income per day using (SQLite.Net.SQLiteConnection conn = new SQLite.Net.SQLiteConnection(new SQLite.Net.Platform.WinRT.SQLitePlatformWinRT(), App.DB_PATH)) { // 1) request SQL table data // RegularItem = model var regulars = conn.Table <RegularItem>().OrderBy(o => o.Amount); // of type TableQuery<T>, BaseTableQuery, IEnumerable<T>, IEnumerable // 2) process each record float totalIncome = 0; float totalExpense = 0; RegularsVM regularsVM = new RegularsVM(); foreach (RegularItem r in regulars) { int amount = r.Amount; if (amount > 0) { // positive = income RegularItemVM regularItemVM = new RegularItemVM(r.Id, r.Name, amount, r.Period); totalIncome += regularItemVM.GetDaily(); } else { // negative = expense amount = -amount; // use absolute value RegularItemVM regularItemVM = new RegularItemVM(r.Id, r.Name, amount, r.Period); totalExpense += regularItemVM.GetDaily(); } } RegularIncomePerDay = (int)Math.Truncate(totalIncome * 100.0f) - (int)Math.Truncate(totalExpense * 100.0f); } }