FruitBankHybridApp/FruitBankHybrid.Shared/Pages/MeasuringIn.razor.cs

174 lines
7.7 KiB
C#

using AyCode.Core.Extensions;
using AyCode.Core.Loggers;
using DevExpress.Blazor;
using FruitBank.Common.Entities;
using FruitBank.Common.Helpers;
using FruitBank.Common.Interfaces;
using FruitBank.Common.Models;
using FruitBankHybrid.Shared.Models;
using FruitBankHybrid.Shared.Services;
using FruitBankHybrid.Shared.Services.Loggers;
using FruitBankHybrid.Shared.Services.SignalRs;
using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Components.Web;
using ILogger = Mango.Nop.Core.Loggers.ILogger;
namespace FruitBankHybrid.Shared.Pages
{
public partial class MeasuringIn : ComponentBase
{
[Inject] public required IEnumerable<IAcLogWriterClientBase> LogWriters { get; set; }
[Inject] public required FruitBankSignalRClient FruitBankSignalRClient { get; set; }
[Inject] public required LoggedInModel LoggedInModel { get; set; }
private ILogger _logger = null!;
private string _errorText;
private List<Shipping> NotMeasuredShippings { get; set; } = null!;
private Shipping? SelectedShipping { get; set; }
private ShippingDocument? SelectedShippingDocument { get; set; }
private ShippingItem? SelectedShippingItem { get; set; }
protected bool BtnSaveEnabled { get; set; }
private List<MeasuringDateSelectorModel> _measuringDates = null!;
protected override async Task OnInitializedAsync()
{
_logger = new LoggerClient<MeasuringIn>(LogWriters.ToArray());
_logger.Info("OnInitializedAsync");
await RefreshShippingsFromDb(DateTime.Now);
await base.OnInitializedAsync();
}
private async Task RefreshShippingsFromDb(DateTime dateTime)
{
var shippings = await FruitBankSignalRClient.GetNotMeasuredShippings() ?? [];
_measuringDates = shippings.Select(shipping => new MeasuringDateSelectorModel(shipping.Id, shipping.ShippingDate.Date, shipping.IsAllMeasured)).ToList();
NotMeasuredShippings = shippings.Where(shipping => MeasuringService.DaysEqual(shipping.ShippingDate.Date, dateTime)).ToList();
SelectedShipping = NotMeasuredShippings.FirstOrDefault();
}
private async Task OnMeasuringDateChanged(DateTime selectedDateTime)
=> await RefreshShippingsFromDb(selectedDateTime);
private void OnCustomDisabledMeasuringDate(CalendarCustomDisabledDateEventArgs args)
=> MeasuringService.OnCustomDisabledDate(args, _measuringDates);
private string GetMeasuringDateCssClassNames(DateTime date)
=> MeasuringService.GetShippingDateCssClassNames(date, _measuringDates);
private string GetShippingPalletsCssClassNames(string fieldName, ShippingItemPallet shippingItemPallet)
=> MeasuringService.GetCustomItemPalletsCssClassNames(fieldName, shippingItemPallet, SelectedShippingItem!.IsMeasurable);
private void OnSelectedShippingChanged(SelectedDataItemChangedEventArgs<Shipping> eventArgs)
{
SelectedShippingDocument = eventArgs.DataItem?.ShippingDocuments?.FirstOrDefault();
}
private void OnSelectedShippingDocumentChanged(SelectedDataItemChangedEventArgs<ShippingDocument> eventArgs)
{
SelectedShippingItem = eventArgs.DataItem?.ShippingItems?.FirstOrDefault();
}
private void OnSelectedShippingItemChanged(SelectedDataItemChangedEventArgs<ShippingItem> eventArgs)
{
BtnSaveEnabled = false;
var shippingItem = eventArgs.DataItem;
if (shippingItem == null)
{
SelectedShippingItem = null;
return;
}
SelectedShippingDocument!.IsAllMeasured = SelectedShippingDocument.ShippingItems?.All(si => si.IsMeasured) ?? false;
SelectedShipping!.IsAllMeasured = SelectedShipping.ShippingDocuments?.All(sd => sd.IsAllMeasured) ?? false;
var shippingDate = _measuringDates.FirstOrDefault(shipping => shipping.ShippingId == SelectedShipping.Id);
if (shippingDate != null) shippingDate.IsMeasured = SelectedShipping.IsAllMeasured;
MeasuringValuesHelper.SetShippingItemTotalMeasuringValues(shippingItem);
shippingItem.ShippingItemPallets ??= new List<ShippingItemPallet>(shippingItem.PalletsOnDocument);
if (shippingItem.ShippingItemPallets.Count >= shippingItem.PalletsOnDocument) return;
for (var i = shippingItem.ShippingItemPallets.Count; i < shippingItem.PalletsOnDocument; i++)
shippingItem.ShippingItemPallets.Add(new ShippingItemPallet
{
ShippingItemId = shippingItem.Id,
PalletWeight = shippingItem.Pallet?.Weight ?? 0,
CreatorId = LoggedInModel.CustomerDto?.Id,
ModifierId = LoggedInModel.CustomerDto?.Id
});
//if (shippingItem.Id == SelectedShippingItem?.Id) return;
//await RefreshSelectedShippingItemMeasuredValuesFromDb(shippingItem.Id);
}
private async Task RefreshSelectedShippingItemMeasuredValuesFromDb(int shippingItemId)
{
RefreshSelectedShippingItemMeasuredValuesFromDb(await FruitBankSignalRClient.GetShippingItemById(shippingItemId));
}
private void RefreshSelectedShippingItemMeasuredValuesFromDb(ShippingItem? shippingItemFromDb)
{
if (SelectedShipping == null || SelectedShippingDocument == null || shippingItemFromDb == null) return;
//SelectedShippingItem.MeasuredQuantity = shippingItemFromDb.MeasuredQuantity;
//SelectedShippingItem.MeasuredNetWeight = shippingItemFromDb.MeasuredNetWeight;
//SelectedShippingItem.MeasuredGrossWeight = shippingItemFromDb.MeasuredGrossWeight;
//SelectedShippingItem.IsMeasurable = shippingItemFromDb.IsMeasurable;
//SelectedShippingItem.IsMeasured = shippingItemFromDb.IsMeasured;
SelectedShippingDocument.ShippingItems!.UpdateCollection(shippingItemFromDb, false);
SelectedShippingItem = shippingItemFromDb;
StateHasChanged();
}
private Task OnShippingItemPalletValueChanged(ShippingItemPallet shippingItemPallet, ShippingItem shippingItem)
{
MeasuringValuesHelper.SetShippingItemTotalMeasuringValues(shippingItem);
BtnSaveEnabled = shippingItem.IsValidMeasuringValues() && shippingItemPallet.IsValidMeasuringValues(shippingItem.IsMeasurable);
StateHasChanged();
return Task.CompletedTask;
}
private Task OnShippingItemPalletSaved(ShippingItemPallet? responseShippingItemPallet)
{
if (responseShippingItemPallet != null)
{
SelectedShippingItem!.ShippingItemPallets!.UpdateCollection(responseShippingItemPallet, false);
MeasuringValuesHelper.SetShippingItemTotalMeasuringValues(SelectedShippingItem);
}
else DisplayErrorText($"Sikertelen volt a raklap adatainak mentése!");
StateHasChanged();
return Task.CompletedTask;
}
private bool IsShippingitemPalletMeasuredAndValid(ShippingItemPallet shippingItemPallet)
=> MeasuringService.IsCustomItemPalletMeasuredAndValid(shippingItemPallet, SelectedShippingItem!.IsMeasurable);
private void LogErrorAndDisplayText(string errorText, Exception? ex = null)
{
_logger.Error($"{errorText}", ex);
DisplayErrorText(errorText);
}
private void DisplayErrorText(string errorText)
{
_errorText = errorText;
//Nem végezhető el a mérés, nincs megadva a ProductId! Jelezze a vezetőségnek...
}
}
}