<?php
class ModelAccountAddress extends Model {
	public function addAddress($data) {

				if ($this->config->get('fields_register_brazil_status')){
					$data['apelido']   = ucwords($data['apelido']);
					$data['firstname'] = ucwords($data['firstname']);
					$data['lastname']  = ucwords($data['lastname']);
					$data['address_1'] = ucwords($data['address_1']);
					$data['address_2'] = ucwords($data['address_2']);
					$data['city']      = ucwords($data['city']);
					$data['numero']    = preg_replace('/([^0-9])/i','',$data['numero']);
					$data['postcode']  = preg_replace('/([^0-9])/i','',$data['postcode']);

					$this->db->query("INSERT INTO " . DB_PREFIX . "address SET customer_id = '" . (int)$this->customer->getId() . "', firstname = '" . $this->db->escape($data['firstname']) . "', lastname = '" . $this->db->escape($data['lastname']) . "', company = '', company_id = '" . $this->db->escape(isset($data['company_id']) ? $data['company_id'] : '') . "', tax_id = '" . $this->db->escape(isset($data['tax_id']) ? $data['tax_id'] : '') . "', address_1 = '" . $this->db->escape($data['address_1']) . "', address_2 = '" . $this->db->escape($data['address_2']) . "', postcode = '" . $this->db->escape($data['postcode']) . "', city = '" . $this->db->escape($data['city']) . "', zone_id = '" . (int)$data['zone_id'] . "', country_id = '" . (int)$data['country_id'] . "', apelido = '" . $this->db->escape($data['apelido']) . "', numero = '" . $this->db->escape($data['numero']) . "', complemento = '" . $this->db->escape($data['complemento']) . "'");
				}else{
					$this->db->query("INSERT INTO " . DB_PREFIX . "address SET customer_id = '" . (int)$this->customer->getId() . "', firstname = '" . $this->db->escape($data['firstname']) . "', lastname = '" . $this->db->escape($data['lastname']) . "', company = '', company_id = '" . $this->db->escape(isset($data['company_id']) ? $data['company_id'] : '') . "', tax_id = '" . $this->db->escape(isset($data['tax_id']) ? $data['tax_id'] : '') . "', address_1 = '" . $this->db->escape($data['address_1']) . "', address_2 = '" . $this->db->escape($data['address_2']) . "', postcode = '" . $this->db->escape($data['postcode']) . "', city = '" . $this->db->escape($data['city']) . "', zone_id = '" . (int)$data['zone_id'] . "', country_id = '" . (int)$data['country_id'] . "'");
				}


		$address_id = $this->db->getLastId();

		if (!empty($data['default'])) {
			$this->db->query("UPDATE " . DB_PREFIX . "customer SET address_id = '" . (int)$address_id . "' WHERE customer_id = '" . (int)$this->customer->getId() . "'");
		}

		return $address_id;
	}

