bundle.js 115 KB

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