bundle.js 107 KB

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