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

    $vat_id = $_REQUEST["vt"];

    $vat_sql = $db->prepare("SELECT * from vat where vat_id = ?");
		$vat_sql->bindParam(1,$vat_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"]    	    = $vat_id;
        $contract_id            	= $sam["contract_id"];
        $output["after_vat"] 		= $sam["after_vat"];
        $after_vat 		            = $sam["after_vat"];
        $output["cont"]			    = $sam["cont_status"] ;
        $output["vat_req"]          = $sam["vat_req"];
        $vat_req                    = $sam["vat_req"];
        $output["re_deduction"]  	= $sam["deduction"] ;
        $output["re_days_cost"] 	= $sam["days_cost"] ;
        $output["start"]   		    = $sam["start"] ;
        $start     		            = $sam["start"] ;
        $end 			            = $sam["end"] ;
        $output["duedate"]		    = $sam["duedate"] ;
        $duedate		            = $sam["duedate"] ;
        $dateCreate                 = $sam["date_create"] ;
        $dateCreate = new DateTime($dateCreate);
			$dateCreate =  $dateCreate->format("d-m-Y");
       
        $days_qty   	            = $sam["days_qty"] ;
        $day_cost		            = $sam["day_cost"];
        $output["days_qty"]   	    = $sam["days_qty"] ;
        $output["day_cost"]		    = $sam["day_cost"];
        $output["re_kilos_cost"] 	= $sam["kilos_cost"];
        $output["re_kilo_qty"]	    = $sam["kilo_qty"];
        $kilo_qty	                = $sam["kilo_qty"];
        $output["re_hours_cost"] 	= $sam["hours_cost"] ;
        $hour_qty		            = $sam["hour_qty"] ;
        $output["re_addtion_cost"]  = $sam["addtion_cost"];
        $output["addtion_desc"]     = $sam["addtion_desc"];
        $output["re_violations"] 	= $sam["violations"] ;
        $output["re_net_cost"]   	= $sam["net_cost"] ;
        $re_net_cost               	= $sam["net_cost"] ;
}
	$sq = $db->prepare("SELECT contracts.date_out ,contracts.branch ,contracts.cont_status, contracts.date_in ,contracts.kilo_cost,contracts.hour_cost,
		 		contracts.cust_id2 ,contracts.cust_id ,contracts.time_out,contracts.time_in, 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"] ; 
        $date_out           = $kok["date_out"] ;
		$date_in            = $kok["date_in"] ;
        $time_out           = $kok["time_out"] ;
		$time_in            = $kok["time_in"] ;
        $branch             = $kok["branch"] ;
		$kilo_cost			= $kok["kilo_cost"];
		$hour_cost			= $kok["hour_cost"];
    }
    $date_out = new DateTime($date_out);
	$date_out =  $date_out->format("d-m-Y");
     $time1 = $time_out;

     if(!empty($date_in) || ($date_in == $end)){
        $time1 = $time_in;
     }
		if(empty($date_in)){
			$date_in = date("Y-m-d");
						}
			
		$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);
			$name1      = $h1["cust_name"];  $ksa1       = $h1["idSA"];
			$address1   = $h1["address"];    $cust_vat1  = $h1["cust_vat"];

            // echo($name1);
		}
            $output["name1"]      = $name1;
            $output["vat_cr"]     = 1;
            $output["address1"]     = $address1;
            if(!empty($cust_vat1)){
                $output["vat_cr"] = " الرقم الضريبي ".$cust_vat1 ." سجل تجاري ".$ksa1;
            }
            
            $output["cust_vat1"]    = $cust_vat1;
            $output["cont_text"] = "عقد إيجار ".$contract_id." بتاريخ ".$date_out;
		if(!empty($cust_id2)){
			$cust2 = $db->prepare("SELECT cust_id , idSA , cust_name,address,cust_vat 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"];   $cust_vat2  = $h2["cust_vat"];
			}
		}
                
		if(!empty($name2)){
                $output["name2"]        = $name2;
                $output["address2"]     = $address2;
                $output["cust_vat2"]    = $cust_vat2;

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

        $date1 = new DateTime($dateCreate);
        $date2 = new DateTime($end);
       if( $date1  > $date2  ){
        $output["end"]  = $end ;
        $last_end       =  $end;

       } else{
        $dateCreate      = new DateTime($dateCreate);
	    $dateCreate      =  $dateCreate->format("Y-m-d");
         $output["end"]  = $dateCreate;
         $last_end       =  $dateCreate;
        
        }

            $start = new DateTime($start);
            $start =  $start->format("d-m-Y");
            $end = new DateTime($end);
            $end =  $end->format("d-m-Y");
        $output["vat_text"] = " سيارة ".$kind." رقم ".$PL." فترة إستحقاق من ".$start ." إلي ".$end ;
        $output["day_text"] = "قيمة إيجار عدد $days_qty  يوم بقيمة إيجار يومي  $day_cost ريال";
        $output['km_text'] = "وذلك قيمة عدد  $kilo_qty كيلو متر زيادة بسعر الكيلو $kilo_cost هللة";
        $output['hours_text'] = "قيمة ساعات إضافية لعدد  $hour_qty  ساعة ";
        $output["money_text"] = samTXT($after_vat) ;
        $output["time1"] = $time1 ;
        //  echo json_encode($output);
        // echo $output;
        $seller_name = "مؤسسة الوسام المعتمد لتأجير السيارات";
        $vat_registration_number ="300098532500003";
        // $invoice_datetimez = $last_end." ".$time1;
        $invoice_datetimez = $duedate ;
        $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>';
}
