Merchant.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470
  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(id, name, date, ingredients, parent){
  57. this.id = id;
  58. this.name = name;
  59. this.date = new Date(date);
  60. this.ingredients = [];
  61. this.parent = parent;
  62. for(let i = 0; i < ingredients.length; i++){
  63. for(let j = 0; j < parent.ingredients.length; j++){
  64. if(ingredients[i].ingredient === parent.ingredients[j].ingredient.id){
  65. this.ingredients.push({
  66. ingredient: parent.ingredients[j].ingredient,
  67. quantity: ingredients[i].quantity,
  68. price: ingredients[i].price
  69. });
  70. }
  71. }
  72. }
  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. isOrder = set true if this is coming from an order
  120. */
  121. editIngredients(ingredients, remove = false, isOrder = false){
  122. for(let i = 0; i < ingredients.length; i++){
  123. let isNew = true;
  124. for(let j = 0; j < merchant.ingredients.length; j++){
  125. if(merchant.ingredients[j].ingredient === ingredients[i].ingredient){
  126. if(remove){
  127. merchant.ingredients.splice(j, 1);
  128. }else if(!remove && isOrder){
  129. merchant.ingredients[j].quantity += ingredients[i].quantity;
  130. }else{
  131. merchant.ingredients[j].quantity = ingredients[i].quantity;
  132. }
  133. isNew = false;
  134. break;
  135. }
  136. }
  137. if(isNew){
  138. merchant.ingredients.push({
  139. ingredient: ingredients[i].ingredient,
  140. quantity: parseFloat(ingredients[i].quantity)
  141. });
  142. }
  143. }
  144. homeStrandObj.drawInventoryCheckCard();
  145. ingredientsStrandObj.populateByProperty("category");
  146. addIngredientsComp.isPopulated = false;
  147. closeSidebar();
  148. }
  149. /*
  150. Updates a recipe in the merchants list of recipes
  151. Can create, edit or remove
  152. recipe = Recipe object
  153. remove = will remove recipe when true
  154. */
  155. editRecipe(recipe, remove = false){
  156. let isNew = true;
  157. for(let i = 0; i < this.recipes.length; i++){
  158. if(recipe === this.recipes[i]){
  159. if(remove){
  160. this.recipes.splice(i, 1);
  161. }else{
  162. this.recipes[i] = recipe;
  163. }
  164. isNew = false;
  165. break;
  166. }
  167. }
  168. if(isNew){
  169. merchant.recipes.push(recipe);
  170. }
  171. recipeBookStrandObj.populateRecipes();
  172. closeSidebar();
  173. }
  174. /*
  175. Updates a list of orders in the merchants list of orders
  176. Create/edit/remove
  177. orders = [Order object]
  178. remove = will remove order when true
  179. */
  180. editOrders(orders, remove = false){
  181. for(let i = 0; i < orders.length; i++){
  182. let isNew = true;
  183. for(let j = 0; j < this.orders.length; j++){
  184. if(orders[i] === this.orders[j]){
  185. if(remove){
  186. this.orders.splice(j, 1);
  187. }else{
  188. this.orders[j] = orders[i];
  189. }
  190. isNew = false;
  191. break;
  192. }
  193. }
  194. if(isNew){
  195. this.orders.push(orders[i]);
  196. }
  197. }
  198. ordersStrandObj.populate();
  199. closeSidebar();
  200. }
  201. /*
  202. Gets the indices of two dates from transactions
  203. Inputs
  204. from: starting date
  205. to: ending date (default to now)
  206. Output
  207. Array containing starting index and ending index
  208. Note: Will return false if it cannot find both necessary dates
  209. */
  210. transactionIndices(from, to = new Date()){
  211. let indices = [];
  212. for(let i = 0; i < this.transactions.length; i++){
  213. if(this.transactions[i].date > from){
  214. indices.push(i);
  215. break;
  216. }
  217. }
  218. for(let i = this.transactions.length - 1; i >=0; i--){
  219. if(this.transactions[i].date < to){
  220. indices.push(i);
  221. break;
  222. }
  223. }
  224. if(indices.length < 2){
  225. return false;
  226. }
  227. return indices;
  228. }
  229. revenue(indices){
  230. let total = 0;
  231. for(let i = indices[0]; i <= indices[1]; i++){
  232. for(let j = 0; j < this.transactions[i].recipes.length; j++){
  233. for(let k = 0; k < this.recipes.length; k++){
  234. if(this.transactions[i].recipes[j].recipe === this.recipes[k]){
  235. total += this.transactions[i].recipes[j].quantity * this.recipes[k].price;
  236. }
  237. }
  238. }
  239. }
  240. return total / 100;
  241. }
  242. /*
  243. Gets the quantity of each ingredient sold between two dates (dateRange)
  244. Inputs
  245. dateRange: list containing a start date and an end date
  246. Return:
  247. [{
  248. ingredient: Ingredient object,
  249. quantity: quantity of ingredient sold
  250. }]
  251. */
  252. ingredientsSold(dateRange){
  253. if(!dateRange){
  254. return false;
  255. }
  256. let recipes = this.recipesSold(dateRange);
  257. let ingredientList = [];
  258. for(let i = 0; i < recipes.length; i++){
  259. for(let j = 0; j < recipes[i].recipe.ingredients.length; j++){
  260. let exists = false;
  261. for(let k = 0; k < ingredientList.length; k++){
  262. if(ingredientList[k].ingredient === recipes[i].recipe.ingredients[j].ingredient){
  263. exists = true;
  264. ingredientList[k].quantity += recipes[i].quantity * recipes[i].recipe.ingredients[j].quantity;
  265. break;
  266. }
  267. }
  268. if(!exists){
  269. ingredientList.push({
  270. ingredient: recipes[i].recipe.ingredients[j].ingredient,
  271. quantity: recipes[i].quantity * recipes[i].recipe.ingredients[j].quantity
  272. });
  273. }
  274. }
  275. }
  276. return ingredientList;
  277. }
  278. singleIngredientSold(dateRange, ingredient){
  279. let total = 0;
  280. for(let i = dateRange[0]; i < dateRange[1]; i++){
  281. for(let j = 0; j < this.transactions[i].recipes.length; j++){
  282. for(let k = 0; k < this.transactions[i].recipes[j].recipe.ingredients.length; k++){
  283. if(this.transactions[i].recipes[j].recipe.ingredients[k].ingredient === ingredient.ingredient){
  284. total += this.transactions[i].recipes[j].recipe.ingredients[k].quantity;
  285. break;
  286. }
  287. }
  288. }
  289. }
  290. return total;
  291. }
  292. /*
  293. Gets the number of recipes sold between two dates (dateRange)
  294. Inputs:
  295. dateRange: array containing a start date and an end date
  296. Return:
  297. [{
  298. recipe: a recipe object
  299. quantity: quantity of the recipe sold
  300. }]
  301. */
  302. recipesSold(dateRange){
  303. let recipeList = [];
  304. for(let i = dateRange[0]; i <= dateRange[1]; i++){
  305. for(let j = 0; j < this.transactions[i].recipes.length; j++){
  306. let exists = false;
  307. for(let k = 0; k < recipeList.length; k++){
  308. if(recipeList[k].recipe === this.transactions[i].recipes[j].recipe){
  309. exists = true;
  310. recipeList[k].quantity += this.transactions[i].recipes[j].quantity;
  311. break;
  312. }
  313. }
  314. if(!exists){
  315. recipeList.push({
  316. recipe: this.transactions[i].recipes[j].recipe,
  317. quantity: this.transactions[i].recipes[j].quantity
  318. });
  319. }
  320. }
  321. }
  322. return recipeList;
  323. }
  324. /*
  325. Create revenue data for graphing
  326. Input:
  327. dateRange: [start index, end index] (this.transactionIndices)
  328. Return:
  329. [total revenue for each day]
  330. */
  331. graphDailyRevenue(dateRange){
  332. if(!dateRange){
  333. return false;
  334. }
  335. let dataList = new Array(30).fill(0);
  336. let currentDate = this.transactions[dateRange[0]].date;
  337. let arrayIndex = 0;
  338. for(let i = dateRange[0]; i <= dateRange[1]; i++){
  339. if(this.transactions[i].date.getDate() !== currentDate.getDate()){
  340. currentDate = this.transactions[i].date;
  341. arrayIndex++;
  342. }
  343. for(let j = 0; j < this.transactions[i].recipes.length; j++){
  344. dataList[arrayIndex] += (this.transactions[i].recipes[j].recipe.price / 100) * this.transactions[i].recipes[j].quantity;
  345. }
  346. }
  347. return dataList;
  348. }
  349. /*
  350. Groups all of the merchant's ingredients by their category
  351. Return: [{
  352. name: category name,
  353. ingredients: [Ingredient Object]
  354. }]
  355. */
  356. categorizeIngredients(){
  357. let ingredientsByCategory = [];
  358. for(let i = 0; i < this.ingredients.length; i++){
  359. let categoryExists = false;
  360. for(let j = 0; j < ingredientsByCategory.length; j++){
  361. if(this.ingredients[i].ingredient.category === ingredientsByCategory[j].name){
  362. ingredientsByCategory[j].ingredients.push(this.ingredients[i]);
  363. categoryExists = true;
  364. break;
  365. }
  366. }
  367. if(!categoryExists){
  368. ingredientsByCategory.push({
  369. name: this.ingredients[i].ingredient.category,
  370. ingredients: [this.ingredients[i]]
  371. });
  372. }
  373. }
  374. return ingredientsByCategory;
  375. }
  376. unitizeIngredients(){
  377. let ingredientsByUnit = [];
  378. for(let i = 0; i < this.ingredients.length; i++){
  379. let unitExists = false;
  380. for(let j = 0; j < ingredientsByUnit.length; j++){
  381. if(this.ingredients[i].ingredient.unit === ingredientsByUnit[j].name){
  382. ingredientsByUnit[j].ingredients.push(this.ingredients[i]);
  383. unitExists = true;
  384. break;
  385. }
  386. }
  387. if(!unitExists){
  388. ingredientsByUnit.push({
  389. name: this.ingredients[i].ingredient.unit,
  390. ingredients: [this.ingredients[i]]
  391. });
  392. }
  393. }
  394. return ingredientsByUnit;
  395. }
  396. getRecipesForIngredient(ingredient){
  397. let recipes = [];
  398. for(let i = 0; i < this.recipes.length; i++){
  399. for(let j = 0; j < this.recipes[i].ingredients.length; j++){
  400. if(this.recipes[i].ingredients[j].ingredient === ingredient){
  401. recipes.push(this.recipes[i]);
  402. }
  403. }
  404. }
  405. return recipes;
  406. }
  407. }