Merchant.js 17 KB

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