Example #1
0
        private StockReagentCreateViewModel SetStockReagent(StockReagentCreateViewModel 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 == userDepartment && !item.IsArchived).ToList();
            model.VolumetricDevices = devices.Where(item => item.DeviceType.Equals("Volumetric") && item.Department == userDepartment && !item.IsArchived).ToList();
            model.Department        = userDepartment;

            return(model);
        }
Example #2
0
        public ActionResult Create([Bind(Include = "CatalogueCode,MSDSNotes,SupplierName,ReagentName,StorageRequirements,Grade,UsedFor,LotNumber,GradeAdditionalNotes,NumberOfBottles,ExpiryDate,InitialAmount,DateReceived,IsExpiryDateBasedOnDays,DaysUntilExpired,OtherUnitExplained")]
                                   StockReagentCreateViewModel model, string[] Unit, HttpPostedFileBase uploadCofA, HttpPostedFileBase uploadMSDS, string submit)
        {
            //model isn't valid, return to the form
            if (!ModelState.IsValid)
            {
                return(View(SetStockReagent(model)));
            }

            var invRepo = _uow.InventoryItemRepository;

            //catalogue code must be unique - let's verify
            bool doesCatalogueCodeExist = invRepo.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(SetStockReagent(model)));
            }

            //last line of defense for number of bottles
            if (model.NumberOfBottles == 0)
            {
                model.NumberOfBottles = 1;
            }

            model.InitialAmountUnits = Unit[0];

            if (Unit.Length > 1)
            {
                model.InitialAmountUnits += "/" + Unit[1];
            }

            var devicesUsed = Request.Form["Devices"];
            var deviceRepo  = _uow.DeviceRepository;


            if (devicesUsed == null)
            {
                ModelState.AddModelError("", "You must select a device that was used.");
                return(View(SetStockReagent(model)));
            }

            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();
            }

            var user       = _uow.GetCurrentUser();
            var numOfItems = invRepo.Get().Count();

            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;
            }

            InventoryItem inventoryItem = new InventoryItem()
            {
                CatalogueCode       = model.CatalogueCode.ToUpper(),
                Department          = user.Department,
                UsedFor             = model.UsedFor,
                Type                = "Reagent",
                StorageRequirements = model.StorageRequirements,
                SupplierName        = model.SupplierName,
                NumberOfBottles     = model.NumberOfBottles,
                InitialAmount       = model.InitialAmount.ToString() + " " + model.InitialAmountUnits,
                OtherUnitExplained  = model.OtherUnitExplained,
                FirstDeviceUsed     = model.DeviceOne,
                SecondDeviceUsed    = model.DeviceTwo
            };

            inventoryItem.MSDS.Add(model.MSDS);
            inventoryItem.CertificatesOfAnalysis.Add(model.CertificateOfAnalysis);
            //getting the enum result

            //CheckModelState result = CheckModelState.Invalid;//default to invalid to expect the worst
            StockReagent reagent = new StockReagent()
            {
                LotNumber            = model.LotNumber,
                IdCode               = user.Department.Location.LocationCode + "-" + (numOfItems + 1) + "-" + model.LotNumber + "/" + model.NumberOfBottles,//append number of bottles
                ReagentName          = model.ReagentName,
                Grade                = model.Grade,
                GradeAdditionalNotes = model.GradeAdditionalNotes,
                DateReceived         = model.DateReceived,
                DateOpened           = null,
                DaysUntilExpired     = model.DaysUntilExpired,
                ExpiryDate           = model.ExpiryDate,
                DateCreated          = DateTime.Today,
                CreatedBy            = user.UserName,
                DateModified         = null,
                CatalogueCode        = model.CatalogueCode.ToUpper()
            };

            reagent.InventoryItems.Add(inventoryItem);
            _uow.StockReagentRepository.Create(reagent);

            var result = _uow.Commit();

            //if (model.NumberOfBottles > 1) {
            //    for (int i = 1; i <= model.NumberOfBottles; i++) {
            //        reagent = new StockReagent() {
            //            LotNumber = model.LotNumber,
            //            IdCode = department.Location.LocationCode + "-" + (numOfItems + 1) + "-" + model.LotNumber + "/" + i,//append number of bottles
            //            ReagentName = model.ReagentName,
            //            Grade = model.Grade,
            //            GradeAdditionalNotes = model.GradeAdditionalNotes,
            //            DateReceived = model.DateReceived,
            //            DateOpened = null,
            //            DaysUntilExpired = model.DaysUntilExpired,
            //            ExpiryDate = model.ExpiryDate,
            //            DateCreated = DateTime.Today,
            //            CreatedBy = user.UserName,
            //            DateModified = null,
            //            CatalogueCode = model.CatalogueCode
            //        };

            //        reagent.InventoryItems.Add(inventoryItem);
            //        result = repo.Create(reagent);

            //        //creation wasn't successful - break from loop and let switch statement handle the problem
            //        if (result != CheckModelState.Valid) { break; }
            //    }
            //} else {
            //    reagent = new StockReagent() {
            //        LotNumber = model.LotNumber,
            //        IdCode = department.Location.LocationCode + "-" + (numOfItems + 1) + "-" + model.LotNumber,//only 1 bottle, no need to concatenate
            //        ReagentName = model.ReagentName,
            //        Grade = model.Grade,
            //        GradeAdditionalNotes = model.GradeAdditionalNotes,
            //        DateReceived = model.DateReceived,
            //        DateOpened = null,
            //        DaysUntilExpired = model.DaysUntilExpired,
            //        ExpiryDate = model.ExpiryDate,
            //        DateCreated = DateTime.Today,
            //        CreatedBy = user.UserName,
            //        DateModified = null,
            //        CatalogueCode = model.CatalogueCode
            //    };

            //    reagent.InventoryItems.Add(inventoryItem);
            //    result = repo.Create(reagent);
            //}


            switch (result)
            {
            case CheckModelState.Invalid:
                ModelState.AddModelError("", "The creation of " + reagent.ReagentName + " failed. Please double check all inputs and try again.");
                return(View(SetStockReagent(model)));

            case CheckModelState.DataError:
                ModelState.AddModelError("", "Unable to save changes. Try again, and if the problem persists please contact your system administrator.");
                return(View(SetStockReagent(model)));

            case CheckModelState.Error:
                ModelState.AddModelError("", "There was an error. Please try again.");
                return(View(SetStockReagent(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(SetStockReagent(model)));
            }
        }