public bool ZoneAdd(Cat_Zone item)
        {
            ResultManager.IsCorrect = false;

            //initial validations
            //-sys validations
            if (item == null)
            {
                ResultManager.Add(ErrorDefault, Trace + "ZoneAdd.111 No se recibio el objeto de la zona a agregar");
                return(false);
            }
            //-business validations
            if (string.IsNullOrWhiteSpace(item.Name))
            {
                ResultManager.Add("El nombre de la zona no puede estar vacio");
                return(false);
            }

            //insert new item
            try
            {
                Cat_Zone auxZone = new Cat_Zone();
                auxZone.Name       = item.Name;
                auxZone.RegionName = item.RegionName;
                Repository.Zones.Add(auxZone);
                Repository.Complete();

                // update AddressMunicipality table
                foreach (AddressMunicipality municipality in item.AddressMunicipalities)
                {
                    AddressMunicipality aux = Repository.AddressMunicipalities
                                              .GetAll()
                                              .Where(x => x.Id == municipality.Id)
                                              .FirstOrDefault();
                    aux.Cat_ZoneId = auxZone.Id;
                }
                Repository.Complete();

                ResultManager.IsCorrect = true;
                return(true);
            }
            catch (Exception ex)
            {
                ResultManager.Add(ErrorDefault, Trace + "ZoneAdd.511 Excepción al agregar la zona nombre '" + item.Name + "': " + ex.Message);
            }
            return(false);
        }
        public ActionResult DistributorCreate(DistributorEmployee model, 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
                ICollection <DistributorCropsXMunicipality> auxCropsXMunicipality = (ICollection <DistributorCropsXMunicipality>)TempData["CropsXMunicipality"];
                foreach (DistributorCropsXMunicipality item in auxCropsXMunicipality)
                {
                    model.Distributor.CropsXMunicipality.Add(new DistributorCropsXMunicipality()
                    {
                        AddressMunicipalityAddressStateId = item.AddressMunicipalityAddressStateId,
                        AddressMunicipalityId             = item.AddressMunicipalityId,
                        Cat_CropId = item.Cat_CropId,
                        //Crop = item.Crop,
                        //Municipality = item.Municipality
                    });
                }
                auxCropsXMunicipality          = null;
                TempData["CropsXMunicipality"] = model.Distributor.CropsXMunicipality;
            }

            List <AddressMunicipality> auxMunicipalities;

            switch (postAction)
            {
            case "getMunicipalities":     //get municipalities for the selected addressState
                auxMunicipalities = controller.GetMunicipalitiesByState(selectedAddressState);
                //ViewBag.SelectedPostalCode = auxPostalCode;
                ViewBag.AvailableMunicipalities = auxMunicipalities;
                ViewBag.AddressStates           = AddressStates(selectedAddressState);
                return(View(model));

            case "addInfluence":     //add "area de influencia" to the list
                auxMunicipalities = controller.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.Distributor.CropsXMunicipality.Add(new DistributorCropsXMunicipality()
                        {
                            AddressMunicipalityAddressStateId = selectedAddressState
                            ,
                            Crop = controller.GetCropById(itemCropId)
                            ,
                            AddressMunicipalityId = itemMunicipalityId
                            ,
                            Municipality = municipalityName
                            ,
                            Cat_CropId = itemCropId
                            ,
                            DistributorId = model.DistributorId
                        });
                        TempData["CropsXMunicipality"] = model.Distributor.CropsXMunicipality;
                    }
                }
                ViewBag.AddressStates = AddressStates();
                return(View(model));

            case "createDistributor":
                if (controller.CreateDistributor(model) && controller.ResultManager.IsCorrect)
                {
                    NotifyUser(messageOk: controller.ResultManager.ToDescriptionString(), messageDebug: controllerTraceId + "EmployeeCreate.831 employee created", resultDetails: controller.ErrorManager.ToStringList());
                    return(RedirectToAction("Index"));
                }
                break;

            default:
                break;
            }
            ViewBag.AddressStates = AddressStates();
            NotifyUser(messageError: controller.ResultManager.ToDescriptionString(), messageDebug: controllerTraceId + "EmployeeCreate.811", resultDetails: controller.ErrorManager.ToStringList());
            return(View(model));
        }
        public ActionResult Edit(DistributorEmployee model, 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)
            {
                model.Distributor.CropsXMunicipality = (ICollection <DistributorCropsXMunicipality>)TempData["CropsXMunicipality"];
                TempData["CropsXMunicipality"]       = model.Distributor.CropsXMunicipality;
            }

            List <AddressMunicipality> auxMunicipalities;

            switch (postAction)
            {
            case "getMunicipalities":     //get municipalities for the selected addressState
                auxMunicipalities = controller.GetMunicipalitiesByState(selectedAddressState);
                //ViewBag.SelectedPostalCode = auxPostalCode;
                ViewBag.AvailableMunicipalities = auxMunicipalities;
                ViewBag.AddressStates           = AddressStates(selectedAddressState);
                model.Distributor.Address       = controller.Repository.Addresses.Get((int)model.Distributor.AddressId);
                return(View(model));

            case "addInfluence":     //add "area de influencia" to the list
                auxMunicipalities = controller.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.Distributor.CropsXMunicipality.Add(new DistributorCropsXMunicipality()
                        {
                            AddressMunicipalityAddressStateId = selectedAddressState
                            ,
                            Crop = controller.GetCropById(itemCropId)
                            ,
                            AddressMunicipalityId = itemMunicipalityId
                            ,
                            Municipality = municipalityName
                            ,
                            Cat_CropId = itemCropId
                            ,
                            DistributorId = model.DistributorId
                        });
                        TempData["CropsXMunicipality"] = model.Distributor.CropsXMunicipality;
                    }
                }
                ViewBag.AddressStates     = AddressStates();
                model.Distributor.Address = controller.Repository.Addresses.Get((int)model.Distributor.AddressId);
                return(View(model));

            case "updateDistributor":
                PSD.Controller.AccountController accountController = new PSD.Controller.AccountController(Configurations);
                if (accountController.UpdateDistributorEmployee(model) && accountController.ResultManager.IsCorrect)
                {
                    NotifyUser(messageOk: accountController.ResultManager.ToDescriptionString(), messageDebug: controllerTraceId + "EmployeeCreate.831 distributor updated", resultDetails: controller.ErrorManager.ToStringList());
                    return(RedirectToAction("Index"));
                }
                break;

            default:
                break;
            }
            ViewBag.AddressStates     = AddressStates();
            model.Distributor.Address = controller.Repository.Addresses.Get((int)model.Distributor.AddressId);
            NotifyUser(messageError: controller.ResultManager.ToDescriptionString(), messageDebug: controllerTraceId + "EmployeeCreate.811", resultDetails: controller.ErrorManager.ToStringList());
            return(View(model));
        }
        public bool ZoneUpdate(Cat_Zone item)
        {
            ResultManager.IsCorrect = false;

            //initial validations
            //-sys validations
            if (item == null)
            {
                ResultManager.Add(ErrorDefault, Trace + "ZoneEdit.111 No se recibio el objeto zona a editar");
                return(false);
            }
            if (item.Id == -1 || item.Id == 0)
            {//no crop id was received
                ResultManager.Add(ErrorDefault, Trace + "ZoneEdit.131 No se recibio el id de la zona a editar");
                return(false);
            }
            //-business validations
            if (string.IsNullOrWhiteSpace(item.Name))
            {
                ResultManager.Add("El nombre de la zona no puede estar vacio");
                return(false);
            }

            //update item
            try
            {
                Cat_Zone auxZone = Repository.Zones.Get(item.Id);
                if (auxZone == null)
                {
                    ResultManager.Add(ErrorDefault, Trace + "ZoneEdit.311 Zona con id '" + item.Id + "' no encontrado en bd");
                    return(false);
                }
                auxZone.Name       = item.Name;
                auxZone.RegionName = item.RegionName;

                // first remove municipalities related to this zone
                List <AddressMunicipality> auxMunicipalities = Repository.AddressMunicipalities
                                                               .GetAll()
                                                               .Where(x => x.Cat_ZoneId == item.Id)
                                                               .ToList();

                foreach (AddressMunicipality municipality in auxMunicipalities)
                {
                    municipality.Cat_ZoneId = null;
                }

                // update AddressMunicipality table
                foreach (AddressMunicipality municipality in item.AddressMunicipalities)
                {
                    AddressMunicipality aux = Repository.AddressMunicipalities
                                              .GetAll()
                                              .Where(x => x.Id == municipality.Id)
                                              .FirstOrDefault();
                    aux.Cat_ZoneId = auxZone.Id;
                }

                Repository.Complete();
                ResultManager.IsCorrect = true;
                return(true);
            }
            catch (Exception ex)
            {
                ResultManager.Add(ErrorDefault, Trace + "ZoneEdit.511 Excepción al editar el cultivo con id '" + item.Id + "': " + ex.Message);
            }
            return(false);
        }
Beispiel #5
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));
        }