bundle.js 125 KB

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