bundle.js 121 KB

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