<?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';

class Retail_sale extends Main {

	protected $base_object;
	protected $db;

	public $list_table_config = array (
		'title' => 'Ladenverkauf',
		'db_table' => 'retail_sale',
		'list_fields' => array(
			array(
				'db_field' => 'date',
				'name' => 'Datum',
				'sortable' => 1
			),
			array(
				'db_field' => 'ec_sum',
				'name' => 'EC-Umsatz',
				'sortable' => 1
			),
			array(
				'db_field' => 'cash_sum',
				'name' => 'Bar-Umsatz',
				'sortable' => 1
			),
			array(
				'db_field' => 'vk_sum',
				'name' => 'Gesamt-Umsatz',
				'sortable' => 1
			)
		),
		'default_sort_item' => 'date',
		'default_sort_direction' => 'down',
		'search_fields' => array('date'),
		'db_id_field' => 'id',
		'edit_link' => 'index.php?admin_modul=admin_retail_sale_editor&id=',
		'toolbar' => array(
			'delete' => '1',
			'new' => 'index.php?admin_modul=admin_retail_sale_editor',
			'copy' => 0,
			'select_all' => 1,
			'edit' => 0,
			'actions' => 0,
			'filter' => 0,
			'search' => 1
		),
		'permanent_filter' => array(
			array(
				'db_field' => 'vk_sum',
				'relational_operator' => ' > ',
				'value' => '0'
			)
		)
	);

	private $theme_fields = array(
		'ec_sum' => 'text',
		'cash_sum' => 'text',
		'vk_sum' => 'text',
		'tax_sum' => 'text'
	);

	public $id;

	private $config;
	private $path;
	private $error;

	public function __construct($base_object) {
		global $config_object;
		$this->base_object = $base_object;
		$this->config = $base_object->config;
		$this->db = $base_object->db;
		$this->id = false;
		$this->error = '';
	} // end __construct

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

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

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

	public function get_all() {
		$sql = "SELECT * FROM retail_sale ORDER BY name";

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

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

		return $data;
	} // end get_all

	public function get_by_id($id) {
		$sql = "SELECT * FROM retail_sale WHERE id = $id";

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

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

			// get items
			include_once './core/item_inventory.class.php';
			$item_inventory_object = new ItemInventory($this->base_object);
			$data->item_list = $item_inventory_object->get_by_object_id($id, 1);

			// calc tax
			$data->tax_7_sum = 0;
			$data->tax_19_sum = 0;
			foreach ($data->item_list as $item) {
				if ($item->item_tax == 7.00) {
					$data->tax_7_sum += $item->price_sum;
				} else if ($item->item_tax == 19) {
					$data->tax_19_sum += $item->price_sum;
				}
			}

			return $data;
		}

		return false;
	} // end get_by_id

	public function get_id_by_date($date) {
		$sql = "SELECT id FROM retail_sale WHERE date = '$date'";
		$result = $this->db->query($sql);
		$id = 0;
		while ($obj = $result->fetch_object()) {
			$id = $obj->id;
		}

		return $id;
	} // end get_by_id

	public function create_new($date) {
		$sql = "INSERT INTO retail_sale (date) VALUES ('".$date."')";
		$this->db->query($sql);
		$id = $this->db->insert_id;

		return $id;
	} // end get_by_id

	public function delete_by_id($id) {
		$sql = "DELETE FROM retail_sale WHERE id=".id;

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

	public function get_where_name_like($name) {
		$sql = "SELECT id FROM retail_sale WHERE name LIKE '%".$name."%'";

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

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

		return $data;
	} // end get_where_name_like

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

		if ($id) {
			$sql .= " WHERE id=".$this->db->real_escape_string($id);
		} else if ($this->id) {
			$sql .= " WHERE id=".$this->db->real_escape_string($this->id);
		} else {
			$this->error = 'no id';

			return false;
		}

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

		if ($result->num_rows > 0) {
			return $result->fetch_object();
		}
		$this->error = 'no data';

		return false;
	} // end get_data

	public function create($data) {
		if ($data) {
			$data['short_uri'] = $this->short_uri($data['short_uri'], $data['name']);

			$sql = "INSERT INTO retail_sale SET ";

			foreach ($data as $var_name => $value) {
				$value = $this->db->real_escape_string($value);

				if ($this->theme_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;
	} // end create

	public function add_item($data) {
		if ($data) {
			$data['inventory_object_type_id'] = 1;
			$data['inventory_object_id'] = $data['inventory_object_id'];

			// get retail sale data
			$sale_date = $this->get_data()->date;
			$data['create_time'] = $sale_date.' 12:00:00';

			// get items for updating sum values
			include_once './core/item_inventory.class.php';
			$item_inventory_object = new ItemInventory($this->base_object);
			$item_inventory_object->add($data);

			// updated
			$this->update(array('tax_sum' => 0));
		}

		return 1;
	} // end update

	public function delete_item($item_id) {
		if ($item_id) {
			// get items for updating sum values
			include_once './core/item_inventory.class.php';
			$item_inventory_object = new ItemInventory($this->base_object);
			$item_inventory_object->delete($item_id);
			// updated
			$this->update(array('tax_sum' => 0));
		}

		return 1;
	} // end update

	public function update($data) {
		if ($data) {
			// get items for updating sum values
			include_once './core/item_inventory.class.php';
			$item_inventory_object = new ItemInventory($this->base_object);

			// update items
			if (isset($data['items'])) {
				foreach ($data['items'] as $item_inventory) {
					$item_inventory_object->id = $item_inventory['item_id'];
					unset($item_inventory['item_id']);
					$item_inventory_object->update($item_inventory);
				}
				unset($data['items']);
			}

			// get item list
			$item_list = $item_inventory_object->get_by_object_id($this->id, 1);

			// calc tax
			$data['tax_sum'] = 0;
			$data['vk_sum'] = 0;
			foreach ($item_list as $item) {
				$data['tax_sum'] += ($item->price_sum * $item->item_tax / 100);
				$data['vk_sum']  += $item->price_sum;
			}

			if (!isset($data['ec_sum'])) {
				// get ec sum
				$sql = "SELECT * FROM retail_sale WHERE id=".$this->db->real_escape_string($this->id);
				$result = $this->db->query($sql);
				$obj = $result->fetch_object();
				$data['ec_sum'] = $obj->ec_sum;
			}
			$data['cash_sum']  = $data['vk_sum'] - $data['ec_sum'];

			$sql = "UPDATE retail_sale SET ";
			foreach ($data as $var_name => $value) {
				$value = $this->db->real_escape_string($value);

				if ($this->theme_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 1;
	} // end update

	public function delete($id) {
		$sql = "DELETE FROM retail_sale
		WHERE id=".$id;

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

	public function data_filter($request) {
		$data = array();
		foreach ($this->theme_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;
	} // end data_filter

	public function save($data) {
		if (isset($data['id'])) {
			$id = $data['id'];
			unset($data['id']);

			$sql = Database::update('retail_sale', $data, 'WHERE id='.$id);
		} else {
			$id = false;
			$sql = Database::insert('retail_sale', $data);
		}
		$return = $this->db->query($sql);

		if ($return) {
			if ($id) {
				return $id;
			} else {
				return $this->db->insert_id;
			}
		}

		return false;
	} // end save

}