示例#1
0
        public OrderViewModel GetOrderViewModel(int orderId)
        {
            OrderViewModel result = new OrderViewModel();
            Order order = db.Orders.FirstOrDefault(m => m.OrderId == orderId);
            if (order != null)
            {
                //Get Basic Info
                result.Order = order;

                // Check the order is enough material or not
                result.IsEnoughMaterial = true;
                //Get Material info
                if (order.OrderStatus == 0)
                {
                    List<MaterialViewModel> materialViewModelList = GetMaterialListForOrder(orderId);
                    if (result.Order.OrderStatus == 0)
                    {
                        foreach (MaterialViewModel materialViewModel in materialViewModelList)
                        {
                            if (!materialViewModel.IsEnough)
                            {
                                result.IsEnoughMaterial = false;
                            }
                        }
                    }

                    result.MaterialList = materialViewModelList;

                    List<InputMaterialViewModel> inputMaterialViewModelListTemp = GetInputMaterialList(result.MaterialList);
                    // Get Material Cost
                    if (inputMaterialViewModelListTemp.Count != 0)
                    {
                        result.MaterialCost = GetMaterialCost(inputMaterialViewModelListTemp);
                    }
                }


                // Get Customer or Guest info
                if (order.CustomerUserId != null)
                {
                    Customer customer = db.Customers.SingleOrDefault(m => m.UserId == order.CustomerUserId);
                    if (customer != null)
                    {
                        result.IsGuest = false;
                        result.OrderPersonName = customer.User.Fullname;
                        result.OrderPersonAddress = customer.CustomerAddress;
                        result.OrderPersonPhoneNumber = customer.CustomerPhoneNumber;
                        result.OrderPersonId = customer.CustomerId;
                        result.OrderPersonTaxCode = customer.TaxCode;
                        result.IsLoyal = customer.IsLoyal;
                    }
                }
                else
                {
                    GuestInfo guestInfo = db.GuestInfoes.FirstOrDefault(m => m.GuestInfoId == order.GuestInfoId);
                    if (guestInfo != null)
                    {
                        result.IsGuest = true;
                        result.OrderPersonName = guestInfo.GuestInfoName;
                        result.OrderPersonAddress = guestInfo.GuestInfoAddress;
                        result.OrderPersonPhoneNumber = guestInfo.GuestInfoPhone;
                        result.OrderPersonId = guestInfo.GuestInfoId;
                        result.OrderPersonTaxCode = null;
                        result.IsLoyal = false;
                    }
                }
            }
            return result;
        }
