<?php
class ControllerCartRecover extends Controller {
	private $error = array();

	public function index() {
		if (!$this->customer->isLogged()) {
			$this->session->data['redirect'] = $this->url->link('cart/recover', '', 'SSL');

			$this->redirect($this->url->link('account/login', '', 'SSL'));
		}
		
		$language = $this->load->language('cart/recover');
		$this->load->model('cart/recover');		
		$this->data = array_merge($this->data, $language);
		$this->data['futuragrade_status'] = $this->config->get('futuragrade_status');
		
		if (isset($this->request->get['data'])) {
				$data = array (
					'customer_id' => $this->customer->getIdFutura(),
					'data_add'    => $this->request->get['data']
				);
				
				if (!empty($data['customer_id'])){
					$recover_info = $this->model_cart_recover->getProductsCartInfo($data);		
				}
				
				if($recover_info){
						
						$results = $this->model_cart_recover->getProductsCart($data);						
						foreach($results as $result){
							
							$this->session->data['success'] = sprintf($this->language->get('text_success'), implode('/', array_reverse(explode('-', $this->request->get['data']))));
									
							$this->cart->add($result['product_id'], $result['quantity'], '');			
											
						}
						
				}
				
			 $this->redirect($this->url->link('checkout/cart'));	
		}
		
		$this->data['breadcrumbs'] = array();

		$this->data['breadcrumbs'][] = array(
			'text'      => $this->language->get('text_home'),
			'href'      => $this->url->link('common/home','','NONSSL','false'),
			'separator' => false
		);

		$this->data['breadcrumbs'][] = array(
			'text'      => $this->language->get('text_account'),
			'href'      => $this->url->link('account/account', '', 'SSL'),
			'separator' => $this->language->get('text_separator')
		);
		
		$url = '';
		
		if (isset($this->request->get['page'])) {
			$url .= '&page=' . $this->request->get['page'];
		}		
		
		$this->data['breadcrumbs'][] = array(
			'text'      => $this->language->get('heading_title'),
			'href'      => $this->url->link('cart/recover', $url, 'SSL'),
			'separator' => $this->language->get('text_separator')
		);

		if (isset($this->request->get['page'])) {
			$page = $this->request->get['page'];
		} else {
			$page = 1;
		}

		$this->data['recovers'] = array();
		
		$data = array (
			'customer_id' => $this->customer->getIdFutura(),
			'start'       => ($page - 1) * 10,
			'limit'       => 10
		);
		if (!empty($data['customer_id'])){
		  $recover_total = $this->model_cart_recover->getTotalProductsCart($data);
			$results = $this->model_cart_recover->getProductsCartInfo($data);
		
			foreach($results as $result){
				$this->data['recovers'][] = array(
					'data_add'	 	=> implode('/', array_reverse(explode('-', $result['data_add']))),
					'hrefview'   	=> $this->url->link('cart/recover/info', 'data=' . $result['data_add'], 'SSL'),								
					'products'   	=> $result['products'],
					'name'			 	=> $result['firstname'].' '.$result['lastname'],
					'total'      	=>$this->currency->format($this->tax->calculate($result['total'], $result['tax_class_id'], $this->config->get('config_tax')))
				);	
			}
				
					
			$pagination = new Pagination();
			$pagination->total = $recover_total;
			$pagination->page = $page;
			$pagination->limit = 10;
			$pagination->text = $this->language->get('text_pagination');
			$pagination->url = $this->url->link('cart/recover', 'page={page}', 'SSL');

			$this->data['pagination'] = $pagination->render();
		}

		$this->data['continue'] = $this->url->link('account/account', '', 'SSL');

		if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/cart/recover_list.tpl')) {
			$this->template = $this->config->get('config_template') . '/template/cart/recover_list.tpl';
		} else {
			$this->template = 'default/template/cart/recover_list.tpl';
		}

		$this->children = array(
			'common/column_right',
			'common/content_top',
			'common/content_bottom',
			'common/footer',
			'common/header'
		);

