SupplierProduct.cs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. using System;
  2. using System.Linq;
  3. using InABox.Core;
  4. using PRSClasses;
  5. namespace Comal.Classes
  6. {
  7. [UserTracking(typeof(Product))]
  8. public class SupplierProduct : StockEntity, IPersistent, IRemotable, ILicense<ProductManagementLicense>, IExportable, IImportable, IManyToMany<Supplier, Product>, IJobMaterial
  9. {
  10. private bool bChanging;
  11. [RequiredColumn]
  12. [EditorSequence(0)]
  13. public SupplierLink SupplierLink { get; set; }
  14. [EntityRelationship(DeleteAction.SetNull)]
  15. [NullEditor]
  16. [Obsolete("Replaced with Product", true)]
  17. public ProductLink ProductLink
  18. {
  19. get { return Product; }
  20. set { Product = value; }
  21. }
  22. [EntityRelationship(DeleteAction.SetNull)]
  23. [EditorSequence(1)]
  24. public override ProductLink Product { get; set; }
  25. [EditorSequence(2)]
  26. [RequiredColumn]
  27. [DimensionsEditor(typeof(StockDimensions))]
  28. public override StockDimensions Dimensions { get; set; }
  29. [EntityRelationship(DeleteAction.SetNull)]
  30. [EditorSequence(3)]
  31. public ProductStyleGroupLink StyleGroup { get; set; }
  32. [EntityRelationship(DeleteAction.SetNull)]
  33. [EditorSequence(4)]
  34. public ProductStyleLink Style { get; set; }
  35. /// <summary>
  36. /// The product code as the supplier knows it.
  37. /// </summary>
  38. [Comment("Product code as the supplier knows it.")]
  39. [CodeEditor(Editable = Editable.Enabled)]
  40. [EditorSequence(5)]
  41. public string SupplierCode { get; set; }
  42. /// <summary>
  43. /// Product description as the supplier knows it.
  44. /// </summary>
  45. [Comment("Product description as the supplier knows it.")]
  46. [TextBoxEditor]
  47. [EditorSequence(6)]
  48. public string SupplierDescription { get; set; }
  49. [TextBoxEditor]
  50. [EditorSequence(7)]
  51. public string SupplierCategory { get; set; }
  52. private class DiscountGroupLookup : LookupDefinitionGenerator<SupplierDiscountGroup, SupplierProduct>
  53. {
  54. public override Filter<SupplierDiscountGroup> DefineFilter(SupplierProduct[] items)
  55. {
  56. var suppliers = items.Select(x => x.SupplierLink.ID).Distinct().ToArray();
  57. if (suppliers.Length != 1)
  58. return new Filter<SupplierDiscountGroup>().None();
  59. return new Filter<SupplierDiscountGroup>(x=>x.Supplier.ID).IsEqualTo(suppliers[0]);
  60. }
  61. public override Columns<SupplierProduct> DefineFilterColumns()
  62. => Columns.None<SupplierProduct>().Add(x => x.SupplierLink.ID);
  63. public override Columns<SupplierDiscountGroup> DefineColumns()
  64. => Columns.None<SupplierDiscountGroup>().Add(x=>x.ID).Add(x=>x.Code).Add(x=>x.Description);
  65. public override string FormatDisplay(CoreRow row)
  66. => $"{row.Get<SupplierDiscountGroup, string>(x => x.Code)}: {row.Get<SupplierDiscountGroup, string>(x => x.Description)}";
  67. }
  68. [LookupDefinition(typeof(DiscountGroupLookup))]
  69. [EntityRelationship(DeleteAction.SetNull)]
  70. [EditorSequence(8)]
  71. public SupplierDiscountGroupLink DiscountGroup { get; set; }
  72. [EditorSequence(8)]
  73. public JobLink Job { get; set; }
  74. [CurrencyEditor(Visible = Visible.Optional)]
  75. [EditorSequence(9)]
  76. public double ForeignCurrencyPrice { get; set; }
  77. [CurrencyEditor(Visible = Visible.Optional)]
  78. [EditorSequence(10)]
  79. public double TradePrice { get; set; }
  80. [DoubleEditor(Visible = Visible.Optional)]
  81. [EditorSequence(11)]
  82. public double Discount { get; set; }
  83. [CurrencyEditor(Visible = Visible.Optional, Editable = Editable.Disabled)]
  84. [EditorSequence(12)]
  85. public double CostPrice { get; set; }
  86. [EditorSequence(13)]
  87. public TaxCodeLink TaxCode { get; set; }
  88. [URLEditor]
  89. [EditorSequence(14)]
  90. public string URL { get; set; }
  91. [TimestampEditor]
  92. [EditorSequence(15)]
  93. public DateTime Expired { get; set; }
  94. static SupplierProduct()
  95. {
  96. LinkedProperties.Register<SupplierProduct, ProductLink, String>(x => x.Product, x => x.Code, x => x.SupplierCode);
  97. LinkedProperties.Register<SupplierProduct, ProductLink, String>(x => x.Product, x => x.Name, x => x.SupplierDescription);
  98. StockEntity.LinkStockDimensions<SupplierProduct>();
  99. }
  100. private static String STYLEGROUPID = CoreUtils.GetFullPropertyName<SupplierProduct, Guid>(x => x.StyleGroup.ID, ".");
  101. private static String STYLEID = CoreUtils.GetFullPropertyName<SupplierProduct, Guid>(x => x.Style.ID, ".");
  102. protected override void DoPropertyChanged(string name, object? before, object? after)
  103. {
  104. base.DoPropertyChanged(name, before, after);
  105. if (bChanging)
  106. return;
  107. bChanging = true;
  108. if (name.Equals(nameof(ForeignCurrencyPrice)) && SupplierLink.Currency.ID != Guid.Empty)
  109. {
  110. TradePrice = (double)after / (SupplierLink.Currency.ExchangeRate.IsEffectivelyEqual(0.0) ? 1.0 : SupplierLink.Currency.ExchangeRate);
  111. CostPrice = TradePrice * (100.0F - Discount) / 100.0F;
  112. }
  113. else if (name.Equals(nameof(TradePrice)))
  114. {
  115. if (SupplierLink.Currency.ID != Guid.Empty)
  116. ForeignCurrencyPrice = (double)after * SupplierLink.Currency.ExchangeRate;
  117. CostPrice = (double)after * (100.0F - Discount) / 100.0F;
  118. }
  119. else if (name.Equals(nameof(Discount)))
  120. CostPrice = TradePrice * (100.0F - (double)after) / 100.0F;
  121. //else if (name.Equals(name,nameof(CostPrice)))
  122. // TradePrice = Discount == 100.0F ? double.MaxValue : (double)after / ((100.0F - Discount) / 100.0F);
  123. else if (name.Equals(STYLEGROUPID))
  124. Style.CopyFrom(new ProductStyleLink());
  125. else if (name.Equals(STYLEID))
  126. StyleGroup.CopyFrom(new ProductStyleGroupLink());
  127. bChanging = false;
  128. }
  129. }
  130. }