Merchant.js 13 KB

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