-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
33df4d6
commit 32adb48
Showing
2 changed files
with
74 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
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; | ||
use Vanilo\Adjustments\Support\IsNotIncluded; | ||
|
||
final class PercentDiscount implements Adjuster | ||
{ | ||
use HasWriteableTitleAndDescription; | ||
use IsLockable; | ||
use IsNotIncluded; | ||
|
||
private ?float $threshold; | ||
|
||
public function __construct( | ||
private float|int $percent | ||
) { | ||
} | ||
|
||
public static function reproduceFromAdjustment(Adjustment $adjustment): Adjuster | ||
{ | ||
$data = $adjustment->getData(); | ||
|
||
return new self(floatval($data['percent'] ?? 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->calculateAmount($adjustable)); | ||
|
||
return $adjustment; | ||
} | ||
|
||
private function calculateAmount(Adjustable $adjustable): float | ||
{ | ||
return -1 * round($adjustable->preAdjustmentTotal() / 100 * $this->percent, 2); | ||
} | ||
|
||
private function getModelAttributes(Adjustable $adjustable): array | ||
{ | ||
return [ | ||
'type' => AdjustmentTypeProxy::PROMOTION(), | ||
'adjuster' => self::class, | ||
'origin' => null, | ||
'title' => $this->getTitle(), | ||
'description' => $this->getDescription(), | ||
'data' => ['percent' => $this->percent,], | ||
'amount' => $this->calculateAmount($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