Skip to content

Commit

Permalink
Merge pull request #140 from staticdev/bug/regras-2-2-2021
Browse files Browse the repository at this point in the history
Fix bug novas regras 2-2-2021
  • Loading branch information
staticdev authored Apr 21, 2022
2 parents 2407e76 + b7a9fe4 commit 6f12ae3
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 29 deletions.
44 changes: 30 additions & 14 deletions src/irpf_investidor/b3.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,14 +68,15 @@
datetime.datetime(2020, 10, 1), datetime.datetime(2020, 11, 1), 0.00003219
),
RatePeriod(
datetime.datetime(2020, 11, 1), datetime.datetime(2020, 12, 1), 0.00003247
),
RatePeriod(
datetime.datetime(2020, 12, 1), datetime.datetime(2022, 1, 1), 0.00003020
datetime.datetime(2020, 11, 1), datetime.datetime(2021, 2, 2), 0.00003247
),
RatePeriod(datetime.datetime(2021, 2, 2), datetime.datetime(2022, 1, 1), 0.00005),
]
EMOLUMENTOS_AUCTION_RATE = 0.00007
LIQUIDACAO_RATE = 0.000275
LIQUIDACAO_PERIODS = [
RatePeriod(datetime.datetime(2019, 1, 3), datetime.datetime(2021, 2, 2), 0.000275),
RatePeriod(datetime.datetime(2021, 2, 2), datetime.datetime(2022, 1, 1), 0.00025),
]

AssetInfo = collections.namedtuple("AssetInfo", ["category", "cnpj"])

Expand Down Expand Up @@ -716,26 +717,41 @@ def get_asset_info(code: str) -> AssetInfo:
return AssetInfo("NOT_FOUND", "")


def get_trading_rate() -> float:
"""Return fixes trading rate.
def get_liquidacao_rates(dates: list[datetime.datetime]) -> list[float]:
"""Get the list of liquidação rates.
Args:
dates: list of trade days.
Returns:
float: constant float.
list of rates.
"""
return LIQUIDACAO_RATE
rates = []
last_period = 0
for date in dates:
for idx_period, period in enumerate(
LIQUIDACAO_PERIODS[last_period:], start=last_period
):
if period.start_date <= date <= period.end_date:
last_period = idx_period
rates.append(period.rate)
break
else:
sys.exit(f"Nenhum período de liquidação encontrado para a data: {date}")
return rates


def get_emoluments_rates(
def get_emolumentos_rates(
dates: list[datetime.datetime], auction_trades: list[int]
) -> list[float]:
"""Get the list of emuluments rates.
"""Get the list of emolumentos rates.
Args:
dates (List[datetime.datetime]): list of trade days.
auction_trades (List[int]): list of indexes of trades in auction.
dates: list of trade days.
auction_trades: list of indexes of trades in auction.
Returns:
List[float]: list of rates.
list of rates.
"""
rates = []
last_period = 0
Expand Down
9 changes: 5 additions & 4 deletions src/irpf_investidor/report_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,18 +182,19 @@ def calculate_taxes(df: pd.DataFrame, auction_trades: list[int]) -> pd.DataFrame
"""Calculate emolumentos and liquidação taxes based on reference year.
Args:
df (pd.DataFrame): grouped trades.
auction_trades (List[int]): list of auction trades.
df: grouped trades.
auction_trades: list of auction trades.
Returns:
pd.DataFrame: trades with two new columns of calculated taxes.
"""
df["Liquidação (R$)"] = (
df["Valor Total (R$)"] * irpf_investidor.b3.get_trading_rate()
df["Valor Total (R$)"]
* irpf_investidor.b3.get_liquidacao_rates(df["Data Negócio"].array)
).apply(round_down_money)
df["Emolumentos (R$)"] = (
df["Valor Total (R$)"]
* irpf_investidor.b3.get_emoluments_rates(
* irpf_investidor.b3.get_emolumentos_rates(
df["Data Negócio"].array, auction_trades
)
).apply(round_down_money)
Expand Down
31 changes: 22 additions & 9 deletions tests/test_b3.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,19 +40,32 @@ def test_get_asset_info_not_found() -> None:
assert asset_info.category == "NOT_FOUND"


def test_get_trading_rate() -> None:
"""Return fixed float value."""
assert b3.get_trading_rate() == 0.000275
def test_get_liquidacao_rates_error() -> None:
"""Raise `SystemExit` when date is not found."""
series = [datetime.datetime(1930, 2, 20)]
with pytest.raises(SystemExit):
assert b3.get_liquidacao_rates(series)


def test_get_liquidacao_rates_success() -> None:
"""Return date rates."""
series = [
datetime.datetime(2019, 2, 20),
datetime.datetime(2021, 12, 31),
]
expected = [0.000275, 0.00025]
result = b3.get_liquidacao_rates(series)
assert result == expected


def test_get_emoluments_rates_error() -> None:
def test_get_emolumentos_rates_error() -> None:
"""Raise `SystemExit` when date is not found."""
series = [datetime.datetime(1930, 2, 20)]
with pytest.raises(SystemExit):
assert b3.get_emoluments_rates(series, [])
assert b3.get_emolumentos_rates(series, [])


def test_get_emoluments_rates_sucess_no_auction() -> None:
def test_get_emolumentos_rates_sucess_no_auction() -> None:
"""Return date rates."""
series = [
datetime.datetime(2019, 2, 20),
Expand All @@ -61,11 +74,11 @@ def test_get_emoluments_rates_sucess_no_auction() -> None:
datetime.datetime(2019, 12, 31),
]
expected = [0.00004032, 0.00004157, 0.00004408, 0.00003802]
result = b3.get_emoluments_rates(series, [])
result = b3.get_emolumentos_rates(series, [])
assert result == expected


def test_get_emoluments_rates_sucess_with_auction() -> None:
def test_get_emolumentos_rates_sucess_with_auction() -> None:
"""Return date rates and auction rates."""
series = [
datetime.datetime(2019, 2, 20),
Expand All @@ -74,7 +87,7 @@ def test_get_emoluments_rates_sucess_with_auction() -> None:
datetime.datetime(2019, 12, 31),
]
expected = [0.00004032, 0.00007, 0.00007, 0.00003802]
result = b3.get_emoluments_rates(series, [1, 2])
result = b3.get_emolumentos_rates(series, [1, 2])
assert result == expected


Expand Down
4 changes: 2 additions & 2 deletions tests/test_report_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -208,9 +208,9 @@ def test_group_trades() -> None:

def test_calculate_taxes_2019(mocker: MockerFixture) -> None:
"""Return calculated taxes."""
mocker.patch("irpf_investidor.b3.get_trading_rate", return_value=0.000275)
mocker.patch("irpf_investidor.b3.get_liquidacao_rates", return_value=0.000275)
mocker.patch(
"irpf_investidor.b3.get_emoluments_rates",
"irpf_investidor.b3.get_emolumentos_rates",
return_value=[0.00004105, 0.00004105, 0.00004105],
)
df = pd.DataFrame(
Expand Down

0 comments on commit 6f12ae3

Please sign in to comment.