<?php 
class ControllerCheckoutConfirm extends Controller { 
	var $error = array();
	
	function index() {
		$address   =& $this->locator->get('address');
		$calculate =& $this->locator->get('calculate');
		$cart      =& $this->locator->get('cart');
		$config    =& $this->locator->get('config');
		$coupon    =& $this->locator->get('coupon');
		$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');
		$order     =& $this->locator->get('order');
		$payment   =& $this->locator->get('payment');
		$request   =& $this->locator->get('request');
		$response  =& $this->locator->get('response');
		$session   =& $this->locator->get('session');
		$shipping  =& $this->locator->get('shipping');
		$tax       =& $this->locator->get('tax');
		$template  =& $this->locator->get('template');
		$url       =& $this->locator->get('url');
		
    	if (!$customer->isLogged()) {
			$session->set('redirect', $url->ssl('checkout_shipping'));

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

    	if ((!$cart->hasProducts()) || ((!$cart->hasStock()) && (!$config->get('config_stock_checkout')))) {
	  		$response->redirect($url->ssl('cart'));
    	}

    	if ($cart->hasShipping()) {
			if (!$session->get('shipping_method')) {
	  			$response->redirect($url->ssl('checkout_shipping'));
    		}
    
			if (!$shipping->getQuote($session->get('shipping_method'))) {
	  			$response->redirect($url->ssl('checkout_shipping'));
			}
			
    		if (!$session->get('shipping_address_id')) {
	  			$response->redirect($url->ssl('checkout_address', 'shipping'));
    		}
		} else {
			$session->delete('shipping_address_id');
			$session->delete('shipping_method');
		}
    	
		if (!$session->get('payment_method')) {
	  		$response->redirect($url->ssl('checkout_payment'));
    	}

    	if (!$payment->hasMethod($session->get('payment_method'))) {
	  		$response->redirect($url->ssl('checkout_payment')); 
    	}
	
    	if (!$address->has($session->get('payment_address_id'))) { 
	  		$response->redirect($url->ssl('checkout_address', 'payment'));
    	}    
		
		$language->load('controller/checkout_confirm.php');

    	$template->set('title', $language->get('heading_title')); 
		
		if (($request->isPost()) && ($this->validate())) {
			$session->set('message', $language->get('text_coupon'));
			
			$response->redirect($url->ssl('checkout_confirm'));
		}
		 	
		$view = $this->locator->create('template');

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

    	$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_your_comments', $language->get('text_your_comments'));
    	$view->set('text_change', $language->get('text_change'));
    	$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('entry_coupon', $language->get('entry_coupon'));
		
    	$view->set('button_continue', $language->get('button_continue'));
		$view->set('button_back', $language->get('button_back'));
    	$view->set('button_update', $language->get('button_update'));
	
		$view->set('error', @$this->error['message']);
		
		$view->set('action', $url->href('checkout_confirm'));
		
		if ($request->has('coupon', 'post')) {
			$view->set('coupon', $request->get('coupon', 'post'));
		} else {
			$view->set('coupon', $coupon->getCode());
		}

    	$view->set('message', $session->get('message'));
    
		$session->delete('message');
							
		$view->set('payment_url', $payment->getActionUrl($session->get('payment_method')));
			
    	$view->set('shipping_address', $address->getFormatted($session->get('shipping_address_id'), '<br />'));
		    	
		$view->set('shipping_method', $shipping->getTitle($session->get('shipping_method')));
		
    	$view->set('checkout_shipping', $url->ssl('checkout_shipping'));

    	$view->set('checkout_shipping_address', $url->ssl('checkout_address', 'shipping'));
		
		$view->set('payment_address', $address->getFormatted($session->get('payment_address_id'), '<br />'));
    
    	$view->set('payment_method', $payment->getTitle($session->get('payment_method')));
	
    	$view->set('checkout_payment', $url->ssl('checkout_payment'));

    	$view->set('checkout_payment_address', $url->ssl('checkout_address', 'payment'));

    	$product_data = array();

    	foreach ($cart->getProducts() as $product) {
      		$option_data = array();

      		foreach ($product['option'] as $option) {
        		$option_data[] = array(
          			'name'  => $option['name'],
          			'value' => $option['value']
        		);
      		} 
 
      		$product_data[] = array(
				'product_id' => $product['product_id'],
        		'href'       => $url->href('product', FALSE, array('product_id' => $product['product_id'])),
        		'name'       => $product['name'],
        		'model'      => $product['model'],
        		'option'     => $option_data,
        		'quantity'   => $product['quantity'],
				'tax'        => $tax->getRate($product['tax_class_id']),
        		'price'      => $currency->format($tax->calculate($product['price'], $product['tax_class_id'], $config->get('config_tax'))),
				'discount'   => ($product['discount'] ? $currency->format($tax->calculate($product['price'] - $product['discount'], $product['tax_class_id'], $config->get('config_tax'))) : NULL),
        		'total'      => $currency->format($tax->calculate($product['total'], $product['tax_class_id'], $config->get('config_tax')))
      		); 
    	} 

    	$view->set('products', $product_data);
   		
		$totals = $calculate->getTotals();
    	
		$view->set('totals', $totals);
	
		$view->set('comment', $session->get('comment'));
    
		$view->set('back', $url->ssl('checkout_payment'));
				
		$order->set('customer_id', $customer->getId());
		$order->set('firstname', $customer->getFirstName());
		$order->set('lastname', $customer->getLastName());
		$order->set('email', $customer->getEmail());
		$order->set('telephone', $customer->getTelephone());
		$order->set('fax', $customer->getFax());
		$order->set('order_status_id', $config->get('config_order_status_id'));
		$order->set('total', $cart->getTotal());
		$order->set('currency', $currency->getCode());
		$order->set('value', $currency->getValue($currency->getCode()));
		$order->set('ip', $_SERVER['REMOTE_ADDR']);
		
		$order->set('shipping_firstname', $address->getFirstName($session->get('shipping_address_id')));
		$order->set('shipping_lastname', $address->getLastName($session->get('shipping_address_id')));
		$order->set('shipping_company', $address->getCompany($session->get('shipping_address_id')));
		$order->set('shipping_address_1', $address->getAddress1($session->get('shipping_address_id')));
		$order->set('shipping_address_2', $address->getAddress2($session->get('shipping_address_id')));
		$order->set('shipping_city', $address->getCity($session->get('shipping_address_id')));
		$order->set('shipping_postcode', $address->getPostCode($session->get('shipping_address_id')));
		$order->set('shipping_zone', $address->getZone($session->get('shipping_address_id')));
		$order->set('shipping_country', $address->getCountry($session->get('shipping_address_id')));
		$order->set('shipping_address_format', $address->getFormat($session->get('shipping_address_id')));
		
		$order->set('shipping_method', $shipping->getTitle($session->get('shipping_method')));
		
		$order->set('payment_firstname', $address->getFirstName($session->get('payment_address_id')));
		$order->set('payment_lastname', $address->getLastName($session->get('payment_address_id')));
		$order->set('payment_company', $address->getCompany($session->get('payment_address_id')));
		$order->set('payment_address_1', $address->getAddress1($session->get('payment_address_id')));
		$order->set('payment_address_2', $address->getAddress2($session->get('payment_address_id')));
		$order->set('payment_city', $address->getCity($session->get('payment_address_id')));
		$order->set('payment_postcode', $address->getPostCode($session->get('payment_address_id')));
		$order->set('payment_zone', $address->getZone($session->get('payment_address_id')));
		$order->set('payment_country', $address->getCountry($session->get('payment_address_id')));
		$order->set('payment_address_format', $address->getFormat($session->get('payment_address_id')));
				
		$order->set('payment_method', $payment->getTitle($session->get('payment_method')));
	
		$email = $this->locator->create('template');
    	
		$email->set('email_greeting', $language->get('email_greeting', $customer->getFirstName()));
    	$email->set('email_thanks', $language->get('email_thanks', $config->get('config_store')));
    	$email->set('email_order', $language->get('email_order', $order->getReference()));
   		$email->set('email_date', $language->get('email_date', date($language->get('date_format_long'))));
    	$email->set('email_invoice', $language->get('email_invoice', $url->ssl('account_invoice', FALSE, array('reference' => $order->getReference())), $url->ssl('account_invoice', FALSE, array('reference' => $order->getReference()))));
    	$email->set('email_date', $language->get('email_date', date($language->get('date_format_long'))));
    	$email->set('email_shipping_address', $language->get('email_shipping_address'));
    	$email->set('email_shipping_method', $language->get('email_shipping_method'));
    	$email->set('email_email', $language->get('email_email'));
    	$email->set('email_telephone', $language->get('email_telephone'));
		$email->set('email_fax', $language->get('email_fax'));
    	$email->set('email_payment_address', $language->get('email_payment_address'));
    	$email->set('email_payment_method', $language->get('email_payment_method'));
    	$email->set('email_comment', $language->get('email_comment'));
    	$email->set('email_thanks_again', $language->get('email_thanks_again', $config->get('config_store')));
    	$email->set('email_product', $language->get('email_product'));
    	$email->set('email_model', $language->get('email_model'));
    	$email->set('email_quantity', $language->get('email_quantity'));
    	$email->set('email_price', $language->get('email_price'));
    	$email->set('email_total', $language->get('email_total'));
	 	
		$email->set('store', $config->get('config_store'));
		$email->set('email', $customer->getEmail());
		$email->set('telephone', $customer->getTelephone());
		$email->set('fax', $customer->getFax());
		$email->set('shipping_address', $address->getFormatted($session->get('shipping_address_id'), '<br />'));
		$email->set('shipping_method', $shipping->getTitle($session->get('shipping_method')));
		$email->set('payment_address', $address->getFormatted($session->get('payment_address_id'), '<br />'));
		$email->set('payment_method', $payment->getTitle($session->get('payment_method')));
		$email->set('products', $product_data);
		$email->set('totals', $totals);
		$email->set('comment', $session->get('comment'));
    
		$product_data = array();
	
		foreach ($cart->getProducts() as $product) {
      		$option_data = array();

      		foreach ($product['option'] as $option) {
        		$option_data[] = array(
          			'name'   => $option['name'],
          			'value'  => $option['value'],
		  			'prefix' => $option['prefix']
        		);
      		}

      		$product_data[] = array(
        		'product_id' => $product['product_id'],
				'name'       => $product['name'],
        		'model'      => $product['model'],
        		'option'     => $option_data,
				'download'   => $product['download'],
				'quantity'   => $product['quantity'],
				'price'      => $product['price'],
				'discount'   => $product['discount'],
        		'total'      => $product['total'],
				'tax'        => $tax->getRate($product['tax_class_id']),
      		); 
    	}
		
		$order->set('products', $product_data);
		$order->set('totals', $totals);
		$order->set('coupon_id', $coupon->getId());
		$order->set('comment', $session->get('comment'));
		$order->set('email_subject', $language->get('email_subject', $order->getReference()));

		$order->set('email_html', $email->fetch('content/checkout_email.tpl'));
		$order->set('email_text', $language->get('email_message', $config->get('config_store'), $order->getReference(), $url->ssl('account_invoice', FALSE, array('reference' => $order->getReference())), date($language->get('date_format_long')), strip_tags($session->get('comment'))));

    	$view->set('fields', $payment->getFields($session->get('payment_method')));

		if ($config->get('config_checkout_id')) {
			$information_info = $database->getRow("select * from information i left join information_description id on (i.information_id = id.information_id) where i.information_id = '" . (int)$config->get('config_checkout_id') . "'");
		
			$view->set('agree', $language->get('text_agree', $url->href('information', FALSE, array('information_id' => $config->get('config_checkout_id'))), $information_info['title']));
		}
		
		$order->save($order->getReference());
				      
    	$template->set('content', $view->fetch('content/checkout_confirm.tpl'));

		$template->set($module->fetch());
		
		$response->set($template->fetch('layout.tpl'));
  	}
	
	function validate() {
		$coupon   =& $this->locator->get('coupon');
		$language =& $this->locator->get('language');
		$request  =& $this->locator->get('request');
		
		if (!$coupon->set($request->get('coupon', 'post'))) {
			$this->error['message'] = $language->get('error_coupon');
			
			if (!$coupon->hasProduct()) {
				$this->error['message'] = $language->get('error_product'); 
			}
		}
		
		if (!$this->error) {
			return TRUE;
		} else {
			return FALSE;
		}
	}
}
?>
