bundle.js 142 KB

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