analytics.js 12 KB

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