Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Calculate and apply the instrument's tax rate percentage to transactions #44

Merged
merged 6 commits into from
Feb 17, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/DataFixtures/InstrumentFixture.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ public function load(ObjectManager $manager): void
$appl_inst->setEusipa(Instrument::EUSIPA_KNOCKOUT);
$appl_inst->setCurrency("EUR");
$appl_inst->setUnderlying($appl);
$appl_inst->setExecutionTaxRate(0.0012); // 0.12 %
$manager->persist($appl_inst);

$appl_terms = new InstrumentTerms();
Expand All @@ -48,6 +49,7 @@ public function load(ObjectManager $manager): void
$msft_inst->setEusipa(Instrument::EUSIPA_UNDERLYING);
$msft_inst->setCurrency("USD");
$msft_inst->setUnderlying($msft);
$appl_inst->setExecutionTaxRate(0.0035); // 0.35 %
$manager->persist($msft_inst);

$manager->flush();
Expand Down
16 changes: 16 additions & 0 deletions src/Entity/Instrument.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,10 @@ class Instrument
#[ORM\Column(type: "text", length: 50000, nullable: true)]
private $notes;

#[ORM\Column(type: "decimal", precision: 5, scale: 4, nullable: true, options: ["unsigned" => true])]
#[Assert\Range(min: 0, max: 1)]
private $executionTaxRate;

public function getId(): ?int
{
return $this->id;
Expand Down Expand Up @@ -200,6 +204,18 @@ public function setNotes(?string $notes): self
return $this;
}

public function getExecutionTaxRate(): ?string
{
return $this->executionTaxRate;
}

public function setExecutionTaxRate(?string $executionTaxRate): self
{
$this->executionTaxRate = $executionTaxRate;

return $this;
}

