using AyCode.Core.Extensions; using AyCode.Utils.Extensions; using FruitBank.Common.Entities; using FruitBank.Common.Enums; using FruitBank.Common.Interfaces; using FruitBank.Common.Models; using LinqToDB.Mapping; using Mango.Nop.Core.Dtos; using Mango.Nop.Core.Extensions; using Mango.Nop.Core.Interfaces; using Newtonsoft.Json; using Nop.Core; //using Nop.Core.Domain.Catalog; using Nop.Core.Domain.Common; using Nop.Core.Domain.Orders; using System.Diagnostics.CodeAnalysis; using System.Globalization; using System.Linq.Expressions; namespace FruitBank.Common.Dtos; public class OrderDto : MgOrderDto, IOrderDto { [NotColumn, JsonIgnore, System.Text.Json.Serialization.JsonIgnore] private static Expression> RelationWithGenericAttribute => (orderDto, genericAttributeDto) => orderDto.Id == genericAttributeDto.EntityId && genericAttributeDto.KeyGroup == nameof(Order); [Association(ThisKey = nameof(Id), OtherKey = nameof(GenericAttribute.EntityId), ExpressionPredicate = nameof(RelationWithGenericAttribute), CanBeNull = true)] public List GenericAttributes { get; set; } [NotColumn, JsonIgnore, System.Text.Json.Serialization.JsonIgnore] public bool IsMeasured { get => IsMeasuredAndValid(); set => throw new Exception($"OrderDto.IsMeasured not set"); } [NotColumn, JsonIgnore, System.Text.Json.Serialization.JsonIgnore] public bool IsMeasurable { get => OrderItemDtos.Any(oi => oi.IsMeasurable); set => throw new Exception($"OrderDto.IsMeasurable not set"); } [NotColumn, JsonIgnore, System.Text.Json.Serialization.JsonIgnore] public string TimeOfReceiptText { get { var resultText = string.Empty; if (IsComplete) resultText = "[AUDITED]"; else if (IsMeasured) resultText = "[KÉSZ]"; else if (MeasurementOwnerId > 0) resultText = "[FOLYAMATBAN]"; //else if (MeasurementOwnerId == ??) resultText = "[SAJÁT MÉRÉS]"; //else if (MeasurementOwnerId > 0) resultText = "[MÁSOK MÉRIK]"; return $"{resultText} {DateOfReceiptOrCreated:H:mm} - {Customer.Company}"; } } [NotColumn, JsonIgnore, System.Text.Json.Serialization.JsonIgnore] public DateTime DateOfReceiptOrCreated => DateOfReceipt ?? CreatedOnUtc; [NotColumn, JsonIgnore, System.Text.Json.Serialization.JsonIgnore] public DateTime? DateOfReceipt => GenericAttributes.GetValueOrNull(nameof(IOrderDto.DateOfReceipt)); [NotColumn, JsonIgnore, System.Text.Json.Serialization.JsonIgnore] public int RevisorId => GenericAttributes.GetValueOrDefault(nameof(IOrderDto.RevisorId), 0); [NotColumn, JsonIgnore, System.Text.Json.Serialization.JsonIgnore] public int MeasurementOwnerId => GenericAttributes.GetValueOrDefault(nameof(IOrderDto.MeasurementOwnerId), 0); [NotColumn, JsonIgnore, System.Text.Json.Serialization.JsonIgnore] public bool IsAllOrderItemAudited => OrderItemDtos.Count > 0 && OrderItemDtos.All(oi => oi.IsAudited); [NotColumn, JsonIgnore, System.Text.Json.Serialization.JsonIgnore] public bool IsAllOrderItemAvgWeightValid => OrderItemDtos.All(oi => oi.AverageWeightIsValid); [NotColumn, JsonIgnore, System.Text.Json.Serialization.JsonIgnore] public MeasuringStatus MeasuringStatus { get { if (IsComplete) return MeasuringStatus.Audited; if (IsMeasured) return MeasuringStatus.Finnished; return OrderItemDtos.Any(oip => oip.MeasuringStatus >= MeasuringStatus.Started) ? MeasuringStatus.Started : MeasuringStatus.NotStarted; } } //if (GenericAttributes.TryGetValue(nameof(IOrderDto.DateOfReceipt), out var value)) return value; //var dateOfReceipt = GenericAttributes.SingleOrDefault(x => x.Key == nameof(IOrderDto.DateOfReceipt))?.Value ?? string.Empty; //return dateOfReceipt.IsNullOrWhiteSpace() ? null : CommonHelper.To(dateOfReceipt); public OrderDto() :base() { } public OrderDto(int orderId) : base(orderId) { } public OrderDto(Order order) : base(order) { } [NotColumn, JsonIgnore, System.Text.Json.Serialization.JsonIgnore] public bool IsComplete => OrderStatus == OrderStatus.Complete; public bool HasMeasuringAccess(int? customerId, bool isRevisorUser = false) => isRevisorUser || (customerId.HasValue && !IsComplete && (customerId.Value == MeasurementOwnerId || MeasurementOwnerId == 0)); public bool IsMeasuredAndValid() => Id > 0 && OrderItemDtos.Count > 0 && OrderItemDtos.All(x => x.IsMeasured); public bool IsValidMeasuringValues() => OrderItemDtos.Count > 0 && OrderItemDtos.All(x => x.IsValidMeasuringValues()); public bool IsOtherMeasuringInProgress(int? customerId) => customerId.GetValueOrDefault(0) != 0 && OrderItemDtos.Any(oi => oi.IsOtherMeasuringInProgress(customerId)); }