bundle.js 130 KB

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