bundle.js 107 KB

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