<?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/main.class.php';

#[\AllowDynamicProperties]
class Customer_group extends Main {

	protected $base_object;
	protected $db;

	public $list_table_config = array (
		'title' => 'Kundengruppen',
		'db_table' => 'customer_groups',
		'list_fields' => array(
			array(
				'db_field' => 'name',
				'name' => 'Bezeichnung',
				'sortable' => 1
			),
			array(
				'db_field' => 'description',
				'name' => 'Beschreibung',
				'sortable' => 1
			),
			array(
				'db_field' => 'adaptPricesFromId',
				'name' => 'Preise',
				'rewrite_function' => 'show_price_source',
				'sortable' => 1
			),
			array(
				'db_field' => 'show_tax',
				'name' => 'MwSt',
				'rewrite_function' => 'show_tax_state',
				'sortable' => 1
			)
		),
		'search_fields' => array('name', 'description'),
		'db_id_field' => 'id',
		'db_selectable_field' => 'name',
		'edit_link' => 'index.php?admin_modul=admin_object_edit&object=Customer_group&object_id=',
		'toolbar' => array(
			'delete' => '1',
			'new' => 'index.php?admin_modul=admin_object_edit&object=Customer_group',
			'copy' => 0,
			'select_all' => 1,
			'edit' => 0,
			'actions' => 0,
			'filter' => 0,
			'search' => 1
		),
		'edit_title' => 'Kundengruppe',
		'edit_fields' => array (
			array(
				'name' => 'Allgemein',
				'type' => 'form_title'
			),
			array(
				'db_field' => 'name',
				'name' => 'Name',
				'type' => 'text'
			),
			array(
				'db_field' => 'description',
				'name' => 'Beschreibung',
				'type' => 'text'
			),
			array(
				'name' => 'Einstellungen',
				'type' => 'form_title'
			),
			array(
				'db_field' => 'show_tax',
				'name' => 'MwSt zeigen',
				'type' => 'int',
				'values' => 'show_tax_values'
			),
			array(
				'db_field' => 'price_tax_text',
				'name' => 'MwSt Preistext',
				'type' => 'text'
			),
			array(
				'db_field' => 'price_tax_text_free_shipping',
				'name' => 'MwSt Preistext - Versandkostenfrei',
				'type' => 'text'
			),
			array(
				'name' => 'Preise übernehmen von Gruppe',
				'type' => 'int',
				'db_field' => 'adaptPricesFromId',
				'values' => 'show_price_source'
			),
			array(
				'name' => 'Status nach Kundenregistrierung',
				'type' => 'int',
				'db_field' => 'state_after_registration',
				'values' => 'first_state_values'
			),
			array(
				'name' => 'Kundennummer überschreibbar',
				'type' => 'int',
				'db_field' => 'customer_number_edit',
				'values' => 'customer_number_values'
			)
		),
		'edit_mandatory_fields' => array('firstname', 'surename', 'email'),
		'edit_toolbar' => array(
			'close' => 'index.php?admin_modul=admin_object_list&object=Customer_group',
			'copy' => 0,
			'undo' => 0,
			'redo' => 0,
			'save' => 1,
			'send' => 0,
			'delete' => 1
		),
	);

	private $object_fields = array(
		'name' => 'text',
		'description' => 'text',
		'show_tax' => 'integer',
		'price_tax_text' => 'text',
		'state_after_registration' => 'integer',
		'adaptPricesFromId' => 'integer'
	);
	public $structure_id = 0;
	private $config;

	public function __construct($base_object) {
		parent::__construct($base_object);
		$this->config_object = $base_object->config;
		$this->db = $base_object->db;
	}

	public function show_tax_state() {
		return array (
			'0' => 'ohne',
			'1' => 'mit'
		);
	}

	public function customer_number_values() {
		return array (
			'1' => 'Ja',
			'0' => 'Nein'
		);
	}

	public function show_tax_values() {
		return array (
			'1' => 'Ja',
			'0' => 'Nein'
		);
	}

	public function first_state_values() {
		return array (
			'2' => 'Aktiv und eingeloggt',
			'1' => 'Aktiv',
			'0' => 'gesperrt'
		);
	}



	public function show_price_source() {
		$data = array(
			'0' => 'Eigene Preise',
		);
		$sql = "SELECT id, name FROM customer_groups";
		$result = $this->db->query($sql);
		while ($obj = $result->fetch_object()) {
			$data[$obj->id] = 'von '.$obj->name;
		}
		return $data;
	}

