bundle.js 134 KB

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