<?php
if(!isset($_SESSION)) {  session_start(); } 
ob_start();
if (!isset($_SESSION["user_id"])) {
    header("location:../../logout.php");
    }
if (isset($_REQUEST["cr"])) {
    $output = array();
    include "../../actions/dbPDO.php";
    include "../../op/number_text.php";

    $credit_id = $_REQUEST["cr"];

    $vat_sql = $db->prepare("SELECT * from credit_vat where credit_id = ?");
		$vat_sql->bindParam(1,$credit_id);
		$vat_sql->execute();
if($vat_sql->rowCount()>0){$sam = $vat_sql->fetch(PDO::FETCH_ASSOC);
        $output["contract_id"]    	= $sam["contract_id"];
        $output["vat_id"]    	    = $sam["vat_id"];
        $output["credit_id"]    	= $credit_id;
        $contract_id            	= $sam["contract_id"];
        $output["after_vat"] 		= $sam["after_vat"];
        $after_vat 		            = $sam["after_vat"];
        $output["vat_req"]          = $sam["vat_req"];
        $vat_req                    = $sam["vat_req"];
        $credit_money  	            = $sam["credit_money"] ;
        $output["credit_money"]  	= $sam["over_vat"]+$credit_money ;
        $output["credit_date"] 	    = $sam["credit_date"] ;
        $credit_date 	            = $sam["credit_date"] ;
        $output["comm"]   		    = $sam["comm"] ;
       
}
	$sq = $db->prepare("SELECT  contracts.cust_id2 ,contracts.cust_id , cars.kind ,
		         concat(cars.letters,' ',cars.numbers) as PL , cars.model , cars.brand
			FROM contracts ,cars WHERE contracts.carid = cars.carid AND contract_id = ? LIMIT 1") ;
		$sq->bindParam(1,$contract_id);
		$sq->execute();
	if($sq->rowCount()>0){$kok = $sq->fetch(PDO::FETCH_ASSOC);
        $output["kind"]     = $kok["kind"]; 
        $kind               = $kok["kind"]; 
        $PL                 = $kok["PL"];
        $cust_id            = $kok["cust_id"];
        $cust_id2           = $kok["cust_id2"];
		$brand              = $kok["brand"]; 
        $model              = $kok["model"] ; 
    }
   			
		$cust = $db->prepare("SELECT cust_id , idSA , cust_name ,address,cust_vat
                     FROM `customers` WHERE cust_id = ? ");
		$cust->bindParam(1,$cust_id);
		$cust->execute();
		if($cust->rowCount()>0){

            $h1 = $cust->fetch(PDO::FETCH_ASSOC);
			$output["cust_name"]  = $h1["cust_name"]; 
            $ksa1                 = $h1["idSA"];
			$output["address1"]   = $h1["address"];   
            $cust_vat             = $h1["cust_vat"];
            $output["cust_vat"]   = $h1["cust_vat"];
		}
            if(!empty($cust_vat)){
                $output["vat_cr"] = " الرقم الضريبي ".$cust_vat ." سجل تجاري ".$ksa1;
            }

		if(!empty($cust_id2)){
			$cust2 = $db->prepare("SELECT cust_id , idSA , cust_name,address FROM `customers` WHERE cust_id = ? ");
			$cust2->bindParam(1,$cust_id2);
			$cust2->execute();
			if($cust2->rowCount()>0){
                $h2 = $cust2->fetch(PDO::FETCH_ASSOC);
				$name2      = $h2["cust_name"];
                $ksa2       = $h2["idSA"];
				$address2   = $h2["address"];   
			}
		} 
        $output["cont_text"] =""       ;
		if(!empty($name2)){
                $output["name2"]        = $name2;
                $output["address2"]     = $address2;

            $output["cont_text"] = " مستأجر إضافي :  ".$name2;
        }//not empty name2

        $output["credit_desc"] = "عقد إيجار رقم".$contract_id." سيارة ".$kind." رقم ".$PL ;
        $output["money_text"] = samTXT($after_vat) ;
       
        $seller_name = "مؤسسة الوسام المعتمد لتأجير السيارات";
        $vat_registration_number ="300098532500003";
        $invoice_datetimez = $credit_date;
        $invoice_amount = $after_vat;
        $invoice_tax_amount = $vat_req ; 
        //
        function zatca_base64_tlv_encode($seller_name,
        $vat_registration_number,
         $invoice_datetimez,
          $invoice_amount, 
          $invoice_tax_amount)
       {
           $result = chr(1) . chr( strlen($seller_name) ) . $seller_name;
           $result.= chr(2) . chr( strlen($vat_registration_number) ) . $vat_registration_number;
           $result.= chr(3) . chr( strlen($invoice_datetimez) ) . $invoice_datetimez;
           $result.= chr(4) . chr( strlen($invoice_amount) ) . $invoice_amount;
           $result.= chr(5) . chr( strlen($invoice_tax_amount) ) . $invoice_tax_amount;
           return base64_encode($result);
       } //

        $master_vat = zatca_base64_tlv_encode($seller_name,
        $vat_registration_number,
         $invoice_datetimez,
          $invoice_amount, 
          $invoice_tax_amount);

    $output["master"] = $master_vat ;
    
    echo json_encode($output);
   
    // echo '<pre>'; print_r($output) ; echo '</pre>';
}
