Skip to content

Commit

Permalink
feat: Added solid calculations and units
Browse files Browse the repository at this point in the history
- TODO: unit conversions of amount to dissolve and tank vol
- TODO: general clean up and verification
  • Loading branch information
PaulGarewal committed Oct 2, 2024
1 parent 3dcd19e commit 0536dc9
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 34 deletions.
115 changes: 85 additions & 30 deletions app/Server/src/SERVERAPI/Controllers/NutrientsController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -488,6 +488,12 @@ private void FertigationDetailsSetup(ref FertigationDetailsViewModel fgvm)
fgvm.densityUnitOptions = GetOptionsList(_fg.DensityUnits);
fgvm.solubilityUnitOptions = GetOptionsList(_fg.SolubilityUnits);
fgvm.applPeriod = GetOptionsList(_fg.Schedules);
fgvm.amountToDissolveUnitOptions = new List<SelectListItem>
{
new SelectListItem { Id = 1, Value = "lbs" },
new SelectListItem { Id = 2, Value = "kgs" },
new SelectListItem { Id = 3, Value = "grams" }
};
FertigationDetailSetup_Fertilizer(ref fgvm);
}

Expand Down Expand Up @@ -631,26 +637,37 @@ public IActionResult FertigationDetails(FertigationDetailsViewModel fgvm)
ModelState.Clear();
fgvm.buttonPressed = "";
fgvm.btnText = "Calculate";
// These are just test conversions. Will need to implement actual conversions!!!
if (fgvm.selSolubilityUnitOption == 3){
var solubility = Convert.ToDecimal(fgvm.solInWater) * 1.2M;
fgvm.solInWater = solubility.ToString("#.##");
}
if (fgvm.selSolubilityUnitOption == 2){
var solubility = Convert.ToDecimal(fgvm.solInWater) * 0.5M;
fgvm.solInWater = solubility.ToString("#.##");

if (decimal.TryParse(fgvm.solInWater, out decimal currentValue))
{
if (fgvm.solInWaterUnits == null) {
fgvm.solInWaterUnits = "1";
}
var fromUnit = (SolubilityUnit)(int.TryParse(fgvm.solInWaterUnits, out int result) ? result : fgvm.selSolubilityUnitOption ?? 0);
var toUnit = (SolubilityUnit)fgvm.selSolubilityUnitOption;

decimal convertedValue = ConvertSolubility(currentValue, fromUnit, toUnit);
fgvm.solInWater = convertedValue.ToString("#.##");
}

fgvm.solInWaterUnits = fgvm.selSolubilityUnitOption.ToString();
return PartialView(fgvm);
}
if (fgvm.buttonPressed == "AmountToDissolveChange")
{
ModelState.Clear();
fgvm.buttonPressed = "";
fgvm.btnText = "Calculate";
// WIP Convert amount to dissolve to grams per liter as a base unit
if (decimal.TryParse(fgvm.amountToDissolve, out decimal amountToDissolve)) {
if (fgvm.amountToDissolveUnits == null) {
fgvm.amountToDissolveUnits = "1";
}
var fromUnit = (SolubilityUnit)(int.TryParse(fgvm.amountToDissolveUnits, out int result) ? result : fgvm.selSolubilityUnitOption ?? 0);
var toUnit = (SolubilityUnit)fgvm.selSolubilityUnitOption;

if (fgvm.selTypOption == "2")
{
FertigationDetailSetup_DefaultDensity(ref fgvm);
decimal convertedValue = ConvertSolubility(amountToDissolve, fromUnit, toUnit);
fgvm.amountToDissolve = convertedValue.ToString("#.##");
}
return View(fgvm);
}
Expand Down Expand Up @@ -805,28 +822,25 @@ public IActionResult FertigationDetails(FertigationDetailsViewModel fgvm)

else { // fertigation is dry in this case
// calculate dry fertigation below (may need to move this else block above ^ to accont for still required and other funcs)
FertilizerNutrients fertilizerNutrients = _calculateFertigationNutrients.GetFertilizerNutrients(fgvm.selFertOption ?? 0,
fgvm.fertilizerType,
Convert.ToDecimal(fgvm.productRate),
Convert.ToInt32(fgvm.selProductRateUnitOption),
fgvm.density != null ? Convert.ToDecimal(fgvm.density) : 0,
Convert.ToInt16(fgvm.selDensityUnitOption),
Convert.ToDecimal(fgvm.valN),
Convert.ToDecimal(fgvm.valP2o5),
Convert.ToDecimal(fgvm.valK2o),
fgvm.manualEntry);
// FertilizerNutrients fertilizerNutrients = _calculateFertigationNutrients.GetFertilizerNutrients(fgvm.selFertOption ?? 0,
// fgvm.fertilizerType,
// Convert.ToDecimal(fgvm.productRate),
// Convert.ToInt32(fgvm.selProductRateUnitOption),
// fgvm.density != null ? Convert.ToDecimal(fgvm.density) : 0,
// Convert.ToInt16(fgvm.selDensityUnitOption),
// Convert.ToDecimal(fgvm.valN),
// Convert.ToDecimal(fgvm.valP2o5),
// Convert.ToDecimal(fgvm.valK2o),
// fgvm.manualEntry);

Field field = _ud.GetFieldDetails(fgvm.fieldName);
fgvm.fieldArea = Convert.ToString(field.Area);
//FertilizerUnit _fU = _sd.GetFertilizerUnit(Convert.ToInt32(fgvm.selProductRateUnitOption));
//decimal convertedProductRate = Convert.ToDecimal(fgvm.productRate) * _fU.ConversionToImperialGallonsPerAcre;


SolidFertigationCalculation(fgvm, fertilizerNutrients);
SolidFertigationCalculation(fgvm);
//Total Applied Nutrients
fgvm.calcTotalN = Convert.ToString(Convert.ToInt32(fertilizerNutrients.fertilizer_N) * Convert.ToInt32(fgvm.eventsPerSeason));
fgvm.calcTotalK2o = Convert.ToString(Convert.ToInt32(fertilizerNutrients.fertilizer_P2O5) * Convert.ToInt32(fgvm.eventsPerSeason));
fgvm.calcTotalP2o5 = Convert.ToString(Convert.ToInt32(fertilizerNutrients.fertilizer_K2O) * Convert.ToInt32(fgvm.eventsPerSeason));
fgvm.calcTotalN = Convert.ToString(Convert.ToDecimal(fgvm.calcN) * fgvm.eventsPerSeason);
fgvm.calcTotalK2o = Convert.ToString(Convert.ToDecimal(fgvm.calcK2o)* fgvm.eventsPerSeason);
fgvm.calcTotalP2o5 = Convert.ToString(Convert.ToDecimal(fgvm.calcP2o5) * fgvm.eventsPerSeason);

fgvm.btnText = fgvm.id == null ? "Add to Field" : "Update Field";
}
Expand Down Expand Up @@ -881,7 +895,41 @@ public IActionResult FertigationDetails(FertigationDetailsViewModel fgvm)
return PartialView(fgvm);
}

