Skip to content

Commit

Permalink
Merge pull request #691 from bcgov/687-delete-working
Browse files Browse the repository at this point in the history
[FEATURE][NMP-687] Delete and Edit Functionality for Fertigation
  • Loading branch information
acatchpole authored Sep 16, 2024
2 parents 1202ae7 + a9752f3 commit dc4ee6d
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 83 deletions.
91 changes: 35 additions & 56 deletions app/Server/src/SERVERAPI/Controllers/NutrientsController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ public IActionResult ManureDetails(string fldName, int? id)
return PartialView(mvm);
}

public IActionResult FertigationDetails(string fldName, int? id)
public IActionResult FertigationDetails(string fldName, int? id, string? groupID)
{
var fgvm = new FertigationDetailsViewModel()
{
Expand All @@ -264,6 +264,7 @@ public IActionResult FertigationDetails(string fldName, int? id)
btnText = id == null ? "Add to Field" : "Update Field",
id = id,
isFertigation = true,
groupID = groupID,
};

if (id != null){
Expand All @@ -286,7 +287,7 @@ public IActionResult FertigationDetails(string fldName, int? id)
}
fgvm.density = nf.liquidDensity.ToString("#.##");
fgvm.selDensityUnitOption = nf.liquidDensityUnitId;
fgvm.eventsPerSeason = nf.eventsPerSeason;
fgvm.eventsPerSeason = getNumberOfEvents(fgvm);
fgvm.selFertSchedOption = nf.applMethodId.ToString();
fgvm.injectionRate = nf.injectionRate.ToString("#.##");
fgvm.selInjectionRateUnitOption = nf.injectionRateUnitId.ToString();
Expand Down Expand Up @@ -482,23 +483,6 @@ public IActionResult FertigationDetails(FertigationDetailsViewModel fgvm)

try
{
/*
if (fgvm.buttonPressed == "Calculate")
{
if (!ModelState.IsValid)
{
FertigationDetailsSetup(ref fgvm);
return PartialView(fgvm);
}
// Calculation logic will be implemented here by Adam and the team
// As of now, clear the model state until that ticket is implemented
ModelState.Clear();
fgvm.btnText = "Add to Field";
FertigationDetailsSetup(ref fgvm);
return PartialView(fgvm);
}
*/
if (fgvm.buttonPressed == "ResetDensity")
{
ModelState.Clear();
Expand Down Expand Up @@ -527,13 +511,6 @@ public IActionResult FertigationDetails(FertigationDetailsViewModel fgvm)

fgvm.density = "";

// if (fgvm.currUnit != typ.DryLiquid)
// {
// fgvm.currUnit = typ.DryLiquid;
// //fgvm.productRateUnitOptions = _sd.GetFertilizerUnitsDll(fgvm.currUnit).ToList();
// fgvm.selProductRateUnitOption = fgvm.productRateUnitOptions[0].Id.ToString();
// fgvm.fertilizerType = typ.DryLiquid;
// }
fgvm.fertilizerType = typ.DryLiquid;
fgvm.manualEntry = typ.Custom;
if (!fgvm.manualEntry)
Expand Down Expand Up @@ -777,7 +754,7 @@ public IActionResult FertigationDetails(FertigationDetailsViewModel fgvm)
_ud.UpdateFieldNutrientsFertilizer(fgvm.fieldName, origFertilizer);
}
}
else if (fgvm.buttonPressed == "Add to Field" || fgvm.buttonPressed == "Update Field") // may need to add update field here as well
else if (fgvm.buttonPressed == "Add to Field" || fgvm.buttonPressed == "Update Field")
{
fgvm.buttonPressed = "";
if (fgvm.id == null)
Expand Down Expand Up @@ -855,6 +832,7 @@ private List<int> FertigationInsert(FertigationDetailsViewModel fgvm)
applUnitId = Convert.ToInt32(fgvm.selProductRateUnitOption),
applRate = Convert.ToDecimal(fgvm.productRate),
applDate = getIncrementedDate(Int32.Parse(fgvm.selFertSchedOption), fgvm.applDate, x),
applMethodId = Convert.ToInt32(fgvm.selFertSchedOption),
customN = fgvm.manualEntry ? Convert.ToDecimal(fgvm.valN) : (decimal?)null,
customP2o5 = fgvm.manualEntry ? Convert.ToDecimal(fgvm.valP2o5) : (decimal?)null,
customK2o = fgvm.manualEntry ? Convert.ToDecimal(fgvm.valK2o) : (decimal?)null,
Expand All @@ -863,10 +841,10 @@ private List<int> FertigationInsert(FertigationDetailsViewModel fgvm)
fertK2o = Convert.ToDecimal(fgvm.calcK2o),
liquidDensity = Convert.ToDecimal(fgvm.density),
liquidDensityUnitId = Convert.ToInt32(fgvm.selDensityUnitOption),
//eventsPerSeason = fgvm.eventsPerSeason,
//injectionRate = Convert.ToDecimal(fgvm.injectionRate),
//injectionRateUnitId = Convert.ToInt32(fgvm.selInjectionRateUnitOption),
isFertigation = true,
injectionRate = Convert.ToDecimal(fgvm.injectionRate),
injectionRateUnitId = Convert.ToInt32(fgvm.selInjectionRateUnitOption),
//eventsPerSeason = fgvm.eventsPerSeason,
groupID = groupID
};
ids.Add( _ud.AddFieldNutrientsFertilizer(fgvm.fieldName, nf));
Expand All @@ -888,12 +866,19 @@ private List<int> FertigationInsert(FertigationDetailsViewModel fgvm)
return startingDate;
}

