bundle.js 139 KB

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