first commit
This commit is contained in:
193
plugins/stCurrencyPlugin/lib/stPrice.class.php
Normal file
193
plugins/stCurrencyPlugin/lib/stPrice.class.php
Normal file
@@ -0,0 +1,193 @@
|
||||
<?php
|
||||
/**
|
||||
* SOTESHOP/stCurrencyPlugin
|
||||
*
|
||||
* Ten plik należy do aplikacji stCurrencyPlugin opartej na licencji (Open License SOTE) Otwarta Licencja 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 stCurrencyPlugin
|
||||
* @subpackage libs
|
||||
* @copyright SOTE (www.sote.pl)
|
||||
* @license http://www.sote.pl/license/open (Open License SOTE) Otwarta Licencja SOTE
|
||||
* @version $Id: $
|
||||
* @author Marcin Butlak <marcin.butlak@sote.pl>
|
||||
*/
|
||||
|
||||
/**
|
||||
* Klasa pomocnicza dodająca podstawowe operacje na cenach
|
||||
*
|
||||
* @package stCurrencyPlugin
|
||||
* @subpackage libs
|
||||
*/
|
||||
class stPrice
|
||||
{
|
||||
const PRICE_MARGIN_AMOUNT = 'A';
|
||||
const PRICE_MARGIN_PERCENT = '%';
|
||||
|
||||
public static function getPriceMarginTypes()
|
||||
{
|
||||
return array(
|
||||
self::PRICE_MARGIN_AMOUNT => stCurrency::getDefault()->getShortcut(),
|
||||
self::PRICE_MARGIN_PERCENT => '%',
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* Oblicza cenę uwzgledniając podatek
|
||||
*
|
||||
* @param float $price Cena bez podatku
|
||||
* @param float $tax Podatek
|
||||
* @return float Cena z podatkiem
|
||||
*/
|
||||
public static function calculate($price, $tax)
|
||||
{
|
||||
return self::round($price * (1 + $tax * 0.01));
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* Wyciąga cenę bez podatku
|
||||
*
|
||||
* @param float $price Cena z podatkiem
|
||||
* @param float $tax Podatek
|
||||
* @return float Cena bez podatku
|
||||
*/
|
||||
public static function extract($price_brutto, $tax)
|
||||
{
|
||||
return self::round($price_brutto / (1 + $tax * 0.01));
|
||||
}
|
||||
|
||||
/**
|
||||
* Zwraca cenę + marża
|
||||
|
||||
* @param string $price Cena
|
||||
* @param string $margin Marża wyrażona w procentach lub kwocie
|
||||
* @param string $type Rodzaj marży proceny/kwota
|
||||
* @return string Cena + marża
|
||||
*/
|
||||
public static function applyPriceMargin($price, $margin, $type = self::PRICE_MARGIN_PERCENT)
|
||||
{
|
||||
if ($margin > 0)
|
||||
{
|
||||
$price = self::PRICE_MARGIN_PERCENT === $type ? stPrice::calculate($price, $margin) : $price + $margin;
|
||||
}
|
||||
|
||||
return $price;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* Zaokrągla cene
|
||||
*
|
||||
* @param float $price Cena
|
||||
* @return float Cena zaokrąglona
|
||||
*/
|
||||
public static function round($price, $precision = 2)
|
||||
{
|
||||
return number_format($price, $precision, '.', '');
|
||||
}
|
||||
|
||||
public static function applyDiscount($price, $discount)
|
||||
{
|
||||
return stPrice::round($price * (1 - $discount * 0.01));
|
||||
}
|
||||
|
||||
public static function discountValue($price, $discount)
|
||||
{
|
||||
return stPrice::round($price * ($discount * 0.01));
|
||||
}
|
||||
|
||||
public static function percentValue($price, $percent)
|
||||
{
|
||||
return self::discountValue($price, $percent);
|
||||
}
|
||||
|
||||
public static function percentFromValue($value, $discount)
|
||||
{
|
||||
return $discount >= $value ? 100 : stPrice::round($discount * 100 / $value, 1);
|
||||
}
|
||||
|
||||
public static function createPriceModifier($value, $type, $prefix, $level = 1, $custom = array())
|
||||
{
|
||||
return array('value' => $value, 'type' => $type, 'level' => $level, 'prefix' => $prefix, 'custom' => $custom);
|
||||
}
|
||||
|
||||
/**
|
||||
* Oblicza cene produktu na podstawie ustawionych modyfikatorów
|
||||
*
|
||||
* @param Product $product
|
||||
* @param string $base_price Cena bazowa
|
||||
* @param string $price_type Typ ceny
|
||||
* @param boolean $with_currency
|
||||
* @return string
|
||||
*/
|
||||
public static function computePriceModifiers($product, $base_price, $price_type = 'netto', $with_currency = false)
|
||||
{
|
||||
$currency = $product->getGlobalCurrency();
|
||||
|
||||
$has_local_currency = $product->hasLocalCurrency();
|
||||
|
||||
if ($with_currency && $has_local_currency)
|
||||
{
|
||||
$price_type = 'currency_'.$price_type;
|
||||
}
|
||||
|
||||
foreach ($product->getPriceModifiers() as $price_modifier)
|
||||
{
|
||||
if ($price_modifier['type'] === null) continue;
|
||||
|
||||
$prefix = $price_modifier['prefix'];
|
||||
|
||||
$value = $price_modifier['value'];
|
||||
|
||||
if ($price_modifier['type'] == 'percent')
|
||||
{
|
||||
$price = stPrice::percentValue($base_price, $value);
|
||||
}
|
||||
else
|
||||
{
|
||||
$price = $value[$price_type];
|
||||
|
||||
// if ($price_type == 'brutto')
|
||||
// {
|
||||
// $countryTaxRate = $product->getTax()->getTaxRateByCountry();
|
||||
|
||||
// if ($countryTaxRate)
|
||||
// {
|
||||
// $price = stPrice::calculate($value['netto'], $countryTaxRate);
|
||||
// }
|
||||
// }
|
||||
|
||||
if ($with_currency && !$has_local_currency)
|
||||
{
|
||||
$price = $currency->exchange($price);
|
||||
}
|
||||
}
|
||||
|
||||
if ($prefix)
|
||||
{
|
||||
$base_price += $prefix.$price;
|
||||
}
|
||||
else
|
||||
{
|
||||
$base_price = $price;
|
||||
}
|
||||
}
|
||||
|
||||
return stPrice::round($base_price >= 0 ? $base_price : 0.00);
|
||||
}
|
||||
|
||||
/**
|
||||
* Poprawia formatowanie ceny. Usuwa wszelkie dodatkowe spacje i zamienia przecinek na kropkę
|
||||
*
|
||||
* @param string $price
|
||||
* @return string
|
||||
*/
|
||||
public static function fix($price)
|
||||
{
|
||||
return strtr($price, [',' => '.', ' ' => '']);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user