dashboard.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450
  1. const home = require("./strands/home.js");
  2. const ingredients = require("./strands/ingredients.js");
  3. const recipeBook = require("./strands/recipeBook.js");
  4. const analytics = require("./strands/analytics.js");
  5. const orders = require("./strands/orders.js");
  6. const transactions = require("./strands/transactions.js");
  7. const account = require("./strands/account.js");
  8. const ingredientDetails = require("./sidebars/ingredientDetails.js");
  9. const newIngredient = require("./sidebars/newIngredient.js");
  10. const editIngredient = require("./sidebars/editIngredient.js");
  11. const newOrder = require("./sidebars/newOrder.js");
  12. const newRecipe = require("./sidebars/newRecipe.js");
  13. const editRecipe = require("./sidebars/editRecipe.js");
  14. const newTransaction = require("./sidebars/newTransaction.js");
  15. const orderDetails = require("./sidebars/orderDetails.js");
  16. const orderFilter = require("./sidebars/orderFilter.js");
  17. const recipeDetails = require("./sidebars/recipeDetails.js");
  18. const transactionDetails = require("./sidebars/transactionDetails.js");
  19. const transactionFilter = require("./sidebars/transactionFilter.js");
  20. const modalScript = require("./modal.js");
  21. const Merchant = require("./classes/Merchant.js");
  22. window.merchant = new Merchant(
  23. data.merchant.name,
  24. data.merchant.pos,
  25. data.merchant.inventory,
  26. data.merchant.recipes,
  27. data.transactions,
  28. data.owner
  29. );
  30. controller = {
  31. openStrand: function(strand, data = undefined){
  32. this.closeSidebar();
  33. let strands = document.querySelectorAll(".strand");
  34. for(let i = 0; i < strands.length; i++){
  35. strands[i].style.display = "none";
  36. }
  37. let buttons = document.querySelectorAll(".menuButton");
  38. for(let i = 0; i < buttons.length - 1; i++){
  39. buttons[i].classList = "menuButton";
  40. buttons[i].disabled = false;
  41. }
  42. let activeButton = {};
  43. switch(strand){
  44. case "home":
  45. activeButton = document.getElementById("homeBtn");
  46. document.getElementById("homeStrand").style.display = "flex";
  47. home.display();
  48. break;
  49. case "ingredients":
  50. activeButton = document.getElementById("ingredientsBtn");
  51. document.getElementById("ingredientsStrand").style.display = "flex";
  52. ingredients.display();
  53. break;
  54. case "recipeBook":
  55. activeButton = document.getElementById("recipeBookBtn");
  56. document.getElementById("recipeBookStrand").style.display = "flex";
  57. recipeBook.display();
  58. break;
  59. case "analytics":
  60. activeButton = document.getElementById("analyticsBtn");
  61. document.getElementById("analyticsStrand").style.display = "flex";
  62. analytics.display();
  63. break;
  64. case "orders":
  65. activeButton = document.getElementById("ordersBtn");
  66. document.getElementById("ordersStrand").style.display = "flex";
  67. orders.display();
  68. break;
  69. case "transactions":
  70. activeButton = document.getElementById("transactionsBtn");
  71. document.getElementById("transactionsStrand").style.display = "flex";
  72. transactions.transactions = data;
  73. transactions.display();
  74. break;
  75. case "account":
  76. activeButton = document.getElementById("accountBtn");
  77. document.getElementById("accountStrand").style.display = "flex";
  78. account.display();
  79. break;
  80. }
  81. activeButton.classList = "menuButton active";
  82. activeButton.disabled = true;
  83. if(screen.height > screen.width || screen.width < 1200){
  84. this.closeMenu();
  85. }
  86. },
  87. /*
  88. Open a specific sidebar
  89. Input:
  90. sidebar: the outermost element of the sidebar (must contain class sidebar)
  91. */
  92. openSidebar: function(sidebar, data = {}){
  93. this.closeSidebar();
  94. document.getElementById("sidebarDiv").classList = "sidebar";
  95. document.getElementById(sidebar).style.display = "flex";
  96. switch(sidebar){
  97. case "ingredientDetails":
  98. ingredientDetails.display(data);
  99. break;
  100. case "newIngredient":
  101. newIngredient.display();
  102. break;
  103. case "editIngredient":
  104. editIngredient.display(data);
  105. break;
  106. case "recipeDetails":
  107. recipeDetails.display(data);
  108. break;
  109. case "editRecipe":
  110. editRecipe.display(data);
  111. break;
  112. case "addRecipe":
  113. newRecipe.display();
  114. break;
  115. case "orderDetails":
  116. orderDetails.display(data);
  117. break;
  118. case "orderFilter":
  119. orderFilter.display();
  120. break;
  121. case "newOrder":
  122. newOrder.display();
  123. break;
  124. case "transactionDetails":
  125. transactionDetails.display(data);
  126. break;
  127. case "transactionFilter":
  128. transactionFilter.display();
  129. break;
  130. case "newTransaction":
  131. newTransaction.display();
  132. break;
  133. }
  134. if(screen.height > screen.width || screen.width < 1200){
  135. document.querySelector(".contentBlock").style.display = "none";
  136. document.getElementById("mobileMenuSelector").style.display = "none";
  137. document.getElementById("sidebarCloser").style.display = "block";
  138. }
  139. },
  140. closeSidebar: function(){
  141. let sidebar = document.getElementById("sidebarDiv");
  142. for(let i = 0; i < sidebar.children.length; i++){
  143. if(sidebar.children[i].style.display !== "none"){
  144. sidebar.children[i].style.display = "none";
  145. let choosables = [];
  146. switch(sidebar.children[i].id){
  147. case "ingredientDetails":
  148. choosables = document.querySelectorAll(".ingredient");
  149. break;
  150. case "transactionDetails":
  151. choosables = document.getElementById("transactionsList").children;
  152. break;
  153. case "recipeDetails":
  154. choosables = document.getElementById("recipeList").children;
  155. break;
  156. case "orderDetails":
  157. choosables = document.getElementById("orderList").children;
  158. break;
  159. }
  160. for(let i = 0; i < choosables.length; i++){
  161. choosables[i].classList.remove("active");
  162. }
  163. }
  164. }
  165. sidebar.classList = "sidebarHide";
  166. if(screen.height > screen.width || screen.width < 1200){
  167. document.querySelector(".contentBlock").style.display = "flex";
  168. document.getElementById("mobileMenuSelector").style.display = "block";
  169. document.getElementById("sidebarCloser").style.display = "none";
  170. }
  171. },
  172. openModal: function(str, data){
  173. let modal = document.getElementById("modal");
  174. modal.style.display = "flex";
  175. document.getElementById("modalClose").addEventListener("click", this.closeModal);
  176. let content = {};
  177. switch(str){
  178. case "ingredientSpreadsheet":
  179. content = document.getElementById("modalSpreadsheetUpload");
  180. content.style.display = "flex";
  181. document.getElementById("modalSpreadsheetTitle").innerText = "ingredients";
  182. document.getElementById("spreadsheetDownload").href = "/ingredients/download/spreadsheet";
  183. content.onsubmit = newIngredient.submitSpreadsheet;
  184. break;
  185. case "recipeSpreadsheet":
  186. content = document.getElementById("modalSpreadsheetUpload");
  187. content.style.display = "flex";
  188. document.getElementById("modalSpreadsheetTitle").innerText = "recipes";
  189. document.getElementById("spreadsheetDownload").href = "/recipes/download/spreadsheet";
  190. content.onsubmit = newRecipe.submitSpreadsheet;
  191. break;
  192. case "orderSpreadsheet":
  193. content = document.getElementById("modalSpreadsheetUpload");
  194. content.style.display = "flex";
  195. document.getElementById("modalSpreadsheetTitle").innerText = "orders";
  196. document.getElementById("spreadsheetDownload").href = "/orders/download/spreadsheet";
  197. content.onsubmit = newOrder.submitSpreadsheet;
  198. break;
  199. case "transactionSpreadsheet":
  200. content = document.getElementById("modalSpreadsheetUpload");
  201. content.style.display = "flex";
  202. document.getElementById("modalSpreadsheetTitle").innerText = "transactions";
  203. document.getElementById("spreadsheetDownload").href = "/transactions/download/spreadsheet";
  204. content.onsubmit = newTransaction.submitSpreadsheet;
  205. break;
  206. case "feedback":
  207. modalScript.feedback();
  208. break;
  209. case "newMerchant":
  210. modalScript.newMerchant();
  211. break;
  212. case "confirmDeleteMerchant":
  213. content = document.getElementById("modalConfirm");
  214. content.style.display = "flex";
  215. content.children[1].innerText = "Are you sure you want to delete this merchant?";
  216. content.children[2].children[0].onclick = ()=>{controller.closeModal()};
  217. content.children[2].children[1].onclick = ()=>{account.deleteMerchant()};
  218. break;
  219. case "confirmDeleteIngredient":
  220. content = document.getElementById("modalConfirm");
  221. content.style.display = "flex";
  222. content.children[1].innerText = `Are you sure you want to delete ingredient: ${data.ingredient.name}?`;
  223. content.children[2].children[0].onclick = ()=>{controller.closeModal()};
  224. content.children[2].children[1].onclick = ()=>{ingredientDetails.remove(data)};
  225. break;
  226. case "confirmDeleteRecipe":
  227. content = document.getElementById("modalConfirm");
  228. content.style.display = "flex";
  229. content.children[1].innerText = `Are you sure you want to delete recipe: ${data.name}?`;
  230. content.children[2].children[0].onclick = ()=>{controller.closeModal()};
  231. content.children[2].children[1].onclick = ()=>{recipeDetails.remove(data)};
  232. break;
  233. case "confirmDeleteOrder":
  234. content = document.getElementById("modalConfirm");
  235. content.style.display = "flex";
  236. content.children[1].innerText = `Are you sure you want to delete order: ${data.name}`;
  237. content.children[2].children[0].onclick = ()=>{controller.closeModal()};
  238. content.children[2].children[1].onclick = ()=>{orderDetails.remove(data)};
  239. break;
  240. case "confirmDeleteTransaction":
  241. content = document.getElementById("modalConfirm");
  242. content.style.display = "flex";
  243. content.children[1].innerText = `Are you sure you want to delete this transaction?`;
  244. content.children[2].children[0].onclick = ()=>{controller.closeModal()};
  245. content.children[2].children[1].onclick = ()=>{transactionDetails.remove(data)};
  246. break;
  247. case "squareLocations":
  248. modalScript.squareLocations(data);
  249. break;
  250. case "editSubIngredients":
  251. modalScript.editSubIngredients(data);
  252. break;
  253. case "circularReference":
  254. modalScript.circularReference(data);
  255. break;
  256. }
  257. },
  258. closeModal: function(){
  259. let modal = document.getElementById("modal");
  260. let modalContent = document.getElementById("modalContent");
  261. for(let i = 0; i < modalContent.children.length; i++){
  262. modalContent.children[i].style.display = "none";
  263. }
  264. modal.style.display = "none";
  265. },
  266. createBanner: function(text, status){
  267. let container = document.getElementById("bannerContainer");
  268. let template = document.getElementById("banner").content.children[0];
  269. let banner = template.cloneNode(true);
  270. switch(status){
  271. case "error":
  272. banner.children[0].style.backgroundColor = "rgb(200, 0, 0)";
  273. banner.children[0].children[0].style.display = "block";
  274. break;
  275. case "alert":
  276. banner.children[0].style.backgroundColor = "rgb(230, 210, 0)";
  277. banner.children[0].children[1].style.display = "block";
  278. break;
  279. case "success":
  280. banner.children[0].style.backgroundColor = "rgb(0, 145, 55)";
  281. banner.children[0].children[2].style.display = "block";
  282. break;
  283. }
  284. banner.children[1].innerText = text;
  285. container.appendChild(banner);
  286. let timer = setTimeout(()=>{
  287. container.removeChild(banner);
  288. }, 10000);
  289. banner.children[2].addEventListener("click", ()=>{
  290. container.removeChild(banner);
  291. clearTimeout(timer);
  292. });
  293. },
  294. changeMenu: function(){
  295. let menu = document.querySelector(".menu");
  296. let buttons = document.querySelectorAll(".menuButton");
  297. let links = document.getElementById("menuLinks");
  298. let merchantName = document.getElementById("menuLocationName");
  299. if(!menu.classList.contains("menuMinimized")){
  300. merchantName.style.display = "none";
  301. menu.classList = "menu menuMinimized";
  302. for(let i = 0; i < buttons.length; i++){
  303. buttons[i].children[1].style.display = "none";
  304. }
  305. document.getElementById("max").style.display = "none";
  306. document.getElementById("min").style.display = "flex";
  307. links.children[0].style.fontSize = "12px";
  308. links.children[1].style.display = "none";
  309. }else if(menu.classList.contains("menuMinimized")){
  310. merchantName.style.display = "block";
  311. menu.classList = "menu";
  312. for(let i = 0; i < buttons.length; i++){
  313. buttons[i].children[1].style.display = "block";
  314. }
  315. document.getElementById("max").style.display = "flex";
  316. document.getElementById("min").style.display = "none";
  317. links.children[0].style.fontSize = "16px";
  318. links.children[1].style.display = "block";
  319. setTimeout(()=>{
  320. document.getElementById("max").style.display = "flex";
  321. document.getElementById("min").style.display = "none";
  322. }, 150);
  323. }
  324. },
  325. openMenu: function(){
  326. document.getElementById("menu").style.display = "flex";
  327. document.querySelector(".contentBlock").style.display = "none";
  328. document.getElementById("mobileMenuSelector").onclick = ()=>{this.closeMenu()};
  329. },
  330. closeMenu: function(){
  331. document.getElementById("menu").style.display = "none";
  332. document.querySelector(".contentBlock").style.display = "flex";
  333. document.getElementById("mobileMenuSelector").onclick = ()=>{this.openMenu()};
  334. },
  335. baseUnit(quantity, unit){
  336. switch(unit){
  337. case "g": return quantity;
  338. case "kg": return quantity * 1000;
  339. case "oz": return quantity * 28.3495;
  340. case "lb": return quantity * 453.5924;
  341. case "ml": return quantity / 1000;
  342. case "l": return quantity;
  343. case "tsp": return quantity / 202.8842;
  344. case "tbsp": return quantity / 67.6278;
  345. case "ozfl": return quantity / 33.8141;
  346. case "cup": return quantity / 4.1667;
  347. case "pt": return quantity / 2.1134;
  348. case "qt": return quantity / 1.0567;
  349. case "gal": return quantity * 3.7854;
  350. case "mm": return quantity / 1000;
  351. case "cm": return quantity / 100;
  352. case "m": return quantity;
  353. case "in": return quantity / 39.3701;
  354. case "ft": return quantity / 3.2808;
  355. default: return quantity;
  356. }
  357. }
  358. }
  359. window.state = {
  360. updateIngredients: function(){
  361. ingredients.populateByProperty();
  362. analytics.populateButtons();
  363. home.drawInventoryCheckCard();
  364. },
  365. updateRecipes: function(){
  366. recipeBook.populateRecipes();
  367. analytics.populateButtons();
  368. },
  369. updateTransactions: function(){
  370. home.isPopulated = false;
  371. ingredients.populateByProperty();
  372. analytics.displayIngredient();
  373. analytics.displayRecipe();
  374. home.drawRevenueGraph();
  375. },
  376. updateOrders: function(){
  377. ingredients.isPopulated = false;
  378. ordersStrand.isPopulated = false;
  379. },
  380. updateMerchant(){
  381. this.updateIngredients();
  382. this.updateRecipes();
  383. this.updateTransactions();
  384. this.updateOrders();
  385. document.getElementById("menuLocationName").innerText = merchant.name;
  386. recipeBook.isPopulated = false;
  387. }
  388. }
  389. //Add click listeners for menu buttons
  390. document.getElementById("menuShifter").onclick = ()=>{controller.changeMenu()}
  391. document.getElementById("menuShifter2").onclick = ()=>{controller.changeMenu()}
  392. document.getElementById("homeBtn").onclick = ()=>{controller.openStrand("home")};
  393. document.getElementById("ingredientsBtn").onclick = ()=>{controller.openStrand("ingredients")};
  394. document.getElementById("recipeBookBtn").onclick = ()=>{controller.openStrand("recipeBook")};
  395. document.getElementById("analyticsBtn").onclick = ()=>{controller.openStrand("analytics")};
  396. document.getElementById("ordersBtn").onclick = async ()=>{controller.openStrand("orders")};
  397. document.getElementById("transactionsBtn").onclick = ()=>{controller.openStrand("transactions", merchant.getTransactions())};
  398. document.getElementById("accountBtn").onclick = ()=>{controller.openStrand("account")};
  399. document.getElementById("feedbackButton").onclick = ()=>{controller.openModal("feedback")};
  400. document.getElementById("menuLocationName").innerText = merchant.name;
  401. controller.openStrand("home");