<?php
/*
 * @version			$Id: index.php 10381 2008-06-01 03:35:53Z $
 * @package			Carteasy
 * @copyright		Copyright (C) 2005 - 2011 Wlanium
 * @license			proprietary
 * @author			Thomas Bartelt
 * Carteasy is a web shop system
 */

include_once './core/orderstatus.class.php';

class CustomerHelper {

	//=========================================
	//
	//	THIS CLASS IS STATIC ONLY
	//
	//=========================================
	private function __construct() {}

	private function __clone() {}

	//=========================================
	//
	//	THE PUBLIC INTERFACE
	//
	//=========================================

	public static function get_customer_email($customer_id) {
		$db = Registry::get('base')->db;

		$sql = "SELECT email FROM customers WHERE id=".$db->real_escape_string($customer_id);

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

		if ($result->num_rows > 0) {
			return $result->fetch_object()->email;
		}

		return false;
	}

	public static function getCustomerBankingAccount($customer_id) {
		$db = Registry::get('base')->db;

		$sql = "SELECT
					bank, bank_number, account_holder, account_number, iban, swift_bic
				FROM
					customers
				WHERE
					id=".$db->real_escape_string($customer_id);

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

		if ($result->num_rows > 0) {
			return $result->fetch_object();
		}

		return false;
	}

	public static function get_default_address_country($customer_id) {
		$db = Registry::get('base')->db;

		$sql = "SELECT ca.country FROM customer_addresses ca JOIN customers c ON ca.id=c.default_address
				WHERE c.id=".$db->real_escape_string($customer_id);

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

		if ($result->num_rows > 0) {
			$obj = $result->fetch_object();

			return $obj->country;
		}

		$sql = "SELECT country FROM customer_addresses WHERE customer_id=".$db->real_escape_string($customer_id);

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

		if ($result->num_rows > 0) {
			$obj = $result->fetch_object();

			return $obj->country;
		}

		return false;
	}

	public static function create_customer_address($data) {
		$db = Registry::get('base')->db;

		$data_line = "";
		$value_line = "";

		$size = count($data);

		$i = 1;
		foreach ($data as $key => $value) {
			if (is_numeric($value)) {
				$data_line .= $key;
				$value_line .= $value;
			} else {
				$data_line .= $key;
				$value_line .= "'".$value."'";
			}

			if ($i < $size) {
				$data_line .= ", ";
				$value_line .= ", ";
				$i++;
			}
		}

		$sql = "INSERT INTO customer_addresses(".$data_line.") ";
		$sql .= "VALUES (".$value_line.");";

		if ($db->query($sql)) {
			return $db->insert_id;
		}

		return false;
	}

	public static function update_customer_address($data, $id) {
		$db = Registry::get('base')->db;

		$set_line = "";
		$size = count($data);

		$i = 1;
		foreach ($data as $key => $value) {
			$set_line .= $key;
			$set_line .= "=";

			if (is_numeric($value)) {
				$set_line .= $value;
			} else {
				$set_line .= "'".$value."'";
			}

			if ($i < $size) {
				$set_line .= ", ";
				$i++;
			}
		}

		$sql = "UPDATE customer_addresses ";
		$sql .= "SET ".$set_line." WHERE id=".$db->real_escape_string($id);

		if ($db->query($sql)) {
			return $id;
		}

		return false;
	}

	public static function get_customer_country($customer_id) {
		$db = Registry::get('base')->db;

		$sql = "SELECT ca.country FROM customer_addresses ca JOIN customers c ON ca.id = c.default_address 
				WHERE c.id=".$db->real_escape_string($customer_id);

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

		if ($result->num_rows > 0) {
			return $result->fetch_object()->country;
		}

		return false;
	}

	public static function get_customer_address($address_id) {
		$db = Registry::get('base')->db;

		$sql = "SELECT * FROM customer_addresses WHERE id=".$db->real_escape_string($address_id);

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

		if ($result->num_rows > 0) {
			return $result->fetch_object();
		}

		else return false;
	}

	public static function get_revisor_data($id) {
		$db = Registry::get('base')->db;

		$sql = "SELECT * FROM customers WHERE id = ".$db->real_escape_string($id);

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

		if ($result->num_rows > 0) {
			$customer = $result->fetch_object();

			$return = $customer->firstname.' '.$customer->surname;

			if ($customer->admin_role_id == 0) {
				$return = ' (Kunde)';
			}

			return $return;
		}

		return false;
	} // end get_revisor_data

	public function email_exists($email) {
		$db = Registry::get('base')->db;

		$sql = "SELECT * 
				FROM customers
				WHERE email='".$db->real_escape_string($email)."'";

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

		if ($result->num_rows > 0) {
			return true;
		}

		return false;
	} // end email_exists

	public function email_has_changed($email, $customer_id) {
		$db = Registry::get('base')->db;

		$sql = "SELECT email FROM customers where id = ".$db->real_escape_string($customer_id);

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

		// not safe but if there are no results this function returns crap anyway
		$old_email = $result->fetch_object()->email;

		if ($old_email == $email) {
			return false;
		}

		return true;
	}

	public static function delete_customer($id) {
		$db = Registry::get('base')->db;

		$sql = "DELETE FROM customers
				WHERE id=".$db->real_escape_string($id);

		return $db->query($sql);
	} // end delete_customer

	public static function delete_customers($ids) {
		$db = Registry::get('base')->db;

		$sql = "DELETE FROM customers
				WHERE id IN (".$db->real_escape_string($ids).")";

		return $db->query($sql);
	} // end delete_customers

