sidebars.js 43 KB

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