示例#1
0
        public void DeleteFarmManure(int id)
        {
            FarmData userData = _ctx.HttpContext.Session.GetObjectFromJson <FarmData>("FarmData");

            userData.unsaved = true;
            YearData   yd = userData.years.FirstOrDefault(y => y.year == userData.farmDetails.year);
            FarmManure fm = yd.farmManures.FirstOrDefault(f => f.id == id);

            yd.farmManures.Remove(fm);

            _ctx.HttpContext.Session.SetObjectAsJson("FarmData", userData);
        }
        public NOrganicMineralizations GetNMineralization(int manureid, int locationid)
        {
            NOrganicMineralizations nOrganicMineralizations = new NOrganicMineralizations();

            FarmManure myManure = _ud.GetFarmManure(manureid);

            NMineralization myNMineralization = _sd.GetNMineralization(myManure.nminerizationid, locationid);

            nOrganicMineralizations.OrganicN_FirstYear = myNMineralization.firstyearvalue;
            nOrganicMineralizations.OrganicN_LongTerm  = myNMineralization.longtermvalue;

            return(nOrganicMineralizations);
        }
        public void GetFieldsAppliedWithImportedManure()
        {
            //arrange
            var farmManure = new FarmManure()
            {
                SourceOfMaterialId = "Imported, 1", Stored_Imported = NutrientAnalysisTypes.Imported
            };

            //act
            var result = _yearData.GetFieldsAppliedWithManure(farmManure);

            //assess
            result.ShouldNotBeNull();
        }
示例#4
0
        public void GetFieldsAppliedWithImportedManure()
        {
            //arrange
            var farmManure = new FarmManure()
            {
                sourceOfMaterialId = "Imported, 1", stored_imported = NutrientAnalysisTypes.Imported
            };

            //act
            var result = _yearData.GetFieldsAppliedWithManure(farmManure);

            //assess
            Assert.IsNotNull(result);
        }
        public AppliedManure GetAppliedManure(YearData yearData, FarmManure farmManure)
        {
            AppliedManure appliedManure = null;

            if (farmManure.stored_imported == NutrientAnalysisTypes.Stored)
            {
                //Stored Manure
                appliedManure = GetAppliedStoredManure(yearData, farmManure);
            }
            else if (farmManure.stored_imported == NutrientAnalysisTypes.Imported)
            {
                appliedManure = GetAppliedImportedManure(yearData, farmManure);
            }

            return(appliedManure);
        }
        private AppliedManure GetAppliedCollectedManure(YearData yearData, FarmManure farmManure)
        {
            //Since the manures ared grouped and as long as one is used to identify the field
            //the farmManure is applied, we can determine the FieldAppliedManures
            //We then pass the list of grouped manures in to order to aggregate the total available for application
            var fieldAppliedManures = new List <FieldAppliedManure>();
            var groupedManures      = new List <ManagedManure>();

            var farmAnimals = yearData.FarmAnimals
                              .Where(fa => farmManure.GroupedWithCollectedAnalysisSourceItemIds
                                     .Where(ids => ids.SourceType == NutrientAnalysisTypes.Collected)
                                     .Select(ids => ids.SourceId).Contains(fa.Id.Value))
                              .Select(fa => fa as ManagedManure)
                              .ToList();

            groupedManures.AddRange(farmAnimals);
            var farmAnimalIds = farmAnimals.Select(fa => fa.Id.GetValueOrDefault(0)).ToList();

            fieldAppliedManures.AddRange(GetFieldsAppliedCollectedManureForFarmAnimals(farmAnimalIds, farmManure, yearData));

            var importedManures = yearData.ImportedManures
                                  .Where(imported => farmManure.GroupedWithCollectedAnalysisSourceItemIds
                                         .Where(ids => ids.SourceType == NutrientAnalysisTypes.Imported)
                                         .Select(ids => ids.SourceId).Contains(imported.Id.Value))
                                  .Select(fa => fa as ManagedManure)
                                  .ToList();

            groupedManures.AddRange(importedManures);

            var importedManure = importedManures.FirstOrDefault();

            if (!fieldAppliedManures.Any() && importedManure != null)
            {
                fieldAppliedManures.AddRange(CalculateFieldAppliedImportedManure(yearData, farmManure, importedManure as ImportedManure));
            }

            var appliedCollectedManure = new AppliedGroupedManure(fieldAppliedManures,
                                                                  groupedManures,
                                                                  farmManure.Name)
            {
                ManureMaterialName = farmManure.Name
            };

            return(appliedCollectedManure);
        }