	public static function verify_customer_data($data, $address, $restrictive = true) {
		$verification = 'success';
		/*if ($data['group_id'] == '0') {
			$verification = 'error';
			$return['group_id'] = "Kundengruppe ist ein Pflichtfeld";
		}

		// firstname
		$cleaned = trim($data['firstname']);
		if (!isset($data['firstname']) || $cleaned == '') {
			$verification = 'error';
			$return['firstname'] = "Vorname ist ein Pflichtfeld";
		}

		// nachname
		$cleaned = trim($data['surname']);
		if (!isset($data['surname']) || $cleaned == '') {
			$verification = 'error';
			$return['surname'] = "Nachname ist ein Pflichtfeld";
		}

		// email
		if (!isset($data['id'])) {
			$cleaned = trim($data['email']);
			if (!isset($data['email']) || $cleaned == '') {
				//$verification = 'error';
				//$return['email'] = "E-Mail ist ein Pflichtfeld";
			} else {
				if (CustomerHelper::email_exists($data['email'])) {
					$verification = 'error';
					$return['email'] = "E-Mail Adresse ist bereits vergeben";
				}
			}
		} else {
			if (CustomerHelper::email_has_changed($data['email'], $data['id'])) {
				if (CustomerHelper::email_exists($data['email'])) {
					$verification = 'error';
					$return['email'] = "E-Mail Adresse ist bereits vergeben";
				}
			}
		}

		// password
		if (!isset($data['id'])) {
			$cleaned = trim($data['pass1']);
			if (!isset($data['pass1']) || $cleaned == '') {
				$verification = 'error';
				$return['pass1'] = "Passwort ist ein Pflichtfeld";
			}

			$cleaned = trim($data['pass2']);
			if (!isset($data['pass2']) || $cleaned == '') {
				$verification = 'error';
				$return['pass2'] = "Passwort ist ein Pflichtfeld";
			}
		}

		// birthdate
		/*if ($restrictive) {
			$cleaned = trim($data['burth_date']);
			if (!isset($data['burth_date']) || $cleaned == '') {
				$verification = 'error';
				$return['burth_date'] = "Geburtsdatum ist ein Pflichtfeld";
			}
		}*/

		// address
		/*if (!isset($data['id'])) {
			$return['address'] = CustomerHelper::verify_customer_address($address);
			if ($return['address']['status'] == 'error') {
				$verification = 'error';
			}
		}*/

		if ($verification == 'success') {
			return array('status' => 'success');
		}

		return array('status' => 'error', 'data' => $return);
	} // end verify_customer_data

	public static function verify_customer_address($address) {
		$verification = 'success';

		// honorific
		$cleaned = trim($address['honorific']);
		if (!isset($address['honorific']) || $cleaned == '') {
			$verification = 'error';
			$return['honorific'] = "Adress Anrede ist ein Pflichtfeld";
		}
		// firstname
		$cleaned = trim($address['firstname']);
		if (!isset($address['firstname']) || $cleaned == '') {
			$verification = 'error';
			$return['firstname'] = "Adress Vorname ist ein Pflichtfeld";
		}

		// surname
		$cleaned = trim($address['surname']);
		if (!isset($address['surname']) || $cleaned == '') {
			$verification = 'error';
			$return['surname'] = "Adress Nachname ist ein Pflichtfeld";
		}

		// street
		$cleaned = trim($address['street']);
		if (!isset($address['street']) || $cleaned == '') {
			$verification = 'error';
			$return['street'] = "Stra&szlig; ist ein Pflichtfeld";
		}

		// house_number
		$cleaned = trim($address['house_number']);
		if (!isset($address['house_number']) || $cleaned == '') {
			$verification = 'error';
			$return['house_number'] = "Hausnummer ist ein Pflichtfeld";
		}

		// zip_code
		$cleaned = trim($address['zip_code']);
		if (!isset($address['zip_code']) || $cleaned == '') {
			$verification = 'error';
			$return['zip_code'] = "Postleitzahl ist ein Pflichtfeld";
		}

		// city
		$cleaned = trim($address['city']);
		if (!isset($address['city']) || $cleaned == '') {
			$verification = 'error';
			$return['city'] = "Ort ist ein Pflichtfeld";
		}

		// country
		$cleaned = trim($address['country']);
		if (!isset($address['country']) || $cleaned == '') {
			$verification = 'error';
			$return['country'] = "Land ist ein Pflichtfeld";
		}

		if ($verification == 'success') {
			return array('status' => 'success');
		}

		return array('status' => 'error', 'data' => $return);
	}


	public static function getAddressByIds($customerId, $addressId) {
		$db = Registry::get('base')->db;

		$query = "
			SELECT
				company, honorific, firstname, surname, street, house_number, 
				zip_code, city, country
			FROM
				customer_addresses
			WHERE
				customer_id = '".$db->real_escape_string($customerId)."'
			AND
				id = '".$db->real_escape_string($addressId)."'
		";

		return $db->query($query)->fetch_object();
	}

	public static function getCustomerDataForOrder($customerId) {
		$db = Registry::get('base')->db;

		$query = "SELECT
					CONCAT_WS(' ', firstname, surname) AS customer_name,
					id AS customer_id,
					number AS customer_number,
					tax_id AS customer_vat_reg_num
				FROM
					customers
				WHERE
					id = '".$db->real_escape_string($customerId)."'";

		return $db->query($query)->fetch_object();
	}

	public static function get_customer_group_id_by_customer_id($customer_id = false) {
		$db = Registry::get('base')->db;

		if ($customer_id) {
			$sql = "SELECT group_id FROM customers WHERE id=".$db->real_escape_string($customer_id);

			return $db->query($sql)->fetch_object()->group_id;
		}

		return false;
	}
}

?>