Merchant.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  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. oldMerchant.inventory[i].ingredient.specialUnit,
  28. oldMerchant.inventory[i].ingredient.unitSize
  29. ),
  30. quantity: oldMerchant.inventory[i].quantity
  31. });
  32. }
  33. for(let i = 0; i < oldMerchant.recipes.length; i++){
  34. this.recipes.push(new Recipe(
  35. oldMerchant.recipes[i]._id,
  36. oldMerchant.recipes[i].name,
  37. oldMerchant.recipes[i].price,
  38. oldMerchant.recipes[i].ingredients,
  39. this
  40. ));
  41. }
  42. for(let i = 0; i < transactions.length; i++){
  43. this.transactions.push(new Transaction(
  44. transactions[i]._id,
  45. transactions[i].date,
  46. transactions[i].recipes,
  47. this
  48. ));
  49. }
  50. }
  51. /*
  52. Updates all specified item in the merchant's inventory and updates the page
  53. If ingredient doesn't exist, add it
  54. ingredients = {
  55. ingredient: Ingredient object,
  56. quantity: new quantity,
  57. defaultUnit: the default unit to be displayed
  58. }
  59. remove = set true if removing
  60. isOrder = set true if this is coming from an order
  61. */
  62. editIngredients(ingredients, remove = false, isOrder = false){
  63. for(let i = 0; i < ingredients.length; i++){
  64. let isNew = true;
  65. for(let j = 0; j < this.ingredients.length; j++){
  66. if(this.ingredients[j].ingredient === ingredients[i].ingredient){
  67. if(remove){
  68. this.ingredients.splice(j, 1);
  69. }else if(!remove && isOrder){
  70. this.ingredients[j].quantity += ingredients[i].quantity;
  71. }else{
  72. this.ingredients[j].quantity = ingredients[i].quantity;
  73. }
  74. isNew = false;
  75. break;
  76. }
  77. }
  78. if(isNew){
  79. this.ingredients.push({
  80. ingredient: ingredients[i].ingredient,
  81. quantity: parseFloat(ingredients[i].quantity)
  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. this.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. revenue(indices){
  182. let total = 0;
  183. for(let i = indices[0]; i <= indices[1]; i++){
  184. for(let j = 0; j < this.transactions[i].recipes.length; j++){
  185. for(let k = 0; k < this.recipes.length; k++){
  186. if(this.transactions[i].recipes[j].recipe === this.recipes[k]){
  187. total += this.transactions[i].recipes[j].quantity * this.recipes[k].price;
  188. }
  189. }
  190. }
  191. }
  192. return total / 100;
  193. }
  194. /*
  195. Gets the quantity of each ingredient sold between two dates (dateRange)
  196. Inputs
  197. dateRange: list containing a start date and an end date
  198. Return:
  199. [{
  200. ingredient: Ingredient object,
  201. quantity: quantity of ingredient sold
  202. }]
  203. */
  204. ingredientsSold(dateRange){
  205. if(!dateRange){
  206. return false;
  207. }
  208. let recipes = this.recipesSold(dateRange);
  209. let ingredientList = [];
  210. for(let i = 0; i < recipes.length; i++){
  211. for(let j = 0; j < recipes[i].recipe.ingredients.length; j++){
  212. let exists = false;
  213. for(let k = 0; k < ingredientList.length; k++){
  214. if(ingredientList[k].ingredient === recipes[i].recipe.ingredients[j].ingredient){
  215. exists = true;
  216. ingredientList[k].quantity += recipes[i].quantity * recipes[i].recipe.ingredients[j].quantity;
  217. break;
  218. }
  219. }
  220. if(!exists){
  221. ingredientList.push({
  222. ingredient: recipes[i].recipe.ingredients[j].ingredient,
  223. quantity: recipes[i].quantity * recipes[i].recipe.ingredients[j].quantity
  224. });
  225. }
  226. }
  227. }
  228. return ingredientList;
  229. }
  230. singleIngredientSold(dateRange, ingredient){
  231. let total = 0;
  232. for(let i = dateRange[0]; i < dateRange[1]; i++){
  233. for(let j = 0; j < this.transactions[i].recipes.length; j++){
  234. for(let k = 0; k < this.transactions[i].recipes[j].recipe.ingredients.length; k++){
  235. if(this.transactions[i].recipes[j].recipe.ingredients[k].ingredient === ingredient.ingredient){
  236. total += this.transactions[i].recipes[j].recipe.ingredients[k].quantity;
  237. break;
  238. }
  239. }
  240. }
  241. }
  242. return total;
  243. }
  244. /*
  245. Gets the number of recipes sold between two dates (dateRange)
  246. Inputs:
  247. dateRange: array containing a start date and an end date
  248. Return:
  249. [{
  250. recipe: a recipe object
  251. quantity: quantity of the recipe sold
  252. }]
  253. */
  254. recipesSold(dateRange){
  255. let recipeList = [];
  256. for(let i = dateRange[0]; i <= dateRange[1]; i++){
  257. for(let j = 0; j < this.transactions[i].recipes.length; j++){
  258. let exists = false;
  259. for(let k = 0; k < recipeList.length; k++){
  260. if(recipeList[k].recipe === this.transactions[i].recipes[j].recipe){
  261. exists = true;
  262. recipeList[k].quantity += this.transactions[i].recipes[j].quantity;
  263. break;
  264. }
  265. }
  266. if(!exists){
  267. recipeList.push({
  268. recipe: this.transactions[i].recipes[j].recipe,
  269. quantity: this.transactions[i].recipes[j].quantity
  270. });
  271. }
  272. }
  273. }
  274. return recipeList;
  275. }
  276. /*
  277. Groups all of the merchant's ingredients by their category
  278. Return: [{
  279. name: category name,
  280. ingredients: [Ingredient Object]
  281. }]
  282. */
  283. categorizeIngredients(){
  284. let ingredientsByCategory = [];
  285. for(let i = 0; i < this.ingredients.length; i++){
  286. let categoryExists = false;
  287. for(let j = 0; j < ingredientsByCategory.length; j++){
  288. if(this.ingredients[i].ingredient.category === ingredientsByCategory[j].name){
  289. ingredientsByCategory[j].ingredients.push(this.ingredients[i]);
  290. categoryExists = true;
  291. break;
  292. }
  293. }
  294. if(!categoryExists){
  295. ingredientsByCategory.push({
  296. name: this.ingredients[i].ingredient.category,
  297. ingredients: [this.ingredients[i]]
  298. });
  299. }
  300. }
  301. return ingredientsByCategory;
  302. }
  303. unitizeIngredients(){
  304. let ingredientsByUnit = [];
  305. for(let i = 0; i < this.ingredients.length; i++){
  306. let unitExists = false;
  307. for(let j = 0; j < ingredientsByUnit.length; j++){
  308. if(this.ingredients[i].ingredient.unit === ingredientsByUnit[j].name){
  309. ingredientsByUnit[j].ingredients.push(this.ingredients[i]);
  310. unitExists = true;
  311. break;
  312. }
  313. }
  314. if(!unitExists){
  315. ingredientsByUnit.push({
  316. name: this.ingredients[i].ingredient.unit,
  317. ingredients: [this.ingredients[i]]
  318. });
  319. }
  320. }
  321. return ingredientsByUnit;
  322. }
  323. getRecipesForIngredient(ingredient){
  324. let recipes = [];
  325. for(let i = 0; i < this.recipes.length; i++){
  326. for(let j = 0; j < this.recipes[i].ingredients.length; j++){
  327. if(this.recipes[i].ingredients[j].ingredient === ingredient){
  328. recipes.push(this.recipes[i]);
  329. }
  330. }
  331. }
  332. return recipes;
  333. }
  334. }
  335. module.exports = Merchant;