bundle.js 129 KB

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