<?php
    include_once 'opendb.php';
     $response["success"] = -1;
     $response["message"] = "Error";
     $response["client_code"] ="";
     $response["serial_prefix"] ="";
     $response["comp_id"] =0;
     $response["comp_name"] ="";
     $response["client_desc"] ="";
     $response["next_so_no"]=-1;
     $response["client_type"] =-1;
     $response["isSales"] =0;
     $response["isSO"] =0;
     $response["isRec"] =0;
     $response["isSRet"] =0;
     $response["isRequest"] =0;
     $response["isQuote"] =0;
     $response["supportphone"] ='';
     $response["exp_date"] ='';
   //print_r($_POST);
     $CompID=0;
    if (isset($_POST["AppID"]))
    {
	
        $AppID=$_POST["AppID"] ;
        $WebCoCode=$_POST["WebCoCode"] ;
        
        $sql = "SELECT * FROM company WHERE  comp_code like '".$WebCoCode."'";
        $sresult = $conn->query($sql);
        if ($sresult->num_rows > 0 )
        { 
               if($srow = $sresult->fetch_assoc())
                 {
                   
                   if($srow['expiry_date']!=''){
                        $expiry_date = date('Y-m-d', strtotime($srow['expiry_date']));
                        $today=date("Y-m-d");
                        if($expiry_date < $today){
                            $response["message"] = 'company status expired '; 
                             $conn->close();
                             die( json_encode($response));
                        }

                   }
                   
                   $CompID=$srow["comp_id"];                   
                   $response["comp_id"]=$CompID;
                   $response["comp_name"]=$srow["comp_name"];
                  }
        }
        if ($CompID==0) {
            $response["message"] = "Invalid Web Code";
            $conn->close();
            die( json_encode($response));
        }
        
        
        $sql = "SELECT set_value FROM appsettings WHERE set_category='GENERAL' and set_name like 'SUPPORT.PHONE' and comp_id=".$CompID;
        $sresult = $conn->query($sql);
        if ($sresult->num_rows > 0 )
        { 
               if($srow = $sresult->fetch_assoc())
                 {
                    $response["supportphone"] = $srow["set_value"];
                  }
        }
            
        $sql = "SELECT counter_code,next_so_no,inactive,issales,isso,isrec,issret,isrequest,isquote,client_type,description,serial_prefix,exp_date FROM mobile_clients WHERE client_app_id like '".$AppID."'  and comp_id=".$CompID;
        $result = $conn->query($sql);
        
        if ($result->num_rows > 0 ) {      
       
        
                if($row = $result->fetch_assoc()) 
                {
                
                    if ($row["inactive"]==0)
                    {
                       $response["success"] = 1;
                       $response["client_code"] = $row["counter_code"];
                       $response["serial_prefix"] =$row["serial_prefix"];
                       $response["client_desc"] =$row["description"];
                       $response["next_so_no"] = $row["next_so_no"];
                       $response["message"] = "active";
                       $response["client_type"] = $row["client_type"];
                       $response["isSales"] =$row["issales"];
                       $response["isSO"] =$row["isso"];
                       $response["isRec"] =$row["isrec"];
                       $response["isSRet"] =$row["issret"];
                       $response["isRequest"] =$row["isrequest"];
                       $response["isQuote"] =$row["isquote"];
                       $response["exp_date"] =$row["exp_date"];
                                             
                        if($row['exp_date']!='')
                        {
                            $expiry_date = date('Y-m-d', strtotime($row['exp_date']));
                            $today=date("Y-m-d");
                            if($expiry_date < $today){
                                $response["success"] = 0;                      
                                $response["message"] = "client licence expired";
                            }
                        }
                       
                       $sql = "SELECT set_value FROM appsettings WHERE set_category='GENERAL' and set_name like 'TAX.SYSTEM'  and comp_id=".$CompID;
                       
                       
                       $sresult = mysqli_query($conn,$sql);
                       
                      
                       if ($sresult->num_rows > 0 ) 
                         
                        {  
                        
                                if($srow = $sresult->fetch_assoc())
                                {
                                $response["taxsystem"] = $srow["set_value"];
                               }
                        }
                        
                        
                        $sql = "SELECT set_value FROM appsettings WHERE set_category='GENERAL' and set_name like 'TAX.EXTRA'  and comp_id=".$CompID;
                       $sresult = $conn->query($sql);
                       if ($sresult->num_rows > 0 )
                        
                        { 
                       
                                if($srow = $sresult->fetch_assoc())
                                {
                                $response["taxextra"] = $srow["set_value"];
                               }
                        }
                        
                      
                    }
                    else
                    {
                           
                       $response["success"] = 0;
                       $response["client_code"] = $row["counter_code"];
                       $response["next_so_no"] = $row["Next_So_No"];
                       $response["message"] = "inactive";
                       $response["isSales"] =$row["issales"];
                       $response["isSO"] =$row["isso"];
                       $response["isRec"] =$row["isrec"];    
                       $response["isSRet"] =$row["issret"];
                       $response["isRequest"] =$row["isrequest"];
                       $response["isQuote"] =$row["isquote"];
                       $response["exp_date"] =$row["exp_date"];
                    }
                }
                
        }
        else {
                  $response["success"] = -1;
                  $response["message"] = "client Not registered..!  : ".$AppID;
        }
        
  
    }
    else
    {
        $response["message"] = "No Data for app id";
    }
    
    $conn->close();
    echo json_encode($response);
?>