transactionDetails.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. let transactionDetails = {
  2. transaction: {},
  3. display: function(transaction){
  4. this.transaction = transaction;
  5. let recipeList = document.getElementById("transactionRecipes");
  6. let template = document.getElementById("transactionRecipe").content.children[0];
  7. let totalRecipes = 0;
  8. let totalPrice = 0;
  9. while(recipeList.children.length > 0){
  10. recipeList.removeChild(recipeList.firstChild);
  11. }
  12. for(let i = 0; i < transaction.recipes.length; i++){
  13. let recipe = template.cloneNode(true);
  14. let price = transaction.recipes[i].quantity * transaction.recipes[i].recipe.price;
  15. recipe.children[0].innerText = transaction.recipes[i].recipe.name;
  16. recipe.children[1].innerText = `${transaction.recipes[i].quantity} x $${transaction.recipes[i].recipe.price.toFixed(2)}`;
  17. recipe.children[2].innerText = `$${price.toFixed(2)}`;
  18. recipe.onclick = ()=>{
  19. controller.openStrand("recipeBook");
  20. controller.openSidebar("recipeDetails", transaction.recipes[i].recipe);
  21. }
  22. recipeList.appendChild(recipe);
  23. totalRecipes += transaction.recipes[i].quantity;
  24. totalPrice += price;
  25. }
  26. let months = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
  27. let days = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"];
  28. let dateString = `${days[transaction.date.getDay()]}, ${months[transaction.date.getMonth()]} ${transaction.date.getDate()}, ${transaction.date.getFullYear()}`;
  29. document.getElementById("transactionDate").innerText = dateString;
  30. document.getElementById("totalRecipes").innerText = `${totalRecipes} recipes`;
  31. document.getElementById("totalPrice").innerText = `$${totalPrice.toFixed(2)}`;
  32. let button = document.getElementById("removeTransBtn");
  33. switch(merchant.pos){
  34. case "square":
  35. button.style.display = "none";
  36. break;
  37. case "none":
  38. button.style.display = "block";
  39. button.onclick = ()=>{controller.openModal("confirmDeleteTransaction", transaction)};
  40. }
  41. },
  42. remove: function(){
  43. let loader = document.getElementById("loaderContainer");
  44. loader.style.display = "flex";
  45. fetch(`/transaction/${this.transaction.id}`, {
  46. method: "delete",
  47. headers: {
  48. "Content-Type": "application/json;charset=utf-8"
  49. },
  50. })
  51. .then((response)=>{
  52. if(typeof(response) === "string"){
  53. controller.createBanner(response, "error");
  54. }else{
  55. merchant.removeTransaction(this.transaction);
  56. state.updateTransactions();
  57. let from = new Date();
  58. from.setDate(from.getDate() - 7);
  59. from.setHours(0, 0, 0, 0);
  60. controller.openStrand("transactions", merchant.getTransactions(from, new Date()));
  61. controller.closeModal();
  62. controller.createBanner("TRANSACTION REMOVED", "success");
  63. }
  64. })
  65. .catch((err)=>{
  66. controller.createBanner("SOMETHING WENT WRONG. PLEASE REFRESH THE PAGE", "error");
  67. })
  68. .finally(()=>{
  69. loader.style.display = "none";
  70. });
  71. },
  72. }
  73. module.exports = transactionDetails;