bundle.js 142 KB

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