Skip to content

Commit

Permalink
feat: Added custom liquidation calculations and validations
Browse files Browse the repository at this point in the history
- added nutrient fields for custom liquidation entries
- confirmed correct calculations
- added calculations to the home nutrients screen
  • Loading branch information
PaulGarewal committed Sep 20, 2024
1 parent 6176f4b commit d31e3d4
Show file tree
Hide file tree
Showing 3 changed files with 114 additions and 13 deletions.
103 changes: 98 additions & 5 deletions app/Server/src/SERVERAPI/Controllers/NutrientsController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -308,9 +308,9 @@ public IActionResult FertigationDetails(string fldName, int? id, string? groupID
}
if (ft.Custom)
{
fgvm.valN = nf.customN.Value.ToString("0");
fgvm.valP2o5 = nf.customP2o5.Value.ToString("0");
fgvm.valK2o = nf.customK2o.Value.ToString("0");
fgvm.valN = nf.customN.Value.ToString("");
fgvm.valP2o5 = nf.customP2o5.Value.ToString("");
fgvm.valK2o = nf.customK2o.Value.ToString("");
fgvm.manualEntry = true;
}
else
Expand All @@ -325,6 +325,13 @@ public IActionResult FertigationDetails(string fldName, int? id, string? groupID
else
{
FertigationDetail_Reset(ref fgvm);
if (fgvm.selTypOption == "4") // This is custom liquid fertigation, could be put directly into details reset
{
fgvm.valN = "";
fgvm.valP2o5 = "";
fgvm.valK2o = "";
fgvm.manualEntry = true;
}
}
FertigationStillRequired(ref fgvm);
FertigationDetailsSetup(ref fgvm);
Expand Down Expand Up @@ -359,6 +366,13 @@ private void FertigationDetail_Reset(ref FertigationDetailsViewModel fgvm)
fgvm.totProductVolPerSeason = 0.0M;

fgvm.eventsPerSeason = 1;

// if custom liquid fertigation, we set these to empty for user to change
if (fgvm.selTypOption == "4"){
fgvm.valN = "";
fgvm.valP2o5 = "";
fgvm.valK2o = "";
}
}

private void FertigationStillRequired(ref FertigationDetailsViewModel fgvm)
Expand Down Expand Up @@ -457,6 +471,7 @@ private void FertigationDetailSetup_Fertilizer(ref FertigationDetailsViewModel f
fgvm.fertOptions = new List<SelectListItem>() { new SelectListItem() { Id = 1, Value = "Custom" } };
fgvm.selFertOption = 1;
fgvm.stdDensity = true;
fgvm.manualEntry = true;
}
}
else
Expand All @@ -465,6 +480,13 @@ private void FertigationDetailSetup_Fertilizer(ref FertigationDetailsViewModel f
fgvm.selFertOption = 0;
}

// if (fgvm.selTypOption == "4") // this is custom liquid fertigation
// {
// fgvm.manualEntry = true;
// fgvm.fertOptions = new List<SelectListItem>() { new SelectListItem() { Id = 1, Value = "Custom" } };
// fgvm.selFertOption = 1;
// }

return;
}

Expand Down Expand Up @@ -638,6 +660,44 @@ public IActionResult FertigationDetails(FertigationDetailsViewModel fgvm)
}
}

// bool isCustomFertigation = fgvm.selTypOption == "4";
// fgvm.manualEntry = isCustomFertigation;

// if (isCustomFertigation)
// {
// // Validate custom nutrient inputs here, may have to be moved and altered
// if (string.IsNullOrEmpty(fgvm.valN))
// {
// ModelState.AddModelError("valN", "Required");
// }
// else if (!decimal.TryParse(fgvm.valN, out decimal customN) || customN < 0 || customN > 100)
// {
// ModelState.AddModelError("valN", "Invalid. Must be between 0 and 100.");
// }

// if (string.IsNullOrEmpty(fgvm.valP2o5))
// {
// ModelState.AddModelError("valP2o5", "Required");
// }
// else if (!decimal.TryParse(fgvm.valP2o5, out decimal customP) || customP < 0 || customP > 100)
// {
// ModelState.AddModelError("valP2o5", "Invalid. Must be between 0 and 100.");
// }

// if (string.IsNullOrEmpty(fgvm.valK2o))
// {
// ModelState.AddModelError("valK2o", "Required");
// }
// else if (!decimal.TryParse(fgvm.valK2o, out decimal customK) || customK < 0 || customK > 100)
// {
// ModelState.AddModelError("valK2o", "Invalid. Must be between 0 and 100.");
// }

// // Set up custom fertilizer options
// fgvm.fertOptions = new List<SelectListItem>() { new SelectListItem() { Id = 1, Value = "Custom" } };
// fgvm.selFertOption = 1;
// }

if (fgvm.buttonPressed == "Calculate" && ModelState.IsValid)
{
ModelState.Clear();
Expand Down Expand Up @@ -672,8 +732,6 @@ public IActionResult FertigationDetails(FertigationDetailsViewModel fgvm)
Convert.ToDecimal(fgvm.valK2o),
fgvm.manualEntry);



