bundle.js 102 KB

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