Dimensions.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479
  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, OriginalValueList.GetValueOrDefault("Unit." + e.PropertyName), Unit.ID);
  61. }
  62. else if (e.PropertyName == "Formula")
  63. {
  64. DoPropertyChanged("Unit." + e.PropertyName, OriginalValueList.GetValueOrDefault("Unit." + e.PropertyName), Unit.Formula);
  65. }
  66. else if (e.PropertyName == "Format")
  67. {
  68. DoPropertyChanged("Unit." + e.PropertyName, OriginalValueList.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. (this as IDimensions).Calculate(Quantity, Length, Width, Height, Weight, Unit.Formula, Unit.Format);
  91. else if (quantity.IsEqualTo(name))
  92. (this as IDimensions).Calculate(after, Length, Width, Height, Weight, Unit.Formula, Unit.Format);
  93. else if (length.IsEqualTo(name))
  94. (this as IDimensions).Calculate(Quantity, after, Width, Height, Weight, Unit.Formula, Unit.Format);
  95. else if (width.IsEqualTo(name))
  96. (this as IDimensions).Calculate(Quantity, Length, after, Height, Weight, Unit.Formula, Unit.Format);
  97. else if (height.IsEqualTo(name))
  98. (this as IDimensions).Calculate(Quantity, Length, Width, after, Weight, Unit.Formula, Unit.Format);
  99. else if (weight.IsEqualTo(name))
  100. (this as IDimensions).Calculate(Quantity, Length, Width, Height, after, Unit.Formula, Unit.Format);
  101. else if (sizeformula.IsEqualTo(name))
  102. (this as IDimensions).Calculate(Quantity, Length, Width, Height, Weight, after as string, Unit.Format);
  103. else if (sizeformat.IsEqualTo(name))
  104. (this as IDimensions).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. (this as IDimensions).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. (this as IDimensions).Calculate(Quantity, Length, Width, Height, Weight, Unit.Formula, Unit.Format);
  141. }
  142. public void CopyFrom(IBaseDimensions source, bool observing = false)
  143. {
  144. if (!observing)
  145. SetObserving(false);
  146. Quantity = source.Quantity;
  147. Length = source.Length;
  148. Width = source.Width;
  149. Height = source.Height;
  150. Weight = source.Weight;
  151. if (!observing)
  152. SetObserving(true);
  153. }
  154. public void CopyFrom(IDimensions source, bool observing = false)
  155. {
  156. if (!observing)
  157. SetObserving(false);
  158. Unit.ID = source.GetUnit().ID;
  159. Unit.Synchronise(source.GetUnit());
  160. Quantity = source.Quantity;
  161. Length = source.Length;
  162. Width = source.Width;
  163. Height = source.Height;
  164. Weight = source.Weight;
  165. Value = source.Value;
  166. UnitSize = source.UnitSize;
  167. if (!observing)
  168. SetObserving(true);
  169. }
  170. public override string ToString()
  171. {
  172. var result = Value != 0 ? Math.Round(Value, 3).ToString() : "";
  173. result = !string.IsNullOrWhiteSpace(UnitSize) ? result + " " + UnitSize + ", " : "Empty unitsize";
  174. result = Quantity != 0 ? result + "Quantity: " + Quantity + ", " : result;
  175. result = Length != 0 ? result + "Length: " + Length + ", " : result;
  176. result = Width != 0 ? result + "Width: " + Width + ", " : result;
  177. result = Height != 0 ? result + "Height: " + Height + ", " : result;
  178. result = Weight != 0 ? result + "Weight: " + Weight : result;
  179. if (result.EndsWith(", "))
  180. result = result.Remove(result.Length - 2);
  181. return result;
  182. }
  183. public override bool Equals(object obj)
  184. {
  185. return obj is IDimensions dim
  186. && Unit.ID == dim.Unit.ID
  187. && Quantity.IsEffectivelyEqual(dim.Quantity)
  188. && Length.IsEffectivelyEqual(dim.Length)
  189. && Width.IsEffectivelyEqual(dim.Width)
  190. && Height.IsEffectivelyEqual(dim.Height)
  191. && Weight.IsEffectivelyEqual(dim.Weight);
  192. }
  193. public override int GetHashCode()
  194. {
  195. return HashCode.Combine(
  196. Unit.ID,
  197. Quantity,
  198. Length,
  199. Width,
  200. Height,
  201. Weight);
  202. }
  203. }
  204. public static class Dimensions
  205. {
  206. public static readonly Column<IDimensions> unitid = new Column<IDimensions>(x => x.Unit.ID);
  207. public static readonly Column<IDimensions> quantity = new Column<IDimensions>(x => x.Quantity);
  208. public static readonly Column<IDimensions> length = new Column<IDimensions>(x => x.Length);
  209. public static readonly Column<IDimensions> width = new Column<IDimensions>(x => x.Width);
  210. public static readonly Column<IDimensions> height = new Column<IDimensions>(x => x.Height);
  211. public static readonly Column<IDimensions> weight = new Column<IDimensions>(x => x.Weight);
  212. public static readonly Column<IDimensions> unitSize = new Column<IDimensions>(x => x.UnitSize);
  213. public static readonly Column<IDimensions> value = new Column<IDimensions>(x => x.Value);
  214. public static IEnumerable<Column<IDimensions>> GetFilterColumns()
  215. {
  216. yield return unitid;
  217. yield return quantity;
  218. yield return length;
  219. yield return width;
  220. yield return height;
  221. yield return weight;
  222. }
  223. public static Filter<T> DimensionEquals<T>(this Filter<T> filter, IDimensions dim)
  224. {
  225. var prop = filter.Property + ".";
  226. filter.Property = prop + unitid.Property;
  227. filter.IsEqualTo(dim.Unit.ID);
  228. filter.And<double>(prop + quantity.Property).IsEqualTo(dim.Quantity);
  229. filter.And<double>(prop + length.Property).IsEqualTo(dim.Length);
  230. filter.And<double>(prop + width.Property).IsEqualTo(dim.Width);
  231. filter.And<double>(prop + height.Property).IsEqualTo(dim.Height);
  232. filter.And<double>(prop + weight.Property).IsEqualTo(dim.Weight);
  233. return filter.Parent ?? filter;
  234. }
  235. public static Filter<T> DimensionEquals<T, TProp>(this FilterBuilder<T, TProp> filter, IDimensions dim)
  236. where TProp : IDimensions
  237. {
  238. return filter.Filter.DimensionEquals(dim);
  239. }
  240. public static IEnumerable<KeyValuePair<Expression<Func<T, object?>>, Expression<Func<U, object?>>>> GetLinks<T, U>()
  241. where T : IDimensioned
  242. where U : IDimensioned
  243. {
  244. return GetLinks<T, U>(x => x.Dimensions, x => x.Dimensions);
  245. }
  246. public static IEnumerable<KeyValuePair<Expression<Func<T, object?>>, Expression<Func<U, object?>>>> GetLinks<T, U>(
  247. Expression<Func<T, IDimensions>> tDimensions,
  248. Expression<Func<U, IDimensions>> uDimensions
  249. )
  250. {
  251. var tPrefix = CoreUtils.GetFullPropertyName(tDimensions, ".") + ".";
  252. var uPrefix = CoreUtils.GetFullPropertyName(uDimensions, ".") + ".";
  253. yield return new KeyValuePair<Expression<Func<T, object?>>, Expression<Func<U, object?>>>(
  254. CoreUtils.GetPropertyExpression<T>(tPrefix + unitid.Property), CoreUtils.GetPropertyExpression<U>(uPrefix + unitid.Property));
  255. yield return new KeyValuePair<Expression<Func<T, object?>>, Expression<Func<U, object?>>>(
  256. CoreUtils.GetPropertyExpression<T>(tPrefix + quantity.Property), CoreUtils.GetPropertyExpression<U>(uPrefix + quantity.Property));
  257. yield return new KeyValuePair<Expression<Func<T, object?>>, Expression<Func<U, object?>>>(
  258. CoreUtils.GetPropertyExpression<T>(tPrefix + length.Property), CoreUtils.GetPropertyExpression<U>(uPrefix + length.Property));
  259. yield return new KeyValuePair<Expression<Func<T, object?>>, Expression<Func<U, object?>>>(
  260. CoreUtils.GetPropertyExpression<T>(tPrefix + width.Property), CoreUtils.GetPropertyExpression<U>(uPrefix + width.Property));
  261. yield return new KeyValuePair<Expression<Func<T, object?>>, Expression<Func<U, object?>>>(
  262. CoreUtils.GetPropertyExpression<T>(tPrefix + height.Property), CoreUtils.GetPropertyExpression<U>(uPrefix + height.Property));
  263. yield return new KeyValuePair<Expression<Func<T, object?>>, Expression<Func<U, object?>>>(
  264. CoreUtils.GetPropertyExpression<T>(tPrefix + weight.Property), CoreUtils.GetPropertyExpression<U>(uPrefix + weight.Property));
  265. }
  266. public enum ColumnsType
  267. {
  268. Local,
  269. Data,
  270. All
  271. }
  272. public static int[] GetFilterColumnIndices<T>(Columns<T> columns, Expression<Func<T, IDimensions>> dimensions)
  273. {
  274. var dimCol = CoreUtils.GetFullPropertyName(dimensions, ".") + ".";
  275. return new int[]
  276. {
  277. columns.IndexOf(dimCol + unitid.Property),
  278. columns.IndexOf(dimCol + quantity.Property),
  279. columns.IndexOf(dimCol + length.Property),
  280. columns.IndexOf(dimCol + width.Property),
  281. columns.IndexOf(dimCol + height.Property),
  282. columns.IndexOf(dimCol + weight.Property)
  283. };
  284. }
  285. public static int[] GetFilterColumnIndices<T>(CoreTable table, Expression<Func<T, IDimensions>> dimensions)
  286. {
  287. var dimCol = CoreUtils.GetFullPropertyName(dimensions, ".") + ".";
  288. return new int[]
  289. {
  290. table.GetColumnIndex(dimCol + unitid.Property),
  291. table.GetColumnIndex(dimCol + quantity.Property),
  292. table.GetColumnIndex(dimCol + length.Property),
  293. table.GetColumnIndex(dimCol + width.Property),
  294. table.GetColumnIndex(dimCol + height.Property),
  295. table.GetColumnIndex(dimCol + weight.Property)
  296. };
  297. }
  298. public static TDim ToDimensions<TDim>(this CoreRow row, int[] columnIndices)
  299. where TDim : IDimensions, new()
  300. {
  301. var dimensions = new TDim();
  302. dimensions.Unit.ID = row.Get<Guid>(columnIndices[0]);
  303. dimensions.Quantity = row.Get<double>(columnIndices[1]);
  304. dimensions.Length = row.Get<double>(columnIndices[2]);
  305. dimensions.Width = row.Get<double>(columnIndices[3]);
  306. dimensions.Height = row.Get<double>(columnIndices[4]);
  307. dimensions.Weight = row.Get<double>(columnIndices[5]);
  308. return dimensions;
  309. }
  310. public static TDim ToDimensions<T, TDim>(this CoreRow row, Expression<Func<T, TDim>> dim)
  311. where TDim : IDimensions, new()
  312. {
  313. var dimensions = new TDim();
  314. var dimCol = CoreUtils.GetFullPropertyName(dim, ".") + ".";
  315. dimensions.Unit.ID = row.Get<Guid>(dimCol + unitid.Property);
  316. dimensions.Length = row.Get<double>(dimCol + length.Property);
  317. dimensions.Quantity = row.Get<double>(dimCol + quantity.Property);
  318. dimensions.Width = row.Get<double>(dimCol + width.Property);
  319. dimensions.Height = row.Get<double>(dimCol + height.Property);
  320. dimensions.Weight = row.Get<double>(dimCol + weight.Property);
  321. return dimensions;
  322. }
  323. public static Columns<T> LocalColumns<T>()
  324. where T : IDimensions
  325. {
  326. return Columns.None<T>()
  327. .Add(x => x.Unit.ID)
  328. .Add(x => x.Quantity)
  329. .Add(x => x.Length)
  330. .Add(x => x.Width)
  331. .Add(x => x.Height)
  332. .Add(x => x.Weight)
  333. .Add(x => x.UnitSize)
  334. .Add(x => x.Value);
  335. }
  336. public static Columns<T> DataColumns<T>()
  337. where T : IDimensions
  338. {
  339. return Columns.None<T>()
  340. .Add(x => x.Unit.ID)
  341. .Add(x => x.Unit.Format)
  342. .Add(x => x.Unit.Formula)
  343. .Add(x => x.Unit.HasHeight)
  344. .Add(x => x.Unit.HasWeight)
  345. .Add(x => x.Unit.HasWidth)
  346. .Add(x => x.Unit.HasQuantity)
  347. .Add(x => x.Unit.HasLength)
  348. .Add(x => x.Quantity)
  349. .Add(x => x.Length)
  350. .Add(x => x.Width)
  351. .Add(x => x.Height)
  352. .Add(x => x.Weight)
  353. .Add(x => x.UnitSize)
  354. .Add(x => x.Value);
  355. }
  356. public static Columns<T> AllColumns<T>()
  357. where T : IDimensions
  358. {
  359. return Columns.None<T>()
  360. .Add(x => x.Unit.ID)
  361. .Add(x => x.Unit.Format)
  362. .Add(x => x.Unit.Formula)
  363. .Add(x => x.Unit.HasHeight)
  364. .Add(x => x.Unit.HasWeight)
  365. .Add(x => x.Unit.HasWidth)
  366. .Add(x => x.Unit.HasQuantity)
  367. .Add(x => x.Unit.HasLength)
  368. .Add(x => x.Unit.Code)
  369. .Add(x => x.Unit.Description)
  370. .Add(x => x.Quantity)
  371. .Add(x => x.Length)
  372. .Add(x => x.Width)
  373. .Add(x => x.Height)
  374. .Add(x => x.Weight)
  375. .Add(x => x.UnitSize)
  376. .Add(x => x.Value);
  377. }
  378. public static Columns<T> AddDimensionsColumns<T, TDim>(this Columns<T> columns, Expression<Func<T, TDim>> dim, ColumnsType type = ColumnsType.Local)
  379. where TDim : IDimensions
  380. {
  381. return columns.AddSubColumns(dim, type switch
  382. {
  383. ColumnsType.Data => DataColumns<TDim>(),
  384. ColumnsType.All => AllColumns<TDim>(),
  385. ColumnsType.Local => LocalColumns<TDim>(),
  386. _ => LocalColumns<TDim>()
  387. });
  388. }
  389. public static bool Evaluate<T>(string? formula, object? quantity, object? length, object? width, object? height, object? weight, out T result)
  390. {
  391. if (!String.IsNullOrWhiteSpace(formula))
  392. {
  393. var variables = new Dictionary<string, object?>()
  394. {
  395. { "Quantity", Convert.ToDouble(quantity) },
  396. { "Length", Convert.ToDouble(length) },
  397. { "Width", Convert.ToDouble(width) },
  398. { "Height", Convert.ToDouble(height) },
  399. { "Weight", Convert.ToDouble(weight) }
  400. };
  401. try
  402. {
  403. var expr = new CoreExpression(formula);
  404. var eval = expr.Evaluate(variables);
  405. result = (T)CoreUtils.ChangeType(eval, typeof(T));
  406. return true;
  407. }
  408. catch (Exception e)
  409. {
  410. Logger.Send(LogType.Information, "", String.Format("Error in Formula: [{0}] ({1})", formula, e.Message));
  411. result = default(T);
  412. return false;
  413. }
  414. }
  415. result = default(T);
  416. return true;
  417. }
  418. public static T Copy<T>(this T dim)
  419. where T : IDimensions, new()
  420. {
  421. var newDim = new T();
  422. newDim.CopyFrom(dim);
  423. return newDim;
  424. }
  425. }
  426. }