Dimensions.cs 20 KB

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