<?
session_start();
$brcode = $_SESSION['brcode'];
$user = $_SESSION['user'];
date_default_timezone_set("Asia/Kolkata"); 
if(strlen($brcode)<=0)
{
 	print "<script> alert('Invalid Login'); parent.document.location='ind.php' </script>";
 	return;
}
include("../opendb.php");
include("../test.php");


date_default_timezone_set("Asia/Kolkata");
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
require '../../PHPMailer/src/Exception.php';

require '../../PHPMailer/src/PHPMailer.php';
require '../../PHPMailer/src/SMTP.php';

// Instantiation and passing [ICODE]true[/ICODE] enables exceptions
$mail = new PHPMailer(true);






$d = $_REQUEST['d'];
$m = $_REQUEST['m'];
$y = $_REQUEST['y'];
$d1 = $_REQUEST['d1'];
$m1 = $_REQUEST['m1'];
$y1 = $_REQUEST['y1'];
$asondt = $_REQUEST['asondt'];

$df=$y.'-'.$m.'-'.$d;
$dt=date('Y-m-d');
$curdt=implode('-', array_reverse(explode('-', $dt)));
$asondt1=implode('-', array_reverse(explode('-', $asondt)));
$chkled = $_REQUEST['chkled'];
$i=0;
foreach($chkled as $a)
{
    $chkled[$i]=$a; 
    $i++;
}
//print_r($chkled);
$branchqry=mysqli_query($db,"select incharge_no from branch where brcode='$brcode'");
$branchrow=mysqli_fetch_array($branchqry);
$branchinchargeno=$branchrow[0];

for($j=0;$j<$i;$j++)
{
    
   $cusleres= mysqli_query($db,"select sum(dramt-cramt) from journal2 where brcode='$brcode' and partic='$chkled[$j]' and date <= '$asondt'");
   $cuslerow=mysqli_fetch_array($cusleres);
   $cusleres1= mysqli_query($db,"select op,drcr from ledger where brcode='$brcode' and led='$chkled[$j]'");
   $cuslerow1=mysqli_fetch_array($cusleres1);
   if($cuslerow1[1]=='Dr')
   {
       $mop=$cuslerow1[0];
   }
   else
   {
       $mop=$cuslerow1[0]*-1;
   }
    $opbal=$mop+$cuslerow[0];
    $smsqry=mysqli_query($db,"select email,acname,father from customer where acname='$chkled[$j]'");
    $smsrow=mysqli_fetch_array($smsqry);
    $custmail=$smsrow[0];
    $customername=$smsrow[1];
    $salescontact=$smsrow[2];
    $customername=trim($customername);
    $customer30=substr($customername, 0, 30);

        
    $msms_code ="";
    for ($ctr=0;$ctr<=3;$ctr++)
    {
	    $msms_code .=chr(rand(48,57)).chr(rand(65,90)).chr(rand(97,122));
    }
    //$smsstring=$msms_code.$chkled[$j];
    $smsstring=$msms_code;
    $curdate=date("Y-m-d H:i:s");
    
    $sendurl='<A href="http://68.178.160.62/ampleicher/paypdf/custledger1.php?custid='.$smsstring.'">CLICK HERE TO VIEW ....</A>';
    //$sendurl="http://68.178.160.62/ampleicher/paypdf/custledger1.php?custid=$smsstring";
    
    
    
    
    
    try {
    
 //Server settings
 $mail->SMTPDebug = 0; // Enable verbose debug output
 $mail->isSMTP(); // Set mailer to use SMTP
 $mail->Host = 'smtpout.secureserver.net'; // Specify main and backup SMTP servers
 
 $mail->SMTPAuth = true; // Enable SMTP authentication
 $mail->Username = 'hrd@psnce.in'; // SMTP username
 $mail->Password = 'HRDpsn@6559*'; // SMTP password
 $mail->SMTPSecure = 'ssl'; // Enable TLS encryption, [ICODE]ssl[/ICODE] also accepted
 $mail->Port = 465; // TCP port to connect to
 $mail->clearAttachments();
$mail->clearAllRecipients();


           
			$emmail=$custmail;
            $tomail =  $emmail;
//echo $tomail;
//exit;
$curdatetm=date("Y-m-d h:i:sa");
$systime=$d.'-'.$m.'-'.$y;
//Recipients
 $mail->setFrom('acc.whspare@psnauto.in', 'PSN Automotive(Accounts)');
 //$mail->addAddress('antonymkj.psn@gmail.com', 'Joe User'); // Add a recipient
 //$mail->addAddress('antonymathew@psninfotech.com'); // Name is optional
 $mail->addAddress($tomail);
 $mail->addReplyTo('acc.whspare@psnauto.in', 'PSN Automotive(Accounts)');
 //$mail->addCC('cc@example.com');
 //$mail->addBCC('bcc@example.com');


// Attachments
 //$mail->addAttachment('/home/cpanelusername/attachment.txt'); // Add attachments
 //$mail->addAttachment('/home/cpanelusername/image.jpg', 'new.jpg'); // Optional name
//$mail->AddAttachment($filename);
// Content
 $mail->isHTML(true); // Set email format to HTML
 $mail->Subject = 'Statement of Accounts with PSN Automotive Marketing Pvt Ltd as on '.$asondt1;
 $mail->Body = '<body>
			<table>
			<tr>
      			<td>To</td>
    			</tr>
    			<tr>
      			<td>'.strtoupper($customername).'</td>
    			</tr>
    			<tr>
      			<td>Dear Sir,</td>
    			</tr>
    			<tr>
      			<td>Confirmation of Balance as on '.$asondt1.'</td>
    			</tr>
			<tr>
      			<td>In connection with the audit of our accounts, we would request you to
confirm direct to us that the balance of Rs.'.$opbal.' is due to us
as on '.$asondt1.' as per our books of accounts If the amount shown is not
in agreement with your books, please inform us the amount shown in your
books with necessary reconciliation. In the absence of any confirmation
or notification of any differences within 15 days, it will be considered
that the above balance is correct and has been accepted by you.</td>
    			</tr>
    			
    			
    			
    			<tr>
      			<td>Please click this link to get the detailed statement of accounts.</td>
    			</tr>
			<tr>
      			<td>'.$sendurl.'</td>
    			</tr>
    			<tr>
      			<td></td>
    			</tr>
    			<tr>
      			<td>Thanking you</td>
    			</tr>
    			<tr>
      			<td>Yours Faithfully</td>
    			</tr>
    			<tr>
      			<td>Authorised Signatory</td>
    			</tr>
    			<tr>
      			<td>PSN Automotive Marketing Pvt Ltd</td>
    			</tr>
  			</table>
			</body>';
 $mail->AltBody = 'This is the body in plain text for non-HTML mail clients';

$mail->send();

mysqli_query($db,"insert into accledgermail (acname,datetime,code,custid,brcode,mobile,asondate,username) values('$smsrow[1]','$curdate','$smsstring','0','$brcode','$tomail','$asondt','$user')");

 $mnos++;

            
} 
catch (Exception $e) {
 echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
}
echo $mnos.' Mails has been sent';	
    
    }



    print "<script>alert('Mails sent successfully'); </script>";
	print "<script>document.location='custledmail.php'</script>";
?>