home.js 9.4 KB

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