Ejemplo n.º 1
0
        public async Task <bool> ValidatePrices(FullOrderModel order, DeliveryMethodsProvider deliveryMethodsProvider, PaymentMethodsProvider paymentMethodsProvider)
        {
            decimal priceAccuracy = 0.1m;
            Task <DeliveryMethodModel> getDeliveryMethodTask = deliveryMethodsProvider.GetDeliveryMethodById(order.DeliveryMethodId.Value);
            Task <PaymentMethodModel>  getPaymentMethodTask  = paymentMethodsProvider.GetPaymentMethodById(order.PaymentMethodId.Value);

            decimal itemsPrice = order.Items.Sum(i => i.Quantity.Value * i.Price.Value);

            if (Math.Abs(itemsPrice - order.Total.Order.Value) > priceAccuracy)
            {
                return(false);
            }

            DeliveryMethodModel deliveryMethod = await getDeliveryMethodTask;
            PaymentMethodModel  paymentMethod  = await getPaymentMethodTask;

            decimal shippingPrice = deliveryMethod.DeliveryPrice;

            if (paymentMethod.ApplyDeliveryTax)
            {
                shippingPrice += ((decimal)deliveryMethod.CODTax * order.Total.Order.Value) / 100m;
            }

            if (Math.Abs(shippingPrice - order.Total.Shipping.Value) > priceAccuracy)
            {
                return(false);
            }

            decimal orderNonDiscountedPrice = order.Items.Where(i => !i.HasDiscount.Value).Sum(i => i.Quantity.Value * i.Price.Value);
            decimal couponesDiscount        = 0m;

            if (order.Coupones != null)
            {
                foreach (CouponeModel coupone in order.Coupones)
                {
                    if (coupone.ValueType.Value == CouponeValueType.Percent)
                    {
                        couponesDiscount += (orderNonDiscountedPrice * coupone.Value.Value) / 100;
                    }
                    else if (coupone.ValueType.Value == CouponeValueType.Absolute)
                    {
                        couponesDiscount += coupone.Value.Value;
                    }
                }
            }

            if (Math.Abs(couponesDiscount - order.Total.CouponesDiscount.Value) > priceAccuracy)
            {
                return(false);
            }

            decimal totalPrice = shippingPrice + itemsPrice - couponesDiscount;

            if (Math.Abs(totalPrice - order.Total.Full.Value) > priceAccuracy)
            {
                return(false);
            }

            return(true);
        }
Ejemplo n.º 2
0
        public async Task <IHttpActionResult> GetDeliveryMethods(bool freeShipping)
        {
            IModelTracker <DeliveryMethodModel> modelTracker            = new DeliveryMethodsTrackerFactory().GetDeliveryMethodsTracker(freeShipping);
            DeliveryMethodsProvider             deliveryMethodsProvider = new DeliveryMethodsProvider(modelTracker);
            IEnumerable <DeliveryMethodModel>   allDeliveryMethods      = await deliveryMethodsProvider.GetAllDeliveryMethods();

            return(this.Ok(allDeliveryMethods));
        }
