Merchant.js 19 KB

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