using System; using System.Collections.Generic; using System.ComponentModel.DataAnnotations; using System.ComponentModel.DataAnnotations.Schema; using System.Linq; using System.Reflection; using EveryThing.Models.CodeTable; namespace EveryThing.Models.Invoice { public class Invoice { public class InvoiceStateAttribute : Attribute { public string AllowedTypes { get; set; } = "0,1,2,3,4"; } public enum InvoiceType { [Display(Name = "Račun", ShortName = "Racun")] Invoice = 0, [Display(Name = "Naročilo", ShortName = "Narocilo")] Order = 1, [Display(Name = "Dobavnica", ShortName = "Dobavnica")] DeliveryNote = 2, [Display(Name = "Naročilo kupca", ShortName = "Narocilo")] BuyersOrder = 3, [Display(Name = "Kooperacija", ShortName = "Kooperacija")] Cooperation = 4, } public enum InvoiceState { [Display(Name = "Nov")] [InvoiceState] New = 0, [Display(Name = "Povpraševanje")] [InvoiceState(AllowedTypes = "1")] Inquiry = 1, [Display(Name = "Ponudba")] [InvoiceState(AllowedTypes = "3")] Offer = 2, [Display(Name = "Potrjen")] [InvoiceState] Confirmed = 10, [Display(Name = "Potrditev naročila")] [InvoiceState(AllowedTypes = "3")] OfferConfirmation = 15, [Display(Name = "Zaprt")] [InvoiceState] Closed = 20 } [Key] public int IdInvoice { get; set; } [Required] [ForeignKey("Company")] public int IdCompanyFk { get; set; } [Required] [Display(Name = "Datum")] [DisplayFormat(DataFormatString = "{0:dd.MM.yyyy}", ApplyFormatInEditMode = true)] public DateTime Date { get; set; } [Required] [Display(Name = "Tip")] public InvoiceType Type { get; set; } [Required] [Display(Name = "Status")] public InvoiceState State { get; set; } = InvoiceState.New; [Required] [ForeignKey("Partner")] [Display(Name = "Partner")] public int? IdPartnerFk { get; set; } [ForeignKey("Projekt")] [Display(Name = "Projekt")] public int? IdProjectFk { get; set; } [Required] public int InvoiceNumber { get; set; } [Required] public int InvoiceYear { get; set; } [Display(Name = "Uvodno besedilo")] public string PreText { get; set; } [Display(Name = "Zaključno besedilo")] public string PostText { get; set; } [Display(Name = "Opomba")] public string Note { get; set; } [Display(Name = "Številka naročila kupca")] public string BuyersOrderNumber { get; set; } /// /// Dobavnica/racun => datum odpreme /// Narocilo => dobavni rok /// [Display(Name = "Datum odpreme")] [DisplayFormat(DataFormatString = "{0:dd.MM.yyyy}", ApplyFormatInEditMode = true)] public DateTime? DateOfDispatch { get; set; } [NotMapped] public string InvoiceNumberFormatted => $"{InvoiceYear}-{InvoiceNumber}"; [NotMapped] public string InvoiceTypeString { get { if (Type == InvoiceType.BuyersOrder && State == InvoiceState.Offer) { return "Ponudba"; } if (Type == InvoiceType.BuyersOrder && State == InvoiceState.OfferConfirmation) { return "Potrditev_narocila"; } if (Type == InvoiceType.Order && State == InvoiceState.Inquiry) { return "Povprasevanje"; } return Type.GetType().GetMember(Type.ToString()).First().GetCustomAttribute()?.ShortName; } } [NotMapped] public string InvoiceNumberFull => $"{InvoiceTypeString}-{InvoiceNumberFormatted}"; [NotMapped] public string InvoiceTypeHeaderString => GetInvoiceTypeHeaderString(Type); // ForeignKey public CodeTableCompany Company { get; set; } public CodeTablePartner Partner { get; set; } public Project.Project Project{ get; set; } // InvoicePart [InverseProperty("Invoice")] public virtual ICollection InvoiceInvoiceItem { get; set; } public static string GetInvoiceTypeHeaderString(InvoiceType iType) { return iType switch { InvoiceType.Order => "Naročila dobaviteljem", 0 => "Računi", InvoiceType.DeliveryNote => "Dobavnice", InvoiceType.BuyersOrder => "Naročila kupcev", InvoiceType.Cooperation => "Kooperacije", _ => "Fakture" }; } } }