Ingredient.js 3.2 KB

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