bundle.js 140 KB

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