sidebars.js 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017
  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].quantity} ${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 = parseFloat(this.recipe.ingredients[i].quantity);
  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 newIngredient = {
  268. ingredient: {
  269. name: document.getElementById("newIngName").value,
  270. category: document.getElementById("newIngCategory").value,
  271. unit: document.getElementById("unitSelector").value
  272. },
  273. quantity: document.querySelector("#newIngQuantity").value
  274. }
  275. let loader = document.getElementById("loaderContainer");
  276. loader.style.display = "flex";
  277. fetch("/ingredients/create", {
  278. method: "POST",
  279. headers: {
  280. "Content-Type": "application/json;charset=utf-8"
  281. },
  282. body: JSON.stringify(newIngredient)
  283. })
  284. .then((response) => response.json())
  285. .then((response)=>{
  286. if(typeof(response) === "string"){
  287. banner.createError(response);
  288. }else{
  289. merchant.editIngredients([{
  290. ingredient: new Ingredient(
  291. response.ingredient._id,
  292. response.ingredient.name,
  293. response.ingredient.category,
  294. response.ingredient.unit
  295. ),
  296. quantity: response.quantity
  297. }]);
  298. banner.createNotification("INGREDIENT CREATED");
  299. }
  300. })
  301. .catch((err)=>{
  302. banner.createError("SOMETHING WENT WRONG. PLEASE REFRESH THE PAGE");
  303. })
  304. .finally(()=>{
  305. loader.style.display = "none";
  306. });
  307. }
  308. }
  309. let orderDetailsComp = {
  310. display: function(order){
  311. openSidebar(document.querySelector("#orderDetails"));
  312. document.querySelector("#removeOrderBtn").onclick = ()=>{this.remove(order)};
  313. document.querySelector("#orderDetails h1").innerText = order.name;
  314. document.querySelector("#orderDetails h3").innerText = order.date.toLocaleDateString("en-US");
  315. let ingredientList = document.querySelector("#orderIngredients");
  316. while(ingredientList.children.length > 0){
  317. ingredientList.removeChild(ingredientList.firstChild);
  318. }
  319. let template = document.querySelector("#orderIngredient").content.children[0];
  320. let grandTotal = 0;
  321. for(let i = 0; i < order.ingredients.length; i++){
  322. let ingredient = template.cloneNode(true);
  323. let price = (order.ingredients[i].quantity * order.ingredients[i].price) / 100;
  324. grandTotal += price;
  325. ingredient.children[0].innerText = order.ingredients[i].ingredient.name;
  326. ingredient.children[1].innerText = `${order.ingredients[i].quantity} x $${(order.ingredients[i].price / 100).toFixed(2)}`;
  327. ingredient.children[2].innerText = `$${price.toFixed(2)}`;
  328. ingredientList.appendChild(ingredient);
  329. }
  330. document.querySelector("#orderTotalPrice p").innerText = `$${grandTotal.toFixed(2)}`;
  331. },
  332. remove: function(order){
  333. let loader = document.getElementById("loaderContainer");
  334. loader.style.display = "flex";
  335. fetch(`/order/${order.id}`, {
  336. method: "DELETE",
  337. headers: {
  338. "Content-Type": "application/json;charset=utf-8"
  339. }
  340. })
  341. .then((response) => response.json())
  342. .then((response)=>{
  343. if(typeof(response) === "string"){
  344. banner.createError(response);
  345. }else{
  346. merchant.editOrders([order], true);
  347. banner.createNotification("ORDER REMOVED");
  348. }
  349. })
  350. .catch((err)=>{
  351. banner.createError("SOMETHING WENT WRONG. PLEASE REFRESH THE PAGE");
  352. })
  353. .finally(()=>{
  354. loader.style.display = "none";
  355. });
  356. }
  357. }
  358. let addIngredientsComp = {
  359. isPopulated: false,
  360. fakeMerchant: {},
  361. chosenIngredients: [],
  362. display: function(){
  363. let sidebar = document.querySelector("#addIngredients");
  364. if(!this.isPopulated){
  365. let loader = document.getElementById("loaderContainer");
  366. loader.style.display = "flex";
  367. fetch("/ingredients")
  368. .then((response) => response.json())
  369. .then((response)=>{
  370. if(typeof(response) === "string"){
  371. banner.createError(response);
  372. }else{
  373. for(let i = 0; i < merchant.ingredients.length; i++){
  374. for(let j = 0; j < response.length; j++){
  375. if(merchant.ingredients[i].ingredient.id === response[j]._id){
  376. response.splice(j, 1);
  377. break;
  378. }
  379. }
  380. }
  381. for(let i = 0; i < response.length; i++){
  382. response[i] = {ingredient: response[i]}
  383. }
  384. this.fakeMerchant = new Merchant(
  385. {
  386. name: "none",
  387. inventory: response,
  388. recipes: [],
  389. },
  390. []
  391. );
  392. this.populateAddIngredients();
  393. }
  394. })
  395. .catch((err)=>{
  396. banner.createError("UNABLE TO RETRIEVE DATA");
  397. })
  398. .finally(()=>{
  399. loader.style.display = "none";
  400. });
  401. this.isPopulated = true;
  402. }
  403. openSidebar(sidebar);
  404. },
  405. populateAddIngredients: function(){
  406. let addIngredientsDiv = document.getElementById("addIngredientList");
  407. let categoryTemplate = document.getElementById("addIngredientsCategory");
  408. let ingredientTemplate = document.getElementById("addIngredientsIngredient");
  409. let categories = this.fakeMerchant.categorizeIngredients();
  410. while(addIngredientsDiv.children.length > 0){
  411. addIngredientsDiv.removeChild(addIngredientsDiv.firstChild);
  412. }
  413. for(let i = 0; i < categories.length; i++){
  414. let categoryDiv = categoryTemplate.content.children[0].cloneNode(true);
  415. categoryDiv.children[0].children[0].innerText = categories[i].name;
  416. categoryDiv.children[0].children[1].onclick = ()=>{addIngredientsComp.toggleAddIngredient(categoryDiv)};
  417. categoryDiv.children[1].style.display = "none";
  418. categoryDiv.children[0].children[1].children[1].style.display = "none";
  419. addIngredientsDiv.appendChild(categoryDiv);
  420. for(let j = 0; j < categories[i].ingredients.length; j++){
  421. let ingredientDiv = ingredientTemplate.content.children[0].cloneNode(true);
  422. ingredientDiv.children[0].innerText = categories[i].ingredients[j].ingredient.name;
  423. ingredientDiv.children[1].onclick = ()=>{this.addOne(ingredientDiv)};
  424. ingredientDiv.ingredient = categories[i].ingredients[j].ingredient;
  425. categoryDiv.children[1].appendChild(ingredientDiv);
  426. }
  427. }
  428. },
  429. toggleAddIngredient: function(categoryElement){
  430. let button = categoryElement.children[0].children[1];
  431. let ingredientDisplay = categoryElement.children[1];
  432. if(ingredientDisplay.style.display === "none"){
  433. ingredientDisplay.style.display = "flex";
  434. button.children[0].style.display = "none";
  435. button.children[1].style.display = "block";
  436. }else{
  437. ingredientDisplay.style.display = "none";
  438. button.children[0].style.display = "block";
  439. button.children[1].style.display = "none";
  440. }
  441. },
  442. addOne: function(element){
  443. element.parentElement.removeChild(element);
  444. document.getElementById("myIngredients").appendChild(element);
  445. document.getElementById("myIngredientsDiv").style.display = "flex";
  446. for(let i = 0; i < this.fakeMerchant.ingredients.length; i++){
  447. if(this.fakeMerchant.ingredients[i].ingredient === element.ingredient){
  448. this.fakeMerchant.ingredients.splice(i, 1);
  449. this.chosenIngredients.push(element.ingredient);
  450. break;
  451. }
  452. }
  453. let input = document.createElement("input");
  454. input.type = "number";
  455. input.min = "0";
  456. input.step = "0.01";
  457. input.placeholder = element._unit;
  458. element.insertBefore(input, element.children[1]);
  459. element.children[2].innerText = "-";
  460. element.children[2].onclick = ()=>{this.removeOne(element)};
  461. },
  462. removeOne: function(element){
  463. element.parentElement.removeChild(element);
  464. element.removeChild(element.children[1]);
  465. element.children[1].innerText = "+";
  466. element.children[1].onclick = ()=>{this.addOne(element)};
  467. if(document.getElementById("myIngredients").children.length === 0){
  468. document.getElementById("myIngredientsDiv").style.display = "none";
  469. }
  470. for(let i = 0; i < this.chosenIngredients.length; i++){
  471. if(this.chosenIngredients[i] === element.ingredient){
  472. this.chosenIngredients.splice(i, 1);
  473. this.fakeMerchant.ingredients.push({
  474. ingredient: element.ingredient
  475. });
  476. break;
  477. }
  478. }
  479. this.populateAddIngredients();
  480. },
  481. submit: function(){
  482. let ingredients = document.getElementById("myIngredients").children;
  483. let newIngredients = [];
  484. let fetchable = [];
  485. for(let i = 0; i < ingredients.length; i++){
  486. if(ingredients[i].children[1].value === ""){
  487. banner.createError("PLEASE ENTER A QUANTITY FOR EACH INGREDIENT YOU WANT TO ADD TO YOUR INVENTORY");
  488. return;
  489. }
  490. newIngredients.push({
  491. ingredient: ingredients[i].ingredient,
  492. quantity: ingredients[i].children[1].value
  493. });
  494. fetchable.push({
  495. id: ingredients[i].ingredient.id,
  496. quantity: ingredients[i].children[1].value
  497. });
  498. }
  499. let loader = document.getElementById("loaderContainer");
  500. loader.style.display = "flex";
  501. fetch("/merchant/ingredients/add", {
  502. method: "POST",
  503. headers: {
  504. "Content-Type": "application/json;charset=utf-8"
  505. },
  506. body: JSON.stringify(fetchable)
  507. })
  508. .then((response) => response.json())
  509. .then((response)=>{
  510. if(typeof(response) === "string"){
  511. banner.createError(response);
  512. }else{
  513. merchant.editIngredients(newIngredients);
  514. this.isPopulated = false;
  515. banner.createNotification("ALL INGREDIENTS ADDED");
  516. }
  517. })
  518. .catch((err)=>{
  519. banner.createError("SOMETHING WENT WRONG. PLEASE REFRESH THE PAGE");
  520. })
  521. .finally(()=>{
  522. loader.style.display = "none";
  523. });
  524. }
  525. }
  526. let ingredientDetailsComp = {
  527. ingredient: {},
  528. display: function(ingredient){
  529. this.ingredient = ingredient;
  530. sidebar = document.querySelector("#ingredientDetails");
  531. document.querySelector("#ingredientDetails p").innerText = ingredient.ingredient.category;
  532. document.querySelector("#ingredientDetails h1").innerText = ingredient.ingredient.name;
  533. let ingredientStock = document.getElementById("ingredientStock");
  534. ingredientStock.innerText = `${ingredient.quantity} ${ingredient.ingredient.unit}`;
  535. ingredientStock.style.display = "block";
  536. let ingredientInput = document.getElementById("ingredientInput");
  537. ingredientInput.placeholder = `${ingredient.quantity} ${ingredient.ingredient.unit}`;
  538. ingredientInput.style.display = "none";
  539. let quantities = [];
  540. let now = new Date();
  541. for(let i = 1; i < 31; i++){
  542. let endDay = new Date(now.getFullYear(), now.getMonth(), now.getDate() - i)
  543. let startDay = new Date(now.getFullYear(), now.getMonth(), now.getDate() - i - 1);
  544. let indices = merchant.transactionIndices(startDay, endDay);
  545. if(indices === false){
  546. quantities.push(0);
  547. }else{
  548. quantities.push(merchant.singleIngredientSold(indices, ingredient));
  549. }
  550. }
  551. let sum = 0;
  552. for(let quantity of quantities){
  553. sum += quantity;
  554. }
  555. document.querySelector("#dailyUse").innerText = `${(sum/quantities.length).toFixed(2)} ${ingredient.ingredient.unit}`;
  556. let ul = document.querySelector("#ingredientRecipeList");
  557. let recipes = merchant.getRecipesForIngredient(ingredient.ingredient);
  558. while(ul.children.length > 0){
  559. ul.removeChild(ul.firstChild);
  560. }
  561. for(let i = 0; i < recipes.length; i++){
  562. let li = document.createElement("li");
  563. li.innerText = recipes[i].name;
  564. li.onclick = ()=>{
  565. changeStrand("recipeBookStrand");
  566. recipeDetailsComp.display(recipes[i]);
  567. }
  568. ul.appendChild(li);
  569. }
  570. openSidebar(sidebar);
  571. },
  572. remove: function(){
  573. for(let i = 0; i < merchant.recipes.length; i++){
  574. for(let j = 0; j < merchant.recipes[i].ingredients.length; j++){
  575. if(this.ingredient.ingredient === merchant.recipes[i].ingredients[j].ingredient){
  576. banner.createError("MUST REMOVE INGREDIENT FROM ALL RECIPES BEFORE REMOVING FROM INVENTORY");
  577. return;
  578. }
  579. }
  580. }
  581. let loader = document.getElementById("loaderContainer");
  582. loader.style.display = "flex";
  583. fetch(`/merchant/ingredients/remove/${this.ingredient.ingredient.id}`, {
  584. method: "DELETE",
  585. })
  586. .then((response) => response.json())
  587. .then((response)=>{
  588. if(typeof(response) === "string"){
  589. banner.createError(response);
  590. }else{
  591. banner.createNotification("INGREDIENT REMOVED");
  592. merchant.editIngredients([this.ingredient], true);
  593. }
  594. })
  595. .catch((err)=>{})
  596. .finally(()=>{
  597. loader.style.display = "none";
  598. });
  599. },
  600. edit: function(){
  601. document.querySelector("#ingredientStock").style.display = "none";
  602. document.querySelector("#ingredientInput").style.display = "block";
  603. document.querySelector("#editSubmitButton").style.display = "block";
  604. },
  605. editSubmit: function(){
  606. this.ingredient.quantity = Number(document.getElementById("ingredientInput").value);
  607. let data = [{
  608. id: this.ingredient.ingredient.id,
  609. quantity: this.ingredient.quantity
  610. }];
  611. let loader = document.getElementById("loaderContainer");
  612. loader.style.display = "flex";
  613. if(validator.ingredientQuantity(data[0].quantity)){
  614. fetch("/merchant/ingredients/update", {
  615. method: "PUT",
  616. headers: {
  617. "Content-Type": "application/json;charset=utf-8"
  618. },
  619. body: JSON.stringify(data)
  620. })
  621. .then((response) => response.json())
  622. .then((response)=>{
  623. if(typeof(response) === "string"){
  624. banner.createError(response);
  625. }else{
  626. merchant.editIngredients([this.ingredient]);
  627. banner.createNotification("INGREDIENT UPDATED");
  628. }
  629. })
  630. .catch((err)=>{
  631. banner.createError("SOMETHING WENT WRONG. PLEASE REFRESH THE PAGE");
  632. })
  633. .finally(()=>{
  634. loader.style.display = "none";
  635. });
  636. }
  637. }
  638. }
  639. let newRecipeComp = {
  640. display: function(){
  641. let ingredientsSelect = document.querySelector("#recipeInputIngredients select");
  642. let categories = merchant.categorizeIngredients();
  643. while(ingredientsSelect.children.length > 0){
  644. ingredientsSelect.removeChild(ingredientsSelect.firstChild);
  645. }
  646. for(let category of categories){
  647. let optgroup = document.createElement("optgroup");
  648. optgroup.label = category.name;
  649. ingredientsSelect.appendChild(optgroup);
  650. for(let ingredient of category.ingredients){
  651. let option = document.createElement("option");
  652. option.value = ingredient.ingredient.id;
  653. option.innerText = ingredient.ingredient.name;
  654. optgroup.appendChild(option);
  655. }
  656. }
  657. openSidebar(document.querySelector("#addRecipe"));
  658. },
  659. //Updates the number of ingredient inputs displayed for new recipes
  660. changeRecipeCount: function(){
  661. let newCount = document.querySelector("#ingredientCount").value;
  662. let ingredientsDiv = document.querySelector("#recipeInputIngredients");
  663. let oldCount = ingredientsDiv.children.length;
  664. if(newCount > oldCount){
  665. let newDivs = newCount - oldCount;
  666. for(let i = 0; i < newDivs; i++){
  667. let newNode = ingredientsDiv.children[0].cloneNode(true);
  668. newNode.children[2].children[0].value = "";
  669. ingredientsDiv.appendChild(newNode);
  670. }
  671. for(let i = 0; i < newCount; i++){
  672. ingredientsDiv.children[i].children[0].innerText = `INGREDIENT ${i + 1}`;
  673. }
  674. }else if(newCount < oldCount){
  675. let newDivs = oldCount - newCount;
  676. for(let i = 0; i < newDivs; i++){
  677. ingredientsDiv.removeChild(ingredientsDiv.children[ingredientsDiv.children.length-1]);
  678. }
  679. }
  680. },
  681. submit: function(){
  682. let newRecipe = {
  683. name: document.querySelector("#newRecipeName").value,
  684. price: document.querySelector("#newRecipePrice").value,
  685. ingredients: []
  686. }
  687. let inputs = document.querySelectorAll("#recipeInputIngredients > div");
  688. for(let input of inputs){
  689. newRecipe.ingredients.push({
  690. ingredient: input.children[1].children[0].value,
  691. quantity: input.children[2].children[0].value
  692. });
  693. }
  694. if(!validator.recipe(newRecipe)){
  695. return;
  696. }
  697. let loader = document.getElementById("loaderContainer");
  698. loader.style.display = "flex";
  699. fetch("/recipe/create", {
  700. method: "POST",
  701. headers: {
  702. "Content-Type": "application/json;charset=utf-8"
  703. },
  704. body: JSON.stringify(newRecipe)
  705. })
  706. .then((response) => response.json())
  707. .then((response)=>{
  708. if(typeof(response) === "string"){
  709. banner.createError(response);
  710. }else{
  711. let recipe = new Recipe(
  712. response._id,
  713. response.name,
  714. response.price,
  715. response.ingredients,
  716. merchant,
  717. );
  718. merchant.editRecipes([recipe]);
  719. banner.createNotification("RECIPE CREATED");
  720. }
  721. })
  722. .catch((err)=>{
  723. banner.createError("SOMETHING WENT WRONG. PLEASE REFRESH THE PAGE");
  724. })
  725. .finally(()=>{
  726. loader.style.display = "none";
  727. });
  728. },
  729. }
  730. let transactionDetailsComp = {
  731. transaction: {},
  732. display: function(transaction){
  733. this.transaction = transaction;
  734. let recipeList = document.getElementById("transactionRecipes");
  735. let template = document.getElementById("transactionRecipe").content.children[0];
  736. let totalRecipes = 0;
  737. let totalPrice = 0;
  738. while(recipeList.children.length > 0){
  739. recipeList.removeChild(recipeList.firstChild);
  740. }
  741. for(let i = 0; i < transaction.recipes.length; i++){
  742. let recipe = template.cloneNode(true);
  743. let price = transaction.recipes[i].quantity * transaction.recipes[i].recipe.price;
  744. recipe.children[0].innerText = transaction.recipes[i].recipe.name;
  745. recipe.children[1].innerText = `${transaction.recipes[i].quantity} x $${parseFloat(transaction.recipes[i].recipe.price / 100).toFixed(2)}`;
  746. recipe.children[2].innerText = `$${(price / 100).toFixed(2)}`;
  747. recipeList.appendChild(recipe);
  748. totalRecipes += transaction.recipes[i].quantity;
  749. totalPrice += price;
  750. }
  751. let months = ["January", "Fecbruary", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
  752. let days = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"];
  753. let dateString = `${days[transaction.date.getDay()]}, ${months[transaction.date.getMonth()]} ${transaction.date.getDate()}, ${transaction.date.getFullYear()}`;
  754. document.getElementById("transactionDate").innerText = dateString;
  755. document.getElementById("transactionTime").innerText = transaction.date.toLocaleTimeString();
  756. document.getElementById("totalRecipes").innerText = `${totalRecipes} recipes`;
  757. document.getElementById("totalPrice").innerText = `$${(totalPrice / 100).toFixed(2)}`;
  758. openSidebar(document.getElementById("transactionDetails"));
  759. },
  760. remove: function(){
  761. let loader = document.getElementById("loaderContainer");
  762. loader.style.display = "flex";
  763. fetch(`/transaction/${this.transaction.id}`, {
  764. method: "delete",
  765. headers: {
  766. "Content-Type": "application/json;charset=utf-8"
  767. },
  768. })
  769. .then(response => response.json())
  770. .then((response)=>{
  771. if(typeof(response) === "string"){
  772. banner.createError(response);
  773. }else{
  774. merchant.editTransactions(this.transaction, true);
  775. banner.createNotification("TRANSACTION REMOVED");
  776. }
  777. })
  778. .catch((err)=>{
  779. banner.createError("SOMETHING WENT WRONG. PLEASE REFRESH THE PAGE");
  780. })
  781. .finally(()=>{
  782. loader.style.display = "none";
  783. });
  784. },
  785. }
  786. let newTransactionComp = {
  787. display: function(){
  788. let recipeList = document.getElementById("newTransactionRecipes");
  789. let template = document.getElementById("createTransaction").content.children[0];
  790. while(recipeList.children.length > 0){
  791. recipeList.removeChild(recipeList.firstChild);
  792. }
  793. for(let i = 0; i < merchant.recipes.length; i++){
  794. let recipeDiv = template.cloneNode(true);
  795. recipeDiv.recipe = merchant.recipes[i];
  796. recipeList.appendChild(recipeDiv);
  797. recipeDiv.children[0].innerText = merchant.recipes[i].name;
  798. }
  799. openSidebar(document.getElementById("newTransaction"));
  800. },
  801. submit: function(){
  802. let recipeDivs = document.getElementById("newTransactionRecipes");
  803. let date = document.getElementById("newTransactionDate").valueAsDate;
  804. if(date > new Date()){
  805. banner.createError("CANNOT HAVE A DATE IN THE FUTURE");
  806. return;
  807. }
  808. let newTransaction = {
  809. date: date,
  810. recipes: []
  811. };
  812. for(let i = 0; i < recipeDivs.children.length; i++){
  813. let quantity = recipeDivs.children[i].children[1].value;
  814. if(quantity !== "" && quantity > 0){
  815. newTransaction.recipes.push({
  816. recipe: recipeDivs.children[i].recipe.id,
  817. quantity: quantity
  818. });
  819. }else if(quantity < 0){
  820. banner.createError("CANNOT HAVE NEGATIVE VALUES");
  821. return;
  822. }
  823. }
  824. if(newTransaction.recipes.length > 0){
  825. let loader = document.getElementById("loaderContainer");
  826. loader.style.display = "flex";
  827. fetch("/transaction", {
  828. method: "post",
  829. headers: {
  830. "Content-Type": "application/json;charset=utf-8"
  831. },
  832. body: JSON.stringify(newTransaction)
  833. })
  834. .then(response => response.json())
  835. .then((response)=>{
  836. if(typeof(response) === "string"){
  837. banner.createError(response);
  838. }else{
  839. let transaction = new Transaction(
  840. response._id,
  841. response.date,
  842. response.recipes,
  843. merchant
  844. );
  845. merchant.editTransactions(transaction);
  846. banner.createNotification("NEW TRANSACTION CREATED");
  847. }
  848. })
  849. .catch((err)=>{
  850. banner.createError("SOMETHING WENT WRONG. PLEASE REFRESH THE PAGE");
  851. })
  852. .finally(()=>{
  853. loader.style.display = "none";
  854. });
  855. }
  856. }
  857. }