enterTransactions.js 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. window.enterTransactionsObj = {
  2. isPopulated: false,
  3. display: function(){
  4. clearScreen();
  5. document.querySelector("#enterTransactionsAction").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 recipes = [];
  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. recipes.push(recipe);
  40. }else if(quantity < 0){
  41. banner.createError("Cannot have negative quantities");
  42. break;
  43. }
  44. }
  45. axios.post("/transactions/create", recipes)
  46. .then((response)=>{
  47. if(typeof(response.data) === "string"){
  48. banner.createError(response.data);
  49. }else{
  50. for(let recipe of recipes){
  51. let merchRecipe = merchant.recipes.find(r => r._id === recipe.id);
  52. for(let recipeIngredient of merchRecipe.ingredients){
  53. let merchInvIngredient = merchant.inventory.find(i => i.ingredient._id === recipeIngredient.ingredient._id);
  54. merchInvIngredient.quantity -= recipeIngredient.quantity * recipe.quantity;
  55. }
  56. }
  57. inventoryObj.isPopulated = false;
  58. inventoryObj.display();
  59. banner.createNotification("Your sales have been logged");
  60. }
  61. })
  62. .catch((err)=>{
  63. banner.createError("Something went wrong and your sales could not be logged");
  64. });
  65. }
  66. }