BillLine.cs 10 KB

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