2024-11-15 09:28:25 -07:00

328 lines
14 KiB
Plaintext

@inject IMRBService mrbService
@inject ISnackbar snackbar
@inject ICustomerService customerService
<MudDialog>
<DialogContent>
<MudPaper Class="m-2 p-2">
<MudForm @bind-Errors="@errors">
<MudSelect T="string"
Label="Action"
Disabled="@(!string.IsNullOrWhiteSpace(mrbAction.Action))"
Required="true"
RequiredError="You must select an action!"
@bind-Value="@mrbAction.Action"
Text="@mrbAction.Action">
<MudSelectItem Value="@("Block")" />
<MudSelectItem Value="@("Convert")" />
<MudSelectItem Value="@("Recall")" />
<MudSelectItem Value="@("Scrap")" />
<MudSelectItem Value="@("Unblock")" />
<MudSelectItem Value="@("Waiver")" />
</MudSelect>
@if (mrbAction.Action.Equals("Convert", StringComparison.InvariantCultureIgnoreCase)) {
<MudSelect T="string"
Label="Convert From Customer"
Required
Variant="Variant.Outlined"
AnchorOrigin="Origin.BottomCenter"
@bind-Value=convertFromCustomer
Text="@convertFromCustomer">
@foreach (string customer in customerNames) {
<MudSelectItem Value="@(customer)" />
}
</MudSelect>
<MudTextField @bind-Value="@convertFromPart"
Label="Convert From Part Number"
Required
RequiredError="Part number required!"
Text="@convertFromPart" />
<MudSelect T="string"
Label="Convert To Customer"
Required
Variant="Variant.Outlined"
AnchorOrigin="Origin.BottomCenter"
@bind-Value=convertToCustomer
Text="@convertToCustomer">
@foreach (string customer in customerNames) {
<MudSelectItem Value="@(customer)" />
}
</MudSelect>
<MudTextField @bind-Value="@convertToPart"
Label="Convert To Part Number"
Required
RequiredError="Part number required!"
Text="@convertToPart" />
} else {
<MudSelect T="string"
Label="Affected Customer"
Required
Variant="Variant.Outlined"
AnchorOrigin="Origin.BottomCenter"
@bind-Value=mrbAction.Customer
Text="@mrbAction.Customer">
@foreach (string customer in customerNames) {
<MudSelectItem Value="@(customer)" />
}
</MudSelect>
<MudAutocomplete T="string"
Label="Part Number"
Variant="Variant.Outlined"
AnchorOrigin="Origin.BottomCenter"
@bind-Value=mrbAction.PartNumber
Text="@mrbAction.PartNumber"
SearchFunc="@PartNumberSearch"
ResetValueOnEmptyText
CoerceText />
}
<MudTextField T="int"
InputType="@InputType.Number"
Label="Qty"
Required="true"
RequiredError="You must supply a quantity!"
@bind-Value=mrbAction.Quantity
Text="@mrbAction.Quantity.ToString()" />
<MudAutocomplete T="string"
Label="Batch Number / Lot Number"
Variant="Variant.Outlined"
AnchorOrigin="Origin.BottomCenter"
@bind-Value=mrbAction.LotNumber
Text="@mrbAction.LotNumber"
SearchFunc="@LotNumberSearch"
ResetValueOnEmptyText
CoerceText />
@if (mrbAction.Action.Equals("Scrap")) {
<MudSelect T="string"
Label="Justification"
Required
RequiredError="Justification required!"
Variant="Variant.Outlined"
AnchorOrigin="Origin.BottomCenter"
@bind-Value=mrbAction.Justification
Text="@mrbAction.Justification">
<MudSelectItem Value="@("Obsolete")" />
<MudSelectItem Value="@("Aged Material")" />
<MudSelectItem Value="@("Out of Specification")" />
</MudSelect>
}
</MudForm>
</MudPaper>
</DialogContent>
<DialogActions>
<MudButton Variant="Variant.Filled"
Color="Color.Tertiary"
Class="m1-auto"
OnClick=SaveMRBAction>
@if (processingSave) {
<MudProgressCircular Class="m-1" Size="Size.Small" Indeterminate="true" />
<MudText>Processing</MudText>
} else {
<MudText>Save</MudText>
}
</MudButton>
@if (mrbAction is not null && mrbAction.ActionID > 0) {
<MudButton Variant="Variant.Filled"
Color="Color.Secondary"
Disabled="@(mrbAction is null || (mrbAction is not null && mrbAction.ActionID <= 0))"
Class="m1-auto"
OnClick=DeleteMRBAction>
@if (processingDelete) {
<MudProgressCircular Class="m-1" Size="Size.Small" Indeterminate="true" />
<MudText>Processing</MudText>
} else {
<MudText>Delete</MudText>
}
</MudButton>
}
<MudButton Variant="Variant.Filled"
Class="grey text-black m1-auto"
OnClick=Cancel>
Cancel
</MudButton>
</DialogActions>
</MudDialog>
@code {
[CascadingParameter] MudDialogInstance MudDialog { get; set; }
[Parameter]
public MRBAction mrbAction { get; set; }
private IEnumerable<MRBAction>? actions = null;
private MRBAction lastAction = null;
private IEnumerable<string> customerNames = new List<string>();
private string convertFromCustomer = string.Empty;
private string convertFromPart = string.Empty;
private string convertToCustomer = string.Empty;
private string convertToPart = string.Empty;
private bool isVisible { get; set; }
private string[] errors = { };
private bool processingSave = false;
private bool processingDelete = false;
protected override async Task OnParametersSetAsync() {
isVisible = true;
if (mrbAction is null) {
snackbar.Add("MRB action cannot be null", Severity.Warning);
MudDialog.Cancel();
} else {
actions = (await mrbService.GetMRBActionsForMRB(mrbAction.MRBNumber, false)).OrderByDescending(a => a.ActionID);
if (actions is not null && actions.Count() > 0) {
if (string.IsNullOrWhiteSpace(mrbAction.Action))
mrbAction.Action = actions.First().Action;
if (string.IsNullOrWhiteSpace(mrbAction.Customer))
mrbAction.Customer = actions.First().Customer;
if (string.IsNullOrWhiteSpace(mrbAction.LotNumber))
mrbAction.LotNumber = actions.First().LotNumber;
if (string.IsNullOrWhiteSpace(mrbAction.PartNumber))
mrbAction.PartNumber = actions.First().PartNumber;
if (string.IsNullOrWhiteSpace(mrbAction.Justification))
mrbAction.Justification = actions.First().Justification;
if (mrbAction.Quantity == 0)
mrbAction.Quantity = actions.First().Quantity;
if (mrbAction.Action.Equals("Convert", StringComparison.InvariantCultureIgnoreCase)) {
string[] convertFrom = actions.First().ConvertFrom.Split(" ");
if (convertFrom.Length > 1) {
convertFromCustomer = convertFrom[0];
foreach (string partStr in convertFrom.Skip(1))
convertFromPart += partStr;
}
string[] convertTo = actions.First().ConvertTo.Split(" ");
if (convertTo.Length > 1) {
convertToCustomer = convertTo[0];
foreach (string partStr in convertTo.Skip(1))
convertToPart += partStr;
}
}
}
}
if (customerNames is null || customerNames.Count() <= 0)
customerNames = await customerService.GetAllCustomerNames();
StateHasChanged();
}
private bool FormIsValid() {
bool actionIsValid = mrbAction.Action.Equals("Block") || mrbAction.Action.Equals("Convert") ||
mrbAction.Action.Equals("Other") || mrbAction.Action.Equals("Recall") || mrbAction.Action.Equals("Scrap") ||
mrbAction.Action.Equals("Unblock") || mrbAction.Action.Equals("Waiver");
actionIsValid = actionIsValid && !string.IsNullOrWhiteSpace(mrbAction.Customer) &&
!string.IsNullOrWhiteSpace(mrbAction.PartNumber) &&
!string.IsNullOrWhiteSpace(mrbAction.LotNumber);
actionIsValid = actionIsValid && mrbAction.Quantity > 0;
if (mrbAction.Action.Equals("Convert", StringComparison.InvariantCultureIgnoreCase)) {
actionIsValid = actionIsValid && !string.IsNullOrWhiteSpace(convertFromCustomer) &&
!string.IsNullOrWhiteSpace(convertFromPart) &&
!string.IsNullOrWhiteSpace(convertToCustomer) &&
!string.IsNullOrWhiteSpace(convertToPart);
}
if (mrbAction.Action.Equals("Scrap", StringComparison.InvariantCultureIgnoreCase))
actionIsValid = actionIsValid && !string.IsNullOrWhiteSpace(mrbAction.Justification);
return actionIsValid;
}
private async void SaveMRBAction() {
processingSave = true;
try {
if (!FormIsValid()) throw new Exception("You must complete the form before saving!");
if (mrbAction.Action.Equals("Convert", StringComparison.InvariantCultureIgnoreCase)) {
mrbAction.ConvertFrom = $"{convertFromCustomer} {convertFromPart}";
mrbAction.ConvertTo = $"{convertToCustomer} {convertToPart}";
}
if (mrbAction.MRBNumber > 0) {
if (mrbAction.ActionID <= 0) {
await mrbService.CreateMRBAction(mrbAction);
snackbar.Add("MRB action created", Severity.Success);
} else {
await mrbService.UpdateMRBAction(mrbAction);
snackbar.Add("MRB action updated", Severity.Success);
}
actions = (await mrbService.GetMRBActionsForMRB(mrbAction.MRBNumber, true)).OrderByDescending(a => a.ActionID);
} else {
snackbar.Add("MRB action saved", Severity.Success);
}
StateHasChanged();
MudDialog.Close(DialogResult.Ok(mrbAction));
} catch (Exception ex) {
snackbar.Add(ex.Message, Severity.Error);
}
processingSave = false;
}
private async void DeleteMRBAction() {
processingDelete = true;
try {
if (mrbAction is null) throw new Exception("MRB action cannot be null!");
if (mrbAction.ActionID <= 0)
throw new Exception("You cannot delete an action before creating it!");
if (mrbAction.MRBNumber <= 0)
throw new Exception("Invalid MRB number!");
await mrbService.DeleteMRBAction(mrbAction);
snackbar.Add("MRB action successfully deleted", Severity.Success);
StateHasChanged();
MudDialog.Close(DialogResult.Ok<MRBAction>(null));
} catch (Exception ex) {
snackbar.Add(ex.Message, Severity.Error);
}
processingDelete = false;
}
private void Cancel() {
MudDialog.Cancel();
}
private async Task<IEnumerable<string>> PartNumberSearch(string value, CancellationToken token) {
if (actions is null) return new List<string> { value };
if (string.IsNullOrWhiteSpace(value)) return new string[0];
HashSet<string> partNumbers = new();
partNumbers.Add(value);
foreach (MRBAction action in actions) {
if (action.PartNumber.Contains(value, StringComparison.InvariantCultureIgnoreCase))
partNumbers.Add(action.PartNumber);
}
return partNumbers;
}
private async Task<IEnumerable<string>> LotNumberSearch(string value, CancellationToken token) {
if (actions is null) return new List<string> { value };
if (string.IsNullOrWhiteSpace(value)) return new string[0];
HashSet<string> lotNumbers = new();
lotNumbers.Add(value);
foreach (MRBAction action in actions) {
if (action.LotNumber.Contains(value, StringComparison.InvariantCultureIgnoreCase))
lotNumbers.Add(action.LotNumber);
}
return lotNumbers;
}
}