bundle.js 141 KB

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