Ejemplo n.º 1
0
        public async Task <IActionResult> Create(PurchasePostRequest model)
        {
            try
            {
                if (!ModelState.IsValid)
                {
                    throw new Exception("Petición de alta inválida");
                }

                return(Ok(await _purchaseService.CreateAsync(model, _apiDbContext, _configuration)));
            }
            catch (Exception e)
            {
                return(StatusCode(500, e.Message));
            }
        }
Ejemplo n.º 2
0
        public async Task <PurchaseDTO> CreateAsync(PurchasePostRequest model, ApiDbContext apiDbContext, IConfiguration configuration)
        {
            try
            {
                Purchase newPurchase = new Purchase
                {
                    StatusDate      = DateTime.Now,
                    CreationDate    = DateTime.Now,
                    Code            = DateTime.Now.ToString("ddMMyyyyHHmmss"),
                    PaymentMethodId = model.PaymentMethoId,
                    PurchaseTypeId  = model.PurchaseTypeId,
                    PaymentStatusId = model.PaymentStatusId
                };

                decimal amount = 0;

                // Todos los productos deben pertenecer a la misma tienda - Se crea un purchase por tienda en el carro
                model.ProductCartIdList.ForEach(id =>
                {
                    decimal am = apiDbContext.Users_Products_Cart.Where(pc => pc.Id == id).First().Product.Pvp;
                    amount    += am;

                    newPurchase.Purchase_Carts.Add(new Purchase_Cart
                    {
                        Purchase          = newPurchase,
                        UserProductCartId = id
                    });
                });

                newPurchase.Amount = amount;

                await apiDbContext.Purchases.AddAsync(newPurchase);

                await apiDbContext.SaveChangesAsync();

                if (newPurchase.PurchaseType.Code.ToUpper().Trim().Equals(configuration["OnlinePurchasTypeCode"].ToString().ToUpper().Trim()) && model.DeliveryData != null)
                {
                    Shop shop = apiDbContext.Users_Products_Cart.Where(pc => model.ProductCartIdList.Contains(pc.Id)).ToList().First().Product.Shop;

                    Delivery newDelivery = new Delivery
                    {
                        PurchaseId    = newPurchase.PurchaseTypeId,
                        DeliveryDate  = DateTime.Now.AddDays(5),
                        DeliveryTaxes = amount < shop.MinAmountTaxes ? shop.Taxes : 0,
                        Address       = model.DeliveryData.Address,
                        Number        = model.DeliveryData.Number,
                        City          = model.DeliveryData.City,
                        PostCode      = model.DeliveryData.PostCode,
                        Region        = model.DeliveryData.Region,
                        IsCompleted   = false,
                        CreationDate  = DateTime.Now
                    };

                    apiDbContext.Deliveries.Add(newDelivery);

                    await apiDbContext.SaveChangesAsync();
                }

                return(ModelToDTO(newPurchase));
            }
            catch (Exception e)
            {
                throw new Exception(e.Message);
            }
        }