bundle.js 131 KB

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