bundle.js 119 KB

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