bundle.js 139 KB

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