Ingredient.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. class Ingredient{
  2. constructor(id, name, category, unitType, unit, parent, unitSize = undefined, convert){
  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. this._convert = convert;
  12. }
  13. get id(){
  14. return this._id;
  15. }
  16. set id(id){
  17. this._id = id;
  18. }
  19. get name(){
  20. return this._name;
  21. }
  22. set name(name){
  23. this._name = name;
  24. }
  25. get category(){
  26. return this._category;
  27. }
  28. set category(category){
  29. this._category = category;
  30. }
  31. get unitType(){
  32. return this._unitType;
  33. }
  34. set unitType(unitType){
  35. this._unitType = unitType;
  36. }
  37. get unit(){
  38. return this._unit;
  39. }
  40. set unit(unit){
  41. this._unit = unit;
  42. }
  43. get specialUnit(){
  44. return this._specialUnit;
  45. }
  46. get convert(){
  47. return this._convert;
  48. }
  49. get unitSize(){
  50. switch(this._unit){
  51. case "g":return this._unitSize;
  52. case "kg": return this._unitSize / 1000;
  53. case "oz": return this._unitSize / 28.3495;
  54. case "lb": return this._unitSize / 453.5924;
  55. case "ml": return this._unitSize * 1000;
  56. case "l": return this._unitSize;
  57. case "tsp": return this._unitSize * 202.8842;
  58. case "tbsp": return this._unitSize * 67.6278;
  59. case "ozfl": return this._unitSize * 33.8141;
  60. case "cup": return this._unitSize * 4.1667;
  61. case "pt": return this._unitSize * 2.1134;
  62. case "qt": return this._unitSize * 1.0567;
  63. case "gal": return this._unitSize / 3.7854;
  64. case "mm": return this._unitSize * 1000;
  65. case "cm": return this._unitSize * 100;
  66. case "m": return this._unitSize;
  67. case "in": return this._unitSize * 39.3701;
  68. case "ft": return this._unitSize * 3.2808;
  69. default: return this._unitSize;
  70. }
  71. }
  72. set unitSize(unitSize){
  73. if(unitSize < 0){
  74. return false;
  75. }
  76. this._unitSize = unitSize;
  77. }
  78. get subIngredients(){
  79. return this._subIngredients;
  80. }
  81. addIngredients(ingredients){
  82. for(let i = 0; i < ingredients.length; i++){
  83. this._subIngredients.push({
  84. ingredient: this._parent.getIngredient(ingredients[i].ingredient).ingredient,
  85. quantity: ingredients[i].quantity
  86. });
  87. }
  88. }
  89. replaceIngredients(ingredients){
  90. this._subIngredients = [];
  91. this.addIngredients(ingredients);
  92. }
  93. getBaseUnitSize(){
  94. return this._unitSize;
  95. }
  96. getNameAndUnit(){
  97. return `${this._name} (${this._unit.toUpperCase()})`;
  98. }
  99. /*
  100. Show matching unit types for this ingredient
  101. return = [String]
  102. */
  103. getPotentialUnits(){
  104. let mass = ["g", "kg", "oz", "lb"];
  105. let volume = ["ml", "l", "tsp", "tbsp", "ozfl", "cup", "pt", "qt", "gal"];
  106. let length = ["mm", "cm", "m", "in", "ft"];
  107. if(mass.includes(this._unit)) return mass;
  108. if(volume.includes(this._unit)) return volume;
  109. if(length.includes(this._unit)) return length;
  110. if(this._unit === "bottle") return volume;
  111. return [];
  112. }
  113. getUnitCost(isDisplay = false){
  114. let totalCost = 0;
  115. let quantity = 0;
  116. for(let i = 0; i < this._parent.orders.length; i++){
  117. for(let j = 0; j < this._parent.orders[i].ingredients.length; j++){
  118. let ingredient = this._parent.orders[i].ingredients[j];
  119. if(ingredient.ingredient === this){
  120. totalCost += ingredient.quantity * (isDisplay === true) ? ingredient.pricePerUnit : ingredient.pricePerBaseUnit;
  121. quantity += ingredient.quantity;
  122. break;
  123. }
  124. }
  125. }
  126. return (quantity === 0) ? 0 : totalCost / quantity;
  127. }
  128. /*
  129. Used when the unit on an ingredient does not match the RecipeIngredient unit
  130. Calculates the multiplier from RecipeIngredient quantity, to ingredient base unit (unit type)
  131. */
  132. calculateRecipeMultiplier(recipeQuantity, ingredientQuantity){
  133. let unitToBase = controller.baseUnit(ingredientQuantity, this._unit);
  134. return unitToBase / recipeQuantity;
  135. }
  136. }
  137. module.exports = Ingredient;