<?php 

class ControllerOrderView extends Controller {

	var $error = array();

   

  	function index() {

    	$response =& $this->locator->get('response');

		$language =& $this->locator->get('language');

		$template =& $this->locator->get('template');

		$module   =& $this->locator->get('module');

	

		$language->load('controller/potential_order.php');

	

		$template->set('title', $language->get('heading_title'));

		

    	$template->set('content', $this->getForm());

	

		$template->set($module->fetchPublic());

	

    	$response->set($template->fetch('layout.tpl'));	

  	}


  	function getForm() {

		$request  =& $this->locator->get('request');

		$database =& $this->locator->get('database');

		$url      =& $this->locator->get('url');

		$language =& $this->locator->get('language');

		$currency =& $this->locator->get('currency');

		$address  =& $this->locator->get('address');

		$order     =& $this->locator->get('order');

		$payment   =& $this->locator->get('payment');
		
			    	

    	$view = $this->locator->create('template');



    	$view->set('heading_title', $language->get('heading_title') . ' #' . $request->get('order_id'));

    	$view->set('heading_description', $language->get('heading_description'));



    	$view->set('text_order', $language->get('text_order'));

		$view->set('text_email', $language->get('text_email'));

		$view->set('text_telephone', $language->get('text_telephone'));

		$view->set('text_fax', $language->get('text_fax'));

		$view->set('text_shipping_address', $language->get('text_shipping_address'));

    	$view->set('text_shipping_method', $language->get('text_shipping_method'));

    	$view->set('text_payment_address', $language->get('text_payment_address'));

    	$view->set('text_payment_method', $language->get('text_payment_method'));

    	$view->set('text_order_history', $language->get('text_order_history'));

    	$view->set('text_order_download', $language->get('text_order_download'));

    	$view->set('text_order_update', $language->get('text_order_update'));

    	$view->set('text_product', $language->get('text_product'));

    	$view->set('text_model', $language->get('text_model'));

    	$view->set('text_quantity', $language->get('text_quantity'));

    	$view->set('text_price', $language->get('text_price'));

    	$view->set('text_total', $language->get('text_total'));



    	$view->set('column_date_added', $language->get('column_date_added'));

    	$view->set('column_status', $language->get('column_status'));

    	$view->set('column_download', $language->get('column_download'));

    	$view->set('column_filename', $language->get('column_filename'));

    	$view->set('column_remaining', $language->get('column_remaining'));

    	$view->set('column_notify', $language->get('column_notify'));

    	$view->set('column_comment', $language->get('column_comment'));



    	$view->set('entry_status', $language->get('entry_status'));

    	$view->set('entry_comment', $language->get('entry_comment'));

    	$view->set('entry_notify', $language->get('entry_notify'));



    	$view->set('button_list', $language->get('button_list'));

    	$view->set('button_insert', $language->get('button_insert'));

    	$view->set('button_update', $language->get('button_update'));

    	$view->set('button_delete', $language->get('button_delete'));

    	$view->set('button_save', $language->get('button_save'));

    	$view->set('button_cancel', $language->get('button_cancel'));



    	$view->set('tab_general', $language->get('tab_general'));

	

		$view->set('error', @$this->error['message']);


      	$order_info = $database->getRow("select order_id,reference,shipping_firstname,	shipping_lastname,shipping_company,shipping_address_1,shipping_address_2,shipping_city,	shipping_postcode,	shipping_zone,shipping_country,payment_firstname,payment_lastname,payment_company,payment_address_1,payment_address_2,payment_city,payment_postcode,payment_zone,payment_country,shipping_address_format,shipping_method,payment_address_format,payment_method,telephone,value,currency from `order` where order_id = '" . (int)$request->get('order_id') . "'");

		

		$view->set('reference', $order_info['reference']);

		$view->set('email', $order_info['email']);

		$view->set('telephone', $order_info['telephone']);



    	$shipping_address = array(

      		'firstname' => $order_info['shipping_firstname'],

      		'lastname'  => $order_info['shipping_lastname'],

      		'company'   => $order_info['shipping_company'],

      		'address_1' => $order_info['shipping_address_1'],

      		'address_2' => $order_info['shipping_address_2'],

      		'city'      => $order_info['shipping_city'],

      		'postcode'  => $order_info['shipping_postcode'],

      		'zone'      => $order_info['shipping_zone'],

      		'country'   => $order_info['shipping_country']

    	);



    	$view->set('shipping_address', $address->format($shipping_address, $order_info['shipping_address_format'], '<br />'));

  

    	$view->set('shipping_method', $order_info['shipping_method']);



    	$payment_address = array(

      		'firstname' => $order_info['payment_firstname'],

      		'lastname'  => $order_info['payment_lastname'],

      		'company'   => $order_info['payment_company'],

      		'address_1' => $order_info['payment_address_1'],

      		'address_2' => $order_info['payment_address_2'],

      		'city'      => $order_info['payment_city'],

      		'postcode'  => $order_info['payment_postcode'],

      		'zone'      => $order_info['payment_zone'],

      		'country'   => $order_info['payment_country']

    	);



    	$view->set('payment_address', nl2br($address->format($payment_address, $order_info['payment_address_format'])));

    	$view->set('payment_method', $order_info['payment_method']);
    	$products = $database->getRows("select order_product_id,itemcode,name,model,quantity,price,tax,discount,total from order_product where order_id = '" . (int)$request->get('order_id') . "'");

    	$product_data = array();

    	foreach ($products as $product) {

      		$options = $database->getRows("select name,value from order_option where order_id = '" . (int)$request->get('order_id') . "' and order_product_id = '" . (int)$product['order_product_id'] . "'");

      		$option_data = array();
      		foreach ($options as $option) {

        		$option_data[] = array(

          			'name'  => $option['name'],

          			'value' => $option['value'],
        		);
      		}
        	$product_data[] = array(
                'product_id' =>  $product['itemcode'],
				
          		'name'     => $product['name'],

          		'model'    => $product['model'],

          		'option'   => $option_data,

          		'quantity' => $product['quantity'],

          		'price'    => $currency->format(($product['price'] + ($product['price'] * $product['tax'] / 100)), $order_info['currency'], $order_info['value']),

          		'discount' => (ceil($product['discount']) ? $currency->format(($product['price'] - $product['discount']) + (($product['price'] - $product['discount']) * $product['tax'] / 100), $order_info['currency'], $order_info['value']) : NULL),

				'total'    => $currency->format($product['total'] + ($product['total'] * $product['tax'] / 100), $order_info['currency'], $order_info['value'])

        	);

    	}



    	$view->set('products', $product_data);

    	
        $totals = $database->getRows("select order_total_id from order_total where order_id = '" . (int)$request->get('order_id') . "' order by sort_order ASC");
		
		$total_count = count($totals);
		if ( $total_count > 0 )
		{
			if ( intval($order_info['total']) != intval(substr($totals[$total_count-1]['text'],1)) )
			{
				$totals[] = array( 'title' => 'Price After Discount','text' =>$order_info['total'] ); 
			}
		}
		
		$view->set('totals', $totals);	


		return $view->fetch('content/order_view.tpl');

  	}
}

?>