Пример #1
0
        public async Task <ActionResult> Add([FromBody] PaymentAddRequest request)
        {
            LoggedUser loggedUser = LoggedUser;

            try
            {
                await UnitOfWork.ExecuteTransactionAsync(async (transaction, timeout) =>
                {
                    Payment payment = new Payment
                    {
                        InboxId     = request.InboxId,
                        UserId      = loggedUser.UserId,
                        TotalAmount = request.Amount,
                        Note        = request.Note
                    };

                    UnitOfWork.PaymentRepository.Add(payment);
                    await UnitOfWork.SaveChangesAsync();

                    IEnumerable <Expense> expensesNotPaid = await UnitOfWork.ExpenseRepository.GetNotPaidExpensesByInboxIdAsync(request.InboxId);

                    decimal totalPayment = payment.TotalAmount;
                    foreach (Expense expense in expensesNotPaid)
                    {
                        if (totalPayment <= 0)
                        {
                            break;
                        }

                        decimal amountToPay = expense.TotalAmount - expense.CurrentAmount;
                        decimal paidAmount  = totalPayment >= amountToPay ? amountToPay : totalPayment;

                        totalPayment          -= paidAmount;
                        expense.CurrentAmount += paidAmount;

                        ExpensePayment expensePayment = new ExpensePayment
                        {
                            PaymentId = payment.Id,
                            ExpenseId = expense.Id,
                            Amount    = paidAmount
                        };

                        UnitOfWork.ExpenseRepository.Update(expense);
                        UnitOfWork.ExpensePaymentRepository.Add(expensePayment);
                    }

                    payment.CurrentAmount = payment.TotalAmount - totalPayment;
                    UnitOfWork.PaymentRepository.Update(payment);

                    await UnitOfWork.SaveChangesAsync();
                }, null, null);

                return(Ok());
            }
            catch (Exception ex)
            {
                return(InternalServerError(ex.InnerExceptionMessage()));
            }
        }
Пример #2
0
        public async Task <Response <PaymentResponse> > Add(PaymentAddRequest request)
        {
            var result = new PaymentModel
            {
                Type        = request.Type,
                IsActive    = request.IsActive,
                Image       = request.Image,
                Name        = request.Name,
                TotalPrice  = new Price(request.PriceWithoutVat),
                Description = request.Description,
            };

            result = await _uow.Payments.InsertAsync(result);

            return(new Response <PaymentResponse>(ResponseStatus.Ok, _mapService.MapPayment(result)));
        }
Пример #3
0
        public async Task <IHttpActionResult> Post([FromBody] PaymentAddRequest request)
        {
            var response = await _paymentService.Add(request);

            return(new CreateResult(response));
        }