BillLine.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  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, IJobScopedItem
  31. {
  32. [RequiredColumn]
  33. [EntityRelationship(DeleteAction.Cascade)]
  34. [NullEditor]
  35. public BillLink Bill { 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.Bill.Supplier.ID).Distinct().SingleOrDefault();
  43. if(supplierID == Guid.Empty)
  44. return new Filter<PurchaseOrderItem>().None();
  45. return Filter<PurchaseOrderItem>.Where(x => x.PurchaseOrder.Supplier.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.Bill.Supplier.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.Bill.Supplier.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.Bill.Supplier.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. [LookupDefinition(typeof(BaseJobScopeLookup<BillLine>))]
  89. [EditorSequence(5)]
  90. public JobScopeLink JobScope { get; set; }
  91. [MemoEditor]
  92. [EditorSequence(6)]
  93. public string Description { get; set; }
  94. [CurrencyEditor(Summary=Summary.Sum)]
  95. [EditorSequence(7)]
  96. public decimal ForeignCurrencyCost { get; set; }
  97. [CurrencyEditor(Summary = Summary.Sum)]
  98. [EditorSequence(8)]
  99. public decimal ExTax { get; set; }
  100. [RequiredColumn]
  101. [EditorSequence(9)]
  102. public TaxCodeLink TaxCode { get; set; }
  103. [CurrencyEditor(Summary = Summary.Sum)]
  104. [EditorSequence(10)]
  105. public decimal Tax { get; set; }
  106. [CurrencyEditor(Summary = Summary.Sum)]
  107. [EditorSequence(11)]
  108. public decimal IncTax { get; set; }
  109. [EditorSequence(12)]
  110. public PurchaseGLCodeLink PurchaseGL { get; set; }
  111. [EditorSequence(13)]
  112. public CostCentreLink CostCentre { get; set; }
  113. [EditorSequence(14)]
  114. [Editable(Editable.Disabled)]
  115. public InvoiceLink Invoice { get; set; }
  116. [EditorSequence(15)]
  117. public ActualCharge Charge { get; set; }
  118. [NullEditor]
  119. public decimal TaxRate { get; set; }
  120. [NullEditor]
  121. public string PostedReference { get; set; }
  122. static BillLine()
  123. {
  124. // If we select a product for this bill line
  125. LinkedProperties.Register<BillLine, ProductLink, String>(x => x.Product, x => x.Name, x => x.Description);
  126. LinkedProperties.Register<BillLine, PurchaseOrderItemLink, decimal>(x => x.OrderItem, x => x.ExTax,
  127. x => x.ExTax);
  128. LinkedProperties.Register<BillLine, PurchaseGLCodeLink, Guid>(x => x.OrderItem.PurchaseGL, x => x.ID,
  129. x => x.PurchaseGL.ID);
  130. LinkedProperties.Register<BillLine, CostCentreLink, Guid>(x => x.OrderItem.CostCentre, x => x.ID,
  131. x => x.CostCentre.ID);
  132. LinkedProperties.Register<BillLine, TaxCodeLink, Guid>(x => x.OrderItem.TaxCode, x => x.ID,
  133. x => x.TaxCode.ID);
  134. LinkedProperties.Register<BillLine, TaxCodeLink, String>(x => x.OrderItem.TaxCode, x => x.Code,
  135. x => x.TaxCode.Code);
  136. LinkedProperties.Register<BillLine, TaxCodeLink, String>(x => x.OrderItem.TaxCode, x => x.Description,
  137. x => x.TaxCode.Description);
  138. LinkedProperties.Register<BillLine, TaxCodeLink, decimal>(x => x.OrderItem.TaxCode, x => x.Rate,
  139. x => x.TaxCode.Rate);
  140. LinkedProperties.Register<BillLine, PurchaseOrderItemLink, decimal>(x => x.OrderItem, x => x.Tax,
  141. x => x.Tax);
  142. LinkedProperties.Register<BillLine, PurchaseOrderItemLink, decimal>(x => x.OrderItem, x => x.IncTax,
  143. x => x.IncTax);
  144. Classes.JobScope.LinkScopeProperties<BillLine>();
  145. LinkedProperties.Register<BillLine, TaxCodeLink, decimal>(x => x.TaxCode, x => x.Rate, x => x.TaxRate);
  146. LinkedProperties.Register<BillLine, PurchaseGLCodeLink, Guid>(x => x.Product.PurchaseGL, x => x.ID, x => x.PurchaseGL.ID);
  147. LinkedProperties.Register<BillLine, CostCentreLink, Guid>(x => x.Product.CostCentre, x => x.ID, x => x.CostCentre.ID);
  148. LinkedProperties.Register<BillLine, TaxCodeLink, Guid>(x => x.Product.TaxCode, x => x.ID, x => x.TaxCode.ID);
  149. LinkedProperties.Register<BillLine, TaxCodeLink, string>(x => x.Product.TaxCode, x => x.Code, x => x.TaxCode.Code);
  150. LinkedProperties.Register<BillLine, TaxCodeLink, string>(x => x.Product.TaxCode, x => x.Description, x => x.TaxCode.Description);
  151. LinkedProperties.Register<BillLine, TaxCodeLink, decimal>(x => x.Product.TaxCode, x => x.Rate, x => x.TaxCode.Rate);
  152. DefaultColumns.Add<BillLine>(x => x.Bill.Number, caption: "Bill");
  153. DefaultColumns.Add<BillLine>(x => x.OrderItem.PurchaseOrder.PONumber, caption: "PO #");
  154. DefaultColumns.Add<BillLine>(x => x.OrderItem.Description, caption: "PO Item");
  155. DefaultColumns.Add<BillLine>(x => x.Consignment.Number, caption: "Consignment");
  156. DefaultColumns.Add<BillLine>(x => x.Product.Code, caption: "Product");
  157. DefaultColumns.Add<BillLine>(x => x.Job.JobNumber, caption: "Job #");
  158. DefaultColumns.Add<BillLine>(x => x.Job.Name, caption: "Job Name");
  159. DefaultColumns.Add<BillLine>(x => x.JobScope.Number, caption: "Job Scope #");
  160. DefaultColumns.Add<BillLine>(x => x.Description);
  161. DefaultColumns.Add<BillLine>(x => x.ExTax);
  162. DefaultColumns.Add<BillLine>(x => x.TaxCode.Code, caption: "Tax Code");
  163. DefaultColumns.Add<BillLine>(x => x.IncTax);
  164. DefaultColumns.Add<BillLine>(x => x.PurchaseGL.Code, caption: "Purchase GL");
  165. DefaultColumns.Add<BillLine>(x => x.CostCentre.Code, caption: "Cost Centre");
  166. }
  167. private static readonly Column<BillLine> OrderItemColumn = new Column<BillLine>(x => x.OrderItem.ID);
  168. private static readonly Column<BillLine> ProductColumn = new Column<BillLine>(x => x.Product.ID);
  169. private static readonly Column<BillLine> JobColumn = new Column<BillLine>(x => x.Job.ID);
  170. private static readonly Column<BillLine> ForeignCurrencyColumn = new Column<BillLine>(x => x.ForeignCurrencyCost);
  171. private static readonly Column<BillLine> ExTaxColumn = new Column<BillLine>(x => x.ExTax);
  172. private static readonly Column<BillLine> IncTaxColumn = new Column<BillLine>(x => x.ExTax);
  173. private bool bChanging = false;
  174. protected override void DoPropertyChanged(string name, object? before, object? after)
  175. {
  176. if (bChanging)
  177. return;
  178. bChanging = true;
  179. try
  180. {
  181. base.DoPropertyChanged(name, before, after);
  182. if (OrderItemColumn.IsEqualTo(name) && after is Guid orderItemID && orderItemID != Guid.Empty)
  183. {
  184. Product.ID = Guid.Empty;
  185. Product.Clear();
  186. Job.ID = Guid.Empty;
  187. Job.Clear();
  188. JobScope.ID = Guid.Empty;
  189. JobScope.Clear();
  190. }
  191. else if((ProductColumn.IsEqualTo(name) && after is Guid productID && productID != Guid.Empty)
  192. || (JobColumn.IsEqualTo(name) && after is Guid jobID && jobID != Guid.Empty))
  193. {
  194. OrderItem.ID = Guid.Empty;
  195. OrderItem.Clear();
  196. }
  197. else if (ForeignCurrencyColumn.IsEqualTo(name) && after is decimal fcc)
  198. {
  199. ExTax = fcc / (Bill.Supplier.Currency.ExchangeRate == 0 ? 1 : Bill.Supplier.Currency.ExchangeRate);
  200. Tax = ExTax * TaxRate / 100;
  201. IncTax = ExTax + Tax;
  202. }
  203. else if (ExTaxColumn.IsEqualTo(name) && after is decimal etc)
  204. {
  205. ForeignCurrencyCost = etc * (Bill.Supplier.Currency.ExchangeRate == 0 ? 1 : Bill.Supplier.Currency.ExchangeRate);
  206. }
  207. else if (IncTaxColumn.IsEqualTo(name) && after is decimal itc)
  208. {
  209. ForeignCurrencyCost = ExTax * (Bill.Supplier.Currency.ExchangeRate == 0 ? 1 : Bill.Supplier.Currency.ExchangeRate);
  210. }
  211. }
  212. finally
  213. {
  214. bChanging = false;
  215. }
  216. }
  217. }
  218. }