<?php
    include_once 'opendb.php';
     $response["success"] = -1;
     $response["message"] = "Error";
     $response["client_code"] ="";   
     $response["client_name"] ="";
     $response["comp_id"] =0;
     $response["comp_name"] ="";
     $response["next_so_no"]=1;
     $response["comp_code"] ="";    
     $response["client_type"] =-1;     
     $response["supportphone"] ='';
     $response["exp_date"] ='';
     $response["isRec"] =0;
     $response["isSales"] =0;
     $response["isSO"] =0;    
     $response["isSRet"] =0;
     $response["isRequest"] =0;
     $response["isQuote"] =0;
     $response["isCost"] =0;
     $response["isPriceLock"] =1;
     $response["currency_name"] ='';
     $response["view_style"] ="1";
     $response["cust_sm_filter"]=0;
     $response["dayclose"]="No";
     $response["photo_edit_time"] =0;
     $response["win_app_name"] ="BitLink";
     $response["price_level"] =1;
     $response["price_level_opt"] ='SMAN';
     $response["price_reduction"]=0;
     
     $response["tax_system"] ="OFF";
     $response["tax_extra"] = "False";
     $response["tax_reg_no"] ='';
     $response["tax_reg_no2"] ='';
     $response["cust_creation"]=0;
     $response["comp_add1"]='';
     $response["comp_add2"]='';
     $response["comp_add3"]='';
     $response["comp_phone1"]='';
     
     $response["comp_name_ar"]='';
     $response["comp_add1_ar"]='';
     $response["comp_add2_ar"]='';
     $response["comp_add3_ar"]='';
     $response["max_uns_mob"]='0';
     $response["loc_services"] =0;
   //print_r($_POST);
     $CompID=0;
     $AppID="";
     $CoID="";
     $User_Name='';
     $isCustAccess=0;
     $Pass_Word="";
     if (isset($_POST["User_Name"]))   {
        $User_Name=$_POST["User_Name"] ;
    }
    if (isset($_POST["Pass_Word"]))   {
        $Pass_Word=$_POST["Pass_Word"] ;
    }
    if ($User_Name=='' || $Pass_Word=='') {
      $response["message"] = 'User id and password  required '; 
      $conn->close();
      die( json_encode($response));  
    }
    if (isset($_POST["AppID"]))   {
        $AppID=$_POST["AppID"] ;
    }
     if (isset($_POST["CoCode"])){	
        $CoCode=$_POST["CoCode"] ;
    }
    if($CoCode=='') {
      $response["message"] = 'Company Code required '; 
      $conn->close();
      die( json_encode($response));  
    }  
    $sql = "SELECT * FROM company WHERE  comp_code ='".$CoCode."';";    
    $sresult = $conn->query($sql);
    if ($sresult->num_rows > 0 )
    { 
        if($srow = $sresult->fetch_assoc())
            {
              $CompID=$srow["comp_id"];                   
              $response["comp_id"]=$CompID;
              $response["comp_name"]=$srow["comp_name"];
              $response["comp_code"]=$srow["comp_code"];
              $response["currency_name"] =$srow["currency_name"];
              $isCustAccess=$srow["iscust_order"];
              $response["win_app_name"] =$srow["app_name"];
              $response["slide_count"] = $srow["slide_count"];
             }
    }
    if ($CompID==0) {
        $response["message"] = "Invalid Company Code-----". $sql;
        $conn->close();
        die( json_encode($response));
    }
    
    
    $sql = "SELECT set_name,set_value FROM appsettings WHERE comp_id=".$CompID;
    $sresult = $conn->query($sql);
    if ($sresult->num_rows > 0 ){ 
        while($srow = $sresult->fetch_assoc()){
            switch ($srow["set_name"]) {
                case "SUPPORT.PHONE" :
                    $response["supportphone"] = $srow["set_value"];
                    break;
                case "CUST_SM_FILTER" :
                    if( $srow["set_value"]=="Yes"){
                          $response["cust_sm_filter"]=1;
                    }
                    break;
                case  "CUST_ADD_MOB"   :
                    if( $srow["set_value"]=="Yes"){
                      $response["cust_creation"]=1;
                    }
                    break;
                case "APPVIEW_STYLE" :
                    $response["view_style"] = $srow["set_value"];
                    break;
                case "DAYCLOSE" :
                    $response["dayclose"] = $srow["set_value"];
                    break;
                case "photo_edit_time" :
                     $response["photo_edit_time"] = $srow["set_value"];
                    break;
                case "PRICE_REDUCTION" :
                    $response["price_reduction"] = $srow["set_value"];
                    break;
                case "TAX.SYSTEM" :
                    $response["tax_system"] = $srow["set_value"];
                    break;
                case "TAX.EXTRA" :
                    $response["tax_extra"] = $srow["set_value"];
                    break;
                case "TAXREG_NO" :
                    $response["tax_reg_no"] = $srow["set_value"];
                    break;
                case "TAXREG_NO2" :
                    $response["tax_reg_no2"] = $srow["set_value"];
                    break;
                case "COMP_NAME" :
                    $response["comp_name"]=$srow["set_value"];
                    break;
                case "COMP_ADD1" :
                    $response["comp_add1"]=$srow["set_value"];
                    break;
                case "COMP_ADD2" :
                    $response["comp_add2"]=$srow["set_value"];
                    break;
                case "COMP_ADD3" :
                    $response["comp_add3"]=$srow["set_value"];
                    break;
                case "COMP_PHONE1" :
                    $response["comp_phone1"]=$srow["set_value"];
                    break;
                case "COMP_NAME_AR" :
                    $response["comp_name_ar"]=$srow["set_value"];
                    break;
                case "COMP_ADD1_AR" :
                   $response["comp_add1_ar"] =$srow["set_value"];
                    break;
                case "COMP_ADD2_AR" :
                    $response["comp_add2_ar"]=$srow["set_value"];
                    break;
                case "COMP_ADD3_AR" :
                   $response["comp_add3_ar"] =$srow["set_value"];
                    break;
                case "MAX_UNS_MOB" :
                   $response["max_uns_mob"] =$srow["set_value"];
                    break;
                case "PRICE_LEVEL_OPT" :
                   $response["prive_level_opt"] =$srow["set_value"];
                   break;
                case "LOC_SERVICES" :
                   $response["loc_services"] =$srow["set_value"];
                break;
            }
        }
    }

    
    $sql = "SELECT counter_code,inactive,client_type,app_pass,description,client_app_id,next_so_no,isrec,issales,isso,issret,isquote,ispricelock,isrequest,price_level,iscost FROM mobile_clients WHERE counter_code like '".$User_Name."'  and comp_id=".$CompID;
    $result = $conn->query($sql);
    if ($result->num_rows > 0 ) {   
            if($row = $result->fetch_assoc()){    
                if ($row["app_pass"]!=$Pass_Word){
                   $response["success"] = 0;
                   $response["client_code"] = $row["counter_code"];   
                   $response["client_name"] =$row["description"];    
                   $response["message"] = "Please Check Password for Salesman"; 
                   $response["client_type"] =$row["client_type"];
                   $response["next_so_no"] = $row["next_so_no"];
                   $conn->close();
                   die( json_encode($response));
                }
                                
                if ($row["inactive"]==0)
                {
                   $response["success"] = 1;
                   $response["client_code"] = $row["counter_code"];                       
                   $response["client_name"] =$row["description"];                      
                   $response["message"] = "active";
                   $response["client_type"] =0; // $row["client_type"];
                   $response["next_so_no"] = $row["next_so_no"];
                   $response["isRec"] =$row["isrec"];                   
                    $response["isSales"] =$row["issales"];
                    $response["isSO"] =$row["isso"];
                    $response["isSRet"] =$row["issret"];
                    $response["isRequest"] =$row["isrequest"];
                    $response["isQuote"] =$row["isquote"];
                    $response["isCost"] =$row["iscost"];
                    $response["isPriceLock"] =$row["ispricelock"];
                    $response["price_level"] =$row["price_level"];
                }
                else
                {    
                   $response["success"] = 0;
                   $response["client_code"] = $row["counter_code"];                       
                   $response["message"] = "inactive salesman";                       
                }
                 $conn->close();
                 die( json_encode($response));
            }
            
    }
    else if ($isCustAccess) {
         $sql = "SELECT sub_code,inactive,1 as client_type,sub_name,app_pass,price_level FROM subsidiary WHERE sub_code like '".$User_Name."'  and comp_id=".$CompID;
         $result = $conn->query($sql);
         if ($result->num_rows > 0 ) { 
            if($row = $result->fetch_assoc()) 
              {
                if ($row["app_pass"]!=$Pass_Word){
                   $response["success"] = 0;
                   $response["client_code"] = $row["sub_code"];  
                   $response["client_name"] =$row["sub_name"];  
                   $response["message"] = "Please Check Password for customer";                       
                   
                   $conn->close();
                   die( json_encode($response));
                }
                   $response["success"] = 1;
                   $response["client_code"] = $row["sub_code"];                       
                   $response["client_name"] =$row["sub_name"];                      
                   $response["message"] = "active";
                   $response["client_type"] = $row["client_type"];  
                   $response["isSO"] =1; 
                   $response["price_level"] =$row["price_level"];
                  if ($row["inactive"]!=0)
                  {
                    $response["message"] = "inactive"; 
                    $response["success"] = 0;
                  }
              }    
         }
         else {
            $response["success"] = -1;
            $response["message"] = "Account Not registered..!  : ".$User_Name;
         }
        
    }
    else {
          $response["success"] = -1;
          $response["message"] = "Account Not Registered..!  : ".$User_Name;
       }
    $conn->close();
    die( json_encode($response));
   
?>