sidebars.js 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095
  1. let recipeDetailsComp = {
  2. recipe: {},
  3. display: function(recipe){
  4. this.recipe = recipe;
  5. openSidebar(document.querySelector("#recipeDetails"));
  6. document.querySelector("#recipeName").style.display = "block";
  7. document.querySelector("#recipeNameIn").style.display = "none";
  8. document.querySelector("#recipeDetails h1").innerText = recipe.name;
  9. let ingredientList = document.querySelector("#recipeIngredientList");
  10. while(ingredientList.children.length > 0){
  11. ingredientList.removeChild(ingredientList.firstChild);
  12. }
  13. let template = document.querySelector("#recipeIngredient").content.children[0];
  14. for(let i = 0; i < recipe.ingredients.length; i++){
  15. ingredientDiv = template.cloneNode(true);
  16. ingredientDiv.children[0].innerText = recipe.ingredients[i].ingredient.name;
  17. ingredientDiv.children[2].innerText = `${recipe.ingredients[i].ingredient.convert(recipe.ingredients[i].quantity).toFixed(2)} ${recipe.ingredients[i].ingredient.unit}`;
  18. ingredientDiv.ingredient = recipe.ingredients[i].ingredient;
  19. ingredientDiv.name = recipe.ingredients[i].ingredient.name;
  20. ingredientList.appendChild(ingredientDiv);
  21. }
  22. document.querySelector("#addRecIng").style.display = "none";
  23. let price = document.querySelector("#recipePrice");
  24. price.children[1].style.display = "block";
  25. price.children[2].style.display = "none";
  26. price.children[1].innerText = `$${(recipe.price / 100).toFixed(2)}`;
  27. document.querySelector("#recipeUpdate").style.display = "none";
  28. },
  29. edit: function(){
  30. let ingredientDivs = document.querySelector("#recipeIngredientList");
  31. if(merchant.pos === "none"){
  32. let name = document.querySelector("#recipeName");
  33. let nameIn = document.querySelector("#recipeNameIn");
  34. name.style.display = "none";
  35. nameIn.style.display = "block";
  36. nameIn.value = this.recipe.name;
  37. let price = document.querySelector("#recipePrice");
  38. price.children[1].style.display = "none";
  39. price.children[2].style.display = "block";
  40. price.children[2].value = parseFloat((this.recipe.price / 100).toFixed(2));
  41. }
  42. for(let i = 0; i < ingredientDivs.children.length; i++){
  43. let div = ingredientDivs.children[i];
  44. div.children[2].innerText = this.recipe.ingredients[i].ingredient.unit;
  45. div.children[1].style.display = "block";
  46. div.children[1].value = this.recipe.ingredients[i].ingredient.convert(this.recipe.ingredients[i].quantity).toFixed(2);
  47. div.children[3].style.display = "block";
  48. div.children[3].onclick = ()=>{div.parentElement.removeChild(div)};
  49. }
  50. document.querySelector("#addRecIng").style.display = "flex";
  51. document.querySelector("#recipeUpdate").style.display = "flex";
  52. },
  53. update: function(){
  54. this.recipe.name = document.querySelector("#recipeNameIn").value || this.recipe.name;
  55. this.recipe.price = Math.round((document.querySelector("#recipePrice").children[2].value * 100)) || this.recipe.price;
  56. this.recipe.ingredients = [];
  57. let divs = document.querySelector("#recipeIngredientList").children;
  58. for(let i = 0; i < divs.length; i++){
  59. if(divs[i].name === "new"){
  60. let select = divs[i].children[0];
  61. this.recipe.ingredients.push({
  62. ingredient: select.options[select.selectedIndex].ingredient,
  63. quantity: divs[i].children[1].value
  64. });
  65. }else{
  66. this.recipe.ingredients.push({
  67. ingredient: divs[i].ingredient,
  68. quantity: divs[i].children[1].value
  69. });
  70. }
  71. }
  72. let data = {
  73. id: this.recipe.id,
  74. name: this.recipe.name,
  75. price: this.recipe.price,
  76. ingredients: []
  77. }
  78. for(let i = 0; i < this.recipe.ingredients.length; i++){
  79. data.ingredients.push({
  80. ingredient: this.recipe.ingredients[i].ingredient.id,
  81. quantity: this.recipe.ingredients[i].quantity
  82. });
  83. }
  84. let loader = document.getElementById("loaderContainer");
  85. loader.style.display = "flex";
  86. fetch("/recipe/update", {
  87. method: "PUT",
  88. headers: {
  89. "Content-Type": "application/json;charset=utf-8"
  90. },
  91. body: JSON.stringify(data)
  92. })
  93. .then((response) => response.json())
  94. .then((response)=>{
  95. if(typeof(response) === "string"){
  96. banner.createError(response);
  97. }else{
  98. merchant.editRecipes([this.recipe]);
  99. banner.createNotification("RECIPE UPDATE");
  100. }
  101. })
  102. .catch((err)=>{
  103. banner.createError("SOMETHING WENT WRONG. PLEASE REFRESH THE PAGE");
  104. })
  105. .finally(()=>{
  106. loader.style.display = "none";
  107. });
  108. },
  109. remove: function(){
  110. fetch(`/merchant/recipes/remove/${this.recipe.id}`, {
  111. method: "DELETE"
  112. })
  113. .then((response) => response.json())
  114. .then((response)=>{
  115. if(typeof(response) === "string"){
  116. banner.createError(response);
  117. }else{
  118. merchant.editRecipes([this.recipe], true);
  119. banner.createNotification("RECIPE REMOVED");
  120. }
  121. })
  122. .catch((err)=>{
  123. banner.createError("SOMETHING WENT WRONG. PLEASE REFRESH THE PAGE");
  124. });
  125. },
  126. displayAddIngredient: function(){
  127. let template = document.querySelector("#addRecIngredient").content.children[0].cloneNode(true);
  128. template.name = "new";
  129. document.querySelector("#recipeIngredientList").appendChild(template);
  130. let categories = merchant.categorizeIngredients();
  131. for(let i = 0; i < categories.length; i++){
  132. let optGroup = document.createElement("optgroup");
  133. optGroup.label = categories[i].name;
  134. template.children[0].appendChild(optGroup);
  135. for(let j = 0; j < categories[i].ingredients.length; j++){
  136. let option = document.createElement("option");
  137. option.innerText = `${categories[i].ingredients[j].ingredient.name} (${categories[i].ingredients[j].ingredient.unit})`;
  138. option.ingredient = categories[i].ingredients[j].ingredient;
  139. optGroup.appendChild(option);
  140. }
  141. }
  142. }
  143. }
  144. let newOrderComp = {
  145. isPopulated: false,
  146. unused: [],
  147. display: function(){
  148. if(!this.isPopulated){
  149. let categories = merchant.categorizeIngredients();
  150. let categoriesList = document.querySelector("#newOrderCategories");
  151. let template = document.querySelector("#addIngredientsCategory").content.children[0];
  152. let ingredientTemplate = document.querySelector("#addIngredientsIngredient").content.children[0];
  153. for(let i = 0; i < categories.length; i++){
  154. let category = template.cloneNode(true);
  155. category.children[0].children[0].innerText = categories[i].name;
  156. category.children[0].children[1].onclick = ()=>{addIngredientsComp.toggleAddIngredient(category)};
  157. category.children[0].children[1].children[1].style.display = "none";
  158. category.children[1].style.display = "none";
  159. categoriesList.appendChild(category);
  160. for(let j = 0; j < categories[i].ingredients.length; j++){
  161. let ingredientDiv = ingredientTemplate.cloneNode(true);
  162. ingredientDiv.children[0].innerText = categories[i].ingredients[j].ingredient.name;
  163. ingredientDiv.children[1].onclick = ()=>{this.addOne(ingredientDiv, category.children[1])};
  164. ingredientDiv.ingredient = categories[i].ingredients[j].ingredient;
  165. this.unused.push(categories[i].ingredients[j]);
  166. category.children[1].appendChild(ingredientDiv);
  167. }
  168. }
  169. this.isPopulated = true;
  170. }
  171. openSidebar(document.querySelector("#newOrder"));
  172. },
  173. addOne: function(ingredientDiv, container){
  174. for(let i = 0; i < this.unused.length; i++){
  175. if(this.unused[i] === ingredientDiv){
  176. this.unused.splice(i, 1);
  177. break;
  178. }
  179. }
  180. let quantityInput = document.createElement("input");
  181. quantityInput.type = "number";
  182. quantityInput.placeholder = ingredientDiv.ingredient.unit;
  183. quantityInput.min = "0";
  184. quantityInput.step = "0.01";
  185. ingredientDiv.insertBefore(quantityInput, ingredientDiv.children[1]);
  186. let priceInput = document.createElement("input");
  187. priceInput.type = "number";
  188. priceInput.placeholder = "Price Per Unit";
  189. priceInput.min = "0";
  190. priceInput.step = "0.01";
  191. ingredientDiv.insertBefore(priceInput, ingredientDiv.children[2]);
  192. ingredientDiv.children[3].innerText = "-";
  193. ingredientDiv.children[3].onclick = ()=>{this.removeOne(ingredientDiv, container)};
  194. container.removeChild(ingredientDiv);
  195. document.getElementById("newOrderAdded").appendChild(ingredientDiv);
  196. },
  197. removeOne: function(ingredientDiv, container){
  198. this.unused.push(ingredientDiv.ingredient);
  199. ingredientDiv.removeChild(ingredientDiv.children[1]);
  200. ingredientDiv.removeChild(ingredientDiv.children[1]);
  201. ingredientDiv.children[1].innerText = "+";
  202. ingredientDiv.children[1].onclick = ()=>{this.addOne(ingredientDiv, container)};
  203. ingredientDiv.parentElement.removeChild(ingredientDiv);
  204. container.appendChild(ingredientDiv);
  205. },
  206. submit: function(){
  207. let categoriesList = document.getElementById("newOrderAdded");
  208. let ingredients = [];
  209. for(let i = 0; i < categoriesList.children.length; i++){
  210. let quantity = categoriesList.children[i].children[1].value;
  211. let price = categoriesList.children[i].children[2].value;
  212. if(quantity !== "" && price !== ""){
  213. ingredients.push({
  214. ingredient: categoriesList.children[i].ingredient.id,
  215. quantity: parseFloat(quantity),
  216. price: parseInt(price * 100)
  217. });
  218. }
  219. }
  220. let data = {
  221. name: document.getElementById("orderName").value,
  222. date: document.getElementById("orderDate").value,
  223. ingredients: ingredients
  224. }
  225. let loader = document.getElementById("loaderContainer");
  226. loader.style.display = "flex";
  227. fetch("/order", {
  228. method: "POST",
  229. headers: {
  230. "Content-Type": "application/json;charset=utf-8"
  231. },
  232. body: JSON.stringify(data)
  233. })
  234. .then(response => response.json())
  235. .then((response)=>{
  236. if(typeof(response) === "string"){
  237. banner.createError(response);
  238. }else{
  239. let order = new Order(
  240. response._id,
  241. response.name,
  242. response.date,
  243. response.ingredients,
  244. merchant
  245. )
  246. merchant.editOrders([order]);
  247. merchant.editIngredients(order.ingredients, false, true);
  248. banner.createNotification("ORDER CREATED");
  249. }
  250. })
  251. .catch((err)=>{
  252. banner.createError("SOEMTHING WENT WRONG. PLEASE REFRESH THE PAGE");
  253. })
  254. .finally(()=>{
  255. loader.style.display = "none";
  256. });
  257. },
  258. }
  259. let newIngredientComp = {
  260. display: function(){
  261. openSidebar(document.querySelector("#newIngredient"));
  262. document.querySelector("#newIngName").value = "";
  263. document.querySelector("#newIngCategory").value = "";
  264. document.querySelector("#newIngQuantity").value = 0;
  265. },
  266. submit: function(){
  267. let unitSelector = document.getElementById("unitSelector");
  268. let options = document.querySelectorAll("#unitSelector option");
  269. let newIngredient = {
  270. ingredient: {
  271. name: document.getElementById("newIngName").value,
  272. category: document.getElementById("newIngCategory").value,
  273. unitType: options[unitSelector.selectedIndex].getAttribute("type"),
  274. },
  275. quantity: document.querySelector("#newIngQuantity").value,
  276. defaultUnit: unitSelector.value
  277. }
  278. let loader = document.getElementById("loaderContainer");
  279. loader.style.display = "flex";
  280. fetch("/ingredients/create", {
  281. method: "POST",
  282. headers: {
  283. "Content-Type": "application/json;charset=utf-8"
  284. },
  285. body: JSON.stringify(newIngredient)
  286. })
  287. .then((response) => response.json())
  288. .then((response)=>{
  289. if(typeof(response) === "string"){
  290. banner.createError(response);
  291. }else{
  292. merchant.editIngredients([{
  293. ingredient: new Ingredient(
  294. response.ingredient._id,
  295. response.ingredient.name,
  296. response.ingredient.category,
  297. response.ingredient.unitType,
  298. response.defaultUnit,
  299. merchant
  300. ),
  301. quantity: response.quantity
  302. }]);
  303. banner.createNotification("INGREDIENT CREATED");
  304. }
  305. })
  306. .catch((err)=>{
  307. banner.createError("SOMETHING WENT WRONG. PLEASE REFRESH THE PAGE");
  308. })
  309. .finally(()=>{
  310. loader.style.display = "none";
  311. });
  312. }
  313. }
  314. let orderDetailsComp = {
  315. display: function(order){
  316. openSidebar(document.querySelector("#orderDetails"));
  317. document.querySelector("#removeOrderBtn").onclick = ()=>{this.remove(order)};
  318. document.querySelector("#orderDetails h1").innerText = order.name;
  319. document.querySelector("#orderDetails h3").innerText = order.date.toLocaleDateString("en-US");
  320. let ingredientList = document.querySelector("#orderIngredients");
  321. while(ingredientList.children.length > 0){
  322. ingredientList.removeChild(ingredientList.firstChild);
  323. }
  324. let template = document.querySelector("#orderIngredient").content.children[0];
  325. let grandTotal = 0;
  326. for(let i = 0; i < order.ingredients.length; i++){
  327. let ingredientDiv = template.cloneNode(true);
  328. let price = (order.ingredients[i].quantity * order.ingredients[i].price) / 100;
  329. grandTotal += price;
  330. let ingredient = order.ingredients[i].ingredient;
  331. let priceText = ingredient.convert(order.ingredients[i].quantity).toFixed(2) + " " +
  332. ingredient.unit.toUpperCase() + " x $" +
  333. (order.convertPrice(ingredient.unitType, ingredient.unit, order.ingredients[i].price) / 100).toFixed(2);
  334. ingredientDiv.children[0].innerText = order.ingredients[i].ingredient.name;
  335. ingredientDiv.children[1].innerText = priceText;
  336. ingredientDiv.children[2].innerText = `$${price.toFixed(2)}`;
  337. ingredientList.appendChild(ingredientDiv);
  338. }
  339. document.querySelector("#orderTotalPrice p").innerText = `$${grandTotal.toFixed(2)}`;
  340. },
  341. remove: function(order){
  342. let loader = document.getElementById("loaderContainer");
  343. loader.style.display = "flex";
  344. fetch(`/order/${order.id}`, {
  345. method: "DELETE",
  346. headers: {
  347. "Content-Type": "application/json;charset=utf-8"
  348. }
  349. })
  350. .then((response) => response.json())
  351. .then((response)=>{
  352. if(typeof(response) === "string"){
  353. banner.createError(response);
  354. }else{
  355. merchant.editOrders([order], true);
  356. banner.createNotification("ORDER REMOVED");
  357. }
  358. })
  359. .catch((err)=>{
  360. banner.createError("SOMETHING WENT WRONG. PLEASE REFRESH THE PAGE");
  361. })
  362. .finally(()=>{
  363. loader.style.display = "none";
  364. });
  365. }
  366. }
  367. let addIngredientsComp = {
  368. isPopulated: false,
  369. fakeMerchant: {},
  370. chosenIngredients: [],
  371. display: function(){
  372. let sidebar = document.querySelector("#addIngredients");
  373. if(!this.isPopulated){
  374. let loader = document.getElementById("loaderContainer");
  375. loader.style.display = "flex";
  376. fetch("/ingredients")
  377. .then((response) => response.json())
  378. .then((response)=>{
  379. if(typeof(response) === "string"){
  380. banner.createError(response);
  381. }else{
  382. for(let i = 0; i < merchant.ingredients.length; i++){
  383. for(let j = 0; j < response.length; j++){
  384. if(merchant.ingredients[i].ingredient.id === response[j]._id){
  385. response.splice(j, 1);
  386. break;
  387. }
  388. }
  389. }
  390. for(let i = 0; i < response.length; i++){
  391. response[i] = {ingredient: response[i]}
  392. }
  393. this.fakeMerchant = new Merchant({
  394. name: "none",
  395. inventory: response,
  396. recipes: [],
  397. },
  398. []
  399. );
  400. this.populateAddIngredients();
  401. }
  402. })
  403. .catch((err)=>{
  404. banner.createError("UNABLE TO RETRIEVE DATA");
  405. })
  406. .finally(()=>{
  407. loader.style.display = "none";
  408. });
  409. this.isPopulated = true;
  410. }
  411. openSidebar(sidebar);
  412. },
  413. populateAddIngredients: function(){
  414. let addIngredientsDiv = document.getElementById("addIngredientList");
  415. let categoryTemplate = document.getElementById("addIngredientsCategory");
  416. let ingredientTemplate = document.getElementById("addIngredientsIngredient");
  417. let categories = this.fakeMerchant.categorizeIngredients();
  418. while(addIngredientsDiv.children.length > 0){
  419. addIngredientsDiv.removeChild(addIngredientsDiv.firstChild);
  420. }
  421. for(let i = 0; i < categories.length; i++){
  422. let categoryDiv = categoryTemplate.content.children[0].cloneNode(true);
  423. categoryDiv.children[0].children[0].innerText = categories[i].name;
  424. categoryDiv.children[0].children[1].onclick = ()=>{addIngredientsComp.toggleAddIngredient(categoryDiv)};
  425. categoryDiv.children[1].style.display = "none";
  426. categoryDiv.children[0].children[1].children[1].style.display = "none";
  427. addIngredientsDiv.appendChild(categoryDiv);
  428. for(let j = 0; j < categories[i].ingredients.length; j++){
  429. let ingredientDiv = ingredientTemplate.content.children[0].cloneNode(true);
  430. ingredientDiv.children[0].innerText = categories[i].ingredients[j].ingredient.name;
  431. ingredientDiv.children[2].onclick = ()=>{this.addOne(ingredientDiv)};
  432. ingredientDiv.ingredient = categories[i].ingredients[j].ingredient;
  433. categoryDiv.children[1].appendChild(ingredientDiv);
  434. }
  435. }
  436. },
  437. toggleAddIngredient: function(categoryElement){
  438. let button = categoryElement.children[0].children[1];
  439. let ingredientDisplay = categoryElement.children[1];
  440. if(ingredientDisplay.style.display === "none"){
  441. ingredientDisplay.style.display = "flex";
  442. button.children[0].style.display = "none";
  443. button.children[1].style.display = "block";
  444. }else{
  445. ingredientDisplay.style.display = "none";
  446. button.children[0].style.display = "block";
  447. button.children[1].style.display = "none";
  448. }
  449. },
  450. addOne: function(element){
  451. element.parentElement.removeChild(element);
  452. document.getElementById("myIngredients").appendChild(element);
  453. document.getElementById("myIngredientsDiv").style.display = "flex";
  454. for(let i = 0; i < this.fakeMerchant.ingredients.length; i++){
  455. if(this.fakeMerchant.ingredients[i].ingredient === element.ingredient){
  456. this.fakeMerchant.ingredients.splice(i, 1);
  457. this.chosenIngredients.push(element.ingredient);
  458. break;
  459. }
  460. }
  461. let input = document.createElement("input");
  462. input.type = "number";
  463. input.min = "0";
  464. input.step = "0.01";
  465. input.placeholder = "QUANTITY";
  466. element.insertBefore(input, element.children[1]);
  467. element.children[2].style.display = "block";
  468. element.children[3].innerText = "-";
  469. element.children[3].onclick = ()=>{this.removeOne(element)};
  470. },
  471. removeOne: function(element){
  472. element.parentElement.removeChild(element);
  473. element.removeChild(element.children[1]);
  474. element.children[1].style.display = "none";
  475. element.children[2].innerText = "+";
  476. element.children[2].onclick = ()=>{this.addOne(element)};
  477. if(document.getElementById("myIngredients").children.length === 0){
  478. document.getElementById("myIngredientsDiv").style.display = "none";
  479. }
  480. for(let i = 0; i < this.chosenIngredients.length; i++){
  481. if(this.chosenIngredients[i] === element.ingredient){
  482. this.chosenIngredients.splice(i, 1);
  483. this.fakeMerchant.ingredients.push({
  484. ingredient: element.ingredient
  485. });
  486. break;
  487. }
  488. }
  489. this.populateAddIngredients();
  490. },
  491. submit: function(){
  492. let ingredients = document.getElementById("myIngredients").children;
  493. let newIngredients = [];
  494. let fetchable = [];
  495. for(let i = 0; i < ingredients.length; i++){
  496. let quantity = ingredients[i].children[1].value;
  497. let unit = ingredients[i].children[2].value;
  498. if(quantity === ""){
  499. banner.createError("PLEASE ENTER A QUANTITY FOR EACH INGREDIENT YOU WANT TO ADD TO YOUR INVENTORY");
  500. return;
  501. }
  502. quantity = convertToMain(unit, quantity);
  503. let newIngredient = {
  504. ingredient: ingredients[i].ingredient,
  505. quantity: quantity
  506. }
  507. newIngredient.ingredient.unit = unit;
  508. newIngredients.push(newIngredient);
  509. fetchable.push({
  510. id: ingredients[i].ingredient.id,
  511. quantity: quantity,
  512. defaultUnit: unit
  513. });
  514. }
  515. let loader = document.getElementById("loaderContainer");
  516. loader.style.display = "flex";
  517. fetch("/merchant/ingredients/add", {
  518. method: "POST",
  519. headers: {
  520. "Content-Type": "application/json;charset=utf-8"
  521. },
  522. body: JSON.stringify(fetchable)
  523. })
  524. .then((response) => response.json())
  525. .then((response)=>{
  526. if(typeof(response) === "string"){
  527. banner.createError(response);
  528. }else{
  529. merchant.editIngredients(newIngredients);
  530. this.isPopulated = false;
  531. banner.createNotification("ALL INGREDIENTS ADDED");
  532. }
  533. })
  534. .catch((err)=>{
  535. banner.createError("SOMETHING WENT WRONG. PLEASE REFRESH THE PAGE");
  536. })
  537. .finally(()=>{
  538. loader.style.display = "none";
  539. });
  540. }
  541. }
  542. let ingredientDetailsComp = {
  543. ingredient: {},
  544. display: function(ingredient){
  545. this.ingredient = ingredient;
  546. sidebar = document.querySelector("#ingredientDetails");
  547. document.querySelector("#ingredientDetails p").innerText = ingredient.ingredient.category;
  548. document.querySelector("#ingredientDetails h1").innerText = ingredient.ingredient.name;
  549. let ingredientStock = document.getElementById("ingredientStock");
  550. ingredientStock.innerText = `${ingredient.ingredient.convert(ingredient.quantity).toFixed(2)} ${ingredient.ingredient.unit.toUpperCase()}`;
  551. ingredientStock.style.display = "block";
  552. let ingredientInput = document.getElementById("ingredientInput");
  553. ingredientInput.value = ingredient.ingredient.convert(ingredient.quantity).toFixed(2);
  554. ingredientInput.style.display = "none";
  555. let quantities = [];
  556. let now = new Date();
  557. for(let i = 1; i < 31; i++){
  558. let endDay = new Date(now.getFullYear(), now.getMonth(), now.getDate() - i)
  559. let startDay = new Date(now.getFullYear(), now.getMonth(), now.getDate() - i - 1);
  560. let indices = merchant.transactionIndices(startDay, endDay);
  561. if(indices === false){
  562. quantities.push(0);
  563. }else{
  564. quantities.push(merchant.singleIngredientSold(indices, ingredient));
  565. }
  566. }
  567. let sum = 0;
  568. for(let quantity of quantities){
  569. sum += quantity;
  570. }
  571. document.querySelector("#dailyUse").innerText = `${(sum/quantities.length).toFixed(2)} ${ingredient.ingredient.unit}`;
  572. let ul = document.querySelector("#ingredientRecipeList");
  573. let recipes = merchant.getRecipesForIngredient(ingredient.ingredient);
  574. while(ul.children.length > 0){
  575. ul.removeChild(ul.firstChild);
  576. }
  577. for(let i = 0; i < recipes.length; i++){
  578. let li = document.createElement("li");
  579. li.innerText = recipes[i].name;
  580. li.onclick = ()=>{
  581. changeStrand("recipeBookStrand");
  582. recipeDetailsComp.display(recipes[i]);
  583. }
  584. ul.appendChild(li);
  585. }
  586. let ingredientButtons = document.getElementById("ingredientButtons");
  587. let units = merchant.units[this.ingredient.ingredient.unitType];
  588. while(ingredientButtons.children.length > 0){
  589. ingredientButtons.removeChild(ingredientButtons.firstChild);
  590. }
  591. for(let i = 0; i < units.length; i++){
  592. let button = document.createElement("button");
  593. button.classList.add("unitButton");
  594. button.innerText = units[i].toUpperCase();
  595. button.onclick = ()=>{this.changeUnit(button, units[i])};
  596. ingredientButtons.appendChild(button);
  597. if(units[i] === this.ingredient.ingredient.unit){
  598. button.classList.add("unitActive");
  599. }
  600. }
  601. openSidebar(sidebar);
  602. },
  603. remove: function(){
  604. for(let i = 0; i < merchant.recipes.length; i++){
  605. for(let j = 0; j < merchant.recipes[i].ingredients.length; j++){
  606. if(this.ingredient.ingredient === merchant.recipes[i].ingredients[j].ingredient){
  607. banner.createError("MUST REMOVE INGREDIENT FROM ALL RECIPES BEFORE REMOVING FROM INVENTORY");
  608. return;
  609. }
  610. }
  611. }
  612. let loader = document.getElementById("loaderContainer");
  613. loader.style.display = "flex";
  614. fetch(`/merchant/ingredients/remove/${this.ingredient.ingredient.id}`, {
  615. method: "DELETE",
  616. })
  617. .then((response) => response.json())
  618. .then((response)=>{
  619. if(typeof(response) === "string"){
  620. banner.createError(response);
  621. }else{
  622. banner.createNotification("INGREDIENT REMOVED");
  623. merchant.editIngredients([this.ingredient], true);
  624. }
  625. })
  626. .catch((err)=>{})
  627. .finally(()=>{
  628. loader.style.display = "none";
  629. });
  630. },
  631. edit: function(){
  632. document.getElementById("ingredientStock").style.display = "none";
  633. document.getElementById("ingredientInput").style.display = "block";
  634. document.getElementById("editSubmitButton").style.display = "block";
  635. },
  636. editSubmit: function(){
  637. this.ingredient.quantity = Number(document.getElementById("ingredientInput").value);
  638. let data = [{
  639. id: this.ingredient.ingredient.id,
  640. quantity: this.ingredient.quantity
  641. }];
  642. let loader = document.getElementById("loaderContainer");
  643. loader.style.display = "flex";
  644. if(validator.ingredientQuantity(data[0].quantity)){
  645. fetch("/merchant/ingredients/update", {
  646. method: "PUT",
  647. headers: {
  648. "Content-Type": "application/json;charset=utf-8"
  649. },
  650. body: JSON.stringify(data)
  651. })
  652. .then((response) => response.json())
  653. .then((response)=>{
  654. if(typeof(response) === "string"){
  655. banner.createError(response);
  656. }else{
  657. merchant.editIngredients([this.ingredient]);
  658. banner.createNotification("INGREDIENT UPDATED");
  659. }
  660. })
  661. .catch((err)=>{
  662. banner.createError("SOMETHING WENT WRONG. PLEASE REFRESH THE PAGE");
  663. })
  664. .finally(()=>{
  665. loader.style.display = "none";
  666. });
  667. }
  668. },
  669. changeUnit: function(newActive, unit){
  670. this.ingredient.ingredient.unit = unit;
  671. let ingredientButtons = document.querySelectorAll(".unitButton");
  672. for(let i = 0; i < ingredientButtons.length; i++){
  673. ingredientButtons[i].classList.remove("unitActive");
  674. }
  675. newActive.classList.add("unitActive");
  676. ingredientsStrandObj.populateByProperty("category");
  677. document.getElementById("ingredientStock").innerText = `${this.ingredient.ingredient.convert(this.ingredient.quantity).toFixed(2)} ${this.ingredient.ingredient.unit.toUpperCase()}`;
  678. },
  679. changeUnitDefault: function(){
  680. let loader = document.getElementById("loaderContainer");
  681. loader.style.display = "flex";
  682. let id = this.ingredient.ingredient.id;
  683. let unit = this.ingredient.ingredient.unit;
  684. fetch(`/merchant/ingredients/update/${id}/${unit}`, {
  685. method: "put",
  686. headers: {
  687. "Content-Type": "application/json;charset=utf-8"
  688. },
  689. })
  690. .then((response)=>{
  691. if(typeof(response) === "string"){
  692. banner.createError(response);
  693. }else{
  694. banner.createNotification("INGREDIENT DEFAULT UNIT UPDATED");
  695. }
  696. })
  697. .catch((err)=>{
  698. banner.createError("SOMETHING WENT WRONG. PLEASE REFRESH THE PAGE");
  699. })
  700. .finally(()=>{
  701. loader.style.display = "none";
  702. });
  703. }
  704. }
  705. let newRecipeComp = {
  706. display: function(){
  707. let ingredientsSelect = document.querySelector("#recipeInputIngredients select");
  708. let categories = merchant.categorizeIngredients();
  709. while(ingredientsSelect.children.length > 0){
  710. ingredientsSelect.removeChild(ingredientsSelect.firstChild);
  711. }
  712. for(let category of categories){
  713. let optgroup = document.createElement("optgroup");
  714. optgroup.label = category.name;
  715. ingredientsSelect.appendChild(optgroup);
  716. for(let ingredient of category.ingredients){
  717. let option = document.createElement("option");
  718. option.value = ingredient.ingredient.id;
  719. option.innerText = ingredient.ingredient.name;
  720. optgroup.appendChild(option);
  721. }
  722. }
  723. openSidebar(document.querySelector("#addRecipe"));
  724. },
  725. //Updates the number of ingredient inputs displayed for new recipes
  726. changeRecipeCount: function(){
  727. let newCount = document.querySelector("#ingredientCount").value;
  728. let ingredientsDiv = document.querySelector("#recipeInputIngredients");
  729. let oldCount = ingredientsDiv.children.length;
  730. if(newCount > oldCount){
  731. let newDivs = newCount - oldCount;
  732. for(let i = 0; i < newDivs; i++){
  733. let newNode = ingredientsDiv.children[0].cloneNode(true);
  734. newNode.children[2].children[0].value = "";
  735. ingredientsDiv.appendChild(newNode);
  736. }
  737. for(let i = 0; i < newCount; i++){
  738. ingredientsDiv.children[i].children[0].innerText = `INGREDIENT ${i + 1}`;
  739. }
  740. }else if(newCount < oldCount){
  741. let newDivs = oldCount - newCount;
  742. for(let i = 0; i < newDivs; i++){
  743. ingredientsDiv.removeChild(ingredientsDiv.children[ingredientsDiv.children.length-1]);
  744. }
  745. }
  746. },
  747. submit: function(){
  748. let newRecipe = {
  749. name: document.querySelector("#newRecipeName").value,
  750. price: document.querySelector("#newRecipePrice").value,
  751. ingredients: []
  752. }
  753. let inputs = document.querySelectorAll("#recipeInputIngredients > div");
  754. for(let input of inputs){
  755. newRecipe.ingredients.push({
  756. ingredient: input.children[1].children[0].value,
  757. quantity: input.children[2].children[0].value
  758. });
  759. }
  760. if(!validator.recipe(newRecipe)){
  761. return;
  762. }
  763. let loader = document.getElementById("loaderContainer");
  764. loader.style.display = "flex";
  765. fetch("/recipe/create", {
  766. method: "POST",
  767. headers: {
  768. "Content-Type": "application/json;charset=utf-8"
  769. },
  770. body: JSON.stringify(newRecipe)
  771. })
  772. .then((response) => response.json())
  773. .then((response)=>{
  774. if(typeof(response) === "string"){
  775. banner.createError(response);
  776. }else{
  777. let recipe = new Recipe(
  778. response._id,
  779. response.name,
  780. response.price,
  781. response.ingredients,
  782. merchant,
  783. );
  784. merchant.editRecipes([recipe]);
  785. banner.createNotification("RECIPE CREATED");
  786. }
  787. })
  788. .catch((err)=>{
  789. banner.createError("SOMETHING WENT WRONG. PLEASE REFRESH THE PAGE");
  790. })
  791. .finally(()=>{
  792. loader.style.display = "none";
  793. });
  794. },
  795. }
  796. let transactionDetailsComp = {
  797. transaction: {},
  798. display: function(transaction){
  799. this.transaction = transaction;
  800. let recipeList = document.getElementById("transactionRecipes");
  801. let template = document.getElementById("transactionRecipe").content.children[0];
  802. let totalRecipes = 0;
  803. let totalPrice = 0;
  804. while(recipeList.children.length > 0){
  805. recipeList.removeChild(recipeList.firstChild);
  806. }
  807. for(let i = 0; i < transaction.recipes.length; i++){
  808. let recipe = template.cloneNode(true);
  809. let price = transaction.recipes[i].quantity * transaction.recipes[i].recipe.price;
  810. recipe.children[0].innerText = transaction.recipes[i].recipe.name;
  811. recipe.children[1].innerText = `${transaction.recipes[i].quantity} x $${parseFloat(transaction.recipes[i].recipe.price / 100).toFixed(2)}`;
  812. recipe.children[2].innerText = `$${(price / 100).toFixed(2)}`;
  813. recipeList.appendChild(recipe);
  814. totalRecipes += transaction.recipes[i].quantity;
  815. totalPrice += price;
  816. }
  817. let months = ["January", "Fecbruary", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
  818. let days = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"];
  819. let dateString = `${days[transaction.date.getDay()]}, ${months[transaction.date.getMonth()]} ${transaction.date.getDate()}, ${transaction.date.getFullYear()}`;
  820. document.getElementById("transactionDate").innerText = dateString;
  821. document.getElementById("transactionTime").innerText = transaction.date.toLocaleTimeString();
  822. document.getElementById("totalRecipes").innerText = `${totalRecipes} recipes`;
  823. document.getElementById("totalPrice").innerText = `$${(totalPrice / 100).toFixed(2)}`;
  824. openSidebar(document.getElementById("transactionDetails"));
  825. },
  826. remove: function(){
  827. let loader = document.getElementById("loaderContainer");
  828. loader.style.display = "flex";
  829. fetch(`/transaction/${this.transaction.id}`, {
  830. method: "delete",
  831. headers: {
  832. "Content-Type": "application/json;charset=utf-8"
  833. },
  834. })
  835. .then(response => response.json())
  836. .then((response)=>{
  837. if(typeof(response) === "string"){
  838. banner.createError(response);
  839. }else{
  840. merchant.editTransactions(this.transaction, true);
  841. banner.createNotification("TRANSACTION REMOVED");
  842. }
  843. })
  844. .catch((err)=>{
  845. banner.createError("SOMETHING WENT WRONG. PLEASE REFRESH THE PAGE");
  846. })
  847. .finally(()=>{
  848. loader.style.display = "none";
  849. });
  850. },
  851. }
  852. let newTransactionComp = {
  853. display: function(){
  854. let recipeList = document.getElementById("newTransactionRecipes");
  855. let template = document.getElementById("createTransaction").content.children[0];
  856. while(recipeList.children.length > 0){
  857. recipeList.removeChild(recipeList.firstChild);
  858. }
  859. for(let i = 0; i < merchant.recipes.length; i++){
  860. let recipeDiv = template.cloneNode(true);
  861. recipeDiv.recipe = merchant.recipes[i];
  862. recipeList.appendChild(recipeDiv);
  863. recipeDiv.children[0].innerText = merchant.recipes[i].name;
  864. }
  865. openSidebar(document.getElementById("newTransaction"));
  866. },
  867. submit: function(){
  868. let recipeDivs = document.getElementById("newTransactionRecipes");
  869. let date = document.getElementById("newTransactionDate").valueAsDate;
  870. if(date > new Date()){
  871. banner.createError("CANNOT HAVE A DATE IN THE FUTURE");
  872. return;
  873. }
  874. let newTransaction = {
  875. date: date,
  876. recipes: []
  877. };
  878. for(let i = 0; i < recipeDivs.children.length; i++){
  879. let quantity = recipeDivs.children[i].children[1].value;
  880. if(quantity !== "" && quantity > 0){
  881. newTransaction.recipes.push({
  882. recipe: recipeDivs.children[i].recipe.id,
  883. quantity: quantity
  884. });
  885. }else if(quantity < 0){
  886. banner.createError("CANNOT HAVE NEGATIVE VALUES");
  887. return;
  888. }
  889. }
  890. if(newTransaction.recipes.length > 0){
  891. let loader = document.getElementById("loaderContainer");
  892. loader.style.display = "flex";
  893. fetch("/transaction", {
  894. method: "post",
  895. headers: {
  896. "Content-Type": "application/json;charset=utf-8"
  897. },
  898. body: JSON.stringify(newTransaction)
  899. })
  900. .then(response => response.json())
  901. .then((response)=>{
  902. if(typeof(response) === "string"){
  903. banner.createError(response);
  904. }else{
  905. let transaction = new Transaction(
  906. response._id,
  907. response.date,
  908. response.recipes,
  909. merchant
  910. );
  911. merchant.editTransactions(transaction);
  912. banner.createNotification("NEW TRANSACTION CREATED");
  913. }
  914. })
  915. .catch((err)=>{
  916. banner.createError("SOMETHING WENT WRONG. PLEASE REFRESH THE PAGE");
  917. })
  918. .finally(()=>{
  919. loader.style.display = "none";
  920. });
  921. }
  922. }
  923. }