<?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 Incomming_goods extends Main {

	public $list_table_config = array (
		'title' => 'Wareneingang',
		'db_table' => 'incomming_goods',
		'list_fields' => array(
			array(
				'db_field' => 'number',
				'name' => 'Nummber',
				'sortable' => 1
			),
			array(
				'db_field' => 'date',
				'name' => 'Datum',
				'format' => 'date_format',
				'sortable' => 1
			),
			array(
				'db_field' => 'vendor_id',
				'name' => 'Lieferant',
				'rewrite_function' => 'vendor_names',
				'sortable' => 1
			)
		),
		'default_sort_item' => 'date',
		'default_sort_direction' => 'down',
		'search_fields' => array('number'),
		'db_id_field' => 'id',
		'edit_link' => 'index.php?admin_modul=admin_incomming_goods_editor&id=',
		'toolbar' => array(
			'delete' => '1',
			'new' => 'index.php?admin_modul=admin_incomming_goods_editor',
			'copy' => 0,
			'select_all' => 1,
			'edit' => 0,
			'actions' => 0,
			'filter' => 1,
			'search' => 1
		),
		'list_filter' => array(
			array (
				'db_field' => 'vendor_id',
				'relational_operator' => '=',
				'name' => 'Lieferant',
				'values' => 'vendor_names'
			)
		),
	);

	private $theme_fields = array(
		'name' => 'text',
		'description' => 'text',
		'modul' => 'text',
		'number' => 'text',
		'date' => 'text',
		'conversion_price' => 'text',
		'currency' => 'text',
		'vendor_id' => 'integer',
		'settings' => 'text'
	);

	public $id;

	protected $db;
	protected $base_object;
	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->url = './web/'.SHOP_SYSTEM.'/documents/incomming_goods_bill/';
		$this->error = '';
	} // end __construct

	public function date_format($string) {
		return substr($string,8,2).'.'.substr($string,5,2).'.'.substr($string,0,4);
	}

	public function vendor_names() {
		include_once './core/vendor.class.php';
		$vendor_object = new Vendor($this->base_object);
		return $vendor_object->get_all_names();
	}

	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 incomming_goods ORDER BY name";

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

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

		return $data;
	} // end get_all

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

		return $id;
	} // end get_by_id

	public function get_id_by_date($date) {
		$sql = "SELECT id FROM incomming_goods WHERE date = '" . $this->db->real_escape_string($date) . "' LIMIT 1";
		$result = $this->db->query($sql);

		if ($result && $result->num_rows > 0) {
			$obj = $result->fetch_object();
			return $obj->id;
		}

		return false;
	}

	public function get_by_id($id) {
		$sql = "SELECT * FROM incomming_goods 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, 2);


			$data->price_sum_netto = 0;
			// calc tax
			$data->tax_7_sum = 0;
			$data->tax_19_sum = 0;
			foreach ($data->item_list as $item) {
				$data->price_sum_netto += $item->price_sum * $item->amount;
				if ($item->item_tax == 7.00) {
					$data->tax_7_sum += $item->price_sum * $item->amount;
				} else if ($item->item_tax == 19) {
					$data->tax_19_sum += $item->price_sum * $item->amount;
				}
			}
			$data->shipping_cost_tax = $data->cost_sum * 0.19;
			$data->tax_19 = ($data->tax_19_sum * 0.19) + $data->shipping_cost_tax;
			$data->tax_7 = $data->tax_7_sum * 0.07;
			$data->tax_sum = $data->tax_19 + $data->tax_7;
			$data->price_sum_brutto = $data->price_sum_netto + $data->tax_19 + $data->tax_7;
			$data->skonto_sum = ($data->skonto / 100) * $data->price_sum_brutto;
			$data->cash_sum = $data->price_sum_brutto - $data->skonto_sum + $data->cost_sum;

			// calc article shipping costs
			$items = array();
			foreach ($data->item_list as $item) {
				$item->price_sum_netto = $item->price_sum * $item->amount;
				$item->item_beschaffungskosten = $data->cost_sum / $data->price_sum_netto * $item->price_sum;
				$item->item_price_ek = ($item->price_sum + $item->item_beschaffungskosten) * (1 - ($data->skonto / 100));
				$items[] = $item;
			}
			$data->item_list = $items;

			// formated date
			$data->formated_date = substr($data->date,8,2).'.'.substr($data->date,5,2).'.'.substr($data->date,0,4);

			return $data;
		}

		return false;
	} // end get_by_id

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

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

	public function get_where_name_like($name) {
		$sql = "SELECT id FROM incomming_goods 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 incomming_goods";

		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 incomming_goods 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 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
			$create_date = $data['date'].' 00:00:00';
			if (isset($data['items'])) {
				foreach ($data['items'] as $item_inventory) {
					$item_inventory_object->id = $item_inventory['item_id'];
					$item_inventory['created'] = $create_date;
					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, 2);

			//$data['short_uri'] = $this->short_uri($data['short_uri'], $data['name'], $this->id);

			$sql = "UPDATE incomming_goods 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 incomming_goods 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 add_item($data) {
		if ($data) {
			$data['inventory_object_type_id'] = 2;
			$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 get_all_paginated($items , $page, $order = false) {
		$page = ($page - 1) * $items;

		$sql = "SELECT * FROM incomming_goods ORDER BY date DESC LIMIT $items OFFSET $page";

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

		return $data;
	} // end get_all

	public function get_number_of_pages($items) {
		$sql = "SELECT COUNT(id) FROM incomming_goods";

		return parent::get_number_of_pages($items, $sql);
	} // end get_number_of_pages

	public function get_pagination_array($items, $page) {
		$sql = "SELECT COUNT(id) FROM incomming_goods";

		return parent::get_pagination_array($items, $page, $sql);
	} // end get_pagination_array

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

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

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

		return false;
	} // end save

	public function save_bill($id, $name) {
		$file_name = 'Rechnung_'.$id.'.pdf';
		if (move_uploaded_file($_FILES['Filedata']['tmp_name'], $this->url.$file_name)) {
			// save filename to db
			$sql = "UPDATE incomming_goods SET bill_file='$file_name' WHERE id=$id";

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

			//return
			return $this->url.$file_name;
		} else {
			return false;
		}
	}

	public function delete_bill_file($id) {
		$file_name = 'Rechnung_'.$id.'.pdf';
		if (unlink($this->url.$file_name)) {
			// save filename to db
			$sql = "UPDATE incomming_goods
			SET bill_file=''
			WHERE id=$id";

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

			//return
			return $this->url.$file_name;
		} else {
			return false;
		}
	}

}

?>