Example #1
0
        private IntermediateStandardCreateViewModel SetIntermediateStandard(IntermediateStandardCreateViewModel model)
        {
            var department = _uow.GetUserDepartment();
            var units      = _uow.UnitRepository.Get();
            var devices    = _uow.DeviceRepository.Get().Where(item => item.Department.DepartmentId == department.DepartmentId).ToList();

            var items = _uow.InventoryItemRepository.Get()
                        .Where(item => item.Department == department)
                        .GroupBy(i => new { i.StockReagent, i.StockStandard, i.IntermediateStandard })
                        .Select(g => g.First())
                        .ToList();

            model.WeightUnits           = units.Where(item => item.UnitType.Equals("Weight")).ToList();
            model.VolumetricUnits       = units.Where(item => item.UnitType.Equals("Volume")).ToList();
            model.OtherUnit             = units.Where(item => item.UnitType.Equals("Other")).FirstOrDefault();
            model.IntermediateStandards = items.Where(item => item.IntermediateStandard != null && item.Department == department).ToList();
            model.StockStandards        = items.Where(item => item.StockStandard != null && item.Department == department).ToList();
            model.StockReagents         = items.Where(item => item.StockReagent != null && item.Department == department).ToList();
            model.Department            = department;

            model.BalanceDevices    = devices.Where(item => item.DeviceType.Equals("Balance") && item.Department == department && !item.IsArchived).ToList();
            model.VolumetricDevices = devices.Where(item => item.DeviceType.Equals("Volumetric") && item.Department == department && !item.IsArchived).ToList();

            return(model);
        }
Example #2
0
        // GET: /IntermediateStandard/Create
        public ActionResult Create()
        {
            var model = new IntermediateStandardCreateViewModel();

            SetIntermediateStandard(model);

            return(View(model));
        }
