bundle.js 112 KB

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