<?
error_reporting(0);
$datecheck=date("Y-m-d H:i:s");
require 'include/databaseinfo.php';
	$sql=mysql_connect($dbhost,$dbuser,$dbpass);
 	if(!$sql) {
		echo "<h1>Unable to Establish Connection to the Server</h1><hr noshade size=2 color=#000000>";
		exit();
	}
	$db_sel=mysql_select_db($dbname,$sql);
	if(!$db_sel) {
		echo "<h1>Unable to Connect to the Database</h1><hr noshade size=2 color=#000000>";
		exit();
	}
$lang_query="select * from language where auto=1";
	$lang_result=mysql_query($lang_query);
	$lang_row=mysql_fetch_array($lang_result);
	$defaultlang =$lang_row['filename'];
require "language/$defaultlang";	
require "language/emails/$defaultlang";	
require 'bidback.php';
$admin="select * from genaralsettings where set_id=1";
	$admin_res=mysql_query($admin);
	$admin_row=mysql_fetch_array($admin_res);
	$site_name=$admin_row[set_value];

 $css2="select options as op from options where auto=1";
   $css_res2=mysql_query($css2);
$css_row2=mysql_fetch_array($css_res2);
$options= $css_row2[op];

        $admin="select * from genaralsettings where set_id=3"; //sitename -> id=1
	    $admin_res=mysql_query($admin);
	    $admin_rec=mysql_fetch_array($admin_res);
	    $mail_from=$admin_rec[set_value];
 
//---------------------  Close Auction ---------------------------------------
$query="SELECT * FROM `auction_details` WHERE (status='Active' or status='Inactive') and (expire_date < '$datecheck')";
$table=mysql_query($query);
if(mysql_num_rows($table)!=0)
{
$date = date("Y-m-d"); 
   while($item_detail_row=mysql_fetch_array($table))
   {
   $del_query="update auction_details set status='Closed' where productid = '$item_detail_row[productid]'";
    mysql_query($del_query);
   $postformat=$item_detail_row['selling_method']; 


   if($postformat=="11")
{
$bid_sql2="select * from site_bids where productid=".$item_detail_row['productid']." order by bidding_amount asc";
   
}
   if($postformat!="11")
{
$bid_sql2="select * from site_bids where productid=".$item_detail_row['productid']." order by bidding_amount desc";
   
}




$bid_res2=mysql_query($bid_sql2);

$bid_row2=mysql_fetch_array($bid_res2);
$user_bid_id=$bid_row2['user_id'];
$user_bid_amt=$bid_row2[bidding_amount];
$cproductid=$item_detail_row[productid];

if($options=="yes"){
bidback($cproductid);
}



    $expire_date =$item_detail_row[expire_date]; 
    $reserve_price=$item_detail_row['reserve_price'];
        $postformat=$item_detail_row['postformat']; 
   $quick_buy_price=$item_detail_row['quick_buy_price'];



   if($postformat=="11")
{
        $item_query="select * from site_bids where productid =$item_detail_row[productid] and user_pos='No' order by bidding_amount ASC ";
   
}
   if($postformat!="11")
{
        $item_query="select * from site_bids where productid =$item_detail_row[productid] and user_pos='No' order by bidding_amount DESC ";
   
}

       $item_tab=mysql_query($item_query);
       $size = mysql_num_rows($item_tab);
	  // if no higer bider process end
          if($size >= 1)
        {

   if($postformat=="11")
{
      $item_query_det="select * from site_bids where productid =$item_detail_row[productid] and user_pos='No' order by bidding_amount ASC ";
    
}
   if($postformat!="11")
{
      $item_query_det="select * from site_bids where productid =$item_detail_row[productid] and user_pos='No' order by bidding_amount DESC ";
    
}
       $item_tab_det=mysql_query($item_query_det);
	   $higher_item_det_row=mysql_fetch_array($item_tab_det);
       $single_amount=$higher_item_det_row['bidding_amount'];
$winning_bid_price="$single_amount";
if($postformat=="3")
{
$single_amount=0;
}
if($postformat=="5")
{
$single_amount=$quick_buy_price;
}


$del_query="update auction_details set  sale_price='$single_amount' where productid = '$item_detail_row[productid]'";
mysql_query($del_query);

$closing_item=$item_detail_row[productid];

$sql2 = "update site_bids set cashback='yes' where productid=".$item_detail_row[productid];
$result2 = mysql_query($sql2);

//echo "<BR>closing_item$closing_item";

$higher_item_row=mysql_fetch_array($item_tab);
			     $higher_user_id=$higher_item_row['user_id'];
                 $higher_quantity=$higher_item_row['quantity'];
                 $higher_bid_id=$higher_item_row['bid_id'];  

//echo "<BR>higher_bid_id$higher_bid_id";
			   $verification=1;

     // ------------------------------------ mail to Buyer ----------------------------------------


                   $buyer_details="select * from member_details where user_id=".$higher_item_row[user_id];
	               $buyer_details_res=mysql_query($buyer_details);
                   $buyer_details_row=mysql_fetch_array($buyer_details_res);
	               $productid=$item_detail_row[productid];
				   $producttitle=$item_detail_row[producttitle];
				   $buyer_name=$buyer_details_row[user_name];
				   $buyer_email_id=$buyer_details_row[email];
			       $message = $bidder_win[mail_message];
				   $subject = $bidder_win[mail_subject];
                           $mail_from=$bidder_win['mail_from'];
			       $message=str_replace("NAME" , $buyer_name , $message);
				   $message=str_replace("<PRODUCTTITLE>" , $item_detail_row[producttitle] , $message);
    			   $message=str_replace("<GET_VIEW_ITEM>" , "$site_name/itechd.php?productid=".$item_detail_row['productid'] , $message);
    			   $message=str_replace("<LOGIN_LINK>" , "$site_name/signin.php", $message);			       
                   $headers  = "MIME-Version: 1.0\n";
			       $headers .= "Content-type: text/html; charset=iso-8859-1\n";
			       $headers .= "From: ". $mail_from."\n";		
			       $mail=mail($buyer_email_id,$subject,$message,$headers);
	   
	 // ------------------------------------ end (mail to Buyer) ----------------------------------------
		$update_query ="update site_bids set user_pos='yes',sale_date='$solddate',quantity=$higher_quantity  where user_id='$higher_user_id' and bidding_amount='$winning_bid_price' and productid='$productid' ";
					mysql_query($update_query);
					$update_query ="update auction_details set quantity=quantity-$higher_quantity,quantity_sold=quantity_sold+$higher_quantity  where productid=$productid";
					mysql_query($update_query); 
	   	 } //  if($size >= 1)
	   else // no bidder  
	    {
		$message=$no_bid_placed['mail_message'];
		$subject=$no_bid_placed['mail_subject'];
		$site_query="select * from genaralsettings where set_id=1";
		$site_table=mysql_query($site_query);
		   
	   $user_email="select * from member_details where user_id=".$item_detail_row[user_id];
	   $productid=$item_detail_row[productid];
	   $producttitle=$item_detail_row[producttitle];
	   $user_email_res=mysql_query($user_email);
       $user_email_row=mysql_fetch_array($user_email_res);
	   $seller_name=$user_email_row[user_name];
	   $mail_to=$user_email_row[email];
	   $message=str_replace("<sitename>" , $site, $message);
       $message=str_replace("<sitename>" ,$site ,$message);
       $message=str_replace("<username>" , $seller_name , $message);
       $message=str_replace("<item_name>" , $producttitle , $message);
       $message=str_replace("<itemid>" , $productid , $message);
       $mail_from=$no_bid_placed['mail_from'];
       $headers  = "MIME-Version: 1.0\n";
       $headers .= "Content-type: text/html; charset=iso-8859-1\n";
       $headers .= "From: ".$mail_from."\n"; 
    //   mail($mail_to,$subject,$message,$headers);

     } 
  } 
}