示例#7
0
        public async Task <string> RenderSources()
        {
            ReportSourcesViewModel rvm = new ReportSourcesViewModel();

            rvm.year    = _ud.FarmDetails().year;
            rvm.details = new List <ReportSourcesDetail>();

            List <Field> fldList = _ud.GetFields();

            foreach (var f in fldList)
            {
                if (f.nutrients != null)
                {
                    if (f.nutrients.nutrientManures != null)
                    {
                        foreach (var m in f.nutrients.nutrientManures)
                        {
                            FarmManure          manure = _ud.GetFarmManure(Convert.ToInt32(m.manureId));
                            ReportSourcesDetail rd     = rvm.details.FirstOrDefault(d => d.nutrientName == manure.name);
                            if (rd != null)
                            {
                                rd.nutrientAmount = String.Format((m.rate * f.area) % 1 == 0 ? "{0:#,##0}" : "{0:#,##0.00}", (m.rate * f.area));
                            }
                            else
                            {
                                rd = new ReportSourcesDetail();
                                rd.nutrientName = manure.name;
                                rd.nutrientUnit = _sd.GetUnit(m.unitId).name;
                                int index = rd.nutrientUnit.LastIndexOf("/");
                                if (index > 0)
                                {
                                    rd.nutrientUnit = rd.nutrientUnit.Substring(0, index);
                                }
                                rd.nutrientAmount = String.Format((m.rate * f.area) % 1 == 0 ? "{0:#,##0}" : "{0:#,##0.00}", (m.rate * f.area));
                                rvm.details.Add(rd);
                            }
                        }
                    }
                }
            }

            var result = await _viewRenderService.RenderToStringAsync("~/Views/Report/ReportSources.cshtml", rvm);

            return(result);
        }
        private List <FieldAppliedManure> CalculateFieldAppliedImportedManure(YearData yearData,
                                                                              FarmManure farmManure, ImportedManure importedManure)
        {
            var fieldAppliedManures             = new List <FieldAppliedManure>();
            var fieldsAppliedWithImportedManure = yearData
                                                  .GetFieldsAppliedWithManure(farmManure)
                                                  .Where(f => !fieldAppliedManures.Any(fam => fam.FieldId == f.Id)).ToList();

            foreach (var field in fieldsAppliedWithImportedManure)
            {
                var nutrientManures = field?.Nutrients.nutrientManures
                                      .Where(nm => Convert.ToInt32(nm.manureId) == farmManure.Id)
                                      .ToList() ?? new List <NutrientManure>();

                foreach (var nutrientManure in nutrientManures)
                {
                    var fieldAppliedManure = new FieldAppliedManure()
                    {
                        FieldId = field.Id
                    };
                    if (importedManure.ManureType == ManureMaterialType.Liquid)
                    {
                        var convertedRate = _manureUnitConversionCalculator
                                            .GetLiquidUSGallonsPerAcreApplicationRate(nutrientManure.rate,
                                                                                      (ApplicationRateUnits)Convert.ToInt32(nutrientManure.unitId));

                        fieldAppliedManure.USGallonsApplied =
                            field.Area * convertedRate;
                    }
                    else
                    {
                        var convertedRate = _manureUnitConversionCalculator
                                            .GetSolidsTonsPerAcreApplicationRate(importedManure.Moisture.Value, nutrientManure.rate,
                                                                                 (ApplicationRateUnits)Convert.ToInt32(nutrientManure.unitId));

                        fieldAppliedManure.TonsApplied = field.Area * convertedRate;
                    }

                    fieldAppliedManures.Add(fieldAppliedManure);
                }
            }

            return(fieldAppliedManures);
        }
        public AppliedImportedManure GetAppliedImportedManure(YearData yearData, FarmManure farmManure)
        {
            var fieldsAppliedWithImportedManure = yearData.GetFieldsAppliedWithManure(farmManure);
            var importedManure = yearData.ImportedManures.SingleOrDefault(mss => mss.Id == farmManure?.sourceOfMaterialImportedManureId.Value);
            var farmManureIds  = yearData.GetFarmManureIds(importedManure);

            var fieldAppliedManures = new List <FieldAppliedManure>();

            foreach (var field in fieldsAppliedWithImportedManure)
            {
                var nutrientManures = field.nutrients.nutrientManures
                                      .Where(nm => farmManureIds.Any(fm => fm == Convert.ToInt32(nm.manureId)))
                                      .ToList();

                foreach (var nutrientManure in nutrientManures)
                {
                    var fieldAppliedManure = new FieldAppliedManure();
                    if (importedManure.ManureType == ManureMaterialType.Liquid)
                    {
                        var convertedRate = _manureUnitConversionCalculator
                                            .GetLiquidUSGallonsPerAcreApplicationRate(nutrientManure.rate,
                                                                                      (ApplicationRateUnits)Convert.ToInt32(nutrientManure.unitId));

                        fieldAppliedManure.USGallonsApplied =
                            field.area * convertedRate;
                    }
                    else
                    {
                        var convertedRate = _manureUnitConversionCalculator
                                            .GetSolidsTonsPerAcreApplicationRate(importedManure.Moisture.Value, nutrientManure.rate,
                                                                                 (ApplicationRateUnits)Convert.ToInt32(nutrientManure.unitId));

                        fieldAppliedManure.TonsApplied = field.area * convertedRate;
                    }

                    fieldAppliedManures.Add(fieldAppliedManure);
                }
            }

            var appliedImportedManure = new AppliedImportedManure(fieldAppliedManures, importedManure);

            return(appliedImportedManure);
        }
示例#10
0
        public void AddFarmManure(FarmManure newManure)
        {
            int nextId = 1;

            FarmData userData = _ctx.HttpContext.Session.GetObjectFromJson <FarmData>("FarmData");

            userData.unsaved = true;
            YearData yd = userData.years.FirstOrDefault(y => y.year == userData.farmDetails.year);

            if (yd.farmManures == null)
            {
                yd.farmManures = new List <FarmManure>();
            }

            foreach (var f in yd.farmManures)
            {
                nextId = nextId <= f.id ? f.id + 1 : nextId;
            }
            newManure.id = nextId;

            yd.farmManures.Add(newManure);
            _ctx.HttpContext.Session.SetObjectAsJson("FarmData", userData);
        }
示例#11
0
        public ActionResult CompostDelete(int id, string target)
        {
            CompostDeleteViewModel dvm = new CompostDeleteViewModel();
            bool manureUsed            = false;

            dvm.id     = id;
            dvm.target = target;

            FarmManure nm = _ud.GetFarmManure(id);

            dvm.manureName = nm.name;

            // determine if the selected manure is currently being used on any of the fields
            List <Field> flds = _ud.GetFields();

            foreach (var fld in flds)
            {
                List <NutrientManure> mans = _ud.GetFieldNutrientsManures(fld.fieldName);

                foreach (var man in mans)
                {
                    if (id.ToString() == man.manureId)
                    {
                        manureUsed = true;
                    }
                }
            }

            if (manureUsed)
            {
                dvm.warning = _sd.GetUserPrompt("manuredeletewarning");
            }

            dvm.act = "Delete";

            return(PartialView("CompostDelete", dvm));
        }
示例#12
0
        public void UpdateFarmManure(FarmManure updtMan)
        {
            FarmData userData = _ctx.HttpContext.Session.GetObjectFromJson <FarmData>("FarmData");

            userData.unsaved = true;
            YearData   yd  = userData.years.FirstOrDefault(y => y.year == userData.farmDetails.year);
            FarmManure frm = yd.farmManures.FirstOrDefault(f => f.id == updtMan.id);

            frm.ammonia         = updtMan.ammonia;
            frm.customized      = updtMan.customized;
            frm.dmid            = updtMan.dmid;
            frm.manureId        = updtMan.manureId;
            frm.manure_class    = updtMan.manure_class;
            frm.moisture        = updtMan.moisture;
            frm.name            = updtMan.name;
            frm.nitrate         = updtMan.nitrate;
            frm.nitrogen        = updtMan.nitrogen;
            frm.nminerizationid = updtMan.nminerizationid;
            frm.phosphorous     = updtMan.phosphorous;
            frm.potassium       = updtMan.potassium;
            frm.solid_liquid    = updtMan.solid_liquid;

            _ctx.HttpContext.Session.SetObjectAsJson("FarmData", userData);
        }
