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

303 lines
13 KiB
C#

using AyCode.Core.Extensions;
using AyCode.Core.Loggers;
using DevExpress.Blazor;
using FruitBank.Common.Entities;
using FruitBank.Common.Interfaces;
using FruitBank.Common.Models;
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 class ShippingDateModel
{
public int ShippingId { get; set; }
public DateTime DateTime { get; set; }
public bool IsMeasured { get; set; }
public ShippingDateModel(int shippingId, DateTime dateTime, bool isMeasured)
{
ShippingId = shippingId;
DateTime = dateTime;
IsMeasured = isMeasured;
}
}
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; }
DateTime DateTimeValue { get; set; } = DateTime.Today;
protected bool BtnSaveEnabled { get; set; }
private List<ShippingDateModel> _shippingDates = 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() ?? [];
_shippingDates = shippings.Select(shipping => new ShippingDateModel(shipping.Id, shipping.ShippingDate.Date, shipping.IsAllMeasured)).ToList();
NotMeasuredShippings = shippings.Where(shipping => DaysEqual(shipping.ShippingDate.Date, dateTime)).ToList();
SelectedShipping = NotMeasuredShippings.FirstOrDefault();
}
private async Task OnSelectedShippingDateChanged(DateTime selectedDateTime)
=> await RefreshShippingsFromDb(selectedDateTime);
private void OnCustomDisabledDate(CalendarCustomDisabledDateEventArgs args)
=> args.IsDisabled = !_shippingDates.Exists(shippingDateModel => DaysEqual(shippingDateModel.DateTime, args.Date));
private string GetCssClassNames(DateTime date)
{
if (_shippingDates.Exists(shipping => !shipping.IsMeasured && shipping.DateTime.Date <= DateTime.Now.Date && DaysEqual(shipping.DateTime, date)))
return "fw-bold text-danger";
if (_shippingDates.Exists(shipping => shipping.IsMeasured && DaysEqual(shipping.DateTime, date)))
return "fw-bold text-success";
if (_shippingDates.Exists(shipping => !shipping.IsMeasured && DaysEqual(shipping.DateTime, date)))
return "fw-bold";
return string.Empty;
}
private static bool DaysEqual(DateTime date1, DateTime date2)
=> (date1.Year == date2.Year && date1.DayOfYear == date2.DayOfYear);
private void OnSelectedShippingChanged(SelectedDataItemChangedEventArgs<Shipping> eventArgs)
{
SelectedShippingDocument = eventArgs.DataItem?.ShippingDocuments?.FirstOrDefault();
}
private void OnSelectedShippingDocumentChanged(SelectedDataItemChangedEventArgs<ShippingDocument> eventArgs)
{
SelectedShippingItem = eventArgs.DataItem?.ShippingItems?.FirstOrDefault();
}
private void UpdateShippingItemTotalMeasuringValues(ShippingItem? shippingItem)
{
if (shippingItem == null) return;
var totalMeasuringValues = GetTotalNetAndGrossWeightFromPallets(shippingItem);
shippingItem.MeasuredQuantity = totalMeasuringValues.TotalQuantity;
shippingItem.MeasuredNetWeight = totalMeasuringValues.TotalNetWeight;
shippingItem.MeasuredGrossWeight = totalMeasuringValues.TotalGrossWeight;
}
private (int TotalQuantity, double TotalNetWeight, double TotalGrossWeight) GetTotalNetAndGrossWeightFromPallets(ShippingItem? shippingItem)
{
if (shippingItem?.ShippingItemPallets == null) return (0, 0d, 0d);
var totalQuantity = 0;
var totalNetWeight = 0d;
var totalGrossWeight = 0d;
foreach (var shippingItemPallet in shippingItem.ShippingItemPallets)
{
totalQuantity += shippingItemPallet.Quantity;
totalNetWeight += shippingItemPallet.NetWeight;
totalGrossWeight += shippingItemPallet.GrossWeight;
}
return (totalQuantity, double.Round(totalNetWeight, 1), Math.Round(totalGrossWeight, 1));
}
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 = _shippingDates.FirstOrDefault(shipping => shipping.ShippingId == SelectedShipping.Id);
if (shippingDate != null) shippingDate.IsMeasured = SelectedShipping.IsAllMeasured;
UpdateShippingItemTotalMeasuringValues(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,
//PalletSize = shippingItem.PalletSize,
});
//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 async Task UpdateShippingItem(ShippingItem? shippingItem)
{
if (shippingItem != null && shippingItem.IsValidMeasuringValues())
{
BtnSaveEnabled = false;
var updatedShippingItem = await FruitBankSignalRClient.UpdateMeasuredShippingItem(shippingItem);
if (updatedShippingItem == null)
{
LogErrorAndDisplayText($"Sikertelen volt a shippingItem mentése! {shippingItem}");
return;
}
RefreshSelectedShippingItemMeasuredValuesFromDb(updatedShippingItem);
}
}
private async Task OnShippingItemPalletSaveClick(ShippingItemPallet shippingItemPallet)
{
ShippingItemPallet? responseShippingItemPallet;
if (shippingItemPallet.Id == 0) responseShippingItemPallet = await FruitBankSignalRClient.AddShippingItemPallet(shippingItemPallet);
else responseShippingItemPallet = await FruitBankSignalRClient.UpdateShippingItemPallet(shippingItemPallet);
if (responseShippingItemPallet != null)
{
shippingItemPallet.Id = responseShippingItemPallet.Id; //Az UpdateCollection miatt kell, hogy megtalálja mit kell kicserélni! - J.
SelectedShippingItem!.ShippingItemPallets!.UpdateCollection(responseShippingItemPallet, false);
UpdateShippingItemTotalMeasuringValues(SelectedShippingItem);
}
else LogErrorAndDisplayText($"Sikertelen volt a raklap adatainak mentése! {shippingItemPallet}");
StateHasChanged();
}
private bool ShippingitemPalletMeasuredAndValid(ShippingItemPallet shippingItemPallet)
=> ShippingitemPalletMeasuredAndValid(shippingItemPallet, SelectedShippingItem!.IsMeasurable);
private static bool ShippingitemPalletMeasuredAndValid(ShippingItemPallet shippingItemPallet, bool isMeasurable)
{
return shippingItemPallet.Id != 0 && shippingItemPallet.IsMeasured && shippingItemPallet.IsValidMeasuringValues(isMeasurable);
}
private async Task HandleValidSubmit()
{
await UpdateShippingItem(SelectedShippingItem);
}
private void HandleInvalidSubmit()
{
//FormValidationState = @"Form data is invalid";
}
protected void OnItemUpdating(string fieldName, object newValue)
{
BtnSaveEnabled = false;
if (SelectedShippingItem == null) return;
switch (fieldName)
{
//case nameof(ShippingItem.Name):
// SelectedShippingItem.Name = newValue.ToString() ?? string.Empty;
// break;
//case nameof(ShippingItem.GrossWeight):
// SelectedShippingItem.GrossWeight = (double)newValue;
// break;
//case nameof(ShippingItem.NetWeight):
// SelectedShippingItem.NetWeight = (double)newValue;
// break;
case nameof(ShippingItem.MeasuredQuantity):
SelectedShippingItem.MeasuredQuantity = (int)newValue <= 0 ? 0 : (int)newValue;
break;
case nameof(ShippingItem.MeasuredNetWeight):
SelectedShippingItem.MeasuredNetWeight = !SelectedShippingItem.IsMeasurable || (double)newValue <= 0 ? 0 : (double)newValue;
break;
case nameof(ShippingItem.MeasuredGrossWeight):
SelectedShippingItem.MeasuredGrossWeight = !SelectedShippingItem.IsMeasurable || (double)newValue <= 0 ? 0 : (double)newValue;
break;
}
BtnSaveEnabled = SelectedShippingItem.IsValidMeasuringValues();
}
protected void OnItemUpdating2(string fieldName, object newValue, ShippingItemPallet shippingItemPallet)
{
BtnSaveEnabled = false;
if (SelectedShippingItem == null) return;
switch (fieldName)
{
case nameof(ShippingItemPallet.Quantity):
shippingItemPallet.Quantity = (int)newValue;
SelectedShippingItem.MeasuredQuantity = GetTotalNetAndGrossWeightFromPallets(SelectedShippingItem).TotalQuantity;
break;
case nameof(ShippingItemPallet.NetWeight):
shippingItemPallet.NetWeight = (double)newValue;
SelectedShippingItem.MeasuredNetWeight = GetTotalNetAndGrossWeightFromPallets(SelectedShippingItem).TotalNetWeight;
break;
case nameof(ShippingItemPallet.GrossWeight):
shippingItemPallet.GrossWeight = (double)newValue;
SelectedShippingItem.MeasuredGrossWeight = GetTotalNetAndGrossWeightFromPallets(SelectedShippingItem).TotalGrossWeight;
break;
}
BtnSaveEnabled = SelectedShippingItem.IsValidMeasuringValues();
}
private void LogErrorAndDisplayText(string errorText, Exception? ex = null)
{
_errorText = errorText;
_logger.Error($"{errorText}", ex);
//Nem végezhető el a mérés, nincs megadva a ProductId! Jelezze a vezetőségnek...
}
protected void OnAnyMeasuringValueKeyDown(KeyboardEventArgs e)
{
BtnSaveEnabled = SelectedShippingItem?.IsValidMeasuringValues() ?? false;
}
}
}