Merchant.js 13 KB

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