Ingredient.js 3.8 KB

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