home.js 10 KB

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