<?php
/**
* @package      Easyway Shop
* @copyright    Copyright (C) 2005 - 2011 TA-EDV
* @license      proprietary
* @author       John T. Daly <jd@ta-edv.de>
* 
* Easyway Shop is a web e-commerce system
*/

include_once './core/customer.class.php';
include_once './core/customerorder.class.php';
include_once './core/customeraddress.class.php';
include_once './core/country.class.php';
include_once './core/honorific.class.php';
include_once './core/orderstatus.class.php';
include_once './core/order.class.php';
include_once './core/orderhelper.class.php';
include_once './core/customeraddress.class.php';
include_once './core/cancellationreasons.class.php';
include_once './core/cancellationrequests.class.php';
include_once './core/cancellationreasonshelper.class.php';
include_once './core/deliverer.class.php';

class website_order_editor {
	
	private $base_object;
	private $layout_object;
	private $country_object;
	private $order_object;
	private $address_object;
	private $cancellation_reason_object;
	private $deliverer_object;
	
	public function __construct($base_object, $layout_object) {
		$this->base_object = $base_object;
		$this->layout_object = $layout_object;
		$this->country_object = new Country($base_object);
		$this->order_object = new Order($base_object);
		$this->address_object = new CustomerAddress($base_object);
		$this->cancellation_reason_object = new CancellationReasons($base_object);
		$this->deliverer_object = new Deliverer($base_object);
	}
	
	public function run() {
		if (isset($_GET['action'])) {
			$action = $_GET['action'];
		} elseif (isset($_POST['action'])) {
			$action = $_POST['action'];
		} else {
			$action = false;
		}

		if ($action == 'cancel_order') {
			$this->cancel_order();
		} elseif ($action == 'edit_customer_comment') {
			$this->edit_customer_comment();
		} elseif ($action == 'edit_shipping_address') {
			$this->edit_shipping_address();
		} elseif ($action == 'edit_billing_address') {
			$this->edit_billing_address();
		} elseif ($action == 'get_address') {
			$this->get_address();
		} elseif ($action == 'get_order_address') {
			$this->get_order_address();
		} else {
			return $this->view_order();
		}
	}
	
	private function get_address() {
		if (isset($_POST['address_id'])) {
			$address_id = $_POST['address_id'];
		} else {
			$address_id = false;
		}
		
		if ($address_id) {
			$address_data = $this->address_object->get($address_id);
			unset($address_data->orders);
			
			if ($address_data) {
				$return_data = array('status' => 'success', 'data' => $address_data);
			} else {
				$return_data = array('status' => 'error', 'message' => 'no return data');
			}
		} else {
			$return_data = array('status' => 'error', 'message' => 'missing argument');
		}
		
		echo json_encode($return_data);
		exit();
	}
	
	private function get_order_address() {
		if (isset($_POST['address_type'])) {
			$address_type = $_POST['address_type'];
		} else {
			$address_type = false;
		}
		
		if (isset($_GET['order_id'])) {
			$order_id = $_GET['order_id'];
		} else {
			$order_id = false;
		}
		
		if ($address_type && $order_id) {
			$order_data = $this->order_object->get_by_id($order_id);
			
			if ($order_data) {
				if ($address_type == 'shipping') {
					$shipping_address['company'] = $order_data->shipping_company;
					$shipping_address['honorific'] = $order_data->shipping_honorific;
					$shipping_address['firstname'] = $order_data->shipping_firstname;
					$shipping_address['surname'] = $order_data->shipping_surname;
					$shipping_address['street'] = $order_data->shipping_street;
					$shipping_address['house_number'] = $order_data->shipping_house_number;
					$shipping_address['zip_code'] = $order_data->shipping_zip_code;
					$shipping_address['city'] = $order_data->shipping_city;
					$shipping_address['country'] = $order_data->shipping_country;
					
					$return_data = array('status' => 'success', 'data' => $shipping_address);
				} elseif ($address_type == 'billing') {
					$billing_address['company'] = $order_data->billing_company;
					$billing_address['honorific'] = $order_data->billing_honorific;
					$billing_address['firstname'] = $order_data->billing_firstname;
					$billing_address['surname'] = $order_data->billing_surname;
					$billing_address['street'] = $order_data->billing_street;
					$billing_address['house_number'] = $order_data->billing_house_number;
					$billing_address['zip_code'] = $order_data->billing_zip_code;
					$billing_address['city'] = $order_data->billing_city;
					$billing_address['country'] = $order_data->billing_country;
					
					$return_data = array('status' => 'success', 'data' => $billing_address);
				} else {
					$return_data = array('status' => 'error', 'message' => 'unsupported argument type');
				}
			} else {
				$return_data = array('status' => 'error', 'message' => 'no return data');
			}
		} else {
			$return_data = array('status' => 'error', 'message' => 'missing argument');
		}
		
		echo json_encode($return_data);
		exit();
	}
	
