Merchant.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450
  1. const Ingredient = require("./Ingredient.js");
  2. const Recipe = require("./Recipe.js");
  3. const Transaction = require("./Transaction.js");
  4. class Merchant{
  5. constructor(oldMerchant, transactions){
  6. this.name = oldMerchant.name;
  7. this.pos = oldMerchant.pos;
  8. this.ingredients = [];
  9. this.recipes = [];
  10. this.transactions = [];
  11. this.orders = [];
  12. this.units = {
  13. mass: ["g", "kg", "oz", "lb"],
  14. volume: ["ml", "l", "tsp", "tbsp", "ozfl", "cup", "pt", "qt", "gal"],
  15. length: ["mm", "cm", "m", "in", "foot"],
  16. other: ["each"]
  17. }
  18. for(let i = 0; i < oldMerchant.inventory.length; i++){
  19. this.ingredients.push({
  20. ingredient: new Ingredient(
  21. oldMerchant.inventory[i].ingredient._id,
  22. oldMerchant.inventory[i].ingredient.name,
  23. oldMerchant.inventory[i].ingredient.category,
  24. oldMerchant.inventory[i].ingredient.unitType,
  25. oldMerchant.inventory[i].defaultUnit,
  26. this
  27. ),
  28. quantity: oldMerchant.inventory[i].quantity
  29. });
  30. }
  31. for(let i = 0; i < oldMerchant.recipes.length; i++){
  32. this.recipes.push(new Recipe(
  33. oldMerchant.recipes[i]._id,
  34. oldMerchant.recipes[i].name,
  35. oldMerchant.recipes[i].price,
  36. oldMerchant.recipes[i].ingredients,
  37. this
  38. ));
  39. }
  40. for(let i = 0; i < transactions.length; i++){
  41. this.transactions.push(new Transaction(
  42. transactions[i]._id,
  43. transactions[i].date,
  44. transactions[i].recipes,
  45. this
  46. ));
  47. }
  48. }
  49. /*
  50. Updates all specified item in the merchant's inventory and updates the page
  51. If ingredient doesn't exist, add it
  52. ingredients = {
  53. ingredient: Ingredient object,
  54. quantity: new quantity,
  55. defaultUnit: the default unit to be displayed
  56. }
  57. remove = set true if removing
  58. isOrder = set true if this is coming from an order
  59. */
  60. editIngredients(ingredients, remove = false, isOrder = false){
  61. for(let i = 0; i < ingredients.length; i++){
  62. let isNew = true;
  63. for(let j = 0; j < this.ingredients.length; j++){
  64. if(this.ingredients[j].ingredient === ingredients[i].ingredient){
  65. if(remove){
  66. this.ingredients.splice(j, 1);
  67. }else if(!remove && isOrder){
  68. this.ingredients[j].quantity += ingredients[i].quantity;
  69. }else{
  70. this.ingredients[j].quantity = ingredients[i].quantity;
  71. }
  72. isNew = false;
  73. break;
  74. }
  75. }
  76. if(isNew){
  77. this.ingredients.push({
  78. ingredient: ingredients[i].ingredient,
  79. quantity: parseFloat(ingredients[i].quantity),
  80. defaultUnit: ingredients[i].defaultUnit
  81. });
  82. }
  83. }
  84. controller.updateData("ingredient");
  85. controller.closeSidebar();
  86. }
  87. /*
  88. Updates a recipe in the merchants list of recipes
  89. Can create, edit or remove
  90. recipe = [Recipe object]
  91. remove = will remove recipe when true
  92. */
  93. editRecipes(recipes, remove = false){
  94. let isNew = true;
  95. for(let i = 0; i < recipes.length; i++){
  96. for(let j = 0; j < this.recipes.length; j++){
  97. if(recipes[i] === this.recipes[j]){
  98. if(remove){
  99. this.recipes.splice(j, 1);
  100. }else{
  101. this.recipes[j] = recipes[i];
  102. }
  103. isNew = false;
  104. break;
  105. }
  106. }
  107. if(isNew){
  108. this.recipes.push(recipes[i]);
  109. }
  110. }
  111. controller.updateData("recipe");
  112. controller.closeSidebar();
  113. }
  114. /*
  115. Updates a list of orders in the merchants list of orders
  116. Create/edit/remove
  117. orders = [Order object]
  118. remove = will remove order when true
  119. */
  120. editOrders(orders, remove = false){
  121. for(let i = 0; i < orders.length; i++){
  122. let isNew = true;
  123. for(let j = 0; j < this.orders.length; j++){
  124. if(orders[i] === this.orders[j]){
  125. if(remove){
  126. this.orders.splice(j, 1);
  127. }else{
  128. this.orders[j] = orders[i];
  129. }
  130. isNew = false;
  131. break;
  132. }
  133. }
  134. if(isNew){
  135. this.orders.push(orders[i]);
  136. }
  137. }
  138. controller.updateData("order");
  139. controller.closeSidebar();
  140. }
  141. /*
  142. transaction = Transaction Object to add
  143. ingredients = The ingredients that need to be updated
  144. keys = ingredient ids
  145. values = quantity to change in grams
  146. remove = If true, removes transaction
  147. */
  148. editTransactions(transaction, ingredients, remove = false, ){
  149. let isNew = true;
  150. for(let i = 0; i < this.transactions.length; i++){
  151. if(this.transactions[i] === transaction){
  152. if(remove){
  153. this.transactions.splice(i, 1);
  154. }
  155. isNew = false;
  156. break;
  157. }
  158. }
  159. if(isNew){
  160. this.transactions.push(transaction);
  161. this.transactions.sort((a, b) => a.date > b.date ? 1 : -1);
  162. }
  163. let keys = Object.keys(ingredients);
  164. for(let i = 0; i < keys.length; i++){
  165. for(let j = 0; j < this.ingredients.length; j++){
  166. if(this.ingredients[j].ingredient.id === keys[i]){
  167. if(remove === false){
  168. this.ingredients[j].quantity -= ingredients[keys[i]];
  169. }else{
  170. this.ingredients[j].quantity += ingredients[keys[i]];
  171. }
  172. break;
  173. }
  174. }
  175. }
  176. controller.updateData("ingredient");
  177. controller.updateData("transaction");
  178. controller.closeSidebar();
  179. }
  180. /*
  181. Gets the indices of two dates from transactions
  182. Inputs
  183. from: starting date
  184. to: ending date (default to now)
  185. Output
  186. Array containing starting index and ending index
  187. Note: Will return false if it cannot find both necessary dates
  188. */
  189. transactionIndices(from, to = new Date()){
  190. let indices = [];
  191. for(let i = 0; i < this.transactions.length; i++){
  192. if(this.transactions[i].date > from){
  193. indices.push(i);
  194. break;
  195. }
  196. }
  197. for(let i = this.transactions.length - 1; i >=0; i--){
  198. if(this.transactions[i].date < to){
  199. indices.push(i);
  200. break;
  201. }
  202. }
  203. if(indices.length < 2){
  204. return false;
  205. }
  206. return indices;
  207. }
  208. revenue(indices){
  209. let total = 0;
  210. for(let i = indices[0]; i <= indices[1]; i++){
  211. for(let j = 0; j < this.transactions[i].recipes.length; j++){
  212. for(let k = 0; k < this.recipes.length; k++){
  213. if(this.transactions[i].recipes[j].recipe === this.recipes[k]){
  214. total += this.transactions[i].recipes[j].quantity * this.recipes[k].price;
  215. }
  216. }
  217. }
  218. }
  219. return total / 100;
  220. }
  221. /*
  222. Gets the quantity of each ingredient sold between two dates (dateRange)
  223. Inputs
  224. dateRange: list containing a start date and an end date
  225. Return:
  226. [{
  227. ingredient: Ingredient object,
  228. quantity: quantity of ingredient sold
  229. }]
  230. */
  231. ingredientsSold(dateRange){
  232. if(!dateRange){
  233. return false;
  234. }
  235. let recipes = this.recipesSold(dateRange);
  236. let ingredientList = [];
  237. for(let i = 0; i < recipes.length; i++){
  238. for(let j = 0; j < recipes[i].recipe.ingredients.length; j++){
  239. let exists = false;
  240. for(let k = 0; k < ingredientList.length; k++){
  241. if(ingredientList[k].ingredient === recipes[i].recipe.ingredients[j].ingredient){
  242. exists = true;
  243. ingredientList[k].quantity += recipes[i].quantity * recipes[i].recipe.ingredients[j].quantity;
  244. break;
  245. }
  246. }
  247. if(!exists){
  248. ingredientList.push({
  249. ingredient: recipes[i].recipe.ingredients[j].ingredient,
  250. quantity: recipes[i].quantity * recipes[i].recipe.ingredients[j].quantity
  251. });
  252. }
  253. }
  254. }
  255. return ingredientList;
  256. }
  257. singleIngredientSold(dateRange, ingredient){
  258. let total = 0;
  259. for(let i = dateRange[0]; i < dateRange[1]; i++){
  260. for(let j = 0; j < this.transactions[i].recipes.length; j++){
  261. for(let k = 0; k < this.transactions[i].recipes[j].recipe.ingredients.length; k++){
  262. if(this.transactions[i].recipes[j].recipe.ingredients[k].ingredient === ingredient.ingredient){
  263. total += this.transactions[i].recipes[j].recipe.ingredients[k].quantity;
  264. break;
  265. }
  266. }
  267. }
  268. }
  269. return total;
  270. }
  271. /*
  272. Gets the number of recipes sold between two dates (dateRange)
  273. Inputs:
  274. dateRange: array containing a start date and an end date
  275. Return:
  276. [{
  277. recipe: a recipe object
  278. quantity: quantity of the recipe sold
  279. }]
  280. */
  281. recipesSold(dateRange){
  282. let recipeList = [];
  283. for(let i = dateRange[0]; i <= dateRange[1]; i++){
  284. for(let j = 0; j < this.transactions[i].recipes.length; j++){
  285. let exists = false;
  286. for(let k = 0; k < recipeList.length; k++){
  287. if(recipeList[k].recipe === this.transactions[i].recipes[j].recipe){
  288. exists = true;
  289. recipeList[k].quantity += this.transactions[i].recipes[j].quantity;
  290. break;
  291. }
  292. }
  293. if(!exists){
  294. recipeList.push({
  295. recipe: this.transactions[i].recipes[j].recipe,
  296. quantity: this.transactions[i].recipes[j].quantity
  297. });
  298. }
  299. }
  300. }
  301. return recipeList;
  302. }
  303. /*
  304. Create revenue data for graphing
  305. Input:
  306. dateRange: [start index, end index] (this.transactionIndices)
  307. Return:
  308. [total revenue for each day]
  309. */
  310. graphDailyRevenue(dateRange){
  311. if(!dateRange){
  312. return false;
  313. }
  314. let dataList = new Array(30).fill(0);
  315. let currentDate = this.transactions[dateRange[0]].date;
  316. let arrayIndex = 0;
  317. for(let i = dateRange[0]; i <= dateRange[1]; i++){
  318. if(this.transactions[i].date.getDate() !== currentDate.getDate()){
  319. currentDate = this.transactions[i].date;
  320. arrayIndex++;
  321. }
  322. for(let j = 0; j < this.transactions[i].recipes.length; j++){
  323. dataList[arrayIndex] += (this.transactions[i].recipes[j].recipe.price / 100) * this.transactions[i].recipes[j].quantity;
  324. }
  325. }
  326. return dataList;
  327. }
  328. /*
  329. Groups all of the merchant's ingredients by their category
  330. Return: [{
  331. name: category name,
  332. ingredients: [Ingredient Object]
  333. }]
  334. */
  335. categorizeIngredients(){
  336. let ingredientsByCategory = [];
  337. for(let i = 0; i < this.ingredients.length; i++){
  338. let categoryExists = false;
  339. for(let j = 0; j < ingredientsByCategory.length; j++){
  340. if(this.ingredients[i].ingredient.category === ingredientsByCategory[j].name){
  341. ingredientsByCategory[j].ingredients.push(this.ingredients[i]);
  342. categoryExists = true;
  343. break;
  344. }
  345. }
  346. if(!categoryExists){
  347. ingredientsByCategory.push({
  348. name: this.ingredients[i].ingredient.category,
  349. ingredients: [this.ingredients[i]]
  350. });
  351. }
  352. }
  353. return ingredientsByCategory;
  354. }
  355. unitizeIngredients(){
  356. let ingredientsByUnit = [];
  357. for(let i = 0; i < this.ingredients.length; i++){
  358. let unitExists = false;
  359. for(let j = 0; j < ingredientsByUnit.length; j++){
  360. if(this.ingredients[i].ingredient.unit === ingredientsByUnit[j].name){
  361. ingredientsByUnit[j].ingredients.push(this.ingredients[i]);
  362. unitExists = true;
  363. break;
  364. }
  365. }
  366. if(!unitExists){
  367. ingredientsByUnit.push({
  368. name: this.ingredients[i].ingredient.unit,
  369. ingredients: [this.ingredients[i]]
  370. });
  371. }
  372. }
  373. return ingredientsByUnit;
  374. }
  375. getRecipesForIngredient(ingredient){
  376. let recipes = [];
  377. for(let i = 0; i < this.recipes.length; i++){
  378. for(let j = 0; j < this.recipes[i].ingredients.length; j++){
  379. if(this.recipes[i].ingredients[j].ingredient === ingredient){
  380. recipes.push(this.recipes[i]);
  381. }
  382. }
  383. }
  384. return recipes;
  385. }
  386. }
  387. module.exports = Merchant;