bundle.js 98 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668
  1. (function(){function r(e,n,t){function o(i,f){if(!n[i]){if(!e[i]){var c="function"==typeof require&&require;if(!f&&c)return c(i,!0);if(u)return u(i,!0);var a=new Error("Cannot find module '"+i+"'");throw a.code="MODULE_NOT_FOUND",a}var p=n[i]={exports:{}};e[i][0].call(p.exports,function(r){var n=e[i][1][r];return o(n||r)},p,p.exports,r,e,n,t)}return n[i].exports}for(var u="function"==typeof require&&require,i=0;i<t.length;i++)o(t[i]);return o}return r})()({1:[function(require,module,exports){
  2. module.exports = {
  3. isPopulated: false,
  4. fakeMerchant: {},
  5. chosenIngredients: [],
  6. display: function(){
  7. let sidebar = document.querySelector("#addIngredients");
  8. if(!this.isPopulated){
  9. let loader = document.getElementById("loaderContainer");
  10. loader.style.display = "flex";
  11. fetch("/ingredients")
  12. .then((response) => response.json())
  13. .then((response)=>{
  14. if(typeof(response) === "string"){
  15. banner.createError(response);
  16. }else{
  17. for(let i = 0; i < merchant.ingredients.length; i++){
  18. for(let j = 0; j < response.length; j++){
  19. if(merchant.ingredients[i].ingredient.id === response[j]._id){
  20. response.splice(j, 1);
  21. break;
  22. }
  23. }
  24. }
  25. for(let i = 0; i < response.length; i++){
  26. response[i] = {ingredient: response[i]}
  27. }
  28. this.fakeMerchant = new Merchant({
  29. name: "none",
  30. inventory: response,
  31. recipes: [],
  32. },
  33. []
  34. );
  35. this.populateAddIngredients();
  36. }
  37. })
  38. .catch((err)=>{
  39. banner.createError("UNABLE TO RETRIEVE DATA");
  40. })
  41. .finally(()=>{
  42. loader.style.display = "none";
  43. });
  44. this.isPopulated = true;
  45. }
  46. openSidebar(sidebar);
  47. },
  48. populateAddIngredients: function(){
  49. let addIngredientsDiv = document.getElementById("addIngredientList");
  50. let categoryTemplate = document.getElementById("addIngredientsCategory");
  51. let ingredientTemplate = document.getElementById("addIngredientsIngredient");
  52. let categories = this.fakeMerchant.categorizeIngredients();
  53. while(addIngredientsDiv.children.length > 0){
  54. addIngredientsDiv.removeChild(addIngredientsDiv.firstChild);
  55. }
  56. for(let i = 0; i < categories.length; i++){
  57. let categoryDiv = categoryTemplate.content.children[0].cloneNode(true);
  58. categoryDiv.children[0].children[0].innerText = categories[i].name;
  59. categoryDiv.children[0].children[1].onclick = ()=>{addIngredientsComp.toggleAddIngredient(categoryDiv)};
  60. categoryDiv.children[1].style.display = "none";
  61. categoryDiv.children[0].children[1].children[1].style.display = "none";
  62. addIngredientsDiv.appendChild(categoryDiv);
  63. for(let j = 0; j < categories[i].ingredients.length; j++){
  64. let ingredientDiv = ingredientTemplate.content.children[0].cloneNode(true);
  65. ingredientDiv.children[0].innerText = categories[i].ingredients[j].ingredient.name;
  66. ingredientDiv.children[2].onclick = ()=>{this.addOne(ingredientDiv)};
  67. ingredientDiv.ingredient = categories[i].ingredients[j].ingredient;
  68. categoryDiv.children[1].appendChild(ingredientDiv);
  69. }
  70. }
  71. let myIngredients = document.getElementById("myIngredients");
  72. while(myIngredients.children.length > 0){
  73. myIngredients.removeChild(myIngredients.firstChild);
  74. }
  75. },
  76. toggleAddIngredient: function(categoryElement){
  77. let button = categoryElement.children[0].children[1];
  78. let ingredientDisplay = categoryElement.children[1];
  79. if(ingredientDisplay.style.display === "none"){
  80. ingredientDisplay.style.display = "flex";
  81. button.children[0].style.display = "none";
  82. button.children[1].style.display = "block";
  83. }else{
  84. ingredientDisplay.style.display = "none";
  85. button.children[0].style.display = "block";
  86. button.children[1].style.display = "none";
  87. }
  88. },
  89. addOne: function(element){
  90. element.parentElement.removeChild(element);
  91. document.getElementById("myIngredients").appendChild(element);
  92. document.getElementById("myIngredientsDiv").style.display = "flex";
  93. for(let i = 0; i < this.fakeMerchant.ingredients.length; i++){
  94. if(this.fakeMerchant.ingredients[i].ingredient === element.ingredient){
  95. this.fakeMerchant.ingredients.splice(i, 1);
  96. this.chosenIngredients.push(element.ingredient);
  97. break;
  98. }
  99. }
  100. let input = document.createElement("input");
  101. input.type = "number";
  102. input.min = "0";
  103. input.step = "0.01";
  104. input.placeholder = "QUANTITY";
  105. element.insertBefore(input, element.children[1]);
  106. let select = element.children[2];
  107. select.style.display = "block";
  108. let units = merchant.units[element.ingredient.unitType];
  109. for(let i = 0; i < units.length; i++){
  110. let option = document.createElement("option");
  111. option.innerText = units[i].toUpperCase();
  112. option.type = element.ingredient.unitType;
  113. option.value = units[i];
  114. select.appendChild(option);
  115. }
  116. element.children[3].innerText = "-";
  117. element.children[3].onclick = ()=>{this.removeOne(element)};
  118. },
  119. removeOne: function(element){
  120. element.parentElement.removeChild(element);
  121. element.removeChild(element.children[1]);
  122. let select = element.children[1];
  123. while(select.children.length > 0){
  124. select.removeChild(select.firstChild);
  125. }
  126. select.style.display = "none";
  127. element.children[2].innerText = "+";
  128. element.children[2].onclick = ()=>{this.addOne(element)};
  129. if(document.getElementById("myIngredients").children.length === 0){
  130. document.getElementById("myIngredientsDiv").style.display = "none";
  131. }
  132. for(let i = 0; i < this.chosenIngredients.length; i++){
  133. if(this.chosenIngredients[i] === element.ingredient){
  134. this.chosenIngredients.splice(i, 1);
  135. this.fakeMerchant.ingredients.push({
  136. ingredient: element.ingredient
  137. });
  138. break;
  139. }
  140. }
  141. this.populateAddIngredients();
  142. },
  143. submit: function(){
  144. let ingredients = document.getElementById("myIngredients").children;
  145. let newIngredients = [];
  146. let fetchable = [];
  147. for(let i = 0; i < ingredients.length; i++){
  148. let quantity = ingredients[i].children[1].value;
  149. let unit = ingredients[i].children[2].value;
  150. if(quantity === ""){
  151. banner.createError("PLEASE ENTER A QUANTITY FOR EACH INGREDIENT YOU WANT TO ADD TO YOUR INVENTORY");
  152. return;
  153. }
  154. quantity = convertToMain(unit, quantity);
  155. let newIngredient = {
  156. ingredient: ingredients[i].ingredient,
  157. quantity: quantity
  158. }
  159. newIngredient.ingredient.unit = unit;
  160. newIngredients.push(newIngredient);
  161. fetchable.push({
  162. id: ingredients[i].ingredient.id,
  163. quantity: quantity,
  164. defaultUnit: unit
  165. });
  166. }
  167. let loader = document.getElementById("loaderContainer");
  168. loader.style.display = "flex";
  169. fetch("/merchant/ingredients/add", {
  170. method: "POST",
  171. headers: {
  172. "Content-Type": "application/json;charset=utf-8"
  173. },
  174. body: JSON.stringify(fetchable)
  175. })
  176. .then((response) => response.json())
  177. .then((response)=>{
  178. if(typeof(response) === "string"){
  179. banner.createError(response);
  180. }else{
  181. merchant.editIngredients(newIngredients);
  182. this.isPopulated = false;
  183. banner.createNotification("ALL INGREDIENTS ADDED");
  184. }
  185. })
  186. .catch((err)=>{
  187. banner.createError("SOMETHING WENT WRONG. PLEASE REFRESH THE PAGE");
  188. })
  189. .finally(()=>{
  190. loader.style.display = "none";
  191. });
  192. }
  193. }
  194. },{}],2:[function(require,module,exports){
  195. const homeStrandObj = require("./home.js");
  196. const ingredientsStrandObj = require("./ingredients.js");
  197. const ordersStrandObj = require("./orders.js");
  198. const recipeBookStrandObj = require("./recipeBook.js");
  199. const transactions = require("./transactions.js");
  200. const addIngredientsComp = require("./addIngredients.js");
  201. const ingredientDetailsComp = require("./ingredientDetails.js");
  202. const newIngredientComp = require("./newIngredient.js");
  203. const newOrderComp = require("./newOrder.js");
  204. const newRecipeComp = require("./newRecipe.js");
  205. const newTransactionComp = require("./newTransaction.js");
  206. const orderDetailsComp = require("./orderDetails.js");
  207. const recipeDetailsComp = require("./recipeDetails.js");
  208. const transactionDetailsComp = require("./transactionDetails.js");
  209. class Ingredient{
  210. constructor(id, name, category, unitType, unit, parent){
  211. this.id = id;
  212. this.name = name;
  213. this.category = category;
  214. this.unitType = unitType;
  215. this.unit = unit;
  216. this.parent = parent;
  217. }
  218. convert(quantity){
  219. if(this.unitType === "mass"){
  220. switch(this.unit){
  221. case "g": break;
  222. case "kg": quantity /= 1000; break;
  223. case "oz": quantity /= 28.3495; break;
  224. case "lb": quantity /= 453.5924; break;
  225. }
  226. }else if(this.unitType === "volume"){
  227. switch(this.unit){
  228. case "ml": quantity *= 1000; break;
  229. case "l": break;
  230. case "tsp": quantity *= 202.8842; break;
  231. case "tbsp": quantity *= 67.6278; break;
  232. case "ozfl": quantity *= 33.8141; break;
  233. case "cup": quantity *= 4.1667; break;
  234. case "pt": quantity *= 2.1134; break;
  235. case "qt": quantity *= 1.0567; break;
  236. case "gal": quantity /= 3.7854; break;
  237. }
  238. }else if(this.unitType === "length"){
  239. switch(this.unit){
  240. case "mm": quantity *= 1000; break;
  241. case "cm": quantity *= 100; break;
  242. case "m": break;
  243. case "in": quantity *= 39.3701; break;
  244. case "ft": quantity *= 3.2808; break;
  245. }
  246. }
  247. return quantity;
  248. }
  249. }
  250. class Recipe{
  251. constructor(id, name, price, ingredients, parent){
  252. this.id = id;
  253. this.name = name;
  254. this.price = price;
  255. this.parent = parent;
  256. this.ingredients = [];
  257. for(let i = 0; i < ingredients.length; i++){
  258. for(let j = 0; j < parent.ingredients.length; j++){
  259. if(ingredients[i].ingredient === parent.ingredients[j].ingredient.id){
  260. this.ingredients.push({
  261. ingredient: parent.ingredients[j].ingredient,
  262. quantity: ingredients[i].quantity
  263. });
  264. break;
  265. }
  266. }
  267. }
  268. }
  269. }
  270. class Transaction{
  271. constructor(id, date, recipes, parent){
  272. this.id = id;
  273. this.parent = parent;
  274. this.date = new Date(date);
  275. this.recipes = [];
  276. for(let i = 0; i < recipes.length; i++){
  277. for(let j = 0; j < parent.recipes.length; j++){
  278. if(recipes[i].recipe === parent.recipes[j].id){
  279. this.recipes.push({
  280. recipe: parent.recipes[j],
  281. quantity: recipes[i].quantity
  282. });
  283. break;
  284. }
  285. }
  286. }
  287. }
  288. }
  289. class Order{
  290. constructor(id, name, date, ingredients, parent){
  291. this.id = id;
  292. this.name = name;
  293. this.date = new Date(date);
  294. this.ingredients = [];
  295. this.parent = parent;
  296. for(let i = 0; i < ingredients.length; i++){
  297. for(let j = 0; j < parent.ingredients.length; j++){
  298. if(ingredients[i].ingredient === parent.ingredients[j].ingredient.id){
  299. this.ingredients.push({
  300. ingredient: parent.ingredients[j].ingredient,
  301. quantity: ingredients[i].quantity,
  302. price: ingredients[i].price
  303. });
  304. }
  305. }
  306. }
  307. }
  308. convertPrice(unitType, unit, price){
  309. if(unitType === "mass"){
  310. switch(unit){
  311. case "g": break;
  312. case "kg": price *= 1000; break;
  313. case "oz": price *= 28.3495; break;
  314. case "lb": price *= 453.5924; break;
  315. }
  316. }else if(unitType === "volume"){
  317. switch(unit){
  318. case "ml": price /= 1000; break;
  319. case "l": break;
  320. case "tsp": price /= 202.8842; break;
  321. case "tbsp": price /= 67.6278; break;
  322. case "ozfl": price /= 33.8141; break;
  323. case "cup": price /= 4.1667; break;
  324. case "pt": price /= 2.1134; break;
  325. case "qt": price /= 1.0567; break;
  326. case "gal": price *= 3.7854; break;
  327. }
  328. }else if(unitType === "length"){
  329. switch(unit){
  330. case "mm": price /= 1000; break;
  331. case "cm": price /= 100; break;
  332. case "m": break;
  333. case "in": price /= 39.3701; break;
  334. case "ft": price /= 3.2808; break;
  335. }
  336. }
  337. return price;
  338. }
  339. }
  340. class Merchant{
  341. constructor(oldMerchant, transactions){
  342. this.name = oldMerchant.name;
  343. this.pos = oldMerchant.pos;
  344. this.ingredients = [];
  345. this.recipes = [];
  346. this.transactions = [];
  347. this.orders = [];
  348. this.units = {
  349. mass: ["g", "kg", "oz", "lb"],
  350. volume: ["ml", "l", "tsp", "tbsp", "ozfl", "cup", "pt", "qt", "gal"],
  351. length: ["mm", "cm", "m", "in", "foot"]
  352. }
  353. for(let i = 0; i < oldMerchant.inventory.length; i++){
  354. this.ingredients.push({
  355. ingredient: new Ingredient(
  356. oldMerchant.inventory[i].ingredient._id,
  357. oldMerchant.inventory[i].ingredient.name,
  358. oldMerchant.inventory[i].ingredient.category,
  359. oldMerchant.inventory[i].ingredient.unitType,
  360. oldMerchant.inventory[i].defaultUnit,
  361. this
  362. ),
  363. quantity: oldMerchant.inventory[i].quantity
  364. });
  365. }
  366. for(let i = 0; i < oldMerchant.recipes.length; i++){
  367. this.recipes.push(new Recipe(
  368. oldMerchant.recipes[i]._id,
  369. oldMerchant.recipes[i].name,
  370. oldMerchant.recipes[i].price,
  371. oldMerchant.recipes[i].ingredients,
  372. this
  373. ));
  374. }
  375. for(let i = 0; i < transactions.length; i++){
  376. this.transactions.push(new Transaction(
  377. transactions[i]._id,
  378. transactions[i].date,
  379. transactions[i].recipes,
  380. this
  381. ));
  382. }
  383. }
  384. /*
  385. Updates all specified item in the merchant's inventory and updates the page
  386. If ingredient doesn't exist, add it
  387. ingredients = {
  388. ingredient: Ingredient object,
  389. quantity: new quantity,
  390. defaultUnit: the default unit to be displayed
  391. }
  392. remove = set true if removing
  393. isOrder = set true if this is coming from an order
  394. */
  395. editIngredients(ingredients, remove = false, isOrder = false){
  396. for(let i = 0; i < ingredients.length; i++){
  397. let isNew = true;
  398. for(let j = 0; j < merchant.ingredients.length; j++){
  399. if(merchant.ingredients[j].ingredient === ingredients[i].ingredient){
  400. if(remove){
  401. merchant.ingredients.splice(j, 1);
  402. }else if(!remove && isOrder){
  403. merchant.ingredients[j].quantity += ingredients[i].quantity;
  404. }else{
  405. merchant.ingredients[j].quantity = ingredients[i].quantity;
  406. }
  407. isNew = false;
  408. break;
  409. }
  410. }
  411. if(isNew){
  412. merchant.ingredients.push({
  413. ingredient: ingredients[i].ingredient,
  414. quantity: parseFloat(ingredients[i].quantity),
  415. defaultUnit: ingredients[i].defaultUnit
  416. });
  417. }
  418. }
  419. homeStrandObj.drawInventoryCheckCard();
  420. ingredientsStrandObj.populateByProperty("category");
  421. addIngredientsComp.isPopulated = false;
  422. closeSidebar();
  423. }
  424. /*
  425. Updates a recipe in the merchants list of recipes
  426. Can create, edit or remove
  427. recipe = [Recipe object]
  428. remove = will remove recipe when true
  429. */
  430. editRecipes(recipes, remove = false){
  431. let isNew = true;
  432. for(let i = 0; i < recipes.length; i++){
  433. for(let j = 0; j < this.recipes.length; j++){
  434. if(recipes[i] === this.recipes[j]){
  435. if(remove){
  436. this.recipes.splice(j, 1);
  437. }else{
  438. this.recipes[j] = recipes[i];
  439. }
  440. isNew = false;
  441. break;
  442. }
  443. }
  444. if(isNew){
  445. merchant.recipes.push(recipes[i]);
  446. }
  447. }
  448. transactionsStrandObj.isPopulated = false;
  449. recipeBookStrandObj.populateRecipes();
  450. closeSidebar();
  451. }
  452. /*
  453. Updates a list of orders in the merchants list of orders
  454. Create/edit/remove
  455. orders = [Order object]
  456. remove = will remove order when true
  457. */
  458. editOrders(orders, remove = false){
  459. for(let i = 0; i < orders.length; i++){
  460. let isNew = true;
  461. for(let j = 0; j < this.orders.length; j++){
  462. if(orders[i] === this.orders[j]){
  463. if(remove){
  464. this.orders.splice(j, 1);
  465. }else{
  466. this.orders[j] = orders[i];
  467. }
  468. isNew = false;
  469. break;
  470. }
  471. }
  472. if(isNew){
  473. this.orders.push(orders[i]);
  474. }
  475. }
  476. ordersStrandObj.populate();
  477. closeSidebar();
  478. }
  479. editTransactions(transaction, remove = false){
  480. let isNew = true;
  481. for(let i = 0; i < this.transactions.length; i++){
  482. if(this.transactions[i] === transaction){
  483. if(remove){
  484. this.transactions.splice(i, 1);
  485. }
  486. isNew = false;
  487. break;
  488. }
  489. }
  490. if(isNew){
  491. this.transactions.push(transaction);
  492. this.transactions.sort((a, b) => a.date > b.date ? 1 : -1);
  493. }
  494. transactionsStrandObj.isPopulated = false;
  495. transactionsStrandObj.display();
  496. closeSidebar();
  497. }
  498. /*
  499. Gets the indices of two dates from transactions
  500. Inputs
  501. from: starting date
  502. to: ending date (default to now)
  503. Output
  504. Array containing starting index and ending index
  505. Note: Will return false if it cannot find both necessary dates
  506. */
  507. transactionIndices(from, to = new Date()){
  508. let indices = [];
  509. for(let i = 0; i < this.transactions.length; i++){
  510. if(this.transactions[i].date > from){
  511. indices.push(i);
  512. break;
  513. }
  514. }
  515. for(let i = this.transactions.length - 1; i >=0; i--){
  516. if(this.transactions[i].date < to){
  517. indices.push(i);
  518. break;
  519. }
  520. }
  521. if(indices.length < 2){
  522. return false;
  523. }
  524. return indices;
  525. }
  526. revenue(indices){
  527. let total = 0;
  528. for(let i = indices[0]; i <= indices[1]; i++){
  529. for(let j = 0; j < this.transactions[i].recipes.length; j++){
  530. for(let k = 0; k < this.recipes.length; k++){
  531. if(this.transactions[i].recipes[j].recipe === this.recipes[k]){
  532. total += this.transactions[i].recipes[j].quantity * this.recipes[k].price;
  533. }
  534. }
  535. }
  536. }
  537. return total / 100;
  538. }
  539. /*
  540. Gets the quantity of each ingredient sold between two dates (dateRange)
  541. Inputs
  542. dateRange: list containing a start date and an end date
  543. Return:
  544. [{
  545. ingredient: Ingredient object,
  546. quantity: quantity of ingredient sold
  547. }]
  548. */
  549. ingredientsSold(dateRange){
  550. if(!dateRange){
  551. return false;
  552. }
  553. let recipes = this.recipesSold(dateRange);
  554. let ingredientList = [];
  555. for(let i = 0; i < recipes.length; i++){
  556. for(let j = 0; j < recipes[i].recipe.ingredients.length; j++){
  557. let exists = false;
  558. for(let k = 0; k < ingredientList.length; k++){
  559. if(ingredientList[k].ingredient === recipes[i].recipe.ingredients[j].ingredient){
  560. exists = true;
  561. ingredientList[k].quantity += recipes[i].quantity * recipes[i].recipe.ingredients[j].quantity;
  562. break;
  563. }
  564. }
  565. if(!exists){
  566. ingredientList.push({
  567. ingredient: recipes[i].recipe.ingredients[j].ingredient,
  568. quantity: recipes[i].quantity * recipes[i].recipe.ingredients[j].quantity
  569. });
  570. }
  571. }
  572. }
  573. return ingredientList;
  574. }
  575. singleIngredientSold(dateRange, ingredient){
  576. let total = 0;
  577. for(let i = dateRange[0]; i < dateRange[1]; i++){
  578. for(let j = 0; j < this.transactions[i].recipes.length; j++){
  579. for(let k = 0; k < this.transactions[i].recipes[j].recipe.ingredients.length; k++){
  580. if(this.transactions[i].recipes[j].recipe.ingredients[k].ingredient === ingredient.ingredient){
  581. total += this.transactions[i].recipes[j].recipe.ingredients[k].quantity;
  582. break;
  583. }
  584. }
  585. }
  586. }
  587. return total;
  588. }
  589. /*
  590. Gets the number of recipes sold between two dates (dateRange)
  591. Inputs:
  592. dateRange: array containing a start date and an end date
  593. Return:
  594. [{
  595. recipe: a recipe object
  596. quantity: quantity of the recipe sold
  597. }]
  598. */
  599. recipesSold(dateRange){
  600. let recipeList = [];
  601. for(let i = dateRange[0]; i <= dateRange[1]; i++){
  602. for(let j = 0; j < this.transactions[i].recipes.length; j++){
  603. let exists = false;
  604. for(let k = 0; k < recipeList.length; k++){
  605. if(recipeList[k].recipe === this.transactions[i].recipes[j].recipe){
  606. exists = true;
  607. recipeList[k].quantity += this.transactions[i].recipes[j].quantity;
  608. break;
  609. }
  610. }
  611. if(!exists){
  612. recipeList.push({
  613. recipe: this.transactions[i].recipes[j].recipe,
  614. quantity: this.transactions[i].recipes[j].quantity
  615. });
  616. }
  617. }
  618. }
  619. return recipeList;
  620. }
  621. /*
  622. Create revenue data for graphing
  623. Input:
  624. dateRange: [start index, end index] (this.transactionIndices)
  625. Return:
  626. [total revenue for each day]
  627. */
  628. graphDailyRevenue(dateRange){
  629. if(!dateRange){
  630. return false;
  631. }
  632. let dataList = new Array(30).fill(0);
  633. let currentDate = this.transactions[dateRange[0]].date;
  634. let arrayIndex = 0;
  635. for(let i = dateRange[0]; i <= dateRange[1]; i++){
  636. if(this.transactions[i].date.getDate() !== currentDate.getDate()){
  637. currentDate = this.transactions[i].date;
  638. arrayIndex++;
  639. }
  640. for(let j = 0; j < this.transactions[i].recipes.length; j++){
  641. dataList[arrayIndex] += (this.transactions[i].recipes[j].recipe.price / 100) * this.transactions[i].recipes[j].quantity;
  642. }
  643. }
  644. return dataList;
  645. }
  646. /*
  647. Groups all of the merchant's ingredients by their category
  648. Return: [{
  649. name: category name,
  650. ingredients: [Ingredient Object]
  651. }]
  652. */
  653. categorizeIngredients(){
  654. let ingredientsByCategory = [];
  655. for(let i = 0; i < this.ingredients.length; i++){
  656. let categoryExists = false;
  657. for(let j = 0; j < ingredientsByCategory.length; j++){
  658. if(this.ingredients[i].ingredient.category === ingredientsByCategory[j].name){
  659. ingredientsByCategory[j].ingredients.push(this.ingredients[i]);
  660. categoryExists = true;
  661. break;
  662. }
  663. }
  664. if(!categoryExists){
  665. ingredientsByCategory.push({
  666. name: this.ingredients[i].ingredient.category,
  667. ingredients: [this.ingredients[i]]
  668. });
  669. }
  670. }
  671. return ingredientsByCategory;
  672. }
  673. unitizeIngredients(){
  674. let ingredientsByUnit = [];
  675. for(let i = 0; i < this.ingredients.length; i++){
  676. let unitExists = false;
  677. for(let j = 0; j < ingredientsByUnit.length; j++){
  678. if(this.ingredients[i].ingredient.unit === ingredientsByUnit[j].name){
  679. ingredientsByUnit[j].ingredients.push(this.ingredients[i]);
  680. unitExists = true;
  681. break;
  682. }
  683. }
  684. if(!unitExists){
  685. ingredientsByUnit.push({
  686. name: this.ingredients[i].ingredient.unit,
  687. ingredients: [this.ingredients[i]]
  688. });
  689. }
  690. }
  691. return ingredientsByUnit;
  692. }
  693. getRecipesForIngredient(ingredient){
  694. let recipes = [];
  695. for(let i = 0; i < this.recipes.length; i++){
  696. for(let j = 0; j < this.recipes[i].ingredients.length; j++){
  697. if(this.recipes[i].ingredients[j].ingredient === ingredient){
  698. recipes.push(this.recipes[i]);
  699. }
  700. }
  701. }
  702. return recipes;
  703. }
  704. }
  705. let convertToMain = (unit, quantity)=>{
  706. let converted = 0;
  707. if(merchant.units.mass.includes(unit)){
  708. switch(unit){
  709. case "g": converted = quantity; break;
  710. case "kg": converted = quantity * 1000; break;
  711. case "oz": converted = quantity * 28.3495; break;
  712. case "lb": converted = quantity * 453.5924; break;
  713. }
  714. }else if(merchant.units.volume.includes(unit)){
  715. switch(unit){
  716. case "ml": converted = quantity / 1000; break;
  717. case "l": converted = quantity; break;
  718. case "tsp": converted = quantity / 202.8842; break;
  719. case "tbsp": converted = quantity / 67.6278; break;
  720. case "ozfl": converted = quantity / 33.8141; break;
  721. case "cup": converted = quantity / 4.1667; break;
  722. case "pt": converted = quantity / 2.1134; break;
  723. case "qt": converted = quantity / 1.0567; break;
  724. case "gal": converted = quantity * 3.7854; break;
  725. }
  726. }else if(merchant.units.length.includes(unit)){
  727. switch(unit){
  728. case "mm": converted = quantity / 1000; break;
  729. case "cm": converted = quantity / 100; break;
  730. case "m": converted = quantity; break;
  731. case "in": converted = quantity / 39.3701; break;
  732. case "ft": converted = quantity / 3.2808; break;
  733. }
  734. }else{
  735. converted = quantity;
  736. }
  737. return converted;
  738. }
  739. /*
  740. Switches to a different strand
  741. Input:
  742. name: name of the strand. Must end with "Strand"
  743. */
  744. let changeStrand = (name)=>{
  745. closeSidebar();
  746. for(let strand of document.querySelectorAll(".strand")){
  747. strand.style.display = "none";
  748. }
  749. let buttons = document.querySelectorAll(".menuButton");
  750. for(let i = 0; i < buttons.length - 1; i++){
  751. buttons[i].classList = "menuButton";
  752. buttons[i].onclick = ()=>{changeStrand(`${buttons[i].id.slice(0, buttons[i].id.indexOf("Btn"))}Strand`)};
  753. }
  754. let activeButton = document.querySelector(`#${name.slice(0, name.indexOf("Strand"))}Btn`);
  755. activeButton.classList = "menuButton active";
  756. activeButton.onclick = undefined;
  757. document.querySelector(`#${name}`).style.display = "flex";
  758. window[`${name}Obj`].display();
  759. if(window.screen.availWidth <= 1000){
  760. closeMenu();
  761. }
  762. }
  763. //Close any open sidebar
  764. let closeSidebar = ()=>{
  765. let sidebar = document.querySelector("#sidebarDiv");
  766. for(let i = 0; i < sidebar.children.length; i++){
  767. sidebar.children[i].style.display = "none";
  768. }
  769. sidebar.classList = "sidebarHide";
  770. if(window.screen.availWidth <= 1000){
  771. document.querySelector(".contentBlock").style.display = "flex";
  772. document.getElementById("mobileMenuSelector").style.display = "block";
  773. document.getElementById("sidebarCloser").style.display = "none";
  774. }
  775. }
  776. /*
  777. Open a specific sidebar
  778. Input:
  779. sidebar: the outermost element of the sidebar (must contain class sidebar)
  780. */
  781. let openSidebar = (sidebar)=>{
  782. document.querySelector("#sidebarDiv").classList = "sidebar";
  783. let sideBars = document.querySelector("#sidebarDiv").children;
  784. for(let i = 0; i < sideBars.length; i++){
  785. sideBars[i].style.display = "none";
  786. }
  787. sidebar.style.display = "flex";
  788. if(window.screen.availWidth <= 1000){
  789. document.querySelector(".contentBlock").style.display = "none";
  790. document.getElementById("mobileMenuSelector").style.display = "none";
  791. document.getElementById("sidebarCloser").style.display = "block";
  792. }
  793. }
  794. let changeMenu = ()=>{
  795. let menu = document.querySelector(".menu");
  796. let buttons = document.querySelectorAll(".menuButton");
  797. if(!menu.classList.contains("menuMinimized")){
  798. menu.classList = "menu menuMinimized";
  799. for(let button of buttons){
  800. button.children[1].style.display = "none";
  801. }
  802. document.querySelector("#max").style.display = "none";
  803. document.querySelector("#min").style.display = "flex";
  804. }else if(menu.classList.contains("menuMinimized")){
  805. menu.classList = "menu";
  806. for(let button of buttons){
  807. button.children[1].style.display = "block";
  808. }
  809. setTimeout(()=>{
  810. document.querySelector("#max").style.display = "flex";
  811. document.querySelector("#min").style.display = "none";
  812. }, 150);
  813. }
  814. }
  815. let openMenu = ()=>{
  816. document.getElementById("menu").style.display = "flex";
  817. document.querySelector(".contentBlock").style.display = "none";
  818. document.getElementById("mobileMenuSelector").onclick = ()=>{closeMenu()};
  819. }
  820. let closeMenu = ()=>{
  821. document.getElementById("menu").style.display = "none";
  822. document.querySelector(".contentBlock").style.display = "flex";
  823. document.getElementById("mobileMenuSelector").onclick = ()=>{openMenu()};
  824. }
  825. if(window.screen.availWidth > 1000 && window.screen.availWidth <= 1400){
  826. changeMenu();
  827. document.getElementById("menuShifter2").style.display = "none";
  828. }
  829. let merchant = new Merchant(data.merchant, data.transactions);
  830. console.log(merchant);
  831. module.exports = merchant;
  832. homeStrandObj.display();
  833. },{"./addIngredients.js":1,"./home.js":3,"./ingredientDetails.js":4,"./ingredients.js":5,"./newIngredient.js":6,"./newOrder.js":7,"./newRecipe.js":8,"./newTransaction.js":9,"./orderDetails.js":10,"./orders.js":11,"./recipeBook.js":12,"./recipeDetails.js":13,"./transactionDetails.js":14,"./transactions.js":15}],3:[function(require,module,exports){
  834. let merchant = require("./dashboard.js");
  835. module.exports = {
  836. isPopulated: false,
  837. graph: {},
  838. display: function(){
  839. if(!this.isPopulated){
  840. this.drawRevenueCard();
  841. this.drawRevenueGraph();
  842. this.drawInventoryCheckCard();
  843. this.drawPopularCard();
  844. this.isPopulated = true;
  845. }
  846. },
  847. drawRevenueCard: function(){
  848. let today = new Date();
  849. let firstOfMonth = new Date(today.getFullYear(), today.getMonth(), 1);
  850. let firstOfLastMonth = new Date(today.getFullYear(), today.getMonth() - 1, 1);
  851. let lastMonthtoDay = new Date(new Date().setMonth(today.getMonth() - 1));
  852. console.log(merchant);
  853. let revenueThisMonth = merchant.revenue(merchant.transactionIndices(firstOfMonth));
  854. let revenueLastmonthToDay = merchant.revenue(merchant.transactionIndices(firstOfLastMonth, lastMonthtoDay));
  855. document.querySelector("#revenue").innerText = `$${revenueThisMonth.toLocaleString("en")}`;
  856. let revenueChange = ((revenueThisMonth - revenueLastmonthToDay) / revenueLastmonthToDay) * 100;
  857. let img = "";
  858. if(revenueChange >= 0){
  859. img = "/shared/images/upArrow.png";
  860. }else{
  861. img = "/shared/images/downArrow.png";
  862. }
  863. document.querySelector("#revenueChange p").innerText = `${Math.abs(revenueChange).toFixed(2)}% vs last month`;
  864. document.querySelector("#revenueChange img").src = img;
  865. },
  866. drawRevenueGraph: function(){
  867. let graphCanvas = document.querySelector("#graphCanvas");
  868. let today = new Date();
  869. graphCanvas.height = graphCanvas.parentElement.clientHeight;
  870. graphCanvas.width = graphCanvas.parentElement.clientWidth;
  871. this.graph = new LineGraph(graphCanvas);
  872. this.graph.addTitle("Revenue");
  873. let thirtyAgo = new Date(today);
  874. thirtyAgo.setDate(today.getDate() - 29);
  875. let data = merchant.graphDailyRevenue(merchant.transactionIndices(thirtyAgo));
  876. if(data){
  877. this.graph.addData(
  878. data,
  879. [thirtyAgo, new Date()],
  880. "Revenue"
  881. );
  882. }else{
  883. document.querySelector("#graphCanvas").style.display = "none";
  884. let notice = document.createElement("h1");
  885. notice.innerText = "NO DATA YET";
  886. notice.classList = "notice";
  887. document.querySelector("#graphCard").appendChild(notice);
  888. }
  889. },
  890. drawInventoryCheckCard: function(){
  891. let num;
  892. if(merchant.ingredients.length < 5){
  893. num = merchant.ingredients.length;
  894. }else{
  895. num = 5;
  896. }
  897. let rands = [];
  898. for(let i = 0; i < num; i++){
  899. let rand = Math.floor(Math.random() * merchant.ingredients.length);
  900. if(rands.includes(rand)){
  901. i--;
  902. }else{
  903. rands[i] = rand;
  904. }
  905. }
  906. let ul = document.querySelector("#inventoryCheckCard ul");
  907. let template = document.querySelector("#ingredientCheck").content.children[0];
  908. while(ul.children.length > 0){
  909. ul.removeChild(ul.firstChild);
  910. }
  911. for(let i = 0; i < rands.length; i++){
  912. let ingredientCheck = template.cloneNode(true);
  913. let input = ingredientCheck.children[1].children[1];
  914. ingredientCheck.ingredient = merchant.ingredients[rands[i]];
  915. ingredientCheck.children[0].innerText = merchant.ingredients[rands[i]].ingredient.name;
  916. ingredientCheck.children[1].children[0].onclick = ()=>{input.value--};
  917. input.value = merchant.ingredients[rands[i]].quantity.toFixed(2);
  918. ingredientCheck.children[1].children[2].onclick = ()=>{input.value++}
  919. ingredientCheck.children[2].innerText = merchant.ingredients[rands[i]].ingredient.unit.toUpperCase();
  920. ul.appendChild(ingredientCheck);
  921. }
  922. },
  923. drawPopularCard: function(){
  924. let dataArray = [];
  925. let now = new Date();
  926. let thisMonth = new Date(now.getFullYear(), now.getMonth(), 1);
  927. let ingredientList = merchant.ingredientsSold(merchant.transactionIndices(thisMonth));
  928. if(ingredientList !== false){
  929. window.ingredientList = [...ingredientList];
  930. let iterations = (ingredientList.length < 5) ? ingredientList.length : 5;
  931. for(let i = 0; i < iterations; i++){
  932. try{
  933. let max = ingredientList[0].quantity;
  934. let index = 0;
  935. for(let j = 0; j < ingredientList.length; j++){
  936. if(ingredientList[j].quantity > max){
  937. max = ingredientList[j].quantity;
  938. index = j;
  939. }
  940. }
  941. dataArray.push({
  942. num: max,
  943. label: ingredientList[index].ingredient.name + ": " +
  944. ingredientList[index].ingredient.convert(ingredientList[index].quantity).toFixed(2) +
  945. " " + ingredientList[index].ingredient.unit
  946. });
  947. ingredientList.splice(index, 1);
  948. }catch(err){
  949. break;
  950. }
  951. }
  952. let thisCanvas = document.getElementById("popularCanvas");
  953. thisCanvas.width = thisCanvas.parentElement.offsetWidth * 0.8;
  954. thisCanvas.height = thisCanvas.parentElement.offsetHeight * 0.8;
  955. let popularGraph = new HorizontalBarGraph(thisCanvas);
  956. popularGraph.addData(dataArray);
  957. }else{
  958. document.querySelector("#popularCanvas").style.display = "none";
  959. let notice = document.createElement("p");
  960. notice.innerText = "N/A";
  961. notice.classList = "notice";
  962. document.querySelector("#popularIngredientsCard").appendChild(notice);
  963. }
  964. },
  965. submitInventoryCheck: function(){
  966. let lis = document.querySelectorAll("#inventoryCheckCard li");
  967. let changes = [];
  968. for(let i = 0; i < lis.length; i++){
  969. if(lis[i].children[1].children[1].value >= 0){
  970. let merchIngredient = lis[i].ingredient;
  971. let value = parseFloat(lis[i].children[1].children[1].value);
  972. if(value !== merchIngredient.quantity){
  973. changes.push({
  974. id: merchIngredient.ingredient.id,
  975. ingredient: merchIngredient.ingredient,
  976. quantity: value
  977. });
  978. }
  979. }else{
  980. banner.createError("CANNOT HAVE NEGATIVE INGREDIENTS");
  981. return;
  982. }
  983. }
  984. let loader = document.getElementById("loaderContainer");
  985. loader.style.display = "flex";
  986. if(changes.length > 0){
  987. fetch("/merchant/ingredients/update", {
  988. method: "PUT",
  989. headers: {
  990. "Content-Type": "application/json;charset=utf-8"
  991. },
  992. body: JSON.stringify(changes)
  993. })
  994. .then((response) => response.json())
  995. .then((response)=>{
  996. if(typeof(response) === "string"){
  997. banner.createError(response);
  998. }else{
  999. merchant.editIngredients(changes);
  1000. banner.createNotification("INGREDIENTS UPDATED");
  1001. }
  1002. })
  1003. .catch((err)=>{})
  1004. .finally(()=>{
  1005. loader.style.display = "none";
  1006. });
  1007. }
  1008. }
  1009. }
  1010. },{"./dashboard.js":2}],4:[function(require,module,exports){
  1011. module.exports = {
  1012. ingredient: {},
  1013. display: function(ingredient){
  1014. this.ingredient = ingredient;
  1015. sidebar = document.querySelector("#ingredientDetails");
  1016. document.querySelector("#ingredientDetails p").innerText = ingredient.ingredient.category;
  1017. document.querySelector("#ingredientDetails h1").innerText = ingredient.ingredient.name;
  1018. let ingredientStock = document.getElementById("ingredientStock");
  1019. ingredientStock.innerText = `${ingredient.ingredient.convert(ingredient.quantity).toFixed(2)} ${ingredient.ingredient.unit.toUpperCase()}`;
  1020. ingredientStock.style.display = "block";
  1021. let ingredientInput = document.getElementById("ingredientInput");
  1022. ingredientInput.value = ingredient.ingredient.convert(ingredient.quantity).toFixed(2);
  1023. ingredientInput.style.display = "none";
  1024. let quantities = [];
  1025. let now = new Date();
  1026. for(let i = 1; i < 31; i++){
  1027. let endDay = new Date(now.getFullYear(), now.getMonth(), now.getDate() - i)
  1028. let startDay = new Date(now.getFullYear(), now.getMonth(), now.getDate() - i - 1);
  1029. let indices = merchant.transactionIndices(startDay, endDay);
  1030. if(indices === false){
  1031. quantities.push(0);
  1032. }else{
  1033. quantities.push(merchant.singleIngredientSold(indices, ingredient));
  1034. }
  1035. }
  1036. let sum = 0;
  1037. for(let quantity of quantities){
  1038. sum += quantity;
  1039. }
  1040. document.querySelector("#dailyUse").innerText = `${(sum/quantities.length).toFixed(2)} ${ingredient.ingredient.unit}`;
  1041. let ul = document.querySelector("#ingredientRecipeList");
  1042. let recipes = merchant.getRecipesForIngredient(ingredient.ingredient);
  1043. while(ul.children.length > 0){
  1044. ul.removeChild(ul.firstChild);
  1045. }
  1046. for(let i = 0; i < recipes.length; i++){
  1047. let li = document.createElement("li");
  1048. li.innerText = recipes[i].name;
  1049. li.onclick = ()=>{
  1050. changeStrand("recipeBookStrand");
  1051. recipeDetailsComp.display(recipes[i]);
  1052. }
  1053. ul.appendChild(li);
  1054. }
  1055. let ingredientButtons = document.getElementById("ingredientButtons");
  1056. let units = [];
  1057. let unitLabel = document.getElementById("displayUnitLabel");
  1058. let defaultButton = document.getElementById("defaultUnit");
  1059. if(this.ingredient.ingredient.unitType !== "other"){
  1060. units = merchant.units[this.ingredient.ingredient.unitType];
  1061. unitLabel.style.display = "block";
  1062. defaultButton.style.display = "block";
  1063. }else{
  1064. unitLabel.style.display = "none";
  1065. defaultButton.style.display = "none";
  1066. }
  1067. while(ingredientButtons.children.length > 0){
  1068. ingredientButtons.removeChild(ingredientButtons.firstChild);
  1069. }
  1070. for(let i = 0; i < units.length; i++){
  1071. let button = document.createElement("button");
  1072. button.classList.add("unitButton");
  1073. button.innerText = units[i].toUpperCase();
  1074. button.onclick = ()=>{this.changeUnit(button, units[i])};
  1075. ingredientButtons.appendChild(button);
  1076. if(units[i] === this.ingredient.ingredient.unit){
  1077. button.classList.add("unitActive");
  1078. }
  1079. }
  1080. openSidebar(sidebar);
  1081. },
  1082. remove: function(){
  1083. for(let i = 0; i < merchant.recipes.length; i++){
  1084. for(let j = 0; j < merchant.recipes[i].ingredients.length; j++){
  1085. if(this.ingredient.ingredient === merchant.recipes[i].ingredients[j].ingredient){
  1086. banner.createError("MUST REMOVE INGREDIENT FROM ALL RECIPES BEFORE REMOVING FROM INVENTORY");
  1087. return;
  1088. }
  1089. }
  1090. }
  1091. let loader = document.getElementById("loaderContainer");
  1092. loader.style.display = "flex";
  1093. fetch(`/merchant/ingredients/remove/${this.ingredient.ingredient.id}`, {
  1094. method: "DELETE",
  1095. })
  1096. .then((response) => response.json())
  1097. .then((response)=>{
  1098. if(typeof(response) === "string"){
  1099. banner.createError(response);
  1100. }else{
  1101. banner.createNotification("INGREDIENT REMOVED");
  1102. merchant.editIngredients([this.ingredient], true);
  1103. }
  1104. })
  1105. .catch((err)=>{})
  1106. .finally(()=>{
  1107. loader.style.display = "none";
  1108. });
  1109. },
  1110. edit: function(){
  1111. document.getElementById("ingredientStock").style.display = "none";
  1112. document.getElementById("ingredientInput").style.display = "block";
  1113. document.getElementById("editSubmitButton").style.display = "block";
  1114. },
  1115. editSubmit: function(){
  1116. this.ingredient.quantity = Number(document.getElementById("ingredientInput").value);
  1117. let data = [{
  1118. id: this.ingredient.ingredient.id,
  1119. quantity: this.ingredient.quantity
  1120. }];
  1121. let loader = document.getElementById("loaderContainer");
  1122. loader.style.display = "flex";
  1123. if(validator.ingredientQuantity(data[0].quantity)){
  1124. fetch("/merchant/ingredients/update", {
  1125. method: "PUT",
  1126. headers: {
  1127. "Content-Type": "application/json;charset=utf-8"
  1128. },
  1129. body: JSON.stringify(data)
  1130. })
  1131. .then((response) => response.json())
  1132. .then((response)=>{
  1133. if(typeof(response) === "string"){
  1134. banner.createError(response);
  1135. }else{
  1136. merchant.editIngredients([this.ingredient]);
  1137. banner.createNotification("INGREDIENT UPDATED");
  1138. }
  1139. })
  1140. .catch((err)=>{
  1141. banner.createError("SOMETHING WENT WRONG. PLEASE REFRESH THE PAGE");
  1142. })
  1143. .finally(()=>{
  1144. loader.style.display = "none";
  1145. });
  1146. }
  1147. },
  1148. changeUnit: function(newActive, unit){
  1149. this.ingredient.ingredient.unit = unit;
  1150. let ingredientButtons = document.querySelectorAll(".unitButton");
  1151. for(let i = 0; i < ingredientButtons.length; i++){
  1152. ingredientButtons[i].classList.remove("unitActive");
  1153. }
  1154. newActive.classList.add("unitActive");
  1155. homeStrandObj.isPopulated = false;
  1156. ingredientsStrandObj.populateByProperty("category");
  1157. document.getElementById("ingredientStock").innerText = `${this.ingredient.ingredient.convert(this.ingredient.quantity).toFixed(2)} ${this.ingredient.ingredient.unit.toUpperCase()}`;
  1158. },
  1159. changeUnitDefault: function(){
  1160. let loader = document.getElementById("loaderContainer");
  1161. loader.style.display = "flex";
  1162. let id = this.ingredient.ingredient.id;
  1163. let unit = this.ingredient.ingredient.unit;
  1164. fetch(`/merchant/ingredients/update/${id}/${unit}`, {
  1165. method: "put",
  1166. headers: {
  1167. "Content-Type": "application/json;charset=utf-8"
  1168. },
  1169. })
  1170. .then((response)=>{
  1171. if(typeof(response) === "string"){
  1172. banner.createError(response);
  1173. }else{
  1174. banner.createNotification("INGREDIENT DEFAULT UNIT UPDATED");
  1175. }
  1176. })
  1177. .catch((err)=>{
  1178. banner.createError("SOMETHING WENT WRONG. PLEASE REFRESH THE PAGE");
  1179. })
  1180. .finally(()=>{
  1181. loader.style.display = "none";
  1182. });
  1183. }
  1184. }
  1185. },{}],5:[function(require,module,exports){
  1186. module.exports = {
  1187. isPopulated: false,
  1188. ingredients: [],
  1189. display: function(){
  1190. if(!this.isPopulated){
  1191. this.populateByProperty("category");
  1192. this.isPopulated = true;
  1193. }
  1194. },
  1195. populateByProperty: function(property){
  1196. let categories;
  1197. if(property === "category"){
  1198. categories = merchant.categorizeIngredients();
  1199. }else if(property === "unit"){
  1200. categories = merchant.unitizeIngredients();
  1201. }
  1202. let ingredientStrand = document.querySelector("#categoryList");
  1203. let categoryTemplate = document.querySelector("#categoryDiv").content.children[0];
  1204. let ingredientTemplate = document.querySelector("#ingredient").content.children[0];
  1205. this.ingredients = [];
  1206. while(ingredientStrand.children.length > 0){
  1207. ingredientStrand.removeChild(ingredientStrand.firstChild);
  1208. }
  1209. for(let i = 0; i < categories.length; i++){
  1210. let categoryDiv = categoryTemplate.cloneNode(true);
  1211. categoryDiv.children[0].children[0].innerText = categories[i].name;
  1212. categoryDiv.children[0].children[1].onclick = ()=>{this.toggleCategory(categoryDiv.children[1], categoryDiv.children[0].children[1])};
  1213. categoryDiv.children[1].style.display = "none";
  1214. ingredientStrand.appendChild(categoryDiv);
  1215. for(let j = 0; j < categories[i].ingredients.length; j++){
  1216. let ingredient = categories[i].ingredients[j];
  1217. let ingredientDiv = ingredientTemplate.cloneNode(true);
  1218. ingredientDiv.children[0].innerText = ingredient.ingredient.name;
  1219. ingredientDiv.children[2].innerText = `${ingredient.ingredient.convert(ingredient.quantity).toFixed(2)} ${ingredient.ingredient.unit.toUpperCase()}`;
  1220. ingredientDiv.onclick = ()=>{ingredientDetailsComp.display(ingredient)};
  1221. ingredientDiv._name = ingredient.ingredient.name.toLowerCase();
  1222. ingredientDiv._unit = ingredient.ingredient.unit.toLowerCase();
  1223. categoryDiv.children[1].appendChild(ingredientDiv);
  1224. this.ingredients.push(ingredientDiv);
  1225. }
  1226. }
  1227. },
  1228. displayIngredientsOnly: function(ingredients){
  1229. let ingredientDiv = document.querySelector("#categoryList");
  1230. while(ingredientDiv.children.length > 0){
  1231. ingredientDiv.removeChild(ingredientDiv.firstChild);
  1232. }
  1233. for(let i = 0; i < ingredients.length; i++){
  1234. ingredientDiv.appendChild(ingredients[i]);
  1235. }
  1236. },
  1237. toggleCategory: function(div, button){
  1238. if(div.style.display === "none"){
  1239. button.innerHTML = '<svg width="32" height="32" viewBox="0 0 24 24" fill="none" stroke="black" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><polyline points="18 15 12 9 6 15"></polyline></svg>';
  1240. div.style.display = "flex";
  1241. }else if(div.style.display === "flex"){
  1242. button.innerHTML = '<svg width="32" height="32" viewBox="0 0 24 24" fill="none" stroke="black" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><polyline points="6 9 12 15 18 9"></polyline></svg>';
  1243. div.style.display = "none";
  1244. }
  1245. },
  1246. search: function(){
  1247. let input = document.querySelector("#ingredientSearch").value.toLowerCase();
  1248. document.querySelector("#ingredientSelect").selectedIndex = 0;
  1249. if(input === ""){
  1250. this.populateByProperty("category");
  1251. document.querySelector("#ingredientClearButton").style.display = "none";
  1252. return;
  1253. }
  1254. let matchingIngredients = [];
  1255. for(let i = 0; i < this.ingredients.length; i++){
  1256. if(this.ingredients[i]._name.includes(input)){
  1257. matchingIngredients.push(this.ingredients[i]);
  1258. }
  1259. }
  1260. document.querySelector("#ingredientClearButton").style.display = "inline";
  1261. this.displayIngredientsOnly(matchingIngredients);
  1262. },
  1263. sort: function(sortType){
  1264. if(sortType === ""){
  1265. return;
  1266. }
  1267. document.querySelector("#ingredientSearch").value = "";
  1268. if(sortType === "category"){
  1269. this.populateByProperty("category");
  1270. return;
  1271. }
  1272. if(sortType === "unit"){
  1273. this.populateByProperty("unit");
  1274. return;
  1275. }
  1276. document.querySelector("#ingredientClearButton").style.display = "inline";
  1277. let sortedIngredients = this.ingredients.slice().sort((a, b)=> (a[sortType] > b[sortType]) ? 1 : -1);
  1278. this.displayIngredientsOnly(sortedIngredients);
  1279. },
  1280. clearSorting: function(button){
  1281. document.querySelector("#ingredientSearch").value = "";
  1282. document.querySelector("#ingredientSelect").selectedIndex = 0;
  1283. document.querySelector("#ingredientClearButton").style.display = "none";
  1284. this.populateByProperty("category");
  1285. }
  1286. }
  1287. },{}],6:[function(require,module,exports){
  1288. module.exports = {
  1289. display: function(){
  1290. openSidebar(document.querySelector("#newIngredient"));
  1291. document.querySelector("#newIngName").value = "";
  1292. document.querySelector("#newIngCategory").value = "";
  1293. document.querySelector("#newIngQuantity").value = 0;
  1294. },
  1295. submit: function(){
  1296. let unitSelector = document.getElementById("unitSelector");
  1297. let options = document.querySelectorAll("#unitSelector option");
  1298. let unit = unitSelector.value;
  1299. let newIngredient = {
  1300. ingredient: {
  1301. name: document.getElementById("newIngName").value,
  1302. category: document.getElementById("newIngCategory").value,
  1303. unitType: options[unitSelector.selectedIndex].getAttribute("type"),
  1304. },
  1305. quantity: convertToMain(unit, document.querySelector("#newIngQuantity").value),
  1306. defaultUnit: unit
  1307. }
  1308. let loader = document.getElementById("loaderContainer");
  1309. loader.style.display = "flex";
  1310. fetch("/ingredients/create", {
  1311. method: "POST",
  1312. headers: {
  1313. "Content-Type": "application/json;charset=utf-8"
  1314. },
  1315. body: JSON.stringify(newIngredient)
  1316. })
  1317. .then((response) => response.json())
  1318. .then((response)=>{
  1319. if(typeof(response) === "string"){
  1320. banner.createError(response);
  1321. }else{
  1322. merchant.editIngredients([{
  1323. ingredient: new Ingredient(
  1324. response.ingredient._id,
  1325. response.ingredient.name,
  1326. response.ingredient.category,
  1327. response.ingredient.unitType,
  1328. response.defaultUnit,
  1329. merchant
  1330. ),
  1331. quantity: response.quantity
  1332. }]);
  1333. banner.createNotification("INGREDIENT CREATED");
  1334. }
  1335. })
  1336. .catch((err)=>{
  1337. banner.createError("SOMETHING WENT WRONG. PLEASE REFRESH THE PAGE");
  1338. })
  1339. .finally(()=>{
  1340. loader.style.display = "none";
  1341. });
  1342. }
  1343. }
  1344. },{}],7:[function(require,module,exports){
  1345. module.exports = {
  1346. isPopulated: false,
  1347. unused: [],
  1348. display: function(){
  1349. if(!this.isPopulated){
  1350. let categories = merchant.categorizeIngredients();
  1351. let categoriesList = document.querySelector("#newOrderCategories");
  1352. let template = document.querySelector("#addIngredientsCategory").content.children[0];
  1353. let ingredientTemplate = document.querySelector("#addIngredientsIngredient").content.children[0];
  1354. for(let i = 0; i < categories.length; i++){
  1355. let category = template.cloneNode(true);
  1356. category.children[0].children[0].innerText = categories[i].name;
  1357. category.children[0].children[1].onclick = ()=>{addIngredientsComp.toggleAddIngredient(category)};
  1358. category.children[0].children[1].children[1].style.display = "none";
  1359. category.children[1].style.display = "none";
  1360. categoriesList.appendChild(category);
  1361. for(let j = 0; j < categories[i].ingredients.length; j++){
  1362. let ingredientDiv = ingredientTemplate.cloneNode(true);
  1363. ingredientDiv.children[0].innerText = categories[i].ingredients[j].ingredient.name;
  1364. ingredientDiv.children[2].onclick = ()=>{this.addOne(ingredientDiv, category.children[1])};
  1365. ingredientDiv.ingredient = categories[i].ingredients[j].ingredient;
  1366. this.unused.push(categories[i].ingredients[j]);
  1367. category.children[1].appendChild(ingredientDiv);
  1368. }
  1369. }
  1370. this.isPopulated = true;
  1371. }
  1372. openSidebar(document.querySelector("#newOrder"));
  1373. },
  1374. addOne: function(ingredientDiv, container){
  1375. for(let i = 0; i < this.unused.length; i++){
  1376. if(this.unused[i] === ingredientDiv){
  1377. this.unused.splice(i, 1);
  1378. break;
  1379. }
  1380. }
  1381. let quantityInput = document.createElement("input");
  1382. quantityInput.type = "number";
  1383. quantityInput.placeholder = `QUANTITY (${ingredientDiv.ingredient.unit})`;
  1384. quantityInput.min = "0";
  1385. quantityInput.step = "0.01";
  1386. ingredientDiv.insertBefore(quantityInput, ingredientDiv.children[1]);
  1387. let priceInput = document.createElement("input");
  1388. priceInput.type = "number";
  1389. priceInput.placeholder = "Price Per Unit";
  1390. priceInput.min = "0";
  1391. priceInput.step = "0.01";
  1392. ingredientDiv.insertBefore(priceInput, ingredientDiv.children[2]);
  1393. ingredientDiv.children[4].innerText = "-";
  1394. ingredientDiv.children[4].onclick = ()=>{this.removeOne(ingredientDiv, container)};
  1395. container.removeChild(ingredientDiv);
  1396. document.getElementById("newOrderAdded").appendChild(ingredientDiv);
  1397. },
  1398. removeOne: function(ingredientDiv, container){
  1399. this.unused.push(ingredientDiv.ingredient);
  1400. ingredientDiv.removeChild(ingredientDiv.children[1]);
  1401. ingredientDiv.removeChild(ingredientDiv.children[1]);
  1402. ingredientDiv.children[1].innerText = "+";
  1403. ingredientDiv.children[1].onclick = ()=>{this.addOne(ingredientDiv, container)};
  1404. ingredientDiv.parentElement.removeChild(ingredientDiv);
  1405. container.appendChild(ingredientDiv);
  1406. },
  1407. submit: function(){
  1408. let categoriesList = document.getElementById("newOrderAdded");
  1409. let ingredients = [];
  1410. for(let i = 0; i < categoriesList.children.length; i++){
  1411. let quantity = categoriesList.children[i].children[1].value;
  1412. let price = categoriesList.children[i].children[2].value;
  1413. let fakeOrder = new Order(undefined, undefined, new Date(), [], undefined);
  1414. if(quantity !== "" && price !== ""){
  1415. ingredients.push({
  1416. ingredient: categoriesList.children[i].ingredient.id,
  1417. quantity: convertToMain(categoriesList.children[i].ingredient.unit, parseFloat(quantity)),
  1418. price: categoriesList.children[i].ingredient.convert(parseInt(price * 100))
  1419. });
  1420. }
  1421. }
  1422. let date = `${document.getElementById("orderDate").value}T${document.getElementById("orderTime").value}:00`
  1423. let data = {
  1424. name: document.getElementById("orderName").value,
  1425. date: date,
  1426. ingredients: ingredients
  1427. };
  1428. let loader = document.getElementById("loaderContainer");
  1429. loader.style.display = "flex";
  1430. fetch("/order/create", {
  1431. method: "POST",
  1432. headers: {
  1433. "Content-Type": "application/json;charset=utf-8"
  1434. },
  1435. body: JSON.stringify(data)
  1436. })
  1437. .then(response => response.json())
  1438. .then((response)=>{
  1439. if(typeof(response) === "string"){
  1440. banner.createError(response);
  1441. }else{
  1442. let order = new Order(
  1443. response._id,
  1444. response.name,
  1445. response.date,
  1446. response.ingredients,
  1447. merchant
  1448. )
  1449. merchant.editOrders([order]);
  1450. merchant.editIngredients(order.ingredients, false, true);
  1451. banner.createNotification("ORDER CREATED");
  1452. }
  1453. })
  1454. .catch((err)=>{
  1455. banner.createError("SOEMTHING WENT WRONG. PLEASE REFRESH THE PAGE");
  1456. })
  1457. .finally(()=>{
  1458. loader.style.display = "none";
  1459. });
  1460. },
  1461. }
  1462. },{}],8:[function(require,module,exports){
  1463. module.exports = {
  1464. display: function(){
  1465. let ingredientsSelect = document.querySelector("#recipeInputIngredients select");
  1466. let categories = merchant.categorizeIngredients();
  1467. while(ingredientsSelect.children.length > 0){
  1468. ingredientsSelect.removeChild(ingredientsSelect.firstChild);
  1469. }
  1470. for(let category of categories){
  1471. let optgroup = document.createElement("optgroup");
  1472. optgroup.label = category.name;
  1473. ingredientsSelect.appendChild(optgroup);
  1474. for(let ingredient of category.ingredients){
  1475. let option = document.createElement("option");
  1476. option.value = ingredient.ingredient.id;
  1477. option.innerText = `${ingredient.ingredient.name} (${ingredient.ingredient.unit})`;
  1478. optgroup.appendChild(option);
  1479. }
  1480. }
  1481. openSidebar(document.querySelector("#addRecipe"));
  1482. },
  1483. //Updates the number of ingredient inputs displayed for new recipes
  1484. changeRecipeCount: function(){
  1485. let newCount = document.querySelector("#ingredientCount").value;
  1486. let ingredientsDiv = document.querySelector("#recipeInputIngredients");
  1487. let oldCount = ingredientsDiv.children.length;
  1488. if(newCount > oldCount){
  1489. let newDivs = newCount - oldCount;
  1490. for(let i = 0; i < newDivs; i++){
  1491. let newNode = ingredientsDiv.children[0].cloneNode(true);
  1492. newNode.children[2].children[0].value = "";
  1493. ingredientsDiv.appendChild(newNode);
  1494. }
  1495. for(let i = 0; i < newCount; i++){
  1496. ingredientsDiv.children[i].children[0].innerText = `INGREDIENT ${i + 1}`;
  1497. }
  1498. }else if(newCount < oldCount){
  1499. let newDivs = oldCount - newCount;
  1500. for(let i = 0; i < newDivs; i++){
  1501. ingredientsDiv.removeChild(ingredientsDiv.children[ingredientsDiv.children.length-1]);
  1502. }
  1503. }
  1504. },
  1505. submit: function(){
  1506. let newRecipe = {
  1507. name: document.getElementById("newRecipeName").value,
  1508. price: document.getElementById("newRecipePrice").value,
  1509. ingredients: []
  1510. }
  1511. let inputs = document.querySelectorAll("#recipeInputIngredients > div");
  1512. for(let i = 0; i < inputs.length; i++){
  1513. for(let j = 0; j < merchant.ingredients.length; j++){
  1514. if(merchant.ingredients[j].ingredient.id === inputs[i].children[1].children[0].value){
  1515. newRecipe.ingredients.push({
  1516. ingredient: inputs[i].children[1].children[0].value,
  1517. quantity: convertToMain(merchant.ingredients[j].ingredient.unit, inputs[i].children[2].children[0].value)
  1518. });
  1519. break;
  1520. }
  1521. }
  1522. }
  1523. if(!validator.recipe(newRecipe)){
  1524. return;
  1525. }
  1526. let loader = document.getElementById("loaderContainer");
  1527. loader.style.display = "flex";
  1528. fetch("/recipe/create", {
  1529. method: "POST",
  1530. headers: {
  1531. "Content-Type": "application/json;charset=utf-8"
  1532. },
  1533. body: JSON.stringify(newRecipe)
  1534. })
  1535. .then((response) => response.json())
  1536. .then((response)=>{
  1537. if(typeof(response) === "string"){
  1538. banner.createError(response);
  1539. }else{
  1540. let recipe = new Recipe(
  1541. response._id,
  1542. response.name,
  1543. response.price,
  1544. response.ingredients,
  1545. merchant,
  1546. );
  1547. merchant.editRecipes([recipe]);
  1548. banner.createNotification("RECIPE CREATED");
  1549. }
  1550. })
  1551. .catch((err)=>{
  1552. banner.createError("SOMETHING WENT WRONG. PLEASE REFRESH THE PAGE");
  1553. })
  1554. .finally(()=>{
  1555. loader.style.display = "none";
  1556. });
  1557. },
  1558. }
  1559. },{}],9:[function(require,module,exports){
  1560. module.exports = {
  1561. display: function(){
  1562. let recipeList = document.getElementById("newTransactionRecipes");
  1563. let template = document.getElementById("createTransaction").content.children[0];
  1564. while(recipeList.children.length > 0){
  1565. recipeList.removeChild(recipeList.firstChild);
  1566. }
  1567. for(let i = 0; i < merchant.recipes.length; i++){
  1568. let recipeDiv = template.cloneNode(true);
  1569. recipeDiv.recipe = merchant.recipes[i];
  1570. recipeList.appendChild(recipeDiv);
  1571. recipeDiv.children[0].innerText = merchant.recipes[i].name;
  1572. }
  1573. openSidebar(document.getElementById("newTransaction"));
  1574. },
  1575. submit: function(){
  1576. let recipeDivs = document.getElementById("newTransactionRecipes");
  1577. let date = document.getElementById("newTransactionDate").valueAsDate;
  1578. if(date > new Date()){
  1579. banner.createError("CANNOT HAVE A DATE IN THE FUTURE");
  1580. return;
  1581. }
  1582. let newTransaction = {
  1583. date: date,
  1584. recipes: []
  1585. };
  1586. for(let i = 0; i < recipeDivs.children.length; i++){
  1587. let quantity = recipeDivs.children[i].children[1].value;
  1588. if(quantity !== "" && quantity > 0){
  1589. newTransaction.recipes.push({
  1590. recipe: recipeDivs.children[i].recipe.id,
  1591. quantity: quantity
  1592. });
  1593. }else if(quantity < 0){
  1594. banner.createError("CANNOT HAVE NEGATIVE VALUES");
  1595. return;
  1596. }
  1597. }
  1598. if(newTransaction.recipes.length > 0){
  1599. let loader = document.getElementById("loaderContainer");
  1600. loader.style.display = "flex";
  1601. fetch("/transaction/create", {
  1602. method: "post",
  1603. headers: {
  1604. "Content-Type": "application/json;charset=utf-8"
  1605. },
  1606. body: JSON.stringify(newTransaction)
  1607. })
  1608. .then(response => response.json())
  1609. .then((response)=>{
  1610. if(typeof(response) === "string"){
  1611. banner.createError(response);
  1612. }else{
  1613. let transaction = new Transaction(
  1614. response._id,
  1615. response.date,
  1616. response.recipes,
  1617. merchant
  1618. );
  1619. merchant.editTransactions(transaction);
  1620. banner.createNotification("NEW TRANSACTION CREATED");
  1621. }
  1622. })
  1623. .catch((err)=>{
  1624. banner.createError("SOMETHING WENT WRONG. PLEASE REFRESH THE PAGE");
  1625. })
  1626. .finally(()=>{
  1627. loader.style.display = "none";
  1628. });
  1629. }
  1630. }
  1631. }
  1632. },{}],10:[function(require,module,exports){
  1633. module.exports = {
  1634. display: function(order){
  1635. openSidebar(document.querySelector("#orderDetails"));
  1636. document.querySelector("#removeOrderBtn").onclick = ()=>{this.remove(order)};
  1637. document.getElementById("orderDetailName").innerText = order.name;
  1638. document.getElementById("orderDetailDate").innerText = order.date.toLocaleDateString("en-US");
  1639. document.getElementById("orderDetailTime").innerText = order.date.toLocaleTimeString("en-US");
  1640. let ingredientList = document.querySelector("#orderIngredients");
  1641. while(ingredientList.children.length > 0){
  1642. ingredientList.removeChild(ingredientList.firstChild);
  1643. }
  1644. let template = document.querySelector("#orderIngredient").content.children[0];
  1645. let grandTotal = 0;
  1646. for(let i = 0; i < order.ingredients.length; i++){
  1647. let ingredientDiv = template.cloneNode(true);
  1648. let price = (order.ingredients[i].quantity * order.ingredients[i].price) / 100;
  1649. grandTotal += price;
  1650. let ingredient = order.ingredients[i].ingredient;
  1651. let priceText = ingredient.convert(order.ingredients[i].quantity).toFixed(2) + " " +
  1652. ingredient.unit.toUpperCase() + " x $" +
  1653. (order.convertPrice(ingredient.unitType, ingredient.unit, order.ingredients[i].price) / 100).toFixed(2);
  1654. ingredientDiv.children[0].innerText = order.ingredients[i].ingredient.name;
  1655. ingredientDiv.children[1].innerText = priceText;
  1656. ingredientDiv.children[2].innerText = `$${price.toFixed(2)}`;
  1657. ingredientList.appendChild(ingredientDiv);
  1658. }
  1659. document.querySelector("#orderTotalPrice p").innerText = `$${grandTotal.toFixed(2)}`;
  1660. },
  1661. remove: function(order){
  1662. let loader = document.getElementById("loaderContainer");
  1663. loader.style.display = "flex";
  1664. fetch(`/order/${order.id}`, {
  1665. method: "DELETE",
  1666. headers: {
  1667. "Content-Type": "application/json;charset=utf-8"
  1668. }
  1669. })
  1670. .then((response) => response.json())
  1671. .then((response)=>{
  1672. if(typeof(response) === "string"){
  1673. banner.createError(response);
  1674. }else{
  1675. merchant.editOrders([order], true);
  1676. banner.createNotification("ORDER REMOVED");
  1677. }
  1678. })
  1679. .catch((err)=>{
  1680. banner.createError("SOMETHING WENT WRONG. PLEASE REFRESH THE PAGE");
  1681. })
  1682. .finally(()=>{
  1683. loader.style.display = "none";
  1684. });
  1685. }
  1686. }
  1687. },{}],11:[function(require,module,exports){
  1688. module.exports = {
  1689. isFetched: false,
  1690. display: async function(){
  1691. if(!this.isFetched){
  1692. let loader = document.getElementById("loaderContainer");
  1693. loader.style.display = "flex";
  1694. fetch("/order", {
  1695. method: "GET",
  1696. headers: {
  1697. "Content-Type": "application/json;charset=utf-8"
  1698. },
  1699. })
  1700. .then((response) => response.json())
  1701. .then((response)=>{
  1702. if(typeof(response) === "string"){
  1703. banner.createError(response);
  1704. }else{
  1705. let newOrders = [];
  1706. for(let i = 0; i < response.length; i++){
  1707. newOrders.push(new Order(
  1708. response[i]._id,
  1709. response[i].name,
  1710. response[i].date,
  1711. response[i].ingredients,
  1712. merchant
  1713. ));
  1714. }
  1715. merchant.editOrders(newOrders);
  1716. this.isFetched = true;
  1717. }
  1718. })
  1719. .catch((err)=>{
  1720. banner.createError("SOMETHING WENT WRONG. TRY REFRESHING THE PAGE");
  1721. })
  1722. .finally(()=>{
  1723. loader.style.display = "none";
  1724. });
  1725. }
  1726. },
  1727. populate: function(){
  1728. let listDiv = document.getElementById("orderList");
  1729. let template = document.getElementById("order").content.children[0];
  1730. let dateDropdown = document.getElementById("dateDropdownOrder");
  1731. let ingredientDropdown = document.getElementById("ingredientDropdown");
  1732. dateDropdown.style.display = "none";
  1733. ingredientDropdown.style.display = "none";
  1734. document.getElementById("dateFilterBtnOrder").onclick = ()=>{this.toggleDropdown(dateDropdown)};
  1735. document.getElementById("ingredientFilterBtn").onclick = ()=>{this.toggleDropdown(ingredientDropdown)};
  1736. for(let i = 0; i < merchant.ingredients.length; i++){
  1737. let checkbox = document.createElement("input");
  1738. checkbox.type = "checkbox";
  1739. checkbox.ingredient = merchant.ingredients[i].ingredient;
  1740. ingredientDropdown.appendChild(checkbox);
  1741. let label = document.createElement("label");
  1742. label.innerText = merchant.ingredients[i].ingredient.name;
  1743. label.for = checkbox;
  1744. ingredientDropdown.appendChild(label);
  1745. let brk = document.createElement("br");
  1746. ingredientDropdown.appendChild(brk);
  1747. }
  1748. while(listDiv.children.length > 0){
  1749. listDiv.removeChild(listDiv.firstChild);
  1750. }
  1751. for(let i = 0; i < merchant.orders.length; i++){
  1752. let row = template.cloneNode(true);
  1753. let totalCost = 0;
  1754. for(let j = 0; j < merchant.orders[i].ingredients.length; j++){
  1755. totalCost += merchant.orders[i].ingredients[j].quantity * merchant.orders[i].ingredients[j].price;
  1756. }
  1757. row.children[0].innerText = merchant.orders[i].name;
  1758. row.children[1].innerText = `${merchant.orders[i].ingredients.length} items`;
  1759. row.children[2].innerText = new Date(merchant.orders[i].date).toLocaleDateString("en-US");
  1760. row.children[3].innerText = `$${(totalCost / 100).toFixed(2)}`;
  1761. row.order = merchant.orders[i];
  1762. row.onclick = ()=>{orderDetailsComp.display(merchant.orders[i])};
  1763. listDiv.appendChild(row);
  1764. }
  1765. },
  1766. submitFilter: function(){
  1767. event.preventDefault();
  1768. let data = {
  1769. startDate: document.getElementById("orderFilDate1").valueAsDate,
  1770. endDate: document.getElementById("orderFilDate2").valueAsDate,
  1771. ingredients: []
  1772. }
  1773. if(data.startDate >= data.endDate){
  1774. banner.createError("START DATE CANNOT BE AFTER END DATE");
  1775. return;
  1776. }
  1777. let ingredientChoices = document.getElementById("ingredientDropdown");
  1778. for(let i = 0; i < ingredientChoices.children.length; i += 3){
  1779. if(ingredientChoices.children[i].checked){
  1780. data.ingredients.push(ingredientChoices.children[i].ingredient.id);
  1781. }
  1782. }
  1783. if(data.ingredients.length === 0){
  1784. for(let i = 0; i < merchant.ingredients.length; i++){
  1785. data.ingredients.push(merchant.ingredients[i].ingredient.id);
  1786. }
  1787. }
  1788. let loader = document.getElementById("loaderContainer");
  1789. loader.style.display = "flex";
  1790. fetch("/order", {
  1791. method: "POST",
  1792. headers: {
  1793. "Content-Type": "application/json;charset=utf-8"
  1794. },
  1795. body: JSON.stringify(data)
  1796. })
  1797. .then((response) => response.json())
  1798. .then((response)=>{
  1799. if(typeof(response) === "string"){
  1800. banner.createError(response);
  1801. }else{
  1802. let orderList = document.getElementById("orderList");
  1803. let template = document.getElementById("order").content.children[0];
  1804. while(orderList.children.length > 0){
  1805. orderList.removeChild(orderList.firstChild);
  1806. }
  1807. for(let i = 0; i < response.length; i++){
  1808. let order = template.cloneNode(true);
  1809. let cost = 0;
  1810. for(let j = 0; j < response[i].ingredients.length; j++){
  1811. cost += (response[i].ingredients[j].price / 100) * response[i].ingredients[j].quantity;
  1812. }
  1813. order.children[0].innerText = response[i].name,
  1814. order.children[1].innerText = `${response[i].ingredients.length} items`;
  1815. order.children[2].innerText = new Date(response[i].date).toLocaleDateString();
  1816. order.children[3].innerText = `$${cost.toFixed(2)}`;
  1817. orderList.appendChild(order);
  1818. }
  1819. }
  1820. })
  1821. .catch((err)=>{
  1822. banner.createError("UNABLE TO DISPLAY THE ORDERS");
  1823. })
  1824. .finally(()=>{
  1825. loader.style.display = "none";
  1826. });
  1827. },
  1828. toggleDropdown: function(dropdown){
  1829. event.preventDefault();
  1830. let polyline = dropdown.parentElement.children[0].children[1].children[0].children[0];
  1831. if(dropdown.style.display === "none"){
  1832. dropdown.style.display = "block";
  1833. polyline.setAttribute("points", "18 15 12 9 6 15");
  1834. }else{
  1835. dropdown.style.display = "none";
  1836. polyline.setAttribute("points", "6 9 12 15 18 9");
  1837. }
  1838. }
  1839. }
  1840. },{}],12:[function(require,module,exports){
  1841. module.exports = {
  1842. isPopulated: false,
  1843. recipeDivList: [],
  1844. display: function(){
  1845. if(!this.isPopulated){
  1846. this.populateRecipes();
  1847. this.isPopulated = true;
  1848. }
  1849. },
  1850. populateRecipes: function(){
  1851. let recipeList = document.getElementById("recipeList");
  1852. let template = document.getElementById("recipe").content.children[0];
  1853. this.recipeDivList = [];
  1854. while(recipeList.children.length > 0){
  1855. recipeList.removeChild(recipeList.firstChild);
  1856. }
  1857. for(let i = 0; i < merchant.recipes.length; i++){
  1858. let recipeDiv = template.cloneNode(true);
  1859. recipeDiv.onclick = ()=>{recipeDetailsComp.display(merchant.recipes[i])};
  1860. recipeDiv._name = merchant.recipes[i].name;
  1861. recipeList.appendChild(recipeDiv);
  1862. recipeDiv.children[0].innerText = merchant.recipes[i].name;
  1863. recipeDiv.children[1].innerText = `$${(merchant.recipes[i].price / 100).toFixed(2)}`;
  1864. this.recipeDivList.push(recipeDiv);
  1865. }
  1866. },
  1867. search: function(){
  1868. let input = document.getElementById("recipeSearch").value.toLowerCase();
  1869. let recipeList = document.getElementById("recipeList");
  1870. let clearButton = document.getElementById("recipeClearButton");
  1871. let matchingRecipes = [];
  1872. for(let i = 0; i < this.recipeDivList.length; i++){
  1873. if(this.recipeDivList[i]._name.toLowerCase().includes(input)){
  1874. matchingRecipes.push(this.recipeDivList[i]);
  1875. }
  1876. }
  1877. while(recipeList.children.length > 0){
  1878. recipeList.removeChild(recipeList.firstChild);
  1879. }
  1880. for(let i = 0; i < matchingRecipes.length; i++){
  1881. recipeList.appendChild(matchingRecipes[i]);
  1882. }
  1883. if(input === ""){
  1884. clearButton.style.display = "none";
  1885. }else{
  1886. clearButton.style.display = "inline";
  1887. }
  1888. },
  1889. clearSorting: function(){
  1890. document.getElementById("recipeSearch").value = "";
  1891. this.search();
  1892. },
  1893. posUpdate: function(){
  1894. let loader = document.getElementById("loaderContainer");
  1895. loader.style.display = "flex";
  1896. fetch("/recipe/update/clover", {
  1897. method: "GET",
  1898. headers: {
  1899. "Content-Type": "application/json;charset=utf-8"
  1900. },
  1901. })
  1902. .then(response => response.json())
  1903. .then((response)=>{
  1904. let newRecipes = [];
  1905. for(let i = 0; i < response.new.length; i++){
  1906. newRecipes.push(new Recipe(
  1907. response.new[i]._id,
  1908. response.new[i].name,
  1909. response.new[i].price,
  1910. merchant,
  1911. []
  1912. ));
  1913. }
  1914. if(newRecipes.length > 0){
  1915. merchant.editRecipes(newRecipes);
  1916. }
  1917. let removeRecipes = [];
  1918. for(let i = 0; i < response.removed.length; i++){
  1919. for(let j = 0; j < merchant.recipes.length; j++){
  1920. if(response.removed[i]._id === merchant.recipes[j].id){
  1921. removeRecipes.push(merchant.recipes[j], true);
  1922. break;
  1923. }
  1924. }
  1925. }
  1926. if(removeRecipes.length > 0){
  1927. merchant.editRecipes(removeRecipes, true);
  1928. }
  1929. })
  1930. .catch((err)=>{
  1931. banner.createError("SOMETHING WENT WRONG. PLEASE REFRESH THE PAGE");
  1932. })
  1933. .finally(()=>{
  1934. loader.style.display = "none";
  1935. });
  1936. }
  1937. }
  1938. },{}],13:[function(require,module,exports){
  1939. module.exports = {
  1940. recipe: {},
  1941. display: function(recipe){
  1942. this.recipe = recipe;
  1943. openSidebar(document.querySelector("#recipeDetails"));
  1944. document.querySelector("#recipeName").style.display = "block";
  1945. document.querySelector("#recipeNameIn").style.display = "none";
  1946. document.querySelector("#recipeDetails h1").innerText = recipe.name;
  1947. let ingredientList = document.querySelector("#recipeIngredientList");
  1948. while(ingredientList.children.length > 0){
  1949. ingredientList.removeChild(ingredientList.firstChild);
  1950. }
  1951. let template = document.querySelector("#recipeIngredient").content.children[0];
  1952. for(let i = 0; i < recipe.ingredients.length; i++){
  1953. ingredientDiv = template.cloneNode(true);
  1954. ingredientDiv.children[0].innerText = recipe.ingredients[i].ingredient.name;
  1955. ingredientDiv.children[2].innerText = `${recipe.ingredients[i].ingredient.convert(recipe.ingredients[i].quantity).toFixed(2)} ${recipe.ingredients[i].ingredient.unit}`;
  1956. ingredientDiv.ingredient = recipe.ingredients[i].ingredient;
  1957. ingredientDiv.name = recipe.ingredients[i].ingredient.name;
  1958. ingredientList.appendChild(ingredientDiv);
  1959. }
  1960. document.querySelector("#addRecIng").style.display = "none";
  1961. let price = document.querySelector("#recipePrice");
  1962. price.children[1].style.display = "block";
  1963. price.children[2].style.display = "none";
  1964. price.children[1].innerText = `$${(recipe.price / 100).toFixed(2)}`;
  1965. document.querySelector("#recipeUpdate").style.display = "none";
  1966. },
  1967. edit: function(){
  1968. let ingredientDivs = document.querySelector("#recipeIngredientList");
  1969. if(merchant.pos === "none"){
  1970. let name = document.querySelector("#recipeName");
  1971. let nameIn = document.querySelector("#recipeNameIn");
  1972. name.style.display = "none";
  1973. nameIn.style.display = "block";
  1974. nameIn.value = this.recipe.name;
  1975. let price = document.querySelector("#recipePrice");
  1976. price.children[1].style.display = "none";
  1977. price.children[2].style.display = "block";
  1978. price.children[2].value = parseFloat((this.recipe.price / 100).toFixed(2));
  1979. }
  1980. for(let i = 0; i < ingredientDivs.children.length; i++){
  1981. let div = ingredientDivs.children[i];
  1982. div.children[2].innerText = this.recipe.ingredients[i].ingredient.unit;
  1983. div.children[1].style.display = "block";
  1984. div.children[1].value = this.recipe.ingredients[i].ingredient.convert(this.recipe.ingredients[i].quantity).toFixed(2);
  1985. div.children[3].style.display = "block";
  1986. div.children[3].onclick = ()=>{div.parentElement.removeChild(div)};
  1987. }
  1988. document.querySelector("#addRecIng").style.display = "flex";
  1989. document.querySelector("#recipeUpdate").style.display = "flex";
  1990. },
  1991. update: function(){
  1992. this.recipe.name = document.querySelector("#recipeNameIn").value || this.recipe.name;
  1993. this.recipe.price = Math.round((document.querySelector("#recipePrice").children[2].value * 100)) || this.recipe.price;
  1994. this.recipe.ingredients = [];
  1995. let divs = document.querySelector("#recipeIngredientList").children;
  1996. for(let i = 0; i < divs.length; i++){
  1997. if(divs[i].name === "new"){
  1998. let select = divs[i].children[0];
  1999. this.recipe.ingredients.push({
  2000. ingredient: select.options[select.selectedIndex].ingredient,
  2001. quantity: convertToMain(select.options[select.selectedIndex].ingredient.unit, divs[i].children[1].value)
  2002. });
  2003. }else{
  2004. this.recipe.ingredients.push({
  2005. ingredient: divs[i].ingredient,
  2006. quantity: convertToMain(divs[i].ingredient.unit, divs[i].children[1].value)
  2007. });
  2008. }
  2009. }
  2010. let data = {
  2011. id: this.recipe.id,
  2012. name: this.recipe.name,
  2013. price: this.recipe.price,
  2014. ingredients: []
  2015. }
  2016. for(let i = 0; i < this.recipe.ingredients.length; i++){
  2017. data.ingredients.push({
  2018. ingredient: this.recipe.ingredients[i].ingredient.id,
  2019. quantity: this.recipe.ingredients[i].quantity
  2020. });
  2021. }
  2022. let loader = document.getElementById("loaderContainer");
  2023. loader.style.display = "flex";
  2024. fetch("/recipe/update", {
  2025. method: "PUT",
  2026. headers: {
  2027. "Content-Type": "application/json;charset=utf-8"
  2028. },
  2029. body: JSON.stringify(data)
  2030. })
  2031. .then((response) => response.json())
  2032. .then((response)=>{
  2033. if(typeof(response) === "string"){
  2034. banner.createError(response);
  2035. }else{
  2036. merchant.editRecipes([this.recipe]);
  2037. banner.createNotification("RECIPE UPDATE");
  2038. }
  2039. })
  2040. .catch((err)=>{
  2041. banner.createError("SOMETHING WENT WRONG. PLEASE REFRESH THE PAGE");
  2042. })
  2043. .finally(()=>{
  2044. loader.style.display = "none";
  2045. });
  2046. },
  2047. remove: function(){
  2048. fetch(`/merchant/recipes/remove/${this.recipe.id}`, {
  2049. method: "DELETE"
  2050. })
  2051. .then((response) => response.json())
  2052. .then((response)=>{
  2053. if(typeof(response) === "string"){
  2054. banner.createError(response);
  2055. }else{
  2056. merchant.editRecipes([this.recipe], true);
  2057. banner.createNotification("RECIPE REMOVED");
  2058. }
  2059. })
  2060. .catch((err)=>{
  2061. banner.createError("SOMETHING WENT WRONG. PLEASE REFRESH THE PAGE");
  2062. });
  2063. },
  2064. displayAddIngredient: function(){
  2065. let template = document.querySelector("#addRecIngredient").content.children[0].cloneNode(true);
  2066. template.name = "new";
  2067. document.querySelector("#recipeIngredientList").appendChild(template);
  2068. let categories = merchant.categorizeIngredients();
  2069. for(let i = 0; i < categories.length; i++){
  2070. let optGroup = document.createElement("optgroup");
  2071. optGroup.label = categories[i].name;
  2072. template.children[0].appendChild(optGroup);
  2073. for(let j = 0; j < categories[i].ingredients.length; j++){
  2074. let option = document.createElement("option");
  2075. option.innerText = `${categories[i].ingredients[j].ingredient.name} (${categories[i].ingredients[j].ingredient.unit})`;
  2076. option.ingredient = categories[i].ingredients[j].ingredient;
  2077. optGroup.appendChild(option);
  2078. }
  2079. }
  2080. }
  2081. }
  2082. },{}],14:[function(require,module,exports){
  2083. module.exports = {
  2084. transaction: {},
  2085. display: function(transaction){
  2086. this.transaction = transaction;
  2087. let recipeList = document.getElementById("transactionRecipes");
  2088. let template = document.getElementById("transactionRecipe").content.children[0];
  2089. let totalRecipes = 0;
  2090. let totalPrice = 0;
  2091. while(recipeList.children.length > 0){
  2092. recipeList.removeChild(recipeList.firstChild);
  2093. }
  2094. for(let i = 0; i < transaction.recipes.length; i++){
  2095. let recipe = template.cloneNode(true);
  2096. let price = transaction.recipes[i].quantity * transaction.recipes[i].recipe.price;
  2097. recipe.children[0].innerText = transaction.recipes[i].recipe.name;
  2098. recipe.children[1].innerText = `${transaction.recipes[i].quantity} x $${parseFloat(transaction.recipes[i].recipe.price / 100).toFixed(2)}`;
  2099. recipe.children[2].innerText = `$${(price / 100).toFixed(2)}`;
  2100. recipeList.appendChild(recipe);
  2101. totalRecipes += transaction.recipes[i].quantity;
  2102. totalPrice += price;
  2103. }
  2104. let months = ["January", "Fecbruary", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
  2105. let days = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"];
  2106. let dateString = `${days[transaction.date.getDay()]}, ${months[transaction.date.getMonth()]} ${transaction.date.getDate()}, ${transaction.date.getFullYear()}`;
  2107. document.getElementById("transactionDate").innerText = dateString;
  2108. document.getElementById("transactionTime").innerText = transaction.date.toLocaleTimeString();
  2109. document.getElementById("totalRecipes").innerText = `${totalRecipes} recipes`;
  2110. document.getElementById("totalPrice").innerText = `$${(totalPrice / 100).toFixed(2)}`;
  2111. openSidebar(document.getElementById("transactionDetails"));
  2112. },
  2113. remove: function(){
  2114. let loader = document.getElementById("loaderContainer");
  2115. loader.style.display = "flex";
  2116. fetch(`/transaction/${this.transaction.id}`, {
  2117. method: "delete",
  2118. headers: {
  2119. "Content-Type": "application/json;charset=utf-8"
  2120. },
  2121. })
  2122. .then(response => response.json())
  2123. .then((response)=>{
  2124. if(typeof(response) === "string"){
  2125. banner.createError(response);
  2126. }else{
  2127. merchant.editTransactions(this.transaction, true);
  2128. banner.createNotification("TRANSACTION REMOVED");
  2129. }
  2130. })
  2131. .catch((err)=>{
  2132. banner.createError("SOMETHING WENT WRONG. PLEASE REFRESH THE PAGE");
  2133. })
  2134. .finally(()=>{
  2135. loader.style.display = "none";
  2136. });
  2137. },
  2138. }
  2139. },{}],15:[function(require,module,exports){
  2140. module.exports = {
  2141. isPopulated: false,
  2142. display: function(){
  2143. if(!this.isPopulated){
  2144. let transactionsList = document.getElementById("transactionsList");
  2145. let dateDropdown = document.getElementById("dateDropdown");
  2146. let recipeDropdown = document.getElementById("recipeDropDown");
  2147. let template = document.getElementById("transaction").content.children[0];
  2148. let now = new Date();
  2149. let monthAgo = new Date(now.getFullYear(), now.getMonth() - 1, now.getDate());
  2150. document.getElementById("transFilDate1").valueAsDate = monthAgo;
  2151. document.getElementById("transFilDate2").valueAsDate = now;
  2152. dateDropdown.style.display = "none";
  2153. recipeDropdown.style.display = "none";
  2154. document.getElementById("dateFilterBtn").onclick = ()=>{this.toggleDropdown(dateDropdown)};
  2155. document.getElementById("recipeFilterBtn").onclick = ()=>{this.toggleDropdown(recipeDropdown)};
  2156. while(recipeDropdown.children.length > 0){
  2157. recipeDropdown.removeChild(recipeDropdown.firstChild);
  2158. }
  2159. for(let i = 0; i < merchant.recipes.length; i++){
  2160. let checkbox = document.createElement("input");
  2161. checkbox.type = "checkbox";
  2162. checkbox.recipe = merchant.recipes[i];
  2163. recipeDropdown.appendChild(checkbox);
  2164. let label = document.createElement("label");
  2165. label.innerText = merchant.recipes[i].name;
  2166. label.for = checkbox;
  2167. recipeDropdown.appendChild(label);
  2168. let brk = document.createElement("br");
  2169. recipeDropdown.appendChild(brk);
  2170. }
  2171. while(transactionsList.children.length > 0){
  2172. transactionsList.removeChild(transactionsList.firstChild);
  2173. }
  2174. let i = 0
  2175. while(i < merchant.transactions.length && i < 100){
  2176. let transactionDiv = template.cloneNode(true);
  2177. let transaction = merchant.transactions[i];
  2178. transactionDiv.onclick = ()=>{transactionDetailsComp.display(transaction)};
  2179. transactionsList.appendChild(transactionDiv);
  2180. let totalRecipes = 0;
  2181. let totalPrice = 0;
  2182. for(let j = 0; j < merchant.transactions[i].recipes.length; j++){
  2183. totalRecipes += merchant.transactions[i].recipes[j].quantity;
  2184. totalPrice += merchant.transactions[i].recipes[j].recipe.price * merchant.transactions[i].recipes[j].quantity;
  2185. }
  2186. transactionDiv.children[0].innerText = `${merchant.transactions[i].date.toLocaleDateString()} ${merchant.transactions[i].date.toLocaleTimeString()}`;
  2187. transactionDiv.children[1].innerText = `${totalRecipes} recipes sold`;
  2188. transactionDiv.children[2].innerText = `$${(totalPrice / 100).toFixed(2)}`;
  2189. i++;
  2190. }
  2191. this.isPopulated = true;
  2192. }
  2193. },
  2194. submitFilter: function(){
  2195. event.preventDefault();
  2196. let data = {
  2197. startDate: document.getElementById("transFilDate1").valueAsDate,
  2198. endDate: document.getElementById("transFilDate2").valueAsDate,
  2199. recipes: []
  2200. }
  2201. if(data.startDate >= data.endDate){
  2202. banner.createError("START DATE CANNOT BE AFTER END DATE");
  2203. return;
  2204. }
  2205. let recipeChoices = document.getElementById("recipeDropDown");
  2206. for(let i = 0; i < recipeChoices.children.length; i += 3){
  2207. if(recipeChoices.children[i].checked){
  2208. data.recipes.push(recipeChoices.children[i].recipe.id);
  2209. }
  2210. }
  2211. if(data.recipes.length === 0){
  2212. for(let i = 0; i < merchant.recipes.length; i++){
  2213. data.recipes.push(merchant.recipes[i].id);
  2214. }
  2215. }
  2216. let loader = document.getElementById("loaderContainer");
  2217. loader.style.display = "flex";
  2218. fetch("/transaction", {
  2219. method: "POST",
  2220. headers: {
  2221. "Content-Type": "application/json;charset=utf-8"
  2222. },
  2223. body: JSON.stringify(data)
  2224. })
  2225. .then((response) => response.json())
  2226. .then((response)=>{
  2227. if(typeof(response) === "string"){
  2228. banner.createError(response);
  2229. }else{
  2230. let transactionList = document.getElementById("transactionsList");
  2231. let template = document.getElementById("transaction").content.children[0];
  2232. while(transactionList.children.length > 0){
  2233. transactionList.removeChild(transactionList.firstChild);
  2234. }
  2235. for(let i = 0; i < response.length; i++){
  2236. let transactionDiv = template.cloneNode(true);
  2237. let recipeCount = 0;
  2238. let cost = 0;
  2239. let transaction = new Transaction(
  2240. response[i]._id,
  2241. response[i].date,
  2242. response[i].recipes,
  2243. merchant
  2244. );
  2245. for(let j = 0; j < transaction.recipes.length; j++){
  2246. recipeCount += transaction.recipes[j].quantity;
  2247. cost += transaction.recipes[j].quantity * transaction.recipes[j].recipe.price;
  2248. }
  2249. transactionDiv.children[0].innerText = `${transaction.date.toLocaleDateString()} ${transaction.date.toLocaleTimeString()}`;
  2250. transactionDiv.children[1].innerText = `${recipeCount} recipes sold`;
  2251. transactionDiv.children[2].innerText = `$${(cost / 100).toFixed(2)}`;
  2252. transactionDiv.onclick = ()=>{transactionDetailsComp.display(transaction)};
  2253. transactionList.appendChild(transactionDiv);
  2254. }
  2255. }
  2256. })
  2257. .catch((err)=>{
  2258. banner.createError("UNABLE TO DISPLAY THE TRANSACTIONS");
  2259. })
  2260. .finally(()=>{
  2261. loader.style.display = "none";
  2262. });
  2263. },
  2264. toggleDropdown: function(dropdown){
  2265. event.preventDefault();
  2266. let polyline = dropdown.parentElement.children[0].children[1].children[0].children[0];
  2267. if(dropdown.style.display === "none"){
  2268. dropdown.style.display = "block";
  2269. polyline.setAttribute("points", "18 15 12 9 6 15");
  2270. }else{
  2271. dropdown.style.display = "none";
  2272. polyline.setAttribute("points", "6 9 12 15 18 9");
  2273. }
  2274. }
  2275. }
  2276. },{}]},{},[2]);