226 lines
9.2 KiB
C#
226 lines
9.2 KiB
C#
using AyCode.Core.Loggers;
|
|
using DevExpress.Blazor;
|
|
using FruitBank.Common.Entities;
|
|
using FruitBank.Common.Models;
|
|
using FruitBankHybrid.Shared.Services.Loggers;
|
|
using FruitBankHybrid.Shared.Services.SignalRs;
|
|
using Microsoft.AspNetCore.Components;
|
|
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 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!;
|
|
|
|
private string _userName = "Partner name";
|
|
private string _message = string.Empty;
|
|
private readonly List<(string User, string Text)> _messages = [];
|
|
|
|
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 async Task OnSelectedShippingItemChanged(SelectedDataItemChangedEventArgs<ShippingItem> eventArgs)
|
|
{
|
|
BtnSaveEnabled = false;
|
|
|
|
if (eventArgs.DataItem == null)
|
|
{
|
|
SelectedShippingItem = null;
|
|
return;
|
|
}
|
|
|
|
await RefreshSelectedShippingItemMeasuredValuesFromDb(eventArgs.DataItem.Id);
|
|
}
|
|
|
|
private async Task RefreshSelectedShippingItemMeasuredValuesFromDb(int shippingItemId)
|
|
{
|
|
RefreshSelectedShippingItemMeasuredValuesFromDb(await FruitBankSignalRClient.GetShippingItemById(shippingItemId));
|
|
}
|
|
|
|
private void RefreshSelectedShippingItemMeasuredValuesFromDb(ShippingItem? shippingItemFromDb)
|
|
{
|
|
if (SelectedShippingItem == null || shippingItemFromDb == null) return;
|
|
|
|
SelectedShippingItem.MeasuredQuantity = shippingItemFromDb.MeasuredQuantity;
|
|
SelectedShippingItem.MeasuredNetWeight = shippingItemFromDb.MeasuredNetWeight;
|
|
SelectedShippingItem.MeasuredGrossWeight = shippingItemFromDb.MeasuredGrossWeight;
|
|
//SelectedShippingItem.IsMeasurable = shippingItemFromDb.IsMeasurable;
|
|
SelectedShippingItem.IsMeasured = shippingItemFromDb.IsMeasured;
|
|
|
|
if (SelectedShippingDocument != null)
|
|
SelectedShippingDocument.IsAllMeasured = SelectedShippingDocument.ShippingItems?.All(si => si.IsMeasured) ?? false;
|
|
|
|
if (SelectedShipping != null)
|
|
{
|
|
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;
|
|
}
|
|
|
|
|
|
//if (SelectedShippingItem is { IsMeasured: true })
|
|
//{
|
|
// SelectedShippingDocument?.ShippingItems?.Remove(SelectedShippingItem);
|
|
// SelectedShippingItem = SelectedShippingDocument?.ShippingItems?.FirstOrDefault();
|
|
//}
|
|
|
|
StateHasChanged();
|
|
}
|
|
|
|
private async Task UpdateShippingItem(ShippingItem? shippingItem)
|
|
{
|
|
if (shippingItem != null && shippingItem.IsValidMeasuringValues())
|
|
{
|
|
BtnSaveEnabled = false;
|
|
|
|
var updatedShippingItem = await FruitBankSignalRClient.UpdateMeasuredShippingItem(shippingItem);
|
|
if (updatedShippingItem == null)
|
|
{
|
|
_logger.Error($"Sikertelen volt a shippingItem mentése! Id: {shippingItem.Id}");
|
|
//TODO: - J.
|
|
|
|
return;
|
|
}
|
|
|
|
RefreshSelectedShippingItemMeasuredValuesFromDb(updatedShippingItem);
|
|
}
|
|
}
|
|
|
|
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 ? null : (int)newValue;
|
|
break;
|
|
case nameof(ShippingItem.MeasuredNetWeight):
|
|
SelectedShippingItem.MeasuredNetWeight = !SelectedShippingItem.IsMeasurable || (double)newValue <= 0 ? null : (double)newValue;
|
|
break;
|
|
case nameof(ShippingItem.MeasuredGrossWeight):
|
|
SelectedShippingItem.MeasuredGrossWeight = !SelectedShippingItem.IsMeasurable || (double)newValue <= 0 ? null : (double)newValue;
|
|
break;
|
|
}
|
|
|
|
if (SelectedShippingItem.IsValidMeasuringValues())
|
|
BtnSaveEnabled = true;
|
|
}
|
|
|
|
private async Task GetPartner()
|
|
{
|
|
var measuringModel = new MeasuringModel();
|
|
|
|
if (int.TryParse(_message, out var partnerId))
|
|
{
|
|
_message = string.Empty;
|
|
|
|
measuringModel.Name = (await FruitBankSignalRClient.GetPartnerById(partnerId))?.Name!;
|
|
//measuringModel.Name = (await FruitBankSignalRClient.GetMeasuringModelByShippingId(partnerId))?.Name!;
|
|
}
|
|
|
|
_messages.Add((_userName, measuringModel?.Name ?? "ERROR"));
|
|
//StateHasChanged();
|
|
}
|
|
}
|
|
}
|