150 lines
4.5 KiB
PHP
150 lines
4.5 KiB
PHP
<?php
|
|
|
|
use KupShop\I18nBundle\Translations\VariationsTranslation;
|
|
use Query\Operator;
|
|
use Query\Translation;
|
|
|
|
class VariationBase extends Product
|
|
{
|
|
public $variationId;
|
|
public $variationTitle;
|
|
|
|
public $variationCode;
|
|
public string $variationVisible;
|
|
public array $variationData = [];
|
|
|
|
public function __construct($id_product = -1, $id_variation = null)
|
|
{
|
|
parent::__construct($id_product);
|
|
|
|
$this->variationId = $id_variation;
|
|
}
|
|
|
|
public function createFromArray($data)
|
|
{
|
|
parent::createFromArray($data);
|
|
|
|
if (!empty($data['id_variation'])) {
|
|
$this->variationId = $data['id_variation'];
|
|
$this->variationTitle = $data['variation_title'];
|
|
$this->inStore = getVal('variation_in_store', $data, $this->inStore);
|
|
if (!empty($data['variation_weight'])) {
|
|
$this->weight = $data['variation_weight'];
|
|
}
|
|
$this->variationCode = $data['variationCode'] ?? '';
|
|
$this->variationVisible = $data['variationVisible'] ?? 'N';
|
|
$this->variationData = json_decode($data['variationData'] ?? '', true) ?: [];
|
|
}
|
|
}
|
|
|
|
public function getTableFields()
|
|
{
|
|
$fields = parent::getTableFields().', COALESCE(pv.code, p.code) as code,COALESCE(pv.ean, p.ean) as ean, pv.id as id_variation, pv.title as variation_title,
|
|
COALESCE(pv.delivery_time, p.delivery_time) as delivery_time, pv.code as variationCode,
|
|
IF(pv.figure="N", pv.figure, p.figure) as figure, COALESCE(pv.width, p.width) as width, COALESCE(pv.height, p.height) as height,
|
|
COALESCE(pv.depth, p.depth) as depth, pv.figure as variationVisible, pv.data as variationData';
|
|
|
|
if (findModule(\Modules::PRODUCTS, \Modules::SUB_WEIGHT)) {
|
|
$fields .= ', pv.weight as variation_weight';
|
|
}
|
|
|
|
if (findModule(\Modules::BONUS_PROGRAM)) {
|
|
$fields .= ', COALESCE(pv.bonus_points, p.bonus_points) as bonus_points';
|
|
}
|
|
|
|
return $fields;
|
|
}
|
|
|
|
public function createQueryBuilder()
|
|
{
|
|
$qb = parent::createQueryBuilder();
|
|
|
|
if (findModule(\Modules::PRODUCTS, \Modules::SUB_PRICE_BUY)) {
|
|
$qb->addSelect('COALESCE(pv.price_buy, p.price_buy) as price_buy');
|
|
}
|
|
|
|
$qb->addSelect('COALESCE(pv.width, p.width) width, COALESCE(pv.height, p.height) height, COALESCE(pv.depth, p.depth) depth');
|
|
|
|
if (findModule(\Modules::PRICE_HISTORY)) {
|
|
$qb->addSelect('COALESCE(pv.price_for_discount, p.price_for_discount) as price_for_discount');
|
|
}
|
|
|
|
$inStoreField = \Query\Product::getInStoreField(true, $qb);
|
|
if (findModule(\Modules::PRODUCTS, \Modules::SUB_SHOW_MAX)) {
|
|
$qb->addSelect('LEAST('.$inStoreField.', COALESCE(pv.in_store_show_max, p.in_store_show_max, '.findModule(\Modules::PRODUCTS, \Modules::SUB_SHOW_MAX).')) variation_in_store');
|
|
} else {
|
|
$qb->addSelect($inStoreField.' variation_in_store');
|
|
}
|
|
|
|
return $qb;
|
|
}
|
|
|
|
/**
|
|
* Fetches data from database, product ID specified in $pid.
|
|
*
|
|
* @param int $pid
|
|
*
|
|
* @return bool
|
|
*/
|
|
public function createFromDB($pid = null)
|
|
{
|
|
if ($pid) {
|
|
$this->id = $pid;
|
|
}
|
|
|
|
$SQL = $this->createQueryBuilder()
|
|
->andWhere(Operator::equals(['p.id' => $this->id, 'pv.id' => $this->variationId]))
|
|
->andWhere(
|
|
Translation::coalesceTranslatedFields(
|
|
VariationsTranslation::class,
|
|
['title' => 'variation_title']
|
|
)
|
|
)
|
|
->execute();
|
|
|
|
if (sqlNumRows($SQL) == 1) {
|
|
$prod = sqlFetchAssoc($SQL);
|
|
|
|
$this->title = $prod['title'];
|
|
|
|
$this->createFromArray($prod);
|
|
|
|
return true;
|
|
} else {
|
|
return false;
|
|
}
|
|
}
|
|
|
|
public function getInStoreSuppliers($id_variation = null)
|
|
{
|
|
if ($id_variation === null) {
|
|
$id_variation = $this->variationId;
|
|
}
|
|
|
|
return parent::getInStoreSuppliers($id_variation);
|
|
}
|
|
|
|
public static function createProductOrVariation($id_product, $id_variation)
|
|
{
|
|
if ($id_variation === null) {
|
|
return new \Product($id_product);
|
|
}
|
|
|
|
return new \Variation($id_product, $id_variation);
|
|
}
|
|
|
|
/**
|
|
* @return array|mixed
|
|
*/
|
|
public function getVariationData()
|
|
{
|
|
return $this->variationData;
|
|
}
|
|
}
|
|
|
|
if (empty($subclass)) {
|
|
class Variation extends VariationBase
|
|
{
|
|
}
|
|
}
|