DimensionUnit.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. using InABox.Core;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. namespace Comal.Classes
  6. {
  7. public abstract class DimensionUnit : Entity, IRemotable, IPersistent, ISequenceable, IDimensionUnit
  8. {
  9. [UniqueCodeEditor(Visible = Visible.Default, Editable = Editable.Enabled)]
  10. [EditorSequence(1)]
  11. public virtual String Code { get; set; }
  12. [TextBoxEditor]
  13. [EditorSequence(2)]
  14. public virtual String Description { get; set; }
  15. [CheckBoxEditor]
  16. [EditorSequence(3)]
  17. public virtual bool HasQuantity { get; set; } = false;
  18. [CheckBoxEditor]
  19. [EditorSequence(4)]
  20. public virtual bool HasLength { get; set; } = false;
  21. [CheckBoxEditor]
  22. [EditorSequence(5)]
  23. public virtual bool HasWidth { get; set; } = false;
  24. [CheckBoxEditor]
  25. [EditorSequence(6)]
  26. public virtual bool HasHeight { get; set; } = false;
  27. [CheckBoxEditor]
  28. [EditorSequence(7)]
  29. public virtual bool HasWeight { get; set; } = false;
  30. [ExpressionEditor(null, typeof(DimensionsExpressionModelGenerator))]
  31. [EditorSequence(8)]
  32. public virtual string Formula { get; set; } = "1";
  33. [ExpressionEditor(null, typeof(DimensionsExpressionModelGenerator))]
  34. [EditorSequence(9)]
  35. public virtual string Format { get; set; } = "\"EACH\"";
  36. [NullEditor]
  37. public long Sequence { get; set; }
  38. public bool Validate(List<String> errors)
  39. {
  40. bool result = true;
  41. var variables = new Dictionary<string, object?>()
  42. {
  43. { "Quantity", 1.00F },
  44. { "Length", 1.00F },
  45. { "Width", 1.00F },
  46. { "Height", 1.00F },
  47. { "Weight", 1.00F }
  48. };
  49. try
  50. {
  51. var expr = new CoreExpression(Formula);
  52. expr.Evaluate(variables);
  53. result = true;
  54. }
  55. catch (Exception e)
  56. {
  57. errors.Add($"{Code}: Formula [{Formula}] => {e.Message}");
  58. result = false;
  59. }
  60. try
  61. {
  62. var expr = new CoreExpression(Format);
  63. expr.Evaluate(variables);
  64. result = true;
  65. }
  66. catch (Exception e)
  67. {
  68. errors.Add($"{Code}: Format [{Format}] => {e.Message}");
  69. result = false;
  70. }
  71. return result;
  72. }
  73. private class DimensionsExpressionModelGenerator : IExpressionModelGenerator
  74. {
  75. public List<string> GetVariables(object?[] items)
  76. {
  77. var dimensionUnits = items.Select(x => x as IDimensionUnit).Where(x => x != null).Cast<IDimensionUnit>();
  78. var variables = new List<string>();
  79. if (dimensionUnits.All(x => x.HasQuantity)) variables.Add("Quantity");
  80. if (dimensionUnits.All(x => x.HasLength)) variables.Add("Length");
  81. if (dimensionUnits.All(x => x.HasWidth)) variables.Add("Width");
  82. if (dimensionUnits.All(x => x.HasHeight)) variables.Add("Height");
  83. if (dimensionUnits.All(x => x.HasWeight)) variables.Add("Weight");
  84. return variables;
  85. }
  86. }
  87. }
  88. }