bundle.js 109 KB

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