Exemple #1
0
        public decimal?GetDefaultYieldByCropId(FarmDetails farmDetails, int _cropid, bool useBushelPerAcreUnits)
        {
            decimal?defaultYield = null;
            int     _locationid;

            if (farmDetails.FarmRegion.HasValue)
            {
                _locationid = _sd.GetRegion(farmDetails.FarmRegion.Value).LocationId;
                CropYield cy = _sd.GetCropYield(_cropid, _locationid);
                if (cy != null && cy.Amount.HasValue)
                {
                    defaultYield = cy.Amount.Value;
                }
                if (useBushelPerAcreUnits && defaultYield.HasValue)
                {
                    //E07US18 - convert to bushels per acre
                    Crop crop = _sd.GetCrop(_cropid);
                    if (crop.HarvestBushelsPerTon.HasValue)
                    {
                        defaultYield = defaultYield * crop.HarvestBushelsPerTon;
                    }
                }
            }

            return(defaultYield);
        }
Exemple #2
0
        public void ShouldAddNewCropYield()
        {
            // 1. Arrange

            CropYield expectedCropYield = new CropYield
            {
                CropYieldID      = 5,
                ProductionAmount = 400,
                ProductionYear   = 2019,
                CropID           = 1,
                FarmID           = 1
            };

            CropYield addedCropYield = null;

            mockCropYieldRepo.Setup(m => m.AddCropYield(It.IsAny <CropYield>())).Returns(Task.CompletedTask).Callback <CropYield>(m => addedCropYield = m);

            // 2. Act

            var actualAddedCrop = cropsController.AddCropYieldHelper(expectedCropYield);

            // 3. Assert
            Assert.Equal(expectedCropYield.CropYieldID, addedCropYield.CropYieldID);
            Assert.Equal(expectedCropYield, addedCropYield);
        }
Exemple #3
0
        public Task DeleteCropYield(int?cropYieldID)
        {
            CropYield cropYield = database.CropYields.Find(cropYieldID);

            database.CropYields.Remove(cropYield);

            return(database.SaveChangesAsync());
        }
Exemple #4
0
        public IQueryable <CropYield> PopulateCropYields()
        {
            List <CropYield> cropYieldsList = new List <CropYield>();

            CropYield cropYield = new CropYield
            {
                CropYieldID      = 1,
                ProductionAmount = 400,
                ProductionYear   = 2019,
                CropID           = 1,
                FarmID           = 1
            };

            cropYieldsList.Add(cropYield);

            cropYield = new CropYield
            {
                CropYieldID      = 2,
                ProductionAmount = 500,
                ProductionYear   = 2019,
                CropID           = 2,
                FarmID           = 2
            };
            cropYieldsList.Add(cropYield);

            cropYield = new CropYield
            {
                CropYieldID      = 3,
                ProductionAmount = 500,
                ProductionYear   = 2018,
                CropID           = 3,
                FarmID           = 1
            };
            cropYieldsList.Add(cropYield);

            cropYield = new CropYield
            {
                CropYieldID      = 4,
                ProductionAmount = 500,
                ProductionYear   = 2019,
                CropID           = 1,
                FarmID           = 2
            };
            cropYieldsList.Add(cropYield);

            return(cropYieldsList.AsQueryable <CropYield>());
        }
Exemple #5
0
        public decimal?GetDefaultYieldByCropId(int _cropid)
        {
            decimal?defaultYield = null;
            int     _locationid;

            if (_ud.FarmDetails().farmRegion.HasValue)
            {
                _locationid = _sd.GetRegion(_ud.FarmDetails().farmRegion.Value).locationid;
                CropYield cy = _sd.GetCropYield(_cropid, _locationid);
                if (cy.amt.HasValue)
                {
                    defaultYield = cy.amt.Value;
                }
            }

            return(defaultYield);
        }
Exemple #6
0
        public List <CropYield> GetCropYields()
        {
            var array      = (JArray)rss["agri"]["nmp"]["cropyields"]["cropyield"];
            var cropYields = new List <CropYield>();

            foreach (var r in array)
            {
                var cropYield = new CropYield()
                {
                    CropId     = Convert.ToInt32(r["cropid"].ToString()),
                    LocationId = Convert.ToInt32(r["locationid"].ToString()),
                    Amount     = r["amt"].ToString() == "null"
                        ? (decimal?)null
                        : Convert.ToDecimal(r["amt"].ToString())
                };

                cropYields.Add(cropYield);
            }

            return(cropYields);
        }
