enterTransactions.js 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. let enterTransactionsObj = {
  2. isPopulated: false,
  3. display: function(){
  4. controller.clearScreen();
  5. controller.enterTransactionsStrand.style.display = "flex";
  6. if(!this.isPopulated){
  7. this.populateRecipes();
  8. this.isPopulated = true;
  9. }
  10. },
  11. populateRecipes: function(){
  12. let tbody = document.querySelector("#enterTransactionsStrand tbody");
  13. for(let recipe of merchant.recipes){
  14. let row = document.createElement("tr");
  15. row._id = recipe._id;
  16. tbody.appendChild(row);
  17. let name = document.createElement("td");
  18. name.innerText = recipe.name;
  19. row.appendChild(name);
  20. let quantity = document.createElement("td");
  21. row.appendChild(quantity);
  22. let input = document.createElement("input");
  23. input.type = "number";
  24. input.step = "1";
  25. input.value = "0";
  26. quantity.appendChild(input);
  27. }
  28. },
  29. submit: function(){
  30. let tbody = document.querySelector("#enterTransactionsStrand tbody");
  31. let recipesSold = [];
  32. for(let row of tbody.children){
  33. let quantity = row.children[1].children[0].value;
  34. if(quantity > 0){
  35. let recipe = {
  36. id: row._id,
  37. quantity: quantity
  38. }
  39. recipesSold.push(recipe);
  40. }else if(quantity < 0){
  41. banner.createError("Cannot have negative quantities");
  42. break;
  43. }
  44. }
  45. axios.post("/transactions/create", recipesSold)
  46. .then(()=>{
  47. for(let soldRecipe of recipesSold){
  48. let merchRecipe = merchant.recipes.find(r => r._id === soldRecipe.id);
  49. for(let recipeIngredient of merchRecipe.ingredients){
  50. let merchInvIngredient = merchant.inventory.find(i => i.ingredient._id === recipeIngredient.ingredient);
  51. merchInvIngredient.quantity -= recipeIngredient.quantity * soldRecipe.quantity;
  52. }
  53. }
  54. inventoryObj.isPopulated = false;
  55. inventoryObj.display();
  56. banner.createNotification("Your sales have been logged");
  57. })
  58. .catch((err)=>{
  59. banner.createError("Something went wrong and your sales could not be logged");
  60. });
  61. }
  62. }