Current File : /var/www/vinorea/modules/ipexportimport/classes/export/EIADiscountsExport.php |
<?php
/**
*
* NOTICE OF LICENSE
*
* @author SmartPresta <tehran.alishov@gmail.com>
* @copyright 2024 SmartPresta
* @license http://opensource.org/licenses/afl-3.0.php Commercial License!
*/
if (!defined('_PS_VERSION_')) {
exit;
}
class EIADiscountsExport extends EIAExport
{
protected $xmlMainTag = 'Discount';
protected $xmlMainTagPl = 'Discounts';
public function __construct($module)
{
parent::__construct($module);
$this->entityNamePl = $module->l('Discounts', 'EIADiscountsExport');
}
protected function getDataFromDb()
{
$newColumns = [
'discount.reduction_tax' => !empty(Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS("SHOW COLUMNS FROM `" . _DB_PREFIX_ . "specific_price` LIKE 'reduction_tax'"))
];
// May be needed on a customer request
// if ($this->auto) {
// $discountType = pSQL($this->inputs['discount_type']);
// } else {
// $discountType = pSQL(Tools::getValue('discount_type'));
// }
$eIHelper = new EIAHelper($this->module);
$this->sql = '
SELECT SQL_CALC_FOUND_ROWS ';
if (in_array($this->fileType, ['xml'])) {
$fieldsInTree = $eIHelper->getFieldsInTree('discounts');
foreach ($this->selectedColumns as $k => $col) {
$this->sql .= "
$k AS `{$fieldsInTree[$k]}`, ";
}
} else {
foreach ($this->selectedColumns as $k => $col) {
if (array_key_exists($k, $newColumns) && !$newColumns[$k]) {
$this->sql .= "
'' `$col`, ";
} else {
$this->sql .= "
$k `$col`, ";
}
}
}
$this->sql = rtrim($this->sql, ', ');
$sps = $sprs = '';
if ($this->auto) {
$ids_sp = [];
$ids_spr = [];
foreach ($this->datatables['discounts']['data'] as $val) {
$val = explode('_', $val);
if ($val[0]) {
$ids_sp[] = $val[0];
}
if ($val[1]) {
$ids_spr[] = $val[1];
}
}
$sps = pSQL(implode(',', $ids_sp));
$sprs = pSQL(implode(',', $ids_spr));
$discounts_type = $this->datatables['discounts']['type'];
} else {
$ids_sp = [];
$ids_spr = [];
foreach (explode(',', Tools::getValue('discounts_data')) as $val) {
$val = explode('_', $val);
if ($val[0]) {
$ids_sp[] = $val[0];
}
if ($val[1]) {
$ids_spr[] = $val[1];
}
}
$sps = pSQL(implode(',', $ids_sp));
$sprs = pSQL(implode(',', $ids_spr));
$discounts_type = Tools::getValue('discounts_type');
}
// Filter By Discount
if ($discounts_type === 'unselected') {
if ($sps) {
$sps = ' AND sp.id_specific_price NOT IN (' . $sps . ')';
}
if ($sprs) {
$sprs = ' AND spr.id_specific_price_rule NOT IN (' . $sprs . ')';
}
} else {
if ($sps) {
$sps = ' AND sp.id_specific_price IN (' . $sps . ')';
}
if ($sprs) {
$sprs = ' AND spr.id_specific_price_rule IN (' . $sprs . ')';
}
}
$this->sql .= '
FROM (
SELECT
sp.id_specific_price,
sp.id_specific_price_rule id_specific_price_rule,
IFNULL(spr.`name`, "") spr_name,
CAST(sprcg.`conds` AS CHAR) conds,
sp.id_product,
p.`reference` prod_reference,
pl.`name` prod_name,
attributes.`id_product_attribute` id_product_attribute,
attributes.`reference` comb_reference,
attributes.`values` comb_values,
sp.reduction reduction,
sp.reduction_type reduction_type,
' . ($newColumns['discount.reduction_tax'] ? 'sp.reduction_tax reduction_tax,' : '') . '
sp.`from` `from`,
sp.`to` `to`,
sp.price price,
sp.from_quantity from_quantity,
sp.id_customer id_customer,
IFNULL(firstname, "") firstname,
IFNULL(lastname, "") lastname,
IFNULL(email, "") email,
sp.id_group id_group,
IFNULL(gl.`name`, "") group_name,
sp.id_country id_country,
IFNULL(cl.`name`, "") country_name,
sp.id_currency id_currency,
IFNULL(cu.iso_code, "") currency_iso_code,
0 is_rule
FROM
' . _DB_PREFIX_ . 'specific_price sp
LEFT JOIN
' . _DB_PREFIX_ . 'product p ON sp.id_product = p.id_product
LEFT JOIN
' . _DB_PREFIX_ . 'product_lang pl ON p.id_product = pl.id_product
AND pl.id_lang = ' . $this->langId . '
AND pl.id_shop = ' . $this->shopId . '
LEFT JOIN (
SELECT
pa.id_product_attribute,
pa.reference,
GROUP_CONCAT(CONCAT_WS(":", IFNULL(agl.`name`, ""), al.`name`) SEPARATOR "' . $this->multivalueSeparator . '") name_values,
GROUP_CONCAT(CONCAT_WS(":", agl.`name`, agl.public_name, ag.group_type) ORDER BY agl.`name`, al.`name` SEPARATOR "' . $this->multivalueSeparator . '") `groups`,
GROUP_CONCAT(CONCAT_WS(":", agl.`name`, al.`name`) ORDER BY agl.`name`, al.`name` SEPARATOR "' . $this->multivalueSeparator . '") `values`
FROM ' . _DB_PREFIX_ . 'product_attribute pa
JOIN ' . _DB_PREFIX_ . 'product_attribute_combination pac ON pa.id_product_attribute = pac.id_product_attribute
JOIN ' . _DB_PREFIX_ . 'attribute a ON pac.id_attribute = a.id_attribute
JOIN ' . _DB_PREFIX_ . 'attribute_shop ash ON a.id_attribute = ash.id_attribute AND ash.id_shop = ' . $this->shopId . '
JOIN ' . _DB_PREFIX_ . 'attribute_lang al ON a.id_attribute = al.id_attribute AND al.id_lang = ' . $this->langId . '
JOIN ' . _DB_PREFIX_ . 'attribute_group ag ON a.id_attribute_group = ag.id_attribute_group
JOIN ' . _DB_PREFIX_ . 'attribute_group_shop agsh ON a.id_attribute_group = agsh.id_attribute_group AND agsh.id_shop = ' . $this->shopId . '
JOIN ' . _DB_PREFIX_ . 'attribute_group_lang agl ON a.id_attribute_group = agl.id_attribute_group AND agl.id_lang = ' . $this->langId . '
GROUP BY pa.id_product_attribute
) attributes ON sp.id_product_attribute = attributes.id_product_attribute
LEFT JOIN
' . _DB_PREFIX_ . 'customer c ON sp.id_country = c.id_customer
LEFT JOIN
' . _DB_PREFIX_ . 'group_lang gl ON sp.id_group = gl.id_group
AND gl.id_lang = ' . $this->langId . '
LEFT JOIN
' . _DB_PREFIX_ . 'currency cu ON sp.id_currency = cu.id_currency
LEFT JOIN
' . _DB_PREFIX_ . 'country_lang cl ON sp.id_country = cl.id_country
AND cl.id_lang = ' . $this->langId . '
LEFT JOIN
' . _DB_PREFIX_ . 'specific_price_rule spr ON sp.id_specific_price_rule = spr.id_specific_price_rule
LEFT JOIN
(SELECT sprcg.id_specific_price_rule, GROUP_CONCAT(cond SEPARATOR " | ") conds
FROM ' . _DB_PREFIX_ . 'specific_price_rule_condition_group sprcg
LEFT JOIN (
SELECT id_specific_price_rule_condition_group, GROUP_CONCAT(CONCAT(sprc.`type`, ":", CASE
WHEN sprc.`type` = "category" THEN cl.`name`
WHEN sprc.`type` = "manufacturer" THEN m.`name`
WHEN sprc.`type` = "supplier" THEN s.`name`
WHEN sprc.`type` = "attribute" THEN CONCAT(agl.`name`, " ^ ", agl.public_name, " ^ ", ag.group_type, " ^ ", al.`name`, " ^ ", a.color)
WHEN sprc.`type` = "feature" THEN CONCAT(fl.`name`, " ^ ", fvl.`value`, " ^ ", fv.`custom`)
ELSE sprc.`value`
END) SEPARATOR " & ") cond
FROM ' . _DB_PREFIX_ . 'specific_price_rule_condition sprc
LEFT JOIN ' . _DB_PREFIX_ . 'category_lang cl ON sprc.`type` = "category" AND sprc.`value` = cl.id_category AND cl.id_shop = ' . $this->shopId . ' AND cl.id_lang = ' . $this->langId . '
LEFT JOIN ' . _DB_PREFIX_ . 'manufacturer m ON sprc.`type` = "manufacturer" AND sprc.`value` = m.id_manufacturer
LEFT JOIN ' . _DB_PREFIX_ . 'supplier s ON sprc.`type` = "supplier" AND sprc.`value` = s.id_supplier
LEFT JOIN ' . _DB_PREFIX_ . 'attribute a ON sprc.`type` = "attribute" AND sprc.`value` = a.id_attribute
LEFT JOIN ' . _DB_PREFIX_ . 'attribute_lang al ON a.id_attribute = al.id_attribute AND al.id_lang = ' . $this->langId . '
LEFT JOIN ' . _DB_PREFIX_ . 'attribute_group ag ON a.id_attribute_group = ag.id_attribute_group
LEFT JOIN ' . _DB_PREFIX_ . 'attribute_group_lang agl ON ag.id_attribute_group = agl.id_attribute_group AND agl.id_lang = ' . $this->langId . '
LEFT JOIN ' . _DB_PREFIX_ . 'feature_value fv ON sprc.`type` = "feature" AND sprc.`value` = fv.id_feature_value
LEFT JOIN ' . _DB_PREFIX_ . 'feature_value_lang fvl ON fv.id_feature_value = fvl.id_feature_value AND fvl.id_lang = ' . $this->langId . '
LEFT JOIN ' . _DB_PREFIX_ . 'feature f ON fv.id_feature = f.id_feature
LEFT JOIN ' . _DB_PREFIX_ . 'feature_lang fl ON f.id_feature = fl.id_feature AND fl.id_lang = ' . $this->langId . '
GROUP BY id_specific_price_rule_condition_group) sprc ON sprc.id_specific_price_rule_condition_group = sprcg.id_specific_price_rule_condition_group
GROUP BY sprcg.id_specific_price_rule) sprcg ON spr.id_specific_price_rule = sprcg.id_specific_price_rule
WHERE 1 ' . $sps . '
UNION
SELECT
NULL `id_specific_price`,
spr.`id_specific_price_rule`,
spr.`name` spr_name,
CAST(sprcg.`conds` AS CHAR) conds,
NULL `id_product`,
NULL `prod_reference`,
NULL `prod_name`,
NULL `id_product_attribute`,
NULL `comb_reference`,
NULL `comb_values`,
spr.`reduction`,
spr.`reduction_type`,
' . ($newColumns['discount.reduction_tax'] ? 'spr.`reduction_tax`,' : '') . '
spr.`from`,
spr.`to`,
spr.`price`,
spr.from_quantity,
NULL id_customer,
NULL firstname,
NULL lastname,
NULL email,
spr.id_group,
gl.`name` group_name,
spr.id_country,
cl.`name`,
spr.id_currency,
c.iso_code,
1 is_rule
FROM ' . _DB_PREFIX_ . 'specific_price_rule spr
LEFT JOIN
(SELECT sprcg.id_specific_price_rule, GROUP_CONCAT(cond SEPARATOR " | ") conds
FROM ' . _DB_PREFIX_ . 'specific_price_rule_condition_group sprcg
LEFT JOIN (
SELECT id_specific_price_rule_condition_group, GROUP_CONCAT(CONCAT(sprc.`type`, ":", CASE
WHEN sprc.`type` = "category" THEN cl.`name`
WHEN sprc.`type` = "manufacturer" THEN m.`name`
WHEN sprc.`type` = "supplier" THEN s.`name`
WHEN sprc.`type` = "attribute" THEN CONCAT(agl.`name`, " ^ ", agl.public_name, " ^ ", ag.group_type, " ^ ", al.`name`, " ^ ", a.color)
WHEN sprc.`type` = "feature" THEN CONCAT(fl.`name`, " ^ ", fvl.`value`, " ^ ", fv.`custom`)
ELSE sprc.`value`
END) SEPARATOR " & ") cond
FROM ' . _DB_PREFIX_ . 'specific_price_rule_condition sprc
LEFT JOIN ' . _DB_PREFIX_ . 'category_lang cl ON sprc.`type` = "category" AND sprc.`value` = cl.id_category AND cl.id_shop = ' . $this->shopId . ' AND cl.id_lang = ' . $this->langId . '
LEFT JOIN ' . _DB_PREFIX_ . 'manufacturer m ON sprc.`type` = "manufacturer" AND sprc.`value` = m.id_manufacturer
LEFT JOIN ' . _DB_PREFIX_ . 'supplier s ON sprc.`type` = "supplier" AND sprc.`value` = s.id_supplier
LEFT JOIN ' . _DB_PREFIX_ . 'attribute a ON sprc.`type` = "attribute" AND sprc.`value` = a.id_attribute
LEFT JOIN ' . _DB_PREFIX_ . 'attribute_lang al ON a.id_attribute = al.id_attribute AND al.id_lang = ' . $this->langId . '
LEFT JOIN ' . _DB_PREFIX_ . 'attribute_group ag ON a.id_attribute_group = ag.id_attribute_group
LEFT JOIN ' . _DB_PREFIX_ . 'attribute_group_lang agl ON ag.id_attribute_group = agl.id_attribute_group AND agl.id_lang = ' . $this->langId . '
LEFT JOIN ' . _DB_PREFIX_ . 'feature_value fv ON sprc.`type` = "feature" AND sprc.`value` = fv.id_feature_value
LEFT JOIN ' . _DB_PREFIX_ . 'feature_value_lang fvl ON fv.id_feature_value = fvl.id_feature_value AND fvl.id_lang = ' . $this->langId . '
LEFT JOIN ' . _DB_PREFIX_ . 'feature f ON fv.id_feature = f.id_feature
LEFT JOIN ' . _DB_PREFIX_ . 'feature_lang fl ON f.id_feature = fl.id_feature AND fl.id_lang = ' . $this->langId . '
GROUP BY id_specific_price_rule_condition_group) sprc ON sprc.id_specific_price_rule_condition_group = sprcg.id_specific_price_rule_condition_group
GROUP BY sprcg.id_specific_price_rule) sprcg ON spr.id_specific_price_rule = sprcg.id_specific_price_rule
LEFT JOIN
' . _DB_PREFIX_ . 'group_lang gl ON spr.id_group = gl.id_group
AND gl.id_lang = ' . $this->langId . '
LEFT JOIN
' . _DB_PREFIX_ . 'currency c ON spr.id_currency = c.id_currency
LEFT JOIN
' . _DB_PREFIX_ . 'country_lang cl ON spr.id_country = cl.id_country
AND cl.id_lang = ' . $this->langId . '
WHERE spr.id_specific_price_rule NOT IN (
SELECT id_specific_price_rule
FROM ' . _DB_PREFIX_ . 'specific_price) ' . $sprs . '
) discount WHERE 1
';
// Filter By Discount Type
// if ($discountType === 'specific') {
// $this->sql .= "
// AND discount.id_specific_price IS NOT NULL";
// } elseif ($discountType === 'catalog') {
// $this->sql .= "
// AND discount.id_specific_price_rule <> 0";
// }
// Filter by fields data
$this->sql .= $eIHelper->getFieldsFilterString($this->auto, $this->inputs);
// Sort By ...
$this->sql .= ' ORDER BY ' . $this->sort . $this->sortWay;
if ($this->sort !== 'discount.id_specific_price') {
$this->sql .= ', discount.id_specific_price ASC';
}
$this->sql .= ' LIMIT ' . $this->offset . ', ' . $this->limit;
// d($this->sql);
return Db::getInstance()->executeS($this->sql);
}
}