enterTransactions.js 2.9 KB

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