<?php

						/*************************************\
						
						    Handshakes Professional (3.0)
						            by DZOIC LTD
						       Copyright(c) 2005-2006
						        http://www.dzoic.com
						
						\*************************************/

						///////////////////////////////////////
							
						admin_validate_section("getpaid",3);
						$action = $GLOBALS["Get"]->val("action");
								
							switch($action){
										
								case 'history':
									sales_history();
									break;
									
								case 'refund':
									refund_sale();
									break;
									
								case 'clear_history':
									clear_history();
									break;
									
								case 'pending':
									pending_sales();
									break;
									
								case 'complete':
									complete_payment();
									break;
									
								case 'delete':
									delete_sale();
									break;
									
								case 'delete_pending':
									delete_pending();
									break;
									
							}//switch
							
						///////////////////////////////////////
						
function sales_history(){
	
	admin_validate();
	
	$search_link = "index.php?section=sales&action=history";
	
	$form = array (
		"search_for",
		"search_text",
		"sort_by",
		"sort_order",
		"amount_from",
		"amount_to",
		"refunded",
		"order_type"
	);
	
	foreach($form as $el){		
		$values[$el] = $GLOBALS["Get"]->val($el);
		if($el == "refunded" && empty($values[$el]))
			$values[$el] = "all";
		$search_link.= "&".$el."=".urlencode(stripslashes(html_entity_decode($values[$el])));
	}//foreach
	
	$date_from = $GLOBALS["Get"]->val("date_from");
	if(!empty($date_from[0]) && !empty($date_from[1]) && !empty($date_from[2]))
	$values["date_from"] = mktime(0,0,0,$date_from[1],$date_from[0],$date_from[2]);
	
	$date_to = $GLOBALS["Get"]->val("date_to");
	if(!empty($date_to[0]) && !empty($date_to[1]) && !empty($date_to[2]))
	$values["date_to"] = mktime(0,0,0,$date_to[1],$date_to[0],$date_to[2]);
	
	$search_link.= "&date_from=".$values["date_from"]."&date_to=".$values["date_to"];
	
	$sql_query_total = "SELECT COUNT(orders.ord_id) ";
	$sql_query_result = "SELECT * ";
	
	$sql_query = "FROM orders,members
				  WHERE orders.mem_id=members.mem_id";
	
	if(!empty($values["search_for"]) && hs_strlen($values["search_text"]) > 0){
		$values["search_text"] = admin_replace_search_string($values["search_text"]);
		switch($values["search_for"]){
			case 'ord_id':
				settype($values["search_text"],"integer");
				$sql_query.= " AND orders.ord_id IN (".$values["search_text"].")";
				break;
			case 'mem_id':
				settype($values["search_text"],"integer");
				$sql_query.= " AND members.mem_id IN (".$values["search_text"].")";
				break;
			case 'email':
				$emails = explode(",",$values["search_text"]);
				$sql_query.= " AND ( ";
				foreach($emails as $email){
					$sql_query.= " members.email LIKE '".$email."' OR ";
				}//foreach
				$sql_query = rtrim($sql_query,' OR ');
				$sql_query.= " ) ";
				break;
			case 'fname':
				$fnames = explode(",",$values["search_text"]);
				$sql_query.= " AND ( ";
				foreach($fnames as $fname){
					$sql_query.= " members.fname LIKE '".$fname."' OR ";
				}//foreach
				$sql_query = rtrim($sql_query,' OR ');
				$sql_query.= " ) ";
				break;
			case 'lname':
				$lnames = explode(",",$values["search_text"]);
				$sql_query.= " AND ( ";
				foreach($lnames as $lname){
					$sql_query.= " members.lname LIKE '".$lname."' OR ";
				}//foreach
				$sql_query = rtrim($sql_query,' OR ');
				$sql_query.= " ) ";
				break;				
		}//switch
	}//if
	
	if($values["order_type"]){
		$sql_query.= " AND order_type='".$values["order_type"]."'";
	}//if
	
	if($values["amount_from"]){
		$sql_query.= " AND amount >= ".$values["amount_from"];
	}//if
	
	if($values["amount_to"]){
		$sql_query.= " AND amount <= ".$values["amount_to"];
	}//if
	
	if($values["date_from"]){
		$sql_query.= " AND date >= ".$values["date_from"];
	}//if
	
	if($values["date_to"]){
		$sql_query.= " AND date <= ".$values["date_to"];
	}//if
	
	if($values["refunded"] == "successful")
		$sql_query.= " AND refunded='0'";
	elseif($values["refunded"] == "refunded")
		$sql_query.= " AND refunded='1'";
	
	if(!empty($values["sort_by"])){
		switch($values["sort_by"]){
			case 'ord_id':
				$sql_query.= " ORDER BY orders.ord_id ";
				break;
			case 'mem_id':
				$sql_query.= " ORDER BY orders.mem_id ";
				break;
			case 'email':
				$sql_query.= " ORDER BY members.email ";
				break;
			case 'fname':
				$sql_query.= " ORDER BY members.fname ";
				break;
			case 'lname':
				$sql_query.= " ORDER BY members.lname ";
				break;
			case 'amount':
				$sql_query.= " ORDER BY orders.amount ";
				break;
			case 'date':
				$sql_query.= " ORDER BY orders.date ";
				break;
		}//switch
		if(!empty($values["sort_order"])){
			$sql_query.= $values["sort_order"];
		}//if
	}//if
	else
		$sql_query.= " ORDER BY orders.date DESC ";
	
	$start = $p*30;
	
	$total = $GLOBALS["DB"]->single($sql_query_total.$sql_query);	
	
	$data["link"] = $search_link."&p=".$p;
	
	$sql_query.= " LIMIT ".$start.",30";
	$result = $GLOBALS["DB"]->result($sql_query_result.$sql_query);
	
	$sales = array();
	foreach($result as $res){
		$sale["id"]          = $res["ord_id"];
		$sale["mem_id"]      = $res["mem_id"];
		$sale["buyer"]       = $GLOBALS["Network"]->card($res["mem_id"]);
		$sale["psystem"]     = $res["psystem"];
		$sale["description"] = sale_description($res["name"],$res["order_type"],$res["order_object_id"]);;
		$sale["amount"]      = extract_option("currencies",$res["currency"]).$res["amount"];
		$sale["date"]        = extract_time($res["date"],1);
		$sale["refund"]      = "index.php?section=sales&action=refund&ord_id=".$res["ord_id"]."&ret_link=".urlencode($data["link"]);
		$sale["refunded"]    = $res["refunded"];
		$sales[] = $sale;
	}//foreach
	
	$data["sales"] = $sales;
	$data["section"] = "getpaid";
	$data["subsection"] = 3;
	$data["title_id"] = "45";	
	$data["pages"] = handle_multipages($p,$total,$search_link,30);	
	$data["values"] = $values;
	
	$layout[0] = "sales_history";
	
	admin_output($layout,$data);
	
}//sales_history