		$this->response->setOutput($this->render());
		
	}

	public function info() { 
		$this->language->load('cart/recover');
		
		if (isset($this->request->get['data'])) {
			$data_add = $this->request->get['data'];
		} else {
			$data_add = 0;
		}	

		if (!$this->customer->isLogged()) {
			$this->session->data['redirect'] = $this->url->link('account/order/info', 'order_id=' . $order_id, 'SSL');			
			$this->redirect($this->url->link('account/login', '', 'SSL'));
    }
		
		if($data_add){
			$this->load->model('cart/recover');
			$this->load->model('catalog/product');
			
			$this->document->setTitle($this->language->get('text_order'));
			
			$this->data['breadcrumbs'] = array();
		
			$this->data['breadcrumbs'][] = array(
				'text'      => $this->language->get('text_home'),
				'href'      => $this->url->link('common/home'),        	
				'separator' => false
			); 
		
			$this->data['breadcrumbs'][] = array(
				'text'      => $this->language->get('text_account'),
				'href'      => $this->url->link('account/account', '', 'SSL'),        	
				'separator' => $this->language->get('text_separator')
			);
			
			$url = '';
			
			if (isset($this->request->get['data'])) {
				$url .= '&data=' . $this->request->get['data'];
			}
						
			$this->data['breadcrumbs'][] = array(
				'text'      => $this->language->get('heading_title'),
				'href'      => $this->url->link('cart/recover', '', 'SSL'),      	
				'separator' => $this->language->get('text_separator')
			);
			
			$this->data['breadcrumbs'][] = array(
				'text'      => $this->language->get('text_order'),
				'href'      => $this->url->link('cart/recover/info', 'data=' . $this->request->get['data'], 'SSL'),
				'separator' => $this->language->get('text_separator')
			);			
			
			$this->data['heading_title'] = $this->language->get('text_order');	
			$this->data['text_order_detail']	= $this->language->get('text_order_detail');
			$this->data['text_order_total']	= $this->language->get('text_total');
			$this->data['text_empty_info'] = $this->language->get('text_empty_info');
			$this->data['column_name'] = $this->language->get('column_name');
			$this->data['column_model'] = $this->language->get('column_model');
			$this->data['column_quantity'] = $this->language->get('column_quantity');
			$this->data['column_price'] = $this->language->get('column_price');
			$this->data['column_total'] = $this->language->get('column_total');
			$this->data['column_repurchase'] = $this->language->get('column_repurchase');	
			$this->data['button_recover']	= $this->language->get('button_recover');			
			$this->data['hrefrecover'] = $this->url->link('cart/recover', 'data=' . $data_add, 'SSL') ;
			
			$data = array (
				'customer_id' => $this->customer->getIdFutura(),
				'data_add'    => $this->request->get['data']
			);
			
			$results = $this->model_cart_recover->getProductsCartInfo($data);		
			foreach($results as $result){
				$this->data['text_recover_data'] = sprintf($this->language->get('text_recover_data') ,implode('/', array_reverse(explode('-', $result['data_add']))));
				$this->data['text_recover_products'] =  sprintf($this->language->get('text_recover_products') ,$result['products']);
				$this->data['total']    = $this->currency->format($this->tax->calculate($result['total'], $result['tax_class_id'], $this->config->get('config_tax')));
			}
			
			$this->data['products'] = array();

			$results = $this->model_cart_recover->getProductsCart($data);					
			foreach($results as $result){
				$product_info = $this->model_catalog_product->getProduct($result['product_id']);
				
				if (($this->config->get('config_customer_price') && $this->customer->isLogged()) || !$this->config->get('config_customer_price')) {
					$price = $this->currency->format($this->tax->calculate($product_info['price'], $product_info['tax_class_id'], $this->config->get('config_tax')));
				} else {
					$price = false;
				}				
				
				if($product_info){
					$this->data['products'][] = array(
						'product_id' => $result['product_id'],
						'name'	 	   => $product_info['name'],
						'model'      => $product_info['model'],
						'quantity'   => (int)$result['quantity'],	
						'price'		   => $price,
						'href'			 => $this->url->link('product/product', 'product_id=' . $result['product_id'], 'SSL')				
					);						
				}
			}
						
			$this->data['continue'] = $this->url->link('account/account', '', 'SSL');
			$this->data['button_continue'] = $this->language->get('button_continue');

			if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/cart/recover_info.tpl')) {
				$this->template = $this->config->get('config_template') . '/template/cart/recover_info.tpl';
			} else {
				$this->template = 'default/template/cart/recover_info.tpl';
			}
			
			$this->children = array(
				'common/column_right',
				'common/content_top',
				'common/content_bottom',
				'common/footer',
				'common/header'	
			);
								
			$this->response->setOutput($this->render());					
			
		}else{
			$this->document->setTitle($this->language->get('text_order'));
			
			$this->data['heading_title'] = $this->language->get('text_order');
	
			$this->data['text_error'] = $this->language->get('text_error');
	
			$this->data['button_continue'] = $this->language->get('button_continue');
			
			$this->data['breadcrumbs'] = array();
			
			$this->data['breadcrumbs'][] = array(
				'text'      => $this->language->get('text_home'),
				'href'      => $this->url->link('common/home'),
				'separator' => false
			);
			
			$this->data['breadcrumbs'][] = array(
				'text'      => $this->language->get('text_account'),
				'href'      => $this->url->link('account/account', '', 'SSL'),
				'separator' => $this->language->get('text_separator')
			);
			
			$this->data['breadcrumbs'][] = array(
				'text'      => $this->language->get('heading_title'),
				'href'      => $this->url->link('cart/recover', '', 'SSL'),
				'separator' => $this->language->get('text_separator')
			);
			
			$this->data['breadcrumbs'][] = array(
				'text'      => $this->language->get('text_order'),
				'href'      => $this->url->link('cart/recover/info', '', 'SSL'),
				'separator' => $this->language->get('text_separator')
			);
												
					$this->data['continue'] = $this->url->link('account/order', '', 'SSL');
						
			if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/error/not_found.tpl')) {
				$this->template = $this->config->get('config_template') . '/template/error/not_found.tpl';
			} else {
				$this->template = 'default/template/error/not_found.tpl';
			}
			
			$this->children = array(
				'common/column_left',
				'common/column_right',
				'common/content_top',
				'common/content_bottom',
				'common/footer',
				'common/header'	
			);
								
			$this->response->setOutput($this->render());				
		}		
	
	}
			
}
?>
