Dimensions.cs 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. using InABox.Core;
  2. using System;
  3. using System.Collections.Generic;
  4. namespace Comal.Classes
  5. {
  6. public abstract class Dimensions<TLink, TUnit> : EnclosedEntity, IDimensions
  7. where TLink : DimensionUnitLink<TUnit>, new()
  8. where TUnit : DimensionUnit, new()
  9. {
  10. [EditorSequence(1)]
  11. [RequiredColumn]
  12. [Caption("Sizing", IncludePath = false)]
  13. public abstract TLink Unit { get; set; }
  14. public IDimensionUnit GetUnit() => Unit;
  15. [DoubleEditor(Visible = Visible.Hidden)]
  16. [EditorSequence(2)]
  17. [Caption("Quantity", IncludePath = false)]
  18. [RequiredColumn]
  19. public abstract double Quantity { get; set; }
  20. [DoubleEditor(Visible = Visible.Hidden)]
  21. [EditorSequence(3)]
  22. [Caption("Length", IncludePath = false)]
  23. [RequiredColumn]
  24. public abstract double Length { get; set; }
  25. [DoubleEditor(Visible = Visible.Hidden)]
  26. [EditorSequence(4)]
  27. [Caption("Width", IncludePath = false)]
  28. [RequiredColumn]
  29. public abstract double Width { get; set; }
  30. [DoubleEditor(Visible = Visible.Hidden)]
  31. [EditorSequence(5)]
  32. [Caption("Height", IncludePath = false)]
  33. [RequiredColumn]
  34. public abstract double Height { get; set; }
  35. [DoubleEditor(Visible = Visible.Hidden)]
  36. [EditorSequence(6)]
  37. [Caption("Weight", IncludePath = false)]
  38. [RequiredColumn]
  39. public abstract double Weight { get; set; }
  40. [DoubleEditor(Visible = Visible.Optional, Editable = Editable.Hidden)]
  41. [Caption("Value", IncludePath = false)]
  42. [EditorSequence(7)]
  43. [RequiredColumn]
  44. public abstract double Value { get; set; }
  45. [TextBoxEditor(Visible = Visible.Default, Editable = Editable.Hidden)]
  46. [EditorSequence(8)]
  47. [Caption("Unit Size", IncludePath = false)]
  48. [RequiredColumn]
  49. public abstract String UnitSize { get; set; }
  50. protected override void Init()
  51. {
  52. base.Init();
  53. Unit.PropertyChanged += (s, e) =>
  54. {
  55. if (e.PropertyName == "ID")
  56. {
  57. DoPropertyChanged("Unit." + e.PropertyName, OriginalValues.GetValueOrDefault("Unit." + e.PropertyName), Unit.ID);
  58. }
  59. else if (e.PropertyName == "Formula")
  60. {
  61. DoPropertyChanged("Unit." + e.PropertyName, OriginalValues.GetValueOrDefault("Unit." + e.PropertyName), Unit.Formula);
  62. }
  63. else if (e.PropertyName == "Format")
  64. {
  65. DoPropertyChanged("Unit." + e.PropertyName, OriginalValues.GetValueOrDefault("Unit." + e.PropertyName), Unit.Format);
  66. }
  67. };
  68. }
  69. private bool bCalculating = false;
  70. private static Column<Dimensions<TLink, TUnit>> unitid = new Column<Dimensions<TLink, TUnit>>(x => x.Unit.ID);
  71. private static Column<Dimensions<TLink, TUnit>> quantity = new Column<Dimensions<TLink, TUnit>>(x => x.Quantity);
  72. private static Column<Dimensions<TLink, TUnit>> length = new Column<Dimensions<TLink, TUnit>>(x => x.Length);
  73. private static Column<Dimensions<TLink, TUnit>> width = new Column<Dimensions<TLink, TUnit>>(x => x.Width);
  74. private static Column<Dimensions<TLink, TUnit>> height = new Column<Dimensions<TLink, TUnit>>(x => x.Height);
  75. private static Column<Dimensions<TLink, TUnit>> weight = new Column<Dimensions<TLink, TUnit>>(x => x.Weight);
  76. private static Column<Dimensions<TLink, TUnit>> sizeformula = new Column<Dimensions<TLink, TUnit>>(x => x.Unit.Formula);
  77. private static Column<Dimensions<TLink, TUnit>> sizeformat = new Column<Dimensions<TLink, TUnit>>(x => x.Unit.Format);
  78. protected override void DoPropertyChanged(string name, object? before, object? after)
  79. {
  80. base.DoPropertyChanged(name, before, after);
  81. if (bCalculating)
  82. return;
  83. bCalculating = true;
  84. try
  85. {
  86. if (unitid.IsEqualTo(name))
  87. Calculate(Quantity, Length, Width, Height, Weight, Unit.Formula, Unit.Format);
  88. else if (quantity.IsEqualTo(name))
  89. Calculate(after, Length, Width, Height, Weight, Unit.Formula, Unit.Format);
  90. else if (length.IsEqualTo(name))
  91. Calculate(Quantity, after, Width, Height, Weight, Unit.Formula, Unit.Format);
  92. else if (width.IsEqualTo(name))
  93. Calculate(Quantity, Length, after, Height, Weight, Unit.Formula, Unit.Format);
  94. else if (height.IsEqualTo(name))
  95. Calculate(Quantity, Length, Width, after, Weight, Unit.Formula, Unit.Format);
  96. else if (weight.IsEqualTo(name))
  97. Calculate(Quantity, Length, Width, Height, after, Unit.Formula, Unit.Format);
  98. else if (sizeformula.IsEqualTo(name))
  99. Calculate(Quantity, Length, Width, Height, Weight, after as string, Unit.Format);
  100. else if (sizeformat.IsEqualTo(name))
  101. Calculate(Quantity, Length, Width, Height, Weight, Unit.Formula, after as string);
  102. }
  103. finally
  104. {
  105. bCalculating = false;
  106. }
  107. }
  108. public void Set(IDimensionUnit unit, double quantity, double length, double width, double height, double weight)
  109. {
  110. bCalculating = true;
  111. try
  112. {
  113. Unit.ID = unit.ID;
  114. Unit.HasQuantity = unit.HasQuantity;
  115. Unit.HasLength = unit.HasLength;
  116. Unit.HasWidth = unit.HasWidth;
  117. Unit.HasHeight = unit.HasHeight;
  118. Unit.HasWeight = unit.HasWeight;
  119. Unit.Code = unit.Code;
  120. Unit.Description = unit.Description;
  121. Unit.Formula = unit.Formula;
  122. Unit.Format = unit.Format;
  123. Quantity = quantity;
  124. Length = length;
  125. Width = width;
  126. Height = height;
  127. Weight = weight;
  128. Calculate(quantity, length, width, height, weight, unit.Formula, unit.Format);
  129. }
  130. finally
  131. {
  132. bCalculating = false;
  133. }
  134. }
  135. private void Calculate(object? quantity, object? length, object? width, object? height, object? weight, string? formula, string? format)
  136. {
  137. if (Evaluate<double>(formula, quantity, length, width, height, weight, out double value))
  138. Value = value;
  139. if (Evaluate<String>(format, quantity, length, width, height, weight, out string unitsize))
  140. UnitSize = unitsize;
  141. }
  142. private bool Evaluate<T>(string? formula, object? quantity, object? length, object? width, object? height, object? weight, out T result)
  143. {
  144. if (!String.IsNullOrWhiteSpace(formula))
  145. {
  146. var variables = new Dictionary<string, object?>()
  147. {
  148. { "Quantity", Convert.ToDouble(quantity) },
  149. { "Length", Convert.ToDouble(length) },
  150. { "Width", Convert.ToDouble(width) },
  151. { "Height", Convert.ToDouble(height) },
  152. { "Weight", Convert.ToDouble(weight) }
  153. };
  154. try
  155. {
  156. var expr = new CoreExpression(formula);
  157. var eval = expr.Evaluate(variables);
  158. result = (T)CoreUtils.ChangeType(eval, typeof(T));
  159. return true;
  160. }
  161. catch (Exception e)
  162. {
  163. Logger.Send(LogType.Information, "", String.Format("Error in Formula: [{0}] ({1})", formula, e.Message));
  164. result = default(T);
  165. return false;
  166. }
  167. }
  168. result = default(T);
  169. return true;
  170. }
  171. public void CopyFrom(IDimensions source, bool observing = false)
  172. {
  173. if (!observing)
  174. SetObserving(false);
  175. Unit.ID = source.GetUnit().ID;
  176. Unit.Synchronise(source.GetUnit());
  177. Quantity = source.Quantity;
  178. Length = source.Length;
  179. Width = source.Width;
  180. Height = source.Height;
  181. Weight = source.Weight;
  182. Value = source.Value;
  183. UnitSize = source.UnitSize;
  184. if (!observing)
  185. SetObserving(true);
  186. }
  187. public override string ToString()
  188. {
  189. var result = Value != 0 ? Math.Round(Value, 3).ToString() : "";
  190. result = !string.IsNullOrWhiteSpace(UnitSize) ? result + " " + UnitSize + ", " : "Empty unitsize";
  191. result = Quantity != 0 ? result + "Quantity: " + Quantity + ", " : result;
  192. result = Length != 0 ? result + "Length: " + Length + ", " : result;
  193. result = Width != 0 ? result + "Width: " + Width + ", " : result;
  194. result = Height != 0 ? result + "Height: " + Height + ", " : result;
  195. result = Weight != 0 ? result + "Weight: " + Weight : result;
  196. if (result.EndsWith(", "))
  197. result = result.Remove(result.Length - 2);
  198. return result;
  199. }
  200. }
  201. }