bundle.js 115 KB

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