	public function get_id() {
		return $this->id;
	}

	public function set_id($id) {
		if ($id) {
			$this->id = $id;
		}

		return $id;
	}

	public function get_all_groups() {
		// get all groups from database
		$sql = "SELECT * FROM customer_groups ORDER BY isLocked DESC, name";
		$result = $this->db->query($sql);
		$data = array();
		while ($obj = $result->fetch_object()) {
			$data[$obj->id] = $obj;
		}
		return $data;
	}

	public function get_all_price_groups() {
		// get all groups from database
		$sql = "SELECT * FROM customer_groups WHERE adaptPricesFromId=0 ORDER BY isLocked DESC, name";
		$result = $this->db->query($sql);
		$data = array();
		while ($obj = $result->fetch_object()) {
			$data[$obj->id] = $obj;
		}
		return $data;
	}

	// TODO: standard interface for all classes
	public function get_all() {
		return $this->get_all_groups();
	}

	public function get_data($id = false) {
		$sql = "SELECT * FROM customer_groups WHERE id=";

		if ($id) {
			$sql .= $this->db->real_escape_string($id);
		} else if ($this->id) {
			$sql .= $this->db->real_escape_string($this->id);
		} else {
			return false;
		}

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

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

		return false;
	}// end get_data


	public function get_group_price_id($id = false) {
		$sql = "SELECT * FROM customer_groups WHERE id=";

		if ($id) {
			$sql .= $this->db->real_escape_string($id);
		} else if ($this->id) {
			$sql .= $this->db->real_escape_string($this->id);
		} else {
			return false;
		}

		$result = $this->db->query($sql);
		$obj = $result->fetch_object();
		if ($obj->adaptPricesFromId) {
			return $obj->adaptPricesFromId;
		} else {
			return $obj->id;
		}

		return false;
	}// end get_data

	public function load_data() {
		$sql = "SELECT * FROM customer_groups WHERE id = ".$this->id;
		$result = $this->db->query($sql);
		$obj = $result->fetch_object();
		foreach ($obj as $key => $value) {
			if ($obj->adaptPricesFromId == 0) {
				$obj->price_group_id = $obj->id;
			} else {
				$obj->price_group_id = $obj->adaptPricesFromId;
			}
			$this->{$key} = $value;
		}
		return;
	}

// end get_data

	public function delete() {
		$sql = "DELETE FROM
					customer_groups
				WHERE 
					isLocked = 0
				AND
					id=".$this->id;
		$this->db->query($sql);
		return;
	}

	public function data_filter($request) {
		$data = array();
		foreach ($this->object_fields as $field_name => $var_type) {
			if (isset($request[$field_name])) {
				if ($var_type == 'text') {
					$data[$field_name] = $request[$field_name];
				} else {
					$data[$field_name] = (int) $request[$field_name];
				}
			}
		}
		return $data;
	}

	public function create($data) {
		if ($data) {
			$sql = "INSERT INTO customer_groups SET ";
			foreach ($data as $var_name => $value) {
				$value = $this->db->real_escape_string($value);
				if ($this->object_fields[$var_name] == 'integer') {
					$sql .= $var_name.' = '.$value.', ';
				} else {
					$sql .= $var_name.' = "'.$value.'", ';
				}
			}
			$sql = substr($sql, 0, -2);
			$this->db->query($sql);
			$this->id = $this->db->insert_id;
		}
		return;
	}

	public function update($data) {
		if ($data) {
			$sql = "UPDATE customer_groups SET ";
			foreach ($data as $var_name => $value) {
				$value = $this->db->real_escape_string($value);
				if ($this->object_fields[$var_name] == 'integer') {
					$sql .= $var_name.'='.$value.', ';
				} else {
					$sql .= $var_name.'="'.$value.'", ';
				}
			}
			$sql = substr($sql, 0, -2);
			$sql .= ' WHERE id='.$this->id;
			$this->db->query($sql);
		}
		return;
	}

	public function delete_by_id($id) {
		$rs = $this->db->query("DELETE FROM customer_groups WHERE isLocked = 0 AND id=$id;");
	}


	public static function get_taxation_type($id) {

		$db = Registry::get('base')->db;

		$tax_obj = $db->query("
			SELECT
				show_tax
			FROM
				customer_groups
			WHERE
				id = ".$id."
		")->fetch_object();

		return $tax_obj->show_tax;

	}

}

?>