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

class Shipping_area extends Main {

	protected $base_object;

	public $list_table_config = array (
		'title' => 'Lieferzonen',
		'db_table' => 'shipping_area',
		'list_fields' => array(
			array(
				'db_field' => 'name',
				'name' => 'Lieferzone',
				'sortable' => 1
			)
		),
		'search_fields' => array('name'),
		'db_id_field' => 'id',
		'edit_link' => 'index.php?admin_modul=admin_shipping_area_editor&id=',
		'toolbar' => array(
			'delete' => 'index.php?admin_modul=admin_shipping_area_editor&object_id=',
			'new' => 'index.php?admin_modul=admin_shipping_area_editor&object=Newsletter',
			'copy' => 0,
			'select_all' => 1,
			'edit' => 0,
			'actions' => 0,
			'filter' => 0,
			'search' => 1
		)
	);



	private $object_fields = array(
		'name' => 'text',
		'active' => 'integer',
		'active_vk' => 'integer',
		'active_nn' => 'integer',
		'active_pp' => 'integer',
		'active_r' => 'integer',
		'active_be' => 'integer',
		'active_bar' => 'integer',
		'active_klarna_rechnung' => 'integer',
		'active_klarna_raten' => 'integer',
		'active_paymorrow_rechnung' => 'integer',
		'active_paymorrow_raten' => 'integer',
		'active_billpay_invoice' => 'integer',
		'active_computop_creditcard' => 'integer',
		'active_customized' => 'integer',
		'add_vk' => 'float',
		'add_nn' => 'float',
		'add_pp' => 'float',
		'add_r' => 'float',
		'add_be' => 'float',
		'add_bar' => 'float',
		'add_klarna_rechnung' => 'float',
		'add_klarna_raten' => 'float',
		'add_paymorrow_rechnung' => 'float',
		'add_paymorrow_raten' => 'float',
		'add_billpay_invoice' => 'float',
		'add_computop_creditcard' => 'float',
		'free_payment_vk' => 'float',
		'free_payment_nn' => 'float',
		'free_payment_pp' => 'float',
		'free_payment_r' => 'float',
		'free_payment_be' => 'float',
		'free_payment_bar' => 'float',
		'free_payment_klarna_rechnung' => 'float',
		'free_payment_klarna_raten' => 'float',
		'free_payment_paymorrow_rechnung' => 'float',
		'free_payment_paymorrow_raten' => 'float',
		'free_payment_billpay_invoice' => 'float',
		'free_payment_computop_creditcard' => 'float',
		'free_payment_customized' => 'float',
		'add_customized' => 'float',
		'account_type_vk' => 'integer',
		'account_type_nn' => 'integer',
		'account_type_pp' => 'integer',
		'account_type_r' => 'integer',
		'account_type_be' => 'integer',
		'account_type_bar' => 'integer',
		'account_type_klarna_rechnung' => 'integer',
		'account_type_klarna_raten' => 'integer',
		'account_type_paymorrow_rechnung' => 'integer',
		'account_type_paymorrow_raten' => 'integer',
		'account_type_billpay_invoice' => 'integer',
		'account_type_computop_creditcard' => 'integer',
		'account_type_customized' => 'integer'
	);


	function __construct($base_object) {
		$this->base_object = $base_object;
		$this->db = $base_object->db;
	}

	public function get_all() {
		$sql = "SELECT * FROM shipping_area";
		$result = $this->db->query($sql);
		$data = array();
		while ($obj = $result->fetch_object()) {
			$data[] = $obj;
		}

		return $data;
	}

	public function get_all_key_id() {
		$sql = "SELECT * FROM shipping_area";
		$result = $this->db->query($sql);
		$data = array();
		while ($obj = $result->fetch_object()) {
			$data[$obj->id] = $obj;
		}

		return $data;
	}

	public function get_all_names() {
		$sql = "SELECT * FROM shipping_area";
		$result = $this->db->query($sql);
		$data = array();
		while ($obj = $result->fetch_object()) {
			$data[$obj->id] = $obj->name;
		}

		return $data;
	}

	public function get_data() {
		$sql = "SELECT * FROM shipping_area WHERE id = ".$this->id;
		$result = $this->db->query($sql);
		$obj = $result->fetch_object();
		if ($obj) {
			// get deliverers
			$sql = "SELECT * FROM shipping_area_deliverers WHERE shipping_area_id = ".$obj->id;
			$result = $this->db->query($sql);
			$deliverer_data = array();
			while ($obj_deliverer = $result->fetch_object()) {
				$deliverer_data[$obj_deliverer->deliverer_id] = $obj_deliverer;
			}
			$obj->deliverer_data = $deliverer_data;
		}
		return $obj;
	} // end get_data

