*/ /** * Subclass for representing a row from the 'st_delivery' table. * * @package stDelivery * @subpackage libs */ class Delivery extends BaseDelivery { protected static $currency = null; protected $withEuTax = true; public function __construct() { if (null === self::$currency && SF_APP == 'frontend') { self::$currency = stCurrency::getInstance(sfContext::getInstance())->get(); } } public function __toString() { return $this->getName(); } /** * Sprawdza czy dostawa jest Aktywna * * @return bool */ public function getIsActive() { return $this->active; } /** * Ustawia aktywną dostawę * * @param bool $active * @return void */ public function setIsActive($active) { $this->setActive($active); } public function isWeekendDeliveryAvailable() { $date = new DateTime(); $currentDay = $date->format('N'); $currentTime = $date->format('H:i'); $from = $this->getWeekendDeliveryAvailabilityFrom(); $to = $this->getWeekendDeliveryAvailabilityTo(); if (!empty($from) && "" !== $from['day'] && ($from['day'] > $currentDay || $from['day'] == $currentDay && strtotime($from['time']) > strtotime($currentTime))) { return false; } if (!empty($to) && "" !== $to['day'] && ($to['day'] < $currentDay || $to['day'] == $currentDay && strtotime($to['time']) < strtotime($currentTime))) { return false; } return $this->isWeekendDelivery(); } public function getWeekendDeliveryAvailabilityFrom() { $params = $this->getWeekendDeliveryAvailability(); return !empty($params) && isset($params['from']) ? $params['from'] : null; } public function setWeekendDeliveryAvailabilityFrom($availability) { $params = !empty($this->getWeekendDeliveryAvailability()) ? $this->getWeekendDeliveryAvailability() : []; $params['from'] = $availability; $this->setWeekendDeliveryAvailability($params); } public function getWeekendDeliveryAvailabilityTo() { $params = $this->getWeekendDeliveryAvailability(); return !empty($params) && isset($params['to']) ? $params['to'] : null; } public function setWeekendDeliveryAvailabilityTo($availability) { $params = $this->getWeekendDeliveryAvailability(); $params['to'] = $availability; $this->setWeekendDeliveryAvailability($params); } public function isWeekendDelivery() { return $this->getIsWeekendDelivery(); } public function isExpressDelivery() { return $this->getIsExpressDelivery(); } public function getIsWeekendDelivery() { $weekendDelivery = parent::getIsWeekendDelivery(); return null === $weekendDelivery && null !== $this->weekend_delivery_cost_netto || $weekendDelivery; } public function setWidth($v) { parent::setWidth($v); $this->computeVolume(); } public function setHeight($v) { parent::setHeight($v); $this->computeVolume(); } public function setDepth($v) { parent::setDepth($v); $this->computeVolume(); } public function getMaxOrderWeight() { return $this->format(parent::getMaxOrderWeight()); } public function getMaxOrderAmount() { return $this->format(parent::getMaxOrderAmount()); } public function getMinOrderWeight() { return $this->format(parent::getMinOrderWeight()); } public function getMinOrderAmount() { return $this->format(parent::getMinOrderAmount()); } public function getDefaultCost() { return $this->format(parent::getDefaultCost()); } public function getCostNetto($with_currency = false) { $v = $this->getDefaultCost(); if ($with_currency) { $v = self::$currency->exchange($v); } return $v; } public function setCostNetto($v) { $this->setDefaultCost($v); } public function getCostBrutto($with_currency = false) { if (SF_APP == 'frontend' && (sfContext::getInstance()->getUser()->hasVatEu() || sfContext::getInstance()->getUser()->hasVatEx())) { return $this->getCostNetto($with_currency); } $cost = $this->getDefaultCostBrutto(); if (null === $cost) { $cost = stPrice::calculate($this->getCostNetto(), $this->getVat()); $this->setCostBrutto($cost); } $countryTaxRate = $this->getTax()->getTaxRateByCountry(); if (null !== $countryTaxRate) { $cost = stPrice::calculate(parent::getDefaultCost(), $countryTaxRate); } if ($with_currency) { $cost = self::$currency->exchange($cost); } return $this->format($cost); } public function getFreeDelivery() { if (SF_APP == 'frontend' && (sfContext::getInstance()->getUser()->hasVatEu() || sfContext::getInstance()->getUser()->hasVatEx())) { return stPrice::extract(parent::getFreeDelivery(), $this->getVat(false)); } return parent::getFreeDelivery(); } public function getVat($withEuTax = true) { $this->withEuTax = $withEuTax; $tax = $this->getTax(); return $tax->getTaxRateByCountry() ? $tax->getTaxRateByCountry() : $tax->getDefaultTaxRate(); } public function setCostBrutto($v) { $this->setDefaultCostBrutto($v); } public function getFreeFrom() { return $this->format($this->getFreeDelivery()); } public function setFreeFrom($v) { $this->setFreeDelivery($v); } public function getService(): ?stDeliveryTypeInterface { return null !== $this->getType() ? stDeliveryTypeService::get($this->getType()) : null; } public function getType(): ?string { return DeliveryTypePeer::retrieveTypeById($this->getTypeId()); } public function isType($type) { return $this->getType() == $type; } public function getSectionCostTypeDesc() { $tmp = DeliverySectionsPeer::getAdditionalSectionCosts(); return $this->getSectionCostType() ? $tmp[$this->getSectionCostType()] : null; } /** * Zwraca typ płatności dla przypisany do dostawy * * @param int $paymentTypeId * @return DeliveryHasPaymentType|null * @throws PropelException */ public function getDeliveryPaymentType(int $paymentTypeId) { $c = new Criteria(); $c->add(DeliveryHasPaymentTypePeer::PAYMENT_TYPE_ID, $paymentTypeId); $c->add(DeliveryHasPaymentTypePeer::IS_ACTIVE, true); $c->setLimit(1); $results = DeliveryHasPaymentTypePeer::doSelectJoinPaymentType($c); return !empty($results) ? $results[0] : null; } /** * * Dodana na potrzeby admin generator * * @param int $v Id vat */ public function setEditTax($v) { $this->setTaxId($v); } /** * @inheritDoc */ public function getTax($con = null) { if (SF_APP == 'frontend' && $this->withEuTax) { $user = sfContext::getInstance()->getUser(); if ($user->hasVatEu()) { return stTax::getEu(); } elseif ($user->hasVatEx()) { return stTax::getEx(); } } $this->aTax = parent::getTax($con); if (null === $this->aTax) { $this->aTax = TaxPeer::doSelectDefaultOne(new Criteria()); } return $this->aTax; } public function isFree(stDeliveryProductInfoAbstract $productInfo) { return $this->getFreeFrom() > 0 && $productInfo->getTotalAmount() >= $this->getFreeFrom(); } /** * Zwraca całkowity koszt brutto * * @param stDeliveryProductInfoAbstract $productInfo * @param bool $withCurrency * @return float|string * @throws PropelException */ public function getTotalCostBrutto(stDeliveryProductInfoAbstract $productInfo, $withCurrency = false) { return $this->getTotalCost($productInfo, true, $withCurrency); } /** * Zwraca całkowity koszt netto * * @param stDeliveryProductInfoAbstract $productInfo * @param bool $withCurrency * @return float|string * @throws PropelException */ public function getTotalCostNetto(stDeliveryProductInfoAbstract $productInfo, $withCurrency = false) { return $this->getTotalCost($productInfo, false, $withCurrency); } /** * Zwraca całkowity koszt * * @param stDeliveryProductInfoAbstract $productInfo * @param bool $withTax * @param bool $withCurrency * @return mixed * @throws PropelException */ public function getTotalCost(stDeliveryProductInfoAbstract $productInfo, $withTax = false, $withCurrency = false) { if ($this->isFree($productInfo)) { return 0; } $priceType = stConfig::getInstance('stProduct')->get('delivery_price_type', 'netto'); $totalProductDeliveryPrice = $productInfo->getTotalDeliveryPrice(); if ($priceType == 'netto' && $withTax) { $totalProductDeliveryPrice = stPrice::calculate($totalProductDeliveryPrice, $this->getVat()); } if ($priceType == 'brutto' && !$withTax) { $totalProductDeliveryPrice = stPrice::extract($totalProductDeliveryPrice, $this->getVat()); } if ($withCurrency) { $totalProductDeliveryPrice = self::$currency->exchange($totalProductDeliveryPrice); } $defaultCost = $withTax ? $this->getCostBrutto($withCurrency) : $this->getCostNetto($withCurrency); return $defaultCost + $totalProductDeliveryPrice + $this->getAdditionaCost($productInfo, $withTax, $withCurrency); } /** * Zwraca dodatkowy koszt * * @param stDeliveryProductInfoAbstract $productInfo * @param bool $withTax * @param bool $withCurrency * @return mixed * @throws PropelException */ public function getAdditionaCost(stDeliveryProductInfoAbstract $productInfo, $withTax = false, $withCurrency = false) { if ($this->isFree($productInfo)) { return 0; } switch ($this->getSectionCostType()) { case "ST_BY_ORDER_AMOUNT": $fromValue = $productInfo->getTotalAmount(); break; case "ST_BY_ORDER_QUANTITY": $fromValue = $productInfo->getTotalQuantity(); break; case "ST_BY_ORDER_WEIGHT": $fromValue = $productInfo->getTotalWeight(); break; default: $fromValue = 0; break; } $c = new Criteria(); $c->add(DeliverySectionsPeer::VALUE_FROM, $fromValue, Criteria::LESS_EQUAL); $c->setLimit(1); $c->addDescendingOrderByColumn(DeliverySectionsPeer::VALUE_FROM); $results = $this->getDeliverySectionss($c); if (empty($results)) { return 0.00; } $results[0]->setDelivery($this); return $withTax ? $results[0]->getCostBrutto($withCurrency) : $results[0]->getCostNetto($withCurrency); } /** * Zwraca koszt dostawy w weekend * * @param stDeliveryProductInfoAbstract $productInfo * @param bool $withTax * @param bool $withCurrency * @return float|string */ public function getWeekendDeliveryCost(stDeliveryProductInfoAbstract $productInfo, $withTax = false, $withCurrency = false) { if ($this->isFree($productInfo) && !$this->getAlwaysChargeWeekendDeliveryCost()) { return 0; } if ($withTax && (SF_APP != 'frontend' || !sfContext::getInstance()->getUser()->hasVatEu() && !sfContext::getInstance()->getUser()->hasVatEx())) { $cost = $this->getWeekendDeliveryCostBrutto(); $countryTaxRate = $this->getTax()->getTaxRateByCountry(); if (null !== $countryTaxRate) { $cost = stPrice::calculate($this->getWeekendDeliveryCostNetto(), $countryTaxRate); } } else { $cost = $this->getWeekendDeliveryCostNetto(); } if ($withCurrency) { $cost = stCurrency::exchange($cost); } return $cost; } /** * Zwraca koszt dostawy ekspresowej * * @param stDeliveryProductInfoAbstract $productInfo * @param bool $withTax * @param bool $withCurrency * @return float|string */ public function getExpressDeliveryCost(stDeliveryProductInfoAbstract $productInfo, $withTax = false, $withCurrency = false) { if ($this->isFree($productInfo) && !$this->getAlwaysChargeExpressDeliveryCost()) { return 0; } if ($withTax && (SF_APP != 'frontend' || !sfContext::getInstance()->getUser()->hasVatEu() && !sfContext::getInstance()->getUser()->hasVatEx())) { $cost = $this->getExpressDeliveryCostBrutto(); $countryTaxRate = $this->getTax()->getTaxRateByCountry(); if (null !== $countryTaxRate) { $cost = stPrice::calculate($this->getExpressDeliveryCostNetto(), $countryTaxRate); } } else { $cost = $this->getExpressDeliveryCostNetto(); } if ($withCurrency) { $cost = stCurrency::exchange($cost); } return $cost; } /** * Przeciążenie hydrate * * @param ResultSet $rs * @param int $startcol * @return object */ public function hydrate(ResultSet $rs, $startcol = 1) { $this->setCulture(stLanguage::getHydrateCulture()); return parent::hydrate($rs, $startcol); } /** * Przeciążenie getName * * @return string */ public function getName() { if ($this->getCulture() == stLanguage::getOptLanguage()) { return stLanguage::getDefaultValue($this, __METHOD__); } $v = parent::getName(); if (is_null($v)) { $v = stLanguage::getDefaultValue($this, __METHOD__); } return $v; } /** * Przeciążenie setName * * @param string $v Nazwa producenta */ public function setName($v) { if ($this->getCulture() == stLanguage::getOptLanguage()) { stLanguage::setDefaultValue($this, __METHOD__, $v); } parent::setName($v); } public function getParam($name, $default = null) { return $this->params && isset($this->params[$name]) ? $this->params[$name] : $default; } /** * Przeciążenie getDescription * * @return string */ public function getDescription() { if ($this->getCulture() == stLanguage::getOptLanguage()) { return stLanguage::getDefaultValue($this, __METHOD__); } $v = parent::getDescription(); if (is_null($v)) { $v = stLanguage::getDefaultValue($this, __METHOD__); } return $v; } /** * Przeciążenie setDescription * * @param string $v Nazwa producenta */ public function setDescription($v) { if ($this->getCulture() == stLanguage::getOptLanguage()) { stLanguage::setDefaultValue($this, __METHOD__, $v); } parent::setDescription($v); } public function delete($con = null) { $ret = parent::delete($con); self::clearCache(); return $ret; } public function save($con = null) { if ($this->getIsDefault() && $this->isColumnModified(DeliveryPeer::IS_DEFAULT)) { $c = new Criteria(); $c->add(DeliveryPeer::COUNTRIES_AREA_ID, $this->getCountriesAreaId()); $delivery = DeliveryPeer::doSelectDefault($c); if ($delivery) { $delivery->setIsDefault(false); $delivery->save($con); } } $ret = parent::save($con); self::clearCache(); return $ret; } protected function format($v) { $v = $v ? $v : 0.00; if (is_numeric($v)) { return stPrice::round($v); } return $v; } protected function computeVolume() { $volume = $this->width * $this->height * $this->depth; $this->setVolume($volume); } public static function clearCache() { $fc = new stFunctionCache('stDelivery'); $fc->removeAll(); } }