analytics.js 11 KB

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