bundle.js 137 KB

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