private void SolidFertigationCalculation(FertigationDetailsViewModel fgvm, FertilizerNutrients fertilizerNutrients){
private enum SolubilityUnit {
GramsPerLiter = 1,
KilogramsPerLiter = 2,
PoundsPerImperialGallon = 3
}

private enum DissolveUnit {
Pounds = 1,
Kilograms = 2,
Grams = 3
}

private decimal ConvertSolubility(decimal value, SolubilityUnit fromUnit, SolubilityUnit toUnit)
{
if (fromUnit == toUnit)
return value;

decimal baseValue = fromUnit switch
{
SolubilityUnit.GramsPerLiter => value,
SolubilityUnit.KilogramsPerLiter => value * 1000,
SolubilityUnit.PoundsPerImperialGallon => value * 99.776373M,
_ => throw new ArgumentException("Invalid fromUnit")
};

return toUnit switch
{
SolubilityUnit.GramsPerLiter => baseValue,
SolubilityUnit.KilogramsPerLiter => baseValue / 1000,
SolubilityUnit.PoundsPerImperialGallon => baseValue / 99.776373M,
_ => throw new ArgumentException("Invalid toUnit")
};
}

private void SolidFertigationCalculation(FertigationDetailsViewModel fgvm){
decimal amountToDissolve = Convert.ToDecimal(fgvm.amountToDissolve);
decimal tankVolume = Convert.ToDecimal(fgvm.tankVolume);
decimal fertArea = Convert.ToDecimal(fgvm.fieldArea);
Expand All @@ -891,7 +939,14 @@ private void SolidFertigationCalculation(FertigationDetailsViewModel fgvm, Ferti
fgvm.nutrientConcentrationP205 = Convert.ToString(amountToDissolve * Convert.ToDecimal(fgvm.valP2o5) / 100 / tankVolume);

decimal injectionRate = Convert.ToDecimal(fgvm.injectionRate);
fgvm.fertigationTime = Math.Round(Convert.ToDecimal(fgvm.tankVolume) * injectionRate, 2);
fgvm.fertigationTime = Math.Round(Convert.ToDecimal(fgvm.tankVolume) / injectionRate, 2);

if (amountToDissolve <= tankVolume * Convert.ToDecimal(fgvm.solInWater)){
fgvm.dryAction = "Good";
}
else{
fgvm.dryAction = "Reduce the amount to dissolve";
}

fgvm.calcN = Convert.ToString(Convert.ToDecimal(fgvm.nutrientConcentrationN) * tankVolume / fertArea);
fgvm.calcK2o = Convert.ToString(Convert.ToDecimal(fgvm.nutrientConcentrationK2O) * tankVolume / fertArea);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ public class FertigationDetailsViewModel
[RequiredIf("selTypOption", "1", "2", ErrorMessage = "Required")]
public string amountToDissolve { get; set; }
public string amountToDissolveUnits { get; set; }
public List<SelectListItem> amountToDissolveUnitOptions { get; set; }
public string dryAction { get; set; }
public string nutrientConcentrationN { get; set; }
public string nutrientConcentrationP205 { get; set; }
Expand Down
21 changes: 17 additions & 4 deletions app/Server/src/SERVERAPI/Views/Nutrients/FertigationDetails.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@
</div>
</div>
<div class="form-group col-sm-4" style="margin-right:0px; padding-right:0px; width:140px">
<label for="density">Amount to Disolve</label>
<label for="density">Amount to Dissolve</label>
<div>
<input class="form-control" asp-for="amountToDissolve" id="ddlAmountToDissolve" type="text"/>
<span asp-validation-for="amountToDissolve" class="text-danger"></span>
Expand All @@ -97,8 +97,8 @@
<div class="form-group col-sm-2" style="margin-left:0px;padding-left:0px; width:100px">
<div style="display:table; width:100%">
<div style="display:table-row">
<label for="ddlDensity" style="text-align: center; display: block;">Units</label>
<select class="form-control" asp-for="amountToDissolveUnits" asp-items="@(new SelectList(Model.densityUnitOptions,"Id","Value"))" id="ddlDensity"></select>
<label for="ddlDissolveUnits" style="text-align: center; display: block;">Units</label>
<select class="form-control" asp-for="amountToDissolveUnits" asp-items="@(new SelectList(Model.amountToDissolveUnitOptions,"Id","Value"))" id="ddlAmountToDissolve"></select>
<span asp-validation-for="amountToDissolveUnits" class="text-danger"></span>
</div>
</div>
Expand All @@ -109,7 +109,17 @@
Action
</label>
<div class="cell" style="text-align:center">
@Model.dryAction
@if (Model.dryAction == "Good")
{
<span style="color: #4BB543;">@Model.dryAction </span>
<img src="~/images/good.svg" title="Good"/>

}
else if (Model.dryAction == "Reduce the amount to dissolve")
{
<span style="color: #FF0000;">@Model.dryAction</span>
<img src="~/images/stop.svg" title="Stop"/>
}
</div>
</div>
</div>
Expand Down Expand Up @@ -425,7 +435,10 @@
@Html.HiddenFor(x => x.tankVolume)
@Html.HiddenFor(x => x.solInWater)
@Html.HiddenFor(x => x.amountToDissolve)
@Html.HiddenFor(x => x.amountToDissolveUnitOptions)
@Html.HiddenFor(x => x.amountToDissolveUnits)
@Html.HiddenFor(x => x.selSolubilityUnitOption)
@Html.HiddenFor(m => m.solInWaterUnits)

@Html.HiddenFor(x => x.fertigationTime)
@Html.HiddenFor(x => x.totProductVolPerFert)
Expand Down

0 comments on commit 0536dc9

Please sign in to comment.