Merchant.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497
  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. this._modules.ingredients.display();
  79. controller.closeSidebar();
  80. }
  81. updateIngredient(ingredient, quantity){
  82. const index = this._ingredients.indexOf(ingredient);
  83. if(index === undefined){
  84. return false;
  85. }
  86. this._ingredients[index].quantity = quantity;
  87. this._modules.home.isPopulated = false;
  88. this._modules.ingredients.isPopulated = false;
  89. this._modules.ingredients.display()
  90. controller.closeSidebar();
  91. }
  92. get recipes(){
  93. return this._recipes;
  94. }
  95. addRecipe(recipe){
  96. this._recipes.push(recipe);
  97. this._modules.transactions.isPopulated = false;
  98. this._modules.recipeBook.isPopulated = false;
  99. this._modules.recipeBook.display();
  100. controller.closeSidebar();
  101. }
  102. removeRecipe(recipe){
  103. const index = this._recipes.indexOf(recipe);
  104. if(index === undefined){
  105. return false;
  106. }
  107. this._recipes.splice(index, 1);
  108. this._modules.transactions.isPopulated = false;
  109. this._modules.recipeBook.isPopulated = false;
  110. this._modules.recipeBook.display();
  111. controller.closeSidebar();
  112. }
  113. getTransactions(from = 0, to = new Date()){
  114. if(from === 0){
  115. from = this._transactions[0].date;
  116. }
  117. const {start, end} = this.getTransactionIndices(from, to);
  118. return this._transactions.slice(start, end);
  119. }
  120. addTransaction(transaction){
  121. this._transactions.push(transaction);
  122. this._transactions.sort((a, b)=>{
  123. if(a.date > b.date){
  124. return -1;
  125. }
  126. return 1;
  127. });
  128. let ingredients = {};
  129. for(let i = 0; i < transaction.recipes.length; i++){
  130. const recipe = transaction.recipes[i];
  131. for(let j = 0; j < recipe.recipe.ingredients.length; j++){
  132. const ingredient = recipe.ingredients[i];
  133. if(ingredients[ingredient.ingredient.id]){
  134. ingredients[ingredient.ingredient.id] += recipe.quantity * ingredient.quantity;
  135. }else{
  136. ingredients[ingredient.ingredient.id] = recipe.quantity * ingredient.quantity;
  137. }
  138. }
  139. }
  140. const keys = Object.keys(ingredients);
  141. for(let i = 0; i < keys.length; i++){
  142. for(let j = 0; j < this._ingredients.length; j++){
  143. if(keys[i] === this._ingredients[j].ingredient.id){
  144. this._ingredients.quantity -= ingredients[keys[i]];
  145. }
  146. }
  147. }
  148. this._modules.home.isPopulated = false;
  149. this._modules.ingredients.isPopulated = false;
  150. this._modules.transactions.isPopulated = false;
  151. this._modules.analytics.newData = true;
  152. this._modeules.transactions.display();
  153. controller.closeSidebar();
  154. }
  155. removeTransaction(transaction){
  156. const index = this._transactions.indexOf(transaction);
  157. if(index === undefined){
  158. return false;
  159. }
  160. this._transactions.splice(index, 1);
  161. let ingredients = {};
  162. for(let i = 0; i < transaction.recipes.length; i++){
  163. const recipe = transaction.recipes[i];
  164. for(let j = 0; j < recipe.recipe.ingredients.length; j++){
  165. const ingredient = recipe.recipe.ingredients[i];
  166. if(ingredients[ingredient.ingredient.id]){
  167. ingredients[ingredient.ingredient.id] += ingredient.quantity * recipe.quantity;
  168. }else{
  169. ingredients[ingredient.ingredient.id] = ingredient.quantity * recipe.quantity;
  170. }
  171. }
  172. }
  173. const keys = Object.keys(ingredients);
  174. for(let i = 0; i < keys.length; i++){
  175. for(let j = 0; j < this._ingredients.length; j++){
  176. if(keys[i] === this._ingredients[j].ingredient.id){
  177. this._ingredients.quantity += ingredients[keys[i]];
  178. }
  179. }
  180. }
  181. this._modules.home.isPopulated = false;
  182. this._modules.ingredients.isPopulated = false;
  183. this._modules.transactions.isPopulated = false;
  184. this._modules.analytics.newData = true;
  185. this._modules.transactions.display();
  186. controller.closeSidebar();
  187. }
  188. get orders(){
  189. return this._orders;
  190. }
  191. addOrder(order, isNew = false){
  192. this._orders.push(order);
  193. if(isNew){
  194. for(let i = 0; i < order.ingredients.length; i++){
  195. for(let j = 0; j < this._ingredients.length; j++){
  196. if(order.ingredients[i] === this._ingredients[j].ingredient){
  197. this._ingredients[j].quantity += order.ingredients[i].quantity;
  198. break;
  199. }
  200. }
  201. }
  202. }
  203. this._modules.ingredients.isPopulated = false;
  204. this._modules.orders.isPopulated = false;
  205. this._modules.orders.display();
  206. controller.closeSidebar();
  207. }
  208. removeOrder(order){
  209. const index = this._orders.indexOf(order);
  210. if(index === undefined){
  211. return false;
  212. }
  213. this._orders.splice(index, 1);
  214. for(let i = 0; i < order.ingredients.length; i++){
  215. for(let j = 0; j < this._ingredients.length; j++){
  216. if(order.ingredients[i].ingredient === this._ingredients[j].ingredient){
  217. this._ingredients[j].quantity -= order.ingredients[i].quantity;
  218. }
  219. }
  220. }
  221. this._modules.ingredients.isPopulated = false;
  222. this._modules.orders.isPopulated = false;
  223. this._modules.orders.display();
  224. controller.closeSidebar();
  225. }
  226. get units(){
  227. return this._units;
  228. }
  229. getRevenue(from, to = new Date()){
  230. if(from === 0){
  231. from = this._transactions[0].date;
  232. }
  233. const {start, end} = this.getTransactionIndices(from, to);
  234. let total = 0;
  235. for(let i = start; i <= end; i++){
  236. for(let j = 0; j < this._transactions[i].recipes.length; j++){
  237. for(let k = 0; k < this.recipes.length; k++){
  238. if(this._transactions[i].recipes[j].recipe === this.recipes[k]){
  239. total += this._transactions[i].recipes[j].quantity * this.recipes[k].price;
  240. }
  241. }
  242. }
  243. }
  244. return total / 100;
  245. }
  246. /*
  247. Gets the quantity of each ingredient sold between two dates (dateRange)
  248. Inputs
  249. dateRange: list containing a start date and an end date
  250. Return:
  251. [{
  252. ingredient: Ingredient object,
  253. quantity: quantity of ingredient sold
  254. }]
  255. */
  256. getIngredientsSold(from = 0, to = new Date()){
  257. if(from = 0){
  258. from = this._ingredients[0].date;
  259. }
  260. let recipes = this.getRecipesSold(from, to);
  261. let ingredientList = [];
  262. for(let i = 0; i < recipes.length; i++){
  263. for(let j = 0; j < recipes[i].recipe.ingredients.length; j++){
  264. let exists = false;
  265. for(let k = 0; k < ingredientList.length; k++){
  266. if(ingredientList[k].ingredient === recipes[i].recipe.ingredients[j].ingredient){
  267. exists = true;
  268. ingredientList[k].quantity += recipes[i].quantity * recipes[i].recipe.ingredients[j].quantity;
  269. break;
  270. }
  271. }
  272. if(!exists){
  273. ingredientList.push({
  274. ingredient: recipes[i].recipe.ingredients[j].ingredient,
  275. quantity: recipes[i].quantity * recipes[i].recipe.ingredients[j].quantity
  276. });
  277. }
  278. }
  279. }
  280. return ingredientList;
  281. }
  282. getSingleIngredientSold(ingredient, from = 0, to = new Date()){
  283. if(from === 0){
  284. from = this._transactions[0].date;
  285. }
  286. const {start, end} = this.getTransactionIndices(from, to);
  287. let total = 0;
  288. for(let i = start; i < end; i++){
  289. for(let j = 0; j < this._transactions[i].recipes.length; j++){
  290. for(let k = 0; k < this._transactions[i].recipes[j].recipe.ingredients.length; k++){
  291. if(this._transactions[i].recipes[j].recipe.ingredients[k].ingredient === ingredient.ingredient){
  292. total += this._transactions[i].recipes[j].recipe.ingredients[k].quantity;
  293. break;
  294. }
  295. }
  296. }
  297. }
  298. return total;
  299. }
  300. /*
  301. Gets the number of recipes sold between two dates (dateRange)
  302. Inputs:
  303. dateRange: array containing a start date and an end date
  304. Return:
  305. [{
  306. recipe: a recipe object
  307. quantity: quantity of the recipe sold
  308. }]
  309. */
  310. getRecipesSold(from = 0, to = new Date()){
  311. if(from = 0){
  312. from = this._transactions[0].date;
  313. }
  314. const {start, end} = this.getTransactionIndices(from, to);
  315. let recipeList = [];
  316. for(let i = start; i <= end; i++){
  317. for(let j = 0; j < this._transactions[i].recipes.length; j++){
  318. let exists = false;
  319. for(let k = 0; k < recipeList.length; k++){
  320. if(recipeList[k].recipe === this._transactions[i].recipes[j].recipe){
  321. exists = true;
  322. recipeList[k].quantity += this._transactions[i].recipes[j].quantity;
  323. break;
  324. }
  325. }
  326. if(!exists){
  327. recipeList.push({
  328. recipe: this._transactions[i].recipes[j].recipe,
  329. quantity: this._transactions[i].recipes[j].quantity
  330. });
  331. }
  332. }
  333. }
  334. return recipeList;
  335. }
  336. /*
  337. Groups all of the merchant's ingredients by their category
  338. Return: [{
  339. name: category name,
  340. ingredients: [Ingredient Object]
  341. }]
  342. */
  343. categorizeIngredients(){
  344. let ingredientsByCategory = [];
  345. for(let i = 0; i < this.ingredients.length; i++){
  346. let categoryExists = false;
  347. for(let j = 0; j < ingredientsByCategory.length; j++){
  348. if(this.ingredients[i].ingredient.category === ingredientsByCategory[j].name){
  349. ingredientsByCategory[j].ingredients.push(this.ingredients[i]);
  350. categoryExists = true;
  351. break;
  352. }
  353. }
  354. if(!categoryExists){
  355. ingredientsByCategory.push({
  356. name: this.ingredients[i].ingredient.category,
  357. ingredients: [this.ingredients[i]]
  358. });
  359. }
  360. }
  361. return ingredientsByCategory;
  362. }
  363. unitizeIngredients(){
  364. let ingredientsByUnit = [];
  365. for(let i = 0; i < this.ingredients.length; i++){
  366. let unitExists = false;
  367. const innerIngredient = this.ingredients[i].ingredient;
  368. for(let j = 0; j < ingredientsByUnit.length; j++){
  369. if(innerIngredient.unit === ingredientsByUnit[j].name || innerIngredient.specialUnit === ingredientsByUnit[j].name){
  370. ingredientsByUnit[j].ingredients.push(this.ingredients[i]);
  371. unitExists = true;
  372. break;
  373. }
  374. }
  375. if(!unitExists){
  376. let unit = "";
  377. if(innerIngredient.specialUnit === "bottle"){
  378. unit = "bottle";
  379. }else{
  380. unit = innerIngredient.unit;
  381. }
  382. ingredientsByUnit.push({
  383. name: unit,
  384. ingredients: [this.ingredients[i]]
  385. });
  386. }
  387. }
  388. return ingredientsByUnit;
  389. }
  390. getRecipesForIngredient(ingredient){
  391. let recipes = [];
  392. for(let i = 0; i < this._recipes.length; i++){
  393. for(let j = 0; j < this._recipes[i].ingredients.length; j++){
  394. if(this._recipes[i].ingredients[j].ingredient === ingredient){
  395. recipes.push(this._recipes[i]);
  396. }
  397. }
  398. }
  399. return recipes;
  400. }
  401. getTransactionIndices(from, to){
  402. for(let i = 0; i < this._transactions.length; i++){
  403. if(this._transactions[i].date >= from){
  404. from = i;
  405. break;
  406. }
  407. }
  408. for(let i = this._transactions.length - 1; i >= 0; i--){
  409. if(this._transactions[i].date <= to){
  410. to = i;
  411. break;
  412. }
  413. }
  414. return {from: from, to: to};
  415. }
  416. }
  417. module.exports = Merchant;