<?php 
class ControllerModuleCart extends Controller {
	public function index() {
		$this->language->load('module/cart');
		
      	if (isset($this->request->get['remove'])) {
          	$this->cart->remove($this->request->get['remove']);
			
			unset($this->session->data['vouchers'][$this->request->get['remove']]);
      	}	
			
		// Totals
		$this->load->model('setting/extension');
		
		$total_data = array();					
		$total = 0;
		$taxes = $this->cart->getTaxes();
		
		// Display prices
		if (($this->config->get('config_customer_price') && $this->customer->isLogged()) || !$this->config->get('config_customer_price')) {
			$sort_order = array(); 
			
			$results = $this->model_setting_extension->getExtensions('total');
			
			foreach ($results as $key => $value) {
				$sort_order[$key] = $this->config->get($value['code'] . '_sort_order');
			}
			
			array_multisort($sort_order, SORT_ASC, $results);
			
			foreach ($results as $result) {
				if ($this->config->get($result['code'] . '_status')) {
					$this->load->model('total/' . $result['code']);
		
					$this->{'model_total_' . $result['code']}->getTotal($total_data, $total, $taxes);
				}
				
				$sort_order = array(); 
			  
				foreach ($total_data as $key => $value) {
					$sort_order[$key] = $value['sort_order'];
				}
	
				array_multisort($sort_order, SORT_ASC, $total_data);			
			}		
		}
		
		$this->data['totals'] = $total_data;
		
		$this->data['heading_title'] = $this->language->get('heading_title');
		
		$this->data['text_items'] = sprintf($this->language->get('text_items'), $this->cart->countProducts() + (isset($this->session->data['vouchers']) ? count($this->session->data['vouchers']) : 0), $this->currency->format($total));
		$this->data['text_empty'] = $this->language->get('text_empty');
		$this->data['text_cart'] = $this->language->get('text_cart');
		$this->data['text_checkout'] = $this->language->get('text_checkout');
		
		$this->data['button_remove'] = $this->language->get('button_remove');
		
		$this->load->model('tool/image');
		
		$this->data['products'] = array();
			
		foreach ($this->cart->getProducts() as $product) {
			if ($product['image']) {
				$image = $this->model_tool_image->resize($product['image'], $this->config->get('config_image_cart_width'), $this->config->get('config_image_cart_height'));
			} else {
				$image = '';
			}
							
			$option_data = array();
			
			foreach ($product['option'] as $option) {
				if ($option['type'] != 'file') {
					$value = $option['option_value'];	
				} else {
					$filename = $this->encryption->decrypt($option['option_value']);
					
					$value = utf8_substr($filename, 0, utf8_strrpos($filename, '.'));
				}				
				
				$option_data[] = array(								   
					'name'  => $option['name'],
					'value' => (utf8_strlen($value) > 20 ? utf8_substr($value, 0, 20) . '..' : $value),
					'type'  => $option['type']
				);
			}
			
			// Display prices
			if (($this->config->get('config_customer_price') && $this->customer->isLogged()) || !$this->config->get('config_customer_price')) {
				$price = $this->currency->format($this->tax->calculate($product['price'], $product['tax_class_id'], $this->config->get('config_tax')));
			} else {
				$price = false;
			}
			
			// Display prices
			if (($this->config->get('config_customer_price') && $this->customer->isLogged()) || !$this->config->get('config_customer_price')) {
				$total = $this->currency->format($this->tax->calculate($product['price'], $product['tax_class_id'], $this->config->get('config_tax')) * $product['quantity']);
			} else {
				$total = false;
			}
													
			$this->data['products'][] = array(

                //Devman Extensions - info@devmanextensions.com - 2017-06-02 17:26:33 - vqmod/xml/devmanextensions_perfect_one_checkout.xml
                'stock'      => $product['stock'],
                'product_id'      => $product['product_id'],
            
				'key'      => $product['key'],
				'thumb'    => $image,
				'name'     => $product['name'],
				'model'    => $product['model'], 
				'option'   => $option_data,
				'quantity' => $product['quantity'],
				'price'    => $price,	
				'total'    => $total,	
				'href'     => $this->url->link('product/product', 'product_id=' . $product['product_id'])		
			);
		}
		
		// Gift Voucher
		$this->data['vouchers'] = array();
		
		if (!empty($this->session->data['vouchers'])) {
			foreach ($this->session->data['vouchers'] as $key => $voucher) {
				$this->data['vouchers'][] = array(
					'key'         => $key,
					'description' => $voucher['description'],
					'amount'      => $this->currency->format($voucher['amount'])
				);
			}
		}
					
		$this->data['cart'] = $this->url->link('checkout/cart');
						
		$this->data['checkout'] = $this->url->link('checkout/checkout', '', 'SSL');
	
		if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/module/cart.tpl')) {
			$this->template = $this->config->get('config_template') . '/template/module/cart.tpl';
		} else {
			$this->template = 'default/template/module/cart.tpl';
		}
				

                //Devman Extensions - info@devmanextensions.com - 2017-06-02 17:26:33 - vqmod/xml/devmanextensions_perfect_one_checkout.xml
                if(!empty($this->session->data['poc_cart_view']))
                {
                    $this->template = $this->session->data['poc_cart_view'];
                    $temp_data = array();
                    $temp_data['cart_insert_coupon'] = !empty($this->session->data['poc']['lang']['cart_insert_coupon']) ? $this->session->data['poc']['lang']['cart_insert_coupon'] : '';
                    $temp_data['cart_insert_gift'] = !empty($this->session->data['poc']['lang']['cart_insert_gift']) ? $this->session->data['poc']['lang']['cart_insert_gift'] : '';
                    $reward_title = !empty($this->session->data['poc']['lang']['cart_insert_reward']) ? $this->session->data['poc']['lang']['cart_insert_reward'] : '';
                    $points = 0;
                    if ($this->config->get('poc_reward') && !empty($reward_title) && strpos($reward_title, '%s') !== false)
                    {
                        $points = $this->customer->getRewardPoints();
                        $reward_title = sprintf($reward_title, $points);
                    }
                    $temp_data['points'] = $points;
                    $temp_data['cart_insert_reward'] = $reward_title;
                    $temp_data['cart_coupon_icon'] = !empty($this->session->data['poc']['lang']['cart_coupon_icon']) ? $this->session->data['poc']['lang']['cart_coupon_icon'] : '';
                    $temp_data['cart_gift_icon'] = !empty($this->session->data['poc']['lang']['cart_gift_icon']) ? $this->session->data['poc']['lang']['cart_gift_icon'] : '';
                    $temp_data['cart_reward_icon'] = !empty($this->session->data['poc']['lang']['cart_reward_icon']) ? $this->session->data['poc']['lang']['cart_reward_icon'] : '';

                    $temp_data['cart_thead_image'] = !empty($this->session->data['poc']['lang']['cart_thead_image']) ? $this->session->data['poc']['lang']['cart_thead_image'] : '';
                    $temp_data['cart_thead_name'] = !empty($this->session->data['poc']['lang']['cart_thead_name']) ? $this->session->data['poc']['lang']['cart_thead_name'] : '';
                    $temp_data['cart_thead_quantity'] = !empty($this->session->data['poc']['lang']['cart_thead_quantity']) ? $this->session->data['poc']['lang']['cart_thead_quantity'] : '';
                    $temp_data['cart_thead_price'] = !empty($this->session->data['poc']['lang']['cart_thead_price']) ? $this->session->data['poc']['lang']['cart_thead_price'] : '';
                    $temp_data['cart_thead_total'] = !empty($this->session->data['poc']['lang']['cart_thead_total']) ? $this->session->data['poc']['lang']['cart_thead_total'] : '';
                    
                    unset($this->session->data['poc_cart_view']);
                    $temp_data['coupon'] = !empty($this->session->data['coupon']) ? $this->session->data['coupon'] : '';
                    $temp_data['voucher'] = !empty($this->session->data['voucher']) ? $this->session->data['voucher'] : '';
                    $temp_data['reward'] = !empty($this->session->data['reward']) ? $this->session->data['reward'] : '';

                    if($this->config->get('poc_design_icons') == 'typicons')
                        $poc_icon_html = '<span class="typcn typcn-classIcon"></span>';
                    else
                        $poc_icon_html = '<i class="fa fa-classIcon"></i>';

                    $icon_apply_class = $this->config->get('poc_design_icons') == 'typicons' ? 'tick' : 'check';
                    $temp_data['icon_apply'] = str_replace('classIcon', $icon_apply_class, $poc_icon_html);

                    $icon_rest_class = $this->config->get('poc_design_icons') == 'typicons' ? 'minus' : 'chevron-down';
                    $temp_data['icon_rest'] = str_replace('classIcon', $icon_rest_class, $poc_icon_html);

                    $icon_sum_class = $this->config->get('poc_design_icons') == 'typicons' ? 'plus' : 'chevron-up';
                    $temp_data['icon_sum'] = str_replace('classIcon', $icon_sum_class, $poc_icon_html);

                    $temp_data['icon_remove'] = str_replace('classIcon', 'times', $poc_icon_html);
                    
                    $temp_data['poc_icon_html'] = $poc_icon_html;

                    $temp_data['poc_coupon'] = $this->config->get('poc_coupon');
                    $temp_data['poc_gift'] = $this->config->get('poc_gift');
                    $temp_data['poc_reward'] = $this->config->get('poc_reward') && isset($points);
                    
                    $temp_data['poc_layout_cart_panel_cols'] = $this->config->get('poc_layout_cart_panel_cols');
                    $icon_background_class = $this->config->get('poc_style_discount_zone_icon_class');

                    if(!empty($icon_background_class))
                        $temp_data['poc_icon_background'] = str_replace('classIcon', $icon_background_class, $poc_icon_html);
                    
                    $temp_data['config_stock_checkout'] = $this->config->get('config_stock_checkout');

                    // Validate cart has products and has stock.
                    if ((!$this->cart->hasProducts() && empty($this->session->data['vouchers'])) || (!$this->cart->hasStock() && !$this->config->get('config_stock_checkout'))) {
                    $temp_data['error'] = $this->session->data['poc']['lang']['cart_out_stock_error'];
                    }

                    // Validate minimum quantity requirements.
                    $products = $this->cart->getProducts();
                    
                    foreach ($products as $product) {
                        $product_total = 0;

                        foreach ($products as $product_2) {
                            if ($product_2['product_id'] == $product['product_id']) {
                                $product_total += $product_2['quantity'];
                            }
                        }

                        if ($product['minimum'] > $product_total) {
                            $temp_data['error'] = sprintf($this->session->data['poc']['lang']['cart_minimum_error'], $product['name'], $product['minimum']);

                            //Insert stock to false to force error and put his row in background color red
                            foreach ($this->data['products'] as $key => $prod) {
                                if($prod['product_id'] == $product['product_id'])
                                {
                                    $this->data['products'][$key]['stock'] = false;
                                    break;
                                }
                            }
                        }
                    }
                    
                    $temp_data['more_oc_2'] = version_compare(VERSION, '2.0.0.0', '>=');
                    $temp_data['cart_id_enabled'] = version_compare(VERSION, '2.1.0.2', '>=');
                    
                    if(version_compare(VERSION, '2.0.0.0', '<'))
                    {
                        foreach ($temp_data as $key => $value) {
                            $this->data[$key] = $value;
                        }
                    }
                    else
                    {
                        foreach ($temp_data as $key => $value) {
                            $data[$key] = $value;
                        }
                    }
                }
            
		$this->response->setOutput($this->render());		
	}
}
?>