public ActionResult Create([Bind(Include = "ID,PlantID,LineID,RawMaterialReceivedID,DateEntered,EnteredBy,LastModified,ModifiedBy")] CurrentRawMaterialViewModel viewModel)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    var dto = MapCurrentRawMaterialViewModelToDTO(viewModel);
                    dto.ModifiedBy   = dto.EnteredBy;
                    dto.LastModified = DateTime.Now;
                    dto.DateEntered  = DateTime.Now;
                    CurrentRawMaterialRepo.Add(dto);

                    TempData["ActionMessage"]     = MessageRepository.GetStringValue(MessageKeys.ResponseMessageSuccessSave);
                    TempData["ActionMessageType"] = MessageRepository.GetStringValue(MessageKeys.ResponseTypeSuccess);
                    return(RedirectToAction("Index", new { lineId = dto.LineId }));
                }
                catch (Exception ex)
                {
                    ViewBag.ExceptionMessage = ex.Message;

                    TempData["ActionMessage"]     = MessageRepository.GetStringValue(MessageKeys.ResponseMessageFailSave);
                    TempData["ActionMessageType"] = MessageRepository.GetStringValue(MessageKeys.ResponseTypeError);
                    return(View(viewModel));
                }
            }

            return(RedirectToAction("Create"));
        }
        public ActionResult Edit([Bind(Include = "ID,PlantID,LineID,RawMaterialReceivedID,DateEntered,EnteredBy,LastModified,ModifiedBy")] CurrentRawMaterialViewModel viewModel)
        {
            if (ModelState.IsValid)
            {
                var dto = MapCurrentRawMaterialViewModelToDTO(viewModel);
                CurrentRawMaterialRepo.Update(dto);

                TempData["ActionMessage"]     = MessageRepository.GetStringValue(MessageKeys.ResponseMessageSuccessSave);
                TempData["ActionMessageType"] = MessageRepository.GetStringValue(MessageKeys.ResponseTypeSuccess);
                return(RedirectToAction("Index", new { lineId = dto.LineId }));
            }
            TempData["ActionMessage"]     = MessageRepository.GetStringValue(MessageKeys.ResponseMessageFailSave);
            TempData["ActionMessageType"] = MessageRepository.GetStringValue(MessageKeys.ResponseTypeError);
            return(ShowEditView(viewModel));
        }
        private ActionResult ShowEditView(CurrentRawMaterialViewModel vm)
        {
            var lines = ProductionLineRepo.GetProductionLines(vm.PlantId);

            ViewBag.LineID = new SelectList(lines, "Code", "Code");

            var rawMaterialCodes = RawMaterialReceivedRepo.GetAvailableRawMaterialIds(vm.PlantId);

            ViewBag.RawMaterialReceivedCode = new SelectList(rawMaterialCodes, vm.RawMaterialReceivedCode);

            var lots = RawMaterialReceivedRepo.GetAll(vm.PlantId, (int)(vm.RawMaterialReceivedId ?? 0));

            ViewBag.Lots = new SelectList(lots, "ID", "LotID", vm.RawMaterialReceivedId);

            return(View(vm));
        }
        private CurrentRawMaterialDTO MapCurrentRawMaterialViewModelToDTO(CurrentRawMaterialViewModel viewModel)
        {
            var dto = new CurrentRawMaterialDTO
            {
                Id      = viewModel.Id,
                PlantId = viewModel.PlantId,
                LineId  = viewModel.LineId,
                LotId   = viewModel.LotId,
                RawMaterialReceivedId   = viewModel.RawMaterialReceivedId,
                RawMaterialReceivedCode = viewModel.RawMaterialReceivedCode,
                LastModified            = viewModel.LastModified,
                ModifiedBy  = viewModel.ModifiedBy,
                DateEntered = viewModel.DateEntered,
                EnteredBy   = viewModel.EnteredBy
            };

            if (dto.PlantId == 0)
            {
                dto.PlantId = CurrentPlantId;
            }
            return(dto);
        }
        private CurrentRawMaterialViewModel MapCurrentRawMaterialDTOToViewModel(CurrentRawMaterialDTO dto)
        {
            var vm = new CurrentRawMaterialViewModel
            {
                Id      = dto.Id,
                LineId  = dto.LineId,
                PlantId = dto.PlantId,
                RawMaterialReceivedId   = dto.RawMaterialReceivedId,
                RawMaterialReceivedCode = dto.RawMaterialReceivedCode,
                LotId        = dto.LotId,
                DateEntered  = dto.DateEntered,
                EnteredBy    = dto.EnteredBy,
                LastModified = dto.LastModified,
                ModifiedBy   = dto.ModifiedBy
            };

            if (vm.PlantId == 0)
            {
                vm.PlantId = CurrentPlantId;
            }
            return(vm);
        }
        // GET: /RawMaterialCurrent/Create
        public ActionResult Create(int plantId)
        {
            var model = new CurrentRawMaterialViewModel
            {
                PlantId      = plantId,
                LastModified = DateTime.Now,
                DateEntered  = DateTime.Now,
                ModifiedBy   = CurrentUser,
                EnteredBy    = CurrentUser
            };

            // MOCK DATA - PRODUCTION LINES
            var lines = ProductionLineRepo.GetProductionLines(plantId);

            ViewBag.LineID = new SelectList(lines, "Code", "Code");


            var rawMaterialCodes = RawMaterialReceivedRepo.GetAvailableRawMaterialIds(plantId);

            ViewBag.RawMaterialReceivedCode = new SelectList(rawMaterialCodes);


            return(View(model));
        }