diff --git a/Changelog.md b/Changelog.md index 2a932f73..61a83d3a 100644 --- a/Changelog.md +++ b/Changelog.md @@ -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: diff --git a/src/Adjustments/Adjusters/SimpleTaxDeduction.php b/src/Adjustments/Adjusters/SimpleTaxDeduction.php new file mode 100644 index 00000000..dc1c207d --- /dev/null +++ b/src/Adjustments/Adjusters/SimpleTaxDeduction.php @@ -0,0 +1,81 @@ +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(), + ]; + } +} diff --git a/src/Adjustments/Changelog.md b/src/Adjustments/Changelog.md index 7fff6373..8de7930d 100644 --- a/src/Adjustments/Changelog.md +++ b/src/Adjustments/Changelog.md @@ -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