bundle.js 108 KB

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