public void AdjustBalance(int accountId) { // get an account (can be a disabled account) var account = Get(accountId, true); var accountMovements = _movementRepository.GetByAccount(accountId); var newBalance = (account.InitialBalance + MovementService.TotalCredit(accountMovements.Where(m => m.Invoice == null))) - MovementService.TotalDebit(accountMovements.Where(m => m.Invoice == null)); account.Balance = newBalance; _repository.Update(account); }
public void Pay(int invoiceId, InvoicePaymentModel invoicePaymentModel) { var invoice = Get(invoiceId); if (invoice.InvoiceStatus.Equals(InvoiceStatus.Pending)) { var _movementService = new MovementService(); var paymentCategory = _categoryService.GetPaymentCategory(); var paymentSubcategories = _categoryService.GetSubcategories(paymentCategory.Id); invoice.PaymentDate = invoicePaymentModel.AccountingDate; invoice.InvoiceStatus = InvoiceStatus.Paid; var movement = new Movement { Type = "D", Description = $"Payment of credit card invoice #{invoiceId} - credit card: {invoice.CreditCard.Name}", AccountingDate = invoicePaymentModel.AccountingDate, AccountId = invoicePaymentModel.AccountId, Value = invoicePaymentModel.Value, MovementStatus = invoicePaymentModel.MovementStatus, AutomaticallyLaunch = invoicePaymentModel.AutomaticallyLaunch, Observation = invoicePaymentModel.Observation, CategoryId = paymentCategory.Id, SubcategoryId = paymentSubcategories.FirstOrDefault(s => s.Name.Equals("Credit card")).Id }; _repository.Update(invoice); _movementService.Insert(movement); _movementService.LaunchMovement(GetMovements(invoice.Id), false); } else { throw new Exceptions.InvalidOperationException("This invoice alright is paid or is not closed yet"); } }