Ingredient.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. class SubIngredient{
  2. constructor(id, ingredient, quantity, 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. getDisplayQuantity(){
  12. let mult = controller.unitMultiplier(controller.unitType(this.parent.unit), this.parent.unit);
  13. return `${this.quantity * mult} ${this.unit} / ${this.parent.unit}`;
  14. }
  15. }
  16. class Ingredient{
  17. constructor(id, name, category, unit, subIngredients, convert, parent){
  18. this._id = id;
  19. this._name = name;
  20. this._category = category;
  21. this._unit = unit;
  22. this._subIngredients = [];
  23. this._parent = parent;
  24. this._convert = convert;
  25. for(let i = 0; i < subIngredients.length; i++){
  26. this._subIngredients.push(new SubIngredient(
  27. subIngredients[i]._id,
  28. subIngredients[i].ingredient,
  29. subIngredients[i].quantity,
  30. this
  31. ));
  32. }
  33. }
  34. get id(){
  35. return this._id;
  36. }
  37. set id(id){
  38. this._id = id;
  39. }
  40. get name(){
  41. return this._name;
  42. }
  43. set name(name){
  44. this._name = name;
  45. }
  46. get category(){
  47. return this._category;
  48. }
  49. set category(category){
  50. this._category = category;
  51. }
  52. get unit(){
  53. return this._unit;
  54. }
  55. set unit(unit){
  56. this._unit = unit;
  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. }
  92. module.exports = Ingredient;