<?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
 */

class Deliverer extends Main {

	protected $base_object;

	public $list_table_config = array (
		'title' => 'Zusteller',
		'db_table' => 'deliverers',
		'list_fields' => array(
			array(
				'db_field' => 'name',
				'name' => 'Zusteller',
				'sortable' => 1
			),
			array(
				'db_field' => 'active',
				'name' => 'Status',
				'rewrite_function' => 'state_values',
				'sortable' => 1
			)
		),
		'search_fields' => array('subject'),
		'db_id_field' => 'id',
		'edit_link' => 'index.php?admin_modul=admin_object_edit&object=Deliverer&object_id=',
		'toolbar' => array(
			'delete' => 'index.php?admin_modul=admin_object_edit&object=Deliverer&object_id=',
			'new' => 'index.php?admin_modul=admin_object_edit&object=Deliverer',
			'copy' => 0,
			'select_all' => 1,
			'edit' => 0,
			'actions' => 0,
			'filter' => 0,
			'search' => 1
		),
		'edit_title' => 'Newsletter',
		'edit_fields' => array (
			array(
				'name' => 'Stammdaten',
				'type' => 'form_title'
			),
			array(
				'db_field' => 'name',
				'name' => 'Name',
				'type' => 'text'
			),
			array(
				'db_field' => 'tacking_url',
				'name' => 'Tracking Link',
				'type' => 'text',
				'info' => 'Plazhalter für den Trackingcode: [trackingcode]'
			),
			array(
				'db_field' => 'active',
				'name' => 'Status',
				'values' => 'state_values',
				'type' => 'int'
			)
		),
		'edit_js_file' => 'admin_newsletter_edit.js',
		'edit_mandatory_fields' => array('firstname', 'surename', 'email'),
		'edit_toolbar' => array(
			'close' => 'index.php?admin_modul=admin_object_list&object=Newsletter',
			'copy' => 0,
			'undo' => 0,
			'redo' => 0,
			'save' => 1,
			'send' => 0,
			'delete' => 1
		),
	);

	function __construct($base_object, $id = false) {
		$this->base_object = $base_object;
		$this->db = $base_object->db;
		$this->id = $id;
		$this->error = '';
	} // end constructor

	function state_values() {
		return array(
			'0' => 'inaktiv',
			'1' => 'aktiv'
		);
	}

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

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

	function get_error() {
		return $this->error;
	} // end get_error

	// TODO: add filters
	public function get_all($active_only = false) {
		$sql = "SELECT * FROM deliverers";

		if ($active_only) {
			$sql .= ' WHERE active=1';
		}

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

		if ($result->num_rows) {
			$data = array();

			while ($obj = $result->fetch_object()) {
				$data[] = $obj;
			}

			return $data;
		}

		return false;
	} // end get_all

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

		if ($id) {
			$sql .= $this->db->real_escape_string($id);
			$this->id = $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 delete($id = false) {
		$sql = "DELETE FROM deliverers WHERE id=";

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

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

		if ($result) {
			$this->id = false;
			return true;
		}

		return false;
	} // end delete

	// TODO: rename to verify 
	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 deliverers 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);

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

			if ($result) {
				$this->id = $this->db->insert_id;

				return $this->id;
			}
			$this->error = "Speicher fehlgeschlagen";

			return false;
		}
		$this->error = "Keine Daten";

		return false;
	} // end create

	public function update($data, $id = false) {
		if ($data) {
			$sql = "UPDATE deliverers 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);

			if ($id) {
				$sql .= ' WHERE id='.$id;
				$this->id = $id;
			} else if ($this->id) {
				$sql .= ' WHERE id='.$this->id;
			} else {
				$this->error = "Keine ID";

				return false;
			}

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

			if ($result) {
				return $this->id;
			}
			$this->error = "Speicher fehlgeschlagen";

			return false;
		}
		$this->error = "Keine Daten";

		return false;
	} // end update


	// TODO: remove this and use delete
	public function delete_by_id($id) {
		$rs = $this->db->query("DELETE FROM deliverers
					WHERE id=$id;");
	}

	// TODO: remove this and add filters to get_all
	public function getAllActive($shippingArea = false, $standard_shipping_items = 1, $bulk_goods_price_add = false, $goods_weight = false, $good_price_sum_net = 0) {
		if (!$shippingArea) {
			$shippingArea = 7;
		}
		$query = "
			SELECT
				sd.*,
				d.name, d.id
			FROM
				shipping_area_deliverers sd
			LEFT JOIN
				deliverers d
			ON
				d.id = sd.deliverer_id 
			WHERE
				sd.shipping_area_id = ".$shippingArea."
			AND
				sd.active = 1
			AND
				d.active = 1
		";

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

		$deliverers = array();
		while ($D = $rs->fetch_object()) {

			// goods weight shipping price
			if ($D->deliverer_cost_type == 1) {
				if (!$goods_weight) {
					$goods_weight = 0;
				}
				if ($goods_weight > 0) {
					$last_price = 0;
					$found = 0;
					for ($i=1;$i<=10;$i++) {
						if ($D->{'weight_'.$i} > 0) {
							$last_price = $D->{'weight_'.$i.'_price'};
						}
						if ($D->{'weight_'.$i} >= $goods_weight && ($i == 1 || $D->{'weight_'.($i - 1)} < $goods_weight)) {
							$D->price_add = $D->{'weight_'.$i.'_price'};
							$found = 1;
						}
					}
					if ($found == 0) {
						$D->price_add = $last_price;
					}
				}
				else {
					$D->price_add = $D->{'weight_1_price'};
				}
			}

			// shipping free price
			if ($D->shipping_free_price > 0 && $good_price_sum_net > $D->shipping_free_price) {
				$D->price_add = 0;
				$D->bulk_goods_price_add = 0;
			}

			if ($bulk_goods_price_add) {
				$D->price_add += $D->bulk_goods_price_add;
			} else {
				$D->bulk_goods_price_add = 0;
			}

			if ($standard_shipping_items == 0) {
				$D->price_add = 0;
			}

			$D->price_add_net = $D->price_add / (100 + $this->base_object->config->shopConfiguration['default_vat_value']) * 100;
			$D->price_add_vat = $D->price_add - $D->price_add_net;
			$D->bulk_goods_price_add_net = $D->bulk_goods_price_add / (100 + $this->base_object->config->shopConfiguration['default_vat_value']) * 100;
			$D->bulk_goods_price_add_vat = $D->bulk_goods_price_add - $D->bulk_goods_price_add_net;

			if ($this->base_object->customer_group->show_tax) {
				$D->price_add_shown = $D->price_add;
				$D->bulk_goods_price_add_shown = $D->bulk_goods_price_add;
			} else {
				$D->price_add_shown = $D->price_add_net;
				$D->bulk_goods_price_add_shown = $D->bulk_goods_price_add_net;
			}

			$deliverers[$D->id] = $D;
		}

		return $deliverers;
	}

	// TODO: WHERE IS THIS USED AND FOR WHAT
	public function checkShipping($shippingId, $shippingArea = 7) {
		$shippings = $this->getAllActive($shippingArea);

		if (isset($shippings[$shippingId])) {
			return $shippings;
		}

		return false;
	}

}

?>