Merchant.js 19 KB

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