<?php 	
use Dompdf\Dompdf;
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;	   
require __DIR__.'/vendor/autoload.php';
session_start();
require_once '../../Server/connection.php';
include('../includes/session.php');
if($_POST){
  $client_id = $_POST['client_id'];
  $file_id  = $_POST['file_id'];
  $file_status  = $_POST['file_status'];
  
  $exp_status  = $_POST['exp_status'];
  $upl_status  = $_POST['upl_status'];
  $space = " ";
  
   
  
  //$message = addslashes($_POST['message']);
  $mailfrom = 'kreenlogisticstz@gmail.com';
 
  
  
     $date = date("Y-m-d");
	$month1 = date('m', strtotime($date));
	$year = date('Y', strtotime($date));
	
	if($month1==1){
	 $month = 'JAN';
	  }else if($month1==2){
	 $month = 'FEB';	  
	  }else if($month1==3){
	$month = 'MARCH';	  
	  }else if($month1==4){
	$month = 'APR';		  
	  }else if($month1==5){
	$month = 'MAY';	  
	  }else if($month1==6){
	$month = 'JUNE';	  
	  }else if($month1==7){
	$month = 'JULY';	  
	  }else if($month1==8){
	$month = 'AUG';	  
	  }else if($month1==9){
	$month = 'SEPT';	  
	  }else if($month1==10){
	$month = 'OCT';	  
	  }else if($month1==11){
	$month = 'NOV';	  
	  }else{
	$month = 'DEC';	  
	  }
  
  
  
  $link = "<a href='https://cms.brightvision.co.tz/'>cms</a>";	
  
  $fileOpenDate = date("Y-m-d");
  
  $sql812 = "SELECT * FROM clients where client_id='$client_id'";
			$result812 = $conn->query($sql812);
				while($data812 = mysqli_fetch_array($result812))
							{
							$toEmail = $data812['client_email']; 
							$clientfName = $data812['client_fname']; 
							}
							
  					
  
  if($file_status=='Yes'){
	  $fileSt = 1;
	  $fileCloseDate = date("Y-m-d");
  }else{
	  $fileSt = 0;
	  $fileCloseDate = '0000-00-00';
  }
  
   $sql = "UPDATE client_files SET file_closeDate='$fileCloseDate', fileStatus = '$fileSt' WHERE file_id = {$file_id}";		

   $result = $conn->query($sql);
  
  if($result){
	
	if($upl_status=='Yes' && $exp_status=='YES'){
	
			  
	 for($x = 0; $x < count($_POST['standardexp_id']); $x++){
		 
				$fileSql = "INSERT INTO client_fileexpenses (file_id,standardexp_id,amount,chargeable_to,debitNote) 
				VALUES ('$file_id','".$_POST['standardexp_id'][$x]."','".$_POST['amount'][$x]."','".$_POST['chargeable_to'][$x]."','".$_POST['debitNote'][$x]."')";
				$resFileSql1 = $conn->query($fileSql);
	                 
					 if($_POST['chargeable_to'][$x]=='On Behalf Of Customer'){
						 
						 if($_POST['debitNote'][$x]=='USD'){ 
						$currency_id = 1;
						 }else{
						$currency_id = 0;	 
						 }
						 
				$fileSql4 = "INSERT INTO expenses (expCate_id,amount,exp_desc,currency_id,month,year,date) 
				VALUES (2,'".$_POST['amount'][$x]."','Customer File Expenses','$currency_id','$month','$year','$date')";
				$resFileSql14 = $conn->query($fileSql4);	 
						 
						 
					 }
					 
					 
					 
					 }
	
		ob_start();
		require("../client_File.php");
		$html = ob_get_contents();
        ob_end_clean();	
		
		//NEW
        define("DOMPDF_UNICODE_ENABLED",true);		
		//new;
		
		$dompdf = new DOMPDF();
		
		//new
		$dompdf -> loadHtml($aData['html']);
		$dompdf -> set_option('isRemoteEnabled',TRUE);
		//new
		
		
		
        $dompdf->load_html($html);
        $dompdf->set_paper("letter","portrait");	
        $dompdf->render();
		$filename = 'CustomerFile.pdf';	
		$pdfoutput = $dompdf->output();
		
		 $mail = new PHPMailer;
 $mail->IsSMTP();        //Sets Mailer to send message using SMTP
 $mail->Host = 'smtp.gmail.com';  //Sets the SMTP hosts of your Email hosting, this for Godaddy
 $mail->Port = '465';        //Sets the default SMTP server port
 $mail->SMTPAuth = true;       //Sets SMTP authentication. Utilizes the Username and Password variables
 $mail->Username = 'jacalminja929@gmail.com';     //Sets SMTP username
 $mail->Password = 'kknipxvlrhpyopga';     //Sets SMTP password
 $mail->SMTPSecure = 'ssl';       //Sets connection prefix. Options are "", "ssl" or "tls"
 $mail->From = $mailfrom;   //Sets the From email address for the message
 $mail->FromName = 'Bright Vision';   //Sets the From name of the message
 $mail->AddAddress($mailto, $firstname);  //Adds a "To" address
 $mail->WordWrap = 50;       //Sets word wrapping on the body of the message to a given number of characters
 $mail->IsHTML(true);       //Sets message type to HTML  

 //$mail->AddStringAttachment($pdfoutput,'Application/pdf',$filename,false);
 $mail->AddStringAttachment($pdfoutput,$filename);
 
 
 
 foreach ($_FILES["file"]["name"] as $key => $name){
		$newFilename = time() . "_" . $name;
		move_uploaded_file($_FILES["file"]["tmp_name"][$key],'../../files/'.$newFilename);
		$location = '../../files/' . $newFilename;
		
		$fSql = "INSERT INTO client_docs (client_id,file_id,role,fileName) 
				VALUES ('$client_id','$file_id','Accountant','$location')";

		$resFSql1 = $conn->query($fSql);
		
  $mail->AddAttachment($_FILES["file"]["tmp_name"][$key], $_FILES["file"]["name"][$key]);
  //$mail->AddStringAttachment($_FILES["file"]["tmp_name"][$key], $_FILES["file"]["name"][$key]);
  //$mime->addAttachment($file,'application/octet-stream');

  
	          }
 
 $mail->Subject = 'Customer File And Attachment';   //Sets the Subject of the message
 //$mail->Body = 'Please See Attachment of your pdf invoice.';    //An HTML or plain text message body
 $mail->Body  = "Please Click this link".$space.$link.$space."to login to your account using your email which is".$space.$toEmail.$space."and your password to view your files and documents.";

 if($mail->Send())        //Send an Email. Return true on success or false on error
 {	
				   
				   
				$_SESSION['success_message'] = "Expenses are recorded Successfully";
                       header("Location: ../recordFileExpenses.php");
                             exit();   	  
 } 
		
			  
	          }else if($upl_status=='No' && $exp_status=='YES'){
				  
		for($x = 0; $x < count($_POST['standardexp_id']); $x++){
		 
				$fileSql = "INSERT INTO client_fileexpenses (file_id,standardexp_id,amount,chargeable_to,debitNote) 
				VALUES ('$file_id','".$_POST['standardexp_id'][$x]."','".$_POST['amount'][$x]."','".$_POST['chargeable_to'][$x]."','".$_POST['debitNote'][$x]."')";
				$resFileSql1 = $conn->query($fileSql);
	                 
					 if($_POST['chargeable_to'][$x]=='On Behalf Of Customer'){
						 
						 if($_POST['debitNote'][$x]=='USD'){ 
						$currency_id = 1;
						 }else{
						$currency_id = 0;	 
						 }
						 
				$fileSql4 = "INSERT INTO expenses (expCate_id,amount,exp_desc,currency_id,month,year,date) 
				VALUES (2,'".$_POST['amount'][$x]."','Customer File Expenses','$currency_id','$month','$year','$date')";
				$resFileSql14 = $conn->query($fileSql4);	 
						 
						 
					 }
					 
					 
					 
					 }
				  
		//if($resFileSql1){		  
			//use template		
		ob_start();
		require("../client_File.php");
		$html = ob_get_contents();
        ob_end_clean();	
		
		//NEW
        define("DOMPDF_UNICODE_ENABLED",true);		
		//new;


        $dompdf = new DOMPDF();
		
		
		//new
		$dompdf -> loadHtml($aData['html']);
		$dompdf -> set_option('isRemoteEnabled',TRUE);
		//new
		
		
        $dompdf->load_html($html);
        $dompdf->set_paper("letter","portrait");	
        $dompdf->render();
		$filename = 'ClientFile.pdf';	
		$pdfoutput = $dompdf->output();

        $mail = new PHPMailer;
 $mail->IsSMTP();        //Sets Mailer to send message using SMTP
 $mail->Host = 'smtp.gmail.com';  //Sets the SMTP hosts of your Email hosting, this for Godaddy
 $mail->Port = '465';        //Sets the default SMTP server port
 $mail->SMTPAuth = true;       //Sets SMTP authentication. Utilizes the Username and Password variables
 $mail->Username = 'jacalminja929@gmail.com';     //Sets SMTP username
 $mail->Password = 'kknipxvlrhpyopga';     //Sets SMTP password
 $mail->SMTPSecure = 'ssl';       //Sets connection prefix. Options are "", "ssl" or "tls"
 $mail->From = $mailfrom;   //Sets the From email address for the message
 $mail->FromName = 'Kreen Logistics';   //Sets the From name of the message
 $mail->AddAddress($mailto, $firstname);  //Adds a "To" address
 $mail->WordWrap = 50;       //Sets word wrapping on the body of the message to a given number of characters
 $mail->IsHTML(true);       //Sets message type to HTML  

 //$mail->AddStringAttachment($pdfoutput,'Application/pdf',$filename,false);
 $mail->AddStringAttachment($pdfoutput,$filename);
 
 $mail->Subject = 'Customer File';   //Sets the Subject of the message
 //$mail->Body = 'Please See Attachment of your pdf invoice.';    //An HTML or plain text message body
 //$mail->Body  = $message; 
 //$mail->Body  = $message.$space.$link.$space."which is".$space.$toEmail.$space."and your password to view your files and documents.";
 $mail->Body  = "Please Click this link".$space.$link.$space."to login to your account using your email which is".$space.$toEmail.$space."and your password to view your files and documents.";
 
 if($mail->Send())        //Send an Email. Return true on success or false on error
 {	
				   
				   
				$_SESSION['success_message'] = "Expenses are recorded Successfully";
                       header("Location: ../recordFileExpenses.php");
                             exit();   	  
 }			  
				  
			  //}				  
				
              //				
			  }
			  
}  
		
			  
                  

	
}