public function getUnderlying(): ?Asset
{
return $this->underlying;
Expand Down
4 changes: 3 additions & 1 deletion src/Form/ExecutionType.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ public function buildForm(FormBuilderInterface $builder, array $options): void
$user = $this->token->getToken()->getUser();

if ($data->instrument) {
$executionTaxRate = $data->instrument->getExecutionTaxRate();
$direction = $data->instrument->getDirection();
$builder->add('instrument', TextType::class, ['disabled' =>'true']);
} else {
$builder->add('instrument', EntityType::class, ['class' => Instrument::class]);
Expand Down Expand Up @@ -109,7 +111,7 @@ public function buildForm(FormBuilderInterface $builder, array $options): void
->add('execution_id', NumberType::class, ['label' => 'Execution ID', 'html5' => true, 'required' => false, 'help' => 'Execution ID used by the broker'])
->add('marketplace', TextType::class, ['required' => false, 'help' => 'Location of the exchange'])
->add('commission', MoneyType::class, ['required' => false, 'html5' => false, 'currency' => $currency, 'scale' => 4, 'help' => 'Commission cost (negative amount)'])
->add('tax', MoneyType::class, ['required' => false, 'html5' => false, 'currency' => $currency, 'scale' => 4, 'help' => 'paid tax is negative, refunded tax positive'])
->add('tax', MoneyType::class, ['required' => false, 'html5' => false, 'currency' => $currency, 'scale' => 4, 'help' => 'Paid tax is negative, refunded tax positive' . (!empty($executionTaxRate) && !empty($direction) ? ' (applying execution tax rate of -' . $executionTaxRate * 100 . '% when empty)' : '')])
->add('interest', MoneyType::class, ['required' => false, 'html5' => false, 'currency' => $currency, 'scale' => 4, 'help' => 'Paid interest (negative amount)'])
->add('notes', TextareaType::class, ['required' => false])
->add('consolidated', CheckboxType::class, ['required' => false, 'help' => 'Check if this transaction matches with your broker'])
Expand Down
2 changes: 2 additions & 0 deletions src/Form/InstrumentType.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Symfony\Component\Form\Extension\Core\Type\ResetType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\Extension\Core\Type\MoneyType;
use Symfony\Component\Form\Extension\Core\Type\TextareaType;
use Symfony\Component\Form\Extension\Core\Type\UrlType;
use Symfony\Component\OptionsResolver\OptionsResolver;
Expand Down Expand Up @@ -89,6 +90,7 @@ public function buildForm(FormBuilderInterface $builder, array $options): void
->add('terminationdate', DateType::class, ['required' => false, 'label'=>'Termination date', 'widget' => 'single_text'])
->add('url', UrlType::class, ['required' => false, 'label' => 'Instrument website'])
->add('notes', TextareaType::class, ['required' => false])
->add('executiontaxrate', TextType::class, ['required' => false, 'label'=>'Execution tax rate %', 'help' => 'Tax rate percentage applicable to executed trades (e.g. 0.12% is 0.0012)'])
->add('save', SubmitType::class, ['label' => 'Submit', 'attr' => ['class' => 'btn btn-primary']])
->add('reset', ResetType::class, ['label' => 'Reset', 'attr' => ['class' => 'btn btn-secondary']])
->add('back', ButtonType::class, ['label' => 'Back', 'attr' => ['class' => 'btn btn-secondary']])
Expand Down
5 changes: 4 additions & 1 deletion src/Form/Model/ExecutionFormModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,11 @@ private function populateTransaction(Transaction $transaction)
} else {
$transaction->setCommission(null);
}
if ($this->tax) {
if ($this->tax !== null) {
$transaction->setTax($this->tax);
} elseif($this->instrument->getExecutionTaxRate() != null && $this->direction != 0 && isset($total)) {
// apply execution tax rate of the instrument if no tax provided in the tax field
$transaction->setTax($this->direction * $total * $this->instrument->getExecutionTaxRate());
} else {
$transaction->setTax(null);
}
Expand Down
1 change: 1 addition & 0 deletions templates/instrument/edit.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
<div class="col-md-6">{{ form_row(form.issuer) }}</div>
<div class="col-md-6">{{ form_row(form.url) }}</div>
<div class="col-12">{{ form_row(form.notes) }}</div>
<div class="col-md-4">{{ form_row(form.executiontaxrate) }}</div>
</div>

<div>{{ form_errors(form) }}</div>
Expand Down
54 changes: 54 additions & 0 deletions tests/ExecutionFormModelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace App\Tests;

use App\Entity\Execution;
use App\Entity\Instrument;
use App\Entity\Transaction;
use App\Form\Model\ExecutionFormModel;
use PHPUnit\Framework\TestCase;
Expand Down Expand Up @@ -45,4 +46,57 @@ public function testRoundtrip(): void
$this->assertSame($transaction2->getCommission(), $transaction->getCommission());
$this->assertSame($transaction2->getInterest(), $transaction->getInterest());
}

public function testTaxCalculation(): void
{
$execution = new Execution();
$transaction = new Transaction();
$instrument = new Instrument();
$execution->setTransaction($transaction);
$execution->setInstrument($instrument);
$instrument->setExecutionTaxRate(0.0012); // 0.12 %

$transaction->setTime(new \DateTime());
$execution->setPrice(123);
$execution->setVolume(15);
$execution->setCurrency("EUR");

// should keep tax 0
$transaction->setTax(0);
$data = new ExecutionFormModel();
$data->fromExecution($execution);
$data->populateExecution($execution);
$this->assertSame($transaction->getTax(), strval(0));

// should keep the inserted tax value
$transaction->setTax(-1.23);
$data = new ExecutionFormModel();
$data->fromExecution($execution);
$data->populateExecution($execution);
$this->assertSame($transaction->getTax(), strval(-1.23));

// should add calculated tax (open)
$calculatedTax = is_numeric($transaction->getPortfolio()) && is_numeric($execution->getInstrument()->getExecutionTaxRate()) ? $transaction->getPortfolio() * $execution->getInstrument()->getExecutionTaxRate() : null; // -1845 * 0.0012 = '-2.214'
$transaction->setTax(null);
$data = new ExecutionFormModel();
$data->fromExecution($execution);
$data->populateExecution($execution);
$this->assertSame($transaction->getTax(), strval($calculatedTax));

// should add calculated tax (close)
$transaction->setTax(null);
$execution->setDirection(-1);
$data = new ExecutionFormModel();
$data->fromExecution($execution);
$data->populateExecution($execution);
$this->assertSame($transaction->getTax(), strval($calculatedTax));

// should add calculated tax (neutral)
$transaction->setTax(null);
$execution->setDirection(0);
$data = new ExecutionFormModel();
$data->fromExecution($execution);
$data->populateExecution($execution);
$this->assertSame($transaction->getTax(), null);
}
}