Ingredient.js 5.3 KB

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