bundle.js 132 KB

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