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

[11.4] Feature New TaxInterface #1003

Draft
wants to merge 5 commits into
base: master
Choose a base branch
from
Draft
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
3 changes: 3 additions & 0 deletions config/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
use Bavix\Wallet\Services\PrepareService;
use Bavix\Wallet\Services\PurchaseService;
use Bavix\Wallet\Services\RegulatorService;
use Bavix\Wallet\Services\TaxCollectionService;
use Bavix\Wallet\Services\TaxService;
use Bavix\Wallet\Services\TransactionService;
use Bavix\Wallet\Services\TransferService;
Expand Down Expand Up @@ -265,6 +266,8 @@
'purchase' => PurchaseService::class,
// Service for handling tax operations.
'tax' => TaxService::class,
// Service for handling tax collection operations.
'tax_collection' => TaxCollectionService::class,
// Service for handling transaction operations.
'transaction' => TransactionService::class,
// Service for handling transfer operations.
Expand Down
13 changes: 13 additions & 0 deletions src/External/Enums/TransactionType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

declare(strict_types=1);

namespace Bavix\Wallet\External\Enums;

use Bavix\Wallet\Models\Transaction;

enum TransactionType: string
{
case Deposit = Transaction::TYPE_DEPOSIT;
case Withdraw = Transaction::TYPE_WITHDRAW;
}
4 changes: 4 additions & 0 deletions src/Interfaces/MaximalTaxable.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@

namespace Bavix\Wallet\Interfaces;

