getName(); } /** * Sprawdza czy rabat jest dostępny dla wybranego użytkownika * * @param null|sfGuardUser $user * @return bool */ public function isAvailableForUser($user = null) { if ($user && !empty($user->getWholesale()) && null === $this->getWholesaleValue()) { return false; } $uid = $user ? DiscountPeer::doSelectIdsByUser($user) : []; return null !== $this->getValueByUser($user) && (!$user && $this->getAllowAnonymousClients() || $user && $this->getAllClients() || isset($uid[$this->getId()])); } public function save($con = null) { if ($this->type != 'O') { $this->setConditions(null); } elseif ($this->type == 'O') { $this->setAllProducts(true); } $clearCache = $this->isModified(); $ret = parent::save($con); if ($clearCache) { DiscountPeer::clearCache(); stPartialCache::clear('stProduct', '_productGroup', array('app' => 'frontend')); stPartialCache::clear('stProduct', '_new', array('app' => 'frontend')); stFastCacheManager::clearCache(); } return $ret; } public function getTypeLabel() { $types = DiscountPeer::getDiscountTypes(); return $this->type ? $types[$this->type] : null; } public function delete($con = null) { $ret = parent::delete($con); DiscountPeer::clearCache(); return $ret; } public function getCondition($name, $default = null) { $conditions = $this->getConditions(); $value = isset($conditions[$name]) ? $conditions[$name] : $default; return is_numeric($value) ? floatval($value) : $value; } public function isDiscountRange() { if (null === $this->isDiscountRange) { $this->isDiscountRange = !$this->isNew() && $this->countDiscountRanges() > 0; } return $this->isDiscountRange; } /** * Zwraca wartość rabatu dla wybranego użytkownika * * @param null|sfGuardUser $user * @return float */ public function getValueByUser($user = null) { return null !== $user && !empty($user->getWholesale()) ? $this->getWholesaleValue() : $this->getValue(); } /** * Nakłada rabat na przekazaną cenę * * @param float $amount Cena * @param null|sfGuardUser $user Użytkownik * @param bool $withCurrency Uwzględnij walutę * @return float */ public function apply($amount, $user = null, $withCurrency = true) { if ($this->getPriceType() == self::PERCENT_TYPE) { $amount = stPrice::applyDiscount($amount, $this->getValueByUser($user)); } else { $discountValue = $this->getValueByUser($user); if ($withCurrency) { $discountValue = stCurrency::exchange($discountValue); } $amount -= $discountValue; } return $amount > 0 ? $amount : 0; } /** * Zwraca produkty przypisane do zestawu * * @param bool $with_default Uwzględnij produkt główny * @param bool $check Zweryfikuj dostępność produktów * @return Product[] * @throws PropelException * @throws SQLException */ public function getProducts($with_default = true, $check = true) { if (null === $this->products) { $c = new Criteria(); $c->addSelectColumn(DiscountHasProductPeer::PRODUCT_ID); $c->add(DiscountHasProductPeer::DISCOUNT_ID, $this->getId()); $rs = DiscountHasProductPeer::doSelectRS($c); $ids = array(); while($rs->next()) { list($id) = $rs->getRow(); $ids[] = $id; } $c = new Criteria(); $c->add(ProductPeer::ID, $ids, Criteria::IN); ProductPeer::addFilterCriteria(null, $c); $this->products = ProductPeer::doSelectWithI18n($c); if ($check) { if (count($ids) != count($this->products)) { $this->products = array(); } $avail = $this->getAvailability(); foreach ($this->products as $p) { if (!stBasket::isEnabled($p) || $avail && ($p->getAvailabilityId() == $avail->getId() || $p->getFrontendAvailability()->getId() == $avail->getId())) { $this->products = array(); break; } } } } if ($with_default && $this->products) { return array_merge(array($this->getProduct()), $this->products); } return $this->products; } /** * Zwraca łączną cenę zestawu * * @param bool $with_discount Uwzględniaj rabaty * @param bool $with_tax Uwzgędniaj podatek * @return float * @throws PropelException * @throws SQLException */ public function getTotalProductAmount($with_discount = true, $with_tax = true) { $total = 0; $sf_user = sfContext::getInstance()->getUser(); $user = $sf_user->isAuthenticated() ? $sf_user->getGuardUser() : null; $with_discount = $with_discount && !stDiscount::isDisabledForWholesale($user); $products = $this->getProducts(); if (empty($products)) { return 0; } if ($this->getPriceType() == self::PERCENT_TYPE) { foreach ($products as $product) { $price = $product->getPriceBrutto(true); $total += $price; } if ($with_discount) { $total = stPrice::applyDiscount($total, $this->getValueByUser($user)); } } else { foreach ($products as $product) { $price = $product->getPriceBrutto(true); $total += $price; } if ($with_discount) { $total -= stCurrency::exchange($this->getValueByUser($user)); } } return $with_tax ? $total : stPrice::extract($total, $products[0]->getVatValue()); } /** * Zwraca łączną wagę zestawu * * @return float * @throws PropelException * @throws SQLException */ public function getTotalWeight() { $weight = 0; foreach ($this->getProducts() as $product) { $weight += $product->getWeight(); } return $weight; } protected static function getAvailability() { if (null === self::$availability) { $config = stConfig::getInstance('stAvailabilityBackend'); $id = $config->get('hide_products_avail'); if ($id && $config->get('hide_products_avail_on')) { self::$availability = AvailabilityPeer::retrieveByPK($id); } else { self::$availability = false; } } return self::$availability; } } sfPropelBehavior::add('Discount', array('act_as_sortable' => array('column' => 'priority')));