DimensionUnit.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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. && entity.HasInterface<IPersistent>())
  133. {
  134. foreach(var property in DatabaseSchema.Properties(entity))
  135. {
  136. if (property.Parent is null
  137. || property.Parent.Parent is null
  138. || property.IsCalculated
  139. || property.Parent.HasParentEntityLink()
  140. || !typeof(TLink).IsAssignableFrom(property.Parent.PropertyType)
  141. || !property.Name.EndsWith(".ID")) continue;
  142. var dimType = dimensionTypes.FirstOrDefault(x => property.Parent.Parent.PropertyType == x.dimType);
  143. if(dimType.dimType != null)
  144. {
  145. var propList = updateTypes.GetValueOrAdd(entity);
  146. propList.Add(property.Parent.Parent);
  147. }
  148. }
  149. }
  150. }
  151. var nResults = new Dictionary<Type, int>();
  152. var ids = items.ToArray(x => x.ID);
  153. foreach(var (type, properties) in updateTypes)
  154. {
  155. var columns = Columns.Create(type, ColumnTypeFlags.Required);
  156. foreach(var prop in properties)
  157. {
  158. columns.Add(prop.Name + "." + Dimensions.unitid.Property);
  159. columns.Add(prop.Name + "." + Dimensions.quantity.Property);
  160. columns.Add(prop.Name + "." + Dimensions.length.Property);
  161. columns.Add(prop.Name + "." + Dimensions.width.Property);
  162. columns.Add(prop.Name + "." + Dimensions.height.Property);
  163. columns.Add(prop.Name + "." + Dimensions.weight.Property);
  164. columns.Add(prop.Name + "." + Dimensions.unitSize.Property);
  165. columns.Add(prop.Name + "." + Dimensions.value.Property);
  166. }
  167. IFilter? filter = null;
  168. foreach(var prop in properties)
  169. {
  170. var newFilter = Filter.Create(type, prop.Name + "." + Dimensions.unitid.Property, Operator.InList, ids);
  171. if(filter is null)
  172. {
  173. filter = newFilter;
  174. }
  175. else
  176. {
  177. filter = filter.Or(newFilter);
  178. }
  179. }
  180. if(filter != null)
  181. {
  182. progress.Report($"Updating {CoreUtils.Neatify(type.GetCaption())}");
  183. var nTotal = Client.Create(type).Query(filter, Columns.None(type).Add("ID")).Rows.Count;
  184. var nProcessed = 0;
  185. var nResult = 0;
  186. var done = false;
  187. var percentStep = Math.Max(nTotal / 100, 1);
  188. var range = CoreRange.Database(1000);
  189. while(nProcessed < nTotal && !done)
  190. {
  191. var rows = Client.Create(type).Query(filter, columns, range: range).Rows;
  192. if (rows.Count == 0) break;
  193. if(rows.Count < 1000)
  194. {
  195. done = true;
  196. }
  197. range.Next();
  198. var results = new List<Entity>(rows.Count);
  199. for(int i = 0; i < rows.Count; ++i)
  200. {
  201. if(nProcessed % percentStep == 0)
  202. {
  203. progress.Report($"Updating {CoreUtils.Neatify(type.GetCaption())}: {(double)nProcessed / (double)nTotal * 100:F0}%");
  204. }
  205. var obj = (rows[i].ToObject(type) as Entity)!;
  206. foreach(var property in properties)
  207. {
  208. var id = CoreUtils.GetPropertyValue(obj, property.Name + "." + Dimensions.unitid.Property);
  209. if(id is Guid guid)
  210. {
  211. var unit = items.First(x => x.ID == guid);
  212. var dim = (property.Getter()(obj) as IDimensions)!;
  213. dim.Calculate(dim.Quantity, dim.Length, dim.Width, dim.Height, dim.Weight, unit.Formula, unit.Format);
  214. }
  215. }
  216. if (obj.IsChanged())
  217. {
  218. results.Add(obj);
  219. nResult++;
  220. }
  221. nProcessed++;
  222. }
  223. Client.Create(type).Save(results, "Updated Value and UnitSize to match dimension unit.");
  224. }
  225. nResults[type] = nResult;
  226. }
  227. }
  228. return nResults;
  229. }
  230. }
  231. }