Example #3
0
        public ActionResult Create([Bind(Include = "IntermediateStandardId,IntermediateStandardName,TotalVolume,UsedFor,FinalConcentration,TotalAmount,ExpiryDate,SafetyNotes,IsExpiryDateBasedOnDays,DaysUntilExpired,OtherUnitExplained,ConcentrationOtherUnitExplained")] IntermediateStandardCreateViewModel model,
                                   string[] PrepListItemTypes, string[] PrepListAmountTakenUnits, string[] PrepListItemAmounts, string[] PrepListItemLotNumbers, string[] TotalAmountUnits, string[] FinalConcentrationUnits, string submit)
        {
            if (!ModelState.IsValid)
            {
                var errors = ModelState.Where(item => item.Value.Errors.Any());
                SetIntermediateStandard(model);
                return(View(model));
            }

            if (PrepListItemTypes == null || PrepListItemAmounts == null || PrepListItemLotNumbers == null)
            {
                ModelState.AddModelError("", "The creation of the Intermediate Standard failed. Make sure the Prep List table is complete.");
                SetIntermediateStandard(model);
                return(View(model));
            }
            //if all 3 arrays are not of equal length, return to view with an error message
            if (!(PrepListItemAmounts.Length == PrepListItemLotNumbers.Length) || !(PrepListItemLotNumbers.Length == PrepListItemTypes.Length))
            {
                ModelState.AddModelError("", "The creation of the Intermediate Standard failed. Make sure the Prep List table is complete.");
                SetIntermediateStandard(model);
                return(View(model));
            }

            //setting the devices used
            var devicesUsed = Request.Form["Devices"];

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

            if (devicesUsed.Contains(","))
            {
                model.DeviceOne = _uow.DeviceRepository.Get().Where(item => item.DeviceCode.Equals(devicesUsed.Split(',')[0])).FirstOrDefault();
                model.DeviceTwo = _uow.DeviceRepository.Get().Where(item => item.DeviceCode.Equals(devicesUsed.Split(',')[1])).FirstOrDefault();
            }
            else
            {
                model.DeviceOne = _uow.DeviceRepository.Get().Where(item => item.DeviceCode.Equals(devicesUsed.Split(',')[0])).FirstOrDefault();
            }
            //finsihed setting the devices used

            //setting the units to amount and concentration
            model.TotalAmountUnits        = TotalAmountUnits[0];
            model.FinalConcentrationUnits = FinalConcentrationUnits[0];

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

            if (FinalConcentrationUnits.Length > 1)
            {
                model.FinalConcentrationUnits += "/" + FinalConcentrationUnits[1];
            }
            //finished setting teh units to amount and concentration

            var user       = _uow.GetCurrentUser();
            var department = _uow.GetUserDepartment();
            var numOfItems = _uow.InventoryItemRepository.Get().Count();

            //retrieving all table rows from recipe builder - replace with view model in the future
            var prepListViewModel = new IntermediateStandardPrepListItemsViewModel()
            {
                AmountsWithUnits = PrepListItemAmounts
            };

            prepListViewModel.LotNumbers = PrepListItemLotNumbers;
            prepListViewModel.Types      = PrepListItemTypes;

            var reagentAndStandardContainer = new List <object>();
            var prepItems = new List <PrepListItem>();

            //go through all types and sort out what they are, instantiate, and build list of objects
            foreach (var lotNumber in prepListViewModel.LotNumbers)
            {
                foreach (var type in prepListViewModel.Types)
                {
                    if (type.Equals("Reagent"))
                    {
                        var add = _uow.StockReagentRepository
                                  .Get()
                                  .Where(x => x.LotNumber.Equals(lotNumber))
                                  .FirstOrDefault();
                        if (add != null)
                        {
                            reagentAndStandardContainer.Add(add); break;
                        }
                    }
                    else if (type.Equals("Standard"))
                    {
                        var add = _uow.StockStandardRepository
                                  .Get()
                                  .Where(x => x.LotNumber.Equals(lotNumber))
                                  .FirstOrDefault();
                        if (add != null)
                        {
                            reagentAndStandardContainer.Add(add); break;
                        }
                    }
                    else if (type.Equals("Intermediate Standard"))
                    {
                        var add = _uow.IntermediateStandardRepository
                                  .Get()
                                  .Where(x => x.IdCode.Equals(lotNumber))
                                  .FirstOrDefault();
                        if (add != null)
                        {
                            reagentAndStandardContainer.Add(add); break;
                        }
                    }
                }
            }

            //loop through all items used and list all items as "opened" if they're not already open
            //if the expiry date hasn't been set yet, set it with the "days until expired" property provided from the "Create" form
            //building the prep list with the desired prep list items
            int counter = 0;

            foreach (var item in reagentAndStandardContainer)
            {
                if (item is StockReagent)
                {
                    var reagent = item as StockReagent;
                    var invItem = _uow.InventoryItemRepository.Get().Where(x => x != null && x.CatalogueCode != null && x.CatalogueCode.Equals(reagent.CatalogueCode)).First();

                    if (invItem.StockReagent.DateOpened == null)
                    {
                        invItem.StockReagent.DateOpened = DateTime.Today;
                    }

                    if (invItem.StockReagent.ExpiryDate == null)
                    {
                        invItem.StockReagent.ExpiryDate = DateTime.Today.AddDays(Convert.ToInt32(invItem.StockReagent.DaysUntilExpired));
                    }

                    invItem.StockReagent.LastModifiedBy = user.UserName;
                    invItem.StockReagent.DateModified   = DateTime.Today;

                    _uow.InventoryItemRepository.Update(invItem);

                    prepItems.Add(new PrepListItem()
                    {
                        StockReagent = reagent,
                        AmountTaken  = prepListViewModel.AmountsWithUnits[counter]
                    });
                }
                else if (item is StockStandard)
                {
                    var standard = item as StockStandard;
                    var invItem  = _uow.InventoryItemRepository.Get().Where(x => x != null && x.CatalogueCode != null && x.CatalogueCode.Equals(standard.CatalogueCode)).First();

                    if (invItem.StockStandard.DateOpened == null)
                    {
                        invItem.StockStandard.DateOpened = DateTime.Today;
                    }

                    if (invItem.StockStandard.ExpiryDate == null)
                    {
                        invItem.StockStandard.ExpiryDate = DateTime.Today.AddDays(Convert.ToInt32(invItem.StockStandard.DaysUntilExpired));
                    }

                    invItem.StockStandard.LastModifiedBy = user.UserName;
                    invItem.StockStandard.DateModified   = DateTime.Today;

                    _uow.InventoryItemRepository.Update(invItem);

                    prepItems.Add(new PrepListItem()
                    {
                        StockStandard = standard,
                        AmountTaken   = prepListViewModel.AmountsWithUnits[counter]
                    });
                }
                else if (item is IntermediateStandard)
                {
                    var intStandard = item as IntermediateStandard;
                    var invItem     = _uow.InventoryItemRepository.Get().Where(x => x.IntermediateStandard == intStandard).First();

                    if (invItem.IntermediateStandard.DateOpened == null)
                    {
                        invItem.IntermediateStandard.DateOpened = DateTime.Today;
                    }

                    if (invItem.IntermediateStandard.ExpiryDate == null)
                    {
                        invItem.IntermediateStandard.ExpiryDate = DateTime.Today.AddDays(Convert.ToInt32(invItem.IntermediateStandard.DaysUntilExpired));
                    }

                    invItem.IntermediateStandard.LastModifiedBy = user.UserName;
                    invItem.IntermediateStandard.DateModified   = DateTime.Today;

                    _uow.InventoryItemRepository.Update(invItem);

                    prepItems.Add(new PrepListItem()
                    {
                        IntermediateStandard = intStandard,
                        AmountTaken          = prepListViewModel.AmountsWithUnits[counter]
                    });
                }
                _uow.Commit();
                counter++;
            }

            PrepList prepList = new PrepList()
            {
                PrepListItems = prepItems
            };

            model.PrepList = prepList;

            //building the intermediate standard
            IntermediateStandard intermediatestandard = new IntermediateStandard()
            {
                TotalVolume              = model.TotalAmount.ToString() + " " + model.TotalAmountUnits,
                FinalConcentration       = model.FinalConcentration.ToString() + " " + model.FinalConcentrationUnits,
                FinalVolume              = model.FinalVolume,
                IdCode                   = department.Location.LocationCode + "-" + (_uow.InventoryItemRepository.Get().Count() + 1) + "-" + user.Department.SubDepartment,// + "/",//append number of bottles?
                PrepList                 = model.PrepList,
                SafetyNotes              = model.SafetyNotes,
                CreatedBy                = user.UserName,
                DateCreated              = DateTime.Today,
                ExpiryDate               = model.ExpiryDate,
                DaysUntilExpired         = model.DaysUntilExpired,
                IntermediateStandardName = model.IntermediateStandardName,
                Replaces                 = !string.IsNullOrEmpty(model.Replaces) ? model.Replaces : "N/A",
                ReplacedBy               = !string.IsNullOrEmpty(model.ReplacedBy) ? model.ReplacedBy : "N/A"
            };

            InventoryItem inventoryItem = new InventoryItem()
            {
                Department           = department,
                IntermediateStandard = intermediatestandard,
                Type = "Intermediate Standard",
                StorageRequirements             = model.StorageRequirements,
                UsedFor                         = model.UsedFor,
                FirstDeviceUsed                 = model.DeviceOne,
                SecondDeviceUsed                = model.DeviceTwo,
                OtherUnitExplained              = model.OtherUnitExplained,
                SupplierName                    = "Maxxam",
                ConcentrationOtherUnitExplained = model.ConcentrationOtherUnitExplained,
                InitialAmount                   = model.TotalAmount.ToString() + " " + model.TotalAmountUnits,
            };

            //creating the prep list and the intermediate standard
            _uow.PrepListRepository.Create(prepList);
            intermediatestandard.InventoryItems.Add(inventoryItem);
            _uow.IntermediateStandardRepository.Create(intermediatestandard);

            var result = _uow.Commit();

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

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

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