home.js 9.4 KB

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