DimensionUnit.cs 2.7 KB

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