DimensionUnit.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. using InABox.Clients;
  2. using InABox.Core;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.ComponentModel;
  6. using System.Linq;
  7. namespace Comal.Classes
  8. {
  9. public abstract class DimensionUnit : Entity, IRemotable, IPersistent, ISequenceable, IDimensionUnit
  10. {
  11. [UniqueCodeEditor(Visible = Visible.Default, Editable = Editable.Enabled)]
  12. [EditorSequence(1)]
  13. public virtual String Code { get; set; }
  14. [TextBoxEditor]
  15. [EditorSequence(2)]
  16. public virtual String Description { get; set; }
  17. [CheckBoxEditor]
  18. [EditorSequence(3)]
  19. public virtual bool HasQuantity { get; set; } = false;
  20. [CheckBoxEditor]
  21. [EditorSequence(4)]
  22. public virtual bool HasLength { get; set; } = false;
  23. [CheckBoxEditor]
  24. [EditorSequence(5)]
  25. public virtual bool HasWidth { get; set; } = false;
  26. [CheckBoxEditor]
  27. [EditorSequence(6)]
  28. public virtual bool HasHeight { get; set; } = false;
  29. [CheckBoxEditor]
  30. [EditorSequence(7)]
  31. public virtual bool HasWeight { get; set; } = false;
  32. [ExpressionEditor(null, typeof(DimensionsExpressionModelGenerator))]
  33. [EditorSequence(8)]
  34. public virtual string Formula { get; set; } = "1";
  35. [ExpressionEditor(null, typeof(DimensionsExpressionModelGenerator))]
  36. [EditorSequence(9)]
  37. public virtual string Format { get; set; } = "\"EACH\"";
  38. [ScriptEditor]
  39. [EditorSequence(10)]
  40. public virtual string Conversion { get; set; } = "";
  41. [NullEditor]
  42. public long Sequence { get; set; }
  43. public bool HasDimensions() => HasHeight || HasWidth || HasLength || HasWeight || HasQuantity;
  44. public static string ConvertDimensionsMethodName() => "ConvertDimensions";
  45. public static string DefaultConvertDimensionsScript()
  46. {
  47. return
  48. "using Comal.Classes;\n"+
  49. "\n"+
  50. "public class Module\n"+
  51. "{\n"+
  52. " public void " + ConvertDimensionsMethodName() + "(PurchaseOrderItem item)\n"+
  53. " {\n"+
  54. " }\n"+
  55. "}\n";
  56. }
  57. public bool Validate(List<String> errors)
  58. {
  59. bool result = true;
  60. var variables = new Dictionary<string, object?>()
  61. {
  62. { "Quantity", 1.00F },
  63. { "Length", 1.00F },
  64. { "Width", 1.00F },
  65. { "Height", 1.00F },
  66. { "Weight", 1.00F }
  67. };
  68. try
  69. {
  70. var expr = new CoreExpression(Formula);
  71. expr.Evaluate(variables);
  72. result = true;
  73. }
  74. catch (Exception e)
  75. {
  76. errors.Add($"{Code}: Formula [{Formula}] => {e.Message}");
  77. result = false;
  78. }
  79. try
  80. {
  81. var expr = new CoreExpression(Format);
  82. expr.Evaluate(variables);
  83. result = true;
  84. }
  85. catch (Exception e)
  86. {
  87. errors.Add($"{Code}: Format [{Format}] => {e.Message}");
  88. result = false;
  89. }
  90. return result;
  91. }
  92. private class DimensionsExpressionModelGenerator : IExpressionModelGenerator
  93. {
  94. public List<string> GetVariables(object?[] items)
  95. {
  96. var dimensionUnits = items.Select(x => x as IDimensionUnit).Where(x => x != null).Cast<IDimensionUnit>();
  97. var variables = new List<string>();
  98. if (dimensionUnits.All(x => x.HasQuantity)) variables.Add("Quantity");
  99. if (dimensionUnits.All(x => x.HasLength)) variables.Add("Length");
  100. if (dimensionUnits.All(x => x.HasWidth)) variables.Add("Width");
  101. if (dimensionUnits.All(x => x.HasHeight)) variables.Add("Height");
  102. if (dimensionUnits.All(x => x.HasWeight)) variables.Add("Weight");
  103. return variables;
  104. }
  105. }
  106. public override string ToString()
  107. {
  108. return $"(ProductDimensionUnit: {Code})";
  109. }
  110. }
  111. public static class DimensionUnitUtils
  112. {
  113. public static Dictionary<Type, int> UpdateExpressions<T, TLink>(T[] items, IProgress<string> progress)
  114. where T : DimensionUnit, new()
  115. where TLink : DimensionUnitLink<T>
  116. {
  117. var dimensionTypes = new List<(Type dimType, Type linkType)>();
  118. foreach(var entity in CoreUtils.Entities)
  119. {
  120. var def = entity.GetSuperclassDefinition(typeof(Dimensions<,>));
  121. if(def != null && def.GenericTypeArguments[1] == typeof(T))
  122. {
  123. dimensionTypes.Add((entity, def.GenericTypeArguments[0]));
  124. }
  125. }
  126. var updateTypes = new Dictionary<Type, List<IProperty>>();
  127. foreach(var entity in CoreUtils.Entities)
  128. {
  129. if(entity.IsSubclassOf(typeof(Entity))
  130. && !entity.HasAttribute<AutoEntity>()
  131. && entity.HasInterface<IRemotable>())
  132. {
  133. foreach(var property in DatabaseSchema.Properties(entity))
  134. {
  135. if (property.Parent is null
  136. || property.Parent.Parent is null
  137. || property.IsCalculated
  138. || property.Parent.HasParentEntityLink()
  139. || !typeof(TLink).IsAssignableFrom(property.Parent.PropertyType)
  140. || !property.Name.EndsWith(".ID")) continue;
  141. var dimType = dimensionTypes.FirstOrDefault(x => property.Parent.Parent.PropertyType == x.dimType);
  142. if(dimType.dimType != null)
  143. {
  144. var propList = updateTypes.GetValueOrAdd(entity);
  145. propList.Add(property.Parent.Parent);
  146. }
  147. }
  148. }
  149. }
  150. var nResults = new Dictionary<Type, int>();
  151. var ids = items.ToArray(x => x.ID);
  152. foreach(var (type, properties) in updateTypes)
  153. {
  154. var columns = Columns.Create(type, ColumnTypeFlags.Required);
  155. foreach(var prop in properties)
  156. {
  157. columns.Add(prop.Name + "." + Dimensions.unitid.Property);
  158. columns.Add(prop.Name + "." + Dimensions.quantity.Property);
  159. columns.Add(prop.Name + "." + Dimensions.length.Property);
  160. columns.Add(prop.Name + "." + Dimensions.width.Property);
  161. columns.Add(prop.Name + "." + Dimensions.height.Property);
  162. columns.Add(prop.Name + "." + Dimensions.weight.Property);
  163. columns.Add(prop.Name + "." + Dimensions.unitSize.Property);
  164. columns.Add(prop.Name + "." + Dimensions.value.Property);
  165. }
  166. IFilter? filter = null;
  167. foreach(var prop in properties)
  168. {
  169. var newFilter = Filter.Create(type, prop.Name + "." + Dimensions.unitid.Property, Operator.InList, ids);
  170. if(filter is null)
  171. {
  172. filter = newFilter;
  173. }
  174. else
  175. {
  176. filter = filter.Or(newFilter);
  177. }
  178. }
  179. if(filter != null)
  180. {
  181. progress.Report($"Updating {CoreUtils.Neatify(type.GetCaption())}");
  182. var nTotal = Client.Create(type).Query(filter, Columns.None(type).Add("ID")).Rows.Count;
  183. var nProcessed = 0;
  184. var nResult = 0;
  185. var done = false;
  186. var percentStep = Math.Max(nTotal / 100, 1);
  187. var range = CoreRange.Database(1000);
  188. while(nProcessed < nTotal && !done)
  189. {
  190. var rows = Client.Create(type).Query(filter, columns, range: range).Rows;
  191. if (rows.Count == 0) break;
  192. if(rows.Count < 1000)
  193. {
  194. done = true;
  195. }
  196. range.Next();
  197. var results = new List<Entity>(rows.Count);
  198. for(int i = 0; i < rows.Count; ++i)
  199. {
  200. if(nProcessed % percentStep == 0)
  201. {
  202. progress.Report($"Updating {CoreUtils.Neatify(type.GetCaption())}: {(double)nProcessed / (double)nTotal * 100:F0}%");
  203. }
  204. var obj = (rows[i].ToObject(type) as Entity)!;
  205. foreach(var property in properties)
  206. {
  207. var id = CoreUtils.GetPropertyValue(obj, property.Name + "." + Dimensions.unitid.Property);
  208. if(id is Guid guid)
  209. {
  210. var unit = items.First(x => x.ID == guid);
  211. var dim = (property.Getter()(obj) as IDimensions)!;
  212. dim.Calculate(dim.Quantity, dim.Length, dim.Width, dim.Height, dim.Weight, unit.Formula, unit.Format);
  213. }
  214. }
  215. if (obj.IsChanged())
  216. {
  217. results.Add(obj);
  218. nResult++;
  219. }
  220. nProcessed++;
  221. }
  222. Client.Create(type).Save(results, "Updated Value and UnitSize to match dimension unit.");
  223. }
  224. nResults[type] = nResult;
  225. }
  226. }
  227. return nResults;
  228. }
  229. }
  230. }