示例#13
0
        public IActionResult CompostDetails(CompostDetailViewModel cvm)
        {
            decimal userNitrogen    = 0;
            decimal userAmmonia     = 0;
            decimal userPhosphorous = 0;
            decimal userPotassium   = 0;
            decimal userMoisture    = 0;
            decimal userNitrate     = 0;

            Models.StaticData.Manure man;

            CompostDetailsSetup(ref cvm);

            try
            {
                if (cvm.buttonPressed == "ManureChange")
                {
                    ModelState.Clear();
                    cvm.buttonPressed = "";

                    if (cvm.selManOption != 0)
                    {
                        man = _sd.GetManure(cvm.selManOption.ToString());
                        if (man.manure_class == "Other" ||
                            man.manure_class == "Compost")
                        {
                            cvm.bookValue   = false;
                            cvm.onlyCustom  = true;
                            cvm.nitrogen    = string.Empty;
                            cvm.moisture    = string.Empty;
                            cvm.ammonia     = string.Empty;
                            cvm.nitrate     = string.Empty;
                            cvm.phosphorous = string.Empty;
                            cvm.potassium   = string.Empty;
                            cvm.compost     = man.manure_class == "Compost" ? true : false;
                            cvm.manureName  = cvm.compost ? "Custom - " + man.name + " - " : "Custom - " + man.solid_liquid + " - ";
                        }
                        else
                        {
                            cvm.bookValue   = true;
                            cvm.nitrogen    = man.nitrogen.ToString();
                            cvm.moisture    = man.moisture.ToString();
                            cvm.ammonia     = man.ammonia.ToString();
                            cvm.nitrate     = string.Empty;
                            cvm.phosphorous = man.phosphorous.ToString();
                            cvm.potassium   = man.potassium.ToString();
                            cvm.manureName  = man.name;
                        }
                    }
                    else
                    {
                        cvm.bookValue   = true;
                        cvm.nitrogen    = string.Empty;
                        cvm.moisture    = string.Empty;
                        cvm.ammonia     = string.Empty;
                        cvm.nitrate     = string.Empty;
                        cvm.phosphorous = string.Empty;
                        cvm.potassium   = string.Empty;
                        cvm.manureName  = string.Empty;
                    }
                    return(View(cvm));
                }
                if (cvm.buttonPressed == "TypeChange")
                {
                    ModelState.Clear();
                    cvm.buttonPressed = "";

                    if (cvm.selManOption != 0)
                    {
                        man            = _sd.GetManure(cvm.selManOption.ToString());
                        cvm.onlyCustom = false;
                        if (cvm.bookValue)
                        {
                            cvm.moisture    = cvm.bookValue ? man.moisture.ToString() : "";
                            cvm.nitrogen    = man.nitrogen.ToString();
                            cvm.ammonia     = man.ammonia.ToString();
                            cvm.nitrate     = string.Empty;
                            cvm.phosphorous = man.phosphorous.ToString();
                            cvm.potassium   = man.potassium.ToString();
                            cvm.manureName  = man.name;
                        }
                        else
                        {
                            cvm.nitrogen    = string.Empty;
                            cvm.moisture    = string.Empty;
                            cvm.ammonia     = string.Empty;
                            cvm.nitrate     = string.Empty;
                            cvm.phosphorous = string.Empty;
                            cvm.potassium   = string.Empty;
                            cvm.manureName  = (!cvm.compost) ? "Custom - " + man.name + " - " : "Custom - " + man.solid_liquid + " - ";

                            cvm.moistureBook    = man.moisture.ToString();
                            cvm.nitrogenBook    = man.nitrogen.ToString();
                            cvm.ammoniaBook     = man.ammonia.ToString();
                            cvm.nitrateBook     = string.Empty;
                            cvm.phosphorousBook = man.phosphorous.ToString();
                            cvm.potassiumBook   = man.potassium.ToString();
                        }
                    }
                    else
                    {
                        cvm.nitrogen    = string.Empty;
                        cvm.moisture    = string.Empty;
                        cvm.ammonia     = string.Empty;
                        cvm.nitrate     = string.Empty;
                        cvm.phosphorous = string.Empty;
                        cvm.potassium   = string.Empty;
                        cvm.manureName  = string.Empty;
                    }
                    return(View(cvm));
                }

                if (ModelState.IsValid)
                {
                    man = _sd.GetManure(cvm.selManOption.ToString());

                    if (!cvm.bookValue)
                    {
                        if (string.IsNullOrEmpty(cvm.moisture))
                        {
                            ModelState.AddModelError("moisture", "Required.");
                        }
                        else
                        {
                            if (!Decimal.TryParse(cvm.moisture, out userMoisture))
                            {
                                ModelState.AddModelError("moisture", "Numbers only.");
                            }
                            else
                            {
                                if (userMoisture < 0 || userMoisture > 100)
                                {
                                    ModelState.AddModelError("moisture", "Invalid %.");
                                }
                                else
                                {
                                    if (man.solid_liquid.ToUpper() == "SOLID")
                                    {
                                        if (userMoisture > 80)
                                        {
                                            ModelState.AddModelError("moisture", "Invalid % for solid.");
                                        }
                                    }
                                    else
                                    {
                                        if (userMoisture <= 80)
                                        {
                                            ModelState.AddModelError("moisture", "Invalid % for liquid.");
                                        }
                                    }
                                }
                            }
                        }
                        if (string.IsNullOrEmpty(cvm.nitrogen))
                        {
                            ModelState.AddModelError("nitrogen", "Required.");
                        }
                        else
                        {
                            if (!Decimal.TryParse(cvm.nitrogen, out userNitrogen))
                            {
                                ModelState.AddModelError("nitrogen", "Numbers only.");
                            }
                            else
                            {
                                if (userNitrogen < 0 || userNitrogen > 100)
                                {
                                    ModelState.AddModelError("nitrogen", "Invalid %.");
                                }
                            }
                        }
                        if (string.IsNullOrEmpty(cvm.ammonia))
                        {
                            ModelState.AddModelError("ammonia", "Required.");
                        }
                        else
                        {
                            if (!Decimal.TryParse(cvm.ammonia, out userAmmonia))
                            {
                                ModelState.AddModelError("ammonia", "Numbers only.");
                            }
                        }
                        if (string.IsNullOrEmpty(cvm.phosphorous))
                        {
                            ModelState.AddModelError("phosphorous", "Required.");
                        }
                        else
                        {
                            if (!Decimal.TryParse(cvm.phosphorous, out userPhosphorous))
                            {
                                ModelState.AddModelError("phosphorous", "Numbers only.");
                            }
                            else
                            {
                                if (userPhosphorous < 0 || userPhosphorous > 100)
                                {
                                    ModelState.AddModelError("phosphorous", "Invalid %.");
                                }
                            }
                        }
                        if (string.IsNullOrEmpty(cvm.potassium))
                        {
                            ModelState.AddModelError("potassium", "Required.");
                        }
                        else
                        {
                            if (!Decimal.TryParse(cvm.potassium, out userPotassium))
                            {
                                ModelState.AddModelError("potassium", "Numbers only.");
                            }
                            else
                            {
                                if (userPotassium < 0 || userPotassium > 100)
                                {
                                    ModelState.AddModelError("potassium", "Invalid %.");
                                }
                            }
                        }
                        if (cvm.compost)
                        {
                            if (string.IsNullOrEmpty(cvm.nitrate))
                            {
                                ModelState.AddModelError("nitrate", "Required.");
                            }
                            else
                            {
                                if (!Decimal.TryParse(cvm.nitrate, out userNitrate))
                                {
                                    ModelState.AddModelError("nitrate", "Numbers only.");
                                }
                            }
                        }
                        if (_sd.GetManureByName(cvm.manureName) != null)
                        {
                            ModelState.AddModelError("manureName", "Description cannot match predefined entries.");
                        }
                    }

                    List <FarmManure> manures = _ud.GetFarmManures();
                    foreach (var m in manures)
                    {
                        if (m.customized &&
                            m.name == cvm.manureName &&
                            m.id != cvm.id)
                        {
                            ModelState.AddModelError("manureName", "Descriptions must be unique.");
                            break;
                        }
                    }


                    if (!ModelState.IsValid)
                    {
                        return(View(cvm));
                    }

                    if (cvm.id == null)
                    {
                        FarmManure fm = new FarmManure();
                        if (cvm.bookValue)
                        {
                            fm.manureId   = cvm.selManOption;
                            fm.customized = false;
                        }
                        else
                        {
                            man = _sd.GetManure(cvm.selManOption.ToString());

                            fm.customized      = true;
                            fm.manureId        = cvm.selManOption;
                            fm.ammonia         = Convert.ToInt32(cvm.ammonia);
                            fm.dmid            = man.dmid;
                            fm.manure_class    = man.manure_class;
                            fm.moisture        = cvm.moisture;
                            fm.name            = cvm.manureName;
                            fm.nitrogen        = Convert.ToDecimal(cvm.nitrogen);
                            fm.nminerizationid = man.nminerizationid;
                            fm.phosphorous     = Convert.ToDecimal(cvm.phosphorous);
                            fm.potassium       = Convert.ToDecimal(cvm.potassium);
                            fm.nitrate         = cvm.compost ? Convert.ToDecimal(cvm.nitrate) : (decimal?)null;
                            fm.solid_liquid    = man.solid_liquid;
                        }


                        _ud.AddFarmManure(fm);
                    }
                    else
                    {
                        FarmManure fm = _ud.GetFarmManure(cvm.id.Value);
                        if (cvm.bookValue)
                        {
                            fm            = new FarmManure();
                            fm.id         = cvm.id.Value;
                            fm.manureId   = cvm.selManOption;
                            fm.customized = false;
                        }
                        else
                        {
                            man = _sd.GetManure(cvm.selManOption.ToString());

                            fm.customized      = true;
                            fm.manureId        = cvm.selManOption;
                            fm.ammonia         = Convert.ToInt32(cvm.ammonia);
                            fm.dmid            = man.dmid;
                            fm.manure_class    = man.manure_class;
                            fm.moisture        = cvm.moisture;
                            fm.name            = cvm.manureName;
                            fm.nitrogen        = Convert.ToDecimal(cvm.nitrogen);
                            fm.nminerizationid = man.nminerizationid;
                            fm.phosphorous     = Convert.ToDecimal(cvm.phosphorous);
                            fm.potassium       = Convert.ToDecimal(cvm.potassium);
                            fm.solid_liquid    = man.solid_liquid;
                            fm.nitrate         = cvm.compost ? Convert.ToDecimal(cvm.nitrate) : (decimal?)null;
                        }

                        _ud.UpdateFarmManure(fm);

                        ReCalculateManure(fm.id);
                    }

                    string url = Url.Action("RefreshCompostList", "Manure");
                    return(Json(new { success = true, url = url, target = cvm.target }));
                }
            }
            catch (Exception ex)
            {
                ModelState.AddModelError("", "Unexpected system error.");
            }

            return(PartialView(cvm));
        }
