Ingredient.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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. get quantity(){
  13. let subQuantity = this._quantity * controller.unitMultiplier(controller.getBaseUnit(this.unit), this.unit);
  14. let parentMultiplier = controller.unitMultiplier(controller.getBaseUnit(this._ingredient.unit), this._ingredient.unit);
  15. return subQuantity / parentMultiplier;
  16. }
  17. getDisplayQuantity(){
  18. console.log(this._ingredient);
  19. console.log(this._parent);
  20. console.log(this._parent.unit);
  21. return `${parseFloat(this._quantity.toFixed(2))} ${this.ingredient.unit} / ${this._parent.quantity} ${this._parent.unit}`;
  22. }
  23. }
  24. class Ingredient{
  25. constructor(id, name, category, unit, subIngredients, convert, parent){
  26. this._id = id;
  27. this._name = name;
  28. this._category = category;
  29. this.unit = unit;
  30. this._subIngredients = [];
  31. this._parent = parent;
  32. this._convert = convert;
  33. for(let i = 0; i < subIngredients.length; i++){
  34. this._subIngredients.push(new SubIngredient(
  35. subIngredients[i]._id,
  36. subIngredients[i].ingredient,
  37. subIngredients[i].quantity,
  38. subIngredients[i].unit,
  39. this
  40. ));
  41. }
  42. }
  43. get id(){
  44. return this._id;
  45. }
  46. set id(id){
  47. this._id = id;
  48. }
  49. get name(){
  50. return this._name;
  51. }
  52. set name(name){
  53. this._name = name;
  54. }
  55. get category(){
  56. return this._category;
  57. }
  58. set category(category){
  59. this._category = category;
  60. }
  61. get convert(){
  62. return this._convert;
  63. }
  64. get subIngredients(){
  65. return this._subIngredients;
  66. }
  67. addIngredients(ingredients){
  68. for(let i = 0; i < ingredients.length; i++){
  69. this._subIngredients.push({
  70. ingredient: this._parent.getIngredient(ingredients[i].ingredient).ingredient,
  71. quantity: ingredients[i].quantity
  72. });
  73. }
  74. }
  75. replaceIngredients(ingredients){
  76. this._subIngredients = [];
  77. this.addIngredients(ingredients);
  78. }
  79. getUnitCost(){
  80. let totalCost = 0;
  81. let quantity = 0;
  82. for(let i = 0; i < this._parent.orders.length; i++){
  83. for(let j = 0; j < this._parent.orders[i].ingredients.length; j++){
  84. let ingredient = this._parent.orders[i].ingredients[j];
  85. if(ingredient.ingredient === this){
  86. totalCost += ingredient.pricePerUnit * ingredient.quantity;
  87. quantity += ingredient.quantity;
  88. break;
  89. }
  90. }
  91. }
  92. return (quantity === 0) ? 0 : totalCost / quantity;
  93. }
  94. getPotentialUnits(){
  95. switch(controller.getUnitType(this.unit)){
  96. case "mass": return ["g", "kg", "oz", "lb"];
  97. case "volume": return ["ml", "l", "tsp", "tbsp", "ozfl", "cup", "pt", "qt", "gal"];
  98. case "length": return ["mm", "cm", "m", "in", "ft"];
  99. }
  100. }
  101. }
  102. module.exports = Ingredient;