Example #1
0
        public virtual ActionResult AcceptBuilding(BuildingViewModel model)
        {
            string currentBuildingId;
            string currentSaurceArea;
            string currentSaurceAreaShortName;

            //TC1 : Press enter in textbox on building view.
            if (string.IsNullOrEmpty(model.ScannedBuildingOrArea))
            {
                //TC2 : Press enter in textbox on building area view or pallet view..
                if (!string.IsNullOrEmpty(model.CurrentBuildingId))
                {
                    model.CurrentBuildingId = model.CurrentBuildingId.ToUpper();
                    // If building is set, just proceed because area is optional
                    currentBuildingId          = model.CurrentBuildingId;
                    currentSaurceArea          = model.CurrentSourceArea;
                    currentSaurceAreaShortName = model.CurrentSourceAreaShortName;
                    MasterModel.ClearSessionValues(this.Session);
                    model.CurrentBuildingId          = currentBuildingId;
                    model.CurrentSourceArea          = currentSaurceArea;
                    model.CurrentSourceAreaShortName = currentSaurceAreaShortName;
                    return(View(Views.Pallet, new PalletViewModel(this.Session)));
                }
                // Building is not set. Start over.
                MasterModel.ClearSessionValues(this.Session);
                return(View(Views.Building, model));
            }

            var fieldName = model.NameFor(m => m.ScannedBuildingOrArea);

            try
            {
                PickContext pickContext = _repos.Value.GetPickContext(model.ScannedBuildingOrArea);
                //TC3 : Scan invlaid building or area.
                if (pickContext == null)
                {
                    // Bad scan. Preserve current building and area
                    ModelState.AddModelError(fieldName, string.Format("{0} is not a valid Building or Area", model.ScannedBuildingOrArea));
                    currentBuildingId          = model.CurrentBuildingId;
                    currentSaurceArea          = model.CurrentSourceArea;
                    currentSaurceAreaShortName = model.CurrentSourceAreaShortName;
                }
                else
                {
                    //TC4 : On first scan of building we reach here
                    if (IsInitialScan(model))
                    {
                        // Both values are
                        currentBuildingId          = pickContext.BuildingId;
                        currentSaurceArea          = pickContext.SourceArea;
                        currentSaurceAreaShortName = pickContext.SourceAreaShortName;
                    }
                    else if (pickContext.IsBuilding)
                    {
                        // Accept the building only
                        currentBuildingId          = pickContext.BuildingId;
                        currentSaurceAreaShortName = model.CurrentSourceAreaShortName;
                        currentSaurceArea          = model.CurrentSourceArea;
                    }
                    else if (pickContext.IsMultiBuildingArea)
                    {
                        currentBuildingId          = model.CurrentBuildingId;
                        currentSaurceArea          = pickContext.SourceArea;
                        currentSaurceAreaShortName = pickContext.SourceAreaShortName;
                    }
                    else if (pickContext.IsSingleBuildingArea)
                    {
                        // Ignore the area if it belongs to a different building
                        if (string.IsNullOrEmpty(model.CurrentBuildingId) || model.CurrentBuildingId == pickContext.BuildingId)
                        {
                            currentBuildingId          = pickContext.BuildingId;
                            currentSaurceArea          = pickContext.SourceArea;
                            currentSaurceAreaShortName = pickContext.SourceAreaShortName;
                        }
                        else
                        {
                            ModelState.AddModelError("", string.Format("Area {0} doesn't belong to building {1}", pickContext.SourceArea, model.CurrentBuildingId));
                            currentBuildingId          = model.CurrentBuildingId;
                            currentSaurceAreaShortName = string.Empty;
                            currentSaurceArea          = string.Empty;
                        }
                    }
                    else
                    {
                        throw new Exception("We never expect to get here");
                    }
                }
            }
            catch (DbException ex)
            {
                ModelState.AddModelError(fieldName, ex.Message);
                currentBuildingId          = model.CurrentBuildingId;
                currentSaurceArea          = model.CurrentSourceArea;
                currentSaurceAreaShortName = model.CurrentSourceAreaShortName;
            }
            MasterModel.ClearSessionValues(this.Session);
            model.CurrentBuildingId          = currentBuildingId;
            model.CurrentSourceArea          = currentSaurceArea;
            model.CurrentSourceAreaShortName = currentSaurceAreaShortName;
            if (!ModelState.IsValid || string.IsNullOrEmpty(model.CurrentBuildingId) || string.IsNullOrEmpty(model.CurrentSourceArea))
            {
                return(View(Views.Building, model));
            }

            return(View(Views.Pallet, new PalletViewModel(this.Session)));
        }