Field field = _ud.GetFieldDetails(fgvm.fieldName);
FertilizerUnit _fU = _sd.GetFertilizerUnit(Convert.ToInt32(fgvm.selProductRateUnitOption));
decimal convertedProductRate = Convert.ToDecimal(fgvm.productRate) * _fU.ConversionToImperialGallonsPerAcre;
Expand Down Expand Up @@ -839,6 +897,24 @@ private List<int> FertigationInsert(FertigationDetailsViewModel fgvm)
injectionRateUnitId = Convert.ToInt32(fgvm.selInjectionRateUnitOption),
groupID = groupID
};

bool isCustomFertigation = fgvm.selTypOption == "4";
fgvm.manualEntry = isCustomFertigation;

if (isCustomFertigation)
{
decimal customN = Convert.ToDecimal(fgvm.calcN);
decimal customP = Convert.ToDecimal(fgvm.calcP2o5);
decimal customK = Convert.ToDecimal(fgvm.calcK2o);

nf.customN = customN;
nf.customP2o5 = customP;
nf.customK2o = customK;
nf.fertN = customN;
nf.fertP2o5 = customP;
nf.fertK2o = customK;
}

ids.Add( _ud.AddFieldNutrientsFertilizer(fgvm.fieldName, nf));
}
return ids;
Expand Down Expand Up @@ -900,6 +976,23 @@ private void FertigationUpdate(FertigationDetailsViewModel fgvm)
isFertigation = true
};

bool isCustomFertigation = fgvm.selTypOption == "4";
fgvm.manualEntry = isCustomFertigation;

if (isCustomFertigation)
{
decimal customN = Convert.ToDecimal(fgvm.valN);
decimal customP = Convert.ToDecimal(fgvm.valP2o5);
decimal customK = Convert.ToDecimal(fgvm.valK2o);

nf.customN = customN;
nf.customP2o5 = customP;
nf.customK2o = customK;
nf.fertN = customN;
nf.fertP2o5 = customP;
nf.fertK2o = customK;
}

_ud.AddFieldNutrientsFertilizer(fgvm.fieldName, nf);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,17 @@ public class FertigationDetailsViewModel
[DisplayFormat(DataFormatString = "{0:dd-MMM-yyyy}", ApplyFormatInEditMode = true)]
public DateTime? applDate { get; set; }
public bool manualEntry { get; set; }

[Required(ErrorMessage = "Required")]
[Range(0, 9999, ErrorMessage = "Required")]
public string valN { get; set; }
[Required(ErrorMessage = "Required")]
[Range(0, 9999, ErrorMessage = "Required")]
public string valP2o5 { get; set; }
[Required(ErrorMessage = "Required")]
[Range(0, 9999, ErrorMessage = "Required")]
public string valK2o { get; set; }

public string calcN { get; set; }
public string calcP2o5 { get; set; }
public string calcK2o { get; set; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,30 +23,30 @@
<span asp-validation-for="selTypOption" class="text-danger"></span>
</div>
<div class="form-group col-sm-3" style="width:200px;">
<label for="ddlFert">Fertilizer</label>
@if(Model.selTypOption == "4"){
<div class="form-group col-sm-2" style="margin-right:0px; padding-right:0px; width:100px">
<label for="valN">N</label>
@if(Model.selTypOption == "4"){ // if custom liquid fertigation
<div class="form-group col-sm-2" style="margin-right:0px; padding-right:0px; width:60px">
<label for="valN">N(%)</label>
<div>
<input class="form-control" asp-for="valN" id="valN" type="text" />
<span asp-validation-for="valN" class="text-danger"></span>
</div>
</div>
<div class="form-group col-sm-2" style="margin-right:0px; padding-right:0px; width:100px">
<label for="valP2o5">P2o5</label>
<div class="form-group col-sm-2" style="margin-right:0px; padding-right:0px; width:60px">
<label for="valP2o5">PO₅(%)</label>
<div>
<input class="form-control" asp-for="valP2o5" id="valP2o5" type="text" />
<span asp-validation-for="valP2o5" class="text-danger"></span>
</div>
</div>
<div class="form-group col-sm-2" style="margin-right:0px; padding-right:0px; width:100px">
<label for="valK2o">K2o</label>
<div class="form-group col-sm-2" style="margin-right:0px; padding-right:0px; width:60px">
<label for="valK2o">KO(%)</label>
<div>
<input class="form-control" asp-for="valK2o" id="valK2o" type="text" />
<span asp-validation-for="valK2o" class="text-danger"></span>
</div>
</div>
} else {
<label for="ddlFert">Fertilizer</label>
<select class="form-control" asp-for="selFertOption" asp-items="@(new SelectList(Model.fertOptions,"Id","Value"))" id="ddlFert">
<option value="">select</option>
</select>
Expand Down

0 comments on commit d31e3d4

Please sign in to comment.