示例#14
0
        public async Task <string> RenderSheets()
        {
            string crpName            = string.Empty;
            ReportSheetsViewModel rvm = new ReportSheetsViewModel();

            rvm.year = _ud.FarmDetails().year;

            rvm.fields = new List <ReportSheetsField>();

            List <Field> fldList = _ud.GetFields();

            foreach (var f in fldList)
            {
                ReportSheetsField rf = new ReportSheetsField();
                rf.fieldName = f.fieldName;
                rf.fieldArea = f.area.ToString();
                rf.nutrients = new List <ReportFieldNutrient>();
                if (f.nutrients != null)
                {
                    if (f.nutrients.nutrientManures != null)
                    {
                        foreach (var m in f.nutrients.nutrientManures)
                        {
                            FarmManure          manure = _ud.GetFarmManure(Convert.ToInt32(m.manureId));
                            ReportFieldNutrient rfn    = new ReportFieldNutrient();

                            rfn.nutrientName        = manure.name;
                            rfn.nutrientAmount      = String.Format((m.rate) % 1 == 0 ? "{0:#,##0}" : "{0:#,##0.00}", (m.rate));
                            rfn.nutrientSeason      = _sd.GetApplication(m.applicationId.ToString()).season;
                            rfn.nutrientApplication = _sd.GetApplication(m.applicationId.ToString()).application_method;
                            rfn.nutrientUnit        = _sd.GetUnit(m.unitId).name;
                            rf.nutrients.Add(rfn);
                        }
                    }
                    if (f.nutrients.nutrientFertilizers != null)
                    {
                        foreach (var ft in f.nutrients.nutrientFertilizers)
                        {
                            string fertilizerName    = string.Empty;
                            ReportFieldNutrient rfn  = new ReportFieldNutrient();
                            FertilizerType      ftyp = _sd.GetFertilizerType(ft.fertilizerTypeId.ToString());

                            if (ftyp.custom)
                            {
                                fertilizerName = ftyp.dry_liquid == "dry" ? "Custom (Dry) " : "Custom (Liquid) ";
                                fertilizerName = fertilizerName + ft.customN.ToString() + "-" + ft.customP2o5.ToString() + "-" + ft.customK2o.ToString();
                            }
                            else
                            {
                                Fertilizer ff = _sd.GetFertilizer(ft.fertilizerId.ToString());
                                fertilizerName = ff.name;
                            }

                            rfn.nutrientName   = fertilizerName;
                            rfn.nutrientAmount = String.Format((ft.applRate) % 1 == 0 ? "{0:#,##0}" : "{0:#,##0.00}", (ft.applRate));
                            rfn.nutrientSeason = ft.applDate != null?ft.applDate.Value.ToString("MMM-yyyy") : "";

                            rfn.nutrientApplication = ft.applMethodId > 0 ? _sd.GetFertilizerMethod(ft.applMethodId.ToString()).name : "";
                            rfn.nutrientUnit        = _sd.GetFertilizerUnit(ft.applUnitId).name;

                            rf.nutrients.Add(rfn);
                        }
                    }
                }
                if (rf.nutrients.Count() == 0)
                {
                    ReportFieldNutrient rfn = new ReportFieldNutrient();
                    rfn.nutrientName   = "None planned";
                    rfn.nutrientAmount = "";
                    rf.nutrients.Add(rfn);
                }
                if (f.crops != null)
                {
                    foreach (var c in f.crops)
                    {
                        crpName       = string.IsNullOrEmpty(c.cropOther) ? _sd.GetCrop(Convert.ToInt32(c.cropId)).cropname : c.cropOther;
                        rf.fieldCrops = string.IsNullOrEmpty(rf.fieldCrops) ? crpName : rf.fieldCrops + "\n" + crpName;
                    }
                }
                if (string.IsNullOrEmpty(rf.fieldCrops))
                {
                    rf.fieldCrops = "None recorded";
                }

                rvm.fields.Add(rf);
            }

            var result = await _viewRenderService.RenderToStringAsync("~/Views/Report/ReportSheets.cshtml", rvm);

            return(result);
        }
