bundle.js 131 KB

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