Ingredient.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. class Ingredient{
  2. constructor(id, name, category, unitType, unit, parent, unitSize = undefined){
  3. this._id = id;
  4. this._name = name;
  5. this._category = category;
  6. this._unitType = unitType;
  7. this._unit = unit;
  8. this._parent = parent;
  9. this._unitSize = unitSize;
  10. this._subIngredients = [];
  11. }
  12. get id(){
  13. return this._id;
  14. }
  15. get name(){
  16. return this._name;
  17. }
  18. set name(name){
  19. this._name = name;
  20. }
  21. get category(){
  22. return this._category;
  23. }
  24. set category(category){
  25. this._category = category;
  26. }
  27. get unitType(){
  28. return this._unitType;
  29. }
  30. get unit(){
  31. return this._unit;
  32. }
  33. set unit(unit){
  34. this._unit = unit;
  35. }
  36. get specialUnit(){
  37. return this._specialUnit;
  38. }
  39. get unitSize(){
  40. switch(this._unit){
  41. case "g":return this._unitSize;
  42. case "kg": return this._unitSize / 1000;
  43. case "oz": return this._unitSize / 28.3495;
  44. case "lb": return this._unitSize / 453.5924;
  45. case "ml": return this._unitSize * 1000;
  46. case "l": return this._unitSize;
  47. case "tsp": return this._unitSize * 202.8842;
  48. case "tbsp": return this._unitSize * 67.6278;
  49. case "ozfl": return this._unitSize * 33.8141;
  50. case "cup": return this._unitSize * 4.1667;
  51. case "pt": return this._unitSize * 2.1134;
  52. case "qt": return this._unitSize * 1.0567;
  53. case "gal": return this._unitSize / 3.7854;
  54. case "mm": return this._unitSize * 1000;
  55. case "cm": return this._unitSize * 100;
  56. case "m": return this._unitSize;
  57. case "in": return this._unitSize * 39.3701;
  58. case "ft": return this._unitSize * 3.2808;
  59. default: return this._unitSize;
  60. }
  61. }
  62. set unitSize(unitSize){
  63. if(unitSize < 0){
  64. return false;
  65. }
  66. this._unitSize = unitSize;
  67. }
  68. get subIngredients(){
  69. return this._subIngredients;
  70. }
  71. addIngredients(ingredients){
  72. for(let i = 0; i < ingredients.length; i++){
  73. this._subIngredients.push({
  74. ingredient: this._parent.getIngredient(ingredients[i].ingredient).ingredient,
  75. quantity: ingredients[i].quantity
  76. });
  77. }
  78. }
  79. replaceIngredients(ingredients){
  80. this._subIngredients = [];
  81. this.addIngredients(ingredients);
  82. }
  83. getBaseUnitSize(){
  84. return this._unitSize;
  85. }
  86. getNameAndUnit(){
  87. return `${this._name} (${this._unit.toUpperCase()})`;
  88. }
  89. getPotentialUnits(){
  90. let mass = ["g", "kg", "oz", "lb"];
  91. let volume = ["ml", "l", "tsp", "tbsp", "ozfl", "cup", "pt", "qt", "gal"];
  92. let length = ["mm", "cm", "m", "in", "ft"];
  93. if(mass.includes(this._unit)) return mass;
  94. if(volume.includes(this._unit)) return volume;
  95. if(length.includes(this._unit)) return length;
  96. if(this._unit === "bottle") return volume;
  97. return [];
  98. }
  99. getUnitCost(){
  100. let totalCost = 0;
  101. let quantity = 0;
  102. for(let i = 0; i < this._parent.orders.length; i++){
  103. for(let j = 0; j < this._parent.orders[i].ingredients.length; j++){
  104. let ingredient = this._parent.orders[i].ingredients[j];
  105. if(ingredient.ingredient === this){
  106. totalCost += ingredient.quantity * ingredient.pricePerUnit;
  107. quantity += ingredient.quantity;
  108. break;
  109. }
  110. }
  111. }
  112. return totalCost / quantity;
  113. }
  114. }
  115. module.exports = Ingredient;