示例#2
0
        /// <summary>
        /// Make OrderViewModel
        /// </summary>
        /// <param name="cartList">List of product</param>
        /// <param name="customerId">The ID of the customer in DB</param>
        /// <param name="customerInput">The new customer info</param>
        /// <returns></returns>
        public OrderViewModel MakeOrderViewModel(List<CartViewModel> cartList, int? customerId, CustomerViewModel customerInput)
        {
            OrderViewModel result = new OrderViewModel();
            // Create new order
            Order order = new Order();
            if (customerId != null && customerInput == null)
            {
                order.CustomerUserId = customerId;
                //Add info for OrderView
                //Customer info
                Customer customer = db.Customers.FirstOrDefault(m => m.UserId == customerId && m.IsActive);
                if (customer != null)
                {
                    result.OrderPersonName = customer.User.Fullname;
                    result.OrderPersonAddress = customer.CustomerAddress;
                    result.OrderPersonPhoneNumber = customer.CustomerPhoneNumber;
                    result.OrderPersonTaxCode = customer.TaxCode;
                    result.IsGuest = false;
                    result.IsLoyal = customer.IsLoyal;
                }
            }
            else if (customerId == null && customerInput != null)
            {
                order.CustomerUserId = 0;
                //Add new customer info for OrderView
                //Customer info

                result.OrderPersonName = customerInput.CustomerName;
                result.OrderPersonAddress = customerInput.CustomerAddress;
                result.OrderPersonPhoneNumber = customerInput.CustomerPhoneNumber;
                result.OrderPersonTaxCode = customerInput.CustomerTaxCode;
                result.IsGuest = true;
                result.IsLoyal = false;
            }

            // Temp var for total amount
            int totalAmount = 0;
            // Taemp var for total quantity
            int totalQuantity = 0;
            // Creat OrderItemViewModel List and MaterialViewModelList
            List<OrderItem> orderItemList = new List<OrderItem>();
            List<MaterialViewModel> materialViewModelList = new List<MaterialViewModel>();

            foreach (CartViewModel cartViewModel in cartList)
            {
                if (cartViewModel.Quantity != 0)
                {
                    //Create OrderItem and add to list
                    OrderItem orderItem = new OrderItem();
                    Product product = db.Products.FirstOrDefault(m => m.ProductId == cartViewModel.ProductId);
                    orderItem.Product = product;
                    orderItem.Quantity = cartViewModel.Quantity;
                    orderItem.RealPrice = cartViewModel.RealPrice;
                    orderItem.Amount = cartViewModel.Quantity * cartViewModel.RealPrice;
                    orderItemList.Add(orderItem);

                    totalAmount += cartViewModel.Quantity * cartViewModel.RealPrice;
                    totalQuantity += cartViewModel.Quantity;

                    //Create MaterialViewModel and add to list
                    // Get Product in DB
                    if (product != null)
                    {
                        foreach (Recipe recipe in product.Recipes)
                        {
                            if (materialViewModelList.Count == 0)
                            {
                                MaterialViewModel materialViewModel = new MaterialViewModel();
                                // Get each product material and multiply with quantity
                                materialViewModel.ProductMaterialId = recipe.ProductMaterialId;
                                materialViewModel.ProductMaterialName = recipe.ProductMaterial.ProductMaterialName;
                                materialViewModel.NeedQuantity =
                                    (int)Math.Floor(recipe.RecipeQuantity * cartViewModel.Quantity);
                                materialViewModel.StorageQuantity = recipe.ProductMaterial.CurrentQuantity;
                                // Add into MaterialViewModelList
                                materialViewModelList.Add(materialViewModel);
                            }
                            else
                            {
                                bool check = true;
                                foreach (MaterialViewModel materialViewModel in materialViewModelList)
                                {
                                    if (materialViewModel.ProductMaterialId == recipe.ProductMaterialId)
                                    {
                                        materialViewModel.NeedQuantity +=
                                            (int)Math.Floor(recipe.RecipeQuantity * cartViewModel.Quantity);
                                        check = false;
                                    }
                                }
                                if (check)
                                {
                                    MaterialViewModel materialViewModel = new MaterialViewModel();
                                    // Get each product material and multiply with quantity
                                    materialViewModel.ProductMaterialId = recipe.ProductMaterialId;
                                    materialViewModel.ProductMaterialName = recipe.ProductMaterial.ProductMaterialName;
                                    materialViewModel.NeedQuantity =
                                        (int)Math.Floor(recipe.RecipeQuantity * cartViewModel.Quantity);
                                    materialViewModel.StorageQuantity = recipe.ProductMaterial.CurrentQuantity;
                                    // Add into MaterialViewModelList
                                    materialViewModelList.Add(materialViewModel);
                                }
                            }

                        }
                    }
                }
            }

            // Complete attribute in order
            //Check enough material for order
            result.IsEnoughMaterial = true;
            foreach (MaterialViewModel materialViewModel in materialViewModelList)
            {
                if (materialViewModel.NeedQuantity > materialViewModel.StorageQuantity)
                {
                    materialViewModel.IsEnough = false;
                    result.IsEnoughMaterial = false;
                }
                else
                {
                    materialViewModel.IsEnough = true;
                }
            }


            int discountAmount = 0;
            DiscountByQuantity discountByQuantity =
                db.DiscountByQuantities.FirstOrDefault(
                    m => m.QuantityFrom <= totalQuantity && m.QuantityTo >= totalQuantity);

            if (discountByQuantity != null)
            {
                discountAmount = totalAmount * discountByQuantity.DiscountValue / 100;
            }

            int totalTax = 0;
            TaxRate taxRate =
                db.TaxRates.FirstOrDefault(
                    m =>
                        m.TaxType.Abbreviation.Equals("GTGT") && m.BeginDate <= DateTime.Now &&
                        m.EndDate >= DateTime.Now);
            if (taxRate != null)
            {
                totalTax = (totalAmount - discountAmount) * taxRate.TaxRateValue / 100;
            }

            order.Amount = totalAmount;
            order.DiscountAmount = discountAmount;
            order.TaxAmount = totalTax;
            order.OrderItems = orderItemList;
            result.Order = order;

            result.MaterialList = materialViewModelList;
            // Calculate the Material cost
            List<InputMaterialViewModel> inputMaterialList = GetInputMaterialList(materialViewModelList);
            result.MaterialCost = GetMaterialCost(inputMaterialList);
            return result;
        }