示例#15
0
        public async Task <string> RenderFields()
        {
            Utility.CalculateNutrients calculateNutrients      = new CalculateNutrients(_env, _ud, _sd);
            NOrganicMineralizations    nOrganicMineralizations = new NOrganicMineralizations();

            Utility.SoilTestConversions     stc = new SoilTestConversions(_ud, _sd);
            CalculateCropRequirementRemoval calculateCropRequirementRemoval = new CalculateCropRequirementRemoval(_ud, _sd);

            ReportFieldsViewModel rvm = new ReportFieldsViewModel();

            rvm.fields     = new List <ReportFieldsField>();
            rvm.year       = _ud.FarmDetails().year;
            rvm.methodName = string.IsNullOrEmpty(_ud.FarmDetails().testingMethod) ? "not selected" : _sd.GetSoilTestMethod(_ud.FarmDetails().testingMethod);
            rvm.prevHdg    = _sd.GetUserPrompt("ncreditlabel");

            List <Field> fldList = _ud.GetFields();

            foreach (var f in fldList)
            {
                ReportFieldsField rf = new ReportFieldsField();
                rf.fieldArea      = f.area.ToString();
                rf.fieldName      = f.fieldName;
                rf.fieldComment   = f.comment;
                rf.soiltest       = new ReportFieldSoilTest();
                rf.crops          = new List <ReportFieldCrop>();
                rf.otherNutrients = new List <ReportFieldOtherNutrient>();
                rf.footnotes      = new List <ReportFieldFootnote>();

                if (f.soilTest != null)
                {
                    rf.soiltest.sampleDate = f.soilTest.sampleDate.ToString("MMM yyyy");
                    rf.soiltest.dispNO3H   = f.soilTest.valNO3H.ToString() + " ppm";
                    rf.soiltest.dispP      = f.soilTest.ValP.ToString() + " ppm (" + _sd.SoilTestRating("phosphorous", stc.GetConvertedSTP(f.soilTest)) + ")";
                    rf.soiltest.dispK      = f.soilTest.valK.ToString() + " ppm (" + _sd.SoilTestRating("potassium", stc.GetConvertedSTK(f.soilTest)) + ")";
                    rf.soiltest.dispPH     = f.soilTest.valPH.ToString();
                }

                rf.nutrients = new List <ReportFieldNutrient>();
                if (f.crops != null)
                {
                    foreach (var c in f.crops)
                    {
                        ReportFieldCrop fc = new ReportFieldCrop();

                        fc.cropname = string.IsNullOrEmpty(c.cropOther) ? _sd.GetCrop(Convert.ToInt32(c.cropId)).cropname : c.cropOther;
                        if (c.coverCropHarvested.HasValue)
                        {
                            fc.cropname = c.coverCropHarvested.Value ? fc.cropname + "(harvested)" : fc.cropname;
                        }
                        if (c.prevCropId > 0)
                        {
                            fc.previousCrop = _sd.GetPrevCropType(c.prevCropId).name;
                        }

                        if (_sd.GetCropType(_sd.GetCrop(Convert.ToInt32(c.cropId)).croptypeid).crudeproteinrequired)
                        {
                            if (c.crudeProtien.Value.ToString("#.#") != calculateCropRequirementRemoval.GetCrudeProtienByCropId(Convert.ToInt32(c.cropId)).ToString("#.#"))
                            {
                                ReportFieldFootnote rff = new ReportFieldFootnote();
                                rff.id      = rf.footnotes.Count() + 1;
                                rff.message = "Crude protein adjusted to " + c.crudeProtien.Value.ToString("#.#") + "%";
                                fc.footnote = rff.id.ToString();
                                rf.footnotes.Add(rff);
                            }
                        }

                        if (_sd.GetCropType(_sd.GetCrop(Convert.ToInt32(c.cropId)).croptypeid).modifynitrogen)
                        {
                            // check for standard
                            CropRequirementRemoval cropRequirementRemoval = new CropRequirementRemoval();

                            calculateCropRequirementRemoval.cropid = Convert.ToInt16(c.cropId);
                            calculateCropRequirementRemoval.yield  = Convert.ToDecimal(c.yield);
                            if (c.crudeProtien == (decimal?)null)
                            {
                                calculateCropRequirementRemoval.crudeProtien = null;
                            }
                            else
                            {
                                calculateCropRequirementRemoval.crudeProtien = Convert.ToDecimal(c.crudeProtien);
                            }
                            calculateCropRequirementRemoval.coverCropHarvested = c.coverCropHarvested;
                            calculateCropRequirementRemoval.fieldName          = f.fieldName;
                            string nCredit = c.prevCropId != 0 ? _sd.GetPrevCropType(Convert.ToInt32(c.prevCropId)).nCreditImperial.ToString() : "0";

                            if (!string.IsNullOrEmpty(nCredit))
                            {
                                calculateCropRequirementRemoval.nCredit = Convert.ToInt16(nCredit);
                            }

                            cropRequirementRemoval = calculateCropRequirementRemoval.GetCropRequirementRemoval();

                            string stdNAmt = cropRequirementRemoval.N_Requirement.ToString();

                            if (c.reqN.ToString() != cropRequirementRemoval.N_Requirement.ToString())
                            {
                                ReportFieldFootnote rff = new ReportFieldFootnote();
                                rff.id      = rf.footnotes.Count() + 1;
                                rff.message = "Crop required nitrogen adjusted to " + c.reqN.ToString();
                                fc.footnote = rff.id.ToString();
                                rf.footnotes.Add(rff);
                            }
                        }

                        fc.yield = c.yield;
                        fc.reqN  = -c.reqN;
                        fc.reqP  = -c.reqP2o5;
                        fc.reqK  = -c.reqK2o;
                        fc.remN  = -c.remN;
                        fc.remP  = -c.remP2o5;
                        fc.remK  = -c.remK2o;

                        rf.reqN = rf.reqN + fc.reqN;
                        rf.reqP = rf.reqP + fc.reqP;
                        rf.reqK = rf.reqK + fc.reqK;
                        rf.remN = rf.remN + fc.remN;
                        rf.remP = rf.remP + fc.remP;
                        rf.remK = rf.remK + fc.remK;

                        rf.fieldCrops = rf.fieldCrops + fc.cropname + " ";

                        rf.crops.Add(fc);
                    }
                }
                if (f.nutrients != null)
                {
                    if (f.nutrients.nutrientManures != null)
                    {
                        foreach (var m in f.nutrients.nutrientManures)
                        {
                            FarmManure          manure = _ud.GetFarmManure(Convert.ToInt32(m.manureId));
                            ReportFieldNutrient rfn    = new ReportFieldNutrient();

                            rfn.nutrientName        = manure.name;
                            rfn.nutrientAmount      = String.Format((m.rate) % 1 == 0 ? "{0:#,##0}" : "{0:#,##0.00}", (m.rate));
                            rfn.nutrientSeason      = _sd.GetApplication(m.applicationId.ToString()).season;
                            rfn.nutrientApplication = _sd.GetApplication(m.applicationId.ToString()).application_method;
                            rfn.nutrientUnit        = _sd.GetUnit(m.unitId).name;
                            rfn.reqN = m.yrN;
                            rfn.reqP = m.yrP2o5;
                            rfn.reqK = m.yrK2o;
                            rfn.remN = m.ltN;
                            rfn.remP = m.ltP2o5;
                            rfn.remK = m.ltK2o;
                            rf.nutrients.Add(rfn);

                            rf.reqN = rf.reqN + rfn.reqN;
                            rf.reqP = rf.reqP + rfn.reqP;
                            rf.reqK = rf.reqK + rfn.reqK;
                            rf.remN = rf.remN + rfn.remN;
                            rf.remP = rf.remP + rfn.remP;
                            rf.remK = rf.remK + rfn.remK;

                            int    regionid = _ud.FarmDetails().farmRegion.Value;
                            Region region   = _sd.GetRegion(regionid);
                            nOrganicMineralizations = calculateNutrients.GetNMineralization(Convert.ToInt32(m.manureId), region.locationid);

                            string footNote = "";

                            if (m.nAvail != nOrganicMineralizations.OrganicN_FirstYear * 100)
                            {
                                footNote = "1st Yr Organic N Availability adjusted to " + m.nAvail.ToString("###") + "%";
                            }
                            if (m.nh4Retention != (calculateNutrients.GetAmmoniaRetention(Convert.ToInt16(m.manureId), Convert.ToInt16(m.applicationId)) * 100))
                            {
                                footNote = string.IsNullOrEmpty(footNote) ? "" : footNote + ", ";
                                footNote = footNote + "Ammonium-N Retention adjusted to " + m.nh4Retention.ToString("###") + "%";
                            }
                            if (!string.IsNullOrEmpty(footNote))
                            {
                                ReportFieldFootnote rff = new ReportFieldFootnote();
                                rff.id       = rf.footnotes.Count() + 1;
                                rff.message  = footNote;
                                rfn.footnote = rff.id.ToString();
                                rf.footnotes.Add(rff);
                            }
                        }
                    }
                    if (f.nutrients.nutrientFertilizers != null)
                    {
                        foreach (var ft in f.nutrients.nutrientFertilizers)
                        {
                            string fertilizerName    = string.Empty;
                            ReportFieldNutrient rfn  = new ReportFieldNutrient();
                            FertilizerType      ftyp = _sd.GetFertilizerType(ft.fertilizerTypeId.ToString());

                            if (ftyp.custom)
                            {
                                fertilizerName = ftyp.dry_liquid == "dry" ? "Custom (Dry) " : "Custom (Liquid) ";
                                fertilizerName = fertilizerName + ft.customN.ToString() + "-" + ft.customP2o5.ToString() + "-" + ft.customK2o.ToString();
                                rfn.reqN       = ft.fertN;
                                rfn.reqP       = ft.fertP2o5;
                                rfn.reqK       = ft.fertK2o;
                                rfn.remN       = ft.fertN;
                                rfn.remP       = ft.fertP2o5;
                                rfn.remK       = ft.fertK2o;
                            }
                            else
                            {
                                Fertilizer ff = _sd.GetFertilizer(ft.fertilizerId.ToString());
                                fertilizerName = ff.name;
                                rfn.reqN       = ff.nitrogen;
                                rfn.reqP       = ff.phosphorous;
                                rfn.reqK       = ff.potassium;
                                rfn.remN       = ff.nitrogen;
                                rfn.remP       = ff.phosphorous;
                                rfn.remK       = ff.potassium;
                            }

                            rfn.nutrientName        = fertilizerName;
                            rfn.nutrientApplication = ft.applMethodId > 0 ? _sd.GetFertilizerMethod(ft.applMethodId.ToString()).name : "";
                            rfn.nutrientUnit        = _sd.GetFertilizerUnit(ft.applUnitId).name;

                            rfn.nutrientAmount = String.Format((ft.applRate) % 1 == 0 ? "{0:#,##0}" : "{0:#,##0.00}", (ft.applRate));
                            rf.nutrients.Add(rfn);

                            rf.reqN = rf.reqN + rfn.reqN;
                            rf.reqP = rf.reqP + rfn.reqP;
                            rf.reqK = rf.reqK + rfn.reqK;
                            rf.remN = rf.remN + rfn.remN;
                            rf.remP = rf.remP + rfn.remP;
                            rf.remK = rf.remK + rfn.remK;

                            string footNote = "";

                            if (ftyp.dry_liquid == "liquid")
                            {
                                if (!ftyp.custom)
                                {
                                    if (ft.liquidDensity.ToString("#.##") != _sd.GetLiquidFertilizerDensity(ft.fertilizerId, ft.liquidDensityUnitId).value.ToString("#.##"))
                                    {
                                        footNote = "Liquid density adjusted to " + ft.liquidDensity.ToString("#.##");
                                    }
                                    if (!string.IsNullOrEmpty(footNote))
                                    {
                                        ReportFieldFootnote rff = new ReportFieldFootnote();
                                        rff.id       = rf.footnotes.Count() + 1;
                                        rff.message  = footNote;
                                        rfn.footnote = rff.id.ToString();
                                        rf.footnotes.Add(rff);
                                    }
                                }
                            }
                        }
                    }
                    if (f.nutrients.nutrientOthers != null)
                    {
                        foreach (var o in f.nutrients.nutrientOthers)
                        {
                            ReportFieldOtherNutrient fon = new ReportFieldOtherNutrient();
                            fon.otherName = o.description;
                            fon.reqN      = o.ltN;
                            fon.reqP      = o.ltP2o5;
                            fon.reqK      = o.ltK;
                            fon.remN      = o.yrN;
                            fon.remP      = o.yrP2o5;
                            fon.remK      = o.yrK;
                            rf.otherNutrients.Add(fon);

                            rf.reqN = rf.reqN + fon.reqN;
                            rf.reqP = rf.reqP + fon.reqP;
                            rf.reqK = rf.reqK + fon.reqK;
                            rf.remN = rf.remN + fon.remN;
                            rf.remP = rf.remP + fon.remP;
                            rf.remK = rf.remK + fon.remK;
                        }
                    }
                }

                if (rf.nutrients.Count() == 0)
                {
                    ReportFieldNutrient rfn = new ReportFieldNutrient();
                    rfn.nutrientName   = "None planned";
                    rfn.nutrientAmount = "";
                    rf.nutrients.Add(rfn);
                }
                ChemicalBalanceMessage cbm = new ChemicalBalanceMessage(_ud, _sd);

                var    request = HttpContext.Request;
                string scheme  = request.Scheme;
                string host    = request.Host.ToString();
                string imgLoc  = scheme + "://" + host + "/images/{0}.svg";

                rf.alertMsgs = cbm.DetermineBalanceMessages(f.fieldName);

                if (rf.alertMsgs.FirstOrDefault(r => r.Chemical == "AgrN") != null)
                {
                    rf.alertN    = true;
                    rf.iconAgriN = string.Format(imgLoc, rf.alertMsgs.FirstOrDefault(r => r.Chemical == "AgrN").Icon);
                }
                if (rf.alertMsgs.FirstOrDefault(r => r.Chemical == "AgrP2O5") != null)
                {
                    rf.alertP    = true;
                    rf.iconAgriP = string.Format(imgLoc, rf.alertMsgs.FirstOrDefault(r => r.Chemical == "AgrP2O5").Icon);
                }
                if (rf.alertMsgs.FirstOrDefault(r => r.Chemical == "AgrK2O") != null)
                {
                    rf.alertK    = true;
                    rf.iconAgriK = string.Format(imgLoc, rf.alertMsgs.FirstOrDefault(r => r.Chemical == "AgrK2O").Icon);
                }
                if (rf.alertMsgs.FirstOrDefault(r => r.Chemical == "CropN") != null)
                {
                    rf.alertN    = true;
                    rf.iconCropN = string.Format(imgLoc, rf.alertMsgs.FirstOrDefault(r => r.Chemical == "CropN").Icon);
                }
                if (rf.alertMsgs.FirstOrDefault(r => r.Chemical == "CropP2O5") != null)
                {
                    rf.alertP    = true;
                    rf.iconCropP = string.Format(imgLoc, rf.alertMsgs.FirstOrDefault(r => r.Chemical == "CropP2O5").Icon);
                }
                if (rf.alertMsgs.FirstOrDefault(r => r.Chemical == "CropK2O") != null)
                {
                    rf.alertK    = true;
                    rf.iconCropK = string.Format(imgLoc, rf.alertMsgs.FirstOrDefault(r => r.Chemical == "CropK2O").Icon);
                }

                //replace icon type with actual icon url for screen processing
                foreach (var i in rf.alertMsgs)
                {
                    i.Icon = string.Format(imgLoc, i.Icon);
                }

                rvm.fields.Add(rf);
            }

            var result = await _viewRenderService.RenderToStringAsync("~/Views/Report/ReportFields.cshtml", rvm);

            return(result);
        }
