bundle.js 115 KB

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