bundle.js 141 KB

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