PurchaseOrderItem.cs 13 KB

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