sidebars.js 43 KB

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