//---------------------  Close Expired Items ---------------------------------------
$exp_cl="update auction_details set status='Closed' where  expire_date < '$datecheck'";
$exp_cl_res=mysql_query($exp_cl);

$exp_cl2="update auction_details set status='Active' where  expire_date > '$datecheck' and quantity>0";
$exp_cl_res2=mysql_query($exp_cl2);

// ----------------------------------------------------------------------



//---------------------  old item clean up---------------------------------------
$admin_old_items="select * from genaralsettings where set_id=20";
$admin_old_res=mysql_query($admin_old_items);
$admin_old_row=mysql_fetch_array($admin_old_res);

$old="select * from auction_details where status='Closed' and (to_days(now()) - to_days(expire_date) ) >= ".$admin_old_row['set_value'];
$old_res=mysql_query($old);
while($old_row=mysql_fetch_array($old_res))
{
$del="delete  from auction_details where productid=".$old_row[productid];
$del_sql=mysql_query($del);

$del="delete  from site_bids where productid=".$old_row[productid];
$del_sql=mysql_query($del);
}

$won_sql="select * from auction_details where ship_stat='unpaid'";
$won_res=mysql_query($won_sql);

while($won_row=mysql_fetch_array($won_res))
{
$pay_sql2="select * from usertransaction where itemid=".$won_row[productid];
$pay_res2=mysql_query($pay_sql2);
$pay_total_records2=mysql_num_rows($pay_res2);
if($pay_total_records2>0){

$paid_cl="update auction_details set ship_stat='paid' where productid=".$won_row[productid];
$paid_cl_res=mysql_query($paid_cl);
}}




$admin="select * from genaralsettings where set_id=65";
$admin_res=mysql_query($admin);
$admin_row=mysql_fetch_array($admin_res);
$addbids=$admin_row[set_value];


$bid_sql2="select * from tellafriend where reg='no'";
$bid_res2=mysql_query($bid_sql2);
while($bid_row2=mysql_fetch_array($bid_res2))
{

$femail=$bid_row2['email'];
$rdate=$bid_row2['rdate'];
$user_id=$bid_row2['user_id'];

$user_sql="select * from member_details where email='$femail' and date_of_registration > '$rdate'";
$user_result=mysql_query($user_sql);
$user_row=mysql_num_rows($user_result);

if($user_row>0){
$bid_sql23="update tellafriend set reg='yes' where email = '$femail'";
$bid_res23=mysql_query($bid_sql23);

$sql = "update member_details set bid_num=bid_num+'$addbids' where user_id='$user_id'";
$result = mysql_query($sql);
}
}
?>