	public function editAddress($address_id, $data) {

				if ($this->config->get('fields_register_brazil_status')){
					$data['apelido']   = ucwords($data['apelido']);
					$data['firstname'] = ucwords($data['firstname']);
					$data['lastname']  = ucwords($data['lastname']);
					$data['address_1'] = ucwords($data['address_1']);
					$data['address_2'] = ucwords($data['address_2']);
					$data['city']      = ucwords($data['city']);
					$data['numero']    = preg_replace('/([^0-9])/i','',$data['numero']);
					$data['postcode']  = preg_replace('/([^0-9])/i','',$data['postcode']);

					$this->db->query("UPDATE " . DB_PREFIX . "address SET firstname = '" . $this->db->escape($data['firstname']) . "', lastname = '" . $this->db->escape($data['lastname']) . "', company = '" . $this->db->escape($data['company']) . "', company_id = '" . $this->db->escape(isset($data['company_id']) ? $data['company_id'] : '') . "', tax_id = '" . $this->db->escape(isset($data['tax_id']) ? $data['tax_id'] : '') . "', address_1 = '" . $this->db->escape($data['address_1']) . "', address_2 = '" . $this->db->escape($data['address_2']) . "', postcode = '" . $this->db->escape($data['postcode']) . "', city = '" . $this->db->escape($data['city']) . "', zone_id = '" . (int)$data['zone_id'] . "', country_id = '" . (int)$data['country_id'] . "', apelido = '" . $this->db->escape($data['apelido']) . "', numero = '" . $this->db->escape($data['numero']) . "', complemento = '" . $this->db->escape($data['complemento']) . "' WHERE address_id  = '" . (int)$address_id . "' AND customer_id = '" . (int)$this->customer->getId() . "'");
					
					if (isset($data['cnpj']) && $this->config->get('fields_register_brazil_enable_endereco_revisao')){
						$this->db->query("UPDATE " . DB_PREFIX . "customer SET approved = 0 where customer_id = '" . (int)$this->customer->getId() . "'");
					}
				}else{
					$this->db->query("UPDATE " . DB_PREFIX . "address SET firstname = '" . $this->db->escape($data['firstname']) . "', lastname = '" . $this->db->escape($data['lastname']) . "', company = '" . $this->db->escape($data['company']) . "', company_id = '" . $this->db->escape(isset($data['company_id']) ? $data['company_id'] : '') . "', tax_id = '" . $this->db->escape(isset($data['tax_id']) ? $data['tax_id'] : '') . "', address_1 = '" . $this->db->escape($data['address_1']) . "', address_2 = '" . $this->db->escape($data['address_2']) . "', postcode = '" . $this->db->escape($data['postcode']) . "', city = '" . $this->db->escape($data['city']) . "', zone_id = '" . (int)$data['zone_id'] . "', country_id = '" . (int)$data['country_id'] . "' WHERE address_id  = '" . (int)$address_id . "' AND customer_id = '" . (int)$this->customer->getId() . "'");
				}


		if (!empty($data['default'])) {
			$this->db->query("UPDATE " . DB_PREFIX . "customer SET address_id = '" . (int)$address_id . "' WHERE customer_id = '" . (int)$this->customer->getId() . "'");
		}
	}







	public function deleteAddress($address_id) {
		$this->db->query("DELETE FROM " . DB_PREFIX . "address WHERE address_id = '" . (int)$address_id . "' AND customer_id = '" . (int)$this->customer->getId() . "'");
	}

	public function getAddress($address_id) {
		$address_query = $this->db->query("SELECT DISTINCT * FROM " . DB_PREFIX . "address WHERE address_id = '" . (int)$address_id . "' AND customer_id = '" . (int)$this->customer->getId() . "'");

		if ($address_query->num_rows) {
			$country_query = $this->db->query("SELECT * FROM `" . DB_PREFIX . "country` WHERE country_id = '" . (int)$address_query->row['country_id'] . "'");

			if ($country_query->num_rows) {
				$country = $country_query->row['name'];
				$iso_code_2 = $country_query->row['iso_code_2'];
				$iso_code_3 = $country_query->row['iso_code_3'];
				$address_format = $country_query->row['address_format'];
			} else {
				$country = '';
				$iso_code_2 = '';
				$iso_code_3 = '';
				$address_format = '';
			}

			$zone_query = $this->db->query("SELECT * FROM `" . DB_PREFIX . "zone` WHERE zone_id = '" . (int)$address_query->row['zone_id'] . "'");

			if ($zone_query->num_rows) {
				$zone = $zone_query->row['name'];
				$zone_code = $zone_query->row['code'];
			} else {
				$zone = '';
				$zone_code = '';
			}

			$address_data = array(
				'firstname'      => $address_query->row['firstname'],
				'lastname'       => $address_query->row['lastname'],
				'company'        => $address_query->row['company'],
				'company_id'     => $address_query->row['company_id'],
				'tax_id'         => $address_query->row['tax_id'],
				'address_1'      => $address_query->row['address_1'],
				'address_2'      => $address_query->row['address_2'],

				'apelido'     => ($this->config->get('fields_register_brazil_status')) ? $address_query->row['apelido'] : '',
				'numero'      => ($this->config->get('fields_register_brazil_status')) ? $address_query->row['numero'] : '',
				'complemento' => ($this->config->get('fields_register_brazil_status')) ? $address_query->row['complemento'] : '',

				'postcode'       => $address_query->row['postcode'],
				'city'           => $address_query->row['city'],
				'zone_id'        => $address_query->row['zone_id'],
				'zone'           => $zone,
				'zone_code'      => $zone_code,
				'country_id'     => $address_query->row['country_id'],
				'country'        => $country,
				'iso_code_2'     => $iso_code_2,
				'iso_code_3'     => $iso_code_3,
				'address_format' => $address_format
			);

			return $address_data;
		} else {
			return false;
		}
	}

