BillLine.cs 10 KB

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