コード例 #1
0
        public async Task <IActionResult> Procure(MaterialProcureViewModel model)
        {
            var materials = materialsService.GetMaterials();
            var suppliers = materialsService.GetSuppliers();

            model.Materials = materials.Select(m => new SelectListItem()
            {
                Text  = $"{m.Element.GetDisplayName()} ({m.Form.ToString()})",
                Value = m.Id.ToString()
            }).OrderBy(i => i.Text);
            model.Suppliers = suppliers.Where(s => s.ToString() != "None").ToArray();
            if (string.IsNullOrWhiteSpace(model.MaterialId))
            {
                ViewData[ErrorKey] = string.Format(SelectionNullErrorMessage, "a material");
                return(View(model));
            }
            if (model.Quantity <= 0)
            {
                ViewData[ErrorKey] = "Quantity must be positive.";
                return(View(model));
            }
            var material = await materialsService.FindByIdAsync(model.MaterialId);

            if (material == null)
            {
                ViewData[ErrorKey] = "No such material found in inventory list.";
                return(View(model));
            }
            int materialRarity         = (int)material.Element;
            int supplierDaysToDelivery = int.Parse(model.SupplierSpeed);

            if (materialRarity > 26 && supplierDaysToDelivery >= 15 ||
                materialRarity > 42 && supplierDaysToDelivery >= 9)
            {
                ViewData[ErrorKey] = "This supplier does not have the required quantity. Try another.";
                return(View(model));
            }
            material.QuantityInKg += model.Quantity;
            materialsService.UpdateStock(material);
            string materialDisplayName = material.Element.GetDisplayName().ToLower() +
                                         $" {material.Form.ToString().ToLower()}";

            ViewData[InfoKey] = $"{model.Quantity}kg of {materialDisplayName} added to inventory.";
            await employeeService.UpdateLastWorkedAsync(User.GetId());

            return(View(model));
        }