Merchant.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  1. const Ingredient = require("./Ingredient.js");
  2. const Recipe = require("./Recipe.js");
  3. const Transaction = require("./Transaction.js");
  4. const merchant = require("../../../models/merchant.js");
  5. class Merchant{
  6. constructor(oldMerchant, transactions){
  7. this.name = oldMerchant.name;
  8. this.pos = oldMerchant.pos;
  9. this.ingredients = [];
  10. this.recipes = [];
  11. this.transactions = [];
  12. this.orders = [];
  13. this.units = {
  14. mass: ["g", "kg", "oz", "lb"],
  15. volume: ["ml", "l", "tsp", "tbsp", "ozfl", "cup", "pt", "qt", "gal"],
  16. length: ["mm", "cm", "m", "in", "foot"],
  17. other: ["each"]
  18. }
  19. for(let i = 0; i < oldMerchant.inventory.length; i++){
  20. this.ingredients.push({
  21. ingredient: new Ingredient(
  22. oldMerchant.inventory[i].ingredient._id,
  23. oldMerchant.inventory[i].ingredient.name,
  24. oldMerchant.inventory[i].ingredient.category,
  25. oldMerchant.inventory[i].ingredient.unitType,
  26. oldMerchant.inventory[i].defaultUnit,
  27. this
  28. ),
  29. quantity: oldMerchant.inventory[i].quantity
  30. });
  31. }
  32. for(let i = 0; i < oldMerchant.recipes.length; i++){
  33. this.recipes.push(new Recipe(
  34. oldMerchant.recipes[i]._id,
  35. oldMerchant.recipes[i].name,
  36. oldMerchant.recipes[i].price,
  37. oldMerchant.recipes[i].ingredients,
  38. this
  39. ));
  40. }
  41. for(let i = 0; i < transactions.length; i++){
  42. this.transactions.push(new Transaction(
  43. transactions[i]._id,
  44. transactions[i].date,
  45. transactions[i].recipes,
  46. this
  47. ));
  48. }
  49. }
  50. /*
  51. Updates all specified item in the merchant's inventory and updates the page
  52. If ingredient doesn't exist, add it
  53. ingredients = {
  54. ingredient: Ingredient object,
  55. quantity: new quantity,
  56. defaultUnit: the default unit to be displayed
  57. }
  58. remove = set true if removing
  59. isOrder = set true if this is coming from an order
  60. */
  61. editIngredients(ingredients, remove = false, isOrder = false){
  62. for(let i = 0; i < ingredients.length; i++){
  63. let isNew = true;
  64. for(let j = 0; j < merchant.ingredients.length; j++){
  65. if(merchant.ingredients[j].ingredient === ingredients[i].ingredient){
  66. if(remove){
  67. merchant.ingredients.splice(j, 1);
  68. }else if(!remove && isOrder){
  69. merchant.ingredients[j].quantity += ingredients[i].quantity;
  70. }else{
  71. merchant.ingredients[j].quantity = ingredients[i].quantity;
  72. }
  73. isNew = false;
  74. break;
  75. }
  76. }
  77. if(isNew){
  78. merchant.ingredients.push({
  79. ingredient: ingredients[i].ingredient,
  80. quantity: parseFloat(ingredients[i].quantity),
  81. defaultUnit: ingredients[i].defaultUnit
  82. });
  83. }
  84. }
  85. controller.updateData("ingredient");
  86. controller.closeSidebar();
  87. }
  88. /*
  89. Updates a recipe in the merchants list of recipes
  90. Can create, edit or remove
  91. recipe = [Recipe object]
  92. remove = will remove recipe when true
  93. */
  94. editRecipes(recipes, remove = false){
  95. let isNew = true;
  96. for(let i = 0; i < recipes.length; i++){
  97. for(let j = 0; j < this.recipes.length; j++){
  98. if(recipes[i] === this.recipes[j]){
  99. if(remove){
  100. this.recipes.splice(j, 1);
  101. }else{
  102. this.recipes[j] = recipes[i];
  103. }
  104. isNew = false;
  105. break;
  106. }
  107. }
  108. if(isNew){
  109. merchant.recipes.push(recipes[i]);
  110. }
  111. }
  112. controller.updateData("recipe");
  113. controller.closeSidebar();
  114. }
  115. /*
  116. Updates a list of orders in the merchants list of orders
  117. Create/edit/remove
  118. orders = [Order object]
  119. remove = will remove order when true
  120. */
  121. editOrders(orders, remove = false){
  122. for(let i = 0; i < orders.length; i++){
  123. let isNew = true;
  124. for(let j = 0; j < this.orders.length; j++){
  125. if(orders[i] === this.orders[j]){
  126. if(remove){
  127. this.orders.splice(j, 1);
  128. }else{
  129. this.orders[j] = orders[i];
  130. }
  131. isNew = false;
  132. break;
  133. }
  134. }
  135. if(isNew){
  136. this.orders.push(orders[i]);
  137. }
  138. }
  139. controller.updateData("order");
  140. controller.closeSidebar();
  141. }
  142. /*
  143. transaction = Transaction Object to add
  144. ingredients = The ingredients that need to be updated
  145. keys = ingredient ids
  146. values = quantity to change in grams
  147. remove = If true, removes transaction
  148. */
  149. editTransactions(transaction, ingredients, remove = false, ){
  150. let isNew = true;
  151. for(let i = 0; i < this.transactions.length; i++){
  152. if(this.transactions[i] === transaction){
  153. if(remove){
  154. this.transactions.splice(i, 1);
  155. }
  156. isNew = false;
  157. break;
  158. }
  159. }
  160. if(isNew){
  161. this.transactions.push(transaction);
  162. this.transactions.sort((a, b) => a.date > b.date ? 1 : -1);
  163. }
  164. let keys = Object.keys(ingredients);
  165. for(let i = 0; i < keys.length; i++){
  166. for(let j = 0; j < this.ingredients.length; j++){
  167. if(this.ingredients[j].ingredient.id === keys[i]){
  168. if(remove === false){
  169. this.ingredients[j].quantity -= ingredients[keys[i]];
  170. }else{
  171. this.ingredients[j].quantity += ingredients[keys[i]];
  172. }
  173. break;
  174. }
  175. }
  176. }
  177. controller.updateData("ingredient");
  178. controller.updateData("transaction");
  179. controller.closeSidebar();
  180. }
  181. /*
  182. Gets the indices of two dates from transactions
  183. Inputs
  184. from: starting date
  185. to: ending date (default to now)
  186. Output
  187. Array containing starting index and ending index
  188. Note: Will return false if it cannot find both necessary dates
  189. */
  190. transactionIndices(from, to = new Date()){
  191. let indices = [];
  192. for(let i = 0; i < this.transactions.length; i++){
  193. if(this.transactions[i].date > from){
  194. indices.push(i);
  195. break;
  196. }
  197. }
  198. for(let i = this.transactions.length - 1; i >=0; i--){
  199. if(this.transactions[i].date < to){
  200. indices.push(i);
  201. break;
  202. }
  203. }
  204. if(indices.length < 2){
  205. return false;
  206. }
  207. return indices;
  208. }
  209. revenue(indices){
  210. let total = 0;
  211. for(let i = indices[0]; i <= indices[1]; i++){
  212. for(let j = 0; j < this.transactions[i].recipes.length; j++){
  213. for(let k = 0; k < this.recipes.length; k++){
  214. if(this.transactions[i].recipes[j].recipe === this.recipes[k]){
  215. total += this.transactions[i].recipes[j].quantity * this.recipes[k].price;
  216. }
  217. }
  218. }
  219. }
  220. return total / 100;
  221. }
  222. /*
  223. Gets the quantity of each ingredient sold between two dates (dateRange)
  224. Inputs
  225. dateRange: list containing a start date and an end date
  226. Return:
  227. [{
  228. ingredient: Ingredient object,
  229. quantity: quantity of ingredient sold
  230. }]
  231. */
  232. ingredientsSold(dateRange){
  233. if(!dateRange){
  234. return false;
  235. }
  236. let recipes = this.recipesSold(dateRange);
  237. let ingredientList = [];
  238. for(let i = 0; i < recipes.length; i++){
  239. for(let j = 0; j < recipes[i].recipe.ingredients.length; j++){
  240. let exists = false;
  241. for(let k = 0; k < ingredientList.length; k++){
  242. if(ingredientList[k].ingredient === recipes[i].recipe.ingredients[j].ingredient){
  243. exists = true;
  244. ingredientList[k].quantity += recipes[i].quantity * recipes[i].recipe.ingredients[j].quantity;
  245. break;
  246. }
  247. }
  248. if(!exists){
  249. ingredientList.push({
  250. ingredient: recipes[i].recipe.ingredients[j].ingredient,
  251. quantity: recipes[i].quantity * recipes[i].recipe.ingredients[j].quantity
  252. });
  253. }
  254. }
  255. }
  256. return ingredientList;
  257. }
  258. singleIngredientSold(dateRange, ingredient){
  259. let total = 0;
  260. for(let i = dateRange[0]; i < dateRange[1]; i++){
  261. for(let j = 0; j < this.transactions[i].recipes.length; j++){
  262. for(let k = 0; k < this.transactions[i].recipes[j].recipe.ingredients.length; k++){
  263. if(this.transactions[i].recipes[j].recipe.ingredients[k].ingredient === ingredient.ingredient){
  264. total += this.transactions[i].recipes[j].recipe.ingredients[k].quantity;
  265. break;
  266. }
  267. }
  268. }
  269. }
  270. return total;
  271. }
  272. /*
  273. Gets the number of recipes sold between two dates (dateRange)
  274. Inputs:
  275. dateRange: array containing a start date and an end date
  276. Return:
  277. [{
  278. recipe: a recipe object
  279. quantity: quantity of the recipe sold
  280. }]
  281. */
  282. recipesSold(dateRange){
  283. let recipeList = [];
  284. for(let i = dateRange[0]; i <= dateRange[1]; i++){
  285. for(let j = 0; j < this.transactions[i].recipes.length; j++){
  286. let exists = false;
  287. for(let k = 0; k < recipeList.length; k++){
  288. if(recipeList[k].recipe === this.transactions[i].recipes[j].recipe){
  289. exists = true;
  290. recipeList[k].quantity += this.transactions[i].recipes[j].quantity;
  291. break;
  292. }
  293. }
  294. if(!exists){
  295. recipeList.push({
  296. recipe: this.transactions[i].recipes[j].recipe,
  297. quantity: this.transactions[i].recipes[j].quantity
  298. });
  299. }
  300. }
  301. }
  302. return recipeList;
  303. }
  304. /*
  305. Create revenue data for graphing
  306. Input:
  307. dateRange: [start index, end index] (this.transactionIndices)
  308. Return:
  309. [total revenue for each day]
  310. */
  311. graphDailyRevenue(dateRange){
  312. if(!dateRange){
  313. return false;
  314. }
  315. let dataList = new Array(30).fill(0);
  316. let currentDate = this.transactions[dateRange[0]].date;
  317. let arrayIndex = 0;
  318. for(let i = dateRange[0]; i <= dateRange[1]; i++){
  319. if(this.transactions[i].date.getDate() !== currentDate.getDate()){
  320. currentDate = this.transactions[i].date;
  321. arrayIndex++;
  322. }
  323. for(let j = 0; j < this.transactions[i].recipes.length; j++){
  324. dataList[arrayIndex] += (this.transactions[i].recipes[j].recipe.price / 100) * this.transactions[i].recipes[j].quantity;
  325. }
  326. }
  327. return dataList;
  328. }
  329. /*
  330. Groups all of the merchant's ingredients by their category
  331. Return: [{
  332. name: category name,
  333. ingredients: [Ingredient Object]
  334. }]
  335. */
  336. categorizeIngredients(){
  337. let ingredientsByCategory = [];
  338. for(let i = 0; i < this.ingredients.length; i++){
  339. let categoryExists = false;
  340. for(let j = 0; j < ingredientsByCategory.length; j++){
  341. if(this.ingredients[i].ingredient.category === ingredientsByCategory[j].name){
  342. ingredientsByCategory[j].ingredients.push(this.ingredients[i]);
  343. categoryExists = true;
  344. break;
  345. }
  346. }
  347. if(!categoryExists){
  348. ingredientsByCategory.push({
  349. name: this.ingredients[i].ingredient.category,
  350. ingredients: [this.ingredients[i]]
  351. });
  352. }
  353. }
  354. return ingredientsByCategory;
  355. }
  356. unitizeIngredients(){
  357. let ingredientsByUnit = [];
  358. for(let i = 0; i < this.ingredients.length; i++){
  359. let unitExists = false;
  360. for(let j = 0; j < ingredientsByUnit.length; j++){
  361. if(this.ingredients[i].ingredient.unit === ingredientsByUnit[j].name){
  362. ingredientsByUnit[j].ingredients.push(this.ingredients[i]);
  363. unitExists = true;
  364. break;
  365. }
  366. }
  367. if(!unitExists){
  368. ingredientsByUnit.push({
  369. name: this.ingredients[i].ingredient.unit,
  370. ingredients: [this.ingredients[i]]
  371. });
  372. }
  373. }
  374. return ingredientsByUnit;
  375. }
  376. getRecipesForIngredient(ingredient){
  377. let recipes = [];
  378. for(let i = 0; i < this.recipes.length; i++){
  379. for(let j = 0; j < this.recipes[i].ingredients.length; j++){
  380. if(this.recipes[i].ingredients[j].ingredient === ingredient){
  381. recipes.push(this.recipes[i]);
  382. }
  383. }
  384. }
  385. return recipes;
  386. }
  387. }
  388. module.exports = Merchant;