예제 #1
0
        public ActionResult Create([Bind(Include = "Name, Company, Phone, Address, Latitude, Longitude, Has, WeatherStationId, CityId, BombsHidden")] WizardFarmViewModel vm)
        {
            if (ModelState.IsValid)
            {
                Farm farmMapped      = new Farm();
                long lFarmPositionId = GetPositionId(vm.Latitude, vm.Longitude);
                long lBombPositionId;

                Position positionFarm = new Position();
                //Not exist position to farm
                if (lFarmPositionId == 0)
                {
                    positionFarm.Latitude  = vm.Latitude;
                    positionFarm.Longitude = vm.Longitude;
                    positionFarm.Name      = vm.Name + " - Establecimiento";
                    farmMapped.Position    = positionFarm;
                }
                else
                {
                    farmMapped.PositionId = lFarmPositionId;
                }

                // working whit bombs
                dynamic items = JsonConvert.DeserializeObject(vm.BombsHidden);
                foreach (var item in items.items)
                {
                    double bombLatitud   = Convert.ToDouble((String)(item.bombLatitude));
                    double bombLongitude = Convert.ToDouble((String)item.bombLongitude);

                    Bomb newbomb = new Bomb();
                    newbomb.Name         = vm.Name + " - " + item.bombShortName;
                    newbomb.ShortName    = item.bombShortName;
                    newbomb.SerialNumber = item.serialNumber;

                    newbomb.ServiceDate  = ((String)item.serviceDate == "") ? Utils.MIN_DATETIME : item.serviceDate;
                    newbomb.PurchaseDate = ((String)item.purchaseDate == "") ? Utils.MIN_DATETIME : item.purchaseDate;

                    //position bomb is diferent to position farm, find position to bomb
                    if (vm.Latitude != bombLatitud || vm.Longitude != bombLongitude)
                    {
                        lBombPositionId = GetPositionId((double)item.bombLatitude, (double)item.bombLongitude);

                        //Not exist position to bomb
                        if (lBombPositionId == 0)
                        {
                            Position positionBomb = new Position();
                            positionBomb.Latitude  = item.bombLatitude;
                            positionBomb.Longitude = item.bombLongitude;
                            positionBomb.Name      = Convert.ToString(item.bombLatitude) + " - " + Convert.ToString(item.bombLatitude);
                            newbomb.Position       = positionBomb;
                        }
                        else
                        {
                            newbomb.PositionId = lBombPositionId;
                        }
                    }
                    else
                    {
                        newbomb.PositionId = lFarmPositionId;
                    }

                    farmMapped.AddBomb(newbomb);
                }

                farmMapped.Name             = vm.Name;
                farmMapped.Company          = vm.Company;
                farmMapped.Address          = vm.Address;
                farmMapped.Phone            = vm.Phone;
                farmMapped.Has              = vm.Has;
                farmMapped.WeatherStationId = vm.WeatherStationId;
                farmMapped.CityId           = vm.CityId;

                db.Farms.Add(farmMapped);
                db.SaveChanges();
            }

            WizardFarmViewModel vmReturn = new WizardFarmViewModel();

            vmReturn.City           = this.LoadCities();
            vmReturn.WeatherStation = this.LoadWeatherStations();
            return(View("~/Views/Wizard/FarmBomb/Wizard.cshtml", vmReturn));
        }