示例#16
0
        public NutrientInputs GetNutrientInputs(NutrientInputs nutrientInputs)
        {
            FarmManure mymanure = _ud.GetFarmManure(Convert.ToInt32(manure));

            ConversionFactor _cf = _sd.GetConversionFactor();

            decimal potassiumAvailabilityFirstYear   = _cf.potassiumAvailabilityFirstYear;
            decimal potassiumAvailabilityLongTerm    = _cf.potassiumAvailabilityLongTerm;
            decimal potassiumKtoK2Oconversion        = _cf.potassiumKtoK2Oconversion;
            decimal phosphorousAvailabilityFirstYear = _cf.phosphorousAvailabilityFirstYear;
            decimal phosphorousAvailabilityLongTerm  = _cf.phosphorousAvailabilityLongTerm;
            decimal phosphorousPtoP2O5Kconversion    = _cf.phosphorousPtoP2O5Kconversion;
            decimal lbPerTonConversion = _cf.lbPerTonConversion;
            decimal tenThousand        = 10000;

            // get conversion factor for selected units to lb/ac
            Unit    myunit     = _sd.GetUnit(applicationRateUnits);
            decimal conversion = myunit.conversion_lbton;

            // for solid manures specified in cubic yards per ac, convert application rate to tons/ac
            if (myunit.id == 6 && mymanure.solid_liquid.ToUpper() == "SOLID")
            {
                Manure manure = _sd.GetManure(mymanure.manureId.ToString());
                applicationRate = applicationRate * manure.cubic_Yard_Conversion;
            }


            // get potassium first year
            nutrientInputs.K2O_FirstYear = Convert.ToInt32(decimal.Multiply(applicationRate, mymanure.potassium)
                                                           * lbPerTonConversion
                                                           * potassiumKtoK2Oconversion
                                                           * potassiumAvailabilityFirstYear
                                                           * conversion);

            // get potassium long term
            nutrientInputs.K2O_LongTerm = Convert.ToInt32(decimal.Multiply(applicationRate, mymanure.potassium)
                                                          * lbPerTonConversion
                                                          * potassiumKtoK2Oconversion
                                                          * potassiumAvailabilityLongTerm
                                                          * conversion);

            // get phosphorous first year
            nutrientInputs.P2O5_FirstYear = Convert.ToInt32(decimal.Multiply(applicationRate, mymanure.phosphorous)
                                                            * lbPerTonConversion
                                                            * phosphorousPtoP2O5Kconversion
                                                            * phosphorousAvailabilityFirstYear
                                                            * conversion);

            // get phosphorous long term
            nutrientInputs.P2O5_LongTerm = Convert.ToInt32(decimal.Multiply(applicationRate, mymanure.phosphorous)
                                                           * lbPerTonConversion
                                                           * phosphorousPtoP2O5Kconversion
                                                           * phosphorousAvailabilityLongTerm
                                                           * conversion);

            // get N values
            // Organic N% = Total N% - NH4-N ppm / 10,000
            decimal organicN = mymanure.nitrogen - (Convert.ToDecimal(mymanure.ammonia) / tenThousand);

            NOrganicMineralizations nOrganicMineralizations = new NOrganicMineralizations();

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

            nOrganicMineralizations = GetNMineralization(mymanure.id, region.locationid);
            nOrganicMineralizations.OrganicN_FirstYear = firstYearOrganicNAvailablityPct / 100; // get data from screen

            //decimal ammoniaRetention = GetAmmoniaRetention(mymanure.id, Convert.ToInt32(applicationSeason));
            decimal ammoniaRetention = ammoniaNRetentionPct / 100; // get data from screen

            // N 1st year lb/ton = [NH4-N ppm/10,000 * NH4 retention + NO3-N/10,000 + Organic N %  * 1st yr Mineralization] * 20

            decimal a = decimal.Divide(mymanure.ammonia, tenThousand) * ammoniaRetention;

            decimal b1 = decimal.Multiply(organicN, nOrganicMineralizations.OrganicN_FirstYear);
            //E07US20
            decimal c1          = a + b1 + Convert.ToDecimal(mymanure.nitrate) / tenThousand;
            decimal N_Firstyear = decimal.Multiply(c1, lbPerTonConversion);

            nutrientInputs.N_FirstYear = Convert.ToInt32(applicationRate * N_Firstyear * conversion);

            // same for long term
            decimal b2 = decimal.Multiply(organicN, nOrganicMineralizations.OrganicN_LongTerm);
            //E07US20
            decimal c2         = a + b2 + Convert.ToDecimal(mymanure.nitrate) / tenThousand;
            decimal N_LongTerm = decimal.Multiply(c2, lbPerTonConversion);

            nutrientInputs.N_LongTerm = Convert.ToInt32(applicationRate * N_LongTerm * conversion);

            return(nutrientInputs);
        }
        private List <FieldAppliedManure> GetFieldsAppliedCollectedManureForFarmAnimals(List <int> farmAnimalIds, FarmManure farmManure, YearData yearData)
        {
            var farmAnimals = yearData.FarmAnimals.Where(fa => farmAnimalIds.Contains(fa.Id.GetValueOrDefault(0))).ToList();

            var fieldAppliedManures = new List <FieldAppliedManure>();

            foreach (var farmAnimal in farmAnimals)
            {
                var fieldsAppliedWithCollectedManure = yearData
                                                       .GetFieldsAppliedWithManure(farmManure)
                                                       .Where(f => !fieldAppliedManures.Any(fam => fam.FieldId == f.Id)).ToList();

                var manure = _repository.GetManure(farmManure.ManureId);

                foreach (var field in fieldsAppliedWithCollectedManure)
                {
                    var nutrientManures = field?.Nutrients.nutrientManures
                                          .Where(nm => Convert.ToInt32(nm.manureId) == farmManure.Id)
                                          .ToList() ?? new List <NutrientManure>();

                    foreach (var nutrientManure in nutrientManures)
                    {
                        var fieldAppliedManure = new FieldAppliedManure {
                            FieldId = field.Id
                        };

                        if (farmAnimal.ManureType == ManureMaterialType.Liquid)
                        {
                            var convertedRate = _manureUnitConversionCalculator
                                                .GetLiquidUSGallonsPerAcreApplicationRate(nutrientManure.rate,
                                                                                          (ApplicationRateUnits)Convert.ToInt32(nutrientManure.unitId));

                            fieldAppliedManure.USGallonsApplied =
                                field.Area * convertedRate;
                        }
                        else
                        {
                            if (!decimal.TryParse(manure.Moisture.Replace("%", ""), out var moisture))
                            {
                                moisture = _repository.GetManure(farmManure.ManureId).DefaultSolidMoisture.GetValueOrDefault(0);
                            }

                            var convertedRate = _manureUnitConversionCalculator
                                                .GetSolidsTonsPerAcreApplicationRate(moisture, nutrientManure.rate,
                                                                                     (ApplicationRateUnits)Convert.ToInt32(nutrientManure.unitId));

                            fieldAppliedManure.TonsApplied = field.Area * convertedRate;
                        }

                        fieldAppliedManures.Add(fieldAppliedManure);
                    }
                }
            }

            return(fieldAppliedManures);
        }
