<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;	   
require __DIR__.'/vendor/autoload.php';
session_start();
require_once '../../Server/connection.php';

    $mailfrom = 'kreenlogistics@gmail.com';
	
	
	$payment_mode_id  = $_POST['payment_mode_id'];
	
	
	
	if($payment_mode_id==1){
		
	$client_id  = $_POST['client_id'];
	$currency_id  = $_POST['currency_id'];
	$inv_id  = $_POST['inv_id'];
	$amount  = $_POST['amount'];
	$upl_status  = $_POST['upl_status'];
	$doc_no  = $_POST['bank_slip_no']; 
	$payment_desc  = addslashes($_POST['payment_desc']);
	$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';	  
	  }
	
	$sql812 = "SELECT vat_settings.rate FROM vat_settings";
				$result812 = $conn->query($sql812);
				while($data812 = mysqli_fetch_array($result812))
							{
							$vat_rate = $data812['rate'];
							}
							
	$sql8122 = "SELECT client_fname,client_email FROM clients where client_id='$client_id'";
				$result8122 = $conn->query($sql8122);
				while($data8122 = mysqli_fetch_array($result8122))
							{
							$client_name = $data8122['client_fname'];
							$client_email = $data8122['client_email'];
							}
							
	$sql81221 = "SELECT * FROM currency where currency_id='$currency_id'";
				$result81221 = $conn->query($sql81221);
				while($data81221 = mysqli_fetch_array($result81221))
							{
							$currencyName = $data81221['currencyName'];
							$bankAmount = $data81221['amount'];
							}
  
  $space = " ";
  //$vat = $vat_rate/100 * $amount;
  $amount_Vat = $amount;
  
  $totalAmount = $bankAmount + $amount_Vat;
  
  
  $msg = "Hello Client".$space."you have paid".$space.$currencyName.$space.$amount_Vat.$space."with vat Below is attachment Of Your Receipt";
	
	$sql = "INSERT INTO collected_payments (client_id,inv_id,amount_paid,payment_mode_id,payment_desc,cheque_no,currency_id,date,month,year) VALUES ('$client_id','$inv_id','$amount','$payment_mode_id','$payment_desc','$doc_no','$currency_id','$date','$month','$year')";			
	$result = $conn->query($sql);
	
		if($result){
		$payment_id = $conn->insert_id;
		
		$sql8123 = "SELECT * FROM collected_payments";
  $result8123 = $conn->query($sql8123);
  $num22 = mysqli_num_rows($result8123);
  //$invo_no = $count + 1;
  
  if($num22>=0 && $num22<9){
  $rec_no = $num22 + 1;
  $y = date("Y");
  $RecNo = "RECKLCL"."/".$y."/".'000'.$rec_no;  
    }else if($num22>=9 && $num22<99){
   $rec_no = $num22 + 1;
   $y = date("Y");
  $RecNo = "RECKLCL"."/".$y."/".'00'.$rec_no;  	 
	 }else if($num22>=99 && $num22<999){
   $rec_no = $num22 + 1;
   $y = date("Y");
  $RecNo = "RECKLCL"."/".$y."/".'0'.$rec_no;  	 
	 }else{
	$rec_no = $num22 + 1;
	$y = date("Y");
  $RecNo = "RECKLCL"."/".$y."/".$rec_no;  	 
	 }
		
		
		$sql2 = "INSERT INTO bank_deposits (currency_id,payment_id,payment_mode_id,client_id,inv_id,amount_paid) VALUES ('$currency_id','$payment_id','$payment_mode_id','$client_id','$inv_id','$amount_Vat')";			
	    $result2 = $conn->query($sql2);
		
		$sql3 = "UPDATE currency SET amount='$totalAmount' WHERE currency_id  = '$currency_id'";
	    $result3 = $conn->query($sql3);
		
		$sql4 = "UPDATE collected_payments SET rec_no='$RecNo' WHERE payment_id  = '$payment_id'";
	    $result4 = $conn->query($sql4);
		
		if($result2&&$result3&&$result4){
			
		ob_start();
		require("receipt10.php");
		$html = ob_get_contents();
        ob_end_clean();	
		
		
			  
		$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($client_email, $client_name);  //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 = 'Payments';   //Sets the Subject of the message
 //$mail->Body = 'Please See Attachment of your pdf invoice.';    //An HTML or plain text message body
 //$mail->Body  = 'Click On This Link to Verify Account'.$link.'';
 $mail->Body  = $msg;
 
  if($mail->Send())        //Send an Email. Return true on success or false on error
 {
	 
  
  
  $_SESSION['success_message'] = "You have successfully recorded Payments";
  header("Location: ../recordPayments.php");
  exit();

 }else{
	echo 'Error'; 
 }

		} 
			  
}

}else if($payment_mode_id==2){
	
	
    $client_id  = $_POST['client_id'];
	$currency_id  = $_POST['currency_id1'];
	$inv_id  = $_POST['inv_id'];
	$amount  = $_POST['amount1'];
	$cheque_no  = $_POST['cheque_no'];
	$upl_status  = $_POST['upl_status'];
	$payment_desc  = addslashes($_POST['payment_desc']);
	$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';	  
	  }
	
	$sql812 = "SELECT vat_settings.rate FROM vat_settings";
				$result812 = $conn->query($sql812);
				while($data812 = mysqli_fetch_array($result812))
							{
							$vat_rate = $data812['rate'];
							}
							
	$sql8122 = "SELECT client_fname,client_email FROM clients where client_id='$client_id'";
				$result8122 = $conn->query($sql8122);
				while($data8122 = mysqli_fetch_array($result8122))
							{
							$client_name = $data8122['client_fname'];
							$client_email = $data8122['client_email'];
							}
							
	$sql81221 = "SELECT * FROM currency where currency_id='$currency_id'";
				$result81221 = $conn->query($sql81221);
				while($data81221 = mysqli_fetch_array($result81221))
							{
							$currencyName = $data81221['currencyName'];
							$bankAmount = $data81221['amount'];
							}
  
  $space = " ";
  //$vat = $vat_rate/100 * $amount;
  $amount_Vat = $amount;
  
  $totalAmount = $bankAmount + $amount_Vat;
 
  $msg = "Hello Client".$space."you have paid".$space.$currencyName.$space.$amount_Vat.$space."with vat Below is attachment Of Your Receipt";
	
	$sql = "INSERT INTO collected_payments (client_id,inv_id,amount_paid,payment_mode_id,payment_desc,cheque_no,currency_id,date,month,year) VALUES ('$client_id','$inv_id','$amount','$payment_mode_id','$payment_desc','$cheque_no','$currency_id','$date','$month','$year')";			
	$result = $conn->query($sql);
	
	
	if($result){
		$payment_id = $conn->insert_id;
		
		$sql8123 = "SELECT * FROM collected_payments";
  $result8123 = $conn->query($sql8123);
  $num22 = mysqli_num_rows($result8123);
  //$invo_no = $count + 1;
  
  if($num22>=0 && $num22<9){
  $rec_no = $num22 + 1;
  $y = date("Y");
  $RecNo = "RECKLCL"."/".$y."/".'000'.$rec_no;  
    }else if($num22>=9 && $num22<99){
   $rec_no = $num22 + 1;
   $y = date("Y");
  $RecNo = "RECKLCL"."/".$y."/".'00'.$rec_no;  	 
	 }else if($num22>=99 && $num22<999){
   $rec_no = $num22 + 1;
   $y = date("Y");
  $RecNo = "RECKLCL"."/".$y."/".'0'.$rec_no;  	 
	 }else{
	$rec_no = $num22 + 1;
	$y = date("Y");
  $RecNo = "RECKLCL"."/".$y."/".$rec_no;  	 
	 }
		
		
		$sql2 = "INSERT INTO bank_deposits (currency_id,payment_id,payment_mode_id,client_id,inv_id,amount_paid) VALUES ('$currency_id','$payment_id','$payment_mode_id','$client_id','$inv_id','$amount_Vat')";			
	    $result2 = $conn->query($sql2);
		
		
		$sql3 = "UPDATE currency SET amount='$totalAmount' WHERE currency_id  = '$currency_id'";
	    $result3 = $conn->query($sql3);
		
		$sql4 = "UPDATE collected_payments SET rec_no='$RecNo' WHERE payment_id  = '$payment_id'";
	    $result4 = $conn->query($sql4);
		
		
		if($result2&&$result3&&$result4){
			
		ob_start();
		require("receipt10.php");
		$html = ob_get_contents();
        ob_end_clean();	
			  
			  
		$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($client_email, $client_name);  //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 = 'Payments';   //Sets the Subject of the message
 //$mail->Body = 'Please See Attachment of your pdf invoice.';    //An HTML or plain text message body
 //$mail->Body  = 'Click On This Link to Verify Account'.$link.'';
 $mail->Body  = $msg;
 
  if($mail->Send())        //Send an Email. Return true on success or false on error
 {
	 
  
  
  $_SESSION['success_message'] = "You have successfully recorded Payments";
  header("Location: ../recordPayments.php");
  exit();

 }else{
	echo 'Error'; 
 }		  
			  
}	
	
	}	
	
}else{
	

    $client_id  = $_POST['client_id'];
	$currency_id  = $_POST['currency_id2'];
	$inv_id  = $_POST['inv_id'];
	$amount  = $_POST['amount2'];
	$doc_no  = $_POST['bank_ref_no'];
	$upl_status  = $_POST['upl_status'];
	$payment_desc  = addslashes($_POST['payment_desc']);
	$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';	  
	  }
	
	$sql812 = "SELECT vat_settings.rate FROM vat_settings";
				$result812 = $conn->query($sql812);
				while($data812 = mysqli_fetch_array($result812))
							{
							$vat_rate = $data812['rate'];
							}
							
	$sql8122 = "SELECT client_fname,client_email FROM clients where client_id='$client_id'";
				$result8122 = $conn->query($sql8122);
				while($data8122 = mysqli_fetch_array($result8122))
							{
							$client_name = $data8122['client_fname'];
							$client_email = $data8122['client_email'];
							}
							
	$sql81221 = "SELECT * FROM currency where currency_id='$currency_id'";
				$result81221 = $conn->query($sql81221);
				while($data81221 = mysqli_fetch_array($result81221))
							{
							$currencyName = $data81221['currencyName'];
							$bankAmount = $data81221['amount'];
							}
  
  $space = " ";
  //$vat = $vat_rate/100 * $amount;
  $amount_Vat = $amount;
  
  $totalAmount = $bankAmount + $amount_Vat;
  
  
  $msg = "Hello Client".$space."you have paid".$space.$currencyName.$space.$amount_Vat.$space."with vat Below is attachment Of Your Receipt";
	
	$sql = "INSERT INTO collected_payments (client_id,inv_id,amount_paid,payment_mode_id,payment_desc,cheque_no,currency_id,date,month,year) VALUES ('$client_id','$inv_id','$amount','$payment_mode_id','$payment_desc','$doc_no','$currency_id','$date','$month','$year')";			
	$result = $conn->query($sql);
	
	
	
	
	
	
	if($result){
		$payment_id = $conn->insert_id;
		
		$sql8123 = "SELECT * FROM collected_payments";
  $result8123 = $conn->query($sql8123);
  $num22 = mysqli_num_rows($result8123);
  //$invo_no = $count + 1;
  
  if($num22>=0 && $num22<9){
  $rec_no = $num22 + 1;
  $y = date("Y");
  $RecNo = "RECKLCL"."/".$y."/".'000'.$rec_no;  
    }else if($num22>=9 && $num22<99){
   $rec_no = $num22 + 1;
   $y = date("Y");
  $RecNo = "RECKLCL"."/".$y."/".'00'.$rec_no;  	 
	 }else if($num22>=99 && $num22<999){
   $rec_no = $num22 + 1;
   $y = date("Y");
  $RecNo = "RECKLCL"."/".$y."/".'0'.$rec_no;  	 
	 }else{
	$rec_no = $num22 + 1;
	$y = date("Y");
  $RecNo = "RECKLCL"."/".$y."/".$rec_no;  	 
	 }
		
		$sql2 = "INSERT INTO bank_deposits (currency_id,payment_id,payment_mode_id,client_id,inv_id,amount_paid) VALUES ('$currency_id','$payment_id','$payment_mode_id','$client_id','$inv_id','$amount_Vat')";			
	    $result2 = $conn->query($sql2);
		
		
		$sql3 = "UPDATE currency SET amount='$totalAmount' WHERE currency_id  = '$currency_id'";
	    $result3 = $conn->query($sql3);
		
		$sql4 = "UPDATE collected_payments SET rec_no='$RecNo' WHERE payment_id  = '$payment_id'";
	    $result4 = $conn->query($sql4);
				  
			
		if($result2&&$result3&&$result4){
			
		ob_start();
		require("receipt10.php");
		$html = ob_get_contents();
        ob_end_clean();			  
			  
			  
			  
		$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($client_email, $client_name);  //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 = 'Payments';   //Sets the Subject of the message
 //$mail->Body = 'Please See Attachment of your pdf invoice.';    //An HTML or plain text message body
 //$mail->Body  = 'Click On This Link to Verify Account'.$link.'';
 $mail->Body  = $msg;
 
  if($mail->Send())        //Send an Email. Return true on success or false on error
 {
	 
  
  
  $_SESSION['success_message'] = "You have successfully recorded Payments";
  header("Location: ../recordPayments.php");
  exit();

 }else{
	echo 'Error'; 
 }		  
			  
}
	}	
	
}
?>