bundle.js 133 KB

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