Merchant.js 14 KB

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