bundle.js 116 KB

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