home.js 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. let home = {
  2. isPopulated: false,
  3. display: function(){
  4. if(!this.isPopulated){
  5. this.mostUsedRecipes();
  6. this.drawInventoryCheckCard();
  7. this.drawPopularCard();
  8. this.isPopulated = true;
  9. }
  10. },
  11. mostUsedRecipes: function(){
  12. let from = new Date();
  13. from.setDate(from.getDate() - 30);
  14. let recipes = merchant.getRecipesSold(from, new Date());
  15. recipes.sort((a, b) => (a.quantity > b.quantity) ? -1 : 1);
  16. let displayCount = (recipes.length < 10) ? recipes.length : 10;
  17. let container = document.getElementById("mostUsedRecipesList");
  18. while(container.children.length > 0){
  19. container.removeChild(container.firstChild);
  20. }
  21. for(let i = 0; i < displayCount; i++){
  22. let item = document.createElement("button");
  23. item.classList.add("choosable");
  24. item.onclick = ()=>{
  25. controller.openStrand("recipeBook");
  26. controller.openSidebar("recipeDetails", recipes[i].recipe);
  27. };
  28. container.appendChild(item);
  29. let leftText = document.createElement("p");
  30. leftText.innerText = recipes[i].recipe.name;
  31. item.appendChild(leftText);
  32. let rightText = document.createElement("p");
  33. rightText.innerText = recipes[i].quantity;
  34. item.appendChild(rightText);
  35. }
  36. },
  37. mostUsedIngredients: function(){
  38. let ingredients = [];
  39. let from = new Date();
  40. from.setDate(from.getDate() - 30);
  41. for(let i = 0; i < merchant.inventory.length; i++){
  42. let unitCost = merchant.inventory[i].ingredient.getUnitCost();
  43. let totalCost = unitCost * merchant.inventory[i].getSoldQuantity(from, new Date());
  44. ingredients.push({
  45. inventoryItem: merchant.inventory[i],
  46. unitCost: unitCost,
  47. totalCost: totalCost
  48. });
  49. }
  50. ingredients.sort((a, b) => (a.unitCost > b.unitCost) ? -1 : 1);
  51. let container = document.getElementById("mostUsedBody");
  52. while(container.children.length > 0){
  53. container.removeChild(container.firstChild);
  54. }
  55. let displayCount = (merchant.inventory.length < 10) ? merchant.inventory.length : 10;
  56. for(let i = 0; i < displayCount; i++){
  57. if(ingredients[i].totalCost === 0) break;
  58. let item = document.createElement("tr");
  59. item.classList.add("choosable");
  60. item.onclick = ()=>{
  61. controller.openStrand("ingredients");
  62. controller.openSidebar("ingredientDetails", ingredients[i].inventoryItem);
  63. }
  64. container.appendChild(item);
  65. let leftText = document.createElement("td");
  66. leftText.innerText = ingredients[i].inventoryItem.ingredient.name;
  67. item.appendChild(leftText);
  68. let centerText = document.createElement("td");
  69. centerText.innerText = `$${ingredients[i].unitCost.toFixed(2)}`;
  70. item.appendChild(centerText);
  71. let rightText = document.createElement("td");
  72. rightText.innerText = `$${ingredients[i].totalCost.toFixed(2)}`;
  73. item.appendChild(rightText);
  74. }
  75. },
  76. drawInventoryCheckCard: function(){
  77. let num;
  78. if(merchant.inventory.length < 5){
  79. num = merchant.inventory.length;
  80. }else{
  81. num = 5;
  82. }
  83. let rands = [];
  84. for(let i = 0; i < num; i++){
  85. let rand = Math.floor(Math.random() * merchant.inventory.length);
  86. if(rands.includes(rand)){
  87. i--;
  88. }else{
  89. rands[i] = rand;
  90. }
  91. }
  92. let ul = document.querySelector("#inventoryCheckCard ul");
  93. let template = document.getElementById("ingredientCheck").content.children[0];
  94. while(ul.children.length > 0){
  95. ul.removeChild(ul.firstChild);
  96. }
  97. for(let i = 0; i < rands.length; i++){
  98. let ingredientCheck = template.cloneNode(true);
  99. let input = ingredientCheck.children[1].children[1];
  100. const ingredient = merchant.inventory[rands[i]];
  101. ingredientCheck.ingredient = ingredient;
  102. ingredientCheck.children[0].innerText = ingredient.ingredient.name;
  103. ingredientCheck.children[1].children[0].onclick = ()=>{
  104. input.value--;
  105. input.changed = true;
  106. };
  107. input.value = ingredient.quantity.toFixed(2);
  108. ingredientCheck.children[2].innerText = ingredient.ingredient.unit.toUpperCase();
  109. ingredientCheck.children[1].children[2].onclick = ()=>{
  110. input.value++;
  111. input.changed = true;
  112. }
  113. input.onchange = ()=>{input.changed = true};
  114. ul.appendChild(ingredientCheck);
  115. }
  116. document.getElementById("inventoryCheck").onclick = ()=>{this.submitInventoryCheck()};
  117. },
  118. drawPopularCard: function(){
  119. let thisMonth = new Date();
  120. thisMonth.setDate(1);
  121. const ingredientList = merchant.getIngredientsSold(thisMonth);
  122. if(ingredientList !== false){
  123. ingredientList.sort((a, b)=>{
  124. if(a.quantity < b.quantity){
  125. return 1;
  126. }
  127. if(a.quantity > b.quantity){
  128. return -1;
  129. }
  130. return 0;
  131. });
  132. let quantities = [];
  133. let labels = [];
  134. let colors = [];
  135. let count = (ingredientList.length < 5) ? ingredientList.length - 1 : 4;
  136. for(let i = count; i >= 0; i--){
  137. const ingredientName = ingredientList[i].ingredient.name;
  138. const ingredientQuantity = ingredientList[i].quantity;
  139. const unitName = ingredientList[i].ingredient.unit;
  140. quantities.push(ingredientList[i].quantity);
  141. labels.push(`${ingredientName}: ${ingredientQuantity.toFixed(2)} ${unitName.toUpperCase()}`);
  142. if(i === 0){
  143. colors.push("rgb(255, 99, 107");
  144. }else{
  145. colors.push("rgb(179, 191, 209");
  146. }
  147. }
  148. let trace = {
  149. x: quantities,
  150. type: "bar",
  151. orientation: "h",
  152. text: labels,
  153. textposition: "auto",
  154. hoverinfo: "none",
  155. marker: {
  156. color: colors
  157. }
  158. }
  159. let layout = {
  160. title: {
  161. text: "MOST POPULAR INGREDIENTS"
  162. },
  163. xaxis: {
  164. zeroline: false,
  165. title: "QUANTITY"
  166. },
  167. yaxis: {
  168. showticklabels: false
  169. },
  170. paper_bgcolor: "rgba(0, 0, 0, 0)"
  171. }
  172. if(screen.width < 1200){
  173. layout.margin = {
  174. l: 10,
  175. r: 10,
  176. t: 80,
  177. b: 40
  178. };
  179. }
  180. Plotly.newPlot("popularIngredientsCard", [trace], layout);
  181. }else{
  182. document.getElementById("popularCanvas").style.display = "none";
  183. let notice = document.createElement("p");
  184. notice.innerText = "N/A";
  185. notice.classList = "notice";
  186. document.getElementById("popularIngredientsCard").appendChild(notice);
  187. }
  188. },
  189. //Need to change the updating of ingredients
  190. //should update the ingredient directly, then send that. Maybe...
  191. submitInventoryCheck: function(){
  192. let lis = document.querySelectorAll("#inventoryCheckCard li");
  193. let data = [];
  194. for(let i = 0; i < lis.length; i++){
  195. if(lis[i].children[1].children[1].value >= 0){
  196. if(lis[i].children[1].children[1].changed === true){
  197. let merchIngredient = lis[i].ingredient;
  198. data.push({
  199. id: merchIngredient.ingredient.id,
  200. quantity: lis[i].children[1].children[1].value
  201. });
  202. lis[i].children[1].children[1].changed = false;
  203. }
  204. }else{
  205. controller.createBanner("CANNOT HAVE NEGATIVE INGREDIENTS", "error");
  206. return;
  207. }
  208. }
  209. if(data.length > 0){
  210. let loader = document.getElementById("loaderContainer");
  211. loader.style.display = "flex";
  212. fetch("/merchant/ingredients/update", {
  213. method: "PUT",
  214. headers: {
  215. "Content-Type": "application/json;charset=utf-8"
  216. },
  217. body: JSON.stringify(data)
  218. })
  219. .then(response => response.json())
  220. .then((response)=>{
  221. if(typeof(response) === "string"){
  222. controller.createBanner(response, "error");
  223. }else{
  224. for(let i = 0; i < response.length; i++){
  225. merchant.removeIngredient(merchant.getIngredient(response[i].ingredient._id));
  226. }
  227. merchant.addIngredients(response);
  228. state.updateIngredients();
  229. controller.createBanner("INGREDIENTS UPDATED", "success");
  230. }
  231. })
  232. .catch((err)=>{
  233. controller.createBanner("SOMETHING WENT WRONG. PLEASE REFRESH THE PAGE", "error");
  234. })
  235. .finally(()=>{
  236. loader.style.display = "none";
  237. });
  238. }
  239. }
  240. }
  241. module.exports = home;