bundle.js 108 KB

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