Merchant.js 11 KB

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