BillLine.cs 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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. [RequiredColumn]
  15. [EntityRelationship(DeleteAction.Cascade)]
  16. [NullEditor]
  17. public BillLink BillLink { get; set; }
  18. private class PurchaseOrderItemLookup : LookupDefinitionGenerator<PurchaseOrderItem, BillLine>
  19. {
  20. public override Filter<PurchaseOrderItem> DefineFilter(BillLine[] items)
  21. {
  22. if (!items.Any())
  23. return new Filter<PurchaseOrderItem>().None();
  24. var supplierID = items.Select(x => x.BillLink.SupplierLink.ID).Distinct().SingleOrDefault();
  25. if(supplierID == Guid.Empty)
  26. return new Filter<PurchaseOrderItem>().None();
  27. return new Filter<PurchaseOrderItem>(x => x.PurchaseOrderLink.SupplierLink.ID).IsEqualTo(supplierID)
  28. .And(x=>x.BillLine.ID).IsEqualTo(Guid.Empty);
  29. }
  30. public override Columns<BillLine> DefineFilterColumns()
  31. {
  32. return Columns.None<BillLine>().Add(x => x.BillLink.SupplierLink.ID);
  33. }
  34. }
  35. [LookupDefinition(typeof(PurchaseOrderItemLookup))]
  36. [EntityRelationship(DeleteAction.SetNull)]
  37. [EditorSequence(1)]
  38. public PurchaseOrderItemLink OrderItem { get; set; }
  39. private class ConsignmentLookup : LookupDefinitionGenerator<Consignment, BillLine>
  40. {
  41. public override Filter<Consignment> DefineFilter(BillLine[] items)
  42. {
  43. if (!items.Any())
  44. return new Filter<Consignment>().None();
  45. var supplierID = items.Select(x => x.BillLink.SupplierLink.ID).Distinct().SingleOrDefault();
  46. if(supplierID == Guid.Empty)
  47. return new Filter<Consignment>().None();
  48. return new Filter<Consignment>(x => x.Supplier.ID).IsEqualTo(supplierID)
  49. .And(x=>x.BillLine.ID).IsEqualTo(Guid.Empty);
  50. }
  51. public override Columns<BillLine> DefineFilterColumns()
  52. {
  53. return Columns.None<BillLine>().Add(x => x.BillLink.SupplierLink.ID);
  54. }
  55. }
  56. [LookupDefinition(typeof(ConsignmentLookup))]
  57. [EntityRelationship(DeleteAction.SetNull)]
  58. [EditorSequence(2)]
  59. public ConsignmentLink Consignment { get; set; }
  60. private class ProductLookupGenerator : LookupDefinitionGenerator<Product, BillLine>
  61. {
  62. public override Filter<Product>? DefineFilter(BillLine[] items)
  63. => new Filter<Product>(x => x.NonStock).IsEqualTo(true);
  64. }
  65. [EditorSequence(3)]
  66. [LookupDefinition(typeof(ProductLookupGenerator))]
  67. public ProductLink Product { get; set; }
  68. [EditorSequence(4)]
  69. public JobLink Job { get; set; }
  70. [MemoEditor]
  71. [EditorSequence(5)]
  72. public string Description { get; set; }
  73. [CurrencyEditor(Summary=Summary.Sum)]
  74. [EditorSequence(6)]
  75. public double ForeignCurrencyCost { get; set; }
  76. [CurrencyEditor(Summary = Summary.Sum)]
  77. [EditorSequence(7)]
  78. public double ExTax { get; set; }
  79. [RequiredColumn]
  80. [EditorSequence(8)]
  81. public TaxCodeLink TaxCode { get; set; }
  82. [CurrencyEditor(Summary = Summary.Sum)]
  83. [EditorSequence(9)]
  84. public double Tax { get; set; }
  85. [CurrencyEditor(Summary = Summary.Sum)]
  86. [EditorSequence(10)]
  87. public double IncTax { get; set; }
  88. [EditorSequence(11)]
  89. public PurchaseGLCodeLink PurchaseGL { get; set; }
  90. [EditorSequence(12)]
  91. public CostCentreLink CostCentre { get; set; }
  92. [NullEditor]
  93. public double TaxRate { get; set; }
  94. [NullEditor]
  95. public string PostedReference { get; set; }
  96. static BillLine()
  97. {
  98. // If we select a product for this bill line
  99. LinkedProperties.Register<BillLine, ProductLink, String>(x => x.Product, x => x.Name, x => x.Description);
  100. LinkedProperties.Register<BillLine, PurchaseOrderItemLink, double>(x => x.OrderItem, x => x.ExTax,
  101. x => x.ExTax);
  102. LinkedProperties.Register<BillLine, PurchaseGLCodeLink, Guid>(x => x.OrderItem.PurchaseGL, x => x.ID,
  103. x => x.PurchaseGL.ID);
  104. LinkedProperties.Register<BillLine, CostCentreLink, Guid>(x => x.OrderItem.CostCentre, x => x.ID,
  105. x => x.CostCentre.ID);
  106. LinkedProperties.Register<BillLine, TaxCodeLink, Guid>(x => x.OrderItem.TaxCode, x => x.ID,
  107. x => x.TaxCode.ID);
  108. LinkedProperties.Register<BillLine, TaxCodeLink, String>(x => x.OrderItem.TaxCode, x => x.Code,
  109. x => x.TaxCode.Code);
  110. LinkedProperties.Register<BillLine, TaxCodeLink, String>(x => x.OrderItem.TaxCode, x => x.Description,
  111. x => x.TaxCode.Description);
  112. LinkedProperties.Register<BillLine, TaxCodeLink, double>(x => x.OrderItem.TaxCode, x => x.Rate,
  113. x => x.TaxCode.Rate);
  114. LinkedProperties.Register<BillLine, PurchaseOrderItemLink, double>(x => x.OrderItem, x => x.Tax,
  115. x => x.Tax);
  116. LinkedProperties.Register<BillLine, PurchaseOrderItemLink, double>(x => x.OrderItem, x => x.IncTax,
  117. x => x.IncTax);
  118. LinkedProperties.Register<BillLine, TaxCodeLink, double>(x => x.TaxCode, x => x.Rate, x => x.TaxRate);
  119. LinkedProperties.Register<BillLine, PurchaseGLCodeLink, Guid>(x => x.Product.PurchaseGL, x => x.ID, x => x.PurchaseGL.ID);
  120. LinkedProperties.Register<BillLine, CostCentreLink, Guid>(x => x.Product.CostCentre, x => x.ID, x => x.CostCentre.ID);
  121. LinkedProperties.Register<BillLine, TaxCodeLink, Guid>(x => x.Product.TaxCode, x => x.ID, x => x.TaxCode.ID);
  122. LinkedProperties.Register<BillLine, TaxCodeLink, string>(x => x.Product.TaxCode, x => x.Code, x => x.TaxCode.Code);
  123. LinkedProperties.Register<BillLine, TaxCodeLink, string>(x => x.Product.TaxCode, x => x.Description, x => x.TaxCode.Description);
  124. LinkedProperties.Register<BillLine, TaxCodeLink, double>(x => x.Product.TaxCode, x => x.Rate, x => x.TaxCode.Rate);
  125. }
  126. private static readonly Column<BillLine> OrderItemColumn = new Column<BillLine>(x => x.OrderItem.ID);
  127. private static readonly Column<BillLine> ProductColumn = new Column<BillLine>(x => x.Product.ID);
  128. private static readonly Column<BillLine> JobColumn = new Column<BillLine>(x => x.Job.ID);
  129. private static readonly Column<BillLine> ForeignCurrencyColumn = new Column<BillLine>(x => x.ForeignCurrencyCost);
  130. private static readonly Column<BillLine> ExTaxColumn = new Column<BillLine>(x => x.ExTax);
  131. private static readonly Column<BillLine> IncTaxColumn = new Column<BillLine>(x => x.ExTax);
  132. private bool bChanging = false;
  133. protected override void DoPropertyChanged(string name, object? before, object? after)
  134. {
  135. if (bChanging)
  136. return;
  137. bChanging = true;
  138. try
  139. {
  140. base.DoPropertyChanged(name, before, after);
  141. if (OrderItemColumn.IsEqualTo(name) && after is Guid orderItemID && orderItemID != Guid.Empty)
  142. {
  143. Product.ID = Guid.Empty;
  144. Product.Clear();
  145. Job.ID = Guid.Empty;
  146. Job.Clear();
  147. }
  148. else if((ProductColumn.IsEqualTo(name) && after is Guid productID && productID != Guid.Empty)
  149. || (JobColumn.IsEqualTo(name) && after is Guid jobID && jobID != Guid.Empty))
  150. {
  151. OrderItem.ID = Guid.Empty;
  152. OrderItem.Clear();
  153. }
  154. else if (ForeignCurrencyColumn.IsEqualTo(name) && after is double fcc)
  155. {
  156. ExTax = fcc / (BillLink.SupplierLink.Currency.ExchangeRate.IsEffectivelyEqual(0.0) ? 1.0 : BillLink.SupplierLink.Currency.ExchangeRate);
  157. Tax = ExTax * TaxRate / 100.0;
  158. IncTax = ExTax + Tax;
  159. }
  160. else if (ExTaxColumn.IsEqualTo(name) && after is double etc)
  161. {
  162. ForeignCurrencyCost = etc * (BillLink.SupplierLink.Currency.ExchangeRate.IsEffectivelyEqual(0.0) ? 1.0 : BillLink.SupplierLink.Currency.ExchangeRate);
  163. }
  164. else if (IncTaxColumn.IsEqualTo(name) && after is double itc)
  165. {
  166. ForeignCurrencyCost = ExTax * (BillLink.SupplierLink.Currency.ExchangeRate.IsEffectivelyEqual(0.0) ? 1.0 : BillLink.SupplierLink.Currency.ExchangeRate);
  167. }
  168. }
  169. finally
  170. {
  171. bChanging = false;
  172. }
  173. }
  174. }
  175. }