function sale_description($name,$type,$id){
	
	switch($type){
		case 'membership':
			return $name;
		case 'blogs':
			return "<a target='_blank' href='".build_link("blog",$id)."'>".$name."</a>";
		case 'classifieds':
			return "<a target='_blank' href='".build_link("classified_ad",$id)."'>".$name."</a>";
		case 'events':
			return "<a target='_blank' href='".build_link("event",$id)."'>".$name."</a>";
		case 'clubs':
			return "<a target='_blank' href='".build_link("club",$id)."'>".$name."</a>";
	}//switch
	
}//sale_description

function refund_sale(){
	
	admin_validate();
	
	$ord_id = $GLOBALS["Get"]->type_val("ord_id","integer");
	
	$ret_link = $GLOBALS["Get"]->val("ret_link");
	$ret_link = stripslashes(html_entity_decode($ret_link));
	
	$sql_query = "SELECT * FROM orders WHERE ord_id=".$ord_id;
	$order = $GLOBALS["DB"]->row($sql_query);
	
	switch($order["order_type"]){
		case 'membership':
			$sql_query = "UPDATE members SET membership=0,access_end=0
						  WHERE mem_id=".$order["mem_id"];
			$GLOBALS["DB"]->execute($sql_query);
			break;
		case 'blogs':
			$sql_query = "UPDATE blogs SET featured='0',featured_end=0
						  WHERE blg_id=".$order["order_object_id"];
			$GLOBALS["DB"]->execute($sql_query);
			break;
		case 'classifieds':
			$sql_query = "UPDATE classifieds SET featured='0',featured_end=0
						  WHERE cls_id=".$order["order_object_id"];
			$GLOBALS["DB"]->execute($sql_query);
			break;
		case 'events':
			$sql_query = "UPDATE events SET featured='0',featured_end=0
						  WHERE evn_id=".$order["order_object_id"];
			$GLOBALS["DB"]->execute($sql_query);
			break;
		case 'clubs':
			$sql_query = "UPDATE clubs SET featured='0',featured_end=0
						  WHERE clb_id=".$order["order_object_id"];
			$GLOBALS["DB"]->execute($sql_query);
			break;
	}//switch
	
	$sql_query = "DELETE FROM payment_gateway WHERE order_type='".$order["order_type"]."' AND mem_id=".$order["mem_id"]." AND order_object_id=".$order["order_object_id"];
	$GLOBALS["DB"]->execute($sql_query);
	
	$sql_query = "UPDATE orders SET refunded='1' WHERE ord_id=".$ord_id;
	$GLOBALS["DB"]->execute($sql_query);
	
	load_page($ret_link);
	
}//refund_sale

