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

    $sanad = $db->prepare("SELECT * FROM deduction where de_id = ?");
    $sanad->bindParam(1, $de_id);
    $sanad->execute();
if ($sanad->rowCount() > 0) { $c_row       = $sanad->fetch(PDO::FETCH_ASSOC);
        $contract_id       = $c_row["contract_id"];
        $father            = $c_row["father"];
        $sanad_branch      = $c_row["branch"];

        $output["de_id"]        = $c_row["de_id"];
        $output["contract_id"]  = $c_row["contract_id"];
        $output["sanad_date"]   = $c_row["sanad_date"];
        $output["money_de"]     = $c_row["money_de"];
        $money_de               = $c_row["money_de"];
        
        $description          = $c_row["description"];
        if(empty($description)){
            $output["description"] =  "" ;
        }else{
            $output["description"] =  $description ;
        }
    }
    $cont = $db->prepare("SELECT * FROM contracts where contract_id = ?");
    $cont->bindParam(1, $contract_id);
    $cont->execute();
if ($cont->rowCount() > 0) { $c_row       = $cont->fetch(PDO::FETCH_ASSOC);
        $carid       = $c_row["carid"];
        $cust_id     = $c_row["cust_id"];
        $branch      = $c_row["branch"];
    }
    // car data 
    $car = $db->prepare("SELECT brand, kind , model , concat(letters,' ', numbers) as PL, color 
                FROM cars  WHERE carid = ? LIMIT 1");
    $car->bindParam(1, $carid);
    $car->execute();
    if ($car->rowCount() > 0) {
        $row = $car->fetch(PDO::FETCH_ASSOC);
            $brand      = $row["brand"];
            $kind       = $row["kind"];
            $model      = $row["model"];
            $PL         = $row["PL"];
    }
    // custmoer hero data
    $c1 = $db->prepare("SELECT cust_name FROM customers WHERE cust_id = ? LIMIT 1");
	$c1->bindParam(1,$cust_id);
	$c1->execute();
  if($c1->rowCount() > 0 ){
                $rc = $c1->fetch(PDO::FETCH_ASSOC);
        $output["cust_name"]       = $rc["cust_name"];
        }    
    // branch and emp data 
        $emp = $db->prepare("SELECT users.realName   FROM  users 
                    WHERE user_id = ? LIMIT 1");
        $emp->bindParam(1,$father);
	    $emp->execute();
    if($emp->rowCount()>0){$mass = $emp->fetch(PDO::FETCH_ASSOC);
        $output["boss"]          = $mass["realName"];
         }
 //  contract branch 
        $emp2 = $db->prepare("SELECT b_name , address,phone FROM branches WHERE branchID = ? LIMIT 1");
        $emp2->bindParam(1,$branch);
	    $emp2->execute();
    if($emp2->rowCount()>0){$m2 = $emp2->fetch(PDO::FETCH_ASSOC);
        $output["contract_branch"] 	 = $m2["b_name"];
        $output["address"]           = $m2["address"];
        $output["phone"]             = $m2["phone"];
         }
        $output["money_text"] = samTXT($money_de) ;
        $output["comm_text"] = " خصم من عقد إيجار سيارة ".$brand.' '.$kind." لوحة رقم ".$PL. " موديل ".$model;
         echo json_encode($output);
        // echo $output;
        //  echo '<pre>'; print_r($output) ; echo '</pre>';
}