private void CalculateBringForword(DateTime month, int budgetId) { var date1 = DateTime.UtcNow; var monthCount = ((date1.Year - month.Year) * 12) + date1.Month - month.Month; for (int i = 0; i < monthCount; i++) { var currentMonth = month.AddMonths(i); var bringForwordThisMonth = bringForwardDac.Get(currentMonth, budgetId); if (bringForwordThisMonth == null) { bringForwordThisMonth = new BringForward { Month = currentMonth, BudgetId = budgetId, }; } var bringForwordNextMonth = bringForwardDac.Get(currentMonth.AddMonths(1), budgetId); if (bringForwordNextMonth == null) { bringForwordNextMonth = new BringForward { Month = currentMonth.AddMonths(1), BudgetId = budgetId, }; } var transactions = transactionDac.Get(currentMonth, budgetId); var sumAmount = bringForwordThisMonth.Amount + transactions.Sum(x => x.Amount); bringForwordNextMonth.Amount = sumAmount; bringForwardDac.Upsert(bringForwordNextMonth); } }
public void Update(BringForward data) { using (MySqlConnection conn = context.GetConnection()) { conn.Open(); MySqlCommand cmd = new MySqlCommand("UPDATE `BringForward` SET `Amount`=@Amount,`Month`=@Month,`BudgetId`=@BudgetId WHERE `Id`=@Id", conn); cmd.Parameters.AddWithValue("@Amount", data.Amount); cmd.Parameters.AddWithValue("@Month", data.Month); cmd.Parameters.AddWithValue("@BudgetId", data.BudgetId); cmd.Parameters.AddWithValue("@Id", data.Id); cmd.ExecuteNonQuery(); } }
public int Insert(BringForward data) { using (MySqlConnection conn = context.GetConnection()) { conn.Open(); MySqlCommand cmd = new MySqlCommand("INSERT INTO `BringForward` VALUES (0,@Amount,@Month,@BudgetId,@CreatedDate)", conn); cmd.Parameters.AddWithValue("@Amount", data.Amount); cmd.Parameters.AddWithValue("@Month", data.Month); cmd.Parameters.AddWithValue("@BudgetId", data.BudgetId); cmd.Parameters.AddWithValue("@CreatedDate", DateTime.UtcNow); cmd.ExecuteNonQuery(); return((int)cmd.LastInsertedId); } }