Merchant.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470
  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. // for(let )
  197. // if(remove){
  198. // }
  199. // }
  200. /*
  201. Gets the indices of two dates from transactions
  202. Inputs
  203. from: starting date
  204. to: ending date (default to now)
  205. Output
  206. Array containing starting index and ending index
  207. Note: Will return false if it cannot find both necessary dates
  208. */
  209. transactionIndices(from, to = new Date()){
  210. let indices = [];
  211. for(let i = 0; i < this.transactions.length; i++){
  212. if(this.transactions[i].date > from){
  213. indices.push(i);
  214. break;
  215. }
  216. }
  217. for(let i = this.transactions.length - 1; i >=0; i--){
  218. if(this.transactions[i].date < to){
  219. indices.push(i);
  220. break;
  221. }
  222. }
  223. if(indices.length < 2){
  224. return false;
  225. }
  226. return indices;
  227. }
  228. revenue(indices){
  229. let total = 0;
  230. for(let i = indices[0]; i <= indices[1]; i++){
  231. for(let j = 0; j < this.transactions[i].recipes.length; j++){
  232. for(let k = 0; k < this.recipes.length; k++){
  233. if(this.transactions[i].recipes[j].recipe === this.recipes[k]){
  234. total += this.transactions[i].recipes[j].quantity * this.recipes[k].price;
  235. }
  236. }
  237. }
  238. }
  239. return total / 100;
  240. }
  241. /*
  242. Gets the quantity of each ingredient sold between two dates (dateRange)
  243. Inputs
  244. dateRange: list containing a start date and an end date
  245. Return:
  246. [{
  247. ingredient: Ingredient object,
  248. quantity: quantity of ingredient sold
  249. }]
  250. */
  251. ingredientsSold(dateRange){
  252. if(!dateRange){
  253. return false;
  254. }
  255. let recipes = this.recipesSold(dateRange);
  256. let ingredientList = [];
  257. for(let i = 0; i < recipes.length; i++){
  258. for(let j = 0; j < recipes[i].recipe.ingredients.length; j++){
  259. let exists = false;
  260. for(let k = 0; k < ingredientList.length; k++){
  261. if(ingredientList[k].ingredient === recipes[i].recipe.ingredients[j].ingredient){
  262. exists = true;
  263. ingredientList[k].quantity += recipes[i].quantity * recipes[i].recipe.ingredients[j].quantity;
  264. break;
  265. }
  266. }
  267. if(!exists){
  268. ingredientList.push({
  269. ingredient: recipes[i].recipe.ingredients[j].ingredient,
  270. quantity: recipes[i].quantity * recipes[i].recipe.ingredients[j].quantity
  271. });
  272. }
  273. }
  274. }
  275. return ingredientList;
  276. }
  277. singleIngredientSold(dateRange, ingredient){
  278. let total = 0;
  279. for(let i = dateRange[0]; i < dateRange[1]; i++){
  280. for(let j = 0; j < this.transactions[i].recipes.length; j++){
  281. for(let k = 0; k < this.transactions[i].recipes[j].recipe.ingredients.length; k++){
  282. if(this.transactions[i].recipes[j].recipe.ingredients[k].ingredient === ingredient.ingredient){
  283. total += this.transactions[i].recipes[j].recipe.ingredients[k].quantity;
  284. break;
  285. }
  286. }
  287. }
  288. }
  289. return total;
  290. }
  291. /*
  292. Gets the number of recipes sold between two dates (dateRange)
  293. Inputs:
  294. dateRange: array containing a start date and an end date
  295. Return:
  296. [{
  297. recipe: a recipe object
  298. quantity: quantity of the recipe sold
  299. }]
  300. */
  301. recipesSold(dateRange){
  302. let recipeList = [];
  303. for(let i = dateRange[0]; i <= dateRange[1]; 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. Create revenue data for graphing
  325. Input:
  326. dateRange: [start index, end index] (this.transactionIndices)
  327. Return:
  328. [total revenue for each day]
  329. */
  330. graphDailyRevenue(dateRange){
  331. if(!dateRange){
  332. return false;
  333. }
  334. let dataList = new Array(30).fill(0);
  335. let currentDate = this.transactions[dateRange[0]].date;
  336. let arrayIndex = 0;
  337. for(let i = dateRange[0]; i <= dateRange[1]; i++){
  338. if(this.transactions[i].date.getDate() !== currentDate.getDate()){
  339. currentDate = this.transactions[i].date;
  340. arrayIndex++;
  341. }
  342. for(let j = 0; j < this.transactions[i].recipes.length; j++){
  343. dataList[arrayIndex] += (this.transactions[i].recipes[j].recipe.price / 100) * this.transactions[i].recipes[j].quantity;
  344. }
  345. }
  346. return dataList;
  347. }
  348. /*
  349. Groups all of the merchant's ingredients by their category
  350. Return: [{
  351. name: category name,
  352. ingredients: [Ingredient Object]
  353. }]
  354. */
  355. categorizeIngredients(){
  356. let ingredientsByCategory = [];
  357. for(let i = 0; i < this.ingredients.length; i++){
  358. let categoryExists = false;
  359. for(let j = 0; j < ingredientsByCategory.length; j++){
  360. if(this.ingredients[i].ingredient.category === ingredientsByCategory[j].name){
  361. ingredientsByCategory[j].ingredients.push(this.ingredients[i]);
  362. categoryExists = true;
  363. break;
  364. }
  365. }
  366. if(!categoryExists){
  367. ingredientsByCategory.push({
  368. name: this.ingredients[i].ingredient.category,
  369. ingredients: [this.ingredients[i]]
  370. });
  371. }
  372. }
  373. return ingredientsByCategory;
  374. }
  375. unitizeIngredients(){
  376. let ingredientsByUnit = [];
  377. for(let i = 0; i < this.ingredients.length; i++){
  378. let unitExists = false;
  379. for(let j = 0; j < ingredientsByUnit.length; j++){
  380. if(this.ingredients[i].ingredient.unit === ingredientsByUnit[j].name){
  381. ingredientsByUnit[j].ingredients.push(this.ingredients[i]);
  382. unitExists = true;
  383. break;
  384. }
  385. }
  386. if(!unitExists){
  387. ingredientsByUnit.push({
  388. name: this.ingredients[i].ingredient.unit,
  389. ingredients: [this.ingredients[i]]
  390. });
  391. }
  392. }
  393. return ingredientsByUnit;
  394. }
  395. getRecipesForIngredient(ingredient){
  396. let recipes = [];
  397. for(let i = 0; i < this.recipes.length; i++){
  398. for(let j = 0; j < this.recipes[i].ingredients.length; j++){
  399. if(this.recipes[i].ingredients[j].ingredient === ingredient){
  400. recipes.push(this.recipes[i]);
  401. }
  402. }
  403. }
  404. return recipes;
  405. }
  406. }