	public function delete() {
		$sql = "DELETE FROM shipping_area WHERE 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 if ($var_type == 'float') {
					$request[$field_name] = str_replace(',','.',$request[$field_name]);
					$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 shipping_area 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.', ';
					if ($value == '') {
						$value = 0;
					}
				} else if ($this->object_fields[$var_name] == 'float') {
					$value = str_replace(',','.',$value);
					if ($value == '') {
						$value = 0;
					} 
					$sql .= $var_name.' = '.$value.', ';
				} else {
					$sql .= $var_name.' = "'.$value.'", ';
				}
			}
			$sql = substr($sql, 0, -2);
			$this->db->query($sql);
			$this->id = $this->db->insert_id;
		}
		$this->update_deliverer();
		return;
	}

	public function update($data) {
		if ($data) {
			$sql = "UPDATE shipping_area 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 if ($this->object_fields[$var_name] == 'float') {
					$value = str_replace(',','.',$value);
					$sql .= $var_name.' = '.$value.', ';
				} else {
					$sql .= $var_name.'="'.$value.'", ';
				}
			}
			$sql = substr($sql, 0, -2);
			$sql .= ' WHERE id='.$this->id;
			$this->db->query($sql);
		}
		$this->update_deliverer();
		return;
	}

	private function update_deliverer() {
		// delete old deliverer data
		$sql = "DELETE FROM shipping_area_deliverers WHERE shipping_area_id=".$this->id;
		$this->db->query($sql);
		// set new deliverer data
		// get deliverer
		$deliverer_object = new Deliverer($this->base_object);
		$deliverers = $deliverer_object->get_all();
		foreach ($deliverers as $deliverer) {
			$active = $_POST['deliverer_active_'.$deliverer->id];
			$add = str_replace(',','.',$_POST['deliverer_add_'.$deliverer->id]);
			$bulk_goods_price_add = str_replace(',','.',$_POST['bulk_goods_price_add_'.$deliverer->id]);
			$cost_type = $_POST['deliverer_cost_type_'.$deliverer->id];
			$shipping_free_price = str_replace(',','.',$_POST['deliverer_shipping_free_price_'.$deliverer->id]);
			$weight_prices = array();
			for ($i=1;$i<=10;$i++) {
				$weight_prices['weight_'.$i] = str_replace(',','.',$_POST['deliverer_'.$deliverer->id.'_shipping_weight_'.$i]);
				$weight_prices['weight_'.$i.'_price'] = str_replace(',','.',$_POST['deliverer_'.$deliverer->id.'_shipping_price_'.$i]);
			}
			$sql = "INSERT INTO shipping_area_deliverers (
				shipping_area_id, 
				deliverer_id, 
				active, 
				price_add, 
				deliverer_cost_type, 
				bulk_goods_price_add,
				weight_1,
				weight_1_price,
				weight_2,
				weight_2_price,
				weight_3,
				weight_3_price,
				weight_4,
				weight_4_price,
				weight_5,
				weight_5_price,
				weight_6,
				weight_6_price,
				weight_7,
				weight_7_price,
				weight_8,
				weight_8_price,
				weight_9,
				weight_9_price,
				weight_10,
				weight_10_price,
				shipping_free_price
			) VALUES (
				".$this->id.", 
				$deliverer->id, $active, 
				'".$add."', 
				$cost_type, 
				'".$bulk_goods_price_add."',
				'".$weight_prices['weight_1']."',
				'".$weight_prices['weight_1_price']."',
				'".$weight_prices['weight_2']."',
				'".$weight_prices['weight_2_price']."',
				'".$weight_prices['weight_3']."',
				'".$weight_prices['weight_3_price']."',
				'".$weight_prices['weight_4']."',
				'".$weight_prices['weight_4_price']."',
				'".$weight_prices['weight_5']."',
				'".$weight_prices['weight_5_price']."',
				'".$weight_prices['weight_6']."',
				'".$weight_prices['weight_6_price']."',
				'".$weight_prices['weight_7']."',
				'".$weight_prices['weight_7_price']."',
				'".$weight_prices['weight_8']."',
				'".$weight_prices['weight_8_price']."',
				'".$weight_prices['weight_9']."',
				'".$weight_prices['weight_9_price']."',
				'".$weight_prices['weight_10']."',
				'".$weight_prices['weight_10_price']."',
				'".$shipping_free_price."')";

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

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

}