bundle.js 141 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995
  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;
  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. break;
  1101. }
  1102. }
  1103. }
  1104. return quantity;
  1105. }
  1106. }
  1107. module.exports = Transaction;
  1108. },{}],6:[function(require,module,exports){
  1109. const home = require("./strands/home.js");
  1110. const ingredients = require("./strands/ingredients.js");
  1111. const recipeBook = require("./strands/recipeBook.js");
  1112. const analytics = require("./strands/analytics.js");
  1113. const orders = require("./strands/orders.js");
  1114. const transactions = require("./strands/transactions.js");
  1115. const ingredientDetails = require("./sidebars/ingredientDetails.js");
  1116. const newIngredient = require("./sidebars/newIngredient.js");
  1117. const editIngredient = require("./sidebars/editIngredient.js");
  1118. const newOrder = require("./sidebars/newOrder.js");
  1119. const newRecipe = require("./sidebars/newRecipe.js");
  1120. const editRecipe = require("./sidebars/editRecipe.js");
  1121. const newTransaction = require("./sidebars/newTransaction.js");
  1122. const orderDetails = require("./sidebars/orderDetails.js");
  1123. const orderFilter = require("./sidebars/orderFilter.js");
  1124. const orderCalculator = require("./sidebars/orderCalculator.js");
  1125. const recipeDetails = require("./sidebars/recipeDetails.js");
  1126. const transactionDetails = require("./sidebars/transactionDetails.js");
  1127. const transactionFilter = require("./sidebars/transactionFilter.js");
  1128. const Merchant = require("./classes/Merchant.js");
  1129. const Ingredient = require("./classes/Ingredient.js");
  1130. const Recipe = require("./classes/Recipe.js");
  1131. const Order = require("./classes/Order.js");
  1132. const Transaction = require("./classes/Transaction.js");
  1133. merchant = new Merchant(data.merchant, data.transactions, {
  1134. home: home,
  1135. ingredients: ingredients,
  1136. transactions: transactions,
  1137. recipeBook: recipeBook,
  1138. analytics: analytics,
  1139. orders: orders,
  1140. Ingredient: Ingredient,
  1141. Recipe: Recipe,
  1142. Transaction: Transaction
  1143. });
  1144. controller = {
  1145. openStrand: function(strand, data = undefined){
  1146. this.closeSidebar();
  1147. let strands = document.querySelectorAll(".strand");
  1148. for(let i = 0; i < strands.length; i++){
  1149. strands[i].style.display = "none";
  1150. }
  1151. let buttons = document.querySelectorAll(".menuButton");
  1152. for(let i = 0; i < buttons.length - 1; i++){
  1153. buttons[i].classList = "menuButton";
  1154. buttons[i].disabled = false;
  1155. }
  1156. let activeButton = {};
  1157. switch(strand){
  1158. case "home":
  1159. activeButton = document.getElementById("homeBtn");
  1160. document.getElementById("homeStrand").style.display = "flex";
  1161. home.display();
  1162. break;
  1163. case "ingredients":
  1164. activeButton = document.getElementById("ingredientsBtn");
  1165. document.getElementById("ingredientsStrand").style.display = "flex";
  1166. ingredients.display();
  1167. break;
  1168. case "recipeBook":
  1169. activeButton = document.getElementById("recipeBookBtn");
  1170. document.getElementById("recipeBookStrand").style.display = "flex";
  1171. recipeBook.display(Recipe);
  1172. break;
  1173. case "analytics":
  1174. activeButton = document.getElementById("analyticsBtn");
  1175. document.getElementById("analyticsStrand").style.display = "flex";
  1176. analytics.display(Transaction);
  1177. break;
  1178. case "orders":
  1179. activeButton = document.getElementById("ordersBtn");
  1180. document.getElementById("ordersStrand").style.display = "flex";
  1181. orders.orders = data;
  1182. orders.display(Order);
  1183. break;
  1184. case "transactions":
  1185. activeButton = document.getElementById("transactionsBtn");
  1186. document.getElementById("transactionsStrand").style.display = "flex";
  1187. transactions.transactions = data;
  1188. transactions.display(Transaction);
  1189. break;
  1190. }
  1191. activeButton.classList = "menuButton active";
  1192. activeButton.disabled = true;
  1193. if(window.screen.availWidth <= 1000){
  1194. this.closeMenu();
  1195. }
  1196. },
  1197. /*
  1198. Open a specific sidebar
  1199. Input:
  1200. sidebar: the outermost element of the sidebar (must contain class sidebar)
  1201. */
  1202. openSidebar: function(sidebar, data = {}){
  1203. this.closeSidebar();
  1204. document.getElementById("sidebarDiv").classList = "sidebar";
  1205. document.getElementById(sidebar).style.display = "flex";
  1206. switch(sidebar){
  1207. case "ingredientDetails":
  1208. ingredientDetails.display(data, ingredients);
  1209. break;
  1210. case "newIngredient":
  1211. newIngredient.display(Ingredient);
  1212. break;
  1213. case "editIngredient":
  1214. editIngredient.display(data);
  1215. break;
  1216. case "recipeDetails":
  1217. recipeDetails.display(data);
  1218. break;
  1219. case "editRecipe":
  1220. editRecipe.display(data);
  1221. break;
  1222. case "addRecipe":
  1223. newRecipe.display(Recipe);
  1224. break;
  1225. case "orderDetails":
  1226. orderDetails.display(data);
  1227. break;
  1228. case "orderFilter":
  1229. orderFilter.display(Order);
  1230. break;
  1231. case "newOrder":
  1232. newOrder.display();
  1233. break;
  1234. case "orderCalculator":
  1235. orderCalculator.display();
  1236. break;
  1237. case "transactionDetails":
  1238. transactionDetails.display(data);
  1239. break;
  1240. case "transactionFilter":
  1241. transactionFilter.display();
  1242. break;
  1243. case "newTransaction":
  1244. newTransaction.display(Transaction);
  1245. break;
  1246. }
  1247. if(window.screen.availWidth <= 1000){
  1248. document.querySelector(".contentBlock").style.display = "none";
  1249. document.getElementById("mobileMenuSelector").style.display = "none";
  1250. document.getElementById("sidebarCloser").style.display = "block";
  1251. }
  1252. },
  1253. closeSidebar: function(){
  1254. let sidebar = document.getElementById("sidebarDiv");
  1255. for(let i = 0; i < sidebar.children.length; i++){
  1256. if(sidebar.children[i].style.display !== "none"){
  1257. sidebar.children[i].style.display = "none";
  1258. let choosables = [];
  1259. switch(sidebar.children[i].id){
  1260. case "ingredientDetails":
  1261. choosables = document.querySelectorAll(".ingredient");
  1262. break;
  1263. case "transactionDetails":
  1264. choosables = document.getElementById("transactionsList").children;
  1265. break;
  1266. case "recipeDetails":
  1267. choosables = document.getElementById("recipeList").children;
  1268. break;
  1269. case "orderDetails":
  1270. choosables = document.getElementById("orderList").children;
  1271. break;
  1272. }
  1273. for(let i = 0; i < choosables.length; i++){
  1274. choosables[i].classList.remove("active");
  1275. }
  1276. }
  1277. }
  1278. sidebar.classList = "sidebarHide";
  1279. if(window.screen.availWidth <= 1000){
  1280. document.querySelector(".contentBlock").style.display = "flex";
  1281. document.getElementById("mobileMenuSelector").style.display = "block";
  1282. document.getElementById("sidebarCloser").style.display = "none";
  1283. }
  1284. },
  1285. openModal: function(str){
  1286. let modal = document.getElementById("modal");
  1287. modal.style.display = "flex";
  1288. document.getElementById("modalClose").addEventListener("click", this.closeModal);
  1289. let content = {};
  1290. switch(str){
  1291. case "ingredientSpreadsheet":
  1292. content = document.getElementById("modalSpreadsheetUpload");
  1293. content.style.display = "flex";
  1294. document.getElementById("modalSpreadsheetTitle").innerText = "ingredients";
  1295. document.getElementById("spreadsheetDownload").href = "/ingredients/download/spreadsheet";
  1296. content.onsubmit = newIngredient.submitSpreadsheet;
  1297. break;
  1298. case "recipeSpreadsheet":
  1299. content = document.getElementById("modalSpreadsheetUpload");
  1300. content.style.display = "flex";
  1301. document.getElementById("modalSpreadsheetTitle").innerText = "recipes";
  1302. document.getElementById("spreadsheetDownload").href = "/recipes/download/spreadsheet";
  1303. content.onsubmit = newRecipe.submitSpreadsheet;
  1304. break;
  1305. case "orderSpreadsheet":
  1306. content = document.getElementById("modalSpreadsheetUpload");
  1307. content.style.display = "flex";
  1308. document.getElementById("modalSpreadsheetTitle").innerText = "orders";
  1309. document.getElementById("spreadsheetDownload").href = "/orders/download/spreadsheet";
  1310. content.onsubmit = newOrder.submitSpreadsheet;
  1311. break;
  1312. case "transactionSpreadsheet":
  1313. content = document.getElementById("modalSpreadsheetUpload");
  1314. content.style.display = "flex";
  1315. document.getElementById("modalSpreadsheetTitle").innerText = "transactions";
  1316. document.getElementById("spreadsheetDownload").href = "/transactions/download/spreadsheet";
  1317. content.onsubmit = newTransaction.submitSpreadsheet;
  1318. }
  1319. },
  1320. closeModal: function(){
  1321. let modal = document.getElementById("modal");
  1322. let modalContent = document.getElementById("modalContent");
  1323. for(let i = 0; i < modalContent.children.length; i++){
  1324. modalContent.children[i].style.display = "none";
  1325. }
  1326. modal.style.display = "none";
  1327. },
  1328. createBanner: function(text, status){
  1329. let container = document.getElementById("bannerContainer");
  1330. let template = document.getElementById("banner").content.children[0];
  1331. let banner = template.cloneNode(true);
  1332. switch(status){
  1333. case "error":
  1334. banner.children[0].style.backgroundColor = "rgb(200, 0, 0)";
  1335. banner.children[0].children[0].style.display = "block";
  1336. break;
  1337. case "alert":
  1338. banner.children[0].style.backgroundColor = "rgb(230, 210, 0)";
  1339. banner.children[0].children[1].style.display = "block";
  1340. break;
  1341. case "success":
  1342. banner.children[0].style.backgroundColor = "rgb(0, 145, 55)";
  1343. banner.children[0].children[2].style.display = "block";
  1344. break;
  1345. }
  1346. banner.children[1].innerText = text;
  1347. container.appendChild(banner);
  1348. let timer = setTimeout(()=>{
  1349. container.removeChild(banner);
  1350. }, 10000);
  1351. banner.children[2].addEventListener("click", ()=>{
  1352. container.removeChild(banner);
  1353. clearTimeout(timer);
  1354. });
  1355. },
  1356. changeMenu: function(){
  1357. let menu = document.querySelector(".menu");
  1358. let buttons = document.querySelectorAll(".menuButton");
  1359. if(!menu.classList.contains("menuMinimized")){
  1360. menu.classList = "menu menuMinimized";
  1361. for(let i = 0; i < buttons.length; i++){
  1362. buttons[i].children[1].style.display = "none";
  1363. }
  1364. document.getElementById("max").style.display = "none";
  1365. document.getElementById("min").style.display = "flex";
  1366. }else if(menu.classList.contains("menuMinimized")){
  1367. menu.classList = "menu";
  1368. for(let i = 0; i < buttons.length; i++){
  1369. buttons[i].children[1].style.display = "block";
  1370. }
  1371. setTimeout(()=>{
  1372. document.getElementById("max").style.display = "flex";
  1373. document.getElementById("min").style.display = "none";
  1374. }, 150);
  1375. }
  1376. },
  1377. openMenu: function(){
  1378. document.getElementById("menu").style.display = "flex";
  1379. document.querySelector(".contentBlock").style.display = "none";
  1380. document.getElementById("mobileMenuSelector").onclick = ()=>{this.closeMenu()};
  1381. },
  1382. closeMenu: function(){
  1383. document.getElementById("menu").style.display = "none";
  1384. document.querySelector(".contentBlock").style.display = "flex";
  1385. document.getElementById("mobileMenuSelector").onclick = ()=>{this.openMenu()};
  1386. },
  1387. updateAnalytics: function(){
  1388. analytics.isPopulated = false;
  1389. }
  1390. }
  1391. if(window.screen.availWidth > 1000 && window.screen.availWidth <= 1400){
  1392. this.changeMenu();
  1393. document.getElementById("menuShifter2").style.display = "none";
  1394. }
  1395. //Add click listeners for menu buttons
  1396. document.getElementById("homeBtn").onclick = ()=>{controller.openStrand("home")};
  1397. document.getElementById("ingredientsBtn").onclick = ()=>{controller.openStrand("ingredients")};
  1398. document.getElementById("recipeBookBtn").onclick = ()=>{controller.openStrand("recipeBook")};
  1399. document.getElementById("analyticsBtn").onclick = ()=>{controller.openStrand("analytics")};
  1400. document.getElementById("ordersBtn").onclick = async ()=>{
  1401. if(merchant.orders.length === 0){
  1402. merchant.setOrders(await orders.getOrders(Order));
  1403. }
  1404. controller.openStrand("orders", merchant.orders);
  1405. }
  1406. document.getElementById("transactionsBtn").onclick = ()=>{controller.openStrand("transactions", merchant.getTransactions())};
  1407. controller.openStrand("home");
  1408. },{"./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){
  1409. let editIngredient = {
  1410. display: function(ingredient){
  1411. let buttonList = document.getElementById("unitButtons");
  1412. let quantLabel = document.getElementById("editIngQuantityLabel");
  1413. let specialLabel = document.getElementById("editSpecialLabel");
  1414. //Clear any existing data
  1415. while(buttonList.children.length > 0){
  1416. buttonList.removeChild(buttonList.firstChild);
  1417. }
  1418. //Populate basic fields
  1419. document.getElementById("editIngTitle").innerText = ingredient.ingredient.name;
  1420. document.getElementById("editIngName").value = ingredient.ingredient.name;
  1421. document.getElementById("editIngCategory").value = ingredient.ingredient.category;
  1422. quantLabel.innerText = `CURRENT STOCK (${ingredient.ingredient.unit.toUpperCase()})`;
  1423. document.getElementById("editIngSubmit").onclick = ()=>{this.submit(ingredient)};
  1424. //Populate the unit buttons
  1425. const units = merchant.units[ingredient.ingredient.unitType];
  1426. for(let i = 0; i < units.length; i++){
  1427. let button = document.createElement("button");
  1428. button.classList.add("unitButton");
  1429. button.innerText = units[i].toUpperCase();
  1430. button.onclick = ()=>{this.changeUnit(button)};
  1431. buttonList.appendChild(button);
  1432. if(units[i] === ingredient.ingredient.unit){
  1433. button.classList.add("unitActive");
  1434. }
  1435. }
  1436. //Make any changes for special ingredients
  1437. if(ingredient.ingredient.specialUnit === "bottle"){
  1438. quantLabel.innerText = "CURRENT STOCK (BOTTLES):";
  1439. specialLabel.style.display = "flex";
  1440. specialLabel.innerText = `BOTTLE SIZE (${ingredient.ingredient.unit.toUpperCase()}):`;
  1441. let sizeInput = document.createElement("input");
  1442. sizeInput.id = "editIngSpecialSize";
  1443. sizeInput.type = "number";
  1444. sizeInput.min = "0";
  1445. sizeInput.step = "0.01";
  1446. sizeInput.value = ingredient.ingredient.unitSize.toFixed(2);
  1447. specialLabel.appendChild(sizeInput);
  1448. }else{
  1449. specialLabel.style.display = "none";
  1450. }
  1451. let quantInput = document.createElement("input");
  1452. quantInput.id = "editIngQuantity";
  1453. quantInput.type = "number";
  1454. quantInput.min = "0";
  1455. quantInput.step = "0.01";
  1456. quantInput.value = ingredient.quantity.toFixed(2);
  1457. quantLabel.appendChild(quantInput);
  1458. },
  1459. changeUnit(button){
  1460. let buttons = document.getElementById("unitButtons");
  1461. for(let i = 0; i < buttons.children.length; i++){
  1462. buttons.children[i].classList.remove("unitActive");
  1463. }
  1464. button.classList.add("unitActive");
  1465. },
  1466. submit(ingredient){
  1467. const quantity = parseFloat(document.getElementById("editIngQuantityLabel").children[0].value);
  1468. let data = {
  1469. id: ingredient.ingredient.id,
  1470. name: document.getElementById("editIngName").value,
  1471. category: document.getElementById("editIngCategory").value
  1472. }
  1473. //Add data based on unit type
  1474. if(ingredient.ingredient.specialUnit === "bottle"){
  1475. let unitSize = ingredient.convertToBase(parseFloat(document.getElementById("editSpecialLabel").children[0].value));
  1476. data.quantity = quantity * unitSize;
  1477. data.unitSize = unitSize;
  1478. }else{
  1479. data.quantity = quantity;
  1480. }
  1481. //Get the measurement unit
  1482. let units = document.getElementById("unitButtons");
  1483. for(let i = 0; i < units.children.length; i++){
  1484. if(units.children[i].classList.contains("unitActive")){
  1485. data.unit = units.children[i].innerText.toLowerCase();
  1486. break;
  1487. }
  1488. }
  1489. let loader = document.getElementById("loaderContainer");
  1490. loader.style.display = "flex";
  1491. fetch("/ingredients/update", {
  1492. method: "put",
  1493. headers: {
  1494. "Content-Type": "application/json;charset=utf-8"
  1495. },
  1496. body: JSON.stringify(data)
  1497. })
  1498. .then(response => response.json())
  1499. .then((response)=>{
  1500. if(typeof(response) === "string"){
  1501. controller.createBanner(response, "error");
  1502. }else{
  1503. merchant.removeIngredient(merchant.getIngredient(response.ingredient._id));
  1504. merchant.addIngredient(response.ingredient, response.quantity, response.unit);
  1505. controller.openStrand("ingredients");
  1506. controller.createBanner("INGREDIENT UPDATED", "success");
  1507. }
  1508. })
  1509. .catch((err)=>{
  1510. controller.createBanner("SOMETHING WENT WRONG, PLEASE REFRESH THE PAGE", "error");
  1511. })
  1512. .finally(()=>{
  1513. loader.style.display = "none";
  1514. });
  1515. }
  1516. }
  1517. module.exports = editIngredient;
  1518. },{}],8:[function(require,module,exports){
  1519. let editRecipe = {
  1520. display: function(recipe){
  1521. let nameInput = document.getElementById("editRecipeName");
  1522. if(merchant.pos === "none"){
  1523. nameInput.value = recipe.name;
  1524. }else{
  1525. document.getElementById("editRecipeNoName").innertext = recipe.name;
  1526. nameInput.parentNode.style.display = "none";
  1527. }
  1528. //Populate ingredients
  1529. let ingredientList = document.getElementById("editRecipeIngList");
  1530. while(ingredientList.children.length > 0){
  1531. ingredientList.removeChild(ingredientList.firstChild);
  1532. }
  1533. let template = document.getElementById("editRecipeIng").content.children[0];
  1534. for(let i = 0; i < recipe.ingredients.length; i++){
  1535. let ingredientDiv = template.cloneNode(true);
  1536. ingredientDiv.children[0].onclick = ()=>{ingredientDiv.parentNode.removeChild(ingredientDiv)};
  1537. ingredientDiv.children[1].innerText = recipe.ingredients[i].ingredient.getNameAndUnit();
  1538. ingredientDiv.children[2].style.display = "none";
  1539. ingredientDiv.children[3].value = recipe.ingredients[i].quantity;
  1540. ingredientDiv.ingredient = recipe.ingredients[i];
  1541. ingredientList.appendChild(ingredientDiv);
  1542. }
  1543. document.getElementById("addRecIng").onclick = ()=>{this.newIngredient()};
  1544. document.getElementById("editRecipePrice").value = recipe.price;
  1545. document.getElementById("editRecipeSubmit").onclick = ()=>{this.submit(recipe)};
  1546. document.getElementById("editRecipeCancel").onclick = ()=>{controller.openSidebar("recipeDetails", recipe)};
  1547. },
  1548. newIngredient: function(){
  1549. let ingredientList = document.getElementById("editRecipeIngList");
  1550. let ingredientDiv = document.getElementById("editRecipeIng").content.children[0].cloneNode(true);
  1551. ingredientDiv.children[0].onclick = ()=>{ingredientDiv.parentNode.removeChild(ingredientDiv)};
  1552. ingredientDiv.children[1].style.display = "none";
  1553. ingredientDiv.children[3].value = "0.00";
  1554. //Populate selector
  1555. let categories = merchant.categorizeIngredients();
  1556. for(let i = 0; i < categories.length; i++){
  1557. let group = document.createElement("optgroup");
  1558. group.label = categories[i].name;
  1559. for(let j = 0; j < categories[i].ingredients.length; j++){
  1560. let option = document.createElement("option");
  1561. option.innerText = categories[i].ingredients[j].ingredient.getNameAndUnit();
  1562. option.ingredient = categories[i].ingredients[j];
  1563. group.appendChild(option);
  1564. }
  1565. ingredientDiv.children[2].appendChild(group);
  1566. }
  1567. ingredientList.appendChild(ingredientDiv);
  1568. },
  1569. submit: function(recipe){
  1570. let data = {
  1571. id: recipe.id,
  1572. name: recipe.name,
  1573. price: document.getElementById("editRecipePrice").value * 100,
  1574. ingredients: []
  1575. }
  1576. if(merchant.pos === "none"){
  1577. data.name = document.getElementById("editRecipeName").value;
  1578. }
  1579. let ingredients = document.getElementById("editRecipeIngList").children;
  1580. for(let i = 0; i < ingredients.length; i++){
  1581. const quantity = parseFloat(ingredients[i].children[3].value);
  1582. let newIngredient = {};
  1583. let ingredient = {};
  1584. if(ingredients[i].children[1].style.display === "none"){
  1585. let selector = ingredients[i].children[2];
  1586. ingredient = selector.options[selector.selectedIndex].ingredient;
  1587. newIngredient = {
  1588. ingredient: ingredient.ingredient.id,
  1589. quantity: ingredient.convertToBase(quantity)
  1590. };
  1591. }else{
  1592. ingredient = ingredients[i].ingredient;
  1593. newIngredient = {
  1594. ingredient: ingredient.ingredient.id,
  1595. quantity: ingredients[i].ingredient.convertToBase(quantity)
  1596. };
  1597. }
  1598. if(ingredient.ingredient.specialUnit === "bottle"){
  1599. newIngredient.quantity = quantity;
  1600. }
  1601. data.ingredients.push(newIngredient);
  1602. }
  1603. let loader = document.getElementById("loaderContainer");
  1604. loader.style.display = "flex";
  1605. fetch("/recipe/update", {
  1606. method: "put",
  1607. headers: {
  1608. "Content-Type": "application/json;charset=utf-8"
  1609. },
  1610. body: JSON.stringify(data)
  1611. })
  1612. .then(response => response.json())
  1613. .then((response)=>{
  1614. if(typeof(response) === "string"){
  1615. controller.createBanner(response, "error");
  1616. }else{
  1617. merchant.updateRecipe(response);
  1618. controller.openStrand("recipeBook");
  1619. controller.createBanner("RECIPE UPDATED", "success");
  1620. }
  1621. })
  1622. .catch((err)=>{
  1623. controller.createBanner("SOMETHING WENT WRONG, PLEASE REFRESH THE PAGE", "error");
  1624. })
  1625. .finally(()=>{
  1626. loader.style.display = "none";
  1627. });
  1628. }
  1629. }
  1630. module.exports = editRecipe;
  1631. },{}],9:[function(require,module,exports){
  1632. let ingredientDetails = {
  1633. dailyUse: 0,
  1634. display: function(ingredient){
  1635. document.getElementById("editIngBtn").onclick = ()=>{controller.openSidebar("editIngredient", ingredient)};
  1636. document.getElementById("removeIngBtn").onclick = ()=>{this.remove(ingredient)};
  1637. document.getElementById("ingredientDetailsCategory").innerText = ingredient.ingredient.category;
  1638. document.getElementById("ingredientDetailsName").innerText = ingredient.ingredient.name;
  1639. document.getElementById("ingredientStock").innerText = ingredient.getQuantityDisplay();
  1640. //Calculate and display average daily use
  1641. let quantities = [];
  1642. let now = new Date();
  1643. for(let i = 1; i < 31; i++){
  1644. let endDay = new Date(now.getFullYear(), now.getMonth(), now.getDate() - i)
  1645. let startDay = new Date(now.getFullYear(), now.getMonth(), now.getDate() - i - 1);
  1646. quantities.push(merchant.getSingleIngredientSold(ingredient, startDay, endDay));
  1647. }
  1648. let sum = 0;
  1649. for(let i = 0; i < quantities.length; i++){
  1650. sum += quantities[i];
  1651. }
  1652. let dailyUse = sum / quantities.length;
  1653. const dailyUseDiv = document.getElementById("dailyUse");
  1654. if(ingredient.ingredient.specialUnit === "bottle"){
  1655. dailyUseDiv.innerText = `${dailyUse.toFixed(2)} BOTTLES`;
  1656. }else{
  1657. dailyUseDiv.innerText = `${dailyUse.toFixed(2)} ${ingredient.ingredient.unit.toUpperCase()}`;
  1658. }
  1659. //Show recipes that this ingredient is a part of
  1660. let recipeList = document.getElementById("ingredientRecipeList");
  1661. let template = document.getElementById("ingredientRecipe").content.children[0];
  1662. let recipes = merchant.getRecipesForIngredient(ingredient.ingredient);
  1663. while(recipeList.children.length > 0){
  1664. recipeList.removeChild(recipeList.firstChild);
  1665. }
  1666. for(let i = 0; i < recipes.length; i++){
  1667. let recipeDiv = template.cloneNode(true);
  1668. recipeDiv.children[0].innerText = recipes[i].name;
  1669. recipeDiv.onclick = ()=>{
  1670. controller.openStrand("recipeBook");
  1671. controller.openSidebar("recipeDetails", recipes[i]);
  1672. }
  1673. recipeDiv.classList.add("choosable");
  1674. recipeList.appendChild(recipeDiv);
  1675. }
  1676. },
  1677. remove: function(ingredient){
  1678. for(let i = 0; i < merchant.recipes.length; i++){
  1679. for(let j = 0; j < merchant.recipes[i].ingredients.length; j++){
  1680. if(ingredient.ingredient === merchant.recipes[i].ingredients[j].ingredient){
  1681. controller.createBanner("MUST REMOVE INGREDIENT FROM ALL RECIPES BEFORE REMOVING FROM INVENTORY", "error");
  1682. return;
  1683. }
  1684. }
  1685. }
  1686. let loader = document.getElementById("loaderContainer");
  1687. loader.style.display = "flex";
  1688. fetch(`/ingredients/remove/${ingredient.ingredient.id}`, {
  1689. method: "delete",
  1690. })
  1691. .then((response) => response.json())
  1692. .then((response)=>{
  1693. if(typeof(response) === "string"){
  1694. controller.createBanner(response, "error");
  1695. }else{
  1696. merchant.removeIngredient(ingredient);
  1697. controller.openStrand("ingredients");
  1698. controller.createBanner("INGREDIENT REMOVED", "success");
  1699. }
  1700. })
  1701. .catch((err)=>{
  1702. controller.createBanner("SOMETHING WENT WRONG. PLEASE REFRESH THE PAGE", "error");
  1703. })
  1704. .finally(()=>{
  1705. loader.style.display = "none";
  1706. });
  1707. }
  1708. }
  1709. module.exports = ingredientDetails;
  1710. },{}],10:[function(require,module,exports){
  1711. let newIngredient = {
  1712. display: function(Ingredient){
  1713. const selector = document.getElementById("unitSelector");
  1714. document.getElementById("newIngName").value = "";
  1715. document.getElementById("newIngCategory").value = "";
  1716. document.getElementById("newIngQuantity").value = 0;
  1717. document.getElementById("bottleSizeLabel").style.display = "none";
  1718. selector.value = "g";
  1719. selector.onchange = ()=>{this.unitChange()};
  1720. document.getElementById("submitNewIng").onclick = ()=>{this.submit(Ingredient)};
  1721. document.getElementById("ingredientFileUpload").addEventListener("click", ()=>{controller.openModal("ingredientSpreadsheet")});
  1722. },
  1723. unitChange: function(){
  1724. const select = document.getElementById("unitSelector");
  1725. const bottleLabel = document.getElementById("bottleSizeLabel");
  1726. if(select.value === "bottle"){
  1727. bottleLabel.style.display = "block";
  1728. }else{
  1729. bottleLabel.style.display = "none";
  1730. }
  1731. },
  1732. submit: function(){
  1733. let unitSelector = document.getElementById("unitSelector");
  1734. let options = document.querySelectorAll("#unitSelector option");
  1735. const quantityValue = parseFloat(document.getElementById("newIngQuantity").value);
  1736. let unit = unitSelector.value;
  1737. let newIngredient = {
  1738. ingredient: {
  1739. name: document.getElementById("newIngName").value,
  1740. category: document.getElementById("newIngCategory").value,
  1741. unitType: options[unitSelector.selectedIndex].getAttribute("type")
  1742. },
  1743. quantity: quantityValue,
  1744. defaultUnit: unit
  1745. }
  1746. //Change the ingredient if it is a special unit type (ie "bottle")
  1747. if(unit === "bottle"){
  1748. newIngredient.ingredient.unitType = "volume";
  1749. newIngredient.ingredient.unitSize = document.getElementById("bottleSize").value;
  1750. newIngredient.defaultUnit = document.getElementById("bottleUnits").value;
  1751. newIngredient.ingredient.specialUnit = unit;
  1752. newIngredient.quantity = quantityValue;
  1753. }
  1754. let loader = document.getElementById("loaderContainer");
  1755. loader.style.display = "flex";
  1756. fetch("/ingredients/create", {
  1757. method: "POST",
  1758. headers: {
  1759. "Content-Type": "application/json;charset=utf-8"
  1760. },
  1761. body: JSON.stringify(newIngredient)
  1762. })
  1763. .then((response) => response.json())
  1764. .then((response)=>{
  1765. if(typeof(response) === "string"){
  1766. controller.createBanner(response, "error");
  1767. }else{
  1768. merchant.addIngredient(response.ingredient, response.quantity, response.defaultUnit);
  1769. controller.openStrand("ingredients");
  1770. controller.createBanner("INGREDIENT CREATED", "success");
  1771. }
  1772. })
  1773. .catch((err)=>{
  1774. controller.createBanner("SOMETHING WENT WRONG. PLEASE REFRESH THE PAGE", "error");
  1775. })
  1776. .finally(()=>{
  1777. loader.style.display = "none";
  1778. });
  1779. },
  1780. submitSpreadsheet: function(){
  1781. event.preventDefault();
  1782. controller.closeModal();
  1783. const file = document.getElementById("spreadsheetInput").files[0];
  1784. let data = new FormData();
  1785. data.append("ingredients", file);
  1786. let loader = document.getElementById("loaderContainer");
  1787. loader.style.display = "flex";
  1788. fetch("/ingredients/create/spreadsheet", {
  1789. method: "post",
  1790. body: data
  1791. })
  1792. .then(response => response.json())
  1793. .then((response)=>{
  1794. if(typeof(response) === "string"){
  1795. controller.createBanner(response, "error");
  1796. }else{
  1797. for(let i = 0; i < response.length; i++){
  1798. merchant.addIngredient(response[i].ingredient, response[i].quantity, response[i].defaultUnit);
  1799. controller.createBanner("INGREDINETS SUCCESSFULLY ADDED", "success");
  1800. controller.openStrand("ingredients");
  1801. }
  1802. }
  1803. })
  1804. .catch((err)=>{
  1805. controller.createBanner("SOMETHING WENT WRONG. TRY REFRESHING THE PAGE", "error");
  1806. })
  1807. .finally(()=>{
  1808. loader.style.display = "none";
  1809. });
  1810. }
  1811. }
  1812. module.exports = newIngredient;
  1813. },{}],11:[function(require,module,exports){
  1814. let newOrder = {
  1815. display: function(){
  1816. document.getElementById("sidebarDiv").classList.add("sidebarWide");
  1817. document.getElementById("newOrderIngredientList").style.display = "flex";
  1818. document.getElementById("orderFileUpload").addEventListener("click", ()=>{controller.openModal("orderSpreadsheet")});
  1819. let selectedList = document.getElementById("selectedIngredientList");
  1820. while(selectedList.children.length > 0){
  1821. selectedList.removeChild(selectedList.firstChild);
  1822. }
  1823. let ingredientList = document.getElementById("newOrderIngredients");
  1824. while(ingredientList.children.length > 0){
  1825. ingredientList.removeChild(ingredientList.firstChild);
  1826. }
  1827. for(let i = 0; i < merchant.ingredients.length; i++){
  1828. let ingredient = document.createElement("button");
  1829. ingredient.classList = "choosable";
  1830. ingredient.innerText = merchant.ingredients[i].ingredient.name;
  1831. ingredient.onclick = ()=>{this.addIngredient(merchant.ingredients[i], ingredient)};
  1832. ingredientList.appendChild(ingredient);
  1833. }
  1834. document.getElementById("submitNewOrder").onclick = ()=>{this.submit()};
  1835. },
  1836. addIngredient: function(ingredient, element){
  1837. element.style.display = "none";
  1838. let div = document.getElementById("selectedIngredient").content.children[0].cloneNode(true);
  1839. div.ingredient = ingredient;
  1840. div.children[0].children[1].onclick = ()=>{this.removeIngredient(div, element)};
  1841. //TODO: this should be handled by the class
  1842. //Display units depending on the whether it is a special unit
  1843. if(ingredient.ingredient.specialUnit === "bottle"){
  1844. div.children[0].children[0].innerText = `${ingredient.ingredient.name} (BOTTLES)`;
  1845. }else{
  1846. div.children[0].children[0].innerText = `${ingredient.ingredient.name} (${ingredient.ingredient.unit.toUpperCase()})`;
  1847. }
  1848. document.getElementById("selectedIngredientList").appendChild(div);
  1849. },
  1850. removeIngredient: function(selectedElement, element){
  1851. selectedElement.parentElement.removeChild(selectedElement);
  1852. element.style.display = "block";
  1853. },
  1854. submit: function(){
  1855. let date = document.getElementById("newOrderDate").value;
  1856. let taxes = document.getElementById("orderTaxes").value * 100;
  1857. let fees = document.getElementById("orderFees").value * 100;
  1858. let ingredients = document.getElementById("selectedIngredientList").children;
  1859. if(date === ""){
  1860. controller.createBanner("DATE IS REQUIRED FOR ORDERS", "error");
  1861. return;
  1862. }
  1863. let data = {
  1864. name: document.getElementById("newOrderName").value,
  1865. date: date,
  1866. taxes: taxes,
  1867. fees: fees,
  1868. ingredients: []
  1869. }
  1870. for(let i = 0; i < ingredients.length; i++){
  1871. let quantity = ingredients[i].children[1].children[0].value;
  1872. let price = ingredients[i].children[1].children[1].value;
  1873. if(ingredients[i].ingredient.ingredient.specialUnit === "bottle"){
  1874. data.ingredients.push({
  1875. ingredient: ingredients[i].ingredient.ingredient.id,
  1876. quantity: quantity * ingredients[i].ingredient.ingredient.getBaseUnitSize(),
  1877. pricePerUnit: this.convertPrice(ingredients[i].ingredient.ingredient, price * 100)
  1878. });
  1879. }else{
  1880. data.ingredients.push({
  1881. ingredient: ingredients[i].ingredient.ingredient.id,
  1882. quantity: ingredients[i].ingredient.convertToBase(quantity),
  1883. pricePerUnit: this.convertPrice(ingredients[i].ingredient.ingredient, price * 100)
  1884. });
  1885. }
  1886. }
  1887. let loader = document.getElementById("loaderContainer");
  1888. loader.style.display = "flex";
  1889. fetch("/order/create", {
  1890. method: "post",
  1891. headers: {
  1892. "Content-Type": "application/json;charset=utf-8"
  1893. },
  1894. body: JSON.stringify(data)
  1895. })
  1896. .then((response)=>response.json())
  1897. .then((response)=>{
  1898. if(typeof(response) === "string"){
  1899. controller.createBanner(response, "error");
  1900. }else{
  1901. merchant.addOrder(response, true);
  1902. controller.openStrand("orders", merchant.orders);
  1903. controller.createBanner("NEW ORDER CREATED", "success");
  1904. }
  1905. })
  1906. .catch((err)=>{
  1907. controller.createBanner("SOMETHING WENT WRONG, PLEASE REFRESH THE PAGE", "error");
  1908. })
  1909. .finally(()=>{
  1910. loader.style.display = "none";
  1911. });
  1912. },
  1913. //TODO: Remove this function, it should be on the order
  1914. convertPrice: function(ingredient, price){
  1915. if(ingredient.specialUnit === "bottle"){
  1916. return price / ingredient.getBaseUnitSize();
  1917. }
  1918. switch(ingredient.unit){
  1919. case "g": return price;
  1920. case "kg": return price / 1000;
  1921. case "oz": return price / 28.3495;
  1922. case "lb": return price / 453.5924;
  1923. case "ml": return price * 1000;
  1924. case "l": return price;
  1925. case "tsp": return price * 202.8842;
  1926. case "tbsp": return price * 67.6278;
  1927. case "ozfl": return price * 33.8141;
  1928. case "cup": return price * 4.1667;
  1929. case "pt": return price * 2.1134;
  1930. case "qt": return price * 1.0567;
  1931. case "gal": return price / 3.7854;
  1932. case "mm": return price * 1000;
  1933. case "cm": return price * 100;
  1934. case "m": return price;
  1935. case "in": return price * 39.3701;
  1936. case "ft": return price * 3.2808;
  1937. }
  1938. },
  1939. submitSpreadsheet: function(){
  1940. event.preventDefault();
  1941. controller.closeModal();
  1942. const file = document.getElementById("spreadsheetInput").files[0];
  1943. let data = new FormData();
  1944. data.append("orders", file);
  1945. data.append("timeOffset", new Date().getTimezoneOffset());
  1946. let loader = document.getElementById("loaderContainer");
  1947. loader.style.display = "flex";
  1948. fetch("/orders/create/spreadsheet", {
  1949. method: "post",
  1950. body: data
  1951. })
  1952. .then(response => response.json())
  1953. .then((response)=>{
  1954. if(typeof(response) === "string"){
  1955. controller.createBanner(response, "error");
  1956. }else{
  1957. for(let i = 0; i < response.length; i++){
  1958. merchant.addOrder(response[i], true);
  1959. }
  1960. controller.createBanner("ORDER CREATED AND INGREDIENTS UPDATED SUCCESSFULLY", "success");
  1961. controller.openStrand("orders");
  1962. }
  1963. })
  1964. .catch((err)=>{
  1965. controller.createBanner("UNABLE TO DISPLAY NEW ORDER. PLEASE REFRESH THE PAGE.", "error");
  1966. })
  1967. .finally(()=>{
  1968. loader.style.display = "none";
  1969. });
  1970. }
  1971. }
  1972. module.exports = newOrder;
  1973. },{}],12:[function(require,module,exports){
  1974. let newRecipe = {
  1975. display: function(Recipe){
  1976. document.getElementById("newRecipeName").value = "";
  1977. document.getElementById("newRecipePrice").value = "";
  1978. document.getElementById("ingredientCount").value = 1;
  1979. let categories = merchant.categorizeIngredients();
  1980. let ingredientsSelect = document.getElementById("recipeInputIngredients");
  1981. while(ingredientsSelect.children.length > 0){
  1982. ingredientsSelect.removeChild(ingredientsSelect.firstChild);
  1983. }
  1984. this.changeIngredientCount(categories);
  1985. document.getElementById("ingredientCount").onchange = ()=>{this.changeIngredientCount(categories)};
  1986. document.getElementById("submitNewRecipe").onclick = ()=>{this.submit(Recipe)};
  1987. document.getElementById("recipeFileUpload").onclick = ()=>{controller.openModal("recipeSpreadsheet")};
  1988. },
  1989. //Updates the number of ingredient inputs displayed for new recipes
  1990. changeIngredientCount: function(categories){
  1991. let newCount = document.getElementById("ingredientCount").value;
  1992. let ingredientsDiv = document.getElementById("recipeInputIngredients");
  1993. let template = document.getElementById("recipeInputIngredient").content.children[0];
  1994. let oldCount = ingredientsDiv.children.length;
  1995. if(newCount > oldCount){
  1996. let newDivs = newCount - oldCount;
  1997. for(let i = 0; i < newDivs; i++){
  1998. let newNode = template.cloneNode(true);
  1999. newNode.children[0].innnerText = `INGREDIENT ${i + oldCount}`;
  2000. newNode.children[2].children[0].value = 0;
  2001. for(let j = 0; j < categories.length; j++){
  2002. let optgroup = document.createElement("optgroup");
  2003. optgroup.label = categories[j].name;
  2004. for(let k = 0; k < categories[j].ingredients.length; k++){
  2005. let option = document.createElement("option");
  2006. option.innerText = categories[j].ingredients[k].ingredient.getNameAndUnit();
  2007. option.ingredient = categories[j].ingredients[k];
  2008. optgroup.appendChild(option);
  2009. }
  2010. newNode.children[1].children[0].appendChild(optgroup);
  2011. }
  2012. ingredientsDiv.appendChild(newNode);
  2013. }
  2014. for(let i = 0; i < newCount; i++){
  2015. ingredientsDiv.children[i].children[0].innerText = `INGREDIENT ${i + 1}`;
  2016. }
  2017. }else if(newCount < oldCount){
  2018. let newDivs = oldCount - newCount;
  2019. for(let i = 0; i < newDivs; i++){
  2020. ingredientsDiv.removeChild(ingredientsDiv.children[ingredientsDiv.children.length-1]);
  2021. }
  2022. }
  2023. },
  2024. submit: function(Recipe){
  2025. let newRecipe = {
  2026. name: document.getElementById("newRecipeName").value,
  2027. price: document.getElementById("newRecipePrice").value,
  2028. ingredients: []
  2029. }
  2030. let inputs = document.getElementById("recipeInputIngredients").children;
  2031. for(let i = 0; i < inputs.length; i++){
  2032. let sel = inputs[i].children[1].children[0];
  2033. let ingredient = sel.options[sel.selectedIndex].ingredient;
  2034. let newIngredient = {
  2035. ingredient: ingredient.ingredient.id,
  2036. quantity: ingredient.convertToBase(inputs[i].children[2].children[0].value)
  2037. };
  2038. if(ingredient.ingredient.specialUnit === "bottle"){
  2039. newIngredient.quantity = inputs[i].children[2].children[0].value;
  2040. }
  2041. newRecipe.ingredients.push(newIngredient);
  2042. }
  2043. let loader = document.getElementById("loaderContainer");
  2044. loader.style.display = "flex";
  2045. fetch("/recipe/create", {
  2046. method: "POST",
  2047. headers: {
  2048. "Content-Type": "application/json;charset=utf-8"
  2049. },
  2050. body: JSON.stringify(newRecipe)
  2051. })
  2052. .then((response) => response.json())
  2053. .then((response)=>{
  2054. if(typeof(response) === "string"){
  2055. controller.createBanner(response, "error");
  2056. }else{
  2057. let ingredients = [];
  2058. for(let i = 0; i < response.ingredients.length; i++){
  2059. for(let j = 0; j < merchant.ingredients.length; j++){
  2060. if(merchant.ingredients[j].ingredient.id === response.ingredients[i].ingredient){
  2061. ingredients.push({
  2062. ingredient: merchant.ingredients[j].ingredient.id,
  2063. quantity: response.ingredients[i].quantity
  2064. });
  2065. break;
  2066. }
  2067. }
  2068. }
  2069. merchant.addRecipe(
  2070. response._id,
  2071. response.name,
  2072. response.price,
  2073. ingredients
  2074. );
  2075. controller.createBanner("RECIPE CREATED", "success");
  2076. controller.openStrand("recipeBook");
  2077. }
  2078. })
  2079. .catch((err)=>{
  2080. controller.createBanner("SOMETHING WENT WRONG. PLEASE REFRESH THE PAGE", "error");
  2081. })
  2082. .finally(()=>{
  2083. loader.style.display = "none";
  2084. });
  2085. },
  2086. submitSpreadsheet: function(){
  2087. event.preventDefault();
  2088. controller.closeModal();
  2089. const file = document.getElementById("spreadsheetInput").files[0];
  2090. let data = new FormData();
  2091. data.append("recipes", file);
  2092. let loader = document.getElementById("loaderContainer");
  2093. loader.style.display = "flex";
  2094. fetch("/recipes/create/spreadsheet", {
  2095. method: "post",
  2096. body: data
  2097. })
  2098. .then(response => response.json())
  2099. .then((response)=>{
  2100. if(typeof(response) === "string"){
  2101. controller.createBanner(response, "error");
  2102. }else{
  2103. for(let i = 0; i < response.length; i++){
  2104. merchant.addRecipe(
  2105. response[i]._id,
  2106. response[i].name,
  2107. response[i].price,
  2108. response[i].ingredients
  2109. );
  2110. }
  2111. controller.createBanner("ALL INGREDIENTS SUCCESSFULLY CREATED", "success");
  2112. controller.openStrand("recipeBook");
  2113. }
  2114. })
  2115. .catch((err)=>{
  2116. controller.createBanner("UNABLE TO DISPLAY NEW RECIPES. PLEASE REFRESH THE PAGE", "error");
  2117. })
  2118. .finally(()=>{
  2119. loader.style.display = "none";
  2120. });
  2121. }
  2122. }
  2123. module.exports = newRecipe;
  2124. },{}],13:[function(require,module,exports){
  2125. let newTransaction = {
  2126. display: function(Transaction){
  2127. let recipeList = document.getElementById("newTransactionRecipes");
  2128. let template = document.getElementById("createTransaction").content.children[0];
  2129. document.getElementById("transactionFileUpload").addEventListener("click", ()=>{controller.openModal("transactionSpreadsheet")});
  2130. while(recipeList.children.length > 0){
  2131. recipeList.removeChild(recipeList.firstChild);
  2132. }
  2133. for(let i = 0; i < merchant.recipes.length; i++){
  2134. let recipeDiv = template.cloneNode(true);
  2135. recipeDiv.recipe = merchant.recipes[i];
  2136. recipeList.appendChild(recipeDiv);
  2137. recipeDiv.children[0].innerText = merchant.recipes[i].name;
  2138. }
  2139. document.getElementById("submitNewTransaction").onclick = ()=>{this.submit(Transaction)};
  2140. },
  2141. submit: function(Transaction){
  2142. let recipeDivs = document.getElementById("newTransactionRecipes");
  2143. let date = document.getElementById("newTransactionDate").valueAsDate;
  2144. if(date > new Date()){
  2145. controller.createBanner("CANNOT HAVE A DATE IN THE FUTURE", "error");
  2146. return;
  2147. }
  2148. let data = {
  2149. date: date,
  2150. recipes: [],
  2151. ingredientUpdates: {}
  2152. };
  2153. for(let i = 0; i < recipeDivs.children.length; i++){
  2154. let quantity = recipeDivs.children[i].children[1].value;
  2155. const recipe = recipeDivs.children[i].recipe;
  2156. if(quantity !== "" && quantity > 0){
  2157. data.recipes.push({
  2158. recipe: recipe.id,
  2159. quantity: quantity
  2160. });
  2161. for(let j = 0; j < recipe.ingredients.length; j++){
  2162. let ingredient = recipe.ingredients[j];
  2163. if(data.ingredientUpdates[ingredient.ingredient.id]){
  2164. data.ingredientUpdates[ingredient.ingredient.id] += ingredient.convertToBase(ingredient.quantity) * quantity;
  2165. }else{
  2166. data.ingredientUpdates[ingredient.ingredient.id] = ingredient.convertToBase(ingredient.quantity) * quantity;
  2167. }
  2168. }
  2169. }else if(quantity < 0){
  2170. controller.createBanner("CANNOT HAVE NEGATIVE VALUES", "error");
  2171. return;
  2172. }
  2173. }
  2174. if(data.recipes.length > 0){
  2175. let loader = document.getElementById("loaderContainer");
  2176. loader.style.display = "flex";
  2177. fetch("/transaction/create", {
  2178. method: "post",
  2179. headers: {
  2180. "Content-Type": "application/json;charset=utf-8"
  2181. },
  2182. body: JSON.stringify(data)
  2183. })
  2184. .then(response => response.json())
  2185. .then((response)=>{
  2186. if(typeof(response) === "string"){
  2187. controller.createBanner(response, "error");
  2188. }else{
  2189. merchant.addTransaction(response);
  2190. controller.updateAnalytics();
  2191. controller.openStrand("transactions", merchant.getTransactions());
  2192. controller.createBanner("TRANSACTION CREATED", "success");
  2193. }
  2194. })
  2195. .catch((err)=>{
  2196. controller.createBanner("SOMETHING WENT WRONG. PLEASE REFRESH THE PAGE", "error");
  2197. })
  2198. .finally(()=>{
  2199. loader.style.display = "none";
  2200. });
  2201. }
  2202. },
  2203. submitSpreadsheet: function(){
  2204. event.preventDefault();
  2205. controller.closeModal();
  2206. const file = document.getElementById("spreadsheetInput").files[0];
  2207. let data = new FormData();
  2208. data.append("transactions", file);
  2209. data.append("timeOffset", new Date().getTimezoneOffset());
  2210. let loader = document.getElementById("loaderContainer");
  2211. loader.style.display = "flex";
  2212. fetch("/transactions/create/spreadsheet", {
  2213. method: "post",
  2214. body: data
  2215. })
  2216. .then(response => response.json())
  2217. .then((response)=>{
  2218. if(typeof(response) === "string"){
  2219. controller.createBanner(response, "error");
  2220. }else{
  2221. for(let i = 0; i < response.recipes.length; i++){
  2222. response.recipes[i].recipe = response.recipes[i].recipe._id;
  2223. }
  2224. merchant.addTransaction(response);
  2225. controller.updateAnalytics();
  2226. controller.openStrand("transactions", merchant.transactions);
  2227. controller.createBanner("TRANSACTION SUCCESSFULLY CREATED. INGREDIENTS UPDATED", "success");
  2228. }
  2229. })
  2230. .catch((err)=>{
  2231. controller.createBanner("UNABLE TO DISPLAY NEW TRANSACTIONS. PLEASE REFRESH THE PAGE", "error");
  2232. })
  2233. .finally(()=>{
  2234. loader.style.display = "none";
  2235. });
  2236. }
  2237. }
  2238. module.exports = newTransaction;
  2239. },{}],14:[function(require,module,exports){
  2240. let orderCalculator = {
  2241. display: function(){
  2242. let calculatorItems = document.getElementById("calculatorItemsBody");
  2243. let template = document.getElementById("calculatorItem").content.children[0];
  2244. let calculations = this.predict();
  2245. while(calculatorItems.children.length > 0){
  2246. calculatorItems.removeChild(calculatorItems.firstChild);
  2247. }
  2248. for(let i = 0; i < calculations.length; i++){
  2249. let item = template.cloneNode(true);
  2250. item.children[0].innerText = calculations[i].ingredient.name,
  2251. item.children[1].innerText = `${calculations[i].output.toFixed(2)} ${calculations[i].ingredient.unit.toUpperCase()}`;
  2252. calculatorItems.appendChild(item);
  2253. }
  2254. },
  2255. predict: function(){
  2256. let now = new Date();
  2257. let yesterday = new Date();
  2258. yesterday.setHours(0, 0, 0, 0);
  2259. let monthAgo = new Date(now.getFullYear(), now.getMonth(), now.getDate() - 30);
  2260. let weekAgo = new Date(now.getFullYear(), now.getMonth(), now.getDate() - 7);
  2261. let calculations = [];
  2262. let month = merchant.getIngredientsSold(monthAgo, yesterday);
  2263. let week = merchant.getIngredientsSold(weekAgo, yesterday);
  2264. let weights = {
  2265. month: 0.33,
  2266. week: 0.67
  2267. }
  2268. for(let i = 0; i < month.length; i++){
  2269. for(let j = 0; j < week.length; j++){
  2270. if(month[i].ingredient.id === week[j].ingredient.id){
  2271. let monthAverage = (month[i].quantity / 30) * weights.month;
  2272. let weekAverage = (week[i].quantity / 7) * weights.week;
  2273. let calc = {
  2274. ingredient: month[i].ingredient,
  2275. output: monthAverage + weekAverage
  2276. };
  2277. calculations.push(calc);
  2278. }
  2279. }
  2280. }
  2281. return calculations;
  2282. }
  2283. }
  2284. module.exports = orderCalculator;
  2285. },{}],15:[function(require,module,exports){
  2286. let orderDetails = {
  2287. display: function(order){
  2288. document.getElementById("removeOrderBtn").onclick = ()=>{this.remove(order)};
  2289. document.getElementById("orderDetailName").innerText = order.name;
  2290. document.getElementById("orderDetailDate").innerText = order.date.toLocaleDateString("en-US");
  2291. document.getElementById("orderDetailTax").innerText = `$${order.taxes.toFixed(2)}`;
  2292. document.getElementById("orderDetailFee").innerText = `$${order.fees.toFixed(2)}`;
  2293. let ingredientList = document.getElementById("orderIngredients");
  2294. while(ingredientList.children.length > 0){
  2295. ingredientList.removeChild(ingredientList.firstChild);
  2296. }
  2297. let template = document.getElementById("orderIngredient").content.children[0];
  2298. for(let i = 0; i < order.ingredients.length; i++){
  2299. let ingredientDiv = template.cloneNode(true);
  2300. const ingredient = order.ingredients[i].ingredient;
  2301. ingredientDiv.children[0].innerText = order.ingredients[i].ingredient.name;
  2302. ingredientDiv.children[2].innerText = `$${order.ingredients[i].cost().toFixed(2)}`;
  2303. ingredientDiv.onclick = ()=>{
  2304. controller.openStrand("ingredients");
  2305. controller.openSidebar("ingredientDetails", merchant.getIngredient(order.ingredients[i].ingredient.id));
  2306. }
  2307. let ingredientDisplay = ingredientDiv.children[1];
  2308. if(ingredient.specialUnit === "bottle"){
  2309. ingredientDisplay.innerText = `${order.ingredients[i].quantity.toFixed(2)} bottles x $${order.ingredients[i].pricePerUnit.toFixed(2)}`;
  2310. }else{
  2311. ingredientDisplay.innerText = `${order.ingredients[i].quantity.toFixed(2)} ${ingredient.unit.toUpperCase()} X $${order.ingredients[i].pricePerUnit.toFixed(2)}`;
  2312. }
  2313. ingredientList.appendChild(ingredientDiv);
  2314. }
  2315. document.getElementById("orderDetailTotal").innerText = `$${order.getIngredientCost().toFixed(2)}`;
  2316. document.querySelector("#orderTotalPrice p").innerText = `$${order.getTotalCost().toFixed(2)}`;
  2317. },
  2318. remove: function(order){
  2319. let loader = document.getElementById("loaderContainer");
  2320. loader.style.display = "flex";
  2321. fetch(`/order/${order.id}`, {
  2322. method: "DELETE",
  2323. headers: {
  2324. "Content-Type": "application/json;charset=utf-8"
  2325. }
  2326. })
  2327. .then((response) => response.json())
  2328. .then((response)=>{
  2329. if(typeof(response) === "string"){
  2330. controller.createBanner(response, "error");
  2331. }else{
  2332. merchant.removeOrder(order);
  2333. controller.openStrand("orders", merchant.orders);
  2334. controller.createBanner("ORDER REMOVED", "success");
  2335. }
  2336. })
  2337. .catch((err)=>{
  2338. controller.createBanner("SOMETHING WENT WRONG. PLEASE REFRESH THE PAGE", "error");
  2339. })
  2340. .finally(()=>{
  2341. loader.style.display = "none";
  2342. });
  2343. }
  2344. }
  2345. module.exports = orderDetails;
  2346. },{}],16:[function(require,module,exports){
  2347. let orderFilter = {
  2348. display: function(Order){
  2349. let now = new Date();
  2350. let past = new Date(now.getFullYear(), now.getMonth(), now.getDate() - 30);
  2351. let ingredientList = document.getElementById("orderFilterIngredients");
  2352. document.getElementById("orderFilterDateFrom").valueAsDate = past;
  2353. document.getElementById("orderFilterDateTo").valueAsDate = now;
  2354. while(ingredientList.children.length > 0){
  2355. ingredientList.removeChild(ingredientList.firstChild);
  2356. }
  2357. for(let i = 0; i < merchant.ingredients.length; i++){
  2358. let element = document.createElement("div");
  2359. element.classList.add("choosable");
  2360. element.ingredient = merchant.ingredients[i].ingredient.id;
  2361. element.onclick = ()=>{this.toggleActive(element)};
  2362. ingredientList.appendChild(element);
  2363. let text = document.createElement("p");
  2364. text.innerText = merchant.ingredients[i].ingredient.name;
  2365. element.appendChild(text);
  2366. }
  2367. document.getElementById("orderFilterSubmit").onclick = ()=>{this.submit(Order)};
  2368. },
  2369. toggleActive: function(element){
  2370. if(element.classList.contains("active")){
  2371. element.classList.remove("active");
  2372. }else{
  2373. element.classList.add("active");
  2374. }
  2375. },
  2376. submit: function(Order){
  2377. let data = {
  2378. startDate: document.getElementById("orderFilterDateFrom").valueAsDate,
  2379. endDate: document.getElementById("orderFilterDateTo").valueAsDate,
  2380. ingredients: []
  2381. }
  2382. if(data.startDate >= data.endDate){
  2383. controller.createBanner("START DATE CANNOT BE AFTER END DATE", "error");
  2384. return;
  2385. }
  2386. let ingredients = document.getElementById("orderFilterIngredients").children;
  2387. for(let i = 0; i < ingredients.length; i++){
  2388. if(ingredients[i].classList.contains("active")){
  2389. data.ingredients.push(ingredients[i].ingredient);
  2390. }
  2391. }
  2392. if(data.ingredients.length === 0){
  2393. for(let i = 0; i < merchant.ingredients.length; i++){
  2394. data.ingredients.push(merchant.ingredients[i].ingredient.id);
  2395. }
  2396. }
  2397. let loader = document.getElementById("loaderContainer");
  2398. loader.style.display = "flex";
  2399. fetch("/order", {
  2400. method: "post",
  2401. headers: {
  2402. "Content-Type": "application/json;charset=utf-8"
  2403. },
  2404. body: JSON.stringify(data)
  2405. })
  2406. .then(response => response.json())
  2407. .then((response)=>{
  2408. let orders = [];
  2409. if(typeof(response) === "string"){
  2410. controller.createBanner(response, "error");
  2411. }else if(response.length === 0){
  2412. controller.createBanner("NO ORDERS MATCH YOUR SEARCH", "error");
  2413. }else{
  2414. for(let i = 0; i < response.length; i++){
  2415. orders.push(new Order(
  2416. response[i]._id,
  2417. response[i].name,
  2418. response[i].date,
  2419. response[i].taxes,
  2420. response[i].fees,
  2421. response[i].ingredients,
  2422. merchant
  2423. ));
  2424. }
  2425. }
  2426. controller.openStrand("orders", orders);
  2427. })
  2428. .catch((err)=>{
  2429. controller.createBanner("UNABLE TO DISPLAY THE ORDERS", "error");
  2430. })
  2431. .finally(()=>{
  2432. loader.style.display = "none";
  2433. });
  2434. }
  2435. }
  2436. module.exports = orderFilter;
  2437. },{}],17:[function(require,module,exports){
  2438. let recipeDetails = {
  2439. display: function(recipe){
  2440. document.getElementById("editRecipeBtn").onclick = ()=>{controller.openSidebar("editRecipe", recipe)};
  2441. document.getElementById("recipeName").innerText = recipe.name;
  2442. if(merchant.pos === "none"){
  2443. document.getElementById("removeRecipeBtn").onclick = ()=>{this.remove(recipe)};
  2444. }
  2445. //ingredient list
  2446. let ingredientsDiv = document.getElementById("recipeIngredientList");
  2447. while(ingredientsDiv.children.length > 0){
  2448. ingredientsDiv.removeChild(ingredientsDiv.firstChild);
  2449. }
  2450. let template = document.getElementById("recipeIngredient").content.children[0];
  2451. for(let i = 0; i < recipe.ingredients.length; i++){
  2452. let recipeDiv = template.cloneNode(true);
  2453. recipeDiv.children[0].innerText = recipe.ingredients[i].ingredient.name;
  2454. recipeDiv.children[1].innerText = `${recipe.ingredients[i].getQuantityDisplay()}`;
  2455. recipeDiv.onclick = ()=>{
  2456. controller.openStrand("ingredients");
  2457. controller.openSidebar("ingredientDetails", merchant.getIngredient(recipe.ingredients[i].ingredient.id));
  2458. }
  2459. ingredientsDiv.appendChild(recipeDiv);
  2460. }
  2461. document.getElementById("recipePrice").children[1].innerText = `$${recipe.price.toFixed(2)}`;
  2462. },
  2463. remove: function(recipe){
  2464. let loader = document.getElementById("loaderContainer");
  2465. loader.style.display = "flex";
  2466. fetch(`/recipe/remove/${recipe.id}`, {
  2467. method: "delete"
  2468. })
  2469. .then((response) => response.json())
  2470. .then((response)=>{
  2471. if(typeof(response) === "string"){
  2472. controller.createBanner(response, "error");
  2473. }else{
  2474. merchant.removeRecipe(recipe);
  2475. controller.createBanner("RECIPE REMOVED", "success");
  2476. controller.openStrand("recipeBook");
  2477. }
  2478. })
  2479. .catch((err)=>{
  2480. controller.createBanner("SOMETHING WENT WRONG. PLEASE REFRESH THE PAGE", "error");
  2481. })
  2482. .finally(()=>{
  2483. loader.style.display = "none";
  2484. });
  2485. }
  2486. }
  2487. module.exports = recipeDetails;
  2488. },{}],18:[function(require,module,exports){
  2489. let transactionDetails = {
  2490. transaction: {},
  2491. display: function(transaction){
  2492. this.transaction = transaction;
  2493. let recipeList = document.getElementById("transactionRecipes");
  2494. let template = document.getElementById("transactionRecipe").content.children[0];
  2495. let totalRecipes = 0;
  2496. let totalPrice = 0;
  2497. while(recipeList.children.length > 0){
  2498. recipeList.removeChild(recipeList.firstChild);
  2499. }
  2500. for(let i = 0; i < transaction.recipes.length; i++){
  2501. let recipe = template.cloneNode(true);
  2502. let price = transaction.recipes[i].quantity * transaction.recipes[i].recipe.price;
  2503. recipe.children[0].innerText = transaction.recipes[i].recipe.name;
  2504. recipe.children[1].innerText = `${transaction.recipes[i].quantity} x $${transaction.recipes[i].recipe.price.toFixed(2)}`;
  2505. recipe.children[2].innerText = `$${price.toFixed(2)}`;
  2506. recipe.onclick = ()=>{
  2507. controller.openStrand("recipeBook");
  2508. controller.openSidebar("recipeDetails", transaction.recipes[i].recipe);
  2509. }
  2510. recipeList.appendChild(recipe);
  2511. totalRecipes += transaction.recipes[i].quantity;
  2512. totalPrice += price;
  2513. }
  2514. let months = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
  2515. let days = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"];
  2516. let dateString = `${days[transaction.date.getDay()]}, ${months[transaction.date.getMonth()]} ${transaction.date.getDate()}, ${transaction.date.getFullYear()}`;
  2517. document.getElementById("transactionDate").innerText = dateString;
  2518. document.getElementById("transactionTime").innerText = transaction.date.toLocaleTimeString();
  2519. document.getElementById("totalRecipes").innerText = `${totalRecipes} recipes`;
  2520. document.getElementById("totalPrice").innerText = `$${totalPrice.toFixed(2)}`;
  2521. if(merchant.pos === "none"){
  2522. document.getElementById("removeTransBtn").onclick = ()=>{this.remove()};
  2523. }
  2524. },
  2525. remove: function(){
  2526. let loader = document.getElementById("loaderContainer");
  2527. loader.style.display = "flex";
  2528. fetch(`/transaction/${this.transaction.id}`, {
  2529. method: "delete",
  2530. headers: {
  2531. "Content-Type": "application/json;charset=utf-8"
  2532. },
  2533. })
  2534. .then((response)=>{
  2535. if(typeof(response) === "string"){
  2536. controller.createBanner(response, "error");
  2537. }else{
  2538. merchant.removeTransaction(this.transaction);
  2539. controller.updateAnalytics();
  2540. controller.openStrand("transactions", merchant.getTransactions());
  2541. controller.createBanner("TRANSACTION REMOVED", "success");
  2542. }
  2543. })
  2544. .catch((err)=>{
  2545. controller.createBanner("SOMETHING WENT WRONG. PLEASE REFRESH THE PAGE", "error");
  2546. })
  2547. .finally(()=>{
  2548. loader.style.display = "none";
  2549. });
  2550. },
  2551. }
  2552. module.exports = transactionDetails;
  2553. },{}],19:[function(require,module,exports){
  2554. let transactionFilter = {
  2555. display: function(){
  2556. //Set default dates
  2557. let today = new Date();
  2558. let monthAgo = new Date(today);
  2559. monthAgo.setMonth(today.getMonth() - 1);
  2560. document.getElementById("transFilterDateStart").valueAsDate = monthAgo;
  2561. document.getElementById("transFilterDateEnd").valueAsDate = today;
  2562. //populate recipes
  2563. let recipeList = document.getElementById("transFilterRecipeList");
  2564. while(recipeList.children.length > 0){
  2565. recipeList.removeChild(recipeList.firstChild);
  2566. }
  2567. for(let i = 0; i < merchant.recipes.length; i++){
  2568. let recipe = document.createElement("div");
  2569. recipe.innerText = merchant.recipes[i].name;
  2570. recipe.recipe = merchant.recipes[i];
  2571. recipe.classList.add("choosable");
  2572. recipe.onclick = ()=>{this.toggleActive(recipe)};
  2573. recipeList.appendChild(recipe);
  2574. }
  2575. //Submit button
  2576. document.getElementById("transFilterSubmit").onclick = ()=>{this.submit()};
  2577. },
  2578. toggleActive: function(element){
  2579. if(element.classList.contains("active")){
  2580. element.classList.remove("active");
  2581. }else{
  2582. element.classList.add("active");
  2583. }
  2584. },
  2585. submit: function(){
  2586. let data = {
  2587. startDate: document.getElementById("transFilterDateStart").valueAsDate,
  2588. endDate: document.getElementById("transFilterDateEnd").valueAsDate,
  2589. recipes: []
  2590. }
  2591. if(data.startDate >= data.endDate){
  2592. controller.createBanner("START DATE CANNOT BE AFTER END DATE", "error");
  2593. return;
  2594. }
  2595. let recipes = document.getElementById("transFilterRecipeList").children;
  2596. for(let i = 0; i < recipes.length; i++){
  2597. if(recipes[i].classList.contains("active")){
  2598. data.recipes.push(recipes[i].recipe.id);
  2599. }
  2600. }
  2601. if(data.recipes.length === 0){
  2602. for(let i = 0; i < merchant.recipes.length; i++){
  2603. data.recipes.push(merchant.recipes[i].id);
  2604. }
  2605. }
  2606. let loader = document.getElementById("loaderContainer");
  2607. loader.style.display = "flex";
  2608. fetch("/transaction", {
  2609. method: "post",
  2610. headers: {
  2611. "Content-Type": "application/json;charset=utf-8"
  2612. },
  2613. body: JSON.stringify(data)
  2614. })
  2615. .then(response => response.json())
  2616. .then((response)=>{
  2617. let transactions = [];
  2618. if(typeof(response) === "string"){
  2619. controller.createBanner(response, "error");
  2620. }else if(response.length === 0){
  2621. controller.createBanner("NO TRANSACTIONS MATCH YOUR SEARCH", "error");
  2622. }else{
  2623. for(let i = 0; i < response.length; i++){
  2624. transactions.push(new Transaction(
  2625. response[i]._id,
  2626. response[i].date,
  2627. response[i].recipes,
  2628. merchant
  2629. ));
  2630. }
  2631. }
  2632. controller.openStrand("transactions", transactions);
  2633. })
  2634. .catch((err)=>{
  2635. controller.createBanner("SOMETHING WENT WRONG. PLEASE REFRESH THE PAGE", "error");
  2636. })
  2637. .finally(()=>{
  2638. loader.style.display = "none";
  2639. });
  2640. }
  2641. }
  2642. module.exports = transactionFilter;
  2643. },{}],20:[function(require,module,exports){
  2644. let analytics = {
  2645. isPopulated: false,
  2646. ingredient: undefined,
  2647. recipe: undefined,
  2648. transactionsByDate: [],
  2649. display: function(Transaction){
  2650. if(!this.isPopulated){
  2651. document.getElementById("analRecipeContent").style.display = "none";
  2652. let to = new Date()
  2653. let from = new Date(to.getFullYear(), to.getMonth() - 1, to.getDate());
  2654. document.getElementById("analStartDate").valueAsDate = from;
  2655. document.getElementById("analEndDate").valueAsDate = to;
  2656. let analSlider = document.getElementById("analSlider");
  2657. analSlider.onclick = ()=>{this.switchDisplay()};
  2658. analSlider.checked = false;
  2659. document.getElementById("analDateBtn").onclick = ()=>{this.newDates(Transaction)};
  2660. this.populateButtons();
  2661. if(merchant.ingredients.length > 0){
  2662. this.ingredient = merchant.ingredients[0].ingredient;
  2663. }
  2664. if(merchant.recipes.length > 0){
  2665. this.recipe = merchant.recipes[0];
  2666. }
  2667. this.newDates(Transaction);
  2668. this.isPopulated = true;
  2669. }
  2670. },
  2671. populateButtons: function(){
  2672. let ingredientButtons = document.getElementById("analIngredientList");
  2673. let recipeButtons = document.getElementById("analRecipeList");
  2674. for(let i = 0; i < merchant.ingredients.length; i++){
  2675. let button = document.createElement("button");
  2676. button.innerText = merchant.ingredients[i].ingredient.name;
  2677. button.classList.add("choosable");
  2678. button.onclick = ()=>{
  2679. this.ingredient = merchant.ingredients[i].ingredient;
  2680. this.displayIngredient()
  2681. };
  2682. ingredientButtons.appendChild(button);
  2683. }
  2684. for(let i = 0; i < merchant.recipes.length; i++){
  2685. let button = document.createElement("button");
  2686. button.innerText = merchant.recipes[i].name;
  2687. button.classList.add("choosable");
  2688. button.onclick = ()=>{
  2689. this.recipe = merchant.recipes[i];
  2690. this.displayRecipe();
  2691. };
  2692. recipeButtons.appendChild(button);
  2693. }
  2694. },
  2695. getData: function(from, to, Transaction){
  2696. let loader = document.getElementById("loaderContainer");
  2697. loader.style.display = "flex";
  2698. return fetch(`/transactions/${from.toISOString()}/${to.toISOString()}`)
  2699. .then(response => response.json())
  2700. .then((response)=>{
  2701. if(typeof(response) === "string"){
  2702. controller.createBanner(response, "error");
  2703. }else{
  2704. this.transactionsByDate = [];
  2705. let startOfDay = new Date(from.getTime());
  2706. startOfDay.setHours(0, 0, 0, 0);
  2707. let endOfDay = new Date(from.getTime());
  2708. endOfDay.setDate(endOfDay.getDate() + 1);
  2709. endOfDay.setHours(0, 0, 0, 0);
  2710. let transactionIndex = 0;
  2711. while(startOfDay <= to){
  2712. let currentTransactions = [];
  2713. while(transactionIndex < response.length && new Date(response[transactionIndex].date) < endOfDay){
  2714. currentTransactions.push(new Transaction(
  2715. response[transactionIndex]._id,
  2716. response[transactionIndex].date,
  2717. response[transactionIndex].recipes,
  2718. merchant
  2719. ));
  2720. transactionIndex++;
  2721. }
  2722. let thing = {
  2723. date: new Date(startOfDay.getTime()),
  2724. transactions: currentTransactions
  2725. };
  2726. this.transactionsByDate.push(thing);
  2727. startOfDay.setDate(startOfDay.getDate() + 1);
  2728. endOfDay.setDate(endOfDay.getDate() + 1);
  2729. }
  2730. }
  2731. })
  2732. .catch((err)=>{
  2733. controller.createBanner("UNABLE TO UPDATE THE PAGE", "error");
  2734. })
  2735. .finally(()=>{
  2736. loader.style.display = "none";
  2737. });
  2738. },
  2739. displayIngredient: function(){
  2740. if(this.ingredient === undefined || this.transactionsByDate.length === 0){
  2741. return;
  2742. }
  2743. //break down data into dates and quantities
  2744. let dates = [];
  2745. let quantities = [];
  2746. for(let i = 0; i < this.transactionsByDate.length; i++){
  2747. dates.push(this.transactionsByDate[i].date);
  2748. let sum = 0;
  2749. for(let j = 0; j < this.transactionsByDate[i].transactions.length; j++){
  2750. let transaction = this.transactionsByDate[i].transactions[j];
  2751. sum += transaction.getIngredientQuantity(this.ingredient);
  2752. }
  2753. quantities.push(sum);
  2754. }
  2755. //create and display the graph
  2756. let trace = {
  2757. x: dates,
  2758. y: quantities,
  2759. mode: "lines+markers",
  2760. line: {
  2761. color: "rgb(255, 99, 107)"
  2762. }
  2763. }
  2764. let yaxis = `QUANTITY (${this.ingredient.unit.toUpperCase()})`;
  2765. if(this.ingredient.specialUnit === "bottle"){
  2766. yaxis = `QUANTITY (${this.ingredient.specialUnit.toUpperCase()})`
  2767. }
  2768. const layout = {
  2769. title: this.ingredient.name.toUpperCase(),
  2770. xaxis: {title: "DATE"},
  2771. yaxis: {title: yaxis}
  2772. }
  2773. Plotly.newPlot("itemUseGraph", [trace], layout);
  2774. //Create min/max/avg
  2775. //Current ingredient is stored on the "analMinUse" element
  2776. let min = quantities[0];
  2777. let max = quantities[0];
  2778. let sum = 0;
  2779. for(let i = 0; i < quantities.length; i++){
  2780. if(quantities[i] < min){
  2781. min = quantities[i];
  2782. }
  2783. if(quantities[i] > max){
  2784. max = quantities[i];
  2785. }
  2786. sum += quantities[i];
  2787. }
  2788. document.getElementById("analMinUse").innerText = `${min.toFixed(2)} ${this.ingredient.unit.toUpperCase()}`;
  2789. document.getElementById("analAvgUse").innerText = `${(sum / quantities.length).toFixed(2)} ${this.ingredient.unit.toUpperCase()}`;
  2790. document.getElementById("analMaxUse").innerText = `${max.toFixed(2)} ${this.ingredient.unit.toUpperCase()}`;
  2791. //Create weekday averages
  2792. let dayUse = [0, 0, 0, 0, 0, 0, 0];
  2793. let dayCount = [0, 0, 0, 0, 0, 0, 0];
  2794. for(let i = 0; i < quantities.length; i++){
  2795. dayUse[dates[i].getDay()] += quantities[i];
  2796. dayCount[dates[i].getDay()]++;
  2797. }
  2798. document.getElementById("analDayOne").innerText = `${(dayUse[0] / dayCount[0]).toFixed(2)} ${this.ingredient.unit.toUpperCase()}`;
  2799. document.getElementById("analDayTwo").innerText = `${(dayUse[1] / dayCount[1]).toFixed(2)} ${this.ingredient.unit.toUpperCase()}`;
  2800. document.getElementById("analDayThree").innerText = `${(dayUse[2] / dayCount[2]).toFixed(2)} ${this.ingredient.unit.toUpperCase()}`;
  2801. document.getElementById("analDayFour").innerText = `${(dayUse[3] / dayCount[3]).toFixed(2)} ${this.ingredient.unit.toUpperCase()}`;
  2802. document.getElementById("analDayFive").innerText = `${(dayUse[4] / dayCount[4]).toFixed(2)} ${this.ingredient.unit.toUpperCase()}`;
  2803. document.getElementById("analDaySix").innerText = `${(dayUse[5] / dayCount[5]).toFixed(2)} ${this.ingredient.unit.toUpperCase()}`;
  2804. document.getElementById("analDaySeven").innerText = `${(dayUse[6] / dayCount[6]).toFixed(2)} ${this.ingredient.unit.toUpperCase()}`;
  2805. },
  2806. displayRecipe: function(){
  2807. if(this.recipe === undefined || this.transactionsByDate.length === 0){
  2808. return;
  2809. }
  2810. //break down data into dates and quantities
  2811. let dates = [];
  2812. let quantities = [];
  2813. for(let i = 0; i < this.transactionsByDate.length; i++){
  2814. dates.push(this.transactionsByDate[i].date);
  2815. let sum = 0;
  2816. for(let j = 0; j < this.transactionsByDate[i].transactions.length; j++){
  2817. const transaction = this.transactionsByDate[i].transactions[j];
  2818. for(let k = 0; k < transaction.recipes.length; k++){
  2819. if(transaction.recipes[k].recipe === this.recipe){
  2820. sum += transaction.recipes[k].quantity;
  2821. }
  2822. }
  2823. }
  2824. quantities.push(sum);
  2825. }
  2826. //create and display the graph
  2827. const trace = {
  2828. x: dates,
  2829. y: quantities,
  2830. mode: "lines+markers",
  2831. line: {
  2832. color: "rgb(255, 99, 107)"
  2833. }
  2834. }
  2835. const layout = {
  2836. title: this.recipe.name.toUpperCase(),
  2837. xaxis: {title: "DATE"},
  2838. yaxis: {title: "QUANTITY"}
  2839. }
  2840. Plotly.newPlot("recipeSalesGraph", [trace], layout);
  2841. //Display the boxes at the bottom
  2842. //Current recipe is stored on the "recipeAvgUse" element
  2843. let avg = 0;
  2844. for(let i = 0; i < quantities.length; i++){
  2845. avg += quantities[i];
  2846. }
  2847. avg = avg / quantities.length;
  2848. document.getElementById("recipeAvgUse").innerText = avg.toFixed(2);
  2849. document.getElementById("recipeAvgRevenue").innerText = `$${(avg * this.recipe.price).toFixed(2)}`;
  2850. },
  2851. switchDisplay: function(){
  2852. const checkbox = document.getElementById("analSlider");
  2853. let ingredient = document.getElementById("analIngredientContent");
  2854. let recipe = document.getElementById("analRecipeContent");
  2855. if(checkbox.checked === true){
  2856. ingredient.style.display = "none";
  2857. recipe.style.display = "flex";
  2858. this.displayRecipe();
  2859. }else{
  2860. ingredient.style.display = "flex";
  2861. recipe.style.display = "none";
  2862. this.displayIngredient();
  2863. }
  2864. },
  2865. newDates: async function(Transaction){
  2866. const from = document.getElementById("analStartDate").valueAsDate;
  2867. const to = document.getElementById("analEndDate").valueAsDate;
  2868. from.setHours(0, 0, 0, 0);
  2869. to.setDate(to.getDate() + 1);
  2870. to.setHours(0, 0, 0, 0);
  2871. await this.getData(from, to, Transaction);
  2872. if(document.getElementById("analSlider").checked === true){
  2873. this.displayRecipe();
  2874. }else{
  2875. this.displayIngredient();
  2876. }
  2877. }
  2878. }
  2879. module.exports = analytics;
  2880. },{}],21:[function(require,module,exports){
  2881. let home = {
  2882. isPopulated: false,
  2883. display: function(){
  2884. if(!this.isPopulated){
  2885. this.drawRevenueCard();
  2886. this.drawRevenueGraph();
  2887. this.drawInventoryCheckCard();
  2888. this.drawPopularCard();
  2889. this.isPopulated = true;
  2890. }
  2891. },
  2892. drawRevenueCard: function(){
  2893. let today = new Date();
  2894. let firstOfMonth = new Date(today.getFullYear(), today.getMonth(), 1);
  2895. let firstOfLastMonth = new Date(today.getFullYear(), today.getMonth() - 1, 1);
  2896. let lastMonthToDay = new Date(new Date().setMonth(today.getMonth() - 1));
  2897. const revenueThisMonth = merchant.getRevenue(firstOfMonth);
  2898. const revenueLastMonthToDay = merchant.getRevenue(firstOfLastMonth, lastMonthToDay);
  2899. document.getElementById("revenue").innerText = `$${revenueThisMonth.toFixed(2)}`;
  2900. let revenueChange = ((revenueThisMonth - revenueLastMonthToDay) / revenueLastMonthToDay) * 100;
  2901. let img = "";
  2902. if(revenueChange >= 0){
  2903. img = "/shared/images/upArrow.png";
  2904. }else{
  2905. img = "/shared/images/downArrow.png";
  2906. }
  2907. document.querySelector("#revenueChange p").innerText = `${Math.abs(revenueChange).toFixed(2)}% vs last month`;
  2908. document.querySelector("#revenueChange img").src = img;
  2909. },
  2910. drawRevenueGraph: function(){
  2911. let monthAgo = new Date();
  2912. monthAgo.setMonth(monthAgo.getMonth() - 1);
  2913. let revenue = [];
  2914. let dates = [];
  2915. let dayRevenue = 0;
  2916. const transactions = merchant.getTransactions(monthAgo);
  2917. let currentDate = (transactions.length > 0) ? transactions[0].date : undefined;
  2918. for(let i = 0; i < transactions.length; i++){
  2919. if(transactions[i].date.getDate() !== currentDate.getDate()){
  2920. revenue.push(dayRevenue / 100);
  2921. dayRevenue = 0;
  2922. dates.push(currentDate);
  2923. currentDate = transactions[i].date;
  2924. }
  2925. for(let j = 0; j < transactions[i].recipes.length; j++){
  2926. const recipe = transactions[i].recipes[j];
  2927. dayRevenue += recipe.recipe.price * recipe.quantity;
  2928. }
  2929. }
  2930. const trace = {
  2931. x: dates,
  2932. y: revenue,
  2933. mode: "lines+markers",
  2934. line: {
  2935. color: "rgb(255, 99, 107)"
  2936. }
  2937. }
  2938. const layout = {
  2939. title: "REVENUE",
  2940. xaxis: {
  2941. title: "DATE"
  2942. },
  2943. yaxis: {
  2944. title: "$"
  2945. }
  2946. }
  2947. Plotly.newPlot("graphCard", [trace], layout);
  2948. },
  2949. drawInventoryCheckCard: function(){
  2950. let num;
  2951. if(merchant.ingredients.length < 5){
  2952. num = merchant.ingredients.length;
  2953. }else{
  2954. num = 5;
  2955. }
  2956. let rands = [];
  2957. for(let i = 0; i < num; i++){
  2958. let rand = Math.floor(Math.random() * merchant.ingredients.length);
  2959. if(rands.includes(rand)){
  2960. i--;
  2961. }else{
  2962. rands[i] = rand;
  2963. }
  2964. }
  2965. let ul = document.querySelector("#inventoryCheckCard ul");
  2966. let template = document.getElementById("ingredientCheck").content.children[0];
  2967. while(ul.children.length > 0){
  2968. ul.removeChild(ul.firstChild);
  2969. }
  2970. for(let i = 0; i < rands.length; i++){
  2971. let ingredientCheck = template.cloneNode(true);
  2972. let input = ingredientCheck.children[1].children[1];
  2973. const ingredient = merchant.ingredients[rands[i]];
  2974. ingredientCheck.ingredient = ingredient;
  2975. ingredientCheck.children[0].innerText = ingredient.ingredient.name;
  2976. ingredientCheck.children[1].children[0].onclick = ()=>{
  2977. input.value--;
  2978. input.changed = true;
  2979. };
  2980. if(ingredient.ingredient.specialUnit === "bottle"){
  2981. input.value = ingredient.quantity.toFixed(2);
  2982. ingredientCheck.children[2].innerText = "BOTTLES";
  2983. }else{
  2984. input.value = ingredient.quantity.toFixed(2);
  2985. ingredientCheck.children[2].innerText = ingredient.ingredient.unit.toUpperCase();
  2986. }
  2987. ingredientCheck.children[1].children[2].onclick = ()=>{
  2988. input.value++;
  2989. input.changed = true;
  2990. }
  2991. input.onchange = ()=>{input.changed = true};
  2992. ul.appendChild(ingredientCheck);
  2993. }
  2994. document.getElementById("inventoryCheck").onclick = ()=>{this.submitInventoryCheck()};
  2995. },
  2996. drawPopularCard: function(){
  2997. let thisMonth = new Date();
  2998. thisMonth.setDate(1);
  2999. const ingredientList = merchant.getIngredientsSold(thisMonth);
  3000. if(ingredientList !== false){
  3001. ingredientList.sort((a, b)=>{
  3002. if(a.quantity < b.quantity){
  3003. return 1;
  3004. }
  3005. if(a.quantity > b.quantity){
  3006. return -1;
  3007. }
  3008. return 0;
  3009. });
  3010. let quantities = [];
  3011. let labels = [];
  3012. let colors = [];
  3013. let count = (ingredientList.length < 5) ? ingredientList.length - 1 : 4;
  3014. for(let i = count; i >= 0; i--){
  3015. const ingredientName = ingredientList[i].ingredient.name;
  3016. const ingredientQuantity = ingredientList[i].quantity;
  3017. const unitName = ingredientList[i].ingredient.unit;
  3018. quantities.push(ingredientList[i].quantity);
  3019. labels.push(`${ingredientName}: ${ingredientQuantity.toFixed(2)} ${unitName.toUpperCase()}`);
  3020. if(i === 0){
  3021. colors.push("rgb(255, 99, 107");
  3022. }else{
  3023. colors.push("rgb(179, 191, 209");
  3024. }
  3025. }
  3026. let trace = {
  3027. x: quantities,
  3028. type: "bar",
  3029. orientation: "h",
  3030. text: labels,
  3031. textposition: "auto",
  3032. hoverinfo: "none",
  3033. marker: {
  3034. color: colors
  3035. }
  3036. }
  3037. let layout = {
  3038. title: "MOST POPULAR INGREDIENTS",
  3039. xaxis: {
  3040. zeroline: false,
  3041. title: "QUANTITY"
  3042. },
  3043. yaxis: {
  3044. showticklabels: false
  3045. }
  3046. }
  3047. Plotly.newPlot("popularIngredientsCard", [trace], layout);
  3048. }else{
  3049. document.getElementById("popularCanvas").style.display = "none";
  3050. let notice = document.createElement("p");
  3051. notice.innerText = "N/A";
  3052. notice.classList = "notice";
  3053. document.getElementById("popularIngredientsCard").appendChild(notice);
  3054. }
  3055. },
  3056. //Need to change the updating of ingredients
  3057. //should update the ingredient directly, then send that. Maybe...
  3058. submitInventoryCheck: function(){
  3059. let lis = document.querySelectorAll("#inventoryCheckCard li");
  3060. let data = [];
  3061. for(let i = 0; i < lis.length; i++){
  3062. if(lis[i].children[1].children[1].value >= 0){
  3063. if(lis[i].children[1].children[1].changed === true){
  3064. let merchIngredient = lis[i].ingredient;
  3065. data.push({
  3066. id: merchIngredient.ingredient.id,
  3067. quantity: lis[i].children[1].children[1].value
  3068. });
  3069. lis[i].children[1].children[1].changed = false;
  3070. }
  3071. }else{
  3072. controller.createBanner("CANNOT HAVE NEGATIVE INGREDIENTS", "error");
  3073. return;
  3074. }
  3075. }
  3076. if(data.length > 0){
  3077. let loader = document.getElementById("loaderContainer");
  3078. loader.style.display = "flex";
  3079. fetch("/merchant/ingredients/update", {
  3080. method: "PUT",
  3081. headers: {
  3082. "Content-Type": "application/json;charset=utf-8"
  3083. },
  3084. body: JSON.stringify(data)
  3085. })
  3086. .then(response => response.json())
  3087. .then((response)=>{
  3088. if(typeof(response) === "string"){
  3089. controller.createBanner(response, "error");
  3090. }else{
  3091. for(let i = 0; i < response.length; i++){
  3092. merchant.removeIngredient(merchant.getIngredient(response[i].ingredient._id));
  3093. merchant.addIngredient(response[i].ingredient, response[i].quantity, response[i].defaultUnit);
  3094. }
  3095. controller.createBanner("INGREDIENTS UPDATED", "success");
  3096. }
  3097. })
  3098. .catch((err)=>{
  3099. controller.createBanner("SOMETHING WENT WRONG. PLEASE REFRESH THE PAGE", "error");
  3100. })
  3101. .finally(()=>{
  3102. loader.style.display = "none";
  3103. });
  3104. }
  3105. }
  3106. }
  3107. module.exports = home;
  3108. },{}],22:[function(require,module,exports){
  3109. let ingredients = {
  3110. isPopulated: false,
  3111. ingredients: [],
  3112. display: function(){
  3113. if(!this.isPopulated){
  3114. document.getElementById("ingredientSearch").oninput = ()=>{this.search()};
  3115. this.populateByProperty();
  3116. this.isPopulated = true;
  3117. }
  3118. },
  3119. populateByProperty: function(){
  3120. let categories;
  3121. categories = merchant.categorizeIngredients();
  3122. let ingredientStrand = document.getElementById("categoryList");
  3123. let categoryTemplate = document.getElementById("categoryDiv").content.children[0];
  3124. let ingredientTemplate = document.getElementById("ingredient").content.children[0];
  3125. this.ingredients = [];
  3126. while(ingredientStrand.children.length > 0){
  3127. ingredientStrand.removeChild(ingredientStrand.firstChild);
  3128. }
  3129. for(let i = 0; i < categories.length; i++){
  3130. let categoryDiv = categoryTemplate.cloneNode(true);
  3131. categoryDiv.children[0].children[0].innerText = categories[i].name.toUpperCase();
  3132. categoryDiv.children[0].onclick = ()=>{
  3133. this.toggleCategory(categoryDiv.children[1], categoryDiv.children[0].children[1]);
  3134. };
  3135. categoryDiv.children[1].style.display = "none";
  3136. ingredientStrand.appendChild(categoryDiv);
  3137. for(let j = 0; j < categories[i].ingredients.length; j++){
  3138. let ingredient = categories[i].ingredients[j];
  3139. let ingredientDiv = ingredientTemplate.cloneNode(true);
  3140. ingredientDiv.children[0].innerText = ingredient.ingredient.name;
  3141. ingredientDiv.onclick = ()=>{
  3142. controller.openSidebar("ingredientDetails", ingredient);
  3143. ingredientDiv.classList.add("active");
  3144. };
  3145. ingredientDiv._name = ingredient.ingredient.name.toLowerCase();
  3146. ingredientDiv._unit = ingredient.ingredient.unit.toLowerCase();
  3147. if(ingredient.ingredient.specialUnit === "bottle"){
  3148. ingredientDiv.children[2].innerText = `${ingredient.quantity.toFixed(2)} BOTTLES`
  3149. }else{
  3150. ingredientDiv.children[2].innerText = `${ingredient.quantity.toFixed(2)} ${ingredient.ingredient.unit.toUpperCase()}`;
  3151. }
  3152. categoryDiv.children[1].appendChild(ingredientDiv);
  3153. this.ingredients.push(ingredientDiv);
  3154. }
  3155. }
  3156. },
  3157. displayIngredientsOnly: function(ingredients){
  3158. let ingredientDiv = document.getElementById("categoryList");
  3159. while(ingredientDiv.children.length > 0){
  3160. ingredientDiv.removeChild(ingredientDiv.firstChild);
  3161. }
  3162. for(let i = 0; i < ingredients.length; i++){
  3163. ingredientDiv.appendChild(ingredients[i]);
  3164. }
  3165. },
  3166. toggleCategory: function(div, button){
  3167. if(div.style.display === "none"){
  3168. 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>';
  3169. div.style.display = "flex";
  3170. }else if(div.style.display === "flex"){
  3171. 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>';
  3172. div.style.display = "none";
  3173. }
  3174. },
  3175. search: function(){
  3176. let input = document.getElementById("ingredientSearch").value.toLowerCase();
  3177. if(input === ""){
  3178. this.populateByProperty();
  3179. return;
  3180. }
  3181. let matchingIngredients = [];
  3182. for(let i = 0; i < this.ingredients.length; i++){
  3183. if(this.ingredients[i]._name.includes(input)){
  3184. matchingIngredients.push(this.ingredients[i]);
  3185. }
  3186. }
  3187. this.displayIngredientsOnly(matchingIngredients);
  3188. }
  3189. }
  3190. module.exports = ingredients;
  3191. },{}],23:[function(require,module,exports){
  3192. let orders = {
  3193. display: function(){
  3194. document.getElementById("orderFilterBtn").addEventListener("click", ()=>{controller.openSidebar("orderFilter")});
  3195. document.getElementById("newOrderBtn").addEventListener("click", ()=>{controller.openSidebar("newOrder")});
  3196. document.getElementById("orderCalcBtn").addEventListener("click", ()=>{controller.openSidebar("orderCalculator")});
  3197. let orderList = document.getElementById("orderList");
  3198. let template = document.getElementById("order").content.children[0];
  3199. while(orderList.children.length > 0){
  3200. orderList.removeChild(orderList.firstChild);
  3201. }
  3202. for(let i = 0; i < merchant.orders.length; i++){
  3203. let orderDiv = template.cloneNode(true);
  3204. orderDiv.order = merchant.orders[i];
  3205. orderDiv.children[0].innerText = merchant.orders[i].name;
  3206. orderDiv.children[1].innerText = `${merchant.orders[i].ingredients.length} ingredients`;
  3207. orderDiv.children[2].innerText = merchant.orders[i].date.toLocaleDateString("en-US");
  3208. orderDiv.children[3].innerText = `$${merchant.orders[i].getTotalCost().toFixed(2)}`;
  3209. orderDiv.onclick = ()=>{
  3210. controller.openSidebar("orderDetails", merchant.orders[i]);
  3211. orderDiv.classList.add("active");
  3212. }
  3213. orderList.appendChild(orderDiv);
  3214. }
  3215. },
  3216. getOrders: function(Order){
  3217. let loader = document.getElementById("loaderContainer");
  3218. loader.style.display = "flex";
  3219. return fetch("/order", {
  3220. method: "get",
  3221. headers: {
  3222. "Content-Type": "application/json;charset=utf-8"
  3223. }
  3224. })
  3225. .then(response => response.json())
  3226. .then((response)=>{
  3227. if(typeof(response) === "string"){
  3228. controller.createBanner(response, "error");
  3229. }else{
  3230. let orders = [];
  3231. for(let i = 0; i < response.length; i++){
  3232. orders.push(new Order(
  3233. response[i]._id,
  3234. response[i].name,
  3235. response[i].date,
  3236. response[i].taxes,
  3237. response[i].fees,
  3238. response[i].ingredients,
  3239. merchant
  3240. ));
  3241. }
  3242. if(merchant.orders.length === 0){
  3243. merchant.setOrders(orders);
  3244. }
  3245. return orders;
  3246. }
  3247. })
  3248. .catch((err)=>{
  3249. controller.createBanner("SOMETHING WENT WRONG. PLEASE REFRESH THE PAGE", "error");
  3250. })
  3251. .finally(()=>{
  3252. loader.style.display = "none";
  3253. });
  3254. }
  3255. }
  3256. module.exports = orders;
  3257. },{}],24:[function(require,module,exports){
  3258. let recipeBook = {
  3259. isPopulated: false,
  3260. recipeDivList: [],
  3261. display: function(Recipe){
  3262. if(!this.isPopulated){
  3263. this.populateRecipes();
  3264. if(merchant.pos !== "none"){
  3265. document.getElementById("posUpdateRecipe").onclick = ()=>{this.posUpdate(Recipe)};
  3266. }
  3267. document.getElementById("recipeSearch").oninput = ()=>{this.search()};
  3268. this.populateRecipes();
  3269. this.isPopulated = true;
  3270. }
  3271. },
  3272. populateRecipes: function(){
  3273. let recipeList = document.getElementById("recipeList");
  3274. let template = document.getElementById("recipe").content.children[0];
  3275. this.recipeDivList = [];
  3276. while(recipeList.children.length > 0){
  3277. recipeList.removeChild(recipeList.firstChild);
  3278. }
  3279. for(let i = 0; i < merchant.recipes.length; i++){
  3280. let recipeDiv = template.cloneNode(true);
  3281. recipeDiv.onclick = ()=>{
  3282. controller.openSidebar("recipeDetails", merchant.recipes[i]);
  3283. recipeDiv.classList.add("active");
  3284. }
  3285. recipeDiv._name = merchant.recipes[i].name;
  3286. recipeList.appendChild(recipeDiv);
  3287. recipeDiv.children[0].innerText = merchant.recipes[i].name;
  3288. recipeDiv.children[1].innerText = `$${merchant.recipes[i].price.toFixed(2)}`;
  3289. this.recipeDivList.push(recipeDiv);
  3290. }
  3291. },
  3292. search: function(){
  3293. let input = document.getElementById("recipeSearch").value.toLowerCase();
  3294. let recipeList = document.getElementById("recipeList");
  3295. let matchingRecipes = [];
  3296. for(let i = 0; i < this.recipeDivList.length; i++){
  3297. if(this.recipeDivList[i]._name.toLowerCase().includes(input)){
  3298. matchingRecipes.push(this.recipeDivList[i]);
  3299. }
  3300. }
  3301. while(recipeList.children.length > 0){
  3302. recipeList.removeChild(recipeList.firstChild);
  3303. }
  3304. for(let i = 0; i < matchingRecipes.length; i++){
  3305. recipeList.appendChild(matchingRecipes[i]);
  3306. }
  3307. },
  3308. posUpdate: function(Recipe){
  3309. let loader = document.getElementById("loaderContainer");
  3310. loader.style.display = "flex";
  3311. let url = `/recipe/update/${merchant.pos}`;
  3312. fetch(url, {
  3313. method: "GET",
  3314. headers: {
  3315. "Content-Type": "application/json;charset=utf-8"
  3316. },
  3317. })
  3318. .then(response => response.json())
  3319. .then((response)=>{
  3320. if(typeof(response) === "string"){
  3321. controller.createBanner(response, "error");
  3322. }else{
  3323. for(let i = 0; i < response.new.length; i++){
  3324. const recipe = new Recipe(
  3325. response.new[i]._id,
  3326. response.new[i].name,
  3327. response.new[i].price,
  3328. merchant,
  3329. []
  3330. );
  3331. merchant.addRecipe(recipe);
  3332. }
  3333. for(let i = 0; i < response.removed.length; i++){
  3334. for(let j = 0; j < merchant.recipes.length; j++){
  3335. if(merchant.recipes[j].id === response.removed[i]._id){
  3336. merchant.removeRecipe(merchant.recipes[j]);
  3337. break;
  3338. }
  3339. }
  3340. }
  3341. controller.createBanner("RECIPES SUCCESSFULLY UPDATED", "success");
  3342. this.display();
  3343. }
  3344. })
  3345. .catch((err)=>{
  3346. controller.createBanner("SOMETHING WENT WRONG. PLEASE REFRESH THE PAGE", "error");
  3347. })
  3348. .finally(()=>{
  3349. loader.style.display = "none";
  3350. });
  3351. }
  3352. }
  3353. module.exports = recipeBook;
  3354. },{}],25:[function(require,module,exports){
  3355. let transactions = {
  3356. transactions: [],
  3357. display: function(Transaction){
  3358. document.getElementById("filterTransactionsButton").onclick = ()=>{controller.openSidebar("transactionFilter")};
  3359. document.getElementById("newTransactionButton").onclick = ()=>{controller.openSidebar("newTransaction")};
  3360. this.populateTransactions(this.transactions);
  3361. this.isPopulated = true;
  3362. },
  3363. populateTransactions: function(transactions){
  3364. let transactionsList = document.getElementById("transactionsList");
  3365. let template = document.getElementById("transaction").content.children[0];
  3366. while(transactionsList.children.length > 0){
  3367. transactionsList.removeChild(transactionsList.firstChild);
  3368. }
  3369. let i = 0;
  3370. while(i < transactions.length && i < 100){
  3371. let transactionDiv = template.cloneNode(true);
  3372. let transaction = transactions[i];
  3373. transactionDiv.onclick = ()=>{
  3374. controller.openSidebar("transactionDetails", transaction);
  3375. transactionDiv.classList.add("active");
  3376. }
  3377. transactionsList.appendChild(transactionDiv);
  3378. let totalRecipes = 0;
  3379. let totalPrice = 0;
  3380. for(let j = 0; j < transactions[i].recipes.length; j++){
  3381. totalRecipes += transactions[i].recipes[j].quantity;
  3382. totalPrice += transactions[i].recipes[j].recipe.price * transactions[i].recipes[j].quantity;
  3383. }
  3384. transactionDiv.children[0].innerText = `${transactions[i].date.toLocaleDateString()} ${transactions[i].date.toLocaleTimeString()}`;
  3385. transactionDiv.children[1].innerText = `${totalRecipes} recipes sold`;
  3386. transactionDiv.children[2].innerText = `$${totalPrice.toFixed(2)}`;
  3387. i++;
  3388. }
  3389. }
  3390. }
  3391. module.exports = transactions;
  3392. },{}]},{},[6]);