Merchant.js 17 KB

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