bundle.js 108 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989
  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 home = require("./home.js");
  196. const ingredients = require("./ingredients.js");
  197. const recipeBook = require("./recipeBook.js");
  198. const orders = require("./orders.js");
  199. const transactions = require("./transactions.js");
  200. const addIngredients = require("./addIngredients.js");
  201. const ingredientDetails = require("./ingredientDetails.js");
  202. const newIngredient = require("./newIngredient.js");
  203. const newOrder = require("./newOrder.js");
  204. const newRecipe = require("./newRecipe.js");
  205. const newTransaction = require("./newTransaction.js");
  206. const orderDetails = require("./orderDetails.js");
  207. const recipeDetails = require("./recipeDetails.js");
  208. const transactionDetails = 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. window.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].disabled = false;
  753. }
  754. let activeButton = {};
  755. switch(name){
  756. case 1:
  757. activeButton = document.getElementById("homeBtn");
  758. document.getElementById("homeStrand").style.display = "flex";
  759. home.display();
  760. break;
  761. case 2:
  762. activeButton = document.getElementById("ingredientsBtn");
  763. document.getElementById("ingredientsStrand").style.display = "flex";
  764. ingredients.display();
  765. break;
  766. case 3:
  767. activeButton = document.getElementById("recipeBookBtn");
  768. document.getElementById("recipeBookStrand").style.display = "flex";
  769. recipeBook.display();
  770. break;
  771. case 4:
  772. activeButton = document.getElementById("ordersBtn");
  773. document.getElementById("ordersStrand").style.display = "flex";
  774. orders.display();
  775. break;
  776. case 5:
  777. activeButton = document.getElementById("transactionsBtn");
  778. document.getElementById("transactionsStrand").style.display = "flex";
  779. transactions.display();
  780. break;
  781. }
  782. activeButton.classList = "menuButton active";
  783. activeButton.disabled = true;
  784. if(window.screen.availWidth <= 1000){
  785. closeMenu();
  786. }
  787. }
  788. //Close any open sidebar
  789. let closeSidebar = ()=>{
  790. let sidebar = document.querySelector("#sidebarDiv");
  791. for(let i = 0; i < sidebar.children.length; i++){
  792. sidebar.children[i].style.display = "none";
  793. }
  794. sidebar.classList = "sidebarHide";
  795. if(window.screen.availWidth <= 1000){
  796. document.querySelector(".contentBlock").style.display = "flex";
  797. document.getElementById("mobileMenuSelector").style.display = "block";
  798. document.getElementById("sidebarCloser").style.display = "none";
  799. }
  800. }
  801. /*
  802. Open a specific sidebar
  803. Input:
  804. sidebar: the outermost element of the sidebar (must contain class sidebar)
  805. */
  806. let openSidebar = (sidebar)=>{
  807. document.querySelector("#sidebarDiv").classList = "sidebar";
  808. let sideBars = document.querySelector("#sidebarDiv").children;
  809. for(let i = 0; i < sideBars.length; i++){
  810. sideBars[i].style.display = "none";
  811. }
  812. sidebar.style.display = "flex";
  813. if(window.screen.availWidth <= 1000){
  814. document.querySelector(".contentBlock").style.display = "none";
  815. document.getElementById("mobileMenuSelector").style.display = "none";
  816. document.getElementById("sidebarCloser").style.display = "block";
  817. }
  818. }
  819. let changeMenu = ()=>{
  820. let menu = document.querySelector(".menu");
  821. let buttons = document.querySelectorAll(".menuButton");
  822. if(!menu.classList.contains("menuMinimized")){
  823. menu.classList = "menu menuMinimized";
  824. for(let button of buttons){
  825. button.children[1].style.display = "none";
  826. }
  827. document.querySelector("#max").style.display = "none";
  828. document.querySelector("#min").style.display = "flex";
  829. }else if(menu.classList.contains("menuMinimized")){
  830. menu.classList = "menu";
  831. for(let button of buttons){
  832. button.children[1].style.display = "block";
  833. }
  834. setTimeout(()=>{
  835. document.querySelector("#max").style.display = "flex";
  836. document.querySelector("#min").style.display = "none";
  837. }, 150);
  838. }
  839. }
  840. let openMenu = ()=>{
  841. document.getElementById("menu").style.display = "flex";
  842. document.querySelector(".contentBlock").style.display = "none";
  843. document.getElementById("mobileMenuSelector").onclick = ()=>{closeMenu()};
  844. }
  845. let closeMenu = ()=>{
  846. document.getElementById("menu").style.display = "none";
  847. document.querySelector(".contentBlock").style.display = "flex";
  848. document.getElementById("mobileMenuSelector").onclick = ()=>{openMenu()};
  849. }
  850. if(window.screen.availWidth > 1000 && window.screen.availWidth <= 1400){
  851. changeMenu();
  852. document.getElementById("menuShifter2").style.display = "none";
  853. }
  854. merchant = new Merchant(data.merchant, data.transactions);
  855. home.display(merchant);
  856. },{"./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){
  857. module.exports = {
  858. isPopulated: false,
  859. graph: {},
  860. display: function(merchant){
  861. if(!this.isPopulated){
  862. this.drawRevenueCard(merchant);
  863. this.drawRevenueGraph();
  864. this.drawInventoryCheckCard();
  865. this.drawPopularCard();
  866. this.isPopulated = true;
  867. }
  868. },
  869. drawRevenueCard: function(merchant){
  870. let today = new Date();
  871. let firstOfMonth = new Date(today.getFullYear(), today.getMonth(), 1);
  872. let firstOfLastMonth = new Date(today.getFullYear(), today.getMonth() - 1, 1);
  873. let lastMonthtoDay = new Date(new Date().setMonth(today.getMonth() - 1));
  874. let revenueThisMonth = merchant.revenue(merchant.transactionIndices(firstOfMonth));
  875. let revenueLastmonthToDay = merchant.revenue(merchant.transactionIndices(firstOfLastMonth, lastMonthtoDay));
  876. document.querySelector("#revenue").innerText = `$${revenueThisMonth.toLocaleString("en")}`;
  877. let revenueChange = ((revenueThisMonth - revenueLastmonthToDay) / revenueLastmonthToDay) * 100;
  878. let img = "";
  879. if(revenueChange >= 0){
  880. img = "/shared/images/upArrow.png";
  881. }else{
  882. img = "/shared/images/downArrow.png";
  883. }
  884. document.querySelector("#revenueChange p").innerText = `${Math.abs(revenueChange).toFixed(2)}% vs last month`;
  885. document.querySelector("#revenueChange img").src = img;
  886. },
  887. drawRevenueGraph: function(){
  888. let graphCanvas = document.querySelector("#graphCanvas");
  889. let today = new Date();
  890. graphCanvas.height = graphCanvas.parentElement.clientHeight;
  891. graphCanvas.width = graphCanvas.parentElement.clientWidth;
  892. let LineGraph = require("../../shared/graphs.js").LineGraph;
  893. this.graph = new LineGraph(graphCanvas);
  894. this.graph.addTitle("Revenue");
  895. let thirtyAgo = new Date(today);
  896. thirtyAgo.setDate(today.getDate() - 29);
  897. let data = merchant.graphDailyRevenue(merchant.transactionIndices(thirtyAgo));
  898. if(data){
  899. this.graph.addData(
  900. data,
  901. [thirtyAgo, new Date()],
  902. "Revenue"
  903. );
  904. }else{
  905. document.querySelector("#graphCanvas").style.display = "none";
  906. let notice = document.createElement("h1");
  907. notice.innerText = "NO DATA YET";
  908. notice.classList = "notice";
  909. document.querySelector("#graphCard").appendChild(notice);
  910. }
  911. },
  912. drawInventoryCheckCard: function(){
  913. let num;
  914. if(merchant.ingredients.length < 5){
  915. num = merchant.ingredients.length;
  916. }else{
  917. num = 5;
  918. }
  919. let rands = [];
  920. for(let i = 0; i < num; i++){
  921. let rand = Math.floor(Math.random() * merchant.ingredients.length);
  922. if(rands.includes(rand)){
  923. i--;
  924. }else{
  925. rands[i] = rand;
  926. }
  927. }
  928. let ul = document.querySelector("#inventoryCheckCard ul");
  929. let template = document.querySelector("#ingredientCheck").content.children[0];
  930. while(ul.children.length > 0){
  931. ul.removeChild(ul.firstChild);
  932. }
  933. for(let i = 0; i < rands.length; i++){
  934. let ingredientCheck = template.cloneNode(true);
  935. let input = ingredientCheck.children[1].children[1];
  936. ingredientCheck.ingredient = merchant.ingredients[rands[i]];
  937. ingredientCheck.children[0].innerText = merchant.ingredients[rands[i]].ingredient.name;
  938. ingredientCheck.children[1].children[0].onclick = ()=>{input.value--};
  939. input.value = merchant.ingredients[rands[i]].quantity.toFixed(2);
  940. ingredientCheck.children[1].children[2].onclick = ()=>{input.value++}
  941. ingredientCheck.children[2].innerText = merchant.ingredients[rands[i]].ingredient.unit.toUpperCase();
  942. ul.appendChild(ingredientCheck);
  943. }
  944. document.getElementById("inventoryCheck").onclick = ()=>{this.submitInventoryCheck()};
  945. },
  946. drawPopularCard: function(){
  947. let dataArray = [];
  948. let now = new Date();
  949. let thisMonth = new Date(now.getFullYear(), now.getMonth(), 1);
  950. let ingredientList = merchant.ingredientsSold(merchant.transactionIndices(thisMonth));
  951. if(ingredientList !== false){
  952. window.ingredientList = [...ingredientList];
  953. let iterations = (ingredientList.length < 5) ? ingredientList.length : 5;
  954. for(let i = 0; i < iterations; i++){
  955. try{
  956. let max = ingredientList[0].quantity;
  957. let index = 0;
  958. for(let j = 0; j < ingredientList.length; j++){
  959. if(ingredientList[j].quantity > max){
  960. max = ingredientList[j].quantity;
  961. index = j;
  962. }
  963. }
  964. dataArray.push({
  965. num: max,
  966. label: ingredientList[index].ingredient.name + ": " +
  967. ingredientList[index].ingredient.convert(ingredientList[index].quantity).toFixed(2) +
  968. " " + ingredientList[index].ingredient.unit
  969. });
  970. ingredientList.splice(index, 1);
  971. }catch(err){
  972. break;
  973. }
  974. }
  975. let thisCanvas = document.getElementById("popularCanvas");
  976. thisCanvas.width = thisCanvas.parentElement.offsetWidth * 0.8;
  977. thisCanvas.height = thisCanvas.parentElement.offsetHeight * 0.8;
  978. let HorizontalBarGraph = require("../../shared/graphs.js").HorizontalBarGraph;
  979. let popularGraph = new HorizontalBarGraph(thisCanvas);
  980. popularGraph.addData(dataArray);
  981. }else{
  982. document.querySelector("#popularCanvas").style.display = "none";
  983. let notice = document.createElement("p");
  984. notice.innerText = "N/A";
  985. notice.classList = "notice";
  986. document.querySelector("#popularIngredientsCard").appendChild(notice);
  987. }
  988. },
  989. submitInventoryCheck: function(){
  990. let lis = document.querySelectorAll("#inventoryCheckCard li");
  991. let changes = [];
  992. let fetchData = [];
  993. for(let i = 0; i < lis.length; i++){
  994. if(lis[i].children[1].children[1].value >= 0){
  995. let merchIngredient = lis[i].ingredient;
  996. let value = parseFloat(lis[i].children[1].children[1].value);
  997. if(value !== merchIngredient.quantity){
  998. changes.push({
  999. id: merchIngredient.ingredient.id,
  1000. ingredient: merchIngredient.ingredient,
  1001. quantity: value
  1002. });
  1003. fetchData.push({
  1004. id: merchIngredient.ingredient.id,
  1005. quantity: value
  1006. });
  1007. }
  1008. }else{
  1009. banner.createError("CANNOT HAVE NEGATIVE INGREDIENTS");
  1010. return;
  1011. }
  1012. }
  1013. let loader = document.getElementById("loaderContainer");
  1014. loader.style.display = "flex";
  1015. if(fetchData.length > 0){
  1016. fetch("/merchant/ingredients/update", {
  1017. method: "PUT",
  1018. headers: {
  1019. "Content-Type": "application/json;charset=utf-8"
  1020. },
  1021. body: JSON.stringify(fetchData)
  1022. })
  1023. .then((response) => response.json())
  1024. .then((response)=>{
  1025. if(typeof(response) === "string"){
  1026. banner.createError(response);
  1027. }else{
  1028. merchant.editIngredients(changes);
  1029. banner.createNotification("INGREDIENTS UPDATED");
  1030. }
  1031. })
  1032. .catch((err)=>{})
  1033. .finally(()=>{
  1034. loader.style.display = "none";
  1035. });
  1036. }
  1037. }
  1038. }
  1039. },{"../../shared/graphs.js":16}],4:[function(require,module,exports){
  1040. module.exports = {
  1041. ingredient: {},
  1042. display: function(ingredient){
  1043. this.ingredient = ingredient;
  1044. sidebar = document.querySelector("#ingredientDetails");
  1045. document.querySelector("#ingredientDetails p").innerText = ingredient.ingredient.category;
  1046. document.querySelector("#ingredientDetails h1").innerText = ingredient.ingredient.name;
  1047. let ingredientStock = document.getElementById("ingredientStock");
  1048. ingredientStock.innerText = `${ingredient.ingredient.convert(ingredient.quantity).toFixed(2)} ${ingredient.ingredient.unit.toUpperCase()}`;
  1049. ingredientStock.style.display = "block";
  1050. let ingredientInput = document.getElementById("ingredientInput");
  1051. ingredientInput.value = ingredient.ingredient.convert(ingredient.quantity).toFixed(2);
  1052. ingredientInput.style.display = "none";
  1053. let quantities = [];
  1054. let now = new Date();
  1055. for(let i = 1; i < 31; i++){
  1056. let endDay = new Date(now.getFullYear(), now.getMonth(), now.getDate() - i)
  1057. let startDay = new Date(now.getFullYear(), now.getMonth(), now.getDate() - i - 1);
  1058. let indices = merchant.transactionIndices(startDay, endDay);
  1059. if(indices === false){
  1060. quantities.push(0);
  1061. }else{
  1062. quantities.push(merchant.singleIngredientSold(indices, ingredient));
  1063. }
  1064. }
  1065. let sum = 0;
  1066. for(let quantity of quantities){
  1067. sum += quantity;
  1068. }
  1069. document.querySelector("#dailyUse").innerText = `${(sum/quantities.length).toFixed(2)} ${ingredient.ingredient.unit}`;
  1070. let ul = document.querySelector("#ingredientRecipeList");
  1071. let recipes = merchant.getRecipesForIngredient(ingredient.ingredient);
  1072. while(ul.children.length > 0){
  1073. ul.removeChild(ul.firstChild);
  1074. }
  1075. for(let i = 0; i < recipes.length; i++){
  1076. let li = document.createElement("li");
  1077. li.innerText = recipes[i].name;
  1078. li.onclick = ()=>{
  1079. changeStrand("recipeBookStrand");
  1080. recipeDetailsComp.display(recipes[i]);
  1081. }
  1082. ul.appendChild(li);
  1083. }
  1084. let ingredientButtons = document.getElementById("ingredientButtons");
  1085. let units = [];
  1086. let unitLabel = document.getElementById("displayUnitLabel");
  1087. let defaultButton = document.getElementById("defaultUnit");
  1088. if(this.ingredient.ingredient.unitType !== "other"){
  1089. units = merchant.units[this.ingredient.ingredient.unitType];
  1090. unitLabel.style.display = "block";
  1091. defaultButton.style.display = "block";
  1092. }else{
  1093. unitLabel.style.display = "none";
  1094. defaultButton.style.display = "none";
  1095. }
  1096. while(ingredientButtons.children.length > 0){
  1097. ingredientButtons.removeChild(ingredientButtons.firstChild);
  1098. }
  1099. for(let i = 0; i < units.length; i++){
  1100. let button = document.createElement("button");
  1101. button.classList.add("unitButton");
  1102. button.innerText = units[i].toUpperCase();
  1103. button.onclick = ()=>{this.changeUnit(button, units[i])};
  1104. ingredientButtons.appendChild(button);
  1105. if(units[i] === this.ingredient.ingredient.unit){
  1106. button.classList.add("unitActive");
  1107. }
  1108. }
  1109. openSidebar(sidebar);
  1110. },
  1111. remove: function(){
  1112. for(let i = 0; i < merchant.recipes.length; i++){
  1113. for(let j = 0; j < merchant.recipes[i].ingredients.length; j++){
  1114. if(this.ingredient.ingredient === merchant.recipes[i].ingredients[j].ingredient){
  1115. banner.createError("MUST REMOVE INGREDIENT FROM ALL RECIPES BEFORE REMOVING FROM INVENTORY");
  1116. return;
  1117. }
  1118. }
  1119. }
  1120. let loader = document.getElementById("loaderContainer");
  1121. loader.style.display = "flex";
  1122. fetch(`/merchant/ingredients/remove/${this.ingredient.ingredient.id}`, {
  1123. method: "DELETE",
  1124. })
  1125. .then((response) => response.json())
  1126. .then((response)=>{
  1127. if(typeof(response) === "string"){
  1128. banner.createError(response);
  1129. }else{
  1130. banner.createNotification("INGREDIENT REMOVED");
  1131. merchant.editIngredients([this.ingredient], true);
  1132. }
  1133. })
  1134. .catch((err)=>{})
  1135. .finally(()=>{
  1136. loader.style.display = "none";
  1137. });
  1138. },
  1139. edit: function(){
  1140. document.getElementById("ingredientStock").style.display = "none";
  1141. document.getElementById("ingredientInput").style.display = "block";
  1142. document.getElementById("editSubmitButton").style.display = "block";
  1143. },
  1144. editSubmit: function(){
  1145. this.ingredient.quantity = Number(document.getElementById("ingredientInput").value);
  1146. let data = [{
  1147. id: this.ingredient.ingredient.id,
  1148. quantity: this.ingredient.quantity
  1149. }];
  1150. let loader = document.getElementById("loaderContainer");
  1151. loader.style.display = "flex";
  1152. if(validator.ingredientQuantity(data[0].quantity)){
  1153. fetch("/merchant/ingredients/update", {
  1154. method: "PUT",
  1155. headers: {
  1156. "Content-Type": "application/json;charset=utf-8"
  1157. },
  1158. body: JSON.stringify(data)
  1159. })
  1160. .then((response) => response.json())
  1161. .then((response)=>{
  1162. if(typeof(response) === "string"){
  1163. banner.createError(response);
  1164. }else{
  1165. merchant.editIngredients([this.ingredient]);
  1166. banner.createNotification("INGREDIENT UPDATED");
  1167. }
  1168. })
  1169. .catch((err)=>{
  1170. banner.createError("SOMETHING WENT WRONG. PLEASE REFRESH THE PAGE");
  1171. })
  1172. .finally(()=>{
  1173. loader.style.display = "none";
  1174. });
  1175. }
  1176. },
  1177. changeUnit: function(newActive, unit){
  1178. this.ingredient.ingredient.unit = unit;
  1179. let ingredientButtons = document.querySelectorAll(".unitButton");
  1180. for(let i = 0; i < ingredientButtons.length; i++){
  1181. ingredientButtons[i].classList.remove("unitActive");
  1182. }
  1183. newActive.classList.add("unitActive");
  1184. homeStrandObj.isPopulated = false;
  1185. ingredientsStrandObj.populateByProperty("category");
  1186. document.getElementById("ingredientStock").innerText = `${this.ingredient.ingredient.convert(this.ingredient.quantity).toFixed(2)} ${this.ingredient.ingredient.unit.toUpperCase()}`;
  1187. },
  1188. changeUnitDefault: function(){
  1189. let loader = document.getElementById("loaderContainer");
  1190. loader.style.display = "flex";
  1191. let id = this.ingredient.ingredient.id;
  1192. let unit = this.ingredient.ingredient.unit;
  1193. fetch(`/merchant/ingredients/update/${id}/${unit}`, {
  1194. method: "put",
  1195. headers: {
  1196. "Content-Type": "application/json;charset=utf-8"
  1197. },
  1198. })
  1199. .then((response)=>{
  1200. if(typeof(response) === "string"){
  1201. banner.createError(response);
  1202. }else{
  1203. banner.createNotification("INGREDIENT DEFAULT UNIT UPDATED");
  1204. }
  1205. })
  1206. .catch((err)=>{
  1207. banner.createError("SOMETHING WENT WRONG. PLEASE REFRESH THE PAGE");
  1208. })
  1209. .finally(()=>{
  1210. loader.style.display = "none";
  1211. });
  1212. }
  1213. }
  1214. },{}],5:[function(require,module,exports){
  1215. module.exports = {
  1216. isPopulated: false,
  1217. ingredients: [],
  1218. display: function(){
  1219. if(!this.isPopulated){
  1220. this.populateByProperty("category");
  1221. this.isPopulated = true;
  1222. }
  1223. },
  1224. populateByProperty: function(property){
  1225. let categories;
  1226. if(property === "category"){
  1227. categories = merchant.categorizeIngredients();
  1228. }else if(property === "unit"){
  1229. categories = merchant.unitizeIngredients();
  1230. }
  1231. let ingredientStrand = document.querySelector("#categoryList");
  1232. let categoryTemplate = document.querySelector("#categoryDiv").content.children[0];
  1233. let ingredientTemplate = document.querySelector("#ingredient").content.children[0];
  1234. this.ingredients = [];
  1235. while(ingredientStrand.children.length > 0){
  1236. ingredientStrand.removeChild(ingredientStrand.firstChild);
  1237. }
  1238. for(let i = 0; i < categories.length; i++){
  1239. let categoryDiv = categoryTemplate.cloneNode(true);
  1240. categoryDiv.children[0].children[0].innerText = categories[i].name;
  1241. categoryDiv.children[0].children[1].onclick = ()=>{this.toggleCategory(categoryDiv.children[1], categoryDiv.children[0].children[1])};
  1242. categoryDiv.children[1].style.display = "none";
  1243. ingredientStrand.appendChild(categoryDiv);
  1244. for(let j = 0; j < categories[i].ingredients.length; j++){
  1245. let ingredient = categories[i].ingredients[j];
  1246. let ingredientDiv = ingredientTemplate.cloneNode(true);
  1247. ingredientDiv.children[0].innerText = ingredient.ingredient.name;
  1248. ingredientDiv.children[2].innerText = `${ingredient.ingredient.convert(ingredient.quantity).toFixed(2)} ${ingredient.ingredient.unit.toUpperCase()}`;
  1249. ingredientDiv.onclick = ()=>{ingredientDetailsComp.display(ingredient)};
  1250. ingredientDiv._name = ingredient.ingredient.name.toLowerCase();
  1251. ingredientDiv._unit = ingredient.ingredient.unit.toLowerCase();
  1252. categoryDiv.children[1].appendChild(ingredientDiv);
  1253. this.ingredients.push(ingredientDiv);
  1254. }
  1255. }
  1256. },
  1257. displayIngredientsOnly: function(ingredients){
  1258. let ingredientDiv = document.querySelector("#categoryList");
  1259. while(ingredientDiv.children.length > 0){
  1260. ingredientDiv.removeChild(ingredientDiv.firstChild);
  1261. }
  1262. for(let i = 0; i < ingredients.length; i++){
  1263. ingredientDiv.appendChild(ingredients[i]);
  1264. }
  1265. },
  1266. toggleCategory: function(div, button){
  1267. if(div.style.display === "none"){
  1268. 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>';
  1269. div.style.display = "flex";
  1270. }else if(div.style.display === "flex"){
  1271. 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>';
  1272. div.style.display = "none";
  1273. }
  1274. },
  1275. search: function(){
  1276. let input = document.querySelector("#ingredientSearch").value.toLowerCase();
  1277. document.querySelector("#ingredientSelect").selectedIndex = 0;
  1278. if(input === ""){
  1279. this.populateByProperty("category");
  1280. document.querySelector("#ingredientClearButton").style.display = "none";
  1281. return;
  1282. }
  1283. let matchingIngredients = [];
  1284. for(let i = 0; i < this.ingredients.length; i++){
  1285. if(this.ingredients[i]._name.includes(input)){
  1286. matchingIngredients.push(this.ingredients[i]);
  1287. }
  1288. }
  1289. document.querySelector("#ingredientClearButton").style.display = "inline";
  1290. this.displayIngredientsOnly(matchingIngredients);
  1291. },
  1292. sort: function(sortType){
  1293. if(sortType === ""){
  1294. return;
  1295. }
  1296. document.querySelector("#ingredientSearch").value = "";
  1297. if(sortType === "category"){
  1298. this.populateByProperty("category");
  1299. return;
  1300. }
  1301. if(sortType === "unit"){
  1302. this.populateByProperty("unit");
  1303. return;
  1304. }
  1305. document.querySelector("#ingredientClearButton").style.display = "inline";
  1306. let sortedIngredients = this.ingredients.slice().sort((a, b)=> (a[sortType] > b[sortType]) ? 1 : -1);
  1307. this.displayIngredientsOnly(sortedIngredients);
  1308. },
  1309. clearSorting: function(button){
  1310. document.querySelector("#ingredientSearch").value = "";
  1311. document.querySelector("#ingredientSelect").selectedIndex = 0;
  1312. document.querySelector("#ingredientClearButton").style.display = "none";
  1313. this.populateByProperty("category");
  1314. }
  1315. }
  1316. },{}],6:[function(require,module,exports){
  1317. module.exports = {
  1318. display: function(){
  1319. openSidebar(document.querySelector("#newIngredient"));
  1320. document.querySelector("#newIngName").value = "";
  1321. document.querySelector("#newIngCategory").value = "";
  1322. document.querySelector("#newIngQuantity").value = 0;
  1323. },
  1324. submit: function(){
  1325. let unitSelector = document.getElementById("unitSelector");
  1326. let options = document.querySelectorAll("#unitSelector option");
  1327. let unit = unitSelector.value;
  1328. let newIngredient = {
  1329. ingredient: {
  1330. name: document.getElementById("newIngName").value,
  1331. category: document.getElementById("newIngCategory").value,
  1332. unitType: options[unitSelector.selectedIndex].getAttribute("type"),
  1333. },
  1334. quantity: convertToMain(unit, document.querySelector("#newIngQuantity").value),
  1335. defaultUnit: unit
  1336. }
  1337. let loader = document.getElementById("loaderContainer");
  1338. loader.style.display = "flex";
  1339. fetch("/ingredients/create", {
  1340. method: "POST",
  1341. headers: {
  1342. "Content-Type": "application/json;charset=utf-8"
  1343. },
  1344. body: JSON.stringify(newIngredient)
  1345. })
  1346. .then((response) => response.json())
  1347. .then((response)=>{
  1348. if(typeof(response) === "string"){
  1349. banner.createError(response);
  1350. }else{
  1351. merchant.editIngredients([{
  1352. ingredient: new Ingredient(
  1353. response.ingredient._id,
  1354. response.ingredient.name,
  1355. response.ingredient.category,
  1356. response.ingredient.unitType,
  1357. response.defaultUnit,
  1358. merchant
  1359. ),
  1360. quantity: response.quantity
  1361. }]);
  1362. banner.createNotification("INGREDIENT CREATED");
  1363. }
  1364. })
  1365. .catch((err)=>{
  1366. banner.createError("SOMETHING WENT WRONG. PLEASE REFRESH THE PAGE");
  1367. })
  1368. .finally(()=>{
  1369. loader.style.display = "none";
  1370. });
  1371. }
  1372. }
  1373. },{}],7:[function(require,module,exports){
  1374. module.exports = {
  1375. isPopulated: false,
  1376. unused: [],
  1377. display: function(){
  1378. if(!this.isPopulated){
  1379. let categories = merchant.categorizeIngredients();
  1380. let categoriesList = document.querySelector("#newOrderCategories");
  1381. let template = document.querySelector("#addIngredientsCategory").content.children[0];
  1382. let ingredientTemplate = document.querySelector("#addIngredientsIngredient").content.children[0];
  1383. for(let i = 0; i < categories.length; i++){
  1384. let category = template.cloneNode(true);
  1385. category.children[0].children[0].innerText = categories[i].name;
  1386. category.children[0].children[1].onclick = ()=>{addIngredientsComp.toggleAddIngredient(category)};
  1387. category.children[0].children[1].children[1].style.display = "none";
  1388. category.children[1].style.display = "none";
  1389. categoriesList.appendChild(category);
  1390. for(let j = 0; j < categories[i].ingredients.length; j++){
  1391. let ingredientDiv = ingredientTemplate.cloneNode(true);
  1392. ingredientDiv.children[0].innerText = categories[i].ingredients[j].ingredient.name;
  1393. ingredientDiv.children[2].onclick = ()=>{this.addOne(ingredientDiv, category.children[1])};
  1394. ingredientDiv.ingredient = categories[i].ingredients[j].ingredient;
  1395. this.unused.push(categories[i].ingredients[j]);
  1396. category.children[1].appendChild(ingredientDiv);
  1397. }
  1398. }
  1399. this.isPopulated = true;
  1400. }
  1401. openSidebar(document.querySelector("#newOrder"));
  1402. },
  1403. addOne: function(ingredientDiv, container){
  1404. for(let i = 0; i < this.unused.length; i++){
  1405. if(this.unused[i] === ingredientDiv){
  1406. this.unused.splice(i, 1);
  1407. break;
  1408. }
  1409. }
  1410. let quantityInput = document.createElement("input");
  1411. quantityInput.type = "number";
  1412. quantityInput.placeholder = `QUANTITY (${ingredientDiv.ingredient.unit})`;
  1413. quantityInput.min = "0";
  1414. quantityInput.step = "0.01";
  1415. ingredientDiv.insertBefore(quantityInput, ingredientDiv.children[1]);
  1416. let priceInput = document.createElement("input");
  1417. priceInput.type = "number";
  1418. priceInput.placeholder = "Price Per Unit";
  1419. priceInput.min = "0";
  1420. priceInput.step = "0.01";
  1421. ingredientDiv.insertBefore(priceInput, ingredientDiv.children[2]);
  1422. ingredientDiv.children[4].innerText = "-";
  1423. ingredientDiv.children[4].onclick = ()=>{this.removeOne(ingredientDiv, container)};
  1424. container.removeChild(ingredientDiv);
  1425. document.getElementById("newOrderAdded").appendChild(ingredientDiv);
  1426. },
  1427. removeOne: function(ingredientDiv, container){
  1428. this.unused.push(ingredientDiv.ingredient);
  1429. ingredientDiv.removeChild(ingredientDiv.children[1]);
  1430. ingredientDiv.removeChild(ingredientDiv.children[1]);
  1431. ingredientDiv.children[1].innerText = "+";
  1432. ingredientDiv.children[1].onclick = ()=>{this.addOne(ingredientDiv, container)};
  1433. ingredientDiv.parentElement.removeChild(ingredientDiv);
  1434. container.appendChild(ingredientDiv);
  1435. },
  1436. submit: function(){
  1437. let categoriesList = document.getElementById("newOrderAdded");
  1438. let ingredients = [];
  1439. for(let i = 0; i < categoriesList.children.length; i++){
  1440. let quantity = categoriesList.children[i].children[1].value;
  1441. let price = categoriesList.children[i].children[2].value;
  1442. let fakeOrder = new Order(undefined, undefined, new Date(), [], undefined);
  1443. if(quantity !== "" && price !== ""){
  1444. ingredients.push({
  1445. ingredient: categoriesList.children[i].ingredient.id,
  1446. quantity: convertToMain(categoriesList.children[i].ingredient.unit, parseFloat(quantity)),
  1447. price: categoriesList.children[i].ingredient.convert(parseInt(price * 100))
  1448. });
  1449. }
  1450. }
  1451. let date = `${document.getElementById("orderDate").value}T${document.getElementById("orderTime").value}:00`
  1452. let data = {
  1453. name: document.getElementById("orderName").value,
  1454. date: date,
  1455. ingredients: ingredients
  1456. };
  1457. let loader = document.getElementById("loaderContainer");
  1458. loader.style.display = "flex";
  1459. fetch("/order/create", {
  1460. method: "POST",
  1461. headers: {
  1462. "Content-Type": "application/json;charset=utf-8"
  1463. },
  1464. body: JSON.stringify(data)
  1465. })
  1466. .then(response => response.json())
  1467. .then((response)=>{
  1468. if(typeof(response) === "string"){
  1469. banner.createError(response);
  1470. }else{
  1471. let order = new Order(
  1472. response._id,
  1473. response.name,
  1474. response.date,
  1475. response.ingredients,
  1476. merchant
  1477. )
  1478. merchant.editOrders([order]);
  1479. merchant.editIngredients(order.ingredients, false, true);
  1480. banner.createNotification("ORDER CREATED");
  1481. }
  1482. })
  1483. .catch((err)=>{
  1484. banner.createError("SOEMTHING WENT WRONG. PLEASE REFRESH THE PAGE");
  1485. })
  1486. .finally(()=>{
  1487. loader.style.display = "none";
  1488. });
  1489. },
  1490. }
  1491. },{}],8:[function(require,module,exports){
  1492. module.exports = {
  1493. display: function(){
  1494. let ingredientsSelect = document.querySelector("#recipeInputIngredients select");
  1495. let categories = merchant.categorizeIngredients();
  1496. while(ingredientsSelect.children.length > 0){
  1497. ingredientsSelect.removeChild(ingredientsSelect.firstChild);
  1498. }
  1499. for(let category of categories){
  1500. let optgroup = document.createElement("optgroup");
  1501. optgroup.label = category.name;
  1502. ingredientsSelect.appendChild(optgroup);
  1503. for(let ingredient of category.ingredients){
  1504. let option = document.createElement("option");
  1505. option.value = ingredient.ingredient.id;
  1506. option.innerText = `${ingredient.ingredient.name} (${ingredient.ingredient.unit})`;
  1507. optgroup.appendChild(option);
  1508. }
  1509. }
  1510. openSidebar(document.querySelector("#addRecipe"));
  1511. },
  1512. //Updates the number of ingredient inputs displayed for new recipes
  1513. changeRecipeCount: function(){
  1514. let newCount = document.querySelector("#ingredientCount").value;
  1515. let ingredientsDiv = document.querySelector("#recipeInputIngredients");
  1516. let oldCount = ingredientsDiv.children.length;
  1517. if(newCount > oldCount){
  1518. let newDivs = newCount - oldCount;
  1519. for(let i = 0; i < newDivs; i++){
  1520. let newNode = ingredientsDiv.children[0].cloneNode(true);
  1521. newNode.children[2].children[0].value = "";
  1522. ingredientsDiv.appendChild(newNode);
  1523. }
  1524. for(let i = 0; i < newCount; i++){
  1525. ingredientsDiv.children[i].children[0].innerText = `INGREDIENT ${i + 1}`;
  1526. }
  1527. }else if(newCount < oldCount){
  1528. let newDivs = oldCount - newCount;
  1529. for(let i = 0; i < newDivs; i++){
  1530. ingredientsDiv.removeChild(ingredientsDiv.children[ingredientsDiv.children.length-1]);
  1531. }
  1532. }
  1533. },
  1534. submit: function(){
  1535. let newRecipe = {
  1536. name: document.getElementById("newRecipeName").value,
  1537. price: document.getElementById("newRecipePrice").value,
  1538. ingredients: []
  1539. }
  1540. let inputs = document.querySelectorAll("#recipeInputIngredients > div");
  1541. for(let i = 0; i < inputs.length; i++){
  1542. for(let j = 0; j < merchant.ingredients.length; j++){
  1543. if(merchant.ingredients[j].ingredient.id === inputs[i].children[1].children[0].value){
  1544. newRecipe.ingredients.push({
  1545. ingredient: inputs[i].children[1].children[0].value,
  1546. quantity: convertToMain(merchant.ingredients[j].ingredient.unit, inputs[i].children[2].children[0].value)
  1547. });
  1548. break;
  1549. }
  1550. }
  1551. }
  1552. if(!validator.recipe(newRecipe)){
  1553. return;
  1554. }
  1555. let loader = document.getElementById("loaderContainer");
  1556. loader.style.display = "flex";
  1557. fetch("/recipe/create", {
  1558. method: "POST",
  1559. headers: {
  1560. "Content-Type": "application/json;charset=utf-8"
  1561. },
  1562. body: JSON.stringify(newRecipe)
  1563. })
  1564. .then((response) => response.json())
  1565. .then((response)=>{
  1566. if(typeof(response) === "string"){
  1567. banner.createError(response);
  1568. }else{
  1569. let recipe = new Recipe(
  1570. response._id,
  1571. response.name,
  1572. response.price,
  1573. response.ingredients,
  1574. merchant,
  1575. );
  1576. merchant.editRecipes([recipe]);
  1577. banner.createNotification("RECIPE CREATED");
  1578. }
  1579. })
  1580. .catch((err)=>{
  1581. banner.createError("SOMETHING WENT WRONG. PLEASE REFRESH THE PAGE");
  1582. })
  1583. .finally(()=>{
  1584. loader.style.display = "none";
  1585. });
  1586. },
  1587. }
  1588. },{}],9:[function(require,module,exports){
  1589. module.exports = {
  1590. display: function(){
  1591. let recipeList = document.getElementById("newTransactionRecipes");
  1592. let template = document.getElementById("createTransaction").content.children[0];
  1593. while(recipeList.children.length > 0){
  1594. recipeList.removeChild(recipeList.firstChild);
  1595. }
  1596. for(let i = 0; i < merchant.recipes.length; i++){
  1597. let recipeDiv = template.cloneNode(true);
  1598. recipeDiv.recipe = merchant.recipes[i];
  1599. recipeList.appendChild(recipeDiv);
  1600. recipeDiv.children[0].innerText = merchant.recipes[i].name;
  1601. }
  1602. openSidebar(document.getElementById("newTransaction"));
  1603. },
  1604. submit: function(){
  1605. let recipeDivs = document.getElementById("newTransactionRecipes");
  1606. let date = document.getElementById("newTransactionDate").valueAsDate;
  1607. if(date > new Date()){
  1608. banner.createError("CANNOT HAVE A DATE IN THE FUTURE");
  1609. return;
  1610. }
  1611. let newTransaction = {
  1612. date: date,
  1613. recipes: []
  1614. };
  1615. for(let i = 0; i < recipeDivs.children.length; i++){
  1616. let quantity = recipeDivs.children[i].children[1].value;
  1617. if(quantity !== "" && quantity > 0){
  1618. newTransaction.recipes.push({
  1619. recipe: recipeDivs.children[i].recipe.id,
  1620. quantity: quantity
  1621. });
  1622. }else if(quantity < 0){
  1623. banner.createError("CANNOT HAVE NEGATIVE VALUES");
  1624. return;
  1625. }
  1626. }
  1627. if(newTransaction.recipes.length > 0){
  1628. let loader = document.getElementById("loaderContainer");
  1629. loader.style.display = "flex";
  1630. fetch("/transaction/create", {
  1631. method: "post",
  1632. headers: {
  1633. "Content-Type": "application/json;charset=utf-8"
  1634. },
  1635. body: JSON.stringify(newTransaction)
  1636. })
  1637. .then(response => response.json())
  1638. .then((response)=>{
  1639. if(typeof(response) === "string"){
  1640. banner.createError(response);
  1641. }else{
  1642. let transaction = new Transaction(
  1643. response._id,
  1644. response.date,
  1645. response.recipes,
  1646. merchant
  1647. );
  1648. merchant.editTransactions(transaction);
  1649. banner.createNotification("NEW TRANSACTION CREATED");
  1650. }
  1651. })
  1652. .catch((err)=>{
  1653. banner.createError("SOMETHING WENT WRONG. PLEASE REFRESH THE PAGE");
  1654. })
  1655. .finally(()=>{
  1656. loader.style.display = "none";
  1657. });
  1658. }
  1659. }
  1660. }
  1661. },{}],10:[function(require,module,exports){
  1662. module.exports = {
  1663. display: function(order){
  1664. openSidebar(document.querySelector("#orderDetails"));
  1665. document.querySelector("#removeOrderBtn").onclick = ()=>{this.remove(order)};
  1666. document.getElementById("orderDetailName").innerText = order.name;
  1667. document.getElementById("orderDetailDate").innerText = order.date.toLocaleDateString("en-US");
  1668. document.getElementById("orderDetailTime").innerText = order.date.toLocaleTimeString("en-US");
  1669. let ingredientList = document.querySelector("#orderIngredients");
  1670. while(ingredientList.children.length > 0){
  1671. ingredientList.removeChild(ingredientList.firstChild);
  1672. }
  1673. let template = document.querySelector("#orderIngredient").content.children[0];
  1674. let grandTotal = 0;
  1675. for(let i = 0; i < order.ingredients.length; i++){
  1676. let ingredientDiv = template.cloneNode(true);
  1677. let price = (order.ingredients[i].quantity * order.ingredients[i].price) / 100;
  1678. grandTotal += price;
  1679. let ingredient = order.ingredients[i].ingredient;
  1680. let priceText = ingredient.convert(order.ingredients[i].quantity).toFixed(2) + " " +
  1681. ingredient.unit.toUpperCase() + " x $" +
  1682. (order.convertPrice(ingredient.unitType, ingredient.unit, order.ingredients[i].price) / 100).toFixed(2);
  1683. ingredientDiv.children[0].innerText = order.ingredients[i].ingredient.name;
  1684. ingredientDiv.children[1].innerText = priceText;
  1685. ingredientDiv.children[2].innerText = `$${price.toFixed(2)}`;
  1686. ingredientList.appendChild(ingredientDiv);
  1687. }
  1688. document.querySelector("#orderTotalPrice p").innerText = `$${grandTotal.toFixed(2)}`;
  1689. },
  1690. remove: function(order){
  1691. let loader = document.getElementById("loaderContainer");
  1692. loader.style.display = "flex";
  1693. fetch(`/order/${order.id}`, {
  1694. method: "DELETE",
  1695. headers: {
  1696. "Content-Type": "application/json;charset=utf-8"
  1697. }
  1698. })
  1699. .then((response) => response.json())
  1700. .then((response)=>{
  1701. if(typeof(response) === "string"){
  1702. banner.createError(response);
  1703. }else{
  1704. merchant.editOrders([order], true);
  1705. banner.createNotification("ORDER REMOVED");
  1706. }
  1707. })
  1708. .catch((err)=>{
  1709. banner.createError("SOMETHING WENT WRONG. PLEASE REFRESH THE PAGE");
  1710. })
  1711. .finally(()=>{
  1712. loader.style.display = "none";
  1713. });
  1714. }
  1715. }
  1716. },{}],11:[function(require,module,exports){
  1717. module.exports = {
  1718. isFetched: false,
  1719. display: async function(){
  1720. if(!this.isFetched){
  1721. let loader = document.getElementById("loaderContainer");
  1722. loader.style.display = "flex";
  1723. fetch("/order", {
  1724. method: "GET",
  1725. headers: {
  1726. "Content-Type": "application/json;charset=utf-8"
  1727. },
  1728. })
  1729. .then((response) => response.json())
  1730. .then((response)=>{
  1731. if(typeof(response) === "string"){
  1732. banner.createError(response);
  1733. }else{
  1734. let newOrders = [];
  1735. for(let i = 0; i < response.length; i++){
  1736. newOrders.push(new Order(
  1737. response[i]._id,
  1738. response[i].name,
  1739. response[i].date,
  1740. response[i].ingredients,
  1741. merchant
  1742. ));
  1743. }
  1744. merchant.editOrders(newOrders);
  1745. this.isFetched = true;
  1746. }
  1747. })
  1748. .catch((err)=>{
  1749. banner.createError("SOMETHING WENT WRONG. TRY REFRESHING THE PAGE");
  1750. })
  1751. .finally(()=>{
  1752. loader.style.display = "none";
  1753. });
  1754. }
  1755. },
  1756. populate: function(){
  1757. let listDiv = document.getElementById("orderList");
  1758. let template = document.getElementById("order").content.children[0];
  1759. let dateDropdown = document.getElementById("dateDropdownOrder");
  1760. let ingredientDropdown = document.getElementById("ingredientDropdown");
  1761. dateDropdown.style.display = "none";
  1762. ingredientDropdown.style.display = "none";
  1763. document.getElementById("dateFilterBtnOrder").onclick = ()=>{this.toggleDropdown(dateDropdown)};
  1764. document.getElementById("ingredientFilterBtn").onclick = ()=>{this.toggleDropdown(ingredientDropdown)};
  1765. for(let i = 0; i < merchant.ingredients.length; i++){
  1766. let checkbox = document.createElement("input");
  1767. checkbox.type = "checkbox";
  1768. checkbox.ingredient = merchant.ingredients[i].ingredient;
  1769. ingredientDropdown.appendChild(checkbox);
  1770. let label = document.createElement("label");
  1771. label.innerText = merchant.ingredients[i].ingredient.name;
  1772. label.for = checkbox;
  1773. ingredientDropdown.appendChild(label);
  1774. let brk = document.createElement("br");
  1775. ingredientDropdown.appendChild(brk);
  1776. }
  1777. while(listDiv.children.length > 0){
  1778. listDiv.removeChild(listDiv.firstChild);
  1779. }
  1780. for(let i = 0; i < merchant.orders.length; i++){
  1781. let row = template.cloneNode(true);
  1782. let totalCost = 0;
  1783. for(let j = 0; j < merchant.orders[i].ingredients.length; j++){
  1784. totalCost += merchant.orders[i].ingredients[j].quantity * merchant.orders[i].ingredients[j].price;
  1785. }
  1786. row.children[0].innerText = merchant.orders[i].name;
  1787. row.children[1].innerText = `${merchant.orders[i].ingredients.length} items`;
  1788. row.children[2].innerText = new Date(merchant.orders[i].date).toLocaleDateString("en-US");
  1789. row.children[3].innerText = `$${(totalCost / 100).toFixed(2)}`;
  1790. row.order = merchant.orders[i];
  1791. row.onclick = ()=>{orderDetailsComp.display(merchant.orders[i])};
  1792. listDiv.appendChild(row);
  1793. }
  1794. },
  1795. submitFilter: function(){
  1796. event.preventDefault();
  1797. let data = {
  1798. startDate: document.getElementById("orderFilDate1").valueAsDate,
  1799. endDate: document.getElementById("orderFilDate2").valueAsDate,
  1800. ingredients: []
  1801. }
  1802. if(data.startDate >= data.endDate){
  1803. banner.createError("START DATE CANNOT BE AFTER END DATE");
  1804. return;
  1805. }
  1806. let ingredientChoices = document.getElementById("ingredientDropdown");
  1807. for(let i = 0; i < ingredientChoices.children.length; i += 3){
  1808. if(ingredientChoices.children[i].checked){
  1809. data.ingredients.push(ingredientChoices.children[i].ingredient.id);
  1810. }
  1811. }
  1812. if(data.ingredients.length === 0){
  1813. for(let i = 0; i < merchant.ingredients.length; i++){
  1814. data.ingredients.push(merchant.ingredients[i].ingredient.id);
  1815. }
  1816. }
  1817. let loader = document.getElementById("loaderContainer");
  1818. loader.style.display = "flex";
  1819. fetch("/order", {
  1820. method: "POST",
  1821. headers: {
  1822. "Content-Type": "application/json;charset=utf-8"
  1823. },
  1824. body: JSON.stringify(data)
  1825. })
  1826. .then((response) => response.json())
  1827. .then((response)=>{
  1828. if(typeof(response) === "string"){
  1829. banner.createError(response);
  1830. }else{
  1831. let orderList = document.getElementById("orderList");
  1832. let template = document.getElementById("order").content.children[0];
  1833. while(orderList.children.length > 0){
  1834. orderList.removeChild(orderList.firstChild);
  1835. }
  1836. for(let i = 0; i < response.length; i++){
  1837. let order = template.cloneNode(true);
  1838. let cost = 0;
  1839. for(let j = 0; j < response[i].ingredients.length; j++){
  1840. cost += (response[i].ingredients[j].price / 100) * response[i].ingredients[j].quantity;
  1841. }
  1842. order.children[0].innerText = response[i].name,
  1843. order.children[1].innerText = `${response[i].ingredients.length} items`;
  1844. order.children[2].innerText = new Date(response[i].date).toLocaleDateString();
  1845. order.children[3].innerText = `$${cost.toFixed(2)}`;
  1846. orderList.appendChild(order);
  1847. }
  1848. }
  1849. })
  1850. .catch((err)=>{
  1851. banner.createError("UNABLE TO DISPLAY THE ORDERS");
  1852. })
  1853. .finally(()=>{
  1854. loader.style.display = "none";
  1855. });
  1856. },
  1857. toggleDropdown: function(dropdown){
  1858. event.preventDefault();
  1859. let polyline = dropdown.parentElement.children[0].children[1].children[0].children[0];
  1860. if(dropdown.style.display === "none"){
  1861. dropdown.style.display = "block";
  1862. polyline.setAttribute("points", "18 15 12 9 6 15");
  1863. }else{
  1864. dropdown.style.display = "none";
  1865. polyline.setAttribute("points", "6 9 12 15 18 9");
  1866. }
  1867. }
  1868. }
  1869. },{}],12:[function(require,module,exports){
  1870. module.exports = {
  1871. isPopulated: false,
  1872. recipeDivList: [],
  1873. display: function(){
  1874. if(!this.isPopulated){
  1875. this.populateRecipes();
  1876. this.isPopulated = true;
  1877. }
  1878. },
  1879. populateRecipes: function(){
  1880. let recipeList = document.getElementById("recipeList");
  1881. let template = document.getElementById("recipe").content.children[0];
  1882. this.recipeDivList = [];
  1883. while(recipeList.children.length > 0){
  1884. recipeList.removeChild(recipeList.firstChild);
  1885. }
  1886. for(let i = 0; i < merchant.recipes.length; i++){
  1887. let recipeDiv = template.cloneNode(true);
  1888. recipeDiv.onclick = ()=>{recipeDetailsComp.display(merchant.recipes[i])};
  1889. recipeDiv._name = merchant.recipes[i].name;
  1890. recipeList.appendChild(recipeDiv);
  1891. recipeDiv.children[0].innerText = merchant.recipes[i].name;
  1892. recipeDiv.children[1].innerText = `$${(merchant.recipes[i].price / 100).toFixed(2)}`;
  1893. this.recipeDivList.push(recipeDiv);
  1894. }
  1895. },
  1896. search: function(){
  1897. let input = document.getElementById("recipeSearch").value.toLowerCase();
  1898. let recipeList = document.getElementById("recipeList");
  1899. let clearButton = document.getElementById("recipeClearButton");
  1900. let matchingRecipes = [];
  1901. for(let i = 0; i < this.recipeDivList.length; i++){
  1902. if(this.recipeDivList[i]._name.toLowerCase().includes(input)){
  1903. matchingRecipes.push(this.recipeDivList[i]);
  1904. }
  1905. }
  1906. while(recipeList.children.length > 0){
  1907. recipeList.removeChild(recipeList.firstChild);
  1908. }
  1909. for(let i = 0; i < matchingRecipes.length; i++){
  1910. recipeList.appendChild(matchingRecipes[i]);
  1911. }
  1912. if(input === ""){
  1913. clearButton.style.display = "none";
  1914. }else{
  1915. clearButton.style.display = "inline";
  1916. }
  1917. },
  1918. clearSorting: function(){
  1919. document.getElementById("recipeSearch").value = "";
  1920. this.search();
  1921. },
  1922. posUpdate: function(){
  1923. let loader = document.getElementById("loaderContainer");
  1924. loader.style.display = "flex";
  1925. fetch("/recipe/update/clover", {
  1926. method: "GET",
  1927. headers: {
  1928. "Content-Type": "application/json;charset=utf-8"
  1929. },
  1930. })
  1931. .then(response => response.json())
  1932. .then((response)=>{
  1933. let newRecipes = [];
  1934. for(let i = 0; i < response.new.length; i++){
  1935. newRecipes.push(new Recipe(
  1936. response.new[i]._id,
  1937. response.new[i].name,
  1938. response.new[i].price,
  1939. merchant,
  1940. []
  1941. ));
  1942. }
  1943. if(newRecipes.length > 0){
  1944. merchant.editRecipes(newRecipes);
  1945. }
  1946. let removeRecipes = [];
  1947. for(let i = 0; i < response.removed.length; i++){
  1948. for(let j = 0; j < merchant.recipes.length; j++){
  1949. if(response.removed[i]._id === merchant.recipes[j].id){
  1950. removeRecipes.push(merchant.recipes[j], true);
  1951. break;
  1952. }
  1953. }
  1954. }
  1955. if(removeRecipes.length > 0){
  1956. merchant.editRecipes(removeRecipes, true);
  1957. }
  1958. })
  1959. .catch((err)=>{
  1960. banner.createError("SOMETHING WENT WRONG. PLEASE REFRESH THE PAGE");
  1961. })
  1962. .finally(()=>{
  1963. loader.style.display = "none";
  1964. });
  1965. }
  1966. }
  1967. },{}],13:[function(require,module,exports){
  1968. module.exports = {
  1969. recipe: {},
  1970. display: function(recipe){
  1971. this.recipe = recipe;
  1972. openSidebar(document.querySelector("#recipeDetails"));
  1973. document.querySelector("#recipeName").style.display = "block";
  1974. document.querySelector("#recipeNameIn").style.display = "none";
  1975. document.querySelector("#recipeDetails h1").innerText = recipe.name;
  1976. let ingredientList = document.querySelector("#recipeIngredientList");
  1977. while(ingredientList.children.length > 0){
  1978. ingredientList.removeChild(ingredientList.firstChild);
  1979. }
  1980. let template = document.querySelector("#recipeIngredient").content.children[0];
  1981. for(let i = 0; i < recipe.ingredients.length; i++){
  1982. ingredientDiv = template.cloneNode(true);
  1983. ingredientDiv.children[0].innerText = recipe.ingredients[i].ingredient.name;
  1984. ingredientDiv.children[2].innerText = `${recipe.ingredients[i].ingredient.convert(recipe.ingredients[i].quantity).toFixed(2)} ${recipe.ingredients[i].ingredient.unit}`;
  1985. ingredientDiv.ingredient = recipe.ingredients[i].ingredient;
  1986. ingredientDiv.name = recipe.ingredients[i].ingredient.name;
  1987. ingredientList.appendChild(ingredientDiv);
  1988. }
  1989. document.querySelector("#addRecIng").style.display = "none";
  1990. let price = document.querySelector("#recipePrice");
  1991. price.children[1].style.display = "block";
  1992. price.children[2].style.display = "none";
  1993. price.children[1].innerText = `$${(recipe.price / 100).toFixed(2)}`;
  1994. document.querySelector("#recipeUpdate").style.display = "none";
  1995. },
  1996. edit: function(){
  1997. let ingredientDivs = document.querySelector("#recipeIngredientList");
  1998. if(merchant.pos === "none"){
  1999. let name = document.querySelector("#recipeName");
  2000. let nameIn = document.querySelector("#recipeNameIn");
  2001. name.style.display = "none";
  2002. nameIn.style.display = "block";
  2003. nameIn.value = this.recipe.name;
  2004. let price = document.querySelector("#recipePrice");
  2005. price.children[1].style.display = "none";
  2006. price.children[2].style.display = "block";
  2007. price.children[2].value = parseFloat((this.recipe.price / 100).toFixed(2));
  2008. }
  2009. for(let i = 0; i < ingredientDivs.children.length; i++){
  2010. let div = ingredientDivs.children[i];
  2011. div.children[2].innerText = this.recipe.ingredients[i].ingredient.unit;
  2012. div.children[1].style.display = "block";
  2013. div.children[1].value = this.recipe.ingredients[i].ingredient.convert(this.recipe.ingredients[i].quantity).toFixed(2);
  2014. div.children[3].style.display = "block";
  2015. div.children[3].onclick = ()=>{div.parentElement.removeChild(div)};
  2016. }
  2017. document.querySelector("#addRecIng").style.display = "flex";
  2018. document.querySelector("#recipeUpdate").style.display = "flex";
  2019. },
  2020. update: function(){
  2021. this.recipe.name = document.querySelector("#recipeNameIn").value || this.recipe.name;
  2022. this.recipe.price = Math.round((document.querySelector("#recipePrice").children[2].value * 100)) || this.recipe.price;
  2023. this.recipe.ingredients = [];
  2024. let divs = document.querySelector("#recipeIngredientList").children;
  2025. for(let i = 0; i < divs.length; i++){
  2026. if(divs[i].name === "new"){
  2027. let select = divs[i].children[0];
  2028. this.recipe.ingredients.push({
  2029. ingredient: select.options[select.selectedIndex].ingredient,
  2030. quantity: convertToMain(select.options[select.selectedIndex].ingredient.unit, divs[i].children[1].value)
  2031. });
  2032. }else{
  2033. this.recipe.ingredients.push({
  2034. ingredient: divs[i].ingredient,
  2035. quantity: convertToMain(divs[i].ingredient.unit, divs[i].children[1].value)
  2036. });
  2037. }
  2038. }
  2039. let data = {
  2040. id: this.recipe.id,
  2041. name: this.recipe.name,
  2042. price: this.recipe.price,
  2043. ingredients: []
  2044. }
  2045. for(let i = 0; i < this.recipe.ingredients.length; i++){
  2046. data.ingredients.push({
  2047. ingredient: this.recipe.ingredients[i].ingredient.id,
  2048. quantity: this.recipe.ingredients[i].quantity
  2049. });
  2050. }
  2051. let loader = document.getElementById("loaderContainer");
  2052. loader.style.display = "flex";
  2053. fetch("/recipe/update", {
  2054. method: "PUT",
  2055. headers: {
  2056. "Content-Type": "application/json;charset=utf-8"
  2057. },
  2058. body: JSON.stringify(data)
  2059. })
  2060. .then((response) => response.json())
  2061. .then((response)=>{
  2062. if(typeof(response) === "string"){
  2063. banner.createError(response);
  2064. }else{
  2065. merchant.editRecipes([this.recipe]);
  2066. banner.createNotification("RECIPE UPDATE");
  2067. }
  2068. })
  2069. .catch((err)=>{
  2070. banner.createError("SOMETHING WENT WRONG. PLEASE REFRESH THE PAGE");
  2071. })
  2072. .finally(()=>{
  2073. loader.style.display = "none";
  2074. });
  2075. },
  2076. remove: function(){
  2077. fetch(`/merchant/recipes/remove/${this.recipe.id}`, {
  2078. method: "DELETE"
  2079. })
  2080. .then((response) => response.json())
  2081. .then((response)=>{
  2082. if(typeof(response) === "string"){
  2083. banner.createError(response);
  2084. }else{
  2085. merchant.editRecipes([this.recipe], true);
  2086. banner.createNotification("RECIPE REMOVED");
  2087. }
  2088. })
  2089. .catch((err)=>{
  2090. banner.createError("SOMETHING WENT WRONG. PLEASE REFRESH THE PAGE");
  2091. });
  2092. },
  2093. displayAddIngredient: function(){
  2094. let template = document.querySelector("#addRecIngredient").content.children[0].cloneNode(true);
  2095. template.name = "new";
  2096. document.querySelector("#recipeIngredientList").appendChild(template);
  2097. let categories = merchant.categorizeIngredients();
  2098. for(let i = 0; i < categories.length; i++){
  2099. let optGroup = document.createElement("optgroup");
  2100. optGroup.label = categories[i].name;
  2101. template.children[0].appendChild(optGroup);
  2102. for(let j = 0; j < categories[i].ingredients.length; j++){
  2103. let option = document.createElement("option");
  2104. option.innerText = `${categories[i].ingredients[j].ingredient.name} (${categories[i].ingredients[j].ingredient.unit})`;
  2105. option.ingredient = categories[i].ingredients[j].ingredient;
  2106. optGroup.appendChild(option);
  2107. }
  2108. }
  2109. }
  2110. }
  2111. },{}],14:[function(require,module,exports){
  2112. module.exports = {
  2113. transaction: {},
  2114. display: function(transaction){
  2115. this.transaction = transaction;
  2116. let recipeList = document.getElementById("transactionRecipes");
  2117. let template = document.getElementById("transactionRecipe").content.children[0];
  2118. let totalRecipes = 0;
  2119. let totalPrice = 0;
  2120. while(recipeList.children.length > 0){
  2121. recipeList.removeChild(recipeList.firstChild);
  2122. }
  2123. for(let i = 0; i < transaction.recipes.length; i++){
  2124. let recipe = template.cloneNode(true);
  2125. let price = transaction.recipes[i].quantity * transaction.recipes[i].recipe.price;
  2126. recipe.children[0].innerText = transaction.recipes[i].recipe.name;
  2127. recipe.children[1].innerText = `${transaction.recipes[i].quantity} x $${parseFloat(transaction.recipes[i].recipe.price / 100).toFixed(2)}`;
  2128. recipe.children[2].innerText = `$${(price / 100).toFixed(2)}`;
  2129. recipeList.appendChild(recipe);
  2130. totalRecipes += transaction.recipes[i].quantity;
  2131. totalPrice += price;
  2132. }
  2133. let months = ["January", "Fecbruary", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
  2134. let days = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"];
  2135. let dateString = `${days[transaction.date.getDay()]}, ${months[transaction.date.getMonth()]} ${transaction.date.getDate()}, ${transaction.date.getFullYear()}`;
  2136. document.getElementById("transactionDate").innerText = dateString;
  2137. document.getElementById("transactionTime").innerText = transaction.date.toLocaleTimeString();
  2138. document.getElementById("totalRecipes").innerText = `${totalRecipes} recipes`;
  2139. document.getElementById("totalPrice").innerText = `$${(totalPrice / 100).toFixed(2)}`;
  2140. openSidebar(document.getElementById("transactionDetails"));
  2141. },
  2142. remove: function(){
  2143. let loader = document.getElementById("loaderContainer");
  2144. loader.style.display = "flex";
  2145. fetch(`/transaction/${this.transaction.id}`, {
  2146. method: "delete",
  2147. headers: {
  2148. "Content-Type": "application/json;charset=utf-8"
  2149. },
  2150. })
  2151. .then(response => response.json())
  2152. .then((response)=>{
  2153. if(typeof(response) === "string"){
  2154. banner.createError(response);
  2155. }else{
  2156. merchant.editTransactions(this.transaction, true);
  2157. banner.createNotification("TRANSACTION REMOVED");
  2158. }
  2159. })
  2160. .catch((err)=>{
  2161. banner.createError("SOMETHING WENT WRONG. PLEASE REFRESH THE PAGE");
  2162. })
  2163. .finally(()=>{
  2164. loader.style.display = "none";
  2165. });
  2166. },
  2167. }
  2168. },{}],15:[function(require,module,exports){
  2169. module.exports = {
  2170. isPopulated: false,
  2171. display: function(){
  2172. if(!this.isPopulated){
  2173. let transactionsList = document.getElementById("transactionsList");
  2174. let dateDropdown = document.getElementById("dateDropdown");
  2175. let recipeDropdown = document.getElementById("recipeDropDown");
  2176. let template = document.getElementById("transaction").content.children[0];
  2177. let now = new Date();
  2178. let monthAgo = new Date(now.getFullYear(), now.getMonth() - 1, now.getDate());
  2179. document.getElementById("transFilDate1").valueAsDate = monthAgo;
  2180. document.getElementById("transFilDate2").valueAsDate = now;
  2181. dateDropdown.style.display = "none";
  2182. recipeDropdown.style.display = "none";
  2183. document.getElementById("dateFilterBtn").onclick = ()=>{this.toggleDropdown(dateDropdown)};
  2184. document.getElementById("recipeFilterBtn").onclick = ()=>{this.toggleDropdown(recipeDropdown)};
  2185. while(recipeDropdown.children.length > 0){
  2186. recipeDropdown.removeChild(recipeDropdown.firstChild);
  2187. }
  2188. for(let i = 0; i < merchant.recipes.length; i++){
  2189. let checkbox = document.createElement("input");
  2190. checkbox.type = "checkbox";
  2191. checkbox.recipe = merchant.recipes[i];
  2192. recipeDropdown.appendChild(checkbox);
  2193. let label = document.createElement("label");
  2194. label.innerText = merchant.recipes[i].name;
  2195. label.for = checkbox;
  2196. recipeDropdown.appendChild(label);
  2197. let brk = document.createElement("br");
  2198. recipeDropdown.appendChild(brk);
  2199. }
  2200. while(transactionsList.children.length > 0){
  2201. transactionsList.removeChild(transactionsList.firstChild);
  2202. }
  2203. let i = 0
  2204. while(i < merchant.transactions.length && i < 100){
  2205. let transactionDiv = template.cloneNode(true);
  2206. let transaction = merchant.transactions[i];
  2207. transactionDiv.onclick = ()=>{transactionDetailsComp.display(transaction)};
  2208. transactionsList.appendChild(transactionDiv);
  2209. let totalRecipes = 0;
  2210. let totalPrice = 0;
  2211. for(let j = 0; j < merchant.transactions[i].recipes.length; j++){
  2212. totalRecipes += merchant.transactions[i].recipes[j].quantity;
  2213. totalPrice += merchant.transactions[i].recipes[j].recipe.price * merchant.transactions[i].recipes[j].quantity;
  2214. }
  2215. transactionDiv.children[0].innerText = `${merchant.transactions[i].date.toLocaleDateString()} ${merchant.transactions[i].date.toLocaleTimeString()}`;
  2216. transactionDiv.children[1].innerText = `${totalRecipes} recipes sold`;
  2217. transactionDiv.children[2].innerText = `$${(totalPrice / 100).toFixed(2)}`;
  2218. i++;
  2219. }
  2220. this.isPopulated = true;
  2221. }
  2222. },
  2223. submitFilter: function(){
  2224. event.preventDefault();
  2225. let data = {
  2226. startDate: document.getElementById("transFilDate1").valueAsDate,
  2227. endDate: document.getElementById("transFilDate2").valueAsDate,
  2228. recipes: []
  2229. }
  2230. if(data.startDate >= data.endDate){
  2231. banner.createError("START DATE CANNOT BE AFTER END DATE");
  2232. return;
  2233. }
  2234. let recipeChoices = document.getElementById("recipeDropDown");
  2235. for(let i = 0; i < recipeChoices.children.length; i += 3){
  2236. if(recipeChoices.children[i].checked){
  2237. data.recipes.push(recipeChoices.children[i].recipe.id);
  2238. }
  2239. }
  2240. if(data.recipes.length === 0){
  2241. for(let i = 0; i < merchant.recipes.length; i++){
  2242. data.recipes.push(merchant.recipes[i].id);
  2243. }
  2244. }
  2245. let loader = document.getElementById("loaderContainer");
  2246. loader.style.display = "flex";
  2247. fetch("/transaction", {
  2248. method: "POST",
  2249. headers: {
  2250. "Content-Type": "application/json;charset=utf-8"
  2251. },
  2252. body: JSON.stringify(data)
  2253. })
  2254. .then((response) => response.json())
  2255. .then((response)=>{
  2256. if(typeof(response) === "string"){
  2257. banner.createError(response);
  2258. }else{
  2259. let transactionList = document.getElementById("transactionsList");
  2260. let template = document.getElementById("transaction").content.children[0];
  2261. while(transactionList.children.length > 0){
  2262. transactionList.removeChild(transactionList.firstChild);
  2263. }
  2264. for(let i = 0; i < response.length; i++){
  2265. let transactionDiv = template.cloneNode(true);
  2266. let recipeCount = 0;
  2267. let cost = 0;
  2268. let transaction = new Transaction(
  2269. response[i]._id,
  2270. response[i].date,
  2271. response[i].recipes,
  2272. merchant
  2273. );
  2274. for(let j = 0; j < transaction.recipes.length; j++){
  2275. recipeCount += transaction.recipes[j].quantity;
  2276. cost += transaction.recipes[j].quantity * transaction.recipes[j].recipe.price;
  2277. }
  2278. transactionDiv.children[0].innerText = `${transaction.date.toLocaleDateString()} ${transaction.date.toLocaleTimeString()}`;
  2279. transactionDiv.children[1].innerText = `${recipeCount} recipes sold`;
  2280. transactionDiv.children[2].innerText = `$${(cost / 100).toFixed(2)}`;
  2281. transactionDiv.onclick = ()=>{transactionDetailsComp.display(transaction)};
  2282. transactionList.appendChild(transactionDiv);
  2283. }
  2284. }
  2285. })
  2286. .catch((err)=>{
  2287. banner.createError("UNABLE TO DISPLAY THE TRANSACTIONS");
  2288. })
  2289. .finally(()=>{
  2290. loader.style.display = "none";
  2291. });
  2292. },
  2293. toggleDropdown: function(dropdown){
  2294. event.preventDefault();
  2295. let polyline = dropdown.parentElement.children[0].children[1].children[0].children[0];
  2296. if(dropdown.style.display === "none"){
  2297. dropdown.style.display = "block";
  2298. polyline.setAttribute("points", "18 15 12 9 6 15");
  2299. }else{
  2300. dropdown.style.display = "none";
  2301. polyline.setAttribute("points", "6 9 12 15 18 9");
  2302. }
  2303. }
  2304. }
  2305. },{}],16:[function(require,module,exports){
  2306. //Creates a line graph within a canvas
  2307. //Will expand or shrink to the size of the canvas
  2308. //Inputs:
  2309. // canvas = the canvas that you would like to draw on
  2310. // yName = a string for the name of the Y axis
  2311. // xName = a string for the name of the X axis
  2312. class LineGraph{
  2313. constructor(canvas){
  2314. this.canvas = canvas;
  2315. this.context = canvas.getContext("2d");
  2316. this.left = canvas.clientWidth - (canvas.clientWidth * 0.95);
  2317. this.right = canvas.clientWidth * 1;
  2318. this.top = canvas.clientHeight - (canvas.clientHeight * 1);
  2319. this.bottom = canvas.clientHeight * 0.85;
  2320. this.data = [];
  2321. this.max = 0;
  2322. this.xRange = [];
  2323. this.colors = [];
  2324. this.colorIndex = 0;
  2325. for(let i = 0; i < 100; i++){
  2326. let redRand = Math.floor(Math.random() * 200);
  2327. let greenRand = Math.floor(Math.random() * 200);
  2328. let blueRand = Math.floor(Math.random() * 200);
  2329. this.colors.push(`rgb(${redRand}, ${greenRand}, ${blueRand})`);
  2330. }
  2331. }
  2332. //Add a dataset to the graph to draw
  2333. //Inputs:
  2334. // data = array containing list of numbers as the data points for the graph
  2335. // data[0] will be on the left. data[data.length-1] will be on the right.
  2336. // xRange = array containing two elements, start and end for x axis data (currently only dates)
  2337. // name = string name for the line. Used for display and finding lines. Each must be unique
  2338. addData(data, xRange, name){
  2339. data = {
  2340. set: data,
  2341. colorIndex: this.colorIndex,
  2342. name: name
  2343. }
  2344. this.colorIndex++;
  2345. this.data.push(data);
  2346. let isChange = false;
  2347. for(let point of data.set){
  2348. if(point > this.max){
  2349. this.max = point;
  2350. this.verticalMultiplier = (this.bottom - this.top) / this.max;
  2351. this.horizontalMultiplier = (this.right - this.left) / (data.set.length - 1);
  2352. isChange = true;
  2353. }
  2354. }
  2355. if(this.xRange.length === 0){
  2356. this.xRange = xRange;
  2357. isChange = true;
  2358. }else{
  2359. if(xRange[0] < this.xRange[0]){
  2360. this.xRange[0] = xRange[0];
  2361. isChange = true;
  2362. }
  2363. if(xRange[1] > this.xRange[1]){
  2364. this.xRange[1] = xRange[1];
  2365. isChange = true;
  2366. }
  2367. }
  2368. if(isChange){
  2369. this.drawGraph();
  2370. }else{
  2371. this.drawLine(data);
  2372. }
  2373. }
  2374. //Removes a single data set from the graph and its line
  2375. //Inputs:
  2376. // id = the unique identifier of the data set that was passed in with addData function
  2377. removeData(name){
  2378. for(let i = 0; i < this.data.length; i++){
  2379. if(this.data[i].name === name){
  2380. this.data.splice(i, 1);
  2381. break;
  2382. }
  2383. }
  2384. this.drawGraph();
  2385. }
  2386. //Completely clears all data
  2387. //Does not delete the current graph displaying
  2388. clearData(){
  2389. this.max = 0;
  2390. this.data = [];
  2391. this.xRange = [];
  2392. }
  2393. addTitle(title){
  2394. this.top = this.canvas.clientHeight - (this.canvas.clientHeight * 0.9);
  2395. this.title = title;
  2396. }
  2397. /**********
  2398. *********PRIVATE*********
  2399. **********/
  2400. drawGraph(){
  2401. this.context.clearRect(0, 0, this.canvas.width, this.canvas.height);
  2402. this.drawYAxis();
  2403. this.drawXAxis();
  2404. for(let dataSet of this.data){
  2405. this.drawLine(dataSet);
  2406. }
  2407. if(this.title){
  2408. this.context.font = "25px Saira";
  2409. let xLocation = ((this.right - this.left) / 2) - (this.context.measureText(this.title).width / 2);
  2410. this.context.fillText(this.title, xLocation, this.top - 10);
  2411. }
  2412. }
  2413. drawLine(data){
  2414. for(let i = 0; i < data.set.length - 1; i++){
  2415. this.context.beginPath();
  2416. this.context.moveTo(this.left + (this.horizontalMultiplier * i), this.bottom - (this.verticalMultiplier * data.set[i]));
  2417. this.context.lineTo(this.left + (this.horizontalMultiplier * (i + 1)), this.bottom - (this.verticalMultiplier * data.set[i + 1]));
  2418. this.context.strokeStyle = this.colors[data.colorIndex];
  2419. this.context.lineWidth = 2;
  2420. this.context.stroke();
  2421. }
  2422. this.context.strokeStyle = "black";
  2423. if(this.data.length > 1){
  2424. this.drawLegend(data.colorIndex, data.name);
  2425. }
  2426. }
  2427. drawXAxis(){
  2428. this.context.beginPath();
  2429. this.context.moveTo(this.left, this.bottom);
  2430. this.context.lineTo(this.right, this.bottom);
  2431. this.context.lineWidth = 4;
  2432. this.context.stroke();
  2433. this.context.setLineDash([5, 10]);
  2434. this.context.font = "10px Arial";
  2435. this.context.lineWidth = 1;
  2436. if(Object.prototype.toString.call(this.xRange[0]) === '[object Date]'){
  2437. let diff = Math.abs(Math.floor((Date.UTC(this.xRange[0].getFullYear(), this.xRange[0].getMonth(), this.xRange[0].getDate()) - Date.UTC(this.xRange[1].getFullYear(), this.xRange[1].getMonth(), this.xRange[1].getDate())) / (1000 * 60 * 60 * 24))) + 1;
  2438. let showDate = new Date(this.xRange[0]);
  2439. for(let i = 0; i < diff; i += Math.floor(diff / 10)){
  2440. this.context.fillText(showDate.toLocaleDateString("en-US", {month: "short", day: "numeric", year: "2-digit"}), this.left + (this.horizontalMultiplier * i) - 20, this.bottom + 15);
  2441. if(i !== 0){
  2442. this.context.beginPath()
  2443. this.context.moveTo(this.left + (this.horizontalMultiplier * i), this.bottom);
  2444. this.context.lineTo(this.left + (this.horizontalMultiplier * i), this.top);
  2445. this.context.strokeStyle = "#a5a5a5";
  2446. this.context.stroke();
  2447. }
  2448. showDate.setDate(showDate.getDate() + Math.abs(diff / 10));
  2449. }
  2450. }
  2451. this.context.strokeStyle = "black";
  2452. this.context.setLineDash([]);
  2453. }
  2454. drawYAxis(){
  2455. this.context.beginPath();
  2456. this.context.moveTo(this.left, this.top);
  2457. this.context.lineTo(this.left, this.bottom);
  2458. this.context.lineWidth = 2;
  2459. this.context.stroke();
  2460. this.context.setLineDash([5, 10]);
  2461. this.context.font = "10px Arial";
  2462. this.context.lineWidth = 1;
  2463. let axisNum = 0;
  2464. let verticalIncrement = (this.bottom - this.top) / 10;
  2465. let verticalOffset = 0;
  2466. do{
  2467. this.context.fillText(Math.round(axisNum).toString(), this.left - 20, this.bottom - verticalOffset + 3);
  2468. this.context.beginPath();
  2469. this.context.moveTo(this.left, this.bottom - verticalOffset);
  2470. this.context.lineTo(this.right, this.bottom - verticalOffset);
  2471. this.context.strokeStyle = "#a5a5a5";
  2472. this.context.stroke();
  2473. verticalOffset += verticalIncrement;
  2474. axisNum += this.max / 10;
  2475. }while(verticalOffset <= (this.bottom - this.top));
  2476. this.context.strokeStyle = "black";
  2477. this.context.setLineDash([]);
  2478. }
  2479. drawLegend(colorIndex, name){
  2480. let verticalOffset;
  2481. for(let i = 0; i < this.data.length; i++){
  2482. if(this.data[i].name === name){
  2483. verticalOffset = i * 25;
  2484. break;
  2485. }
  2486. }
  2487. this.context.beginPath();
  2488. this.context.fillStyle = this.colors[colorIndex];
  2489. this.context.fillRect(this.right + 50, this.top + 50 + verticalOffset, 10, 10);
  2490. this.context.stroke();
  2491. this.context.font = "15px Arial";
  2492. this.context.fillText(name, this.right + 65, this.top + 60 + verticalOffset);
  2493. this.context.fillStyle = "black";
  2494. }
  2495. }
  2496. class HorizontalBarGraph{
  2497. constructor(canvas){
  2498. this.canvas = canvas;
  2499. this.context = canvas.getContext("2d");
  2500. this.left = 0;
  2501. this.right = canvas.clientWidth;
  2502. this.top = canvas.clientHeight - (canvas.clientHeight * 0.99);
  2503. this.bottom = canvas.clientHeight;
  2504. this.data = [];
  2505. this.max = 0;
  2506. }
  2507. //Adds an array of data points to the chart
  2508. //All data is removed and redrawn when called
  2509. //Must pass in all data points
  2510. //Inputs:
  2511. // dataArray: array of objects
  2512. // num: number for the actual data
  2513. // label: text to display on bar
  2514. addData(dataArray){
  2515. this.context.clearRect(0, 0, this.canvas.width, this.canvas.height);
  2516. for(let point of dataArray){
  2517. if(point.num > this.max){
  2518. this.max = point.num;
  2519. }
  2520. this.data.push(point);
  2521. }
  2522. this.drawGraph();
  2523. }
  2524. drawGraph(){
  2525. let barHeight = ((this.bottom - this.top) / this.data.length) - 2;
  2526. for(let i = 0; i < this.data.length; i++){
  2527. let topLocation = this.top + (i * barHeight) + 5;
  2528. let width = (this.right - this.left) * (this.data[i].num / this.max);
  2529. if(this.data[i].num >= this.max){
  2530. this.context.fillStyle = "rgb(255, 99, 107)";
  2531. }else{
  2532. this.context.fillStyle = "rgb(179, 191, 209)";
  2533. }
  2534. this.context.beginPath();
  2535. this.context.fillRect(this.left, topLocation, width, barHeight - 5);
  2536. this.context.stroke();
  2537. let textLocation = 15;
  2538. this.context.font = "12px Saira";
  2539. this.context.fillStyle = "black";
  2540. this.context.fillText(this.data[i].label, textLocation, (this.top) + (i * barHeight) + (barHeight / 1.5));
  2541. }
  2542. }
  2543. }
  2544. module.exports = {
  2545. LineGraph: LineGraph,
  2546. HorizontalBarGraph: HorizontalBarGraph
  2547. }
  2548. },{}]},{},[2]);