PurchaseOrderItem.cs 12 KB

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