<?php session_start();  //Developed by ITechScripts
require 'include/announcements.php'; // ITECHBIDS Configuration File
require 'include/itechbids.php'; // Server compatibility Patch
$productid=$_REQUEST['productid'];

//  admin detail
                    $site_query="select * from genaralsettings where set_id=1";
					$site_table=mysql_query($site_query);
     				if($site_row=mysql_fetch_array($site_table))
					$site_name=$site_row[set_value];

$site_query="select * from genaralsettings where set_id=3";
$site_table=mysql_query($site_query);
if($site_row=mysql_fetch_array($site_table))
{
$admin_mail=$site_row['set_value'];
}
$mail_from=$admin_mail;
// item detail 
$item_detail_query="select * from auction_details where productid =". $productid;
$item_detail_table=mysql_query($item_detail_query);
$item_detail_row=mysql_fetch_array($item_detail_table);
$producttitle=$item_detail_row[producttitle];
$currency=$item_detail_row[currency];
$item_shipping_cost=$currency.$item_detail_row[shipping_cost];
$item_shipping_route=$item_detail_row[shipping_route];
$item_shipping_tax=$currency.$item_detail_row[tax];
//bid detail 
$item_query="select * from site_bids where productid =$productid and user_pos='No' order by quantity DESC , bidding_amount DESC";
$item_tab=mysql_query($item_query);
$size = mysql_num_rows($item_tab);
// echo "size".$size;
if($size >= 1)
{

// mail to seller 

$message=$bidder_win['mail_message'];
$postsubject=$bidder_win['mail_subject'];
$seller_name=$_SESSION['username'];
$message=str_replace("SELLER" , $_SESSION[username] , $message);
$message=str_replace("<PRODUCTTITLE>" , $item_detail_row[producttitle] , $message);
$message=str_replace("<GET_VIEW_ITEM>" , "$site_name/itechd.php?productid=".$productid , $message);
$mess="<tr bgcolor=#999999 height=3><td  colspan=2></td></tr>";
if($item_detail_row[Quantity] > 1)
{
while($item_row=mysql_fetch_array($item_tab))
{
if(empty($amt))
$amt=$item_row['bidding_amount'];
$sin_item_detail_query="select * from auction_details where productid =". $productid;
$sin_item_detail_table=mysql_query($sin_item_detail_query);
$sin_item_detail_row=mysql_fetch_array($sin_item_detail_table);
$higher_quantity_id = $item_row['productid'];
$single_amount=$item_row['bidding_amount'];
$user_id=$item_row['user_id'];
$sin_item_detail_row['Quantity'];
$item_row['quantity'];
if($sin_item_detail_row['Quantity'] >= $item_row['quantity'])
$qty=$item_row['quantity'];
else
$qty=$sin_item_detail_row['Quantity'];
if($sin_item_detail_row['Quantity']!=0)
{
$solddate=date("Y-m-d G:i:s");
$update_query ="update site_bids set user_pos='Yes',sale_date='$solddate',quantity=$qty where user_id='$user_id' and productid=".$productid;
mysql_query($update_query);
$update_query ="update auction_details set status='Closed',Quantity=Quantity-$qty,quantity_sold=quantity_sold+$qty , sale_price='$amt' where productid=".$productid;
mysql_query($update_query);  

$query="select * from member_details where user_id='$user_id'";
$mytable=mysql_query($query);
 // seller mail message 
 if($myrow=mysql_fetch_array($mytable))
 {
 $higher_user_name=$myrow['user_name'];
 $email=$myrow['email'];
 $mess.="<tr><td><font color=#000088 >Buyer Name</td> <td><b> $higher_user_name </td></tr>";
 $mess.="<tr><td><font color=#000088 >Email Id</td> <td><B> $email</td></tr>";
 }
 $single="$currency"."$amt";
 $mess.="<tr><td><font color=#000088 > Quantity</td> <td><B> $qty</td></tr>";
 $mess.="<tr><td><font color=#000088 > Sale Price(Single Item)</td> <td><B> $single </td></tr>"; 
 $tot_price ="$currency".($qty * $amt);
 $mess.="<tr><td> <font color=#000088 >Total Price</td> <td><B>$tot_price</td></tr>"; 
 $mess.="<tr bgcolor=#999999 ><td colspan=2></td></tr>";
  // Buyer mail message
     $buy_message = $bidder_win[mail_message];
	 $query="insert into trader_list(seller,buyer)values('$_SESSION[username]','$higher_user_name')";
     mysql_query($query); 
	   
	               $buy_message=str_replace("NAME" , $higher_user_name , $buy_message);
				   $buy_message=str_replace("<PRODUCTTITLE>" , $producttitle , $buy_message);
    	           $buy_message=str_replace("<GET_VIEW_ITEM>" , "$site_name/itechd.php?productid=".$productid , $buy_message);
	   
	   
       $mail_from=$admin_mail;
       $mail_to=$email;
       $subject= $bidder_win[mail_subject];
 $headers  = "MIME-Version: 1.0\r\n";
 $headers .= "Content-type: text/html; charset=iso-8859-1\r\n";
 $headers .= "From: ". $mail_from."\r\n";
 

 
 $mail=mail($mail_to,$subject,$buy_message,$headers);
}
} 
} 
else if($item_detail_row[Quantity]==1) 
{
$item_row=mysql_fetch_array($item_tab);
$user_id=$item_row['user_id'];
$amt=$item_row['bidding_amount'];
if($item_detail_row['Quantity'] >= $item_row['quantity'])
$qty=$item_row['quantity'];
else
$qty=$sin_item_detail_row['Quantity'];
$solddate=date("Y-m-d G:i:s"); 
$update_query ="update site_bids set user_pos='Yes',sale_date='$solddate',quantity=$qty where user_id='$user_id' and productid=".$productid;
mysql_query($update_query);
$update_query ="update auction_details set status='Closed',quantity=quantity-$qty,quantity_sold=quantity_sold+$qty , sale_price='$amt' where productid=".$productid;
mysql_query($update_query);  
 $query="select * from member_details where user_id='$user_id'";
 $mytable=mysql_query($query);
 if($myrow=mysql_fetch_array($mytable))
 {
 $higher_user_name=$myrow['user_name'];
 $email=$myrow['email'];
 $mess.="<tr><td><font color=#000088 >Buyer Name</td> <td><b> $higher_user_name </td></tr>";
 $mess.="<tr><td><font color=#000088 > Email Id</td> <td><B> $email</td></tr>";
 }
 $single="$currency".$amt;
 $mess.="<tr><td><font color=#000088 > Quantity</td> <td><B> $qty</td></tr>";
 $mess.="<tr><td><font color=#000088 > Sale Price(Single Item)</td> <td><B>$single</td></tr>"; 
 $tot_price ="$currency".($amt * $qty );
 $mess.="<tr><td> <font color=#000088 >Total Price</td> <td><B> $tot_price</td></tr>"; 
 $mess.="<tr bgcolor=#999999 ><td  colspan=2></td></tr>";
 
  $buy_message = $bidder_win[mail_message];
	   
	    $buy_message=str_replace("NAME" , $higher_user_name , $buy_message);
		$buy_message=str_replace("<PRODUCTTITLE>" , $producttitle , $buy_message);
    	$buy_message=str_replace("<GET_VIEW_ITEM>" , "$site_name/itechd.php?productid=".$productid , $buy_message);
	   
	   
       $mail_from=$admin_mail;
       $mail_to=$email;
       $subject= $bidder_win[mail_subject];
       $headers  = "MIME-Version: 1.0\r\n";
       $headers .= "Content-type: text/html; charset=iso-8859-1\r\n";
       $headers .= "From: ". $mail_from."\r\n";
	   
	   
 $mail=mail($mail_to,$subject,$buy_message,$headers);
  } //else if($item_detail_row[quantity]==1) 
  
  // mail to admin
  
  } // if($size > 1)
   else if($size==0) // no bidder
  {
  $upsql="update auction_details set status='Closed',no_of_repost=0 where productid=".$productid;
  mysql_query($upsql);
  $solddate=date("Y-m-d G:i:s"); 
  $upsql="update site_bids set sale_date='$solddate' where productid=".$productid;
  mysql_query($upsql);
  $useremailid="select * from member_details where user_id=$_SESSION[userid]";
  $message=$reserve_price_nmet['mail_message'];
  $subject=$reserve_price_nmet['mail_subject'];
  $mail_from=$reserve_price_nmet['mail_from'];

       $subject=$subject;
	   $user_email="select * from member_details where user_id=".$_SESSION[userid];
	   $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("<itemname>" , $producttitle , $message);
        $message=str_replace("<itemid>" , $productid , $message);
	   
	   
	   
      
       $headers  = "MIME-Version: 1.0\r\n";
       $headers .= "Content-type: text/html; charset=iso-8859-1\r\n";
       $headers .= "From: ".$mail_from."\r\n"; 
	  mail($mail_to,$subject,$message,$headers);
  }  //else if($size==0) 

echo '<meta http-equiv="refresh" content="0;url=myauction.php?mode=sell&status=Active">';
exit();
?>