<?php
/*
	osCommerce, Open Source E-Commerce Solutions 
	http://www.oscommerce.com 
	
	Copyright (c) 2003 osCommerce 
	
	osCommRes, Services Oline 
	http://www.oscommres.com 
	Copyright (c) 2005 osCommRes 
	
	Freeway eCommerce 
	http://www.zac-ware.com/freeway 
	Copyright (c) 2007 ZacWare
	
	Released under the GNU General Public License
*/


// Set flag that this is a parent file
	define( '_FEXEC', 1 );
	


  require('includes/application_top.php');
  require(DIR_WS_CLASSES . 'order.php');
	
  if (!tep_session_is_registered('customer_id')) {
    tep_redirect(tep_href_link(FILENAME_LOGIN, '', 'SSL'));
  }
  
    require(DIR_WS_LANGUAGES . $language . '/' . FILENAME_SUPPORT_PACKS_LIST);  

  
  $command=(isset($HTTP_GET_VARS['command'])?tep_db_prepare_input($HTTP_GET_VARS['command']):'');
  $pid=(isset($HTTP_GET_VARS['pid'])?(int)$HTTP_GET_VARS['pid']:'');
  $oid=(isset($HTTP_GET_VARS['oid'])?(int)$HTTP_GET_VARS['oid']:'');
  $sp_id=(isset($HTTP_GET_VARS['sp_id'])?(int)$HTTP_GET_VARS['sp_id']:'');
  $lid=(isset($HTTP_GET_VARS['lid'])?(int)$HTTP_GET_VARS['lid']:'');
