Skip to content

Commit

Permalink
Merge pull request #202 from bocabitlabs/make-company-select-searchable
Browse files Browse the repository at this point in the history
Make company select searchable
  • Loading branch information
renefs authored Sep 3, 2024
2 parents 0f9e2d9 + db5f07e commit 62be953
Show file tree
Hide file tree
Showing 10 changed files with 145 additions and 54 deletions.
8 changes: 4 additions & 4 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
FROM python:3.10-slim

WORKDIR /usr/src/app
WORKDIR /usr/src/

ENV PYTHONPATH "${PYTHONPATH}:/usr/src"
ENV PIP_DISABLE_PIP_VERSION_CHECK 1
Expand Down Expand Up @@ -31,10 +31,10 @@ COPY poetry.lock ./
# Using Poetry to install dependencies without requiring the project main files to be present
RUN pip install poetry==${POETRY_VERSION} && poetry install --only main --no-root --no-directory

COPY ./backend $WORKDIR
COPY ./etc /usr/src/etc
COPY ./backend $WORKDIR/app
COPY ./etc ${WORKDIR}/etc

RUN chmod +x /usr/src/etc/entrypoint.sh
RUN chmod +x ${WORKDIR}/etc/entrypoint.sh

EXPOSE 8000

Expand Down
19 changes: 8 additions & 11 deletions backend/companies/data_calculators_closed.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,20 +140,17 @@ def calculate_return_on_year(self, year: int) -> Decimal:
logger.debug("Year is before last sell")
return super().calculate_return_on_year(year)

