bundle.js 111 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033
  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 = ()=>{controller.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. case "recipeDetails":
  729. recipeDetails.display(data);
  730. break;
  731. case "newRecipe":
  732. newRecipe.display();
  733. break;
  734. case "orderDetails":
  735. orderDetails.display(data);
  736. break;
  737. case "newOrder":
  738. newOrder.display();
  739. break;
  740. case "transactionDetails":
  741. transactionDetails.display(data);
  742. break;
  743. case "newTransaction":
  744. newTransaction.display();
  745. break;
  746. }
  747. if(window.screen.availWidth <= 1000){
  748. document.querySelector(".contentBlock").style.display = "none";
  749. document.getElementById("mobileMenuSelector").style.display = "none";
  750. document.getElementById("sidebarCloser").style.display = "block";
  751. }
  752. },
  753. closeSidebar: function(){
  754. let sidebar = document.querySelector("#sidebarDiv");
  755. for(let i = 0; i < sidebar.children.length; i++){
  756. sidebar.children[i].style.display = "none";
  757. }
  758. sidebar.classList = "sidebarHide";
  759. if(window.screen.availWidth <= 1000){
  760. document.querySelector(".contentBlock").style.display = "flex";
  761. document.getElementById("mobileMenuSelector").style.display = "block";
  762. document.getElementById("sidebarCloser").style.display = "none";
  763. }
  764. },
  765. changeMenu: function(){
  766. let menu = document.querySelector(".menu");
  767. let buttons = document.querySelectorAll(".menuButton");
  768. if(!menu.classList.contains("menuMinimized")){
  769. menu.classList = "menu menuMinimized";
  770. for(let button of buttons){
  771. button.children[1].style.display = "none";
  772. }
  773. document.querySelector("#max").style.display = "none";
  774. document.querySelector("#min").style.display = "flex";
  775. }else if(menu.classList.contains("menuMinimized")){
  776. menu.classList = "menu";
  777. for(let button of buttons){
  778. button.children[1].style.display = "block";
  779. }
  780. setTimeout(()=>{
  781. document.querySelector("#max").style.display = "flex";
  782. document.querySelector("#min").style.display = "none";
  783. }, 150);
  784. }
  785. },
  786. openMenu: function(){
  787. document.getElementById("menu").style.display = "flex";
  788. document.querySelector(".contentBlock").style.display = "none";
  789. document.getElementById("mobileMenuSelector").onclick = ()=>{this.closeMenu()};
  790. },
  791. closeMenu: function(){
  792. document.getElementById("menu").style.display = "none";
  793. document.querySelector(".contentBlock").style.display = "flex";
  794. document.getElementById("mobileMenuSelector").onclick = ()=>{this.openMenu()};
  795. },
  796. convertToMain: function(unit, quantity){
  797. let converted = 0;
  798. if(merchant.units.mass.includes(unit)){
  799. switch(unit){
  800. case "g": converted = quantity; break;
  801. case "kg": converted = quantity * 1000; break;
  802. case "oz": converted = quantity * 28.3495; break;
  803. case "lb": converted = quantity * 453.5924; break;
  804. }
  805. }else if(merchant.units.volume.includes(unit)){
  806. switch(unit){
  807. case "ml": converted = quantity / 1000; break;
  808. case "l": converted = quantity; break;
  809. case "tsp": converted = quantity / 202.8842; break;
  810. case "tbsp": converted = quantity / 67.6278; break;
  811. case "ozfl": converted = quantity / 33.8141; break;
  812. case "cup": converted = quantity / 4.1667; break;
  813. case "pt": converted = quantity / 2.1134; break;
  814. case "qt": converted = quantity / 1.0567; break;
  815. case "gal": converted = quantity * 3.7854; break;
  816. }
  817. }else if(merchant.units.length.includes(unit)){
  818. switch(unit){
  819. case "mm": converted = quantity / 1000; break;
  820. case "cm": converted = quantity / 100; break;
  821. case "m": converted = quantity; break;
  822. case "in": converted = quantity / 39.3701; break;
  823. case "ft": converted = quantity / 3.2808; break;
  824. }
  825. }else{
  826. converted = quantity;
  827. }
  828. return converted;
  829. },
  830. /*
  831. Sets certain strands to repopulate everything the next time it is opened
  832. Use for when any data is changed
  833. item = whatever is being updated
  834. */
  835. updateData: function(item){
  836. switch(item){
  837. case "ingredient":
  838. home.drawInventoryCheckCard();
  839. ingredients.populateByProperty("category");
  840. addIngredients.isPopulated = false;
  841. break;
  842. case "recipe":
  843. transactions.isPopulated = false;
  844. recipeBook.populateRecipes();
  845. break;
  846. case "order":
  847. orders.populate();
  848. break;
  849. case "transaction":
  850. transactions.isPopulated = false;
  851. transaction.display();
  852. break;
  853. case "unit":
  854. home.isPopulated = false;
  855. ingredients.populateByProperty("category");
  856. document.getElementById("ingredientStock").innerText = `${this.ingredient.ingredient.convert(this.ingredient.quantity).toFixed(2)} ${this.ingredient.ingredient.unit.toUpperCase()}`;
  857. break;
  858. }
  859. }
  860. }
  861. if(window.screen.availWidth > 1000 && window.screen.availWidth <= 1400){
  862. this.changeMenu();
  863. document.getElementById("menuShifter2").style.display = "none";
  864. }
  865. controller.openStrand("home");
  866. },{"./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){
  867. module.exports = {
  868. isPopulated: false,
  869. graph: {},
  870. display: function(){
  871. if(!this.isPopulated){
  872. this.drawRevenueCard();
  873. this.drawRevenueGraph();
  874. this.drawInventoryCheckCard();
  875. this.drawPopularCard();
  876. this.isPopulated = true;
  877. }
  878. },
  879. drawRevenueCard: function(){
  880. let today = new Date();
  881. let firstOfMonth = new Date(today.getFullYear(), today.getMonth(), 1);
  882. let firstOfLastMonth = new Date(today.getFullYear(), today.getMonth() - 1, 1);
  883. let lastMonthtoDay = new Date(new Date().setMonth(today.getMonth() - 1));
  884. let revenueThisMonth = merchant.revenue(merchant.transactionIndices(firstOfMonth));
  885. let revenueLastmonthToDay = merchant.revenue(merchant.transactionIndices(firstOfLastMonth, lastMonthtoDay));
  886. document.querySelector("#revenue").innerText = `$${revenueThisMonth.toLocaleString("en")}`;
  887. let revenueChange = ((revenueThisMonth - revenueLastmonthToDay) / revenueLastmonthToDay) * 100;
  888. let img = "";
  889. if(revenueChange >= 0){
  890. img = "/shared/images/upArrow.png";
  891. }else{
  892. img = "/shared/images/downArrow.png";
  893. }
  894. document.querySelector("#revenueChange p").innerText = `${Math.abs(revenueChange).toFixed(2)}% vs last month`;
  895. document.querySelector("#revenueChange img").src = img;
  896. },
  897. drawRevenueGraph: function(){
  898. let graphCanvas = document.querySelector("#graphCanvas");
  899. let today = new Date();
  900. graphCanvas.height = graphCanvas.parentElement.clientHeight;
  901. graphCanvas.width = graphCanvas.parentElement.clientWidth;
  902. let LineGraph = require("../../shared/graphs.js").LineGraph;
  903. this.graph = new LineGraph(graphCanvas);
  904. this.graph.addTitle("Revenue");
  905. let thirtyAgo = new Date(today);
  906. thirtyAgo.setDate(today.getDate() - 29);
  907. let data = merchant.graphDailyRevenue(merchant.transactionIndices(thirtyAgo));
  908. if(data){
  909. this.graph.addData(
  910. data,
  911. [thirtyAgo, new Date()],
  912. "Revenue"
  913. );
  914. }else{
  915. document.querySelector("#graphCanvas").style.display = "none";
  916. let notice = document.createElement("h1");
  917. notice.innerText = "NO DATA YET";
  918. notice.classList = "notice";
  919. document.querySelector("#graphCard").appendChild(notice);
  920. }
  921. },
  922. drawInventoryCheckCard: function(){
  923. let num;
  924. if(merchant.ingredients.length < 5){
  925. num = merchant.ingredients.length;
  926. }else{
  927. num = 5;
  928. }
  929. let rands = [];
  930. for(let i = 0; i < num; i++){
  931. let rand = Math.floor(Math.random() * merchant.ingredients.length);
  932. if(rands.includes(rand)){
  933. i--;
  934. }else{
  935. rands[i] = rand;
  936. }
  937. }
  938. let ul = document.querySelector("#inventoryCheckCard ul");
  939. let template = document.querySelector("#ingredientCheck").content.children[0];
  940. while(ul.children.length > 0){
  941. ul.removeChild(ul.firstChild);
  942. }
  943. for(let i = 0; i < rands.length; i++){
  944. let ingredientCheck = template.cloneNode(true);
  945. let input = ingredientCheck.children[1].children[1];
  946. ingredientCheck.ingredient = merchant.ingredients[rands[i]];
  947. ingredientCheck.children[0].innerText = merchant.ingredients[rands[i]].ingredient.name;
  948. ingredientCheck.children[1].children[0].onclick = ()=>{input.value--};
  949. input.value = merchant.ingredients[rands[i]].quantity.toFixed(2);
  950. ingredientCheck.children[1].children[2].onclick = ()=>{input.value++}
  951. ingredientCheck.children[2].innerText = merchant.ingredients[rands[i]].ingredient.unit.toUpperCase();
  952. ul.appendChild(ingredientCheck);
  953. }
  954. document.getElementById("inventoryCheck").onclick = ()=>{this.submitInventoryCheck()};
  955. },
  956. drawPopularCard: function(){
  957. let dataArray = [];
  958. let now = new Date();
  959. let thisMonth = new Date(now.getFullYear(), now.getMonth(), 1);
  960. let ingredientList = merchant.ingredientsSold(merchant.transactionIndices(thisMonth));
  961. if(ingredientList !== false){
  962. window.ingredientList = [...ingredientList];
  963. let iterations = (ingredientList.length < 5) ? ingredientList.length : 5;
  964. for(let i = 0; i < iterations; i++){
  965. try{
  966. let max = ingredientList[0].quantity;
  967. let index = 0;
  968. for(let j = 0; j < ingredientList.length; j++){
  969. if(ingredientList[j].quantity > max){
  970. max = ingredientList[j].quantity;
  971. index = j;
  972. }
  973. }
  974. dataArray.push({
  975. num: max,
  976. label: ingredientList[index].ingredient.name + ": " +
  977. ingredientList[index].ingredient.convert(ingredientList[index].quantity).toFixed(2) +
  978. " " + ingredientList[index].ingredient.unit
  979. });
  980. ingredientList.splice(index, 1);
  981. }catch(err){
  982. break;
  983. }
  984. }
  985. let thisCanvas = document.getElementById("popularCanvas");
  986. thisCanvas.width = thisCanvas.parentElement.offsetWidth * 0.8;
  987. thisCanvas.height = thisCanvas.parentElement.offsetHeight * 0.8;
  988. let HorizontalBarGraph = require("../../shared/graphs.js").HorizontalBarGraph;
  989. let popularGraph = new HorizontalBarGraph(thisCanvas);
  990. popularGraph.addData(dataArray);
  991. }else{
  992. document.querySelector("#popularCanvas").style.display = "none";
  993. let notice = document.createElement("p");
  994. notice.innerText = "N/A";
  995. notice.classList = "notice";
  996. document.querySelector("#popularIngredientsCard").appendChild(notice);
  997. }
  998. },
  999. submitInventoryCheck: function(){
  1000. let lis = document.querySelectorAll("#inventoryCheckCard li");
  1001. let changes = [];
  1002. let fetchData = [];
  1003. for(let i = 0; i < lis.length; i++){
  1004. if(lis[i].children[1].children[1].value >= 0){
  1005. let merchIngredient = lis[i].ingredient;
  1006. let value = parseFloat(lis[i].children[1].children[1].value);
  1007. if(value !== merchIngredient.quantity){
  1008. changes.push({
  1009. id: merchIngredient.ingredient.id,
  1010. ingredient: merchIngredient.ingredient,
  1011. quantity: value
  1012. });
  1013. fetchData.push({
  1014. id: merchIngredient.ingredient.id,
  1015. quantity: value
  1016. });
  1017. }
  1018. }else{
  1019. banner.createError("CANNOT HAVE NEGATIVE INGREDIENTS");
  1020. return;
  1021. }
  1022. }
  1023. let loader = document.getElementById("loaderContainer");
  1024. loader.style.display = "flex";
  1025. if(fetchData.length > 0){
  1026. fetch("/merchant/ingredients/update", {
  1027. method: "PUT",
  1028. headers: {
  1029. "Content-Type": "application/json;charset=utf-8"
  1030. },
  1031. body: JSON.stringify(fetchData)
  1032. })
  1033. .then((response) => response.json())
  1034. .then((response)=>{
  1035. if(typeof(response) === "string"){
  1036. banner.createError(response);
  1037. }else{
  1038. merchant.editIngredients(changes);
  1039. banner.createNotification("INGREDIENTS UPDATED");
  1040. }
  1041. })
  1042. .catch((err)=>{})
  1043. .finally(()=>{
  1044. loader.style.display = "none";
  1045. });
  1046. }
  1047. }
  1048. }
  1049. },{"../../shared/graphs.js":20}],8:[function(require,module,exports){
  1050. module.exports = {
  1051. ingredient: {},
  1052. display: function(ingredient){
  1053. this.ingredient = ingredient;
  1054. document.getElementById("editIngBtn").onclick = ()=>{this.edit()};
  1055. document.getElementById("removeIngBtn").onclick = ()=>{this.remove(merchant)};
  1056. document.querySelector("#ingredientDetails p").innerText = ingredient.ingredient.category;
  1057. document.querySelector("#ingredientDetails h1").innerText = ingredient.ingredient.name;
  1058. let ingredientStock = document.getElementById("ingredientStock");
  1059. ingredientStock.innerText = `${ingredient.ingredient.convert(ingredient.quantity).toFixed(2)} ${ingredient.ingredient.unit.toUpperCase()}`;
  1060. ingredientStock.style.display = "block";
  1061. let ingredientInput = document.getElementById("ingredientInput");
  1062. ingredientInput.value = ingredient.ingredient.convert(ingredient.quantity).toFixed(2);
  1063. ingredientInput.style.display = "none";
  1064. let quantities = [];
  1065. let now = new Date();
  1066. for(let i = 1; i < 31; i++){
  1067. let endDay = new Date(now.getFullYear(), now.getMonth(), now.getDate() - i)
  1068. let startDay = new Date(now.getFullYear(), now.getMonth(), now.getDate() - i - 1);
  1069. let indices = merchant.transactionIndices(startDay, endDay);
  1070. if(indices === false){
  1071. quantities.push(0);
  1072. }else{
  1073. quantities.push(merchant.singleIngredientSold(indices, ingredient));
  1074. }
  1075. }
  1076. let sum = 0;
  1077. for(let quantity of quantities){
  1078. sum += quantity;
  1079. }
  1080. document.querySelector("#dailyUse").innerText = `${(sum/quantities.length).toFixed(2)} ${ingredient.ingredient.unit}`;
  1081. let ul = document.querySelector("#ingredientRecipeList");
  1082. let recipes = merchant.getRecipesForIngredient(ingredient.ingredient);
  1083. while(ul.children.length > 0){
  1084. ul.removeChild(ul.firstChild);
  1085. }
  1086. for(let i = 0; i < recipes.length; i++){
  1087. let li = document.createElement("li");
  1088. li.innerText = recipes[i].name;
  1089. li.onclick = ()=>{
  1090. controller.openStrand("recipeBook");
  1091. controller.openSidebar("recipeDetails", recipes[i]);
  1092. }
  1093. ul.appendChild(li);
  1094. }
  1095. let ingredientButtons = document.getElementById("ingredientButtons");
  1096. let units = [];
  1097. let unitLabel = document.getElementById("displayUnitLabel");
  1098. let defaultButton = document.getElementById("defaultUnit");
  1099. if(this.ingredient.ingredient.unitType !== "other"){
  1100. units = merchant.units[this.ingredient.ingredient.unitType];
  1101. unitLabel.style.display = "block";
  1102. defaultButton.style.display = "block";
  1103. }else{
  1104. unitLabel.style.display = "none";
  1105. defaultButton.style.display = "none";
  1106. }
  1107. while(ingredientButtons.children.length > 0){
  1108. ingredientButtons.removeChild(ingredientButtons.firstChild);
  1109. }
  1110. for(let i = 0; i < units.length; i++){
  1111. let button = document.createElement("button");
  1112. button.classList.add("unitButton");
  1113. button.innerText = units[i].toUpperCase();
  1114. button.onclick = ()=>{this.changeUnit(button, units[i])};
  1115. ingredientButtons.appendChild(button);
  1116. if(units[i] === this.ingredient.ingredient.unit){
  1117. button.classList.add("unitActive");
  1118. }
  1119. }
  1120. document.getElementById("defaultUnit").onclick = ()=>{this.changeUnitDefault};
  1121. document.getElementById("editSubmitButton").onclick = ()=>{this.editSubmit()};
  1122. },
  1123. remove: function(merchant){
  1124. for(let i = 0; i < merchant.recipes.length; i++){
  1125. for(let j = 0; j < merchant.recipes[i].ingredients.length; j++){
  1126. if(this.ingredient.ingredient === merchant.recipes[i].ingredients[j].ingredient){
  1127. banner.createError("MUST REMOVE INGREDIENT FROM ALL RECIPES BEFORE REMOVING FROM INVENTORY");
  1128. return;
  1129. }
  1130. }
  1131. }
  1132. let loader = document.getElementById("loaderContainer");
  1133. loader.style.display = "flex";
  1134. fetch(`/merchant/ingredients/remove/${this.ingredient.ingredient.id}`, {
  1135. method: "DELETE",
  1136. })
  1137. .then((response) => response.json())
  1138. .then((response)=>{
  1139. if(typeof(response) === "string"){
  1140. banner.createError(response);
  1141. }else{
  1142. banner.createNotification("INGREDIENT REMOVED");
  1143. merchant.editIngredients([this.ingredient], true);
  1144. }
  1145. })
  1146. .catch((err)=>{})
  1147. .finally(()=>{
  1148. loader.style.display = "none";
  1149. });
  1150. },
  1151. edit: function(){
  1152. document.getElementById("ingredientStock").style.display = "none";
  1153. document.getElementById("ingredientInput").style.display = "block";
  1154. document.getElementById("editSubmitButton").style.display = "block";
  1155. },
  1156. editSubmit: function(){
  1157. this.ingredient.quantity = controller.convertToMain(
  1158. this.ingredient.ingredient.unit,
  1159. Number(document.getElementById("ingredientInput").value)
  1160. );
  1161. let data = [{
  1162. id: this.ingredient.ingredient.id,
  1163. quantity: controller.convertToMain(this.ingredient.ingredient.unit, this.ingredient.quantity)
  1164. }];
  1165. let loader = document.getElementById("loaderContainer");
  1166. loader.style.display = "flex";
  1167. fetch("/merchant/ingredients/update", {
  1168. method: "PUT",
  1169. headers: {
  1170. "Content-Type": "application/json;charset=utf-8"
  1171. },
  1172. body: JSON.stringify(data)
  1173. })
  1174. .then((response) => response.json())
  1175. .then((response)=>{
  1176. if(typeof(response) === "string"){
  1177. banner.createError(response);
  1178. }else{
  1179. merchant.editIngredients([this.ingredient]);
  1180. banner.createNotification("INGREDIENT UPDATED");
  1181. }
  1182. })
  1183. .catch((err)=>{
  1184. console.log(err);
  1185. banner.createError("SOMETHING WENT WRONG. PLEASE REFRESH THE PAGE");
  1186. })
  1187. .finally(()=>{
  1188. loader.style.display = "none";
  1189. });
  1190. },
  1191. changeUnit: function(newActive, unit){
  1192. this.ingredient.ingredient.unit = unit;
  1193. let ingredientButtons = document.querySelectorAll(".unitButton");
  1194. for(let i = 0; i < ingredientButtons.length; i++){
  1195. ingredientButtons[i].classList.remove("unitActive");
  1196. }
  1197. newActive.classList.add("unitActive");
  1198. controller.updateData("unit");
  1199. },
  1200. changeUnitDefault: function(){
  1201. let loader = document.getElementById("loaderContainer");
  1202. loader.style.display = "flex";
  1203. let id = this.ingredient.ingredient.id;
  1204. let unit = this.ingredient.ingredient.unit;
  1205. fetch(`/merchant/ingredients/update/${id}/${unit}`, {
  1206. method: "put",
  1207. headers: {
  1208. "Content-Type": "application/json;charset=utf-8"
  1209. },
  1210. })
  1211. .then((response)=>{
  1212. if(typeof(response) === "string"){
  1213. banner.createError(response);
  1214. }else{
  1215. banner.createNotification("INGREDIENT DEFAULT UNIT UPDATED");
  1216. }
  1217. })
  1218. .catch((err)=>{
  1219. banner.createError("SOMETHING WENT WRONG. PLEASE REFRESH THE PAGE");
  1220. })
  1221. .finally(()=>{
  1222. loader.style.display = "none";
  1223. });
  1224. }
  1225. }
  1226. },{}],9:[function(require,module,exports){
  1227. module.exports = {
  1228. isPopulated: false,
  1229. ingredients: [],
  1230. display: function(){
  1231. if(!this.isPopulated){
  1232. this.populateByProperty("category");
  1233. document.getElementById("ingredientSearch").oninput = ()=>{this.search()};
  1234. document.getElementById("ingredientClearButton").onclick = ()=>{this.clearSorting()};
  1235. document.getElementById("ingredientSelect").onchange = ()=>{this.sort()};
  1236. this.isPopulated = true;
  1237. }
  1238. },
  1239. populateByProperty: function(property){
  1240. let categories;
  1241. if(property === "category"){
  1242. categories = merchant.categorizeIngredients();
  1243. }else if(property === "unit"){
  1244. categories = merchant.unitizeIngredients();
  1245. }
  1246. let ingredientStrand = document.querySelector("#categoryList");
  1247. let categoryTemplate = document.querySelector("#categoryDiv").content.children[0];
  1248. let ingredientTemplate = document.querySelector("#ingredient").content.children[0];
  1249. this.ingredients = [];
  1250. while(ingredientStrand.children.length > 0){
  1251. ingredientStrand.removeChild(ingredientStrand.firstChild);
  1252. }
  1253. for(let i = 0; i < categories.length; i++){
  1254. let categoryDiv = categoryTemplate.cloneNode(true);
  1255. categoryDiv.children[0].children[0].innerText = categories[i].name;
  1256. categoryDiv.children[0].children[1].onclick = ()=>{this.toggleCategory(categoryDiv.children[1], categoryDiv.children[0].children[1])};
  1257. categoryDiv.children[1].style.display = "none";
  1258. ingredientStrand.appendChild(categoryDiv);
  1259. for(let j = 0; j < categories[i].ingredients.length; j++){
  1260. let ingredient = categories[i].ingredients[j];
  1261. let ingredientDiv = ingredientTemplate.cloneNode(true);
  1262. ingredientDiv.children[0].innerText = ingredient.ingredient.name;
  1263. ingredientDiv.children[2].innerText = `${ingredient.ingredient.convert(ingredient.quantity).toFixed(2)} ${ingredient.ingredient.unit.toUpperCase()}`;
  1264. ingredientDiv.onclick = ()=>{controller.openSidebar("ingredientDetails", ingredient)};
  1265. ingredientDiv._name = ingredient.ingredient.name.toLowerCase();
  1266. ingredientDiv._unit = ingredient.ingredient.unit.toLowerCase();
  1267. categoryDiv.children[1].appendChild(ingredientDiv);
  1268. this.ingredients.push(ingredientDiv);
  1269. }
  1270. }
  1271. },
  1272. displayIngredientsOnly: function(ingredients){
  1273. let ingredientDiv = document.querySelector("#categoryList");
  1274. while(ingredientDiv.children.length > 0){
  1275. ingredientDiv.removeChild(ingredientDiv.firstChild);
  1276. }
  1277. for(let i = 0; i < ingredients.length; i++){
  1278. ingredientDiv.appendChild(ingredients[i]);
  1279. }
  1280. },
  1281. toggleCategory: function(div, button){
  1282. if(div.style.display === "none"){
  1283. 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>';
  1284. div.style.display = "flex";
  1285. }else if(div.style.display === "flex"){
  1286. 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>';
  1287. div.style.display = "none";
  1288. }
  1289. },
  1290. search: function(){
  1291. let input = document.querySelector("#ingredientSearch").value.toLowerCase();
  1292. document.querySelector("#ingredientSelect").selectedIndex = 0;
  1293. if(input === ""){
  1294. this.populateByProperty("category");
  1295. document.querySelector("#ingredientClearButton").style.display = "none";
  1296. return;
  1297. }
  1298. let matchingIngredients = [];
  1299. for(let i = 0; i < this.ingredients.length; i++){
  1300. if(this.ingredients[i]._name.includes(input)){
  1301. matchingIngredients.push(this.ingredients[i]);
  1302. }
  1303. }
  1304. document.querySelector("#ingredientClearButton").style.display = "inline";
  1305. this.displayIngredientsOnly(matchingIngredients);
  1306. },
  1307. sort: function(){
  1308. let sortType = document.getElementById("ingredientSelect");
  1309. if(sortType === ""){
  1310. return;
  1311. }
  1312. document.querySelector("#ingredientSearch").value = "";
  1313. if(sortType === "category"){
  1314. this.populateByProperty("category");
  1315. return;
  1316. }
  1317. if(sortType === "unit"){
  1318. this.populateByProperty("unit");
  1319. return;
  1320. }
  1321. document.querySelector("#ingredientClearButton").style.display = "inline";
  1322. let sortedIngredients = this.ingredients.slice().sort((a, b)=> (a[sortType] > b[sortType]) ? 1 : -1);
  1323. this.displayIngredientsOnly(sortedIngredients);
  1324. },
  1325. clearSorting: function(button){
  1326. document.querySelector("#ingredientSearch").value = "";
  1327. document.querySelector("#ingredientSelect").selectedIndex = 0;
  1328. document.querySelector("#ingredientClearButton").style.display = "none";
  1329. this.populateByProperty("category");
  1330. }
  1331. }
  1332. },{}],10:[function(require,module,exports){
  1333. module.exports = {
  1334. display: function(){
  1335. document.getElementById("newIngName").value = "";
  1336. document.getElementById("newIngCategory").value = "";
  1337. document.getElementById("newIngQuantity").value = 0;
  1338. document.getElementById("submitNewIng").onclick = ()=>{this.submit()};
  1339. },
  1340. submit: function(){
  1341. let unitSelector = document.getElementById("unitSelector");
  1342. let options = document.querySelectorAll("#unitSelector option");
  1343. let unit = unitSelector.value;
  1344. let newIngredient = {
  1345. ingredient: {
  1346. name: document.getElementById("newIngName").value,
  1347. category: document.getElementById("newIngCategory").value,
  1348. unitType: options[unitSelector.selectedIndex].getAttribute("type"),
  1349. },
  1350. quantity: controller.convertToMain(unit, document.querySelector("#newIngQuantity").value),
  1351. defaultUnit: unit
  1352. }
  1353. let loader = document.getElementById("loaderContainer");
  1354. loader.style.display = "flex";
  1355. fetch("/ingredients/create", {
  1356. method: "POST",
  1357. headers: {
  1358. "Content-Type": "application/json;charset=utf-8"
  1359. },
  1360. body: JSON.stringify(newIngredient)
  1361. })
  1362. .then((response) => response.json())
  1363. .then((response)=>{
  1364. if(typeof(response) === "string"){
  1365. banner.createError(response);
  1366. }else{
  1367. merchant.editIngredients([{
  1368. ingredient: new Ingredient(
  1369. response.ingredient._id,
  1370. response.ingredient.name,
  1371. response.ingredient.category,
  1372. response.ingredient.unitType,
  1373. response.defaultUnit,
  1374. merchant
  1375. ),
  1376. quantity: response.quantity
  1377. }]);
  1378. banner.createNotification("INGREDIENT CREATED");
  1379. }
  1380. })
  1381. .catch((err)=>{
  1382. banner.createError("SOMETHING WENT WRONG. PLEASE REFRESH THE PAGE");
  1383. })
  1384. .finally(()=>{
  1385. loader.style.display = "none";
  1386. });
  1387. }
  1388. }
  1389. },{}],11:[function(require,module,exports){
  1390. module.exports = {
  1391. isPopulated: false,
  1392. unused: [],
  1393. display: function(){
  1394. if(!this.isPopulated){
  1395. let categories = merchant.categorizeIngredients();
  1396. let categoriesList = document.querySelector("#newOrderCategories");
  1397. let template = document.querySelector("#addIngredientsCategory").content.children[0];
  1398. let ingredientTemplate = document.querySelector("#addIngredientsIngredient").content.children[0];
  1399. for(let i = 0; i < categories.length; i++){
  1400. let category = template.cloneNode(true);
  1401. category.children[0].children[0].innerText = categories[i].name;
  1402. category.children[0].children[1].onclick = ()=>{this.toggleAddIngredient(category)};
  1403. category.children[0].children[1].children[1].style.display = "none";
  1404. category.children[1].style.display = "none";
  1405. categoriesList.appendChild(category);
  1406. for(let j = 0; j < categories[i].ingredients.length; j++){
  1407. let ingredientDiv = ingredientTemplate.cloneNode(true);
  1408. ingredientDiv.children[0].innerText = categories[i].ingredients[j].ingredient.name;
  1409. ingredientDiv.children[2].onclick = ()=>{this.addOne(ingredientDiv, category.children[1])};
  1410. ingredientDiv.ingredient = categories[i].ingredients[j].ingredient;
  1411. this.unused.push(categories[i].ingredients[j]);
  1412. category.children[1].appendChild(ingredientDiv);
  1413. }
  1414. }
  1415. document.getElementById("submitNewOrder").onclick = ()=>{this.submit()};
  1416. this.isPopulated = true;
  1417. }
  1418. },
  1419. addOne: function(ingredientDiv, container){
  1420. for(let i = 0; i < this.unused.length; i++){
  1421. if(this.unused[i] === ingredientDiv){
  1422. this.unused.splice(i, 1);
  1423. break;
  1424. }
  1425. }
  1426. let quantityInput = document.createElement("input");
  1427. quantityInput.type = "number";
  1428. quantityInput.placeholder = `QUANTITY (${ingredientDiv.ingredient.unit})`;
  1429. quantityInput.min = "0";
  1430. quantityInput.step = "0.01";
  1431. ingredientDiv.insertBefore(quantityInput, ingredientDiv.children[1]);
  1432. let priceInput = document.createElement("input");
  1433. priceInput.type = "number";
  1434. priceInput.placeholder = "Price Per Unit";
  1435. priceInput.min = "0";
  1436. priceInput.step = "0.01";
  1437. ingredientDiv.insertBefore(priceInput, ingredientDiv.children[2]);
  1438. ingredientDiv.children[4].innerText = "-";
  1439. ingredientDiv.children[4].onclick = ()=>{this.removeOne(ingredientDiv, container)};
  1440. container.removeChild(ingredientDiv);
  1441. document.getElementById("newOrderAdded").appendChild(ingredientDiv);
  1442. },
  1443. removeOne: function(ingredientDiv, container){
  1444. this.unused.push(ingredientDiv.ingredient);
  1445. ingredientDiv.removeChild(ingredientDiv.children[1]);
  1446. ingredientDiv.removeChild(ingredientDiv.children[1]);
  1447. ingredientDiv.children[1].innerText = "+";
  1448. ingredientDiv.children[1].onclick = ()=>{this.addOne(ingredientDiv, container)};
  1449. ingredientDiv.parentElement.removeChild(ingredientDiv);
  1450. container.appendChild(ingredientDiv);
  1451. },
  1452. toggleAddIngredient: function(categoryElement){
  1453. let button = categoryElement.children[0].children[1];
  1454. let ingredientDisplay = categoryElement.children[1];
  1455. if(ingredientDisplay.style.display === "none"){
  1456. ingredientDisplay.style.display = "flex";
  1457. button.children[0].style.display = "none";
  1458. button.children[1].style.display = "block";
  1459. }else{
  1460. ingredientDisplay.style.display = "none";
  1461. button.children[0].style.display = "block";
  1462. button.children[1].style.display = "none";
  1463. }
  1464. },
  1465. submit: function(){
  1466. let categoriesList = document.getElementById("newOrderAdded");
  1467. let ingredients = [];
  1468. for(let i = 0; i < categoriesList.children.length; i++){
  1469. let quantity = categoriesList.children[i].children[1].value;
  1470. let price = categoriesList.children[i].children[2].value;
  1471. let fakeOrder = new Order(undefined, undefined, new Date(), [], undefined);
  1472. if(quantity !== "" && price !== ""){
  1473. ingredients.push({
  1474. ingredient: categoriesList.children[i].ingredient.id,
  1475. quantity: controller.convertToMain(categoriesList.children[i].ingredient.unit, parseFloat(quantity)),
  1476. price: categoriesList.children[i].ingredient.convert(parseInt(price * 100))
  1477. });
  1478. }
  1479. }
  1480. let date = `${document.getElementById("orderDate").value}T${document.getElementById("orderTime").value}:00`
  1481. let data = {
  1482. name: document.getElementById("orderName").value,
  1483. date: date,
  1484. ingredients: ingredients
  1485. };
  1486. let loader = document.getElementById("loaderContainer");
  1487. loader.style.display = "flex";
  1488. fetch("/order/create", {
  1489. method: "POST",
  1490. headers: {
  1491. "Content-Type": "application/json;charset=utf-8"
  1492. },
  1493. body: JSON.stringify(data)
  1494. })
  1495. .then(response => response.json())
  1496. .then((response)=>{
  1497. if(typeof(response) === "string"){
  1498. banner.createError(response);
  1499. }else{
  1500. let order = new Order(
  1501. response._id,
  1502. response.name,
  1503. response.date,
  1504. response.ingredients,
  1505. merchant
  1506. )
  1507. merchant.editOrders([order]);
  1508. merchant.editIngredients(order.ingredients, false, true);
  1509. banner.createNotification("ORDER CREATED");
  1510. }
  1511. })
  1512. .catch((err)=>{
  1513. banner.createError("SOEMTHING WENT WRONG. PLEASE REFRESH THE PAGE");
  1514. })
  1515. .finally(()=>{
  1516. loader.style.display = "none";
  1517. });
  1518. },
  1519. }
  1520. },{}],12:[function(require,module,exports){
  1521. module.exports = {
  1522. display: function(){
  1523. let ingredientsSelect = document.querySelector("#recipeInputIngredients select");
  1524. let categories = merchant.categorizeIngredients();
  1525. while(ingredientsSelect.children.length > 0){
  1526. ingredientsSelect.removeChild(ingredientsSelect.firstChild);
  1527. }
  1528. for(let category of categories){
  1529. let optgroup = document.createElement("optgroup");
  1530. optgroup.label = category.name;
  1531. ingredientsSelect.appendChild(optgroup);
  1532. for(let ingredient of category.ingredients){
  1533. let option = document.createElement("option");
  1534. option.value = ingredient.ingredient.id;
  1535. option.innerText = `${ingredient.ingredient.name} (${ingredient.ingredient.unit})`;
  1536. optgroup.appendChild(option);
  1537. }
  1538. }
  1539. },
  1540. //Updates the number of ingredient inputs displayed for new recipes
  1541. changeRecipeCount: function(){
  1542. let newCount = document.querySelector("#ingredientCount").value;
  1543. let ingredientsDiv = document.querySelector("#recipeInputIngredients");
  1544. let oldCount = ingredientsDiv.children.length;
  1545. if(newCount > oldCount){
  1546. let newDivs = newCount - oldCount;
  1547. for(let i = 0; i < newDivs; i++){
  1548. let newNode = ingredientsDiv.children[0].cloneNode(true);
  1549. newNode.children[2].children[0].value = "";
  1550. ingredientsDiv.appendChild(newNode);
  1551. }
  1552. for(let i = 0; i < newCount; i++){
  1553. ingredientsDiv.children[i].children[0].innerText = `INGREDIENT ${i + 1}`;
  1554. }
  1555. }else if(newCount < oldCount){
  1556. let newDivs = oldCount - newCount;
  1557. for(let i = 0; i < newDivs; i++){
  1558. ingredientsDiv.removeChild(ingredientsDiv.children[ingredientsDiv.children.length-1]);
  1559. }
  1560. }
  1561. },
  1562. submit: function(){
  1563. let newRecipe = {
  1564. name: document.getElementById("newRecipeName").value,
  1565. price: document.getElementById("newRecipePrice").value,
  1566. ingredients: []
  1567. }
  1568. let inputs = document.querySelectorAll("#recipeInputIngredients > div");
  1569. for(let i = 0; i < inputs.length; i++){
  1570. for(let j = 0; j < merchant.ingredients.length; j++){
  1571. if(merchant.ingredients[j].ingredient.id === inputs[i].children[1].children[0].value){
  1572. newRecipe.ingredients.push({
  1573. ingredient: inputs[i].children[1].children[0].value,
  1574. quantity: controller.convertToMain(merchant.ingredients[j].ingredient.unit, inputs[i].children[2].children[0].value)
  1575. });
  1576. break;
  1577. }
  1578. }
  1579. }
  1580. if(!validator.recipe(newRecipe)){
  1581. return;
  1582. }
  1583. let loader = document.getElementById("loaderContainer");
  1584. loader.style.display = "flex";
  1585. fetch("/recipe/create", {
  1586. method: "POST",
  1587. headers: {
  1588. "Content-Type": "application/json;charset=utf-8"
  1589. },
  1590. body: JSON.stringify(newRecipe)
  1591. })
  1592. .then((response) => response.json())
  1593. .then((response)=>{
  1594. if(typeof(response) === "string"){
  1595. banner.createError(response);
  1596. }else{
  1597. let recipe = new Recipe(
  1598. response._id,
  1599. response.name,
  1600. response.price,
  1601. response.ingredients,
  1602. merchant,
  1603. );
  1604. merchant.editRecipes([recipe]);
  1605. banner.createNotification("RECIPE CREATED");
  1606. }
  1607. })
  1608. .catch((err)=>{
  1609. banner.createError("SOMETHING WENT WRONG. PLEASE REFRESH THE PAGE");
  1610. })
  1611. .finally(()=>{
  1612. loader.style.display = "none";
  1613. });
  1614. },
  1615. }
  1616. },{}],13:[function(require,module,exports){
  1617. module.exports = {
  1618. display: function(){
  1619. let recipeList = document.getElementById("newTransactionRecipes");
  1620. let template = document.getElementById("createTransaction").content.children[0];
  1621. while(recipeList.children.length > 0){
  1622. recipeList.removeChild(recipeList.firstChild);
  1623. }
  1624. for(let i = 0; i < merchant.recipes.length; i++){
  1625. let recipeDiv = template.cloneNode(true);
  1626. recipeDiv.recipe = merchant.recipes[i];
  1627. recipeList.appendChild(recipeDiv);
  1628. recipeDiv.children[0].innerText = merchant.recipes[i].name;
  1629. }
  1630. document.getElementById("submitNewTransaction").onclick = ()=>{this.submit()};
  1631. },
  1632. submit: function(){
  1633. let recipeDivs = document.getElementById("newTransactionRecipes");
  1634. let date = document.getElementById("newTransactionDate").valueAsDate;
  1635. if(date > new Date()){
  1636. banner.createError("CANNOT HAVE A DATE IN THE FUTURE");
  1637. return;
  1638. }
  1639. let newTransaction = {
  1640. date: date,
  1641. recipes: []
  1642. };
  1643. for(let i = 0; i < recipeDivs.children.length; i++){
  1644. let quantity = recipeDivs.children[i].children[1].value;
  1645. if(quantity !== "" && quantity > 0){
  1646. newTransaction.recipes.push({
  1647. recipe: recipeDivs.children[i].recipe.id,
  1648. quantity: quantity
  1649. });
  1650. }else if(quantity < 0){
  1651. banner.createError("CANNOT HAVE NEGATIVE VALUES");
  1652. return;
  1653. }
  1654. }
  1655. if(newTransaction.recipes.length > 0){
  1656. let loader = document.getElementById("loaderContainer");
  1657. loader.style.display = "flex";
  1658. fetch("/transaction/create", {
  1659. method: "post",
  1660. headers: {
  1661. "Content-Type": "application/json;charset=utf-8"
  1662. },
  1663. body: JSON.stringify(newTransaction)
  1664. })
  1665. .then(response => response.json())
  1666. .then((response)=>{
  1667. if(typeof(response) === "string"){
  1668. banner.createError(response);
  1669. }else{
  1670. let transaction = new Transaction(
  1671. response._id,
  1672. response.date,
  1673. response.recipes,
  1674. merchant
  1675. );
  1676. merchant.editTransactions(transaction);
  1677. banner.createNotification("NEW TRANSACTION CREATED");
  1678. }
  1679. })
  1680. .catch((err)=>{
  1681. banner.createError("SOMETHING WENT WRONG. PLEASE REFRESH THE PAGE");
  1682. })
  1683. .finally(()=>{
  1684. loader.style.display = "none";
  1685. });
  1686. }
  1687. }
  1688. }
  1689. },{}],14:[function(require,module,exports){
  1690. module.exports = {
  1691. display: function(order){
  1692. document.querySelector("#removeOrderBtn").onclick = ()=>{this.remove(order)};
  1693. document.getElementById("orderDetailName").innerText = order.name;
  1694. document.getElementById("orderDetailDate").innerText = order.date.toLocaleDateString("en-US");
  1695. document.getElementById("orderDetailTime").innerText = order.date.toLocaleTimeString("en-US");
  1696. let ingredientList = document.querySelector("#orderIngredients");
  1697. while(ingredientList.children.length > 0){
  1698. ingredientList.removeChild(ingredientList.firstChild);
  1699. }
  1700. let template = document.querySelector("#orderIngredient").content.children[0];
  1701. let grandTotal = 0;
  1702. for(let i = 0; i < order.ingredients.length; i++){
  1703. let ingredientDiv = template.cloneNode(true);
  1704. let price = (order.ingredients[i].quantity * order.ingredients[i].price) / 100;
  1705. grandTotal += price;
  1706. let ingredient = order.ingredients[i].ingredient;
  1707. let priceText = ingredient.convert(order.ingredients[i].quantity).toFixed(2) + " " +
  1708. ingredient.unit.toUpperCase() + " x $" +
  1709. (order.convertPrice(ingredient.unitType, ingredient.unit, order.ingredients[i].price) / 100).toFixed(2);
  1710. ingredientDiv.children[0].innerText = order.ingredients[i].ingredient.name;
  1711. ingredientDiv.children[1].innerText = priceText;
  1712. ingredientDiv.children[2].innerText = `$${price.toFixed(2)}`;
  1713. ingredientList.appendChild(ingredientDiv);
  1714. }
  1715. document.querySelector("#orderTotalPrice p").innerText = `$${grandTotal.toFixed(2)}`;
  1716. },
  1717. remove: function(order){
  1718. let loader = document.getElementById("loaderContainer");
  1719. loader.style.display = "flex";
  1720. fetch(`/order/${order.id}`, {
  1721. method: "DELETE",
  1722. headers: {
  1723. "Content-Type": "application/json;charset=utf-8"
  1724. }
  1725. })
  1726. .then((response) => response.json())
  1727. .then((response)=>{
  1728. if(typeof(response) === "string"){
  1729. banner.createError(response);
  1730. }else{
  1731. merchant.editOrders([order], true);
  1732. banner.createNotification("ORDER REMOVED");
  1733. }
  1734. })
  1735. .catch((err)=>{
  1736. banner.createError("SOMETHING WENT WRONG. PLEASE REFRESH THE PAGE");
  1737. })
  1738. .finally(()=>{
  1739. loader.style.display = "none";
  1740. });
  1741. }
  1742. }
  1743. },{}],15:[function(require,module,exports){
  1744. module.exports = {
  1745. isFetched: false,
  1746. display: async function(){
  1747. if(!this.isFetched){
  1748. let loader = document.getElementById("loaderContainer");
  1749. loader.style.display = "flex";
  1750. fetch("/order", {
  1751. method: "GET",
  1752. headers: {
  1753. "Content-Type": "application/json;charset=utf-8"
  1754. },
  1755. })
  1756. .then((response) => response.json())
  1757. .then((response)=>{
  1758. if(typeof(response) === "string"){
  1759. banner.createError(response);
  1760. }else{
  1761. let newOrders = [];
  1762. for(let i = 0; i < response.length; i++){
  1763. newOrders.push(new Order(
  1764. response[i]._id,
  1765. response[i].name,
  1766. response[i].date,
  1767. response[i].ingredients,
  1768. merchant
  1769. ));
  1770. }
  1771. merchant.editOrders(newOrders);
  1772. document.getElementById("orderSubmitForm").onsubmit = ()=>{this.submitFilter()};
  1773. this.isFetched = true;
  1774. }
  1775. })
  1776. .catch((err)=>{
  1777. banner.createError("SOMETHING WENT WRONG. TRY REFRESHING THE PAGE");
  1778. })
  1779. .finally(()=>{
  1780. loader.style.display = "none";
  1781. });
  1782. }
  1783. },
  1784. populate: function(){
  1785. let listDiv = document.getElementById("orderList");
  1786. let template = document.getElementById("order").content.children[0];
  1787. let dateDropdown = document.getElementById("dateDropdownOrder");
  1788. let ingredientDropdown = document.getElementById("ingredientDropdown");
  1789. dateDropdown.style.display = "none";
  1790. ingredientDropdown.style.display = "none";
  1791. document.getElementById("dateFilterBtnOrder").onclick = ()=>{this.toggleDropdown(dateDropdown)};
  1792. document.getElementById("ingredientFilterBtn").onclick = ()=>{this.toggleDropdown(ingredientDropdown)};
  1793. for(let i = 0; i < merchant.ingredients.length; i++){
  1794. let checkbox = document.createElement("input");
  1795. checkbox.type = "checkbox";
  1796. checkbox.ingredient = merchant.ingredients[i].ingredient;
  1797. ingredientDropdown.appendChild(checkbox);
  1798. let label = document.createElement("label");
  1799. label.innerText = merchant.ingredients[i].ingredient.name;
  1800. label.for = checkbox;
  1801. ingredientDropdown.appendChild(label);
  1802. let brk = document.createElement("br");
  1803. ingredientDropdown.appendChild(brk);
  1804. }
  1805. while(listDiv.children.length > 0){
  1806. listDiv.removeChild(listDiv.firstChild);
  1807. }
  1808. for(let i = 0; i < merchant.orders.length; i++){
  1809. let row = template.cloneNode(true);
  1810. let totalCost = 0;
  1811. for(let j = 0; j < merchant.orders[i].ingredients.length; j++){
  1812. totalCost += merchant.orders[i].ingredients[j].quantity * merchant.orders[i].ingredients[j].price;
  1813. }
  1814. row.children[0].innerText = merchant.orders[i].name;
  1815. row.children[1].innerText = `${merchant.orders[i].ingredients.length} items`;
  1816. row.children[2].innerText = new Date(merchant.orders[i].date).toLocaleDateString("en-US");
  1817. row.children[3].innerText = `$${(totalCost / 100).toFixed(2)}`;
  1818. row.order = merchant.orders[i];
  1819. row.onclick = ()=>{controller.openSidebar("orderDetails", merchant.orders[i])};
  1820. listDiv.appendChild(row);
  1821. }
  1822. },
  1823. submitFilter: function(){
  1824. event.preventDefault();
  1825. let data = {
  1826. startDate: document.getElementById("orderFilDate1").valueAsDate,
  1827. endDate: document.getElementById("orderFilDate2").valueAsDate,
  1828. ingredients: []
  1829. }
  1830. if(data.startDate >= data.endDate){
  1831. banner.createError("START DATE CANNOT BE AFTER END DATE");
  1832. return;
  1833. }
  1834. let ingredientChoices = document.getElementById("ingredientDropdown");
  1835. for(let i = 0; i < ingredientChoices.children.length; i += 3){
  1836. if(ingredientChoices.children[i].checked){
  1837. data.ingredients.push(ingredientChoices.children[i].ingredient.id);
  1838. }
  1839. }
  1840. if(data.ingredients.length === 0){
  1841. for(let i = 0; i < merchant.ingredients.length; i++){
  1842. data.ingredients.push(merchant.ingredients[i].ingredient.id);
  1843. }
  1844. }
  1845. let loader = document.getElementById("loaderContainer");
  1846. loader.style.display = "flex";
  1847. fetch("/order", {
  1848. method: "POST",
  1849. headers: {
  1850. "Content-Type": "application/json;charset=utf-8"
  1851. },
  1852. body: JSON.stringify(data)
  1853. })
  1854. .then((response) => response.json())
  1855. .then((response)=>{
  1856. if(typeof(response) === "string"){
  1857. banner.createError(response);
  1858. }else{
  1859. let orderList = document.getElementById("orderList");
  1860. let template = document.getElementById("order").content.children[0];
  1861. while(orderList.children.length > 0){
  1862. orderList.removeChild(orderList.firstChild);
  1863. }
  1864. for(let i = 0; i < response.length; i++){
  1865. let order = template.cloneNode(true);
  1866. let cost = 0;
  1867. for(let j = 0; j < response[i].ingredients.length; j++){
  1868. cost += (response[i].ingredients[j].price / 100) * response[i].ingredients[j].quantity;
  1869. }
  1870. order.children[0].innerText = response[i].name,
  1871. order.children[1].innerText = `${response[i].ingredients.length} items`;
  1872. order.children[2].innerText = new Date(response[i].date).toLocaleDateString();
  1873. order.children[3].innerText = `$${cost.toFixed(2)}`;
  1874. orderList.appendChild(order);
  1875. }
  1876. }
  1877. })
  1878. .catch((err)=>{
  1879. banner.createError("UNABLE TO DISPLAY THE ORDERS");
  1880. })
  1881. .finally(()=>{
  1882. loader.style.display = "none";
  1883. });
  1884. },
  1885. toggleDropdown: function(dropdown){
  1886. event.preventDefault();
  1887. let polyline = dropdown.parentElement.children[0].children[1].children[0].children[0];
  1888. if(dropdown.style.display === "none"){
  1889. dropdown.style.display = "block";
  1890. polyline.setAttribute("points", "18 15 12 9 6 15");
  1891. }else{
  1892. dropdown.style.display = "none";
  1893. polyline.setAttribute("points", "6 9 12 15 18 9");
  1894. }
  1895. }
  1896. }
  1897. },{}],16:[function(require,module,exports){
  1898. module.exports = {
  1899. isPopulated: false,
  1900. recipeDivList: [],
  1901. display: function(){
  1902. if(!this.isPopulated){
  1903. this.populateRecipes();
  1904. document.getElementById("posUpdateRecipe").onclick = ()=>{this.posUpdate()};
  1905. document.getElementById("recipeSearch").oninput = ()=>{this.search()};
  1906. document.getElementById("recipeClearButton").onclick = ()=>{this.clearSorting()};
  1907. this.isPopulated = true;
  1908. }
  1909. },
  1910. populateRecipes: function(){
  1911. let recipeList = document.getElementById("recipeList");
  1912. let template = document.getElementById("recipe").content.children[0];
  1913. this.recipeDivList = [];
  1914. while(recipeList.children.length > 0){
  1915. recipeList.removeChild(recipeList.firstChild);
  1916. }
  1917. for(let i = 0; i < merchant.recipes.length; i++){
  1918. let recipeDiv = template.cloneNode(true);
  1919. recipeDiv.onclick = ()=>{ocntroller.openSidebar("recipeDetails", merchant.recipes[i])};
  1920. recipeDiv._name = merchant.recipes[i].name;
  1921. recipeList.appendChild(recipeDiv);
  1922. recipeDiv.children[0].innerText = merchant.recipes[i].name;
  1923. recipeDiv.children[1].innerText = `$${(merchant.recipes[i].price / 100).toFixed(2)}`;
  1924. this.recipeDivList.push(recipeDiv);
  1925. }
  1926. },
  1927. search: function(){
  1928. let input = document.getElementById("recipeSearch").value.toLowerCase();
  1929. let recipeList = document.getElementById("recipeList");
  1930. let clearButton = document.getElementById("recipeClearButton");
  1931. let matchingRecipes = [];
  1932. for(let i = 0; i < this.recipeDivList.length; i++){
  1933. if(this.recipeDivList[i]._name.toLowerCase().includes(input)){
  1934. matchingRecipes.push(this.recipeDivList[i]);
  1935. }
  1936. }
  1937. while(recipeList.children.length > 0){
  1938. recipeList.removeChild(recipeList.firstChild);
  1939. }
  1940. for(let i = 0; i < matchingRecipes.length; i++){
  1941. recipeList.appendChild(matchingRecipes[i]);
  1942. }
  1943. if(input === ""){
  1944. clearButton.style.display = "none";
  1945. }else{
  1946. clearButton.style.display = "inline";
  1947. }
  1948. },
  1949. clearSorting: function(){
  1950. document.getElementById("recipeSearch").value = "";
  1951. this.search();
  1952. },
  1953. posUpdate: function(){
  1954. let loader = document.getElementById("loaderContainer");
  1955. loader.style.display = "flex";
  1956. fetch("/recipe/update/clover", {
  1957. method: "GET",
  1958. headers: {
  1959. "Content-Type": "application/json;charset=utf-8"
  1960. },
  1961. })
  1962. .then(response => response.json())
  1963. .then((response)=>{
  1964. let newRecipes = [];
  1965. for(let i = 0; i < response.new.length; i++){
  1966. newRecipes.push(new Recipe(
  1967. response.new[i]._id,
  1968. response.new[i].name,
  1969. response.new[i].price,
  1970. merchant,
  1971. []
  1972. ));
  1973. }
  1974. if(newRecipes.length > 0){
  1975. merchant.editRecipes(newRecipes);
  1976. }
  1977. let removeRecipes = [];
  1978. for(let i = 0; i < response.removed.length; i++){
  1979. for(let j = 0; j < merchant.recipes.length; j++){
  1980. if(response.removed[i]._id === merchant.recipes[j].id){
  1981. removeRecipes.push(merchant.recipes[j], true);
  1982. break;
  1983. }
  1984. }
  1985. }
  1986. if(removeRecipes.length > 0){
  1987. merchant.editRecipes(removeRecipes, true);
  1988. }
  1989. })
  1990. .catch((err)=>{
  1991. banner.createError("SOMETHING WENT WRONG. PLEASE REFRESH THE PAGE");
  1992. })
  1993. .finally(()=>{
  1994. loader.style.display = "none";
  1995. });
  1996. }
  1997. }
  1998. },{}],17:[function(require,module,exports){
  1999. module.exports = {
  2000. recipe: {},
  2001. display: function(recipe){
  2002. this.recipe = recipe;
  2003. document.getElementById("recipeName").style.display = "block";
  2004. document.getElementById("recipeNameIn").style.display = "none";
  2005. document.querySelector("#recipeDetails h1").innerText = recipe.name;
  2006. let ingredientList = document.getElementById("recipeIngredientList");
  2007. while(ingredientList.children.length > 0){
  2008. ingredientList.removeChild(ingredientList.firstChild);
  2009. }
  2010. let template = document.getElementById("recipeIngredient").content.children[0];
  2011. for(let i = 0; i < recipe.ingredients.length; i++){
  2012. ingredientDiv = template.cloneNode(true);
  2013. ingredientDiv.children[0].innerText = recipe.ingredients[i].ingredient.name;
  2014. ingredientDiv.children[2].innerText = `${recipe.ingredients[i].ingredient.convert(recipe.ingredients[i].quantity).toFixed(2)} ${recipe.ingredients[i].ingredient.unit}`;
  2015. ingredientDiv.ingredient = recipe.ingredients[i].ingredient;
  2016. ingredientDiv.name = recipe.ingredients[i].ingredient.name;
  2017. ingredientList.appendChild(ingredientDiv);
  2018. }
  2019. document.getElementById("addRecIng").style.display = "none";
  2020. let price = document.getElementById("recipePrice");
  2021. price.children[1].style.display = "block";
  2022. price.children[2].style.display = "none";
  2023. price.children[1].innerText = `$${(recipe.price / 100).toFixed(2)}`;
  2024. document.getElementById("recipeUpdate").style.display = "none";
  2025. document.getElementById("editRecipeBtn").onclick = ()=>{this.edit()};
  2026. document.getElementById("removeRecipeBtn").onclick = ()=>{this.remove()};
  2027. document.getElementById("addRecIng").onclick = ()=>{this.displayAddIngredient()};
  2028. document.getElementById("recipeUpdate").onclick = ()=>{this.update()};
  2029. },
  2030. edit: function(){
  2031. let ingredientDivs = document.querySelector("#recipeIngredientList");
  2032. if(merchant.pos === "none"){
  2033. let name = document.querySelector("#recipeName");
  2034. let nameIn = document.querySelector("#recipeNameIn");
  2035. name.style.display = "none";
  2036. nameIn.style.display = "block";
  2037. nameIn.value = this.recipe.name;
  2038. let price = document.querySelector("#recipePrice");
  2039. price.children[1].style.display = "none";
  2040. price.children[2].style.display = "block";
  2041. price.children[2].value = parseFloat((this.recipe.price / 100).toFixed(2));
  2042. }
  2043. for(let i = 0; i < ingredientDivs.children.length; i++){
  2044. let div = ingredientDivs.children[i];
  2045. div.children[2].innerText = this.recipe.ingredients[i].ingredient.unit;
  2046. div.children[1].style.display = "block";
  2047. div.children[1].value = this.recipe.ingredients[i].ingredient.convert(this.recipe.ingredients[i].quantity).toFixed(2);
  2048. div.children[3].style.display = "block";
  2049. div.children[3].onclick = ()=>{div.parentElement.removeChild(div)};
  2050. }
  2051. document.querySelector("#addRecIng").style.display = "flex";
  2052. document.querySelector("#recipeUpdate").style.display = "flex";
  2053. },
  2054. update: function(){
  2055. this.recipe.name = document.querySelector("#recipeNameIn").value || this.recipe.name;
  2056. this.recipe.price = Math.round((document.querySelector("#recipePrice").children[2].value * 100)) || this.recipe.price;
  2057. this.recipe.ingredients = [];
  2058. let divs = document.querySelector("#recipeIngredientList").children;
  2059. for(let i = 0; i < divs.length; i++){
  2060. if(divs[i].name === "new"){
  2061. let select = divs[i].children[0];
  2062. this.recipe.ingredients.push({
  2063. ingredient: select.options[select.selectedIndex].ingredient,
  2064. quantity: controller.convertToMain(select.options[select.selectedIndex].ingredient.unit, divs[i].children[1].value)
  2065. });
  2066. }else{
  2067. this.recipe.ingredients.push({
  2068. ingredient: divs[i].ingredient,
  2069. quantity: controller.convertToMain(divs[i].ingredient.unit, divs[i].children[1].value)
  2070. });
  2071. }
  2072. }
  2073. let data = {
  2074. id: this.recipe.id,
  2075. name: this.recipe.name,
  2076. price: this.recipe.price,
  2077. ingredients: []
  2078. }
  2079. for(let i = 0; i < this.recipe.ingredients.length; i++){
  2080. data.ingredients.push({
  2081. ingredient: this.recipe.ingredients[i].ingredient.id,
  2082. quantity: this.recipe.ingredients[i].quantity
  2083. });
  2084. }
  2085. let loader = document.getElementById("loaderContainer");
  2086. loader.style.display = "flex";
  2087. fetch("/recipe/update", {
  2088. method: "PUT",
  2089. headers: {
  2090. "Content-Type": "application/json;charset=utf-8"
  2091. },
  2092. body: JSON.stringify(data)
  2093. })
  2094. .then((response) => response.json())
  2095. .then((response)=>{
  2096. if(typeof(response) === "string"){
  2097. banner.createError(response);
  2098. }else{
  2099. merchant.editRecipes([this.recipe]);
  2100. banner.createNotification("RECIPE UPDATE");
  2101. }
  2102. })
  2103. .catch((err)=>{
  2104. banner.createError("SOMETHING WENT WRONG. PLEASE REFRESH THE PAGE");
  2105. })
  2106. .finally(()=>{
  2107. loader.style.display = "none";
  2108. });
  2109. },
  2110. remove: function(){
  2111. fetch(`/merchant/recipes/remove/${this.recipe.id}`, {
  2112. method: "DELETE"
  2113. })
  2114. .then((response) => response.json())
  2115. .then((response)=>{
  2116. if(typeof(response) === "string"){
  2117. banner.createError(response);
  2118. }else{
  2119. merchant.editRecipes([this.recipe], true);
  2120. banner.createNotification("RECIPE REMOVED");
  2121. }
  2122. })
  2123. .catch((err)=>{
  2124. banner.createError("SOMETHING WENT WRONG. PLEASE REFRESH THE PAGE");
  2125. });
  2126. },
  2127. displayAddIngredient: function(){
  2128. let template = document.querySelector("#addRecIngredient").content.children[0].cloneNode(true);
  2129. template.name = "new";
  2130. document.querySelector("#recipeIngredientList").appendChild(template);
  2131. let categories = merchant.categorizeIngredients();
  2132. for(let i = 0; i < categories.length; i++){
  2133. let optGroup = document.createElement("optgroup");
  2134. optGroup.label = categories[i].name;
  2135. template.children[0].appendChild(optGroup);
  2136. for(let j = 0; j < categories[i].ingredients.length; j++){
  2137. let option = document.createElement("option");
  2138. option.innerText = `${categories[i].ingredients[j].ingredient.name} (${categories[i].ingredients[j].ingredient.unit})`;
  2139. option.ingredient = categories[i].ingredients[j].ingredient;
  2140. optGroup.appendChild(option);
  2141. }
  2142. }
  2143. }
  2144. }
  2145. },{}],18:[function(require,module,exports){
  2146. module.exports = {
  2147. transaction: {},
  2148. display: function(transaction){
  2149. this.transaction = transaction;
  2150. let recipeList = document.getElementById("transactionRecipes");
  2151. let template = document.getElementById("transactionRecipe").content.children[0];
  2152. let totalRecipes = 0;
  2153. let totalPrice = 0;
  2154. while(recipeList.children.length > 0){
  2155. recipeList.removeChild(recipeList.firstChild);
  2156. }
  2157. for(let i = 0; i < transaction.recipes.length; i++){
  2158. let recipe = template.cloneNode(true);
  2159. let price = transaction.recipes[i].quantity * transaction.recipes[i].recipe.price;
  2160. recipe.children[0].innerText = transaction.recipes[i].recipe.name;
  2161. recipe.children[1].innerText = `${transaction.recipes[i].quantity} x $${parseFloat(transaction.recipes[i].recipe.price / 100).toFixed(2)}`;
  2162. recipe.children[2].innerText = `$${(price / 100).toFixed(2)}`;
  2163. recipeList.appendChild(recipe);
  2164. totalRecipes += transaction.recipes[i].quantity;
  2165. totalPrice += price;
  2166. }
  2167. let months = ["January", "Fecbruary", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
  2168. let days = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"];
  2169. let dateString = `${days[transaction.date.getDay()]}, ${months[transaction.date.getMonth()]} ${transaction.date.getDate()}, ${transaction.date.getFullYear()}`;
  2170. document.getElementById("transactionDate").innerText = dateString;
  2171. document.getElementById("transactionTime").innerText = transaction.date.toLocaleTimeString();
  2172. document.getElementById("totalRecipes").innerText = `${totalRecipes} recipes`;
  2173. document.getElementById("totalPrice").innerText = `$${(totalPrice / 100).toFixed(2)}`;
  2174. document.getElementById("removeTransBtn").onclick = ()=>{this.remove()};
  2175. },
  2176. remove: function(){
  2177. let loader = document.getElementById("loaderContainer");
  2178. loader.style.display = "flex";
  2179. fetch(`/transaction/${this.transaction.id}`, {
  2180. method: "delete",
  2181. headers: {
  2182. "Content-Type": "application/json;charset=utf-8"
  2183. },
  2184. })
  2185. .then(response => response.json())
  2186. .then((response)=>{
  2187. if(typeof(response) === "string"){
  2188. banner.createError(response);
  2189. }else{
  2190. merchant.editTransactions(this.transaction, true);
  2191. banner.createNotification("TRANSACTION REMOVED");
  2192. }
  2193. })
  2194. .catch((err)=>{
  2195. banner.createError("SOMETHING WENT WRONG. PLEASE REFRESH THE PAGE");
  2196. })
  2197. .finally(()=>{
  2198. loader.style.display = "none";
  2199. });
  2200. },
  2201. }
  2202. },{}],19:[function(require,module,exports){
  2203. module.exports = {
  2204. isPopulated: false,
  2205. display: function(){
  2206. if(!this.isPopulated){
  2207. let transactionsList = document.getElementById("transactionsList");
  2208. let dateDropdown = document.getElementById("dateDropdown");
  2209. let recipeDropdown = document.getElementById("recipeDropDown");
  2210. let template = document.getElementById("transaction").content.children[0];
  2211. let now = new Date();
  2212. let monthAgo = new Date(now.getFullYear(), now.getMonth() - 1, now.getDate());
  2213. document.getElementById("transFilDate1").valueAsDate = monthAgo;
  2214. document.getElementById("transFilDate2").valueAsDate = now;
  2215. dateDropdown.style.display = "none";
  2216. recipeDropdown.style.display = "none";
  2217. document.getElementById("dateFilterBtn").onclick = ()=>{this.toggleDropdown(dateDropdown)};
  2218. document.getElementById("recipeFilterBtn").onclick = ()=>{this.toggleDropdown(recipeDropdown)};
  2219. while(recipeDropdown.children.length > 0){
  2220. recipeDropdown.removeChild(recipeDropdown.firstChild);
  2221. }
  2222. for(let i = 0; i < merchant.recipes.length; i++){
  2223. let checkbox = document.createElement("input");
  2224. checkbox.type = "checkbox";
  2225. checkbox.recipe = merchant.recipes[i];
  2226. recipeDropdown.appendChild(checkbox);
  2227. let label = document.createElement("label");
  2228. label.innerText = merchant.recipes[i].name;
  2229. label.for = checkbox;
  2230. recipeDropdown.appendChild(label);
  2231. let brk = document.createElement("br");
  2232. recipeDropdown.appendChild(brk);
  2233. }
  2234. while(transactionsList.children.length > 0){
  2235. transactionsList.removeChild(transactionsList.firstChild);
  2236. }
  2237. let i = 0
  2238. while(i < merchant.transactions.length && i < 100){
  2239. let transactionDiv = template.cloneNode(true);
  2240. let transaction = merchant.transactions[i];
  2241. transactionDiv.onclick = ()=>{controller.openStrand("transactionDetails", transaction)};
  2242. transactionsList.appendChild(transactionDiv);
  2243. let totalRecipes = 0;
  2244. let totalPrice = 0;
  2245. for(let j = 0; j < merchant.transactions[i].recipes.length; j++){
  2246. totalRecipes += merchant.transactions[i].recipes[j].quantity;
  2247. totalPrice += merchant.transactions[i].recipes[j].recipe.price * merchant.transactions[i].recipes[j].quantity;
  2248. }
  2249. transactionDiv.children[0].innerText = `${merchant.transactions[i].date.toLocaleDateString()} ${merchant.transactions[i].date.toLocaleTimeString()}`;
  2250. transactionDiv.children[1].innerText = `${totalRecipes} recipes sold`;
  2251. transactionDiv.children[2].innerText = `$${(totalPrice / 100).toFixed(2)}`;
  2252. i++;
  2253. }
  2254. document.getElementById("transFormSubmit").onsubmit = ()=>{this.submitFilter()};
  2255. this.isPopulated = true;
  2256. }
  2257. },
  2258. submitFilter: function(){
  2259. event.preventDefault();
  2260. let data = {
  2261. startDate: document.getElementById("transFilDate1").valueAsDate,
  2262. endDate: document.getElementById("transFilDate2").valueAsDate,
  2263. recipes: []
  2264. }
  2265. if(data.startDate >= data.endDate){
  2266. banner.createError("START DATE CANNOT BE AFTER END DATE");
  2267. return;
  2268. }
  2269. let recipeChoices = document.getElementById("recipeDropDown");
  2270. for(let i = 0; i < recipeChoices.children.length; i += 3){
  2271. if(recipeChoices.children[i].checked){
  2272. data.recipes.push(recipeChoices.children[i].recipe.id);
  2273. }
  2274. }
  2275. if(data.recipes.length === 0){
  2276. for(let i = 0; i < merchant.recipes.length; i++){
  2277. data.recipes.push(merchant.recipes[i].id);
  2278. }
  2279. }
  2280. let loader = document.getElementById("loaderContainer");
  2281. loader.style.display = "flex";
  2282. fetch("/transaction", {
  2283. method: "POST",
  2284. headers: {
  2285. "Content-Type": "application/json;charset=utf-8"
  2286. },
  2287. body: JSON.stringify(data)
  2288. })
  2289. .then((response) => response.json())
  2290. .then((response)=>{
  2291. if(typeof(response) === "string"){
  2292. banner.createError(response);
  2293. }else{
  2294. let transactionList = document.getElementById("transactionsList");
  2295. let template = document.getElementById("transaction").content.children[0];
  2296. while(transactionList.children.length > 0){
  2297. transactionList.removeChild(transactionList.firstChild);
  2298. }
  2299. for(let i = 0; i < response.length; i++){
  2300. let transactionDiv = template.cloneNode(true);
  2301. let recipeCount = 0;
  2302. let cost = 0;
  2303. let transaction = new Transaction(
  2304. response[i]._id,
  2305. response[i].date,
  2306. response[i].recipes,
  2307. merchant
  2308. );
  2309. for(let j = 0; j < transaction.recipes.length; j++){
  2310. recipeCount += transaction.recipes[j].quantity;
  2311. cost += transaction.recipes[j].quantity * transaction.recipes[j].recipe.price;
  2312. }
  2313. transactionDiv.children[0].innerText = `${transaction.date.toLocaleDateString()} ${transaction.date.toLocaleTimeString()}`;
  2314. transactionDiv.children[1].innerText = `${recipeCount} recipes sold`;
  2315. transactionDiv.children[2].innerText = `$${(cost / 100).toFixed(2)}`;
  2316. transactionDiv.onclick = ()=>{controller.openSidebar("transactionDetails", transaction)};
  2317. transactionList.appendChild(transactionDiv);
  2318. }
  2319. }
  2320. })
  2321. .catch((err)=>{
  2322. banner.createError("UNABLE TO DISPLAY THE TRANSACTIONS");
  2323. })
  2324. .finally(()=>{
  2325. loader.style.display = "none";
  2326. });
  2327. },
  2328. toggleDropdown: function(dropdown){
  2329. event.preventDefault();
  2330. let polyline = dropdown.parentElement.children[0].children[1].children[0].children[0];
  2331. if(dropdown.style.display === "none"){
  2332. dropdown.style.display = "block";
  2333. polyline.setAttribute("points", "18 15 12 9 6 15");
  2334. }else{
  2335. dropdown.style.display = "none";
  2336. polyline.setAttribute("points", "6 9 12 15 18 9");
  2337. }
  2338. }
  2339. }
  2340. },{}],20:[function(require,module,exports){
  2341. //Creates a line graph within a canvas
  2342. //Will expand or shrink to the size of the canvas
  2343. //Inputs:
  2344. // canvas = the canvas that you would like to draw on
  2345. // yName = a string for the name of the Y axis
  2346. // xName = a string for the name of the X axis
  2347. class LineGraph{
  2348. constructor(canvas){
  2349. this.canvas = canvas;
  2350. this.context = canvas.getContext("2d");
  2351. this.left = canvas.clientWidth - (canvas.clientWidth * 0.95);
  2352. this.right = canvas.clientWidth * 1;
  2353. this.top = canvas.clientHeight - (canvas.clientHeight * 1);
  2354. this.bottom = canvas.clientHeight * 0.85;
  2355. this.data = [];
  2356. this.max = 0;
  2357. this.xRange = [];
  2358. this.colors = [];
  2359. this.colorIndex = 0;
  2360. for(let i = 0; i < 100; i++){
  2361. let redRand = Math.floor(Math.random() * 200);
  2362. let greenRand = Math.floor(Math.random() * 200);
  2363. let blueRand = Math.floor(Math.random() * 200);
  2364. this.colors.push(`rgb(${redRand}, ${greenRand}, ${blueRand})`);
  2365. }
  2366. }
  2367. //Add a dataset to the graph to draw
  2368. //Inputs:
  2369. // data = array containing list of numbers as the data points for the graph
  2370. // data[0] will be on the left. data[data.length-1] will be on the right.
  2371. // xRange = array containing two elements, start and end for x axis data (currently only dates)
  2372. // name = string name for the line. Used for display and finding lines. Each must be unique
  2373. addData(data, xRange, name){
  2374. data = {
  2375. set: data,
  2376. colorIndex: this.colorIndex,
  2377. name: name
  2378. }
  2379. this.colorIndex++;
  2380. this.data.push(data);
  2381. let isChange = false;
  2382. for(let point of data.set){
  2383. if(point > this.max){
  2384. this.max = point;
  2385. this.verticalMultiplier = (this.bottom - this.top) / this.max;
  2386. this.horizontalMultiplier = (this.right - this.left) / (data.set.length - 1);
  2387. isChange = true;
  2388. }
  2389. }
  2390. if(this.xRange.length === 0){
  2391. this.xRange = xRange;
  2392. isChange = true;
  2393. }else{
  2394. if(xRange[0] < this.xRange[0]){
  2395. this.xRange[0] = xRange[0];
  2396. isChange = true;
  2397. }
  2398. if(xRange[1] > this.xRange[1]){
  2399. this.xRange[1] = xRange[1];
  2400. isChange = true;
  2401. }
  2402. }
  2403. if(isChange){
  2404. this.drawGraph();
  2405. }else{
  2406. this.drawLine(data);
  2407. }
  2408. }
  2409. //Removes a single data set from the graph and its line
  2410. //Inputs:
  2411. // id = the unique identifier of the data set that was passed in with addData function
  2412. removeData(name){
  2413. for(let i = 0; i < this.data.length; i++){
  2414. if(this.data[i].name === name){
  2415. this.data.splice(i, 1);
  2416. break;
  2417. }
  2418. }
  2419. this.drawGraph();
  2420. }
  2421. //Completely clears all data
  2422. //Does not delete the current graph displaying
  2423. clearData(){
  2424. this.max = 0;
  2425. this.data = [];
  2426. this.xRange = [];
  2427. }
  2428. addTitle(title){
  2429. this.top = this.canvas.clientHeight - (this.canvas.clientHeight * 0.9);
  2430. this.title = title;
  2431. }
  2432. /**********
  2433. *********PRIVATE*********
  2434. **********/
  2435. drawGraph(){
  2436. this.context.clearRect(0, 0, this.canvas.width, this.canvas.height);
  2437. this.drawYAxis();
  2438. this.drawXAxis();
  2439. for(let dataSet of this.data){
  2440. this.drawLine(dataSet);
  2441. }
  2442. if(this.title){
  2443. this.context.font = "25px Saira";
  2444. let xLocation = ((this.right - this.left) / 2) - (this.context.measureText(this.title).width / 2);
  2445. this.context.fillText(this.title, xLocation, this.top - 10);
  2446. }
  2447. }
  2448. drawLine(data){
  2449. for(let i = 0; i < data.set.length - 1; i++){
  2450. this.context.beginPath();
  2451. this.context.moveTo(this.left + (this.horizontalMultiplier * i), this.bottom - (this.verticalMultiplier * data.set[i]));
  2452. this.context.lineTo(this.left + (this.horizontalMultiplier * (i + 1)), this.bottom - (this.verticalMultiplier * data.set[i + 1]));
  2453. this.context.strokeStyle = this.colors[data.colorIndex];
  2454. this.context.lineWidth = 2;
  2455. this.context.stroke();
  2456. }
  2457. this.context.strokeStyle = "black";
  2458. if(this.data.length > 1){
  2459. this.drawLegend(data.colorIndex, data.name);
  2460. }
  2461. }
  2462. drawXAxis(){
  2463. this.context.beginPath();
  2464. this.context.moveTo(this.left, this.bottom);
  2465. this.context.lineTo(this.right, this.bottom);
  2466. this.context.lineWidth = 4;
  2467. this.context.stroke();
  2468. this.context.setLineDash([5, 10]);
  2469. this.context.font = "10px Arial";
  2470. this.context.lineWidth = 1;
  2471. if(Object.prototype.toString.call(this.xRange[0]) === '[object Date]'){
  2472. 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;
  2473. let showDate = new Date(this.xRange[0]);
  2474. for(let i = 0; i < diff; i += Math.floor(diff / 10)){
  2475. this.context.fillText(showDate.toLocaleDateString("en-US", {month: "short", day: "numeric", year: "2-digit"}), this.left + (this.horizontalMultiplier * i) - 20, this.bottom + 15);
  2476. if(i !== 0){
  2477. this.context.beginPath()
  2478. this.context.moveTo(this.left + (this.horizontalMultiplier * i), this.bottom);
  2479. this.context.lineTo(this.left + (this.horizontalMultiplier * i), this.top);
  2480. this.context.strokeStyle = "#a5a5a5";
  2481. this.context.stroke();
  2482. }
  2483. showDate.setDate(showDate.getDate() + Math.abs(diff / 10));
  2484. }
  2485. }
  2486. this.context.strokeStyle = "black";
  2487. this.context.setLineDash([]);
  2488. }
  2489. drawYAxis(){
  2490. this.context.beginPath();
  2491. this.context.moveTo(this.left, this.top);
  2492. this.context.lineTo(this.left, this.bottom);
  2493. this.context.lineWidth = 2;
  2494. this.context.stroke();
  2495. this.context.setLineDash([5, 10]);
  2496. this.context.font = "10px Arial";
  2497. this.context.lineWidth = 1;
  2498. let axisNum = 0;
  2499. let verticalIncrement = (this.bottom - this.top) / 10;
  2500. let verticalOffset = 0;
  2501. do{
  2502. this.context.fillText(Math.round(axisNum).toString(), this.left - 20, this.bottom - verticalOffset + 3);
  2503. this.context.beginPath();
  2504. this.context.moveTo(this.left, this.bottom - verticalOffset);
  2505. this.context.lineTo(this.right, this.bottom - verticalOffset);
  2506. this.context.strokeStyle = "#a5a5a5";
  2507. this.context.stroke();
  2508. verticalOffset += verticalIncrement;
  2509. axisNum += this.max / 10;
  2510. }while(verticalOffset <= (this.bottom - this.top));
  2511. this.context.strokeStyle = "black";
  2512. this.context.setLineDash([]);
  2513. }
  2514. drawLegend(colorIndex, name){
  2515. let verticalOffset;
  2516. for(let i = 0; i < this.data.length; i++){
  2517. if(this.data[i].name === name){
  2518. verticalOffset = i * 25;
  2519. break;
  2520. }
  2521. }
  2522. this.context.beginPath();
  2523. this.context.fillStyle = this.colors[colorIndex];
  2524. this.context.fillRect(this.right + 50, this.top + 50 + verticalOffset, 10, 10);
  2525. this.context.stroke();
  2526. this.context.font = "15px Arial";
  2527. this.context.fillText(name, this.right + 65, this.top + 60 + verticalOffset);
  2528. this.context.fillStyle = "black";
  2529. }
  2530. }
  2531. class HorizontalBarGraph{
  2532. constructor(canvas){
  2533. this.canvas = canvas;
  2534. this.context = canvas.getContext("2d");
  2535. this.left = 0;
  2536. this.right = canvas.clientWidth;
  2537. this.top = canvas.clientHeight - (canvas.clientHeight * 0.99);
  2538. this.bottom = canvas.clientHeight;
  2539. this.data = [];
  2540. this.max = 0;
  2541. }
  2542. //Adds an array of data points to the chart
  2543. //All data is removed and redrawn when called
  2544. //Must pass in all data points
  2545. //Inputs:
  2546. // dataArray: array of objects
  2547. // num: number for the actual data
  2548. // label: text to display on bar
  2549. addData(dataArray){
  2550. this.context.clearRect(0, 0, this.canvas.width, this.canvas.height);
  2551. for(let point of dataArray){
  2552. if(point.num > this.max){
  2553. this.max = point.num;
  2554. }
  2555. this.data.push(point);
  2556. }
  2557. this.drawGraph();
  2558. }
  2559. drawGraph(){
  2560. let barHeight = ((this.bottom - this.top) / this.data.length) - 2;
  2561. for(let i = 0; i < this.data.length; i++){
  2562. let topLocation = this.top + (i * barHeight) + 5;
  2563. let width = (this.right - this.left) * (this.data[i].num / this.max);
  2564. if(this.data[i].num >= this.max){
  2565. this.context.fillStyle = "rgb(255, 99, 107)";
  2566. }else{
  2567. this.context.fillStyle = "rgb(179, 191, 209)";
  2568. }
  2569. this.context.beginPath();
  2570. this.context.fillRect(this.left, topLocation, width, barHeight - 5);
  2571. this.context.stroke();
  2572. let textLocation = 15;
  2573. this.context.font = "12px Saira";
  2574. this.context.fillStyle = "black";
  2575. this.context.fillText(this.data[i].label, textLocation, (this.top) + (i * barHeight) + (barHeight / 1.5));
  2576. }
  2577. }
  2578. }
  2579. module.exports = {
  2580. LineGraph: LineGraph,
  2581. HorizontalBarGraph: HorizontalBarGraph
  2582. }
  2583. },{}]},{},[6]);