bundle.js 139 KB

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