PurchaseOrderItem.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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. [EditorSequence(4)]
  41. public JobLink Job { get; set; }
  42. [MemoEditor(Visible = Visible.Default)]
  43. [EditorSequence(6)]
  44. public string Description { get; set; }
  45. [DoubleEditor(Visible = Visible.Default)]
  46. [EditorSequence(7)]
  47. public double Qty { get; set; } = 1;
  48. [CurrencyEditor(Visible = Visible.Optional)]
  49. [EditorSequence(8)]
  50. public double ForeignCurrencyCost { get; set; }
  51. [CurrencyEditor(Visible = Visible.Default)]
  52. [EditorSequence(9)]
  53. public double Cost { get; set; }
  54. [CurrencyEditor(Visible = Visible.Default, Summary = Summary.Sum)]
  55. [EditorSequence(10)]
  56. public double ExTax { get; set; }
  57. [EditorSequence(11)]
  58. public TaxCodeLink TaxCode { get; set; }
  59. [NullEditor]
  60. public double TaxRate { get; set; }
  61. [EditorSequence(12)]
  62. [CurrencyEditor(Visible = Visible.Default, Summary = Summary.Sum)]
  63. public double Tax { get; set; }
  64. [EditorSequence(13)]
  65. [CurrencyEditor(Visible = Visible.Default, Summary = Summary.Sum)]
  66. public double IncTax { get; set; }
  67. [EditorSequence("Additional",1)]
  68. [IntegerEditor(Visible = Visible.Optional)]
  69. public int PORevision { get; set; }
  70. [CodeEditor(Visible = Visible.Default, Editable = Editable.Enabled)]
  71. [EditorSequence("Additional",2)]
  72. public string SupplierCode { get; set; }
  73. [EditorSequence("Additional",3)]
  74. public PurchaseGLCodeLink PurchaseGL { get; set; }
  75. [EditorSequence("Additional",4)]
  76. public CostCentreLink CostCentre { get; set; }
  77. [DateTimeEditor(Visible = Visible.Default)]
  78. [EditorSequence("Additional",5)]
  79. public DateTime DueDate { get; set; }
  80. [EditorSequence("Additional",6)]
  81. [EntityRelationship(DeleteAction.SetNull)]
  82. public ConsignmentLink Consignment { get; set; }
  83. [EditorSequence("Additional",7)]
  84. public StockLocationLink StockLocation { get; set; }
  85. [DateTimeEditor(Visible = Visible.Default)]
  86. [EditorSequence("Additional",8)]
  87. public DateTime ReceivedDate { get; set; }
  88. [TextBoxEditor(Visible = Visible.Optional)]
  89. [EditorSequence("Additional",9)]
  90. public string ReceivedReference { get; set; }
  91. [CurrencyEditor(Visible = Visible.Optional, Editable = Editable.Hidden, Summary = Summary.Sum)]
  92. public double Balance { get; set; }
  93. [EntityRelationship(DeleteAction.SetNull)]
  94. public BillLineLink BillLine { get; set; }
  95. [NullEditor]
  96. public string PostedReference { get; set; }
  97. [EntityRelationship(DeleteAction.SetNull)]
  98. [NullEditor]
  99. [Obsolete("Replaced with Product", true)]
  100. public ProductLink ProductLink
  101. {
  102. get { return Product; }
  103. set { /* We cannot set the 'Product' to this value, because that would stuff the SubObject internal linking, so we do nothing instead. */ }
  104. }
  105. [EntityRelationship(DeleteAction.SetNull)]
  106. [NullEditor]
  107. [Obsolete("Replaced with Style", true)]
  108. public ProductStyleLink StyleLink
  109. {
  110. get { return Style; }
  111. set { /* Same as ProductLink */ }
  112. }
  113. [NullEditor]
  114. [Obsolete("Replaced with Dimensions", true)]
  115. public double UnitSize { get; set; }
  116. [NullEditor]
  117. [Obsolete]
  118. public StockMovementLink StockMovement { get; set; }
  119. [NullEditor]
  120. [EntityRelationship(DeleteAction.SetNull)]
  121. public ManufacturingPacketLink Packet { get; set; }
  122. private class FormCountAggregate : ComplexFormulaGenerator<PurchaseOrderItem, int>
  123. {
  124. public override IComplexFormulaNode<PurchaseOrderItem, int> GetFormula() =>
  125. Count<PurchaseOrderItemForm, Guid>(
  126. x => x.Property(x => x.ID))
  127. .WithLink(x => x.Parent.ID, x => x.ID);
  128. }
  129. [NullEditor]
  130. [ComplexFormula(typeof(FormCountAggregate))]
  131. public int FormCount { get; set; }
  132. private class OpenFormsAggregate : ComplexFormulaGenerator<PurchaseOrderItem, int>
  133. {
  134. public override IComplexFormulaNode<PurchaseOrderItem, int> GetFormula() =>
  135. Count<PurchaseOrderItemForm, Guid>(
  136. x => x.Property(x => x.ID),
  137. new Filter<PurchaseOrderItemForm>(x => x.FormCompleted).IsEqualTo(DateTime.MinValue)
  138. .Or(x => x.FormData).IsEqualTo(""))
  139. .WithLink(x => x.Parent.ID, x => x.ID);
  140. }
  141. [NullEditor]
  142. [ComplexFormula(typeof(OpenFormsAggregate))]
  143. public int OpenForms { get; set; }
  144. static PurchaseOrderItem()
  145. {
  146. LinkedProperties.Register<PurchaseOrderItem, ProductLink, String>(x => x.Product, x => x.Code, x => x.SupplierCode);
  147. LinkedProperties.Register<PurchaseOrderItem, ProductLink, String>(x => x.Product, x => x.Name, x => x.Description);
  148. LinkedProperties.Register<PurchaseOrderItem, TaxCodeLink, Guid>(x => x.Product.TaxCode, x => x.ID, x => x.TaxCode.ID);
  149. LinkedProperties.Register<PurchaseOrderItem, TaxCodeLink, String>(x => x.Product.TaxCode, x => x.Code,
  150. x => x.TaxCode.Code);
  151. LinkedProperties.Register<PurchaseOrderItem, TaxCodeLink, String>(x => x.Product.TaxCode, x => x.Description,
  152. x => x.TaxCode.Description);
  153. LinkedProperties.Register<PurchaseOrderItem, TaxCodeLink, double>(x => x.Product.TaxCode, x => x.Rate,
  154. x => x.TaxCode.Rate);
  155. LinkedProperties.Register<PurchaseOrderItem, TaxCodeLink, double>(x => x.TaxCode, x => x.Rate, x => x.TaxRate);
  156. LinkedProperties.Register<PurchaseOrderItem, PurchaseGLCodeLink, Guid>(x => x.Product.PurchaseGL, x => x.ID, x => x.PurchaseGL.ID);
  157. LinkedProperties.Register<PurchaseOrderItem, CostCentreLink, Guid>(x => x.Product.CostCentre, x => x.ID, x => x.CostCentre.ID);
  158. LinkedProperties.Register<PurchaseOrderItem, ProductStyleLink, Guid>(x => x.Product.DefaultInstance.Style, x => x.ID, x => x.Style.ID);
  159. LinkedProperties.Register<PurchaseOrderItem, ProductStyleLink, String>(x => x.Product.DefaultInstance.Style, x => x.Code, x => x.Style.Code);
  160. LinkedProperties.Register<PurchaseOrderItem, ProductStyleLink, String>(x => x.Product.DefaultInstance.Style, x => x.Description, x => x.Style.Description);
  161. LinkedProperties.Register<PurchaseOrderItem, ProductInstanceLink, double>(x => x.Product.DefaultInstance, x => x.NettCost,
  162. x => x.Cost);
  163. StockEntity.LinkStockDimensions<PurchaseOrderItem>();
  164. }
  165. private bool bChanging;
  166. protected override void DoPropertyChanged(string name, object? before, object? after)
  167. {
  168. base.DoPropertyChanged(name, before, after);
  169. if (bChanging)
  170. return;
  171. try
  172. {
  173. bChanging = true;
  174. if (name.Equals(nameof(Qty)) && after is double qty)
  175. ExTax = qty * Cost;
  176. else if (name.Equals(nameof(ForeignCurrencyCost)) && (after is double foreigncost) &&
  177. PurchaseOrderLink.SupplierLink.Currency.ID != Guid.Empty)
  178. {
  179. Cost = foreigncost / (PurchaseOrderLink.SupplierLink.Currency.ExchangeRate.IsEffectivelyEqual(0.0) ? 1.0 : PurchaseOrderLink.SupplierLink.Currency.ExchangeRate);
  180. ExTax = Qty * Cost;
  181. }
  182. else if (name.Equals(nameof(Cost)) && (after is double cost))
  183. {
  184. ExTax = cost * Qty;
  185. if (PurchaseOrderLink.SupplierLink.Currency.ID != Guid.Empty)
  186. ForeignCurrencyCost = cost * (PurchaseOrderLink.SupplierLink.Currency.ExchangeRate.IsEffectivelyEqual(0.0) ? 1.0 : PurchaseOrderLink.SupplierLink.Currency.ExchangeRate);
  187. }
  188. else if (name.Equals(nameof(ExTax)) && after is double extax)
  189. {
  190. if (Qty == 0)
  191. Qty = 1;
  192. Cost = extax / Qty;
  193. if (PurchaseOrderLink.SupplierLink.Currency.ID != Guid.Empty)
  194. ForeignCurrencyCost = Cost * (PurchaseOrderLink.SupplierLink.Currency.ExchangeRate.IsEffectivelyEqual(0.0) ? 1.0 : PurchaseOrderLink.SupplierLink.Currency.ExchangeRate);
  195. }
  196. else if (name.Equals(nameof(IncTax)) && after is double inctax)
  197. Balance = ReceivedDate.IsEmpty() ? inctax : 0.00F;
  198. else if (name.Equals(nameof(ReceivedDate)) && after is DateTime received)
  199. Balance = received.IsEmpty() ? IncTax : 0.00F;
  200. }
  201. finally
  202. {
  203. bChanging = false;
  204. }
  205. }
  206. }
  207. }