bundle.js 109 KB

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