PurchaseOrderItem.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Linq.Expressions;
  5. using InABox.Core;
  6. using PRSClasses;
  7. namespace Comal.Classes
  8. {
  9. [UserTracking(typeof(Bill))]
  10. [Caption("Purchase Order Items")]
  11. public class PurchaseOrderItem : StockEntity, IRemotable, IPersistent, IOneToMany<PurchaseOrder>, ITaxable, IOneToMany<Consignment>, IOneToMany<Job>,
  12. ILicense<AccountsPayableLicense>, IPostableFragment<PurchaseOrder>
  13. {
  14. [RequiredColumn]
  15. [EntityRelationship(DeleteAction.Cascade)]
  16. public PurchaseOrderLink PurchaseOrderLink { get; set; }
  17. [EntityRelationship(DeleteAction.SetNull)]
  18. [EditorSequence(1)]
  19. [RequiredColumn]
  20. public override ProductLink Product { get; set; }
  21. [EntityRelationship(DeleteAction.SetNull)]
  22. [EditorSequence(2)]
  23. public ProductStyleLink Style { get; set; }
  24. [EditorSequence(3)]
  25. [RequiredColumn]
  26. [DimensionsEditor(typeof(StockDimensions))]
  27. public override StockDimensions Dimensions { get; set; }
  28. private class AllocationsFormula : ComplexFormulaGenerator<PurchaseOrderItem, string>
  29. {
  30. public override IComplexFormulaNode<PurchaseOrderItem, string> GetFormula() =>
  31. Aggregate<PurchaseOrderItemAllocation>(
  32. AggregateCalculation.Concat,
  33. x => x.Property(x => x.Job.JobNumber))
  34. .WithLink(x => x.Item.ID, x => x.ID);
  35. }
  36. [ComplexFormula(typeof(AllocationsFormula))]
  37. [TextBoxEditor(Visible=Visible.Optional,Editable = Editable.Hidden)]
  38. public string Allocations { get; set; }
  39. [EntityRelationship(DeleteAction.SetNull)]
  40. [NullEditor]
  41. [Obsolete("Replaced with Allocations Aggregate", true)]
  42. public JobLink Job { get; set; }
  43. [MemoEditor(Visible = Visible.Default)]
  44. [EditorSequence(6)]
  45. public string Description { get; set; }
  46. [DoubleEditor(Visible = Visible.Default)]
  47. [EditorSequence(7)]
  48. public double Qty { get; set; } = 1;
  49. [CurrencyEditor(Visible = Visible.Optional)]
  50. [EditorSequence(8)]
  51. public double ForeignCurrencyCost { get; set; }
  52. [CurrencyEditor(Visible = Visible.Default)]
  53. [EditorSequence(9)]
  54. public double Cost { get; set; }
  55. [CurrencyEditor(Visible = Visible.Default, Summary = Summary.Sum)]
  56. [EditorSequence(10)]
  57. public double ExTax { get; set; }
  58. [EditorSequence(11)]
  59. public TaxCodeLink TaxCode { get; set; }
  60. [NullEditor]
  61. public double TaxRate { get; set; }
  62. [EditorSequence(12)]
  63. [CurrencyEditor(Visible = Visible.Default, Summary = Summary.Sum)]
  64. public double Tax { get; set; }
  65. [EditorSequence(13)]
  66. [CurrencyEditor(Visible = Visible.Default, Summary = Summary.Sum)]
  67. public double IncTax { get; set; }
  68. [EditorSequence("Additional",1)]
  69. [IntegerEditor(Visible = Visible.Optional)]
  70. public int PORevision { get; set; }
  71. [CodeEditor(Visible = Visible.Default, Editable = Editable.Enabled)]
  72. [EditorSequence("Additional",2)]
  73. public string SupplierCode { get; set; }
  74. [EditorSequence("Additional",3)]
  75. public PurchaseGLCodeLink PurchaseGL { get; set; }
  76. [EditorSequence("Additional",4)]
  77. public CostCentreLink CostCentre { get; set; }
  78. [DateTimeEditor(Visible = Visible.Default)]
  79. [EditorSequence("Additional",5)]
  80. public DateTime DueDate { get; set; }
  81. [EditorSequence("Additional",6)]
  82. [EntityRelationship(DeleteAction.SetNull)]
  83. public ConsignmentLink Consignment { get; set; }
  84. [EditorSequence("Additional",7)]
  85. public StockLocationLink StockLocation { get; set; }
  86. [DateTimeEditor(Visible = Visible.Default)]
  87. [EditorSequence("Additional",8)]
  88. public DateTime ReceivedDate { get; set; }
  89. [TextBoxEditor(Visible = Visible.Optional)]
  90. [EditorSequence("Additional",9)]
  91. public string ReceivedReference { get; set; }
  92. [CurrencyEditor(Visible = Visible.Optional, Editable = Editable.Hidden, Summary = Summary.Sum)]
  93. public double Balance { get; set; }
  94. [EntityRelationship(DeleteAction.SetNull)]
  95. public BillLineLink BillLine { get; set; }
  96. [NullEditor]
  97. public string PostedReference { get; set; }
  98. [EntityRelationship(DeleteAction.SetNull)]
  99. [NullEditor]
  100. [Obsolete("Replaced with Product", true)]
  101. public ProductLink ProductLink
  102. {
  103. get { return Product; }
  104. set { /* We cannot set the 'Product' to this value, because that would stuff the SubObject internal linking, so we do nothing instead. */ }
  105. }
  106. [EntityRelationship(DeleteAction.SetNull)]
  107. [NullEditor]
  108. [Obsolete("Replaced with Style", true)]
  109. public ProductStyleLink StyleLink
  110. {
  111. get { return Style; }
  112. set { /* Same as ProductLink */ }
  113. }
  114. [NullEditor]
  115. [Obsolete("Replaced with Dimensions", true)]
  116. public double UnitSize { get; set; }
  117. [NullEditor]
  118. [Obsolete]
  119. public StockMovementLink StockMovement { get; set; }
  120. [NullEditor]
  121. [EntityRelationship(DeleteAction.SetNull)]
  122. public ManufacturingPacketLink Packet { get; set; }
  123. private class FormCountAggregate : ComplexFormulaGenerator<PurchaseOrderItem, int>
  124. {
  125. public override IComplexFormulaNode<PurchaseOrderItem, int> GetFormula() =>
  126. Count<PurchaseOrderItemForm, Guid>(
  127. x => x.Property(x => x.ID))
  128. .WithLink(x => x.Parent.ID, x => x.ID);
  129. }
  130. [NullEditor]
  131. [ComplexFormula(typeof(FormCountAggregate))]
  132. public int FormCount { get; set; }
  133. private class OpenFormsAggregate : ComplexFormulaGenerator<PurchaseOrderItem, int>
  134. {
  135. public override IComplexFormulaNode<PurchaseOrderItem, int> GetFormula() =>
  136. Count<PurchaseOrderItemForm, Guid>(
  137. x => x.Property(x => x.ID),
  138. new Filter<PurchaseOrderItemForm>(x => x.FormCompleted).IsEqualTo(DateTime.MinValue)
  139. .Or(x => x.FormData).IsEqualTo(""))
  140. .WithLink(x => x.Parent.ID, x => x.ID);
  141. }
  142. [NullEditor]
  143. [ComplexFormula(typeof(OpenFormsAggregate))]
  144. public int OpenForms { get; set; }
  145. [ChildEntity(typeof(PurchaseOrderItemAllocationNominatedJob))]
  146. public PurchaseOrderItemAllocationJobLink NominatedJob { get; set; }
  147. static PurchaseOrderItem()
  148. {
  149. LinkedProperties.Register<PurchaseOrderItem, ProductLink, String>(x => x.Product, x => x.Code, x => x.SupplierCode);
  150. LinkedProperties.Register<PurchaseOrderItem, ProductLink, String>(x => x.Product, x => x.Name, x => x.Description);
  151. LinkedProperties.Register<PurchaseOrderItem, TaxCodeLink, Guid>(x => x.Product.TaxCode, x => x.ID, x => x.TaxCode.ID);
  152. LinkedProperties.Register<PurchaseOrderItem, TaxCodeLink, String>(x => x.Product.TaxCode, x => x.Code,
  153. x => x.TaxCode.Code);
  154. LinkedProperties.Register<PurchaseOrderItem, TaxCodeLink, String>(x => x.Product.TaxCode, x => x.Description,
  155. x => x.TaxCode.Description);
  156. LinkedProperties.Register<PurchaseOrderItem, TaxCodeLink, double>(x => x.Product.TaxCode, x => x.Rate,
  157. x => x.TaxCode.Rate);
  158. LinkedProperties.Register<PurchaseOrderItem, TaxCodeLink, double>(x => x.TaxCode, x => x.Rate, x => x.TaxRate);
  159. LinkedProperties.Register<PurchaseOrderItem, PurchaseGLCodeLink, Guid>(x => x.Product.PurchaseGL, x => x.ID, x => x.PurchaseGL.ID);
  160. LinkedProperties.Register<PurchaseOrderItem, CostCentreLink, Guid>(x => x.Product.CostCentre, x => x.ID, x => x.CostCentre.ID);
  161. LinkedProperties.Register<PurchaseOrderItem, ProductStyleLink, Guid>(x => x.Product.DefaultInstance.Style, x => x.ID, x => x.Style.ID);
  162. LinkedProperties.Register<PurchaseOrderItem, ProductStyleLink, String>(x => x.Product.DefaultInstance.Style, x => x.Code, x => x.Style.Code);
  163. LinkedProperties.Register<PurchaseOrderItem, ProductStyleLink, String>(x => x.Product.DefaultInstance.Style, x => x.Description, x => x.Style.Description);
  164. LinkedProperties.Register<PurchaseOrderItem, ProductInstanceLink, double>(x => x.Product.DefaultInstance, x => x.NettCost,
  165. x => x.Cost);
  166. StockEntity.LinkStockDimensions<PurchaseOrderItem>();
  167. }
  168. private bool bChanging;
  169. protected override void DoPropertyChanged(string name, object? before, object? after)
  170. {
  171. base.DoPropertyChanged(name, before, after);
  172. if (bChanging)
  173. return;
  174. try
  175. {
  176. bChanging = true;
  177. if (name.Equals(nameof(Qty)) && after is double qty)
  178. ExTax = qty * Cost;
  179. else if (name.Equals(nameof(ForeignCurrencyCost)) && (after is double foreigncost) &&
  180. PurchaseOrderLink.SupplierLink.Currency.ID != Guid.Empty)
  181. {
  182. Cost = foreigncost / (PurchaseOrderLink.SupplierLink.Currency.ExchangeRate.IsEffectivelyEqual(0.0) ? 1.0 : PurchaseOrderLink.SupplierLink.Currency.ExchangeRate);
  183. ExTax = Qty * Cost;
  184. }
  185. else if (name.Equals(nameof(Cost)) && (after is double cost))
  186. {
  187. ExTax = cost * Qty;
  188. if (PurchaseOrderLink.SupplierLink.Currency.ID != Guid.Empty)
  189. ForeignCurrencyCost = cost * (PurchaseOrderLink.SupplierLink.Currency.ExchangeRate.IsEffectivelyEqual(0.0) ? 1.0 : PurchaseOrderLink.SupplierLink.Currency.ExchangeRate);
  190. }
  191. else if (name.Equals(nameof(ExTax)) && after is double extax)
  192. {
  193. if (Qty == 0)
  194. Qty = 1;
  195. Cost = extax / Qty;
  196. if (PurchaseOrderLink.SupplierLink.Currency.ID != Guid.Empty)
  197. ForeignCurrencyCost = Cost * (PurchaseOrderLink.SupplierLink.Currency.ExchangeRate.IsEffectivelyEqual(0.0) ? 1.0 : PurchaseOrderLink.SupplierLink.Currency.ExchangeRate);
  198. }
  199. else if (name.Equals(nameof(IncTax)) && after is double inctax)
  200. Balance = ReceivedDate.IsEmpty() ? inctax : 0.00F;
  201. else if (name.Equals(nameof(ReceivedDate)) && after is DateTime received)
  202. Balance = received.IsEmpty() ? IncTax : 0.00F;
  203. }
  204. finally
  205. {
  206. bChanging = false;
  207. }
  208. }
  209. }
  210. public class PurchaseOrderItemAllocationNominatedJob : IChildEntityDefinition<PurchaseOrderItemAllocation>
  211. {
  212. public Filter<PurchaseOrderItemAllocation>? Filter { get; set; } = new Filter<PurchaseOrderItemAllocation>(x => x.Nominated).IsEqualTo(true);
  213. public SortOrder<PurchaseOrderItemAllocation>? Sort { get; set; }
  214. = new SortOrder<PurchaseOrderItemAllocation>(x => x.Job.JobNumber, SortDirection.Descending).ThenBy(x => x.Created, SortDirection.Descending);
  215. public Expression<Func<PurchaseOrderItemAllocation, Guid>> Parent => x => x.Item.ID;
  216. }
  217. }