bundle.js 133 KB

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