Merchant.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573
  1. class Ingredient{
  2. constructor(id, name, category, unitType, unit, parent){
  3. this.id = id;
  4. this.name = name;
  5. this.category = category;
  6. this.unitType = unitType;
  7. this.unit = unit;
  8. this.parent = parent;
  9. }
  10. convert(unit){
  11. let converted = 0;
  12. let parentIngredient;
  13. for(let i = 0; i < this.parent.ingredients.length; i++){
  14. if(this === this.parent.ingredients[i].ingredient){
  15. parentIngredient = this.parent.ingredients[i];
  16. break;
  17. }
  18. }
  19. if(this.unitType === "mass"){
  20. switch(this.unit){
  21. case "g": converted = parentIngredient.quantity; break;
  22. case "kg": converted = parentIngredient.quantity * 1000; break;
  23. case "oz": converted = parentIngredient.quantity * 28.3495; break;
  24. case "lb": converted = parentIngredient.quantity * 453.5924; break;
  25. }
  26. switch(unit){
  27. case "g": break;
  28. case "kg": converted = converted / 1000; break;
  29. case "oz": converted = converted / 28.3495; break;
  30. case "lb": converted = converted / 453.5924; break;
  31. }
  32. }else if(this.unitType === "volume"){
  33. switch(this.unit){
  34. case "ml": converted = parentIngredient.quantity / 1000; break;
  35. case "l": converted = parentIngredient.quantity; break;
  36. case "tsp": converted = parentIngredient.quantity / 202.8842; break;
  37. case "tbsp": converted = parentIngredient.quantity / 67.6278; break;
  38. case "ozfl": converted = parentIngredient.quantity / 33.8141; break;
  39. case "cup": converted = parentIngredient.quantity / 4.1667; break;
  40. case "pt": converted = parentIngredient.quantity / 2.1134; break;
  41. case "qt": converted = parentIngredient.quantity / 1.0567; break;
  42. case "gal": converted = parentIngredient.quantity * 3.7854; break;
  43. }
  44. switch(unit){
  45. case "ml": converted *= 1000; break;
  46. case "l": break;
  47. case "tsp": converted *= 202.8842; break;
  48. case "tbsp": converted *= 67.6278; break;
  49. case "ozfl": converted *= 33.8141; break;
  50. case "cup": converted *= 4.1667; break;
  51. case "pt": converted *= 2.1134; break;
  52. case "qt": converted *= 1.0567; break;
  53. case "gal": converted /= 3.7854; break;
  54. }
  55. }else if(this.unitType === "length"){
  56. switch(this.unit){
  57. case "mm": converted = parentIngredient.quantity / 1000; break;
  58. case "cm": converted = parentIngredient.quantity / 100; break;
  59. case "m": converted = parentIngredient.quantity; break;
  60. case "in": converted = parentIngredient.quantity / 39.3701; break;
  61. case "ft": converted = parentIngredient.quantity / 3.2808; break;
  62. }
  63. switch(unit){
  64. case "mm": converted *= 1000; break;
  65. case "cm": converted *= 100; break;
  66. case "m": break;
  67. case "in": converted *= 39.3701; break;
  68. case "ft": converted *= 3.2808; break;
  69. }
  70. }
  71. parentIngredient.quantity = converted;
  72. this.unit = unit;
  73. homeStrandObj.drawInventoryCheckCard();
  74. ingredientsStrandObj.populateByProperty("category");
  75. addIngredientsComp.isPopulated = false;
  76. document.getElementById("ingredientStock").innerText = `${converted.toFixed(2)} ${this.unit.toUpperCase()}`;
  77. }
  78. }
  79. class Recipe{
  80. constructor(id, name, price, ingredients, parent){
  81. this.id = id;
  82. this.name = name;
  83. this.price = price;
  84. this.parent = parent;
  85. this.ingredients = [];
  86. for(let i = 0; i < ingredients.length; i++){
  87. for(let j = 0; j < parent.ingredients.length; j++){
  88. if(ingredients[i].ingredient === parent.ingredients[j].ingredient.id){
  89. this.ingredients.push({
  90. ingredient: parent.ingredients[j].ingredient,
  91. quantity: ingredients[i].quantity
  92. });
  93. break;
  94. }
  95. }
  96. }
  97. }
  98. }
  99. class Transaction{
  100. constructor(id, date, recipes, parent){
  101. this.id = id;
  102. this.parent = parent;
  103. this.date = new Date(date);
  104. this.recipes = [];
  105. for(let i = 0; i < recipes.length; i++){
  106. for(let j = 0; j < parent.recipes.length; j++){
  107. if(recipes[i].recipe === parent.recipes[j].id){
  108. this.recipes.push({
  109. recipe: parent.recipes[j],
  110. quantity: recipes[i].quantity
  111. });
  112. break;
  113. }
  114. }
  115. }
  116. }
  117. }
  118. class Order{
  119. constructor(id, name, date, ingredients, parent){
  120. this.id = id;
  121. this.name = name;
  122. this.date = new Date(date);
  123. this.ingredients = [];
  124. this.parent = parent;
  125. for(let i = 0; i < ingredients.length; i++){
  126. for(let j = 0; j < parent.ingredients.length; j++){
  127. if(ingredients[i].ingredient === parent.ingredients[j].ingredient.id){
  128. this.ingredients.push({
  129. ingredient: parent.ingredients[j].ingredient,
  130. quantity: ingredients[i].quantity,
  131. price: ingredients[i].price
  132. });
  133. }
  134. }
  135. }
  136. }
  137. }
  138. class Merchant{
  139. constructor(oldMerchant, transactions){
  140. this.name = oldMerchant.name;
  141. this.pos = oldMerchant.pos;
  142. this.ingredients = [];
  143. this.recipes = [];
  144. this.transactions = [];
  145. this.orders = [];
  146. this.units = {
  147. mass: ["g", "kg", "oz", "lb"],
  148. volume: ["ml", "l", "tsp", "tbsp", "ozfl", "cup", "pt", "qt", "gal"],
  149. length: ["mm", "cm", "m", "in", "foot"]
  150. }
  151. for(let i = 0; i < oldMerchant.inventory.length; i++){
  152. this.ingredients.push({
  153. ingredient: new Ingredient(
  154. oldMerchant.inventory[i].ingredient._id,
  155. oldMerchant.inventory[i].ingredient.name,
  156. oldMerchant.inventory[i].ingredient.category,
  157. oldMerchant.inventory[i].ingredient.unitType,
  158. oldMerchant.inventory[i].defaultUnit,
  159. this
  160. ),
  161. quantity: oldMerchant.inventory[i].quantity
  162. });
  163. }
  164. for(let i = 0; i < oldMerchant.recipes.length; i++){
  165. this.recipes.push(new Recipe(
  166. oldMerchant.recipes[i]._id,
  167. oldMerchant.recipes[i].name,
  168. oldMerchant.recipes[i].price,
  169. oldMerchant.recipes[i].ingredients,
  170. this
  171. ));
  172. }
  173. for(let i = 0; i < transactions.length; i++){
  174. this.transactions.push(new Transaction(
  175. transactions[i]._id,
  176. transactions[i].date,
  177. transactions[i].recipes,
  178. this
  179. ));
  180. }
  181. }
  182. /*
  183. Updates all specified item in the merchant's inventory and updates the page
  184. If ingredient doesn't exist, add it
  185. ingredients = {
  186. ingredient: Ingredient object,
  187. quantity: new quantity
  188. }
  189. remove = set true if removing
  190. isOrder = set true if this is coming from an order
  191. */
  192. editIngredients(ingredients, remove = false, isOrder = false){
  193. for(let i = 0; i < ingredients.length; i++){
  194. let isNew = true;
  195. for(let j = 0; j < merchant.ingredients.length; j++){
  196. if(merchant.ingredients[j].ingredient === ingredients[i].ingredient){
  197. if(remove){
  198. merchant.ingredients.splice(j, 1);
  199. }else if(!remove && isOrder){
  200. merchant.ingredients[j].quantity += ingredients[i].quantity;
  201. }else{
  202. merchant.ingredients[j].quantity = ingredients[i].quantity;
  203. }
  204. isNew = false;
  205. break;
  206. }
  207. }
  208. if(isNew){
  209. merchant.ingredients.push({
  210. ingredient: ingredients[i].ingredient,
  211. quantity: parseFloat(ingredients[i].quantity)
  212. });
  213. }
  214. }
  215. homeStrandObj.drawInventoryCheckCard();
  216. ingredientsStrandObj.populateByProperty("category");
  217. addIngredientsComp.isPopulated = false;
  218. closeSidebar();
  219. }
  220. /*
  221. Updates a recipe in the merchants list of recipes
  222. Can create, edit or remove
  223. recipe = [Recipe object]
  224. remove = will remove recipe when true
  225. */
  226. editRecipes(recipes, remove = false){
  227. let isNew = true;
  228. for(let i = 0; i < recipes.length; i++){
  229. for(let j = 0; j < this.recipes.length; j++){
  230. if(recipes[i] === this.recipes[j]){
  231. if(remove){
  232. this.recipes.splice(j, 1);
  233. }else{
  234. this.recipes[j] = recipes[i];
  235. }
  236. isNew = false;
  237. break;
  238. }
  239. }
  240. if(isNew){
  241. merchant.recipes.push(recipes[i]);
  242. }
  243. }
  244. recipeBookStrandObj.populateRecipes();
  245. closeSidebar();
  246. }
  247. /*
  248. Updates a list of orders in the merchants list of orders
  249. Create/edit/remove
  250. orders = [Order object]
  251. remove = will remove order when true
  252. */
  253. editOrders(orders, remove = false){
  254. for(let i = 0; i < orders.length; i++){
  255. let isNew = true;
  256. for(let j = 0; j < this.orders.length; j++){
  257. if(orders[i] === this.orders[j]){
  258. if(remove){
  259. this.orders.splice(j, 1);
  260. }else{
  261. this.orders[j] = orders[i];
  262. }
  263. isNew = false;
  264. break;
  265. }
  266. }
  267. if(isNew){
  268. this.orders.push(orders[i]);
  269. }
  270. }
  271. ordersStrandObj.populate();
  272. closeSidebar();
  273. }
  274. editTransactions(transaction, remove = false){
  275. let isNew = true;
  276. for(let i = 0; i < this.transactions.length; i++){
  277. if(this.transactions[i] === transaction){
  278. if(remove){
  279. this.transactions.splice(i, 1);
  280. }
  281. isNew = false;
  282. break;
  283. }
  284. }
  285. if(isNew){
  286. this.transactions.push(transaction);
  287. this.transactions.sort((a, b) => a.date > b.date ? 1 : -1);
  288. }
  289. transactionsStrandObj.isPopulated = false;
  290. transactionsStrandObj.display();
  291. closeSidebar();
  292. }
  293. /*
  294. Gets the indices of two dates from transactions
  295. Inputs
  296. from: starting date
  297. to: ending date (default to now)
  298. Output
  299. Array containing starting index and ending index
  300. Note: Will return false if it cannot find both necessary dates
  301. */
  302. transactionIndices(from, to = new Date()){
  303. let indices = [];
  304. for(let i = 0; i < this.transactions.length; i++){
  305. if(this.transactions[i].date > from){
  306. indices.push(i);
  307. break;
  308. }
  309. }
  310. for(let i = this.transactions.length - 1; i >=0; i--){
  311. if(this.transactions[i].date < to){
  312. indices.push(i);
  313. break;
  314. }
  315. }
  316. if(indices.length < 2){
  317. return false;
  318. }
  319. return indices;
  320. }
  321. revenue(indices){
  322. let total = 0;
  323. for(let i = indices[0]; i <= indices[1]; i++){
  324. for(let j = 0; j < this.transactions[i].recipes.length; j++){
  325. for(let k = 0; k < this.recipes.length; k++){
  326. if(this.transactions[i].recipes[j].recipe === this.recipes[k]){
  327. total += this.transactions[i].recipes[j].quantity * this.recipes[k].price;
  328. }
  329. }
  330. }
  331. }
  332. return total / 100;
  333. }
  334. /*
  335. Gets the quantity of each ingredient sold between two dates (dateRange)
  336. Inputs
  337. dateRange: list containing a start date and an end date
  338. Return:
  339. [{
  340. ingredient: Ingredient object,
  341. quantity: quantity of ingredient sold
  342. }]
  343. */
  344. ingredientsSold(dateRange){
  345. if(!dateRange){
  346. return false;
  347. }
  348. let recipes = this.recipesSold(dateRange);
  349. let ingredientList = [];
  350. for(let i = 0; i < recipes.length; i++){
  351. for(let j = 0; j < recipes[i].recipe.ingredients.length; j++){
  352. let exists = false;
  353. for(let k = 0; k < ingredientList.length; k++){
  354. if(ingredientList[k].ingredient === recipes[i].recipe.ingredients[j].ingredient){
  355. exists = true;
  356. ingredientList[k].quantity += recipes[i].quantity * recipes[i].recipe.ingredients[j].quantity;
  357. break;
  358. }
  359. }
  360. if(!exists){
  361. ingredientList.push({
  362. ingredient: recipes[i].recipe.ingredients[j].ingredient,
  363. quantity: recipes[i].quantity * recipes[i].recipe.ingredients[j].quantity
  364. });
  365. }
  366. }
  367. }
  368. return ingredientList;
  369. }
  370. singleIngredientSold(dateRange, ingredient){
  371. let total = 0;
  372. for(let i = dateRange[0]; i < dateRange[1]; i++){
  373. for(let j = 0; j < this.transactions[i].recipes.length; j++){
  374. for(let k = 0; k < this.transactions[i].recipes[j].recipe.ingredients.length; k++){
  375. if(this.transactions[i].recipes[j].recipe.ingredients[k].ingredient === ingredient.ingredient){
  376. total += this.transactions[i].recipes[j].recipe.ingredients[k].quantity;
  377. break;
  378. }
  379. }
  380. }
  381. }
  382. return total;
  383. }
  384. /*
  385. Gets the number of recipes sold between two dates (dateRange)
  386. Inputs:
  387. dateRange: array containing a start date and an end date
  388. Return:
  389. [{
  390. recipe: a recipe object
  391. quantity: quantity of the recipe sold
  392. }]
  393. */
  394. recipesSold(dateRange){
  395. let recipeList = [];
  396. for(let i = dateRange[0]; i <= dateRange[1]; i++){
  397. for(let j = 0; j < this.transactions[i].recipes.length; j++){
  398. let exists = false;
  399. for(let k = 0; k < recipeList.length; k++){
  400. if(recipeList[k].recipe === this.transactions[i].recipes[j].recipe){
  401. exists = true;
  402. recipeList[k].quantity += this.transactions[i].recipes[j].quantity;
  403. break;
  404. }
  405. }
  406. if(!exists){
  407. recipeList.push({
  408. recipe: this.transactions[i].recipes[j].recipe,
  409. quantity: this.transactions[i].recipes[j].quantity
  410. });
  411. }
  412. }
  413. }
  414. return recipeList;
  415. }
  416. /*
  417. Create revenue data for graphing
  418. Input:
  419. dateRange: [start index, end index] (this.transactionIndices)
  420. Return:
  421. [total revenue for each day]
  422. */
  423. graphDailyRevenue(dateRange){
  424. if(!dateRange){
  425. return false;
  426. }
  427. let dataList = new Array(30).fill(0);
  428. let currentDate = this.transactions[dateRange[0]].date;
  429. let arrayIndex = 0;
  430. for(let i = dateRange[0]; i <= dateRange[1]; i++){
  431. if(this.transactions[i].date.getDate() !== currentDate.getDate()){
  432. currentDate = this.transactions[i].date;
  433. arrayIndex++;
  434. }
  435. for(let j = 0; j < this.transactions[i].recipes.length; j++){
  436. dataList[arrayIndex] += (this.transactions[i].recipes[j].recipe.price / 100) * this.transactions[i].recipes[j].quantity;
  437. }
  438. }
  439. return dataList;
  440. }
  441. /*
  442. Groups all of the merchant's ingredients by their category
  443. Return: [{
  444. name: category name,
  445. ingredients: [Ingredient Object]
  446. }]
  447. */
  448. categorizeIngredients(){
  449. let ingredientsByCategory = [];
  450. for(let i = 0; i < this.ingredients.length; i++){
  451. let categoryExists = false;
  452. for(let j = 0; j < ingredientsByCategory.length; j++){
  453. if(this.ingredients[i].ingredient.category === ingredientsByCategory[j].name){
  454. ingredientsByCategory[j].ingredients.push(this.ingredients[i]);
  455. categoryExists = true;
  456. break;
  457. }
  458. }
  459. if(!categoryExists){
  460. ingredientsByCategory.push({
  461. name: this.ingredients[i].ingredient.category,
  462. ingredients: [this.ingredients[i]]
  463. });
  464. }
  465. }
  466. return ingredientsByCategory;
  467. }
  468. unitizeIngredients(){
  469. let ingredientsByUnit = [];
  470. for(let i = 0; i < this.ingredients.length; i++){
  471. let unitExists = false;
  472. for(let j = 0; j < ingredientsByUnit.length; j++){
  473. if(this.ingredients[i].ingredient.unit === ingredientsByUnit[j].name){
  474. ingredientsByUnit[j].ingredients.push(this.ingredients[i]);
  475. unitExists = true;
  476. break;
  477. }
  478. }
  479. if(!unitExists){
  480. ingredientsByUnit.push({
  481. name: this.ingredients[i].ingredient.unit,
  482. ingredients: [this.ingredients[i]]
  483. });
  484. }
  485. }
  486. return ingredientsByUnit;
  487. }
  488. getRecipesForIngredient(ingredient){
  489. let recipes = [];
  490. for(let i = 0; i < this.recipes.length; i++){
  491. for(let j = 0; j < this.recipes[i].ingredients.length; j++){
  492. if(this.recipes[i].ingredients[j].ingredient === ingredient){
  493. recipes.push(this.recipes[i]);
  494. }
  495. }
  496. }
  497. return recipes;
  498. }
  499. }