<?php
/**
* @version      $Id: admin_retail_sale_editor.php 
* @package      Easyshop
* @copyright    Copyright (C) 2005 - 2011 TA-EDV
* @license      proprietary
* @author       Richard Kammermayer <rk@ta-edv.de>
* Easyshop is a web shop system
*/

include_once './core/retail_sale.class.php';

class admin_retail_sale_editor {
	private $base_object;
	private $layout_object;
	private $retail_sale_object;
	
	function __construct($base_object, $layout_object) {
		$this->base_object = $base_object;
		$this->layout_object = $layout_object;
		$this->retail_sale_object = new Retail_sale($base_object);
	} // end __construct

	function run() {
		if (isset($_GET['action'])) {
			$action = $_GET['action'];
		} elseif (isset($_POST['action'])) {
			$action = $_POST['action'];
		} else {
			$action = false;
		}
		
		if (isset($_GET['id'])) {
			$id = $_GET['id'];
		} elseif (isset($_POST['id'])) {
			$id = $_POST['id'];
		} else {
			$id = false;
		}
		
		if ($action == "get_tab") {
			$this->get_tab($id);
		} elseif ($action == "view_stat") {
			$this->view_stat($id);
		} elseif ($action == "update") {
			$this->update($id);
		} elseif ($action == "add_item") {
			$this->add_item($id);
		} elseif ($action == "delete_item") {
			$this->delete_item($id);
		} else {
			return $this->setup_list($id);
		}
	} // end run
	
	private function setup_list($id) {
		$today = '';
		if ($id) {
			$data = $this->retail_sale_object->get_by_id($id);
			
			if ($data) {			
				$this->layout_object->assign('form_data', $data);
				$today = substr($data->date,8,2).'.'.substr($data->date,5,2).'.'.substr($data->date,0,4);
			} else {
				$this->layout_object->assign('error_message', "Diese Statistik existiert nicht.");
			}
		}
		else {
			// get today date
			$today = date("d.m.Y", time());
		}
		$this->layout_object->assign('today', $today);
		return $this->layout_object->fetch('admin_retail_sale_editor.tpl');
	} // end default_action
	

	
	private function view_stat($id) {	
		if ($id) {
			$data = $this->retail_sale_object->get_by_id($id);
			$data->item_list = array_reverse($data->item_list);
		
			if ($data) {
				$this->layout_object->assign('form_data', $data);
				$today = substr($data->date,8,2).'.'.substr($data->date,5,2).'.'.substr($data->date,0,4);
			} else {
				$this->layout_object->assign('error_message', "Es ist ein Fehler aufgetreten.");
			}
		}
		elseif(!$id && $_POST['date']) {
			$date = substr($_POST['date'],6,4).'-'.substr($_POST['date'],3,2).'-'.substr($_POST['date'],0,2);
			$id = $this->retail_sale_object->get_id_by_date($date);
			if(!$id) {
				$id = $this->retail_sale_object->create_new($date);
			}
			// get data
			$data = $this->retail_sale_object->get_by_id($id);
		
			if ($data) {
				$this->layout_object->assign('form_data', $data);
				$today = substr($data->date,8,2).'.'.substr($data->date,5,2).'.'.substr($data->date,0,4);
			} else {
				$this->layout_object->assign('error_message', "Es ist ein Fehler aufgetreten.");
			}
		}
		
			//$data = $this->retail_sale_object->get_by_id($id);
		
			
			/*if ($data) {
				$this->layout_object->assign('form_data', $data);
			} else {
				$this->layout_object->assign('error_message', "Es ist ein Fehler aufgetreten.");
			}*/
		
		echo $this->layout_object->fetch('admin_retail_sale_view.tpl');
		exit();
	}
	
	
	// only used in ajax, so i terminate with exit
	private function update($id) {
		$data = array();
		$item_amount = $_POST['item_amount'];
		$item_price_sum = str_replace(',','.',$_POST['item_price_sum']);
		$item_id = $_POST['item_id'];
		$data['items'] = array();
		for ($i=0;$i < count($item_id);$i++) {
			$data['items'][] = array(
				'item_id' => $item_id[$i],
				'amount' => $item_amount[$i],
				'price_sum' => $item_price_sum[$i]
			);
		}
		
		if (isset($_POST['ec_sum'])) {
			$data['ec_sum'] = str_replace(',','.',$_POST['ec_sum']);
			
			if ($data['id'] == '') {
				unset($data['id']);
			}
			$this->retail_sale_object->set_id($id);
			$result = $this->retail_sale_object->update($data);
			
			if ($result) {
				$return = array('status' => 'success', 'id' => $result);
			} else {
				$return = array('status' => 'error', 'message' => 'save failed');
			}
		} else {
			$return = array('status' => 'error', 'message' => 'no forms data');
		}
		
		echo json_encode($return);
		
		exit();
	} // end save
	
	// only used in ajax, so i terminate with exit
	private function add_item($id) {
		$data = array();
		$this->retail_sale_object->set_id($id);
		$data['item_id'] = $_POST['item_id'];
		$data['amount'] = $_POST['amount'];
		$data['price_sum'] = str_replace(',','.',$_POST['price_sum']);
		$data['inventory_object_id'] = $id;
		//$data['create_time'] = '2011-03-25 00:00:00';
		
		
		$result = $this->retail_sale_object->add_item($data);
		if ($result) {
			$return = array('status' => 'success');
		} else {
			$return = array('status' => 'error', 'message' => 'save failed');
		}
		
		echo json_encode($return);
		
		exit();
	} // end save
	
	// only used in ajax, so i terminate with exit
	private function delete_item($id) {
		$this->retail_sale_object->set_id($id);
		$result = $this->retail_sale_object->delete_item($_POST['item_id']);
		if ($result) {
			$return = array('status' => 'success');
		} else {
			$return = array('status' => 'error', 'message' => 'save failed');
		}
	
		echo json_encode($return);
	
		exit();
	} // end save
	
	
} // end
