home.js 9.5 KB

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