newTransaction.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. let newTransaction = {
  2. display: function(){
  3. let recipeList = document.getElementById("newTransactionRecipes");
  4. let template = document.getElementById("createTransaction").content.children[0];
  5. while(recipeList.children.length > 0){
  6. recipeList.removeChild(recipeList.firstChild);
  7. }
  8. for(let i = 0; i < merchant.recipes.length; i++){
  9. let recipeDiv = template.cloneNode(true);
  10. recipeDiv.recipe = merchant.recipes[i];
  11. recipeList.appendChild(recipeDiv);
  12. recipeDiv.children[0].innerText = merchant.recipes[i].name;
  13. }
  14. document.getElementById("submitNewTransaction").onclick = ()=>{this.submit()};
  15. },
  16. submit: function(){
  17. let recipeDivs = document.getElementById("newTransactionRecipes");
  18. let date = document.getElementById("newTransactionDate").valueAsDate;
  19. if(date === null){
  20. controller.createBanner("DATE IS REQUIRED FOR TRANSACTIONS", "error");
  21. return;
  22. }
  23. date.setHours(0, 0, 0, 0);
  24. let data = {
  25. date: date,
  26. recipes: [],
  27. ingredientUpdates: {}
  28. };
  29. for(let i = 0; i < recipeDivs.children.length; i++){
  30. let quantity = recipeDivs.children[i].children[1].value;
  31. const recipe = recipeDivs.children[i].recipe;
  32. if(quantity !== "" && quantity > 0){
  33. data.recipes.push({
  34. recipe: recipe.id,
  35. quantity: quantity
  36. });
  37. for(let j = 0; j < recipe.ingredients.length; j++){
  38. let ingredient = recipe.ingredients[j];
  39. if(data.ingredientUpdates[ingredient.ingredient.id]){
  40. data.ingredientUpdates[ingredient.ingredient.id] += controller.baseUnit(ingredient.quantity, ingredient.ingredient.unit) * quantity;
  41. }else{
  42. data.ingredientUpdates[ingredient.ingredient.id] = controller.baseUnit(ingredient.quantity, ingredient.ingredient.unit) * quantity;
  43. }
  44. }
  45. }else if(quantity < 0){
  46. controller.createBanner("CANNOT HAVE NEGATIVE VALUES", "error");
  47. return;
  48. }
  49. }
  50. if(data.recipes.length > 0){
  51. let loader = document.getElementById("loaderContainer");
  52. loader.style.display = "flex";
  53. fetch("/transaction/create", {
  54. method: "post",
  55. headers: {
  56. "Content-Type": "application/json;charset=utf-8"
  57. },
  58. body: JSON.stringify(data)
  59. })
  60. .then(response => response.json())
  61. .then((response)=>{
  62. if(typeof(response) === "string"){
  63. controller.createBanner(response, "error");
  64. }else{
  65. let thirtyAgo = new Date();
  66. thirtyAgo.setDate(thirtyAgo.getDate() - 30);
  67. thirtyAgo.setHours(0, 0, 0);
  68. if(
  69. merchant.transactions.length === 0 ||
  70. new Date(response.date) > thirtyAgo
  71. ){
  72. merchant.addTransactions([response], true);
  73. state.updateTransactions();
  74. }
  75. let from = new Date();
  76. from.setDate(from.getDate() - 7);
  77. from.setHours(0, 0, 0, 0);
  78. controller.openStrand("transactions", merchant.getTransactions(from, new Date()));
  79. controller.createBanner("TRANSACTION CREATED", "success");
  80. }
  81. })
  82. .catch((err)=>{
  83. controller.createBanner("SOMETHING WENT WRONG. PLEASE REFRESH THE PAGE", "error");
  84. })
  85. .finally(()=>{
  86. loader.style.display = "none";
  87. });
  88. }
  89. }
  90. }
  91. module.exports = newTransaction;