<?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;
$status = 0;
// Ajout de nouvel utilisateur
if (isset($_REQUEST['lastName']) && strlen($_REQUEST['lastName']) > 0)
    $lastName = addslashes($_REQUEST['lastName']);
else
    $errorCode = 1;
if (isset($_REQUEST['firstName']) && strlen($_REQUEST['firstName']) > 0)
    $firstName = addslashes($_REQUEST['firstName']);
else
    $errorCode = 2;

if ((isset($_REQUEST['mobilePhone1']) && is_numeric($_REQUEST['mobilePhone1'])) || $_REQUEST['mobilePhone1'] == "")
    $mobilePhone1 = addslashes($_REQUEST['mobilePhone1']);
else
    $errorCode = 3;
	
if ((isset($_REQUEST['mobilePhone2']) && is_numeric($_REQUEST['mobilePhone2'])) || $_REQUEST['mobilePhone2'] == "")
    $mobilePhone2 = addslashes($_REQUEST['mobilePhone2']);


if (isset($_REQUEST['ticketReasonCategory']) && $_REQUEST['ticketReasonCategory'] != "-1")
    $ticketReasonCategory = addslashes($_REQUEST['ticketReasonCategory']);
else
    $errorCode = 4;
	
if (isset($_REQUEST['ticketReason']) && $_REQUEST['ticketReason'] != "-1")
    $ticketReason = addslashes($_REQUEST['ticketReason']);
else
    $errorCode = 5;

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

if (isset($_REQUEST['email']) && strlen($_REQUEST['email']) > 0 && $svaTool->regularEmailCheck($_REQUEST['email']))
    $email = addslashes($_REQUEST['email']);
else
    $errorCode = 7;
	
if (isset($_REQUEST['gender']) && strlen($_REQUEST['gender']) != "-1" )
    $gender = addslashes($_REQUEST['gender']);
else
    $errorCode = 8;

	
	if (isset($_REQUEST['ticketReasonDesc']) && strlen($_REQUEST['ticketReasonDesc']) > 0)
    $ticketReasonDesc = addslashes($_REQUEST['ticketReasonDesc']);


  if (isset($_REQUEST['ticketStatus'])) {
	  
	   $status = 1;
	  
  }



//if (isset($_REQUEST['tariffId']) &&  $_REQUEST['tariffId'] !=-1) $tariffId = $_REQUEST['tariffId'] ;else $errorCode = 14;



if ($errorCode == 0) {
	$appName = "crm";
	$ticketNumber = $dba->getSequenceNumber($appName) ;
	
	
	$res =  $dba->crmNewTicket($ticketNumber, $_SESSION['ownerId'], $firstName, $lastName, $email, $mobilePhone1, $mobilePhone2, $ticketReasonCategory, $ticketReason, $localisation, $gender, $ticketReasonDesc, $status) ;
/*
  $userId = $dba->getSequenceNextValue("MASTER_USERS_SEQ") ;

  $res =  $dba->addUserShort($userId, $login, $password, $firstName, $lastName, $profileId, $mobilePhone, $email, $_SESSION['ownerId'], $_REQUEST['fallback'] );
   
   if ($res > 0) 
      { 
      $dba->UserAddTarifPLan($tariffId, $res, 0);
	  $dba->addNewApi($res , "",  "", "", md5($login), "127.0.0.1") ;
	  $dba->InsertUpdateUserSmpp($res, "127.0.0.1", 14089, 14089, 1, null, null, null, null, 34, 10, "ISO-8859-1", 10, 1, 0) ;
	  
	  if(isset($_REQUEST['defautltSmscId'])  && $_REQUEST['defautltSmscId'] !=-1)  {
	  
	     $dba->InsertUpdateUserSmppRouting($res, $_REQUEST['defautltSmscId'], 0) ;
	    
	  }
   
    }
	  else {
	       
		  
		  $errorCode=13;
	   
	  }
   //$errorCode = 10;
   */
}




switch ($errorCode) {

    case 0 : $msg = "{ \"errorCode\": $errorCode,\"errorMessage\" :\"TICKET CREE AVEC SUCCES !\"  }"; 
        break;
    case 1 : $msg = "{ \"errorCode\": $errorCode,\"errorMessage\" :\"ERREUR ! LE NOM ENTRE N'EST PAS VALIDE !\"  }";
        break;
    case 2 : $msg = "{ \"errorCode\": $errorCode,\"errorMessage\" :\"ERREUR ! LE PRENOM ENTRE N'EST PAS VALIDE !\"  }"; 
        break;
    case 3 : $msg = "{ \"errorCode\": $errorCode,\"errorMessage\" :\"ERREUR ! LE NUMERO DE TELEPHONE ENTRE N'EST PAS VALIDE.\"  }"; 
        break;
    case 4 : $msg = "{ \"errorCode\": $errorCode,\"errorMessage\" :\"ERREUR ! VEUILLEZ SELECTIONNER LA CATEGORIE DU MOTIF DU TICKET.\"  }";
        break;
    case 5 : $msg = "{ \"errorCode\": $errorCode,\"errorMessage\" :\"ERREUR ! VEUILLEZ CHOISIR LE MOTIF DU TICKET.\"  }"; 
        break;
    case 6 : $msg = "{ \"errorCode\": $errorCode,\"errorMessage\" :\"ERREUR ! LA LOCALISATION EST INVALIDE.\"  }";
        break;
    case 7 : $msg =  "ERREUR !  L'EMAIL EST INVALIDE.";
        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 ;


?>