BillLine.cs 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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 Columns.None<BillLine>().Add(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 ConsignmentLookup : LookupDefinitionGenerator<Consignment, BillLine>
  39. {
  40. public override Filter<Consignment> DefineFilter(BillLine[] items)
  41. {
  42. if (!items.Any())
  43. return new Filter<Consignment>().None();
  44. var supplierID = items.Select(x => x.BillLink.SupplierLink.ID).Distinct().SingleOrDefault();
  45. if(supplierID == Guid.Empty)
  46. return new Filter<Consignment>().None();
  47. return new Filter<Consignment>(x => x.Supplier.ID).IsEqualTo(supplierID)
  48. .And(x=>x.BillLine.ID).IsEqualTo(Guid.Empty);
  49. }
  50. public override Columns<BillLine> DefineFilterColumns()
  51. {
  52. return Columns.None<BillLine>().Add(x => x.BillLink.SupplierLink.ID);
  53. }
  54. }
  55. [LookupDefinition(typeof(ConsignmentLookup))]
  56. [EntityRelationship(DeleteAction.SetNull)]
  57. public ConsignmentLink Consignment { get; set; }
  58. private class ProductLookupGenerator : LookupDefinitionGenerator<Product, BillLine>
  59. {
  60. public override Filter<Product>? DefineFilter(BillLine[] items)
  61. => new Filter<Product>(x => x.NonStock);
  62. }
  63. [EditorSequence(2)]
  64. [LookupDefinition(typeof(ProductLookupGenerator))]
  65. public ProductLink Product { get; set; }
  66. [MemoEditor]
  67. [EditorSequence(2)]
  68. public string Description { get; set; }
  69. [EditorSequence(3)]
  70. public PurchaseGLCodeLink PurchaseGL { get; set; }
  71. [EditorSequence(4)]
  72. public CostCentreLink CostCentre { get; set; }
  73. [CurrencyEditor(Summary = Summary.Sum)]
  74. [EditorSequence(5)]
  75. public double ExTax { get; set; }
  76. [RequiredColumn]
  77. [EditorSequence(6)]
  78. public TaxCodeLink TaxCode { get; set; }
  79. [DoubleEditor(Editable = Editable.Hidden)]
  80. public double TaxRate { get; set; }
  81. [CurrencyEditor(Summary = Summary.Sum)]
  82. [EditorSequence(7)]
  83. public double Tax { get; set; }
  84. [CurrencyEditor(Summary = Summary.Sum)]
  85. [EditorSequence(8)]
  86. public double IncTax { get; set; }
  87. [EditorSequence(9)]
  88. public JobLink Job { get; set; }
  89. [NullEditor]
  90. public string PostedReference { get; set; }
  91. static BillLine()
  92. {
  93. LinkedProperties.Register<BillLine, PurchaseOrderItemLink, double>(x => x.OrderItem, x => x.ExTax,
  94. x => x.ExTax);
  95. LinkedProperties.Register<BillLine, PurchaseGLCodeLink, Guid>(x => x.OrderItem.PurchaseGL, x => x.ID,
  96. x => x.PurchaseGL.ID);
  97. LinkedProperties.Register<BillLine, CostCentreLink, Guid>(x => x.OrderItem.CostCentre, x => x.ID,
  98. x => x.CostCentre.ID);
  99. LinkedProperties.Register<BillLine, TaxCodeLink, Guid>(x => x.OrderItem.TaxCode, x => x.ID,
  100. x => x.TaxCode.ID);
  101. LinkedProperties.Register<BillLine, TaxCodeLink, String>(x => x.OrderItem.TaxCode, x => x.Code,
  102. x => x.TaxCode.Code);
  103. LinkedProperties.Register<BillLine, TaxCodeLink, String>(x => x.OrderItem.TaxCode, x => x.Description,
  104. x => x.TaxCode.Description);
  105. LinkedProperties.Register<BillLine, TaxCodeLink, double>(x => x.OrderItem.TaxCode, x => x.Rate,
  106. x => x.TaxCode.Rate);
  107. LinkedProperties.Register<BillLine, PurchaseOrderItemLink, double>(x => x.OrderItem, x => x.Tax,
  108. x => x.Tax);
  109. LinkedProperties.Register<BillLine, PurchaseOrderItemLink, double>(x => x.OrderItem, x => x.IncTax,
  110. x => x.IncTax);
  111. LinkedProperties.Register<BillLine, TaxCodeLink, double>(x => x.TaxCode, x => x.Rate, x => x.TaxRate);
  112. LinkedProperties.Register<BillLine, PurchaseGLCodeLink, Guid>(x => x.Product.PurchaseGL, x => x.ID, x => x.PurchaseGL.ID);
  113. LinkedProperties.Register<BillLine, CostCentreLink, Guid>(x => x.Product.CostCentre, x => x.ID, x => x.CostCentre.ID);
  114. LinkedProperties.Register<BillLine, TaxCodeLink, Guid>(x => x.Product.TaxCode, x => x.ID, x => x.TaxCode.ID);
  115. LinkedProperties.Register<BillLine, TaxCodeLink, string>(x => x.Product.TaxCode, x => x.Code, x => x.TaxCode.Code);
  116. LinkedProperties.Register<BillLine, TaxCodeLink, string>(x => x.Product.TaxCode, x => x.Description, x => x.TaxCode.Description);
  117. LinkedProperties.Register<BillLine, TaxCodeLink, double>(x => x.Product.TaxCode, x => x.Rate, x => x.TaxCode.Rate);
  118. }
  119. private static readonly Column<BillLine> OrderItemColumn = new Column<BillLine>(x => x.OrderItem.ID);
  120. private static readonly Column<BillLine> ProductColumn = new Column<BillLine>(x => x.Product.ID);
  121. private static readonly Column<BillLine> JobColumn = new Column<BillLine>(x => x.Job.ID);
  122. protected override void DoPropertyChanged(string name, object? before, object? after)
  123. {
  124. base.DoPropertyChanged(name, before, after);
  125. if (OrderItemColumn.IsEqualTo(name) && after is Guid orderItemID && orderItemID != Guid.Empty)
  126. {
  127. Product.ID = Guid.Empty;
  128. Product.Clear();
  129. Job.ID = Guid.Empty;
  130. Job.Clear();
  131. }
  132. else if((ProductColumn.IsEqualTo(name) && after is Guid productID && productID != Guid.Empty)
  133. || (JobColumn.IsEqualTo(name) && after is Guid jobID && jobID != Guid.Empty))
  134. {
  135. OrderItem.ID = Guid.Empty;
  136. OrderItem.Clear();
  137. }
  138. }
  139. }
  140. }