home.js 10 KB

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