Merchant.js 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655
  1. const Ingredient = require("./Ingredient.js");
  2. const Recipe = require("./Recipe.js");
  3. const Transaction = require("./Transaction.js");
  4. const Order = require("./Order.js");
  5. const homeStrand = require("../strands/home.js");
  6. const ingredientsStrand = require("../strands/ingredients.js");
  7. const recipeBookStrand = require("../strands/recipeBook");
  8. const analytics = require("../strands/analytics.js");
  9. const orders = require("../strands/orders");
  10. class MerchantIngredient{
  11. constructor(ingredient, quantity){
  12. this._quantity = quantity;
  13. this._ingredient = ingredient;
  14. }
  15. get ingredient(){
  16. return this._ingredient;
  17. }
  18. get quantity(){
  19. switch(this._ingredient.unit){
  20. case "g":return this._quantity;
  21. case "kg": return this._quantity / 1000;
  22. case "oz": return this._quantity / 28.3495;
  23. case "lb": return this._quantity / 453.5924;
  24. case "ml": return this._quantity * 1000;
  25. case "l": return this._quantity;
  26. case "tsp": return this._quantity * 202.8842;
  27. case "tbsp": return this._quantity * 67.6278;
  28. case "ozfl": return this._quantity * 33.8141;
  29. case "cup": return this._quantity * 4.1667;
  30. case "pt": return this._quantity * 2.1134;
  31. case "qt": return this._quantity * 1.0567;
  32. case "gal": return this._quantity / 3.7854;
  33. case "mm": return this._quantity * 1000;
  34. case "cm": return this._quantity * 100;
  35. case "m": return this._quantity;
  36. case "in": return this._quantity * 39.3701;
  37. case "ft": return this._quantity * 3.2808;
  38. default: return this._quantity;
  39. }
  40. }
  41. updateQuantity(quantity){
  42. this._quantity += this.convertToBase(quantity);
  43. }
  44. convertToBase(quantity){
  45. switch(this._ingredient.unit){
  46. case "g": return quantity;
  47. case "kg": return quantity * 1000;
  48. case "oz": return quantity * 28.3495;
  49. case "lb": return quantity * 453.5924;
  50. case "ml": return quantity / 1000;
  51. case "l": return quantity;
  52. case "tsp": return quantity / 202.8842;
  53. case "tbsp": return quantity / 67.6278;
  54. case "ozfl": return quantity / 33.8141;
  55. case "cup": return quantity / 4.1667;
  56. case "pt": return quantity / 2.1134;
  57. case "qt": return quantity / 1.0567;
  58. case "gal": return quantity * 3.7854;
  59. case "mm": return quantity / 1000;
  60. case "cm": return quantity / 100;
  61. case "m": return quantity;
  62. case "in": return quantity / 39.3701;
  63. case "ft": return quantity / 3.2808;
  64. default: return quantity;
  65. }
  66. }
  67. getQuantityDisplay(){
  68. return `${this.quantity.toFixed(2)} ${this._ingredient.unit.toUpperCase()}`;
  69. }
  70. }
  71. class Merchant{
  72. constructor(oldMerchant, transactions){
  73. this._name = oldMerchant.name;
  74. this._email = oldMerchant.email;
  75. this._pos = oldMerchant.pos;
  76. this._ingredients = [];
  77. this._recipes = [];
  78. this._transactions = [];
  79. this._orders = [];
  80. //populate ingredients
  81. for(let i = 0; i < oldMerchant.inventory.length; i++){
  82. const ingredient = new Ingredient(
  83. oldMerchant.inventory[i].ingredient._id,
  84. oldMerchant.inventory[i].ingredient.name,
  85. oldMerchant.inventory[i].ingredient.category,
  86. oldMerchant.inventory[i].ingredient.unitType,
  87. oldMerchant.inventory[i].defaultUnit,
  88. this,
  89. oldMerchant.inventory[i].ingredient.unitSize
  90. );
  91. const merchantIngredient = new MerchantIngredient(
  92. ingredient,
  93. oldMerchant.inventory[i].quantity,
  94. );
  95. this._ingredients.push(merchantIngredient);
  96. }
  97. //populate recipes
  98. for(let i = 0; i < oldMerchant.recipes.length; i++){
  99. let ingredients = [];
  100. for(let j = 0; j < oldMerchant.recipes[i].ingredients.length; j++){
  101. const ingredient = oldMerchant.recipes[i].ingredients[j];
  102. for(let k = 0; k < this._ingredients.length; k++){
  103. if(ingredient.ingredient === this._ingredients[k].ingredient.id){
  104. ingredients.push({
  105. ingredient: this._ingredients[k].ingredient.id,
  106. quantity: ingredient.quantity
  107. });
  108. break;
  109. }
  110. }
  111. }
  112. this._recipes.push(new Recipe(
  113. oldMerchant.recipes[i]._id,
  114. oldMerchant.recipes[i].name,
  115. oldMerchant.recipes[i].price,
  116. ingredients,
  117. this
  118. ));
  119. }
  120. //populate transactions
  121. for(let i = 0; i < transactions.length; i++){
  122. this._transactions.push(new Transaction(
  123. transactions[i]._id,
  124. transactions[i].date,
  125. transactions[i].recipes,
  126. this
  127. ));
  128. }
  129. }
  130. get name(){
  131. return this._name;
  132. }
  133. set name(name){
  134. this._name = name;
  135. }
  136. get email(){
  137. return this._email;
  138. }
  139. set email(email){
  140. this._email = email;
  141. }
  142. get pos(){
  143. return this._pos;
  144. }
  145. get ingredients(){
  146. return this._ingredients;
  147. }
  148. /*
  149. ingredient: [{
  150. ingredient: {
  151. _id: String,
  152. name: String,
  153. category: String,
  154. unitType: String,
  155. specialUnit: String || undefined,
  156. unitSize: Number || undefined
  157. }
  158. quantity: Number
  159. defaultUnit: String
  160. }]
  161. */
  162. addIngredients(ingredients){
  163. for(let i = 0; i < ingredients.length; i++){
  164. let ingredient = ingredients[i].ingredient;
  165. let quantity = ingredients[i].quantity;
  166. let defaultUnit = ingredients[i].defaultUnit;
  167. const createdIngredient = new Ingredient(
  168. ingredient._id,
  169. ingredient.name,
  170. ingredient.category,
  171. ingredient.unitType,
  172. defaultUnit,
  173. this,
  174. ingredient.unitSize
  175. );
  176. const merchantIngredient = new MerchantIngredient(createdIngredient, quantity);
  177. this._ingredients.push(merchantIngredient);
  178. }
  179. ingredientsStrand.populateByProperty();
  180. }
  181. removeIngredient(ingredient){
  182. const index = this._ingredients.indexOf(ingredient);
  183. if(index === undefined){
  184. return false;
  185. }
  186. this._ingredients.splice(index, 1);
  187. homeStrand.drawInventoryCheckCard();
  188. ingredientsStrand.populateByProperty();
  189. }
  190. getIngredient(id){
  191. for(let i = 0; i < this._ingredients.length; i++){
  192. if(this._ingredients[i].ingredient.id === id){
  193. return this._ingredients[i];
  194. }
  195. }
  196. }
  197. get recipes(){
  198. return this._recipes;
  199. }
  200. /*
  201. recipes: [{
  202. _id: String
  203. name: String
  204. price: Number
  205. ingredients: [{
  206. ingredient: String (id)
  207. quantity: Number
  208. }]
  209. }]
  210. */
  211. addRecipes(recipes){
  212. for(let i = 0; i < recipes.length; i++){
  213. this._recipes.push(new Recipe(
  214. recipes[i]._id,
  215. recipes[i].name,
  216. recipes[i].price,
  217. recipes[i].ingredients,
  218. this
  219. ));
  220. }
  221. recipeBookStrand.populateRecipes();
  222. }
  223. removeRecipe(recipe){
  224. const index = this._recipes.indexOf(recipe);
  225. if(index === undefined){
  226. return false;
  227. }
  228. this._recipes.splice(index, 1);
  229. recipeBookStrand.populateRecipes();
  230. }
  231. /*
  232. recipe = {
  233. name: required,
  234. price: required,
  235. ingredients: [{
  236. ingredient: id of ingredient,
  237. quantity: quantity of ingredient
  238. }]
  239. }
  240. */
  241. updateRecipe(recipe){
  242. for(let i = 0; i < this._recipes.length; i++){
  243. if(this._recipes[i].id === recipe._id){
  244. this._recipes[i].name = recipe.name;
  245. this._recipes[i].price = recipe.price;
  246. this._recipes[i].removeIngredients();
  247. for(let j = 0; j < recipe.ingredients.length; j++){
  248. for(let k = 0; k < this._ingredients.length; k++){
  249. if(this._ingredients[k].ingredient.id === recipe.ingredients[j].ingredient){
  250. this._recipes[i].addIngredient(
  251. this._ingredients[k].ingredient,
  252. recipe.ingredients[j].quantity
  253. );
  254. break;
  255. }
  256. }
  257. }
  258. break;
  259. }
  260. }
  261. recipeBookStrand.isPopulated = false;
  262. }
  263. get transactions(){
  264. return this._transactions;
  265. }
  266. getTransactions(from = 0, to = new Date()){
  267. if(merchant._transactions.length <= 0){
  268. return [];
  269. }
  270. if(from === 0){
  271. from = this._transactions[this._transactions.length-1].date;
  272. }
  273. const {start, end} = this.getTransactionIndices(from, to);
  274. return this._transactions.slice(start, end + 1);
  275. }
  276. addTransaction(transaction){
  277. transaction = new Transaction(
  278. transaction._id,
  279. transaction.date,
  280. transaction.recipes,
  281. this
  282. );
  283. this._transactions.push(transaction);
  284. this._transactions.sort((a, b)=>{
  285. if(a.date > b.date){
  286. return -1;
  287. }
  288. return 1;
  289. });
  290. let ingredients = {};
  291. for(let i = 0; i < transaction.recipes.length; i++){
  292. const recipe = transaction.recipes[i];
  293. for(let j = 0; j < recipe.recipe.ingredients.length; j++){
  294. const ingredient = recipe.recipe.ingredients[j];
  295. if(ingredients[ingredient.ingredient.id]){
  296. ingredients[ingredient.ingredient.id] += recipe.quantity * ingredient.quantity;
  297. }else{
  298. ingredients[ingredient.ingredient.id] = recipe.quantity * ingredient.quantity;
  299. }
  300. }
  301. }
  302. const keys = Object.keys(ingredients);
  303. for(let i = 0; i < keys.length; i++){
  304. for(let j = 0; j < this._ingredients.length; j++){
  305. if(keys[i] === this._ingredients[j].ingredient.id){
  306. this._ingredients[j].updateQuantity(-ingredients[keys[i]]);
  307. }
  308. }
  309. }
  310. homeStrand.isPopulated = false;
  311. ingredientsStrand.isPopulated = false;
  312. analytics.newData = true;
  313. }
  314. removeTransaction(transaction){
  315. const index = this._transactions.indexOf(transaction);
  316. if(index === undefined){
  317. return false;
  318. }
  319. this._transactions.splice(index, 1);
  320. let ingredients = {};
  321. for(let i = 0; i < transaction.recipes.length; i++){
  322. const recipe = transaction.recipes[i];
  323. for(let j = 0; j < recipe.recipe.ingredients.length; j++){
  324. const ingredient = recipe.recipe.ingredients[j];
  325. if(ingredients[ingredient.ingredient.id]){
  326. ingredients[ingredient.ingredient.id] += ingredient.quantity * recipe.quantity;
  327. }else{
  328. ingredients[ingredient.ingredient.id] = ingredient.quantity * recipe.quantity;
  329. }
  330. }
  331. }
  332. const keys = Object.keys(ingredients);
  333. for(let i = 0; i < keys.length; i++){
  334. for(let j = 0; j < this._ingredients.length; j++){
  335. if(keys[i] === this._ingredients[j].ingredient.id){
  336. this._ingredients[j].updateQuantity(ingredients[keys[i]]);
  337. break;
  338. }
  339. }
  340. }
  341. homeStrand.isPopulated = false;
  342. ingredientsStrand.isPopulated = false;
  343. analytics.newData = true;
  344. }
  345. get orders(){
  346. return this._orders;
  347. }
  348. clearOrders(){
  349. this._orders = [];
  350. }
  351. addOrder(data, isNew = false){
  352. let order = new Order(
  353. data._id,
  354. data.name,
  355. data.date,
  356. data.taxes,
  357. data.fees,
  358. data.ingredients,
  359. this
  360. );
  361. this._orders.push(order);
  362. if(isNew){
  363. for(let i = 0; i < order.ingredients.length; i++){
  364. for(let j = 0; j < this._ingredients.length; j++){
  365. if(order.ingredients[i].ingredient === this._ingredients[j].ingredient){
  366. this._ingredients[j].updateQuantity(order.ingredients[i].quantity);
  367. break;
  368. }
  369. }
  370. }
  371. }
  372. ingredientsStrand.isPopulated = false;
  373. orders.isPopulated = false;
  374. }
  375. removeOrder(order){
  376. const index = this._orders.indexOf(order);
  377. if(index === undefined){
  378. return false;
  379. }
  380. this._orders.splice(index, 1);
  381. for(let i = 0; i < order.ingredients.length; i++){
  382. for(let j = 0; j < this._ingredients.length; j++){
  383. if(order.ingredients[i].ingredient === this._ingredients[j].ingredient){
  384. this._ingredients[j].updateQuantity(-order.ingredients[i].quantity);
  385. break;
  386. }
  387. }
  388. }
  389. ingredientsStrand.isPopulated = false;
  390. orders.isPopulated = false;
  391. }
  392. get units(){
  393. return this._units;
  394. }
  395. getRevenue(from, to = new Date()){
  396. const {start, end} = this.getTransactionIndices(from, to);
  397. let total = 0;
  398. for(let i = start; i <= end; i++){
  399. for(let j = 0; j < this._transactions[i].recipes.length; j++){
  400. for(let k = 0; k < this.recipes.length; k++){
  401. if(this._transactions[i].recipes[j].recipe === this.recipes[k]){
  402. total += this._transactions[i].recipes[j].quantity * this.recipes[k].price;
  403. }
  404. }
  405. }
  406. }
  407. return total / 100;
  408. }
  409. /*
  410. Gets the quantity of each ingredient sold between two dates (dateRange)
  411. Inputs:
  412. dateRange: list containing a start date and an end date
  413. Return:
  414. [{
  415. ingredient: Ingredient object,
  416. quantity: quantity of ingredient sold in default unit
  417. }]
  418. */
  419. getIngredientsSold(from, to = new Date()){
  420. let recipes = this.getRecipesSold(from, to);
  421. let ingredientList = [];
  422. for(let i = 0; i < recipes.length; i++){
  423. for(let j = 0; j < recipes[i].recipe.ingredients.length; j++){
  424. let exists = false;
  425. for(let k = 0; k < ingredientList.length; k++){
  426. if(ingredientList[k].ingredient === recipes[i].recipe.ingredients[j].ingredient){
  427. exists = true;
  428. ingredientList[k].quantity += recipes[i].quantity * recipes[i].recipe.ingredients[j].quantity;
  429. break;
  430. }
  431. }
  432. if(!exists){
  433. ingredientList.push({
  434. ingredient: recipes[i].recipe.ingredients[j].ingredient,
  435. quantity: recipes[i].quantity * recipes[i].recipe.ingredients[j].quantity
  436. });
  437. }
  438. }
  439. }
  440. return ingredientList;
  441. }
  442. /*
  443. Gets the quantity of a single ingredient sold between two dates
  444. Inputs:
  445. ingredient = MerchantIngredient object to find
  446. from = start Date
  447. to = end Date
  448. return: quantity sold in default unit
  449. */
  450. getSingleIngredientSold(ingredient, from, to = new Date()){
  451. const {start, end} = this.getTransactionIndices(from, to);
  452. let total = 0;
  453. for(let i = start; i < end; i++){
  454. for(let j = 0; j < this._transactions[i].recipes.length; j++){
  455. for(let k = 0; k < this._transactions[i].recipes[j].recipe.ingredients.length; k++){
  456. if(this._transactions[i].recipes[j].recipe.ingredients[k].ingredient === ingredient.ingredient){
  457. total += this._transactions[i].recipes[j].recipe.ingredients[k].quantity;
  458. break;
  459. }
  460. }
  461. }
  462. }
  463. return total;
  464. }
  465. /*
  466. Gets the number of recipes sold between two dates (dateRange)
  467. Inputs:
  468. dateRange: array containing a start date and an end date
  469. Return:
  470. [{
  471. recipe: a recipe object
  472. quantity: quantity of the recipe sold
  473. }]
  474. */
  475. getRecipesSold(from = 0, to = new Date()){
  476. if(from === 0){
  477. from = this._transactions[0].date;
  478. }
  479. const {start, end} = this.getTransactionIndices(from, to);
  480. let recipeList = [];
  481. for(let i = start; i <= end; i++){
  482. for(let j = 0; j < this._transactions[i].recipes.length; j++){
  483. let exists = false;
  484. for(let k = 0; k < recipeList.length; k++){
  485. if(recipeList[k].recipe === this._transactions[i].recipes[j].recipe){
  486. exists = true;
  487. recipeList[k].quantity += this._transactions[i].recipes[j].quantity;
  488. break;
  489. }
  490. }
  491. if(!exists){
  492. recipeList.push({
  493. recipe: this._transactions[i].recipes[j].recipe,
  494. quantity: this._transactions[i].recipes[j].quantity
  495. });
  496. }
  497. }
  498. }
  499. return recipeList;
  500. }
  501. /*
  502. Groups all of the merchant's ingredients by their category
  503. Return: [{
  504. name: category name,
  505. ingredients: [MerchantIngredient Object]
  506. }]
  507. */
  508. categorizeIngredients(){
  509. let ingredientsByCategory = [];
  510. for(let i = 0; i < this.ingredients.length; i++){
  511. let categoryExists = false;
  512. for(let j = 0; j < ingredientsByCategory.length; j++){
  513. if(this.ingredients[i].ingredient.category === ingredientsByCategory[j].name){
  514. ingredientsByCategory[j].ingredients.push(this.ingredients[i]);
  515. categoryExists = true;
  516. break;
  517. }
  518. }
  519. if(!categoryExists){
  520. ingredientsByCategory.push({
  521. name: this.ingredients[i].ingredient.category,
  522. ingredients: [this.ingredients[i]]
  523. });
  524. }
  525. }
  526. return ingredientsByCategory;
  527. }
  528. getRecipesForIngredient(ingredient){
  529. let recipes = [];
  530. for(let i = 0; i < this._recipes.length; i++){
  531. for(let j = 0; j < this._recipes[i].ingredients.length; j++){
  532. if(this._recipes[i].ingredients[j].ingredient === ingredient){
  533. recipes.push(this._recipes[i]);
  534. break;
  535. }
  536. }
  537. }
  538. return recipes;
  539. }
  540. getTransactionIndices(from, to){
  541. let start, end;
  542. for(let i = this._transactions.length - 1; i >= 0; i--){
  543. if(this._transactions[i].date >= from){
  544. end = i;
  545. break;
  546. }
  547. }
  548. for(let i = 0; i < this._transactions.length; i++){
  549. if(this._transactions[i].date < to){
  550. start = i;
  551. break;
  552. }
  553. }
  554. if(end === undefined){
  555. return false;
  556. }
  557. return {start: start, end: end};
  558. }
  559. }
  560. module.exports = Merchant;