Ingredient.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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 unitSize(){
  47. switch(this._unit){
  48. case "g":return this._unitSize;
  49. case "kg": return this._unitSize / 1000;
  50. case "oz": return this._unitSize / 28.3495;
  51. case "lb": return this._unitSize / 453.5924;
  52. case "ml": return this._unitSize * 1000;
  53. case "l": return this._unitSize;
  54. case "tsp": return this._unitSize * 202.8842;
  55. case "tbsp": return this._unitSize * 67.6278;
  56. case "ozfl": return this._unitSize * 33.8141;
  57. case "cup": return this._unitSize * 4.1667;
  58. case "pt": return this._unitSize * 2.1134;
  59. case "qt": return this._unitSize * 1.0567;
  60. case "gal": return this._unitSize / 3.7854;
  61. case "mm": return this._unitSize * 1000;
  62. case "cm": return this._unitSize * 100;
  63. case "m": return this._unitSize;
  64. case "in": return this._unitSize * 39.3701;
  65. case "ft": return this._unitSize * 3.2808;
  66. default: return this._unitSize;
  67. }
  68. }
  69. set unitSize(unitSize){
  70. if(unitSize < 0){
  71. return false;
  72. }
  73. this._unitSize = unitSize;
  74. }
  75. get subIngredients(){
  76. return this._subIngredients;
  77. }
  78. addIngredients(ingredients){
  79. for(let i = 0; i < ingredients.length; i++){
  80. this._subIngredients.push({
  81. ingredient: this._parent.getIngredient(ingredients[i].ingredient).ingredient,
  82. quantity: ingredients[i].quantity
  83. });
  84. }
  85. }
  86. replaceIngredients(ingredients){
  87. this._subIngredients = [];
  88. this.addIngredients(ingredients);
  89. }
  90. getBaseUnitSize(){
  91. return this._unitSize;
  92. }
  93. getNameAndUnit(){
  94. return `${this._name} (${this._unit.toUpperCase()})`;
  95. }
  96. /*
  97. Show matching unit types for this ingredient
  98. return = [String]
  99. */
  100. getPotentialUnits(){
  101. let mass = ["g", "kg", "oz", "lb"];
  102. let volume = ["ml", "l", "tsp", "tbsp", "ozfl", "cup", "pt", "qt", "gal"];
  103. let length = ["mm", "cm", "m", "in", "ft"];
  104. if(mass.includes(this._unit)) return mass;
  105. if(volume.includes(this._unit)) return volume;
  106. if(length.includes(this._unit)) return length;
  107. if(this._unit === "bottle") return volume;
  108. return [];
  109. }
  110. getUnitCost(isDisplay = false){
  111. let totalCost = 0;
  112. let quantity = 0;
  113. for(let i = 0; i < this._parent.orders.length; i++){
  114. for(let j = 0; j < this._parent.orders[i].ingredients.length; j++){
  115. let ingredient = this._parent.orders[i].ingredients[j];
  116. if(ingredient.ingredient === this){
  117. totalCost += ingredient.quantity * (isDisplay === true) ? ingredient.pricePerUnit : ingredient.pricePerBaseUnit;
  118. quantity += ingredient.quantity;
  119. break;
  120. }
  121. }
  122. }
  123. return (quantity === 0) ? 0 : totalCost / quantity;
  124. }
  125. /*
  126. Used when the unit on an ingredient does not match the RecipeIngredient unit
  127. Calculates the multiplier from RecipeIngredient quantity, to ingredient base unit (unit type)
  128. */
  129. calculateRecipeMultiplier(recipeQuantity, ingredientQuantity){
  130. let unitToBase = controller.baseUnit(ingredientQuantity, this._unit);
  131. return unitToBase / recipeQuantity;
  132. }
  133. }
  134. module.exports = Ingredient;