<?php 
//use Dompdf\Dompdf;
//use PHPMailer\PHPMailer\PHPMailer;
//use PHPMailer\PHPMailer\Exception;
//require __DIR__.'/vendor/autoload.php';	
session_start();
require_once '../../Server/connection.php';
if($_POST){	
  $inv_id = $_POST['inv_id'];
  $client_id = $_POST['client_id'];
  $currency_id = $_POST['currency_id'];
  $orderNo = $_POST['orderNo'];
  $payment_mode_id = $_POST['payment_mode_id'];
  $inv_date = $_POST['inv_date'];
  $due_date = $_POST['due_date'];
  
  $InvoiceDate = date('Y-m-d', strtotime($inv_date));
  $dueDate = date('Y-m-d', strtotime($due_date));
  
 
  $subTotalValue  = $_POST['subTotalValue'];
  
  $sql812 = "SELECT vat_settings.rate FROM vat_settings";

				$result812 = $conn->query($sql812);
				//$data81 = $result81->fetch_row();
				
				while($data812 = mysqli_fetch_array($result812))
							{
							$vat_rate = $data812['rate'];
							}
  
  $vat = $vat_rate/100 * $subTotalValue;
  $grand_total_Value = $subTotalValue + $vat;
  
  

    $sql = "UPDATE invoices SET client_id = '$client_id', currency_id='$currency_id', payment_mode_id='$payment_mode_id',inv_date = '$InvoiceDate',due_date='$dueDate',orderNo='$orderNo',total_amount='$subTotalValue',vat='$vat',grand_total_amount='$grand_total_Value',vat_rate='$vat_rate' WHERE inv_id = {$inv_id}";		

	$result = $conn->query($sql);
	
	if($result){
	
	for($x = 0; $x < count($_POST['measure_id']); $x++) {			
		$removeInvSql = "DELETE FROM invoices_items WHERE inv_id = {$inv_id}";
		$result1 = $conn->query($removeInvSql);	
	}
	
	}
	

    if($result1) {
		
	for($x = 0; $x < count($_POST['measure_id']); $x++) {		
	$invItemSql = "INSERT INTO invoices_items (inv_id,services_desc,measure_id,qty,unit_price,amount) 
				VALUES ('$inv_id','".$_POST['serv_desc'][$x]."','".$_POST['measure_id'][$x]."','".$_POST['quantity'][$x]."','".$_POST['rate'][$x]."','".$_POST['totalValue'][$x]."')";

	$result11 = $conn->query($invItemSql);
		
	}
	}
	
	if($result11){
		
		
			//ob_start();
		//require("../invoice2.php");
		//$html = ob_get_contents();
        //ob_end_clean();	
		
		
		//$dompdf = new DOMPDF();
        //$dompdf->load_html($html);
        //$dompdf->set_paper("letter","portrait");	
        //$dompdf->render();
		//$filename = 'ClientInvoice.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 = 'Client Invoice';   //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 attachment of your pdf invoice'.'&nbsp&nbsp'.$link;
 //$mail->Body  = 'Please attachment of your pdf invoice';
 //$mail->Body  = $message;
 
 
 //if($mail->Send())        //Send an Email. Return true on success or false on error
 //{
	 
 
  
  $_SESSION['success_message'] = "Invoice has been Updated Successfully";
  header("Location: ../editInvoice.php?i=$inv_id");
  exit();
  
     
  
 //}	
		
	
}

} // /if $_POST
// echo json_encode($valid);