Skip to content

Commit

Permalink
Improve capital gains subcommand output
Browse files Browse the repository at this point in the history
  • Loading branch information
tacgomes committed Dec 4, 2024
1 parent 757fdc6 commit 48eeb0b
Show file tree
Hide file tree
Showing 16 changed files with 175 additions and 101 deletions.
7 changes: 5 additions & 2 deletions src/investir/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
from investir.transaction import Acquisition, Disposal, Transaction
from investir.trhistory import TrHistory
from investir.typing import Ticker, Year
from investir.utils import boldify, tax_year_full_date, tax_year_short_date

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -352,8 +353,10 @@ def capital_gains_command(
)

if table:
leading_nl = tax_year_idx == 0 and config.logging_enabled
print(table.to_string(leading_nl=leading_nl).rstrip())
print(end="\n" if tax_year_idx == 0 and config.logging_enabled else "")
print(boldify(f"Capital Gains Tax Report {tax_year_short_date(tax_year)}"))
print(tax_year_full_date(tax_year))
print(table.to_string(leading_nl=True))
print(summary)


Expand Down
16 changes: 7 additions & 9 deletions src/investir/taxcalculator.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,14 +89,13 @@ def net_gains(self) -> Decimal:

def __str__(self) -> str:
return (
f"{'Number of disposals:':44}{self.num_disposals:>10} "
f"{'Gains in the year, before losses:':44}{gbp(self.total_gains):>10}\n"
f"{'Disposal proceeds:':44}{gbp(self.disposal_proceeds):>10} "
f"{'Losses in the year:':44}{gbp(self.total_losses):>10}\n"
f"{'Allowable costs (including purchase price):':44}"
f"{gbp(self.total_cost):>10} "
f"{'Net gain or loss:':44}"
f"{gbp(self.net_gains):>10}\n"
f"{'Number of disposals:':40}{self.num_disposals:>10} "
f"{'Gains in the year, before losses:':34}{gbp(self.total_gains):>10}\n"
f"{'Disposal proceeds:':40}{gbp(self.disposal_proceeds):>10} "
f"{'Losses in the year:':34}{gbp(self.total_losses):>10}\n"
f"{'Allowable costs (incl. purchase price):':40}"
f"{gbp(self.total_cost):>10} "
f"{'Net gain or loss:':34}{gbp(self.net_gains):>10}\n"
)


