Files
grzanieplus.pl/plugins/stDeliveryPlugin/lib/stDeliveryFrontend.class.php
2025-03-12 17:06:23 +01:00

1321 lines
34 KiB
PHP

<?php
/**
* SOTESHOP/stDelivery
*
* Ten plik należy do aplikacji stDelivery opartej na licencji (Professional License SOTE).
* Nie zmieniaj tego pliku, jeśli chcesz korzystać z automatycznych aktualizacji oprogramowania.
* Jeśli chcesz wprowadzać swoje modyfikacje do programu, zapoznaj się z dokumentacją, jak zmieniać
* oprogramowanie bez zmiany kodu bazowego http://www.sote.pl/modifications
*
* @package stDelivery
* @subpackage libs
* @copyright SOTE (www.sote.pl)
* @license http://www.sote.pl/license/sote (Professional License SOTE)
* @version $Id: stDeliveryFrontend.class.php 7534 2010-08-11 13:48:48Z marcin $
* @author Marcin Butlak <marcin.butlak@sote.pl>
*/
/**
* Klasa zarządzająca dostawami w sklepie
*
* @author Marcin Butlak <marcin.butlak@sote.pl>
*
* @package stDelivery
* @subpackage libs
*/
class stDeliveryFrontend
{
const SESSION_NAMESPACE = 'soteshop/stDeliveryFrontend';
const SESSION_WEEKEND_DELIVERY_NAME = 'weekend_delivery';
const SESSION_EXPRESS_DELIVERY_NAME = 'express_delivery';
const SESSION_DELIVERY_COUNTRY_NAME = 'delivery_country';
/**
* Singleton
*
* @var stDeliveryFrontend
*/
protected static $instance = null;
protected
$minOrderAmount = null,
$minOrderQuantity = null,
$minOrderWeight = null,
$maxOrderAmount = null,
$maxOrderQuantity = null,
$maxOrderWeight = null;
/**
*
* @var stDeliveryProductInfoAbstract
*/
protected $productInfo = null;
/**
* Koszyk
*
* @var stBasket
*/
protected $basket;
/**
* Dyspozytor zdarzeń
*
* @var stEventDispatcher
*/
protected $dispatcher = null;
/**
* Dostawa
*
* @var Delivery
*/
protected $delivery = null;
/**
* Kraj dostawy
*
* @var Countries
*/
protected $deliveryCountry = null;
/**
* Lista Dostaw
*
* @var Delivery[]
*/
protected $deliveries = null;
/**
* Lista krajów dostawy
*
* @var Countries[]
*/
protected $deliveryCountries = null;
/**
* Konfiguracja dostaw
*
* @var stConfig
*/
protected $config;
/**
* Incjalizacja klasy stDelivery
*
* @param stBasket $basket
*/
public function initialize(stBasket $basket)
{
$this->basket = $basket;
$this->dispatcher = stEventDispatcher::getInstance();
$this->config = stConfig::getInstance('stDeliveryBackend');
}
public function clearBasketTotals()
{
$this->getProductInfo()->refresh();
}
/**
* Zwraca instancje obiektu
*
* @param stBasket $basket
* @return stDeliveryFrontend
*/
public static function getInstance(stBasket $basket)
{
if (!isset(self::$instance))
{
$class = __CLASS__;
self::$instance = new $class();
self::$instance->initialize($basket);
}
return self::$instance;
}
/**
* Usuwa z sesji domyślna dostawę oraz domyślną płatność
*/
public function clearSession()
{
$this->getUser()->getAttributeHolder()->remove('delivery', self::SESSION_NAMESPACE);
$this->getUser()->getAttributeHolder()->remove('delivery_payment', self::SESSION_NAMESPACE);
}
/**
*
* @return stUser
*/
public function getUser()
{
return $this->basket->getUser();
}
/**
* Koszyk
*
* @return stBasket
*/
public function getBasket()
{
return $this->basket;
}
/**
* Ustawia domyslna dostawe
*
* @param Delivery|int $delivery
*/
public function setDefaultDelivery($delivery)
{
if ($delivery)
{
if (!is_object($delivery))
{
$this->delivery = $this->getDeliveryById($delivery);
}
else
{
$this->delivery = $delivery;
}
$this->getUser()->setAttribute('delivery', $this->delivery ? $this->delivery->getId() : null, self::SESSION_NAMESPACE);
}
}
/**
* Ustawia domyślny kraj dostawy
*
* @param Countries|int $delivery_country
* @return void
* @throws PropelException
* @throws SQLException
*/
public function setDefaultDeliveryCountry($deliveryCountry)
{
if ($deliveryCountry)
{
if (!is_object($deliveryCountry))
{
$this->deliveryCountry = $this->getCountryById($deliveryCountry);
}
else
{
$this->deliveryCountry = $deliveryCountry instanceof Countries ? $this->deliveryCountryCallback($deliveryCountry) : $deliveryCountry;
}
$this->getUser()->setAttribute(self::SESSION_DELIVERY_COUNTRY_NAME, $this->deliveryCountry ? $this->deliveryCountry->getId() : null, self::SESSION_NAMESPACE);
$this->deliveries = null;
$this->delivery = null;
$this->getProductInfo()->refresh();
}
}
public function setWeekendDelivery($weekendDelivery)
{
$this->getUser()->setAttribute(self::SESSION_WEEKEND_DELIVERY_NAME, $weekendDelivery, self::SESSION_NAMESPACE);
}
public function setExpressDelivery($expressDelivery)
{
$this->getUser()->setAttribute(self::SESSION_EXPRESS_DELIVERY_NAME, $expressDelivery, self::SESSION_NAMESPACE);
}
public function getWeekendDelivery()
{
return $this->getUser()->getAttribute(self::SESSION_WEEKEND_DELIVERY_NAME, false, self::SESSION_NAMESPACE);
}
public function getExpressDelivery()
{
return $this->getUser()->getAttribute(self::SESSION_EXPRESS_DELIVERY_NAME, false, self::SESSION_NAMESPACE);
}
public function getIsWeekendDelivery()
{
return $this->getWeekendDelivery() && $this->getDefaultDelivery() && $this->getDefaultDelivery()->isWeekendDeliveryAvailable();
}
public function getIsExpressDelivery()
{
return $this->getExpressDelivery() && $this->getDefaultDelivery() && $this->getDefaultDelivery()->getIsExpressDelivery();
}
/**
* Pobiera listę aktywnych dostaw
*
* @return stDeliveryFrontendContainer[]
*/
public function getDeliveries()
{
if (is_null($this->deliveries))
{
$this->deliveries = $this->doSelectDeliveries();
}
return $this->deliveries;
}
/**
*
* Metoda pomocniczna pobierająca listę aktywnych dostaw
*
* @param Criteria $c Dodatkowe kryteria filtrujace liste dostaw
* @return stDeliveryFrontendContainer[]
*/
public function doSelectDeliveries(Criteria $c = null)
{
if (is_null($c))
{
$c = new Criteria();
}
else
{
$c = clone $c;
}
$deliveries = array();
if (null === $this->getProductInfo()->getAllowedDeliveries())
{
$deliveries = array();
}
else
{
$this->setBaseFilterCriteria($c);
$this->setCountriesFilterCriteria($c);
$this->setMaxFilterCriteria($c);
$c->addAscendingOrderByColumn(DeliveryPeer::POSITION);
$deliveries = DeliveryPeer::doSelect($c);
}
return array_map(array($this, 'deliveryCallback'), $deliveries);
}
/**
* Zwraca kraje dostawy
*
* @param boolean $without_max_restrictions
* @return stDeliveryCountryFrontendContainer[]
*/
public function getDeliveryCountries($without_max_restrictions = false)
{
if (is_null($this->deliveryCountries))
{
$c = new Criteria();
$c->addSelectColumn(CountriesPeer::ID);
$c->addJoin(CountriesAreaHasCountriesPeer::COUNTRIES_ID, CountriesPeer::ID);
$c->addJoin(CountriesAreaHasCountriesPeer::COUNTRIES_AREA_ID, CountriesAreaPeer::ID);
$c->addJoin(CountriesAreaPeer::ID, DeliveryPeer::COUNTRIES_AREA_ID);
$c->add(CountriesAreaPeer::IS_ACTIVE, true);
$this->setBaseFilterCriteria($c);
if (!$without_max_restrictions)
{
$this->setMaxFilterCriteria($c);
}
$c->addGroupByColumn(CountriesPeer::ID);
$rs = CountriesPeer::doSelectRS($c);
$ids = array();
while ($rs->next())
{
list($id) = $rs->getRow();
$ids[$id] = $id;
}
$countries = array();
foreach (CountriesPeer::doSelectActiveCached() as $country)
{
if (isset($ids[$country->getId()]))
{
$countries[] = $this->deliveryCountryCallback($country);
}
}
$this->deliveryCountries = $countries;
}
return $this->deliveryCountries;
}
/**
*
* Jest dostawa domyślna?
*
* @return bool
*/
public function hasDefaultDelivery()
{
return (bool) $this->getDefaultDelivery();
}
/**
*
* Są dostawy?
*
* @return bool
*/
public function hasDeliveries()
{
$deliveries = $this->getDeliveries();
return !empty($deliveries);
}
/**
* Sprawdza czy dostawy nie wykluczają się wzajemnie
*
* @return boolean
*/
public function hasValidAllowCriteria()
{
$this->getDeliveries();
$allowedDeliveries = $this->getProductInfo()->getAllowedDeliveries();
return !empty($allowedDeliveries);
}
/**
*
* Zwraca łączny koszt dostawy
*
* @return float
*/
public function getTotalDeliveryCost($with_tax = false, $with_currency = false)
{
$totalAmount = 0.00;
$delivery = $this->getDefaultDelivery();
if ($delivery)
{
$totalAmount += $delivery->getTotalCost($with_tax, $with_currency, true);
$payment = $delivery->getDefaultPayment();
if ($payment && !$payment->isFree())
{
$totalAmount += $payment->getCost($with_tax, $with_currency);
}
if ($this->getIsWeekendDelivery())
{
$totalAmount += $delivery->getWeekendDeliveryCost($with_tax, $with_currency);
}
if ($this->getIsExpressDelivery())
{
$totalAmount += $delivery->getExpressDeliveryCost($with_tax, $with_currency);
}
}
return $totalAmount > 0 ? $totalAmount : 0.00;
}
/**
* Pobiera domyslna dostawe
*
* @return stDeliveryFrontendContainer
*/
public function getDefaultDelivery()
{
if (null === $this->delivery)
{
$deliveries = $this->getDeliveries();
$default = $this->getUser()->getAttribute('delivery', null, self::SESSION_NAMESPACE);
$defaultDelivery = null;
foreach ($deliveries as $delivery)
{
if (null === $defaultDelivery && $delivery->getDelivery()->getIsDefault())
{
$defaultDelivery = $delivery;
}
if ($default)
{
if ($default == $delivery->getId())
{
$this->setDefaultDelivery($delivery);
break;
}
}
elseif ($delivery->getDelivery()->getIsDefault())
{
$this->setDefaultDelivery($delivery);
break;
}
}
if (null === $this->delivery && !empty($deliveries))
{
$this->setDefaultDelivery($defaultDelivery ? $defaultDelivery : current($deliveries));
}
}
return $this->delivery;
}
/**
* Pobiera domyslny kraj dostawy
*
* @return stDeliveryCountryFrontendContainer
*/
public function getDefaultDeliveryCountry()
{
if (is_null($this->deliveryCountry))
{
$countries = $this->getDeliveryCountries();
$default = $this->getUser()->getAttribute(self::SESSION_DELIVERY_COUNTRY_NAME, null, self::SESSION_NAMESPACE);
foreach ($countries as $country)
{
if ($default)
{
if ($default == $country->getId())
{
$this->setDefaultDeliveryCountry($country);
break;
}
}
else
{
if ($country->getDeliveryCountry()->getIsDefault())
{
$this->setDefaultDeliveryCountry($country);
break;
}
}
}
if (is_null($this->deliveryCountry) && !empty($countries))
{
$this->setDefaultDeliveryCountry(current($countries));
}
}
return $this->deliveryCountry;
}
/**
*
* Ustawia kryteria filtrowania dla dostaw - podstawowe kryteria filtrowania
*
* @param Criteria $c
*/
protected function setBaseFilterCriteria($c)
{
$c->add(DeliveryPeer::ACTIVE, true);
$this->dispatcher->notify(new sfEvent($this, 'stDeliveryFrontend.postSetBaseFilterCriteria', array('criteria' => $c)));
}
/**
* Ustawia kryteria filtrowania dla dostaw - krytuje filtrowania wedlug domyslnego kraju
*/
protected function setCountriesFilterCriteria($c)
{
if ($this->getDefaultDeliveryCountry())
{
$ca = new Criteria();
$ca->addSelectColumn(CountriesAreaPeer::ID);
$ca->addJoin(CountriesAreaHasCountriesPeer::COUNTRIES_AREA_ID, CountriesAreaPeer::ID);
$ca->add(CountriesAreaHasCountriesPeer::COUNTRIES_ID, $this->getDefaultDeliveryCountry()->getId());
$ca->add(CountriesAreaPeer::IS_ACTIVE, true);
$rs = CountriesAreaPeer::doSelectRS($ca);
while ($rs->next())
{
$row = $rs->getRow();
$ids[] = $row[0];
}
$c->add(DeliveryPeer::COUNTRIES_AREA_ID, $ids, Criteria::IN);
}
}
/**
*
* Ustawia kryteria filtrowania dla dostaw - maksymalne wartości zamówienia
*
* @param Criteria $c
*/
public function setMaxFilterCriteria($c)
{
if (!$this->basket->isEmpty())
{
DeliveryPeer::addMaxFilterCriteria($c, $this->getProductInfo());
}
}
/**
*
* Pobiera maksymalną dozwoloną kwotę zamówienia
*
* @return float
*/
public function getMaxOrderAmount()
{
if (is_null($this->maxOrderAmount))
{
$this->maxOrderAmount = $this->getValueByColumn(DeliveryPeer::MAX_ORDER_AMOUNT);
}
return $this->maxOrderAmount;
}
public function getMinOrderAmount()
{
if (is_null($this->minOrderAmount))
{
$this->minOrderAmount = $this->getValueByColumn(DeliveryPeer::MIN_ORDER_AMOUNT, false);
}
return $this->minOrderAmount;
}
/**
*
* Pobiera maksymalną dozwoloną wagę zamówienia
*
* @return float
*/
public function getMaxOrderWeight()
{
if (is_null($this->maxOrderWeight))
{
$this->maxOrderWeight = $this->getValueByColumn(DeliveryPeer::MAX_ORDER_WEIGHT);
}
return $this->maxOrderWeight;
}
public function getMinOrderWeight()
{
if (is_null($this->minOrderWeight))
{
$this->minOrderWeight = $this->getValueByColumn(DeliveryPeer::MIN_ORDER_WEIGHT, false);
}
return $this->minOrderWeight;
}
/**
*
* Pobiera maksymlaną dozwoloną ilość sztuk w zamówieniu
*
* @return integer
*/
public function getMaxOrderQuantity()
{
if (is_null($this->maxOrderQuantity))
{
$this->maxOrderQuantity = $this->getValueByColumn(DeliveryPeer::MAX_ORDER_QUANTITY);
}
return $this->maxOrderQuantity;
}
public function getMinOrderQuantity()
{
if (is_null($this->minOrderQuantity))
{
$this->minOrderQuantity = $this->getValueByColumn(DeliveryPeer::MIN_ORDER_QUANTITY, false);
}
return $this->minOrderQuantity;
}
protected function getValueByColumn($column, $max = true)
{
$con = Propel::getConnection();
$c = new Criteria();
if ($max)
{
$c->addAsColumn('max_value', sprintf('MAX(%s)', $column));
}
else
{
$c->addAsColumn('max_value', sprintf('MIN(%s)', $column));
}
$this->setBaseFilterCriteria($c);
$this->setCountriesFilterCriteria($c);
$rs = BasePeer::doSelect($c, $con);
return $rs->next() ? $rs->get(1) : 0;
}
public function getProductInfo(): stDeliveryProductInfoAbstract
{
if (null === $this->productInfo)
{
$this->productInfo = new stDeliveryBasketProductInfo($this->basket, stCurrency::getInstance(sfContext::getInstance())->get());
}
return $this->productInfo;
}
/**
*
* Metoda pomocnicza - Zwraca rozszerzony obiekt Delivery
*
* @param DeliveryHasPaymentType $delivery_payment
* @return stDeliveryFrontendContainer Obiekt rozszerzony
*/
protected function deliveryCallback($delivery)
{
$delivery = new stDeliveryFrontendContainer($this, $delivery);
return $delivery;
}
protected function deliveryCountryCallback($delivery_country)
{
$delivery_country = new stDeliveryCountryFrontendContainer($this, $delivery_country);
return $delivery_country;
}
/**
*
* Pobiera rozszerzoną dostawę na podstawie id
*
* @param integer $id Id dostawy
* @return stDeliveryFrontendContainer Rozszerzona dostawa
*/
protected function getDeliveryById($id)
{
$deliveries = $this->getDeliveries();
foreach ($deliveries as $delivery)
{
if ($delivery->getId() == $id)
{
return $delivery;
}
}
return null;
}
/**
*
* Pobiera kraj dostawy na podstawie id
*
* @param integer $id Id dostawy
* @return stDeliveryCountryFrontendContainer Kraj dostawy
*/
protected function getCountryById($id)
{
$delivery_countries = $this->getDeliveryCountries();
foreach ($delivery_countries as $delivery_country)
{
if ($delivery_country->getId() == $id)
{
return $delivery_country;
}
}
return null;
}
}
/**
* Kontener rozszerzający funkcjonalność modelu Delivery
*
* @see Delivery
*/
class stDeliveryFrontendContainer
{
/**
* @var Delivery
*/
protected $delivery = null;
protected $deliveryPayments = null;
protected $deliveryPayment = null;
/**
*
* @var stDeliveryFrontend
*/
protected $deliveryFrontend = null;
protected $additionalCost = null;
/**
* Zwraca orginalny obiekt dostawy
*
* @return Delivery
*/
public function getDelivery()
{
return $this->delivery;
}
public function __construct(stDeliveryFrontend $delivery_frontend, Delivery $delivery)
{
$this->delivery = $delivery;
$this->deliveryFrontend = $delivery_frontend;
$tax = $delivery_frontend->getProductInfo()->getMaxTax();
$this->delivery->setTax($tax);
$this->delivery->resetModified();
}
public function getAdditionalCost(bool $withTax = false, bool $withCurrency = false)
{
return $this->delivery->getAdditionaCost($this->deliveryFrontend->getProductInfo(), $withTax, $withCurrency);
}
public function getService(): ?stDeliveryTypeInterface
{
return $this->delivery->getService();
}
/**
*
* Zwraca aktualnego użytkownika
*
* @return stUser
*/
public function getUser()
{
return $this->deliveryFrontend->getUser();
}
public function getDefaultCost(bool $withTax = false, bool $withCurrency = false)
{
if ($this->isFree())
{
return '0.00';
}
if ($withTax)
{
$cost = $this->delivery->getCostBrutto($withCurrency);
}
else
{
$cost = $this->delivery->getCostNetto($withCurrency);
}
return $cost;
}
public function getExpressDeliveryOrderTimeLimit()
{
$expressDeliveryTimeLimit = stConfig::getInstance('stDeliveryBackend')->get('express_delivery_order_time_limit');
return $this->delivery->getExpressDeliveryOrderTimeLimit() ? $this->delivery->getExpressDeliveryOrderTimeLimit() : $expressDeliveryTimeLimit;
}
public function getWeekendDeliveryCost(bool $withTax = false, bool $withCurrency = false)
{
return $this->delivery->getWeekendDeliveryCost($this->deliveryFrontend->getProductInfo(), $withTax, $withCurrency);
}
public function getExpressDeliveryCost(bool $withTax = false, bool $withCurrency = false)
{
return $this->delivery->getExpressDeliveryCost($this->deliveryFrontend->getProductInfo(), $withTax, $withCurrency);
}
public function getTotalCost(bool $withTax = false, bool $withCurrency = false)
{
if ($this->isFree())
{
return 0;
}
return $this->delivery->getTotalCost($this->deliveryFrontend->getProductInfo(), $withTax, $withCurrency);
}
/**
*
* Jest darmowa?
*
* @return bool
*/
public function isFree()
{
return $this->delivery->isFree($this->deliveryFrontend->getProductInfo());
}
/**
*
* Jest domyślna?
*
* @return bool
*/
public function getIsDefault()
{
$delivery = $this->deliveryFrontend->getDefaultDelivery();
if ($delivery)
{
return $delivery->getId() == $this->delivery->getId();
}
return $this->delivery->getIsDefault();
}
/**
*
* Zwraca listę płatności dostawy
*
* @return stDeliveryPaymentFrontendContainer[]
*/
public function getDeliveryPayments()
{
if (is_null($this->deliveryPayments))
{
$c = new Criteria();
$this->setFilterCriteria($c);
$deliveryPayments = [];
$defaultPayment = $this->getUser()->getAttribute('delivery_payment', null, stDeliveryFrontend::SESSION_NAMESPACE);
$deliveryHasPayments = DeliveryHasPaymentTypePeer::doSelect($c);
/**
* @todo Dostosować przelewy24 do interfejsu stPaymentTypeInterface
*/
$api = new stPrzelewy24();
if ($api->checkPaymentConfiguration() && $api->hasChannels())
{
$additionalPaymentTypes = $api->getBasketPaymentTypes();
foreach ($additionalPaymentTypes as $paymentType)
{
if ($paymentType->getActive())
{
$additionalDeliveryPayment = new DeliveryHasPaymentType();
$additionalDeliveryPayment->setPaymentType($paymentType);
$additionalDeliveryPayment->setDelivery($this);
$additionalDeliveryPayment->resetModified();
$deliveryHasPayments[] = $additionalDeliveryPayment;
}
}
}
foreach ($deliveryHasPayments as $deliveryPayment)
{
$payment = $deliveryPayment->getPayment();
if ($payment && $payment->getActive() && $payment->checkPaymentConfiguration() && (!$payment->getHideForWholesale() || $payment->getHideForWholesale() && !stWholesalePluginListener::getWholesaleType()))
{
$currentDeliveryPayment = stDeliveryPaymentFrontendContainer::create($this->deliveryFrontend, $deliveryPayment);
if ($defaultPayment == $currentDeliveryPayment->getId())
{
$this->setDefaultPayment($currentDeliveryPayment);
}
if (null === $defaultPayment && $deliveryPayment->getIsDefault())
{
$this->setDefaultPayment($currentDeliveryPayment);
}
$deliveryPayments[] = $currentDeliveryPayment;
}
}
if (null === $this->deliveryPayment && !empty($deliveryPayments))
{
$this->setDefaultPayment(current($deliveryPayments));
}
$totalAmount = $this->deliveryFrontend->getProductInfo()->getTotalAmount(true) + $this->getTotalCost(true, true);
if (!empty($deliveryPayments) && !$this->deliveryPayment->getPayment()->checkPaymentConfiguration($totalAmount))
{
$this->setDefaultPayment(current($deliveryPayments));
}
foreach ($deliveryPayments as $index => $deliveryPayment)
{
if (!$deliveryPayment->getPayment()->checkPaymentConfiguration($totalAmount))
{
unset($deliveryPayments[$index]);
}
}
$this->deliveryPayments = $deliveryPayments;
}
return $this->deliveryPayments;
}
/**
*
* Ustawia kryteria filtrowania dla płatności
*
* @param Criteria $c
*/
protected function setFilterCriteria($c)
{
$c->add(DeliveryHasPaymentTypePeer::DELIVERY_ID, $this->getId());
$c->add(DeliveryHasPaymentTypePeer::IS_ACTIVE, true);
$c->addAscendingOrderByColumn(DeliveryHasPaymentTypePeer::ID);
}
/**
*
* Są płatności?
*
* @return bool
*/
public function hasDeliveryPayments()
{
$delivery_payments = $this->getDeliveryPayments();
return !empty($delivery_payments);
}
/**
*
* @return stDeliveryPaymentFrontendContainer
*/
public function getDefaultPayment()
{
if (null === $this->deliveryPayment)
{
$this->getDeliveryPayments();
}
return $this->deliveryPayment;
}
/**
*
* Ustawia domyślną płatność
*
* @param int|stDeliveryPaymentFrontendContainer|DeliveryHasPaymentType $deliveryPayment Id lub instancja modelu płatności w zamówieniu
*/
public function setDefaultPayment($deliveryPayment, $saveToSession = true)
{
if ($deliveryPayment)
{
if (!is_object($deliveryPayment))
{
$this->deliveryPayment = $this->getPaymentById($deliveryPayment);
}
else
{
$this->deliveryPayment = get_class($deliveryPayment) != 'stDeliveryPaymentFrontendContainer' ? stDeliveryPaymentFrontendContainer::create($this->deliveryFrontend, $deliveryPayment) : $deliveryPayment;
}
if ($saveToSession)
{
$this->getUser()->setAttribute('delivery_payment', $this->deliveryPayment ? $this->deliveryPayment->getId() : null, stDeliveryFrontend::SESSION_NAMESPACE);
}
}
}
/**
*
* Wykonuje metody obiektu Delivery nie występujące w stDeliveryFrontendContainer
*
* @param string $name Nazwa metody
* @param array $arguments Argumenty metody
* @return mixed
*/
public function __call($name, $arguments)
{
return call_user_func_array(array($this->delivery, $name), $arguments);
}
/**
*
* Pobiera rozszerzony obiekt stDeliveryPaymentFrontendContainer na podstawie id
*
* @param integer $id
* @return stDeliveryPaymentFrontendContainer
*/
protected function getPaymentById($id)
{
$payments = $this->getDeliveryPayments();
foreach ($payments as $payment)
{
if ($payment->getId() == $id)
{
return $payment;
}
}
return null;
}
}
/**
* Kontener rozszerzający funkcjonalność modelu DeliveryHasPaymentType
*
* @see DeliveryHasPaymentType
*/
class stDeliveryPaymentFrontendContainer
{
/**
*
* @var DeliveryHasPaymentType
*/
protected $deliveryPayment = null;
protected $deliveryFrontend = null;
/**
*
* Metoda pomocnicza - Zwraca rozszerzony obiekt DeliveryHasPaymentType
*
* @param DeliveryHasPaymentType $delivery_payment
* @return stDeliveryPaymentFrontendContainer Obiekt rozszerzony
*/
public static function create(stDeliveryFrontend $deliveryFrontend, DeliveryHasPaymentType $deliveryHasPaymentType)
{
$proxy = new stDeliveryPaymentFrontendContainer($deliveryFrontend, $deliveryHasPaymentType);
$proxy->setDelivery($deliveryFrontend->getDefaultDelivery());
return $proxy;
}
public function __construct(stDeliveryFrontend $deliveryFrontend, DeliveryHasPaymentType $deliveryHasPaymentType)
{
$this->deliveryFrontend = $deliveryFrontend;
$this->deliveryPayment = $deliveryHasPaymentType;
}
/**
* Zwraca orginalny obiekt platnosci dostawt
*
* @return DeliveryHasPaymentType
*/
public function getDeliveryPayment()
{
return $this->deliveryPayment;
}
/**
* Zwraca klase obslugi typu platnosci
*
* @return stPaymentTypeInterface|null
* @throws sfCacheException
*/
public function getPaymentTypeApi()
{
return $this->deliveryPayment->getPaymentType()->getApiInstance();
}
public function getId()
{
return $this->getPayment() ? $this->getPayment()->getId() : null;
}
public function getSocketName()
{
return $this->getPayment() ? 'stPayment_show_' . $this->getPayment()->getModuleName() . '_info' : null;
}
public function getSocketNameExists($type = 'component')
{
return (bool) array_key_exists($this->getSocketName(), sfConfig::get('st_socket_' . $type));
}
public function getName()
{
return $this->getPayment() ? $this->getPayment()->getName() : '';
}
public function getDescription()
{
$description = null;
if ($this->deliveryPayment->getPayment()->hasChannels())
{
$channels = [];
$totalAmount = $this->deliveryFrontend->getProductInfo()->getTotalAmount() + $this->getCost();
foreach ($this->deliveryPayment->getPayment()->getChannels($totalAmount) as $channel)
{
if ($channel['highlighted'])
{
$description .= '<div class="highlighted-payment"><span class="payment-logo"><img src="'.$channel['logo'].'" alt="'.$channel['name'].'"></span><span class="payment-name">'.$channel['description'].'</span></div>';
}
else
{
$channels[] = $channel['name'];
}
}
if (!empty($channels))
{
$description .= '<div class="text-left">'.implode(', ', $channels).'</div>';
}
}
if (null === $description)
{
$description = $this->getPayment() ? $this->getPayment()->getDescription() : '';
}
return $description;
}
public function getCost(bool $withTax = false, bool $withCurrency = false)
{
if ($this->isFree())
{
return '0.00';
}
if ($this->deliveryPayment->getCostType() == '%')
{
$totalAmount = $this->deliveryFrontend->getDefaultDelivery()->getTotalCost($withTax) + $this->deliveryFrontend->getProductInfo()->getTotalAmount();
$cost = stPrice::percentValue($totalAmount, $this->deliveryPayment->getCost());
if ($withCurrency)
{
$cost = stCurrency::exchange($cost);
}
}
elseif ($withTax)
{
$cost = $this->deliveryPayment->getCostBrutto($withCurrency);
}
else
{
$cost = $this->deliveryPayment->getCostNetto($withCurrency);
}
return $cost;
}
public function isFree()
{
$freeFrom = $this->deliveryPayment->getFreeFrom();
$totalAmount = $this->deliveryFrontend->getBasket()->getTotalAmount(true);
return $freeFrom > 0 && $totalAmount >= $freeFrom;
}
public function getIsDefault()
{
$deliveryPayment = $this->deliveryFrontend->getDefaultDelivery()->getDefaultPayment();
if ($deliveryPayment)
{
return $deliveryPayment->getId() == $this->getId();
}
return $this->deliveryPayment->getIsDefault();
}
public function hasPaymentLogo(): bool
{
return $this->getPaymentTypeApi() instanceof stPaymentTypeInterface;
}
public function getPaymentLogo(): ?string
{
if ($this->getPaymentTypeApi() instanceof stPaymentTypeInterface)
{
return $this->getPaymentTypeApi()->getLogoPath();
}
return null;
}
public function __call($name, $arguments)
{
return call_user_func_array(array($this->deliveryPayment, $name), $arguments);
}
}
/**
* Kontener rozszerzający funkcjonalność modelu Countries
*
* @mixin Countries
*/
class stDeliveryCountryFrontendContainer
{
protected
$deliveryCountry = null,
$deliveryFrontend = null;
public function __construct($delivery_frontend, $delivery_country)
{
$this->deliveryCountry = $delivery_country;
$this->deliveryFrontend = $delivery_frontend;
}
/**
* Zwraca orginalny obiekt kraju dostawy
*
* @return Countires
*/
public function getDeliveryCountry()
{
return $this->deliveryCountry;
}
public function getIsDefault()
{
$delivery_country = $this->deliveryFrontend->getDefaultDeliveryCountry();
if ($delivery_country)
{
return $delivery_country->getId() == $this->deliveryCountry->getId();
}
return $this->deliveryCountry->getIsDefault();
}
public function __call($name, $arguments)
{
return call_user_func_array(array($this->deliveryCountry, $name), $arguments);
}
}