Esempio n. 1
0
        public MaterialInfoForProductListViewModel GetMaterialListForOrder(List<CartViewModel> cartList)
        {
            MaterialInfoForProductListViewModel result = new MaterialInfoForProductListViewModel();
            List<MaterialViewModel> materialViewModelList = new List<MaterialViewModel>();
            if (cartList != null && cartList.Count > 0)
            {

                foreach (CartViewModel cartViewModel in cartList)
                {
                    Product product = db.Products.FirstOrDefault(m => m.ProductId == cartViewModel.ProductId && m.IsActive);
                    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);
                                }
                            }
                        }
                    }

                }

                // Check enough material
                bool isEnoughMaterial = true;
                foreach (MaterialViewModel materialViewModel in materialViewModelList)
                {
                    if (materialViewModel.NeedQuantity > materialViewModel.StorageQuantity)
                    {
                        materialViewModel.IsEnough = false;
                        isEnoughMaterial = false;
                    }
                    else
                    {
                        materialViewModel.IsEnough = true;
                    }
                }
                result.MaterialList = materialViewModelList;
                result.IsEnough = isEnoughMaterial;
                // Get material cost
                int materialCost = 0;
                foreach (MaterialViewModel materialViewModel in materialViewModelList)
                {
                    int needQuantity = materialViewModel.NeedQuantity;
                    List<InputMaterial> inputMaterialList = db.InputMaterials.Where(
                        m => m.ProductMaterialId == materialViewModel.ProductMaterialId && m.IsActive && m.RemainQuantity > 0).OrderBy(m => m.ImportDate).ToList();
                    foreach (InputMaterial inputMaterial in inputMaterialList)
                    {
                        if (needQuantity > 0)
                        {
                            if (inputMaterial.RemainQuantity >= needQuantity)
                            {
                                materialCost += (int)Math.Floor(inputMaterial.InputMaterialPrice * needQuantity);
                                needQuantity = 0;
                            }
                            else
                            {
                                materialCost +=
                                    (int)Math.Floor(inputMaterial.InputMaterialPrice * inputMaterial.RemainQuantity);
                                needQuantity -= inputMaterial.RemainQuantity;
                            }
                        }
                    }
                }
                result.MaterialCost = materialCost;
            }

            return result;
        }
Esempio n. 2
0
        public List<InputMaterialViewModel> GetInputMaterialList(List<MaterialViewModel> inputList)
        {
            List<MaterialViewModel> materialList = new List<MaterialViewModel>();
            foreach (MaterialViewModel materialViewModelInput in inputList)
            {
                MaterialViewModel materialViewModel = new MaterialViewModel
                {
                    IsEnough = materialViewModelInput.IsEnough,
                    NeedQuantity = materialViewModelInput.NeedQuantity,
                    ProductMaterialId = materialViewModelInput.ProductMaterialId,
                    ProductMaterialName = materialViewModelInput.ProductMaterialName,
                    StorageQuantity = materialViewModelInput.StorageQuantity,
                    Unit = materialViewModelInput.Unit
                };
                materialList.Add(materialViewModel);
            }

            List<InputMaterialViewModel> resultList = new List<InputMaterialViewModel>();
            if (materialList.Count != 0)
            {
                foreach (MaterialViewModel materialViewModel in materialList)
                {

                    List<InputMaterial> tempList = db.InputMaterials.Where(
                        m => m.ProductMaterialId == materialViewModel.ProductMaterialId && m.IsActive && m.RemainQuantity > 0).OrderByDescending(m => m.InputMaterialExpiryDate).ToList();
                    foreach (InputMaterial inputMaterial in tempList)
                    {

                        if (materialViewModel.NeedQuantity > 0 && inputMaterial.RemainQuantity >= materialViewModel.NeedQuantity)
                        {
                            InputMaterialViewModel inputMaterialViewModel = new InputMaterialViewModel();
                            inputMaterialViewModel.InputMaterialId = inputMaterial.InputMaterialId;
                            inputMaterialViewModel.Quantity = materialViewModel.NeedQuantity;
                            inputMaterialViewModel.Price = inputMaterial.InputMaterialPrice;
                            materialViewModel.NeedQuantity = 0;
                            resultList.Add(inputMaterialViewModel);
                        }
                        else if ((materialViewModel.NeedQuantity > 0 && inputMaterial.RemainQuantity < materialViewModel.NeedQuantity))
                        {
                            InputMaterialViewModel inputMaterialViewModel = new InputMaterialViewModel();
                            inputMaterialViewModel.InputMaterialId = inputMaterial.InputMaterialId;
                            inputMaterialViewModel.Price = inputMaterial.InputMaterialPrice;
                            inputMaterialViewModel.Quantity = inputMaterial.RemainQuantity;
                            materialViewModel.NeedQuantity -= inputMaterial.RemainQuantity;
                            resultList.Add(inputMaterialViewModel);
                        }
                    }
                }
            }

            return resultList;
        }
Esempio n. 3
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;
        }
Esempio n. 4
0
 /// <summary>
 /// Get Material list for order item (Product with quantity
 /// </summary>
 /// <param name="productId">Product Id</param>
 /// <param name="quantity">Quantity</param>
 /// <returns>List of Material View Model</returns>
 private List<MaterialViewModel> GetMaterialListForOrderItem(int productId, int quantity)
 {
     List<MaterialViewModel> resultList = new List<MaterialViewModel>();
     Product product = db.Products.FirstOrDefault(m => m.ProductId == productId);
     if (product != null)
     {
         foreach (Recipe recipe in product.Recipes)
         {
             MaterialViewModel materialViewModel = new MaterialViewModel();
             materialViewModel.ProductMaterialId = recipe.ProductMaterialId;
             materialViewModel.NeedQuantity = (int)Math.Floor(recipe.RecipeQuantity * quantity);
             materialViewModel.StorageQuantity = recipe.ProductMaterial.CurrentQuantity;
             materialViewModel.Unit = recipe.ProductMaterial.ProductMaterialUnit;
             materialViewModel.ProductMaterialName = recipe.ProductMaterial.ProductMaterialName;
             resultList.Add(materialViewModel);
         }
     }
     return resultList;
 }