1   2   3   4   5   6   7   8   9  10  11  12  13  14  15  16  17  18  19  20  21  22  23  24  25  26  27  28  29  30  31  32  33  34  35  36  37  38  39  40  41  42  43  44  45  46  47  48  49  50  51  52  53  54  55  56  57  58  59  60  61  62  63  64  65  66  67  68  69  70  71  72  73  74  75  76  77  78  79  80  81  82  83  84  85  86  87  88  89  90  91  92  93  94  95  96  97  98  99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 
<?php
/* --------------------------------------------------------------
   CustomerGroupReader.inc.php 2018-01-30
   Gambio GmbH
   http://www.gambio.de
   Copyright (c) 2017 Gambio GmbH
   Released under the GNU General Public License (Version 2)
   [http://www.gnu.org/licenses/gpl-2.0.html]
   --------------------------------------------------------------
*/

/**
 * Class CustomerGroupReader
 *
 * @category   System
 * @package    CustomerGroup
 * @subpackage Repositories
 */
class CustomerGroupReader implements CustomerGroupReaderInterface
{
    /**
     * @var \CI_DB_query_builder
     */
    protected $queryBuilder;
    
    
    public function __construct(CI_DB_query_builder $queryBuilder)
    {
        $this->queryBuilder = $queryBuilder;
    }
    
    
    /**
     * Returns all customer group entities data as array.
     *
     * @return array
     */
    public function getAll()
    {
        $rawData    = $this->_getAllGroups();
        $configData = $this->_getDefaultValue();
        
        $resultData     = [];
        $customerGroups = [];
        
        foreach($rawData as $data)
        {
            if(isset($oldId) && $oldId !== (int)$data['customers_status_id'])
            {
                $resultData[]   = $customerGroups;
                $customerGroups = [];
            }
            $oldId = (int)$data['customers_status_id'];
            
            $customerGroups['id']                          = $oldId;
            $customerGroups['names'][$data['language_id']] = $data['customers_status_name'];
            $customerGroups['default']                     = $configData[0]['configuration_value']
                                                             === (string)$customerGroups['id'];
            
            $customerGroups['members']        = $this->_countMembers($oldId);
            $customerGroups['settings']       = $this->_getSettingsData($data);
            $customerGroups['configurations'] = $this->_getConfigurationsData($data);
        }
        if(count($rawData) > 0)
        {
            $resultData[] = $customerGroups;
        }
        
        return $resultData;
    }
    
    
    /**
     * Returns customer group entity data by the given id.
     *
     * @param \IntType $id
     *
     * @throws \EntityNotFoundException
     *
     * @return array
     */
    public function getById(IntType $id)
    {
        $rawData = $this->queryBuilder->select()
                                      ->from('customers_status')
                                      ->where('customers_status_id', $id->asInt())
                                      ->order_by('customers_status_id', 'asc')
                                      ->order_by('language_id', 'asc')
                                      ->get()
                                      ->result_array() ? : [];
        
        $configData = $this->_getDefaultValue();
        
        if(count($rawData) === 0)
        {
            throw new EntityNotFoundException('Customer group entity was not found with provided id "' . $id->asInt()
                                              . '"');
        }
        
        $result                   = ['id' => $id->asInt()];
        $result['default']        = $configData[0]['configuration_value'] === (string)$id->asInt();
        $result['settings']       = $this->_getSettingsData($rawData[0]);
        $result['configurations'] = $this->_getConfigurationsData($rawData[0]);
        $result['members']        = $this->_countMembers($id->asInt());
        
        $customerGroupsNames = [];
        
        foreach($rawData as $data)
        {
            $customerGroupsNames[$data['language_id']] = $data['customers_status_name'];
        }
        $result['names'] = $customerGroupsNames;
        
        return $result;
    }
    
    
    /**
     * Returns customer group counted members by id.
     *
     * @param $int
     *
     * @return int
     */
    protected function _countMembers($int)
    {
        $number = $this->queryBuilder->query('SELECT COUNT(`customers_status`) AS `total`
                                              FROM `customers`
                                              WHERE `customers_status`=' . $int)->result_array();
        
        return count($number) ? $number[0]['total'] : 0;
    }
    
    
    /**
     * Gets the default value from configuration table.protected
     *
     * @return array
     */
    protected function _getDefaultValue()
    {
        return $this->queryBuilder->select('configuration_value')
                                  ->from('configuration')
                                  ->where('configuration_key', 'DEFAULT_CUSTOMERS_STATUS_ID')
                                  ->get()
                                  ->result_array();
    }
    
    
    /**
     * Gets the Settings data from data array.
     *
     * @param array $customerGroupData
     *
     * @return array
     */
    protected function _getSettingsData(array $customerGroupData)
    {
        return [
            'public'             => (int)$customerGroupData['customers_status_public'] === 1,
            'otDiscount'         => (int)$customerGroupData['customers_status_ot_discount_flag'] === 1,
            'graduatedPrices'    => (int)$customerGroupData['customers_status_graduated_prices'] === 1,
            'showPrice'          => (int)$customerGroupData['customers_status_show_price'] === 1,
            'showPriceTax'       => (int)$customerGroupData['customers_status_show_price_tax'] === 1,
            'addTaxOt'           => (int)$customerGroupData['customers_status_add_tax_ot'] === 1,
            'discountAttributes' => (int)$customerGroupData['customers_status_discount_attributes'] === 1,
            'fsk18'              => (int)$customerGroupData['customers_fsk18'] === 1,
            'fsk18Display'       => (int)$customerGroupData['customers_fsk18_display'] === 1,
            'writeReviews'       => (int)$customerGroupData['customers_status_write_reviews'] === 1,
            'readReviews'        => (int)$customerGroupData['customers_status_read_reviews'] === 1,
        ];
    }
    
    
    /**
     * Gets the Configuration data from data array.
     *
     * @param array $customerGroupData
     *
     * @return array
     */
    protected function _getConfigurationsData(array $customerGroupData)
    {
        return [
            'minOrder'                 => $customerGroupData['customers_status_min_order'] ? (double)$customerGroupData['customers_status_min_order'] : null,
            'maxOrder'                 => $customerGroupData['customers_status_max_order'] ? (double)$customerGroupData['customers_status_max_order'] : null,
            'discount'                 => $customerGroupData['customers_status_discount'] ? (double)$customerGroupData['customers_status_discount'] : null,
            'otDiscount'               => $customerGroupData['customers_status_ot_discount'] ? (double)$customerGroupData['customers_status_ot_discount'] : null,
            'unallowedPaymentModules'  => $customerGroupData['customers_status_payment_unallowed'] !== '' ? explode(',',
                                                                                                                    $customerGroupData['customers_status_payment_unallowed']) : [],
            'unallowedShippingModules' => $customerGroupData['customers_status_shipping_unallowed'] !== '' ? explode(',',
                                                                                                                    $customerGroupData['customers_status_shipping_unallowed']) : [],
        ];
    }
    
    
    /**
     * Returns an array of all customer groups from customer_status table.
     *
     * @return array
     */
    protected function _getAllGroups()
    {
        return $this->queryBuilder->from('customers_status')
                                  ->order_by('customers_status_id', 'asc')
                                  ->order_by('language_id', 'asc')
                                  ->get()
                                  ->result_array() ? : [];
    }
}