Merchant.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485
  1. const transactions = require("./transactions");
  2. class Merchant{
  3. constructor(oldMerchant, transactions, modules){
  4. this._modules = modules;
  5. this._name = oldMerchant.name;
  6. this._pos = oldMerchant.pos;
  7. this._ingredients = [];
  8. this._recipes = [];
  9. this._transactions = [];
  10. this._orders = [];
  11. this._units = {
  12. mass: ["g", "kg", "oz", "lb"],
  13. volume: ["ml", "l", "tsp", "tbsp", "ozfl", "cup", "pt", "qt", "gal"],
  14. length: ["mm", "cm", "m", "in", "ft"],
  15. other: ["each", "bottle"]
  16. }
  17. for(let i = 0; i < oldMerchant.inventory.length; i++){
  18. this.ingredients.push({
  19. ingredient: new modules.Ingredient(
  20. oldMerchant.inventory[i].ingredient._id,
  21. oldMerchant.inventory[i].ingredient.name,
  22. oldMerchant.inventory[i].ingredient.category,
  23. oldMerchant.inventory[i].ingredient.unitType,
  24. oldMerchant.inventory[i].defaultUnit,
  25. this,
  26. oldMerchant.inventory[i].ingredient.specialUnit,
  27. oldMerchant.inventory[i].ingredient.unitSize
  28. ),
  29. quantity: oldMerchant.inventory[i].quantity
  30. });
  31. }
  32. for(let i = 0; i < oldMerchant.recipes.length; i++){
  33. this.recipes.push(new modules.Recipe(
  34. oldMerchant.recipes[i]._id,
  35. oldMerchant.recipes[i].name,
  36. oldMerchant.recipes[i].price,
  37. oldMerchant.recipes[i].ingredients,
  38. this
  39. ));
  40. }
  41. for(let i = 0; i < transactions.length; i++){
  42. this.transactions.push(new modules.Transaction(
  43. transactions[i]._id,
  44. transactions[i].date,
  45. transactions[i].recipes,
  46. this
  47. ));
  48. }
  49. }
  50. get name(){
  51. return this._name;
  52. }
  53. set name(name){
  54. if(sanitaryString(name)){
  55. this._name = name;
  56. }
  57. return false;
  58. }
  59. get pos(){
  60. return this._pos;
  61. }
  62. get ingredients(){
  63. return this._ingredients;
  64. }
  65. addIngredient(ingredient, quantity){
  66. this.ingredients.push({ingredient, quantity});
  67. this._modules.home.isPopulated = false;
  68. this._modules.ingredients.isPopulated = false;
  69. }
  70. removeIngredient(ingredient){
  71. const index = this._ingredients.indexOf(ingredient);
  72. if(index === undefined){
  73. return false;
  74. }
  75. this._ingredients.splice(index, 1);
  76. this._modules.home.isPopulated = false;
  77. this._modules.ingredients.isPopulated = false;
  78. }
  79. updateIngredient(ingredient, quantity){
  80. const index = this._ingredients.indexOf(ingredient);
  81. if(index = undefined){
  82. return false;
  83. }
  84. this._ingredients[index].quantity = quantity;
  85. this._modules.home.isPopulated = false;
  86. this._modules.ingredients.isPopulated = false;
  87. }
  88. get recipes(){
  89. return this._recipes;
  90. }
  91. addRecipe(recipe){
  92. this._recipes.push(recipe);
  93. this._modules.transactions.isPopulated = false;
  94. this._modules.recipeBook.isPopulated = false;
  95. }
  96. removeRecipe(recipe){
  97. const index = this._recipes.indexOf(recipe);
  98. if(index === undefined){
  99. return false;
  100. }
  101. this._recipes.splice(index, 1);
  102. this._modules.transactions.isPopulated = false;
  103. this._modules.recipeBook.isPopulated = false;
  104. }
  105. getTransactions(from = 0, to = new Date()){
  106. if(from > end){
  107. return false;
  108. }
  109. if(from === 0){
  110. from = this.transactions[0].date;
  111. }
  112. const {from, to} = this.getTransactionIndices(from, to);
  113. return this.transactions.slice(from, to);
  114. }
  115. addTransaction(transaction){
  116. this._transactions.push(transaction);
  117. this._transactions.sort((a, b)=>{
  118. if(a.date > b.date){
  119. return -1;
  120. }
  121. return 1;
  122. });
  123. let ingredients = {};
  124. for(let i = 0; i < transaction.recipes.length; i++){
  125. const recipe = transaction.recipes[i];
  126. for(let j = 0; j < recipe.recipe.ingredients.length; j++){
  127. const ingredient = recipe.ingredients[i];
  128. if(ingredients[ingredient.ingredient.id]){
  129. ingredients[ingredient.ingredient.id] += recipe.quantity * ingredient.quantity;
  130. }else{
  131. ingredients[ingredient.ingredient.id] = recipe.quantity * ingredient.quantity;
  132. }
  133. }
  134. }
  135. const keys = Object.keys(ingredients);
  136. for(let i = 0; i < keys.length; i++){
  137. for(let j = 0; j < this._ingredients.length; j++){
  138. if(keys[i] === this._ingredients[j].ingredient.id){
  139. this._ingredients.quantity -= ingredients[keys[i]];
  140. }
  141. }
  142. }
  143. this._modules.home.isPopulated = false;
  144. this._modules.ingredients.isPopulated = false;
  145. this._modules.transactions.isPopulated = false;
  146. this._modules.analytics.newData = true;
  147. }
  148. removeTransaction(transaction){
  149. const index = this._transactions.indexOf(transaction);
  150. if(index === undefined){
  151. return false;
  152. }
  153. this._transactions.splice(index, 1);
  154. let ingredients = {};
  155. for(let i = 0; i < transaction.recipes.length; i++){
  156. const recipe = transaction.recipes[i];
  157. for(let j = 0; j < recipe.recipe.ingredients.length; j++){
  158. const ingredient = recipe.recipe.ingredients[i];
  159. if(ingredients[ingredient.ingredient.id]){
  160. ingredients[ingredient.ingredient.id] += ingredient.quantity * recipe.quantity;
  161. }else{
  162. ingredients[ingredient.ingredient.id] = ingredient.quantity * recipe.quantity;
  163. }
  164. }
  165. }
  166. const keys = Object.keys(ingredients);
  167. for(let i = 0; i < keys.length; i++){
  168. for(let j = 0; j < this._ingredients.length; j++){
  169. if(keys[i] === this._ingredients[j].ingredient.id){
  170. this._ingredients.quantity += ingredients[keys[i]];
  171. }
  172. }
  173. }
  174. this._modules.home.isPopulated = false;
  175. this._modules.ingredients.isPopulated = false;
  176. this._modules.transactions.isPopulated = false;
  177. this._modules.analytics.newData = true;
  178. }
  179. get orders(){
  180. return this._orders;
  181. }
  182. addOrder(order, isNew = false){
  183. this._orders.push(order);
  184. if(isNew){
  185. for(let i = 0; i < order.ingredients.length; i++){
  186. for(let j = 0; j < this._ingredients.length; j++){
  187. if(order.ingredients[i] === this._ingredients[j].ingredient){
  188. this._ingredients[j].quantity += order.ingredients[i].quantity;
  189. break;
  190. }
  191. }
  192. }
  193. }
  194. this._modules.ingredients.isPopulated = false;
  195. this._modules.orders.isPopulated = false;
  196. }
  197. removeOrder(order){
  198. const index = this._orders.indexOf(order);
  199. if(index === undefined){
  200. return false;
  201. }
  202. this._orders.splice(index, 1);
  203. for(let i = 0; i < order.ingredients.length; i++){
  204. for(let j = 0; j < this._ingredients.length; j++){
  205. if(order.ingredients[i].ingredient === this._ingredients[j].ingredient){
  206. this._ingredients[j].quantity -= order.ingredients[i].quantity;
  207. }
  208. }
  209. }
  210. this._modules.ingredients.isPopulated = false;
  211. this._modules.orders.isPopulated = false;
  212. }
  213. get units(){
  214. return this._units;
  215. }
  216. getRevenue(from, to = new Date()){
  217. if(from === 0){
  218. from = this.transactions[0].date;
  219. }
  220. const {start, end} = this.getTransactionIndices(from, to);
  221. let total = 0;
  222. for(let i = start; i <= end; i++){
  223. for(let j = 0; j < this.transactions[i].recipes.length; j++){
  224. for(let k = 0; k < this.recipes.length; k++){
  225. if(this.transactions[i].recipes[j].recipe === this.recipes[k]){
  226. total += this.transactions[i].recipes[j].quantity * this.recipes[k].price;
  227. }
  228. }
  229. }
  230. }
  231. return total / 100;
  232. }
  233. /*
  234. Gets the quantity of each ingredient sold between two dates (dateRange)
  235. Inputs
  236. dateRange: list containing a start date and an end date
  237. Return:
  238. [{
  239. ingredient: Ingredient object,
  240. quantity: quantity of ingredient sold
  241. }]
  242. */
  243. getIngredientsSold(from = 0, to = new Date()){
  244. if(from = 0){
  245. from = this._ingredients[0].date;
  246. }
  247. let recipes = this.getRecipesSold(from, to);
  248. let ingredientList = [];
  249. for(let i = 0; i < recipes.length; i++){
  250. for(let j = 0; j < recipes[i].recipe.ingredients.length; j++){
  251. let exists = false;
  252. for(let k = 0; k < ingredientList.length; k++){
  253. if(ingredientList[k].ingredient === recipes[i].recipe.ingredients[j].ingredient){
  254. exists = true;
  255. ingredientList[k].quantity += recipes[i].quantity * recipes[i].recipe.ingredients[j].quantity;
  256. break;
  257. }
  258. }
  259. if(!exists){
  260. ingredientList.push({
  261. ingredient: recipes[i].recipe.ingredients[j].ingredient,
  262. quantity: recipes[i].quantity * recipes[i].recipe.ingredients[j].quantity
  263. });
  264. }
  265. }
  266. }
  267. return ingredientList;
  268. }
  269. getSingleIngredientSold(ingredient, from = 0, to = new Date()){
  270. if(from === 0){
  271. from = this._transactions[0].date;
  272. }
  273. const {start, end} = this.getTransactionIndices(from, to);
  274. let total = 0;
  275. for(let i = start; i < end; i++){
  276. for(let j = 0; j < this._transactions[i].recipes.length; j++){
  277. for(let k = 0; k < this._transactions[i].recipes[j].recipe.ingredients.length; k++){
  278. if(this._transactions[i].recipes[j].recipe.ingredients[k].ingredient === ingredient.ingredient){
  279. total += this._transactions[i].recipes[j].recipe.ingredients[k].quantity;
  280. break;
  281. }
  282. }
  283. }
  284. }
  285. return total;
  286. }
  287. /*
  288. Gets the number of recipes sold between two dates (dateRange)
  289. Inputs:
  290. dateRange: array containing a start date and an end date
  291. Return:
  292. [{
  293. recipe: a recipe object
  294. quantity: quantity of the recipe sold
  295. }]
  296. */
  297. getRecipesSold(from = 0, to = new Date()){
  298. if(from = 0){
  299. from = this._transactions[0].date;
  300. }
  301. const {start, end} = this.getTransactionIndices(from, to);
  302. let recipeList = [];
  303. for(let i = start; i <= end; i++){
  304. for(let j = 0; j < this._transactions[i].recipes.length; j++){
  305. let exists = false;
  306. for(let k = 0; k < recipeList.length; k++){
  307. if(recipeList[k].recipe === this._transactions[i].recipes[j].recipe){
  308. exists = true;
  309. recipeList[k].quantity += this._transactions[i].recipes[j].quantity;
  310. break;
  311. }
  312. }
  313. if(!exists){
  314. recipeList.push({
  315. recipe: this._transactions[i].recipes[j].recipe,
  316. quantity: this._transactions[i].recipes[j].quantity
  317. });
  318. }
  319. }
  320. }
  321. return recipeList;
  322. }
  323. /*
  324. Groups all of the merchant's ingredients by their category
  325. Return: [{
  326. name: category name,
  327. ingredients: [Ingredient Object]
  328. }]
  329. */
  330. categorizeIngredients(){
  331. let ingredientsByCategory = [];
  332. for(let i = 0; i < this.ingredients.length; i++){
  333. let categoryExists = false;
  334. for(let j = 0; j < ingredientsByCategory.length; j++){
  335. if(this.ingredients[i].ingredient.category === ingredientsByCategory[j].name){
  336. ingredientsByCategory[j].ingredients.push(this.ingredients[i]);
  337. categoryExists = true;
  338. break;
  339. }
  340. }
  341. if(!categoryExists){
  342. ingredientsByCategory.push({
  343. name: this.ingredients[i].ingredient.category,
  344. ingredients: [this.ingredients[i]]
  345. });
  346. }
  347. }
  348. return ingredientsByCategory;
  349. }
  350. unitizeIngredients(){
  351. let ingredientsByUnit = [];
  352. for(let i = 0; i < this.ingredients.length; i++){
  353. let unitExists = false;
  354. const innerIngredient = this.ingredients[i].ingredient;
  355. for(let j = 0; j < ingredientsByUnit.length; j++){
  356. if(innerIngredient.unit === ingredientsByUnit[j].name || innerIngredient.specialUnit === ingredientsByUnit[j].name){
  357. ingredientsByUnit[j].ingredients.push(this.ingredients[i]);
  358. unitExists = true;
  359. break;
  360. }
  361. }
  362. if(!unitExists){
  363. let unit = "";
  364. if(innerIngredient.specialUnit === "bottle"){
  365. unit = "bottle";
  366. }else{
  367. unit = innerIngredient.unit;
  368. }
  369. ingredientsByUnit.push({
  370. name: unit,
  371. ingredients: [this.ingredients[i]]
  372. });
  373. }
  374. }
  375. return ingredientsByUnit;
  376. }
  377. getRecipesForIngredient(ingredient){
  378. let recipes = [];
  379. for(let i = 0; i < this._recipes.length; i++){
  380. for(let j = 0; j < this._recipes[i].ingredients.length; j++){
  381. if(this._recipes[i].ingredients[j].ingredient === ingredient){
  382. recipes.push(this._recipes[i]);
  383. }
  384. }
  385. }
  386. return recipes;
  387. }
  388. getTransactionIndices(from, to){
  389. for(let i = 0; i < this.transactions.length; i++){
  390. if(this.transactions[i].date >= from){
  391. from = i;
  392. break;
  393. }
  394. }
  395. for(let i = this.transactions.length - 1; i >= 0; i--){
  396. if(transactions[i].date <= to){
  397. to = i;
  398. break;
  399. }
  400. }
  401. return {from: from, to: to};
  402. }
  403. }
  404. module.exports = Merchant;