Merchant.js 13 KB

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