PurchaseOrderItem.cs 12 KB

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