Exemple #7
0
        public Task EditCropYield(CropYield cropYield)
        {
            database.CropYields.Update(cropYield);

            return(database.SaveChangesAsync());
        }
Exemple #8
0
        public Task AddCropYield(CropYield cropYield)
        {
            database.CropYields.AddAsync(cropYield);

            return(database.SaveChangesAsync());
        }
        public async Task <IActionResult> AddCropYield(CropYield cropYield)
        {
            await AddCropYieldHelper(cropYield);

            return(RedirectToAction("ListAllCrops"));
        }
 public async Task AddCropYieldHelper(CropYield cropYield)
 {
     await cropYieldRepoInterface.AddCropYield(cropYield);
 }
        // Instance or object method
        // Crop crop = new Crop();
        // crop.FindCrop();

        // Class or static method
        //Crop.FindCrop();

        public async static Task Initialize(IServiceProvider services)
        {
            // Services
            ApplicationDbContext          database    = services.GetRequiredService <ApplicationDbContext>();
            UserManager <ApplicationUser> userManager = services.GetRequiredService <UserManager <ApplicationUser> >();
            RoleManager <IdentityRole>    roleManager = services.GetRequiredService <RoleManager <IdentityRole> >();

            // Farms
            if (!database.Farms.Any())
            {
                Farm farm = new Farm
                {
                    FarmName    = "Grow OV",
                    FarmAddress = "1006 Grandview St, Wheeling, WV 26003",
                    FarmSize    = 5500
                };
                database.Farms.Add(farm);

                farm = new Farm
                {
                    FarmName    = "Bluebird",
                    FarmAddress = "190 Alamo Rd SE, Carrollton, OH 44615",
                    FarmSize    = 3000
                };
                database.Farms.Add(farm);

                farm = new Farm
                {
                    FarmName    = "Family Roots",
                    FarmAddress = "245 Hervey Ln, Wellsburg, WV 26070",
                    FarmSize    = 1000
                };
                database.Farms.Add(farm);

                database.Farms.Add(farm);

                farm = new Farm
                {
                    FarmName    = "Oak Hill",
                    FarmAddress = "37 Old Trails Rd. Avella, PA 15312",
                    FarmSize    = 1500
                };
                database.Farms.Add(farm);

                database.SaveChanges();
            }

            // Role strings
            string roleAnalyst = "Analyst";
            string roleFarmer  = "Farmer";

            // Roles
            if (!database.Roles.Any())
            {
                IdentityRole role = new IdentityRole(roleAnalyst);
                await roleManager.CreateAsync(role);

                role = new IdentityRole(roleFarmer);
                await roleManager.CreateAsync(role);

                database.SaveChanges();
            }
            // Users
            if (!database.ApplicationUsers.Any())
            {
                ApplicationUser applicationUser = new Analyst("Test", "Analyst1", "*****@*****.**", "304-000-0001", "TestAnalyst1");
                applicationUser.Id = "1";
                await userManager.CreateAsync(applicationUser);

                await userManager.AddToRoleAsync(applicationUser, roleAnalyst);

                applicationUser    = new Analyst("Test", "Analyst2", "*****@*****.**", "304-000-0001", "TestAnalyst1");
                applicationUser.Id = "2";
                await userManager.CreateAsync(applicationUser);

                await userManager.AddToRoleAsync(applicationUser, roleAnalyst);

                applicationUser = new Farmer("Test", "Farmer1", "*****@*****.**", "304-000-0002", "TestFarmer1", 1);
                await userManager.CreateAsync(applicationUser);

                await userManager.AddToRoleAsync(applicationUser, roleFarmer);

                applicationUser = new Farmer("Test", "Farmer2", "*****@*****.**", "304-000-0003", "TestFarmer2", 2);
                await userManager.CreateAsync(applicationUser);

                await userManager.AddToRoleAsync(applicationUser, roleFarmer);

                // database.SaveChanges();
            }
            // Classifications
            if (!database.Classifications.Any())
            {
                Classification classification = new Classification
                {
                    ClassificationName = "Fruit"
                };
                database.Classifications.Add(classification);

                classification = new Classification
                {
                    ClassificationName = "Vegetable"
                };
                database.Classifications.Add(classification);

                classification = new Classification
                {
                    ClassificationName = "Herb"
                };
                database.Classifications.Add(classification);

                database.SaveChanges();
            }
            // Crops
            if (!database.Crops.Any())
            {
                Crop crop = new Crop
                {
                    CropName         = "Apple",
                    CropVariety      = null,
                    ClassificationID = 1
                };
                database.Crops.Add(crop);

                crop = new Crop
                {
                    CropName         = "Bok Choi",
                    CropVariety      = null,
                    ClassificationID = 2
                };
                database.Crops.Add(crop);

                crop = new Crop
                {
                    CropName         = "Basil",
                    CropVariety      = null,
                    ClassificationID = 3
                };
                database.Crops.Add(crop);

                crop = new Crop
                {
                    CropName         = "Beans",
                    CropVariety      = "Green",
                    ClassificationID = 2
                };
                database.Crops.Add(crop);

                crop = new Crop
                {
                    CropName         = "Beans",
                    CropVariety      = "Roma",
                    ClassificationID = 2
                };
                database.Crops.Add(crop);

                crop = new Crop
                {
                    CropName         = "Beets",
                    CropVariety      = null,
                    ClassificationID = 2
                };
                database.Crops.Add(crop);

                crop = new Crop
                {
                    CropName         = "Berries",
                    CropVariety      = "Mixed",
                    ClassificationID = 1
                };
                database.Crops.Add(crop);

                crop = new Crop
                {
                    CropName         = "BlueBerries",
                    CropVariety      = null,
                    ClassificationID = 1
                };
                database.Crops.Add(crop);

                crop = new Crop
                {
                    CropName         = "Broccoli",
                    CropVariety      = null,
                    ClassificationID = 2
                };
                database.Crops.Add(crop);

                crop = new Crop
                {
                    CropName         = "Brussel sprouts",
                    CropVariety      = null,
                    ClassificationID = 2
                };
                database.Crops.Add(crop);

                crop = new Crop
                {
                    CropName         = "Cabbage",
                    CropVariety      = "Green",
                    ClassificationID = 2
                };
                database.Crops.Add(crop);

                crop = new Crop
                {
                    CropName         = "Cabbage",
                    CropVariety      = "Red",
                    ClassificationID = 2
                };
                database.Crops.Add(crop);

                crop = new Crop
                {
                    CropName         = "Carrot",
                    CropVariety      = null,
                    ClassificationID = 2
                };
                database.Crops.Add(crop);

                crop = new Crop
                {
                    CropName         = "Cauliflower",
                    CropVariety      = null,
                    ClassificationID = 2
                };
                database.Crops.Add(crop);

                crop = new Crop
                {
                    CropName         = "Celeriac",
                    CropVariety      = null,
                    ClassificationID = 2
                };
                database.Crops.Add(crop);

                crop = new Crop
                {
                    CropName         = "Celery",
                    CropVariety      = null,
                    ClassificationID = 2
                };
                database.Crops.Add(crop);

                crop = new Crop
                {
                    CropName         = "Chard",
                    CropVariety      = null,
                    ClassificationID = 2
                };
                database.Crops.Add(crop);

                crop = new Crop
                {
                    CropName         = "Cilantro",
                    CropVariety      = null,
                    ClassificationID = 3
                };
                database.Crops.Add(crop);

                crop = new Crop
                {
                    CropName         = "Collards",
                    CropVariety      = null,
                    ClassificationID = 2
                };
                database.Crops.Add(crop);

                database.SaveChanges();
            }
            // CanProduce
            if (!database.CanProduce.Any())
            {
                CanProduce produce = new CanProduce
                {
                    CropID = 1,
                    FarmID = 1
                };
                database.CanProduce.Add(produce);

                produce = new CanProduce
                {
                    CropID = 17,
                    FarmID = 1
                };
                database.CanProduce.Add(produce);

                produce = new CanProduce
                {
                    CropID = 16,
                    FarmID = 1
                };
                database.CanProduce.Add(produce);

                produce = new CanProduce
                {
                    CropID = 15,
                    FarmID = 1
                };
                database.CanProduce.Add(produce);

                produce = new CanProduce
                {
                    CropID = 11,
                    FarmID = 1
                };
                database.CanProduce.Add(produce);

                produce = new CanProduce
                {
                    CropID = 18,
                    FarmID = 1
                };
                database.CanProduce.Add(produce);

                produce = new CanProduce
                {
                    CropID = 10,
                    FarmID = 1
                };
                database.CanProduce.Add(produce);

                produce = new CanProduce
                {
                    CropID = 8,
                    FarmID = 1
                };
                database.CanProduce.Add(produce);

                produce = new CanProduce
                {
                    CropID = 6,
                    FarmID = 1
                };
                database.CanProduce.Add(produce);

                produce = new CanProduce
                {
                    CropID = 5,
                    FarmID = 1
                };
                database.CanProduce.Add(produce);

                produce = new CanProduce
                {
                    CropID = 1,
                    FarmID = 2
                };
                database.CanProduce.Add(produce);

                produce = new CanProduce
                {
                    CropID = 17,
                    FarmID = 2
                };
                database.CanProduce.Add(produce);

                produce = new CanProduce
                {
                    CropID = 16,
                    FarmID = 2
                };
                database.CanProduce.Add(produce);

                produce = new CanProduce
                {
                    CropID = 15,
                    FarmID = 2
                };
                database.CanProduce.Add(produce);

                produce = new CanProduce
                {
                    CropID = 14,
                    FarmID = 2
                };
                database.CanProduce.Add(produce);

                produce = new CanProduce
                {
                    CropID = 15,
                    FarmID = 3
                };
                database.CanProduce.Add(produce);

                produce = new CanProduce
                {
                    CropID = 11,
                    FarmID = 3
                };
                database.CanProduce.Add(produce);

                produce = new CanProduce
                {
                    CropID = 18,
                    FarmID = 3
                };
                database.CanProduce.Add(produce);

                produce = new CanProduce
                {
                    CropID = 10,
                    FarmID = 3
                };
                database.CanProduce.Add(produce);

                produce = new CanProduce
                {
                    CropID = 8,
                    FarmID = 4
                };
                database.CanProduce.Add(produce);

                produce = new CanProduce
                {
                    CropID = 6,
                    FarmID = 4
                };
                database.CanProduce.Add(produce);

                produce = new CanProduce
                {
                    CropID = 5,
                    FarmID = 4
                };
                database.CanProduce.Add(produce);

                database.SaveChanges();
            }
            // Forecasts
            if (!database.Forecasts.Any())
            {
                Forecast forecast = new Forecast
                {
                    StartDate      = new DateTime(2019, 2, 17),
                    EndDate        = new DateTime(2019, 2, 23),
                    ForecastAmount = 85,
                    ActualSales    = 80,
                    CropID         = 1,
                    Id             = "1"
                };
                database.Forecasts.Add(forecast);

                forecast = new Forecast
                {
                    StartDate      = new DateTime(2019, 2, 24),
                    EndDate        = new DateTime(2019, 3, 2),
                    ForecastAmount = 100,
                    ActualSales    = 90,
                    CropID         = 1,
                    Id             = "1"
                };
                database.Forecasts.Add(forecast);

                forecast = new Forecast
                {
                    StartDate      = new DateTime(2019, 3, 3),
                    EndDate        = new DateTime(2019, 3, 9),
                    ForecastAmount = 120,
                    ActualSales    = null,
                    CropID         = 1,
                    Id             = "1"
                };
                database.Forecasts.Add(forecast);

                forecast = new Forecast
                {
                    StartDate      = new DateTime(2019, 2, 17),
                    EndDate        = new DateTime(2019, 2, 23),
                    ForecastAmount = 15,
                    ActualSales    = 15,
                    CropID         = 17,
                    Id             = "1"
                };
                database.Forecasts.Add(forecast);

                forecast = new Forecast
                {
                    StartDate      = new DateTime(2019, 2, 24),
                    EndDate        = new DateTime(2019, 3, 2),
                    ForecastAmount = 20,
                    ActualSales    = 20,
                    CropID         = 17,
                    Id             = "1"
                };
                database.Forecasts.Add(forecast);

                forecast = new Forecast
                {
                    StartDate      = new DateTime(2019, 3, 3),
                    EndDate        = new DateTime(2019, 3, 9),
                    ForecastAmount = 25,
                    ActualSales    = null,
                    CropID         = 17,
                    Id             = "2"
                };
                database.Forecasts.Add(forecast);

                forecast = new Forecast
                {
                    StartDate      = new DateTime(2019, 2, 17),
                    EndDate        = new DateTime(2019, 2, 23),
                    ForecastAmount = 3,
                    ActualSales    = 2,
                    CropID         = 16,
                    Id             = "2"
                };
                database.Forecasts.Add(forecast);

                forecast = new Forecast
                {
                    StartDate      = new DateTime(2019, 2, 24),
                    EndDate        = new DateTime(2019, 3, 2),
                    ForecastAmount = 4,
                    ActualSales    = 3,
                    CropID         = 16,
                    Id             = "2"
                };
                database.Forecasts.Add(forecast);

                forecast = new Forecast
                {
                    StartDate      = new DateTime(2019, 3, 3),
                    EndDate        = new DateTime(2019, 3, 9),
                    ForecastAmount = 4,
                    ActualSales    = null,
                    CropID         = 16,
                    Id             = "2"
                };
                database.Forecasts.Add(forecast);

                forecast = new Forecast
                {
                    StartDate      = new DateTime(2019, 2, 17),
                    EndDate        = new DateTime(2019, 2, 23),
                    ForecastAmount = 4,
                    ActualSales    = 5,
                    CropID         = 13,
                    Id             = "2"
                };
                database.Forecasts.Add(forecast);

                forecast = new Forecast
                {
                    StartDate      = new DateTime(2019, 2, 24),
                    EndDate        = new DateTime(2019, 3, 2),
                    ForecastAmount = 4,
                    ActualSales    = 3,
                    CropID         = 13,
                    Id             = "2"
                };
                database.Forecasts.Add(forecast);

                database.SaveChanges();
            }
            // CropYield
            if (!database.CropYields.Any())
            {
                CropYield cropYield = new CropYield
                {
                    ProductionAmount = 400,
                    ProductionYear   = 2019,
                    CropID           = 1,
                    FarmID           = 1
                };
                database.CropYields.Add(cropYield);

                cropYield = new CropYield
                {
                    ProductionAmount = 500,
                    ProductionYear   = 2019,
                    CropID           = 2,
                    FarmID           = 2
                };
                database.CropYields.Add(cropYield);

                database.SaveChanges();
            }
        }
