Merchant.js 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631
  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 home = require("../strands/home.js");
  6. const ingredients = require("../strands/ingredients.js");
  7. const recipeBook = 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. _id: String,
  151. name: String,
  152. category: String,
  153. unitType: String,
  154. specialUnit: String || undefined,
  155. unitSize: Number || undefined
  156. }
  157. quantity: Number
  158. defaultUnit: String
  159. */
  160. addIngredient(ingredient, quantity, defaultUnit){
  161. const createdIngredient = new Ingredient(
  162. ingredient._id,
  163. ingredient.name,
  164. ingredient.category,
  165. ingredient.unitType,
  166. defaultUnit,
  167. this,
  168. ingredient.unitSize
  169. );
  170. const merchantIngredient = new MerchantIngredient(createdIngredient, quantity);
  171. this._ingredients.push(merchantIngredient);
  172. home.isPopulated = false;
  173. ingredients.isPopulated = false;
  174. }
  175. removeIngredient(ingredient){
  176. const index = this._ingredients.indexOf(ingredient);
  177. if(index === undefined){
  178. return false;
  179. }
  180. this._ingredients.splice(index, 1);
  181. home.isPopulated = false;
  182. ingredients.isPopulated = false;
  183. }
  184. getIngredient(id){
  185. for(let i = 0; i < this._ingredients.length; i++){
  186. if(this._ingredients[i].ingredient.id === id){
  187. return this._ingredients[i];
  188. }
  189. }
  190. }
  191. get recipes(){
  192. return this._recipes;
  193. }
  194. addRecipe(id, name, price, ingredients){
  195. let recipe = new Recipe(id, name, price, ingredients, this);
  196. this._recipes.push(recipe);
  197. recipeBook.isPopulated = false;
  198. }
  199. removeRecipe(recipe){
  200. const index = this._recipes.indexOf(recipe);
  201. if(index === undefined){
  202. return false;
  203. }
  204. this._recipes.splice(index, 1);
  205. recipeBook.isPopulated = false;
  206. }
  207. /*
  208. recipe = {
  209. name: required,
  210. price: required,
  211. ingredients: [{
  212. ingredient: id of ingredient,
  213. quantity: quantity of ingredient
  214. }]
  215. }
  216. */
  217. updateRecipe(recipe){
  218. for(let i = 0; i < this._recipes.length; i++){
  219. if(this._recipes[i].id === recipe._id){
  220. this._recipes[i].name = recipe.name;
  221. this._recipes[i].price = recipe.price;
  222. this._recipes[i].removeIngredients();
  223. for(let j = 0; j < recipe.ingredients.length; j++){
  224. for(let k = 0; k < this._ingredients.length; k++){
  225. if(this._ingredients[k].ingredient.id === recipe.ingredients[j].ingredient){
  226. this._recipes[i].addIngredient(
  227. this._ingredients[k].ingredient,
  228. recipe.ingredients[j].quantity
  229. );
  230. break;
  231. }
  232. }
  233. }
  234. break;
  235. }
  236. }
  237. recipeBook.isPopulated = false;
  238. }
  239. get transactions(){
  240. return this._transactions;
  241. }
  242. getTransactions(from = 0, to = new Date()){
  243. if(merchant._transactions.length <= 0){
  244. return [];
  245. }
  246. if(from === 0){
  247. from = this._transactions[this._transactions.length-1].date;
  248. }
  249. const {start, end} = this.getTransactionIndices(from, to);
  250. return this._transactions.slice(start, end + 1);
  251. }
  252. addTransaction(transaction){
  253. transaction = new Transaction(
  254. transaction._id,
  255. transaction.date,
  256. transaction.recipes,
  257. this
  258. );
  259. this._transactions.push(transaction);
  260. this._transactions.sort((a, b)=>{
  261. if(a.date > b.date){
  262. return -1;
  263. }
  264. return 1;
  265. });
  266. let ingredients = {};
  267. for(let i = 0; i < transaction.recipes.length; i++){
  268. const recipe = transaction.recipes[i];
  269. for(let j = 0; j < recipe.recipe.ingredients.length; j++){
  270. const ingredient = recipe.recipe.ingredients[j];
  271. if(ingredients[ingredient.ingredient.id]){
  272. ingredients[ingredient.ingredient.id] += recipe.quantity * ingredient.quantity;
  273. }else{
  274. ingredients[ingredient.ingredient.id] = recipe.quantity * ingredient.quantity;
  275. }
  276. }
  277. }
  278. const keys = Object.keys(ingredients);
  279. for(let i = 0; i < keys.length; i++){
  280. for(let j = 0; j < this._ingredients.length; j++){
  281. if(keys[i] === this._ingredients[j].ingredient.id){
  282. this._ingredients[j].updateQuantity(-ingredients[keys[i]]);
  283. }
  284. }
  285. }
  286. home.isPopulated = false;
  287. ingredients.isPopulated = false;
  288. analytics.newData = true;
  289. }
  290. removeTransaction(transaction){
  291. const index = this._transactions.indexOf(transaction);
  292. if(index === undefined){
  293. return false;
  294. }
  295. this._transactions.splice(index, 1);
  296. let ingredients = {};
  297. for(let i = 0; i < transaction.recipes.length; i++){
  298. const recipe = transaction.recipes[i];
  299. for(let j = 0; j < recipe.recipe.ingredients.length; j++){
  300. const ingredient = recipe.recipe.ingredients[j];
  301. if(ingredients[ingredient.ingredient.id]){
  302. ingredients[ingredient.ingredient.id] += ingredient.quantity * recipe.quantity;
  303. }else{
  304. ingredients[ingredient.ingredient.id] = ingredient.quantity * recipe.quantity;
  305. }
  306. }
  307. }
  308. const keys = Object.keys(ingredients);
  309. for(let i = 0; i < keys.length; i++){
  310. for(let j = 0; j < this._ingredients.length; j++){
  311. if(keys[i] === this._ingredients[j].ingredient.id){
  312. this._ingredients[j].updateQuantity(ingredients[keys[i]]);
  313. break;
  314. }
  315. }
  316. }
  317. home.isPopulated = false;
  318. ingredients.isPopulated = false;
  319. analytics.newData = true;
  320. }
  321. get orders(){
  322. return this._orders;
  323. }
  324. clearOrders(){
  325. this._orders = [];
  326. }
  327. addOrder(data, isNew = false){
  328. let order = new Order(
  329. data._id,
  330. data.name,
  331. data.date,
  332. data.taxes,
  333. data.fees,
  334. data.ingredients,
  335. this
  336. );
  337. this._orders.push(order);
  338. if(isNew){
  339. for(let i = 0; i < order.ingredients.length; i++){
  340. for(let j = 0; j < this._ingredients.length; j++){
  341. if(order.ingredients[i].ingredient === this._ingredients[j].ingredient){
  342. this._ingredients[j].updateQuantity(order.ingredients[i].quantity);
  343. break;
  344. }
  345. }
  346. }
  347. }
  348. ingredients.isPopulated = false;
  349. orders.isPopulated = false;
  350. }
  351. removeOrder(order){
  352. const index = this._orders.indexOf(order);
  353. if(index === undefined){
  354. return false;
  355. }
  356. this._orders.splice(index, 1);
  357. for(let i = 0; i < order.ingredients.length; i++){
  358. for(let j = 0; j < this._ingredients.length; j++){
  359. if(order.ingredients[i].ingredient === this._ingredients[j].ingredient){
  360. this._ingredients[j].updateQuantity(-order.ingredients[i].quantity);
  361. break;
  362. }
  363. }
  364. }
  365. ingredients.isPopulated = false;
  366. orders.isPopulated = false;
  367. }
  368. get units(){
  369. return this._units;
  370. }
  371. getRevenue(from, to = new Date()){
  372. const {start, end} = this.getTransactionIndices(from, to);
  373. let total = 0;
  374. for(let i = start; i <= end; i++){
  375. for(let j = 0; j < this._transactions[i].recipes.length; j++){
  376. for(let k = 0; k < this.recipes.length; k++){
  377. if(this._transactions[i].recipes[j].recipe === this.recipes[k]){
  378. total += this._transactions[i].recipes[j].quantity * this.recipes[k].price;
  379. }
  380. }
  381. }
  382. }
  383. return total / 100;
  384. }
  385. /*
  386. Gets the quantity of each ingredient sold between two dates (dateRange)
  387. Inputs:
  388. dateRange: list containing a start date and an end date
  389. Return:
  390. [{
  391. ingredient: Ingredient object,
  392. quantity: quantity of ingredient sold in default unit
  393. }]
  394. */
  395. getIngredientsSold(from, to = new Date()){
  396. let recipes = this.getRecipesSold(from, to);
  397. let ingredientList = [];
  398. for(let i = 0; i < recipes.length; i++){
  399. for(let j = 0; j < recipes[i].recipe.ingredients.length; j++){
  400. let exists = false;
  401. for(let k = 0; k < ingredientList.length; k++){
  402. if(ingredientList[k].ingredient === recipes[i].recipe.ingredients[j].ingredient){
  403. exists = true;
  404. ingredientList[k].quantity += recipes[i].quantity * recipes[i].recipe.ingredients[j].quantity;
  405. break;
  406. }
  407. }
  408. if(!exists){
  409. ingredientList.push({
  410. ingredient: recipes[i].recipe.ingredients[j].ingredient,
  411. quantity: recipes[i].quantity * recipes[i].recipe.ingredients[j].quantity
  412. });
  413. }
  414. }
  415. }
  416. return ingredientList;
  417. }
  418. /*
  419. Gets the quantity of a single ingredient sold between two dates
  420. Inputs:
  421. ingredient = MerchantIngredient object to find
  422. from = start Date
  423. to = end Date
  424. return: quantity sold in default unit
  425. */
  426. getSingleIngredientSold(ingredient, from, to = new Date()){
  427. const {start, end} = this.getTransactionIndices(from, to);
  428. let total = 0;
  429. for(let i = start; i < end; i++){
  430. for(let j = 0; j < this._transactions[i].recipes.length; j++){
  431. for(let k = 0; k < this._transactions[i].recipes[j].recipe.ingredients.length; k++){
  432. if(this._transactions[i].recipes[j].recipe.ingredients[k].ingredient === ingredient.ingredient){
  433. total += this._transactions[i].recipes[j].recipe.ingredients[k].quantity;
  434. break;
  435. }
  436. }
  437. }
  438. }
  439. return total;
  440. }
  441. /*
  442. Gets the number of recipes sold between two dates (dateRange)
  443. Inputs:
  444. dateRange: array containing a start date and an end date
  445. Return:
  446. [{
  447. recipe: a recipe object
  448. quantity: quantity of the recipe sold
  449. }]
  450. */
  451. getRecipesSold(from = 0, to = new Date()){
  452. if(from === 0){
  453. from = this._transactions[0].date;
  454. }
  455. const {start, end} = this.getTransactionIndices(from, to);
  456. let recipeList = [];
  457. for(let i = start; i <= end; i++){
  458. for(let j = 0; j < this._transactions[i].recipes.length; j++){
  459. let exists = false;
  460. for(let k = 0; k < recipeList.length; k++){
  461. if(recipeList[k].recipe === this._transactions[i].recipes[j].recipe){
  462. exists = true;
  463. recipeList[k].quantity += this._transactions[i].recipes[j].quantity;
  464. break;
  465. }
  466. }
  467. if(!exists){
  468. recipeList.push({
  469. recipe: this._transactions[i].recipes[j].recipe,
  470. quantity: this._transactions[i].recipes[j].quantity
  471. });
  472. }
  473. }
  474. }
  475. return recipeList;
  476. }
  477. /*
  478. Groups all of the merchant's ingredients by their category
  479. Return: [{
  480. name: category name,
  481. ingredients: [MerchantIngredient Object]
  482. }]
  483. */
  484. categorizeIngredients(){
  485. let ingredientsByCategory = [];
  486. for(let i = 0; i < this.ingredients.length; i++){
  487. let categoryExists = false;
  488. for(let j = 0; j < ingredientsByCategory.length; j++){
  489. if(this.ingredients[i].ingredient.category === ingredientsByCategory[j].name){
  490. ingredientsByCategory[j].ingredients.push(this.ingredients[i]);
  491. categoryExists = true;
  492. break;
  493. }
  494. }
  495. if(!categoryExists){
  496. ingredientsByCategory.push({
  497. name: this.ingredients[i].ingredient.category,
  498. ingredients: [this.ingredients[i]]
  499. });
  500. }
  501. }
  502. return ingredientsByCategory;
  503. }
  504. getRecipesForIngredient(ingredient){
  505. let recipes = [];
  506. for(let i = 0; i < this._recipes.length; i++){
  507. for(let j = 0; j < this._recipes[i].ingredients.length; j++){
  508. if(this._recipes[i].ingredients[j].ingredient === ingredient){
  509. recipes.push(this._recipes[i]);
  510. break;
  511. }
  512. }
  513. }
  514. return recipes;
  515. }
  516. getTransactionIndices(from, to){
  517. let start, end;
  518. for(let i = this._transactions.length - 1; i >= 0; i--){
  519. if(this._transactions[i].date >= from){
  520. end = i;
  521. break;
  522. }
  523. }
  524. for(let i = 0; i < this._transactions.length; i++){
  525. if(this._transactions[i].date < to){
  526. start = i;
  527. break;
  528. }
  529. }
  530. if(end === undefined){
  531. return false;
  532. }
  533. return {start: start, end: end};
  534. }
  535. }
  536. module.exports = Merchant;