Merchant.js 12 KB

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