Dimensions.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  1. using InABox.Core;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Collections.Immutable;
  5. using System.Linq.Expressions;
  6. namespace Comal.Classes
  7. {
  8. public abstract class Dimensions<TLink, TUnit> : EnclosedEntity, IDimensions
  9. where TLink : DimensionUnitLink<TUnit>, new()
  10. where TUnit : DimensionUnit, new()
  11. {
  12. [EditorSequence(1)]
  13. [RequiredColumn]
  14. [Caption("Sizing", IncludePath = false)]
  15. public abstract TLink Unit { get; set; }
  16. public IDimensionUnit GetUnit() => Unit;
  17. [DoubleEditor(Visible = Visible.Hidden)]
  18. [EditorSequence(2)]
  19. [Caption("Quantity", IncludePath = false)]
  20. [RequiredColumn]
  21. public abstract double Quantity { get; set; }
  22. [DoubleEditor(Visible = Visible.Hidden)]
  23. [EditorSequence(3)]
  24. [Caption("Length", IncludePath = false)]
  25. [RequiredColumn]
  26. public abstract double Length { get; set; }
  27. [DoubleEditor(Visible = Visible.Hidden)]
  28. [EditorSequence(4)]
  29. [Caption("Width", IncludePath = false)]
  30. [RequiredColumn]
  31. public abstract double Width { get; set; }
  32. [DoubleEditor(Visible = Visible.Hidden)]
  33. [EditorSequence(5)]
  34. [Caption("Height", IncludePath = false)]
  35. [RequiredColumn]
  36. public abstract double Height { get; set; }
  37. [DoubleEditor(Visible = Visible.Hidden)]
  38. [EditorSequence(6)]
  39. [Caption("Weight", IncludePath = false)]
  40. [RequiredColumn]
  41. public abstract double Weight { get; set; }
  42. [DoubleEditor(Visible = Visible.Optional, Editable = Editable.Hidden)]
  43. [Caption("Value", IncludePath = false)]
  44. [EditorSequence(7)]
  45. [RequiredColumn]
  46. public abstract double Value { get; set; }
  47. [TextBoxEditor(Visible = Visible.Default, Editable = Editable.Hidden)]
  48. [EditorSequence(8)]
  49. [Caption("Unit Size", IncludePath = false)]
  50. [RequiredColumn]
  51. public abstract String UnitSize { get; set; }
  52. IDimensionUnit IDimensions.Unit => Unit;
  53. protected override void Init()
  54. {
  55. base.Init();
  56. Unit.PropertyChanged += (s, e) =>
  57. {
  58. if (e.PropertyName == "ID")
  59. {
  60. DoPropertyChanged("Unit." + e.PropertyName, OriginalValues.GetValueOrDefault("Unit." + e.PropertyName), Unit.ID);
  61. }
  62. else if (e.PropertyName == "Formula")
  63. {
  64. DoPropertyChanged("Unit." + e.PropertyName, OriginalValues.GetValueOrDefault("Unit." + e.PropertyName), Unit.Formula);
  65. }
  66. else if (e.PropertyName == "Format")
  67. {
  68. DoPropertyChanged("Unit." + e.PropertyName, OriginalValues.GetValueOrDefault("Unit." + e.PropertyName), Unit.Format);
  69. }
  70. };
  71. }
  72. private bool bCalculating = false;
  73. private static Column<Dimensions<TLink, TUnit>> unitid = new Column<Dimensions<TLink, TUnit>>(x => x.Unit.ID);
  74. private static Column<Dimensions<TLink, TUnit>> quantity = new Column<Dimensions<TLink, TUnit>>(x => x.Quantity);
  75. private static Column<Dimensions<TLink, TUnit>> length = new Column<Dimensions<TLink, TUnit>>(x => x.Length);
  76. private static Column<Dimensions<TLink, TUnit>> width = new Column<Dimensions<TLink, TUnit>>(x => x.Width);
  77. private static Column<Dimensions<TLink, TUnit>> height = new Column<Dimensions<TLink, TUnit>>(x => x.Height);
  78. private static Column<Dimensions<TLink, TUnit>> weight = new Column<Dimensions<TLink, TUnit>>(x => x.Weight);
  79. private static Column<Dimensions<TLink, TUnit>> sizeformula = new Column<Dimensions<TLink, TUnit>>(x => x.Unit.Formula);
  80. private static Column<Dimensions<TLink, TUnit>> sizeformat = new Column<Dimensions<TLink, TUnit>>(x => x.Unit.Format);
  81. protected override void DoPropertyChanged(string name, object? before, object? after)
  82. {
  83. base.DoPropertyChanged(name, before, after);
  84. if (bCalculating)
  85. return;
  86. bCalculating = true;
  87. try
  88. {
  89. if (unitid.IsEqualTo(name))
  90. Calculate(Quantity, Length, Width, Height, Weight, Unit.Formula, Unit.Format);
  91. else if (quantity.IsEqualTo(name))
  92. Calculate(after, Length, Width, Height, Weight, Unit.Formula, Unit.Format);
  93. else if (length.IsEqualTo(name))
  94. Calculate(Quantity, after, Width, Height, Weight, Unit.Formula, Unit.Format);
  95. else if (width.IsEqualTo(name))
  96. Calculate(Quantity, Length, after, Height, Weight, Unit.Formula, Unit.Format);
  97. else if (height.IsEqualTo(name))
  98. Calculate(Quantity, Length, Width, after, Weight, Unit.Formula, Unit.Format);
  99. else if (weight.IsEqualTo(name))
  100. Calculate(Quantity, Length, Width, Height, after, Unit.Formula, Unit.Format);
  101. else if (sizeformula.IsEqualTo(name))
  102. Calculate(Quantity, Length, Width, Height, Weight, after as string, Unit.Format);
  103. else if (sizeformat.IsEqualTo(name))
  104. Calculate(Quantity, Length, Width, Height, Weight, Unit.Formula, after as string);
  105. }
  106. finally
  107. {
  108. bCalculating = false;
  109. }
  110. }
  111. public void Set(IDimensionUnit unit, double quantity, double length, double width, double height, double weight)
  112. {
  113. bCalculating = true;
  114. try
  115. {
  116. Unit.ID = unit.ID;
  117. Unit.HasQuantity = unit.HasQuantity;
  118. Unit.HasLength = unit.HasLength;
  119. Unit.HasWidth = unit.HasWidth;
  120. Unit.HasHeight = unit.HasHeight;
  121. Unit.HasWeight = unit.HasWeight;
  122. Unit.Code = unit.Code;
  123. Unit.Description = unit.Description;
  124. Unit.Formula = unit.Formula;
  125. Unit.Format = unit.Format;
  126. Quantity = quantity;
  127. Length = length;
  128. Width = width;
  129. Height = height;
  130. Weight = weight;
  131. Calculate(quantity, length, width, height, weight, unit.Formula, unit.Format);
  132. }
  133. finally
  134. {
  135. bCalculating = false;
  136. }
  137. }
  138. public void CalculateValueAndUnitSize()
  139. {
  140. Calculate(Quantity, Length, Width, Height, Weight, Unit.Formula, Unit.Format);
  141. }
  142. private void Calculate(object? quantity, object? length, object? width, object? height, object? weight, string? formula, string? format)
  143. {
  144. if (Evaluate<double>(formula, quantity, length, width, height, weight, out double value))
  145. Value = value;
  146. if (Evaluate<String>(format, quantity, length, width, height, weight, out string unitsize))
  147. UnitSize = unitsize;
  148. }
  149. private bool Evaluate<T>(string? formula, object? quantity, object? length, object? width, object? height, object? weight, out T result)
  150. {
  151. if (!String.IsNullOrWhiteSpace(formula))
  152. {
  153. var variables = new Dictionary<string, object?>()
  154. {
  155. { "Quantity", Convert.ToDouble(quantity) },
  156. { "Length", Convert.ToDouble(length) },
  157. { "Width", Convert.ToDouble(width) },
  158. { "Height", Convert.ToDouble(height) },
  159. { "Weight", Convert.ToDouble(weight) }
  160. };
  161. try
  162. {
  163. var expr = new CoreExpression(formula);
  164. var eval = expr.Evaluate(variables);
  165. result = (T)CoreUtils.ChangeType(eval, typeof(T));
  166. return true;
  167. }
  168. catch (Exception e)
  169. {
  170. Logger.Send(LogType.Information, "", String.Format("Error in Formula: [{0}] ({1})", formula, e.Message));
  171. result = default(T);
  172. return false;
  173. }
  174. }
  175. result = default(T);
  176. return true;
  177. }
  178. public void CopyFrom(IDimensions source, bool observing = false)
  179. {
  180. if (!observing)
  181. SetObserving(false);
  182. Unit.ID = source.GetUnit().ID;
  183. Unit.Synchronise(source.GetUnit());
  184. Quantity = source.Quantity;
  185. Length = source.Length;
  186. Width = source.Width;
  187. Height = source.Height;
  188. Weight = source.Weight;
  189. Value = source.Value;
  190. UnitSize = source.UnitSize;
  191. if (!observing)
  192. SetObserving(true);
  193. }
  194. public override string ToString()
  195. {
  196. var result = Value != 0 ? Math.Round(Value, 3).ToString() : "";
  197. result = !string.IsNullOrWhiteSpace(UnitSize) ? result + " " + UnitSize + ", " : "Empty unitsize";
  198. result = Quantity != 0 ? result + "Quantity: " + Quantity + ", " : result;
  199. result = Length != 0 ? result + "Length: " + Length + ", " : result;
  200. result = Width != 0 ? result + "Width: " + Width + ", " : result;
  201. result = Height != 0 ? result + "Height: " + Height + ", " : result;
  202. result = Weight != 0 ? result + "Weight: " + Weight : result;
  203. if (result.EndsWith(", "))
  204. result = result.Remove(result.Length - 2);
  205. return result;
  206. }
  207. public override bool Equals(object obj)
  208. {
  209. return obj is IDimensions dim
  210. && Unit.ID == dim.Unit.ID
  211. && Quantity.IsEffectivelyEqual(dim.Quantity)
  212. && Length.IsEffectivelyEqual(dim.Length)
  213. && Width.IsEffectivelyEqual(dim.Width)
  214. && Height.IsEffectivelyEqual(dim.Height)
  215. && Weight.IsEffectivelyEqual(dim.Weight);
  216. }
  217. public override int GetHashCode()
  218. {
  219. return HashCode.Combine(
  220. Unit.ID,
  221. Quantity,
  222. Length,
  223. Width,
  224. Height,
  225. Weight);
  226. }
  227. }
  228. public static class Dimensions
  229. {
  230. private static readonly Column<IDimensions> unitid = new Column<IDimensions>(x => x.Unit.ID);
  231. private static readonly Column<IDimensions> quantity = new Column<IDimensions>(x => x.Quantity);
  232. private static readonly Column<IDimensions> length = new Column<IDimensions>(x => x.Length);
  233. private static readonly Column<IDimensions> width = new Column<IDimensions>(x => x.Width);
  234. private static readonly Column<IDimensions> height = new Column<IDimensions>(x => x.Height);
  235. private static readonly Column<IDimensions> weight = new Column<IDimensions>(x => x.Weight);
  236. public static Filter<T> DimensionEquals<T>(this Filter<T> filter, IDimensions dim)
  237. {
  238. if (!CoreUtils.TryFindMemberExpression(filter.Expression, out var mexp))
  239. {
  240. throw new ArgumentException("Filter expression is not a MemberExpression");
  241. }
  242. var prop = CoreUtils.GetFullPropertyName(mexp, ".") + ".";
  243. filter.Expression = CoreUtils.GetMemberExpression(typeof(T), prop + unitid.Property);
  244. filter.IsEqualTo(dim.Unit.ID);
  245. filter.And(prop + quantity.Property).IsEqualTo(dim.Quantity);
  246. filter.And(prop + length.Property).IsEqualTo(dim.Length);
  247. filter.And(prop + width.Property).IsEqualTo(dim.Width);
  248. filter.And(prop + height.Property).IsEqualTo(dim.Height);
  249. filter.And(prop + weight.Property).IsEqualTo(dim.Weight);
  250. return filter.Parent ?? filter;
  251. }
  252. public enum ColumnsType
  253. {
  254. Local,
  255. Data,
  256. All
  257. }
  258. public static Columns<T> LocalColumns<T>()
  259. where T : IDimensions
  260. {
  261. return new Columns<T>(x => x.Unit.ID)
  262. .Add(x => x.Quantity)
  263. .Add(x => x.Length)
  264. .Add(x => x.Width)
  265. .Add(x => x.Height)
  266. .Add(x => x.Weight)
  267. .Add(x => x.UnitSize)
  268. .Add(x => x.Value);
  269. }
  270. public static Columns<T> DataColumns<T>()
  271. where T : IDimensions
  272. {
  273. return new Columns<T>(x => x.Unit.ID)
  274. .Add(x => x.Unit.ID)
  275. .Add(x => x.Unit.Format)
  276. .Add(x => x.Unit.Formula)
  277. .Add(x => x.Unit.HasHeight)
  278. .Add(x => x.Unit.HasWeight)
  279. .Add(x => x.Unit.HasWidth)
  280. .Add(x => x.Unit.HasQuantity)
  281. .Add(x => x.Unit.HasLength)
  282. .Add(x => x.Quantity)
  283. .Add(x => x.Length)
  284. .Add(x => x.Width)
  285. .Add(x => x.Height)
  286. .Add(x => x.Weight)
  287. .Add(x => x.UnitSize)
  288. .Add(x => x.Value);
  289. }
  290. public static Columns<T> AllColumns<T>()
  291. where T : IDimensions
  292. {
  293. return new Columns<T>(x => x.Unit.ID)
  294. .Add(x => x.Unit.ID)
  295. .Add(x => x.Unit.Format)
  296. .Add(x => x.Unit.Formula)
  297. .Add(x => x.Unit.HasHeight)
  298. .Add(x => x.Unit.HasWeight)
  299. .Add(x => x.Unit.HasWidth)
  300. .Add(x => x.Unit.HasQuantity)
  301. .Add(x => x.Unit.HasLength)
  302. .Add(x => x.Unit.Code)
  303. .Add(x => x.Unit.Description)
  304. .Add(x => x.Quantity)
  305. .Add(x => x.Length)
  306. .Add(x => x.Width)
  307. .Add(x => x.Height)
  308. .Add(x => x.Weight)
  309. .Add(x => x.UnitSize)
  310. .Add(x => x.Value);
  311. }
  312. public static Columns<T> AddDimensionsColumns<T, TDim>(this Columns<T> columns, Expression<Func<T, TDim>> dim, ColumnsType type = ColumnsType.Local)
  313. where TDim : IDimensions
  314. {
  315. return columns.AddSubColumns(dim, type switch
  316. {
  317. ColumnsType.Data => DataColumns<TDim>(),
  318. ColumnsType.All => AllColumns<TDim>(),
  319. ColumnsType.Local => LocalColumns<TDim>(),
  320. _ => LocalColumns<TDim>()
  321. });
  322. }
  323. }
  324. }