public int getNumberOfEvents(FertigationDetailsViewModel fgvm){
var fertigations = _ud.GetFieldNutrientsFertilizers(fgvm.fieldName)
.Where(nf => nf.isFertigation && nf.groupID == fgvm.groupID)
.ToList();

return fertigations.Count();
}

private void FertigationUpdate(FertigationDetailsViewModel fgvm)
{
var existingFertigations = _ud.GetFieldNutrientsFertilizers(fgvm.fieldName)
.Where(nf => nf.id == fgvm.id || (nf.isFertigation && nf.fertilizerTypeId == Convert.ToInt32(fgvm.selTypOption)))
.Where(nf => nf.isFertigation && nf.groupID == fgvm.groupID)
.ToList();

foreach (var existingFert in existingFertigations)
{
_ud.DeleteFieldNutrientsFertilizer(fgvm.fieldName, existingFert.id);
Expand All @@ -906,19 +891,21 @@ private void FertigationUpdate(FertigationDetailsViewModel fgvm)
fertilizerTypeId = Convert.ToInt32(fgvm.selTypOption),
fertilizerId = fgvm.selFertOption ?? 0,
applUnitId = Convert.ToInt32(fgvm.selProductRateUnitOption),
applRate = Convert.ToDecimal(fgvm.productRate) / fgvm.eventsPerSeason,
applRate = Convert.ToDecimal(fgvm.productRate),
applDate = getIncrementedDate(Int32.Parse(fgvm.selFertSchedOption), fgvm.applDate, x),
applMethodId = Convert.ToInt32(fgvm.selFertSchedOption),
customN = fgvm.manualEntry ? Convert.ToDecimal(fgvm.valN) : (decimal?)null,
customP2o5 = fgvm.manualEntry ? Convert.ToDecimal(fgvm.valP2o5) : (decimal?)null,
customK2o = fgvm.manualEntry ? Convert.ToDecimal(fgvm.valK2o) : (decimal?)null,
fertN = Convert.ToDecimal(fgvm.calcN) / fgvm.eventsPerSeason,
fertP2o5 = Convert.ToDecimal(fgvm.calcP2o5) / fgvm.eventsPerSeason,
fertK2o = Convert.ToDecimal(fgvm.calcK2o) / fgvm.eventsPerSeason,
fertN = Convert.ToDecimal(fgvm.calcN),
fertP2o5 = Convert.ToDecimal(fgvm.calcP2o5),
fertK2o = Convert.ToDecimal(fgvm.calcK2o),
liquidDensity = Convert.ToDecimal(fgvm.density),
liquidDensityUnitId = Convert.ToInt32(fgvm.selDensityUnitOption),
groupID = fgvm.groupID,
injectionRate = Convert.ToDecimal(fgvm.injectionRate),
injectionRateUnitId = Convert.ToInt32(fgvm.selInjectionRateUnitOption),
//eventsPerSeason = fgvm.eventsPerSeason,
//injectionRate = Convert.ToDecimal(fgvm.injectionRate),
//injectionRateUnitId = Convert.ToInt32(fgvm.selInjectionRateUnitOption),
isFertigation = true
};

Expand Down Expand Up @@ -969,28 +956,20 @@ public ActionResult FertigationDelete(FertigationDeleteViewModel dvm)
{
if (ModelState.IsValid)
{
//var fertigationToDelete = _ud.GetFieldNutrientsFertilizer(dvm.fldName, dvm.id);

// have to fix the logic here to delete the correct fertigation, currently it deletes all fertigations
var fertigationsToDelete = _ud.GetFieldNutrientsFertilizers(dvm.fldName)
.Where(nf => nf.isFertigation && nf.groupID == dvm.groupID)
.ToList();

foreach (var fertigation in fertigationsToDelete)
{
_ud.DeleteFieldNutrientsFertilizer(dvm.fldName, fertigation.id);
}
foreach (var fertigation in fertigationsToDelete)
{
_ud.DeleteFieldNutrientsFertilizer(dvm.fldName, fertigation.id);
}

return Json(ReDisplay("#fertigation", dvm.fldName));
return Json(ReDisplay("#fertigation", dvm.fldName));
}
return PartialView("FertigationDelete", dvm);
}

public ActionResult FertigationCalculateDates(CalculateViewModel dvm)
{
return null;
}

private void MaunureStillRequired(ref ManureDetailsViewModel mvm)
{
//recalc totals for display
Expand Down Expand Up @@ -2042,7 +2021,7 @@ public IActionResult OtherDetails(OtherDetailsViewModel ovm)
{
decimal tmp = 0;

if (!(string.IsNullOrEmpty(ovm.ltN)))
if (!string.IsNullOrEmpty(ovm.ltN))
{
if (decimal.TryParse(ovm.ltN, out tmp))
{
Expand All @@ -2064,7 +2043,7 @@ public IActionResult OtherDetails(OtherDetailsViewModel ovm)
ovm.ltN = "0";
}

if (!(string.IsNullOrEmpty(ovm.ltP)))
if (!string.IsNullOrEmpty(ovm.ltP))
{
if (decimal.TryParse(ovm.ltP, out tmp))
{
Expand All @@ -2086,7 +2065,7 @@ public IActionResult OtherDetails(OtherDetailsViewModel ovm)
ovm.ltP = "0";
}

if (!(string.IsNullOrEmpty(ovm.ltK)))
if (!string.IsNullOrEmpty(ovm.ltK))
{
if (decimal.TryParse(ovm.ltK, out tmp))

Expand All @@ -2108,7 +2087,7 @@ public IActionResult OtherDetails(OtherDetailsViewModel ovm)
{
ovm.ltK = "0";
}
if (!(string.IsNullOrEmpty(ovm.yrN)))
if (!string.IsNullOrEmpty(ovm.yrN))
{
if (decimal.TryParse(ovm.yrN, out tmp))
{
Expand All @@ -2130,7 +2109,7 @@ public IActionResult OtherDetails(OtherDetailsViewModel ovm)
ovm.yrN = "0";
}

if (!(string.IsNullOrEmpty(ovm.yrP)))
if (!string.IsNullOrEmpty(ovm.yrP))
{
if (decimal.TryParse(ovm.yrP, out tmp))
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,14 +56,6 @@ private Task<CalcFertigationViewModel> GetFertigationAsync(string fldName)
dm.fldName = fldName;
dm.fertilizerId = f.id;
dm.fertilizerName = fertilizerName;

// int startIndex = fertilizerName.IndexOf('(');
// int endIndex = fertilizerName.IndexOf(')');
// if (startIndex != -1 && endIndex != -1 && endIndex > startIndex)
// {
// string result = fertilizerName.Substring(startIndex +1, endIndex - startIndex -1);
// dm.fertilizerName = result;
// }
dm.valN = f.fertN.ToString("G29");
dm.valP = f.fertP2o5.ToString("G29");
dm.valK = f.fertK2o.ToString("G29");
Expand Down
4 changes: 2 additions & 2 deletions app/Server/src/SERVERAPI/Views/Nutrients/Calculate.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -476,14 +476,14 @@ $(document).ready(function () {
$(document).ready(function () {
$("body").on("click", "#calc_button", function () {
$('#buttonPressed').val("Calculate");
$("#modForm").submit();
//$("#modForm").submit();
})
});
$(document).ready(function () {
$("body").on("click", "#submit_button", function () {
$('#buttonPressed').val("Add to Field");
$("#modForm").submit();
//$("#modForm").submit();
})
});
$(document).ready(function () {
Expand Down
23 changes: 12 additions & 11 deletions app/Server/src/SERVERAPI/Views/Nutrients/FertigationDetails.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -86,14 +86,14 @@
<div class="form-group col-sm-4">
<label for="eventsPerSeason">Proposed Number of Fertigation Per Season</label>
<div>
<input class="form-control" id="eventsPerSeason" type="text" asp-for="eventsPerSeason" style="width:100px" />
<input class="form-control" id="eventsPerSeason" type="number" min="1" asp-for="eventsPerSeason" style="width:100px" />
<span asp-validation-for="eventsPerSeason" class="text-danger" value=1></span>
</div>
</div>
<div class="form-group col-sm-3">
<label for="ddlMan">Fertigation Scheduling</label>
<select class="form-control" id="ddlApplPeriod" style="width:100%" asp-for="selFertSchedOption" asp-items="@(new SelectList(Model.applPeriod,"Id","Value"))">
<option>select</option>
<option></option>
</select>
<div class="mt-2">
<div class="input-group date">
Expand Down Expand Up @@ -196,7 +196,7 @@
<p>K<sub>2</sub>O</p>
</div>
</div>
<div class="Row">@* Temporarily val. need to switch back to calcTotal *@
<div class="Row">
<div class="Cell" style="text-align:center">
@Model.calcTotalN
</div>
Expand Down Expand Up @@ -230,21 +230,21 @@
@if (!string.IsNullOrEmpty(Model.totNIcon))
{
<img src="~/images/@(Model.totNIcon).svg" title="@Model.totNIconText"/>
}
}
@Model.totN
</div>
<div class="Cell" style="text-align:center">
@if (!string.IsNullOrEmpty(Model.totPIcon))
{
<img src="~/images/@(Model.totPIcon).svg" title="@Model.totPIconText" />
}
@if (!string.IsNullOrEmpty(Model.totPIcon))
{
<img src="~/images/@(Model.totPIcon).svg" title="@Model.totPIconText" />
}
@Model.totP2o5
</div>
<div class="Cell" style="text-align:center">
@if (!string.IsNullOrEmpty(Model.totKIcon))
{
<img src="~/images/@(Model.totKIcon).svg" title="@Model.totKIconText" />
}
{
<img src="~/images/@(Model.totKIcon).svg" title="@Model.totKIconText" />
}
@Model.totK2o
</div>
</div>
Expand All @@ -264,6 +264,7 @@
@Html.HiddenFor(x => x.buttonPressed)
@Html.HiddenFor(x => x.selFertSchedOption)
@Html.HiddenFor(x => x.applDate)
@Html.HiddenFor(x => x.groupID)

@Html.HiddenFor(x => x.valN)
@Html.HiddenFor(x => x.valP2o5)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,10 @@
</div>

bool isFirstItem = true;
string fertilizerKey = "";

string currentGroup = "";
foreach (var m in Model.fldFertilizers)
{
string currKey = m.fertilizerName + m.eventsPerSeason;
if( fertilizerKey != currKey){
if( m.groupID != currentGroup){
isFirstItem = true;
}

Expand Down Expand Up @@ -86,7 +84,7 @@
@if (isFirstItem)
{
<div class="Cell2">
<button title="Edit" aria-label="Edit" class="form-box-table-button" type="button" data_modal="" data-type="@Url.Action("FertigationDetails", "Nutrients", new { fldName = m.fldName, id = m.fertilizerId})">
<button title="Edit" aria-label="Edit" class="form-box-table-button" type="button" data_modal="" data-type="@Url.Action("FertigationDetails", "Nutrients", new { fldName = m.fldName, id = m.fertilizerId, groupID = m.groupID})">
<svg version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px" width="24px" height="24px" viewBox="0 0 24 24" enable-background="new 0 0 24 24" xml:space="preserve" stroke-width="2"><g stroke-width="2" transform="translate(0, 0)"><line data-cap="butt" data-color="color-2" fill="none" stroke="#444444" stroke-width="2" stroke-miterlimit="10" x1="14.5" y1="4.5" x2="19.5" y2="9.5" stroke-linejoin="miter" stroke-linecap="butt"></line> <line data-cap="butt" data-color="color-2" fill="none" stroke="#444444" stroke-width="2" stroke-miterlimit="10" x1="3" y1="16" x2="8" y2="21" stroke-linejoin="miter" stroke-linecap="butt"></line> <path fill="none" stroke="#444444" stroke-width="2" stroke-linecap="square" stroke-miterlimit="10" d="M8,21l-6,1l1-6 L16.586,2.414c0.781-0.781,2.047-0.781,2.828,0l2.172,2.172c0.781,0.781,0.781,2.047,0,2.828L8,21z" stroke-linejoin="miter"></path></g></svg>
</button>
</div>
Expand All @@ -95,7 +93,7 @@
<svg version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px" width="24px" height="24px" viewBox="0 0 24 24" enable-background="new 0 0 24 24" xml:space="preserve" stroke-width="2"><g stroke-width="2" transform="translate(0, 0)"><polyline fill="none" stroke="#444444" stroke-width="2" stroke-linecap="square" stroke-miterlimit="10" points="20,9 20,23 4,23 4,9 " stroke-linejoin="miter"></polyline> <line fill="none" stroke="#444444" stroke-width="2" stroke-linecap="square" stroke-miterlimit="10" x1="1" y1="5" x2="23" y2="5" stroke-linejoin="miter"></line> <line data-color="color-2" fill="none" stroke="#444444" stroke-width="2" stroke-linecap="square" stroke-miterlimit="10" x1="12" y1="12" x2="12" y2="18" stroke-linejoin="miter"></line> <line data-color="color-2" fill="none" stroke="#444444" stroke-width="2" stroke-linecap="square" stroke-miterlimit="10" x1="8" y1="12" x2="8" y2="18" stroke-linejoin="miter"></line> <line data-color="color-2" fill="none" stroke="#444444" stroke-width="2" stroke-linecap="square" stroke-miterlimit="10" x1="16" y1="12" x2="16" y2="18" stroke-linejoin="miter"></line> <polyline fill="none" stroke="#444444" stroke-width="2" stroke-linecap="square" stroke-miterlimit="10" points="8,5 8,1 16,1 16,5 " stroke-linejoin="miter"></polyline></g></svg>
</button>
</div>
fertilizerKey = m.fertilizerName + m.eventsPerSeason;
currentGroup = m.groupID;
isFirstItem = false;
}
</div>
Expand Down

0 comments on commit dc4ee6d

Please sign in to comment.