Transaction.js 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. class TransactionRecipe{
  2. constructor(recipe, quantity, merchant){
  3. this._recipe = merchant.getRecipe(recipe);
  4. this._quantity = quantity;
  5. }
  6. get recipe(){
  7. return this._recipe;
  8. }
  9. get quantity(){
  10. return this._quantity;
  11. }
  12. }
  13. class Transaction{
  14. constructor(id, date, recipes, parent){
  15. this._id = id;
  16. this._date = new Date(date);
  17. this._recipes = [];
  18. for(let i = 0; i < recipes.length; i++){
  19. this._recipes.push(new TransactionRecipe(
  20. recipes[i].recipe,
  21. recipes[i].quantity,
  22. parent
  23. ));
  24. }
  25. }
  26. get id(){
  27. return this._id;
  28. }
  29. get date(){
  30. return this._date;
  31. }
  32. get recipes(){
  33. return this._recipes;
  34. }
  35. /*
  36. Gets the quantity for a given ingredient
  37. */
  38. getIngredientQuantity(ingredient){
  39. let total = 0;
  40. for(let i = 0; i < this._recipes.length; i++){
  41. total += this._recipes[i].recipe.getIngredientTotal(ingredient.id) * this._recipes[i].quantity;
  42. }
  43. return total;
  44. }
  45. getIngredientQuantityBase(ingredient){
  46. let total = 0;
  47. for(let i = 0; i < this._recipes.length; i++){
  48. console.log(total);
  49. total += this._recipes[i].recipe.getIngredientTotalBase(ingredient.id) * this._recipes[i].quantity;
  50. console.log(total);
  51. }
  52. return total;
  53. }
  54. /*
  55. Gets the quantity for a given recipe
  56. */
  57. getRecipeQuantity(recipe){
  58. for(let i = 0; i < this._recipes.length; i++){
  59. if(this._recipes[i].recipe === recipe) return this._recipes[i].quantity;
  60. }
  61. return 0;
  62. }
  63. }
  64. module.exports = Transaction;