public void Insert(Movement movement) { movement.Increase = movement.Increase ?? 0; movement.Decrease = movement.Decrease ?? 0; movement.InclusionDate = DateTime.Now; movement.CanEdit = true; if (IsCreditCardMovement(movement)) { if (movement.Type.Equals("D")) { RelateToCreditCard(movement); } else { throw new Exceptions.InvalidOperationException("Credit cards can be used only with expenses"); } } _repository.Insert(movement); if (IsLaunched(movement)) { _accountService.AdjustBalance(movement.AccountId); } }
/// <summary> /// Add a new movement /// </summary> /// <param name="movement"></param> public async Task Add(Movement movement) { movement.Increase = movement.Increase ?? 0; movement.Decrease = movement.Decrease ?? 0; movement.InclusionDate = DateTime.Now; movement.CanEdit = true; var creditCardId = movement.InvoiceId; if (creditCardId.HasValue) { var creditCard = await _creditCardService.GetById(creditCardId.Value); _creditCardService.CanBeUsed(creditCard, movement); try { movement.InvoiceId = (await _creditCardService.GetInvoiceByAccountingDate(creditCard.Id, movement.AccountingDate)).Id; } catch (InvoiceNotFoundException) { var createdInvoice = _invoiceService.CreateInvoiceObject(creditCard, movement.AccountingDate); _invoiceService.Insert(createdInvoice); movement.InvoiceId = (await _creditCardService.GetInvoiceByAccountingDate(creditCard.Id, movement.AccountingDate)).Id; } movement.MovementStatus = MovementStatus.Pending; } await _repository.Insert(movement); if (movement.MovementStatus.Equals(MovementStatus.Launched) && !movement.InvoiceId.HasValue) { await _accountService.AdjustBalance(movement.AccountId); } }