Merchant.js 15 KB

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