analytics.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. let analytics = {
  2. dateChange: false,
  3. transactions: [],
  4. ingredient: {},
  5. recipe: {},
  6. display: function(Transaction){
  7. document.getElementById("analDateBtn").onclick = ()=>{this.changeDates(Transaction)};
  8. if(this.transactions.length === 0){
  9. let startDate = new Date();
  10. startDate.setMonth(startDate.getMonth() - 1);
  11. const dateIndices = controller.transactionIndices(merchant.transactions, startDate);
  12. this.transactions = merchant.transactions.slice(dateIndices[0], dateIndices[1]);
  13. }
  14. let slider = document.getElementById("analSlider");
  15. slider.onchange = ()=>{this.display(Transaction)};
  16. let ingredientContent = document.getElementById("analIngredientContent");
  17. let recipeContent = document.getElementById("analRecipeContent");
  18. if(slider.checked){
  19. ingredientContent.style.display = "none";
  20. recipeContent.style.display = "flex";
  21. this.displayRecipes();
  22. }else{
  23. ingredientContent.style.display = "flex";
  24. recipeContent.style.display = "none"
  25. this.displayIngredients();
  26. }
  27. },
  28. displayIngredients: function(){
  29. const itemsList = document.getElementById("itemsList");
  30. while(itemsList.children.length > 0){
  31. itemsList.removeChild(itemsList.firstChild);
  32. }
  33. for(let i = 0; i < merchant.ingredients.length; i++){
  34. let li = document.createElement("li");
  35. li.classList.add("itemButton");
  36. li.item = merchant.ingredients[i];
  37. li.innerText = merchant.ingredients[i].ingredient.name;
  38. li.onclick = ()=>{
  39. const itemsList = document.getElementById("itemsList");
  40. for(let i = 0; i < itemsList.children.length; i++){
  41. itemsList.children[i].classList.remove("analItemActive");
  42. }
  43. li.classList.add("analItemActive");
  44. this.ingredient = merchant.ingredients[i];
  45. this.ingredientDisplay();
  46. };
  47. itemsList.appendChild(li);
  48. }
  49. if(this.dateChange && Object.keys(this.ingredient).length !== 0){
  50. this.ingredientDisplay();
  51. }
  52. this.dateChange = false;
  53. },
  54. displayRecipes: function(){
  55. let recipeList = document.getElementById("analRecipeList");
  56. while(recipeList.children.length > 0){
  57. recipeList.removeChild(recipeList.firstChild);
  58. }
  59. for(let i = 0; i < merchant.recipes.length; i++){
  60. let li = document.createElement("li");
  61. li.classList.add("itemButton");
  62. li.recipe = merchant.recipes[i];
  63. li.innerText = merchant.recipes[i].name;
  64. li.onclick = ()=>{
  65. let recipeList = document.getElementById("analRecipeList");
  66. for(let i = 0; i < recipeList.children.length; i++){
  67. recipeList.children[i].classList.remove("analItemActive");
  68. }
  69. li.classList.add("analItemActive");
  70. this.recipe = merchant.recipes[i];
  71. this.recipeDisplay();
  72. }
  73. recipeList.appendChild(li);
  74. }
  75. if(this.dateChange && Object.keys(this.recipe).length !== 0){
  76. this.recipeDisplay();
  77. }
  78. this.dateChange = false;
  79. },
  80. ingredientDisplay: function(){
  81. //Get list of recipes that contain the ingredient
  82. let containingRecipes = [];
  83. for(let i = 0; i < merchant.recipes.length; i++){
  84. for(let j = 0; j < merchant.recipes[i].ingredients.length; j++){
  85. if(merchant.recipes[i].ingredients[j].ingredient === this.ingredient.ingredient){
  86. containingRecipes.push({
  87. recipe: merchant.recipes[i],
  88. quantity: merchant.recipes[i].ingredients[j].quantity
  89. });
  90. break;
  91. }
  92. }
  93. }
  94. //Create Graph
  95. let quantities = [];
  96. let dates = [];
  97. let currentDate = this.transactions[0].date;
  98. let currentQuantity = 0;
  99. for(let i = 0; i < this.transactions.length; i++){
  100. if(currentDate.getDate() !== this.transactions[i].date.getDate()){
  101. quantities.push(this.ingredient.ingredient.convert(currentQuantity));
  102. dates.push(currentDate);
  103. currentQuantity = 0;
  104. currentDate = this.transactions[i].date;
  105. }
  106. for(let j = 0; j < this.transactions[i].recipes.length; j++){
  107. for(let k = 0; k < containingRecipes.length; k++){
  108. if(this.transactions[i].recipes[j].recipe === containingRecipes[k].recipe){
  109. for(let l = 0; l < this.transactions[i].recipes[j].recipe.ingredients.length; l++){
  110. const transIngredient = this.transactions[i].recipes[j].recipe.ingredients[l];
  111. if(transIngredient.ingredient === this.ingredient.ingredient){
  112. currentQuantity += transIngredient.quantity * this.transactions[i].recipes[j].quantity;
  113. break;
  114. }
  115. }
  116. }
  117. }
  118. }
  119. }
  120. let trace = {
  121. x: dates,
  122. y: quantities,
  123. mode: "lines+markers",
  124. line: {
  125. color: "rgb(255, 99, 107)"
  126. }
  127. }
  128. const layout = {
  129. title: this.ingredient.ingredient.name.toUpperCase(),
  130. xaxis: {
  131. title: "DATE"
  132. },
  133. yaxis: {
  134. title: `QUANTITY (${this.ingredient.ingredient.unit.toUpperCase()})`,
  135. }
  136. }
  137. Plotly.newPlot("itemUseGraph", [trace], layout);
  138. //Create use cards
  139. let sum = 0;
  140. let max = 0;
  141. let min = quantities[0];
  142. for(let i = 0; i < quantities.length; i++){
  143. sum += quantities[i];
  144. if(quantities[i] > max){
  145. max = quantities[i];
  146. }else if(quantities[i] < min){
  147. min = quantities[i];
  148. }
  149. }
  150. document.getElementById("analMinUse").innerText = `${min.toFixed(2)} ${this.ingredient.ingredient.unit}`;
  151. document.getElementById("analAvgUse").innerText = `${(sum / quantities.length).toFixed(2)} ${this.ingredient.ingredient.unit}`;
  152. document.getElementById("analMaxUse").innerText = `${max.toFixed(2)} ${this.ingredient.ingredient.unit}`;
  153. let dayUse = [0, 0, 0, 0, 0, 0, 0];
  154. let dayCount = [0, 0, 0, 0, 0, 0, 0];
  155. for(let i = 0; i < quantities.length; i++){
  156. dayUse[dates[i].getDay()] += quantities[i];
  157. dayCount[dates[i].getDay()]++;
  158. }
  159. document.getElementById("analDayOne").innerText = `${(dayUse[0] / dayCount[0]).toFixed(2)} ${this.ingredient.ingredient.unit}`;
  160. document.getElementById("analDayTwo").innerText = `${(dayUse[1] / dayCount[1]).toFixed(2)} ${this.ingredient.ingredient.unit}`;
  161. document.getElementById("analDayThree").innerText = `${(dayUse[2] / dayCount[2]).toFixed(2)} ${this.ingredient.ingredient.unit}`;
  162. document.getElementById("analDayFour").innerText = `${(dayUse[3] / dayCount[3]).toFixed(2)} ${this.ingredient.ingredient.unit}`;
  163. document.getElementById("analDayFive").innerText = `${(dayUse[4] / dayCount[4]).toFixed(2)} ${this.ingredient.ingredient.unit}`;
  164. document.getElementById("analDaySix").innerText = `${(dayUse[5] / dayCount[5]).toFixed(2)} ${this.ingredient.ingredient.unit}`;
  165. document.getElementById("analDaySeven").innerText = `${(dayUse[6] / dayCount[6]).toFixed(2)} ${this.ingredient.ingredient.unit}`;
  166. },
  167. recipeDisplay: function(){
  168. let quantities = [];
  169. let dates = [];
  170. let currentDate = this.transactions[0].date;
  171. let quantity = 0;
  172. for(let i = 0; i < this.transactions.length; i++){
  173. if(currentDate.getDate() !== this.transactions[i].date.getDate()){
  174. quantities.push(quantity);
  175. quantity = 0;
  176. dates.push(currentDate);
  177. currentDate = this.transactions[i].date;
  178. }
  179. for(let j = 0; j < this.transactions[i].recipes.length; j++){
  180. const recipe = this.transactions[i].recipes[j];
  181. if(recipe.recipe === this.recipe){
  182. quantity += recipe.quantity;
  183. }
  184. }
  185. }
  186. const trace = {
  187. x: dates,
  188. y: quantities,
  189. mode: "lines+markers",
  190. line: {
  191. color: "rgb(255, 99, 107"
  192. }
  193. }
  194. const layout = {
  195. title: this.recipe.name.toUpperCase(),
  196. xaxis: {
  197. title: "DATE"
  198. },
  199. yaxis: {
  200. title: "Quantity"
  201. }
  202. }
  203. Plotly.newPlot("recipeSalesGraph", [trace], layout);
  204. let sum = 0;
  205. for(let i = 0; i < quantities.length; i++){
  206. sum += quantities[i];
  207. }
  208. document.getElementById("recipeAvgUse").innerText = (sum / quantities.length).toFixed(2);
  209. document.getElementById("recipeAvgRevenue").innerText = `$${(((sum / quantities.length) * this.recipe.price) / 100).toFixed(2)}`;
  210. },
  211. changeDates: function(Transaction){
  212. let dates = {
  213. from: document.getElementById("analStartDate").valueAsDate,
  214. to: document.getElementById("analEndDate").valueAsDate
  215. }
  216. if(dates.from > dates.to || dates.from === "" || dates.to === "" || dates.to > new Date()){
  217. banner.createError("INVALID DATE");
  218. return;
  219. }
  220. let loader = document.getElementById("loaderContainer");
  221. loader.style.display = "flex";
  222. fetch("/transaction/retrieve", {
  223. method: "post",
  224. headers: {
  225. "Content-Type": "application/json;charset=utf-8"
  226. },
  227. body: JSON.stringify(dates)
  228. })
  229. .then((response)=>response.json())
  230. .then((response)=>{
  231. if(typeof(response) === "string"){
  232. banner.createError(response.data);
  233. }else{
  234. this.transactions = [];
  235. for(let i = 0; i < response.length; i++){
  236. this.transactions.push(new Transaction(
  237. response[i]._id,
  238. new Date(response[i].date),
  239. response[i].recipes,
  240. merchant
  241. ));
  242. }
  243. let isRecipe = document.getElementById("analSlider").checked;
  244. if(isRecipe && Object.keys(this.recipe).length !== 0){
  245. this.recipeDisplay();
  246. }else if(!isRecipe && Object.keys(this.ingredient).length !== 0){
  247. this.ingredientDisplay();
  248. }
  249. this.dateChange = true;
  250. }
  251. })
  252. .catch((err)=>{
  253. banner.createError("ERROR: UNABLE TO DISPLAY THE DATA");
  254. })
  255. .finally(()=>{
  256. loader.style.display = "none";
  257. });
  258. }
  259. }
  260. module.exports = analytics;