Expand Down Expand Up @@ -151,7 +150,6 @@ def get_capital_gains_table(
self._calculate_capital_gains()

table = PrettyTable(
title=f"Tax year {tax_year}-{tax_year + 1}",
field_names=(
"Date Disposed",
"Identification",
Expand Down
8 changes: 8 additions & 0 deletions src/investir/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,14 @@ def date_to_tax_year(date: datetime.date) -> Year:
return Year(tax_year_start.year - 1)


def tax_year_short_date(tax_year: Year) -> str:
return f"{tax_year}/{(tax_year + 1) % 100}"


def tax_year_full_date(tax_year: Year) -> str:
return f"6th April {tax_year} to 5th April {tax_year + 1}"


def multifilter(filters: Sequence[Callable] | None, iterable: Iterable) -> Iterable:
if not filters:
return iterable
Expand Down
26 changes: 16 additions & 10 deletions tests/test_cli/capital_gains
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
Tax year 2021-2022
Capital Gains Tax Report 2021/22
6th April 2021 to 5th April 2022

Date Disposed Identification Security Name ISIN Quantity Cost (£) Proceeds (£) Gain/loss (£)
--------------------------------------------------------------------------------------------------------------------------------
25/08/2021 Section 104 Microsoft US5949181045 13.00246544 2366.57 2952.48 585.91
Expand All @@ -7,19 +9,23 @@
13/09/2021 Bed & B. (2021-09-26) SMT GB00BLDYK618 40.00000000 482.67 558.62 75.95
13/09/2021 Section 104 SMT GB00BLDYK618 14.00000000 189.85 195.52 5.67
02/11/2021 Section 104 Apple US0378331005 26.22913238 2647.31 3653.35 1006.04
27/01/2022 Section 104 Skyworks US83088M1027 1.24584888 167.21 178.13 10.92
Number of disposals: 7 Gains in the year, before losses: £1715.67
Disposal proceeds: £8166.14 Losses in the year: £13.95
Allowable costs (including purchase price): £6464.42 Net gain or loss: £1701.72
27/01/2022 Section 104 Skyworks US83088M1027 1.24584888 167.21 178.13 10.92

Number of disposals: 7 Gains in the year, before losses: £1715.67
Disposal proceeds: £8166.14 Losses in the year: £13.95
Allowable costs (incl. purchase price): £6464.42 Net gain or loss: £1701.72

Capital Gains Tax Report 2022/23
6th April 2022 to 5th April 2023

Tax year 2022-2023
Date Disposed Identification Security Name ISIN Quantity Cost (£) Proceeds (£) Gain/loss (£)
-------------------------------------------------------------------------------------------------------------------------
20/09/2022 Section 104 Amazon US0231351067 48.31896981 3493.12 4941.53 1448.41
14/10/2022 Section 104 Microsoft US5949181045 1.32642000 324.79 319.76 -5.03
16/12/2022 Section 104 Skyworks US83088M1027 8.30000000 1094.16 979.69 -114.47
03/03/2023 Section 104 Skyworks US83088M1027 2.10000000 277.20 312.95 35.75
Number of disposals: 4 Gains in the year, before losses: £1484.16
Disposal proceeds: £6553.93 Losses in the year: £119.50
Allowable costs (including purchase price): £5189.27 Net gain or loss: £1364.66
03/03/2023 Section 104 Skyworks US83088M1027 2.10000000 277.20 312.95 35.75

Number of disposals: 4 Gains in the year, before losses: £1484.16
Disposal proceeds: £6553.93 Losses in the year: £119.50
Allowable costs (incl. purchase price): £5189.27 Net gain or loss: £1364.66

13 changes: 8 additions & 5 deletions tests/test_cli/capital_gains_amzn
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
Tax year 2022-2023
Capital Gains Tax Report 2022/23
6th April 2022 to 5th April 2023

Date Disposed Identification Security Name ISIN Quantity Cost (£) Proceeds (£) Gain/loss (£)
-------------------------------------------------------------------------------------------------------------------------
20/09/2022 Section 104 Amazon US0231351067 48.31896981 3493.12 4941.53 1448.41
Number of disposals: 1 Gains in the year, before losses: £1448.41
Disposal proceeds: £4941.53 Losses in the year: £0.00
Allowable costs (including purchase price): £3493.12 Net gain or loss: £1448.41
20/09/2022 Section 104 Amazon US0231351067 48.31896981 3493.12 4941.53 1448.41

Number of disposals: 1 Gains in the year, before losses: £1448.41
Disposal proceeds: £4941.53 Losses in the year: £0.00
Allowable costs (incl. purchase price): £3493.12 Net gain or loss: £1448.41

26 changes: 16 additions & 10 deletions tests/test_cli/capital_gains_gains
Original file line number Diff line number Diff line change
@@ -1,22 +1,28 @@
Tax year 2021-2022
Capital Gains Tax Report 2021/22
6th April 2021 to 5th April 2022

Date Disposed Identification Security Name ISIN Quantity Cost (£) Proceeds (£) Gain/loss (£)
--------------------------------------------------------------------------------------------------------------------------------
25/08/2021 Section 104 Microsoft US5949181045 13.00246544 2366.57 2952.48 585.91
13/09/2021 Same day SMT GB00BLDYK618 34.00000000 443.65 474.83 31.18
13/09/2021 Bed & B. (2021-09-26) SMT GB00BLDYK618 40.00000000 482.67 558.62 75.95
13/09/2021 Section 104 SMT GB00BLDYK618 14.00000000 189.85 195.52 5.67
02/11/2021 Section 104 Apple US0378331005 26.22913238 2647.31 3653.35 1006.04
27/01/2022 Section 104 Skyworks US83088M1027 1.24584888 167.21 178.13 10.92
Number of disposals: 6 Gains in the year, before losses: £1715.67
Disposal proceeds: £8012.93 Losses in the year: £0.00
Allowable costs (including purchase price): £6297.26 Net gain or loss: £1715.67
27/01/2022 Section 104 Skyworks US83088M1027 1.24584888 167.21 178.13 10.92

Number of disposals: 6 Gains in the year, before losses: £1715.67
Disposal proceeds: £8012.93 Losses in the year: £0.00
Allowable costs (incl. purchase price): £6297.26 Net gain or loss: £1715.67

Capital Gains Tax Report 2022/23
6th April 2022 to 5th April 2023

Tax year 2022-2023
Date Disposed Identification Security Name ISIN Quantity Cost (£) Proceeds (£) Gain/loss (£)
-------------------------------------------------------------------------------------------------------------------------
20/09/2022 Section 104 Amazon US0231351067 48.31896981 3493.12 4941.53 1448.41
03/03/2023 Section 104 Skyworks US83088M1027 2.10000000 277.20 312.95 35.75
Number of disposals: 2 Gains in the year, before losses: £1484.16
Disposal proceeds: £5254.48 Losses in the year: £0.00
Allowable costs (including purchase price): £3770.32 Net gain or loss: £1484.16
03/03/2023 Section 104 Skyworks US83088M1027 2.10000000 277.20 312.95 35.75

Number of disposals: 2 Gains in the year, before losses: £1484.16
Disposal proceeds: £5254.48 Losses in the year: £0.00
Allowable costs (incl. purchase price): £3770.32 Net gain or loss: £1484.16

26 changes: 16 additions & 10 deletions tests/test_cli/capital_gains_losses
Original file line number Diff line number Diff line change
@@ -1,17 +1,23 @@
Tax year 2021-2022
Capital Gains Tax Report 2021/22
6th April 2021 to 5th April 2022

Date Disposed Identification Security Name ISIN Quantity Cost (£) Proceeds (£) Gain/loss (£)
------------------------------------------------------------------------------------------------------------------------
01/09/2021 Section 104 Skyworks US83088M1027 1.24584888 167.16 153.21 -13.95
Number of disposals: 1 Gains in the year, before losses: £0.00
Disposal proceeds: £153.21 Losses in the year: £13.95
Allowable costs (including purchase price): £167.16 Net gain or loss: -£13.95
01/09/2021 Section 104 Skyworks US83088M1027 1.24584888 167.16 153.21 -13.95

Number of disposals: 1 Gains in the year, before losses: £0.00
Disposal proceeds: £153.21 Losses in the year: £13.95
Allowable costs (incl. purchase price): £167.16 Net gain or loss: -£13.95

Capital Gains Tax Report 2022/23
6th April 2022 to 5th April 2023

Tax year 2022-2023
Date Disposed Identification Security Name ISIN Quantity Cost (£) Proceeds (£) Gain/loss (£)
------------------------------------------------------------------------------------------------------------------------
14/10/2022 Section 104 Microsoft US5949181045 1.32642000 324.79 319.76 -5.03
16/12/2022 Section 104 Skyworks US83088M1027 8.30000000 1094.16 979.69 -114.47
Number of disposals: 2 Gains in the year, before losses: £0.00
Disposal proceeds: £1299.45 Losses in the year: £119.50
Allowable costs (including purchase price): £1418.95 Net gain or loss: -£119.50
16/12/2022 Section 104 Skyworks US83088M1027 8.30000000 1094.16 979.69 -114.47

Number of disposals: 2 Gains in the year, before losses: £0.00
Disposal proceeds: £1299.45 Losses in the year: £119.50
Allowable costs (incl. purchase price): £1418.95 Net gain or loss: -£119.50

26 changes: 16 additions & 10 deletions tests/test_cli/capital_gains_swks
Original file line number Diff line number Diff line change
@@ -1,18 +1,24 @@
Tax year 2021-2022
Capital Gains Tax Report 2021/22
6th April 2021 to 5th April 2022

Date Disposed Identification Security Name ISIN Quantity Cost (£) Proceeds (£) Gain/loss (£)
------------------------------------------------------------------------------------------------------------------------
01/09/2021 Section 104 Skyworks US83088M1027 1.24584888 167.16 153.21 -13.95
27/01/2022 Section 104 Skyworks US83088M1027 1.24584888 167.21 178.13 10.92
Number of disposals: 2 Gains in the year, before losses: £10.92
Disposal proceeds: £331.34 Losses in the year: £13.95
Allowable costs (including purchase price): £334.37 Net gain or loss: -£3.03
27/01/2022 Section 104 Skyworks US83088M1027 1.24584888 167.21 178.13 10.92

Number of disposals: 2 Gains in the year, before losses: £10.92
Disposal proceeds: £331.34 Losses in the year: £13.95
Allowable costs (incl. purchase price): £334.37 Net gain or loss: -£3.03

Capital Gains Tax Report 2022/23
6th April 2022 to 5th April 2023

Tax year 2022-2023
Date Disposed Identification Security Name ISIN Quantity Cost (£) Proceeds (£) Gain/loss (£)
------------------------------------------------------------------------------------------------------------------------
16/12/2022 Section 104 Skyworks US83088M1027 8.30000000 1094.16 979.69 -114.47
03/03/2023 Section 104 Skyworks US83088M1027 2.10000000 277.20 312.95 35.75
Number of disposals: 2 Gains in the year, before losses: £35.75
Disposal proceeds: £1292.64 Losses in the year: £114.47
Allowable costs (including purchase price): £1371.36 Net gain or loss: -£78.72
03/03/2023 Section 104 Skyworks US83088M1027 2.10000000 277.20 312.95 35.75

Number of disposals: 2 Gains in the year, before losses: £35.75
Disposal proceeds: £1292.64 Losses in the year: £114.47
Allowable costs (incl. purchase price): £1371.36 Net gain or loss: -£78.72

Loading

0 comments on commit 48eeb0b

Please sign in to comment.