//echo $HTTP_GET_VARS['command'];
 
  if($command!=''){
  	echo tep_output_string($command) . "{^{}^}";
  	switch ($command) {
		case 'get_software_pack':
			echo  tep_output_string($pid)  . '_' . tep_output_string($oid) . '{^{}^}';
			echo display_software_pack($pid,$oid);
		break;
		case 'get_software_license':
			echo tep_output_string($sp_id) . '{^{}^}';
			echo display_license($sp_id);
		break;
		case 'open_license':
			echo tep_output_string($lid) . '{^{}^}';
			echo display_license_edit($lid,false);
		break;
		case 'save_license':
			$error=false;
			$domain_name=(isset($HTTP_POST_VARS['domain_name'])?tep_db_prepare_input($HTTP_POST_VARS['domain_name']):'');
			$customer_name=(isset($HTTP_POST_VARS['customer_name'])?tep_db_prepare_input($HTTP_POST_VARS['customer_name']):'');
			$email_address=(isset($HTTP_POST_VARS['email_address'])?tep_db_prepare_input($HTTP_POST_VARS['email_address']):'');
			if (!tep_validate_email($email_address)) {
			  $error = true;
			}
			if(!$error){
	
				//echo $lid . '{^{}^}';
				$pack_query=tep_db_query("select support_packs_id,activation_time from " . TABLE_SUPPORT_PACKS_LICENSE . " where license_code='" . tep_db_input($lid) . "'");
				$pack_result=tep_db_fetch_array($pack_query);
				//echo $pack_result['support_packs_id'];
				//save_license($lid);			
				$sql_data_array=array(
							'customer_domain'=>tep_db_input($domain_name),
							'customer_name'=>tep_db_input($customer_name),
							'customer_email'=>tep_db_input($email_address),
							'activate'=>'Y'
						);
						
				if(substr($pack_result['activation_time'],0,2)=='00')
					$sql_data_array['activation_time']=date('Y-m-d H:i:s',getServerDate(false));
				tep_db_perform(TABLE_SUPPORT_PACKS_LICENSE,$sql_data_array,"update"," license_code='" . $lid . "'");
				tep_send_support_packs_code_activation_email('LCA',$lid);
				echo $pack_result['support_packs_id'] . '{^{}^}';
				echo display_license($pack_result['support_packs_id']);
			} else {
				echo 'error' . '{^{}^}';
				echo tep_output_string($lid) . '{^{}^}';
				echo display_license_edit($lid,true);
			}
		break;
	}
	exit;
  }
  //   echo date('Y-m-d H:i:s',getServerDate(false));
  $breadcrumb->add(NAVBAR_TITLE, tep_href_link(FILENAME_SUPPORT_PACKS_LIST, '', 'SSL'));
  $content = CONTENT_SUPPORT_PACKS_LIST;
  $query="select opsp.*,pd.products_id,pd.products_name,date_format(o.date_purchased,'%Y-%m-%d') as date_purchased,opsp.orders_id from " . TABLE_ORDERS . " o, " . TABLE_ORDERS_PRODUCTS . " op, " . TABLE_ORDERS_PRODUCTS_SUPPORT_PACKS . " opsp, " .TABLE_PRODUCTS_DESCRIPTION . " pd, " . TABLE_PRODUCTS . " p where p.products_id=opsp.products_id and opsp.orders_id=o.orders_id and opsp.products_id=p.products_id and pd.products_id=p.products_id and o.orders_id=op.orders_id and op.products_type='P' and op.support_packs_type='S' and pd.language_id='" . tep_db_input($languages_id) . "' and o.customers_id='" . tep_db_input($customer_id) . "' group by pd.products_id,opsp.orders_id order by o.date_purchased,pd.products_name";

  $listing_split = new splitPageResults($query, MAX_DISPLAY_SEARCH_RESULTS,'opsp.orders_id');
  $support_pack_query=tep_db_query($listing_split->sql_query);
  
  require(DIR_WS_TEMPLATES . TEMPLATE_NAME . '/' . TEMPLATENAME_MAIN_PAGE);
  require(DIR_WS_INCLUDES . 'application_bottom.php');
  
  
  function display_software_pack($id,$oid){
  	$result='';
  	$software_pack_query=tep_db_query("select opsp.subscription_name,opsp.support_minutes,opsp.no_users,opsp.support_packs_id,opsp.subscription_id from " . TABLE_ORDERS_PRODUCTS_SUPPORT_PACKS . " opsp where  opsp.products_id='" . tep_db_input($id) . "' and opsp.orders_id='" . tep_db_input($oid) . "'");
	if(tep_db_num_rows($software_pack_query)>0){
		$result='<table cellpadding="0" cellspacing="0" border="0" width="98%">';
		$result.='<tr><td colspan="4">'.tep_draw_separator('pixel_trans.gif','5','5').'</td></tr><tr><td class="main" width="150"><b>' . HEADING_SUBSCRIPTION . '</b></td><td class="main"  align="right" width="100"><b>' . HEADING_SUPPORT_MINITES . '</b></td><td class="main" align="right" width="70"><b>' . HEADING_NO_PACK . '</b></td><td class="main" align="right" width="65"><b>' . HEADING_ACTIVATED . '</b></td></tr>';
		while($software_pack_result=tep_db_fetch_array($software_pack_query)){
		
			$user_count_query=tep_db_query("select count(*) as used_count from " . TABLE_SUPPORT_PACKS_LICENSE . " where support_packs_id='" . tep_db_input($software_pack_result['support_packs_id']) . "' and activate='Y' group by support_packs_id");
			$user_count_result=tep_db_fetch_array($user_count_query);
				$result.='<tr class="moduleRow" height="25" id="packs_' . $software_pack_result['support_packs_id'] . '" style="cursor:hand; cursor:pointer" onmouseout="rowOutEffect(this);" onmouseover="rowOverEffect(this);" onClick="javascript:do_fetch_license(\'get_software_license\',\'' . $software_pack_result['support_packs_id']. '\');">' . 
							'<td class="main" width="150">' . $software_pack_result['subscription_name'] . '</td>'.
							'<td class="main" align="right" width="100">' . $software_pack_result['support_minutes'] . '</td>'. 
							'<td class="main" align="right" width="70">' . $software_pack_result['no_users'] . '</td>'. 
							'<td class="main" align="right" width="65">' . (($user_count_result['used_count'])?$user_count_result['used_count']:'0') . '</td>'.
						'</tr>
						<tr><td class="main" colspan="4"><div id="sp_license_' . $software_pack_result['support_packs_id'] .'" style="display:none; padding-left:10px; " ></div></td></tr>';	
		}
		$result.='<tr><td colspan="4">'.tep_draw_separator('pixel_trans.gif','5','5').'</td></tr></table>';
	}
	return $result;
  }
  
  function display_license($sp_id){
  	$result='';
	$active=false;
  	$license_query=tep_db_query("select * from " . TABLE_SUPPORT_PACKS_LICENSE . " where support_packs_id='" . tep_db_input($sp_id) . "' order by license_code" );
			$support_minute_query=tep_db_query("select support_minutes from " . TABLE_ORDERS_PRODUCTS_SUPPORT_PACKS . " where support_packs_id='" . tep_db_input($sp_id) ."'");
			$support_minutes_result=tep_db_fetch_array($support_minute_query);
	if(tep_db_num_rows($license_query)>0){
		$result='<table cellpadding="3" cellspacing="0" border="0" width="98%"><tr><td valign="middle"><table cellpadding="0" cellspacing="0" border="0" width="100%" style="border: solid 1px;padding-left:5px; border-color:#D3D1D1">';
		$result.='<tr><td colspan="6">'.tep_draw_separator('pixel_trans.gif','5','5').'</td></tr><tr><td width="9">' .tep_draw_separator('pixel_trans.gif','9','9'). '</td><td class="main" width="50" align="left"><b>' . HEADING_LICENSE_CODE . '</b></td><td class="main" width="150"><b>' . HEADING_DOMAIN_NAME . '</b></td><td class="main" width="150"><b>' . HEADING_CUSTOMERS_EMAIL . '</b></td><td class="main" width="100" align="left"><b>' .HEADING_DATE_ADDED . '</b></td><td class="main" width="70" align="right"><b>' . HEADING_REMAINING_TIME . '</b></td></tr></table></td></tr>';//<td class="main"><b>' . HEADING_ACTIVATE . '</b></td></tr>' ;
		$result.='<tr><td><div style="height:200px; overflow:auto"><table cellpadding="2" cellspacing="0" width="100%" border="0" style="padding-left:5px">';
		while($license_result=tep_db_fetch_array($license_query)){
			//if($license_result['customer_domain']!='')
			//	$result.='<tr class="moduleRow" id="spl_' . $license_result['license_code'] . '" onmouseout="rowOutEffect(this);" onmouseover="rowOverEffect(this);" onClick="javascript:do_fetch_license(\'open_license\',\''.$license_result['license_code'].'\');">';
			// if($license_result['customer_domain']!='') {
			if($license_result['activate']=='Y') {
				$result.='<tr  class="moduleRow" id="spl_' . $license_result['license_code'] . '" >';
				$result.='<td id="active_'. $license_result['license_code']. '" align="center" width="9">' . tep_template_image('icon_active.gif') . '</td>';
			 } else if($license_result['activate']=='N' && !$active) {//($license_result['customer_domain']=='' && !$active)
				$result.='<tr  class="moduleRow" id="spl_' . $license_result['license_code'] . '" style="cursor:hand; cursor:pointer" onmouseout="rowOutEffect(this);" onmouseover="rowOverEffect(this);" >';
				$result.='<td id="active_'. $license_result['license_code']. '" align="center" onClick="javascript:do_fetch_license(\'open_license\',\''.$license_result['license_code'].'\');"  width="9">' . tep_template_image('icon_inactive.gif') . '</td>';
				$active=true;
			} else {
				$result.='<tr  class="moduleRow" id="spl_' . $license_result['license_code'] . '" >';
				$result.='<td id="active_'. $license_result['license_code']. '" align="center"  width="9">' . tep_template_image('icon_inactive.gif') . '</td>';
			}	
			$time_query=tep_db_query("select sum(minutes_used) used_time from " . TABLE_SUPPORT_PACKS_ORDER_HISTORY . " where support_pack_id='" . tep_db_input($sp_id) . "' and license_code='" . tep_db_input($license_result['license_code']) . "' group by license_code ");
			$time_result=tep_db_fetch_array($time_query);
			$remaining_time=$support_minutes_result['support_minutes'] - $time_result['used_time'];
			
			if($remaining_time<=0)
				$remaining_time='Finished!';
			$atime=split(' ',$license_result['activation_time']);
				
			$result.='<td class="main" width="50" align="center">' . sprintf("%05d",$license_result['license_code']) . '</td>'.
					'<td class="main" width="150">' . $license_result['customer_domain'] . '</td><td class="main" width="150">' . $license_result['customer_email'] . '</td><td class="main" width="70" align="left">' . format_date($atime[0]) .' ' . (($atime[1]!='00:00:00')?$atime[1]:'')  . '</td><td width="100" align="right" class="main">' . (($license_result['activate']=='Y')?$remaining_time:'') . '</td>';
			'</tr>';
			$result.='<tr><td colspan="6" class="main"><div id="license_edit_' . $license_result['license_code'] . '" style="display:none;padding-left:50px;" align="center"></div></td></tr>'; 
		}
		$result.='</table></div></td></tr>';
		$result.='<tr><td colspan="6">'.tep_draw_separator('pixel_trans.gif','5','5').'</td></tr></table>';
	}
	return $result;
  }
  
  function display_license_edit($lid,$error){
  	global $HTTP_POST_VARS;
  	$result='';
/*  	$license_query=tep_db_query("select * from " . TABLE_SUPPORT_PACKS_LICENSE . " where license_code='" . $lid . "'");
	$license_result=tep_db_fetch_array($license_query);
*/	$result.=tep_draw_form('license',FILENAME_SUPPORT_PACKS_LIST,'post','onSubmit="javascript:return validateForm();"') . tep_draw_hidden_field('license_id',$lid);
  	$result.='<table cellpadding="0" cellspacing="0" border="0" width="100%"><tr><td colspan="2">'.tep_draw_separator('pixel_trans.gif','5','5').'</td></tr><tr><td width="80%"><table cellpadding="2" cellspacing="0" border="0" width="100%">';
	$result.='<tr><td class="main" width="100">' . TITLE_DOMAIN_NAME . '</td><td class="main" width="150">'. tep_draw_input_field('domain_name','',' size="30"') .' <span style="color:#FF0000">(http(s)://)</span></td></tr>';
	$result.='<tr><td class="main" width="100">' .TITLE_CUSTOMER_NAME . '</td><td class="main" width="150">' . tep_draw_input_field('customer_name','',' size="30"') . '</td></tr>';
	$result.='<tr><td class="main" width="100" valign="top">' . TITLE_CUSTOMER_EMAIL . '</td><td class="main" width="150">'. tep_draw_input_field('email_address','',' size="30"') . (($error)? '<span class="inputRequirement">' . ENTRY_EMAIL_ADDRESS_CHECK_ERROR . '</span>':'') .'</td></tr>';
	$result.='</table></td>';
	$result.='<td align="left"><table cellpadding="0" cellspacing="0" border="0" width="100%">';
	$result.='<tr><td align="left">'. tep_template_image_submit('button_update.gif',IMAGE_ADD) . '</td></tr>';
	$result.='</table></td></tr><tr><td colspan="2">'.tep_draw_separator('pixel_trans.gif','5','5').'</td></tr></table></form>';
	return $result;	
  }
  
  function save_license($lid){
  	global $HTTP_POST_VARS;
	$sql_data_array=array(
						'customer_domain'=>tep_db_prepare_input($HTTP_POST_VARS['domain_name']),
						'customer_name'=>tep_db_prepare_input($HTTP_POST_VARS['customer_name']),
						'customer_email'=>tep_db_prepare_input($HTTP_POST_VARS['email_address']),
						'activation_time'=>tep_db_prepare_input($HTTP_POST_VARS['activation_time']),
						'activate'=>'Y'
					);
		tep_db_perform(TABLE_SUPPORT_PACKS_LICENSE,$sql_data_array,"update"," license_code='" . $lid . "'");
  }
  
?>