bundle.js 135 KB

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