327 lines
8.0 KiB
PHP
327 lines
8.0 KiB
PHP
<?php
|
|
|
|
abstract class stDeliveryProductInfoAbstract
|
|
{
|
|
/**
|
|
* Waluta
|
|
* @var stCurrencyModelInterface
|
|
*/
|
|
protected $currency;
|
|
|
|
/**
|
|
*
|
|
* @var stConfig
|
|
*/
|
|
protected $config;
|
|
|
|
private $initialized = false;
|
|
|
|
/**
|
|
* Łączna kwota produktów w danej walucie
|
|
* @var float|string
|
|
*/
|
|
private $totalAmountWithCurrency = 0;
|
|
|
|
/**
|
|
* Łączba waga produktów
|
|
* @var float|string
|
|
*/
|
|
private $totalWeight = 0;
|
|
|
|
/**
|
|
* Łączna ilość produktów
|
|
* @var int
|
|
*/
|
|
private $totalQuantity = 0;
|
|
|
|
/**
|
|
* Maksymalna szerokość produktów
|
|
* @var float|string
|
|
*/
|
|
private $maxWidth = 0;
|
|
|
|
/**
|
|
* Maksymalna wysokość produktów
|
|
* @var float|string
|
|
*/
|
|
private $maxHeight = 0;
|
|
|
|
/**
|
|
* Maksymalna głębokość produktów
|
|
* @var float|string
|
|
*/
|
|
private $maxDepth = 0;
|
|
|
|
/**
|
|
* Maksymalna wysokość podatku VAT w koszyku
|
|
* @var Tax|null
|
|
*/
|
|
private $maxTax = null;
|
|
|
|
/**
|
|
* Łączna objętość produktów
|
|
* @var float|string
|
|
*/
|
|
private $totalVolume = 0;
|
|
/**
|
|
* Łączna dodatkowa cena za dostawę
|
|
* @var float|string
|
|
*/
|
|
private $totalDeliveryPrice = 0;
|
|
|
|
/**
|
|
* Domyślne dozwolone dostawy
|
|
* @var mixed
|
|
*/
|
|
private $defaultAllowedDeliveries = [];
|
|
|
|
/**
|
|
* Dozwolone dostawy
|
|
* @var array|null
|
|
*/
|
|
private $allowedDeliveries = [];
|
|
private $allowedDeliveriesModified = false;
|
|
|
|
/**
|
|
* Konstruktor
|
|
* @param Currency $currency Waluta
|
|
*/
|
|
public function __construct(stCurrencyModelInterface $currency)
|
|
{
|
|
$this->currency = $currency;
|
|
$this->defaultAllowedDeliveries = array_values(DeliveryPeer::retrieveAllowedIdsCached());
|
|
$this->allowedDeliveries = $this->defaultAllowedDeliveries;
|
|
$this->config = stConfig::getInstance('stDeliveryBackend');
|
|
}
|
|
|
|
/**
|
|
* Zwraca łączny koszt brutto produktów
|
|
*
|
|
* @return float|string
|
|
*/
|
|
final public function getTotalAmount(bool $withCurrency = false)
|
|
{
|
|
$this->doInitialize();
|
|
return $withCurrency ? $this->totalAmountWithCurrency : $this->currency->exchange($this->totalAmountWithCurrency, true);
|
|
}
|
|
|
|
/**
|
|
* Zwraca łączną wagę produktów
|
|
* @return float|string
|
|
*/
|
|
final public function getTotalWeight()
|
|
{
|
|
$this->doInitialize();
|
|
return $this->totalWeight;
|
|
}
|
|
|
|
/**
|
|
* Zwraca łączną ilość produktów
|
|
* @return int
|
|
*/
|
|
final public function getTotalQuantity(): int
|
|
{
|
|
$this->doInitialize();
|
|
return $this->totalQuantity;
|
|
}
|
|
|
|
/**
|
|
* Zwraca maksymalną szerokość produktów
|
|
* @return float|string
|
|
*/
|
|
final public function getMaxWidth()
|
|
{
|
|
$this->doInitialize();
|
|
return $this->maxWidth;
|
|
}
|
|
|
|
/**
|
|
* Zwraca maksymalną wysokość produktów
|
|
* @return float|string
|
|
*/
|
|
final public function getMaxHeight()
|
|
{
|
|
$this->doInitialize();
|
|
return $this->maxHeight;
|
|
}
|
|
|
|
/**
|
|
* Zwraca maksymalną głębokość produktów
|
|
* @return float|string
|
|
*/
|
|
final public function getMaxDepth()
|
|
{
|
|
$this->doInitialize();
|
|
return $this->maxDepth;
|
|
}
|
|
|
|
/**
|
|
* Zwraca łączną objętość produktów
|
|
* @return float|string
|
|
*/
|
|
final public function getTotalVolume()
|
|
{
|
|
$this->doInitialize();
|
|
return $this->totalVolume;
|
|
}
|
|
|
|
/**
|
|
* Zwraca łączną dodatkową cenę dostawy produktów
|
|
* @return float|string
|
|
*/
|
|
final public function getTotalDeliveryPrice()
|
|
{
|
|
$this->doInitialize();
|
|
return $this->totalDeliveryPrice;
|
|
}
|
|
|
|
/**
|
|
* Zwraca dozwolone id dostaw dla wszystkich produktów
|
|
* @return array|null
|
|
*/
|
|
final public function getAllowedDeliveries(): ?array
|
|
{
|
|
$this->doInitialize();
|
|
|
|
if (empty($this->allowedDeliveries) && !empty($this->config->get('alternate_deliveries')))
|
|
{
|
|
$this->allowedDeliveries = $this->config->get('alternate_deliveries');
|
|
}
|
|
|
|
return $this->allowedDeliveries;
|
|
}
|
|
|
|
/**
|
|
* Zwraca maksymalna stawkę VAT
|
|
* @return Tax|null
|
|
*/
|
|
public function getMaxTax(): ?Tax
|
|
{
|
|
return $this->maxTax;
|
|
}
|
|
|
|
/**
|
|
* Wymusza odświeżenie informacji o produktach
|
|
* @return void
|
|
*/
|
|
public function refresh(): void
|
|
{
|
|
$this->initialized = false;
|
|
$this->totalAmountWithCurrency = 0;
|
|
$this->totalWeight = 0;
|
|
$this->totalQuantity = 0;
|
|
$this->maxWidth = 0;
|
|
$this->maxHeight = 0;
|
|
$this->maxDepth = 0;
|
|
$this->maxTax = null;
|
|
$this->totalVolume = 0;
|
|
$this->totalDeliveryPrice = 0;
|
|
$this->allowedDeliveries = $this->defaultAllowedDeliveries;
|
|
$this->allowedDeliveriesModified = false;
|
|
}
|
|
|
|
final protected function addAmountWithCurrency($amount): void
|
|
{
|
|
$this->totalAmountWithCurrency += $amount;
|
|
}
|
|
|
|
final protected function addWeight($weight): void
|
|
{
|
|
$this->totalWeight += $weight;
|
|
}
|
|
|
|
final protected function addMaxTax(Tax $tax)
|
|
{
|
|
if (null !== $tax->getTaxRateByCountry() && (null === $this->maxTax || $this->maxTax->getTaxRateByCountry() < $tax->getTaxRateByCountry()))
|
|
{
|
|
$this->maxTax = $tax;
|
|
}
|
|
}
|
|
|
|
final protected function addQuantity(int $quantity): void
|
|
{
|
|
$this->totalQuantity += $quantity;
|
|
}
|
|
|
|
final protected function addDimensions($width, $height, $depth, int $quantity)
|
|
{
|
|
if ($this->maxWidth < $width)
|
|
{
|
|
$this->maxWidth = $width;
|
|
}
|
|
|
|
if ($this->maxHeight < $height)
|
|
{
|
|
$this->maxHeight = $height;
|
|
}
|
|
|
|
if ($this->maxDepth < $depth)
|
|
{
|
|
$this->maxDepth = $depth;
|
|
}
|
|
|
|
$volume = $this->applyDimensionMargin($width) * $this->applyDimensionMargin($height) * $this->applyDimensionMargin($depth);
|
|
$this->totalVolume += $volume * $quantity;
|
|
}
|
|
|
|
|
|
final protected function addDeliveryPrice($deliveryPrice)
|
|
{
|
|
$this->totalDeliveryPrice += $deliveryPrice;
|
|
}
|
|
|
|
final protected function addAllowedDeliveries(array $allowedDelivieries)
|
|
{
|
|
if (!$allowedDelivieries || !$allowedDelivieries['ids'])
|
|
{
|
|
$allowedDelivieries = array('mode' => 'allow', 'ids' => $this->defaultAllowedDeliveries);
|
|
}
|
|
|
|
if ($allowedDelivieries)
|
|
{
|
|
if ($allowedDelivieries['ids'])
|
|
{
|
|
$ids = array_intersect(DeliveryPeer::retrieveIdsCached(), $allowedDelivieries['ids']);
|
|
|
|
if ($allowedDelivieries['mode'] == 'allow' && null !== $this->allowedDeliveries && $ids)
|
|
{
|
|
$this->allowedDeliveries = $this->allowedDeliveries && $this->allowedDeliveriesModified ? array_intersect($this->allowedDeliveries, $ids) : $ids;
|
|
$this->allowedDeliveriesModified = true;
|
|
}
|
|
elseif ($allowedDelivieries['mode'] == 'exclude' && $this->allowedDeliveries)
|
|
{
|
|
$this->allowedDeliveries = array_intersect($this->allowedDeliveries, array_diff($this->defaultAllowedDeliveries, $ids));
|
|
$this->allowedDeliveriesModified = true;
|
|
}
|
|
}
|
|
}
|
|
|
|
if (empty($this->allowedDeliveries))
|
|
{
|
|
$this->allowedDeliveries = null;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Initializuje informacje o produktach
|
|
* @return void
|
|
*/
|
|
abstract protected function initialize(): void;
|
|
|
|
private function doInitialize(): void
|
|
{
|
|
if (!$this->initialized)
|
|
{
|
|
$this->initialized = true;
|
|
$this->initialize();
|
|
}
|
|
}
|
|
|
|
private function applyDimensionMargin($value)
|
|
{
|
|
$margin = $this->config->get('product_package_margin');
|
|
|
|
return $margin > 0 ? $value + stPrice::percentValue($value, $margin) : $value;
|
|
}
|
|
}
|