bundle.js 104 KB

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