bundle.js 111 KB

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