106 lines
3.9 KiB
C#
106 lines
3.9 KiB
C#
using System.Globalization;
|
|
using System.Linq.Expressions;
|
|
using FruitBank.Common.Entities;
|
|
using FruitBank.Common.Interfaces;
|
|
using LinqToDB.Mapping;
|
|
using Mango.Nop.Core.Dtos;
|
|
using Newtonsoft.Json;
|
|
using Nop.Core;
|
|
using Nop.Core.Domain.Common;
|
|
using Nop.Core.Domain.Orders;
|
|
|
|
namespace FruitBank.Common.Dtos;
|
|
|
|
public class OrderItemDto : MgOrderItemDto<ProductDto>, IOrderItemDto
|
|
{
|
|
[NotColumn, JsonIgnore, System.Text.Json.Serialization.JsonIgnore]
|
|
private static Expression<Func<OrderItemDto, GenericAttribute, bool>> RelationWithGenericAttribute => (orderItemDto, genericAttribute) =>
|
|
orderItemDto.Id == genericAttribute.EntityId && genericAttribute.KeyGroup == nameof(OrderItem);
|
|
|
|
|
|
[Association(ThisKey = nameof(Id), OtherKey = nameof(GenericAttribute.EntityId), ExpressionPredicate = nameof(RelationWithGenericAttribute), CanBeNull = true)]
|
|
public List<GenericAttribute> GenericAttributes { get; set; }
|
|
|
|
[Association(ThisKey = nameof(Id), OtherKey = nameof(OrderItemPallet.OrderItemId), CanBeNull = true)]
|
|
public List<OrderItemPallet> OrderItemPallets { get; set; }
|
|
|
|
[Association(ThisKey = nameof(OrderId), OtherKey = nameof(OrderDto.Id), CanBeNull = false)]
|
|
public OrderDto OrderDto { get; set; }
|
|
|
|
[NotColumn, JsonIgnore, System.Text.Json.Serialization.JsonIgnore]
|
|
public bool IsMeasured
|
|
{
|
|
get => IsMeasuredAndValid();
|
|
set => throw new Exception($"OrderItemDto.IsMeasured not set");
|
|
}
|
|
|
|
[NotColumn, JsonIgnore, System.Text.Json.Serialization.JsonIgnore]
|
|
public bool IsMeasurable
|
|
{
|
|
get => ProductDto!.IsMeasurable;
|
|
set => throw new Exception($"OrderItemDto.IsMeasurable not set");
|
|
}
|
|
|
|
[NotColumn, JsonIgnore, System.Text.Json.Serialization.JsonIgnore]
|
|
public int TrayQuantity
|
|
{
|
|
get => OrderItemPallets.Sum(x => x.TrayQuantity);
|
|
set => throw new Exception($"OrderItemDto.TrayQuantity not set");
|
|
}
|
|
|
|
[NotColumn, JsonIgnore, System.Text.Json.Serialization.JsonIgnore]
|
|
public double NetWeight
|
|
{
|
|
get
|
|
{
|
|
return double.Round(OrderItemPallets.Sum(x => x.NetWeight), 1);
|
|
//return CommonHelper.To<double>(GenericAttributes.SingleOrDefault(x => x.Key == nameof(IMeasuringNetWeight.NetWeight))?.Value ?? "0");
|
|
}
|
|
set
|
|
{
|
|
throw new Exception($"OrderItemDto.NetWeight not set");
|
|
////Direkt legyen exception! - J.
|
|
//var ga = GenericAttributes?.SingleOrDefault(x => x.Key == nameof(IMeasuringNetWeight.NetWeight))!;
|
|
//ga.Value = value.ToString(CultureInfo.InvariantCulture);
|
|
}
|
|
}
|
|
|
|
[NotColumn, JsonIgnore, System.Text.Json.Serialization.JsonIgnore]
|
|
public double GrossWeight
|
|
{
|
|
get => CommonHelper.To<double>(GenericAttributes.SingleOrDefault(x => x.Key == nameof(IMeasuringGrossWeight.GrossWeight))?.Value ?? "0");
|
|
set
|
|
{
|
|
//Direkt legyen exception! - J.
|
|
var ga = GenericAttributes?.SingleOrDefault(x => x.Key == nameof(IMeasuringGrossWeight.GrossWeight))!;
|
|
ga.Value = value.ToString(CultureInfo.InvariantCulture);
|
|
}
|
|
}
|
|
|
|
public OrderItemDto() : base()
|
|
{
|
|
}
|
|
|
|
public OrderItemDto(int orderItemId) : base(orderItemId)
|
|
{
|
|
}
|
|
|
|
public OrderItemDto(OrderItem orderItem) : base(orderItem)
|
|
{
|
|
}
|
|
|
|
public void CopyEntityValuesToDto(OrderItem entity, ProductDto productDto, List<OrderItemPallet> orderItemPallets)
|
|
{
|
|
base.CopyEntityValuesToDto(entity, productDto);
|
|
|
|
InitializeOrderItemPallets(orderItemPallets);
|
|
}
|
|
|
|
public void InitializeOrderItemPallets(List<OrderItemPallet> orderItemPallets)
|
|
{
|
|
OrderItemPallets = orderItemPallets;
|
|
}
|
|
|
|
public bool IsMeasuredAndValid() => Id > 0 && IsValidMeasuringValues() && OrderItemPallets.All(oip => oip.IsMeasuredAndValid(IsMeasurable));
|
|
public bool IsValidMeasuringValues() => OrderItemPallets.Count > 0 && (!IsMeasurable || NetWeight > 0) && TrayQuantity == Quantity;
|
|
} |