first commit
This commit is contained in:
162
class/class.ParameterConfiguration.php
Normal file
162
class/class.ParameterConfiguration.php
Normal file
@@ -0,0 +1,162 @@
|
||||
<?php
|
||||
|
||||
use KupShop\I18nBundle\Translations\ParametersListTranslation;
|
||||
use KupShop\I18nBundle\Translations\ParametersTranslation;
|
||||
use KupShop\KupShopBundle\Context\CurrencyContext;
|
||||
use KupShop\KupShopBundle\Util\Contexts;
|
||||
use Query\Operator;
|
||||
use Query\QueryBuilder;
|
||||
use Query\Translation;
|
||||
|
||||
#[AllowDynamicProperties]
|
||||
class ParameterConfiguration extends TemplateAccess
|
||||
{
|
||||
use DatabaseCommunication;
|
||||
|
||||
public $id;
|
||||
public $id_product;
|
||||
|
||||
public $name;
|
||||
public $value_type;
|
||||
public $unit;
|
||||
public $descr;
|
||||
public $position;
|
||||
|
||||
/**
|
||||
* @return self[]
|
||||
*/
|
||||
public static function createFromSpec($spec)
|
||||
{
|
||||
$items = sqlQueryBuilder()->select('par.*')
|
||||
->from('parameters_configurations', 'parc')
|
||||
->join('parc', 'parameters', 'par', 'parc.id_parameter = par.id')
|
||||
->where($spec)
|
||||
->execute();
|
||||
|
||||
$result = [];
|
||||
foreach ($items as $data) {
|
||||
$item = self::createFromArray($data);
|
||||
$result[$item->id] = $item;
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
public static function createFromArray($data)
|
||||
{
|
||||
$item = new self();
|
||||
|
||||
foreach ($data as $key => $value) {
|
||||
$item->{$key} = $value;
|
||||
}
|
||||
|
||||
return $item;
|
||||
}
|
||||
|
||||
public static function insertValue($id_parameter, $id_product)
|
||||
{
|
||||
sqlQuery('INSERT INTO parameters_configurations (id_parameter, id_product)
|
||||
(
|
||||
SELECT * FROM (SELECT :id_parameter as id_parameter, :id_product as id_product) AS tmp
|
||||
WHERE NOT EXISTS(SELECT * FROM parameters_configurations WHERE id_parameter=:id_parameter AND id_product=:id_product)
|
||||
);', ['id_parameter' => $id_parameter, 'id_product' => $id_product]
|
||||
);
|
||||
}
|
||||
|
||||
public function deleteValue($id_parameter, $id_product)
|
||||
{
|
||||
return $this->deleteSQL('parameters_configurations', ['id_parameter' => $id_parameter, 'id_product' => $id_product]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return ParameterConfigurationValue[]
|
||||
*
|
||||
* @throws \Doctrine\DBAL\Exception
|
||||
*/
|
||||
public function fetchValues(bool $applyCurrency = true): array
|
||||
{
|
||||
$qb = sqlQueryBuilder()->select('pl.*, pp.*, COALESCE(pp.unit, pa.unit) as unit')
|
||||
->from('parameters', 'pa')
|
||||
->join('pa', 'parameters_products', 'pp', 'pp.id_parameter=pa.id')
|
||||
->leftJoin('pa', 'parameters_list', 'pl', 'pp.value_list=pl.id')
|
||||
->orderBy('pa.position ASC, pp.weight DESC, pa.id ASC, pl.position')
|
||||
->where(Operator::equals(['pp.id_parameter' => $this->id, 'pp.id_product' => $this->id_product]));
|
||||
|
||||
$qb->andWhere(Translation::joinTranslatedFields(ParametersTranslation::class,
|
||||
function (QueryBuilder $qb, $columnName, $translatedField) {
|
||||
if ($columnName == 'unit') {
|
||||
$field = Operator::coalesce($translatedField, 'pp.unit');
|
||||
$qb->addSelect("COALESCE({$field}, pa.unit) as unit");
|
||||
|
||||
return false;
|
||||
}
|
||||
},
|
||||
['unit', 'name', 'descr']));
|
||||
|
||||
$qb->andWhere(Translation::joinTranslatedFields(ParametersListTranslation::class,
|
||||
function (QueryBuilder $qb, $columnName, $translatedField) {
|
||||
if ($columnName == 'value') {
|
||||
$field = Operator::coalesce($translatedField, 'pl.value');
|
||||
$qb->addSelect("CASE pa.value_type
|
||||
WHEN 'char' THEN pp.value_char
|
||||
WHEN 'list' THEN {$field}
|
||||
ELSE pp.value_float
|
||||
END as value");
|
||||
|
||||
return false;
|
||||
}
|
||||
},
|
||||
['value', 'description']));
|
||||
|
||||
$values = [];
|
||||
foreach ($qb->execute() as $value) {
|
||||
$value = ParameterConfigurationValue::createFromArray($value, $applyCurrency);
|
||||
$values[$value->id] = $value;
|
||||
}
|
||||
|
||||
return $values;
|
||||
}
|
||||
}
|
||||
|
||||
#[AllowDynamicProperties]
|
||||
class ParameterConfigurationValue extends TemplateAccess
|
||||
{
|
||||
public $id;
|
||||
public $value;
|
||||
|
||||
/**
|
||||
* @var Decimal
|
||||
*/
|
||||
public $configuration_price;
|
||||
public $id_product;
|
||||
|
||||
public static function createFromArray($data, $applyCurrency = true): ParameterConfigurationValue
|
||||
{
|
||||
$item = new self();
|
||||
|
||||
foreach ($data as $key => $value) {
|
||||
$item->{$key} = $value;
|
||||
}
|
||||
|
||||
$item->configuration_price = toDecimal($item->configuration_price);
|
||||
if ($applyCurrency) {
|
||||
$item->configuration_price = applyCurrency($item->configuration_price);
|
||||
}
|
||||
|
||||
return $item;
|
||||
}
|
||||
|
||||
public function fetchPrice()
|
||||
{
|
||||
/*
|
||||
* Tohle je dmntni, ale musi to tu bejt, aby do template chodila cena priplatku s aplikovanou cenovou hladinou.
|
||||
* Ve funkci createFromArray to naopak bejt nema, aby tam byla defaultni cena, protoze kdyz se to pak fetchuje v kosiku,
|
||||
* tak se aplikuje potom formatCustomerPrice na celou polozku.
|
||||
* */
|
||||
// protože už mohla být aplikována currency v createFromArray
|
||||
$currencyContext = Contexts::get(CurrencyContext::class);
|
||||
$price = formatCustomerPrice($this->configuration_price, 0, 0, $this->id_product, null, null, null, $currencyContext->getActive())['value_with_vat'];
|
||||
|
||||
return $price;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user