analytics.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. let analytics = {
  2. newData: false,
  3. dateChange: false,
  4. transactions: [],
  5. ingredient: {},
  6. recipe: {},
  7. display: function(Transaction){
  8. document.getElementById("analDateBtn").onclick = ()=>{this.changeDates(Transaction)};
  9. if(this.transactions.length === 0 || this.newData === true){
  10. let startDate = new Date();
  11. startDate.setMonth(startDate.getMonth() - 1);
  12. this.transactions = merchant.getTransactions(startDate);
  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("choosable");
  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("active");
  42. }
  43. li.classList.add("active");
  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("choosable");
  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("active");
  68. }
  69. li.classList.add("active");
  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.length > 0) ? this.transactions[0].date : undefined;
  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(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. if(i === this.transactions.length - 1){
  120. quantities.push(currentQuantity);
  121. dates.push(currentDate);
  122. }
  123. }
  124. let trace = {
  125. x: dates,
  126. y: quantities,
  127. mode: "lines+markers",
  128. line: {
  129. color: "rgb(255, 99, 107)"
  130. }
  131. }
  132. const layout = {
  133. title: this.ingredient.ingredient.name.toUpperCase(),
  134. xaxis: {
  135. title: "DATE"
  136. },
  137. yaxis: {
  138. title: `QUANTITY (${this.ingredient.ingredient.unit.toUpperCase()})`,
  139. }
  140. }
  141. Plotly.newPlot("itemUseGraph", [trace], layout);
  142. //Create use cards
  143. let sum = 0;
  144. let max = 0;
  145. let min = (quantities.length > 0) ? quantities[0] : 0;
  146. for(let i = 0; i < quantities.length; i++){
  147. sum += quantities[i];
  148. if(quantities[i] > max){
  149. max = quantities[i];
  150. }else if(quantities[i] < min){
  151. min = quantities[i];
  152. }
  153. }
  154. document.getElementById("analMinUse").innerText = `${min.toFixed(2)} ${this.ingredient.ingredient.unit}`;
  155. document.getElementById("analAvgUse").innerText = `${(sum / quantities.length).toFixed(2)} ${this.ingredient.ingredient.unit}`;
  156. document.getElementById("analMaxUse").innerText = `${max.toFixed(2)} ${this.ingredient.ingredient.unit}`;
  157. let dayUse = [0, 0, 0, 0, 0, 0, 0];
  158. let dayCount = [0, 0, 0, 0, 0, 0, 0];
  159. for(let i = 0; i < quantities.length; i++){
  160. dayUse[dates[i].getDay()] += quantities[i];
  161. dayCount[dates[i].getDay()]++;
  162. }
  163. document.getElementById("analDayOne").innerText = `${(dayUse[0] / dayCount[0]).toFixed(2)} ${this.ingredient.ingredient.unit}`;
  164. document.getElementById("analDayTwo").innerText = `${(dayUse[1] / dayCount[1]).toFixed(2)} ${this.ingredient.ingredient.unit}`;
  165. document.getElementById("analDayThree").innerText = `${(dayUse[2] / dayCount[2]).toFixed(2)} ${this.ingredient.ingredient.unit}`;
  166. document.getElementById("analDayFour").innerText = `${(dayUse[3] / dayCount[3]).toFixed(2)} ${this.ingredient.ingredient.unit}`;
  167. document.getElementById("analDayFive").innerText = `${(dayUse[4] / dayCount[4]).toFixed(2)} ${this.ingredient.ingredient.unit}`;
  168. document.getElementById("analDaySix").innerText = `${(dayUse[5] / dayCount[5]).toFixed(2)} ${this.ingredient.ingredient.unit}`;
  169. document.getElementById("analDaySeven").innerText = `${(dayUse[6] / dayCount[6]).toFixed(2)} ${this.ingredient.ingredient.unit}`;
  170. },
  171. recipeDisplay: function(){
  172. let quantities = [];
  173. let dates = [];
  174. let currentDate;
  175. let quantity = 0;
  176. if(this.transactions.length > 0){
  177. currentDate = this.transactions[0].date;
  178. }
  179. for(let i = 0; i < this.transactions.length; i++){
  180. if(currentDate.getDate() !== this.transactions[i].date.getDate()){
  181. quantities.push(quantity);
  182. quantity = 0;
  183. dates.push(currentDate);
  184. currentDate = this.transactions[i].date;
  185. }
  186. for(let j = 0; j < this.transactions[i].recipes.length; j++){
  187. const recipe = this.transactions[i].recipes[j];
  188. if(recipe.recipe === this.recipe){
  189. quantity += recipe.quantity;
  190. }
  191. }
  192. if(i === this.transactions.length - 1){
  193. quantities.push(quantity);
  194. dates.push(currentDate);
  195. }
  196. }
  197. const trace = {
  198. x: dates,
  199. y: quantities,
  200. mode: "lines+markers",
  201. line: {
  202. color: "rgb(255, 99, 107"
  203. }
  204. }
  205. const layout = {
  206. title: this.recipe.name.toUpperCase(),
  207. xaxis: {
  208. title: "DATE"
  209. },
  210. yaxis: {
  211. title: "Quantity"
  212. }
  213. }
  214. Plotly.newPlot("recipeSalesGraph", [trace], layout);
  215. let sum = 0;
  216. for(let i = 0; i < quantities.length; i++){
  217. sum += quantities[i];
  218. }
  219. document.getElementById("recipeAvgUse").innerText = (sum / quantities.length).toFixed(2);
  220. document.getElementById("recipeAvgRevenue").innerText = `$${(((sum / quantities.length) * this.recipe.price) / 100).toFixed(2)}`;
  221. },
  222. changeDates: function(Transaction){
  223. let dates = {
  224. from: document.getElementById("analStartDate").valueAsDate,
  225. to: document.getElementById("analEndDate").valueAsDate
  226. }
  227. if(dates.from > dates.to || dates.from === "" || dates.to === "" || dates.to > new Date()){
  228. banner.createError("INVALID DATE");
  229. return;
  230. }
  231. let loader = document.getElementById("loaderContainer");
  232. loader.style.display = "flex";
  233. fetch("/transaction/retrieve", {
  234. method: "post",
  235. headers: {
  236. "Content-Type": "application/json;charset=utf-8"
  237. },
  238. body: JSON.stringify(dates)
  239. })
  240. .then(response => response.json())
  241. .then((response)=>{
  242. if(typeof(response) === "string"){
  243. banner.createError(response.data);
  244. }else{
  245. this.transactions = [];
  246. for(let i = 0; i < response.length; i++){
  247. this.transactions.push(new Transaction(
  248. response[i]._id,
  249. new Date(response[i].date),
  250. response[i].recipes,
  251. merchant
  252. ));
  253. }
  254. let isRecipe = document.getElementById("analSlider").checked;
  255. if(isRecipe && Object.keys(this.recipe).length !== 0){
  256. this.recipeDisplay();
  257. }else if(!isRecipe && Object.keys(this.ingredient).length !== 0){
  258. this.ingredientDisplay();
  259. }
  260. this.dateChange = true;
  261. }
  262. })
  263. .catch((err)=>{
  264. banner.createError("ERROR: UNABLE TO DISPLAY THE DATA");
  265. })
  266. .finally(()=>{
  267. loader.style.display = "none";
  268. });
  269. }
  270. }
  271. module.exports = analytics;