bundle.js 107 KB

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