예제 #1
0
        public JsonResult GetMunicipalityData(string selectedStateId)
        {
            int parsedSelectedStateId;

            if (Int32.TryParse(selectedStateId, out parsedSelectedStateId))
            {
                var distributorController = new PSD.Controller.DistributorController(Configurations);
                var result = distributorController.GetMunicipalitiesByState(parsedSelectedStateId)
                             .Select(x => new MunicipalityDataViewModel {
                    MunicipalityId = x.Id, MunicipalityName = x.Name
                });
                return(Json(result, JsonRequestBehavior.AllowGet));
            }
            else
            {
                return(Json("Error when tried to retrieve municipality", JsonRequestBehavior.AllowGet));
            }
        }
예제 #2
0
        public ActionResult Create(Subdistributor model, string selectedType = "", string selectedCommercialNames = "", string postAction = "", int selectedAddressState = -1, List <int> selectedMunicipalities = null, List <int> selectedCrops = null)
        {
            if (model == null)
            {
                return(RedirectToError(messageDebug: controllerTraceId + "DistributorCreate[Post].111 No model was received"));
            }

            if (TempData["CropsXMunicipality"] != null)
            {
                //copy the info from TempData into an aux object (cause we cannot use the same object from TempData, it would cause to have two UnitOfWork objects (one from the TempDate and one from the controller object), causing an error on the controller ('An entity object cannot be referenced by multiple instances of IEntityChangeTracker')
                ICollection <SubdistributorCropsXMunicipality> auxCropsXMunicipality = (ICollection <SubdistributorCropsXMunicipality>)TempData["CropsXMunicipality"];
                foreach (SubdistributorCropsXMunicipality item in auxCropsXMunicipality)
                {
                    model.CropsXMunicipality.Add(new SubdistributorCropsXMunicipality()
                    {
                        AddressMunicipalityAddressStateId = item.AddressMunicipalityAddressStateId,
                        AddressMunicipalityId             = item.AddressMunicipalityId,
                        Cat_CropId = item.Cat_CropId,
                        //Crop = item.Crop,
                        //Municipality = item.Municipality
                    });
                }
                auxCropsXMunicipality = null;

                TempData["CropsXMunicipality"] = model.CropsXMunicipality;
            }

            List <AddressMunicipality> auxMunicipalities;

            PSD.Controller.DistributorController distributorController = null;
            switch (postAction)
            {
            case "getMunicipalities":     //get municipalities for the selected addressState
                distributorController = new PSD.Controller.DistributorController(Configurations);
                auxMunicipalities     = distributorController.GetMunicipalitiesByState(selectedAddressState);
                //ViewBag.SelectedPostalCode = auxPostalCode;
                ViewBag.AvailableMunicipalities = auxMunicipalities;
                ViewBag.AddressStates           = AddressStates(selectedAddressState);
                return(View(model));

            case "addInfluence":     //add "area de influencia" to the list
                distributorController = new PSD.Controller.DistributorController(Configurations);
                auxMunicipalities     = distributorController.GetMunicipalitiesByState(selectedAddressState);
                foreach (int itemMunicipalityId in selectedMunicipalities)
                {
                    foreach (int itemCropId in selectedCrops)
                    {
                        AddressMunicipality municipalityName = new AddressMunicipality();
                        foreach (AddressMunicipality aMName in auxMunicipalities)
                        {
                            if (aMName.Id == itemMunicipalityId)
                            {
                                municipalityName = aMName;
                                break;
                            }
                        }

                        model.CropsXMunicipality.Add(new SubdistributorCropsXMunicipality()
                        {
                            AddressMunicipalityAddressStateId = selectedAddressState
                            ,
                            Crop = distributorController.GetCropById(itemCropId)
                            ,
                            AddressMunicipalityId = itemMunicipalityId
                            ,
                            Municipality = municipalityName
                            ,
                            Cat_CropId = itemCropId
                            ,
                            SubdistributorId = model.Id
                        });
                    }
                }
                TempData["CropsXMunicipality"] = model.CropsXMunicipality;
                ViewBag.AddressStates          = AddressStates();
                return(View(model));

            case "createDistributor":
                model.Type = selectedType;

                List <Model.SubdistributorCommercialName> businesses = new List <SubdistributorCommercialName>();
                bool isFirstCommercialName = true;
                foreach (string item in selectedCommercialNames.Split('/'))
                {
                    businesses.Add(new SubdistributorCommercialName()
                    {
                        Name = item, SubdistributorId = model.Id, IsMain = isFirstCommercialName
                    });
                    isFirstCommercialName = false;
                }
                model.CommercialNames = businesses;


                if (controller.CreateSubdistributor(model) && controller.ResultManager.IsCorrect)
                {
                    NotifyUser(resultManager: controller.ResultManager);
                    return(RedirectToAction("Index"));
                }
                else
                {
                    NotifyUser(resultManager: controller.ResultManager);
                }
                break;

            default:
                break;
            }
            ViewBag.AddressStates = AddressStates();
            return(View(model));
        }