bundle.js 113 KB

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