enterTransactions.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. window.enterTransactionsObj = {
  2. isPopulated: false,
  3. display: function(){
  4. clearScreen();
  5. document.querySelector("#enterTransactionsAction").style.display = "flex";
  6. console.log(typeof(merchant.lastUpdatedTime));
  7. document.querySelector("#updated").innerText = new Date(Number(merchant.lastUpdatedTime)).toDateString();
  8. if(!this.isPopulated){
  9. this.populateRecipes();
  10. this.isPopulated = true;
  11. }
  12. },
  13. populateRecipes: function(){
  14. let tbody = document.querySelector("#enterTransactionsAction tbody");
  15. for(let recipe of merchant.recipes){
  16. let row = document.createElement("tr");
  17. row._id = recipe._id;
  18. tbody.appendChild(row);
  19. let name = document.createElement("td");
  20. name.innerText = recipe.name;
  21. row.appendChild(name);
  22. let quantity = document.createElement("td");
  23. row.appendChild(quantity);
  24. let input = document.createElement("input");
  25. input.type = "number";
  26. input.step = "1";
  27. input.value = "0";
  28. quantity.appendChild(input);
  29. }
  30. },
  31. submit: function(){
  32. let tbody = document.querySelector("#enterTransactionsAction tbody");
  33. let recipes = [];
  34. for(let row of tbody.children){
  35. let quantity = row.children[1].children[0].value;
  36. if(quantity > 0){
  37. let recipe = {
  38. id: row._id,
  39. quantity: quantity
  40. }
  41. recipes.push(recipe);
  42. }else if(quantity < 0){
  43. banner.createError("Cannot have negative quantities");
  44. break;
  45. }
  46. }
  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. }