home.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. window.homeStrandObj = {
  2. isPopulated: false,
  3. graph: {},
  4. display: function(){
  5. if(!this.isPopulated){
  6. let today = new Date();
  7. let firstOfMonth = new Date(today.getFullYear(), today.getMonth(), 1);
  8. let firstOfLastMonth = new Date(today.getFullYear(), today.getMonth() - 1, 1);
  9. let lastMonthtoDay = new Date(new Date().setMonth(today.getMonth() - 1));
  10. let revenueThisMonth = this.calculateRevenue(dateIndices(firstOfMonth));
  11. let revenueLastmonthToDay = this.calculateRevenue(dateIndices(firstOfLastMonth, lastMonthtoDay));
  12. document.querySelector("#revenue").innerText = `$${revenueThisMonth.toLocaleString("en")}`;
  13. let revenueChange = ((revenueThisMonth - revenueLastmonthToDay) / revenueLastmonthToDay) * 100;
  14. let img = "";
  15. if(revenueChange >= 0){
  16. img = "/shared/images/upArrow.png";
  17. }else{
  18. img = "/shared/images/downArrow.png";
  19. }
  20. document.querySelector("#revenueChange p").innerText = `${Math.abs(revenueChange).toFixed(2)}% vs last month`;
  21. document.querySelector("#revenueChange img").src = img;
  22. let graphCanvas = document.querySelector("#graphCanvas");
  23. graphCanvas.height = graphCanvas.parentElement.clientHeight;
  24. graphCanvas.width = graphCanvas.parentElement.clientWidth;
  25. this.graph = new LineGraph(graphCanvas);
  26. this.graph.addTitle("Revenue");
  27. let thirtyAgo = new Date(today);
  28. thirtyAgo.setDate(today.getDate() - 29);
  29. this.graph.addData(
  30. this.graphData(dateIndices(thirtyAgo)),
  31. [thirtyAgo, new Date()],
  32. "Revenue"
  33. );
  34. //Inventory Check
  35. let rands = [];
  36. for(let i = 0; i < 5; i++){
  37. let rand = Math.floor(Math.random() * merchant.inventory.length);
  38. if(rands.includes(rand)){
  39. i--;
  40. }else{
  41. rands[i] = rand;
  42. }
  43. }
  44. let ul = document.querySelector("#inventoryCheckCard ul");
  45. for(let rand of rands){
  46. let li = document.createElement("li");
  47. li.classList = "flexRow";
  48. li.ingredientIndex = rand;
  49. ul.appendChild(li);
  50. let name = document.createElement("p");
  51. name.innerText = merchant.inventory[rand].ingredient.name;
  52. li.appendChild(name);
  53. let input = document.createElement("input");
  54. input.type = "number";
  55. input.value = merchant.inventory[rand].quantity;
  56. li.appendChild(input);
  57. let label = document.createElement("p");
  58. label.innerText = merchant.inventory[rand].ingredient.unit;
  59. li.appendChild(label);
  60. }
  61. //Most Popular ingredients
  62. let dataArray = [];
  63. let now = new Date();
  64. let thisMonth = new Date(now.getFullYear(), now.getMonth(), 1);
  65. let ingredientList = this.ingredientsSold(this.getDateIndex(thisMonth));
  66. for(let i = 0; i < 5; i++){
  67. let max = ingredientList[0].quantity
  68. let index = 0;
  69. for(let j = 0; j < ingredientList.length; j++){
  70. if(ingredientList[j].quantity > max){
  71. max = ingredientList[j].quantity;
  72. index = j;
  73. }
  74. }
  75. dataArray.push({
  76. num: max,
  77. label: `${ingredientList[index].name}: ${ingredientList[index].quantity} ${ingredientList[index].unit}`
  78. });
  79. ingredientList.splice(index, 1);
  80. }
  81. let thisCanvas = document.querySelector("#popularCanvas");
  82. thisCanvas.width = thisCanvas.parentElement.clientWidth;
  83. thisCanvas.height = thisCanvas.parentElement.clientHeight;
  84. let popularGraph = new HorizontalBarGraph(document.querySelector("#popularCanvas"));
  85. popularGraph.addData(dataArray);
  86. this.isPopulated = true;
  87. }
  88. },
  89. calculateRevenue: function(indices){
  90. let total = 0;
  91. for(let i = indices[0]; i <= indices[1]; i++){
  92. for(let recipe of transactions[i].recipes){
  93. for(let merchRecipe of merchant.recipes){
  94. if(recipe.recipe === merchRecipe._id){
  95. total += recipe.quantity * merchRecipe.price;
  96. }
  97. }
  98. }
  99. }
  100. return total / 100;
  101. },
  102. graphData: function(indices){
  103. let dataList = new Array(30).fill(0);
  104. let currentDate = transactions[indices[0]].date;
  105. let arrayIndex = 0;
  106. for(let i = indices[0]; i <= indices[1]; i++){
  107. if(transactions[i].date.getDate() !== currentDate.getDate()){
  108. currentDate = transactions[i].date;
  109. arrayIndex++;
  110. }
  111. for(let recipe of transactions[i].recipes){
  112. for(let merchRecipe of merchant.recipes){
  113. if(recipe.recipe === merchRecipe._id){
  114. dataList[arrayIndex] = parseFloat((dataList[arrayIndex] + (recipe.quantity * merchRecipe.price) / 100).toFixed(2));
  115. break;
  116. }
  117. }
  118. }
  119. }
  120. return dataList;
  121. },
  122. updateInventory: function(){
  123. let lis = document.querySelectorAll("#inventoryCheckCard ul li");
  124. let changes = [];
  125. for(let li of lis){
  126. if(li.children[1].value >= 0){
  127. let merchIngredient = merchant.inventory[li.ingredientIndex];
  128. let change = li.children[1].value - merchIngredient.quantity;
  129. if(change !== 0){
  130. changes.push({
  131. id: merchIngredient.ingredient._id,
  132. quantityChange: change
  133. });
  134. }
  135. }else{
  136. banner.createError("Cannot have negative ingredients");
  137. return;
  138. }
  139. }
  140. if(changes.length > 0){
  141. fetch("/merchant/ingredients/update", {
  142. method: "POST",
  143. headers: {
  144. "Content-Type": "application/json;charset=utf-8"
  145. },
  146. body: JSON.stringify(changes)
  147. })
  148. .then((response)=>{
  149. banner.createError("Ingredients updated");
  150. 1
  151. if(typeof(response.data) === "string"){
  152. console.log(err);
  153. }else{
  154. for(let change of changes){
  155. merchant.inventory.find((item)=> item.ingredient._id === change.id).quantity += change.quantityChange;
  156. }
  157. }
  158. })
  159. .catch((err)=>{
  160. console.log(err);
  161. });
  162. }
  163. },
  164. //Gets the quantity of each ingredient sold between two dates (dateRange)
  165. //Inputs
  166. // dateRange: array containing a start date and an end date
  167. //Output
  168. // List of objects
  169. // id: id of specific ingredient
  170. // quantity: quantity sold of that ingredient
  171. // name: name of the ingredient
  172. ingredientsSold: function(dateRange){
  173. let recipes = this.recipesSold(dateRange);
  174. let ingredientList = [];
  175. for(let recipe of recipes){
  176. for(let merchRecipe of merchant.recipes){
  177. for(let ingredient of merchRecipe.ingredients){
  178. let exists = false;
  179. for(let item of ingredientList){
  180. if(item.id === ingredient.ingredient._id){
  181. exists = true;
  182. item.quantity += ingredient.quantity * recipe.quantity;
  183. break;
  184. }
  185. }
  186. if(!exists){
  187. ingredientList.push({
  188. id: ingredient.ingredient._id,
  189. quantity: ingredient.quantity * recipe.quantity,
  190. name: ingredient.ingredient.name,
  191. unit: ingredient.ingredient.unit
  192. })
  193. }
  194. }
  195. }
  196. }
  197. return ingredientList;
  198. },
  199. //Gets the number of recipes sold between two dates (dateRange)
  200. //Inputs
  201. // dateRange: array containing a start date and an end date
  202. //Output
  203. // List of objects
  204. // id: id of specific recipe
  205. // quantity: quantity sold of that recipe
  206. recipesSold: function(dateRange){
  207. let recipeList = [];
  208. for(let i = dateRange[0]; i <= dateRange[1]; i++){
  209. for(let recipe of transactions[i].recipes){
  210. let exists = false;
  211. for(let item of recipeList){
  212. if(item.id === recipe.recipe){
  213. exists = true;
  214. item.quantity += recipe.quantity;
  215. break;
  216. }
  217. }
  218. if(!exists){
  219. recipeList.push({
  220. id: recipe.recipe,
  221. quantity: recipe.quantity
  222. })
  223. }
  224. }
  225. }
  226. return recipeList;
  227. },
  228. //Gives start and stop indices from transactions for a date range
  229. //Inputs
  230. // from: datetime to start at
  231. // to: datetimie to end at
  232. //Output
  233. // Array containing 2 elements, start index and stop index
  234. getDateIndex: function(from, to = new Date()){
  235. let indexRange = [0, transactions.length - 1];
  236. for(let i = 0; i < transactions.length; i++){
  237. if(from <= transactions[i].date && indexRange[0] === 0){
  238. indexRange[0] = i;
  239. }
  240. if(to < transactions[i].date){
  241. indexRange[1] = i - 1;
  242. break;
  243. }
  244. }
  245. return indexRange;
  246. }
  247. }