bundle.js 111 KB

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