Skip to content

Commit

Permalink
Added the SimpleTaxDeduction adjuster
Browse files Browse the repository at this point in the history
  • Loading branch information
fulopattila122 committed Apr 3, 2024
1 parent 9db58d7 commit 456d616
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 0 deletions.
1 change: 1 addition & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
- Removed the Vanilo v2 `Framework` namespace compatibility layer
- Removed the throwing of `CartUpdated` event when destroying a cart (`CartDeleting` and `CartDeleted` remains)
- Removed the deprecated `BuyableImageSpatieV7` and `BuyableImageSpatieV8` traits
- Added the `SimpleTaxDeduction` adjuster
- Added Cart item configuration support (different configurations constitute separate cart items) to the `Cart::addItem()` method
- Added the `currency` field to the orders table
- Added the following fields to the Channel model/table:
Expand Down
81 changes: 81 additions & 0 deletions src/Adjustments/Adjusters/SimpleTaxDeduction.php
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(),
];
}
}
1 change: 1 addition & 0 deletions src/Adjustments/Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
- Added Laravel 11 Support
- Changed minimum Laravel version to v10.38.2
- Changed minimal Enum requirement to v4.2
- Added the `SimpleTaxDeduction` adjuster
- Added the `AdjusterAliases` class that for decoupling FQCNs from the database
- Added automatic mapping of adjuster FQCN <-> aliases when saving an adjustment into the DB and when calling the `getAdjuster()` method
- Added the `mapInto()` method to the `RelationAdjustmentCollection` class, which forwards the call to the underlying Eloquent collection
Expand Down

0 comments on commit 456d616

Please sign in to comment.