private BalanceVerificationViewModel SetVerificationBalance(BalanceVerificationViewModel model)
        {
            //TODO use in post method of verification
            var locations      = _uow.LocationRepository.Get();
            var userDepartment = _uow.GetUserDepartment();
            var device         = _uow.DeviceRepository.Get().Where(item => item.DeviceId == model.BalanceId).First();

            model.LocationNames         = locations.Select(item => item.LocationName).ToList();
            model.Department            = userDepartment;
            model.DeviceCode            = device.DeviceCode;
            model.CurrentLocation       = device.Department.Location.LocationName;
            model.NumberOfDecimals      = device.NumberOfDecimals;
            model.BalanceType           = device.BalanceType;
            model.NumberOfTestsToVerify = device.NumberOfTestsToVerify;
            model.WeightLimitOne        = device.AmountLimitOne + " g";
            model.WeightLimitTwo        = device.AmountLimitTwo + " g ";
            model.WeightLimitThree      = device.AmountLimitThree == null ? null : device.AmountLimitThree + " g";
            model.NumberOfDecimals      = device.NumberOfDecimals;

            return(model);
        }
        public ActionResult Verification(int?id)
        {
            //sending all Locations to the view
            var locations = _uow.LocationRepository.Get().Select(name => name.LocationName).ToList();
            var balance   = _uow.BalanceDeviceRepository.Get(id);

            var device = new BalanceVerificationViewModel()
            {
                BalanceId             = balance.DeviceId,
                Location              = balance.Department.Location,
                DeviceCode            = balance.DeviceCode,
                CurrentLocation       = balance.Department.Location.LocationName,
                LocationNames         = locations,
                BalanceType           = balance.BalanceType,
                NumberOfTestsToVerify = balance.NumberOfTestsToVerify,
                WeightLimitOne        = balance.AmountLimitOne + " g",
                WeightLimitTwo        = balance.AmountLimitTwo + " g ",
                WeightLimitThree      = balance.AmountLimitThree == null ? null : balance.AmountLimitThree + " g",
                NumberOfDecimals      = balance.NumberOfDecimals
            };

            return(View(device));
        }
        public ActionResult CreateVerification([Bind(Include = "BalanceId,WeightId,DeviceCode,BalanceType,WeightOne,WeightTwo,WeightThree,Comments,WeightId")] BalanceVerificationViewModel balancetest,
                                               string[] WeightOneTable, string[] WeightTwoTable, string[] WeightThreeTable, string[] PassOrFailTable)
        {
            if (!ModelState.IsValid)
            {
                var errors = ModelState.Where(item => item.Value.Errors.Any());
                return(View("Verification", SetVerificationBalance(balancetest)));
            }

            if (WeightOneTable == null || WeightTwoTable == null || WeightThreeTable == null || PassOrFailTable == null)
            {
                ModelState.AddModelError("", "Writing the device verification failed. Make sure the device verification is complete and try again.");
                return(View("Verification", SetVerificationBalance(balancetest)));
            }
            //if all 3 arrays are not of equal length, return to view with an error message
            if (!(WeightOneTable.Length == WeightTwoTable.Length) || !(WeightThreeTable.Length == PassOrFailTable.Length))
            {
                ModelState.AddModelError("", "Writing the device verification failed. Make sure the device verification is complete and try again.");
                return(View("Verification", SetVerificationBalance(balancetest)));
            }

            var balance      = _uow.BalanceDeviceRepository.Get().Where(item => item.DeviceCode.Equals(balancetest.DeviceCode)).First();
            var result       = CheckModelState.Invalid;
            var user         = _uow.GetCurrentUser();
            var verification = new DeviceVerification();

            //arrays are aligned, so let's use a traditional for-loop
            for (int i = 0; i < PassOrFailTable.Length; i++)
            {
                verification = new DeviceVerification()
                {
                    DidTestPass = PassOrFailTable[i].Equals("Pass") ? true : false,
                    VerifiedOn  = DateTime.Today,
                    WeightOne   = string.IsNullOrEmpty(WeightOneTable[i]) ? (double?)null : Convert.ToDouble(WeightOneTable[i]),
                    WeightTwo   = string.IsNullOrEmpty(WeightTwoTable[i]) ? (double?)null : Convert.ToDouble(WeightTwoTable[i]),
                    WeightThree = string.IsNullOrEmpty(WeightThreeTable[i]) ? (double?)null : Convert.ToDouble(WeightThreeTable[i]),
                    WeightId    = balancetest.WeightId,
                    Device      = balance,
                    User        = user
                };

                _uow.DeviceVerificationRepostory.Create(verification);
                result = _uow.Commit();

                if (result != CheckModelState.Valid)
                {
                    //writing to db didn't work, break from loop
                    break;
                }
                //add verification to the balance
                balance.DeviceVerifications.Add(verification);
            }

            switch (result)
            {
            case CheckModelState.Invalid:
                ModelState.AddModelError("", "The verification of " + balancetest.DeviceCode + " failed. Please double check all inputs and try again.");
                return(View("Verification", SetVerificationBalance(balancetest)));

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

            case CheckModelState.Error:
                ModelState.AddModelError("", "There was an error. Please try again.");
                return(View("Verification", SetVerificationBalance(balancetest)));

            case CheckModelState.Valid:
                balance.IsVerified = verification.DidTestPass;
                balance.Status     = "In Good Standing";
                _uow.BalanceDeviceRepository.Update(balance);
                //_uow.Commit();
                //save pressed
                return(RedirectToAction("Index"));

            default:
                ModelState.AddModelError("", "An unknown error occurred.");
                return(View("Verification", SetVerificationBalance(balancetest)));
            }
        }