transactionDetails.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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 $${parseFloat(transaction.recipes[i].recipe.price / 100).toFixed(2)}`;
  17. recipe.children[2].innerText = `$${(price / 100).toFixed(2)}`;
  18. recipeList.appendChild(recipe);
  19. totalRecipes += transaction.recipes[i].quantity;
  20. totalPrice += price;
  21. }
  22. let months = ["January", "Fecbruary", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
  23. let days = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"];
  24. let dateString = `${days[transaction.date.getDay()]}, ${months[transaction.date.getMonth()]} ${transaction.date.getDate()}, ${transaction.date.getFullYear()}`;
  25. document.getElementById("transactionDate").innerText = dateString;
  26. document.getElementById("transactionTime").innerText = transaction.date.toLocaleTimeString();
  27. document.getElementById("totalRecipes").innerText = `${totalRecipes} recipes`;
  28. document.getElementById("totalPrice").innerText = `$${(totalPrice / 100).toFixed(2)}`;
  29. if(merchant.pos === "none"){
  30. document.getElementById("removeTransBtn").onclick = ()=>{this.remove()};
  31. }
  32. },
  33. remove: function(){
  34. let loader = document.getElementById("loaderContainer");
  35. loader.style.display = "flex";
  36. fetch(`/transaction/${this.transaction.id}`, {
  37. method: "delete",
  38. headers: {
  39. "Content-Type": "application/json;charset=utf-8"
  40. },
  41. })
  42. .then(response => response.json())
  43. .then((response)=>{
  44. if(typeof(response) === "string"){
  45. banner.createError(response);
  46. }else{
  47. let ingredients = {};
  48. for(let i = 0; i < this.transaction.recipes.length; i++){
  49. let recipe = this.transaction.recipes[i];
  50. for(let j = 0; j < recipe.recipe.ingredients.length; j++){
  51. ingredient = recipe.recipe.ingredients[j];
  52. ingredients[ingredient.ingredient.id] = ingredient.quantity * recipe.quantity;
  53. }
  54. }
  55. merchant.editTransactions(this.transaction, ingredients, true);
  56. banner.createNotification("TRANSACTION REMOVED");
  57. }
  58. })
  59. .catch((err)=>{
  60. banner.createError("SOMETHING WENT WRONG. PLEASE REFRESH THE PAGE");
  61. })
  62. .finally(()=>{
  63. loader.style.display = "none";
  64. });
  65. },
  66. }
  67. module.exports = transactionDetails;