bundle.js 106 KB

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