Ingredient.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. class SubIngredient{
  2. constructor(id, ingredient, quantity, unit, parent){
  3. this.id = id;
  4. this._ingredient = ingredient;
  5. this.quantity = quantity;
  6. this._parent = parent;
  7. }
  8. get ingredient(){
  9. return merchant.getIngredient(this._ingredient).ingredient;
  10. }
  11. get quantity(){
  12. let convertMultiplier = 1;
  13. switch(controller.getBaseUnit(this._ingredient.unit)){
  14. case "g":
  15. convertMultiplier = this._ingredient.convert.toMass;
  16. break;
  17. case "l":
  18. convertMultiplier = this._ingredient.convert.toVolume;
  19. break;
  20. case "m":
  21. convertMultiplier = this._ingredient.convert.toLength;
  22. break;
  23. }
  24. return this._quantity * controller.unitMultiplier(controller.getBaseUnit(this._ingredient.unit), this._ingredient.unit) * convertMultiplier;
  25. }
  26. getDisplayQuantity(){
  27. return `${this._quantity} ${this._ingredient.unit} / ${this._parent.quantity} ${this._parent.unit}`;
  28. }
  29. }
  30. class Ingredient{
  31. constructor(id, name, category, unit, subIngredients, convert, parent){
  32. this._id = id;
  33. this._name = name;
  34. this._category = category;
  35. this._unit = unit;
  36. this._subIngredients = [];
  37. this._parent = parent;
  38. this._convert = convert;
  39. for(let i = 0; i < subIngredients.length; i++){
  40. this._subIngredients.push(new SubIngredient(
  41. subIngredients[i]._id,
  42. subIngredients[i].ingredient,
  43. subIngredients[i].quantity,
  44. this
  45. ));
  46. }
  47. }
  48. get id(){
  49. return this._id;
  50. }
  51. set id(id){
  52. this._id = id;
  53. }
  54. get name(){
  55. return this._name;
  56. }
  57. set name(name){
  58. this._name = name;
  59. }
  60. get category(){
  61. return this._category;
  62. }
  63. set category(category){
  64. this._category = category;
  65. }
  66. get unit(){
  67. return this._unit;
  68. }
  69. set unit(unit){
  70. this._unit = unit;
  71. }
  72. get convert(){
  73. return this._convert;
  74. }
  75. get subIngredients(){
  76. return this._subIngredients;
  77. }
  78. addIngredients(ingredients){
  79. for(let i = 0; i < ingredients.length; i++){
  80. this._subIngredients.push({
  81. ingredient: this._parent.getIngredient(ingredients[i].ingredient).ingredient,
  82. quantity: ingredients[i].quantity
  83. });
  84. }
  85. }
  86. replaceIngredients(ingredients){
  87. this._subIngredients = [];
  88. this.addIngredients(ingredients);
  89. }
  90. getUnitCost(){
  91. let totalCost = 0;
  92. let quantity = 0;
  93. for(let i = 0; i < this._parent.orders.length; i++){
  94. for(let j = 0; j < this._parent.orders[i].ingredients.length; j++){
  95. let ingredient = this._parent.orders[i].ingredients[j];
  96. if(ingredient.ingredient === this){
  97. totalCost += ingredient.pricePerUnit * ingredient.quantity;
  98. quantity += ingredient.quantity;
  99. break;
  100. }
  101. }
  102. }
  103. return (quantity === 0) ? 0 : totalCost / quantity;
  104. }
  105. }
  106. module.exports = Ingredient;