Skip to content

Commit

Permalink
Merge pull request #693 from hargata/Hargata/order.missing.supplies
Browse files Browse the repository at this point in the history
Add functionality to order insufficient supplies for plan templates.
  • Loading branch information
hargata authored Nov 3, 2024
2 parents feaf631 + 7a0636a commit 21d135a
Show file tree
Hide file tree
Showing 8 changed files with 118 additions and 9 deletions.
26 changes: 24 additions & 2 deletions Controllers/Vehicle/PlanController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,24 @@ public IActionResult DeletePlanRecordTemplateById(int planRecordTemplateId)
var result = _planRecordTemplateDataAccess.DeletePlanRecordTemplateById(planRecordTemplateId);
return Json(result);
}
[HttpGet]
public IActionResult OrderPlanSupplies(int planRecordTemplateId)
{
var existingRecord = _planRecordTemplateDataAccess.GetPlanRecordTemplateById(planRecordTemplateId);
if (existingRecord.Id == default)
{
return Json(new OperationResponse { Success = false, Message = "Unable to find template" });
}
if (existingRecord.Supplies.Any())
{
var suppliesToOrder = CheckSupplyRecordsAvailability(existingRecord.Supplies);
return PartialView("_PlanOrderSupplies", suppliesToOrder);
}
else
{
return Json(new OperationResponse { Success = false, Message = "Template has No Supplies" });
}
}
[HttpPost]
public IActionResult ConvertPlanRecordTemplateToPlanRecord(int planRecordTemplateId)
{
Expand All @@ -78,9 +96,13 @@ public IActionResult ConvertPlanRecordTemplateToPlanRecord(int planRecordTemplat
{
//check if all supplies are available
var supplyAvailability = CheckSupplyRecordsAvailability(existingRecord.Supplies);
if (supplyAvailability.Any())
if (supplyAvailability.Any(x => x.Missing))
{
return Json(new OperationResponse { Success = false, Message = "Missing Supplies, Please Delete This Template and Recreate It." });
}
else if (supplyAvailability.Any(x => x.Insufficient))
{
return Json(new OperationResponse { Success = false, Message = string.Join("<br>", supplyAvailability) });
return Json(new OperationResponse { Success = false, Message = "Insufficient Supplies" });
}
}
if (existingRecord.ReminderRecordId != default)
Expand Down
10 changes: 5 additions & 5 deletions Controllers/Vehicle/SupplyController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,21 @@ namespace CarCareTracker.Controllers
{
public partial class VehicleController
{
private List<string> CheckSupplyRecordsAvailability(List<SupplyUsage> supplyUsage)
private List<SupplyAvailability> CheckSupplyRecordsAvailability(List<SupplyUsage> supplyUsage)
{
//returns empty string if all supplies are available
var result = new List<string>();
var result = new List<SupplyAvailability>();
foreach (SupplyUsage supply in supplyUsage)
{
//get supply record.
var supplyData = _supplyRecordDataAccess.GetSupplyRecordById(supply.SupplyId);
if (supplyData == null)
{
result.Add("Missing Supplies, Please Delete This Template and Recreate It.");
result.Add(new SupplyAvailability { Missing = true });
}
else if (supply.Quantity > supplyData.Quantity)
else
{
result.Add($"Insufficient Quantity for {supplyData.Description}, need: {supply.Quantity}, available: {supplyData.Quantity}");
result.Add(new SupplyAvailability { Missing = false, Description = supplyData.Description, Required = supply.Quantity, InStock = supplyData.Quantity });
}
}
return result;
Expand Down
11 changes: 11 additions & 0 deletions Models/Supply/SupplyAvailability.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
namespace CarCareTracker.Models
{
public class SupplyAvailability
{
public bool Missing { get; set; }
public string Description { get; set; } = string.Empty;
public decimal Required { get; set; }
public decimal InStock { get; set; }
public bool Insufficient { get { return Required > InStock; } }
}
}
46 changes: 46 additions & 0 deletions Views/Vehicle/_PlanOrderSupplies.cshtml
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
@using CarCareTracker.Helper
@inject IConfigHelper config
@inject ITranslationHelper translator
@model List<SupplyAvailability>
@{
var userConfig = config.GetUserConfig(User);
var userLanguage = userConfig.UserLanguage;
}
<div class="modal-header">
<h5 class="modal-title">@translator.Translate(userLanguage, "Order Supplies")</h5>
<button type="button" class="btn-close" onclick="hideOrderSupplyModal()" aria-label="Close"></button>
</div>
<div class="modal-body">
@if (!Model.Any() || Model.Any(x => x.Missing))
{
<p class="lead">@translator.Translate(userLanguage, "Missing Supplies, Please Delete This Template and Recreate It.")</p>
} else
{
<div class="row">
<div class="col-12" style="max-height:50vh; overflow-y:auto;">
<table class="table table-hover">
<thead class="sticky-top">
<tr class="d-flex">
<th scope="col" class="col-6 text-truncate">@translator.Translate(userLanguage, "Description")</th>
<th scope="col" class="col-3 text-truncate">@translator.Translate(userLanguage, "Required")</th>
<th scope="col" class="col-3 text-truncate">@translator.Translate(userLanguage, "In Stock")</th>
</tr>
</thead>
<tbody>
@foreach (SupplyAvailability supplyAvailability in Model)
{
<tr class="d-flex @(supplyAvailability.Insufficient ? "table-danger" : "")">
<td class="col-6 text-truncate">@StaticHelper.TruncateStrings(supplyAvailability.Description)</td>
<td class="col-3 text-truncate">@supplyAvailability.Required.ToString("N2")</td>
<td class="col-3 text-truncate">@supplyAvailability.InStock.ToString("N2")</td>
</tr>
}
</tbody>
</table>
</div>
</div>
}
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" onclick="hideOrderSupplyModal()">@translator.Translate(userLanguage, "Cancel")</button>
</div>
2 changes: 1 addition & 1 deletion Views/Vehicle/_PlanRecordTemplateModal.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
}
@if (planRecordTemplate.Supplies.Any())
{
<i class="bi bi-shop ms-2"></i>
<i class="bi bi-shop ms-2" style="cursor:pointer;"onclick="orderPlanSupplies(@planRecordTemplate.Id)"></i>
}
@if (planRecordTemplate.ImportMode == ImportMode.ServiceRecord)
{
Expand Down
7 changes: 7 additions & 0 deletions Views/Vehicle/_PlanRecords.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -109,4 +109,11 @@
<div class="modal-content" id="planRecordTemplateModalContent">
</div>
</div>
</div>

<div class="modal fade" data-bs-focus="false" id="planRecordTemplateSupplyOrderModal" tabindex="-1" role="dialog" aria-hidden="true">
<div class="modal-dialog modal-lg" role="document">
<div class="modal-content" id="planRecordTemplateSupplyOrderModalContent">
</div>
</div>
</div>
2 changes: 1 addition & 1 deletion wwwroot/defaults/en_US.json

Large diffs are not rendered by default.

23 changes: 23 additions & 0 deletions wwwroot/js/planrecord.js
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,9 @@ function usePlannerRecordTemplate(planRecordTemplateId) {
saveScrollPosition();
getVehiclePlanRecords(vehicleId);
} else {
if (data.message == "Insufficient Supplies") {
data.message += `<br /><br /><a class='text-link' style='cursor:pointer;' onclick='orderPlanSupplies(${planRecordTemplateId}, true)'>Order Required Supplies</a>`
}
errorToast(data.message);
}
});
Expand Down Expand Up @@ -325,4 +328,24 @@ function updatePlanRecordProgress(newProgress) {
draggedId = 0;
}
}
}
function orderPlanSupplies(planRecordTemplateId, closeSwal) {
if (closeSwal) {
Swal.close();
}
$.get(`/Vehicle/OrderPlanSupplies?planRecordTemplateId=${planRecordTemplateId}`, function (data) {
if (data.success != undefined && !data.success) {
//success is provided.
errorToast(data.message);
} else {
//hide plan record template modal.
hidePlanRecordTemplatesModal();
$("#planRecordTemplateSupplyOrderModalContent").html(data);
$("#planRecordTemplateSupplyOrderModal").modal('show');
}
})
}
function hideOrderSupplyModal() {
$("#planRecordTemplateSupplyOrderModal").modal('hide');
showPlanRecordTemplatesModal();
}

0 comments on commit 21d135a

Please sign in to comment.