Ingredient.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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. addIngredients(ingredients){
  42. for(let i = 0; i < ingredients.length; i++){
  43. this.subIngredients.push(new SubIngredient(
  44. ingredients[i].id,
  45. ingredients[i].ingredient,
  46. ingredients[i].quantity,
  47. ingredients[i].unit,
  48. this
  49. ));
  50. }
  51. }
  52. replaceIngredients(ingredients){
  53. this.subIngredients = [];
  54. this.addIngredients(ingredients);
  55. }
  56. getUnitCost(){
  57. let totalCost = 0;
  58. let quantity = 0;
  59. for(let i = 0; i < this.parent.orders.length; i++){
  60. for(let j = 0; j < this.parent.orders[i].ingredients.length; j++){
  61. let ingredient = this.parent.orders[i].ingredients[j];
  62. if(ingredient.ingredient === this){
  63. totalCost += ingredient.pricePerUnit * ingredient.quantity;
  64. quantity += ingredient.quantity;
  65. break;
  66. }
  67. }
  68. }
  69. return (quantity === 0) ? 0 : totalCost / quantity;
  70. }
  71. getPotentialUnits(){
  72. switch(controller.getUnitType(this.unit)){
  73. case "mass": return ["g", "kg", "oz", "lb"];
  74. case "volume": return ["ml", "l", "tsp", "tbsp", "ozfl", "cup", "pt", "qt", "gal"];
  75. case "length": return ["mm", "cm", "m", "in", "ft"];
  76. case "bottle": return ["ml", "l", "tsp", "tbsp", "ozfl", "cup", "pt", "qt", "gal"];
  77. case "each": return ["each"];
  78. }
  79. }
  80. }
  81. module.exports = Ingredient;