bundle.js 104 KB

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