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

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

class JLTicketCenterViewCart extends JViewLegacy
{
	function display($tpl = null)
	{
		$pparams = new JLTicketCenterHelperSetup;
		
		switch (JRequest::getVar('task')){
			case 'clean':
				self::clean();
			break;
			
			case 'cron':
				self::runCron();
			break;
			
			case 'cron_order':
				self::runCronOrder();
			break;
                    
                        case 'eselect':
                            self::eSelectPaymentRedirect();
                            break;
                        
                        case 'eselectpaymentprocess':
                            self::eSelectPaymentProcess();
                            break;
                        

		}
		
		
		$response = array();
		$delivery = JRequest::getInt('delivery_method',0);
		if ($delivery>0){
			$response = $this->get('DeliveryChange');

			echo $response ;
			die;
			//$response = json_encode($response);
		}
	
		$id = JRequest::getVar('id',0);
		$qty = JRequest::getVar('qty',0);
		
		if ($id>0){

			//get cart line;
			
			$line = $this->get('CartLine');
			
			
			JRequest::setVar('event_id',$line->event_id,'post');
			JRequest::setVar('ticket_type',$line->ticket_type,'post');
			JRequest::setVar('price',$line->ticket_price,'post');
			$response = $this->get('Cart');
			
			
			if ($response){
				$calc_res = array();
				$calc_res['Total']=0;
				$calc_res['line_sum']=0;
				
				foreach($response as $item){
					if ($item->cart_line_id==$id){
						$calc_res['line_sum'] = $item->ticket_price * $item->quantity;		
					}
					$calc_res['Total'] += $item->ticket_price * $item->quantity;
				}
				 
				
				$calc_res['line_sum'] = number_format($calc_res['line_sum'],2);
				$calc_res['Total'] = number_format($calc_res['Total'],2);
				
				$calc_res['commission'] = $this->getCommission($response);
				
				$response = $calc_res;
				
			}
			
		}
		
		$response = json_encode($response);
		echo $response ;
	}
	
	function clean(){
		$this->get('CleanCart');
		die;
	}
	
	private function getCommission($cart){
		if(file_exists(JPATH_SITE.'/components/com_jlticketcenter/helpers/helper_pro.php')){
			JLoader::import('components.com_jlticketcenter.helpers.helper_pro',JPATH_SITE);
			$commission = JLTicketCenterProHelper::getComissionRate($cart);
			return $commission;
		}else{
			return 0;
		}
	} 
	
	private function runCron(){
		
		JLoader::import('components.com_jlticketcenter.models.cart_cron',JPATH_ROOT);
		//jimport('',JPATH_SITE);
		$model = JModelLegacy::getInstance('CartCorn', 'JLTicketCenterModel');
		
		$model->runCron();
		die;
			
	}

	private function runCronOrder(){
		
		JLoader::import('components.com_jlticketcenter.models.cart_cron',JPATH_ROOT);
		$model = JModelLegacy::getInstance('CartCorn', 'JLTicketCenterModel');
		
		$model->clearPendingOrders();
		die;
	}
	
	
	
        private function eSelectPaymentRedirect(){

            JLoader::import('components.com_jlticketcenter.models.payment.eselect',JPATH_ADMINISTRATOR);
            
            $eSelect = new JLTicketCenterESelect;
            $eSelect->eSelectConfig();
            $url  = '';
            
            if ($eSelect->payment_params->test_mode == 1){
                $url = $eSelect->payment_params->test_url;
            }else{
                $url = $eSelect->payment_params->production_url;
            }
            
            if ($url == ''){
                $url = $eSelect->eSelect_URL;
            }
            
            echo 'Please wait... <br /> 
                <form method="post" action="'.$url.'" id="eselect" >
                    <input type="hidden" name="ps_store_id" value="'.$eSelect->payment_params->ps_store_id.'">
                    <input type="hidden" name="hpp_key" value="'.$eSelect->payment_params->hpp_key.'">
                    <input type="hidden" name="charge_total" value="'.JRequest::getVar('total').'">
                    
                    <input type="hidden" name="order_id" value="'.$eSelect->payment_params->order_prefix.JRequest::getVar('order_id').'">
                    
                    
                   <INPUT TYPE="SUBMIT" NAME="SUBMIT" VALUE="Please wait..." style="visibility: hidden;">
            </form>';

            
            echo '<script>
                    document.forms["eselect"].submit();
            </script>';
            

            exit();
            
        }//EOF eSelectPaymentRedirect
        
        private function eSelectPaymentProcess(){
            
            $myFile = $_SERVER['DOCUMENT_ROOT']."/testFile2.txt";
            
            $fh = fopen($myFile, 'w') or die("can't open file");
            
            $stringData = "test\n";
            fwrite($fh, $stringData);
            
            foreach ($_POST as $k=>$v){
                $stringData .= "$k=>$v\n";
            }
            
            fwrite($fh, $stringData);
                fclose($fh);
                
            
            JLoader::import('components.com_jlticketcenter.models.payment.eselect',JPATH_ADMINISTRATOR);
            
            $eSelect = new JLTicketCenterESelect;
            $eSelect->post_payment_proccess();
        
            
        }//EOF eSelectPaymentProcess
	
} // EOC VIEW