Merchant.js 13 KB

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