PurchaseOrderItem.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  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. [EntityRelationship(DeleteAction.SetNull)]
  121. public BillLineLink BillLine { get; set; }
  122. [NullEditor]
  123. public string PostedReference { get; set; }
  124. [EntityRelationship(DeleteAction.SetNull)]
  125. [NullEditor]
  126. [Obsolete("Replaced with Product", true)]
  127. public ProductLink ProductLink
  128. {
  129. get { return Product; }
  130. set { /* We cannot set the 'Product' to this value, because that would stuff the SubObject internal linking, so we do nothing instead. */ }
  131. }
  132. [EntityRelationship(DeleteAction.SetNull)]
  133. [NullEditor]
  134. [Obsolete("Replaced with Style", true)]
  135. public ProductStyleLink StyleLink
  136. {
  137. get { return Style; }
  138. set { /* Same as ProductLink */ }
  139. }
  140. [NullEditor]
  141. [Obsolete("Replaced with Dimensions", true)]
  142. public double UnitSize { get; set; }
  143. [NullEditor]
  144. [Obsolete]
  145. public StockMovementLink StockMovement { get; set; }
  146. [NullEditor]
  147. [EntityRelationship(DeleteAction.SetNull)]
  148. public ManufacturingPacketLink Packet { get; set; }
  149. private class FormCountAggregate : ComplexFormulaGenerator<PurchaseOrderItem, int>
  150. {
  151. public override IComplexFormulaNode<PurchaseOrderItem, int> GetFormula() =>
  152. Count<PurchaseOrderItemForm, Guid>(
  153. x => x.Property(x => x.ID))
  154. .WithLink(x => x.Parent.ID, x => x.ID);
  155. }
  156. [NullEditor]
  157. [ComplexFormula(typeof(FormCountAggregate))]
  158. public int FormCount { get; set; }
  159. private class OpenFormsAggregate : ComplexFormulaGenerator<PurchaseOrderItem, int>
  160. {
  161. public override IComplexFormulaNode<PurchaseOrderItem, int> GetFormula() =>
  162. Count<PurchaseOrderItemForm, Guid>(
  163. x => x.Property(x => x.ID),
  164. new Filter<PurchaseOrderItemForm>(x => x.FormCompleted).IsEqualTo(DateTime.MinValue)
  165. .Or(x => x.FormData).IsEqualTo(""))
  166. .WithLink(x => x.Parent.ID, x => x.ID);
  167. }
  168. [NullEditor]
  169. [ComplexFormula(typeof(OpenFormsAggregate))]
  170. public int OpenForms { get; set; }
  171. static PurchaseOrderItem()
  172. {
  173. LinkedProperties.Register<PurchaseOrderItem, ProductLink, String>(x => x.Product, x => x.Code, x => x.SupplierCode);
  174. LinkedProperties.Register<PurchaseOrderItem, ProductLink, String>(x => x.Product, x => x.Name, x => x.Description);
  175. LinkedProperties.Register<PurchaseOrderItem, TaxCodeLink, Guid>(x => x.Product.TaxCode, x => x.ID, x => x.TaxCode.ID);
  176. LinkedProperties.Register<PurchaseOrderItem, TaxCodeLink, String>(x => x.Product.TaxCode, x => x.Code,
  177. x => x.TaxCode.Code);
  178. LinkedProperties.Register<PurchaseOrderItem, TaxCodeLink, String>(x => x.Product.TaxCode, x => x.Description,
  179. x => x.TaxCode.Description);
  180. LinkedProperties.Register<PurchaseOrderItem, TaxCodeLink, double>(x => x.Product.TaxCode, x => x.Rate,
  181. x => x.TaxCode.Rate);
  182. LinkedProperties.Register<PurchaseOrderItem, TaxCodeLink, double>(x => x.TaxCode, x => x.Rate, x => x.TaxRate);
  183. LinkedProperties.Register<PurchaseOrderItem, PurchaseGLCodeLink, Guid>(x => x.Product.PurchaseGL, x => x.ID, x => x.PurchaseGL.ID);
  184. LinkedProperties.Register<PurchaseOrderItem, CostCentreLink, Guid>(x => x.Product.CostCentre, x => x.ID, x => x.CostCentre.ID);
  185. LinkedProperties.Register<PurchaseOrderItem, ProductStyleLink, Guid>(x => x.Product.DefaultInstance.Style, x => x.ID, x => x.Style.ID);
  186. LinkedProperties.Register<PurchaseOrderItem, ProductStyleLink, String>(x => x.Product.DefaultInstance.Style, x => x.Code, x => x.Style.Code);
  187. LinkedProperties.Register<PurchaseOrderItem, ProductStyleLink, String>(x => x.Product.DefaultInstance.Style, x => x.Description, x => x.Style.Description);
  188. LinkedProperties.Register<PurchaseOrderItem, ProductInstanceLink, double>(x => x.Product.DefaultInstance, x => x.NettCost,
  189. x => x.Cost);
  190. StockEntity.LinkStockDimensions<PurchaseOrderItem>();
  191. }
  192. private bool bChanging;
  193. protected override void DoPropertyChanged(string name, object? before, object? after)
  194. {
  195. base.DoPropertyChanged(name, before, after);
  196. if (bChanging)
  197. return;
  198. try
  199. {
  200. bChanging = true;
  201. if (name.Equals(nameof(Qty)) && after is double qty)
  202. ExTax = qty * Cost;
  203. else if (name.Equals(nameof(ForeignCurrencyCost)) && (after is double foreigncost) &&
  204. PurchaseOrderLink.SupplierLink.Currency.ID != Guid.Empty)
  205. {
  206. Cost = foreigncost / (PurchaseOrderLink.SupplierLink.Currency.ExchangeRate.IsEffectivelyEqual(0.0) ? 1.0 : PurchaseOrderLink.SupplierLink.Currency.ExchangeRate);
  207. ExTax = Qty * Cost;
  208. }
  209. else if (name.Equals(nameof(Cost)) && (after is double cost))
  210. {
  211. ExTax = cost * Qty;
  212. if (PurchaseOrderLink.SupplierLink.Currency.ID != Guid.Empty)
  213. ForeignCurrencyCost = cost * (PurchaseOrderLink.SupplierLink.Currency.ExchangeRate.IsEffectivelyEqual(0.0) ? 1.0 : PurchaseOrderLink.SupplierLink.Currency.ExchangeRate);
  214. }
  215. else if (name.Equals(nameof(ExTax)) && after is double extax)
  216. {
  217. if (Qty == 0)
  218. Qty = 1;
  219. Cost = extax / Qty;
  220. if (PurchaseOrderLink.SupplierLink.Currency.ID != Guid.Empty)
  221. ForeignCurrencyCost = Cost * (PurchaseOrderLink.SupplierLink.Currency.ExchangeRate.IsEffectivelyEqual(0.0) ? 1.0 : PurchaseOrderLink.SupplierLink.Currency.ExchangeRate);
  222. }
  223. else if (name.Equals(nameof(IncTax)) && after is double inctax)
  224. Balance = ReceivedDate.IsEmpty() ? inctax : 0.00F;
  225. else if (name.Equals(nameof(ReceivedDate)) && after is DateTime received)
  226. Balance = received.IsEmpty() ? IncTax : 0.00F;
  227. }
  228. finally
  229. {
  230. bChanging = false;
  231. }
  232. }
  233. }
  234. }