bundle.js 140 KB

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