enterTransactions.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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. while(tbody.children.length > 0){
  15. tbody.removeChild(tbody.firstChild);
  16. }
  17. for(let recipe of merchant.recipes){
  18. let row = document.createElement("tr");
  19. row._id = recipe._id;
  20. tbody.appendChild(row);
  21. let name = document.createElement("td");
  22. name.innerText = recipe.name;
  23. row.appendChild(name);
  24. let quantity = document.createElement("td");
  25. row.appendChild(quantity);
  26. let input = document.createElement("input");
  27. input.type = "number";
  28. input.step = "1";
  29. input.value = "0";
  30. quantity.appendChild(input);
  31. }
  32. },
  33. submit: function(){
  34. let tbody = document.querySelector("#enterTransactionsAction tbody");
  35. let recipes = [];
  36. for(let row of tbody.children){
  37. let quantity = row.children[1].children[0].value;
  38. if(quantity > 0){
  39. let recipe = {
  40. id: row._id,
  41. quantity: quantity
  42. }
  43. recipes.push(recipe);
  44. }else if(quantity < 0){
  45. banner.createError("Cannot have negative quantities");
  46. return;
  47. }
  48. }
  49. if(recipes.length > 0){
  50. axios.post("/transactions/create", recipes)
  51. .then((response)=>{
  52. if(typeof(response.data) === "string"){
  53. banner.createError(response.data);
  54. }else{
  55. for(let recipe of recipes){
  56. let merchRecipe = merchant.recipes.find(r => r._id === recipe.id);
  57. for(let recipeIngredient of merchRecipe.ingredients){
  58. let merchInvIngredient = merchant.inventory.find(i => i.ingredient._id === recipeIngredient.ingredient._id);
  59. merchInvIngredient.quantity -= recipeIngredient.quantity * recipe.quantity;
  60. }
  61. }
  62. inventoryObj.isPopulated = false;
  63. inventoryObj.display();
  64. banner.createNotification("Your sales have been logged");
  65. }
  66. })
  67. .catch((err)=>{
  68. banner.createError("Something went wrong and your sales could not be logged");
  69. });
  70. }
  71. }
  72. }