示例#3
0
        /// <summary>
        /// Get sorted order view model list
        /// </summary>
        /// <returns></returns>
        #region Get sorted order view model list
        public List<OrderViewModel> GetSortedOrderViewModelList(DateTime? fromDate, DateTime? toDate, string customerName, int? orderStatus)
        {
            List<Order> orderList;
            if (fromDate != null && toDate != null)
            {
                DateTime toDateTemp = toDate.Value.AddDays(1);
                if (orderStatus != null && orderStatus != -1)
                {

                    orderList =
                        db.Orders.Where(
                            m =>
                                !m.IsStaffEdit && m.CreateTime >= fromDate.Value && m.CreateTime < toDateTemp &&
                                m.OrderStatus == orderStatus.Value).ToList();
                }
                else
                {
                    orderList = db.Orders.Where(
                        m =>
                            !m.IsStaffEdit && m.CreateTime >= fromDate.Value && m.CreateTime <= toDateTemp).ToList();
                }
            }
            else if (fromDate == null && toDate == null && orderStatus != null && orderStatus != -1)
            {
                orderList = db.Orders.Where(m => !m.IsStaffEdit && m.OrderStatus == orderStatus.Value).ToList();
            }
            else
            {
                orderList = db.Orders.Where(m => !m.IsStaffEdit).ToList();
            }
            List<Order> resultList;
            if (customerName != null && !customerName.Trim().IsEmpty())
            {
                resultList = new List<Order>();
                foreach (Order order in orderList)
                {
                    if (order.GuestInfo != null)
                    {
                        if (order.GuestInfo.GuestInfoName.Contains(customerName))
                        {
                            resultList.Add(order);
                        }
                    }
                    else
                    {
                        if (order.User.Fullname.Contains(customerName))
                        {
                            resultList.Add(order);
                        }
                    }
                }
            }
            else
            {
                resultList = orderList;
            }

            // Custom sort
            resultList.Sort(
                delegate(Order o1, Order o2)
                {
                    if (o1.OrderStatus != o2.OrderStatus)
                    {
                        return o1.OrderStatus.CompareTo(o2.OrderStatus);
                    }
                    return o1.CreateTime.CompareTo(o2.CreateTime);

                });
            List<OrderViewModel> orderViewModelList = new List<OrderViewModel>();
            foreach (Order order in resultList)
            {
                OrderViewModel orderViewModel = new OrderViewModel { Order = order };
                if (order.GuestInfo != null)
                {
                    orderViewModel.OrderPersonName = order.GuestInfo.GuestInfoName;
                }
                else
                {
                    orderViewModel.OrderPersonName = order.User.Fullname;
                }
                if (order.OrderStatus == 0)
                {
                    orderViewModel.IsEnoughMaterial = IsEnoughMaterialForOrder(order);
                }
                orderViewModelList.Add(orderViewModel);
            }
            return orderViewModelList;
        }