<?php
include "includes/config.php";
include "includes/session.php";
include "includes/functions.php";

$curDate	= date("Y-m-d");

$sql_date_check = "SELECT ar . * , am . *
						FROM ".$tableprefix."artists_membershipDetails am
						INNER JOIN ".$tableprefix."artists ar ON ar.artist_id = am.artist_id
						INNER JOIN ".$tableprefix."sellerPlans s ON s.plan_Id = ar.sellerPlanID AND  s.plan_Id = am.plan_Id 
						WHERE DATE_ADD(CURDATE() , INTERVAL 3
						DAY ) = am.artist_memEndDate AND s.plan_Period IN ('Y','M') ";

$rs_date_check	=mysql_query($sql_date_check) or die(mysql_error());


		$site_name	= SITE_NAME;
		
		$site_URL	= SITE_URL;
		
		$curDate	= date('Y-m-d');
		
		$mailcontent_seller_expiry= "";
		$mailcontent_seller			="";

if(mysql_num_rows($rs_date_check)>0)
{
				while($sellerObj=mysql_fetch_object($rs_date_check))
				{
					
					  $mailcontent_seller=readEmailTemplate('seller_plan_expirenotification');
					  $mailcontent_seller=str_replace('[seller_fname]',$sellerObj->first_name,$mailcontent_seller);
					  
					  
					  $mailcontent_seller=str_replace('[seller_lname]',$sellerObj->last_name,$mailcontent_seller);
					  
					  $mailcontent_seller=str_replace('[site_name]',$site_name,$mailcontent_seller);
					  
					  $mailcontent_seller=str_replace('[date_expiry]',dateFormat($sellerObj->artist_memEndDate, "Y-m-d", "m/d/Y"),$mailcontent_seller);
					  
					 $link='<br><br><a href='.$site_URL."/sellers/seller_plan_upgrade.php?sellerid=".$sellerObj->artist_id.' target="_blank">'.MAIL_PAYMENT_LINK.'</a><br><br>';
					  
					  $mailcontent_seller=str_replace('[LINK]',$link,$mailcontent_seller);

					
					$mail_send_details_seller = '<html>
<head>
   <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
   <title >'.SITE_NAME.'</title>
  <link href="./themes/'. $active_template[4].' rel="stylesheet" type="text/css">
</head>
<body>

<table width="103%" cellspacing="10" cellpadding="0" bgcolor="#EAEAEA">
   <tr>
     <td align="center" valign="top">
         
         <table width="600" border="0" cellpadding="0" cellspacing="0" bordercolor="#EAEAEA" bgcolor="#FFFFFF" class="manage_table">
            <tr>
               <td align="right" >
              </td>
            </tr>
            <tr>
               <td valign="middle" align="left"><table width="100%"  border="0" cellspacing="0" cellpadding="0">
                 <tr>
                   <td  bgcolor="#EAEAEA" > <img src='.SITE_URL.'/banners/'.getSettingsValue('site_logo').' width="125" height="60"/></td>
                 </tr>
               </table></td>
            </tr>
            <tr>
              <td align="center" valign="top"><table width="100%"  border="0" cellspacing="0" cellpadding="10">
                <tr>
                  <td align="left"><p>'.$mailcontent_seller.'</p>
                  </td>
                </tr>
              </table></td>
            </tr>
            <tr>
               <td align="center" bgcolor="#EAEAEA" class="footer">
                  <p>&nbsp;</p>
              </td>
            </tr>
       </table>

     </td>
   </tr>
</table>


</body>
</html>';

		
		$subject = MAIL_SELLER_PLAN_EXPIRY_NOTIFICATION_SUBJECT." ".SITE_NAME;
		
		$headers  = "MIME-Version: 1.0" . "\r\n";
		$headers .= "Content-type: text/html; charset=iso-8859-1" . "\r\n";
		$headers .= "From: ".SITE_EMAIL."<".SITE_EMAIL.">" . "\r\n";
		$headers .= "Reply-To: ".SITE_NAME."<".SITE_EMAIL.">" . "\r\n";
		$headers .= "Return-Path: ".SITE_NAME."<noreply@".SITE_EMAIL.">" . "\r\n"; 
		
		$mailsent = mail($sellerObj->email, $subject, $mail_send_details_seller, $headers);
		
		// **********************************************************************Send Mail To Administrator 
		
				
		} 

}				
				
				
	// Update Expiry Of Seller if Seller Expired
			
  	  $sql_date_check_expiry = "SELECT ar . * , am . *
						FROM ".$tableprefix."artists_membershipDetails am
						INNER JOIN ".$tableprefix."artists ar ON ar.artist_id = am.artist_id
						INNER JOIN ".$tableprefix."sellerPlans s ON s.plan_Id = ar.sellerPlanID AND  s.plan_Id = am.plan_Id 
						WHERE am.artist_memEndDate <= CURDATE() AND s.plan_Period IN ('Y','M') ";
						
	 $rs_date_check_expiry	=mysql_query($sql_date_check_expiry) or die(mysql_error());

		 mysql_num_rows($rs_date_check_expiry);
		if(mysql_num_rows($rs_date_check_expiry)>0)
		{		
		
			while($sellerObj=mysql_fetch_object($rs_date_check_expiry))
			{

				
				
					   $seller_expiry	= "UPDATE  ".$tableprefix."artists SET plan_expired='Y' where artist_id='".$sellerObj->artist_id."' ";
						
						$rs_expiry		= mysql_query($seller_expiry) or die(mysql_error());
						
						$subject_expiry = MAIL_SELLER_PLAN_EXPIRY_NOTIFICATION_SUBJECT." ".SITE_NAME;
						
					  $mailcontent_seller_expiry=readEmailTemplate('seller_plan_expiry');
					  $mailcontent_seller_expiry=str_replace('[seller_fname]',$sellerObj->first_name,$mailcontent_seller_expiry);
					  $mailcontent_seller_expiry=str_replace('[seller_lname]',$sellerObj->last_name,$mailcontent_seller_expiry);
					  
					  $mailcontent_seller_expiry=str_replace('[site_name]',$site_name,$mailcontent_seller_expiry);
					  
					  $mailcontent_seller_expiry=str_replace('[date_expiry]',dateFormat($sellerObj->artist_memEndDate, "Y-m-d", "m/d/Y"),$mailcontent_seller_expiry);
					  
					 $link='<br><br><a href='.$site_URL."/sellers/seller_plan_upgrade.php?sellerid=".$sellerObj->artist_id.' target="_blank">'.MAIL_PAYMENT_LINK.'</a><br><br>';
					  
					   $mailcontent_seller_expiry=str_replace('[LINK]',$link,$mailcontent_seller_expiry);

					
					 $mail_send_details_seller_expiry = '<html>
<head>
   <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
   <title >'.SITE_NAME.'</title>
  <link href="./themes/'. $active_template[4].' rel="stylesheet" type="text/css">
</head>
<body>

<table width="103%" cellspacing="10" cellpadding="0" bgcolor="#EAEAEA">
   <tr>
     <td align="center" valign="top">
         
         <table width="600" border="0" cellpadding="0" cellspacing="0" bordercolor="#EAEAEA" bgcolor="#FFFFFF" class="manage_table">
            <tr>
               <td align="right" >
              </td>
            </tr>
            <tr>
               <td valign="middle" align="left"><table width="100%"  border="0" cellspacing="0" cellpadding="0">
                 <tr>
                   <td  bgcolor="#EAEAEA" > <img src='.SITE_URL.'/banners/'.getSettingsValue('site_logo').' width="125" height="60"/></td>
                 </tr>
               </table></td>
            </tr>
            <tr>
              <td align="center" valign="top"><table width="100%"  border="0" cellspacing="0" cellpadding="10">
                <tr>
                  <td align="left"><p>'.$mailcontent_seller_expiry.'</p>
                  </td>
                </tr>
              </table></td>
            </tr>
            <tr>
               <td align="center" bgcolor="#EAEAEA" class="footer">
                  <p>&nbsp;</p>
              </td>
            </tr>
       </table>

     </td>
   </tr>
</table>


</body>
</html>';

		$subject = MAIL_SELLER_PLAN_EXPIRY_NOTIFICATION_SUBJECT." ".SITE_NAME;
		
		$headers  = "MIME-Version: 1.0" . "\r\n";
		$headers .= "Content-type: text/html; charset=iso-8859-1" . "\r\n";
		$headers .= "From: ".SITE_EMAIL."<".SITE_EMAIL.">" . "\r\n";
		$headers .= "Reply-To: ".SITE_NAME."<".SITE_EMAIL.">" . "\r\n";
		$headers .= "Return-Path: ".SITE_NAME."<noreply@".SITE_EMAIL.">" . "\r\n"; 
		
		$mailsent = mail($sellerObj->email, $subject_expiry, $mail_send_details_seller_expiry, $headers);
				
				// End 
	   }		  
	
}
?>