Merchant.js 15 KB

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