Current File : /var/www/vinorea/modules/ipexportimport/classes/export/EIAAddressesExport.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 EIAAddressesExport extends EIAExport
{
    protected $xmlMainTag = 'Address';
    protected $xmlMainTagPl = 'Addresses';
    
    public function __construct($module)
    {
        parent::__construct($module);
        $this->entityNamePl = $module->l('Addresses', 'EIAAddressesExport');
    }

    protected function getDataFromDb()
    {
        if ($this->auto) {
            $addresses = pSQL(implode(',', $this->datatables['addresses']['data']));
            $addressesType = $this->datatables['addresses']['type'];
        } else {
            $addresses = pSQL(Tools::getValue('addresses_data'));
            $addressesType = Tools::getValue('addresses_type');
        }
        
        $eIHelper = new EIAHelper($this->module);
        
        $this->sql = '
            SELECT SQL_CALC_FOUND_ROWS ';
        if (in_array($this->fileType, ['xml'])) {
            $fieldsInTree = $eIHelper->getFieldsInTree('addresses');
            foreach ($this->selectedColumns as $k => $col) {
                $this->sql .= "
                    $k AS `{$fieldsInTree[$k]}`, ";
            }
        } else {
            foreach ($this->selectedColumns as $k => $col) {
                $this->sql .= "
                    $k AS `$col`, ";
            }
        }

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

        $this->sql .= '
            FROM 
            ' . _DB_PREFIX_ . 'address address
                LEFT JOIN
            ' . _DB_PREFIX_ . 'country_lang country_lang ON address.id_country = country_lang.id_country
                    AND country_lang.id_lang = ' . $this->langId . '
                LEFT JOIN
            ' . _DB_PREFIX_ . 'state state ON address.id_state = state.id_state
                LEFT JOIN
            ' . _DB_PREFIX_ . 'customer customer ON address.id_customer = customer.id_customer
                LEFT JOIN
            ' . _DB_PREFIX_ . 'manufacturer manufacturer ON address.id_manufacturer = manufacturer.id_manufacturer
                LEFT JOIN
            ' . _DB_PREFIX_ . 'supplier supplier ON address.id_supplier = supplier.id_supplier
                ';

        $this->sql .= '
                WHERE address.deleted = 0 AND address.id_country <> 0
            ';

        // Filter By Address
        $addressCond = '';
        if ($addresses) {
            if ($addressesType === 'unselected') {
                $addressCond = 'address.id_address NOT IN (' . $addresses . ')';
            } else {
                $addressCond = 'address.id_address IN (' . $addresses . ')';
            }
        }
        if ($addressCond) {
            $this->sql .= ' 
                        AND (' . $addressCond . ') ';
        }

        // 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 !== 'address.id_address') {
            $this->sql .= ', address.id_address ASC ';
        }
        
        $this->sql .= ' LIMIT ' . $this->offset . ', ' . $this->limit;

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