Ejemplo n.º 3
0
        public async Task <IHttpActionResult> ValidateAndSaveOrder(FullOrderModel order)
        {
            if (!this.ModelState.IsValid)
            {
                Logger.Current.LogWarning("Orders.ValidateAndSaveOrder: invalid model state");

                return(this.BadRequest(this.ModelState));
            }

            if (order.Items.Count() == 0)
            {
                Logger.Current.LogWarning("Orders.ValidateAndSaveOrder: no items");

                return(this.BadRequest("Empty order"));
            }

            OrderItemsGrouper        itemsGrouper    = new OrderItemsGrouper();
            IList <OrderedItemModel> normalizedItems = itemsGrouper.NormalizeOrderedItems(order.Items.ToList());

            if (normalizedItems == null)
            {
                Logger.Current.LogWarning("Orders.ValidateAndSaveOrder: grouping error");

                return(this.BadRequest("Different prices of the same item"));
            }

            OrderValidator orderValidator     = new OrderValidator();
            bool           areOrderItemsValid = await orderValidator.ValidateOrderItems(normalizedItems);

            if (!areOrderItemsValid)
            {
                Logger.Current.LogWarning("Orders.ValidateAndSaveOrder: invalid quantities");

                return(this.BadRequest("Invalid quantities"));
            }

            bool freeShipping = order.Coupones != null && order.Coupones.Any(c => c.FreeShipping.Value);

            IModelTracker <DeliveryMethodModel> deliveryMethodModelTracker = new DeliveryMethodsTrackerFactory().GetDeliveryMethodsTracker(freeShipping);
            IModelTracker <PaymentMethodModel>  paymentMethodModelTracker  = new PaymentMethodsTrackerFactory().GetPaymentMethodsTracker(freeShipping);
            bool isLoyal = deliveryMethodModelTracker is FreeShippingDeliveryMethodsTracker;
            DeliveryMethodsProvider deliveryMethodsProvider = new DeliveryMethodsProvider(deliveryMethodModelTracker);
            PaymentMethodsProvider  paymentMethodsProvider  = new PaymentMethodsProvider(paymentMethodModelTracker);

            if (order.Coupones != null)
            {
                bool areCouponesValid = await orderValidator.ValidateCoupones(this.User.Identity.GetUserId(), order);

                if (!areCouponesValid)
                {
                    Logger.Current.LogWarning("Orders.ValidateAndSaveOrder: invalid coupones");

                    return(this.BadRequest("Invalid coupones"));
                }
            }

            bool areOrderPricesValid = await orderValidator.ValidatePrices(order, deliveryMethodsProvider, paymentMethodsProvider);

            if (!areOrderPricesValid)
            {
                Logger.Current.LogWarning("Orders.ValidateAndSaveOrder: invalid prive");

                return(this.BadRequest("Invalid price"));
            }

            StocksProvider stocksProvider = new StocksProvider();
            bool           stockQuantitiesUpdatedSuccessfully = await stocksProvider.UpdateStocks(
                normalizedItems.Select(o => new StockChangeModel(o.ArticleId.Value, o.SizeId.Value, o.ColorId, -o.Quantity.Value)),
                Logger.Current);

            if (!stockQuantitiesUpdatedSuccessfully)
            {
                Logger.Current.LogWarning("Orders.ValidateAndSaveOrder: invalid quantities (update stocks)");

                return(this.BadRequest("Invalid quantities"));
            }


            Task <DeliveryMethodModel> getDeliveryMethodTask = deliveryMethodsProvider.GetDeliveryMethodById(order.DeliveryMethodId.Value);
            Task <PaymentMethodModel>  getPaymentMethodTask  = paymentMethodsProvider.GetPaymentMethodById(order.PaymentMethodId.Value);

            DeliveryMethodModel deliveryMethod = await getDeliveryMethodTask;
            PaymentMethodModel  paymentMethod  = await getPaymentMethodTask;

            Order newOrder = new Order();

            newOrder.UserId             = new Guid(this.User.Identity.GetUserId());
            newOrder.DeliveryMerchant   = deliveryMethod.Name;
            newOrder.DeliveryPrice      = deliveryMethod.DeliveryPrice;
            newOrder.PaymentMethodId    = paymentMethod.Id;
            newOrder.HasCommission      = paymentMethod.ApplyDeliveryTax;
            newOrder.CommissionPercents = deliveryMethod.CODTax;
            newOrder.ShippingPrice      = order.Total.Shipping.Value;
            newOrder.ItemsPrice         = order.Total.Order.Value;
            newOrder.TotalPrice         = order.Total.Full.Value;
            newOrder.StatusId           = 1;
            newOrder.DateCreated        = DateTime.UtcNow;

            List <OrderDetail> orderDetails = new List <OrderDetail>();

            foreach (OrderedItemModel orderedItem in normalizedItems)
            {
                OrderDetail orderDetail = new OrderDetail();
                orderDetail.ItemId    = orderedItem.ArticleId.Value;
                orderDetail.SizeId    = orderedItem.SizeId.Value;
                orderDetail.ColorId   = orderedItem.ColorId;
                orderDetail.Quantity  = orderedItem.Quantity.Value;
                orderDetail.ItemPrice = orderedItem.Price.Value;

                orderDetails.Add(orderDetail);
            }

            OrdersProvider ordersProvider = new OrdersProvider();
            int            newOrderId     = await ordersProvider.SaveOrder(newOrder, orderDetails);

            MailSendingFacade mailSender = new MailSendingFacade();

            mailSender.SendNewOrderMail(newOrderId);

            return(this.Ok(newOrderId));
        }