/**
* @deprecated Use TaxConstraintsInterface.
* @see TaxConstraintsInterface
*/
interface MaximalTaxable extends Taxable
{
/**
Expand Down
4 changes: 4 additions & 0 deletions src/Interfaces/MinimalTaxable.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@

namespace Bavix\Wallet\Interfaces;

/**
* @deprecated Use TaxConstraintsInterface.
* @see TaxConstraintsInterface
*/
interface MinimalTaxable extends Taxable
{
/**
Expand Down
14 changes: 14 additions & 0 deletions src/Interfaces/TaxConstraintsInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

declare(strict_types=1);

namespace Bavix\Wallet\Interfaces;

use Bavix\Wallet\External\Enums\TransactionType;

interface TaxConstraintsInterface extends TaxInterface
{
public function getMinimumTax(TransactionType $enum): float|int|null;

public function getMaximumTax(TransactionType $enum): float|int|null;
}
12 changes: 12 additions & 0 deletions src/Interfaces/TaxInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

declare(strict_types=1);

namespace Bavix\Wallet\Interfaces;

use Bavix\Wallet\External\Enums\TransactionType;

interface TaxInterface
{
public function getTaxPercent(TransactionType $enum): float|int;
}
4 changes: 4 additions & 0 deletions src/Interfaces/Taxable.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@

namespace Bavix\Wallet\Interfaces;

/**
* @deprecated Use TaxInterface.
* @see TaxInterface
*/
interface Taxable
{
/**
Expand Down
60 changes: 60 additions & 0 deletions src/Services/TaxCollectionService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php

declare(strict_types=1);

namespace Bavix\Wallet\Services;

use Bavix\Wallet\External\Enums\TransactionType;
use Bavix\Wallet\Interfaces\TaxConstraintsInterface;
use Bavix\Wallet\Interfaces\TaxInterface;
use Bavix\Wallet\Interfaces\Wallet;
use Bavix\Wallet\Internal\Service\MathServiceInterface;

/**
* @internal
*/
final readonly class TaxCollectionService implements TaxCollectionServiceInterface
{
public function __construct(
private MathServiceInterface $mathService,
private CastServiceInterface $castService,
) {
}

public function calculate(TransactionType $type, Wallet $wallet, float|int|string $amount): string
{
$feePercent = null;
if ($wallet instanceof TaxInterface) {
$feePercent = $wallet->getTaxPercent($type);
}

$feeMinimum = null;
$feeMaximum = null;
if ($wallet instanceof TaxConstraintsInterface) {
$feeMinimum = $wallet->getMinimumTax($type);
$feeMaximum = $wallet->getMaximumTax($type);
}

$fee = 0;
if ($feePercent !== null) {
$fee = $this->mathService->floor(
$this->mathService->div(
$this->mathService->mul($amount, $feePercent, 0),
100,
$this->castService->getWallet($wallet)
->decimal_places
)
);
}

if ($feeMinimum !== null && $this->mathService->compare($fee, $feeMinimum) === -1) {
$fee = $feeMinimum;
}

if ($feeMaximum !== null && $this->mathService->compare($feeMaximum, $fee) === -1) {
$fee = $feeMaximum;
}

return (string) $fee;
}
}
16 changes: 16 additions & 0 deletions src/Services/TaxCollectionServiceInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

declare(strict_types=1);

namespace Bavix\Wallet\Services;

use Bavix\Wallet\External\Enums\TransactionType;
use Bavix\Wallet\Interfaces\Wallet;

/**
* @api
*/
interface TaxCollectionServiceInterface
{
public function calculate(TransactionType $type, Wallet $wallet, float|int|string $amount): string;
}
13 changes: 12 additions & 1 deletion src/Services/TaxService.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,36 @@

namespace Bavix\Wallet\Services;

use Bavix\Wallet\External\Enums\TransactionType;
use Bavix\Wallet\Interfaces\MaximalTaxable;
use Bavix\Wallet\Interfaces\MinimalTaxable;
use Bavix\Wallet\Interfaces\Taxable;
use Bavix\Wallet\Interfaces\TaxInterface;
use Bavix\Wallet\Interfaces\Wallet;
use Bavix\Wallet\Internal\Service\MathServiceInterface;

/**
* @internal
*
* @deprecated use TaxCollectionServiceInterface instead.
* @see TaxCollectionServiceInterface
*/
final readonly class TaxService implements TaxServiceInterface
{
public function __construct(
private MathServiceInterface $mathService,
private CastServiceInterface $castService
private CastServiceInterface $castService,
private TaxCollectionServiceInterface $taxCollectionService
) {
}

public function getFee(Wallet $wallet, float|int|string $amount): string
{
if ($wallet instanceof TaxInterface) {
return $this->taxCollectionService->calculate(TransactionType::Withdraw, $wallet, $amount);
}

// backward compatibility
$fee = 0;
if ($wallet instanceof Taxable) {
$fee = $this->mathService->floor(
Expand Down
7 changes: 7 additions & 0 deletions src/WalletServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,8 @@
use Bavix\Wallet\Services\PurchaseServiceInterface;
use Bavix\Wallet\Services\RegulatorService;
use Bavix\Wallet\Services\RegulatorServiceInterface;
use Bavix\Wallet\Services\TaxCollectionService;
use Bavix\Wallet\Services\TaxCollectionServiceInterface;
use Bavix\Wallet\Services\TaxService;
use Bavix\Wallet\Services\TaxServiceInterface;
use Bavix\Wallet\Services\TransactionService;
Expand Down Expand Up @@ -285,6 +287,10 @@ private function services(array $configure, array $cache): void
$this->app->singleton(FormatterServiceInterface::class, $configure['formatter'] ?? FormatterService::class);
$this->app->singleton(PrepareServiceInterface::class, $configure['prepare'] ?? PrepareService::class);
$this->app->singleton(PurchaseServiceInterface::class, $configure['purchase'] ?? PurchaseService::class);
$this->app->singleton(
TaxCollectionServiceInterface::class,
$configure['tax_collection'] ?? TaxCollectionService::class
);
$this->app->singleton(TaxServiceInterface::class, $configure['tax'] ?? TaxService::class);
$this->app->singleton(
TransactionServiceInterface::class,
Expand Down Expand Up @@ -479,6 +485,7 @@ private function servicesProviders(): array
FormatterServiceInterface::class,
PrepareServiceInterface::class,
PurchaseServiceInterface::class,
TaxCollectionServiceInterface::class,
TaxServiceInterface::class,
TransactionServiceInterface::class,
TransferServiceInterface::class,
Expand Down