<?
define("PARTNER_AUTHORIZATION",1);
include("../../../includes/common.php");

/*PAGE TITLE*/
$page_title = "List of promotional codes";

$header = "List of promotional codes";

$table = "promo_codes";
/* pager start */
$params['tables'] = $table;
$params['nocheckfield']=true;
$params['s_prefix'] = $table;
$params['query'] = "select count(*) from $table where partner_id=$PARTNER_ID";

$pager = GetPagerParams($params);
/* pager end */


$table_headers = Array(
	"itter"       =>Array("#","width=20 align=center"),
	"code"=>Array("<a href='".$pager['new_sort']."$table".".code' title='Sort'>Promo code</a>","align=center width=100"),
	"discount"=>Array("Discount","align=center width=100"),
	"orders"=>Array("Orders amount","align=center"),	
	"amount"=>Array("Partner amount","align=center"),	
	"regdate"=>Array("<a href='".$pager['new_sort']."$table".".regdate' title='Sort'>Issue date</a>","align=center"),	
	"expdate"=>Array("<a href='".$pager['new_sort']."$table".".expdate' title='Sort'>Expires</a>","align=center width=40"),	
	"status"=>Array("<a href='".$pager['new_sort']."$table".".status' title='Sort'>Status</a>","align=center width=60")
	);
if ($group_funcs_options) {
	$table_headers["group_funcs"]=Array("<select name=act_sel onChange='SubmitForm();'>
									$group_funcs_options
									</select>");
}

$table_items = Array();

$query = "
  select 
  $table.*,
  promo_vendors.name as vendor_name,
  tariffs.name as tariff_name
  from $table 
  inner join promo_vendors on $table.vendor_id=promo_vendors.id
  left join tariffs on $table.tariff_id=tariffs.id
  where $table.partner_id = $PARTNER_ID ORDER BY ".$pager['sort_by']." ".$pager['sort_ord']
  ." LIMIT ".$SETTINGS['admin_items_per_page']." OFFSET ".($pager['start_item']-1);

$res = mysql_query($query);

$itter =$pager['start_item'];
$i=0;

$def_currency=mysql_fetch_object(mysql_query("select * from currencies where is_default=true"));
while ($row = mysql_fetch_object($res)){
	switch($row->status){
		case 0:
			$s= $ADMIN_ICONS[status_off];
		break;
		case 1:
			$s= $ADMIN_ICONS[status_on];
		break;
	}
	
	$tmp_row = mysql_fetch_row(mysql_query("select count(*), sum(partner_amount) from orders where promo_code_id = $row->id and partner_id=$PARTNER_ID and status=1"));
	
	$orders = $tmp_row[0]+0;
	$amount = $tmp_row[1]+0;

	
	$table_items[$i++] = Array(
		"id"=>Array($row->id,"align=center"),
		"itter"=>Array($itter,"align=center"),
		"code"=>Array($row->code,"align=center"),
		"orders"=>Array($orders,"align=center"),
		"amount"=>Array($PARTNER_DATA['currency_symbol'].$amount,"align=center"),
		"discount"=>Array("<nobr>".($row->discount_perc*100)." %<br>$def_currency->symbol. ".$row->discount_fix,"align=center"),
		"regdate"=>Array(strftime($DATE_FORMAT,$row->regdate),"align=center"),
		"expdate"=>Array($row->expdate>0?strftime($DATE_FORMAT,$row->expdate):"N/A","align=center"),
		"status"=>Array($s,"align=center"));
	$itter++;
}

$smarty->assign('page_title',"List of promotional codes");

$smarty->assign('error',$error);

/* table template */
$smarty->assign('pager',$pager);
$smarty->assign('table_headers',$table_headers);
$smarty->assign('table_items',$table_items);
$smarty->assign('table_width',"90%");
$smarty->assign('top_menu_tpl',"partner_promo_menu.tpl");
$smarty->assign('main_tpl',"admin_list_table.tpl");

/* display content */
$smarty->display('partner_index.tpl');
?>