示例#18
0
        public NutrientInputs GetNutrientInputs(
            FarmManure farmManure,
            Region region,
            decimal applicationRate,
            string applicationRateUnits,
            decimal ammoniaNRetentionPct,
            decimal firstYearOrganicNAvailablityPct)
        {
            var nutrientInputs = new NutrientInputs();
            var _cf            = _sd.GetConversionFactor();

            decimal potassiumAvailabilityFirstYear   = _cf.PotassiumAvailabilityFirstYear;
            decimal potassiumAvailabilityLongTerm    = _cf.PotassiumAvailabilityLongTerm;
            decimal potassiumKtoK2Oconversion        = _cf.PotassiumKtoK2OConversion;
            decimal phosphorousAvailabilityFirstYear = _cf.PhosphorousAvailabilityFirstYear;
            decimal phosphorousAvailabilityLongTerm  = _cf.PhosphorousAvailabilityLongTerm;
            decimal phosphorousPtoP2O5Kconversion    = _cf.PhosphorousPtoP2O5Conversion;
            decimal lbPerTonConversion = _cf.PoundPerTonConversion;
            decimal tenThousand        = 10000;

            // get conversion factor for selected units to lb/ac
            Unit    myunit     = _sd.GetUnit(applicationRateUnits);
            decimal conversion = myunit.ConversionlbTon;

            // for solid manures specified in cubic yards per ac, convert application rate to tons/ac
            if (myunit.Id == 6 && farmManure.SolidLiquid.ToUpper() == "SOLID")
            {
                Manure manure = _sd.GetManure(farmManure.ManureId);
                applicationRate = applicationRate * manure.CubicYardConversion;
            }

            // get potassium first year
            nutrientInputs.K2O_FirstYear = Convert.ToInt32(decimal.Multiply(applicationRate, farmManure.Potassium)
                                                           * lbPerTonConversion
                                                           * potassiumKtoK2Oconversion
                                                           * potassiumAvailabilityFirstYear
                                                           * conversion);

            // get potassium long term
            nutrientInputs.K2O_LongTerm = Convert.ToInt32(decimal.Multiply(applicationRate, farmManure.Potassium)
                                                          * lbPerTonConversion
                                                          * potassiumKtoK2Oconversion
                                                          * potassiumAvailabilityLongTerm
                                                          * conversion);

            // get phosphorous first year
            nutrientInputs.P2O5_FirstYear = Convert.ToInt32(decimal.Multiply(applicationRate, farmManure.Phosphorous)
                                                            * lbPerTonConversion
                                                            * phosphorousPtoP2O5Kconversion
                                                            * phosphorousAvailabilityFirstYear
                                                            * conversion);

            // get phosphorous long term
            nutrientInputs.P2O5_LongTerm = Convert.ToInt32(decimal.Multiply(applicationRate, farmManure.Phosphorous)
                                                           * lbPerTonConversion
                                                           * phosphorousPtoP2O5Kconversion
                                                           * phosphorousAvailabilityLongTerm
                                                           * conversion);

            // get N values
            // Organic N% = Total N% - NH4-N ppm / 10,000
            decimal organicN = farmManure.Nitrogen - Convert.ToDecimal(farmManure.Ammonia) / tenThousand;

            NOrganicMineralizations nOrganicMineralizations = new NOrganicMineralizations();

            nOrganicMineralizations = GetNMineralization(farmManure, region.LocationId);
            nOrganicMineralizations.OrganicN_FirstYear = firstYearOrganicNAvailablityPct / 100; // get data from screen

            //decimal ammoniaRetention = GetAmmoniaRetention(mymanure.id, Convert.ToInt32(applicationSeason));
            decimal ammoniaRetention = ammoniaNRetentionPct / 100; // get data from screen

            // N 1st year lb/ton = [NH4-N ppm/10,000 * NH4 retention + NO3-N/10,000 + Organic N %  * 1st yr Mineralization] * 20

            decimal a = decimal.Divide(farmManure.Ammonia, tenThousand) * ammoniaRetention;

            decimal b1 = decimal.Multiply(organicN, nOrganicMineralizations.OrganicN_FirstYear);
            //E07US20
            decimal c1          = a + b1 + Convert.ToDecimal(farmManure.Nitrate) / tenThousand;
            decimal N_Firstyear = decimal.Multiply(c1, lbPerTonConversion);

            nutrientInputs.N_FirstYear = Convert.ToInt32(applicationRate * N_Firstyear * conversion);

            // same for long term
            decimal b2 = decimal.Multiply(organicN, nOrganicMineralizations.OrganicN_LongTerm);
            //E07US20
            decimal c2         = a + b2 + Convert.ToDecimal(farmManure.Nitrate) / tenThousand;
            decimal N_LongTerm = decimal.Multiply(c2, lbPerTonConversion);

            nutrientInputs.N_LongTerm = Convert.ToInt32(applicationRate * N_LongTerm * conversion);

            return(nutrientInputs);
        }