Merchant.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  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"]
  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. 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. this.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. /*
  142. transaction = Transaction Object to add
  143. ingredients = The ingredients that need to be updated
  144. keys = ingredient ids
  145. values = quantity to change in grams
  146. remove = If true, removes transaction
  147. */
  148. editTransactions(transaction, ingredients, remove = false, ){
  149. let isNew = true;
  150. for(let i = 0; i < this.transactions.length; i++){
  151. if(this.transactions[i] === transaction){
  152. if(remove){
  153. this.transactions.splice(i, 1);
  154. }
  155. isNew = false;
  156. break;
  157. }
  158. }
  159. if(isNew){
  160. this.transactions.push(transaction);
  161. this.transactions.sort((a, b) => a.date > b.date ? 1 : -1);
  162. }
  163. let keys = Object.keys(ingredients);
  164. for(let i = 0; i < keys.length; i++){
  165. for(let j = 0; j < this.ingredients.length; j++){
  166. if(this.ingredients[j].ingredient.id === keys[i]){
  167. if(remove === false){
  168. this.ingredients[j].quantity -= ingredients[keys[i]];
  169. }else{
  170. this.ingredients[j].quantity += ingredients[keys[i]];
  171. }
  172. break;
  173. }
  174. }
  175. }
  176. controller.updateData("ingredient");
  177. controller.updateData("transaction");
  178. controller.closeSidebar();
  179. }
  180. revenue(indices){
  181. let total = 0;
  182. for(let i = indices[0]; i <= indices[1]; i++){
  183. for(let j = 0; j < this.transactions[i].recipes.length; j++){
  184. for(let k = 0; k < this.recipes.length; k++){
  185. if(this.transactions[i].recipes[j].recipe === this.recipes[k]){
  186. total += this.transactions[i].recipes[j].quantity * this.recipes[k].price;
  187. }
  188. }
  189. }
  190. }
  191. return total / 100;
  192. }
  193. /*
  194. Gets the quantity of each ingredient sold between two dates (dateRange)
  195. Inputs
  196. dateRange: list containing a start date and an end date
  197. Return:
  198. [{
  199. ingredient: Ingredient object,
  200. quantity: quantity of ingredient sold
  201. }]
  202. */
  203. ingredientsSold(dateRange){
  204. if(!dateRange){
  205. return false;
  206. }
  207. let recipes = this.recipesSold(dateRange);
  208. let ingredientList = [];
  209. for(let i = 0; i < recipes.length; i++){
  210. for(let j = 0; j < recipes[i].recipe.ingredients.length; j++){
  211. let exists = false;
  212. for(let k = 0; k < ingredientList.length; k++){
  213. if(ingredientList[k].ingredient === recipes[i].recipe.ingredients[j].ingredient){
  214. exists = true;
  215. ingredientList[k].quantity += recipes[i].quantity * recipes[i].recipe.ingredients[j].quantity;
  216. break;
  217. }
  218. }
  219. if(!exists){
  220. ingredientList.push({
  221. ingredient: recipes[i].recipe.ingredients[j].ingredient,
  222. quantity: recipes[i].quantity * recipes[i].recipe.ingredients[j].quantity
  223. });
  224. }
  225. }
  226. }
  227. return ingredientList;
  228. }
  229. singleIngredientSold(dateRange, ingredient){
  230. let total = 0;
  231. for(let i = dateRange[0]; i < dateRange[1]; i++){
  232. for(let j = 0; j < this.transactions[i].recipes.length; j++){
  233. for(let k = 0; k < this.transactions[i].recipes[j].recipe.ingredients.length; k++){
  234. if(this.transactions[i].recipes[j].recipe.ingredients[k].ingredient === ingredient.ingredient){
  235. total += this.transactions[i].recipes[j].recipe.ingredients[k].quantity;
  236. break;
  237. }
  238. }
  239. }
  240. }
  241. return total;
  242. }
  243. /*
  244. Gets the number of recipes sold between two dates (dateRange)
  245. Inputs:
  246. dateRange: array containing a start date and an end date
  247. Return:
  248. [{
  249. recipe: a recipe object
  250. quantity: quantity of the recipe sold
  251. }]
  252. */
  253. recipesSold(dateRange){
  254. let recipeList = [];
  255. for(let i = dateRange[0]; i <= dateRange[1]; i++){
  256. for(let j = 0; j < this.transactions[i].recipes.length; j++){
  257. let exists = false;
  258. for(let k = 0; k < recipeList.length; k++){
  259. if(recipeList[k].recipe === this.transactions[i].recipes[j].recipe){
  260. exists = true;
  261. recipeList[k].quantity += this.transactions[i].recipes[j].quantity;
  262. break;
  263. }
  264. }
  265. if(!exists){
  266. recipeList.push({
  267. recipe: this.transactions[i].recipes[j].recipe,
  268. quantity: this.transactions[i].recipes[j].quantity
  269. });
  270. }
  271. }
  272. }
  273. return recipeList;
  274. }
  275. /*
  276. Groups all of the merchant's ingredients by their category
  277. Return: [{
  278. name: category name,
  279. ingredients: [Ingredient Object]
  280. }]
  281. */
  282. categorizeIngredients(){
  283. let ingredientsByCategory = [];
  284. for(let i = 0; i < this.ingredients.length; i++){
  285. let categoryExists = false;
  286. for(let j = 0; j < ingredientsByCategory.length; j++){
  287. if(this.ingredients[i].ingredient.category === ingredientsByCategory[j].name){
  288. ingredientsByCategory[j].ingredients.push(this.ingredients[i]);
  289. categoryExists = true;
  290. break;
  291. }
  292. }
  293. if(!categoryExists){
  294. ingredientsByCategory.push({
  295. name: this.ingredients[i].ingredient.category,
  296. ingredients: [this.ingredients[i]]
  297. });
  298. }
  299. }
  300. return ingredientsByCategory;
  301. }
  302. unitizeIngredients(){
  303. let ingredientsByUnit = [];
  304. for(let i = 0; i < this.ingredients.length; i++){
  305. let unitExists = false;
  306. for(let j = 0; j < ingredientsByUnit.length; j++){
  307. if(this.ingredients[i].ingredient.unit === ingredientsByUnit[j].name){
  308. ingredientsByUnit[j].ingredients.push(this.ingredients[i]);
  309. unitExists = true;
  310. break;
  311. }
  312. }
  313. if(!unitExists){
  314. ingredientsByUnit.push({
  315. name: this.ingredients[i].ingredient.unit,
  316. ingredients: [this.ingredients[i]]
  317. });
  318. }
  319. }
  320. return ingredientsByUnit;
  321. }
  322. getRecipesForIngredient(ingredient){
  323. let recipes = [];
  324. for(let i = 0; i < this.recipes.length; i++){
  325. for(let j = 0; j < this.recipes[i].ingredients.length; j++){
  326. if(this.recipes[i].ingredients[j].ingredient === ingredient){
  327. recipes.push(this.recipes[i]);
  328. }
  329. }
  330. }
  331. return recipes;
  332. }
  333. }
  334. module.exports = Merchant;