public async Task <ActionResult <ProductInvoiceViewModel> > PutProductInvoice(int id,
                                                                                      ProductInvoiceEditModel productInvoiceModel)
        {
            ProductInvoice productInvoice = await _productInvoicesRepository.FindByIdAsync(id);

            if (productInvoice == null)
            {
                return(BadRequest($"No existe ninguna factura de productos con el código {id}."));
            }

            _mapper.Map(productInvoiceModel, productInvoice);
            _productInvoicesRepository.Update(productInvoice);

            try
            {
                await _unitWork.SaveAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!ProductInvoiceExists(id))
                {
                    return(NotFound(
                               $"Error de actialización. No existe ninguna factura de productos con el código {id}."));
                }

                throw;
            }

            return(_mapper.Map <ProductInvoiceViewModel>(productInvoice));
        }
Example #2
0
        public async Task <ActionResult <ProductInvoiceViewModel> > PayProductInvoice(int id,
                                                                                      [FromBody] PaymentModel paymentModel)
        {
            ProductInvoice productInvoice = await _productInvoicesRepository.FindByIdAsync(id);

            if (productInvoice is null)
            {
                return(NotFound($"No existe ninguna factura de producto con el código {id}"));
            }

            productInvoice.CalculateTotal();

            PaymentCreateRequest paymentCreateRequest = new PaymentCreateRequest
            {
                Token             = paymentModel.Token,
                PaymentMethodId   = paymentModel.PaymentMethodId,
                TransactionAmount = productInvoice.Total,
                Description       = $"Pay for product invoice {id}",
                Installments      = 1,
                Payer             = new PaymentPayerRequest
                {
                    FirstName = productInvoice.Client.FirstName,
                    LastName  = productInvoice.Client.LastName,
                    Email     = paymentModel.Email
                }
            };

            Payment payment = await _paymentClient.CreateAsync(paymentCreateRequest);

            if (payment.Status == PaymentStatus.Rejected)
            {
                return(BadRequest("El pago no pudo ser procesado."));
            }

            productInvoice.PublishEvent(new PaidInvoice(productInvoice));
            productInvoice.State         = InvoiceState.Paid;
            productInvoice.PaymentDate   = DateTime.Now;
            productInvoice.PaymentMethod = PaymentMethod.CreditCard;

            _productInvoicesRepository.Update(productInvoice);

            try
            {
                await _unitWork.SaveAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!ProductInvoiceExists(id))
                {
                    return(NotFound(
                               $"Error de actualizacón. No existe ninguna factura de producto con el código {id}."));
                }

                throw;
            }

            return(_mapper.Map <ProductInvoiceViewModel>(productInvoice));
        }
Example #3
0
        private async Task FindAndReportExpiredProductInvoices(CancellationToken cancellationToken)
        {
            IEnumerable <ProductInvoice> productInvoices = await _productInvoicesRepository.GetPendingExpiredProductInvoices();

            foreach (ProductInvoice productInvoice in productInvoices)
            {
                productInvoice.State = InvoiceState.Expired;
                _productInvoicesRepository.Update(productInvoice);
            }

            await _unitWork.SaveAsync();

            await _invoiceHub.Clients.Group("Administrator").SendAsync("OverdueProductBills", productInvoices, cancellationToken);
        }
        public async Task Update_Existent_ProductInvoice()
        {
            ProductInvoice productInvoice = await _productInvoicesRepository.FindByIdAsync(1);

            Assert.IsNotNull(productInvoice);

            productInvoice.State         = InvoiceState.Paid;
            productInvoice.PaymentMethod = PaymentMethod.Cash;

            _productInvoicesRepository.Update(productInvoice);
            await _dbContext.SaveChangesAsync();

            productInvoice = await _productInvoicesRepository.FindByIdAsync(1);

            Assert.AreEqual(InvoiceState.Paid, productInvoice.State);
            Assert.AreEqual(PaymentMethod.Cash, productInvoice.PaymentMethod);
        }