bundle.js 111 KB

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