enterTransactions.js 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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. input.id = "transactionQuantity";
  27. quantity.appendChild(input);
  28. }
  29. },
  30. submit: function(){
  31. let tbody = document.querySelector("#enterTransactionsStrand tbody");
  32. let recipesSold = [];
  33. for(let row of tbody.children){
  34. let quantity = document.querySelector("#transactionQuantity").value;
  35. if(quantity >= 0){
  36. let recipe = {
  37. id: row._id,
  38. quantity: quantity
  39. }
  40. recipesSold.push(recipe);
  41. }else{
  42. banner.createError("Cannot have negative quantities");
  43. break;
  44. }
  45. }
  46. axios.post("/transactions/create", recipesSold)
  47. .then(()=>{
  48. banner.createNotification("Your sales have been logged");
  49. inventoryObj.display();
  50. })
  51. .catch((err)=>{
  52. banner.createError("Something went wrong and your sales could not be logged");
  53. });
  54. }
  55. }