bundle.js 108 KB

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