예제 #1
0
 private OrderDetail MapOrderDetail(OrderDetailAPIViewModel src)
 {
     return(src.ToEntity());
 }
        public static PromotionRule checkPromotionRule(OrderAPIViewModel order, int quantity, OrderDetailAPIViewModel od)
        {
            #region call service
            IVoucherService         voucherService         = DependencyUtils.Resolve <IVoucherService>();
            IPromotionService       promotionService       = DependencyUtils.Resolve <IPromotionService>();
            IPromotionDetailService promotionDetailService = DependencyUtils.Resolve <IPromotionDetailService>();
            IProductService         productService         = DependencyUtils.Resolve <IProductService>();
            #endregion

            PromotionRule result = new PromotionRule
            {
                rule           = 0,
                discountAmount = 0
            };
            #region Voucher
            var voucher = voucherService.GetVoucherIsNotUsedAndCode(order.VoucherCode);
            if (voucher == null)
            {
                return(result);
            }
            #endregion


            //var voucher = voucherApi
            //TODO after have voucher
            int checkCountProduct = 0;//dùng check trường hợp gửi 2 đơn hàng giống nhau nhưng mỗi cái có 1 quantity

            //get date to get promotion is experied ??
            #region Promotion
            var      promotion = promotionService.GetPromotionByDateAndId(voucher.PromotionID);
            DateTime now       = DateTime.Now;

            if (promotion == null)
            {
                return(result);
            }
            else if (!(promotion.ApplyFromTime <= now.Hour && now.Hour <= promotion.ApplyToTime))
            {
                return(result);
            }

            #endregion
            #region PromtionDetail rule 1 min , max order???
            // loop to get total amount, final amount to get check in promotiondetail
            double finalAmount = 0;

            foreach (var item in order.OrderDetails)
            {
                finalAmount += (productService.GetProductById(item.ProductID).Price *item.Quantity);
            }

            var promotionDetail = promotionDetailService.GetDetailByCode(promotion.PromotionCode).FirstOrDefault();
            if (promotionDetail == null)
            {
                return(result);
            }
            //check promotion detail is have min, max order != null ???
            if (promotionDetail.MinOrderAmount != null || promotionDetail.MaxOrderAmount != null)
            {
                if (promotionDetail.MinOrderAmount != null && finalAmount < promotionDetail.MinOrderAmount)
                {
                    throw ApiException.Get(false, ConstantManager.MES_CREATE_ORDER_EXCEED_VOUCHER_MIN_MAX, ResultEnum.VoucherMin, HttpStatusCode.BadRequest);
                }
                else if (promotionDetail.MaxOrderAmount != null && finalAmount > promotionDetail.MaxOrderAmount)
                {
                    throw  ApiException.Get(false, ConstantManager.MES_CREATE_ORDER_EXCEED_VOUCHER_MIN_MAX, ResultEnum.VoucherMax, HttpStatusCode.BadRequest);
                }
                else
                {
                    try
                    {
                        double discountAmount = 0;//amount return
                        if (promotionDetail.DiscountRate != null && promotionDetail.DiscountRate > 0)
                        {
                            //nhân với phần trăm giảm giá và trả về số tiền lun
                            discountAmount = (productService.GetProductById(od.ProductID).Price *promotionDetail.DiscountRate.Value) / 100;
                        }
                        else if (promotionDetail.DiscountAmount != null && promotionDetail.DiscountAmount > 0)
                        {
                            //nếu giảm giá theo tiền mặt
                            discountAmount = Convert.ToDouble(promotionDetail.DiscountAmount.Value);
                        }
                        result.rule           = Models.ConstantManager.PROMOTION_RULE_1;
                        result.discountAmount = discountAmount;
                        return(result);
                    }
                    catch
                    {
                        result.rule           = 0;
                        result.discountAmount = 0;
                        return(result);
                    }
                }
            }
            #endregion

            #region rule 2 buy min, max quantity of each product
            else if (promotionDetail.BuyProductCode != null)
            {
                double discountAmount = 0;
                //check product code is in order ????
                bool checkProductCode = false;
                //list product discount
                List <ProductDiscount> listProductDiscount = new List <ProductDiscount>();
                // var pmDetail = promotionDetailApi.GetDetailByCode(promotion.PromotionCode);
                int     pDetailId         = voucher.PromotionDetailID == null ? 0 : voucher.PromotionDetailID.Value;
                var     pmDetail          = promotionDetailService.GetDetailById(pDetailId);
                decimal tmpDiscountAmount = 0;
                double  tmpDiscountRate   = 0;
                string  mesMinBuyProduct  = "";
                bool    checkCount        = true;//check quanitty min order buy
                var     tmpProductOrder   = productService.GetProductById(od.ProductID);

                bool checkCountProductQuantity = true; //false => đơn hàng gửi lên giống nhau nhưng ko đủ quantity min order,
                                                       //true => đơn hàng gửi lên giống nhau nhưng đủ quantity
                foreach (var item in order.OrderDetails)
                {
                    if (tmpProductOrder.Code == pmDetail.BuyProductCode)
                    {
                        checkCountProduct += item.Quantity;//cộng đồn quantity trong order
                        if (checkCountProduct < pmDetail.MinBuyQuantity)
                        {
                            checkCountProductQuantity = false;//false => đơn hàng gửi lên giống nhau nhưng ko đủ quantity min order,
                        }
                        else
                        {
                            checkCountProductQuantity = true;
                        }
                    }
                }
                if (pmDetail != null)
                {
                    if (tmpProductOrder.Code == pmDetail.BuyProductCode)
                    {
                        quantity         += od.Quantity;
                        tmpDiscountAmount = pmDetail.DiscountAmount == null ? 0 : pmDetail.DiscountAmount.Value * od.Quantity;
                        tmpDiscountRate   = pmDetail.DiscountRate == null ? 0 : pmDetail.DiscountRate.Value;
                        checkProductCode  = true;
                        if (quantity < pmDetail.MinBuyQuantity)
                        {
                            mesMinBuyProduct = pmDetail.MinBuyQuantity + " " + tmpProductOrder.ProductName;
                            checkCount       = false;
                        }
                    }
                    else
                    {
                        // checkCount = true;
                    }
                }

                try
                {
                    //if true => get list product discount
                    if (checkProductCode)
                    {
                        //check amount discount and rate, return value
                        if (tmpDiscountAmount > 0)
                        {
                            discountAmount = System.Convert.ToDouble(tmpDiscountAmount);
                        }
                        else if (tmpDiscountRate > 0)
                        {
                            discountAmount = (tmpProductOrder.Price * tmpDiscountRate * od.Quantity) / 100;
                        }
                    }
                    checkProductCode = false;
                    //return list product with discount amount, rate
                }
                catch
                {
                    result.rule           = 0;
                    result.discountAmount = 0;
                    result.quantity       = 0;
                    result.countProduct   = false;
                    return(result);
                }


                if (!checkCount && !checkCountProductQuantity)
                {
                    result.rule           = 0;
                    result.discountAmount = 0;
                    result.quantity       = quantity;
                    result.countProduct   = checkCountProductQuantity;
                    return(result);
                }
                result.rule           = ConstantManager.PROMOTION_RULE_2;
                result.discountAmount = discountAmount;
                result.quantity       = quantity;
                result.countProduct   = checkCountProductQuantity;
                return(result);
            }
            result.rule           = 0;
            result.discountAmount = 0;
            result.quantity       = 0;
            return(result);

            #endregion
        }
        public void CalculateOrderPrice(OrderAPIViewModel order, DateTime time)
        {
            #region OrderDetail

            #region Service and variable
            IProductService productService         = DependencyUtils.Resolve <IProductService>();
            double          orderDetailTotalAmount = 0;
            double          orderDetailFinalAmount = 0;
            //double discountOrderDetail = 0;
            //biến giảm giá trên mỗi sản phẩm
            double discountEachProduct = 0;
            //biến giảm giá trên toàn hóa đơn
            double discount         = 0;
            bool   checkDeliveryFee = false;
            double finalAmount      = 0;
            double totalAmount      = 0;
            double deliveryFee      = 0;
            //add order detail have product is a delivery fee
            //giảm giá trên từng sản phẩm, hóa đơn tùy theo rule ở hàm checkPromotionRUle
            int quantity = 0;//check quantity trong order gửi 1 đơn hàng và có quantity trong đó
            #endregion

            foreach (var item in order.OrderDetails)
            {
                if (item.ParentId == 0)
                {
                    item.ParentId = null;
                }

                if (item.Quantity <= 0)
                {
                    throw ApiException.Get(false, ConstantManager.MES_CREATE_ORDER_NEGATIVE_QUANTITY, ResultEnum.OrderDetailQuantity, HttpStatusCode.BadRequest);
                }
                var product = productService.GetProductById(item.ProductID);
                if (product == null)
                {
                    throw ApiException.Get(false, ConstantManager.MES_CREATE_ORDER_NOT_FOUND_PRODUCT, ResultEnum.ProductNotFound, HttpStatusCode.NotFound);
                }
                item.ProductType = product.ProductType;
                item.TotalAmount = product.Price * item.Quantity;

                orderDetailTotalAmount += item.TotalAmount;
                item.UnitPrice          = product.Price;
                //lấy giảm giá theo rule
                PromotionRule rule = new PromotionRule();
                rule = PromotionRuleUtility.checkPromotionRule(order, quantity, item);
                //check rule 1, 2 dc định nghĩ ở ConstantManager
                if (rule.rule == ConstantManager.PROMOTION_RULE_2 || rule.countProduct)
                {
                    item.Discount = rule.discountAmount;
                }
                else if (rule.rule == ConstantManager.PROMOTION_RULE_1)
                {
                    discount = rule.discountAmount;
                }

                quantity                = rule.quantity;
                item.FinalAmount        = item.TotalAmount - item.Discount;
                orderDetailFinalAmount += item.FinalAmount;
                discountEachProduct    += item.Discount;
                //discountOrderDetail += item.Discount;
                item.OrderDate = time;
            }
            foreach (var item in order.OrderDetails)
            {
                if (item.ProductType != (int)ProductTypeEnum.ServiceFee)
                {
                    totalAmount  = productService.GetProductById(item.ProductID).Price *item.Quantity;
                    finalAmount += totalAmount - item.Discount;
                    if (finalAmount >= ConstantManager.DELIVERY_FREE || order.OrderType != (int)OrderTypeEnum.MobileDelivery)
                    {
                        checkDeliveryFee = true;
                    }
                }
            }

            if (!checkDeliveryFee)
            {
                var tmpOrderDetail  = order.OrderDetails.ToList();
                var productDelivery = productService.GetProductDeliveryFee();
                OrderDetailAPIViewModel deliveryOrderDt = new OrderDetailAPIViewModel();
                deliveryOrderDt.ProductID   = productDelivery.ProductID;
                deliveryOrderDt.Quantity    = 1;
                deliveryOrderDt.TotalAmount = productDelivery.Price;
                deliveryOrderDt.FinalAmount = productDelivery.Price;
                deliveryOrderDt.UnitPrice   = productDelivery.Price;
                deliveryFee = productDelivery.Price;

                deliveryOrderDt.ProductOrderType = (int)Models.ProductOrderType.Single;
                deliveryOrderDt.OrderDate        = time;
                deliveryOrderDt.ProductType      = (int)ProductTypeEnum.ServiceFee;
                tmpOrderDetail.Add(deliveryOrderDt);
                order.OrderDetails = tmpOrderDetail;
            }
            #endregion
            #region Order
            order.CheckInDate = time;
            var vatAmount = 0; //VAT 10%
            #region edit promotion

            #endregion

            order.TotalAmount         = orderDetailTotalAmount;
            order.Discount            = discount;
            order.DiscountOrderDetail = discountEachProduct;
            order.FinalAmount         = orderDetailTotalAmount + deliveryFee - vatAmount - discount - discountEachProduct;//l?y order detail sum l?i => ra du?c order thi?t c?a passio
            //order.DiscountOrderDetail = discountOrderDetail;
            //gán giản giá trên từng sản phẩm cho order
            #endregion
        }