Merchant.js 13 KB

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