-
-
Notifications
You must be signed in to change notification settings - Fork 102
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added the
SimpleTaxDeduction
adjuster
- Loading branch information
1 parent
9db58d7
commit 456d616
Showing
3 changed files
with
83 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/** | ||
* Contains the SimpleTaxDeduction class. | ||
* | ||
* @copyright Copyright (c) 2024 Vanilo UG | ||
* @author Attila Fulop | ||
* @license MIT | ||
* @since 2024-04-03 | ||
* | ||
*/ | ||
|
||
namespace Vanilo\Adjustments\Adjusters; | ||
|
||
use Vanilo\Adjustments\Contracts\Adjustable; | ||
use Vanilo\Adjustments\Contracts\Adjuster; | ||
use Vanilo\Adjustments\Contracts\Adjustment; | ||
use Vanilo\Adjustments\Models\AdjustmentProxy; | ||
use Vanilo\Adjustments\Models\AdjustmentTypeProxy; | ||
use Vanilo\Adjustments\Support\HasWriteableTitleAndDescription; | ||
use Vanilo\Adjustments\Support\IsLockable; | ||
|
||
final class SimpleTaxDeduction implements Adjuster | ||
{ | ||
use HasWriteableTitleAndDescription; | ||
use IsLockable; | ||
|
||
public function __construct( | ||
private float $rate, | ||
private bool $isIncluded = false, | ||
) { | ||
} | ||
|
||
public static function reproduceFromAdjustment(Adjustment $adjustment): Adjuster | ||
{ | ||
$data = $adjustment->getData(); | ||
|
||
return new self(floatval($data['rate'] ?? 0)); | ||
} | ||
|
||
public function createAdjustment(Adjustable $adjustable): Adjustment | ||
{ | ||
$adjustmentClass = AdjustmentProxy::modelClass(); | ||
|
||
return new $adjustmentClass($this->getModelAttributes($adjustable)); | ||
} | ||
|
||
public function recalculate(Adjustment $adjustment, Adjustable $adjustable): Adjustment | ||
{ | ||
$adjustment->setAmount($this->calculateTaxAmount($adjustable)); | ||
|
||
return $adjustment; | ||
} | ||
|
||
public function isIncluded(): bool | ||
{ | ||
return $this->isIncluded; | ||
} | ||
|
||
private function calculateTaxAmount(Adjustable $adjustable): float | ||
{ | ||
return -1 * $adjustable->preAdjustmentTotal() * $this->rate / 100; | ||
} | ||
|
||
private function getModelAttributes(Adjustable $adjustable): array | ||
{ | ||
return [ | ||
'type' => AdjustmentTypeProxy::TAX(), | ||
'adjuster' => self::class, | ||
'origin' => null, | ||
'title' => $this->getTitle(), | ||
'description' => $this->getDescription(), | ||
'data' => ['rate' => $this->rate], | ||
'amount' => $this->calculateTaxAmount($adjustable), | ||
'is_locked' => $this->isLocked(), | ||
'is_included' => $this->isIncluded(), | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters