<?php
class CalculateCoupon extends Calculate {
	function __construct(&$locator) {		
		$this->cart     =& $locator->get('cart');
		$this->coupon   =& $locator->get('coupon');
		$this->config   =& $locator->get('config');
		$this->currency =& $locator->get('currency');
		$this->language =& $locator->get('language');
		$this->session  =& $locator->get('session');
		$this->shipping =& $locator->get('shipping');
		$this->tax      =& $locator->get('tax');
	
		$this->language->load('extension/calculate/coupon.php');
  	}

	function calculate() {
		$total_data = array();
		
		if (($this->config->get('coupon_status')) && ($this->coupon->getId())) {
			if ($this->coupon->getDiscount($this->cart->getTotal()) > 0) {
      			$total_data[] = array(
        			'title' => $this->language->get('text_coupon_title', $this->coupon->getName()),
	    			'text'  => '-' . $this->currency->format($this->coupon->getDiscount($this->cart->getTotal())),
        			'value' => $this->coupon->getDiscount($this->cart->getTotal())
      			);
				
				$this->cart->decreaseTotal($this->coupon->getDiscount($this->cart->getTotal()));
			}
						
			if (($this->coupon->getShipping()) && ($this->cart->hasShipping())) {
      			$total_data[] = array(
        			'title' => $this->language->get('text_coupon_shipping'),
	    			'text'  => '-' . $this->currency->format($this->tax->calculate($this->shipping->getCost($this->session->get('shipping_method')), $this->shipping->getTaxClassId($this->session->get('shipping_method')))),
        			'value' => $this->tax->calculate($this->shipping->getCost($this->session->get('shipping_method')), $this->shipping->getTaxClassId($this->session->get('shipping_method')))
      			);
							
				$this->cart->decreaseTotal($this->tax->calculate($this->shipping->getCost($this->session->get('shipping_method')), $this->shipping->getTaxClassId($this->session->get('shipping_method'))));
			}
    	}
	
    	return $total_data;
	}
	
	function getSortOrder() {
		return $this->config->get('coupon_sort_order');
	}
}
?>
