DimensionUnit.cs 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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. }
  107. public static class DimensionUnitUtils
  108. {
  109. public static Dictionary<Type, int> UpdateExpressions<T, TLink>(T[] items)
  110. where T : DimensionUnit, new()
  111. where TLink : DimensionUnitLink<T>
  112. {
  113. var dimensionTypes = new List<(Type dimType, Type linkType)>();
  114. foreach(var entity in CoreUtils.Entities)
  115. {
  116. var def = entity.GetSuperclassDefinition(typeof(Dimensions<,>));
  117. if(def != null && def.GenericTypeArguments[1] == typeof(T))
  118. {
  119. dimensionTypes.Add((entity, def.GenericTypeArguments[0]));
  120. }
  121. }
  122. var updateTypes = new Dictionary<Type, List<IProperty>>();
  123. foreach(var entity in CoreUtils.Entities)
  124. {
  125. if(entity.IsSubclassOf(typeof(Entity))
  126. && !entity.HasAttribute<AutoEntity>()
  127. && entity.HasInterface<IRemotable>()
  128. && entity.HasInterface<IPersistent>())
  129. {
  130. if(entity == typeof(SupplierProduct))
  131. {
  132. var props = DatabaseSchema.Properties(entity).OrderBy(x => x.Name).ToArray();
  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 dict = updateTypes.GetValueOrAdd(entity);
  146. dict.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. var results = Client.Create(type).Query(filter, columns).ToObjects(type).Cast<Entity>().ToArray();
  183. if(results.Length > 0)
  184. {
  185. foreach(var result in results)
  186. {
  187. foreach(var property in properties)
  188. {
  189. var id = CoreUtils.GetPropertyValue(result, property.Name + "." + Dimensions.unitid.Property);
  190. if(id is Guid guid)
  191. {
  192. var unit = items.First(x => x.ID == guid);
  193. var dim = (property.Getter()(result) as IDimensions)!;
  194. dim.Calculate(dim.Quantity, dim.Length, dim.Width, dim.Height, dim.Weight, unit.Formula, unit.Format);
  195. }
  196. }
  197. }
  198. results = results.Where(x => x.IsChanged()).ToArray();
  199. nResults[type] = results.Length;
  200. Client.Create(type).Save(results, "Updated Value and UnitSize to match dimension unit.");
  201. }
  202. }
  203. }
  204. return nResults;
  205. }
  206. }
  207. }