<?php 
 
// array for JSON response
$response = array();
 
 require_once __DIR__ . '/catchexception.php';
// include db connect class
require_once __DIR__ . '/db_config.php';
  
 
$paraimeino = urldecode($_POST['paraimeino']);
 // $paraimeino ='f4f8ffbef470be87'; 
 try{
if ($paraimeino !="" ) {
    
    // get a product from products table
   /* $paracountqry="SELECT count(*) as countval from tblsyncdetails where imeino=$1 and synctype='branchmaster';";
  $resultmax=  pg_prepare($con,"paracountqry",$paracountqry);
 $data = [$paraimeino];
 $res = pg_execute($con,"paracountqry",$data);
  if (!empty($res)) {
    if (pg_num_rows($res) > 0) {   
      while ($row = pg_fetch_assoc($res)) {
          $countval= $row["countval"] ;
        }
      }
    }

    if ($countval > 0){
      $paraquery="SELECT * from (select *,'Insert' as  process  from tblbranchmaster where (select syncdate from tblsyncdetails where imeino=$1 and synctype='branchmaster' order by syncdate desc limit 1 ) <= createddate and statuscode='1'
      union all  select *,'Update' as process from tblbranchmaster where (select syncdate from tblsyncdetails where imeino=$2 and synctype='branchmaster' order by syncdate desc limit 1 ) <=updateddate  
      ) as dev;";
      $resultmax=  pg_prepare($con,"paraquery",$paraquery);
      $data = [$paraimeino,$paraimeino];
      $result = pg_execute($con,"paraquery",$data);
    }
    else{
      $paraquery="SELECT *,'Insert' as  process  from tblbranchmaster where  statuscode='1'  "; 
      $result = pg_query($con,$paraquery );
    } */
    $paraquery="SELECT autonum,branchcode,coalesce(branchname,'') as branchname,coalesce(branchnameintamil,'') as branchnameintamil,coalesce(statuscode,0) as statuscode,coalesce(enablediscount,false)::text as enablediscount,createddate,updateddate,makerid,'Insert' as  process , coalesce(kot_enabled,'no') as kot_enabled,coalesce(enabledayclose,'no') as enabledayclose,coalesce(tenderid,'') as tenderid,coalesce(posid,'') as posid,coalesce(payment_type_popup,'yes') as payment_type_popup,coalesce(auto_sync,'no') as auto_sync,coalesce(enable_usbprinter,'no') as enable_usbprinter,coalesce(print_logo,'no') as print_logo from tblbranchmaster where branchcode in (select branchcode from tblbranchdevicemapping where deviceid = '$paraimeino') and statuscode='1'  "; 
      $result = pg_query($con,$paraquery );
    if (!empty($result)) {
        // check for empty result
        if (pg_num_rows($result) > 0) {
      // user node
            $response["Value"] = array();
 
            while ($row = pg_fetch_assoc($result)) {        
              $report = array(); 
                $report["autonum"] = $row["autonum"];
                $report["branchcode"] = $row["branchcode"];
                $report["branchname"] = $row["branchname"];
                $report["branchnameintamil"] = $row["branchnameintamil"];
                $report["statuscode"] = $row["statuscode"];
                $report["enablediscount"] = $row["enablediscount"];
                $report["createddate"] = $row["createddate"];
                $report["updateddate"] = $row["updateddate"]; 
                $report["makerid"] = $row["makerid"];  
                $report["process"] = $row["process"];
                $report["kot_enabled"] = $row["kot_enabled"];
                $report["enabledayclose"] = $row["enabledayclose"];
                $report["tenderid"] = $row["tenderid"];
                $report["posid"] = $row["posid"];
                $report["payment_type_popup"] = $row["payment_type_popup"];
                $report["auto_sync"] = $row["auto_sync"];
                $report["enable_usbprinter"] = $row["enable_usbprinter"];
                $report["print_logo"] = $row["print_logo"];
           
              // push single product into final response array
              array_push($response["Value"], $report);
            }
            // success
            $response["success"] = 1;
 
            // echoing JSON response
            echo  json_encode($response,JSON_UNESCAPED_UNICODE);


        } else {
            // no user found
            $response["success"] = 0;
            $response["message"] = "No Report found";
 
            // echo no users JSON
            echo json_encode($response);
        }
    } else {
        // no user found
        $response["success"] = 0;
        $response["message"] = "No found";
 
        // echo no users JSON
        echo json_encode($response);
    }
} else {
    // required field is missing
    $response["success"] = 0;
    $response["message"] = "Required field(s) is missing";
 
     echo  json_encode($response,JSON_UNESCAPED_UNICODE);
  }
  mch_db_release($con);
}
//catch exception
catch(Exception $e) {
mch_db_release($con);
echo 'Message: ' .$e->getMessage();
}
?>
