analytics.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  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("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.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(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. if(i === this.transactions.length - 1){
  120. quantities.push(this.ingredient.ingredient.convert(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 = this.transactions[0].date;
  175. let quantity = 0;
  176. for(let i = 0; i < this.transactions.length; i++){
  177. if(currentDate.getDate() !== this.transactions[i].date.getDate()){
  178. quantities.push(quantity);
  179. quantity = 0;
  180. dates.push(currentDate);
  181. currentDate = this.transactions[i].date;
  182. }
  183. for(let j = 0; j < this.transactions[i].recipes.length; j++){
  184. const recipe = this.transactions[i].recipes[j];
  185. if(recipe.recipe === this.recipe){
  186. quantity += recipe.quantity;
  187. }
  188. }
  189. if(i === this.transactions.length - 1){
  190. quantities.push(quantity);
  191. dates.push(currentDate);
  192. }
  193. }
  194. const trace = {
  195. x: dates,
  196. y: quantities,
  197. mode: "lines+markers",
  198. line: {
  199. color: "rgb(255, 99, 107"
  200. }
  201. }
  202. const layout = {
  203. title: this.recipe.name.toUpperCase(),
  204. xaxis: {
  205. title: "DATE"
  206. },
  207. yaxis: {
  208. title: "Quantity"
  209. }
  210. }
  211. Plotly.newPlot("recipeSalesGraph", [trace], layout);
  212. let sum = 0;
  213. for(let i = 0; i < quantities.length; i++){
  214. sum += quantities[i];
  215. }
  216. document.getElementById("recipeAvgUse").innerText = (sum / quantities.length).toFixed(2);
  217. document.getElementById("recipeAvgRevenue").innerText = `$${(((sum / quantities.length) * this.recipe.price) / 100).toFixed(2)}`;
  218. },
  219. changeDates: function(Transaction){
  220. let dates = {
  221. from: document.getElementById("analStartDate").valueAsDate,
  222. to: document.getElementById("analEndDate").valueAsDate
  223. }
  224. if(dates.from > dates.to || dates.from === "" || dates.to === "" || dates.to > new Date()){
  225. banner.createError("INVALID DATE");
  226. return;
  227. }
  228. let loader = document.getElementById("loaderContainer");
  229. loader.style.display = "flex";
  230. fetch("/transaction/retrieve", {
  231. method: "post",
  232. headers: {
  233. "Content-Type": "application/json;charset=utf-8"
  234. },
  235. body: JSON.stringify(dates)
  236. })
  237. .then(response => response.json())
  238. .then((response)=>{
  239. if(typeof(response) === "string"){
  240. banner.createError(response.data);
  241. }else{
  242. this.transactions = [];
  243. for(let i = 0; i < response.length; i++){
  244. this.transactions.push(new Transaction(
  245. response[i]._id,
  246. new Date(response[i].date),
  247. response[i].recipes,
  248. merchant
  249. ));
  250. }
  251. let isRecipe = document.getElementById("analSlider").checked;
  252. if(isRecipe && Object.keys(this.recipe).length !== 0){
  253. this.recipeDisplay();
  254. }else if(!isRecipe && Object.keys(this.ingredient).length !== 0){
  255. this.ingredientDisplay();
  256. }
  257. this.dateChange = true;
  258. }
  259. })
  260. .catch((err)=>{
  261. banner.createError("ERROR: UNABLE TO DISPLAY THE DATA");
  262. })
  263. .finally(()=>{
  264. loader.style.display = "none";
  265. });
  266. }
  267. }
  268. module.exports = analytics;