/// <summary> /// Returns the amount saved of a vault for a specific month /// </summary> private OVaultMonthlyInformation getMonthVaultAmount(int userId, int vaultId, int month, int year) { if (month <= 0 || month > 12) { throw new Exception("Invalid month"); } // Get all the transactions related to the vault from the beginning of the month to the end TransactionManager transactionManager = new TransactionManager(context); string formattedMonth = month < 10 ? $"0{month}" : month.ToString(); string from = $"01/{formattedMonth}/{year}"; string to = $"{DateTime.DaysInMonth(year, month)}/{formattedMonth}/{year}"; List <OTransaction> vaultTransactions = transactionManager.getAllByDates(userId, from, to, vaultId); // Calculate the total amount of the month double amount = 0; foreach (OTransaction transaction in vaultTransactions) { double?transAmount = VaultManager.transformAmount(transaction.amount, transaction.category.categoryType.id); if (transAmount.HasValue) { amount += transAmount.Value; } } OVaultMonthlyInformation vaultInfo = new OVaultMonthlyInformation(); vaultInfo.month = month; vaultInfo.amount = amount; return(vaultInfo); }
public OTransaction saveTransaction(OTransaction transaction) { // Validations if (transaction == null) { throw new Exception("ERROR: Transaction null"); } // Get model and modify properties Transaction model; Transaction modelBeforeEdited = null; if (transaction.id.HasValue) { model = getByIdEdition(transaction.id.Value); modelBeforeEdited = model; } else { model = new Transaction(); } model.tAmount = transaction.amount; model.tCategoryId = transaction.category.id.Value; model.tDate = DateTime.ParseExact(transaction.date, "d'/'M'/'yyyy", CultureInfo.InvariantCulture, DateTimeStyles.None); model.tDescription = transaction.description; model.tUserId = transaction.userId; model.tVaultId = transaction.vaultId; // Update or Add if is a new one if (transaction.id.HasValue) { context.Transactions.Update(model); } else { context.Transactions.Add(model); } context.SaveChanges(); context.Entry(model).Reference(t => t.CategoryNavigation) .Query() .Include(c => c.categoryTypeNavigation).Load(); // Update vault if necessary if (transaction.vaultId.HasValue) { VaultManager vaultManager = new VaultManager(context); vaultManager.updateVaultAmount(model, modelBeforeEdited); } return(convert(model)); }
public bool delete(int transactionId) { bool deleted = false; Transaction transaction = getByIdEdition(transactionId); int cagegoryTypeId = transaction.CategoryNavigation.categoryTypeNavigation.ctId.Value; if (cagegoryTypeId == (int)CategoryTypesId.VaultsIncomes || cagegoryTypeId == (int)CategoryTypesId.VaultsExpenses) { VaultManager vaultManager = new VaultManager(context); vaultManager.updateVaultAmount(transaction, null, true); } context.Transactions.Remove(transaction); context.SaveChanges(); deleted = true; return(deleted); }