Merchant.js 18 KB

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