bundle.js 135 KB

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