<?php
final class DModelSpecific4861ResellerReseller extends Model
{

  public function getFilters($data){

    $filter = '';

    if(!empty($data['codigo_municipio'])){
      $filter.= " and a.codigo_municipio = ".$data['codigo_municipio'];
    }

    return $filter;

  }

  public function getResellers($data = array()){

    $sql = 'SELECT 
              concat (c.firstname, \' \', c.lastname) as name,
              concat(a.address_2, \' / \', a.city) as endereco,
              c.email as email,
              c.celular as celular
            FROM oc_customer c INNER JOIN oc_address a ON (c.customer_id = a.customer_id)
            WHERE c.email !="" ';

    $sql.= $this->getFilters($data);

    $sort_data = array();

    if (isset($data['sort']) && in_array($data['sort'], $sort_data)) {
      $sql .= " ORDER BY " . $data['sort'];
    } else {
      $sql .= " ORDER BY a.address_id";
    }

    if (isset($data['order']) && ($data['order'] == 'DESC')) {
      $sql .= " DESC";
    } else {
      $sql .= " ASC";
    }

    if (isset($data['start']) || isset($data['limit'])) {
      if ($data['start'] < 0) {
        $data['start'] = 0;
      }

      if ($data['limit'] < 1) {
        $data['limit'] = 20;
      }

      $sql .= " LIMIT " . (int)$data['start'] . "," . (int)$data['limit'];
    }

    $query = $this->db->query($sql);

    return $query->rows;

  }

  public function getTotalResellers($data){

    $sql = 'SELECT count(c.customer_id) as total FROM oc_customer c INNER JOIN oc_address a ON (c.customer_id = a.customer_id) WHERE c.email !=""';

    $sql.= $this->getFilters($data);

    $sort_data = array();

    if (isset($data['sort']) && in_array($data['sort'], $sort_data)) {
      $sql .= " ORDER BY " . $data['sort'];
    } else {
      $sql .= " ORDER BY a.address_id";
    }

    if (isset($data['order']) && ($data['order'] == 'DESC')) {
      $sql .= " DESC";
    } else {
      $sql .= " ASC";
    }


    $query = $this->db->query($sql);

    return $query->row['total'];

  }


}