BillLine.cs 10 KB

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