bundle.js 141 KB

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