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