示例#1
0
        public async Task <IActionResult> Update(long id, [FromBody] UpdateMovementReq req)
        {
            Console.WriteLine(req);
            var result = await movementRepo.Update(id, req);

            return(Ok(result));
        }
        /// <summary>
        /// Update an existing movement
        /// </summary>
        /// <param name="movement"></param>
        public async Task Update(Movement movement)
        {
            var oldMovement = await GetById(movement.Id);

            if (oldMovement.CanEdit)
            {
                movement.Increase = movement.Increase ?? 0;
                movement.Decrease = movement.Decrease ?? 0;
                movement.CanEdit  = oldMovement.CanEdit;

                var creditCardId = movement.InvoiceId;

                if (creditCardId.HasValue)
                {
                    var creditCard = await _creditCardService.GetById(creditCardId.Value);

                    _creditCardService.CanBeUsed(creditCard, movement);

                    try
                    {
                        var invoiceId = (await _creditCardService.GetInvoiceByAccountingDate(creditCard.Id, movement.AccountingDate)).Id;
                        movement.InvoiceId = invoiceId;
                    }
                    catch (InvoiceNotFoundException)
                    {
                        var createdInvoice = _invoiceService.CreateInvoiceObject(creditCard, movement.AccountingDate);
                        _invoiceService.Insert(createdInvoice);
                        movement.InvoiceId = _creditCardService.GetInvoiceByAccountingDate(creditCard.Id, movement.AccountingDate).Id;
                    }

                    movement.MovementStatus = MovementStatus.Pending;
                }

                await _repository.Update(movement);

                if (!oldMovement.Account.Id.Equals(movement.AccountId))
                {
                    await _accountService.AdjustBalance(oldMovement.Account.Id);
                }
                await _accountService.AdjustBalance(movement.AccountId);
            }
            else
            {
                throw new NotValidOperationException("This movement was generated by another routine and cannot be changed");
            }
        }
        public void Update(Movement movement)
        {
            var oldMovement = Get(movement.Id);

            if (oldMovement.CanEdit)
            {
                movement.Increase      = movement.Increase ?? 0;
                movement.Decrease      = movement.Decrease ?? 0;
                movement.InclusionDate = oldMovement.InclusionDate;
                movement.CanEdit       = oldMovement.CanEdit;

                if (IsCreditCardMovement(movement))
                {
                    if (movement.Type.Equals("D"))
                    {
                        RelateToCreditCard(movement);
                    }
                    else
                    {
                        throw new Exceptions.InvalidOperationException("Credit cards can be used only with expenses");
                    }
                }

                _repository.Update(movement);

                if (!movement.MovementStatus.Equals(oldMovement.MovementStatus))
                {
                    _accountService.AdjustBalance(movement.AccountId);
                }

                if (!oldMovement.AccountId.Equals(movement.AccountId))
                {
                    _accountService.AdjustBalance(oldMovement.AccountId);
                }
            }
            else
            {
                throw new Exceptions.InvalidOperationException("This movement was generated by another routine and can not be changed");
            }
        }