Merchant.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  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", "ft"],
  16. other: ["each", "bottle"]
  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 < this.ingredients.length; j++){
  64. if(this.ingredients[j].ingredient === ingredients[i].ingredient){
  65. if(remove){
  66. this.ingredients.splice(j, 1);
  67. }else if(!remove && isOrder){
  68. this.ingredients[j].quantity += ingredients[i].quantity;
  69. }else{
  70. this.ingredients[j].quantity = ingredients[i].quantity;
  71. }
  72. isNew = false;
  73. break;
  74. }
  75. }
  76. if(isNew){
  77. this.ingredients.push({
  78. ingredient: ingredients[i].ingredient,
  79. quantity: parseFloat(ingredients[i].quantity)
  80. });
  81. }
  82. }
  83. controller.updateData("ingredient");
  84. controller.closeSidebar();
  85. }
  86. /*
  87. Updates a recipe in the merchants list of recipes
  88. Can create, edit or remove
  89. recipe = [Recipe object]
  90. remove = will remove recipe when true
  91. */
  92. editRecipes(recipes, remove = false){
  93. let isNew = true;
  94. for(let i = 0; i < recipes.length; i++){
  95. for(let j = 0; j < this.recipes.length; j++){
  96. if(recipes[i] === this.recipes[j]){
  97. if(remove){
  98. this.recipes.splice(j, 1);
  99. }else{
  100. this.recipes[j] = recipes[i];
  101. }
  102. isNew = false;
  103. break;
  104. }
  105. }
  106. if(isNew){
  107. this.recipes.push(recipes[i]);
  108. }
  109. }
  110. controller.updateData("recipe");
  111. controller.closeSidebar();
  112. }
  113. /*
  114. Updates a list of orders in the merchants list of orders
  115. Create/edit/remove
  116. orders = [Order object]
  117. remove = will remove order when true
  118. */
  119. editOrders(orders, remove = false){
  120. for(let i = 0; i < orders.length; i++){
  121. let isNew = true;
  122. for(let j = 0; j < this.orders.length; j++){
  123. if(orders[i] === this.orders[j]){
  124. if(remove){
  125. this.orders.splice(j, 1);
  126. }else{
  127. this.orders[j] = orders[i];
  128. }
  129. isNew = false;
  130. break;
  131. }
  132. }
  133. if(isNew){
  134. this.orders.push(orders[i]);
  135. }
  136. }
  137. controller.updateData("order");
  138. controller.closeSidebar();
  139. }
  140. /*
  141. transaction = Transaction Object to add
  142. ingredients = The ingredients that need to be updated
  143. keys = ingredient ids
  144. values = quantity to change in grams
  145. remove = If true, removes transaction
  146. */
  147. editTransactions(transaction, ingredients, remove = false, ){
  148. let isNew = true;
  149. for(let i = 0; i < this.transactions.length; i++){
  150. if(this.transactions[i] === transaction){
  151. if(remove){
  152. this.transactions.splice(i, 1);
  153. }
  154. isNew = false;
  155. break;
  156. }
  157. }
  158. if(isNew){
  159. this.transactions.push(transaction);
  160. this.transactions.sort((a, b) => a.date > b.date ? -1 : 1);
  161. }
  162. let keys = Object.keys(ingredients);
  163. for(let i = 0; i < keys.length; i++){
  164. for(let j = 0; j < this.ingredients.length; j++){
  165. if(this.ingredients[j].ingredient.id === keys[i]){
  166. if(remove === false){
  167. this.ingredients[j].quantity -= ingredients[keys[i]];
  168. }else{
  169. this.ingredients[j].quantity += ingredients[keys[i]];
  170. }
  171. break;
  172. }
  173. }
  174. }
  175. controller.updateData("ingredient");
  176. controller.updateData("transaction");
  177. controller.closeSidebar();
  178. }
  179. revenue(indices){
  180. let total = 0;
  181. for(let i = indices[0]; i <= indices[1]; i++){
  182. for(let j = 0; j < this.transactions[i].recipes.length; j++){
  183. for(let k = 0; k < this.recipes.length; k++){
  184. if(this.transactions[i].recipes[j].recipe === this.recipes[k]){
  185. total += this.transactions[i].recipes[j].quantity * this.recipes[k].price;
  186. }
  187. }
  188. }
  189. }
  190. return total / 100;
  191. }
  192. /*
  193. Gets the quantity of each ingredient sold between two dates (dateRange)
  194. Inputs
  195. dateRange: list containing a start date and an end date
  196. Return:
  197. [{
  198. ingredient: Ingredient object,
  199. quantity: quantity of ingredient sold
  200. }]
  201. */
  202. ingredientsSold(dateRange){
  203. if(!dateRange){
  204. return false;
  205. }
  206. let recipes = this.recipesSold(dateRange);
  207. let ingredientList = [];
  208. for(let i = 0; i < recipes.length; i++){
  209. for(let j = 0; j < recipes[i].recipe.ingredients.length; j++){
  210. let exists = false;
  211. for(let k = 0; k < ingredientList.length; k++){
  212. if(ingredientList[k].ingredient === recipes[i].recipe.ingredients[j].ingredient){
  213. exists = true;
  214. ingredientList[k].quantity += recipes[i].quantity * recipes[i].recipe.ingredients[j].quantity;
  215. break;
  216. }
  217. }
  218. if(!exists){
  219. ingredientList.push({
  220. ingredient: recipes[i].recipe.ingredients[j].ingredient,
  221. quantity: recipes[i].quantity * recipes[i].recipe.ingredients[j].quantity
  222. });
  223. }
  224. }
  225. }
  226. return ingredientList;
  227. }
  228. singleIngredientSold(dateRange, ingredient){
  229. let total = 0;
  230. for(let i = dateRange[0]; i < dateRange[1]; i++){
  231. for(let j = 0; j < this.transactions[i].recipes.length; j++){
  232. for(let k = 0; k < this.transactions[i].recipes[j].recipe.ingredients.length; k++){
  233. if(this.transactions[i].recipes[j].recipe.ingredients[k].ingredient === ingredient.ingredient){
  234. total += this.transactions[i].recipes[j].recipe.ingredients[k].quantity;
  235. break;
  236. }
  237. }
  238. }
  239. }
  240. return total;
  241. }
  242. /*
  243. Gets the number of recipes sold between two dates (dateRange)
  244. Inputs:
  245. dateRange: array containing a start date and an end date
  246. Return:
  247. [{
  248. recipe: a recipe object
  249. quantity: quantity of the recipe sold
  250. }]
  251. */
  252. recipesSold(dateRange){
  253. let recipeList = [];
  254. for(let i = dateRange[0]; i <= dateRange[1]; i++){
  255. for(let j = 0; j < this.transactions[i].recipes.length; j++){
  256. let exists = false;
  257. for(let k = 0; k < recipeList.length; k++){
  258. if(recipeList[k].recipe === this.transactions[i].recipes[j].recipe){
  259. exists = true;
  260. recipeList[k].quantity += this.transactions[i].recipes[j].quantity;
  261. break;
  262. }
  263. }
  264. if(!exists){
  265. recipeList.push({
  266. recipe: this.transactions[i].recipes[j].recipe,
  267. quantity: this.transactions[i].recipes[j].quantity
  268. });
  269. }
  270. }
  271. }
  272. return recipeList;
  273. }
  274. /*
  275. Groups all of the merchant's ingredients by their category
  276. Return: [{
  277. name: category name,
  278. ingredients: [Ingredient Object]
  279. }]
  280. */
  281. categorizeIngredients(){
  282. let ingredientsByCategory = [];
  283. for(let i = 0; i < this.ingredients.length; i++){
  284. let categoryExists = false;
  285. for(let j = 0; j < ingredientsByCategory.length; j++){
  286. if(this.ingredients[i].ingredient.category === ingredientsByCategory[j].name){
  287. ingredientsByCategory[j].ingredients.push(this.ingredients[i]);
  288. categoryExists = true;
  289. break;
  290. }
  291. }
  292. if(!categoryExists){
  293. ingredientsByCategory.push({
  294. name: this.ingredients[i].ingredient.category,
  295. ingredients: [this.ingredients[i]]
  296. });
  297. }
  298. }
  299. return ingredientsByCategory;
  300. }
  301. unitizeIngredients(){
  302. let ingredientsByUnit = [];
  303. for(let i = 0; i < this.ingredients.length; i++){
  304. let unitExists = false;
  305. for(let j = 0; j < ingredientsByUnit.length; j++){
  306. if(this.ingredients[i].ingredient.unit === ingredientsByUnit[j].name){
  307. ingredientsByUnit[j].ingredients.push(this.ingredients[i]);
  308. unitExists = true;
  309. break;
  310. }
  311. }
  312. if(!unitExists){
  313. ingredientsByUnit.push({
  314. name: this.ingredients[i].ingredient.unit,
  315. ingredients: [this.ingredients[i]]
  316. });
  317. }
  318. }
  319. return ingredientsByUnit;
  320. }
  321. getRecipesForIngredient(ingredient){
  322. let recipes = [];
  323. for(let i = 0; i < this.recipes.length; i++){
  324. for(let j = 0; j < this.recipes[i].ingredients.length; j++){
  325. if(this.recipes[i].ingredients[j].ingredient === ingredient){
  326. recipes.push(this.recipes[i]);
  327. }
  328. }
  329. }
  330. return recipes;
  331. }
  332. }
  333. module.exports = Merchant;