Exemple #12
0
        public CropRequirementRemoval GetCropRequirementRemoval(
            int cropid,
            decimal yield,
            decimal?crudeProtien,
            bool?coverCropHarvested,
            int nCredit,
            int regionId,
            Field field)
        {
            ConversionFactor _cf = _sd.GetConversionFactor();

            var  crr  = new CropRequirementRemoval();
            Crop crop = _sd.GetCrop(cropid);

            decimal n_removal;

            //      For testing we're using Soil Test Kelowna of 65ppm, should actually by 100+ (say 101)
            //
            //      Nutrient removal
            //          P2O5 = crop.cropremovalfactor_P2O5 * yield
            //          K2O = crop.cropremovalfactor_K2O * yield
            //          N = crop.cropremovalfactor_N * yield
            //          if crude_protien exists and is not default
            //                  crop.cropremovalfactor_N = (crude protien / (0.625 * 0.5))
            //                  N = crop.cropremovalfactor_N * yield
            //           else
            //                  N = crop.cropremovalfactor_N * yield
            //
            //      Note for Cover crops (only)
            //          if Cover Crop harvested
            //              don't change numbers
            //          if Cover crop not harvested
            //              set all removal amts to zero

            if (!crudeProtien.HasValue || crudeProtien.HasValue && crudeProtien.Value == 0)
            {
                decimal tmpDec;
                if (decimal.TryParse(crop.CropRemovalFactorNitrogen.ToString(), out tmpDec))
                {
                    n_removal = tmpDec * yield;
                }
                else
                {
                    n_removal = 0;
                }
            }
            else
            {
                n_removal = decimal.Divide(Convert.ToDecimal(crudeProtien), _cf.NitrogenProteinConversion * _cf.UnitConversion) * yield;
            }

            crr.P2O5_Removal = Convert.ToInt32(crop.CropRemovalFactorP2O5 * yield);
            crr.K2O_Removal  = Convert.ToInt32(crop.CropRemovalFactorK2O * yield);
            crr.N_Removal    = Convert.ToInt32(n_removal);

            if (coverCropHarvested.HasValue && coverCropHarvested.Value == false)
            {
                crr.P2O5_Removal = 0;
                crr.K2O_Removal  = 0;
                crr.N_Removal    = 0;
            }

            //      Crop Requirement
            //          P205
            //              get region.soil_test_phospherous_region_cd
            //              get phosphorous_crop_group_region_cd  = crop_stp_regioncd(cropid, region.soil_test_phospherous_region_cd)
            //              get stp_kelowna_range.id usign default ST Kelowna (65) between range_low and range_high
            //              get P2O5 recommedation  = stp_recommend(stp_kelowna_range.id, region.soil_test_phospherous_region_cd, phosphorous_crop_group_region_cd)
            //
            //          same as above for K2O
            //
            //          For N
            //          Look up crop.n_recommcd
            //          if 0 or 1
            //              N = crop.n_recomm_lbperac
            //          if 2
            //              same as N removal
            //          if 3
            //              get default yield = cropyield(cropid, locationid)
            //              N = (yield / default yield) * crop.n_recomm_lbperac

            var region = _sd.GetRegion(regionId);

            if (field.SoilTest == null)
            {
                field.SoilTest = new SoilTest();
                var dt = _sd.GetDefaultSoilTest();
                field.SoilTest.valNO3H           = dt.Nitrogen;
                field.SoilTest.ValP              = dt.Phosphorous;
                field.SoilTest.valK              = dt.Potassium;
                field.SoilTest.valPH             = dt.pH;
                field.SoilTest.ConvertedKelownaK = dt.ConvertedKelownaK;
                field.SoilTest.ConvertedKelownaP = dt.ConvertedKelownaP;
            }

            int _STP = field.SoilTest.ConvertedKelownaP;

            if (_STP == 0)
            {
                _STP = _cf.DefaultSoilTestKelownaPhosphorous;
            }

            int _STK = field.SoilTest.ConvertedKelownaK;

            if (field.SoilTest.ConvertedKelownaK == 0)
            {
                _STK = _cf.DefaultSoilTestKelownaPotassium;
            }

            // p2o5 recommend calculations
            CropSoilTestPhosphorousRegion cropSTPRegionCd   = _sd.GetCropSTPRegionCd(cropid, region.SoilTestPhosphorousRegionCd);
            int?phosphorous_crop_group_region_cd            = cropSTPRegionCd.PhosphorousCropGroupRegionCode;
            SoilTestPhosphorousKelownaRange sTPKelownaRange = _sd.GetSTPKelownaRangeByPpm(_STP);
            int stp_kelowna_range_id = sTPKelownaRange.Id;

            if (phosphorous_crop_group_region_cd == null)
            {
                crr.P2O5_Requirement = 0;
            }
            else
            {
                SoilTestPhosphorousRecommendation sTPRecommend = _sd.GetSTPRecommend(stp_kelowna_range_id, region.SoilTestPhosphorousRegionCd, Convert.ToInt16(phosphorous_crop_group_region_cd));
                crr.P2O5_Requirement = Convert.ToInt32(Convert.ToDecimal(sTPRecommend.P2O5RecommendationKilogramPerHectare) * _cf.KilogramPerHectareToPoundPerAcreConversion);
            }

            // k2o recommend calculations
            CropSoilTestPotassiumRegion cropSTKRegionCd   = _sd.GetCropSTKRegionCd(cropid, region.SoilTestPotassiumRegionCd);
            int?potassium_crop_group_region_cd            = cropSTKRegionCd?.PotassiumCropGroupRegionCode;
            SoilTestPotassiumKelownaRange sTKKelownaRange = _sd.GetSTKKelownaRangeByPpm(_STK);
            int stk_kelowna_range_id = sTKKelownaRange.Id;

            if (potassium_crop_group_region_cd == null)
            {
                crr.K2O_Requirement = 0;
            }
            else
            {
                SoilTestPotassiumRecommendation sTKRecommend = _sd.GetSTKRecommend(stk_kelowna_range_id, region.SoilTestPotassiumRegionCd, Convert.ToInt16(potassium_crop_group_region_cd));
                crr.K2O_Requirement = Convert.ToInt32(Convert.ToDecimal(sTKRecommend.K2ORecommendationKilogramPerHectare) * _cf.KilogramPerHectareToPoundPerAcreConversion);
            }

            // n recommend calculations -note the excel n_recommd are zero based, the static data is 1 based
            switch (crop.NitrogenRecommendationId)
            {
            case 1:
                crr.N_Requirement = Convert.ToInt16(crop.NitrogenRecommendationPoundPerAcre);
                break;

            case 2:
                crr.N_Requirement = Convert.ToInt16(crop.NitrogenRecommendationPoundPerAcre);
                break;

            case 3:
                crr.N_Requirement = crr.N_Removal;
                break;

            case 4:
                CropYield cropYield = _sd.GetCropYield(cropid, region.LocationId);
                if (cropYield?.Amount != null)
                {
                    crr.N_Requirement = Convert.ToInt16(decimal.Divide(yield, Convert.ToDecimal(cropYield.Amount)) * crop.NitrogenRecommendationPoundPerAcre);
                }
                else
                {
                    crr.N_Requirement = 0;
                }
                break;
            }

            // if a previous crop has been ploughed dowm account for the N in the field (passed in as a credit)
            crr.N_Requirement = crr.N_Requirement - nCredit;

            // only reduce to 0
            crr.N_Requirement = crr.N_Requirement < 0 ? 0 : crr.N_Requirement;

            return(crr);
        }
        public CropRequirementRemoval GetCropRequirementRemoval()
        {
            decimal n_removal = 0;

            ConversionFactor _cf = _sd.GetConversionFactor();

            CropRequirementRemoval crr = new CropRequirementRemoval();
            Crop crop = _sd.GetCrop(cropid);

            //      For testing we're using Soil Test Kelowna of 65ppm, should actually by 100+ (say 101)
            //
            //      Nutrient removal
            //          P2O5 = crop.cropremovalfactor_P2O5 * yield
            //          K2O = crop.cropremovalfactor_K2O * yield
            //          N = crop.cropremovalfactor_N * yield
            //          if crude_protien exists and is not default
            //                  crop.cropremovalfactor_N = (crude protien / (0.625 * 0.5))
            //                  N = crop.cropremovalfactor_N * yield
            //           else
            //                  N = crop.cropremovalfactor_N * yield
            //
            //      Note for Cover crops (only)
            //          if Cover Crop harvested
            //              don't change numbers
            //          if Cover crop not harvested
            //              set all removal amts to zero

            if (!crudeProtien.HasValue || (crudeProtien.HasValue && crudeProtien.Value == 0))
            {
                decimal tmpDec;
                if (decimal.TryParse(crop.cropremovalfactor_N.ToString(), out tmpDec))
                {
                    n_removal = tmpDec * yield;
                }
                else
                {
                    n_removal = 0;
                }
            }
            else
            {
                n_removal = decimal.Divide(Convert.ToDecimal(crudeProtien), (_cf.n_protein_conversion * _cf.unit_conversion)) * yield;
            }

            crr.P2O5_Removal = Convert.ToInt32(crop.cropremovalfactor_P2O5 * yield);
            crr.K2O_Removal  = Convert.ToInt32(crop.cropremovalfactor_K2O * yield);
            crr.N_Removal    = Convert.ToInt32(n_removal);

            if (coverCropHarvested.HasValue && coverCropHarvested.Value == false)
            {
                crr.P2O5_Removal = 0;
                crr.K2O_Removal  = 0;
                crr.N_Removal    = 0;
            }


            //      Crop Requirement
            //          P205
            //              get region.soil_test_phospherous_region_cd
            //              get phosphorous_crop_group_region_cd  = crop_stp_regioncd(cropid, region.soil_test_phospherous_region_cd)
            //              get stp_kelowna_range.id usign default ST Kelowna (65) between range_low and range_high
            //              get P2O5 recommedation  = stp_recommend(stp_kelowna_range.id, region.soil_test_phospherous_region_cd, phosphorous_crop_group_region_cd)
            //
            //          same as above for K2O
            //
            //          For N
            //          Look up crop.n_recommcd
            //          if 0 or 1
            //              N = crop.n_recomm_lbperac
            //          if 2
            //              same as N removal
            //          if 3
            //              get default yield = cropyield(cropid, locationid)
            //              N = (yield / default yield) * crop.n_recomm_lbperac

            int    regionid = _ud.FarmDetails().farmRegion.Value;
            Region region   = _sd.GetRegion(regionid);

            Field fld = _ud.GetFieldDetails(fieldName);

            if (fld.soilTest == null)
            {
                fld.soilTest = new SoilTest();
                DefaultSoilTest dt = _sd.GetDefaultSoilTest();
                fld.soilTest.valNO3H           = dt.nitrogen;
                fld.soilTest.ValP              = dt.phosphorous;
                fld.soilTest.valK              = dt.potassium;
                fld.soilTest.valPH             = dt.pH;
                fld.soilTest.ConvertedKelownaK = dt.convertedKelownaK;
                fld.soilTest.ConvertedKelownaP = dt.convertedKelownaP;
            }

            int _STP = fld.soilTest.ConvertedKelownaP;

            if (_STP == 0)
            {
                _STP = _cf.defaultSoilTestKelownaP;
            }

            int _STK = fld.soilTest.ConvertedKelownaK;

            if (fld.soilTest.ConvertedKelownaK == 0)
            {
                _STK = _cf.defaultSoilTestKelownaK;
            }

            // p2o5 recommend calculations
            CropSTPRegionCd cropSTPRegionCd = _sd.GetCropSTPRegionCd(cropid, region.soil_test_phospherous_region_cd);
            int?            phosphorous_crop_group_region_cd = cropSTPRegionCd.phosphorous_crop_group_region_cd;
            STPKelownaRange sTPKelownaRange      = _sd.GetSTPKelownaRangeByPpm(_STP);
            int             stp_kelowna_range_id = sTPKelownaRange.id;

            if (phosphorous_crop_group_region_cd == null)
            {
                crr.P2O5_Requirement = 0;
            }
            else
            {
                STPRecommend sTPRecommend = _sd.GetSTPRecommend(stp_kelowna_range_id, region.soil_test_phospherous_region_cd, Convert.ToInt16(phosphorous_crop_group_region_cd));
                crr.P2O5_Requirement = Convert.ToInt32(Convert.ToDecimal(sTPRecommend.p2o5_recommend_kgperha) * _cf.kgperha_lbperac_conversion);
            }

            // k2o recommend calculations
            CropSTKRegionCd cropSTKRegionCd = _sd.GetCropSTKRegionCd(cropid, region.soil_test_potassium_region_cd);
            int?            potassium_crop_group_region_cd = cropSTKRegionCd.potassium_crop_group_region_cd;
            STKKelownaRange sTKKelownaRange      = _sd.GetSTKKelownaRangeByPpm(_STK);
            int             stk_kelowna_range_id = sTKKelownaRange.id;

            if (potassium_crop_group_region_cd == null)
            {
                crr.K2O_Requirement = 0;
            }
            else
            {
                STKRecommend sTKRecommend = _sd.GetSTKRecommend(stk_kelowna_range_id, region.soil_test_potassium_region_cd, Convert.ToInt16(potassium_crop_group_region_cd));
                crr.K2O_Requirement = Convert.ToInt32(Convert.ToDecimal(sTKRecommend.k2o_recommend_kgperha) * _cf.kgperha_lbperac_conversion);
            }

            // n recommend calculations -note the excel n_recommd are zero based, the static data is 1 based
            switch (crop.n_recommcd)
            {
            case 1:
                crr.N_Requirement = Convert.ToInt16(crop.n_recomm_lbperac);
                break;

            case 2:
                crr.N_Requirement = Convert.ToInt16(crop.n_recomm_lbperac);
                break;

            case 3:
                crr.N_Requirement = crr.N_Removal;
                break;

            case 4:
                CropYield cropYield = _sd.GetCropYield(cropid, region.locationid);
                if (cropYield.amt != null)
                {
                    crr.N_Requirement = Convert.ToInt16(decimal.Divide(yield, Convert.ToDecimal(cropYield.amt)) * crop.n_recomm_lbperac);
                }
                else
                {
                    crr.N_Requirement = 0;
                }
                break;
            }

            // if a previous crop has been ploughed dowm account for the N in the field (passed in as a credit)
            crr.N_Requirement = crr.N_Requirement - nCredit;

            // only reduce to 0
            crr.N_Requirement = (crr.N_Requirement < 0) ? 0 : crr.N_Requirement;

            return(crr);
        }