BillLine.cs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. using System;
  2. using System.Linq;
  3. using InABox.Core;
  4. namespace Comal.Classes
  5. {
  6. public class BillLineLink : EntityLink<BillLine>
  7. {
  8. [NullEditor]
  9. public override Guid ID { get; set; }
  10. }
  11. [UserTracking(typeof(Bill))]
  12. public class BillLine : Entity, IPersistent, IRemotable, IOneToMany<Bill>, ITaxable, ILicense<AccountsPayableLicense>, IPostableFragment<Bill>
  13. {
  14. [EntityRelationship(DeleteAction.Cascade)]
  15. [NullEditor]
  16. public BillLink BillLink { get; set; }
  17. private class PurchaseOrderItemLookup : LookupDefinitionGenerator<PurchaseOrderItem, BillLine>
  18. {
  19. public override Filter<PurchaseOrderItem> DefineFilter(BillLine[] items)
  20. {
  21. if (!items.Any())
  22. return new Filter<PurchaseOrderItem>().None();
  23. var supplierID = items.Select(x => x.BillLink.SupplierLink.ID).Distinct().SingleOrDefault();
  24. if(supplierID == Guid.Empty)
  25. return new Filter<PurchaseOrderItem>().None();
  26. return new Filter<PurchaseOrderItem>(x => x.PurchaseOrderLink.SupplierLink.ID).IsEqualTo(supplierID)
  27. .And(x=>x.BillLine.ID).IsEqualTo(Guid.Empty);
  28. }
  29. public override Columns<BillLine> DefineFilterColumns()
  30. {
  31. return new Columns<BillLine>(x => x.BillLink.SupplierLink.ID);
  32. }
  33. }
  34. [LookupDefinition(typeof(PurchaseOrderItemLookup))]
  35. [EntityRelationship(DeleteAction.SetNull)]
  36. [EditorSequence(1)]
  37. public PurchaseOrderItemLink OrderItem { get; set; }
  38. private class ProductLookupGenerator : LookupDefinitionGenerator<Product, BillLine>
  39. {
  40. public override Filter<Product>? DefineFilter(BillLine[] items)
  41. => new Filter<Product>(x => x.NonStock);
  42. }
  43. [EditorSequence(2)]
  44. [LookupDefinition(typeof(ProductLookupGenerator))]
  45. public ProductLink Product { get; set; }
  46. [MemoEditor]
  47. [EditorSequence(2)]
  48. public string Description { get; set; }
  49. [EditorSequence(3)]
  50. public PurchaseGLCodeLink PurchaseGL { get; set; }
  51. [EditorSequence(4)]
  52. public CostCentreLink CostCentre { get; set; }
  53. [CurrencyEditor(Summary = Summary.Sum)]
  54. [EditorSequence(5)]
  55. public double ExTax { get; set; }
  56. [RequiredColumn]
  57. [EditorSequence(6)]
  58. public TaxCodeLink TaxCode { get; set; }
  59. [DoubleEditor(Editable = Editable.Hidden)]
  60. public double TaxRate { get; set; }
  61. [CurrencyEditor(Summary = Summary.Sum)]
  62. public double Tax { get; set; }
  63. [CurrencyEditor(Summary = Summary.Sum)]
  64. [EditorSequence(7)]
  65. public double IncTax { get; set; }
  66. [EditorSequence(8)]
  67. public JobLink Job { get; set; }
  68. [NullEditor]
  69. public string PostedReference { get; set; }
  70. static BillLine()
  71. {
  72. LinkedProperties.Register<BillLine, PurchaseOrderItemLink, double>(x => x.OrderItem, x => x.ExTax,
  73. x => x.ExTax);
  74. LinkedProperties.Register<BillLine, PurchaseGLCodeLink, Guid>(x => x.OrderItem.PurchaseGL, x => x.ID,
  75. x => x.PurchaseGL.ID);
  76. LinkedProperties.Register<BillLine, CostCentreLink, Guid>(x => x.OrderItem.CostCentre, x => x.ID,
  77. x => x.CostCentre.ID);
  78. LinkedProperties.Register<BillLine, TaxCodeLink, Guid>(x => x.OrderItem.TaxCode, x => x.ID,
  79. x => x.TaxCode.ID);
  80. LinkedProperties.Register<BillLine, TaxCodeLink, String>(x => x.OrderItem.TaxCode, x => x.Code,
  81. x => x.TaxCode.Code);
  82. LinkedProperties.Register<BillLine, TaxCodeLink, String>(x => x.OrderItem.TaxCode, x => x.Description,
  83. x => x.TaxCode.Description);
  84. LinkedProperties.Register<BillLine, TaxCodeLink, double>(x => x.OrderItem.TaxCode, x => x.Rate,
  85. x => x.TaxCode.Rate);
  86. LinkedProperties.Register<BillLine, PurchaseOrderItemLink, double>(x => x.OrderItem, x => x.Tax,
  87. x => x.Tax);
  88. LinkedProperties.Register<BillLine, PurchaseOrderItemLink, double>(x => x.OrderItem, x => x.IncTax,
  89. x => x.IncTax);
  90. LinkedProperties.Register<BillLine, TaxCodeLink, double>(x => x.TaxCode, x => x.Rate, x => x.TaxRate);
  91. LinkedProperties.Register<BillLine, PurchaseGLCodeLink, Guid>(x => x.Product.PurchaseGL, x => x.ID, x => x.PurchaseGL.ID);
  92. LinkedProperties.Register<BillLine, CostCentreLink, Guid>(x => x.Product.CostCentre, x => x.ID, x => x.CostCentre.ID);
  93. LinkedProperties.Register<BillLine, TaxCodeLink, Guid>(x => x.Product.TaxCode, x => x.ID, x => x.TaxCode.ID);
  94. LinkedProperties.Register<BillLine, TaxCodeLink, string>(x => x.Product.TaxCode, x => x.Code, x => x.TaxCode.Code);
  95. LinkedProperties.Register<BillLine, TaxCodeLink, string>(x => x.Product.TaxCode, x => x.Description, x => x.TaxCode.Description);
  96. LinkedProperties.Register<BillLine, TaxCodeLink, double>(x => x.Product.TaxCode, x => x.Rate, x => x.TaxCode.Rate);
  97. }
  98. private static readonly Column<BillLine> OrderItemColumn = new Column<BillLine>(x => x.OrderItem.ID);
  99. private static readonly Column<BillLine> ProductColumn = new Column<BillLine>(x => x.Product.ID);
  100. private static readonly Column<BillLine> JobColumn = new Column<BillLine>(x => x.Job.ID);
  101. protected override void DoPropertyChanged(string name, object? before, object? after)
  102. {
  103. base.DoPropertyChanged(name, before, after);
  104. if (OrderItemColumn.IsEqualTo(name) && after is Guid orderItemID && orderItemID != Guid.Empty)
  105. {
  106. Product.ID = Guid.Empty;
  107. Product.Clear();
  108. Job.ID = Guid.Empty;
  109. Job.Clear();
  110. }
  111. else if((ProductColumn.IsEqualTo(name) && after is Guid productID && productID != Guid.Empty)
  112. || (JobColumn.IsEqualTo(name) && after is Guid jobID && jobID != Guid.Empty))
  113. {
  114. OrderItem.ID = Guid.Empty;
  115. OrderItem.Clear();
  116. }
  117. }
  118. }
  119. }