*  @copyright  2007-2011 PrestaShop SA
*  @version  Release: $Revision: 7046 $
*  @license    http://opensource.org/licenses/osl-3.0.php  Open Software License (OSL 3.0)
*  International Registered Trademark & Property of PrestaShop SA
*/
include_once(PS_ADMIN_DIR.'/../classes/AdminTab.php');
class AdminCarts extends AdminTab
{
	public function __construct()
	{
	 	$this->table = 'cart';
	 	$this->className = 'Cart';
		$this->lang = false;
	 	$this->edit = false;
	 	$this->view = true;
	 	$this->delete = true;
		$this->_select = 'CONCAT(LEFT(c.`firstname`, 1), \'. \', c.`lastname`) `customer`, a.id_cart total, ca.name carrier, o.id_order ';
		$this->_join = 'LEFT JOIN '._DB_PREFIX_.'customer c ON (c.id_customer = a.id_customer)
		LEFT JOIN '._DB_PREFIX_.'currency cu ON (cu.id_currency = a.id_currency)
		LEFT JOIN '._DB_PREFIX_.'carrier ca ON (ca.id_carrier = a.id_carrier)
		LEFT JOIN '._DB_PREFIX_.'orders o ON (o.id_cart = a.id_cart) ';
 		$this->fieldsDisplay = array(
			'id_cart' => array('title' => $this->l('ID'), 'align' => 'center', 'width' => 25),
			'id_order' => array('title' => $this->l('ID Order'), 'align' => 'center', 'width' => 25),
			'customer' => array('title' => $this->l('Customer'), 'width' => 80, 'filter_key' => 'c!lastname'),
			'total' => array('title' => $this->l('Total'), 'callback' => 'getOrderTotalUsingTaxCalculationMethod', 'orderby' => false, 'search' => false, 'width' => 50, 'align' => 'right', 'prefix' => '', 'suffix' => '', 'currency' => true),
			'carrier' => array('title' => $this->l('Carrier'), 'width' => 25, 'align' => 'center', 'callback' => 'replaceZeroByShopName', 'filter_key' => 'ca!name'),
			'date_add' => array('title' => $this->l('Date'), 'width' => 90, 'align' => 'right', 'type' => 'datetime', 'filter_key' => 'a!date_add'));
		parent::__construct();
	}
	public function viewDetails()
	{
		global $currentIndex, $cookie;
		if (!($cart = $this->loadObject(true)))
			return;
		$customer = new Customer($cart->id_customer);
		$customerStats = $customer->getStats();
		$products = $cart->getProducts();
		$customizedDatas = Product::getAllCustomizedDatas((int)($cart->id));
		Product::addCustomizationPrice($products, $customizedDatas);
		$summary = $cart->getSummaryDetails();
		$discounts = $cart->getDiscounts();
		$currency = new Currency($cart->id_currency);
		$currentLanguage = new Language((int)($cookie->id_lang));
		// display cart header
		echo '
'.(($customer->id) ? $customer->firstname.' '.$customer->lastname : $this->l('Guest')).' - '.$this->l('Cart #').sprintf('%06d', $cart->id).' '.$this->l('from').' '.$cart->date_upd.'
';
		/* Display customer information */
		echo '
		
		
		';
		echo '
		
 
		';
		/* Display order information */
		$id_order = (int)(Order::getOrderByCartId($cart->id));
		$order = new Order($id_order);
		if ($order->getTaxCalculationMethod() == PS_TAX_EXC)
		{
		    $total_products = $summary['total_products'];
		    $total_discount = $summary['total_discounts_tax_exc'];
		    $total_wrapping = $summary['total_wrapping_tax_exc'];
		    $total_price = $summary['total_price_without_tax'];
   		    $total_shipping = $summary['total_shipping_tax_exc'];
		} else {
		    $total_products = $summary['total_products_wt'];
		    $total_discount = $summary['total_discounts'];
		    $total_wrapping = $summary['total_wrapping'];
		    $total_price = $summary['total_price'];
  		    $total_shipping = $summary['total_shipping'];
		}
		echo '
		
';
		echo '
		
 ';
		// List of products
		echo '
		
			
		 
';
	}
	private function displayCustomizedDatas(&$customizedDatas, &$product, &$currency, &$image, $tokenCatalog, &$stock)
	{
		if (!($order = $this->loadObject(true)))
			return;
		if (is_array($customizedDatas) AND isset($customizedDatas[(int)($product['id_product'])][(int)($product['id_product_attribute'])]))
		{
			$image = new Image($image['id_image']);
			echo '
			
				| '.(isset($image['id_image']) ? cacheImage(_PS_IMG_DIR_.'p/'.$image->getExistingImgPath().'.jpg',
				'product_mini_'.(int)($product['id_product']).(isset($product['id_product_attribute']) ? '_'.(int)($product['id_product_attribute']) : '').'.jpg', 45, 'jpg') : '--').' | 
				
					'.$product['name'].' 
					'.($product['reference'] ? $this->l('Ref:').' '.$product['reference'] : '')
					.(($product['reference'] AND $product['supplier_reference']) ? ' / '.$product['supplier_reference'] : '')
					.' | 
				'.Tools::displayPrice($product['price_wt'], $currency, false).' | 
				'.$product['customizationQuantityTotal'].' | 
				'.(int)($stock['quantity']).' | 
				'.Tools::displayPrice($product['total_customization_wt'], $currency, false).' | 
			
';
			foreach ($customizedDatas[(int)($product['id_product'])][(int)($product['id_product_attribute'])] AS $customization)
			{
				echo '
				
					';
				foreach ($customization['datas'] AS $type => $datas)
					if ($type == _CUSTOMIZE_FILE_)
					{
						$i = 0;
						echo '';
						foreach ($datas AS $data)
							echo '- 
									
 
								 ';
						echo ' ';
					}
					elseif ($type == _CUSTOMIZE_TEXTFIELD_)
					{
						$i = 0;
						echo '';
						foreach ($datas AS $data)
							echo '- '.($data['name'] ? $data['name'] : $this->l('Text #').++$i).$this->l(':').' '.$data['value'].'
 ';
						echo ' ';
					}
				echo ' | 
					 | 
					'.$customization['quantity'].' | 
					 | 
					 | 
				
';
			}
		}
	}
	public function display()
	{
		global $cookie;
		if (isset($_GET['view'.$this->table]))
			$this->viewDetails();
		else
		{
			$this->getList((int)($cookie->id_lang), !Tools::getValue($this->table.'Orderby') ? 'date_add' : NULL, !Tools::getValue($this->table.'Orderway') ? 'DESC' : NULL);
			$this->displayList();
		}
	}
	
	protected function _displayDeleteLink($token = NULL, $id)
	{
		global $currentIndex;
		
		foreach ($this->_list as $cart)
			if ($id == $cart['id_cart'])
				if ($cart['id_order'])
					return;
		
		$_cacheLang['Delete'] = $this->l('Delete', __CLASS__, true, false);
		$_cacheLang['DeleteItem'] = $this->l('Delete item #', __CLASS__, true, false).$id.' ?)';
		
		echo '
			
			
		';
	}
}