bundle.js 130 KB

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