Merge pull request #693 from hargata/Hargata/order.missing.supplies

Add functionality to order insufficient supplies for plan templates.
This commit is contained in:
Hargata Softworks 2024-11-02 20:41:06 -06:00 committed by GitHub
commit 21d135a8ab
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
8 changed files with 118 additions and 9 deletions

View File

@ -66,6 +66,24 @@ namespace CarCareTracker.Controllers
var result = _planRecordTemplateDataAccess.DeletePlanRecordTemplateById(planRecordTemplateId); var result = _planRecordTemplateDataAccess.DeletePlanRecordTemplateById(planRecordTemplateId);
return Json(result); 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] [HttpPost]
public IActionResult ConvertPlanRecordTemplateToPlanRecord(int planRecordTemplateId) public IActionResult ConvertPlanRecordTemplateToPlanRecord(int planRecordTemplateId)
{ {
@ -78,9 +96,13 @@ namespace CarCareTracker.Controllers
{ {
//check if all supplies are available //check if all supplies are available
var supplyAvailability = CheckSupplyRecordsAvailability(existingRecord.Supplies); var supplyAvailability = CheckSupplyRecordsAvailability(existingRecord.Supplies);
if (supplyAvailability.Any()) if (supplyAvailability.Any(x => x.Missing))
{ {
return Json(new OperationResponse { Success = false, Message = string.Join("<br>", supplyAvailability) }); 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 = "Insufficient Supplies" });
} }
} }
if (existingRecord.ReminderRecordId != default) if (existingRecord.ReminderRecordId != default)

View File

@ -7,21 +7,21 @@ namespace CarCareTracker.Controllers
{ {
public partial class VehicleController 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 //returns empty string if all supplies are available
var result = new List<string>(); var result = new List<SupplyAvailability>();
foreach (SupplyUsage supply in supplyUsage) foreach (SupplyUsage supply in supplyUsage)
{ {
//get supply record. //get supply record.
var supplyData = _supplyRecordDataAccess.GetSupplyRecordById(supply.SupplyId); var supplyData = _supplyRecordDataAccess.GetSupplyRecordById(supply.SupplyId);
if (supplyData == null) 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; return result;

View File

@ -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; } }
}
}

View File

@ -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>

View File

@ -39,7 +39,7 @@
} }
@if (planRecordTemplate.Supplies.Any()) @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) @if (planRecordTemplate.ImportMode == ImportMode.ServiceRecord)
{ {

View File

@ -109,4 +109,11 @@
<div class="modal-content" id="planRecordTemplateModalContent"> <div class="modal-content" id="planRecordTemplateModalContent">
</div> </div>
</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> </div>

File diff suppressed because one or more lines are too long

View File

@ -150,6 +150,9 @@ function usePlannerRecordTemplate(planRecordTemplateId) {
saveScrollPosition(); saveScrollPosition();
getVehiclePlanRecords(vehicleId); getVehiclePlanRecords(vehicleId);
} else { } 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); errorToast(data.message);
} }
}); });
@ -325,4 +328,24 @@ function updatePlanRecordProgress(newProgress) {
draggedId = 0; 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();
} }