Current File : /var/www/vinorea/modules/ipexportimport/classes/export/EIACustomersExport.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 EIACustomersExport extends EIAExport
{
    protected $xmlMainTag = 'Customer';
    protected $xmlMainTagPl = 'Customers';

    public function __construct($module)
    {
        parent::__construct($module);
        $this->entityNamePl = $module->l('Customers', 'EIACustomersExport');
    }

    protected function getDataFromDb()
    {
        // Columns that do not exist directly in the DB
        $absentColumns = array(
            'new_passwd' => '""'
        );

        if ($this->auto) {
            $customers = pSQL(implode(',', $this->datatables['customers']['data']));
            $customersType = $this->datatables['customers']['type'];

            $groups = pSQL(implode(',', $this->datatables['groups']['data']));
            $groups_type = $this->datatables['groups']['type'];
            $groups_without = $this->inputs['group_without'];
        } else {
            $customers = pSQL(Tools::getValue('customers_data'));
            $customersType = Tools::getValue('customers_type');

            $groups = pSQL(Tools::getValue('groups_data'));
            $groups_type = Tools::getValue('groups_type');
            $groups_without = Tools::getValue('group_without');
        }
        
        $eIHelper = new EIAHelper($this->module);

        $this->sql = '
            SELECT SQL_CALC_FOUND_ROWS ';
        if (in_array($this->fileType, ['xml'])) {
            $fieldsInTree = $eIHelper->getFieldsInTree('customers');
            foreach ($this->selectedColumns as $k => $col) {
                if (array_key_exists($k, $absentColumns)) {
                    $this->sql .= "
                    {$absentColumns[$k]} `{$fieldsInTree[$k]}`, ";
                } else {
                    $this->sql .= "
                    $k AS `{$fieldsInTree[$k]}`, ";
                }
            }
        } else {
            foreach ($this->selectedColumns as $k => $col) {
                if (array_key_exists($k, $absentColumns)) {
                    $this->sql .= "
                    {$absentColumns[$k]} `$col`, ";
                } else {
                    $this->sql .= "
                        $k `$col`, ";
                }
            }
        }

        $this->sql = rtrim($this->sql, ', ');

        // Filter By Group
        $groupsCond = $groupsCond2 = '';
        if ($groups) {
            if ($groups_type === 'unselected') {
                $groupsCond = ' AND id_group NOT IN (' . $groups . ')';
            } else {
                $groupsCond = ' AND id_group IN (' . $groups . ')';
            }
            $groupsCond2 .= 'groupp.id_customer IS NOT NULL';
            if ($groups_without !== '0') {
                $groupsCond2 .= ' OR for_null_group.id_customer IS NULL';
            }
            $groupsCond2 = ' 
                AND (' . $groupsCond2 . ')';
        } else {
            if ($groups_without !== '0' && $groups_type === 'selected') {
                $groupsCond2 .= ' 
                    AND (for_null_group.id_customer IS NULL)';
            } elseif ($groups_without === '0' && $groups_type === 'unselected') {
                $groupsCond2 .= ' 
                    AND (for_null_group.id_customer IS NOT NULL)';
            }
        }

        $this->sql .= '
            FROM 
            ' . _DB_PREFIX_ . 'customer customer
                LEFT JOIN
            ' . _DB_PREFIX_ . 'gender gender ON customer.id_gender = gender.id_gender
                LEFT JOIN
            ' . _DB_PREFIX_ . 'group_lang default_group_lang ON customer.id_default_group = default_group_lang.id_group
                    AND default_group_lang.id_lang = ' . $this->langId . '
                LEFT JOIN ' . _DB_PREFIX_ . 'lang lang ON customer.id_lang = lang.id_lang
                LEFT JOIN (
                    SELECT 
                        cg.id_customer,
                        GROUP_CONCAT(cg.id_group SEPARATOR "' . $this->multivalueSeparator . '") ids,
                        GROUP_CONCAT(gl.name SEPARATOR "' . $this->multivalueSeparator . '") names
                    FROM (
                        SELECT DISTINCT id_customer FROM ' . _DB_PREFIX_ . 'customer_group WHERE 1' . $groupsCond . '
                    ) sub_group
                    JOIN ' . _DB_PREFIX_ . 'customer_group cg ON sub_group.id_customer = cg.id_customer
                    LEFT JOIN ' . _DB_PREFIX_ . 'group_lang gl ON cg.id_group = gl.id_group AND gl.id_lang = ' . $this->langId . '
                    GROUP BY cg.id_customer
                ) groupp ON customer.id_customer = groupp.id_customer
                LEFT JOIN (
                    SELECT DISTINCT id_customer FROM ' . _DB_PREFIX_ . 'customer_group
                ) for_null_group ON customer.id_customer = for_null_group.id_customer
                LEFT JOIN ' . _DB_PREFIX_ . 'risk risk ON customer.id_risk = risk.id_risk
                LEFT JOIN ' . _DB_PREFIX_ . 'risk_lang risk_lang ON risk.id_risk = risk_lang.id_risk AND risk_lang.id_lang = ' . $this->langId . '
                ';

        $this->sql .= '
                WHERE customer.deleted = 0
            ';

        // Filter By Customer
        $customerCond = '';
        if ($customers) {
            if ($customersType === 'unselected') {
                $customerCond = 'customer.id_customer NOT IN (' . $customers . ')';
            } else {
                $customerCond = 'customer.id_customer IN (' . $customers . ')';
            }
        }
        if ($customerCond) {
            $this->sql .= ' 
                        AND (' . $customerCond . ') ';
        }

        // Filter By Group
        $this->sql .= $groupsCond2;

        // 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 !== 'customer.id_customer') {
            $this->sql .= ', customer.id_customer ASC';
        }

        $this->sql .= ' LIMIT ' . $this->offset . ', ' . $this->limit;

//        d($this->sql);
        return Db::getInstance()->executeS($this->sql);
    }
}