From d3e6e2ea85c85872de473fddd47f121ff6775164 Mon Sep 17 00:00:00 2001 From: Maxim Babichev Date: Thu, 10 Oct 2024 21:49:30 +0300 Subject: [PATCH 1/5] Feature New TaxInterface --- src/External/Enums/TransactionType.php | 13 ++++ src/Interfaces/MaximalTaxable.php | 4 ++ src/Interfaces/MinimalTaxable.php | 4 ++ src/Interfaces/TaxConstraintsInterface.php | 14 +++++ src/Interfaces/TaxInterface.php | 12 ++++ src/Interfaces/Taxable.php | 4 ++ src/Services/TaxCollectionService.php | 60 +++++++++++++++++++ .../TaxCollectionServiceInterface.php | 17 ++++++ 8 files changed, 128 insertions(+) create mode 100644 src/External/Enums/TransactionType.php create mode 100644 src/Interfaces/TaxConstraintsInterface.php create mode 100644 src/Interfaces/TaxInterface.php create mode 100644 src/Services/TaxCollectionService.php create mode 100644 src/Services/TaxCollectionServiceInterface.php diff --git a/src/External/Enums/TransactionType.php b/src/External/Enums/TransactionType.php new file mode 100644 index 000000000..d59f31194 --- /dev/null +++ b/src/External/Enums/TransactionType.php @@ -0,0 +1,13 @@ +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 $fee; + } +} diff --git a/src/Services/TaxCollectionServiceInterface.php b/src/Services/TaxCollectionServiceInterface.php new file mode 100644 index 000000000..11ba866a4 --- /dev/null +++ b/src/Services/TaxCollectionServiceInterface.php @@ -0,0 +1,17 @@ + Date: Thu, 10 Oct 2024 21:58:35 +0300 Subject: [PATCH 2/5] fix --- src/Services/TaxCollectionService.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Services/TaxCollectionService.php b/src/Services/TaxCollectionService.php index 3da3fbd44..c673de2eb 100644 --- a/src/Services/TaxCollectionService.php +++ b/src/Services/TaxCollectionService.php @@ -10,7 +10,7 @@ use Bavix\Wallet\Interfaces\Wallet; use Bavix\Wallet\Internal\Service\MathServiceInterface; -class TaxCollectionService implements TaxCollectionServiceInterface +final readonly class TaxCollectionService implements TaxCollectionServiceInterface { public function __construct( private MathServiceInterface $mathService, @@ -35,7 +35,7 @@ public function calculate( $feeMaximum = $wallet->getMaximumTax($type); } - $fee = '0'; + $fee = 0; if ($feePercent !== null) { $fee = $this->mathService->floor( $this->mathService->div( @@ -55,6 +55,6 @@ public function calculate( $fee = $feeMaximum; } - return $fee; + return (string) $fee; } } From 2e1ea7aad5f7964fee0bee8904e945bbf6e1f43d Mon Sep 17 00:00:00 2001 From: Github bot Date: Thu, 10 Oct 2024 19:02:13 +0000 Subject: [PATCH 3/5] autofix --- src/Services/TaxCollectionService.php | 7 ++----- src/Services/TaxCollectionServiceInterface.php | 6 +----- 2 files changed, 3 insertions(+), 10 deletions(-) diff --git a/src/Services/TaxCollectionService.php b/src/Services/TaxCollectionService.php index c673de2eb..6a5b4f1f5 100644 --- a/src/Services/TaxCollectionService.php +++ b/src/Services/TaxCollectionService.php @@ -18,11 +18,8 @@ public function __construct( ) { } - public function calculate( - TransactionType $type, - Wallet $wallet, - float|int|string $amount, - ): string { + public function calculate(TransactionType $type, Wallet $wallet, float|int|string $amount): string + { $feePercent = null; if ($wallet instanceof TaxInterface) { $feePercent = $wallet->getTaxPercent($type); diff --git a/src/Services/TaxCollectionServiceInterface.php b/src/Services/TaxCollectionServiceInterface.php index 11ba866a4..1b9e65ef1 100644 --- a/src/Services/TaxCollectionServiceInterface.php +++ b/src/Services/TaxCollectionServiceInterface.php @@ -9,9 +9,5 @@ interface TaxCollectionServiceInterface { - public function calculate( - TransactionType $type, - Wallet $wallet, - float|int|string $amount, - ): string; + public function calculate(TransactionType $type, Wallet $wallet, float|int|string $amount): string; } From c57f536f93cddc6e56abdf696eb7067175553830 Mon Sep 17 00:00:00 2001 From: Maxim Babichev Date: Thu, 10 Oct 2024 22:12:22 +0300 Subject: [PATCH 4/5] register tax collection service --- config/config.php | 3 +++ src/Services/TaxCollectionService.php | 3 +++ src/Services/TaxCollectionServiceInterface.php | 3 +++ src/Services/TaxService.php | 16 +++++++++++++++- src/WalletServiceProvider.php | 5 +++++ 5 files changed, 29 insertions(+), 1 deletion(-) diff --git a/config/config.php b/config/config.php index c4ef3f09a..2451593e6 100644 --- a/config/config.php +++ b/config/config.php @@ -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; @@ -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. diff --git a/src/Services/TaxCollectionService.php b/src/Services/TaxCollectionService.php index 6a5b4f1f5..eed2ce98d 100644 --- a/src/Services/TaxCollectionService.php +++ b/src/Services/TaxCollectionService.php @@ -10,6 +10,9 @@ use Bavix\Wallet\Interfaces\Wallet; use Bavix\Wallet\Internal\Service\MathServiceInterface; +/** + * @internal + */ final readonly class TaxCollectionService implements TaxCollectionServiceInterface { public function __construct( diff --git a/src/Services/TaxCollectionServiceInterface.php b/src/Services/TaxCollectionServiceInterface.php index 1b9e65ef1..fc7d17a58 100644 --- a/src/Services/TaxCollectionServiceInterface.php +++ b/src/Services/TaxCollectionServiceInterface.php @@ -7,6 +7,9 @@ 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; diff --git a/src/Services/TaxService.php b/src/Services/TaxService.php index adcb3175e..340ae1925 100644 --- a/src/Services/TaxService.php +++ b/src/Services/TaxService.php @@ -4,25 +4,39 @@ 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( diff --git a/src/WalletServiceProvider.php b/src/WalletServiceProvider.php index 364307b07..38122b4e1 100644 --- a/src/WalletServiceProvider.php +++ b/src/WalletServiceProvider.php @@ -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; @@ -121,6 +123,7 @@ use Illuminate\Database\Events\TransactionRolledBack; use Illuminate\Support\Facades\Event; use Illuminate\Support\ServiceProvider; +use Laravel\Cashier\Tax; final class WalletServiceProvider extends ServiceProvider implements DeferrableProvider { @@ -285,6 +288,7 @@ 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, @@ -479,6 +483,7 @@ private function servicesProviders(): array FormatterServiceInterface::class, PrepareServiceInterface::class, PurchaseServiceInterface::class, + TaxCollectionServiceInterface::class, TaxServiceInterface::class, TransactionServiceInterface::class, TransferServiceInterface::class, From e41d587923c2efbbbe1a380f059670a9fa9e1778 Mon Sep 17 00:00:00 2001 From: Github bot Date: Thu, 10 Oct 2024 19:15:48 +0000 Subject: [PATCH 5/5] autofix --- src/Services/TaxService.php | 7 ++----- src/WalletServiceProvider.php | 6 ++++-- 2 files changed, 6 insertions(+), 7 deletions(-) diff --git a/src/Services/TaxService.php b/src/Services/TaxService.php index 340ae1925..302910a3d 100644 --- a/src/Services/TaxService.php +++ b/src/Services/TaxService.php @@ -14,6 +14,7 @@ /** * @internal + * * @deprecated use TaxCollectionServiceInterface instead. * @see TaxCollectionServiceInterface */ @@ -29,11 +30,7 @@ public function __construct( public function getFee(Wallet $wallet, float|int|string $amount): string { if ($wallet instanceof TaxInterface) { - return $this->taxCollectionService->calculate( - TransactionType::Withdraw, - $wallet, - $amount, - ); + return $this->taxCollectionService->calculate(TransactionType::Withdraw, $wallet, $amount); } // backward compatibility diff --git a/src/WalletServiceProvider.php b/src/WalletServiceProvider.php index 38122b4e1..abce6f24c 100644 --- a/src/WalletServiceProvider.php +++ b/src/WalletServiceProvider.php @@ -123,7 +123,6 @@ use Illuminate\Database\Events\TransactionRolledBack; use Illuminate\Support\Facades\Event; use Illuminate\Support\ServiceProvider; -use Laravel\Cashier\Tax; final class WalletServiceProvider extends ServiceProvider implements DeferrableProvider { @@ -288,7 +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( + TaxCollectionServiceInterface::class, + $configure['tax_collection'] ?? TaxCollectionService::class + ); $this->app->singleton(TaxServiceInterface::class, $configure['tax'] ?? TaxService::class); $this->app->singleton( TransactionServiceInterface::class,