function clear_history(){
	
	admin_validate();
	
	$sql_query = "DELETE FROM orders WHERE ord_id > 0";
	$GLOBALS["DB"]->execute($sql_query);
	
	load_page("index.php?section=sales&action=history");
	
}//clear_history

function pending_sales(){
	
	admin_validate();
	
	$search_link = "index.php?section=sales&action=pending";
	
	$form = array (
		"search_for",
		"search_text",
		"sort_by",
		"sort_order",
		"order_type",
		"future"
	);
	
	foreach($form as $el){		
		$values[$el] = $GLOBALS["Get"]->val($el);
		$search_link.= "&".$el."=".urlencode(stripslashes(html_entity_decode($values[$el])));
	}//foreach
	
	$date_from = $GLOBALS["Get"]->val("date_from");
	if(!empty($date_from[0]) && !empty($date_from[1]) && !empty($date_from[2]))
	$values["date_from"] = mktime(0,0,0,$date_from[1],$date_from[0],$date_from[2]);
	
	$date_to = $GLOBALS["Get"]->val("date_to");
	if(!empty($date_to[0]) && !empty($date_to[1]) && !empty($date_to[2]))
	$values["date_to"] = mktime(0,0,0,$date_to[1],$date_to[0],$date_to[2]);
	
	$search_link.= "&date_from=".$values["date_from"]."&date_to=".$values["date_to"];
	
	$sql_query_total = "SELECT COUNT(pg.order_id) ";
	$sql_query_result = "SELECT * ";
	
	$sql_query = "FROM payment_gateway AS pg,members
				  WHERE pg.mem_id=members.mem_id";
	
	if(!empty($values["search_for"]) && hs_strlen($values["search_text"]) > 0){
		$values["search_text"] = admin_replace_search_string($values["search_text"]);
		switch($values["search_for"]){
			case 'mem_id':
				settype($values["search_text"],"integer");
				$sql_query.= " AND members.mem_id IN (".$values["search_text"].")";
				break;
			case 'email':
				$emails = explode(",",$values["search_text"]);
				$sql_query.= " AND ( ";
				foreach($emails as $email){
					$sql_query.= " members.email LIKE '".$email."' OR ";
				}//foreach
				$sql_query = rtrim($sql_query,' OR ');
				$sql_query.= " ) ";
				break;
			case 'fname':
				$fnames = explode(",",$values["search_text"]);
				$sql_query.= " AND ( ";
				foreach($fnames as $fname){
					$sql_query.= " members.fname LIKE '".$fname."' OR ";
				}//foreach
				$sql_query = rtrim($sql_query,' OR ');
				$sql_query.= " ) ";
				break;
			case 'lname':
				$lnames = explode(",",$values["search_text"]);
				$sql_query.= " AND ( ";
				foreach($lnames as $lname){
					$sql_query.= " members.lname LIKE '".$lname."' OR ";
				}//foreach
				$sql_query = rtrim($sql_query,' OR ');
				$sql_query.= " ) ";
				break;				
		}//switch
	}//if
	
	if(!$values["future"]){
		$sql_query.= " AND pg.next_payment < ".time();
	}//if
	
	if($values["order_type"]){
		$sql_query.= " AND order_type='".$values["order_type"]."'";
	}//if
	
	if($values["amount_from"]){
		$sql_query.= " AND amount >= ".$values["amount_from"];
	}//if
	
	if($values["amount_to"]){
		$sql_query.= " AND amount <= ".$values["amount_to"];
	}//if
	
	if($values["date_from"]){
		$sql_query.= " AND next_payment >= ".$values["date_from"];
	}//if
	
	if($values["date_to"]){
		$sql_query.= " AND next_payment <= ".$values["date_to"];
	}//if
	
	if(!empty($values["sort_by"])){
		switch($values["sort_by"]){
			case 'mem_id':
				$sql_query.= " ORDER BY orders.mem_id ";
				break;
			case 'email':
				$sql_query.= " ORDER BY members.email ";
				break;
			case 'fname':
				$sql_query.= " ORDER BY members.fname ";
				break;
			case 'lname':
				$sql_query.= " ORDER BY members.lname ";
				break;
			case 'amount':
				$sql_query.= " ORDER BY pg.amount ";
				break;
			case 'date':
				$sql_query.= " ORDER BY pg.next_payment ";
				break;
		}//switch
		if(!empty($values["sort_order"])){
			$sql_query.= $values["sort_order"];
		}//if
	}//if
	else
		$sql_query.= " ORDER BY pg.next_payment DESC ";
	
	$start = $p*30;
	
	$total = $GLOBALS["DB"]->single($sql_query_total.$sql_query);	
	
	$data["link"] = $search_link."&p=".$p;
	
	$sql_query.= " LIMIT ".$start.",30";
	$result = $GLOBALS["DB"]->result($sql_query_result.$sql_query);
	
	$sales = array();
	foreach($result as $res){
		$sale["id"]          = $res["order_id"];
		$sale["mem_id"]      = $res["mem_id"];
		$sale["buyer"]       = $GLOBALS["Network"]->card($res["mem_id"]);
		$sale["description"] = $res["order_name"];
		$sale["amount"]      = extract_option("currencies",$res["currency"]).$res["amount"];
		$sale["due_date"]    = extract_date($res["next_payment"]);
		$sale["last_paid"]   = ($res["last_paid"]) ? extract_date($res["last_paid"]) : "Never";
		$sale["complete"]    = "index.php?section=sales&action=complete&order_id=".$res["order_id"]."&ret_link=".urlencode($data["link"]);
		$sale["delete"]      = "index.php?section=sales&action=delete&order_id=".$res["order_id"]."&ret_link=".urlencode($data["link"]);
		$sales[] = $sale;
	}//foreach
	
	$data["sales"] = $sales;
	$data["section"] = "getpaid";
	$data["subsection"] = 4;
	$data["title_id"] = "45";	
	$data["pages"] = handle_multipages($p,$total,$search_link,30);	
	$data["values"] = $values;
	
	$layout[0] = "pending_sales";
	
	admin_output($layout,$data);
	
}//pending_sales

