<?php
session_start();
$currentBaseDir = dirname($_SERVER['PHP_SELF']);
include($_SESSION['basedir']."core.php");

$svaTool = new svaTool();

//print_r($_REQUEST);


$errorCode = 0;

$password = NULL;
$confPasswd = NULL;
// Ajout de nouvel utilisateur

if (isset($_REQUEST['callReasonType']) && $_REQUEST['callReasonType'] != "-1")
    $callReasonType = addslashes($_REQUEST['callReasonType']);
else
    $errorCode = 1;


if (isset($_REQUEST['csrDetails']) && strlen($_REQUEST['csrDetails'])>0 )
    $csrDetails = addslashes($_REQUEST['csrDetails']);
else
    $errorCode = 2;

if (isset($_REQUEST['csrComment']) && strlen($_REQUEST['csrComment'])>0 )
    $csrComment = addslashes($_REQUEST['csrComment']);
else
    $errorCode = 3;
	
	
	
if (isset($_REQUEST['mobileNumber']) && strlen($_REQUEST['mobileNumber'])>0 && is_numeric($_REQUEST['mobileNumber']) ){
   $mobileNumber = addslashes($_REQUEST['mobileNumber']); 
   $mobilePhone =  $mobileNumber ;
}
else
    $errorCode = 4;
	

if (isset($_REQUEST['firstName']) && strlen($_REQUEST['firstName'])>0 )
    $firstName = addslashes($_REQUEST['firstName']);
else
    $errorCode = 5;
	
if (isset($_REQUEST['company']) && strlen($_REQUEST['company']) >0 )
    $company = addslashes($_REQUEST['company']);
else
    $errorCode = 6;
	
if (isset($_REQUEST['emailId']) && strlen($_REQUEST['emailId'])>0 )
    $emailId = addslashes($_REQUEST['emailId']);
else
    $errorCode = 7;
	
if (isset($_REQUEST['customerType']) && $_REQUEST['customerType'] != "-1")
    $customerType = addslashes($_REQUEST['customerType']);
else
    $errorCode = 9;
/*		
if (isset($_REQUEST['csrComment']) && strlen($_REQUEST['csrComment'])>0 )
    $csrComment = addslashes($_REQUEST['csrComment']);
else
    $errorCode = 3;


if (isset($_REQUEST['emailId']) && strlen($_REQUEST['csrComment'])>0 )
    $csrDetails = addslashes($_REQUEST['csrComment']);
else
    $errorCode = 4;
	*/


//$csrId = addslashes($_REQUEST['csrId']);
//$callerId = addslashes($_REQUEST['callerId']);

 
$lastName = addslashes($_REQUEST['lastName']);
$callType = addslashes($_REQUEST['callType']);


//$mobileNumber = addslashes($_REQUEST['mobileNumber']);


//$callId = addslashes($_REQUEST['callId']);
$userId = $_SESSION['ownerId'];
$statut = addslashes($_REQUEST['operationId']);
$actionType = addslashes($_REQUEST['actionType']);

$companyPart = explode("[", $company);

$company = str_replace("]","",$companyPart[1]);


if ($errorCode == 0) {
$id = 1;
$field[0] = date('Y-m-d H:i:s');
$field[1] = '';
$field[2] = '';
$field[3] = '';
$field[4] = '';
$csrId = $dba->getCSRID("crm");
$callId = $mobilePhone ;
$callerId = $callId;

$res = $dba->UpdateAddTicket($id, $firstName, $lastName, $callId, $userId, $csrId, $callerId, $company, $company, $mobileNumber, $emailId, $callReasonType, $csrDetails,$callType, $actionType,$customerType) ;
/*
$res = $dba->UpdateAddTicket($id, $firstName, $lastName, $callId, $userId, $csrId, $callerId, $address,$mobileNumber, $emailId, $callReasonType, $csrDetails, $callType, $actionType) ; */

$customerName = $firstName." ".$lastName ;
$dba->UpdateAddSCCCustomer(0,0,$firstName, $lastName, $mobileNumber, "", $address, $emailId,$field);
$dba->UpdateAddComment($id, $userId, $csrId, $csrDetails, 0, $customerName,0 );
sleep(1);
$dba->UpdateAddComment($id, $userId, $csrId, $csrComment, 1, $customerName,0 );

$dba->UpdateCallHistory($callId, $statut );

   //$errorCode = 10;
}




switch ($errorCode) {

    case 0 : $msg = "{ \"errorCode\": $errorCode,\"errorMessage\" :\"TICKET CREE AVEC SUCCES !\"  }"; 
        break;
    case 1 : $msg = "{ \"errorCode\": $errorCode,\"errorMessage\" :\"ERREUR ! VEUILLEZ SELECTIONNER LA CATEGORIE DU MOTIF D'APPEL !\"  }";
        break;
    case 2 : $msg = "{ \"errorCode\": $errorCode,\"errorMessage\" :\"ERREUR ! VEUILLEZ DETAILLER LA REQUETE DU CLIENT !\"  }"; 
        break;
    case 3 : $msg = "{ \"errorCode\": $errorCode,\"errorMessage\" :\"ERREUR ! VEUILLEZ INSERER UN COMMENTAIRE SUR LE TICKET.\"  }"; 
        break;
    case 4 : $msg = "{ \"errorCode\": $errorCode,\"errorMessage\" :\"ERREUR ! VEUILLEZ ENTRER LE NUMERO DE L'APPELANT.\"  }";
        break;
    case 5 : $msg = "{ \"errorCode\": $errorCode,\"errorMessage\" :\"ERREUR ! LE NOM  EST INVALIDE.\"  }"; 
        break;
    case 6 : $msg = "{ \"errorCode\": $errorCode,\"errorMessage\" :\"ERREUR ! L'ADRESSE  EST INVALIDE.\"  }";
        break;
    case 7 : $msg = "{ \"errorCode\": $errorCode,\"errorMessage\" :\"ERREUR ! L'EMAIL  EST INVALIDE.\"  }";
        break;
		case 9 : $msg = "{ \"errorCode\": $errorCode,\"errorMessage\" :\"ERREUR ! VEUILLEZ CHOISIR LE TYPE DU CLIENT!\"  }";
        break;
		/*
    case 8 : $msg = "{ \"errorCode\": $errorCode,\"errorMessage\" :\"ERREUR ! LE MOT DE PASSE DE CONFIRMATION EST INVALIDE .!\"  }"; 
        break;
    case 9 : $msg = "{ \"errorCode\": $errorCode,\"errorMessage\" :\"ERREUR ! LES MOTS DE PASSE NE CONCORDENT PAS.\"  }";
        break; */
    case 13 : $msg = "{ \"errorCode\": $errorCode,\"errorMessage\" :\"ERREUR ! IMPOSSIBLE DE CREER LE COMPTE. VEUILLEZ RE-ESSAYER ULTERIEUREMENT!\"  }";  break;
	/*
		case 14 : $msg = "{ \"errorCode\": $errorCode,\"errorMessage\" :\"ERREUR ! VEUILLEZ SELECTIONNER LE PLAN TARIFAIRE ASSOCIE AU COMPTE !\"  }";   break; */
   
	 
}


echo $msg ;


?>