bundle.js 139 KB

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