home.js 9.5 KB

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