function complete_payment(){
	
	admin_validate();
	
	$order_id = $GLOBALS["Get"]->type_val("order_id","integer");
	
	$sql_query = "SELECT next_payment FROM payment_gateway WHERE order_id=".$order_id;
	$now = $GLOBALS["DB"]->single($sql_query);
	
	$sql_query = "UPDATE payment_gateway SET `status`='paid',`now`=".$now." WHERE order_id=".$order_id;
	$GLOBALS["DB"]->execute($sql_query);
	
	$redirect = file(URL."/index.php?handler=getpaid&action=payment_complete&order_id=".$order_id);
	
	$ret_link = $GLOBALS["Get"]->val("ret_link");
	$ret_link = stripslashes(html_entity_decode($ret_link));
	
	load_page($ret_link);
	
}//complete_payment

function delete_sale(){
	
	admin_validate();
	
	$order_id = $GLOBALS["Get"]->type_val("order_id","integer");
	
	$sql_query = "DELETE FROM payment_gateway WHERE order_id=".$order_id;
	$GLOBALS["DB"]->execute($sql_query);
	
	$ret_link = $GLOBALS["Get"]->val("ret_link");
	$ret_link = stripslashes(html_entity_decode($ret_link));
	
	load_page($ret_link);
	
}//delete_sale

function delete_pending(){
	
	admin_validate();
	
	$sql_query = "DELETE FROM payment_gateway WHERE order_id > 0";
	$GLOBALS["DB"]->execute($sql_query);
	
	load_page("index.php?section=sales&action=pending");
	
}//delete_pending
						
?>