70 lines
1.7 KiB
PHP
70 lines
1.7 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace KupShop\KupShopBundle\DiscountFieldDefinition;
|
|
|
|
use Query\Operator;
|
|
|
|
abstract class AbstractDiscountFieldDefinition implements DiscountFieldDefinitionInterface
|
|
{
|
|
protected ?DiscountFieldDefinitionInterface $fallback = null;
|
|
|
|
public function getField(): string
|
|
{
|
|
$discountField = $this->getColumn();
|
|
|
|
if ($priceForDiscountColumn = $this->getPriceForDiscountColumn()) {
|
|
$discountField = "IF({$priceForDiscountColumn} > 0, (100 - {$this->getPriceColumn()}*(100-{$this->getColumn()})/{$priceForDiscountColumn}), {$this->getColumn()})";
|
|
}
|
|
|
|
return $discountField;
|
|
}
|
|
|
|
public function getSpec(): callable
|
|
{
|
|
return Operator::andX($this->getSpecs() ?: '1');
|
|
}
|
|
|
|
public function setFallback(DiscountFieldDefinitionInterface $fallback): DiscountFieldDefinitionInterface
|
|
{
|
|
$this->fallback = $fallback;
|
|
|
|
return $this;
|
|
}
|
|
|
|
/**
|
|
* Discount column with real discount.
|
|
*/
|
|
protected function getColumn(): string
|
|
{
|
|
throw new \RuntimeException('Method not implemented');
|
|
}
|
|
|
|
/**
|
|
* Price column that is used when calculating discount using price for discount column.
|
|
*/
|
|
protected function getPriceColumn(): string
|
|
{
|
|
throw new \RuntimeException('Method not implemented');
|
|
}
|
|
|
|
/**
|
|
* Price for discount column that is used when calculating discount.
|
|
*
|
|
* Can be nullable so discount is not calculated using price for discount and real discount is used instead.
|
|
*/
|
|
protected function getPriceForDiscountColumn(): ?string
|
|
{
|
|
return null;
|
|
}
|
|
|
|
/**
|
|
* Query builder specs.
|
|
*/
|
|
protected function getSpecs(): array
|
|
{
|
|
return [];
|
|
}
|
|
}
|