bundle.js 108 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984
  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. module.exports = {
  3. isPopulated: false,
  4. fakeMerchant: {},
  5. chosenIngredients: [],
  6. display: function(){
  7. let sidebar = document.querySelector("#addIngredients");
  8. if(!this.isPopulated){
  9. let loader = document.getElementById("loaderContainer");
  10. loader.style.display = "flex";
  11. fetch("/ingredients")
  12. .then((response) => response.json())
  13. .then((response)=>{
  14. if(typeof(response) === "string"){
  15. banner.createError(response);
  16. }else{
  17. for(let i = 0; i < merchant.ingredients.length; i++){
  18. for(let j = 0; j < response.length; j++){
  19. if(merchant.ingredients[i].ingredient.id === response[j]._id){
  20. response.splice(j, 1);
  21. break;
  22. }
  23. }
  24. }
  25. for(let i = 0; i < response.length; i++){
  26. response[i] = {ingredient: response[i]}
  27. }
  28. this.fakeMerchant = new Merchant({
  29. name: "none",
  30. inventory: response,
  31. recipes: [],
  32. },
  33. []
  34. );
  35. this.populateAddIngredients();
  36. }
  37. })
  38. .catch((err)=>{
  39. banner.createError("UNABLE TO RETRIEVE DATA");
  40. })
  41. .finally(()=>{
  42. loader.style.display = "none";
  43. });
  44. this.isPopulated = true;
  45. }
  46. openSidebar(sidebar);
  47. },
  48. populateAddIngredients: function(){
  49. let addIngredientsDiv = document.getElementById("addIngredientList");
  50. let categoryTemplate = document.getElementById("addIngredientsCategory");
  51. let ingredientTemplate = document.getElementById("addIngredientsIngredient");
  52. let categories = this.fakeMerchant.categorizeIngredients();
  53. while(addIngredientsDiv.children.length > 0){
  54. addIngredientsDiv.removeChild(addIngredientsDiv.firstChild);
  55. }
  56. for(let i = 0; i < categories.length; i++){
  57. let categoryDiv = categoryTemplate.content.children[0].cloneNode(true);
  58. categoryDiv.children[0].children[0].innerText = categories[i].name;
  59. categoryDiv.children[0].children[1].onclick = ()=>{addIngredientsComp.toggleAddIngredient(categoryDiv)};
  60. categoryDiv.children[1].style.display = "none";
  61. categoryDiv.children[0].children[1].children[1].style.display = "none";
  62. addIngredientsDiv.appendChild(categoryDiv);
  63. for(let j = 0; j < categories[i].ingredients.length; j++){
  64. let ingredientDiv = ingredientTemplate.content.children[0].cloneNode(true);
  65. ingredientDiv.children[0].innerText = categories[i].ingredients[j].ingredient.name;
  66. ingredientDiv.children[2].onclick = ()=>{this.addOne(ingredientDiv)};
  67. ingredientDiv.ingredient = categories[i].ingredients[j].ingredient;
  68. categoryDiv.children[1].appendChild(ingredientDiv);
  69. }
  70. }
  71. let myIngredients = document.getElementById("myIngredients");
  72. while(myIngredients.children.length > 0){
  73. myIngredients.removeChild(myIngredients.firstChild);
  74. }
  75. },
  76. toggleAddIngredient: function(categoryElement){
  77. let button = categoryElement.children[0].children[1];
  78. let ingredientDisplay = categoryElement.children[1];
  79. if(ingredientDisplay.style.display === "none"){
  80. ingredientDisplay.style.display = "flex";
  81. button.children[0].style.display = "none";
  82. button.children[1].style.display = "block";
  83. }else{
  84. ingredientDisplay.style.display = "none";
  85. button.children[0].style.display = "block";
  86. button.children[1].style.display = "none";
  87. }
  88. },
  89. addOne: function(element){
  90. element.parentElement.removeChild(element);
  91. document.getElementById("myIngredients").appendChild(element);
  92. document.getElementById("myIngredientsDiv").style.display = "flex";
  93. for(let i = 0; i < this.fakeMerchant.ingredients.length; i++){
  94. if(this.fakeMerchant.ingredients[i].ingredient === element.ingredient){
  95. this.fakeMerchant.ingredients.splice(i, 1);
  96. this.chosenIngredients.push(element.ingredient);
  97. break;
  98. }
  99. }
  100. let input = document.createElement("input");
  101. input.type = "number";
  102. input.min = "0";
  103. input.step = "0.01";
  104. input.placeholder = "QUANTITY";
  105. element.insertBefore(input, element.children[1]);
  106. let select = element.children[2];
  107. select.style.display = "block";
  108. let units = merchant.units[element.ingredient.unitType];
  109. for(let i = 0; i < units.length; i++){
  110. let option = document.createElement("option");
  111. option.innerText = units[i].toUpperCase();
  112. option.type = element.ingredient.unitType;
  113. option.value = units[i];
  114. select.appendChild(option);
  115. }
  116. element.children[3].innerText = "-";
  117. element.children[3].onclick = ()=>{this.removeOne(element)};
  118. },
  119. removeOne: function(element){
  120. element.parentElement.removeChild(element);
  121. element.removeChild(element.children[1]);
  122. let select = element.children[1];
  123. while(select.children.length > 0){
  124. select.removeChild(select.firstChild);
  125. }
  126. select.style.display = "none";
  127. element.children[2].innerText = "+";
  128. element.children[2].onclick = ()=>{this.addOne(element)};
  129. if(document.getElementById("myIngredients").children.length === 0){
  130. document.getElementById("myIngredientsDiv").style.display = "none";
  131. }
  132. for(let i = 0; i < this.chosenIngredients.length; i++){
  133. if(this.chosenIngredients[i] === element.ingredient){
  134. this.chosenIngredients.splice(i, 1);
  135. this.fakeMerchant.ingredients.push({
  136. ingredient: element.ingredient
  137. });
  138. break;
  139. }
  140. }
  141. this.populateAddIngredients();
  142. },
  143. submit: function(){
  144. let ingredients = document.getElementById("myIngredients").children;
  145. let newIngredients = [];
  146. let fetchable = [];
  147. for(let i = 0; i < ingredients.length; i++){
  148. let quantity = ingredients[i].children[1].value;
  149. let unit = ingredients[i].children[2].value;
  150. if(quantity === ""){
  151. banner.createError("PLEASE ENTER A QUANTITY FOR EACH INGREDIENT YOU WANT TO ADD TO YOUR INVENTORY");
  152. return;
  153. }
  154. quantity = convertToMain(unit, quantity);
  155. let newIngredient = {
  156. ingredient: ingredients[i].ingredient,
  157. quantity: quantity
  158. }
  159. newIngredient.ingredient.unit = unit;
  160. newIngredients.push(newIngredient);
  161. fetchable.push({
  162. id: ingredients[i].ingredient.id,
  163. quantity: quantity,
  164. defaultUnit: unit
  165. });
  166. }
  167. let loader = document.getElementById("loaderContainer");
  168. loader.style.display = "flex";
  169. fetch("/merchant/ingredients/add", {
  170. method: "POST",
  171. headers: {
  172. "Content-Type": "application/json;charset=utf-8"
  173. },
  174. body: JSON.stringify(fetchable)
  175. })
  176. .then((response) => response.json())
  177. .then((response)=>{
  178. if(typeof(response) === "string"){
  179. banner.createError(response);
  180. }else{
  181. merchant.editIngredients(newIngredients);
  182. this.isPopulated = false;
  183. banner.createNotification("ALL INGREDIENTS ADDED");
  184. }
  185. })
  186. .catch((err)=>{
  187. banner.createError("SOMETHING WENT WRONG. PLEASE REFRESH THE PAGE");
  188. })
  189. .finally(()=>{
  190. loader.style.display = "none";
  191. });
  192. }
  193. }
  194. },{}],2:[function(require,module,exports){
  195. const homeStrand = require("./home.js");
  196. const ingredientsStrand = require("./ingredients.js");
  197. const ordersStrand = require("./orders.js");
  198. const recipeBookStrand = require("./recipeBook.js");
  199. const transactionsStrand = require("./transactions.js");
  200. const addIngredientsComp = require("./addIngredients.js");
  201. const ingredientDetailsComp = require("./ingredientDetails.js");
  202. const newIngredientComp = require("./newIngredient.js");
  203. const newOrderComp = require("./newOrder.js");
  204. const newRecipeComp = require("./newRecipe.js");
  205. const newTransactionComp = require("./newTransaction.js");
  206. const orderDetailsComp = require("./orderDetails.js");
  207. const recipeDetailsComp = require("./recipeDetails.js");
  208. const transactionDetailsComp = require("./transactionDetails.js");
  209. const ingredients = require("./ingredients.js");
  210. const recipeBook = require("./recipeBook.js");
  211. const orders = require("./orders.js");
  212. const transactions = require("./transactions.js");
  213. class Ingredient{
  214. constructor(id, name, category, unitType, unit, parent){
  215. this.id = id;
  216. this.name = name;
  217. this.category = category;
  218. this.unitType = unitType;
  219. this.unit = unit;
  220. this.parent = parent;
  221. }
  222. convert(quantity){
  223. if(this.unitType === "mass"){
  224. switch(this.unit){
  225. case "g": break;
  226. case "kg": quantity /= 1000; break;
  227. case "oz": quantity /= 28.3495; break;
  228. case "lb": quantity /= 453.5924; break;
  229. }
  230. }else if(this.unitType === "volume"){
  231. switch(this.unit){
  232. case "ml": quantity *= 1000; break;
  233. case "l": break;
  234. case "tsp": quantity *= 202.8842; break;
  235. case "tbsp": quantity *= 67.6278; break;
  236. case "ozfl": quantity *= 33.8141; break;
  237. case "cup": quantity *= 4.1667; break;
  238. case "pt": quantity *= 2.1134; break;
  239. case "qt": quantity *= 1.0567; break;
  240. case "gal": quantity /= 3.7854; break;
  241. }
  242. }else if(this.unitType === "length"){
  243. switch(this.unit){
  244. case "mm": quantity *= 1000; break;
  245. case "cm": quantity *= 100; break;
  246. case "m": break;
  247. case "in": quantity *= 39.3701; break;
  248. case "ft": quantity *= 3.2808; break;
  249. }
  250. }
  251. return quantity;
  252. }
  253. }
  254. class Recipe{
  255. constructor(id, name, price, ingredients, parent){
  256. this.id = id;
  257. this.name = name;
  258. this.price = price;
  259. this.parent = parent;
  260. this.ingredients = [];
  261. for(let i = 0; i < ingredients.length; i++){
  262. for(let j = 0; j < parent.ingredients.length; j++){
  263. if(ingredients[i].ingredient === parent.ingredients[j].ingredient.id){
  264. this.ingredients.push({
  265. ingredient: parent.ingredients[j].ingredient,
  266. quantity: ingredients[i].quantity
  267. });
  268. break;
  269. }
  270. }
  271. }
  272. }
  273. }
  274. class Transaction{
  275. constructor(id, date, recipes, parent){
  276. this.id = id;
  277. this.parent = parent;
  278. this.date = new Date(date);
  279. this.recipes = [];
  280. for(let i = 0; i < recipes.length; i++){
  281. for(let j = 0; j < parent.recipes.length; j++){
  282. if(recipes[i].recipe === parent.recipes[j].id){
  283. this.recipes.push({
  284. recipe: parent.recipes[j],
  285. quantity: recipes[i].quantity
  286. });
  287. break;
  288. }
  289. }
  290. }
  291. }
  292. }
  293. class Order{
  294. constructor(id, name, date, ingredients, parent){
  295. this.id = id;
  296. this.name = name;
  297. this.date = new Date(date);
  298. this.ingredients = [];
  299. this.parent = parent;
  300. for(let i = 0; i < ingredients.length; i++){
  301. for(let j = 0; j < parent.ingredients.length; j++){
  302. if(ingredients[i].ingredient === parent.ingredients[j].ingredient.id){
  303. this.ingredients.push({
  304. ingredient: parent.ingredients[j].ingredient,
  305. quantity: ingredients[i].quantity,
  306. price: ingredients[i].price
  307. });
  308. }
  309. }
  310. }
  311. }
  312. convertPrice(unitType, unit, price){
  313. if(unitType === "mass"){
  314. switch(unit){
  315. case "g": break;
  316. case "kg": price *= 1000; break;
  317. case "oz": price *= 28.3495; break;
  318. case "lb": price *= 453.5924; break;
  319. }
  320. }else if(unitType === "volume"){
  321. switch(unit){
  322. case "ml": price /= 1000; break;
  323. case "l": break;
  324. case "tsp": price /= 202.8842; break;
  325. case "tbsp": price /= 67.6278; break;
  326. case "ozfl": price /= 33.8141; break;
  327. case "cup": price /= 4.1667; break;
  328. case "pt": price /= 2.1134; break;
  329. case "qt": price /= 1.0567; break;
  330. case "gal": price *= 3.7854; break;
  331. }
  332. }else if(unitType === "length"){
  333. switch(unit){
  334. case "mm": price /= 1000; break;
  335. case "cm": price /= 100; break;
  336. case "m": break;
  337. case "in": price /= 39.3701; break;
  338. case "ft": price /= 3.2808; break;
  339. }
  340. }
  341. return price;
  342. }
  343. }
  344. class Merchant{
  345. constructor(oldMerchant, transactions){
  346. this.name = oldMerchant.name;
  347. this.pos = oldMerchant.pos;
  348. this.ingredients = [];
  349. this.recipes = [];
  350. this.transactions = [];
  351. this.orders = [];
  352. this.units = {
  353. mass: ["g", "kg", "oz", "lb"],
  354. volume: ["ml", "l", "tsp", "tbsp", "ozfl", "cup", "pt", "qt", "gal"],
  355. length: ["mm", "cm", "m", "in", "foot"]
  356. }
  357. for(let i = 0; i < oldMerchant.inventory.length; i++){
  358. this.ingredients.push({
  359. ingredient: new Ingredient(
  360. oldMerchant.inventory[i].ingredient._id,
  361. oldMerchant.inventory[i].ingredient.name,
  362. oldMerchant.inventory[i].ingredient.category,
  363. oldMerchant.inventory[i].ingredient.unitType,
  364. oldMerchant.inventory[i].defaultUnit,
  365. this
  366. ),
  367. quantity: oldMerchant.inventory[i].quantity
  368. });
  369. }
  370. for(let i = 0; i < oldMerchant.recipes.length; i++){
  371. this.recipes.push(new Recipe(
  372. oldMerchant.recipes[i]._id,
  373. oldMerchant.recipes[i].name,
  374. oldMerchant.recipes[i].price,
  375. oldMerchant.recipes[i].ingredients,
  376. this
  377. ));
  378. }
  379. for(let i = 0; i < transactions.length; i++){
  380. this.transactions.push(new Transaction(
  381. transactions[i]._id,
  382. transactions[i].date,
  383. transactions[i].recipes,
  384. this
  385. ));
  386. }
  387. }
  388. /*
  389. Updates all specified item in the merchant's inventory and updates the page
  390. If ingredient doesn't exist, add it
  391. ingredients = {
  392. ingredient: Ingredient object,
  393. quantity: new quantity,
  394. defaultUnit: the default unit to be displayed
  395. }
  396. remove = set true if removing
  397. isOrder = set true if this is coming from an order
  398. */
  399. editIngredients(ingredients, remove = false, isOrder = false){
  400. for(let i = 0; i < ingredients.length; i++){
  401. let isNew = true;
  402. for(let j = 0; j < merchant.ingredients.length; j++){
  403. if(merchant.ingredients[j].ingredient === ingredients[i].ingredient){
  404. if(remove){
  405. merchant.ingredients.splice(j, 1);
  406. }else if(!remove && isOrder){
  407. merchant.ingredients[j].quantity += ingredients[i].quantity;
  408. }else{
  409. merchant.ingredients[j].quantity = ingredients[i].quantity;
  410. }
  411. isNew = false;
  412. break;
  413. }
  414. }
  415. if(isNew){
  416. merchant.ingredients.push({
  417. ingredient: ingredients[i].ingredient,
  418. quantity: parseFloat(ingredients[i].quantity),
  419. defaultUnit: ingredients[i].defaultUnit
  420. });
  421. }
  422. }
  423. homeStrandObj.drawInventoryCheckCard();
  424. ingredientsStrandObj.populateByProperty("category");
  425. addIngredientsComp.isPopulated = false;
  426. closeSidebar();
  427. }
  428. /*
  429. Updates a recipe in the merchants list of recipes
  430. Can create, edit or remove
  431. recipe = [Recipe object]
  432. remove = will remove recipe when true
  433. */
  434. editRecipes(recipes, remove = false){
  435. let isNew = true;
  436. for(let i = 0; i < recipes.length; i++){
  437. for(let j = 0; j < this.recipes.length; j++){
  438. if(recipes[i] === this.recipes[j]){
  439. if(remove){
  440. this.recipes.splice(j, 1);
  441. }else{
  442. this.recipes[j] = recipes[i];
  443. }
  444. isNew = false;
  445. break;
  446. }
  447. }
  448. if(isNew){
  449. merchant.recipes.push(recipes[i]);
  450. }
  451. }
  452. transactionsStrandObj.isPopulated = false;
  453. recipeBookStrandObj.populateRecipes();
  454. closeSidebar();
  455. }
  456. /*
  457. Updates a list of orders in the merchants list of orders
  458. Create/edit/remove
  459. orders = [Order object]
  460. remove = will remove order when true
  461. */
  462. editOrders(orders, remove = false){
  463. for(let i = 0; i < orders.length; i++){
  464. let isNew = true;
  465. for(let j = 0; j < this.orders.length; j++){
  466. if(orders[i] === this.orders[j]){
  467. if(remove){
  468. this.orders.splice(j, 1);
  469. }else{
  470. this.orders[j] = orders[i];
  471. }
  472. isNew = false;
  473. break;
  474. }
  475. }
  476. if(isNew){
  477. this.orders.push(orders[i]);
  478. }
  479. }
  480. ordersStrandObj.populate();
  481. closeSidebar();
  482. }
  483. editTransactions(transaction, remove = false){
  484. let isNew = true;
  485. for(let i = 0; i < this.transactions.length; i++){
  486. if(this.transactions[i] === transaction){
  487. if(remove){
  488. this.transactions.splice(i, 1);
  489. }
  490. isNew = false;
  491. break;
  492. }
  493. }
  494. if(isNew){
  495. this.transactions.push(transaction);
  496. this.transactions.sort((a, b) => a.date > b.date ? 1 : -1);
  497. }
  498. transactionsStrandObj.isPopulated = false;
  499. transactionsStrandObj.display();
  500. closeSidebar();
  501. }
  502. /*
  503. Gets the indices of two dates from transactions
  504. Inputs
  505. from: starting date
  506. to: ending date (default to now)
  507. Output
  508. Array containing starting index and ending index
  509. Note: Will return false if it cannot find both necessary dates
  510. */
  511. transactionIndices(from, to = new Date()){
  512. let indices = [];
  513. for(let i = 0; i < this.transactions.length; i++){
  514. if(this.transactions[i].date > from){
  515. indices.push(i);
  516. break;
  517. }
  518. }
  519. for(let i = this.transactions.length - 1; i >=0; i--){
  520. if(this.transactions[i].date < to){
  521. indices.push(i);
  522. break;
  523. }
  524. }
  525. if(indices.length < 2){
  526. return false;
  527. }
  528. return indices;
  529. }
  530. revenue(indices){
  531. let total = 0;
  532. for(let i = indices[0]; i <= indices[1]; i++){
  533. for(let j = 0; j < this.transactions[i].recipes.length; j++){
  534. for(let k = 0; k < this.recipes.length; k++){
  535. if(this.transactions[i].recipes[j].recipe === this.recipes[k]){
  536. total += this.transactions[i].recipes[j].quantity * this.recipes[k].price;
  537. }
  538. }
  539. }
  540. }
  541. return total / 100;
  542. }
  543. /*
  544. Gets the quantity of each ingredient sold between two dates (dateRange)
  545. Inputs
  546. dateRange: list containing a start date and an end date
  547. Return:
  548. [{
  549. ingredient: Ingredient object,
  550. quantity: quantity of ingredient sold
  551. }]
  552. */
  553. ingredientsSold(dateRange){
  554. if(!dateRange){
  555. return false;
  556. }
  557. let recipes = this.recipesSold(dateRange);
  558. let ingredientList = [];
  559. for(let i = 0; i < recipes.length; i++){
  560. for(let j = 0; j < recipes[i].recipe.ingredients.length; j++){
  561. let exists = false;
  562. for(let k = 0; k < ingredientList.length; k++){
  563. if(ingredientList[k].ingredient === recipes[i].recipe.ingredients[j].ingredient){
  564. exists = true;
  565. ingredientList[k].quantity += recipes[i].quantity * recipes[i].recipe.ingredients[j].quantity;
  566. break;
  567. }
  568. }
  569. if(!exists){
  570. ingredientList.push({
  571. ingredient: recipes[i].recipe.ingredients[j].ingredient,
  572. quantity: recipes[i].quantity * recipes[i].recipe.ingredients[j].quantity
  573. });
  574. }
  575. }
  576. }
  577. return ingredientList;
  578. }
  579. singleIngredientSold(dateRange, ingredient){
  580. let total = 0;
  581. for(let i = dateRange[0]; i < dateRange[1]; i++){
  582. for(let j = 0; j < this.transactions[i].recipes.length; j++){
  583. for(let k = 0; k < this.transactions[i].recipes[j].recipe.ingredients.length; k++){
  584. if(this.transactions[i].recipes[j].recipe.ingredients[k].ingredient === ingredient.ingredient){
  585. total += this.transactions[i].recipes[j].recipe.ingredients[k].quantity;
  586. break;
  587. }
  588. }
  589. }
  590. }
  591. return total;
  592. }
  593. /*
  594. Gets the number of recipes sold between two dates (dateRange)
  595. Inputs:
  596. dateRange: array containing a start date and an end date
  597. Return:
  598. [{
  599. recipe: a recipe object
  600. quantity: quantity of the recipe sold
  601. }]
  602. */
  603. recipesSold(dateRange){
  604. let recipeList = [];
  605. for(let i = dateRange[0]; i <= dateRange[1]; i++){
  606. for(let j = 0; j < this.transactions[i].recipes.length; j++){
  607. let exists = false;
  608. for(let k = 0; k < recipeList.length; k++){
  609. if(recipeList[k].recipe === this.transactions[i].recipes[j].recipe){
  610. exists = true;
  611. recipeList[k].quantity += this.transactions[i].recipes[j].quantity;
  612. break;
  613. }
  614. }
  615. if(!exists){
  616. recipeList.push({
  617. recipe: this.transactions[i].recipes[j].recipe,
  618. quantity: this.transactions[i].recipes[j].quantity
  619. });
  620. }
  621. }
  622. }
  623. return recipeList;
  624. }
  625. /*
  626. Create revenue data for graphing
  627. Input:
  628. dateRange: [start index, end index] (this.transactionIndices)
  629. Return:
  630. [total revenue for each day]
  631. */
  632. graphDailyRevenue(dateRange){
  633. if(!dateRange){
  634. return false;
  635. }
  636. let dataList = new Array(30).fill(0);
  637. let currentDate = this.transactions[dateRange[0]].date;
  638. let arrayIndex = 0;
  639. for(let i = dateRange[0]; i <= dateRange[1]; i++){
  640. if(this.transactions[i].date.getDate() !== currentDate.getDate()){
  641. currentDate = this.transactions[i].date;
  642. arrayIndex++;
  643. }
  644. for(let j = 0; j < this.transactions[i].recipes.length; j++){
  645. dataList[arrayIndex] += (this.transactions[i].recipes[j].recipe.price / 100) * this.transactions[i].recipes[j].quantity;
  646. }
  647. }
  648. return dataList;
  649. }
  650. /*
  651. Groups all of the merchant's ingredients by their category
  652. Return: [{
  653. name: category name,
  654. ingredients: [Ingredient Object]
  655. }]
  656. */
  657. categorizeIngredients(){
  658. let ingredientsByCategory = [];
  659. for(let i = 0; i < this.ingredients.length; i++){
  660. let categoryExists = false;
  661. for(let j = 0; j < ingredientsByCategory.length; j++){
  662. if(this.ingredients[i].ingredient.category === ingredientsByCategory[j].name){
  663. ingredientsByCategory[j].ingredients.push(this.ingredients[i]);
  664. categoryExists = true;
  665. break;
  666. }
  667. }
  668. if(!categoryExists){
  669. ingredientsByCategory.push({
  670. name: this.ingredients[i].ingredient.category,
  671. ingredients: [this.ingredients[i]]
  672. });
  673. }
  674. }
  675. return ingredientsByCategory;
  676. }
  677. unitizeIngredients(){
  678. let ingredientsByUnit = [];
  679. for(let i = 0; i < this.ingredients.length; i++){
  680. let unitExists = false;
  681. for(let j = 0; j < ingredientsByUnit.length; j++){
  682. if(this.ingredients[i].ingredient.unit === ingredientsByUnit[j].name){
  683. ingredientsByUnit[j].ingredients.push(this.ingredients[i]);
  684. unitExists = true;
  685. break;
  686. }
  687. }
  688. if(!unitExists){
  689. ingredientsByUnit.push({
  690. name: this.ingredients[i].ingredient.unit,
  691. ingredients: [this.ingredients[i]]
  692. });
  693. }
  694. }
  695. return ingredientsByUnit;
  696. }
  697. getRecipesForIngredient(ingredient){
  698. let recipes = [];
  699. for(let i = 0; i < this.recipes.length; i++){
  700. for(let j = 0; j < this.recipes[i].ingredients.length; j++){
  701. if(this.recipes[i].ingredients[j].ingredient === ingredient){
  702. recipes.push(this.recipes[i]);
  703. }
  704. }
  705. }
  706. return recipes;
  707. }
  708. }
  709. let convertToMain = (unit, quantity)=>{
  710. let converted = 0;
  711. if(merchant.units.mass.includes(unit)){
  712. switch(unit){
  713. case "g": converted = quantity; break;
  714. case "kg": converted = quantity * 1000; break;
  715. case "oz": converted = quantity * 28.3495; break;
  716. case "lb": converted = quantity * 453.5924; break;
  717. }
  718. }else if(merchant.units.volume.includes(unit)){
  719. switch(unit){
  720. case "ml": converted = quantity / 1000; break;
  721. case "l": converted = quantity; break;
  722. case "tsp": converted = quantity / 202.8842; break;
  723. case "tbsp": converted = quantity / 67.6278; break;
  724. case "ozfl": converted = quantity / 33.8141; break;
  725. case "cup": converted = quantity / 4.1667; break;
  726. case "pt": converted = quantity / 2.1134; break;
  727. case "qt": converted = quantity / 1.0567; break;
  728. case "gal": converted = quantity * 3.7854; break;
  729. }
  730. }else if(merchant.units.length.includes(unit)){
  731. switch(unit){
  732. case "mm": converted = quantity / 1000; break;
  733. case "cm": converted = quantity / 100; break;
  734. case "m": converted = quantity; break;
  735. case "in": converted = quantity / 39.3701; break;
  736. case "ft": converted = quantity / 3.2808; break;
  737. }
  738. }else{
  739. converted = quantity;
  740. }
  741. return converted;
  742. }
  743. /*
  744. Switches to a different strand
  745. Input:
  746. name: name of the strand. Must end with "Strand"
  747. */
  748. window.changeStrand = (name)=>{
  749. closeSidebar();
  750. for(let strand of document.querySelectorAll(".strand")){
  751. strand.style.display = "none";
  752. }
  753. let buttons = document.querySelectorAll(".menuButton");
  754. for(let i = 0; i < buttons.length - 1; i++){
  755. buttons[i].classList = "menuButton";
  756. buttons[i].disabled = false;
  757. }
  758. let activeButton = {};
  759. switch(name){
  760. case 1:
  761. activeButton = document.getElementById("homeBtn");
  762. document.getElementById("homeStrand").style.display = "flex";
  763. homeStrand.display();
  764. break;
  765. case 2:
  766. activeButton = document.getElementById("ingredientsBtn");
  767. document.getElementById("ingredientsStrand").style.display = "flex";
  768. ingredients.display();
  769. break;
  770. case 3:
  771. activeButton = document.getElementById("recipeBookBtn");
  772. document.getElementById("recipeBookStrand").style.display = "flex";
  773. recipeBook.display();
  774. break;
  775. case 4:
  776. activeButton = document.getElementById("ordersBtn");
  777. document.getElementById("ordersStrand").style.display = "flex";
  778. orders.display();
  779. break;
  780. case 5:
  781. activeButton = document.getElementById("transactionsBtn");
  782. document.getElementById("transactionsStrand").style.display = "flex";
  783. transactions.display();
  784. break;
  785. }
  786. activeButton.classList = "menuButton active";
  787. activeButton.disabled = true;
  788. if(window.screen.availWidth <= 1000){
  789. closeMenu();
  790. }
  791. }
  792. //Close any open sidebar
  793. let closeSidebar = ()=>{
  794. let sidebar = document.querySelector("#sidebarDiv");
  795. for(let i = 0; i < sidebar.children.length; i++){
  796. sidebar.children[i].style.display = "none";
  797. }
  798. sidebar.classList = "sidebarHide";
  799. if(window.screen.availWidth <= 1000){
  800. document.querySelector(".contentBlock").style.display = "flex";
  801. document.getElementById("mobileMenuSelector").style.display = "block";
  802. document.getElementById("sidebarCloser").style.display = "none";
  803. }
  804. }
  805. /*
  806. Open a specific sidebar
  807. Input:
  808. sidebar: the outermost element of the sidebar (must contain class sidebar)
  809. */
  810. let openSidebar = (sidebar)=>{
  811. document.querySelector("#sidebarDiv").classList = "sidebar";
  812. let sideBars = document.querySelector("#sidebarDiv").children;
  813. for(let i = 0; i < sideBars.length; i++){
  814. sideBars[i].style.display = "none";
  815. }
  816. sidebar.style.display = "flex";
  817. if(window.screen.availWidth <= 1000){
  818. document.querySelector(".contentBlock").style.display = "none";
  819. document.getElementById("mobileMenuSelector").style.display = "none";
  820. document.getElementById("sidebarCloser").style.display = "block";
  821. }
  822. }
  823. let changeMenu = ()=>{
  824. let menu = document.querySelector(".menu");
  825. let buttons = document.querySelectorAll(".menuButton");
  826. if(!menu.classList.contains("menuMinimized")){
  827. menu.classList = "menu menuMinimized";
  828. for(let button of buttons){
  829. button.children[1].style.display = "none";
  830. }
  831. document.querySelector("#max").style.display = "none";
  832. document.querySelector("#min").style.display = "flex";
  833. }else if(menu.classList.contains("menuMinimized")){
  834. menu.classList = "menu";
  835. for(let button of buttons){
  836. button.children[1].style.display = "block";
  837. }
  838. setTimeout(()=>{
  839. document.querySelector("#max").style.display = "flex";
  840. document.querySelector("#min").style.display = "none";
  841. }, 150);
  842. }
  843. }
  844. let openMenu = ()=>{
  845. document.getElementById("menu").style.display = "flex";
  846. document.querySelector(".contentBlock").style.display = "none";
  847. document.getElementById("mobileMenuSelector").onclick = ()=>{closeMenu()};
  848. }
  849. let closeMenu = ()=>{
  850. document.getElementById("menu").style.display = "none";
  851. document.querySelector(".contentBlock").style.display = "flex";
  852. document.getElementById("mobileMenuSelector").onclick = ()=>{openMenu()};
  853. }
  854. if(window.screen.availWidth > 1000 && window.screen.availWidth <= 1400){
  855. changeMenu();
  856. document.getElementById("menuShifter2").style.display = "none";
  857. }
  858. merchant = new Merchant(data.merchant, data.transactions);
  859. homeStrand.display(merchant);
  860. },{"./addIngredients.js":1,"./home.js":3,"./ingredientDetails.js":4,"./ingredients.js":5,"./newIngredient.js":6,"./newOrder.js":7,"./newRecipe.js":8,"./newTransaction.js":9,"./orderDetails.js":10,"./orders.js":11,"./recipeBook.js":12,"./recipeDetails.js":13,"./transactionDetails.js":14,"./transactions.js":15}],3:[function(require,module,exports){
  861. module.exports = {
  862. isPopulated: false,
  863. graph: {},
  864. display: function(merchant){
  865. if(!this.isPopulated){
  866. this.drawRevenueCard(merchant);
  867. this.drawRevenueGraph();
  868. this.drawInventoryCheckCard();
  869. this.drawPopularCard();
  870. this.isPopulated = true;
  871. }
  872. },
  873. drawRevenueCard: function(merchant){
  874. let today = new Date();
  875. let firstOfMonth = new Date(today.getFullYear(), today.getMonth(), 1);
  876. let firstOfLastMonth = new Date(today.getFullYear(), today.getMonth() - 1, 1);
  877. let lastMonthtoDay = new Date(new Date().setMonth(today.getMonth() - 1));
  878. let revenueThisMonth = merchant.revenue(merchant.transactionIndices(firstOfMonth));
  879. let revenueLastmonthToDay = merchant.revenue(merchant.transactionIndices(firstOfLastMonth, lastMonthtoDay));
  880. document.querySelector("#revenue").innerText = `$${revenueThisMonth.toLocaleString("en")}`;
  881. let revenueChange = ((revenueThisMonth - revenueLastmonthToDay) / revenueLastmonthToDay) * 100;
  882. let img = "";
  883. if(revenueChange >= 0){
  884. img = "/shared/images/upArrow.png";
  885. }else{
  886. img = "/shared/images/downArrow.png";
  887. }
  888. document.querySelector("#revenueChange p").innerText = `${Math.abs(revenueChange).toFixed(2)}% vs last month`;
  889. document.querySelector("#revenueChange img").src = img;
  890. },
  891. drawRevenueGraph: function(){
  892. let graphCanvas = document.querySelector("#graphCanvas");
  893. let today = new Date();
  894. graphCanvas.height = graphCanvas.parentElement.clientHeight;
  895. graphCanvas.width = graphCanvas.parentElement.clientWidth;
  896. let LineGraph = require("../../shared/graphs.js").LineGraph;
  897. this.graph = new LineGraph(graphCanvas);
  898. this.graph.addTitle("Revenue");
  899. let thirtyAgo = new Date(today);
  900. thirtyAgo.setDate(today.getDate() - 29);
  901. let data = merchant.graphDailyRevenue(merchant.transactionIndices(thirtyAgo));
  902. if(data){
  903. this.graph.addData(
  904. data,
  905. [thirtyAgo, new Date()],
  906. "Revenue"
  907. );
  908. }else{
  909. document.querySelector("#graphCanvas").style.display = "none";
  910. let notice = document.createElement("h1");
  911. notice.innerText = "NO DATA YET";
  912. notice.classList = "notice";
  913. document.querySelector("#graphCard").appendChild(notice);
  914. }
  915. },
  916. drawInventoryCheckCard: function(){
  917. let num;
  918. if(merchant.ingredients.length < 5){
  919. num = merchant.ingredients.length;
  920. }else{
  921. num = 5;
  922. }
  923. let rands = [];
  924. for(let i = 0; i < num; i++){
  925. let rand = Math.floor(Math.random() * merchant.ingredients.length);
  926. if(rands.includes(rand)){
  927. i--;
  928. }else{
  929. rands[i] = rand;
  930. }
  931. }
  932. let ul = document.querySelector("#inventoryCheckCard ul");
  933. let template = document.querySelector("#ingredientCheck").content.children[0];
  934. while(ul.children.length > 0){
  935. ul.removeChild(ul.firstChild);
  936. }
  937. for(let i = 0; i < rands.length; i++){
  938. let ingredientCheck = template.cloneNode(true);
  939. let input = ingredientCheck.children[1].children[1];
  940. ingredientCheck.ingredient = merchant.ingredients[rands[i]];
  941. ingredientCheck.children[0].innerText = merchant.ingredients[rands[i]].ingredient.name;
  942. ingredientCheck.children[1].children[0].onclick = ()=>{input.value--};
  943. input.value = merchant.ingredients[rands[i]].quantity.toFixed(2);
  944. ingredientCheck.children[1].children[2].onclick = ()=>{input.value++}
  945. ingredientCheck.children[2].innerText = merchant.ingredients[rands[i]].ingredient.unit.toUpperCase();
  946. ul.appendChild(ingredientCheck);
  947. }
  948. },
  949. drawPopularCard: function(){
  950. let dataArray = [];
  951. let now = new Date();
  952. let thisMonth = new Date(now.getFullYear(), now.getMonth(), 1);
  953. let ingredientList = merchant.ingredientsSold(merchant.transactionIndices(thisMonth));
  954. if(ingredientList !== false){
  955. window.ingredientList = [...ingredientList];
  956. let iterations = (ingredientList.length < 5) ? ingredientList.length : 5;
  957. for(let i = 0; i < iterations; i++){
  958. try{
  959. let max = ingredientList[0].quantity;
  960. let index = 0;
  961. for(let j = 0; j < ingredientList.length; j++){
  962. if(ingredientList[j].quantity > max){
  963. max = ingredientList[j].quantity;
  964. index = j;
  965. }
  966. }
  967. dataArray.push({
  968. num: max,
  969. label: ingredientList[index].ingredient.name + ": " +
  970. ingredientList[index].ingredient.convert(ingredientList[index].quantity).toFixed(2) +
  971. " " + ingredientList[index].ingredient.unit
  972. });
  973. ingredientList.splice(index, 1);
  974. }catch(err){
  975. break;
  976. }
  977. }
  978. let thisCanvas = document.getElementById("popularCanvas");
  979. thisCanvas.width = thisCanvas.parentElement.offsetWidth * 0.8;
  980. thisCanvas.height = thisCanvas.parentElement.offsetHeight * 0.8;
  981. let HorizontalBarGraph = require("../../shared/graphs.js").HorizontalBarGraph;
  982. let popularGraph = new HorizontalBarGraph(thisCanvas);
  983. popularGraph.addData(dataArray);
  984. }else{
  985. document.querySelector("#popularCanvas").style.display = "none";
  986. let notice = document.createElement("p");
  987. notice.innerText = "N/A";
  988. notice.classList = "notice";
  989. document.querySelector("#popularIngredientsCard").appendChild(notice);
  990. }
  991. },
  992. submitInventoryCheck: function(){
  993. let lis = document.querySelectorAll("#inventoryCheckCard li");
  994. let changes = [];
  995. for(let i = 0; i < lis.length; i++){
  996. if(lis[i].children[1].children[1].value >= 0){
  997. let merchIngredient = lis[i].ingredient;
  998. let value = parseFloat(lis[i].children[1].children[1].value);
  999. if(value !== merchIngredient.quantity){
  1000. changes.push({
  1001. id: merchIngredient.ingredient.id,
  1002. ingredient: merchIngredient.ingredient,
  1003. quantity: value
  1004. });
  1005. }
  1006. }else{
  1007. banner.createError("CANNOT HAVE NEGATIVE INGREDIENTS");
  1008. return;
  1009. }
  1010. }
  1011. let loader = document.getElementById("loaderContainer");
  1012. loader.style.display = "flex";
  1013. if(changes.length > 0){
  1014. fetch("/merchant/ingredients/update", {
  1015. method: "PUT",
  1016. headers: {
  1017. "Content-Type": "application/json;charset=utf-8"
  1018. },
  1019. body: JSON.stringify(changes)
  1020. })
  1021. .then((response) => response.json())
  1022. .then((response)=>{
  1023. if(typeof(response) === "string"){
  1024. banner.createError(response);
  1025. }else{
  1026. merchant.editIngredients(changes);
  1027. banner.createNotification("INGREDIENTS UPDATED");
  1028. }
  1029. })
  1030. .catch((err)=>{})
  1031. .finally(()=>{
  1032. loader.style.display = "none";
  1033. });
  1034. }
  1035. }
  1036. }
  1037. },{"../../shared/graphs.js":16}],4:[function(require,module,exports){
  1038. module.exports = {
  1039. ingredient: {},
  1040. display: function(ingredient){
  1041. this.ingredient = ingredient;
  1042. sidebar = document.querySelector("#ingredientDetails");
  1043. document.querySelector("#ingredientDetails p").innerText = ingredient.ingredient.category;
  1044. document.querySelector("#ingredientDetails h1").innerText = ingredient.ingredient.name;
  1045. let ingredientStock = document.getElementById("ingredientStock");
  1046. ingredientStock.innerText = `${ingredient.ingredient.convert(ingredient.quantity).toFixed(2)} ${ingredient.ingredient.unit.toUpperCase()}`;
  1047. ingredientStock.style.display = "block";
  1048. let ingredientInput = document.getElementById("ingredientInput");
  1049. ingredientInput.value = ingredient.ingredient.convert(ingredient.quantity).toFixed(2);
  1050. ingredientInput.style.display = "none";
  1051. let quantities = [];
  1052. let now = new Date();
  1053. for(let i = 1; i < 31; i++){
  1054. let endDay = new Date(now.getFullYear(), now.getMonth(), now.getDate() - i)
  1055. let startDay = new Date(now.getFullYear(), now.getMonth(), now.getDate() - i - 1);
  1056. let indices = merchant.transactionIndices(startDay, endDay);
  1057. if(indices === false){
  1058. quantities.push(0);
  1059. }else{
  1060. quantities.push(merchant.singleIngredientSold(indices, ingredient));
  1061. }
  1062. }
  1063. let sum = 0;
  1064. for(let quantity of quantities){
  1065. sum += quantity;
  1066. }
  1067. document.querySelector("#dailyUse").innerText = `${(sum/quantities.length).toFixed(2)} ${ingredient.ingredient.unit}`;
  1068. let ul = document.querySelector("#ingredientRecipeList");
  1069. let recipes = merchant.getRecipesForIngredient(ingredient.ingredient);
  1070. while(ul.children.length > 0){
  1071. ul.removeChild(ul.firstChild);
  1072. }
  1073. for(let i = 0; i < recipes.length; i++){
  1074. let li = document.createElement("li");
  1075. li.innerText = recipes[i].name;
  1076. li.onclick = ()=>{
  1077. changeStrand("recipeBookStrand");
  1078. recipeDetailsComp.display(recipes[i]);
  1079. }
  1080. ul.appendChild(li);
  1081. }
  1082. let ingredientButtons = document.getElementById("ingredientButtons");
  1083. let units = [];
  1084. let unitLabel = document.getElementById("displayUnitLabel");
  1085. let defaultButton = document.getElementById("defaultUnit");
  1086. if(this.ingredient.ingredient.unitType !== "other"){
  1087. units = merchant.units[this.ingredient.ingredient.unitType];
  1088. unitLabel.style.display = "block";
  1089. defaultButton.style.display = "block";
  1090. }else{
  1091. unitLabel.style.display = "none";
  1092. defaultButton.style.display = "none";
  1093. }
  1094. while(ingredientButtons.children.length > 0){
  1095. ingredientButtons.removeChild(ingredientButtons.firstChild);
  1096. }
  1097. for(let i = 0; i < units.length; i++){
  1098. let button = document.createElement("button");
  1099. button.classList.add("unitButton");
  1100. button.innerText = units[i].toUpperCase();
  1101. button.onclick = ()=>{this.changeUnit(button, units[i])};
  1102. ingredientButtons.appendChild(button);
  1103. if(units[i] === this.ingredient.ingredient.unit){
  1104. button.classList.add("unitActive");
  1105. }
  1106. }
  1107. openSidebar(sidebar);
  1108. },
  1109. remove: function(){
  1110. for(let i = 0; i < merchant.recipes.length; i++){
  1111. for(let j = 0; j < merchant.recipes[i].ingredients.length; j++){
  1112. if(this.ingredient.ingredient === merchant.recipes[i].ingredients[j].ingredient){
  1113. banner.createError("MUST REMOVE INGREDIENT FROM ALL RECIPES BEFORE REMOVING FROM INVENTORY");
  1114. return;
  1115. }
  1116. }
  1117. }
  1118. let loader = document.getElementById("loaderContainer");
  1119. loader.style.display = "flex";
  1120. fetch(`/merchant/ingredients/remove/${this.ingredient.ingredient.id}`, {
  1121. method: "DELETE",
  1122. })
  1123. .then((response) => response.json())
  1124. .then((response)=>{
  1125. if(typeof(response) === "string"){
  1126. banner.createError(response);
  1127. }else{
  1128. banner.createNotification("INGREDIENT REMOVED");
  1129. merchant.editIngredients([this.ingredient], true);
  1130. }
  1131. })
  1132. .catch((err)=>{})
  1133. .finally(()=>{
  1134. loader.style.display = "none";
  1135. });
  1136. },
  1137. edit: function(){
  1138. document.getElementById("ingredientStock").style.display = "none";
  1139. document.getElementById("ingredientInput").style.display = "block";
  1140. document.getElementById("editSubmitButton").style.display = "block";
  1141. },
  1142. editSubmit: function(){
  1143. this.ingredient.quantity = Number(document.getElementById("ingredientInput").value);
  1144. let data = [{
  1145. id: this.ingredient.ingredient.id,
  1146. quantity: this.ingredient.quantity
  1147. }];
  1148. let loader = document.getElementById("loaderContainer");
  1149. loader.style.display = "flex";
  1150. if(validator.ingredientQuantity(data[0].quantity)){
  1151. fetch("/merchant/ingredients/update", {
  1152. method: "PUT",
  1153. headers: {
  1154. "Content-Type": "application/json;charset=utf-8"
  1155. },
  1156. body: JSON.stringify(data)
  1157. })
  1158. .then((response) => response.json())
  1159. .then((response)=>{
  1160. if(typeof(response) === "string"){
  1161. banner.createError(response);
  1162. }else{
  1163. merchant.editIngredients([this.ingredient]);
  1164. banner.createNotification("INGREDIENT UPDATED");
  1165. }
  1166. })
  1167. .catch((err)=>{
  1168. banner.createError("SOMETHING WENT WRONG. PLEASE REFRESH THE PAGE");
  1169. })
  1170. .finally(()=>{
  1171. loader.style.display = "none";
  1172. });
  1173. }
  1174. },
  1175. changeUnit: function(newActive, unit){
  1176. this.ingredient.ingredient.unit = unit;
  1177. let ingredientButtons = document.querySelectorAll(".unitButton");
  1178. for(let i = 0; i < ingredientButtons.length; i++){
  1179. ingredientButtons[i].classList.remove("unitActive");
  1180. }
  1181. newActive.classList.add("unitActive");
  1182. homeStrandObj.isPopulated = false;
  1183. ingredientsStrandObj.populateByProperty("category");
  1184. document.getElementById("ingredientStock").innerText = `${this.ingredient.ingredient.convert(this.ingredient.quantity).toFixed(2)} ${this.ingredient.ingredient.unit.toUpperCase()}`;
  1185. },
  1186. changeUnitDefault: function(){
  1187. let loader = document.getElementById("loaderContainer");
  1188. loader.style.display = "flex";
  1189. let id = this.ingredient.ingredient.id;
  1190. let unit = this.ingredient.ingredient.unit;
  1191. fetch(`/merchant/ingredients/update/${id}/${unit}`, {
  1192. method: "put",
  1193. headers: {
  1194. "Content-Type": "application/json;charset=utf-8"
  1195. },
  1196. })
  1197. .then((response)=>{
  1198. if(typeof(response) === "string"){
  1199. banner.createError(response);
  1200. }else{
  1201. banner.createNotification("INGREDIENT DEFAULT UNIT UPDATED");
  1202. }
  1203. })
  1204. .catch((err)=>{
  1205. banner.createError("SOMETHING WENT WRONG. PLEASE REFRESH THE PAGE");
  1206. })
  1207. .finally(()=>{
  1208. loader.style.display = "none";
  1209. });
  1210. }
  1211. }
  1212. },{}],5:[function(require,module,exports){
  1213. module.exports = {
  1214. isPopulated: false,
  1215. ingredients: [],
  1216. display: function(){
  1217. if(!this.isPopulated){
  1218. this.populateByProperty("category");
  1219. this.isPopulated = true;
  1220. }
  1221. },
  1222. populateByProperty: function(property){
  1223. let categories;
  1224. if(property === "category"){
  1225. categories = merchant.categorizeIngredients();
  1226. }else if(property === "unit"){
  1227. categories = merchant.unitizeIngredients();
  1228. }
  1229. let ingredientStrand = document.querySelector("#categoryList");
  1230. let categoryTemplate = document.querySelector("#categoryDiv").content.children[0];
  1231. let ingredientTemplate = document.querySelector("#ingredient").content.children[0];
  1232. this.ingredients = [];
  1233. while(ingredientStrand.children.length > 0){
  1234. ingredientStrand.removeChild(ingredientStrand.firstChild);
  1235. }
  1236. for(let i = 0; i < categories.length; i++){
  1237. let categoryDiv = categoryTemplate.cloneNode(true);
  1238. categoryDiv.children[0].children[0].innerText = categories[i].name;
  1239. categoryDiv.children[0].children[1].onclick = ()=>{this.toggleCategory(categoryDiv.children[1], categoryDiv.children[0].children[1])};
  1240. categoryDiv.children[1].style.display = "none";
  1241. ingredientStrand.appendChild(categoryDiv);
  1242. for(let j = 0; j < categories[i].ingredients.length; j++){
  1243. let ingredient = categories[i].ingredients[j];
  1244. let ingredientDiv = ingredientTemplate.cloneNode(true);
  1245. ingredientDiv.children[0].innerText = ingredient.ingredient.name;
  1246. ingredientDiv.children[2].innerText = `${ingredient.ingredient.convert(ingredient.quantity).toFixed(2)} ${ingredient.ingredient.unit.toUpperCase()}`;
  1247. ingredientDiv.onclick = ()=>{ingredientDetailsComp.display(ingredient)};
  1248. ingredientDiv._name = ingredient.ingredient.name.toLowerCase();
  1249. ingredientDiv._unit = ingredient.ingredient.unit.toLowerCase();
  1250. categoryDiv.children[1].appendChild(ingredientDiv);
  1251. this.ingredients.push(ingredientDiv);
  1252. }
  1253. }
  1254. },
  1255. displayIngredientsOnly: function(ingredients){
  1256. let ingredientDiv = document.querySelector("#categoryList");
  1257. while(ingredientDiv.children.length > 0){
  1258. ingredientDiv.removeChild(ingredientDiv.firstChild);
  1259. }
  1260. for(let i = 0; i < ingredients.length; i++){
  1261. ingredientDiv.appendChild(ingredients[i]);
  1262. }
  1263. },
  1264. toggleCategory: function(div, button){
  1265. if(div.style.display === "none"){
  1266. 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>';
  1267. div.style.display = "flex";
  1268. }else if(div.style.display === "flex"){
  1269. 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>';
  1270. div.style.display = "none";
  1271. }
  1272. },
  1273. search: function(){
  1274. let input = document.querySelector("#ingredientSearch").value.toLowerCase();
  1275. document.querySelector("#ingredientSelect").selectedIndex = 0;
  1276. if(input === ""){
  1277. this.populateByProperty("category");
  1278. document.querySelector("#ingredientClearButton").style.display = "none";
  1279. return;
  1280. }
  1281. let matchingIngredients = [];
  1282. for(let i = 0; i < this.ingredients.length; i++){
  1283. if(this.ingredients[i]._name.includes(input)){
  1284. matchingIngredients.push(this.ingredients[i]);
  1285. }
  1286. }
  1287. document.querySelector("#ingredientClearButton").style.display = "inline";
  1288. this.displayIngredientsOnly(matchingIngredients);
  1289. },
  1290. sort: function(sortType){
  1291. if(sortType === ""){
  1292. return;
  1293. }
  1294. document.querySelector("#ingredientSearch").value = "";
  1295. if(sortType === "category"){
  1296. this.populateByProperty("category");
  1297. return;
  1298. }
  1299. if(sortType === "unit"){
  1300. this.populateByProperty("unit");
  1301. return;
  1302. }
  1303. document.querySelector("#ingredientClearButton").style.display = "inline";
  1304. let sortedIngredients = this.ingredients.slice().sort((a, b)=> (a[sortType] > b[sortType]) ? 1 : -1);
  1305. this.displayIngredientsOnly(sortedIngredients);
  1306. },
  1307. clearSorting: function(button){
  1308. document.querySelector("#ingredientSearch").value = "";
  1309. document.querySelector("#ingredientSelect").selectedIndex = 0;
  1310. document.querySelector("#ingredientClearButton").style.display = "none";
  1311. this.populateByProperty("category");
  1312. }
  1313. }
  1314. },{}],6:[function(require,module,exports){
  1315. module.exports = {
  1316. display: function(){
  1317. openSidebar(document.querySelector("#newIngredient"));
  1318. document.querySelector("#newIngName").value = "";
  1319. document.querySelector("#newIngCategory").value = "";
  1320. document.querySelector("#newIngQuantity").value = 0;
  1321. },
  1322. submit: function(){
  1323. let unitSelector = document.getElementById("unitSelector");
  1324. let options = document.querySelectorAll("#unitSelector option");
  1325. let unit = unitSelector.value;
  1326. let newIngredient = {
  1327. ingredient: {
  1328. name: document.getElementById("newIngName").value,
  1329. category: document.getElementById("newIngCategory").value,
  1330. unitType: options[unitSelector.selectedIndex].getAttribute("type"),
  1331. },
  1332. quantity: convertToMain(unit, document.querySelector("#newIngQuantity").value),
  1333. defaultUnit: unit
  1334. }
  1335. let loader = document.getElementById("loaderContainer");
  1336. loader.style.display = "flex";
  1337. fetch("/ingredients/create", {
  1338. method: "POST",
  1339. headers: {
  1340. "Content-Type": "application/json;charset=utf-8"
  1341. },
  1342. body: JSON.stringify(newIngredient)
  1343. })
  1344. .then((response) => response.json())
  1345. .then((response)=>{
  1346. if(typeof(response) === "string"){
  1347. banner.createError(response);
  1348. }else{
  1349. merchant.editIngredients([{
  1350. ingredient: new Ingredient(
  1351. response.ingredient._id,
  1352. response.ingredient.name,
  1353. response.ingredient.category,
  1354. response.ingredient.unitType,
  1355. response.defaultUnit,
  1356. merchant
  1357. ),
  1358. quantity: response.quantity
  1359. }]);
  1360. banner.createNotification("INGREDIENT CREATED");
  1361. }
  1362. })
  1363. .catch((err)=>{
  1364. banner.createError("SOMETHING WENT WRONG. PLEASE REFRESH THE PAGE");
  1365. })
  1366. .finally(()=>{
  1367. loader.style.display = "none";
  1368. });
  1369. }
  1370. }
  1371. },{}],7:[function(require,module,exports){
  1372. module.exports = {
  1373. isPopulated: false,
  1374. unused: [],
  1375. display: function(){
  1376. if(!this.isPopulated){
  1377. let categories = merchant.categorizeIngredients();
  1378. let categoriesList = document.querySelector("#newOrderCategories");
  1379. let template = document.querySelector("#addIngredientsCategory").content.children[0];
  1380. let ingredientTemplate = document.querySelector("#addIngredientsIngredient").content.children[0];
  1381. for(let i = 0; i < categories.length; i++){
  1382. let category = template.cloneNode(true);
  1383. category.children[0].children[0].innerText = categories[i].name;
  1384. category.children[0].children[1].onclick = ()=>{addIngredientsComp.toggleAddIngredient(category)};
  1385. category.children[0].children[1].children[1].style.display = "none";
  1386. category.children[1].style.display = "none";
  1387. categoriesList.appendChild(category);
  1388. for(let j = 0; j < categories[i].ingredients.length; j++){
  1389. let ingredientDiv = ingredientTemplate.cloneNode(true);
  1390. ingredientDiv.children[0].innerText = categories[i].ingredients[j].ingredient.name;
  1391. ingredientDiv.children[2].onclick = ()=>{this.addOne(ingredientDiv, category.children[1])};
  1392. ingredientDiv.ingredient = categories[i].ingredients[j].ingredient;
  1393. this.unused.push(categories[i].ingredients[j]);
  1394. category.children[1].appendChild(ingredientDiv);
  1395. }
  1396. }
  1397. this.isPopulated = true;
  1398. }
  1399. openSidebar(document.querySelector("#newOrder"));
  1400. },
  1401. addOne: function(ingredientDiv, container){
  1402. for(let i = 0; i < this.unused.length; i++){
  1403. if(this.unused[i] === ingredientDiv){
  1404. this.unused.splice(i, 1);
  1405. break;
  1406. }
  1407. }
  1408. let quantityInput = document.createElement("input");
  1409. quantityInput.type = "number";
  1410. quantityInput.placeholder = `QUANTITY (${ingredientDiv.ingredient.unit})`;
  1411. quantityInput.min = "0";
  1412. quantityInput.step = "0.01";
  1413. ingredientDiv.insertBefore(quantityInput, ingredientDiv.children[1]);
  1414. let priceInput = document.createElement("input");
  1415. priceInput.type = "number";
  1416. priceInput.placeholder = "Price Per Unit";
  1417. priceInput.min = "0";
  1418. priceInput.step = "0.01";
  1419. ingredientDiv.insertBefore(priceInput, ingredientDiv.children[2]);
  1420. ingredientDiv.children[4].innerText = "-";
  1421. ingredientDiv.children[4].onclick = ()=>{this.removeOne(ingredientDiv, container)};
  1422. container.removeChild(ingredientDiv);
  1423. document.getElementById("newOrderAdded").appendChild(ingredientDiv);
  1424. },
  1425. removeOne: function(ingredientDiv, container){
  1426. this.unused.push(ingredientDiv.ingredient);
  1427. ingredientDiv.removeChild(ingredientDiv.children[1]);
  1428. ingredientDiv.removeChild(ingredientDiv.children[1]);
  1429. ingredientDiv.children[1].innerText = "+";
  1430. ingredientDiv.children[1].onclick = ()=>{this.addOne(ingredientDiv, container)};
  1431. ingredientDiv.parentElement.removeChild(ingredientDiv);
  1432. container.appendChild(ingredientDiv);
  1433. },
  1434. submit: function(){
  1435. let categoriesList = document.getElementById("newOrderAdded");
  1436. let ingredients = [];
  1437. for(let i = 0; i < categoriesList.children.length; i++){
  1438. let quantity = categoriesList.children[i].children[1].value;
  1439. let price = categoriesList.children[i].children[2].value;
  1440. let fakeOrder = new Order(undefined, undefined, new Date(), [], undefined);
  1441. if(quantity !== "" && price !== ""){
  1442. ingredients.push({
  1443. ingredient: categoriesList.children[i].ingredient.id,
  1444. quantity: convertToMain(categoriesList.children[i].ingredient.unit, parseFloat(quantity)),
  1445. price: categoriesList.children[i].ingredient.convert(parseInt(price * 100))
  1446. });
  1447. }
  1448. }
  1449. let date = `${document.getElementById("orderDate").value}T${document.getElementById("orderTime").value}:00`
  1450. let data = {
  1451. name: document.getElementById("orderName").value,
  1452. date: date,
  1453. ingredients: ingredients
  1454. };
  1455. let loader = document.getElementById("loaderContainer");
  1456. loader.style.display = "flex";
  1457. fetch("/order/create", {
  1458. method: "POST",
  1459. headers: {
  1460. "Content-Type": "application/json;charset=utf-8"
  1461. },
  1462. body: JSON.stringify(data)
  1463. })
  1464. .then(response => response.json())
  1465. .then((response)=>{
  1466. if(typeof(response) === "string"){
  1467. banner.createError(response);
  1468. }else{
  1469. let order = new Order(
  1470. response._id,
  1471. response.name,
  1472. response.date,
  1473. response.ingredients,
  1474. merchant
  1475. )
  1476. merchant.editOrders([order]);
  1477. merchant.editIngredients(order.ingredients, false, true);
  1478. banner.createNotification("ORDER CREATED");
  1479. }
  1480. })
  1481. .catch((err)=>{
  1482. banner.createError("SOEMTHING WENT WRONG. PLEASE REFRESH THE PAGE");
  1483. })
  1484. .finally(()=>{
  1485. loader.style.display = "none";
  1486. });
  1487. },
  1488. }
  1489. },{}],8:[function(require,module,exports){
  1490. module.exports = {
  1491. display: function(){
  1492. let ingredientsSelect = document.querySelector("#recipeInputIngredients select");
  1493. let categories = merchant.categorizeIngredients();
  1494. while(ingredientsSelect.children.length > 0){
  1495. ingredientsSelect.removeChild(ingredientsSelect.firstChild);
  1496. }
  1497. for(let category of categories){
  1498. let optgroup = document.createElement("optgroup");
  1499. optgroup.label = category.name;
  1500. ingredientsSelect.appendChild(optgroup);
  1501. for(let ingredient of category.ingredients){
  1502. let option = document.createElement("option");
  1503. option.value = ingredient.ingredient.id;
  1504. option.innerText = `${ingredient.ingredient.name} (${ingredient.ingredient.unit})`;
  1505. optgroup.appendChild(option);
  1506. }
  1507. }
  1508. openSidebar(document.querySelector("#addRecipe"));
  1509. },
  1510. //Updates the number of ingredient inputs displayed for new recipes
  1511. changeRecipeCount: function(){
  1512. let newCount = document.querySelector("#ingredientCount").value;
  1513. let ingredientsDiv = document.querySelector("#recipeInputIngredients");
  1514. let oldCount = ingredientsDiv.children.length;
  1515. if(newCount > oldCount){
  1516. let newDivs = newCount - oldCount;
  1517. for(let i = 0; i < newDivs; i++){
  1518. let newNode = ingredientsDiv.children[0].cloneNode(true);
  1519. newNode.children[2].children[0].value = "";
  1520. ingredientsDiv.appendChild(newNode);
  1521. }
  1522. for(let i = 0; i < newCount; i++){
  1523. ingredientsDiv.children[i].children[0].innerText = `INGREDIENT ${i + 1}`;
  1524. }
  1525. }else if(newCount < oldCount){
  1526. let newDivs = oldCount - newCount;
  1527. for(let i = 0; i < newDivs; i++){
  1528. ingredientsDiv.removeChild(ingredientsDiv.children[ingredientsDiv.children.length-1]);
  1529. }
  1530. }
  1531. },
  1532. submit: function(){
  1533. let newRecipe = {
  1534. name: document.getElementById("newRecipeName").value,
  1535. price: document.getElementById("newRecipePrice").value,
  1536. ingredients: []
  1537. }
  1538. let inputs = document.querySelectorAll("#recipeInputIngredients > div");
  1539. for(let i = 0; i < inputs.length; i++){
  1540. for(let j = 0; j < merchant.ingredients.length; j++){
  1541. if(merchant.ingredients[j].ingredient.id === inputs[i].children[1].children[0].value){
  1542. newRecipe.ingredients.push({
  1543. ingredient: inputs[i].children[1].children[0].value,
  1544. quantity: convertToMain(merchant.ingredients[j].ingredient.unit, inputs[i].children[2].children[0].value)
  1545. });
  1546. break;
  1547. }
  1548. }
  1549. }
  1550. if(!validator.recipe(newRecipe)){
  1551. return;
  1552. }
  1553. let loader = document.getElementById("loaderContainer");
  1554. loader.style.display = "flex";
  1555. fetch("/recipe/create", {
  1556. method: "POST",
  1557. headers: {
  1558. "Content-Type": "application/json;charset=utf-8"
  1559. },
  1560. body: JSON.stringify(newRecipe)
  1561. })
  1562. .then((response) => response.json())
  1563. .then((response)=>{
  1564. if(typeof(response) === "string"){
  1565. banner.createError(response);
  1566. }else{
  1567. let recipe = new Recipe(
  1568. response._id,
  1569. response.name,
  1570. response.price,
  1571. response.ingredients,
  1572. merchant,
  1573. );
  1574. merchant.editRecipes([recipe]);
  1575. banner.createNotification("RECIPE CREATED");
  1576. }
  1577. })
  1578. .catch((err)=>{
  1579. banner.createError("SOMETHING WENT WRONG. PLEASE REFRESH THE PAGE");
  1580. })
  1581. .finally(()=>{
  1582. loader.style.display = "none";
  1583. });
  1584. },
  1585. }
  1586. },{}],9:[function(require,module,exports){
  1587. module.exports = {
  1588. display: function(){
  1589. let recipeList = document.getElementById("newTransactionRecipes");
  1590. let template = document.getElementById("createTransaction").content.children[0];
  1591. while(recipeList.children.length > 0){
  1592. recipeList.removeChild(recipeList.firstChild);
  1593. }
  1594. for(let i = 0; i < merchant.recipes.length; i++){
  1595. let recipeDiv = template.cloneNode(true);
  1596. recipeDiv.recipe = merchant.recipes[i];
  1597. recipeList.appendChild(recipeDiv);
  1598. recipeDiv.children[0].innerText = merchant.recipes[i].name;
  1599. }
  1600. openSidebar(document.getElementById("newTransaction"));
  1601. },
  1602. submit: function(){
  1603. let recipeDivs = document.getElementById("newTransactionRecipes");
  1604. let date = document.getElementById("newTransactionDate").valueAsDate;
  1605. if(date > new Date()){
  1606. banner.createError("CANNOT HAVE A DATE IN THE FUTURE");
  1607. return;
  1608. }
  1609. let newTransaction = {
  1610. date: date,
  1611. recipes: []
  1612. };
  1613. for(let i = 0; i < recipeDivs.children.length; i++){
  1614. let quantity = recipeDivs.children[i].children[1].value;
  1615. if(quantity !== "" && quantity > 0){
  1616. newTransaction.recipes.push({
  1617. recipe: recipeDivs.children[i].recipe.id,
  1618. quantity: quantity
  1619. });
  1620. }else if(quantity < 0){
  1621. banner.createError("CANNOT HAVE NEGATIVE VALUES");
  1622. return;
  1623. }
  1624. }
  1625. if(newTransaction.recipes.length > 0){
  1626. let loader = document.getElementById("loaderContainer");
  1627. loader.style.display = "flex";
  1628. fetch("/transaction/create", {
  1629. method: "post",
  1630. headers: {
  1631. "Content-Type": "application/json;charset=utf-8"
  1632. },
  1633. body: JSON.stringify(newTransaction)
  1634. })
  1635. .then(response => response.json())
  1636. .then((response)=>{
  1637. if(typeof(response) === "string"){
  1638. banner.createError(response);
  1639. }else{
  1640. let transaction = new Transaction(
  1641. response._id,
  1642. response.date,
  1643. response.recipes,
  1644. merchant
  1645. );
  1646. merchant.editTransactions(transaction);
  1647. banner.createNotification("NEW TRANSACTION CREATED");
  1648. }
  1649. })
  1650. .catch((err)=>{
  1651. banner.createError("SOMETHING WENT WRONG. PLEASE REFRESH THE PAGE");
  1652. })
  1653. .finally(()=>{
  1654. loader.style.display = "none";
  1655. });
  1656. }
  1657. }
  1658. }
  1659. },{}],10:[function(require,module,exports){
  1660. module.exports = {
  1661. display: function(order){
  1662. openSidebar(document.querySelector("#orderDetails"));
  1663. document.querySelector("#removeOrderBtn").onclick = ()=>{this.remove(order)};
  1664. document.getElementById("orderDetailName").innerText = order.name;
  1665. document.getElementById("orderDetailDate").innerText = order.date.toLocaleDateString("en-US");
  1666. document.getElementById("orderDetailTime").innerText = order.date.toLocaleTimeString("en-US");
  1667. let ingredientList = document.querySelector("#orderIngredients");
  1668. while(ingredientList.children.length > 0){
  1669. ingredientList.removeChild(ingredientList.firstChild);
  1670. }
  1671. let template = document.querySelector("#orderIngredient").content.children[0];
  1672. let grandTotal = 0;
  1673. for(let i = 0; i < order.ingredients.length; i++){
  1674. let ingredientDiv = template.cloneNode(true);
  1675. let price = (order.ingredients[i].quantity * order.ingredients[i].price) / 100;
  1676. grandTotal += price;
  1677. let ingredient = order.ingredients[i].ingredient;
  1678. let priceText = ingredient.convert(order.ingredients[i].quantity).toFixed(2) + " " +
  1679. ingredient.unit.toUpperCase() + " x $" +
  1680. (order.convertPrice(ingredient.unitType, ingredient.unit, order.ingredients[i].price) / 100).toFixed(2);
  1681. ingredientDiv.children[0].innerText = order.ingredients[i].ingredient.name;
  1682. ingredientDiv.children[1].innerText = priceText;
  1683. ingredientDiv.children[2].innerText = `$${price.toFixed(2)}`;
  1684. ingredientList.appendChild(ingredientDiv);
  1685. }
  1686. document.querySelector("#orderTotalPrice p").innerText = `$${grandTotal.toFixed(2)}`;
  1687. },
  1688. remove: function(order){
  1689. let loader = document.getElementById("loaderContainer");
  1690. loader.style.display = "flex";
  1691. fetch(`/order/${order.id}`, {
  1692. method: "DELETE",
  1693. headers: {
  1694. "Content-Type": "application/json;charset=utf-8"
  1695. }
  1696. })
  1697. .then((response) => response.json())
  1698. .then((response)=>{
  1699. if(typeof(response) === "string"){
  1700. banner.createError(response);
  1701. }else{
  1702. merchant.editOrders([order], true);
  1703. banner.createNotification("ORDER REMOVED");
  1704. }
  1705. })
  1706. .catch((err)=>{
  1707. banner.createError("SOMETHING WENT WRONG. PLEASE REFRESH THE PAGE");
  1708. })
  1709. .finally(()=>{
  1710. loader.style.display = "none";
  1711. });
  1712. }
  1713. }
  1714. },{}],11:[function(require,module,exports){
  1715. module.exports = {
  1716. isFetched: false,
  1717. display: async function(){
  1718. if(!this.isFetched){
  1719. let loader = document.getElementById("loaderContainer");
  1720. loader.style.display = "flex";
  1721. fetch("/order", {
  1722. method: "GET",
  1723. headers: {
  1724. "Content-Type": "application/json;charset=utf-8"
  1725. },
  1726. })
  1727. .then((response) => response.json())
  1728. .then((response)=>{
  1729. if(typeof(response) === "string"){
  1730. banner.createError(response);
  1731. }else{
  1732. let newOrders = [];
  1733. for(let i = 0; i < response.length; i++){
  1734. newOrders.push(new Order(
  1735. response[i]._id,
  1736. response[i].name,
  1737. response[i].date,
  1738. response[i].ingredients,
  1739. merchant
  1740. ));
  1741. }
  1742. merchant.editOrders(newOrders);
  1743. this.isFetched = true;
  1744. }
  1745. })
  1746. .catch((err)=>{
  1747. banner.createError("SOMETHING WENT WRONG. TRY REFRESHING THE PAGE");
  1748. })
  1749. .finally(()=>{
  1750. loader.style.display = "none";
  1751. });
  1752. }
  1753. },
  1754. populate: function(){
  1755. let listDiv = document.getElementById("orderList");
  1756. let template = document.getElementById("order").content.children[0];
  1757. let dateDropdown = document.getElementById("dateDropdownOrder");
  1758. let ingredientDropdown = document.getElementById("ingredientDropdown");
  1759. dateDropdown.style.display = "none";
  1760. ingredientDropdown.style.display = "none";
  1761. document.getElementById("dateFilterBtnOrder").onclick = ()=>{this.toggleDropdown(dateDropdown)};
  1762. document.getElementById("ingredientFilterBtn").onclick = ()=>{this.toggleDropdown(ingredientDropdown)};
  1763. for(let i = 0; i < merchant.ingredients.length; i++){
  1764. let checkbox = document.createElement("input");
  1765. checkbox.type = "checkbox";
  1766. checkbox.ingredient = merchant.ingredients[i].ingredient;
  1767. ingredientDropdown.appendChild(checkbox);
  1768. let label = document.createElement("label");
  1769. label.innerText = merchant.ingredients[i].ingredient.name;
  1770. label.for = checkbox;
  1771. ingredientDropdown.appendChild(label);
  1772. let brk = document.createElement("br");
  1773. ingredientDropdown.appendChild(brk);
  1774. }
  1775. while(listDiv.children.length > 0){
  1776. listDiv.removeChild(listDiv.firstChild);
  1777. }
  1778. for(let i = 0; i < merchant.orders.length; i++){
  1779. let row = template.cloneNode(true);
  1780. let totalCost = 0;
  1781. for(let j = 0; j < merchant.orders[i].ingredients.length; j++){
  1782. totalCost += merchant.orders[i].ingredients[j].quantity * merchant.orders[i].ingredients[j].price;
  1783. }
  1784. row.children[0].innerText = merchant.orders[i].name;
  1785. row.children[1].innerText = `${merchant.orders[i].ingredients.length} items`;
  1786. row.children[2].innerText = new Date(merchant.orders[i].date).toLocaleDateString("en-US");
  1787. row.children[3].innerText = `$${(totalCost / 100).toFixed(2)}`;
  1788. row.order = merchant.orders[i];
  1789. row.onclick = ()=>{orderDetailsComp.display(merchant.orders[i])};
  1790. listDiv.appendChild(row);
  1791. }
  1792. },
  1793. submitFilter: function(){
  1794. event.preventDefault();
  1795. let data = {
  1796. startDate: document.getElementById("orderFilDate1").valueAsDate,
  1797. endDate: document.getElementById("orderFilDate2").valueAsDate,
  1798. ingredients: []
  1799. }
  1800. if(data.startDate >= data.endDate){
  1801. banner.createError("START DATE CANNOT BE AFTER END DATE");
  1802. return;
  1803. }
  1804. let ingredientChoices = document.getElementById("ingredientDropdown");
  1805. for(let i = 0; i < ingredientChoices.children.length; i += 3){
  1806. if(ingredientChoices.children[i].checked){
  1807. data.ingredients.push(ingredientChoices.children[i].ingredient.id);
  1808. }
  1809. }
  1810. if(data.ingredients.length === 0){
  1811. for(let i = 0; i < merchant.ingredients.length; i++){
  1812. data.ingredients.push(merchant.ingredients[i].ingredient.id);
  1813. }
  1814. }
  1815. let loader = document.getElementById("loaderContainer");
  1816. loader.style.display = "flex";
  1817. fetch("/order", {
  1818. method: "POST",
  1819. headers: {
  1820. "Content-Type": "application/json;charset=utf-8"
  1821. },
  1822. body: JSON.stringify(data)
  1823. })
  1824. .then((response) => response.json())
  1825. .then((response)=>{
  1826. if(typeof(response) === "string"){
  1827. banner.createError(response);
  1828. }else{
  1829. let orderList = document.getElementById("orderList");
  1830. let template = document.getElementById("order").content.children[0];
  1831. while(orderList.children.length > 0){
  1832. orderList.removeChild(orderList.firstChild);
  1833. }
  1834. for(let i = 0; i < response.length; i++){
  1835. let order = template.cloneNode(true);
  1836. let cost = 0;
  1837. for(let j = 0; j < response[i].ingredients.length; j++){
  1838. cost += (response[i].ingredients[j].price / 100) * response[i].ingredients[j].quantity;
  1839. }
  1840. order.children[0].innerText = response[i].name,
  1841. order.children[1].innerText = `${response[i].ingredients.length} items`;
  1842. order.children[2].innerText = new Date(response[i].date).toLocaleDateString();
  1843. order.children[3].innerText = `$${cost.toFixed(2)}`;
  1844. orderList.appendChild(order);
  1845. }
  1846. }
  1847. })
  1848. .catch((err)=>{
  1849. banner.createError("UNABLE TO DISPLAY THE ORDERS");
  1850. })
  1851. .finally(()=>{
  1852. loader.style.display = "none";
  1853. });
  1854. },
  1855. toggleDropdown: function(dropdown){
  1856. event.preventDefault();
  1857. let polyline = dropdown.parentElement.children[0].children[1].children[0].children[0];
  1858. if(dropdown.style.display === "none"){
  1859. dropdown.style.display = "block";
  1860. polyline.setAttribute("points", "18 15 12 9 6 15");
  1861. }else{
  1862. dropdown.style.display = "none";
  1863. polyline.setAttribute("points", "6 9 12 15 18 9");
  1864. }
  1865. }
  1866. }
  1867. },{}],12:[function(require,module,exports){
  1868. module.exports = {
  1869. isPopulated: false,
  1870. recipeDivList: [],
  1871. display: function(){
  1872. if(!this.isPopulated){
  1873. this.populateRecipes();
  1874. this.isPopulated = true;
  1875. }
  1876. },
  1877. populateRecipes: function(){
  1878. let recipeList = document.getElementById("recipeList");
  1879. let template = document.getElementById("recipe").content.children[0];
  1880. this.recipeDivList = [];
  1881. while(recipeList.children.length > 0){
  1882. recipeList.removeChild(recipeList.firstChild);
  1883. }
  1884. for(let i = 0; i < merchant.recipes.length; i++){
  1885. let recipeDiv = template.cloneNode(true);
  1886. recipeDiv.onclick = ()=>{recipeDetailsComp.display(merchant.recipes[i])};
  1887. recipeDiv._name = merchant.recipes[i].name;
  1888. recipeList.appendChild(recipeDiv);
  1889. recipeDiv.children[0].innerText = merchant.recipes[i].name;
  1890. recipeDiv.children[1].innerText = `$${(merchant.recipes[i].price / 100).toFixed(2)}`;
  1891. this.recipeDivList.push(recipeDiv);
  1892. }
  1893. },
  1894. search: function(){
  1895. let input = document.getElementById("recipeSearch").value.toLowerCase();
  1896. let recipeList = document.getElementById("recipeList");
  1897. let clearButton = document.getElementById("recipeClearButton");
  1898. let matchingRecipes = [];
  1899. for(let i = 0; i < this.recipeDivList.length; i++){
  1900. if(this.recipeDivList[i]._name.toLowerCase().includes(input)){
  1901. matchingRecipes.push(this.recipeDivList[i]);
  1902. }
  1903. }
  1904. while(recipeList.children.length > 0){
  1905. recipeList.removeChild(recipeList.firstChild);
  1906. }
  1907. for(let i = 0; i < matchingRecipes.length; i++){
  1908. recipeList.appendChild(matchingRecipes[i]);
  1909. }
  1910. if(input === ""){
  1911. clearButton.style.display = "none";
  1912. }else{
  1913. clearButton.style.display = "inline";
  1914. }
  1915. },
  1916. clearSorting: function(){
  1917. document.getElementById("recipeSearch").value = "";
  1918. this.search();
  1919. },
  1920. posUpdate: function(){
  1921. let loader = document.getElementById("loaderContainer");
  1922. loader.style.display = "flex";
  1923. fetch("/recipe/update/clover", {
  1924. method: "GET",
  1925. headers: {
  1926. "Content-Type": "application/json;charset=utf-8"
  1927. },
  1928. })
  1929. .then(response => response.json())
  1930. .then((response)=>{
  1931. let newRecipes = [];
  1932. for(let i = 0; i < response.new.length; i++){
  1933. newRecipes.push(new Recipe(
  1934. response.new[i]._id,
  1935. response.new[i].name,
  1936. response.new[i].price,
  1937. merchant,
  1938. []
  1939. ));
  1940. }
  1941. if(newRecipes.length > 0){
  1942. merchant.editRecipes(newRecipes);
  1943. }
  1944. let removeRecipes = [];
  1945. for(let i = 0; i < response.removed.length; i++){
  1946. for(let j = 0; j < merchant.recipes.length; j++){
  1947. if(response.removed[i]._id === merchant.recipes[j].id){
  1948. removeRecipes.push(merchant.recipes[j], true);
  1949. break;
  1950. }
  1951. }
  1952. }
  1953. if(removeRecipes.length > 0){
  1954. merchant.editRecipes(removeRecipes, true);
  1955. }
  1956. })
  1957. .catch((err)=>{
  1958. banner.createError("SOMETHING WENT WRONG. PLEASE REFRESH THE PAGE");
  1959. })
  1960. .finally(()=>{
  1961. loader.style.display = "none";
  1962. });
  1963. }
  1964. }
  1965. },{}],13:[function(require,module,exports){
  1966. module.exports = {
  1967. recipe: {},
  1968. display: function(recipe){
  1969. this.recipe = recipe;
  1970. openSidebar(document.querySelector("#recipeDetails"));
  1971. document.querySelector("#recipeName").style.display = "block";
  1972. document.querySelector("#recipeNameIn").style.display = "none";
  1973. document.querySelector("#recipeDetails h1").innerText = recipe.name;
  1974. let ingredientList = document.querySelector("#recipeIngredientList");
  1975. while(ingredientList.children.length > 0){
  1976. ingredientList.removeChild(ingredientList.firstChild);
  1977. }
  1978. let template = document.querySelector("#recipeIngredient").content.children[0];
  1979. for(let i = 0; i < recipe.ingredients.length; i++){
  1980. ingredientDiv = template.cloneNode(true);
  1981. ingredientDiv.children[0].innerText = recipe.ingredients[i].ingredient.name;
  1982. ingredientDiv.children[2].innerText = `${recipe.ingredients[i].ingredient.convert(recipe.ingredients[i].quantity).toFixed(2)} ${recipe.ingredients[i].ingredient.unit}`;
  1983. ingredientDiv.ingredient = recipe.ingredients[i].ingredient;
  1984. ingredientDiv.name = recipe.ingredients[i].ingredient.name;
  1985. ingredientList.appendChild(ingredientDiv);
  1986. }
  1987. document.querySelector("#addRecIng").style.display = "none";
  1988. let price = document.querySelector("#recipePrice");
  1989. price.children[1].style.display = "block";
  1990. price.children[2].style.display = "none";
  1991. price.children[1].innerText = `$${(recipe.price / 100).toFixed(2)}`;
  1992. document.querySelector("#recipeUpdate").style.display = "none";
  1993. },
  1994. edit: function(){
  1995. let ingredientDivs = document.querySelector("#recipeIngredientList");
  1996. if(merchant.pos === "none"){
  1997. let name = document.querySelector("#recipeName");
  1998. let nameIn = document.querySelector("#recipeNameIn");
  1999. name.style.display = "none";
  2000. nameIn.style.display = "block";
  2001. nameIn.value = this.recipe.name;
  2002. let price = document.querySelector("#recipePrice");
  2003. price.children[1].style.display = "none";
  2004. price.children[2].style.display = "block";
  2005. price.children[2].value = parseFloat((this.recipe.price / 100).toFixed(2));
  2006. }
  2007. for(let i = 0; i < ingredientDivs.children.length; i++){
  2008. let div = ingredientDivs.children[i];
  2009. div.children[2].innerText = this.recipe.ingredients[i].ingredient.unit;
  2010. div.children[1].style.display = "block";
  2011. div.children[1].value = this.recipe.ingredients[i].ingredient.convert(this.recipe.ingredients[i].quantity).toFixed(2);
  2012. div.children[3].style.display = "block";
  2013. div.children[3].onclick = ()=>{div.parentElement.removeChild(div)};
  2014. }
  2015. document.querySelector("#addRecIng").style.display = "flex";
  2016. document.querySelector("#recipeUpdate").style.display = "flex";
  2017. },
  2018. update: function(){
  2019. this.recipe.name = document.querySelector("#recipeNameIn").value || this.recipe.name;
  2020. this.recipe.price = Math.round((document.querySelector("#recipePrice").children[2].value * 100)) || this.recipe.price;
  2021. this.recipe.ingredients = [];
  2022. let divs = document.querySelector("#recipeIngredientList").children;
  2023. for(let i = 0; i < divs.length; i++){
  2024. if(divs[i].name === "new"){
  2025. let select = divs[i].children[0];
  2026. this.recipe.ingredients.push({
  2027. ingredient: select.options[select.selectedIndex].ingredient,
  2028. quantity: convertToMain(select.options[select.selectedIndex].ingredient.unit, divs[i].children[1].value)
  2029. });
  2030. }else{
  2031. this.recipe.ingredients.push({
  2032. ingredient: divs[i].ingredient,
  2033. quantity: convertToMain(divs[i].ingredient.unit, divs[i].children[1].value)
  2034. });
  2035. }
  2036. }
  2037. let data = {
  2038. id: this.recipe.id,
  2039. name: this.recipe.name,
  2040. price: this.recipe.price,
  2041. ingredients: []
  2042. }
  2043. for(let i = 0; i < this.recipe.ingredients.length; i++){
  2044. data.ingredients.push({
  2045. ingredient: this.recipe.ingredients[i].ingredient.id,
  2046. quantity: this.recipe.ingredients[i].quantity
  2047. });
  2048. }
  2049. let loader = document.getElementById("loaderContainer");
  2050. loader.style.display = "flex";
  2051. fetch("/recipe/update", {
  2052. method: "PUT",
  2053. headers: {
  2054. "Content-Type": "application/json;charset=utf-8"
  2055. },
  2056. body: JSON.stringify(data)
  2057. })
  2058. .then((response) => response.json())
  2059. .then((response)=>{
  2060. if(typeof(response) === "string"){
  2061. banner.createError(response);
  2062. }else{
  2063. merchant.editRecipes([this.recipe]);
  2064. banner.createNotification("RECIPE UPDATE");
  2065. }
  2066. })
  2067. .catch((err)=>{
  2068. banner.createError("SOMETHING WENT WRONG. PLEASE REFRESH THE PAGE");
  2069. })
  2070. .finally(()=>{
  2071. loader.style.display = "none";
  2072. });
  2073. },
  2074. remove: function(){
  2075. fetch(`/merchant/recipes/remove/${this.recipe.id}`, {
  2076. method: "DELETE"
  2077. })
  2078. .then((response) => response.json())
  2079. .then((response)=>{
  2080. if(typeof(response) === "string"){
  2081. banner.createError(response);
  2082. }else{
  2083. merchant.editRecipes([this.recipe], true);
  2084. banner.createNotification("RECIPE REMOVED");
  2085. }
  2086. })
  2087. .catch((err)=>{
  2088. banner.createError("SOMETHING WENT WRONG. PLEASE REFRESH THE PAGE");
  2089. });
  2090. },
  2091. displayAddIngredient: function(){
  2092. let template = document.querySelector("#addRecIngredient").content.children[0].cloneNode(true);
  2093. template.name = "new";
  2094. document.querySelector("#recipeIngredientList").appendChild(template);
  2095. let categories = merchant.categorizeIngredients();
  2096. for(let i = 0; i < categories.length; i++){
  2097. let optGroup = document.createElement("optgroup");
  2098. optGroup.label = categories[i].name;
  2099. template.children[0].appendChild(optGroup);
  2100. for(let j = 0; j < categories[i].ingredients.length; j++){
  2101. let option = document.createElement("option");
  2102. option.innerText = `${categories[i].ingredients[j].ingredient.name} (${categories[i].ingredients[j].ingredient.unit})`;
  2103. option.ingredient = categories[i].ingredients[j].ingredient;
  2104. optGroup.appendChild(option);
  2105. }
  2106. }
  2107. }
  2108. }
  2109. },{}],14:[function(require,module,exports){
  2110. module.exports = {
  2111. transaction: {},
  2112. display: function(transaction){
  2113. this.transaction = transaction;
  2114. let recipeList = document.getElementById("transactionRecipes");
  2115. let template = document.getElementById("transactionRecipe").content.children[0];
  2116. let totalRecipes = 0;
  2117. let totalPrice = 0;
  2118. while(recipeList.children.length > 0){
  2119. recipeList.removeChild(recipeList.firstChild);
  2120. }
  2121. for(let i = 0; i < transaction.recipes.length; i++){
  2122. let recipe = template.cloneNode(true);
  2123. let price = transaction.recipes[i].quantity * transaction.recipes[i].recipe.price;
  2124. recipe.children[0].innerText = transaction.recipes[i].recipe.name;
  2125. recipe.children[1].innerText = `${transaction.recipes[i].quantity} x $${parseFloat(transaction.recipes[i].recipe.price / 100).toFixed(2)}`;
  2126. recipe.children[2].innerText = `$${(price / 100).toFixed(2)}`;
  2127. recipeList.appendChild(recipe);
  2128. totalRecipes += transaction.recipes[i].quantity;
  2129. totalPrice += price;
  2130. }
  2131. let months = ["January", "Fecbruary", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
  2132. let days = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"];
  2133. let dateString = `${days[transaction.date.getDay()]}, ${months[transaction.date.getMonth()]} ${transaction.date.getDate()}, ${transaction.date.getFullYear()}`;
  2134. document.getElementById("transactionDate").innerText = dateString;
  2135. document.getElementById("transactionTime").innerText = transaction.date.toLocaleTimeString();
  2136. document.getElementById("totalRecipes").innerText = `${totalRecipes} recipes`;
  2137. document.getElementById("totalPrice").innerText = `$${(totalPrice / 100).toFixed(2)}`;
  2138. openSidebar(document.getElementById("transactionDetails"));
  2139. },
  2140. remove: function(){
  2141. let loader = document.getElementById("loaderContainer");
  2142. loader.style.display = "flex";
  2143. fetch(`/transaction/${this.transaction.id}`, {
  2144. method: "delete",
  2145. headers: {
  2146. "Content-Type": "application/json;charset=utf-8"
  2147. },
  2148. })
  2149. .then(response => response.json())
  2150. .then((response)=>{
  2151. if(typeof(response) === "string"){
  2152. banner.createError(response);
  2153. }else{
  2154. merchant.editTransactions(this.transaction, true);
  2155. banner.createNotification("TRANSACTION REMOVED");
  2156. }
  2157. })
  2158. .catch((err)=>{
  2159. banner.createError("SOMETHING WENT WRONG. PLEASE REFRESH THE PAGE");
  2160. })
  2161. .finally(()=>{
  2162. loader.style.display = "none";
  2163. });
  2164. },
  2165. }
  2166. },{}],15:[function(require,module,exports){
  2167. module.exports = {
  2168. isPopulated: false,
  2169. display: function(){
  2170. if(!this.isPopulated){
  2171. let transactionsList = document.getElementById("transactionsList");
  2172. let dateDropdown = document.getElementById("dateDropdown");
  2173. let recipeDropdown = document.getElementById("recipeDropDown");
  2174. let template = document.getElementById("transaction").content.children[0];
  2175. let now = new Date();
  2176. let monthAgo = new Date(now.getFullYear(), now.getMonth() - 1, now.getDate());
  2177. document.getElementById("transFilDate1").valueAsDate = monthAgo;
  2178. document.getElementById("transFilDate2").valueAsDate = now;
  2179. dateDropdown.style.display = "none";
  2180. recipeDropdown.style.display = "none";
  2181. document.getElementById("dateFilterBtn").onclick = ()=>{this.toggleDropdown(dateDropdown)};
  2182. document.getElementById("recipeFilterBtn").onclick = ()=>{this.toggleDropdown(recipeDropdown)};
  2183. while(recipeDropdown.children.length > 0){
  2184. recipeDropdown.removeChild(recipeDropdown.firstChild);
  2185. }
  2186. for(let i = 0; i < merchant.recipes.length; i++){
  2187. let checkbox = document.createElement("input");
  2188. checkbox.type = "checkbox";
  2189. checkbox.recipe = merchant.recipes[i];
  2190. recipeDropdown.appendChild(checkbox);
  2191. let label = document.createElement("label");
  2192. label.innerText = merchant.recipes[i].name;
  2193. label.for = checkbox;
  2194. recipeDropdown.appendChild(label);
  2195. let brk = document.createElement("br");
  2196. recipeDropdown.appendChild(brk);
  2197. }
  2198. while(transactionsList.children.length > 0){
  2199. transactionsList.removeChild(transactionsList.firstChild);
  2200. }
  2201. let i = 0
  2202. while(i < merchant.transactions.length && i < 100){
  2203. let transactionDiv = template.cloneNode(true);
  2204. let transaction = merchant.transactions[i];
  2205. transactionDiv.onclick = ()=>{transactionDetailsComp.display(transaction)};
  2206. transactionsList.appendChild(transactionDiv);
  2207. let totalRecipes = 0;
  2208. let totalPrice = 0;
  2209. for(let j = 0; j < merchant.transactions[i].recipes.length; j++){
  2210. totalRecipes += merchant.transactions[i].recipes[j].quantity;
  2211. totalPrice += merchant.transactions[i].recipes[j].recipe.price * merchant.transactions[i].recipes[j].quantity;
  2212. }
  2213. transactionDiv.children[0].innerText = `${merchant.transactions[i].date.toLocaleDateString()} ${merchant.transactions[i].date.toLocaleTimeString()}`;
  2214. transactionDiv.children[1].innerText = `${totalRecipes} recipes sold`;
  2215. transactionDiv.children[2].innerText = `$${(totalPrice / 100).toFixed(2)}`;
  2216. i++;
  2217. }
  2218. this.isPopulated = true;
  2219. }
  2220. },
  2221. submitFilter: function(){
  2222. event.preventDefault();
  2223. let data = {
  2224. startDate: document.getElementById("transFilDate1").valueAsDate,
  2225. endDate: document.getElementById("transFilDate2").valueAsDate,
  2226. recipes: []
  2227. }
  2228. if(data.startDate >= data.endDate){
  2229. banner.createError("START DATE CANNOT BE AFTER END DATE");
  2230. return;
  2231. }
  2232. let recipeChoices = document.getElementById("recipeDropDown");
  2233. for(let i = 0; i < recipeChoices.children.length; i += 3){
  2234. if(recipeChoices.children[i].checked){
  2235. data.recipes.push(recipeChoices.children[i].recipe.id);
  2236. }
  2237. }
  2238. if(data.recipes.length === 0){
  2239. for(let i = 0; i < merchant.recipes.length; i++){
  2240. data.recipes.push(merchant.recipes[i].id);
  2241. }
  2242. }
  2243. let loader = document.getElementById("loaderContainer");
  2244. loader.style.display = "flex";
  2245. fetch("/transaction", {
  2246. method: "POST",
  2247. headers: {
  2248. "Content-Type": "application/json;charset=utf-8"
  2249. },
  2250. body: JSON.stringify(data)
  2251. })
  2252. .then((response) => response.json())
  2253. .then((response)=>{
  2254. if(typeof(response) === "string"){
  2255. banner.createError(response);
  2256. }else{
  2257. let transactionList = document.getElementById("transactionsList");
  2258. let template = document.getElementById("transaction").content.children[0];
  2259. while(transactionList.children.length > 0){
  2260. transactionList.removeChild(transactionList.firstChild);
  2261. }
  2262. for(let i = 0; i < response.length; i++){
  2263. let transactionDiv = template.cloneNode(true);
  2264. let recipeCount = 0;
  2265. let cost = 0;
  2266. let transaction = new Transaction(
  2267. response[i]._id,
  2268. response[i].date,
  2269. response[i].recipes,
  2270. merchant
  2271. );
  2272. for(let j = 0; j < transaction.recipes.length; j++){
  2273. recipeCount += transaction.recipes[j].quantity;
  2274. cost += transaction.recipes[j].quantity * transaction.recipes[j].recipe.price;
  2275. }
  2276. transactionDiv.children[0].innerText = `${transaction.date.toLocaleDateString()} ${transaction.date.toLocaleTimeString()}`;
  2277. transactionDiv.children[1].innerText = `${recipeCount} recipes sold`;
  2278. transactionDiv.children[2].innerText = `$${(cost / 100).toFixed(2)}`;
  2279. transactionDiv.onclick = ()=>{transactionDetailsComp.display(transaction)};
  2280. transactionList.appendChild(transactionDiv);
  2281. }
  2282. }
  2283. })
  2284. .catch((err)=>{
  2285. banner.createError("UNABLE TO DISPLAY THE TRANSACTIONS");
  2286. })
  2287. .finally(()=>{
  2288. loader.style.display = "none";
  2289. });
  2290. },
  2291. toggleDropdown: function(dropdown){
  2292. event.preventDefault();
  2293. let polyline = dropdown.parentElement.children[0].children[1].children[0].children[0];
  2294. if(dropdown.style.display === "none"){
  2295. dropdown.style.display = "block";
  2296. polyline.setAttribute("points", "18 15 12 9 6 15");
  2297. }else{
  2298. dropdown.style.display = "none";
  2299. polyline.setAttribute("points", "6 9 12 15 18 9");
  2300. }
  2301. }
  2302. }
  2303. },{}],16:[function(require,module,exports){
  2304. //Creates a line graph within a canvas
  2305. //Will expand or shrink to the size of the canvas
  2306. //Inputs:
  2307. // canvas = the canvas that you would like to draw on
  2308. // yName = a string for the name of the Y axis
  2309. // xName = a string for the name of the X axis
  2310. class LineGraph{
  2311. constructor(canvas){
  2312. this.canvas = canvas;
  2313. this.context = canvas.getContext("2d");
  2314. this.left = canvas.clientWidth - (canvas.clientWidth * 0.95);
  2315. this.right = canvas.clientWidth * 1;
  2316. this.top = canvas.clientHeight - (canvas.clientHeight * 1);
  2317. this.bottom = canvas.clientHeight * 0.85;
  2318. this.data = [];
  2319. this.max = 0;
  2320. this.xRange = [];
  2321. this.colors = [];
  2322. this.colorIndex = 0;
  2323. for(let i = 0; i < 100; i++){
  2324. let redRand = Math.floor(Math.random() * 200);
  2325. let greenRand = Math.floor(Math.random() * 200);
  2326. let blueRand = Math.floor(Math.random() * 200);
  2327. this.colors.push(`rgb(${redRand}, ${greenRand}, ${blueRand})`);
  2328. }
  2329. }
  2330. //Add a dataset to the graph to draw
  2331. //Inputs:
  2332. // data = array containing list of numbers as the data points for the graph
  2333. // data[0] will be on the left. data[data.length-1] will be on the right.
  2334. // xRange = array containing two elements, start and end for x axis data (currently only dates)
  2335. // name = string name for the line. Used for display and finding lines. Each must be unique
  2336. addData(data, xRange, name){
  2337. data = {
  2338. set: data,
  2339. colorIndex: this.colorIndex,
  2340. name: name
  2341. }
  2342. this.colorIndex++;
  2343. this.data.push(data);
  2344. let isChange = false;
  2345. for(let point of data.set){
  2346. if(point > this.max){
  2347. this.max = point;
  2348. this.verticalMultiplier = (this.bottom - this.top) / this.max;
  2349. this.horizontalMultiplier = (this.right - this.left) / (data.set.length - 1);
  2350. isChange = true;
  2351. }
  2352. }
  2353. if(this.xRange.length === 0){
  2354. this.xRange = xRange;
  2355. isChange = true;
  2356. }else{
  2357. if(xRange[0] < this.xRange[0]){
  2358. this.xRange[0] = xRange[0];
  2359. isChange = true;
  2360. }
  2361. if(xRange[1] > this.xRange[1]){
  2362. this.xRange[1] = xRange[1];
  2363. isChange = true;
  2364. }
  2365. }
  2366. if(isChange){
  2367. this.drawGraph();
  2368. }else{
  2369. this.drawLine(data);
  2370. }
  2371. }
  2372. //Removes a single data set from the graph and its line
  2373. //Inputs:
  2374. // id = the unique identifier of the data set that was passed in with addData function
  2375. removeData(name){
  2376. for(let i = 0; i < this.data.length; i++){
  2377. if(this.data[i].name === name){
  2378. this.data.splice(i, 1);
  2379. break;
  2380. }
  2381. }
  2382. this.drawGraph();
  2383. }
  2384. //Completely clears all data
  2385. //Does not delete the current graph displaying
  2386. clearData(){
  2387. this.max = 0;
  2388. this.data = [];
  2389. this.xRange = [];
  2390. }
  2391. addTitle(title){
  2392. this.top = this.canvas.clientHeight - (this.canvas.clientHeight * 0.9);
  2393. this.title = title;
  2394. }
  2395. /**********
  2396. *********PRIVATE*********
  2397. **********/
  2398. drawGraph(){
  2399. this.context.clearRect(0, 0, this.canvas.width, this.canvas.height);
  2400. this.drawYAxis();
  2401. this.drawXAxis();
  2402. for(let dataSet of this.data){
  2403. this.drawLine(dataSet);
  2404. }
  2405. if(this.title){
  2406. this.context.font = "25px Saira";
  2407. let xLocation = ((this.right - this.left) / 2) - (this.context.measureText(this.title).width / 2);
  2408. this.context.fillText(this.title, xLocation, this.top - 10);
  2409. }
  2410. }
  2411. drawLine(data){
  2412. for(let i = 0; i < data.set.length - 1; i++){
  2413. this.context.beginPath();
  2414. this.context.moveTo(this.left + (this.horizontalMultiplier * i), this.bottom - (this.verticalMultiplier * data.set[i]));
  2415. this.context.lineTo(this.left + (this.horizontalMultiplier * (i + 1)), this.bottom - (this.verticalMultiplier * data.set[i + 1]));
  2416. this.context.strokeStyle = this.colors[data.colorIndex];
  2417. this.context.lineWidth = 2;
  2418. this.context.stroke();
  2419. }
  2420. this.context.strokeStyle = "black";
  2421. if(this.data.length > 1){
  2422. this.drawLegend(data.colorIndex, data.name);
  2423. }
  2424. }
  2425. drawXAxis(){
  2426. this.context.beginPath();
  2427. this.context.moveTo(this.left, this.bottom);
  2428. this.context.lineTo(this.right, this.bottom);
  2429. this.context.lineWidth = 4;
  2430. this.context.stroke();
  2431. this.context.setLineDash([5, 10]);
  2432. this.context.font = "10px Arial";
  2433. this.context.lineWidth = 1;
  2434. if(Object.prototype.toString.call(this.xRange[0]) === '[object Date]'){
  2435. let diff = Math.abs(Math.floor((Date.UTC(this.xRange[0].getFullYear(), this.xRange[0].getMonth(), this.xRange[0].getDate()) - Date.UTC(this.xRange[1].getFullYear(), this.xRange[1].getMonth(), this.xRange[1].getDate())) / (1000 * 60 * 60 * 24))) + 1;
  2436. let showDate = new Date(this.xRange[0]);
  2437. for(let i = 0; i < diff; i += Math.floor(diff / 10)){
  2438. this.context.fillText(showDate.toLocaleDateString("en-US", {month: "short", day: "numeric", year: "2-digit"}), this.left + (this.horizontalMultiplier * i) - 20, this.bottom + 15);
  2439. if(i !== 0){
  2440. this.context.beginPath()
  2441. this.context.moveTo(this.left + (this.horizontalMultiplier * i), this.bottom);
  2442. this.context.lineTo(this.left + (this.horizontalMultiplier * i), this.top);
  2443. this.context.strokeStyle = "#a5a5a5";
  2444. this.context.stroke();
  2445. }
  2446. showDate.setDate(showDate.getDate() + Math.abs(diff / 10));
  2447. }
  2448. }
  2449. this.context.strokeStyle = "black";
  2450. this.context.setLineDash([]);
  2451. }
  2452. drawYAxis(){
  2453. this.context.beginPath();
  2454. this.context.moveTo(this.left, this.top);
  2455. this.context.lineTo(this.left, this.bottom);
  2456. this.context.lineWidth = 2;
  2457. this.context.stroke();
  2458. this.context.setLineDash([5, 10]);
  2459. this.context.font = "10px Arial";
  2460. this.context.lineWidth = 1;
  2461. let axisNum = 0;
  2462. let verticalIncrement = (this.bottom - this.top) / 10;
  2463. let verticalOffset = 0;
  2464. do{
  2465. this.context.fillText(Math.round(axisNum).toString(), this.left - 20, this.bottom - verticalOffset + 3);
  2466. this.context.beginPath();
  2467. this.context.moveTo(this.left, this.bottom - verticalOffset);
  2468. this.context.lineTo(this.right, this.bottom - verticalOffset);
  2469. this.context.strokeStyle = "#a5a5a5";
  2470. this.context.stroke();
  2471. verticalOffset += verticalIncrement;
  2472. axisNum += this.max / 10;
  2473. }while(verticalOffset <= (this.bottom - this.top));
  2474. this.context.strokeStyle = "black";
  2475. this.context.setLineDash([]);
  2476. }
  2477. drawLegend(colorIndex, name){
  2478. let verticalOffset;
  2479. for(let i = 0; i < this.data.length; i++){
  2480. if(this.data[i].name === name){
  2481. verticalOffset = i * 25;
  2482. break;
  2483. }
  2484. }
  2485. this.context.beginPath();
  2486. this.context.fillStyle = this.colors[colorIndex];
  2487. this.context.fillRect(this.right + 50, this.top + 50 + verticalOffset, 10, 10);
  2488. this.context.stroke();
  2489. this.context.font = "15px Arial";
  2490. this.context.fillText(name, this.right + 65, this.top + 60 + verticalOffset);
  2491. this.context.fillStyle = "black";
  2492. }
  2493. }
  2494. class HorizontalBarGraph{
  2495. constructor(canvas){
  2496. this.canvas = canvas;
  2497. this.context = canvas.getContext("2d");
  2498. this.left = 0;
  2499. this.right = canvas.clientWidth;
  2500. this.top = canvas.clientHeight - (canvas.clientHeight * 0.99);
  2501. this.bottom = canvas.clientHeight;
  2502. this.data = [];
  2503. this.max = 0;
  2504. }
  2505. //Adds an array of data points to the chart
  2506. //All data is removed and redrawn when called
  2507. //Must pass in all data points
  2508. //Inputs:
  2509. // dataArray: array of objects
  2510. // num: number for the actual data
  2511. // label: text to display on bar
  2512. addData(dataArray){
  2513. this.context.clearRect(0, 0, this.canvas.width, this.canvas.height);
  2514. for(let point of dataArray){
  2515. if(point.num > this.max){
  2516. this.max = point.num;
  2517. }
  2518. this.data.push(point);
  2519. }
  2520. this.drawGraph();
  2521. }
  2522. drawGraph(){
  2523. let barHeight = ((this.bottom - this.top) / this.data.length) - 2;
  2524. for(let i = 0; i < this.data.length; i++){
  2525. let topLocation = this.top + (i * barHeight) + 5;
  2526. let width = (this.right - this.left) * (this.data[i].num / this.max);
  2527. if(this.data[i].num >= this.max){
  2528. this.context.fillStyle = "rgb(255, 99, 107)";
  2529. }else{
  2530. this.context.fillStyle = "rgb(179, 191, 209)";
  2531. }
  2532. this.context.beginPath();
  2533. this.context.fillRect(this.left, topLocation, width, barHeight - 5);
  2534. this.context.stroke();
  2535. let textLocation = 15;
  2536. this.context.font = "12px Saira";
  2537. this.context.fillStyle = "black";
  2538. this.context.fillText(this.data[i].label, textLocation, (this.top) + (i * barHeight) + (barHeight / 1.5));
  2539. }
  2540. }
  2541. }
  2542. module.exports = {
  2543. LineGraph: LineGraph,
  2544. HorizontalBarGraph: HorizontalBarGraph
  2545. }
  2546. },{}]},{},[2]);