<?php
error_reporting(1|0|0);
set_time_limit (0) ;
session_start();		
require_once(dirname(__FILE__).'/wp-config.php');
global $wpdb; 

//Load Composer's autoloader
include_once (dirname(__FILE__).'/vendors/autoload.php');

use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;

//$bookings = "SELECT booking.code as p_code,booking.total_amount as total_amount, s.max_months as months from customer_booking as booking JOIN schemes  ON s.code=c.scheme where c.booking_status_code =7 group by c.code";

$bookings="select booking.code as p_code,booking.order_no as dhs,booking.bookfor_name as customer_name, booking.total_amount as total_amount,booking.last_payment_date as last_payment_date, s.max_months as months from customer_booking booking inner join schemes s ON  s.code=booking.scheme where booking.booking_status_code =7";

//echo '<br/>'.$bookings;

$getBookings = $wpdb->get_results($bookings);
$counter = 	0;
$template = '';

if(!empty($getBookings)){
	
	foreach($getBookings as $key=> $getBookingsData){
		
	$payments = "select count('code') as total_paid FROM  consolidate_policy where  policy_code='".$getBookingsData->p_code."' and paid=1";
	$totalPaid  = $wpdb->get_var($payments);

		/* echo '<br/>---> '.$payments;
		echo '<br/>---> '.$totalPaid;
		echo '<pre> ';
		print_r($getBookingsData);
		echo '</pre>'; */
		
		if($totalPaid < $getBookingsData->months){
			//echo '<br/>'.$payments;
			//echo '<br/>'.$key.' = '.$getBookingsData->last_payment_date.' === '.date("Y-m-d",strtotime("-3 Months"));
			if($getBookingsData->last_payment_date > date("Y-m-d",strtotime("-3 Months"))){
				//echo '<br/>===  UNPAID '.$policy['dhs'].' '.$policy['customer_name'];
				//echo '<BR/>CHANGE STATUS';
				$updateUnpaidStatus = "UPDATE customer_booking SET booking_status_code=2 where code =".$getBookingsData->p_code;
				if($wpdb->query($updateUnpaidStatus)){
					$template .= '<p>Unpaid:  '.$getBookingsData->dhs.' -- '.$getBookingsData->customer_name.'</p>';					
				}
			}else{
				//echo '<BR/>CHANGE STATUS';
				$updateDropoutStatus = "UPDATE customer_booking SET booking_status_code=5 where code =".$getBookingsData->p_code;
				if($wpdb->query($updateDropoutStatus)){
					$template .= '<p>Dropout:  '.$getBookingsData->dhs.' -- '.$getBookingsData->customer_name.'</p>';					
				}
			}
		}
	}
}
if(!empty($template)){
	echo $template;
	echo order_status_update_email($template);					
} 
	
/* #########################
# Method order_email_to_purchase() send new order emailt o admin
# Authod: kantsverma
# Date: 18-10-22
# Params: template dynamic data
 ###########################*/
function  order_status_update_email( $data)
{	
	$message = '<html> 
	<body> 
	<p style=\"text-align:center;height:100px; border-radius:3px;padding:10px;\">
	'.$data.'
	</p>
	</body>
	</html>';

		//echo '<br/>'.$message;
		//Create an instance; passing `true` enables exceptions
		$mail = new PHPMailer(true);

		try {
			//Server settings
			//$mail->SMTPDebug = SMTP::DEBUG_SERVER;                       //Enable verbose debug output
			$mail->SMTPDebug = false;                       //Enable verbose debug output			
			$mail->isSMTP();                                             //Send using SMTP

			$mail->Host         = SMTP_HOST;
			$mail->SMTPAuth     = SMTP_AUTH;
			$mail->Username     = SMTP_USERNAME;
			$mail->Password     = SMTP_PASSWORD;
			$mail->SMTPSecure   = 'tls';
			$mail->Port         = SMTP_PORT;
			$mail->SMTPOptions = array(
				'ssl' => array(
					'verify_peer' => false,
					'verify_peer_name' => false,
					'allow_self_signed' => true
				)
			);
			//Recipients
			$mail->setFrom('contact@jshine.in', 'JShine Status Update');
			$mail->addAddress('developer@jshine.in');     //Add a recipient
			$mail->addReplyTo('contact@jshine.in', 'JShine Jewellery');
			$mail->addCC('developer@jshine.in');
			//$mail->addBCC(ORDER_EMAIL_BCC);

			//Attachments
			//$mail->addAttachment('/var/tmp/file.tar.gz');         //Add attachments
			//$mail->addAttachment('/tmp/image.jpg', 'new.jpg');    //Optional name

			//Content
			$mail->isHTML(true);                                  //Set email format to HTML
			$mail->Subject = 'JShine Status Update';
			$mail->Body    = $message;
			$mail->AltBody = $message;

			if($mail->send()){
				return '1';					
			}else{
				return 'Order email sending failed';
			}

		} catch (Exception $e) {
			return "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
		}	
}
?>