transactionDetails.js 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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. controller.openStrand("transactions", merchant.getTransactions());
  58. controller.closeModal();
  59. controller.createBanner("TRANSACTION REMOVED", "success");
  60. }
  61. })
  62. .catch((err)=>{
  63. controller.createBanner("SOMETHING WENT WRONG. PLEASE REFRESH THE PAGE", "error");
  64. })
  65. .finally(()=>{
  66. loader.style.display = "none";
  67. });
  68. },
  69. }
  70. module.exports = transactionDetails;