<?php 



class ControllerAccountInvoice extends Controller {


	function index() { 



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

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

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

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

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

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

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

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

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

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

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

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

    	if (!$customer->isLogged()) {

			if ($request->has('order_id')) {

				$session->set('redirect', $url->ssl('account_invoice', FALSE, array('order_id' => $request->get('order_id'))));

			} else {

				$session->set('redirect', $url->ssl('account_invoice', FALSE, array('reference' => $request->get('reference'))));
			}

			$response->redirect($url->ssl('account_login'));
    	}


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

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

		if ($request->has('order_id')) {



			$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,email from `order` where order_id = '" . (int)$request->get('order_id') . "' and customer_id = '" . (int)$customer->getId() . "'");



    	} else {



			$sql = "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,email from `order` where reference = '?' and customer_id = '?'";



			$order_info = $database->getRow($database->parse($sql, $request->get('reference'), $customer->getId()));



		}


		if ($order_info) {

      		$view = $this->locator->create('template');
      		$view->set('heading_title', $language->get('heading_title'));

    		$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_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_product', $language->get('text_product'));    		



      		$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_comment', $language->get('column_comment'));

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


			$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', $address->format($payment_address, $order_info['payment_address_format'], '<br />'));

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

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

      		$product_data = array();

      		foreach ($products as $product) {

               $image_info=$database->getRow("select i.filename from product p left join image i on p.image_id=i.image_id where p.product_id='".$product['itemcode']."'");  

        		$options = $database->getRows("select name,value from order_option where order_id = '" . (int)$order_info['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(

					'id'       => $product['itemcode'],					

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

          			'thumb'    => $image->resize($image_info['filename'], 100, 100),          			

          			'option'   => $option_data,

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

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

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

					'total'    => $product['total']

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

      		$view->set('totals', $database->getRows("select order_id,title,text from order_total where order_id = '" . (int)$order_info['order_id'] . "' order by sort_order ASC"));

      		$history_data = array();

      		$results = $database->getRows("select date_added, os.name as status, oh.comment from order_history oh left join order_status os on oh.order_status_id = os.order_status_id where oh.order_id = '" . (int)$order_info['order_id'] . "' and oh.notify = '1' and os.language_id = '" . (int)$language->getId() . "' order by oh.date_added ASC");


      		foreach ($results as $result) {

        		$history_data[] = array(
          			'date_added' => date($language->get('date_format_short'), strtotime($result['date_added'])),
          			'status'     => $result['status'],
          			'comment'    => $result['comment']
        		);

      		}


      		$view->set('historys', $history_data);

      		$view->set('continue', $url->ssl('account_history'));
	  		$template->set('content', $view->fetch('content/account_invoice.tpl'));
      		$template->set($module->fetchAccount());	
    	    $response->set($template->fetch('layout.tpl')); 
    	} else {

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

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

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

      		$view->set('button_continue', $language->get('button_continue'));
      		$view->set('continue', $url->href('account_history'));

	  		$template->set('content', $view->fetch('content/error.tpl'));
      		$template->set($module->fetchAccount());	
    	    $response->set($template->fetch('layout.tpl')); 
    	}

  	}

}
?>



