Merchant.js 17 KB

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