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


  $payment_mode_id  = $_POST['payment_mode_id'];
  
  $payment_id = $_POST['payment_id'];
  //OLD
  $currencyId  = $_POST['currencyId'];
  //NEW
  $currency_id  = $_POST['currency_id'];
  
  $inv_id = $_POST['inv_id'];
  $upl_status  = $_POST['upl_status'];
  $client_id = $_POST['client_id'];
  $payment_desc = addslashes($_POST['payment_desc']);
  
  if($payment_mode_id==1){
  $doc_no  = $_POST['bank_slip_no']; 
  }else if($payment_mode_id==2){
  $doc_no  = $_POST['cheque_no'];  
  }else{
  $doc_no  = $_POST['bank_ref_no']; 
  }
  
  
  
  //new
  $amount = $_POST['amount'];
  
  //old
  $amountPaid = $_POST['amountPaid'];
  
  $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'];
							}
							
 
							
   if($currencyId!=$currency_id){
	   
	 $sql81221 = "SELECT * FROM currency where currency_id='$currencyId'";
				$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;
  
  $RAmount1 = $bankAmount - $amountPaid;
  
  $sql7 = "UPDATE currency SET amount = '$RAmount1' WHERE currency_id = {$currencyId}";		

  $result7 = $conn->query($sql7);
  
  if($result7){
	  
	  $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'];
							}
							
  //update bank
  $tAmount = $amount_Vat + $bankAmount;  
	  
      }
      }else{
		  
	$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;
  
  $RAmount = $bankAmount - $amountPaid;
  
  //update bank
  $tAmount = $amount_Vat + $RAmount;
		  
	  }
							
   
							
  $msg = "Hello Client".$space."you have paid".$space.$currencyName.$space.$amount_Vat.$space."with vat Below is attachment Of Your Receipt";
  
  $sql = "UPDATE collected_payments SET client_id = '$client_id',inv_id = '$inv_id',amount_paid='$amount',payment_mode_id='$payment_mode_id',payment_desc='$payment_desc',cheque_no='$doc_no',currency_id='$currency_id' WHERE payment_id = {$payment_id}";		

  $result = $conn->query($sql);
  
  
  $sql2 = "UPDATE bank_deposits SET amount_paid='$amount' WHERE payment_id  = '$payment_id'";
  $result2 = $conn->query($sql2);
  
  
  $sql3 = "UPDATE currency SET amount='$tAmount' WHERE currency_id  = '$currency_id'";
  $result3 = $conn->query($sql3);
  
  if($result&&$result2&&$result3){
	  
	 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 Updated Payments";
  header("Location: ../editPayment.php");
  exit();

 }else{
	echo 'Error'; 
 }
		
		
	
	}

} 