Exemplo n.º 1
0
        private void ReCalculateManure(int id)
        {
            Utility.CalculateNutrients calculateNutrients      = new CalculateNutrients(_env, _ud, _sd);
            NOrganicMineralizations    nOrganicMineralizations = new NOrganicMineralizations();

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

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

                foreach (var nm in mans)
                {
                    if (id.ToString() == nm.manureId)
                    {
                        int    regionid = _ud.FarmDetails().farmRegion.Value;
                        Region region   = _sd.GetRegion(regionid);
                        nOrganicMineralizations = calculateNutrients.GetNMineralization(Convert.ToInt16(nm.manureId), region.locationid);

                        string avail = (nOrganicMineralizations.OrganicN_FirstYear * 100).ToString("###");

                        string nh4 = (calculateNutrients.GetAmmoniaRetention(Convert.ToInt16(nm.manureId), Convert.ToInt16(nm.applicationId)) * 100).ToString("###");

                        NutrientInputs nutrientInputs = new NutrientInputs();

                        calculateNutrients.manure                          = nm.manureId;
                        calculateNutrients.applicationSeason               = nm.applicationId;
                        calculateNutrients.applicationRate                 = Convert.ToDecimal(nm.rate);
                        calculateNutrients.applicationRateUnits            = nm.unitId;
                        calculateNutrients.ammoniaNRetentionPct            = Convert.ToDecimal(nh4);
                        calculateNutrients.firstYearOrganicNAvailablityPct = Convert.ToDecimal(avail);

                        calculateNutrients.GetNutrientInputs(nutrientInputs);

                        nm.yrN    = nutrientInputs.N_FirstYear;
                        nm.yrP2o5 = nutrientInputs.P2O5_FirstYear;
                        nm.yrK2o  = nutrientInputs.K2O_FirstYear;
                        nm.ltN    = nutrientInputs.N_LongTerm;
                        nm.ltP2o5 = nutrientInputs.P2O5_LongTerm;
                        nm.ltK2o  = nutrientInputs.K2O_LongTerm;

                        _ud.UpdateFieldNutrientsManure(fld.fieldName, nm);
                    }
                }
            }
        }
Exemplo n.º 2
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);
        }