Ingredient.js 3.3 KB

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