private StockStandardCreateViewModel SetStockStandard(StockStandardCreateViewModel model) { var units = _uow.UnitRepository.Get(); var devices = _uow.DeviceRepository.Get().ToList(); var userDepartment = _uow.GetUserDepartment(); model.WeightUnits = units.Where(item => item.UnitType.Equals("Weight")).ToList(); model.VolumetricUnits = units.Where(item => item.UnitType.Equals("Volume")).ToList(); model.BalanceDevices = devices.Where(item => item.DeviceType.Equals("Balance") && item.Department.Equals(userDepartment) && !item.IsArchived).ToList(); model.VolumetricDevices = devices.Where(item => item.DeviceType.Equals("Volumetric") && item.Department.Equals(userDepartment) && !item.IsArchived).ToList(); model.Department = userDepartment; return(model); }
public static CheckModelState EnterStandardIntoDatabase(StockStandardCreateViewModel model, InventoryItem inventoryItem, int numOfItems, Department department, string user, UnitOfWork _uow) { CheckModelState result = CheckModelState.Invalid;//default to invalid to expect the worst var repo = _uow.StockStandardRepository; StockStandard createStandard = new StockStandard() { LotNumber = model.LotNumber, StockStandardName = model.StockStandardName, Purity = model.Purity, SolventUsed = model.SolventUsed, Concentration = model.Concentration.ToString() + " " + model.InitialConcentrationUnits, DateReceived = model.DateReceived, DateCreated = DateTime.Today, DateOpened = null, DaysUntilExpired = model.DaysUntilExpired, ExpiryDate = model.ExpiryDate, CreatedBy = user, DateModified = null, CatalogueCode = model.CatalogueCode.ToUpper() }; if (model.NumberOfBottles > 1) { //for (int i = 1; i <= model.NumberOfBottles; i++) { createStandard.IdCode = department.Location.LocationCode + "-" + (numOfItems + 1) + "-" + model.LotNumber + "/" + model.NumberOfBottles; //append number of bottles createStandard.InventoryItems.Add(inventoryItem); repo.Create(createStandard); result = _uow.Commit(); //creation wasn't successful - break from loop and let switch statement handle the problem //if (result != CheckModelState.Valid) { break; } //} } else { createStandard.IdCode = department.Location.LocationCode + "-" + (numOfItems + 1) + "-" + model.LotNumber + "/" + model.NumberOfBottles;//only 1 bottle, no need to concatenate createStandard.InventoryItems.Add(inventoryItem); repo.Create(createStandard); result = _uow.Commit(); } return(result); }
public static InventoryItem BuildStandardInventoryItem(StockStandardCreateViewModel model, Department department) { InventoryItem inventoryItem = new InventoryItem() { CatalogueCode = model.CatalogueCode.ToUpper(), Department = department, UsedFor = model.UsedFor, Type = "Standard", FirstDeviceUsed = model.DeviceOne, SecondDeviceUsed = model.DeviceTwo, StorageRequirements = model.StorageRequirements, SupplierName = model.SupplierName, NumberOfBottles = model.NumberOfBottles, InitialAmount = model.InitialAmount.ToString() + " " + model.InitialAmountUnits, OtherUnitExplained = model.OtherUnitExplained, ConcentrationOtherUnitExplained = model.ConcentrationOtherUnitExplained }; inventoryItem.MSDS.Add(model.MSDS); inventoryItem.CertificatesOfAnalysis.Add(model.CertificateOfAnalysis); return(inventoryItem); }
public ActionResult Create([Bind(Include = "StockStandardName,SupplierName,CatalogueCode,StorageRequirements,MSDSNotes,LotNumber,MSDSNotes,UsedFor,SolventUsed,Purity,ExpiryDate,NumberOfBottles,InitialAmount,Concentration,DateReceived,IsExpiryDateBasedOnDays,DaysUntilExpired,OtherUnitExplained,ConcentrationOtherUnitExplained")] StockStandardCreateViewModel model, string[] AmountUnit, string[] ConcentrationUnit, HttpPostedFileBase uploadCofA, HttpPostedFileBase uploadMSDS, string submit) { //model isn't valid, return to the form if (!ModelState.IsValid) { var errors = ModelState.Values.SelectMany(v => v.Errors); return(View(SetStockStandard(model))); } var inventoryRepository = _uow.InventoryItemRepository; //catalogue code must be unique - let's verify bool doesCatalogueCodeExist = inventoryRepository.Get() .Any(item => item.CatalogueCode != null && item.CatalogueCode.Equals(model.CatalogueCode)); if (doesCatalogueCodeExist) { ModelState.AddModelError("", "The Catalogue Code provided is not unique. If the Catalogue Code provided is in fact correct, add the item as a new Lot Number under the existing Catalogue Code."); return(View(SetStockStandard(model))); } var devicesUsed = Request.Form["Devices"]; var user = _uow.GetCurrentUser(); var department = _uow.GetUserDepartment(); var numOfItems = inventoryRepository.Get().Count(); if (devicesUsed == null) { ModelState.AddModelError("", "You must select a device that was used."); return(View(SetStockStandard(model))); } model = BuildReagentOrStandard.BuildStandard(model, devicesUsed, AmountUnit, ConcentrationUnit, uploadCofA, uploadMSDS, _uow); InventoryItem inventoryItem = BuildReagentOrStandard.BuildStandardInventoryItem(model, department); StockStandard createStandard = null; CheckModelState result = BuildReagentOrStandard.EnterStandardIntoDatabase(model, inventoryItem, numOfItems, department, user.UserName, _uow); switch (result) { case CheckModelState.Invalid: ModelState.AddModelError("", "The creation of " + createStandard.StockStandardName + " failed. Please double check all inputs and try again."); return(View(SetStockStandard(model))); case CheckModelState.DataError: ModelState.AddModelError("", "Unable to save changes. Try again, and if the problem persists please contact your system administrator."); return(View(SetStockStandard(model))); case CheckModelState.Error: ModelState.AddModelError("", "There was an error. Please try again."); return(View(SetStockStandard(model))); case CheckModelState.Valid: if (!string.IsNullOrEmpty(submit) && submit.Equals("Save")) { //save pressed return(RedirectToAction("Index")); } else { //save & new pressed return(RedirectToAction("Create")); } default: ModelState.AddModelError("", "An unknown error occurred."); return(View(SetStockStandard(model))); } }
/* Standard Code */ public static StockStandardCreateViewModel BuildStandard(StockStandardCreateViewModel model, string devicesUsed, string[] AmountUnit, string[] ConcentrationUnit, HttpPostedFileBase uploadCofA, HttpPostedFileBase uploadMSDS, UnitOfWork _uow) { var deviceRepo = _uow.BalanceDeviceRepository; if (model.NumberOfBottles == 0) { model.NumberOfBottles = 1; } if (devicesUsed.Contains(",")) { model.DeviceOne = deviceRepo.Get().Where(item => item.DeviceCode.Equals(devicesUsed.Split(',')[0])).FirstOrDefault(); model.DeviceTwo = deviceRepo.Get().Where(item => item.DeviceCode.Equals(devicesUsed.Split(',')[1])).FirstOrDefault(); } else { model.DeviceOne = deviceRepo.Get().Where(item => item.DeviceCode.Equals(devicesUsed.Split(',')[0])).FirstOrDefault(); } model.InitialAmountUnits = AmountUnit[0]; model.InitialConcentrationUnits = ConcentrationUnit[0]; if (AmountUnit.Length > 1) { model.InitialAmountUnits += "/" + AmountUnit[1]; } if (ConcentrationUnit.Length > 1) { model.InitialConcentrationUnits += "/" + ConcentrationUnit[1]; } if (uploadCofA != null) { var cofa = new CertificateOfAnalysis() { FileName = uploadCofA.FileName, ContentType = uploadCofA.ContentType, DateAdded = DateTime.Today }; using (var reader = new System.IO.BinaryReader(uploadCofA.InputStream)) { cofa.Content = reader.ReadBytes(uploadCofA.ContentLength); } model.CertificateOfAnalysis = cofa; } if (uploadMSDS != null) { var msds = new MSDS() { FileName = uploadMSDS.FileName, ContentType = uploadMSDS.ContentType, DateAdded = DateTime.Today, MSDSNotes = model.MSDSNotes }; using (var reader = new System.IO.BinaryReader(uploadMSDS.InputStream)) { msds.Content = reader.ReadBytes(uploadMSDS.ContentLength); } model.MSDS = msds; } return(model); }