<?php
/**
* @version		3.0.0
* @package		JomLand TicketCenter
* @copyright	Copyright (C) 2010 JomLand.com. All rights reserved.
* @license		GNU/GPLv3 http://www.gnu.org/licenses/gpl-3.0.html
*/
defined('_JEXEC') or die();

jimport( 'joomla.application.component.model' );

class JLTicketCenterModelTickets extends JModelLegacy
{
	
	private $event;
	
	function getEvent(){
		$where_array = array();
		if (JRequest::getInt('id',0)>0){
			$where_array[] = 'e.`id`='.JRequest::getInt('id',0);
		}else{
			return;
		}
		
		$this->hit();
		
		$db = JFactory::getDBO();
		$pparams = new JLTicketCenterHelperSetup;
		
		$query = 'SELECT e.*,p.`performer_name`,v.`venue_name`,v.`address`,v.`seats_chart`,city.`city_name`,state.`state_name`,country.`country`  AS `country`, cat.`title` as `cat_name`,p.`cat_id`,cat.`description` as `cat_description` ';
		$query .= ' FROM #__jltcenter_events e';
		
		$query .= " LEFT JOIN #__jltcenter_performer p  ON p.`id`=e.`performer_id` ";
		$query .= " LEFT JOIN #__jltcenter_venue v  ON v.`id`=e.`venue_id` ";
		$query .= " LEFT JOIN #__jltcenter_city city  ON city.`id`=e.`city_id` ";
		$query .= " LEFT JOIN #__jltcenter_state state  ON state.`id`=e.`state_id` ";
		$query .= " LEFT JOIN #__jltcenter_country country ON country.`id`=e.`country_id` ";
		$query .= " LEFT JOIN #__categories cat ON cat.`id`=p.`cat_id`";
		
		$query .=" WHERE ".implode(' AND ',$where_array)." 
					ORDER BY `event_date`";

		$db->setQuery($query);
		$event  = $db->loadObject();
		 
		$this->event =$event; 
		return $event; 
	}
	
	
	function getTickets(){
		
		$where_array = array();
		if (JRequest::getInt('id',0)>0){
			$where_array[] = 'e.`id`='.JRequest::getInt('id',0);
		}else{
			return;
		}
		
		$db = JFactory::getDBO();
		$db = $this->getDBO();
		
		$query = "SELECT * 
				FROM #__jltcenter_events_ticket_type t
				WHERE t.`event_id`=".JRequest::getInt('id',0);
				
		$pparams = new JLTicketCenterHelperSetup;
		
		if ($pparams->get('smart_cart_manage')==1){
			$cart_tickets  = "(SELECT IFNULL(SUM(cart.`quantity`),0)
							  FROM #__jltcenter_cart cart
							WHERE cart.`ticket_type`= t.`ticket_type`)";
			 $query .=" AND t.`tickets`-t.`tickets_sold`-t.`tickets_ordered`- $cart_tickets >0 ";
		}else{
			$query .=" AND t.`tickets`-t.`tickets_sold`-t.`tickets_ordered`>0 ";
		}
		$query .=" ORDER BY t.`ticket_price`";
		
		$db->setQuery($query);
		$tickets = $db->loadObjectList();
		
		if (!empty($tickets)){
			// Check availibility
			$pparams = new JLTicketCenterHelperSetup;
			if ($pparams->get('cart_countdown',10)>0){
				$start_time = $pparams->get('cart_countdown',10)*60;
				
				$query = "SELECT cart.`ticket_type`,SUM(cart.`quantity`) AS `amount`
					 	  FROM `#__jltcenter_cart` cart
					 	  WHERE  cart.`event_id`= ".JRequest::getInt('id',0) .
							" AND `add_time` BETWEEN UNIX_TIMESTAMP()-".$start_time. " AND UNIX_TIMESTAMP()
							 GROUP BY cart.`ticket_type`"; 
				$db->setQuery($query);
				//echo $db->getQuery();
				$in_cart_results  = $db->loadObjectList();
				if(!empty($in_cart_results)){
					$in_cart = array();
					foreach ($in_cart_results as $cart_line){
						$in_cart[$cart_line->ticket_type]  = $cart_line->amount ;
					}
				}
			}//end cart check
			
			JLoader::import('components.com_jlticketcenter.models.cart_class',JPATH_ROOT);
			foreach ($tickets as $k=>$ticket){
				
				
				if (!empty($in_cart[$ticket->ticket_type])){
					$ticket->in_cart = $in_cart[$ticket->ticket_type];
				}else{
					$ticket->in_cart = 0;	
				}
				if($ticket->tickets-$ticket->tickets_sold-$ticket->tickets_ordered-$ticket->in_cart <=0){
					unset($tickets[$k]);				
				}else{
					$dropdown = JLTicketCenterCart::quantityDropdown(0,'qty','',$ticket);
					$ticket->dropdown  = $dropdown;	
				}
				$now  = new JDate(); 

				if($this->event->pre_sale_date>0 && $this->event->pre_sale_date >= $now->toSql() && $this->event->pre_sale_discount>0){
					$ticket->pre_sale_date=$this->event->pre_sale_date;
					$ticket->pre_sale_price=  number_format($ticket->ticket_price - ($ticket->ticket_price*($this->event->pre_sale_discount/100)),2);
				}
			}
			return $tickets;
		}else{
			return  false;
		}
		
		
		
		 
	}//EOF getTickets
	
	
	function array2object($array) {
	    if (is_array($array)) {
	        $obj = new StdClass();
	 
	        foreach ($array as $key => $val){
	            $obj->$key = $val;
	        }
	    }
	    else { $obj = $array; }
	 
	    return $obj;
	}
	
	
	function getPerformer(){
		$db = JFactory::getDBO();
		
		$query = " SELECT p.*, cat.`title` as `cat_title` "
			. " FROM #__jltcenter_performer p "
			. " LEFT JOIN #__categories cat ON cat.`id`=p.`cat_id` AND cat.`extension` = 'com_jlticketcenter'"
			. " WHERE p.`id`=" .JRequest::getInt('performer',0);
		
		$db->setQuery($query);
			
		$performer = $db->loadObject();
		
		return $performer;
	}
	
	
	function hit()
	{
		if (JRequest::getInt('id',0))
		{
			$event = JTable::getInstance('Events','Table');
			$event->hit(JRequest::getInt('id',0));
			return true;
		}
		return false;
	}//EOF Hit
}//EOC