322 lines
8.3 KiB
PHP
322 lines
8.3 KiB
PHP
<?php
|
|
|
|
class stTax
|
|
{
|
|
const SESSION_NAMESPACE = 'soteshop/stTaxPlugin';
|
|
|
|
protected static $activeCached = null;
|
|
|
|
protected static $requiredCountriesIds = null;
|
|
|
|
/**
|
|
* Lista krajów dla VAT UE Konsumenta
|
|
*
|
|
* @var Countries[]
|
|
*/
|
|
protected static $countriesForConsumerEuRates = null;
|
|
|
|
/**
|
|
* Zwraca stawkę VAT po ID
|
|
*
|
|
* @param int $id
|
|
* @return Tax
|
|
*/
|
|
public static function getById($id)
|
|
{
|
|
$taxes = self::get();
|
|
|
|
return isset($taxes[$id]) ? $taxes[$id] : null;
|
|
}
|
|
|
|
/**
|
|
* Zwraca czy VAT UE dla konsumentów jest włączony
|
|
*
|
|
* @return bool
|
|
*/
|
|
public static function getIsCustomerEuTaxEnabled($checkConfigOnly = false)
|
|
{
|
|
$config = stConfig::getInstance('stTaxBackend');
|
|
|
|
$isCustomerEuTaxEnabled = sfContext::getInstance()->getUser()->getAttribute('is_customer_eu_tax_enabled', true, self::SESSION_NAMESPACE);
|
|
|
|
return $config->get('is_customer_eu_tax_enabled') && ($isCustomerEuTaxEnabled || $checkConfigOnly);
|
|
}
|
|
|
|
/**
|
|
* Ustawia czy VAT UE dla konsumentów jest włączony
|
|
*
|
|
* @param bool $isCustomerEuTaxEnabled Czy VAT UE dla konsumentów jest włączony
|
|
*/
|
|
public static function setIsCustomerEuTaxEnabled($isCustomerEuTaxEnabled)
|
|
{
|
|
sfContext::getInstance()->getUser()->setAttribute('is_customer_eu_tax_enabled', $isCustomerEuTaxEnabled, self::SESSION_NAMESPACE);
|
|
}
|
|
|
|
/**
|
|
* Zwraca stawkę VAT po wartości
|
|
*
|
|
* @param string $value
|
|
* @return Tax
|
|
*/
|
|
public static function getByValue($value)
|
|
{
|
|
$value = stPrice::round($value);
|
|
|
|
$results = self::fetchCached();
|
|
|
|
$rates = $results['rates'];
|
|
|
|
return isset($rates[$value]) ? $results['active'][$rates[$value]] : null;
|
|
}
|
|
|
|
/**
|
|
* Zwraca listę aktywnych stawek VAT
|
|
*
|
|
* @return Tax[]
|
|
*/
|
|
public static function get()
|
|
{
|
|
$results = self::fetchCached();
|
|
|
|
return $results['active'];
|
|
}
|
|
|
|
/**
|
|
* Zwraca domyślną stawkę VAT
|
|
*
|
|
* @return void
|
|
*/
|
|
public static function getDefault()
|
|
{
|
|
$results = self::fetchCached();
|
|
|
|
return $results['default'] ? $results['active'][$results['default']] : null;
|
|
}
|
|
|
|
public static function hasEx($country_id = null)
|
|
{
|
|
/**
|
|
* @var stBasket
|
|
*/
|
|
$basket = sfContext::getInstance()->getUser()->getBasket();
|
|
|
|
/**
|
|
* @var Countries
|
|
*/
|
|
$country = stDeliveryFrontend::getInstance($basket)->getDefaultDeliveryCountry();
|
|
|
|
return null !== self::getEx() && $country && !$country->isEU() && (null === $country_id || !CountriesPeer::retrieveByPK($country_id)->isEU());
|
|
}
|
|
|
|
/**
|
|
* Zwraca stawkę EX
|
|
*
|
|
* @return Tax
|
|
*/
|
|
public static function getEx()
|
|
{
|
|
$results = self::fetchCached();
|
|
|
|
return $results['ex'] && isset($results['active'][$results['ex']]) ? $results['active'][$results['ex']] : null;
|
|
}
|
|
|
|
/**
|
|
* Zwraca stawkę EU
|
|
*
|
|
* @return Tax
|
|
*/
|
|
public static function getEu()
|
|
{
|
|
$results = self::fetchCached();
|
|
|
|
return $results['eu'] && isset($results['active'][$results['eu']]) ? $results['active'][$results['eu']] : null;
|
|
}
|
|
|
|
public static function fetchCached()
|
|
{
|
|
if (null === self::$activeCached)
|
|
{
|
|
$cache = new stFunctionCache('stTax');
|
|
self::$activeCached = $cache->cacheCall(array('stTax', 'fetch'));
|
|
}
|
|
|
|
return self::$activeCached;
|
|
}
|
|
|
|
public static function fetch()
|
|
{
|
|
$c = new Criteria();
|
|
$c->add(TaxPeer::IS_ACTIVE, true);
|
|
|
|
$rs = TaxPeer::doSelectRS($c);
|
|
|
|
$results = array();
|
|
$rates = array();
|
|
$default = null;
|
|
$ex = null;
|
|
$eu = null;
|
|
|
|
while ($rs->next())
|
|
{
|
|
$tax = new Tax();
|
|
$tax->hydrate($rs);
|
|
$results[$tax->getId()] = $tax;
|
|
|
|
if (!in_array($tax->getVatName(), ['ex', 'ue', 'zw']))
|
|
{
|
|
$rates[stPrice::round($tax->getVat())] = $tax->getId();
|
|
}
|
|
|
|
if ($tax->getIsDefault())
|
|
{
|
|
$default = $tax->getId();
|
|
}
|
|
|
|
if ($tax->getVatName() == 'ex')
|
|
{
|
|
$ex = $tax->getId();
|
|
}
|
|
|
|
if ($tax->getVatName() == 'ue')
|
|
{
|
|
$eu = $tax->getId();
|
|
}
|
|
}
|
|
|
|
return array('active' => $results, 'default' => $default, 'rates' => $rates, 'ex' => $ex, 'eu' => $eu);
|
|
}
|
|
|
|
|
|
public static function getEUTaxRates()
|
|
{
|
|
return sfConfig::get('app_st_tax_rates', array());
|
|
}
|
|
|
|
/**
|
|
* Zwraca listę krajów dla VAT UE Konsumenta
|
|
*
|
|
* @return Countries[]
|
|
* @throws PropelException
|
|
*/
|
|
public static function getCountriesForConsumerEuRates()
|
|
{
|
|
if (null === self::$countriesForConsumerEuRates)
|
|
{
|
|
$rates = self::getEUTaxRates();
|
|
|
|
$countryCodes = array_keys($rates);
|
|
|
|
$c = new Criteria();
|
|
$c->add(CountriesPeer::ISO_A2, $countryCodes, Criteria::IN);
|
|
$c->addAscendingOrderByColumn(CountriesPeer::OPT_NAME);
|
|
|
|
CountriesPeer::setHydrateMethod(function (ResultSet $rs)
|
|
{
|
|
$results = array();
|
|
|
|
$rs->setFetchmode(ResultSet::FETCHMODE_NUM);
|
|
|
|
while ($rs->next())
|
|
{
|
|
$country = new Countries();
|
|
$country->hydrate($rs);
|
|
$results[$country->getIsoA2()] = $country;
|
|
}
|
|
|
|
return $results;
|
|
});
|
|
|
|
self::$countriesForConsumerEuRates = CountriesPeer::doSelect($c);
|
|
}
|
|
|
|
return self::$countriesForConsumerEuRates;
|
|
}
|
|
|
|
/**
|
|
* Zwraca listę kodów krajów wymaganych
|
|
*
|
|
* @return array
|
|
*/
|
|
public static function getRequiredCountriesCodes()
|
|
{
|
|
if (null === self::$requiredCountriesIds)
|
|
{
|
|
$config = stConfig::getInstance('stShopInfoBackend');
|
|
$ids = array();
|
|
|
|
foreach (self::getCountriesForConsumerEuRates() as $country)
|
|
{
|
|
$ids[] = $country->getId();
|
|
}
|
|
|
|
$c = new Criteria();
|
|
$c->addSelectColumn(CountriesPeer::ID);
|
|
$c->addSelectColumn(CountriesPeer::ISO_A2);
|
|
$c->add(CountriesPeer::ID, $ids, Criteria::IN);
|
|
$c->addJoin(CountriesPeer::ID, CountriesAreaHasCountriesPeer::COUNTRIES_ID);
|
|
$c->addJoin(CountriesAreaHasCountriesPeer::COUNTRIES_AREA_ID, CountriesAreaPeer::ID);
|
|
$c->addJoin(CountriesAreaPeer::ID, DeliveryPeer::COUNTRIES_AREA_ID);
|
|
$c->add(DeliveryPeer::ACTIVE, true);
|
|
$c->addGroupByColumn(CountriesPeer::ID);
|
|
|
|
CountriesPeer::setHydrateMethod(function (ResultSet $rs) use ($config)
|
|
{
|
|
$results = array();
|
|
|
|
while ($rs->next())
|
|
{
|
|
$id = $rs->getInt(1);
|
|
$code = $rs->getString(2);
|
|
|
|
if ($config->get('country') != $code)
|
|
{
|
|
$results[$id] = $code;
|
|
}
|
|
}
|
|
|
|
return $results;
|
|
});
|
|
|
|
|
|
|
|
self::$requiredCountriesIds = CountriesPeer::doSelect($c);
|
|
}
|
|
|
|
return self::$requiredCountriesIds;
|
|
}
|
|
|
|
public static function checkRequiredCountriesTaxRateConfiguration(array $countryCodes, array &$errors = null)
|
|
{
|
|
$errors = array();
|
|
|
|
$requiredCodes = self::getEUTaxRates();
|
|
|
|
$config = stConfig::getInstance('stShopInfoBackend');
|
|
|
|
foreach (TaxPeer::doSelectActive() as $tax)
|
|
{
|
|
if ($tax->isSystemDefault())
|
|
{
|
|
continue;
|
|
}
|
|
|
|
$rates = $tax->getRatesByCountry();
|
|
|
|
foreach ($countryCodes as $code)
|
|
{
|
|
if ($config->get('country') == $code)
|
|
{
|
|
continue;
|
|
}
|
|
|
|
if (isset($requiredCodes[$code]) && (!$rates || !isset($rates[$code]) || $rates[$code] === ""))
|
|
{
|
|
$errors[$code][$tax->getId()] = $tax;
|
|
}
|
|
}
|
|
}
|
|
|
|
return empty($errors);
|
|
}
|
|
}
|