Ejemplo n.º 1
0
        public ActionResult EditExport(int id)
        {
            var model = new EditMaterialExportViewModel();

            try
            {
                // get import info
                var export = db.MaterialExports.Where(t => t.MaterialExportId == id).FirstOrDefault();
                if (export == null)
                {
                    throw new Exception("Không tìm thấy thông tin nhập nguyên liệu!");
                }

                model.MaterialExportId   = export.MaterialExportId;
                model.MaterialExportDate = export.MaterialExportDate.ToString("dd/MM/yyyy");
                model.MaterialId         = export.MaterialId;
                model.StockId            = export.StockId ?? 0;
                model.Quantity           = export.Quantity.ToString("#,###");
                model.Notes = export.Notes;
            }
            catch (Exception ex)
            {
                ModelState.AddModelError("", ex.Message);
            }

            try
            {
                var materials = db.Materials.OrderBy(t => t.MaterialName)
                                .AsEnumerable()
                                .Select(t => new SelectListItem
                {
                    Value = t.MaterialId.ToString(),
                    Text  = t.MaterialName
                })
                                .ToList();
                var stocks = db.Stocks.OrderBy(r => r.StockName)
                             .AsEnumerable()
                             .Select(r => new SelectListItem
                {
                    Value = r.StockId.ToString(),
                    Text  = r.StockName
                })
                             .ToList();
                ViewBag.Materials = materials;
                ViewBag.Stocks    = stocks;
            }
            catch (Exception ex)
            {
                ModelState.AddModelError("", ex.Message);
            }

            return(View(model));
        }
Ejemplo n.º 2
0
        public ActionResult EditExport(EditMaterialExportViewModel model)
        {
            try
            {
                // get import info
                var export = db.MaterialExports.Where(t => t.MaterialExportId == model.MaterialExportId).FirstOrDefault();
                if (export == null)
                {
                    throw new Exception("Không tìm thấy thông tin xuất nguyên liệu!");
                }

                // get material info
                var material = db.Materials.Where(t => t.MaterialId == export.MaterialId).FirstOrDefault();
                if (material == null)
                {
                    throw new Exception("Không tìm thấy thông tin nguyên liệu!");
                }

                // get quantity
                double quantity = 0;
                if (!double.TryParse(model.Quantity.Replace(",", ""), out quantity))
                {
                    throw new Exception("Số lượng nhập không hợp lệ!");
                }

                // update material unit in stock
                var unitInStock = db.MaterialInStocks.Where(r => r.MaterialId == model.MaterialId && r.StockId == model.StockId).FirstOrDefault();
                if (unitInStock != null)
                {
                    unitInStock.Quantity = unitInStock.Quantity + export.Quantity - quantity;
                }
                else
                {
                    var materialInStock = new MaterialInStock();
                    materialInStock.MaterialId = model.MaterialId;
                    materialInStock.StockId    = model.StockId;
                    materialInStock.Quantity   = 0;
                    db.MaterialInStocks.Add(materialInStock);
                }
                // update import info
                export.MaterialExportDate = DateTime.ParseExact(model.MaterialExportDate, "dd/MM/yyyy", CultureInfo.InvariantCulture, DateTimeStyles.None) +
                                            new TimeSpan(DateTime.Now.Hour, DateTime.Now.Minute, DateTime.Now.Second);
                export.MaterialId = model.MaterialId;
                export.Quantity   = quantity;
                export.Notes      = model.Notes;

                db.SaveChanges();
                return(RedirectToAction("ListExports"));
            }
            catch (Exception ex)
            {
                ModelState.AddModelError("", ex.Message);
            }

            try
            {
                // initialize view bag values
                var materials = db.Materials.OrderBy(t => t.MaterialName)
                                .AsEnumerable()
                                .Select(t => new SelectListItem
                {
                    Value = t.MaterialId.ToString(),
                    Text  = t.MaterialName
                })
                                .ToList();
                var stocks = db.Stocks.OrderBy(r => r.StockName)
                             .AsEnumerable()
                             .Select(r => new SelectListItem
                {
                    Value = r.StockId.ToString(),
                    Text  = r.StockName
                })
                             .ToList();
                ViewBag.Materials = materials;
                ViewBag.Stocks    = stocks;
            }
            catch (Exception ex)
            {
                ModelState.AddModelError("", ex.Message);
            }
            return(View(model));
        }