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

    $in_id = $_REQUEST["sn"];
    
    $sanad = $db->prepare("SELECT * FROM invoice where in_id = ?");
    $sanad->bindParam(1, $in_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["in_id"]         = $c_row["in_id"];
        $output["contract_id"]   = $c_row["contract_id"];
        $output["printed_no"]    = $c_row["printed_no"];
        $output["sanad_date"]    = $c_row["sanad_date"];
        $output["money_in"]      = $c_row["money_in"];
        $money_in                = $c_row["money_in"];
        $pay                     = $c_row["payMethod"];
        $output["payFor"]        = $c_row["payFor"];
        
        $comm          = $c_row["comm"];
        if(empty($comm)){
            $output["comm"] =  "" ;
        }else{
            $output["comm"] =  $comm ;
        }
    }
    $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"];
        $output["model"]  = $row["model"];
        $PL           = $row["PL"];
    }
    // custmoer hero data
    $c1 = $db->prepare("SELECT cust_name ,lic_expire  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"];
        $output["lic_expire"]      = $rc["lic_expire"];
        }
       
    // 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 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"];
         }
// Sanad Branch 
         $s_brnach = $db->prepare("SELECT b_name , address,phone FROM branches
          WHERE branchID = ? LIMIT 1");
         $s_brnach->bindParam(1,$sanad_branch);
         $s_brnach->execute();
     if($s_brnach->rowCount()>0){$bra = $s_brnach->fetch(PDO::FETCH_ASSOC);
         $output["sanad_branch"] 	  = $bra["b_name"];
         $output["address"]           = $bra["address"];
         $output["phone"]             = $bra["phone"];
          }
         switch ( $pay) {
            case '1':      $payMethod  = "كاش";         break;
            case '2':      $payMethod  = "تحويل الراجحي";  break;
            case '3':      $payMethod  = "نقاط بيع";    break;
            case '4':      $payMethod  = "فواتير";      break;
            case '5':      $payMethod  = "تحويل الاهلي";      break;
        }
        $output["money_text"] = samTXT($money_in) ;
        $output["payMethod"] = $payMethod ;
        $output["comm_text"] = "دفعة إيجار ".$brand.' '.$kind." لوحة رقم ".$PL ;
         echo json_encode($output);
        // echo $output;
        //  echo '<pre>'; print_r($output) ; echo '</pre>';
}