	private function view_order() {
		$customer_id = $this->base_object->customer->customer_id;
		if ($customer_id) {
			if (isset($_GET['order_id'])) {
				$order_id = $_GET['order_id'];
			} else {
				$order_id = false;
			}
		
			if ($order_id) {
				$order_object = new Order($this->base_object);
				$order = $order_object->get_order_by_id($order_id);
				$order_header = $order['header'];
				$deliverer_id = $order_header->shipping_method;
				$deliverer_data = $this->deliverer_object->get_data($deliverer_id);
				if ($deliverer_data) {
					$tracking_url = $deliverer_data->tacking_url;
					$tracking_code = $order_header->trackingcode;
					if ($tracking_code != '') {
						$tracking_url = str_replace('[trackingcode]', $tracking_code, $tracking_url);
					} else {
						$tracking_url = false;
					}
				} else {
					$tracking_url = false;
				}
				$order_header->cancellable = OrderHelper::order_cancellability_status($order_id);
				$order_footer = $order['footer'];
					
				if ($order_header->customer_id == $customer_id) {
					$order_items_data = $order['items'];
		
					$customer_address_object = new CustomerAddress($this->base_object);
					$customer_address_object->set_customer_id($customer_id);
					$customer_address_data = $customer_address_object->get_all();
		
					$this->layout_object->assign('addresses', $customer_address_data);
					$this->layout_object->assign('orderitems', $order_items_data);
					$this->layout_object->assign('countries', $this->country_object->get_all());
					$this->layout_object->assign('order', $order_header);
					$this->layout_object->assign('footer', $order_footer);
					$this->layout_object->assign('tracking_url', $tracking_url);
					
					// cancellation mechanism
					$cancellation_reasons = $this->cancellation_reason_object->get_all('customer-active');
					
					if ($cancellation_reasons) {
						$this->layout_object->assign('cancellation_reasons', $cancellation_reasons);
					}
				} else {
					$this->layout_object->assign('errormessage', "Keine Bestellung.");
				}
			} else {
				$this->layout_object->assign('errormessage', "Keine Bestellung.");
			}
		
			return $this->layout_object->_fetch('content_order_editor.tpl');
		} else {
			return $this->layout_object->_fetch('content_login.tpl');
		}
	}
	
	private function edit_customer_comment() {
		if ($_GET['order_id']) {
			$order_id = $_GET['order_id'];
		} else {
			$order_id = false;
		}
		
		$customer_id = $this->base_object->customer->customer_id;
		
		if ($order_id) {
			OrderHelper::set_order_comment($order_id, $_POST['customer_comment'], $customer_id);
		}
		
		header('location: ' . $_SERVER['HTTP_REFERER']);
	}
	
	private function edit_shipping_address() {		
		if ($_GET['order_id']) {
			$order_id = $_GET['order_id'];
		} else {
			$order_id = false;
		}
		
		if  ($_POST['select_shipping_address'] == '-1') {
			$save_address = true;
		} else {
			$save_address = false;
		}
		
		unset($_POST['select_shipping_address']);
		unset($_POST['action']);
		
		$customer_id = $this->base_object->customer->customer_id;
		
		if ($order_id) {
			OrderHelper::set_shipping_address($order_id, $_POST, $customer_id);
		}
		
		if ($save_address) {
			$address['honorific'] = $_POST['shipping_honorific'];
			$address['firstname'] = $_POST['shipping_firstname'];
			$address['surname'] = $_POST['shipping_surname'];
			$address['company'] = $_POST['shipping_company'];
			$address['street'] = $_POST['shipping_street'];
			$address['house_number'] = $_POST['shipping_house_number'];
			$address['zip_code'] = $_POST['shipping_zip_code'];
			$address['city'] = $_POST['shipping_city'];
			$address['country'] = $_POST['shipping_country'];
			$address['orders'] = 0;
			
			$this->address_object->set_customer_id($customer_id);
			$this->address_object->create($address);
		}
		
		header('location: ' . $_SERVER['HTTP_REFERER']);
	}
	
	private function edit_billing_address() {
		if ($_GET['order_id']) {
			$order_id = $_GET['order_id'];
		} else {
			$order_id = false;
		}
		
		if  ($_POST['select_billing_address'] == '-1') {
			$save_address = true;
		} else {
			$save_address = false;
		}
		
		unset($_POST['select_billing_address']);
		unset($_POST['action']);
		
		$customer_id = $this->base_object->customer->customer_id;
		
		if ($order_id) {
			OrderHelper::set_billing_address($order_id, $_POST, $customer_id);
		}
		
		if ($save_address) {
			$address['honorific'] = $_POST['billing_honorific'];
			$address['firstname'] = $_POST['billing_firstname'];
			$address['surname'] = $_POST['billing_surname'];
			$address['company'] = $_POST['billing_company'];
			$address['street'] = $_POST['billing_street'];
			$address['house_number'] = $_POST['billing_house_number'];
			$address['zip_code'] = $_POST['billing_zip_code'];
			$address['city'] = $_POST['billing_city'];
			$address['country'] = $_POST['billing_country'];
			$address['orders'] = 0;

			$this->address_object->set_id($customer_id);
			$this->address_object->add_address($address);
		}
		
		header('location: ' . $_SERVER['HTTP_REFERER']);
	}
	
	private function cancel_order() {
		echo "bestellung stornieren";
		if (isset($_GET['order_id'])) {
			$order_id = $_GET['order_id'];
		} else {
			$order_id = false;
		}
		$data = array();
		if (isset($_POST['cancellation_reason'])) {
			if ($order_id) {
				$date = getdate();

				$data['cancellation_reason_id'] = $_POST['cancellation_reason'];
				$data['cancellation_info'] = $_POST['cancellation_text'];
				$data['cancellation_date'] = $date['year'] . '-' . $date['mon'] . '-' . $date['mday'];
				$data['cancellation_status'] = 1;
			}
		} else {
			$data = false;
		}
		
		if ($order_id) {
			if ($data) {		
				OrderHelper::request_cancellation($order_id, $data);
			}
		}
		
		header('location: ' . $_SERVER['HTTP_REFERER']);
	}
}