Transaction.js 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. class TransactionRecipe{
  2. constructor(recipe, quantity, merchant){
  3. this.recipe = merchant.getRecipe(recipe, true);
  4. this.quantity = quantity;
  5. }
  6. }
  7. class Transaction{
  8. constructor(id, date, recipes, parent){
  9. this.id = id;
  10. this.date = new Date(date);
  11. this.recipes = [];
  12. for(let i = 0; i < recipes.length; i++){
  13. this.recipes.push(new TransactionRecipe(
  14. recipes[i].recipe,
  15. recipes[i].quantity,
  16. parent
  17. ));
  18. }
  19. }
  20. /*
  21. Gets the quantity for a given ingredient
  22. */
  23. getIngredientQuantity(ingredient){
  24. let total = 0;
  25. for(let i = 0; i < this.recipes.length; i++){
  26. total += this.recipes[i].recipe.getIngredientTotal(ingredient.id) * this.recipes[i].quantity;
  27. }
  28. return total;
  29. }
  30. /*
  31. Gets the quantity for a given recipe
  32. */
  33. getRecipeQuantity(recipe){
  34. for(let i = 0; i < this.recipes.length; i++){
  35. if(this.recipes[i].recipe === recipe) return this.recipes[i].quantity;
  36. }
  37. return 0;
  38. }
  39. }
  40. module.exports = Transaction;