using EgwCoreLib.Lux.Core.Generic;
using EgwCoreLib.Lux.Data.DbModel.Production;
using Newtonsoft.Json;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using static EgwCoreLib.Lux.Core.Enums;
//
// This is here so CodeMaid doesn't reorganize this document
//
namespace EgwCoreLib.Lux.Data.DbModel.Sales
{
///
/// Classe degli ordini commerciali
///
[Table("sales_order")]
public class OrderModel
{
///
/// ID del record
///
[Key]
public int OrderID { get; set; }
///
/// Environment della richiesta
///
public EgwMultiEngineManager.Data.Constants.EXECENVIRONMENTS Envir { get; set; } = EgwMultiEngineManager.Data.Constants.EXECENVIRONMENTS.WINDOW;
///
/// Offerta correlata
///
public int OfferID { get; set; }
///
/// Anno rif Ordine
///
public int RefYear { get; set; } = DateTime.Today.Year;
///
/// Numero prog Ordine nell'anno (da calcolare)
///
public int RefNum { get; set; } = 0;
///
/// Indice revisione
///
public int RefRev { get; set; } = 1;
///
/// Codice calcolato Ordine ANNO.NUMERO.REV
///
[NotMapped]
public string OrderCode
{
get => $"PO.{RefYear:00}.{RefNum:00000}.{RefRev:00}";
}
///
/// Descrizione generale
///
public string Description { get; set; } = "";
///
/// ID Cliente
///
public int CustomerID { get; set; }
///
/// ID Dealer
///
public int DealerID { get; set; }
///
/// Dizionario serializzato delle preselezioni (opzionale)
///
public string DictPresel { get; set; } = "";
///
/// Validità Ordine
///
public DateTime ValidUntil { get; set; } = DateTime.Now.AddMonths(1);
///
/// DataOra inserimento
///
public DateTime Inserted { get; set; } = DateTime.Now;
///
/// DataOra ultima modifica
///
public DateTime Modified { get; set; } = DateTime.Now;
///
/// DataOra richiesta per il completamento dell'ordine
///
public DateTime DueDateReq { get; set; } = DateTime.Today.AddDays(30);
///
/// DataOra promessa per il completamento dell'ordine
///
public DateTime DueDateProm { get; set; } = DateTime.Today.AddDays(60);
///
/// note di consegna (opzionali)
///
public string ConsNote { get; set; } = "";
///
/// Enum stato Ordine
///
public OrderStates OrderState { get; set; } = OrderStates.Created;
///
/// Sconto applicato (deve essere < del MAX)
///
public double Discount { get; set; } = 0;
///
/// History associata (tipicamente richieste batch lunghe come stima/prod...)
///
public string RawHistory { get; set; } = "";
[NotMapped]
public List LogHistory
{
get
{
List currHist = new List();
if (!string.IsNullOrEmpty(RawHistory))
{
try
{
currHist = JsonConvert.DeserializeObject>(RawHistory) ?? new List();
}
catch
{ }
}
return currHist;
}
set
{
RawHistory = JsonConvert.SerializeObject(value);
}
}
///
/// Numero Item compresi
///
[NotMapped]
public double NumItems
{
get => OrderRowNav?.Sum(x => x.Qty) ?? 0;
}
///
/// Numero ProdItem compresi
///
[NotMapped]
public double NumProdItems
{
get => OrderRowNav?.Sum(x => x.ProdItemQtyTot) ?? 0;
}
///
/// Numero Item compresi
///
[NotMapped]
public int NumRows
{
get => OrderRowNav?.Count ?? 0;
}
///
/// Costo totale offerta (rock bottom)
///
[NotMapped]
public double TotalCost
{
get => OrderRowNav?.Sum(x => x.TotalCost) ?? 0;
}
///
/// Prezzo totale offerta (compreso di amrginalità)
///
[NotMapped]
public double TotalPrice
{
get => OrderRowNav?.Sum(x => x.TotalPrice) ?? 0;
}
///
/// Sconto massimo applicabile
///
[NotMapped]
public double MaxDiscount
{
get => (TotalCost > 0 && TotalPrice > TotalCost) ? (TotalPrice - TotalCost) / TotalPrice : 0;
}
///
/// Navigazione Customer
///
[ForeignKey("CustomerID")]
public virtual CustomerModel CustomerNav { get; set; } = null!;
///
/// Navigazione Dealer
///
[ForeignKey("DealerID")]
public virtual DealerModel DealerNav { get; set; } = null!;
///
/// Navigazione Ordine
///
[ForeignKey("OfferID")]
public virtual OfferModel OfferNav { get; set; } = null!;
///
/// Navigazione alle righe ordine
///
public virtual ICollection OrderRowNav { get; set; } = new List();
}
}