bundle.js 135 KB

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