Пример #1
0
        public async Task <IActionResult> Edit(int id, [Bind("ProductMaterial,MaterialId,ProductId,Quantity")] ProductsMaterial productsMaterial)
        {
            if (id != productsMaterial.ProductMaterial)
            {
                return(NotFound());
            }

            if (ModelState.IsValid)
            {
                try
                {
                    _context.Update(productsMaterial);
                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!ProductsMaterialExists(productsMaterial.ProductMaterial))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }
                return(RedirectToAction(nameof(Index)));
            }
            ViewData["MaterialId"] = new SelectList(_context.Materials, "MaterialId", "MaterialName");
            ViewData["ProductId"]  = new SelectList(_context.Products, "ProductId", "ProductName");
            return(View(productsMaterial));
        }
Пример #2
0
        public async Task <IActionResult> Create([Bind("ProductMaterial,MaterialId,ProductId,Quantity")] ProductsMaterial productsMaterial)
        {
            if (ModelState.IsValid)
            {
                _context.Add(productsMaterial);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            ViewData["MaterialId"] = new SelectList(_context.Materials, "MaterialId", "MaterialName");
            ViewData["ProductId"]  = new SelectList(_context.Products, "ProductId", "ProductName");
            return(View(productsMaterial));
        }
Пример #3
0
        public List <MaterialReportViewModel> GetPeriodMatreialReport(DateTime beginDate = default, DateTime endTime = default)
        {
            List <ProductsMaterial>        globalMaterial = new List <ProductsMaterial>();
            List <MaterialReportViewModel> periodMaterialPlanViewModels = new List <MaterialReportViewModel>();
            IEnumerable <DayPlan>          dayPlans;

            if (beginDate != default && endTime != default)
            {
                dayPlans = _context.DayPlans.Where(d => d.Date >= beginDate && d.Date <= endTime);
            }
            else
            {
                dayPlans = _context.DayPlans;
            }
            foreach (DayPlan dayPlan in dayPlans)
            {
                var productsPlans = _context.ProductsPlans.Where(d => d.DayPlanId == dayPlan.DayPlanId).ToList();
                foreach (ProductsPlan pp in productsPlans)
                {
                    var materials = _context.ProductsMaterials.Where(n => n.ProductId == pp.ProductId);
                    foreach (ProductsMaterial pm in materials)
                    {
                        double quantity = (double)pm.Quantity;
                        quantity *= (double)pp.Count;
                        if (globalMaterial.Contains(pm))
                        {
                            globalMaterial.Where(g => g.MaterialId == pm.MaterialId).FirstOrDefault().Quantity += quantity;
                        }
                        else
                        {
                            ProductsMaterial material = new ProductsMaterial
                            {
                                MaterialId = pm.MaterialId,
                                Quantity   = quantity
                            };
                            globalMaterial.Add(material);
                        }
                    }
                }
            }

            var newGlobalNorm = globalMaterial.GroupBy(n => n.MaterialId).Select(n => new { Id = n.Key, Sum = n.Sum(q => q.Quantity) }).ToDictionary(n => n.Id, n => n.Sum);

            foreach (KeyValuePair <int?, double?> keyValue in newGlobalNorm)
            {
                MaterialReportViewModel d = new MaterialReportViewModel
                {
                    MaterialName     = _context.Materials.Where(m => m.MaterialId == keyValue.Key).FirstOrDefault().MaterialName,
                    MaterialQuantity = (double)keyValue.Value
                };
                bool flag = true;
                foreach (MaterialReportViewModel mr in periodMaterialPlanViewModels)
                {
                    if (mr.MaterialName.Equals(d.MaterialName))
                    {
                        mr.MaterialQuantity += d.MaterialQuantity;
                        flag = false;
                    }
                }
                if (flag)
                {
                    periodMaterialPlanViewModels.Add(d);
                }
            }
            return(periodMaterialPlanViewModels);
        }