| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690 |
- const Ingredient = require("./Ingredient.js");
- const Recipe = require("./Recipe.js");
- const Transaction = require("./Transaction.js");
- const Order = require("./Order.js");
- class MerchantIngredient{
- constructor(ingredient, quantity, parent){
- this._quantity = quantity;
- this._ingredient = ingredient;
- this._parent = parent;
- }
- get ingredient(){
- return this._ingredient;
- }
- set quantity(quantity){
- this._quantity = quantity;
- }
- get quantity(){
- let convertMultiplier = 1;
- switch(controller.getBaseUnit(this._ingredient.unit)){
- case "g":
- convertMultiplier = this._ingredient.convert.toMass;
- break;
- case "l":
- convertMultiplier = this._ingredient.convert.toVolume;
- break;
- case "m":
- convertMultiplier = this._ingredient.convert.toLength;
- break;
- case "bottle":
- convertMultiplier = this._ingredient.convert.toBottle;
- break;
- }
-
- return this._quantity * controller.unitMultiplier(controller.getBaseUnit(this._ingredient.unit), this._ingredient.unit) * convertMultiplier;
- }
- /*
- Takes in quantity and unit of that quantity and subtracts from the quantity on the ingredient
- quantity: Number
- unit: String
- */
- updateQuantity(quantity, unit){
- quantity *= controller.unitMultiplier(unit, controller.getBaseUnit(unit))
- switch(controller.getUnitType(this._ingredient.unit)){
- case "mass": quantity /= this._ingredient.convert.toMass; break;
- case "volume": quantity /= this._ingredient.convert.toVolume; break;
- case "length": quantity /= this._ingredient.convert.toLength; break;
- }
- this._quantity += quantity;
- }
- getQuantityDisplay(){
- return `${this.quantity.toFixed(2)} ${this._ingredient.unit.toUpperCase()}`;
- }
- /*
- Gets the quantity of a single ingredient sold between two dates
- Inputs:
- from = start Date
- to = end Date
- return: quantity sold in default unit
- */
- getSoldQuantity(from, to){
- let total = 0;
- const {start, end} = this._parent.getTransactionIndices(from, to);
- for(let i = start; i < end; i++){
- total += this._parent.transactions[i].getIngredientQuantity(this._ingredient);
- }
- return total;
- }
- }
- class Merchant{
- constructor(
- name,
- pos,
- ingredients,
- recipes,
- transactions,
- address,
- owner,
- id
- ){
- this._name = name;
- this._pos = pos;
- this._inventory = [];
- this._recipes = [];
- this._transactions = [];
- this._orders = [];
- this._address = address;
- this._owner = {
- id: owner._id,
- email: owner.email,
- merchants: owner.merchants,
- name: owner.name
- };
- this.id = id;
-
- //populate ingredients
- for(let i = 0; i < ingredients.length; i++){
- const ingredient = new Ingredient(
- ingredients[i].ingredient._id,
- ingredients[i].ingredient.name,
- ingredients[i].ingredient.category,
- ingredients[i].ingredient.unit,
- ingredients[i].ingredient.altUnit,
- ingredients[i].ingredient.ingredients,
- ingredients[i].ingredient.convert,
- this
- );
- const merchantIngredient = new MerchantIngredient(
- ingredient,
- ingredients[i].quantity,
- this
- );
- this._inventory.push(merchantIngredient);
- }
- //populate recipes
- for(let i = 0; i < recipes.length; i++){
- let ingredients = [];
- for(let j = 0; j < recipes[i].ingredients.length; j++){
- const ingredient = recipes[i].ingredients[j];
- for(let k = 0; k < this._inventory.length; k++){
- if(ingredient.ingredient === this._inventory[k].ingredient.id){
- ingredients.push({
- ingredient: this._inventory[k].ingredient.id,
- quantity: ingredient.quantity,
- unit: ingredient.unit,
- baseUnitMultiplier: ingredient.baseUnitMultiplier
- });
- break;
- }
- }
- }
- let newRecipe = new Recipe(
- recipes[i]._id,
- recipes[i].name,
- recipes[i].category,
- recipes[i].price,
- ingredients,
- this,
- recipes[i].hidden
- );
- this._recipes.push(newRecipe);
- }
- //populate transactions
- for(let i = 0; i < transactions.length; i++){
- this._transactions.push(new Transaction(
- transactions[i]._id,
- transactions[i].date,
- transactions[i].recipes,
- this
- ));
- }
- //populate orders
- let from = new Date();
- from.setDate(from.getDate() - 30);
- let data = {
- from: from,
- to: new Date(),
- ingredients: []
- };
- let loader = document.getElementById("loaderContainer");
- loader.style.display = "flex";
- fetch("/orders/get", {
- method: "post",
- headers: {
- "Content-Type": "application/json"
- },
- body: JSON.stringify(data)
- })
- .then(response => response.json())
- .then((response)=>{
- if(typeof(response) === "string"){
- controller.createBanner(response, "error");
- }else{
- this.addOrders(response);
- state.updateOrders(this._orders);
- }
- })
- .catch((err)=>{
- controller.createBanner("SOMETHING WENT WRONG. PLEASE REFRESH THE PAGE", "error");
- })
- .finally(()=>{
- loader.style.display = "none";
- });
- }
- get name(){
- return this._name;
- }
- set name(name){
- this._name = name;
- }
- get email(){
- return this._email;
- }
- set email(email){
- this._email = email;
- }
- get pos(){
- return this._pos;
- }
- get inventory(){
- return this._inventory;
- }
- get address(){
- return this._address;
- }
- set address(address){
- this._address = address;
- }
- /*
- ingredient: [{
- ingredient: {
- _id: String,
- name: String,
- category: String,
- specialUnit: String || undefined,
- }
- quantity: Number
- defaultUnit: String
- }]
- */
- addIngredients(ingredients){
- for(let i = 0; i < ingredients.length; i++){
- let ingredient = ingredients[i].ingredient;
- let quantity = ingredients[i].quantity;
- let unit = ingredients[i].ingredient.unit;
- const createdIngredient = new Ingredient(
- ingredient._id,
- ingredient.name,
- ingredient.category,
- unit,
- ingredients[i].ingredient.altUnit,
- ingredient.ingredients,
- ingredient.convert,
- this
- );
- const merchantIngredient = new MerchantIngredient(createdIngredient, quantity, this);
- this._inventory.push(merchantIngredient);
- }
- }
- removeIngredient(ingredient){
- const index = this._inventory.indexOf(ingredient);
- if(index === undefined) return false;
- this._inventory.splice(index, 1);
- }
- updateIngredients(ingredients){
- for(let i = 0; i < ingredients.length; i++){
- let inventoryItem = this.getIngredient(ingredients[i].ingredient._id);
- inventoryItem.quantity = ingredients[i].quantity;
- inventoryItem.ingredient.id = ingredients[i].ingredient._id;
- inventoryItem.ingredient.name = ingredients[i].ingredient.name;
- inventoryItem.ingredient.unit = ingredients[i].ingredient.unit;
- inventoryItem.ingredient.addIngredients(ingredients[i].ingredient.ingredients);
- }
- }
- getIngredient(id){
- for(let i = 0; i < this._inventory.length; i++){
- if(this._inventory[i].ingredient.id === id) return this._inventory[i];
- }
- }
- /*
- Groups all of the merchant's ingredients by their category
- Return: [{
- name: category name,
- ingredients: [MerchantIngredient Object]
- }]
- */
- categorizeIngredients(){
- let ingredientsByCategory = [];
- for(let i = 0; i < this._inventory.length; i++){
- let categoryExists = false;
- for(let j = 0; j < ingredientsByCategory.length; j++){
- if(this._inventory[i].ingredient.category === ingredientsByCategory[j].name){
- ingredientsByCategory[j].ingredients.push(this._inventory[i]);
- categoryExists = true;
- break;
- }
- }
- if(!categoryExists){
- ingredientsByCategory.push({
- name: this._inventory[i].ingredient.category,
- ingredients: [this._inventory[i]]
- });
- }
- }
- return ingredientsByCategory;
- }
- get recipes(){
- return this._recipes;
- }
- getRecipe(id){
- for(let i = 0; i < this._recipes.length; i++){
- if(this._recipes[i].id === id) return this._recipes[i];
- }
- return new Recipe(
- "",
- "Deleted Recipe",
- "",
- 0,
- [],
- undefined,
- true
- );
- }
- /*
- recipes: [{
- _id: String
- name: String
- price: Number
- ingredients: [{
- ingredient: String (id)
- quantity: Number
- }]
- }]
- */
- addRecipes(recipes){
- for(let i = 0; i < recipes.length; i++){
- let newRecipe = new Recipe(
- recipes[i]._id,
- recipes[i].name,
- recipes[i].category,
- recipes[i].price,
- recipes[i].ingredients,
- this,
- recipes[i].hidden
- );
- newRecipe.calculateIngredientTotals();
- this._recipes.push(newRecipe);
- }
- }
- /*
- Updates a single recipe
- recipe: Recipe
- updates: Object
- */
- updateRecipe(recipe, updates){
- recipe.name = updates.name;
- recipe.category = updates.category;
- recipe.hidden = updates.category;
- recipe.price = updates.price;
- recipe.clearIngredients();
- for(let i = 0; i < updates.ingredients.length; i++){
- newIngredient = this.getIngredient(updates.ingredients[i].ingredient);
- recipe.addIngredient(
- newIngredient.ingredient,
- updates.ingredients[i].quantity,
- updates.ingredients[i].unit,
- updates.ingredients[i].baseUnitMultiplier
- );
- }
- recipe.calculateIngredientTotals();
- }
- removeRecipe(recipe){
- const index = this._recipes.indexOf(recipe);
- if(index === undefined) return false;
- this._recipes.splice(index, 1);
- }
- /*
- Groups recipes by their categories
- return: [{
- name: String,
- recipes: [Recipe]
- }]
- */
- categorizeRecipes(){
- let categories = [];
- for(let i = 0; i < this._recipes.length; i++){
- let exists = false;
- for(let j = 0; j < categories.length; j++){
- if(this._recipes[i].category === categories[j].name){
- categories[j].recipes.push(this._recipes[i]);
- exists = true;
- break;
- }
- }
- if(exists === false){
- categories.push({
- name: this._recipes[i].category,
- recipes: [this._recipes[i]]
- });
- }
- }
- return categories;
- }
- get transactions(){
- return this._transactions;
- }
- getTransactions(from, to){
- if(merchant._transactions.length <= 0) return [];
- const {start, end} = this.getTransactionIndices(from, to);
- return this._transactions.slice(start, end);
- }
- /*
- transactions: [{
- _id: String,
- date: String (date)
- recipes: [{
- recipe: String (id)
- quantity: Number
- }]
- }]
- */
- addTransactions(transactions, isNew = false){
- for(let i = 0; i < transactions.length; i++){
- let transaction = new Transaction(
- transactions[i]._id,
- transactions[i].date,
- transactions[i].recipes,
- this
- );
- this._transactions.push(transaction);
- if(isNew === true){
- for(let j = 0; j < transaction.recipes.length; j++){
- let recipe = transaction.recipes[j].recipe;
- for(let k = 0; k < recipe.ingredients.length; k++){
- let ingredient = recipe.ingredients[k].ingredient;
- let quantity = transaction.recipes[j].quantity * recipe.ingredients[k].quantity;
- this.getIngredient(ingredient.id).updateQuantity(-quantity);
- }
- }
- }
- }
- this.transactions.sort((a, b) => (a.date > b.date) ? 1 : -1);
- }
- removeTransaction(transaction){
- for(let j = 0; j < transaction.recipes.length; j++){
- let recipe = transaction.recipes[j].recipe;
- for(let k = 0; k < recipe.ingredients.length; k++){
- let ingredient = recipe.ingredients[k].ingredient;
- let quantity = transaction.recipes[j].quantity * recipe.ingredients[k].quantity;
- this.getIngredient(ingredient.id).updateQuantity(quantity);
- }
- }
- this._transactions.splice(this._transactions.indexOf(transaction), 1);
- state.updateTransactions();
- }
- get orders(){
- return this._orders;
- }
- /*
- orders: [{
- _id: String,
- name: String,
- date: String (date)
- taxes: Number
- fees: Number
- ingredients: [{
- ingredient: String (id),
- pricePerUnit: Number
- quantity: Number
- }]
- }]
- */
- addOrders(orders, isNew = false){
- for(let i = 0; i < orders.length; i++){
- let order = new Order(
- orders[i]._id,
- orders[i].name,
- orders[i].date,
- orders[i].taxes,
- orders[i].fees,
- orders[i].ingredients,
- this
- );
- this._orders.push(order);
- if(isNew === true){
- for(let j = 0; j < order.ingredients.length; j++){
- this.getIngredient(order.ingredients[j].ingredient.id).updateQuantity(order.ingredients[j].quantity);
- }
- }
- }
- }
- removeOrder(order){
- const index = this._orders.indexOf(order);
- if(index === undefined){
- return false;
- }
- this._orders.splice(index, 1);
- for(let i = 0; i < order.ingredients.length; i++){
- for(let j = 0; j < this._inventory.length; j++){
- if(order.ingredients[i].ingredient === this._inventory[j].ingredient){
- this._inventory[j].updateQuantity(-order.ingredients[i].quantity);
- break;
- }
- }
- }
- }
- get units(){
- return this._units;
- }
- get owner(){
- return this._owner;
- }
- getRevenue(from, to = new Date()){
- const {start, end} = this.getTransactionIndices(from, to);
- let total = 0;
- for(let i = start; i < end; i++){
- for(let j = 0; j < this._transactions[i].recipes.length; j++){
- for(let k = 0; k < this.recipes.length; k++){
- if(this._transactions[i].recipes[j].recipe === this.recipes[k]){
- total += this._transactions[i].recipes[j].quantity * this.recipes[k].price;
- }
- }
- }
- }
- return total;
- }
- /*
- Gets the quantity of each ingredient sold between two dates (dateRange)
- Inputs:
- dateRange: list containing a start date and an end date
- Return:
- [{
- ingredient: Ingredient object,
- quantity: quantity of ingredient sold in default unit
- }]
- */
- getIngredientsSold(from, to = new Date()){
- let recipes = this.getRecipesSold(from, to);
- let ingredientList = [];
- for(let i = 0; i < recipes.length; i++){
- for(let j = 0; j < recipes[i].recipe.ingredients.length; j++){
- let exists = false;
- for(let k = 0; k < ingredientList.length; k++){
- if(ingredientList[k].ingredient === recipes[i].recipe.ingredients[j].ingredient){
- exists = true;
- ingredientList[k].quantity += recipes[i].quantity * recipes[i].recipe.ingredients[j].quantity;
- break;
- }
- }
- if(!exists){
- ingredientList.push({
- ingredient: recipes[i].recipe.ingredients[j].ingredient,
- quantity: recipes[i].quantity * recipes[i].recipe.ingredients[j].quantity
- });
- }
- }
- }
-
- return ingredientList;
- }
- /*
- Gets the number of recipes sold between two dates (dateRange)
- Inputs:
- dateRange: array containing a start date and an end date
- Return:
- [{
- recipe: a recipe object
- quantity: quantity of the recipe sold
- }]
- */
- getRecipesSold(from = 0, to = new Date()){
- if(from === 0) from = this._transactions[0].date;
- const {start, end} = this.getTransactionIndices(from, to);
- let recipeList = [];
- for(let i = start; i < end; i++){
- for(let j = 0; j < this._transactions[i].recipes.length; j++){
- let exists = false;
- for(let k = 0; k < recipeList.length; k++){
- if(recipeList[k].recipe === this._transactions[i].recipes[j].recipe){
- exists = true;
- recipeList[k].quantity += this._transactions[i].recipes[j].quantity;
- break;
- }
- }
- if(!exists){
- recipeList.push({
- recipe: this._transactions[i].recipes[j].recipe,
- quantity: this._transactions[i].recipes[j].quantity
- });
- }
- }
- }
- return recipeList;
- }
- getTransactionIndices(from, to){
- let start = 0;
- let end = 0;
- if(
- this._transactions.length === 0 ||
- from > this._transactions[0].date ||
- to >= this._transactions[this._transactions.length-1].date
- ){
- for(let i = this._transactions.length - 1; i >= 0; i--){
- if(this._transactions[i].date > from){
- end = i + 1;
- break;
- }
- }
-
- for(let i = 0; i < this._transactions.length; i++){
- if(this._transactions[i].date <= to){
- start = i;
- break;
- }
- }
- }
- return {start: start, end: end};
- }
- }
- module.exports = Merchant;
|