sales = (
self.shares_calculator.calculate_accumulated_return_from_sales_until_year(
year
)
investments = self.shares_calculator.calculate_total_investments_until_year(
year
)
# Commissions from all the transactions
commissions = self.shares_calculator.calculate_total_commissions_until_year(
year
)
buys = self.shares_calculator.calculate_total_buys_until_year(year)

# accum_investment = self.shares_calculator.calculate_accumulated_investment_until_year_excluding_last_sale( # noqa
# year
# )

# company_value = self.calculate_company_value_on_year(year)
sales = self.shares_calculator.calculate_total_sales_until_year(year)

total = sales - buys
total = sales - investments - commissions

return total

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -221,25 +221,43 @@ def get_shares_count_until_current_year(self) -> int:
total = self.calculate_shares_count_until_year(year)
return total

def calculate_accumulated_return_from_sales_until_year(self, year: int) -> Decimal:
def calculate_total_sales_until_year(self, year: int) -> Decimal:
total: Decimal = Decimal(0)
if year == settings.YEAR_FOR_ALL:
year = date.today().year

query = self._get_multiple_sell_transactions_query(year, use_accumulated=True)

transactions_utils = TransactionCalculator()
total = transactions_utils.calculate_transactions_amount(
total = transactions_utils.calculate_investments(
query, use_portfolio_currency=self.use_portfolio_currency
)
# logger.debug(f"Total accumulated return from sales: {total}")
return total

def calculate_total_buys_until_year(self, year: int) -> Decimal:
def calculate_total_investments_until_year(self, year: int) -> Decimal:
total: Decimal = Decimal(0)
if year == settings.YEAR_FOR_ALL:
year = date.today().year

query = self._get_multiple_buy_transactions_query(year, use_accumulated=True)

transactions_utils = TransactionCalculator()
total = transactions_utils.calculate_investments(
query, use_portfolio_currency=self.use_portfolio_currency
)
# logger.debug(f"Total accumulated return from sales: {total}")
return total

def calculate_total_commissions_until_year(self, year: int) -> Decimal:
total: Decimal = Decimal(0)
if year == settings.YEAR_FOR_ALL:
year = date.today().year

query = self._get_multiple_transactions_query(year, use_accumulated=True)

transactions_utils = TransactionCalculator()
total = transactions_utils.calculate_transactions_amount(
total = transactions_utils.calculate_commissions(
query, use_portfolio_currency=self.use_portfolio_currency
)
# logger.debug(f"Total accumulated return from sales: {total}")
Expand Down
109 changes: 88 additions & 21 deletions backend/shares_transactions/calculators/transaction_calculator.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,40 +9,72 @@


class TransactionCalculator:
def calculate_single_transaction_amount(
self,
transaction: Transaction,
use_portfolio_currency: bool = True,

def calculate_transactions_amount(
self, transactions: QuerySet[Transaction], use_portfolio_currency: bool = True
) -> Decimal:
"""Get the total amount of a transaction, calculating the price
based on the number of shares, the exchange rate and the commission
"""Get the total amount of a list of transactions
Args:
transaction (Transaction): A given Transaction object
transactions (list[Transaction]): A list of transactions
use_portfolio_currency (bool, optional): If set to portfolio,
it will use the exchange rate stored on the transaction
(Will correspond to the exchange rate of the portfolio's currency).
Defaults to "True". Otherwise, there won't be any exchange rate conversion.
Returns:
Decimal: The total amount of the transaction
Decimal: The total amount of all the transactions
"""
total: Decimal = Decimal(0)
for item in transactions:
exchange_rate: Decimal = Decimal(1)
total = Decimal(0)
if use_portfolio_currency:
exchange_rate = item.exchange_rate

exchange_rate: Decimal = Decimal(1)
total = Decimal(0)
if use_portfolio_currency:
exchange_rate = transaction.exchange_rate
if item.total_amount.amount < 0:
item.total_amount.amount *= -1

if transaction.total_amount.amount < 0:
transaction.total_amount.amount *= -1
item_total = (item.total_amount.amount * exchange_rate) + (
item.total_commission.amount * exchange_rate
)
total += item_total

return total

total = (transaction.total_amount.amount * exchange_rate) + (
transaction.total_commission.amount * exchange_rate
)
def calculate_invested_amount(
self, transactions: QuerySet, use_portfolio_currency: bool = True
) -> Decimal:
"""Get the total amount of a list of transactions
Args:
transactions (list[Transaction]): A list of transactions
use_portfolio_currency (bool, optional): If set to portfolio,
it will use the exchange rate stored on the transaction
(Will correspond to the exchange rate of the portfolio's currency).
Defaults to "True". Otherwise, there won't be any exchange rate conversion.
Returns:
Decimal: The total amount of all the transactions
"""
total: Decimal = Decimal(0)
for transaction in transactions:
exchange_rate: Decimal = Decimal(1)
total = Decimal(0)
if use_portfolio_currency:
exchange_rate = transaction.exchange_rate

if transaction.total_amount.amount < 0:
transaction.total_amount.amount *= -1

item_total = (
transaction.total_amount.amount * exchange_rate
- transaction.total_commission.amount * exchange_rate
)
total += item_total
return total

def calculate_transactions_amount(
def calculate_investments(
self, transactions: QuerySet, use_portfolio_currency: bool = True
) -> Decimal:
"""Get the total amount of a list of transactions
Expand All @@ -58,8 +90,43 @@ def calculate_transactions_amount(
Decimal: The total amount of all the transactions
"""
total: Decimal = Decimal(0)
for item in transactions:
total += self.calculate_single_transaction_amount(
item, use_portfolio_currency=use_portfolio_currency
for transaction in transactions:
exchange_rate: Decimal = Decimal(1)
if use_portfolio_currency:
exchange_rate = transaction.exchange_rate

if transaction.total_amount.amount < 0:
transaction.total_amount.amount *= -1

item_total = (
transaction.total_amount.amount * exchange_rate
- transaction.total_commission.amount * exchange_rate
)
total += item_total
return total

def calculate_commissions(
self, transactions: QuerySet, use_portfolio_currency: bool = True
) -> Decimal:
"""Get the total amount of a list of transactions
Args:
transactions (list[Transaction]): A list of transactions
use_portfolio_currency (bool, optional): If set to portfolio,
it will use the exchange rate stored on the transaction
(Will correspond to the exchange rate of the portfolio's currency).
Defaults to "True". Otherwise, there won't be any exchange rate conversion.
Returns:
Decimal: The total amount of all the transactions
"""
total: Decimal = Decimal(0)
for transaction in transactions:
exchange_rate: Decimal = Decimal(1)
total = Decimal(0)
if use_portfolio_currency:
exchange_rate = transaction.exchange_rate

item_total = transaction.total_commission.amount * exchange_rate
total += item_total
return total
4 changes: 2 additions & 2 deletions client/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion client/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "client",
"version": "1.0.4-beta.9",
"version": "1.0.4-beta.10",
"homepage": "/",
"private": true,
"dependencies": {
Expand Down
4 changes: 3 additions & 1 deletion client/src/components/TasksModal/TasksModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,9 @@ function TasksModal() {
),
withCloseButton: lastJsonMessage.status.status === "COMPLETED",
loading: lastJsonMessage.status.status === "PROGRESS",
autoClose: false,
autoClose:
lastJsonMessage.status.status === "COMPLETED" ? 5 : false,
showCloseButton: lastJsonMessage.status.status === "COMPLETED",
});
return updatedMessages;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
import { useState } from "react";
import { useTranslation } from "react-i18next";
import { Button, Group, Modal, NumberInput, Textarea } from "@mantine/core";
import {
Button,
Group,
Modal,
NumberInput,
Select,
Textarea,
} from "@mantine/core";
import { DateInput } from "@mantine/dates";
import { useForm } from "@mantine/form";
import dayjs from "dayjs";
Expand Down Expand Up @@ -106,7 +113,6 @@ export default function RightsTransactionForm({
key={form.key("totalAmount")}
suffix={` ${companyBaseCurrency}`}
decimalScale={2}
// eslint-disable-next-line react/jsx-props-no-spreading
{...form.getInputProps("totalAmount")}
/>

Expand All @@ -115,18 +121,23 @@ export default function RightsTransactionForm({
withAsterisk
label={t("Shares count")}
key={form.key("count")}
// eslint-disable-next-line react/jsx-props-no-spreading
{...form.getInputProps("count")}
/>

<Select
mt="md"
label={t("Type")}
data={["BUY", "SELL"]}
{...form.getInputProps("type")}
/>

<NumberInput
mt="md"
withAsterisk
label={t("Gross price per right")}
key={form.key("grossPricePerShare")}
suffix={` ${companyBaseCurrency}`}
decimalScale={2}
// eslint-disable-next-line react/jsx-props-no-spreading
{...form.getInputProps("grossPricePerShare")}
/>

Expand All @@ -137,7 +148,6 @@ export default function RightsTransactionForm({
key={form.key("totalCommission")}
suffix={` ${companyBaseCurrency}`}
decimalScale={2}
// eslint-disable-next-line react/jsx-props-no-spreading
{...form.getInputProps("totalCommission")}
/>

Expand All @@ -148,7 +158,6 @@ export default function RightsTransactionForm({
key={form.key("transactionDate")}
valueFormat={dateFormat}
defaultValue={new Date()}
// eslint-disable-next-line react/jsx-props-no-spreading
{...form.getInputProps("transactionDate")}
/>

Expand All @@ -163,7 +172,6 @@ export default function RightsTransactionForm({
description={`${companyBaseCurrency} ${t(
"to",
)} ${portfolioBaseCurrency}`}
// eslint-disable-next-line react/jsx-props-no-spreading
{...form.getInputProps("exchangeRate")}
/>
<Button
Expand All @@ -184,7 +192,6 @@ export default function RightsTransactionForm({
mt="md"
label={t("Notes")}
key={form.key("notes")}
// eslint-disable-next-line react/jsx-props-no-spreading
{...form.getInputProps("notes")}
/>
<Group justify="space-between" mt="md">
Expand Down
2 changes: 1 addition & 1 deletion etc/entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ if [ "$DB_TYPE" = "mysql" ] || [ "$DB_TYPE" = "postgresql" ]; then
fi

echo 'Load virtual env...'
. /usr/src/app/.venv/bin/activate
. /usr/src/.venv/bin/activate
echo 'Virtual env loaded!'

echo 'Running migrations...'
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "buho-stocks"
version = "1.0.4-beta.9"
version = "1.0.4-beta.10"
description = "Application to manage and track a stocks portfolio with dividends and return for a Buy & Hold investmentinvestment strategy."
authors = ["Rene Fernandez <[email protected]>"]
license = "GPL-3.0-or-later"
Expand Down

0 comments on commit 62be953

Please sign in to comment.