	public function getAddresses() {
		$address_data = array();
		if ($this->config->get('fields_register_brazil_enable_endereco_padrao')){ 
			$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "address WHERE address_id = '" . (int)$this->customer->getAddressId() . "'");
		}else{
			$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "address WHERE customer_id = '" . (int)$this->customer->getId() . "'");
		}

		foreach ($query->rows as $result) {
			$country_query = $this->db->query("SELECT * FROM `" . DB_PREFIX . "country` WHERE country_id = '" . (int)$result['country_id'] . "'");

			if ($country_query->num_rows) {
				$country = $country_query->row['name'];
				$iso_code_2 = $country_query->row['iso_code_2'];
				$iso_code_3 = $country_query->row['iso_code_3'];
				$address_format = $country_query->row['address_format'];
			} else {
				$country = '';
				$iso_code_2 = '';
				$iso_code_3 = '';
				$address_format = '';
			}

			$zone_query = $this->db->query("SELECT * FROM `" . DB_PREFIX . "zone` WHERE zone_id = '" . (int)$result['zone_id'] . "'");

			if ($zone_query->num_rows) {
				$zone = $zone_query->row['name'];
				$zone_code = $zone_query->row['code'];
			} else {
				$zone = '';
				$zone_code = '';
			}

			$address_data[$result['address_id']] = array(
				'address_id'     => $result['address_id'],
				'firstname'      => $result['firstname'],
				'lastname'       => $result['lastname'],
				'company'        => $result['company'],
				'company_id'     => $result['company_id'],
				'tax_id'         => $result['tax_id'],
				'address_1'      => $result['address_1'],
				'address_2'      => $result['address_2'],

				'apelido'     => ($this->config->get('fields_register_brazil_status')) ? $result['apelido'] : '',
				'numero'      => ($this->config->get('fields_register_brazil_status')) ? $result['numero'] : '',
				'complemento' => ($this->config->get('fields_register_brazil_status')) ? $result['complemento'] : '',

				'postcode'       => $result['postcode'],
				'city'           => $result['city'],
				'zone_id'        => $result['zone_id'],
				'zone'           => $zone,
				'zone_code'      => $zone_code,
				'country_id'     => $result['country_id'],
				'country'        => $country,
				'iso_code_2'     => $iso_code_2,
				'iso_code_3'     => $iso_code_3,
				'address_format' => $address_format
			);
		}

		return $address_data;
	}



	public function getAddressesBrasil() {
		$address_data = array();

		$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "address WHERE customer_id = '" . (int)$this->customer->getId() . "'");

		foreach ($query->rows as $result) {
			$country_query = $this->db->query("SELECT * FROM `" . DB_PREFIX . "country` WHERE country_id = '" . (int)$result['country_id'] . "'");

			if ($country_query->num_rows) {
				$country = $country_query->row['name'];
				$iso_code_2 = $country_query->row['iso_code_2'];
				$iso_code_3 = $country_query->row['iso_code_3'];
				$address_format = $country_query->row['address_format'];
			} else {
				$country = '';
				$iso_code_2 = '';
				$iso_code_3 = '';
				$address_format = '';
			}

			$zone_query = $this->db->query("SELECT * FROM `" . DB_PREFIX . "zone` WHERE zone_id = '" . (int)$result['zone_id'] . "'");

			if ($zone_query->num_rows) {
				$zone = $zone_query->row['name'];
				$zone_code = $zone_query->row['code'];
			} else {
				$zone = '';
				$zone_code = '';
			}

			$address_data[$result['address_id']] = array(
				'address_id'     => $result['address_id'],
				'firstname'      => $result['firstname'],
				'lastname'       => $result['lastname'],
				'company'        => $result['company'],
				'company_id'     => $result['company_id'],
				'tax_id'         => $result['tax_id'],
				'address_1'      => $result['address_1'],
				'address_2'      => $result['address_2'],
				'postcode'       => $result['postcode'],
				'city'           => $result['city'],
				'zone_id'        => $result['zone_id'],
				'zone'           => $zone,
				'zone_code'      => $zone_code,
				'country_id'     => $result['country_id'],
				'country'        => $country,
				'iso_code_2'     => $iso_code_2,
				'iso_code_3'     => $iso_code_3,
				'local'          => $result['apelido'],
				'numero'         => $result['numero'],
				'complemento'    => $result['complemento'],
				'address_format' => $address_format
			);
		}

		return $address_data;
	}

	public function getTotalAddresses() {
		$query = $this->db->query("SELECT COUNT(*) AS total FROM " . DB_PREFIX . "address WHERE customer_id = '" . (int)$this->customer->getId() . "'");

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


	public function editAddressBrasil($data) {


	    $query = $this->db->query("SELECT `address_id` FROM `" . DB_PREFIX . "customer`  WHERE customer_id = '" . (int)$this->customer->getId() . "'");
	    $customer_dress_id  = $query->row['address_id'];
			$enderecos         = $this->getAddresses();
			$pay_endereco_id   = "";
			$ship_endereco_id  = "";

			$data['pay_numero']    = preg_replace('/([^0-9])/i','',$data['pay_numero']);
			$data['pay_postcode']  = preg_replace('/([^0-9])/i','',$data['pay_postcode']);	
			
			$data['ship_numero']    = preg_replace('/([^0-9])/i','',$data['ship_numero']);
			$data['ship_postcode']  = preg_replace('/([^0-9])/i','',$data['ship_postcode']);				

			foreach ($enderecos as $endereco){

				if ($endereco['address_id'] == $customer_dress_id ){
							//throw new exception($endereco['address_id']);

					$pay_endereco_id = $customer_dress_id;
				}else{
					$ship_endereco_id = $endereco['address_id'];
					}
			}
		//throw new exception("MINHA QUERY PADRAO= ". $customer_dress_id."  PADRAO= ".$pay_endereco_id."   ENTREGA= ".$ship_endereco_id);

		$this->db->query("UPDATE " . DB_PREFIX . "address SET    numero = '" .$this->db->escape($data['pay_numero'])."', complemento = '" .$this->db->escape($data['pay_complemento'])."', apelido = '" .$this->db->escape($data['pay_local']). "',  address_1 = '" . $this->db->escape($data['pay_address_1']) . "', address_2 = '" . $this->db->escape($data['pay_address_2']) . "', postcode = '" . $this->db->escape($data['pay_postcode']) . "', city = '" . $this->db->escape($data['pay_city']) . "', zone_id = '" . (int)$data['pay_zone_id'] . "', country_id = '" . (int)$data['pay_country_id'] . "' WHERE address_id  = '" . (int)$pay_endereco_id . "' AND customer_id = '" . (int)$this->customer->getId() . "'");

		if ($ship_endereco_id != ''){
		  $this->db->query("UPDATE " . DB_PREFIX . "address SET  numero = '" .$this->db->escape($data['ship_numero'])."', complemento = '" .$this->db->escape($data['ship_complemento'])."', apelido = '" .$this->db->escape($data['ship_local']). "', address_1 = '" . $this->db->escape($data['ship_address_1']) . "', address_2 = '" . $this->db->escape($data['ship_address_2']) . "', postcode = '" . $this->db->escape($data['ship_postcode']) . "', city = '" . $this->db->escape($data['ship_city']) . "', zone_id = '" . (int)$data['ship_zone_id'] . "', country_id = '" . (int)$data['ship_country_id'] . "' WHERE address_id  = '" . (int)$ship_endereco_id . "' AND customer_id = '" . (int)$this->customer->getId() . "'");
		}

	}



	public function getAddressEntrega() {

		$query = $this->db->query("SELECT address_id FROM " . DB_PREFIX . "address  WHERE customer_id = '" . (int)$this->customer->getId() . "' AND address_id <> (select address_id FROM ". DB_PREFIX .   "customer WHERE customer_id = '" . (int)$this->customer->getId() . "')");

		if($query->rows){
		  return true;
		}else{
		  return false;
		}


	}
  
  public function getAddressDeafult() {
	
		$query = $this->db->query("select a.address_id from oc_customer c inner join oc_address a on (c.address_id = a.address_id) where c.customer_id = '" . (int)$this->customer->getId() . "' ");
	
		if($query->rows){
		  return $query->row['address_id'];
		}else{
		  return false;
		}

	}  
	
	public function getSiglaByZone($zone_id) {
		$query = $this->db->query("SELECT DISTINCT (code) as sigla FROM " . DB_PREFIX . "zone WHERE zone_id = '" . (int)$zone_id . "'");
		if (isset($query->row['sigla'])){
            return $query->row['sigla'];
    }		
	}  




}
?>
