<?php


class MysqlJson {
    /*
     * Cette classe dÃ©finit toutes les propriÃ©tÃ©s et methodes pour l'intÃ©raction des services avec les base de donnÃ©es
     *
     */

    public $dbHost;
    public $dbUser;
    public $dbPassword;
    public $dbDatabase;
    public $dbTnsnames;
    public $svaLog;

    /*     * ********* Connection au base de donnÃ©es Oracle **************** */

    function __construct($dbHost, $dbUser, $dbPassword, $dbDatabase, $dbTnsnames, $svaLog) {

        $this->dbHost = $dbHost;
        $this->dbUser = $dbUser;
        $this->dbPassword = $dbPassword;
        $this->dbTnsnames = $dbTnsnames;
        $this->dbDatabase = $dbDatabase;
        $this->svaLog = $svaLog;
    }

    private function connectMysqlDatabase() {
        $dbAccess['dbConnection'] = false;
        $dbAccess['errorMessage'] = "";

        if ($this->dbUser == "")
            $dbAccess['errorCode'] = 1000;
        /// elseif($this->dbPassword=="") $dbAccess['errorCode']=1001;
        elseif ($this->dbHost == "")
            $dbAccess['errorCode'] = 1002;
        else
            $dbAccess['errorCode'] = 0;


        //echo $this->dbDatabase.$this->dbHost.$this->dbPassword.$this->dbUser;

        if ($dbAccess['errorCode'] == 0) {
            // echo "$this->dbUser,$this->dbPassword,$this->dbTnsnames";

            $Compt = 0;
            $dbAccess['dbConnection'] = mysql_connect($this->dbHost, $this->dbUser, $this->dbPassword) or die(mysql_error());
            //echo $dbAccess['dbConnection']; 
            //$dbAccess['dbConnection'] =mysql_connect($this->dbHost,$this->dbUser,$this->dbPassword) or die (mysql_error());
            //     echo $dbAccess['dbConnection']; 

            while (!$dbAccess['dbConnection'] && $Compt <= 5) {

                $dbAccess['dbConnection'] = @mysql_connect($this->dbHost, $this->dbUser, $this->dbPassword);
                $Compt++;
            }

            if (!$dbAccess['dbConnection']) {
                $data = oci_error();
                $dbAccess['errorMessage'] = $data['message'];
            } else {

                @mysql_select_db($this->dbDatabase, $dbAccess['dbConnection']);
                mysql_set_charset("utf8");
            }
        }


        return $dbAccess;
    }

    /*     * *********************************************Obtention de la connexion Mysql**************************************************************** */

    public function getMysqlConnection() {

        $dbAccess = $this->connectMysqlDatabase();
        return $dbAccess;
    }

    /*     * ****************** Fermeture de la connexion de la connexion Mysql ********************** */

    public function closeMysqlConnection($dbConnection) {

        if ($dbConnection)
            mysql_close($dbConnection);
    }

    public function getManagerDetails($login) {

        $dbAccess = $this->connectMysqlDatabase();

        if (!$dbAccess['dbConnection']) {
            $logDebug = " Echec getManagerDetails()  " . $dbAccess['errorMessage'] . "  " . $dbAccess['errorCode'] . " MSISDN :: $this->msisdn";
            $this->svaLog->writeSystemLog($logDebug);
            die("IMPOSSIBLE DE SE CONNECTER A LA BASE DE DONNEES");
        } else {


            $sqlQuery = "SELECT *
                FROM master_users_tab 
                WHERE user_name_v ='$login'"
                . "AND status_n=0";

            //echo  $sqlQuery ;
            $sqlResult = mysql_query($sqlQuery, $dbAccess['dbConnection']);

            //echo $sqlResult;
            $queryData = mysql_fetch_array($sqlResult);
            $this->closeMysqlConnection($dbAccess['dbConnection']);
            // print_r($queryData);
            // echo mysql_error();
        }

        return $queryData;
    }

    public function getPlatformsAll() {

        $dbAccess = $this->connectMysqlDatabase();

        if (!$dbAccess['dbConnection']) {
            $logDebug = " Echec getPlatformsAll()  " . $dbAccess['errorMessage'] . "  " . $dbAccess['errorCode'] . " MSISDN :: $this->msisdn";
            $this->svaLog->writeSystemLog($logDebug);
            die("IMPOSSIBLE DE SE CONNECTER A LA BASE DE DONNEES");
        } else {


            $sqlQuery = "
                    
                    select * from master_platforms_tab

                ";
            // echo $sqlQuery;
            $sqlResult = mysql_query($sqlQuery, $dbAccess['dbConnection']);
            $this->closeMysqlConnection($dbAccess['dbConnection']);
        }

        return $this->mysqlRessourceJson($sqlResult);
    }

    private function mysqlRessourceJson($ressource) {
        $work = null;
        $numfield = mysql_num_fields($ressource);
        $numrows = mysql_num_rows($ressource);
        $fieldArray = null;

        for ($i = 0; $i < $numfield; $i++) {
            $fieldArray [$i] = mysql_field_name($ressource, $i);
        }

        $j = 0;
        while ($data = mysql_fetch_array($ressource)) {

            for ($i = 0; $i < $numfield; $i++) {

                $work[$j][$fieldArray[$i]] = $data[$i];
            }

            $j++;
        }


        return $work;
    }

    public function getModulesByPlatformId($platformId) {

        $dbAccess = $this->connectMysqlDatabase();

        if (!$dbAccess['dbConnection']) {
            $logDebug = " Echec getModulesByPlatformId()  " . $dbAccess['errorMessage'] . "  " . $dbAccess['errorCode'] . " MSISDN :: $this->msisdn";
            $this->svaLog->writeSystemLog($logDebug);
            die("IMPOSSIBLE DE SE CONNECTER A LA BASE DE DONNEES");
        } else {


            $sqlQuery = "
                    
                    select b.* 
                    from   master_platforms_tab a
                        , master_modules_tab b 
                    where a.platform_id_n = b.platform_id_n
                    and a.platform_id_n = '$platformId' 

                ";
            // echo $sqlQuery;
            $sqlResult = mysql_query($sqlQuery, $dbAccess['dbConnection']);
            $this->closeMysqlConnection($dbAccess['dbConnection']);
        }

        return $this->mysqlRessourceJson($sqlResult);
    }

    public function getMenuByModuleIdAndProfile($moduleId, $profileId) {

        $dbAccess = $this->connectMysqlDatabase();

        if (!$dbAccess['dbConnection']) {
            $logDebug = " Echec getMenuByModuleIdAndProfile()  " . $dbAccess['errorMessage'] . "  " . $dbAccess['errorCode'] . " MSISDN :: $this->msisdn";
            $this->svaLog->writeSystemLog($logDebug);
            die("IMPOSSIBLE DE SE CONNECTER A LA BASE DE DONNEES");
        } else {


            $sqlQuery = "
                    
                   select * from  menu_builder_tab where module_id_n='$moduleId' and char_field_1 like '%$profileId%' order by menu_order_n asc

                ";
            // echo $sqlQuery;
            $sqlResult = mysql_query($sqlQuery, $dbAccess['dbConnection']);
            $this->closeMysqlConnection($dbAccess['dbConnection']);
        }

        return $this->mysqlRessourceJson($sqlResult);
    }

    public function getMenuDesc($menuId, $lang) {

        $dbAccess = $this->connectMysqlDatabase();

        if (!$dbAccess['dbConnection']) {
            $logDebug = " Echec getMenuDesc()  " . $dbAccess['errorMessage'] . "  " . $dbAccess['errorCode'] . " MSISDN :: $this->msisdn";
            $this->svaLog->writeSystemLog($logDebug);
            die("IMPOSSIBLE DE SE CONNECTER A LA BASE DE DONNEES");
        } else {


            $sqlQuery = "select * from menu_builder_tab where menu_id_n='$menuId' and language_id_n='$lang'";

            //echo  $sqlQuery ;
            $sqlResult = mysql_query($sqlQuery, $dbAccess['dbConnection']);

            //echo $sqlResult;
            $queryData = mysql_fetch_array($sqlResult);
            $this->closeMysqlConnection($dbAccess['dbConnection']);
            // print_r($queryData);
            // echo mysql_error();
        }

        return $queryData;
    }

    public function getMenuDetail($menuId, $lang) {

        $dbAccess = $this->connectMysqlDatabase();

        if (!$dbAccess['dbConnection']) {
            $logDebug = " Echec getMenuDetail()  " . $dbAccess['errorMessage'] . "  " . $dbAccess['errorCode'] . " MSISDN :: $this->msisdn";
            $this->svaLog->writeSystemLog($logDebug);
            die("IMPOSSIBLE DE SE CONNECTER A LA BASE DE DONNEES");
        } else {


            $sqlQuery = "select * from menu_details_tab  where parent_menu_id='$menuId' and language_id_n='$lang' order by menu_order_n asc";

            //echo  $sqlQuery ;
            $sqlResult = mysql_query($sqlQuery, $dbAccess['dbConnection']);

            //echo $sqlResult;
            $this->closeMysqlConnection($dbAccess['dbConnection']);
            // print_r($queryData);
            // echo mysql_error();
        }

        return $this->mysqlRessourceJson($sqlResult);
    }

    public function getIadQosParams() {

        $dbAccess = $this->connectMysqlDatabase();

        if (!$dbAccess['dbConnection']) {
            $logDebug = " Echec getIadQosParams()  " . $dbAccess['errorMessage'] . "  " . $dbAccess['errorCode'] . " MSISDN :: $this->msisdn";
            $this->svaLog->writeSystemLog($logDebug);
            die("IMPOSSIBLE DE SE CONNECTER A LA BASE DE DONNEES");
        } else {


            $sqlQuery = "
                    
                   select * from iad_qos_tab

                ";
            // echo $sqlQuery;
            $sqlResult = mysql_query($sqlQuery, $dbAccess['dbConnection']);
            $this->closeMysqlConnection($dbAccess['dbConnection']);
        }

        return $this->mysqlRessourceJson($sqlResult);
    }

    public function updateQosParams($qosCode, $qosValue) {
        $dbAccess = $this->getMysqlConnection();
        $status = false;

        if (!$dbAccess['dbConnection']) {
            $logDebug = " Echec updateQosParams()  " . $dbAccess['errorMessage'] . "  " . $dbAccess['errorCode'] . " MSISDN :: $msisdn";
            $this->svaLog->writeSystemLog($logDebug);
            die("IMPOSSIBLE DE SE CONNECTER A LA BASE DE DONNEES");
        } else {


            $sqlQuery = "UPDATE iad_qos_tab
                SET qos_value_v='$qosValue'
                WHERE qos_code_v='$qosCode'
                ";
            //echo $sqlQuery;
            $status = mysql_query($sqlQuery, $dbAccess['dbConnection']);
            $this->closeMysqlConnection($dbAccess['dbConnection']);
        }

        return $status;
    }

    public function getIadHomeNetworkInfo() {

        $dbAccess = $this->connectMysqlDatabase();

        if (!$dbAccess['dbConnection']) {
            $logDebug = " Echec getIadHomeNetworkInfo()  " . $dbAccess['errorMessage'] . "  " . $dbAccess['errorCode'] . " MSISDN :: $this->msisdn";
            $this->svaLog->writeSystemLog($logDebug);
            die("IMPOSSIBLE DE SE CONNECTER A LA BASE DE DONNEES");
        } else {


            $sqlQuery = "
                    
                   select * from iad_home_network_tab limit 1

                ";
            // echo $sqlQuery;
            $sqlResult = mysql_query($sqlQuery, $dbAccess['dbConnection']);
            $data = mysql_fetch_array($sqlResult);
            $this->closeMysqlConnection($dbAccess['dbConnection']);
        }

        return $data;
    }

    public function getIadNDCList() {

        $dbAccess = $this->connectMysqlDatabase();

        if (!$dbAccess['dbConnection']) {
            $logDebug = " Echec getIadNDCList()  " . $dbAccess['errorMessage'] . "  " . $dbAccess['errorCode'] . " MSISDN :: $this->msisdn";
            $this->svaLog->writeSystemLog($logDebug);
            die("IMPOSSIBLE DE SE CONNECTER A LA BASE DE DONNEES");
        } else {


            $sqlQuery = "
                    
                   select * from iad_home_ndcs_tab

                ";
            // echo $sqlQuery;
            $sqlResult = mysql_query($sqlQuery, $dbAccess['dbConnection']);
            $this->closeMysqlConnection($dbAccess['dbConnection']);
        }

        return $this->mysqlRessourceJson($sqlResult);
    }

    public function updateIadHomeNwDtls($nwName, $mcc, $mnc, $cc) {
        $dbAccess = $this->getMysqlConnection();
        $status = false;

        if (!$dbAccess['dbConnection']) {
            $logDebug = " Echec updateIadHomeNwDtls()  " . $dbAccess['errorMessage'] . "  " . $dbAccess['errorCode'] . " MSISDN :: $msisdn";
            $this->svaLog->writeSystemLog($logDebug);
            die("IMPOSSIBLE DE SE CONNECTER A LA BASE DE DONNEES");
        } else {


            $sqlQuery = "UPDATE iad_home_network_tab
                SET mcc_v='$mcc'
                   ,mnc_v='$mnc'
                   ,cc_v='$cc'
                   ,network_name_v ='$nwName'
                WHERE network_id_n='1'
                ";
            //echo $sqlQuery;
            $status = mysql_query($sqlQuery, $dbAccess['dbConnection']);
            $this->closeMysqlConnection($dbAccess['dbConnection']);
        }

        return $status;
    }

    public function updateIadNdc($id, $ndc) {

        $dbAccess = $this->getMysqlConnection();
        $totalUser = 0;

        if (!$dbAccess['dbConnection']) {
            $logDebug = " Echec updateIadNdc()  " . $dbAccess['errorMessage'] . "  " . $dbAccess['errorCode'] . " MSISDN :: $msisdn";
            $this->svaLog->writeSystemLog($logDebug);
            die("IMPOSSIBLE DE SE CONNECTER A LA BASE DE DONNEES");
        } else {


            $sqlQuery = "update iad_home_ndcs_tab  set ndc_v='$ndc' where network_id_n='$id'";
            $queryResult = mysql_query($sqlQuery, $dbAccess['dbConnection']);
            //echo $sqlQuery;
            // $totalUser = mysql_affected_rows($queryResult);
            $this->closeMysqlConnection($dbAccess['dbConnection']);
        }

        if ($queryResult)
            $totalUser = 1;
        return $totalUser;
    }

    public function deleteNdc($id) {

        $dbAccess = $this->getMysqlConnection();
        $totalUser = 0;

        if (!$dbAccess['dbConnection']) {
            $logDebug = " Echec deleteNdc()  " . $dbAccess['errorMessage'] . "  " . $dbAccess['errorCode'] . " MSISDN :: $msisdn";
            $this->svaLog->writeSystemLog($logDebug);
            die("IMPOSSIBLE DE SE CONNECTER A LA BASE DE DONNEES");
        } else {


            $sqlQuery = "delete from iad_home_ndcs_tab where  network_id_n='$id'";
            $queryResult = mysql_query($sqlQuery, $dbAccess['dbConnection']);
            // $totalUser = mysql_affected_rows($queryResult);
            $this->closeMysqlConnection($dbAccess['dbConnection']);
        }

        if ($queryResult)
            $totalUser = 1;
        return $totalUser;
    }

    public function addNDC($ndc) {

        $dbAccess = $this->getMysqlConnection();
        $totalUser = 0;

        if (!$dbAccess['dbConnection']) {
            $logDebug = " Echec addNDC()  " . $dbAccess['errorMessage'] . "  " . $dbAccess['errorCode'] . " MSISDN :: $msisdn";
            $this->svaLog->writeSystemLog($logDebug);
            die("IMPOSSIBLE DE SE CONNECTER A LA BASE DE DONNEES");
        } else {


            $sqlQuery = "INSERT INTO iad_home_ndcs_tab  VALUES (NULL,'$ndc','0')";
            $queryResult = mysql_query($sqlQuery, $dbAccess['dbConnection']);
            //echo $sqlQuery;
            // $totalUser = mysql_affected_rows($queryResult);
            $this->closeMysqlConnection($dbAccess['dbConnection']);
        }

        if ($queryResult)
            $totalUser = 1;
        return $totalUser;
    }

    public function getIadSenderListAll() {

        $dbAccess = $this->connectMysqlDatabase();

        if (!$dbAccess['dbConnection']) {
            $logDebug = " Echec getIadSenderListAll()  " . $dbAccess['errorMessage'] . "  " . $dbAccess['errorCode'] . " MSISDN :: $this->msisdn";
            $this->svaLog->writeSystemLog($logDebug);
            die("IMPOSSIBLE DE SE CONNECTER A LA BASE DE DONNEES");
        } else {

            $sqlQuery = "
                    
                  select    sender_id_n
                            , creation_date_d
                            , short_code_v
                            , status_n 
                    FROM iad_senders_tab
                ";


            // echo $sqlQuery;
            $sqlResult = mysql_query($sqlQuery, $dbAccess['dbConnection']);

            $this->closeMysqlConnection($dbAccess['dbConnection']);
        }

        return $this->mysqlRessourceJson($sqlResult);
    }

    public function shortcodeUpdate($rowId, $code) {

        $dbAccess = $this->getMysqlConnection();
        $totalUser = 0;

        if (!$dbAccess['dbConnection']) {
            $logDebug = " Echec shortcodeUpdate()  " . $dbAccess['errorMessage'] . "  " . $dbAccess['errorCode'] . " MSISDN :: $msisdn";
            $this->svaLog->writeSystemLog($logDebug);
            die("IMPOSSIBLE DE SE CONNECTER A LA BASE DE DONNEES");
        } else {


            $sqlQuery = "update iad_senders_tab  set short_code_v='$code' where sender_id_n='$rowId'";
            $queryResult = mysql_query($sqlQuery, $dbAccess['dbConnection']);
            //echo $sqlQuery;
            // $totalUser = mysql_affected_rows($queryResult);
            $this->closeMysqlConnection($dbAccess['dbConnection']);
        }

        if ($queryResult)
            $totalUser = 1;
        return $totalUser;
    }

    public function deleteShortCode($shortcodeId) {

        $dbAccess = $this->getMysqlConnection();
        $totalUser = 0;

        if (!$dbAccess['dbConnection']) {
            $logDebug = " Echec deleteShortCode()  " . $dbAccess['errorMessage'] . "  " . $dbAccess['errorCode'] . " MSISDN :: $msisdn";
            $this->svaLog->writeSystemLog($logDebug);
            die("IMPOSSIBLE DE SE CONNECTER A LA BASE DE DONNEES");
        } else {


            $sqlQuery = "delete from iad_senders_tab where  sender_id_n='$shortcodeId'";
            $queryResult = mysql_query($sqlQuery, $dbAccess['dbConnection']);
            // $totalUser = mysql_affected_rows($queryResult);
            $this->closeMysqlConnection($dbAccess['dbConnection']);
        }

        if ($queryResult)
            $totalUser = 1;
        return $totalUser;
    }

    public function addShortCode($shortcode) {

        $dbAccess = $this->getMysqlConnection();
        $totalUser = 0;

        if (!$dbAccess['dbConnection']) {
            $logDebug = " Echec addShortCode()  " . $dbAccess['errorMessage'] . "  " . $dbAccess['errorCode'] . " MSISDN :: $msisdn";
            $this->svaLog->writeSystemLog($logDebug);
            die("IMPOSSIBLE DE SE CONNECTER A LA BASE DE DONNEES");
        } else {


            $sqlQuery = "INSERT INTO iad_senders_tab  VALUES (NULL, now(),'$shortcode','0')";
            $queryResult = mysql_query($sqlQuery, $dbAccess['dbConnection']);
            //echo $sqlQuery;
            // $totalUser = mysql_affected_rows($queryResult);
            $this->closeMysqlConnection($dbAccess['dbConnection']);
        }

        if ($queryResult)
            $totalUser = 1;
        return $totalUser;
    }

    public function getIadWhiteListAll() {

        $dbAccess = $this->connectMysqlDatabase();

        if (!$dbAccess['dbConnection']) {
            $logDebug = " Echec getIadWhiteListAll()  " . $dbAccess['errorMessage'] . "  " . $dbAccess['errorCode'] . " MSISDN :: $this->msisdn";
            $this->svaLog->writeSystemLog($logDebug);
            die("IMPOSSIBLE DE SE CONNECTER A LA BASE DE DONNEES");
        } else {

            $sqlQuery = "
                        select detail_id_n, phone_number_v from group_details_tab where group_id_n='1';
                ";


            // echo $sqlQuery;
            $sqlResult = mysql_query($sqlQuery, $dbAccess['dbConnection']);

            $this->closeMysqlConnection($dbAccess['dbConnection']);
        }

        return $this->mysqlRessourceJson($sqlResult);
    }

    public function updatePhoneNumber($rowId, $number) {

        $dbAccess = $this->getMysqlConnection();
        $totalUser = 0;

        if (!$dbAccess['dbConnection']) {
            $logDebug = " Echec updatePhoneNumber()  " . $dbAccess['errorMessage'] . "  " . $dbAccess['errorCode'] . " MSISDN :: $msisdn";
            $this->svaLog->writeSystemLog($logDebug);
            die("IMPOSSIBLE DE SE CONNECTER A LA BASE DE DONNEES");
        } else {


            $sqlQuery = "update group_details_tab  set phone_number_v='$number' where detail_id_n='$rowId'";
            $queryResult = mysql_query($sqlQuery, $dbAccess['dbConnection']);
            //echo $sqlQuery;
            // $totalUser = mysql_affected_rows($queryResult);
            $this->closeMysqlConnection($dbAccess['dbConnection']);
        }

        if ($queryResult)
            $totalUser = 1;
        return $totalUser;
    }

    public function deletePhoneNumber($rowId) {

        $dbAccess = $this->getMysqlConnection();
        $totalUser = 0;

        if (!$dbAccess['dbConnection']) {
            $logDebug = " Echec deleteNdc()  " . $dbAccess['errorMessage'] . "  " . $dbAccess['errorCode'] . " MSISDN :: $msisdn";
            $this->svaLog->writeSystemLog($logDebug);
            die("IMPOSSIBLE DE SE CONNECTER A LA BASE DE DONNEES");
        } else {


            $sqlQuery = "delete from group_details_tab where  detail_id_n='$rowId'";
            $queryResult = mysql_query($sqlQuery, $dbAccess['dbConnection']);
            // $totalUser = mysql_affected_rows($queryResult);
            $this->closeMysqlConnection($dbAccess['dbConnection']);
        }

        if ($queryResult)
            $totalUser = 1;
        return $totalUser;
    }

    public function addPhoneNumber($phoneNumber) {

        $dbAccess = $this->getMysqlConnection();
        $totalUser = 0;

        if (!$dbAccess['dbConnection']) {
            $logDebug = " Echec addPhoneNumber()  " . $dbAccess['errorMessage'] . "  " . $dbAccess['errorCode'] . " MSISDN :: $msisdn";
            $this->svaLog->writeSystemLog($logDebug);
            die("IMPOSSIBLE DE SE CONNECTER A LA BASE DE DONNEES");
        } else {


            $sqlQuery = "INSERT INTO group_details_tab (detail_id_n, group_id_n, creation_date_d,phone_number_v)  VALUES (NULL,'1',now(),'$phoneNumber' )";
            $queryResult = mysql_query($sqlQuery, $dbAccess['dbConnection']);
            //echo $sqlQuery;
            // $totalUser = mysql_affected_rows($queryResult);
            $this->closeMysqlConnection($dbAccess['dbConnection']);
        }

        if ($queryResult)
            $totalUser = 1;
        return $totalUser;
    }

    public function getRulesList($option, $startLimit, $maxElement) {

        $dbAccess = $this->connectMysqlDatabase();

        if (!$dbAccess['dbConnection']) {
            $logDebug = " Echec getRulesList()  " . $dbAccess['errorMessage'] . "  " . $dbAccess['errorCode'] . " MSISDN :: $this->msisdn";
            $this->svaLog->writeSystemLog($logDebug);
            die("IMPOSSIBLE DE SE CONNECTER A LA BASE DE DONNEES");
        } else {

            $limitQuery = "";
            if ($option)
                $limitQuery = "LIMIT $startLimit, $maxElement";

            $sqlQuery = "
                    
                  select a.*, b.description_v status_desc 
                  from iad_master_rules_tab a, master_status_tab b
                  where a.status_n = b.status_n
                  $limitQuery

                ";
            // echo $sqlQuery;
            $sqlResult = mysql_query($sqlQuery, $dbAccess['dbConnection']);

            $this->closeMysqlConnection($dbAccess['dbConnection']);
        }

        return $this->mysqlRessourceJson($sqlResult);
    }

    public function getApplicationListByOwner($ownerId, $profileId, $option, $startLimit, $maxElement) {

        $dbAccess = $this->connectMysqlDatabase();

        if (!$dbAccess['dbConnection']) {
            $logDebug = " Echec getApplicationListByOwner()  " . $dbAccess['errorMessage'] . "  " . $dbAccess['errorCode'] . " MSISDN :: $this->msisdn";
            $this->svaLog->writeSystemLog($logDebug);
            die("IMPOSSIBLE DE SE CONNECTER A LA BASE DE DONNEES");
        } else {

            $limitQuery = "";
            if ($option)
                $limitQuery = "LIMIT $startLimit, $maxElement";

            if ($profileId == 1) {


                $sqlQuery = "
                    
                  select a.*, b.description_v status_desc 
                  from iad_apps_rule_tab a, master_status_tab b
                  where a.status_n = b.status_n
                  $limitQuery

                ";
            } else {

                $sqlQuery = "
                    
                  select a.*, b.description_v status_desc 
                  from iad_apps_rule_tab a, master_status_tab b
                  where a.status_n = b.status_n
                  and a.owner_id_n = '$ownerId'
                  $limitQuery

                ";
            }


            // echo $sqlQuery;
            $sqlResult = mysql_query($sqlQuery, $dbAccess['dbConnection']);

            $this->closeMysqlConnection($dbAccess['dbConnection']);
        }

        return $this->mysqlRessourceJson($sqlResult);
    }

    public function getApplicationById($appId) {

        $dbAccess = $this->connectMysqlDatabase();

        if (!$dbAccess['dbConnection']) {
            $logDebug = " Echec getApplicationById()  " . $dbAccess['errorMessage'] . "  " . $dbAccess['errorCode'] . " MSISDN :: $this->msisdn";
            $this->svaLog->writeSystemLog($logDebug);
            die("IMPOSSIBLE DE SE CONNECTER A LA BASE DE DONNEES");
        } else {


            $sqlQuery = "
                    
                  select *
                  from iad_apps_rule_tab 
                  where app_id_n='$appId'

                ";



            // echo $sqlQuery;
            $sqlResult = mysql_query($sqlQuery, $dbAccess['dbConnection']);
            $data = mysql_fetch_array($sqlResult);
            $this->closeMysqlConnection($dbAccess['dbConnection']);
        }

        return $data;
    }

    public function getRulesById($ruleId) {

        $dbAccess = $this->connectMysqlDatabase();

        if (!$dbAccess['dbConnection']) {
            $logDebug = " Echec getRulesById()  " . $dbAccess['errorMessage'] . "  " . $dbAccess['errorCode'] . " MSISDN :: $this->msisdn";
            $this->svaLog->writeSystemLog($logDebug);
            die("IMPOSSIBLE DE SE CONNECTER A LA BASE DE DONNEES");
        } else {


            $sqlQuery = "
                    
                  select a.*, b.description_v status_desc 
                  from iad_master_rules_tab a, master_status_tab b
                  where a.status_n = b.status_n
                  and a.rule_id_n='$ruleId'

                ";
            // echo $sqlQuery;
            $sqlResult = mysql_query($sqlQuery, $dbAccess['dbConnection']);
            $data = mysql_fetch_array($sqlResult);
            $this->closeMysqlConnection($dbAccess['dbConnection']);
        }

        return $data;
    }

    public function getRuleVariableMapping($ruleId) {

        $dbAccess = $this->connectMysqlDatabase();

        if (!$dbAccess['dbConnection']) {
            $logDebug = " Echec getRuleVariableMapping()  " . $dbAccess['errorMessage'] . "  " . $dbAccess['errorCode'] . " MSISDN :: $this->msisdn";
            $this->svaLog->writeSystemLog($logDebug);
            die("IMPOSSIBLE DE SE CONNECTER A LA BASE DE DONNEES");
        } else {


            $sqlQuery = "
                    
                  select variable_name_v, var_type_n from iad_rulervar_mapping_tab where rule_id_n='$ruleId';

                ";
            //echo $sqlQuery;
            $sqlResult = mysql_query($sqlQuery, $dbAccess['dbConnection']);
            $this->closeMysqlConnection($dbAccess['dbConnection']);
        }

        return $this->mysqlRessourceJson($sqlResult);
    }

    public function iadUpdateApplication($appName, $appDesc, $ruleId, $ruleData, $appId) {
        $dbAccess = $this->getMysqlConnection();
        $id = 0;

        if (!$dbAccess['dbConnection']) {
            $logDebug = " Echec iadCreateApplication()  " . $dbAccess['errorMessage'] . "  " . $dbAccess['errorCode'] . " MSISDN :: $msisdn";
            $this->svaLog->writeSystemLog($logDebug);
            die("IMPOSSIBLE DE SE CONNECTER A LA BASE DE DONNEES");
        } else {


            $sqlQuery = "UPDATE iad_apps_rule_tab
                             SET  app_name_v='$appName',
                                  app_desc_v='$appDesc',
                                  rule_data_v='$ruleData',
                                  rule_id_n = '$ruleId'
                            where app_id_n ='$appId'
                          ";
            //echo $sqlQuery;
            $queryResult = mysql_query($sqlQuery, $dbAccess['dbConnection']);
            $id = mysql_insert_id($dbAccess['dbConnection']);
            $this->closeMysqlConnection($dbAccess['dbConnection']);
        }

        return $id;
    }

    public function getApplicationCampaignsCount($appId) {

        $dbAccess = $this->getMysqlConnection();
        $total = 0;

        if (!$dbAccess['dbConnection']) {
            $logDebug = " Echec getApplicationCampaignsCount()  " . $dbAccess['errorMessage'] . "  " . $dbAccess['errorCode'] . " MSISDN :: $msisdn";
            $this->svaLog->writeSystemLog($logDebug);
            die("IMPOSSIBLE DE SE CONNECTER A LA BASE DE DONNEES");
        } else {


            $sqlQuery = "select count(1) total from master_campaigns_tab where appid='$appId'";
            $queryResult = mysql_query($sqlQuery, $dbAccess['dbConnection']);
            //echo $sqlQuery;
            $data = mysql_fetch_array($queryResult);
            $total = $data['total'];
            $this->closeMysqlConnection($dbAccess['dbConnection']);
        }


        return $total;
    }

    public function deleteIadApplication($rowId) {

        $dbAccess = $this->getMysqlConnection();
        $totalUser = 0;

        if (!$dbAccess['dbConnection']) {
            $logDebug = " Echec deleteIadApplication()  " . $dbAccess['errorMessage'] . "  " . $dbAccess['errorCode'] . " MSISDN :: $msisdn";
            $this->svaLog->writeSystemLog($logDebug);
            die("IMPOSSIBLE DE SE CONNECTER A LA BASE DE DONNEES");
        } else {


            $sqlQuery = "delete from iad_apps_rule_tab where  app_id_n='$rowId'";
            $queryResult = mysql_query($sqlQuery, $dbAccess['dbConnection']);
            // $totalUser = mysql_affected_rows($queryResult);
            $this->closeMysqlConnection($dbAccess['dbConnection']);
        }

        if ($queryResult)
            $totalUser = 1;
        return $totalUser;
    }

    public function iadCreateApplication($appName, $appDesc, $ruleId, $ruleData, $ownerId, $statusN) {
        $dbAccess = $this->getMysqlConnection();
        $id = 0;

        if (!$dbAccess['dbConnection']) {
            $logDebug = " Echec iadCreateApplication()  " . $dbAccess['errorMessage'] . "  " . $dbAccess['errorCode'] . " MSISDN :: $msisdn";
            $this->svaLog->writeSystemLog($logDebug);
            die("IMPOSSIBLE DE SE CONNECTER A LA BASE DE DONNEES");
        } else {


            $sqlQuery = "INSERT 
                INTO iad_apps_rule_tab  VALUES (NULL,now(),'$appName','$ruleData','$appDesc','$ownerId','$ruleId','$statusN')";
            //echo $sqlQuery;
            $queryResult = mysql_query($sqlQuery, $dbAccess['dbConnection']);
            $id = mysql_insert_id($dbAccess['dbConnection']);
            $this->closeMysqlConnection($dbAccess['dbConnection']);
        }

        return $id;
    }

    public function getOfferList($option, $startLimit, $maxElement) {

        $dbAccess = $this->connectMysqlDatabase();

        if (!$dbAccess['dbConnection']) {
            $logDebug = " Echec getOfferList()  " . $dbAccess['errorMessage'] . "  " . $dbAccess['errorCode'] . " MSISDN :: $this->msisdn";
            $this->svaLog->writeSystemLog($logDebug);
            die("IMPOSSIBLE DE SE CONNECTER A LA BASE DE DONNEES");
        } else {

            $limitQuery = "";
            if ($option)
                $limitQuery = "LIMIT $startLimit, $maxElement";

            $sqlQuery = "
                    
                   select   offer_id_n
                            , DATE_FORMAT(creation_date_d, '%d/%m/%Y %H:%i:%s') eventDateTime 
                            , offer_name_v
                            , offer_type_n
                            , owner_id_n
                            , offer_desc_v
                            , short_code_v
                            , keyword_v
                            , sms_charged_v
                            , service_price_v
                            , char_field_1
                            , char_field_2
                            , char_field_3
                            , char_field_4
                            , char_field_5
                            , b.description_v status_desc
                    from iad_master_offers_tab a,  master_status_tab b
                    where a.status_n = b.status_n
                    ORDER BY creation_date_d DESC
                    $limitQuery

                ";
            // echo $sqlQuery;
            $sqlResult = mysql_query($sqlQuery, $dbAccess['dbConnection']);
            $this->closeMysqlConnection($dbAccess['dbConnection']);
        }

        return $this->mysqlRessourceJson($sqlResult);
    }

    public function getOfferListById($offerId) {

        $dbAccess = $this->connectMysqlDatabase();

        if (!$dbAccess['dbConnection']) {
            $logDebug = " Echec getOfferListById()  " . $dbAccess['errorMessage'] . "  " . $dbAccess['errorCode'] . " MSISDN :: $this->msisdn";
            $this->svaLog->writeSystemLog($logDebug);
            die("IMPOSSIBLE DE SE CONNECTER A LA BASE DE DONNEES");
        } else {


            $sqlQuery = "
                    
                   select   offer_id_n
                            , DATE_FORMAT(creation_date_d, '%d/%m/%Y %H:%i:%s') eventDateTime 
                            , offer_name_v
                            , offer_type_n
                            , owner_id_n
                            , offer_desc_v
                            , short_code_v
                            , keyword_v
                            , sms_charged_v
                            , service_price_v
                            , char_field_1
                            , char_field_2
                            , char_field_3
                            , char_field_4
                            , char_field_5
                            , status_n 
                    from iad_master_offers_tab
                    where offer_id_n ='$offerId'

                ";
            // echo $sqlQuery;
            $sqlResult = mysql_query($sqlQuery, $dbAccess['dbConnection']);
            $data = mysql_fetch_array($sqlResult);
            $this->closeMysqlConnection($dbAccess['dbConnection']);
        }

        return $data;
    }

    public function iadUpdateOffer($offerName, $offerDesc, $offerType, $shortCode, $keyword, $price, $offerId) {
        $dbAccess = $this->getMysqlConnection();
        $id = 0;

        if (!$dbAccess['dbConnection']) {
            $logDebug = " Echec iadUpdateOffer()  " . $dbAccess['errorMessage'] . "  " . $dbAccess['errorCode'] . " MSISDN :: $msisdn";
            $this->svaLog->writeSystemLog($logDebug);
            die("IMPOSSIBLE DE SE CONNECTER A LA BASE DE DONNEES");
        } else {


            $sqlQuery = "UPDATE iad_master_offers_tab
                             SET  offer_name_v='$offerName',
                                  offer_type_n='$offerType',
                                  offer_desc_v='$offerDesc',
                                  short_code_v = '$shortCode',
                                  keyword_v = '$keyword',
                                  service_price_v = '$price'
                            where offer_id_n ='$offerId'
                          ";
            //echo $sqlQuery;
            $queryResult = mysql_query($sqlQuery, $dbAccess['dbConnection']);

            $this->closeMysqlConnection($dbAccess['dbConnection']);
        }

        return $queryResult;
    }

    public function iadCreateOffer($offerName, $offerDesc, $offerType, $shortCode, $ownerId, $keyword, $charged, $price, $field, $statusN) {
        $dbAccess = $this->getMysqlConnection();
        $id = 0;

        if (!$dbAccess['dbConnection']) {
            $logDebug = " Echec iadCreateOffer()  " . $dbAccess['errorMessage'] . "  " . $dbAccess['errorCode'] . " MSISDN :: $msisdn";
            $this->svaLog->writeSystemLog($logDebug);
            die("IMPOSSIBLE DE SE CONNECTER A LA BASE DE DONNEES");
        } else {


            $sqlQuery = "INSERT 
                INTO iad_master_offers_tab  VALUES (NULL,now(),'$offerName','$offerType','$offerDesc','$ownerId','$shortCode','$keyword','$charged','$price','$field[0]','$field[1]','$field[2]','$field[3]','$field[4]','$statusN')";
            //echo $sqlQuery;
            $queryResult = mysql_query($sqlQuery, $dbAccess['dbConnection']);
            $id = mysql_insert_id($dbAccess['dbConnection']);
            $this->closeMysqlConnection($dbAccess['dbConnection']);
        }

        return $id;
    }

    public function deleteOffer($offerId) {

        $dbAccess = $this->connectMysqlDatabase();

        if (!$dbAccess['dbConnection']) {
            $logDebug = " Echec deleteOffer()  " . $dbAccess['errorMessage'] . "  " . $dbAccess['errorCode'] . " MSISDN :: $this->msisdn";
            $this->svaLog->writeSystemLog($logDebug);
            die("IMPOSSIBLE DE SE CONNECTER A LA BASE DE DONNEES");
        } else {

            $sqlQuery = "
                   delete from   iad_master_offers_tab  where offer_id_n='$offerId'
                ";


            // echo $sqlQuery;
            $sqlResult = mysql_query($sqlQuery, $dbAccess['dbConnection']);

            $this->closeMysqlConnection($dbAccess['dbConnection']);
        }

        return $sqlResult;
    }

    public function getCampaignListOld($profileId, $ownerId, $startLimit, $maxElement, $option) {

        $dbAccess = $this->connectMysqlDatabase();

        if (!$dbAccess['dbConnection']) {
            $logDebug = " Echec getCampaignList()  " . $dbAccess['errorMessage'] . "  " . $dbAccess['errorCode'] . " MSISDN :: $this->msisdn";
            $this->svaLog->writeSystemLog($logDebug);
            die("IMPOSSIBLE DE SE CONNECTER A LA BASE DE DONNEES");
        } else {

            $limitSql = "";
            $profileSql = "";
            $userSql = "";
            if ($option)
                $limitSql = "LIMIT $startLimit, $maxElement   ";
            if ($profileId == 2)
                $profileSql = "AND b.profile_id_n='$profileId' ";
            if (isset($ownerId) && $profileId == 2)
                $userSql = "AND b.user_id_n='$ownerId' ";



            $sqlQuery = " 
                    
                      select    campaign_id_n
                                , a.creation_date_d
                                , campaign_name_v
                                , date_format(start_date_d,'%d/%m/%Y') start_date_d 
                                , date_format(start_time_d,'%H:%i:%s') start_time_d 
                                , owner_id_n
                                , sender_id_n
                                , appid
                                , c.description_v status_desc
                                , b.user_name_v
                                , c.status_n
                                , a.char_field_2  totalElement
                                , a.char_field_3 totalCalled
                            from master_campaigns_tab a,
                                master_users_tab b,
                                master_status_tab c
                            where a.owner_id_n = b.user_id_n
                            and a.status_n = c.status_n
                            $userSql
                            $profileSql
                            ORDER BY a.creation_date_d DESC
                            $limitSql
                ";







            // echo $sqlQuery;
            $sqlResult = mysql_query($sqlQuery, $dbAccess['dbConnection']);

            $this->closeMysqlConnection($dbAccess['dbConnection']);
        }

        return $this->mysqlRessourceJson($sqlResult);
    }

    public function getUsersInfo($startLimit, $maxElement, $option) {

        $dbAccess = $this->connectMysqlDatabase();

        if (!$dbAccess['dbConnection']) {
            $logDebug = " Echec getCampaignList()  " . $dbAccess['errorMessage'] . "  " . $dbAccess['errorCode'] . " MSISDN :: $this->msisdn";
            $this->svaLog->writeSystemLog($logDebug);
            die("IMPOSSIBLE DE SE CONNECTER A LA BASE DE DONNEES");
        } else {

            $limitSql = "";
            $profileSql = "";
            $userSql = "";
            if ($option)
                $limitSql = "LIMIT $startLimit, $maxElement   ";

            $sqlQuery = "select * from master_users_tab $limitSql ";

            // echo $sqlQuery;
            $sqlResult = mysql_query($sqlQuery, $dbAccess['dbConnection']);

            $this->closeMysqlConnection($dbAccess['dbConnection']);
        }

        return $this->mysqlRessourceJson($sqlResult);
    }

    public function getApplicationsListAll() {

        $dbAccess = $this->connectMysqlDatabase();

        if (!$dbAccess['dbConnection']) {
            $logDebug = " Echec getApplicationsListAll()  " . $dbAccess['errorMessage'] . "  " . $dbAccess['errorCode'] . " MSISDN :: $this->msisdn";
            $this->svaLog->writeSystemLog($logDebug);
            die("IMPOSSIBLE DE SE CONNECTER A LA BASE DE DONNEES");
        } else {

            $sqlQuery = "
                    
                  select app_id_n
                        , creation_date_d
                        , app_name_v
                        , rule_data_v
                        , app_desc_v
                        , owner_id_n
                        , rule_id_n
                        , status_n  
                    from iad_apps_rule_tab
                    ORDER BY app_id_n DESC

                ";


            // echo $sqlQuery;
            $sqlResult = mysql_query($sqlQuery, $dbAccess['dbConnection']);

            $this->closeMysqlConnection($dbAccess['dbConnection']);
        }

        return $this->mysqlRessourceJson($sqlResult);
    }

    public function checkCampaignName($name) {
        $dbAccess = $this->getMysqlConnection();
        $totalUser = 2;

        if (!$dbAccess['dbConnection']) {
            $logDebug = " Echec checkCampaignName()  " . $dbAccess['errorMessage'] . "  " . $dbAccess['errorCode'] . " MSISDN :: $msisdn";
            $this->svaLog->writeSystemLog($logDebug);
            die("IMPOSSIBLE DE SE CONNECTER A LA BASE DE DONNEES");
        } else {


            $sqlQuery = "SELECT count(1) total
                FROM master_campaigns_tab 
                WHERE campaign_name_v ='$name'";
            $queryResult = mysql_query($sqlQuery, $dbAccess['dbConnection']);
            $queryData = mysql_fetch_array($queryResult);
            $totalUser = $queryData['total'];
            $this->closeMysqlConnection($dbAccess['dbConnection']);
        }

        return $totalUser;
    }

    public function getDbConnection() {
        $dbAccess = $this->getMysqlConnection();

        return $dbAccess;
    }

    public function closeDbConnection($dbConnection) {

        $this->closeMysqlConnection($dbConnection);
    }

    public function uploadTemporaryData($session, $entity, $dbAccess) {
        // $dbAccess = $this->getMysqlConnection();   
        $id = -1;
        if (!$dbAccess['dbConnection']) {
            $logDebug = " Echec uploadTemporaryData()  " . $dbAccess['errorMessage'] . "  " . $dbAccess['errorCode'] . " MSISDN :: $msisdn";
            $this->svaLog->writeSystemLog($logDebug);
            die("IMPOSSIBLE DE SE CONNECTER A LA BASE DE DONNEES");
        } else {


            $sqlQuery = "INSERT 
                INTO temporary_entities_tab
                VALUES (null,'$session','$entity')";
            //echo $sqlQuery;
            $queryResult = mysql_query($sqlQuery, $dbAccess['dbConnection']);
            $id = mysql_insert_id();
            // $this->closeMysqlConnection($dbAccess['dbConnection']);
        }

        return $id;
    }

    //////******************************************///////////////
    public function trimNumero($numero) {

        $numeroData = str_split($numero);
        $nbr = count($numeroData);
        $numerotr = "";
        for ($i = 0; $i < $nbr; $i++) {
            if (is_numeric($numeroData[$i])) {
                $numerotr = $numerotr . $numeroData[$i];
            }
        }
        $numerotr = trim($numerotr);
        return $numerotr;
    }

    ///////****************************************/////

    public function getGroupDetailByGroupId($groupId) {

        $dbAccess = $this->getMysqlConnection();

        if (!$dbAccess['dbConnection']) {
            $logDebug = " Echec getGroupDetailByGroupId()  " . $dbAccess['errorMessage'] . "  " . $dbAccess['errorCode'] . " MSISDN :: $this->msisdn";
            $this->svaLog->writeSystemLog($logDebug);
            die("IMPOSSIBLE DE SE CONNECTER A LA BASE DE DONNEES");
        } else {

            $sqlQuery = "
                    
                  select  detail_id_n
                            , group_id_n
                            , DATE_FORMAT(creation_date_d, '%d/%m/%Y %H:%i:%s') eventDateTime
                            , entity_name_v
                            , additionnal_info_v
                            , phone_number_v
                            , email_v
                            , char_field_1 nom
                            , char_field_2 prenom
                            , char_field_3
                            , char_field_4
                            , char_field_5
                            , status_n 
                    from group_details_tab
                    where group_id_n='$groupId'
                ";


            // echo $sqlQuery;
            $sqlResult = mysql_query($sqlQuery, $dbAccess['dbConnection']);

            //$this->closeMysqlConnection($dbAccess['dbConnection'])  ;
        }

        return $this->mysqlRessourceJson($sqlResult);
    }

    public function getBillingDetailById($billingId) {

        $dbAccess = $this->getMysqlConnection();

        if (!$dbAccess['dbConnection']) {
            $logDebug = " Echec getGroupDetailByGroupId()  " . $dbAccess['errorMessage'] . "  " . $dbAccess['errorCode'] . " MSISDN :: $this->msisdn";
            $this->svaLog->writeSystemLog($logDebug);
            die("IMPOSSIBLE DE SE CONNECTER A LA BASE DE DONNEES");
        } else {

            $sqlQuery = '
                    
                  select * from master_networks_tab a ,billing_details_tab b where a.network_id_n = b.network_id_n and billing_id_n = "' . $billingId . '"  
                ';


            // echo $sqlQuery;
            $sqlResult = mysql_query($sqlQuery, $dbAccess['dbConnection']);

            //$this->closeMysqlConnection($dbAccess['dbConnection'])  ;
        }

        return $this->mysqlRessourceJson($sqlResult);
    }

    public function checkGroupName($name) {
        $dbAccess = $this->getMysqlConnection();
        $totalUser = 2;

        if (!$dbAccess['dbConnection']) {
            $logDebug = " Echec checkGroupName()  " . $dbAccess['errorMessage'] . "  " . $dbAccess['errorCode'] . " MSISDN :: $msisdn";
            $this->svaLog->writeSystemLog($logDebug);
            die("IMPOSSIBLE DE SE CONNECTER A LA BASE DE DONNEES");
        } else {


            $sqlQuery = "SELECT count(1) total
                FROM master_groups_tab 
                WHERE group_name_v ='$name'";
            $queryResult = mysql_query($sqlQuery, $dbAccess['dbConnection']);
            $queryData = @mysql_fetch_array($queryResult);
            $totalUser = $queryData['total'];
            $this->closeMysqlConnection($dbAccess['dbConnection']);
        }

        return $totalUser;
    }

    ///**********************************************************/////////////////

    public function createCampaign($campaignName
    , $internalCode
    , $startDate
    , $startTime
    , $endDate
    , $endTime
    , $ownerId
    , $priority
    , $senderId
    , $message
    , $moduleId
    , $field
    , $status
    ) {

        $dbAccess = $this->getMysqlConnection();
        $id = -1;
        if (!$dbAccess['dbConnection']) {
            $logDebug = " Echec createCampaign()  " . $dbAccess['errorMessage'] . "  " . $dbAccess['errorCode'] . " MSISDN :: $msisdn";
            $this->svaLog->writeSystemLog($logDebug);
            die("IMPOSSIBLE DE SE CONNECTER A LA BASE DE DONNEES");
        } else {


            $sqlQuery = "INSERT 
                             INTO master_campaigns_tab 
                             VALUES (NULL,now(),'$campaignName','$internalCode','$startDate','$startTime','$endDate','$endTime','$ownerId','$priority','$senderId','$message','','$moduleId','$field[0]','$field[1]','$field[2]','$field[3]','$field[4]','$field[5]','$field[6]','$field[7]','$field[8]','$field[9]','$status')";
            //echo $sqlQuery;
            $queryResult = mysql_query($sqlQuery, $dbAccess['dbConnection']);
            $id = mysql_insert_id();
            $this->closeMysqlConnection($dbAccess['dbConnection']);
        }

        return $id;
    }

    public function getTemporaryDataBySession($session) {

        $dbAccess = $this->connectMysqlDatabase();

        if (!$dbAccess['dbConnection']) {
            $logDebug = " Echec getTemporaryDataBySession()  " . $dbAccess['errorMessage'] . "  " . $dbAccess['errorCode'] . " MSISDN :: $this->msisdn";
            $this->svaLog->writeSystemLog($logDebug);
            die("IMPOSSIBLE DE SE CONNECTER A LA BASE DE DONNEES");
        } else {

            $sqlQuery = "
                  select distinct entity_v from temporary_entities_tab where session_id_v='$session'
                ";


            // echo $sqlQuery;
            $sqlResult = mysql_query($sqlQuery, $dbAccess['dbConnection']);

            $this->closeMysqlConnection($dbAccess['dbConnection']);
        }

        return $this->mysqlRessourceJson($sqlResult);
    }

    public function processTemporaryData($session, $campaignId, $statut) {

        $dbAccess = $this->connectMysqlDatabase();
        $total = null;

        if (!$dbAccess['dbConnection']) {
            $logDebug = " Echec processTemporaryData()  " . $dbAccess['errorMessage'] . "  " . $dbAccess['errorCode'] . " MSISDN :: $this->msisdn";
            $this->svaLog->writeSystemLog($logDebug);
            die("IMPOSSIBLE DE SE CONNECTER A LA BASE DE DONNEES");
        } else {

            $sqlQuery = "
                  
      
                INSERT INTO queues_processor_tab (item_id_n, event_datetime_d, campaign_id_n, phone_number_v, entity_v, status_n )  (select distinct null,now(),'$campaignId', entity_v,entity_v,'$statut' from temporary_entities_tab where session_id_v='$session')
                ";


            // echo $sqlQuery;
            $sqlResult = mysql_query($sqlQuery, $dbAccess['dbConnection']);
            $total = mysql_affected_rows();
            $this->closeMysqlConnection($dbAccess['dbConnection']);
        }

        return $total;
    }

    public function deleteTemporaryDataBySession($session) {

        $dbAccess = $this->connectMysqlDatabase();

        if (!$dbAccess['dbConnection']) {
            $logDebug = " Echec deleteTemporaryDataBySession()  " . $dbAccess['errorMessage'] . "  " . $dbAccess['errorCode'] . " MSISDN :: $this->msisdn";
            $this->svaLog->writeSystemLog($logDebug);
            die("IMPOSSIBLE DE SE CONNECTER A LA BASE DE DONNEES");
        } else {

            $sqlQuery = "
                  delete from   temporary_entities_tab where session_id_v='$session'
                ";


            // echo $sqlQuery;
            $sqlResult = mysql_query($sqlQuery, $dbAccess['dbConnection']);

            $this->closeMysqlConnection($dbAccess['dbConnection']);
        }

        return $sqlResult;
    }

    public function getSystemUserByProfile($profileId, $userId) {

        $dbAccess = $this->connectMysqlDatabase();

        if (!$dbAccess['dbConnection']) {
            $logDebug = " Echec getSystemUserByProfile()  " . $dbAccess['errorMessage'] . "  " . $dbAccess['errorCode'] . " MSISDN :: $this->msisdn";
            $this->svaLog->writeSystemLog($logDebug);
            die("IMPOSSIBLE DE SE CONNECTER A LA BASE DE DONNEES");
        } else {

            if ($profileId == 1 || $profileId == 3) {
                $sqlQuery = "  
                 select   user_id_n
                        , user_name_v
                        , profile_id_n
                    FROM master_users_tab
                ";
            } elseif ($profileId == 2) {

                $sqlQuery = "  
                  select   user_id_n
                        , user_name_v
                        , profile_id_n
                    FROM master_users_tab
                    where user_id_n='$userId'
                    order by 2 asc
                ";
            }
            // echo $sqlQuery;
            $sqlResult = mysql_query($sqlQuery, $dbAccess['dbConnection']);
            $this->closeMysqlConnection($dbAccess['dbConnection']);
        }

        return $this->mysqlRessourceJson($sqlResult);
    }

    public function getSystemShortcodeByProfile($profileId, $userId) {

        $dbAccess = $this->connectMysqlDatabase();

        if (!$dbAccess['dbConnection']) {
            $logDebug = " Echec getSystemUserByProfile()  " . $dbAccess['errorMessage'] . "  " . $dbAccess['errorCode'] . " MSISDN :: $this->msisdn";
            $this->svaLog->writeSystemLog($logDebug);
            die("IMPOSSIBLE DE SE CONNECTER A LA BASE DE DONNEES");
        } else {

            if ($profileId == 1 || $profileId == 3) {
                $sqlQuery = "  
                 select   shortcode_id_n
                        , shortcode_number_n
                    FROM master_shortcode_tab
                ";
            } elseif ($profileId == 2) {

                $sqlQuery = "  
                  select   shortcode_id_n
                        , shortcode_number_n
                    FROM master_shortcode_tab
                    where user_id_n='$userId'
                ";
            }
            // echo $sqlQuery;
            $sqlResult = mysql_query($sqlQuery, $dbAccess['dbConnection']);
            $this->closeMysqlConnection($dbAccess['dbConnection']);
        }

        return $this->mysqlRessourceJson($sqlResult);
    }

    public function getCampaignByProfile($profileId, $userId) {

        $dbAccess = $this->connectMysqlDatabase();

        if (!$dbAccess['dbConnection']) {
            $logDebug = " Echec getCampaignByProfile()  " . $dbAccess['errorMessage'] . "  " . $dbAccess['errorCode'] . " MSISDN :: $this->msisdn";
            $this->svaLog->writeSystemLog($logDebug);
            die("IMPOSSIBLE DE SE CONNECTER A LA BASE DE DONNEES");
        } else {

            if ($profileId == 1 || $profileId == 3) {
                $sqlQuery = "  
                 select campaign_id_n, campaign_name_v, owner_id_n 
                 FROM master_campaigns_tab
                 order by campaign_id_n desc
                ";
            } elseif ($profileId == 2) {

                $sqlQuery = "  
                  select campaign_id_n, campaign_name_v, owner_id_n 
                 FROM master_campaigns_tab where owner_id_n='$userId'
                  order by campaign_id_n desc
                ";
            }
            // echo $sqlQuery;
            $sqlResult = mysql_query($sqlQuery, $dbAccess['dbConnection']);
            $this->closeMysqlConnection($dbAccess['dbConnection']);
        }

        return $this->mysqlRessourceJson($sqlResult);
    }

    public function getOfferListAll() {

        $dbAccess = $this->connectMysqlDatabase();

        if (!$dbAccess['dbConnection']) {
            $logDebug = " Echec getOfferListAll()  " . $dbAccess['errorMessage'] . "  " . $dbAccess['errorCode'] . " MSISDN :: $this->msisdn";
            $this->svaLog->writeSystemLog($logDebug);
            die("IMPOSSIBLE DE SE CONNECTER A LA BASE DE DONNEES");
        } else {



            $sqlQuery = "  
                  select offer_id_n, offer_name_v FROM iad_master_offers_tab
                ";
            // echo $sqlQuery;
            $sqlResult = mysql_query($sqlQuery, $dbAccess['dbConnection']);
            $this->closeMysqlConnection($dbAccess['dbConnection']);
        }

        return $this->mysqlRessourceJson($sqlResult);
    }

    public function getStatusAll() {

        $dbAccess = $this->connectMysqlDatabase();

        if (!$dbAccess['dbConnection']) {
            $logDebug = " Echec getCampaignByProfile()  " . $dbAccess['errorMessage'] . "  " . $dbAccess['errorCode'] . " MSISDN :: $this->msisdn";
            $this->svaLog->writeSystemLog($logDebug);
            die("IMPOSSIBLE DE SE CONNECTER A LA BASE DE DONNEES");
        } else {

            $sqlQuery = "  
                 select status_n, description_v FROM master_status_tab
                ";

            // echo $sqlQuery;
            $sqlResult = mysql_query($sqlQuery, $dbAccess['dbConnection']);
            $this->closeMysqlConnection($dbAccess['dbConnection']);
        }

        return $this->mysqlRessourceJson($sqlResult);
    }

    public function getIadDetailledStatParam($profileId, $userId, $campaignId, $startLimit, $maxElement, $startDate, $endDate, $statusId, $option) {

        $dbAccess = $this->connectMysqlDatabase();

        if (!$dbAccess['dbConnection']) {
            $logDebug = " Echec getIadDetailledStatParam()  " . $dbAccess['errorMessage'] . "  " . $dbAccess['errorCode'] . " MSISDN :: $this->msisdn";
            $this->svaLog->writeSystemLog($logDebug);
            die("IMPOSSIBLE DE SE CONNECTER A LA BASE DE DONNEES");
        } else {

            $profileSql = "";
            $userSql = "";
            $limitSql = "";
            $statusSql = "";
            $campaignSql = "";

            if ($profileId == 2)
                $profileSql = "AND d.profile_id_n='$profileId' ";
            if (($userId == -1 && $profileId == 2) || ($profileId != 2 && $userId != -1 ))
                $userSql = "AND d.user_id_n='$userId' ";
            if ($option)
                $limitSql = "LIMIT $startLimit, $maxElement   ";
            if ($campaignId != -1 && $campaignId != "")
                $campaignSql = " AND a.campaign_id_n = '$campaignId' ";
            if ($statusId != -1)
                $statusSql = "AND a.status_n='$statusId' ";

            $sqlQuery = "
                      
                    select  date_format(a.event_datetime_d,'%d/%m/%Y %H:%i:%s') DATE
                                ,a.phone_number_v MSISDN
                                ,a.duration_n DUREE
                                ,date_format(concat(b.start_date_d,\" \",b.end_date_d ),'%d/%m/%Y %H:%i:%s')  DEBUT
                                ,f.short_code_v IDENTIFIANT
                                ,e.description_v STATUT
                                ,c.app_name_v APPLICATION
                                ,b.campaign_name_v CAMPAGNE
                                ,d.user_name_v LOGIN
                        from queues_processor_tab a,
                                    master_campaigns_tab b,
                                    iad_apps_rule_tab c,
                                    master_users_tab d,
                                    master_status_tab e,
                                    iad_senders_tab f
                        where a.campaign_id_n = b.campaign_id_n
                        and b.appid = c.app_id_n
                        and b.owner_id_n = d.user_id_n
                        and e.status_n = a.status_n
                        and a.event_datetime_d>='$startDate'
                        and a.event_datetime_d <='$endDate'
                        and f.sender_id_n = b.sender_id_n
                        $profileSql
                        $userSql
                        $campaignSql
                        $statusSql
                        order by DATE  desc
                        $limitSql
                        

                ";


            //echo $sqlQuery;
            $sqlResult = mysql_query($sqlQuery, $dbAccess['dbConnection']);
            $this->closeMysqlConnection($dbAccess['dbConnection']);
        }

        return $this->mysqlRessourceJson($sqlResult);
    }

    public function getIadDetailledSubsStatParam($profileId, $userId, $campaignId, $startLimit, $maxElement, $startDate, $endDate, $offerId) {

        $dbAccess = $this->connectMysqlDatabase();

        if (!$dbAccess['dbConnection']) {
            $logDebug = " Echec getIadDetailledStatParam()  " . $dbAccess['errorMessage'] . "  " . $dbAccess['errorCode'] . " MSISDN :: $this->msisdn";
            $this->svaLog->writeSystemLog($logDebug);
            die("IMPOSSIBLE DE SE CONNECTER A LA BASE DE DONNEES");
        } else {

            $profileSql = "";
            $userSql = "";
            $limitSql = "";
            $offerSql = "";
            $campaignSql = "";

            if ($profileId == 2)
                $profileSql = "AND d.profile_id_n='$profileId' ";
            if (($userId == -1 && $profileId == 2) || ($profileId != 2 && $userId != -1 ))
                $userSql = "AND d.user_id_n='$userId' ";
            if (isset($startLimit) && isset($maxElement))
                $limitSql = "LIMIT $startLimit, $maxElement   ";
            if ($campaignId != -1 && $campaignId != "")
                $campaignSql = " AND a.campaign_id_n = '$campaignId' ";
            if ($offerId != -1)
                $offerSql = "AND a.offer_id_n='$offerId' ";

            $sqlQuery = "
                      
                    select  date_format(a.event_datetime_d,'%d/%m/%Y %H:%i:%s') DATE
                                ,a.telephone_v MSISDN
                                ,c.short_code_v IDENTIFIANT
                                ,c.offer_name_v OFFRE
                                ,c.keyword_v \"MOT-CLE\"
                                ,b.campaign_name_v CAMPAGNE
                                ,d.user_name_v LOGIN
                        from  subs_offer_details_tab a,
                                    master_campaigns_tab b,
                                    iad_master_offers_tab c ,
                                    master_users_tab d
                        where a.campaign_id_n = b.campaign_id_n
                        and b.owner_id_n = d.user_id_n
                        and c.offer_id_n = a.offer_id_n
                        and a.event_datetime_d>='$startDate'
                        and a.event_datetime_d <='$endDate'
                        $profileSql
                        $userSql
                        $campaignSql
                        $offerSql
                        order by DATE  desc
                        $limitSql
                        

                ";

            // echo $sqlQuery;

            $sqlResult = mysql_query($sqlQuery, $dbAccess['dbConnection']);
            $this->closeMysqlConnection($dbAccess['dbConnection']);
        }

        return $this->mysqlRessourceJson($sqlResult);
    }

    public function getIadDetailledSharingStatParam($profileId, $userId, $campaignId, $startLimit, $maxElement, $startDate, $endDate) {

        $dbAccess = $this->connectMysqlDatabase();

        if (!$dbAccess['dbConnection']) {
            $logDebug = " Echec getIadDetailledStatParam()  " . $dbAccess['errorMessage'] . "  " . $dbAccess['errorCode'] . " MSISDN :: $this->msisdn";
            $this->svaLog->writeSystemLog($logDebug);
            die("IMPOSSIBLE DE SE CONNECTER A LA BASE DE DONNEES");
        } else {

            $profileSql = "";
            $userSql = "";
            $limitSql = "";
            $campaignSql = "";

            if ($profileId == 2)
                $profileSql = "AND d.profile_id_n='$profileId' ";
            if (($userId == -1 && $profileId == 2) || ($profileId != 2 && $userId != -1 ))
                $userSql = "AND d.user_id_n='$userId' ";
            if (isset($startLimit) && isset($maxElement))
                $limitSql = "LIMIT $startLimit, $maxElement   ";
            if ($campaignId != -1 && $campaignId != "")
                $campaignSql = " AND a.campaign_id_n = '$campaignId' ";

            $sqlQuery = "
                      
                    select  date_format(a.event_datetime_d,'%d/%m/%Y %H:%i:%s') DATE
                                ,a.a_number_v EMETTEUR
                                ,a.b_number_v DESTINATAIRE
                                ,d.user_name_v LOGIN
                                ,b.campaign_name_v CAMPAGNE
                        from  subs_sharing_details_tab a,
                                    master_campaigns_tab b,
                                    master_users_tab d
                        where a.campaign_id_n = b.campaign_id_n
                        and b.owner_id_n = d.user_id_n
                        and a.event_datetime_d>='$startDate'
                        and a.event_datetime_d <='$endDate'
                        $profileSql
                        $userSql
                        $campaignSql
                        order by DATE  desc
                        $limitSql
                        

                ";


            // echo $sqlQuery;
            $sqlResult = mysql_query($sqlQuery, $dbAccess['dbConnection']);
            $this->closeMysqlConnection($dbAccess['dbConnection']);
        }

        return $this->mysqlRessourceJson($sqlResult);
    }

    public function getGroupDetailByGroupIdInside($groupId) {

        $dbAccess = $this->connectMysqlDatabase();

        if (!$dbAccess['dbConnection']) {
            $logDebug = " Echec getGroupDetailByGroupId()  " . $dbAccess['errorMessage'] . "  " . $dbAccess['errorCode'] . " MSISDN :: $this->msisdn";
            $this->svaLog->writeSystemLog($logDebug);
            die("IMPOSSIBLE DE SE CONNECTER A LA BASE DE DONNEES");
        } else {

            $sqlQuery = "
                    
                  select  detail_id_n
                            , group_id_n
                            , creation_date_d
                            , entity_name_v
                            , additionnal_info_v
                            , phone_number_v
                            , email_v
                            , char_field_1
                            , char_field_2
                            , char_field_3
                            , char_field_4
                            , char_field_5
                            , status_n 
                    from group_details_tab
                    where group_id_n='$groupId'
                ";


            // echo $sqlQuery;
            $sqlResult = mysql_query($sqlQuery, $dbAccess['dbConnection']);

            $this->closeMysqlConnection($dbAccess['dbConnection']);
        }

        return $this->mysqlRessourceJson($sqlResult);
    }

    public function upldQueuesProcessor($campaignId, $phone, $entity, $statut) {

        $dbAccess = $this->getMysqlConnection();
        $totalUser = 0;

        if (!$dbAccess['dbConnection']) {
            $logDebug = " Echec upldQueuesProcessor()  " . $dbAccess['errorMessage'] . "  " . $dbAccess['errorCode'] . " MSISDN :: $msisdn";
            $this->svaLog->writeSystemLog($logDebug);
            die("IMPOSSIBLE DE SE CONNECTER A LA BASE DE DONNEES");
        } else {


            $sqlQuery = "INSERT INTO queues_processor_tab (item_id_n, event_datetime_d, campaign_id_n, phone_number_v, entity_v, status_n ) 
                             VALUES (NULL, now(), '$campaignId','$phone','$entity','$statut')";
            $queryResult = mysql_query($sqlQuery, $dbAccess['dbConnection']);
            //echo $sqlQuery;
            // $totalUser = mysql_affected_rows($queryResult);
            $this->closeMysqlConnection($dbAccess['dbConnection']);
        }

        if ($queryResult)
            $totalUser = 1;
        return $totalUser;
    }

    public function getCurrentCampaignStatus($campaignId) {

        $dbAccess = $this->getMysqlConnection();
        $total = 0;

        if (!$dbAccess['dbConnection']) {
            $logDebug = " Echec getCurrentCampaignStatus()  " . $dbAccess['errorMessage'] . "  " . $dbAccess['errorCode'] . " MSISDN :: $msisdn";
            $this->svaLog->writeSystemLog($logDebug);
            die("IMPOSSIBLE DE SE CONNECTER A LA BASE DE DONNEES");
        } else {


            $sqlQuery = "select status_n statut from master_campaigns_tab where campaign_id_n='$campaignId'";
            $queryResult = mysql_query($sqlQuery, $dbAccess['dbConnection']);
            //echo $sqlQuery;
            $data = mysql_fetch_array($queryResult);
            $total = $data['statut'];
            $this->closeMysqlConnection($dbAccess['dbConnection']);
        }


        return $total;
    }

    public function updateQueuedEventsBulk($campaignId, $newStatus, $oldStatus) {
        $dbAccess = $this->getMysqlConnection();
        $id = 0;

        if (!$dbAccess['dbConnection']) {
            $logDebug = " Echec updateQueuedEventsBulk()  " . $dbAccess['errorMessage'] . "  " . $dbAccess['errorCode'] . " MSISDN :: $msisdn";
            $this->svaLog->writeSystemLog($logDebug);
            die("IMPOSSIBLE DE SE CONNECTER A LA BASE DE DONNEES");
        } else {


            $sqlQuery = "UPDATE broadcast_queues_tab
                             SET  status_n ='$newStatus'
                            where campaign_id_n = '$campaignId'
                            and status_n='$oldStatus'
                          ";
            //echo $sqlQuery;
            $queryResult = mysql_query($sqlQuery, $dbAccess['dbConnection']);
            $this->closeMysqlConnection($dbAccess['dbConnection']);
        }

        return $queryResult;
    }

    public function updateCampaignStatus($campaignId, $newStatus) {
        $dbAccess = $this->getMysqlConnection();
        $id = 0;

        if (!$dbAccess['dbConnection']) {
            $logDebug = " Echec updateCampaignStatus()  " . $dbAccess['errorMessage'] . "  " . $dbAccess['errorCode'] . " MSISDN :: $msisdn";
            $this->svaLog->writeSystemLog($logDebug);
            die("IMPOSSIBLE DE SE CONNECTER A LA BASE DE DONNEES");
        } else {


            $sqlQuery = "UPDATE master_campaigns_tab
                             SET  status_n ='$newStatus'
                            where campaign_id_n = '$campaignId'
                          ";
            //echo $sqlQuery;
            $queryResult = mysql_query($sqlQuery, $dbAccess['dbConnection']);
            $id = mysql_insert_id($dbAccess['dbConnection']);
            $this->closeMysqlConnection($dbAccess['dbConnection']);
        }

        return $id;
    }

    public function updateQueuedProcessordEventsBulk($campaignId, $newStatus, $oldStatus) {
        $dbAccess = $this->getMysqlConnection();
        $id = 0;

        if (!$dbAccess['dbConnection']) {
            $logDebug = " Echec updateQueuedProcessordEventsBulk()  " . $dbAccess['errorMessage'] . "  " . $dbAccess['errorCode'] . " MSISDN :: $msisdn";
            $this->svaLog->writeSystemLog($logDebug);
            die("IMPOSSIBLE DE SE CONNECTER A LA BASE DE DONNEES");
        } else {


            $sqlQuery = "UPDATE queues_processor_tab
                             SET  status_n ='$newStatus'
                            where campaign_id_n = '$campaignId'
                            and status_n in ($oldStatus)
                          ";
            //echo $sqlQuery;
            $queryResult = mysql_query($sqlQuery, $dbAccess['dbConnection']);
            $this->closeMysqlConnection($dbAccess['dbConnection']);
        }

        return $queryResult;
    }

    public function getStdMenuDesc($menuId, $lang) {

        $dbAccess = $this->connectMysqlDatabase();

        if (!$dbAccess['dbConnection']) {
            $logDebug = " Echec getMenuDesc()  " . $dbAccess['errorMessage'] . "  " . $dbAccess['errorCode'] . " MSISDN :: $this->msisdn";
            $this->svaLog->writeSystemLog($logDebug);
            die("IMPOSSIBLE DE SE CONNECTER A LA BASE DE DONNEES");
        } else {


            $sqlQuery = "select * from std_menu_builder_tab where menu_id_n='$menuId' and language_id_n='$lang'";

            //echo  $sqlQuery ;
            $sqlResult = mysql_query($sqlQuery, $dbAccess['dbConnection']);

            //echo $sqlResult;
            $queryData = mysql_fetch_array($sqlResult);
            $this->closeMysqlConnection($dbAccess['dbConnection']);
            // print_r($queryData);
            // echo mysql_error();
        }

        return $queryData;
    }

    public function getStdMenuDetail($menuId, $lang) {

        $dbAccess = $this->connectMysqlDatabase();

        if (!$dbAccess['dbConnection']) {
            $logDebug = " Echec getMenuDetail()  " . $dbAccess['errorMessage'] . "  " . $dbAccess['errorCode'] . " MSISDN :: $this->msisdn";
            $this->svaLog->writeSystemLog($logDebug);
            die("IMPOSSIBLE DE SE CONNECTER A LA BASE DE DONNEES");
        } else {


            $sqlQuery = "select * from std_menu_detail_tab  where parent_menu_id='$menuId' and language_id_n='$lang' order by menu_order_n asc";

            //echo  $sqlQuery ;
            $sqlResult = mysql_query($sqlQuery, $dbAccess['dbConnection']);

            //echo $sqlResult;
            $this->closeMysqlConnection($dbAccess['dbConnection']);
            // print_r($queryData);
            // echo mysql_error();
        }

        return $this->mysqlRessourceJson($sqlResult);
    }

    public function getSystemUsersAll($option, $startLimit, $maxElement) {

        $dbAccess = $this->connectMysqlDatabase();

        if (!$dbAccess['dbConnection']) {
            $logDebug = " Echec getSystemUsersAll()  " . $dbAccess['errorMessage'] . "  " . $dbAccess['errorCode'] . " MSISDN :: $this->msisdn";
            $this->svaLog->writeSystemLog($logDebug);
            die("IMPOSSIBLE DE SE CONNECTER A LA BASE DE DONNEES");
        } else {


            $limitSql = "";

            if ($option)
                $limitSql = "LIMIT $startLimit,$maxElement";

            $sqlQuery = "
                    
                   select  a.user_id_n
                        ,a.user_name_v
                        ,a.first_name_v
                        ,a.last_name_v
                        ,date_format(a.creation_date_d,'%d/%m/%Y %H:%i:%s') creationDate
                        ,a.mobile_phone_v
                        ,a.email_v
                        ,b.profile_name_v 
                        ,c.description_v status_desc
                        ,a.status_n
                        ,e.billing_name_v
                from master_users_tab a,
                    master_profile_tab b,
                    master_status_tab c,
                    mmg_user_billing_tab d,
                    master_billing_tab e
                where a.profile_id_n = b.profile_id_n
                and a.status_n = c.status_n
                and d.user_id_n = a.user_id_n
                and e.billing_id_n = d.billing_id_n
                $limitSql
                ";


            //echo $sqlQuery;
            $req = mysql_query($sqlQuery, $dbAccess['dbConnection']) or die("error" . mysql_error());

            $this->closeMysqlConnection($dbAccess['dbConnection']);
        }

        return $req;
    }

    public function getProfileList() {

        $dbAccess = $this->connectMysqlDatabase();

        if (!$dbAccess['dbConnection']) {
            $logDebug = " Echec getProfileList()  " . $dbAccess['errorMessage'] . "  " . $dbAccess['errorCode'] . " MSISDN :: $this->msisdn";
            $this->svaLog->writeSystemLog($logDebug);
            die("IMPOSSIBLE DE SE CONNECTER A LA BASE DE DONNEES");
        } else {

            $sqlQuery = "  
                   select   profile_id_n
                        , profile_name_v
                        , long_desc_v
                        , status_n 
                    FROM master_profile_tab
                ";


            // echo $sqlQuery;
            $sqlResult = mysql_query($sqlQuery, $dbAccess['dbConnection']);

            $this->closeMysqlConnection($dbAccess['dbConnection']);
        }

        return $this->mysqlRessourceJson($sqlResult);
    }

    public function updateMainSubsInfo($rowId, $firstName, $lastName, $phoneNumber, $email) {

        $dbAccess = $this->getMysqlConnection();
        $totalUser = 0;

        if (!$dbAccess['dbConnection']) {
            $logDebug = " Echec updateMainSubsInfo()  " . $dbAccess['errorMessage'] . "  " . $dbAccess['errorCode'] . " MSISDN :: $msisdn";
            $this->svaLog->writeSystemLog($logDebug);
            die("IMPOSSIBLE DE SE CONNECTER A LA BASE DE DONNEES");
        } else {


            $sqlQuery = "update master_users_tab  
                                    set  first_name_v='$firstName'
                                        ,last_name_v = '$lastName'
                                        ,mobile_phone_v = '$phoneNumber'
                                        ,email_v = '$email'
                             where user_id_n='$rowId'";

            //echo $sqlQuery;

            $queryResult = mysql_query($sqlQuery, $dbAccess['dbConnection']);
            //echo $sqlQuery;
            // $totalUser = mysql_affected_rows($queryResult);
            $this->closeMysqlConnection($dbAccess['dbConnection']);
        }

        if ($queryResult)
            $totalUser = 1;
        return $totalUser;
    }

    public function updateMyAccountPassword($rowId, $newPassword) {

        $dbAccess = $this->getMysqlConnection();
        $totalUser = 0;

        if (!$dbAccess['dbConnection']) {
            $logDebug = " Echec updateMyAccountPassword()  " . $dbAccess['errorMessage'] . "  " . $dbAccess['errorCode'] . " MSISDN :: $msisdn";
            $this->svaLog->writeSystemLog($logDebug);
            die("IMPOSSIBLE DE SE CONNECTER A LA BASE DE DONNEES");
        } else {


            $sqlQuery = "update master_users_tab  
                                    set  password_v='$newPassword'
                             where user_id_n='$rowId'";
            $queryResult = mysql_query($sqlQuery, $dbAccess['dbConnection']);
            //echo $sqlQuery;
            // $totalUser = mysql_affected_rows($queryResult);
            $this->closeMysqlConnection($dbAccess['dbConnection']);
        }

        if ($queryResult)
            $totalUser = 1;
        return $totalUser;
    }

    public function addUserShort($username, $password, $firstName, $lastName, $profileId, $mobilePhone, $emailId, $ownerId, $fallback) {
        $dbAccess = $this->getMysqlConnection();
        $id = -1;

        if (!$dbAccess['dbConnection']) {
            $logDebug = " Echec addUserShort()  " . $dbAccess['errorMessage'] . "  " . $dbAccess['errorCode'] . " MSISDN :: $msisdn";
            $this->svaLog->writeSystemLog($logDebug);
            die("IMPOSSIBLE DE SE CONNECTER A LA BASE DE DONNEES");
        } else {


            $sqlQuery = "INSERT 
                INTO master_users_tab  (user_id_n,creation_date_d,user_name_v,password_v,profile_id_n,creator_id_n, first_name_v, last_name_v, mobile_phone_v, email_v,char_field_10, status_n )
                VALUES (NULL,now(),'$username','$password','$profileId','$ownerId','$firstName','$lastName','$mobilePhone','$emailId','$fallback','0')";
            //echo $sqlQuery;
            mysql_query($sqlQuery, $dbAccess['dbConnection']);
            $id = mysql_insert_id();

            $sqlQuery = "INSERT 
                INTO master_stock_tab  
                VALUES (NULL,'$id','0','','','')";
            //echo $sqlQuery;
            mysql_query($sqlQuery, $dbAccess['dbConnection']);

            $this->closeMysqlConnection($dbAccess['dbConnection']);
        }


        return $id;
    }

    public function addNewApi($username, $inUrl, $outUrl, $dlrUrl, $appKey, $clientIp) {
        $dbAccess = $this->getMysqlConnection();

        if (!$dbAccess['dbConnection']) {
            $logDebug = " Echec addUserShort()  " . $dbAccess['errorMessage'] . "  " . $dbAccess['errorCode'] . " MSISDN :: $msisdn";
            $this->svaLog->writeSystemLog($logDebug);
            die("IMPOSSIBLE DE SE CONNECTER A LA BASE DE DONNEES");
        } else {


            $sqlQuery = "INSERT 
                INTO client_api_tab 
                VALUES (NULL,'$username','$inUrl','$outUrl','$dlrUrl','$appKey','$clientIp',NULL,NULL,NULL,NULL,NULL,'0')";
            //echo $sqlQuery;
            $queryResult = mysql_query($sqlQuery, $dbAccess['dbConnection']);
            $this->closeMysqlConnection($dbAccess['dbConnection']);
        }

        return $queryResult;
    }

    public function userChangeStatus($userId, $newStatus) {
        $dbAccess = $this->getMysqlConnection();
        $id = 0;

        if (!$dbAccess['dbConnection']) {
            $logDebug = " Echec userChangeStatus()  " . $dbAccess['errorMessage'] . "  " . $dbAccess['errorCode'] . " MSISDN :: $msisdn";
            $this->svaLog->writeSystemLog($logDebug);
            die("IMPOSSIBLE DE SE CONNECTER A LA BASE DE DONNEES");
        } else {


            $sqlQuery = " UPDATE master_users_tab SET status_n='$newStatus' WHERE user_id_n='$userId'
                          ";
            //echo $sqlQuery;
            $queryResult = mysql_query($sqlQuery, $dbAccess['dbConnection']);
            $this->closeMysqlConnection($dbAccess['dbConnection']);
        }

        return $queryResult;
    }

    public function getSystemUserById($userId) {

        $dbAccess = $this->connectMysqlDatabase();

        if (!$dbAccess['dbConnection']) {
            $logDebug = " Echec getSystemUserById()  " . $dbAccess['errorMessage'] . "  " . $dbAccess['errorCode'] . " MSISDN :: $this->msisdn";
            $this->svaLog->writeSystemLog($logDebug);
            die("IMPOSSIBLE DE SE CONNECTER A LA BASE DE DONNEES");
        } else {

            $sqlQuery = "  
                 select   user_id_n
                        , user_name_v
                        , profile_id_n
                        , first_name_v
                        , last_name_v
                        , email_v
                        , mobile_phone_v 
                    FROM master_users_tab
                    WHERE user_id_n='$userId'
                ";


            // echo $sqlQuery;
            $sqlResult = mysql_query($sqlQuery, $dbAccess['dbConnection']);
            $data = mysql_fetch_array($sqlResult);
            $this->closeMysqlConnection($dbAccess['dbConnection']);
        }

        return $data;
    }

    public function updateMainSubsInfoAdmin($rowId, $firstName, $lastName, $phoneNumber, $email, $profileId, $fallback) {

        $dbAccess = $this->getMysqlConnection();
        $totalUser = 0;

        if (!$dbAccess['dbConnection']) {
            $logDebug = " Echec updateMainSubsInfoAdmin()  " . $dbAccess['errorMessage'] . "  " . $dbAccess['errorCode'] . " MSISDN :: $msisdn";
            $this->svaLog->writeSystemLog($logDebug);
            die("IMPOSSIBLE DE SE CONNECTER A LA BASE DE DONNEES");
        } else {


            $sqlQuery = "update master_users_tab  
                                    set  first_name_v='$firstName'
                                        ,last_name_v = '$lastName'
                                        ,mobile_phone_v = '$phoneNumber'
                                        ,email_v = '$email'
                                        ,profile_id_n ='$profileId'
                                        , char_field_10 = '$fallback'
                             where user_id_n='$rowId'";

            //echo $sqlQuery;

            $queryResult = mysql_query($sqlQuery, $dbAccess['dbConnection']);
            //echo $sqlQuery;
            // $totalUser = mysql_affected_rows($queryResult);
            $this->closeMysqlConnection($dbAccess['dbConnection']);
        }

        if ($queryResult)
            $totalUser = 1;
        return $totalUser;
    }

    public function updateUserCredit($Id, $credit) {

        $dbAccess = $this->getMysqlConnection();
        $totalUser = 0;

        if (!$dbAccess['dbConnection']) {
            $logDebug = " Echec updateMainSubsInfoAdmin()  " . $dbAccess['errorMessage'] . "  " . $dbAccess['errorCode'] . " MSISDN :: $msisdn";
            $this->svaLog->writeSystemLog($logDebug);
            die("IMPOSSIBLE DE SE CONNECTER A LA BASE DE DONNEES");
        } else {


            $sqlQuery = "update master_stock_tab  
                                    set  balance_n='$credit'
                             where stock_id_n='$Id'";

            //echo $sqlQuery;

            $queryResult = mysql_query($sqlQuery, $dbAccess['dbConnection']) or die("error" . mysql_error());
            //echo $sqlQuery;
            // $totalUser = mysql_affected_rows($queryResult);
            $this->closeMysqlConnection($dbAccess['dbConnection']);
        }
    }

    public function dropUserById($userId) {

        $dbAccess = $this->getMysqlConnection();
        $status = false;

        if (!$dbAccess['dbConnection']) {
            $logDebug = " Echec dropUserById()  " . $dbAccess['errorMessage'] . "  " . $dbAccess['errorCode'] . " MSISDN :: $msisdn";
            $this->svaLog->writeSystemLog($logDebug);
            die("IMPOSSIBLE DE SE CONNECTER A LA BASE DE DONNEES");
        } else {


            $sqlQuery = "DELETE FROM   master_users_tab
                WHERE user_id_n='$userId'";
            //echo $sqlQuery;
            $status = mysql_query($sqlQuery, $dbAccess['dbConnection']);
            $this->closeMysqlConnection($dbAccess['dbConnection']);
        }

        return $status;
    }

    public function createGroupe($goupName, $groupDesc, $moduleId, $groupOwner, $groupType, $groupEnabled, $otherEnabled, $charfield, $statut) {
        $dbAccess = $this->getMysqlConnection();

        if (!$dbAccess['dbConnection']) {
            $logDebug = " Echec createGroupe()  " . $dbAccess['errorMessage'] . "  " . $dbAccess['errorCode'] . " MSISDN :: $msisdn";
            $this->svaLog->writeSystemLog($logDebug);
            die("IMPOSSIBLE DE SE CONNECTER A LA BASE DE DONNEES");
        } else {


            $sqlQuery = "INSERT 
                INTO master_groups_tab  
                VALUES (NULL,now(),'$goupName','$groupDesc','$moduleId','$groupOwner','$groupType','$groupEnabled','$otherEnabled','$charfield[0]','$charfield[1]','$charfield[2]','$charfield[3]','$charfield[4]','$statut')";
            //echo $sqlQuery;
            $queryResult = mysql_query($sqlQuery, $dbAccess['dbConnection']);
            $id = mysql_insert_id($dbAccess['dbConnection']);
            $this->closeMysqlConnection($dbAccess['dbConnection']);
        }

        return $id;
    }

    public function upldGroupDetail($groupId, $entityName, $additionalInfo, $contact, $email, $charfield, $statut) {

        $dbAccess = $this->getMysqlConnection();
        $status = false;

        if (!$dbAccess['dbConnection']) {
            $logDebug = " Echec upldGroupDetail()  " . $dbAccess['errorMessage'] . "  " . $dbAccess['errorCode'] . " MSISDN :: $msisdn";
            $this->svaLog->writeSystemLog($logDebug);
            die("IMPOSSIBLE DE SE CONNECTER A LA BASE DE DONNEES");
        } else {


            $sqlQuery = "INSERT 
                INTO group_details_tab   
                VALUES (null,'$groupId',now(),'$entityName','$additionalInfo','$contact','$email','$charfield[0]','$charfield[1]','$charfield[2]','$charfield[3]','$charfield[4]','$statut')";
            //echo $sqlQuery;
            $status = mysql_query($sqlQuery, $dbAccess['dbConnection']);
            $id = mysql_insert_id($dbAccess['dbConnection']);
            $this->closeMysqlConnection($dbAccess['dbConnection']);
        }

        return $id;
    }

    public function getGroupDtlsByOwner($ownerId, $groupType, $groupId, $startLimit) {

        $dbAccess = $this->connectMysqlDatabase();

        if (!$dbAccess['dbConnection']) {
            $logDebug = " Echec getGroupDtlsByOwner()  " . $dbAccess['errorMessage'] . "  " . $dbAccess['errorCode'] . " MSISDN :: $this->msisdn";
            $this->svaLog->writeSystemLog($logDebug);
            die("IMPOSSIBLE DE SE CONNECTER A LA BASE DE DONNEES");
        } else {


            $sqlQuery = "
                    
                    select * 
                    from   master_group_tab a
                          ,group_details_tab b
                    where a.group_id_n = b.group_id_n
                    and a.owner_id_n='$ownerId' 
                    and a.group_type_n='$groupType'
                    and a.group_id_n = '$groupId'
                    order by b.first_name_v asc


                ";
            //echo $sqlQuery;
            $sqlResult = mysql_query($sqlQuery, $dbAccess['dbConnection']);
            $this->closeMysqlConnection($dbAccess['dbConnection']);
        }

        return $this->mysqlRessourceJson($sqlResult);
    }

    public function getGroupByOwnerId($profileId, $ownerId, $option, $startLimit, $maxElement) {

        $dbAccess = $this->connectMysqlDatabase();

        if (!$dbAccess['dbConnection']) {
            $logDebug = " Echec getProfileList()  " . $dbAccess['errorMessage'] . "  " . $dbAccess['errorCode'] . " MSISDN :: $this->msisdn";
            $this->svaLog->writeSystemLog($logDebug);
            die("IMPOSSIBLE DE SE CONNECTER A LA BASE DE DONNEES");
        } else {

            $limitQuery = "";
            if ($option)
                $limitQuery = "LIMIT $startLimit, $maxElement ";


            if ($profileId == 1) {

                $sqlQuery = "  
                   select  date_format(a.creation_date_d,'%d/%m/%Y %H:%I:%S') eventDate
                            ,a.group_id_n
                            ,a.group_name_v
                            ,a.group_type_n
                            ,b.user_name_v
                            ,a.short_desc_v
                     from master_groups_tab a,
                          master_users_tab b
                     where a.owner_id_n =  b.user_id_n
                     and a.group_id_n <> '1'
                     ORDER BY a.group_id_n DESC
                     $limitQuery
                     

                ";
            } else {



                $sqlQuery = " 
                    
select  date_format(a.creation_date_d,'%d/%m/%Y %H:%I:%S') eventDate
                            ,a.group_id_n
                            ,a.group_name_v
                            ,a.group_type_n
                            ,b.user_name_v
                            ,a.short_desc_v
                     from master_groups_tab a,
                          master_users_tab b
                     where a.owner_id_n =  b.user_id_n
                     and a.owner_id_n = '$ownerId'
                     and a.group_id_n <> '1'
                          ORDER BY a.group_id_n DESC
                     $limitQuery
                         
                ";
            }






            // echo $sqlQuery;
            $sqlResult = mysql_query($sqlQuery, $dbAccess['dbConnection']);

            $this->closeMysqlConnection($dbAccess['dbConnection']);
        }

        return $this->mysqlRessourceJson($sqlResult);
    }

    public function getGroupDetail($groupId, $option, $startLimit, $maxElement) {

        $dbAccess = $this->connectMysqlDatabase();

        if (!$dbAccess['dbConnection']) {
            $logDebug = " Echec getGroupDetail()  " . $dbAccess['errorMessage'] . "  " . $dbAccess['errorCode'] . " MSISDN :: $this->msisdn";
            $this->svaLog->writeSystemLog($logDebug);
            die("IMPOSSIBLE DE SE CONNECTER A LA BASE DE DONNEES");
        } else {

            $limitQuery = "";
            if ($option)
                $limitQuery = "LIMIT $startLimit, $maxElement ";

            $sqlQuery = "  
                  select  date_format(c.creation_date_d,'%d/%m/%Y %H:%I:%S') eventDate
                            ,a.group_id_n
                            ,a.group_name_v
                            ,a.group_type_n
                            ,b.user_name_v
                            ,a.short_desc_v
                            ,c.phone_number_v
                     from master_groups_tab a,
                          master_users_tab b,
                          group_details_tab c
                     where a.owner_id_n =  b.user_id_n
                     and c.group_id_n = a.group_id_n
                     and c.group_id_n = '$groupId'
                     ORDER BY a.group_id_n DESC
                     $limitQuery
                     

                ";






            // echo $sqlQuery;
            $sqlResult = mysql_query($sqlQuery, $dbAccess['dbConnection']);

            $this->closeMysqlConnection($dbAccess['dbConnection']);
        }

        return $this->mysqlRessourceJson($sqlResult);
    }

    public function deleteGroupAndDataById($groupId) {

        $dbAccess = $this->getMysqlConnection();
        $totalUser = 0;

        if (!$dbAccess['dbConnection']) {
            $logDebug = " Echec deleteIadApplication()  " . $dbAccess['errorMessage'] . "  " . $dbAccess['errorCode'] . " MSISDN :: $msisdn";
            $this->svaLog->writeSystemLog($logDebug);
            die("IMPOSSIBLE DE SE CONNECTER A LA BASE DE DONNEES");
        } else {


            $sqlQuery = "delete from master_groups_tab where  group_id_n='$groupId'";
            $queryResult = mysql_query($sqlQuery, $dbAccess['dbConnection']);

            $sqlQuery = "delete from group_details_tab where  group_id_n='$groupId'";
            $queryResult = mysql_query($sqlQuery, $dbAccess['dbConnection']);
            // $totalUser = mysql_affected_rows($queryResult);
            $this->closeMysqlConnection($dbAccess['dbConnection']);
        }

        if ($queryResult)
            $totalUser = 1;
        return $totalUser;
    }

    public function uploadNotificationPhone($campaignId, $entityId, $statut) {
        $dbAccess = $this->getMysqlConnection();

        if (!$dbAccess['dbConnection']) {
            $logDebug = " Echec createGroupe()  " . $dbAccess['errorMessage'] . "  " . $dbAccess['errorCode'] . " MSISDN :: $msisdn";
            $this->svaLog->writeSystemLog($logDebug);
            die("IMPOSSIBLE DE SE CONNECTER A LA BASE DE DONNEES");
        } else {


            $sqlQuery = "INSERT 
                INTO campaign_notification_tab  
                VALUES (NULL,'$campaignId','$entityId','$statut')";
            //echo $sqlQuery;
            $queryResult = mysql_query($sqlQuery, $dbAccess['dbConnection']);
            $id = mysql_insert_id($dbAccess['dbConnection']);
            $this->closeMysqlConnection($dbAccess['dbConnection']);
        }

        return $id;
    }

    public function updateMasterCampaignMetaData($campaignId, $current, $total) {
        $dbAccess = $this->getMysqlConnection();
        $id = 0;

        if (!$dbAccess['dbConnection']) {
            $logDebug = " Echec updateQueuedEventsBulk()  " . $dbAccess['errorMessage'] . "  " . $dbAccess['errorCode'] . " MSISDN :: $msisdn";
            $this->svaLog->writeSystemLog($logDebug);
            die("IMPOSSIBLE DE SE CONNECTER A LA BASE DE DONNEES");
        } else {


            $sqlQuery = "UPDATE master_campaigns_tab
                             SET  char_field_2 =char_field_2 + $total
                                 , char_field_3 =char_field_3 + $current
                            where campaign_id_n = '$campaignId'
                          ";
            //echo $sqlQuery;
            $queryResult = mysql_query($sqlQuery, $dbAccess['dbConnection']);
            $this->closeMysqlConnection($dbAccess['dbConnection']);
        }

        return $queryResult;
    }

    public function getActiveCampaignByProfile($profileId, $userId, $campaignId, $option, $startLimit, $maxElement, $startDate, $endDate) {

        $dbAccess = $this->connectMysqlDatabase();

        if (!$dbAccess['dbConnection']) {
            $logDebug = " Echec getCampaignByProfile()  " . $dbAccess['errorMessage'] . "  " . $dbAccess['errorCode'] . " MSISDN :: $this->msisdn";
            $this->svaLog->writeSystemLog($logDebug);
            die("IMPOSSIBLE DE SE CONNECTER A LA BASE DE DONNEES");
        } else {

            $limitSql = "";
            $campaignSql = "";
            $dateSql = "";
            if ($option)
                $limitSql = "LIMIT $startLimit, $maxElement";

            if ($campaignId != -1) {

                $campaignSql = "AND a.campaign_id_n = '$campaignId'";
            } else {

                $dateSql = "and a.creation_date_d>='$startDate'";
                $dateSql .= " and a.creation_date_d<='$endDate'";
            }


            if ($profileId == 1 || $profileId == 3) {
                $sqlQuery = "  
                 select    a.campaign_id_n
                        , a.campaign_name_v
                        , a.owner_id_n 
                        , concat(a.start_date_d,' ',a.start_time_d) start_datetime_d
                        , concat(a.end_date_d,' ',a.end_time_d) end_datetime_d  
                        ,a.creation_date_d
                        , b.item_id_n
                        , b.start_datetime_d
                        , b.end_datetime_d
                        , b.total_count_n
                        , b.total_processed_n
                        , b.total_received_n
                        , b.total_canceled_n
                        , b.total_failed_n
                        , b.total_expired_n
                        , b.total_susbcription_n
                        , b.total_forwarded_n
                        , b.total_rejected_n
                        , b.total_dlvr_phone_n
                        , b.total_dlvr_smsc_n
                        , b.total_undlvr_phone_n
                        , b.total_queued_smsc_n
                 FROM master_campaigns_tab a,
                      campaign_summary_tab b
                 where a.campaign_id_n = b.campaign_id_n
                 $dateSql
                 $campaignSql
                 order by a.campaign_id_n desc
                 $limitSql
                ";
            } elseif ($profileId == 2) {

                $sqlQuery = "  
                        select    a.campaign_id_n
                               , a.campaign_name_v
                               , a.owner_id_n 
                               , concat(a.start_date_d,' ',a.start_time_d) start_datetime_d
                               , concat(a.end_date_d,' ',a.end_time_d) end_datetime_d  
                               ,a.creation_date_d
                               , b.item_id_n
                               , b.start_datetime_d
                               , b.end_datetime_d
                               , b.total_count_n
                               , b.total_processed_n
                               , b.total_received_n
                               , b.total_canceled_n
                               , b.total_failed_n
                               , b.total_expired_n
                               , b.total_susbcription_n
                               , b.total_forwarded_n
                               , b.total_rejected_n
                               , b.total_dlvr_phone_n
                               , b.total_dlvr_smsc_n
                               , b.total_undlvr_phone_n
                               , b.total_queued_smsc_n
                        FROM master_campaigns_tab a,
                             campaign_summary_tab b
                        where a.campaign_id_n = b.campaign_id_n
                        and a.owner_id_n='$userId'
                        $dateSql
                        $campaignSql
                        order by a.campaign_id_n desc
                        $limitSql

                ";
            }
            //echo $sqlQuery;
            $sqlResult = mysql_query($sqlQuery, $dbAccess['dbConnection']);
            $this->closeMysqlConnection($dbAccess['dbConnection']);
        }

        return $this->mysqlRessourceJson($sqlResult);
    }

    public function getStockStats($profileId, $userId, $exp, $dest, $option, $startLimit, $maxElement, $startDate) {

        $dbAccess = $this->connectMysqlDatabase();

        if (!$dbAccess['dbConnection']) {
            $logDebug = " Echec getCampaignByProfile()  " . $dbAccess['errorMessage'] . "  " . $dbAccess['errorCode'] . " MSISDN :: $this->msisdn";
            $this->svaLog->writeSystemLog($logDebug);
            die("IMPOSSIBLE DE SE CONNECTER A LA BASE DE DONNEES");
        } else {

            $limitSql = "";
            $campaignSql = "";
            if ($option)
                $limitSql = "LIMIT $startLimit, $maxElement";


            if ($profileId == 1 || $profileId == 3) {
                $sqlQuery = "  
                 SELECT *    
                 FROM mmg_replenishments_tab
                 WHERE  
				 from_userid_n = '$exp' and to_userid_n = '$dest' and
                event_datetime_d ='$startDate'
                 order by item_id_n desc
                 $limitSql
                ";
            } elseif ($profileId == 2) {
                $sqlQuery = "  
                 select *    
                 FROM mmg_replenishments_tab
                 where  
				 from_userid_n = '$exp' and to_userid_n = '$dest' and
                event_datetime_d ='$startDate'
                 order by item_id_n desc
                 $limitSql
                ";
            }
            //echo $sqlQuery;
            $sqlResult = mysql_query($sqlQuery, $dbAccess['dbConnection']);
            $this->closeMysqlConnection($dbAccess['dbConnection']);
        }

        return $this->mysqlRessourceJson($sqlResult);
    }

    public function exportStockStats($profileId, $userId, $exp, $dest, $option, $startLimit, $maxElement, $startDate) {

        $dbAccess = $this->connectMysqlDatabase();

        if (!$dbAccess['dbConnection']) {
            $logDebug = " Echec getCampaignByProfile()  " . $dbAccess['errorMessage'] . "  " . $dbAccess['errorCode'] . " MSISDN :: $this->msisdn";
            $this->svaLog->writeSystemLog($logDebug);
            die("IMPOSSIBLE DE SE CONNECTER A LA BASE DE DONNEES");
        } else {

            $limitSql = "";
            $campaignSql = "";
            if ($option)
                $limitSql = "LIMIT $startLimit, $maxElement";


            if ($profileId == 1 || $profileId == 3) {
                $sqlQuery = "  
                 SELECT *    
                 FROM mmg_replenishments_tab
                 WHERE  
				 from_userid_n = '$exp' and to_userid_n = '$dest' and
                event_datetime_d ='$startDate'
                 order by item_id_n desc
                 $limitSql
                ";
            } elseif ($profileId == 2) {
                $sqlQuery = "  
                 select *    
                 FROM mmg_replenishments_tab
                 where  
				 from_userid_n = '$exp' and to_userid_n = '$dest' and
                event_datetime_d ='$startDate'
                 order by item_id_n desc
                 $limitSql
                ";
            }
            //echo $sqlQuery;
            $sqlResult = mysql_query($sqlQuery, $dbAccess['dbConnection']);
            $this->closeMysqlConnection($dbAccess['dbConnection']);
        }

        return $this->mysqlRessourceJson($sqlResult);
    }

    public function getReceiveSms($profileId, $userId, $dest, $option, $startLimit, $maxElement, $startDate) {

        $dbAccess = $this->connectMysqlDatabase();

        if (!$dbAccess['dbConnection']) {
            $logDebug = " Echec getCampaignByProfile()  " . $dbAccess['errorMessage'] . "  " . $dbAccess['errorCode'] . " MSISDN :: $this->msisdn";
            $this->svaLog->writeSystemLog($logDebug);
            die("IMPOSSIBLE DE SE CONNECTER A LA BASE DE DONNEES");
        } else {

            $limitSql = "";
            $campaignSql = "";
            if ($option)
                $limitSql = "LIMIT $startLimit, $maxElement";


            if ($profileId == 1 || $profileId == 3) {
                $sqlQuery = "  
                 SELECT *    
                 FROM mmg_upld_incoming_cdr_tab
                 WHERE  
				 dst_phone_v = '$dest' and
                event_datetime_d ='$startDate'
                 order by event_id_n desc
                 $limitSql
                ";
            } elseif ($profileId == 2) {
                $sqlQuery = "  
                 SELECT *    
                 FROM mmg_upld_incoming_cdr_tab
                 WHERE  
				 dst_phone_v = '$dest' and
                event_datetime_d ='$startDate'
                 order by event_id_n desc
                 $limitSql
                ";
            }
            //echo $sqlQuery;
            $sqlResult = mysql_query($sqlQuery, $dbAccess['dbConnection']);
            $this->closeMysqlConnection($dbAccess['dbConnection']);
        }

        return $this->mysqlRessourceJson($sqlResult);
    }

    public function rrgetStockStats($profileId, $userId, $exped, $dest, $option, $startLimit, $maxElement, $startDate) {

        $dbAccess = $this->connectMysqlDatabase();

        if (!$dbAccess['dbConnection']) {
            $logDebug = " Echec getCampaignByProfile()  " . $dbAccess['errorMessage'] . "  " . $dbAccess['errorCode'] . " MSISDN :: $this->msisdn";
            $this->svaLog->writeSystemLog($logDebug);
            die("IMPOSSIBLE DE SE CONNECTER A LA BASE DE DONNEES");
        } else {

            $limitSql = "";
            $campaignSql = "";
            if ($option)
                $limitSql = "LIMIT $startLimit, $maxElement";


            if ($profileId == 1 || $profileId == 3) {
                $sqlQuery = "  
                  select * from mmg_replenishments_tab where from_userid_n = '$exped' and to_userid_n ='$dest';
                 order by item_id_n desc
                 $limitSql
                ";
            } elseif ($profileId == 2) {

                $sqlQuery = "  
                    select * from mmg_replenishments_tab where from_userid_n = '$exped' and to_userid_n ='$dest';
                 order by item_id_n desc
                 $limitSql

                ";
            }
            //echo $sqlQuery;
            $sqlResult = mysql_query($sqlQuery, $dbAccess['dbConnection']);
            $this->closeMysqlConnection($dbAccess['dbConnection']);
        }

        return $this->mysqlRessourceJson($sqlResult);
    }

    public function getAllsender($profileId, $userId) {

        $dbAccess = $this->connectMysqlDatabase();

        if (!$dbAccess['dbConnection']) {
            $logDebug = " Echec getCampaignByProfile()  " . $dbAccess['errorMessage'] . "  " . $dbAccess['errorCode'] . " MSISDN :: $this->msisdn";
            $this->svaLog->writeSystemLog($logDebug);
            die("IMPOSSIBLE DE SE CONNECTER A LA BASE DE DONNEES");
        } else {

            if ($profileId == 1 || $profileId == 3) {
                $sqlQuery = "  
                select user_name_v,user_id_n  from master_users_tab,mmg_replenishments_tab where  user_id_n = to_userid_n 
                ";
            } elseif ($profileId == 2) {
                $sqlQuery = "  
                 select user_name_v,user_id_n  from master_users_tab,mmg_replenishments_tab where creator_id_n = '$userId' and user_id_n = from_userid_n 
                ";
                //echo $sqlQuery;
            }
            $sqlResult = mysql_query($sqlQuery, $dbAccess['dbConnection']);
            $this->closeMysqlConnection($dbAccess['dbConnection']);
            return $sqlResult;
        }
    }

    public function getUsernameById($userId) {

        $dbAccess = $this->connectMysqlDatabase();

        if (!$dbAccess['dbConnection']) {
            $logDebug = " Echec getCampaignByProfile()  " . $dbAccess['errorMessage'] . "  " . $dbAccess['errorCode'] . " MSISDN :: $this->msisdn";
            $this->svaLog->writeSystemLog($logDebug);
            die("IMPOSSIBLE DE SE CONNECTER A LA BASE DE DONNEES");
        } else {

            $sqlQuery = "  
                 select user_name_v from master_users_tab where  user_id_n = '$userId' 
                ";
            //echo $sqlQuery;
        }
        $sqlResult = mysql_query($sqlQuery, $dbAccess['dbConnection']);
        $data = mysql_fetch_array($sqlResult);
        $this->closeMysqlConnection($dbAccess['dbConnection']);
        return $data[0];
    }

    public function getAllReceiver($profileId, $userId) {

        $dbAccess = $this->connectMysqlDatabase();

        if (!$dbAccess['dbConnection']) {
            $logDebug = " Echec getCampaignByProfile()  " . $dbAccess['errorMessage'] . "  " . $dbAccess['errorCode'] . " MSISDN :: $this->msisdn";
            $this->svaLog->writeSystemLog($logDebug);
            die("IMPOSSIBLE DE SE CONNECTER A LA BASE DE DONNEES");
        } else {

            if ($profileId == 1 || $profileId == 3) {
                $sqlQuery = "  
                select user_name_v,user_id_n  from master_users_tab,mmg_replenishments_tab where  user_id_n = to_userid_n 
                ";
            } elseif ($profileId == 2) {
                $sqlQuery = "  
                 select user_name_v,user_id_n  from master_users_tab,mmg_replenishments_tab where creator_id_n = '$userId' and user_id_n = to_userid_n 
                ";
                //echo $sqlQuery;
            }
            $sqlResult = mysql_query($sqlQuery, $dbAccess['dbConnection']);
            $this->closeMysqlConnection($dbAccess['dbConnection']);
            return $sqlResult;
        }
    }

    public function getDestphone($profileId, $userId) {

        $dbAccess = $this->connectMysqlDatabase();

        if (!$dbAccess['dbConnection']) {
            $logDebug = " Echec getCampaignByProfile()  " . $dbAccess['errorMessage'] . "  " . $dbAccess['errorCode'] . " MSISDN :: $this->msisdn";
            $this->svaLog->writeSystemLog($logDebug);
            die("IMPOSSIBLE DE SE CONNECTER A LA BASE DE DONNEES");
        } else {

            if ($profileId == 1 || $profileId == 3) {
                $sqlQuery = "  
                select dst_phone_v from mmg_upld_incoming_cdr_tab 
                ";
            } elseif ($profileId == 2) {
                $sqlQuery = "  
                  select dst_phone_v from mmg_upld_incoming_cdr_tab
                ";
                //echo $sqlQuery;
            }
            $sqlResult = mysql_query($sqlQuery, $dbAccess['dbConnection']);
            $this->closeMysqlConnection($dbAccess['dbConnection']);
            return $sqlResult;
        }
    }

    public function getIadCallStatus($campaignId, $option) {

        $dbAccess = $this->connectMysqlDatabase();

        if (!$dbAccess['dbConnection']) {
            $logDebug = " Echec getIadCallStatus()  " . $dbAccess['errorMessage'] . "  " . $dbAccess['errorCode'] . " MSISDN :: $this->msisdn";
            $this->svaLog->writeSystemLog($logDebug);
            die("IMPOSSIBLE DE SE CONNECTER A LA BASE DE DONNEES");
        } else {

            switch ($option) {

                case 0 : $statusSql = " status_n in ('3','8') ";
                    break; // Picked up
                case 1 : $statusSql = " status_n in ('7') ";
                    break;  // Non-Picked up
                case 2 : $statusSql = " status_n not in ('7','3','8') ";
                    break;  // Non-Processed up   
            }

            $sqlQuery = "  
                 select count(1) total from queues_processor_tab where campaign_id_n='$campaignId' and  $statusSql
                ";


            // echo $sqlQuery;
            $sqlResult = mysql_query($sqlQuery, $dbAccess['dbConnection']);
            $data = mysql_fetch_array($sqlResult);

            $total = $data['total'];
            $this->closeMysqlConnection($dbAccess['dbConnection']);
        }

        return $total;
    }

    public function getIadTransfertCalls($campaignId) {

        $dbAccess = $this->connectMysqlDatabase();

        if (!$dbAccess['dbConnection']) {
            $logDebug = " Echec getIadTransfertCalls()  " . $dbAccess['errorMessage'] . "  " . $dbAccess['errorCode'] . " MSISDN :: $this->msisdn";
            $this->svaLog->writeSystemLog($logDebug);
            die("IMPOSSIBLE DE SE CONNECTER A LA BASE DE DONNEES");
        } else {



            $sqlQuery = "  
                 select count(1) total from subs_sharing_details_tab where campaign_id_n='$campaignId' 
                ";


            // echo $sqlQuery;
            $sqlResult = mysql_query($sqlQuery, $dbAccess['dbConnection']);
            $data = mysql_fetch_array($sqlResult);

            $total = $data['total'];
            $this->closeMysqlConnection($dbAccess['dbConnection']);
        }

        return $total;
    }

    public function getIadSubscriptionCalls($campaignId) {

        $dbAccess = $this->connectMysqlDatabase();

        if (!$dbAccess['dbConnection']) {
            $logDebug = " Echec getIadSubscriptionCalls()  " . $dbAccess['errorMessage'] . "  " . $dbAccess['errorCode'] . " MSISDN :: $this->msisdn";
            $this->svaLog->writeSystemLog($logDebug);
            die("IMPOSSIBLE DE SE CONNECTER A LA BASE DE DONNEES");
        } else {



            $sqlQuery = "  
                 select count(1) total from subs_offer_details_tab where campaign_id_n='$campaignId' 
                ";


            // echo $sqlQuery;
            $sqlResult = mysql_query($sqlQuery, $dbAccess['dbConnection']);
            $data = mysql_fetch_array($sqlResult);

            $total = $data['total'];
            $this->closeMysqlConnection($dbAccess['dbConnection']);
        }

        return $total;
    }

    public function getIadStatistic($profileId, $userId, $campaignId, $option, $startLimit, $maxElement, $startDate, $endDate) {

        $dbAccess = $this->connectMysqlDatabase();

        if (!$dbAccess['dbConnection']) {
            $logDebug = " Echec getCampaignByProfile()  " . $dbAccess['errorMessage'] . "  " . $dbAccess['errorCode'] . " MSISDN :: $this->msisdn";
            $this->svaLog->writeSystemLog($logDebug);
            die("IMPOSSIBLE DE SE CONNECTER A LA BASE DE DONNEES");
        } else {

            $limitSql = "";
            $campaignSql = "";
            if ($option)
                $limitSql = "LIMIT $startLimit, $maxElement";
            if ($campaignId != -1)
                $campaignSql = "AND b.campaign_id_n = '$campaignId'";

            if ($profileId == 1 || $profileId == 3) {
                $sqlQuery = "  
                 select b.campaign_id_n
                        ,b.campaign_name_v
                        ,b.owner_id_n 
                        , concat(b.start_date_d,' ',b.start_time_d) start_datetime_d
                        , concat(b.end_date_d,' ',b.end_time_d) end_datetime_d  
                        ,b.creation_date_d
                        ,b.char_field_2
                        ,a.total_picked_v
                        ,a.total_unpicked_v
                        ,a.total_forwarded_v
                        ,a.total_subscription_v
                        ,round( 100*(a.total_picked_v/a.total_count_v),2) avgSucess
                        ,c.user_name_v
                  from iad_statistics_tab a
                       ,  master_campaigns_tab b
                       ,  master_users_tab c
                  where a.campaign_id_n = b.campaign_id_n
                  and c.user_id_n = b.owner_id_n
                  and b.creation_date_d>='$startDate'
                  and b.creation_date_d<='$endDate'
                      $campaignSql
                  order by a.campaign_id_n asc
                  $limitSql
                ";
            } elseif ($profileId == 2) {



                $sqlQuery = "  
                 select b.campaign_id_n
                        ,b.campaign_name_v
                        ,b.owner_id_n 
                        , concat(b.start_date_d,' ',b.start_time_d) start_datetime_d
                        , concat(b.end_date_d,' ',b.end_time_d) end_datetime_d  
                        ,b.creation_date_d
                        ,b.char_field_2
                        ,a.total_picked_v
                        ,a.total_unpicked_v
                        ,a.total_forwarded_v
                        ,a.total_subscription_v
                        ,round(100 * (a.total_picked_v/a.total_count_v),2) avgSucess
                        ,c.user_name_v
                  from iad_statistics_tab a
                       ,  master_campaigns_tab b
                       ,  master_users_tab c
                  where a.campaign_id_n = b.campaign_id_n
                  and c.user_id_n = b.owner_id_n
                  and b.creation_date_d>='$startDate'
                  and b.creation_date_d<='$endDate'
                  and b.owner_id_n='$userId'
                      $campaignSql
                  order by a.campaign_id_n asc
                  $limitSql
                ";
            }
            // echo $sqlQuery;
            $sqlResult = mysql_query($sqlQuery, $dbAccess['dbConnection']);
            $this->closeMysqlConnection($dbAccess['dbConnection']);
        }

        return $this->mysqlRessourceJson($sqlResult);
    }

    public function getIadStatisticDetailled($profileId, $userId, $campaignId, $option, $startLimit, $maxElement, $startDate, $endDate) {

        $dbAccess = $this->connectMysqlDatabase();

        if (!$dbAccess['dbConnection']) {
            $logDebug = " Echec getIadStatisticDetailled()  " . $dbAccess['errorMessage'] . "  " . $dbAccess['errorCode'] . " MSISDN :: $this->msisdn";
            $this->svaLog->writeSystemLog($logDebug);
            die("IMPOSSIBLE DE SE CONNECTER A LA BASE DE DONNEES");
        } else {

            $limitSql = "";
            $campaignSql = "";
            if ($option)
                $limitSql = "LIMIT $startLimit, $maxElement";
            if ($campaignId != -1)
                $campaignSql = "AND b.campaign_id_n = '$campaignId'";

            if ($profileId == 1 || $profileId == 3) {
                $sqlQuery = " 
                    select b.campaign_id_n
                            ,b.campaign_name_v
                            ,b.owner_id_n 
                            , concat(b.start_date_d,' ',b.start_time_d) start_datetime_d
                            , concat(b.end_date_d,' ',b.end_time_d) end_datetime_d  
                            ,b.creation_date_d
                            ,b.char_field_2
                            ,a.entity_v
                            ,a.has_forwarded_n
                            ,a.has_subscribe_n
                            ,c.user_name_v
                             , case a.status_n
                            when 3 then 'DECROCHE'
                            when 7 then 'NON-DECROCHE'
                           end status_desc
                      from sva_upld_events_tab a
                           ,  master_campaigns_tab b
                           ,  master_users_tab c
                      where a.campaign_id_n = b.campaign_id_n
                      and c.user_id_n = b.owner_id_n
                      and b.creation_date_d>='$startDate'
                      and b.creation_date_d<='$endDate'
                          $campaignSql
                      order by a.campaign_id_n asc
                      $limitSql

                 
                ";
            } elseif ($profileId == 2) {



                $sqlQuery = "  
                     select b.campaign_id_n
                            ,b.campaign_name_v
                            ,b.owner_id_n 
                            , concat(b.start_date_d,' ',b.start_time_d) start_datetime_d
                            , concat(b.end_date_d,' ',b.end_time_d) end_datetime_d  
                            ,b.creation_date_d
                            ,b.char_field_2
                            ,a.entity_v
                            ,a.has_forwarded_n
                            ,a.has_subscribe_n
                            ,c.user_name_v
                             , case a.status_n
                                when 3 then 'DECROCHE'
                                when 7 then 'NON-DECROCHE'
                               end status_desc
                      from sva_upld_events_tab a
                           ,  master_campaigns_tab b
                           ,  master_users_tab c
                      where a.campaign_id_n = b.campaign_id_n
                      and c.user_id_n = b.owner_id_n
                      and b.creation_date_d>='$startDate'
                      and b.creation_date_d<='$endDate'
                      and b.owner_id_n='$userId'
                          $campaignSql
                      order by a.campaign_id_n asc
                      $limitSql


            
                ";
            }
            // echo $sqlQuery;
            $sqlResult = mysql_query($sqlQuery, $dbAccess['dbConnection']);
            $this->closeMysqlConnection($dbAccess['dbConnection']);
        }

        return $this->mysqlRessourceJson($sqlResult);
    }

    public function getActiveCampaign($dbAccess) {

        //$dbAccess = $this->connectMysqlDatabase();

        if (!$dbAccess['dbConnection']) {
            $logDebug = " Echec getActiveCampaign()  " . $dbAccess['errorMessage'] . "  " . $dbAccess['errorCode'] . " MSISDN :: $this->msisdn";
            $this->svaLog->writeSystemLog($logDebug);
            die("IMPOSSIBLE DE SE CONNECTER A LA BASE DE DONNEES");
        } else {

            $sqlQuery = "  
                   select  campaign_id_n
                            ,start_date_d
                            ,start_time_d
                            ,end_date_d
                            ,end_time_d
                            ,char_field_2 total
                            ,char_field_3 processed
                     from master_campaigns_tab 
                     where status_n not in ('3','9')
                    
                ";


            // echo $sqlQuery;
            $sqlResult = mysql_query($sqlQuery, $dbAccess['dbConnection']);

            //$this->closeMysqlConnection($dbAccess['dbConnection'])  ;
        }


        return $this->mysqlRessourceJson($sqlResult);
    }

    public function updateCampaignByIdBulk($campaignId, $statut, $dbAccess) {
        //$dbAccess = $this->getMysqlConnection();  
        $status = false;

        if (!$dbAccess['dbConnection']) {
            $logDebug = " Echec updateCampaignByIdBulk()  " . $dbAccess['errorMessage'] . "  " . $dbAccess['errorCode'] . " MSISDN :: $msisdn";
            $this->svaLog->writeSystemLog($logDebug);
            die("IMPOSSIBLE DE SE CONNECTER A LA BASE DE DONNEES");
        } else {


            $sqlQuery = "update master_campaigns_tab 
                             set status_n='$statut'   
                             where campaign_id_n= '$campaignId'  

                ";
            //echo $sqlQuery;
            $queryResult = mysql_query($sqlQuery, $dbAccess['dbConnection']);
            // $this->closeMysqlConnection($dbAccess['dbConnection']);
            $status = true;
        }

        return $queryResult;
    }

    public function updateCampaignDetailById($campaignId, $currentStatus, $newStatus, $dbAccess) {
        //$dbAccess = $this->getMysqlConnection();  
        $status = false;

        if (!$dbAccess['dbConnection']) {
            $logDebug = " Echec updateCampaignDetailById()  " . $dbAccess['errorMessage'] . "  " . $dbAccess['errorCode'] . " MSISDN :: $msisdn";
            $this->svaLog->writeSystemLog($logDebug);
            die("IMPOSSIBLE DE SE CONNECTER A LA BASE DE DONNEES");
        } else {


            $sqlQuery = "update queues_processor_tab 
                             set status_n='$newStatus'   
                             where campaign_id_n= '$campaignId' 
                             and status_n = '$currentStatus'

                ";
            //echo $sqlQuery;
            $queryResult = mysql_query($sqlQuery, $dbAccess['dbConnection']);
            // $this->closeMysqlConnection($dbAccess['dbConnection']);
            $status = true;
        }

        return $queryResult;
    }

    public function updateProcessStats($issueDate, $itemId, $campaignId, $phone, $statusId, $dbAccess) {
        // $dbAccess = $this->getMysqlConnection();  
        $status = false;

        if (!$dbAccess['dbConnection']) {
            $logDebug = " Echec updateProcessStats()  " . $dbAccess['errorMessage'] . "  " . $dbAccess['errorCode'] . " MSISDN :: $msisdn";
            $this->svaLog->writeSystemLog($logDebug);
            die("IMPOSSIBLE DE SE CONNECTER A LA BASE DE DONNEES");
        } else {


            $sqlQuery = "update queues_processor_tab 
                             set issued_date_d='$issueDate' 
                                 ,status_n='$statusId'
                             where item_id_n='$itemId'
                             and campaign_id_n= '$campaignId' 
                             and phone_number_v= '$phone'

                ";
            //echo $sqlQuery;
            $queryResult = mysql_query($sqlQuery, $dbAccess['dbConnection']);
            $this->closeMysqlConnection($dbAccess['dbConnection']);
            $status = true;
        }

        return $status;
    }

    public function iadRunEodStats() {
        $dbAccess = $this->getMysqlConnection();
        $status = false;

        if (!$dbAccess['dbConnection']) {
            $logDebug = " Echec iadRunDetailledStats()  " . $dbAccess['errorMessage'] . "  " . $dbAccess['errorCode'] . " MSISDN :: $msisdn";
            $this->svaLog->writeSystemLog($logDebug);
            die("IMPOSSIBLE DE SE CONNECTER A LA BASE DE DONNEES");
        } else {


            $sqlQuery = " call SCM_EOD_HISTORY_GEN()            ";
            //echo $sqlQuery;
            $queryResult = mysql_query($sqlQuery, $dbAccess['dbConnection']);

            $sqlQuery = " call IAD_EOD_STATS_AGGRAGATE()            ";
            //echo $sqlQuery;
            $queryResult = mysql_query($sqlQuery, $dbAccess['dbConnection']);

            // $this->closeMysqlConnection($dbAccess['dbConnection']);
            $status = true;
        }

        return $queryResult;
    }

    public function getIpLocation($ip2number) {

        $dbAccess = $this->getMysqlConnection();
        $status = false;

        if (!$dbAccess['dbConnection']) {
            $logDebug = " Echec getIpLocation()  " . $dbAccess['errorMessage'] . "  " . $dbAccess['errorCode'] . " MSISDN :: $msisdn";
            $this->svaLog->writeSystemLog($logDebug);
            die("IMPOSSIBLE DE SE CONNECTER A LA BASE DE DONNEES");
        } else {


            $sqlQuery = "SELECT * FROM ip_locator_tab  where $ip2number between `ip_from_n` and `ip_to_n` 


				";

           // echo $sqlQuery;
            $queryResult = mysql_query($sqlQuery, $dbAccess['dbConnection']);
            $queryData = mysql_fetch_array($queryResult);
            $this->closeMysqlConnection($dbAccess['dbConnection']);
            $status = true;
        }

        return $queryData;
    }

    public function upldCampaignResultData($campaignId, $queueId, $adresseIP, $country, $city, $regionName, $platform, $status) {

        $dbAccess = $this->getMysqlConnection();
        $result = NULL;

        if (!$dbAccess['dbConnection']) {
            $logDebug = " Echec upldCampaignResultData()  " . $dbAccess['errorMessage'] . "  " . $dbAccess['errorCode'] . " MSISDN :: $msisdn";
            $this->svaLog->writeSystemLog($logDebug);
            die("IMPOSSIBLE DE SE CONNECTER A LA BASE DE DONNEES");
        } else {


            $sqlQuery = "INSERT INTO `smm_upld_report_tab`  VALUES (NULL, now(), '$campaignId', '$queueId', '$adresseIP', '$country', '$city','$regionName','$platform','$status')";
            //echo  $sqlQuery;
            $result = mysql_query($sqlQuery, $dbAccess['dbConnection']);
            $this->closeMysqlConnection($dbAccess['dbConnection']);
        }

        return $result;
    }

    public function getViewMailReportByCampaignId($campaignId, $option, $startLimit, $maxElement) {

        $dbAccess = $this->connectMysqlDatabase();

        if (!$dbAccess['dbConnection']) {
            $logDebug = " Echec getViewMailReportByCampaignId()  " . $dbAccess['errorMessage'] . "  " . $dbAccess['errorCode'] . " MSISDN :: $this->msisdn";
            $this->svaLog->writeSystemLog($logDebug);
            die("IMPOSSIBLE DE SE CONNECTER A LA BASE DE DONNEES");
        } else {

            $limitQuery = "";
            if ($option)
                $limitQuery = "LIMIT $startLimit, $maxElement";

            $sqlQuery = "
                    
                   SELECT b.campaign_id_n
             ,DATE_FORMAT(a.event_datetime_d, '%d/%m/%Y %H:%i:%s') eventDateTime  
             ,b.campaign_name_v
             ,c.entity_v
             , a.country_name_v
             , a.city_name_v
             , a.platform_name_v
FROM smm_upld_report_tab a
         , master_campaigns_tab b
         , queues_processor_tab c
where a.campain_id_n = b.campaign_id_n
and c.item_id_n = a.queue_id_n
and a.campain_id_n = '$campaignId'
                    $limitQuery

                ";
            // echo $sqlQuery;
            $sqlResult = mysql_query($sqlQuery, $dbAccess['dbConnection']);
            $this->closeMysqlConnection($dbAccess['dbConnection']);
        }

        return $this->mysqlRessourceJson($sqlResult);
    }

    public function upldGroupDetailBulk($dbAccess, $groupId, $entityName, $additionalInfo, $contact, $email, $charfield, $statut) {

        // $dbAccess = $this->getMysqlConnection();
        $status = false;

        if (!$dbAccess['dbConnection']) {
            $logDebug = " Echec upldGroupDetail()  " . $dbAccess['errorMessage'] . "  " . $dbAccess['errorCode'] . " MSISDN :: $msisdn";
            $this->svaLog->writeSystemLog($logDebug);
            die("IMPOSSIBLE DE SE CONNECTER A LA BASE DE DONNEES");
        } else {


            $sqlQuery = "INSERT 
                INTO group_details_tab   
                VALUES (null,'$groupId',now(),'$entityName','$additionalInfo','$contact','$email','$charfield[0]','$charfield[1]','$charfield[2]','$charfield[3]','$charfield[4]','$statut')";
            //echo $sqlQuery;
            $status = mysql_query($sqlQuery, $dbAccess['dbConnection']);
            $id = mysql_insert_id($dbAccess['dbConnection']);
            //$this->closeMysqlConnection($dbAccess['dbConnection']);
        }

        return $id;
    }

    public function getIadSenderListByUserId($userId) {

        $dbAccess = $this->connectMysqlDatabase();

        if (!$dbAccess['dbConnection']) {
            $logDebug = " Echec getIadSenderListByUserId()  " . $dbAccess['errorMessage'] . "  " . $dbAccess['errorCode'] . " MSISDN :: $this->msisdn";
            $this->svaLog->writeSystemLog($logDebug);
            die("IMPOSSIBLE DE SE CONNECTER A LA BASE DE DONNEES");
        } else {

            $sqlQuery = "
                    
                  select    sender_id_n
                            , creation_date_d
                            , short_code_v
                            , status_n 
                    FROM iad_senders_tab
					WHERE user_id_n= $userId
                ";


            // echo $sqlQuery;
            $sqlResult = mysql_query($sqlQuery, $dbAccess['dbConnection']);

            $this->closeMysqlConnection($dbAccess['dbConnection']);
        }

        return $this->mysqlRessourceJson($sqlResult);
    }

    //********************************messasges templates*****************************************//

    public function getTemplateById($ownerId, $startLimit, $maxLimit) {

        $dbAccess = $this->connectMysqlDatabase();

        if (!$dbAccess['dbConnection']) {
            $logDebug = " Echec getTemplateById()  " . $dbAccess['errorMessage'] . "  " . $dbAccess['errorCode'] . " MSISDN :: $this->msisdn";
            $this->svaLog->writeSystemLog($logDebug);
            die("IMPOSSIBLE DE SE CONNECTER A LA BASE DE DONNEES");
        } else {


            $sqlQuery = "
                SELECT `template_id_n`
				, DATE_FORMAT(`creation_date_d`, '%d/%m/%Y %H:%i:%s') eventDate
				, `template_name_v`
				, `template_message_v`
				, `owner_id_n`
				, `group_enable_n`
				, `other_enable_n`
				, `status_n`
				from message_template_tab where owner_id_n = '$ownerId'
				Limit $startLimit,$maxLimit
                      

                ";
            // echo $sqlQuery;
            $sqlResult = mysql_query($sqlQuery, $dbAccess['dbConnection']);
            $this->closeMysqlConnection($dbAccess['dbConnection']);
        }

        return $this->mysqlRessourceJson($sqlResult);
    }

    public function getALLuser() {

        $dbAccess = $this->connectMysqlDatabase();

        if (!$dbAccess['dbConnection']) {
            $logDebug = " Echec getALLuser()  " . $dbAccess['errorMessage'] . "  " . $dbAccess['errorCode'] . " MSISDN :: $this->msisdn";
            $this->svaLog->writeSystemLog($logDebug);
            die("IMPOSSIBLE DE SE CONNECTER A LA BASE DE DONNEES");
        } else {


            $sqlQuery = 'SELECT * FROM master_users_tab';
            // echo $sqlQuery;
            $sqlResult = mysql_query($sqlQuery, $dbAccess['dbConnection']);
            $this->closeMysqlConnection($dbAccess['dbConnection']);
        }

        return $sqlResult;
    }

    /*     * *************Obtention du profil de l'abonné sur la plateforme SVA ******************* */

    public function getUserAccountById($userId) {

        $dbAccess = $this->getMysqlConnection();

        if (!$dbAccess['dbConnection']) {
            $logDebug = " Echec getUserAccountById()  " . $dbAccess['errorMessage'] . "  " . $dbAccess['errorCode'] . " MSISDN :: $msisdn";
            $this->svaLog->writeSystemLog($logDebug);
            die("IMPOSSIBLE DE SE CONNECTER A LA BASE DE DONNEES");
        } else {

            $sqlQuery = "SELECT                                             
                                                                            first_name_v
									   ,last_name_v 
									   ,balance_n
									   ,user_name_v
									   ,date_format(expiry_date_1 ,'%d/%m/%Y') expiry_date_1
                                       ,stock_id_n
                                       ,a.user_id_n
								FROM   `master_users_tab` a
									  ,master_stock_tab b
								where a.user_id_n = b.user_id_n
								and a.user_id_n='$userId'";

            //echo $sqlQuery;
            $queryResult = mysql_query($sqlQuery, $dbAccess['dbConnection']);
            $queryData = mysql_fetch_array($queryResult);
            $this->closeMysqlConnection($dbAccess['dbConnection']);
        }

        return $queryData;
    }

    public function getUserCreditById($userId) {

        $dbAccess = $this->getMysqlConnection();

        if (!$dbAccess['dbConnection']) {
            $logDebug = " Echec getUserAccountById()  " . $dbAccess['errorMessage'] . "  " . $dbAccess['errorCode'] . " MSISDN :: $msisdn";
            $this->svaLog->writeSystemLog($logDebug);
            die("IMPOSSIBLE DE SE CONNECTER A LA BASE DE DONNEES");
        } else {

            $sqlQuery = "SELECT       
									     balance_n
								       FROM  master_stock_tab 
								where user_id_n='$userId'";

            //echo $sqlQuery;
            $queryResult = mysql_query($sqlQuery, $dbAccess['dbConnection']);
            $queryData = mysql_fetch_array($queryResult);
            $this->closeMysqlConnection($dbAccess['dbConnection']);
        }

        return $queryData;
    }

    public function getUserApiById($userId) {

        $dbAccess = $this->getMysqlConnection();

        if (!$dbAccess['dbConnection']) {
            $logDebug = " Echec getUserAccountById()  " . $dbAccess['errorMessage'] . "  " . $dbAccess['errorCode'] . " MSISDN :: $msisdn";
            $this->svaLog->writeSystemLog($logDebug);
            die("IMPOSSIBLE DE SE CONNECTER A LA BASE DE DONNEES");
        } else {

            $sqlQuery = "SELECT       
									     
								       * FROM  client_api_tab 
								where user_id_n='$userId'";

            //echo $sqlQuery;
            $queryResult = mysql_query($sqlQuery, $dbAccess['dbConnection']);
            $queryData = mysql_fetch_array($queryResult);
            $this->closeMysqlConnection($dbAccess['dbConnection']);
        }

        return $queryData;
    }

    public function getUserApinfById($userId) {

        $dbAccess = $this->getMysqlConnection();

        if (!$dbAccess['dbConnection']) {
            $logDebug = " Echec getUserAccountById()  " . $dbAccess['errorMessage'] . "  " . $dbAccess['errorCode'] . " MSISDN :: $msisdn";
            $this->svaLog->writeSystemLog($logDebug);
            die("IMPOSSIBLE DE SE CONNECTER A LA BASE DE DONNEES");
        } else {

            $sqlQuery = " SELECT  a.api_id_n
                                ,a.user_id_n
                                ,a.incoming_url_v
                                ,a.outgoing_url_v
                                ,a.dlr_url_v
                                ,a.api_key_v
                                ,a.client_ip_v
                         FROM  client_api_tab a, master_users_tab b 
                         where a.user_id_n = b.user_id_n 
                         and a.user_id_n = $userId ";

            //echo $sqlQuery;
            $queryResult = mysql_query($sqlQuery, $dbAccess['dbConnection']);
            $queryData = mysql_fetch_array($queryResult);
            $this->closeMysqlConnection($dbAccess['dbConnection']);
        }

        return $queryData;
    }

    //************************super fonction Delete**********************
    public function superDelete($offerId, $tableName, $primary) {

        $dbAccess = $this->connectMysqlDatabase();

        if (!$dbAccess['dbConnection']) {
            $logDebug = " Echec superDelete()  " . $dbAccess['errorMessage'] . "  " . $dbAccess['errorCode'] . " MSISDN :: $this->msisdn";
            $this->svaLog->writeSystemLog($logDebug);
            die("IMPOSSIBLE DE SE CONNECTER A LA BASE DE DONNEES");
        } else {

            $sqlQuery = "
                   delete from   `$tableName`  where `$primary`='$offerId'
                ";


            // echo $sqlQuery;
            $sqlResult = mysql_query($sqlQuery, $dbAccess['dbConnection']);

            $this->closeMysqlConnection($dbAccess['dbConnection']);
        }

        return $sqlResult;
    }

    /*     * *************insertion des SMSC ******************* */

    public function uploadSmsc($server, $nport, $recpPort, $transceiver, $login, $pwd, $url, $type, $dest_ton, $dest_npi, $bind_ton, $bind_npi) {

        $dbAccess = $this->getMysqlConnection();

        if (!$dbAccess['dbConnection']) {
            $logDebug = " Echec uploadSmsc()  " . $dbAccess['errorMessage'] . "  " . $dbAccess['errorCode'] . " MSISDN :: $msisdn";
            $this->svaLog->writeSystemLog($logDebug);
            die("IMPOSSIBLE DE SE CONNECTER A LA BASE DE DONNEES");
        } else {


            $sqlQuery = "INSERT 
                INTO master_smscs_tab   
                VALUES (null,now(),'$server','$nport','$recpPort','$transceiver','$login','$pwd','$url','$type','$dest_ton','$dest_npi','$bind_ton','$bind_npi','','','','','','','','','','','1')";
            // echo $sqlQuery;
            $status = mysql_query($sqlQuery, $dbAccess['dbConnection']);
            $id = mysql_insert_id($dbAccess['dbConnection']);
            //$this->closeMysqlConnection($dbAccess['dbConnection']);
        }

        return $id;
    }

    ////*******************update smsc******************
    public function mmgSmscUpdate($smscId, $server, $nport, $recpPort, $transceiver, $login, $pwd, $url, $type, $dest_ton, $dest_npi, $bind_ton, $bind_npi) {
        $dbAccess = $this->getMysqlConnection();
        $id = 0;

        if (!$dbAccess['dbConnection']) {
            $logDebug = " Echec mmgSmscUpdate()  " . $dbAccess['errorMessage'] . "  " . $dbAccess['errorCode'] . " MSISDN :: $msisdn";
            $this->svaLog->writeSystemLog($logDebug);
            die("IMPOSSIBLE DE SE CONNECTER A LA BASE DE DONNEES");
        } else {
            $serverSql = "";
            $nportSql = "";
            $recpPortSql = "";
            $transceiverSql = "";
            $loginSql = "";
            $pwdSql = "";
            $urlSql = "";
            $typeSql = "";
            $dest_tonSql = "";
            $dest_npiSql = "";
            $bind_tonSql = "";
            $bind_npiSql = "";

            if (isset($server) && strlen($server) > 0) {
                $serverSql = "`host_name_v`='$server',";
            } else {
                $serverSql = "";
            }
            if (isset($nport) && strlen($nport) > 0) {
                $nportSql = "`port_number_n`='$nport',";
            } else {
                $nportSql = "";
            }
            if (isset($recpPort) && strlen($recpPort) > 0) {
                $recpPortSql = "`receive_port_n`='$recpPort',";
            } else {
                $recpPortSql = "";
            }
            if (isset($transceiver) && strlen($transceiver) > 0) {
                $transceiverSql = "`is_transceiver_v`='$transceiver',";
            } else {
                $transceiverSql = "";
            }
            if (isset($login) && strlen($login) > 0) {
                $loginSql = "`smsc_login_v`='$login',";
            } else {
                $loginSql = "";
            }
            if (isset($pwd) && strlen($pwd) > 0) {
                $pwdSql = "`sms_password_v`='$pwd',";
            } else {
                $pwdSql = "";
            }
            if (isset($url) && strlen($url) > 0) {
                $urlSql = "`smsc_url_v`='$url',";
            } else {
                $urlSql = "";
            }
            if (isset($type) && strlen($type) > 0) {
                $typeSql = "`system_type_v`='$type',";
            } else {
                $typeSql = "";
            }
            if (isset($dest_ton) && strlen($dest_ton) > 0) {
                $dest_tonSql = "`dest_ton_n`='$dest_ton',";
            } else {
                $dest_tonSql = "";
            }
            if (isset($dest_npi) && strlen($dest_npi) > 0) {
                $dest_npiSql = "`dest_npi_n`='$dest_npi',";
            } else {
                $dest_npiSql = "";
            }
            if (isset($bind_ton) && strlen($bind_ton) > 0) {
                $bind_tonSql = "`bind_ton_n`='$bind_ton',";
            } else {
                $bind_tonSql = "";
            }
            if (isset($bind_npi) && strlen($bind_npi) > 0) {
                $bind_npiSql = "`bind_npi_n`='$bind_npi'";
            } else {
                $bind_npiSql = "";
            }



            $sqlQuery = "UPDATE master_smscs_tab
							 SET   
								   $serverSql
								   $nportSql
								   $recpPortSql
								  $transceiverSql
								  $loginSql
								  $pwdSql
								  $urlSql
								  $typeSql
								  $dest_tonSql
								   $dest_npiSql
								    $bind_tonSql
									 $bind_npiSql
                            where `sms_id_n`=$smscId
                          ";
            // echo $sqlQuery;
            $queryResult = mysql_query($sqlQuery, $dbAccess['dbConnection']);

            $this->closeMysqlConnection($dbAccess['dbConnection']);
        }

        return $queryResult;
    }

    /*     * *************Obtention des SMSC ******************* */

    public function getSmsc($option, $startLimit, $maxElement) {

        $dbAccess = $this->getMysqlConnection();

        if (!$dbAccess['dbConnection']) {
            $logDebug = " Echec getSmsc()  " . $dbAccess['errorMessage'] . "  " . $dbAccess['errorCode'] . " MSISDN :: $msisdn";
            $this->svaLog->writeSystemLog($logDebug);
            die("IMPOSSIBLE DE SE CONNECTER A LA BASE DE DONNEES");
        } else {

            $limitQuery = "";
            if ($option) {
                $limitQuery = "LIMIT $startLimit, $maxElement";
            }

            $sqlQuery = " select  a.smsc_id_n
                                    , DATE_FORMAT(a.creation_date_d, '%d/%m/%Y %H:%i:%s') eventDateTime 
                                    , a.smsc_name_v
                                    , a.smsc_desc_v
                                    , a.host_name_v
                                    , a.port_number_n
                                    , a.receive_port_n
                                    , a.is_transceiver_v
                                    , a.smsc_login_v
                                    , a.sms_password_v
                                    , a.smsc_url_v
                                    , a.system_type_v
                                    , a.dest_ton_n
                                    , a.dest_npi_n
                                    , a.src_ton_n
                                    , a.src_npi_n
                                    , a.ssl_client_file_v
                                    , a.use_ssl_n
                                    , a.service_type_v
                                    , a.interface_version_n
                                    , a.address_range_v
                                    , a.phone_number_v
                                    , a.enquire_link_interval_n
                                    , a.max_pending_submit_n
                                    , a.reconnect_delay_n
                                    , a.src_address_autodetect_n
                                    , a.bind_address_ton_n
                                    , a.bind_address_npi_n
                                    , a.msg_id_type_n
                                    , a.alt_charset_v
                                    , a.alt_address_charset_v
                                    , a.connection_timeout_n
                                    , a.wait_ack_time_n
                                    , a.wait_ack_expire_time_n
                                    , a.validity_period_n
                                    , a.esm_class_n
                                    , a.throught_put_n
                                    , a.char_field_1
                                    , a.char_field_2
                                    , a.char_field_3
                                    , a.char_field_4
                                    , a.char_field_5 
                                    , a.status_n
                                    , b.billing_id_n
                                    , c.billing_name_v
                                    , d.brand_id_n
                                    , d.brand_name_v
                               FROM master_smscs_tab a, smsc_billing_tab b, master_billing_tab c, master_brands_tab d
                               WHERE a.smsc_id_n = b.smsc_id_n
                               AND b.billing_id_n = c.billing_id_n
                               AND d.brand_id_n = a.char_field_1
                               ORDER BY creation_date_d desc 
                               $limitQuery  ";


            //echo $sqlQuery;
            $sqlResult = mysql_query($sqlQuery, $dbAccess['dbConnection']);
            $this->closeMysqlConnection($dbAccess['dbConnection']);
        }

        return $this->mysqlRessourceJson($sqlResult);
    }

    /*     * *************Obtention des SMSC ******************* */

    public function getSmscById($smscId) {

        $dbAccess = $this->getMysqlConnection();

        if (!$dbAccess['dbConnection']) {
            $logDebug = " Echec getSmsc()  " . $dbAccess['errorMessage'] . "  " . $dbAccess['errorCode'] . " MSISDN :: $msisdn";
            $this->svaLog->writeSystemLog($logDebug);
            die("IMPOSSIBLE DE SE CONNECTER A LA BASE DE DONNEES");
        } else {

            $sqlQuery = "SELECT    *
								FROM   `master_smscs_tab`
								where `sms_id_n`=$smscId";


            // echo $sqlQuery;
            $sqlResult = mysql_query($sqlQuery, $dbAccess['dbConnection']);
            $this->closeMysqlConnection($dbAccess['dbConnection']);
        }

        return $this->mysqlRessourceJson($sqlResult);
    }

    /*     * *************insertion des pays ******************* */

    public function uploadCountry($country, $map, $code, $indicatif, $internet) {

        $dbAccess = $this->getMysqlConnection();

        if (!$dbAccess['dbConnection']) {
            $logDebug = " Echec uploadCountry()  " . $dbAccess['errorMessage'] . "  " . $dbAccess['errorCode'] . " MSISDN :: $msisdn";
            $this->svaLog->writeSystemLog($logDebug);
            die("IMPOSSIBLE DE SE CONNECTER A LA BASE DE DONNEES");
        } else {


            $sqlQuery = "INSERT 
                INTO master_countries_tab   
                VALUES (null,'$country','$map','$code','$indicatif','$internet','','','','','','1')";
            // echo $sqlQuery;
            $status = mysql_query($sqlQuery, $dbAccess['dbConnection']);
            $id = mysql_insert_id($dbAccess['dbConnection']);
            //$this->closeMysqlConnection($dbAccess['dbConnection']);
        }

        return $id;
    }

    /*     * *************insertion des pays ******************* */

    public function uploadCountryDetails($res, $mccCode, $ccCode) {

        $dbAccess = $this->getMysqlConnection();

        if (!$dbAccess['dbConnection']) {
            $logDebug = " Echec uploadCountryDetails()  " . $dbAccess['errorMessage'] . "  " . $dbAccess['errorCode'] . " MSISDN :: $msisdn";
            $this->svaLog->writeSystemLog($logDebug);
            die("IMPOSSIBLE DE SE CONNECTER A LA BASE DE DONNEES");
        } else {


            $sqlQuery = "INSERT 
                INTO country_details_tab   
                VALUES (null,'$res','$mccCode','$ccCode','0')";
            // echo $sqlQuery;
            $status = mysql_query($sqlQuery, $dbAccess['dbConnection']);
            $id = mysql_insert_id($dbAccess['dbConnection']);
            //$this->closeMysqlConnection($dbAccess['dbConnection']);
        }

        return $id;
    }

    ////*******************update country******************
    public function countryUpdate($cId, $country, $map, $code, $indicatif, $internet) {
        $dbAccess = $this->getMysqlConnection();
        $id = 0;

        if (!$dbAccess['dbConnection']) {
            $logDebug = " Echec mmgSmscUpdate()  " . $dbAccess['errorMessage'] . "  " . $dbAccess['errorCode'] . " MSISDN :: $msisdn";
            $this->svaLog->writeSystemLog($logDebug);
            die("IMPOSSIBLE DE SE CONNECTER A LA BASE DE DONNEES");
        } else {
            $countrySql = "";
            $mapSql = "";
            $codeSql = "";
            $indicatifSql = "";
            $internetSql = "";


            if (isset($country) && strlen($country) > 0) {
                $countrySql = "`country_name_v`='$country',";
            } else {
                $countrySql = "";
            }
            if (isset($map) && strlen($map) > 0) {
                $mapSql = "`map_v`='$map',";
            } else {
                $mapSql = "";
            }
            if (isset($code) && strlen($code) > 0) {
                $codeSql = "`iso_code_v`='$code',";
            } else {
                $codeSql = "";
            }
            if (isset($indicatif) && strlen($indicatif) > 0) {
                $indicatifSql = "`short_name_v`='$indicatif',";
            } else {
                $indicatifSql = "";
            }
            if (isset($internet) && strlen($internet) > 0) {
                $internetSql = "`internet_domain_v`='$internet'";
            } else {
                $internetSql = "";
            }




            $sqlQuery = "UPDATE master_countries_tab
							 SET   
								   $countrySql
								   $mapSql
								   $codeSql
								  $indicatifSql
								  $internetSql
                            where `country_id_n`=$cId
                          ";
            //  echo $sqlQuery;
            $queryResult = mysql_query($sqlQuery, $dbAccess['dbConnection']);

            $this->closeMysqlConnection($dbAccess['dbConnection']);
        }

        return $queryResult;
    }

    ////*******************update country details******************
    public function countryDetailsUpdate($cId, $mccCode, $ccCode) {
        $dbAccess = $this->getMysqlConnection();
        $id = 0;

        if (!$dbAccess['dbConnection']) {
            $logDebug = " Echec countryDetailsUpdate()  " . $dbAccess['errorMessage'] . "  " . $dbAccess['errorCode'] . " MSISDN :: $msisdn";
            $this->svaLog->writeSystemLog($logDebug);
            die("IMPOSSIBLE DE SE CONNECTER A LA BASE DE DONNEES");
        } else {
            $mccCodeSql = "";
            $ccCodeSql = "";



            if (isset($mccCode) && strlen($mccCode) > 0) {
                $mccCodeSql = "`mcc_code_v`='$mccCode',";
            } else {
                $mccCodeSql = "";
            }
            if (isset($ccCode) && strlen($ccCode) > 0) {
                $ccCodeSql = "`cc_code_v`='$ccCode'";
            } else {
                $ccCodeSql = "";
            }




            $sqlQuery = "UPDATE country_details_tab
							 SET   
								   $mccCodeSql
								   $ccCodeSql
								  
                            where `country_id_n`=$cId
                          ";
            // echo $sqlQuery;
            $queryResult = mysql_query($sqlQuery, $dbAccess['dbConnection']);

            $this->closeMysqlConnection($dbAccess['dbConnection']);
        }

        return $queryResult;
    }

    /////-----------------------------------------------------------------////////

    public function getAllCountries($option, $startLimit, $maxElement) {

        $dbAccess = $this->connectMysqlDatabase();

        if (!$dbAccess['dbConnection']) {
            $logDebug = " Echec getAllCountries()  " . $dbAccess['errorMessage'] . "  " . $dbAccess['errorCode'] . " MSISDN :: $this->msisdn";
            $this->svaLog->writeSystemLog($logDebug);
            die("IMPOSSIBLE DE SE CONNECTER A LA BASE DE DONNEES");
        } else {
            $limitQuery = "";

            if ($option)
                $limitQuery = "LIMIT $startLimit, $maxElement";

            $sqlQuery = "
                    
                  select distinct(a.country_id_n),a.country_name_v, a.iso_code_v,b.mcc_code_v, b.cc_code_v
                  from master_countries_tab a ,country_details_tab b
				  where a.country_id_n=b.country_id_n
				  order by a.country_name_v asc
				   $limitQuery
                ";



            //  echo $sqlQuery;
            $sqlResult = mysql_query($sqlQuery, $dbAccess['dbConnection']);

            $this->closeMysqlConnection($dbAccess['dbConnection']);
        }

        return $this->mysqlRessourceJson($sqlResult);
    }

    /////-----------------------------------------------------------------////////

    public function getCountryById($id) {

        $dbAccess = $this->connectMysqlDatabase();

        if (!$dbAccess['dbConnection']) {
            $logDebug = " Echec getCountryById()  " . $dbAccess['errorMessage'] . "  " . $dbAccess['errorCode'] . " MSISDN :: $this->msisdn";
            $this->svaLog->writeSystemLog($logDebug);
            die("IMPOSSIBLE DE SE CONNECTER A LA BASE DE DONNEES");
        } else {
            $limitQuery = "";


            $sqlQuery = "
                    
                  select distinct(a.country_id_n),a.country_name_v, a.iso_code_v,a.short_name_v,a.map_v,a.internet_domain_v, b.mcc_code_v, b.cc_code_v
                  from master_countries_tab a ,country_details_tab b
				  where a.country_id_n=b.country_id_n
				  and a.country_id_n=$id
                ";

            // echo $sqlQuery;
            $sqlResult = mysql_query($sqlQuery, $dbAccess['dbConnection']);

            $this->closeMysqlConnection($dbAccess['dbConnection']);
        }

        return $this->mysqlRessourceJson($sqlResult);
    }

    /*     * *************insertion des pays ******************* */

    public function uploadNetwork($networkId, $networkName, $countryId, $icon, $mcc, $mnc, $digitMini, $digitMax) {

        $dbAccess = $this->getMysqlConnection();

        if (!$dbAccess['dbConnection']) {
            $logDebug = " Echec uploadNetwork()  " . $dbAccess['errorMessage'] . "  " . $dbAccess['errorCode'] . " MSISDN :: $msisdn";
            $this->svaLog->writeSystemLog($logDebug);
            die("IMPOSSIBLE DE SE CONNECTER A LA BASE DE DONNEES");
        } else {


            $sqlQuery = "INSERT 
                INTO master_networks_tab   
                VALUES (null,'$networkId','$networkName','$countryId','$icon','$mcc','$mnc','$digitMini','$digitMax','','','','0')";
            // echo $sqlQuery;
            $status = mysql_query($sqlQuery, $dbAccess['dbConnection']);
            $id = mysql_insert_id($dbAccess['dbConnection']);
            //$this->closeMysqlConnection($dbAccess['dbConnection']);
        }

        return $id;
    }

    ////-----------------------------------------------//////
    public function getAllNetwork($option, $startLimit, $maxElement) {

        $dbAccess = $this->connectMysqlDatabase();

        if (!$dbAccess['dbConnection']) {
            $logDebug = " Echec getAllNetwork()  " . $dbAccess['errorMessage'] . "  " . $dbAccess['errorCode'] . " MSISDN :: $this->msisdn";
            $this->svaLog->writeSystemLog($logDebug);
            die("IMPOSSIBLE DE SE CONNECTER A LA BASE DE DONNEES");
        } else {
            $limitQuery = "";

            if ($option)
                $limitQuery = "LIMIT $startLimit, $maxElement";

            $sqlQuery = " 
                      select   a.network_id_n
                              ,a.network_name_v
                              ,a.icon_v, b.country_id_n,b.country_name_v
					     from master_networks_tab a,master_countries_tab b
						 where a.country_id_n=b.country_id_n
					     order by b.country_name_v asc
				         $limitQuery             
                ";
            // echo $sqlQuery;
            $sqlResult = mysql_query($sqlQuery, $dbAccess['dbConnection']);

            $this->closeMysqlConnection($dbAccess['dbConnection']);
        }

        return $this->mysqlRessourceJson($sqlResult);
    }

    /////-----------------------------------------------------------------////////

    public function getNetworkById($id) {

        $dbAccess = $this->connectMysqlDatabase();

        if (!$dbAccess['dbConnection']) {
            $logDebug = " Echec getNetworkById()  " . $dbAccess['errorMessage'] . "  " . $dbAccess['errorCode'] . " MSISDN :: $this->msisdn";
            $this->svaLog->writeSystemLog($logDebug);
            die("IMPOSSIBLE DE SE CONNECTER A LA BASE DE DONNEES");
        } else {
            $limitQuery = "";


            $sqlQuery = "
                    
                  select *
                  from master_networks_tab 
				  where network_id_n=$id
				 
                ";

            //   echo $sqlQuery;
            $sqlResult = mysql_query($sqlQuery, $dbAccess['dbConnection']);

            $this->closeMysqlConnection($dbAccess['dbConnection']);
        }

        return $this->mysqlRessourceJson($sqlResult);
    }

    ////*******************update network details******************

    public function networkUpdate($nId, $network, $countryId, $mcc, $mnc, $digitMini, $digitMaxi) {

        $dbAccess = $this->getMysqlConnection();
        $id = 0;

        if (!$dbAccess['dbConnection']) {
            $logDebug = " Echec countryDetailsUpdate()  " . $dbAccess['errorMessage'] . "  " . $dbAccess['errorCode'] . " MSISDN :: $msisdn";
            $this->svaLog->writeSystemLog($logDebug);
            die("IMPOSSIBLE DE SE CONNECTER A LA BASE DE DONNEES");
        } else {
            $networkSql = "";
            $countryIdSql = "";
            $iconSql = "";



            if (isset($countryId) && strlen($countryId) > 0) {
                $countryIdSql = "";
            } else {
                $countryIdSql = "";
            }
            if (isset($network) && strlen($network) > 0) {
                $networkdSql = "`network_name_v`='$network',";
            } else {
                $networkSql = "";
            }
            if (isset($icon) && strlen($icon) > 0) {
                $iconSql = "`icon_v`='$icon'";
            } else {
                $iconSql = "";
            }




            $sqlQuery = "UPDATE master_networks_tab
							 SET 
                                                         mcc_v='$mcc',
                                                         mnc_v='$mnc',
                                                         char_field_1 ='$digitMini',
                                                         char_field_2='$digitMaxi',
							 country_id_n='$countryId',
                                                         network_name_v='$network'

                                                         
								  
                            where `network_id_n`=$nId
                          ";
            // echo $sqlQuery;
            $queryResult = mysql_query($sqlQuery, $dbAccess['dbConnection']);

            $this->closeMysqlConnection($dbAccess['dbConnection']);
        }

        return $queryResult;
    }

    /*     * *************insertion des pays ******************* */

    public function uploadBilling($billing, $userId, $type) {

        $dbAccess = $this->getMysqlConnection();

        if (!$dbAccess['dbConnection']) {
            $logDebug = " Echec uploadBilling()  " . $dbAccess['errorMessage'] . "  " . $dbAccess['errorCode'] . " MSISDN :: $msisdn";
            $this->svaLog->writeSystemLog($logDebug);
            die("IMPOSSIBLE DE SE CONNECTER A LA BASE DE DONNEES");
        } else {


            $sqlQuery = "INSERT 
                INTO master_billing_tab   
                VALUES (null,now(),'$billing','$userId','$type','0')";
            // echo $sqlQuery;
            $status = mysql_query($sqlQuery, $dbAccess['dbConnection']);
            $id = mysql_insert_id($dbAccess['dbConnection']);
            //$this->closeMysqlConnection($dbAccess['dbConnection']);
        }

        return $id;
    }

    public function updateUserApi($id, $inurl, $ourl, $durl, $cltip, $apikey) {

        $dbAccess = $this->getMysqlConnection();

        if (!$dbAccess['dbConnection']) {
            $logDebug = " Echec uploadBilling()  " . $dbAccess['errorMessage'] . "  " . $dbAccess['errorCode'] . " MSISDN :: $msisdn";
            $this->svaLog->writeSystemLog($logDebug);
            die("IMPOSSIBLE DE SE CONNECTER A LA BASE DE DONNEES");
        } else {


            $sqlQuery = 'UPDATE 
               client_api_tab   
			   SET incoming_url_v = "' . $inurl . '",
			   outgoing_url_v ="' . $ourl . '",
			   dlr_url_v = "' . $durl . '",
			   client_ip_v = "' . $cltip . '",
                           api_key_v = "' . $apikey . '"
                WHERE user_id_n = "' . $id . '"
				';
            // echo $sqlQuery;
            mysql_query($sqlQuery, $dbAccess['dbConnection']);
        }
    }

    /*     * *************insertion des detail de fact ******************* */

    public function uploadBillingDetails($billingId, $networkId, $price) {

        $dbAccess = $this->getMysqlConnection();

        if (!$dbAccess['dbConnection']) {
            $logDebug = " Echec uploadBillingDetails()  " . $dbAccess['errorMessage'] . "  " . $dbAccess['errorCode'] . " MSISDN :: $msisdn";
            $this->svaLog->writeSystemLog($logDebug);
            die("IMPOSSIBLE DE SE CONNECTER A LA BASE DE DONNEES");
        } else {


            $sqlQuery = "INSERT 
                INTO billing_details_tab   
                VALUES (null,'$billingId','$networkId','$price','0')";
           // echo $sqlQuery;
            $status = mysql_query($sqlQuery, $dbAccess['dbConnection']);
            $id = mysql_insert_id($dbAccess['dbConnection']);
            //$this->closeMysqlConnection($dbAccess['dbConnection']);
        }

        return $id;
    }

    public function getNetworkId($mcc, $mnc) {

        $dbAccess = $this->getMysqlConnection();

        if (!$dbAccess['dbConnection']) {
            $logDebug = " Echec uploadBillingDetails()  " . $dbAccess['errorMessage'] . "  " . $dbAccess['errorCode'] . " MSISDN :: $msisdn";
            $this->svaLog->writeSystemLog($logDebug);
            die("IMPOSSIBLE DE SE CONNECTER A LA BASE DE DONNEES");
        } else {


            $sqlQuery = "SELECT network_id_n FROM master_networks_tab WHERE mcc_v='$mcc' and mnc_v = '$mnc'";
           // echo $sqlQuery;
            $req = mysql_query($sqlQuery, $dbAccess['dbConnection']);
            $data = mysql_fetch_array($req);
            //$this->closeMysqlConnection($dbAccess['dbConnection']);
        }

        return $data[0];
    }

    ////-----------------------------------------------//////
    /* public function getAllBillings($option, $startLimit, $maxElement) {

      $dbAccess = $this->connectMysqlDatabase();

      if (!$dbAccess['dbConnection']) {
      $logDebug = " Echec getAllBillings()  " . $dbAccess['errorMessage'] . "  " . $dbAccess['errorCode'] . " MSISDN :: $this->msisdn";
      $this->svaLog->writeSystemLog($logDebug);
      die("IMPOSSIBLE DE SE CONNECTER A LA BASE DE DONNEES");
      } else {
      $limitQuery = "";
      if ($option)
      $limitQuery = "LIMIT $startLimit, $maxElement";

      $sqlQuery = "
      select   a.billing_id_n,a.creation_datetime_d,a.billing_name_v,a.user_id_n , b.*
      from master_billing_tab a,billing_details_tab b
      where a.billing_id_n=b.billing_id_n
      $limitQuery
      ";
      // echo $sqlQuery;
      $sqlResult = mysql_query($sqlQuery, $dbAccess['dbConnection']);

      $this->closeMysqlConnection($dbAccess['dbConnection']);
      }

      return $this->mysqlRessourceJson($sqlResult);
      } */


    public function getAllBillings($profile, $userId, $option, $startLimit, $maxElement) {

        $dbAccess = $this->connectMysqlDatabase();

        if (!$dbAccess['dbConnection']) {
            $logDebug = " Echec getAllBillings()  " . $dbAccess['errorMessage'] . "  " . $dbAccess['errorCode'] . " MSISDN :: $this->msisdn";
            $this->svaLog->writeSystemLog($logDebug);
            die("IMPOSSIBLE DE SE CONNECTER A LA BASE DE DONNEES");
        } else {
            $limitQuery = "";
            $userSql = "";
            if ($option)
                $limitQuery = "LIMIT $startLimit, $maxElement";

            if ($profile == 2) {

                $userSql = "and b.user_id_n=$userId";
            }

            $sqlQuery = " 
                      select   a.billing_id_n 
                              ,a.creation_datetime_d
                              ,a.billing_name_v
                              ,a.user_id_n 
                              , b.*  
			      from master_billing_tab a,master_users_tab b
			      where a.user_id_n=b.user_id_n
                              $userSql
					   $limitQuery          
                ";
            // echo $sqlQuery;
            $sqlResult = mysql_query($sqlQuery, $dbAccess['dbConnection']);

            $this->closeMysqlConnection($dbAccess['dbConnection']);
        }

        return $this->mysqlRessourceJson($sqlResult);
    }

    public function getAllsenders($option, $startLimit, $maxElement) {

        $dbAccess = $this->connectMysqlDatabase();

        if (!$dbAccess['dbConnection']) {
            $logDebug = " Echec getAllBillings()  " . $dbAccess['errorMessage'] . "  " . $dbAccess['errorCode'] . " MSISDN :: $this->msisdn";
            $this->svaLog->writeSystemLog($logDebug);
            die("IMPOSSIBLE DE SE CONNECTER A LA BASE DE DONNEES");
        } else {
            $limitQuery = "";
            if ($option)
                $limitQuery = "LIMIT $startLimit, $maxElement";

            $sqlQuery = " 
                      select * from master_users_tab, mmg_senderid_tab where user_id_n = owner_id_n 
					   $limitQuery          
                ";
            // echo $sqlQuery;
            $sqlResult = mysql_query($sqlQuery, $dbAccess['dbConnection']);

            $this->closeMysqlConnection($dbAccess['dbConnection']);
        }

        return $this->mysqlRessourceJson($sqlResult);
    }

    ////-----------------------------------------------//////
    public function getBillingById($bId) {

        $dbAccess = $this->connectMysqlDatabase();

        if (!$dbAccess['dbConnection']) {
            $logDebug = " Echec getBillingById()  " . $dbAccess['errorMessage'] . "  " . $dbAccess['errorCode'] . " MSISDN :: $this->msisdn";
            $this->svaLog->writeSystemLog($logDebug);
            die("IMPOSSIBLE DE SE CONNECTER A LA BASE DE DONNEES");
        } else {
            $limitQuery = "";


            $sqlQuery = " 
                      select   a.billing_id_n,a.creation_datetime_d,a.billing_name_v,a.user_id_n , b.*  
					  from master_billing_tab a,billing_details_tab b
					  where a.billing_id_n=b.billing_id_n 
					   and    a.billing_id_n= $bId      
                ";
            // echo $sqlQuery;
            $sqlResult = mysql_query($sqlQuery, $dbAccess['dbConnection']);

            $this->closeMysqlConnection($dbAccess['dbConnection']);
        }

        return $this->mysqlRessourceJson($sqlResult);
    }

    public function getSenderById($bId) {

        $dbAccess = $this->connectMysqlDatabase();

        if (!$dbAccess['dbConnection']) {
            $logDebug = " Echec getBillingById()  " . $dbAccess['errorMessage'] . "  " . $dbAccess['errorCode'] . " MSISDN :: $this->msisdn";
            $this->svaLog->writeSystemLog($logDebug);
            die("IMPOSSIBLE DE SE CONNECTER A LA BASE DE DONNEES");
        } else {
            $limitQuery = "";


            $sqlQuery = " 
                      select * from master_users_tab, mmg_senderid_tab where sender_id_n ='$bId' and user_id_n = owner_id_n  
                ";
            // echo $sqlQuery;
            $sqlResult = mysql_query($sqlQuery, $dbAccess['dbConnection']);

            $this->closeMysqlConnection($dbAccess['dbConnection']);
        }

        return $this->mysqlRessourceJson($sqlResult);
    }

    ////*******************update billing details******************

    public function billingUpdate($bId, $billing, $userId) {

        $dbAccess = $this->getMysqlConnection();
        $id = 0;

        if (!$dbAccess['dbConnection']) {
            $logDebug = " Echec billingUpdate()  " . $dbAccess['errorMessage'] . "  " . $dbAccess['errorCode'] . " MSISDN :: $msisdn";
            $this->svaLog->writeSystemLog($logDebug);
            die("IMPOSSIBLE DE SE CONNECTER A LA BASE DE DONNEES");
        } else {
            $billingSql = "";
            $userIdSql = "";




            if (isset($billing) && strlen($billing) > 0) {
                $billingSql = "`billing_name_v`='$billing',";
            } else {
                $billingSql = "";
            }
            if (isset($userId) && strlen($userId) > 0) {
                $userIdSql = "`user_id_n`='$userId'";
            } else {
                $userIdSql = "";
            }





            $sqlQuery = "UPDATE master_billing_tab
							 SET   
								  
								   $billingSql
								   $userIdSql
								  
								  
                            where `billing_id_n`=$bId
                          ";
            // echo $sqlQuery;
            $queryResult = mysql_query($sqlQuery, $dbAccess['dbConnection']);

            $this->closeMysqlConnection($dbAccess['dbConnection']);
        }

        return $queryResult;
    }

    ////*******************update billing details******************

    public function billingDetailsUpdate($bId, $networkId, $price) {

        $dbAccess = $this->getMysqlConnection();
        $id = 0;

        if (!$dbAccess['dbConnection']) {
            $logDebug = " Echec billingDetailsUpdate()  " . $dbAccess['errorMessage'] . "  " . $dbAccess['errorCode'] . " MSISDN :: $msisdn";
            $this->svaLog->writeSystemLog($logDebug);
            die("IMPOSSIBLE DE SE CONNECTER A LA BASE DE DONNEES");
        } else {
            $networkIdSql = "";
            $priceSql = "";




            if (isset($networkId) && strlen($networkId) > 0) {
                $networkIdSql = "`network_id_n`='$networkId',";
            } else {
                $networkIdSql = "";
            }
            if (isset($price) && strlen($price) > 0) {
                $priceSql = "`price_v`='$price'";
            } else {
                $priceSql = "";
            }





            $sqlQuery = "UPDATE billing_details_tab
							 SET   
								  
								   $networkIdSql
								   $priceSql
								  
								  
                            where `billing_id_n`=$bId
                          ";
            //  echo $sqlQuery;
            $queryResult = mysql_query($sqlQuery, $dbAccess['dbConnection']);

            $this->closeMysqlConnection($dbAccess['dbConnection']);
        }

        return $queryResult;
    }

    /*     * *************insertion des detail de fact ******************* */

    public function uploadRoute($route, $userId, $desc, $shortCode) {

        $dbAccess = $this->getMysqlConnection();

        if (!$dbAccess['dbConnection']) {
            $logDebug = " Echec uploadBillingDetails()  " . $dbAccess['errorMessage'] . "  " . $dbAccess['errorCode'] . " MSISDN :: $msisdn";
            $this->svaLog->writeSystemLog($logDebug);
            die("IMPOSSIBLE DE SE CONNECTER A LA BASE DE DONNEES");
        } else {


            $sqlQuery = "INSERT 
                INTO mmg_routes_tab   
                VALUES (null,now(),'$route','$desc','$userId','$shortCode','0')";
            // echo $sqlQuery;
            $status = mysql_query($sqlQuery, $dbAccess['dbConnection']);
            $id = mysql_insert_id($dbAccess['dbConnection']);
            //$this->closeMysqlConnection($dbAccess['dbConnection']);
        }

        return $id;
    }

    ////-----------------------------------------------//////
    public function getAllRoutes($option, $startLimit, $maxElement) {

        $dbAccess = $this->connectMysqlDatabase();

        if (!$dbAccess['dbConnection']) {
            $logDebug = " Echec getAllRoutes()  " . $dbAccess['errorMessage'] . "  " . $dbAccess['errorCode'] . " MSISDN :: $this->msisdn";
            $this->svaLog->writeSystemLog($logDebug);
            die("IMPOSSIBLE DE SE CONNECTER A LA BASE DE DONNEES");
        } else {

            $limitQuery = "";
            if ($option)
                $limitQuery = " LIMIT $startLimit, $maxElement ";

            $sqlQuery = " 
                      select a.routing_id_n
                            , DATE_FORMAT( a.creation_datetime_d , '%d/%m/%Y %H:%i:%s') creation_datetime_d
                            , a.route_name_v
                            , a.route_desc_v
                            , a.status_n 
                            , b.shortcode_id_n
                            , b.shortcode_number_n
                            , b.shortcode_desc_v
                            , b.user_id_n
                            , b.module_id_n
                            , b.status_n
                            , c.user_name_v
                       FROM  mmg_routes_tab a
                            , master_shortcode_tab b
                            , master_users_tab c
                       WHERE a.shortcode_id_n = b.shortcode_id_n
                       and c.user_id_n = a.user_id_n
		      $limitQuery        
                ";
            // echo $sqlQuery;
            $sqlResult = mysql_query($sqlQuery, $dbAccess['dbConnection']);

            $this->closeMysqlConnection($dbAccess['dbConnection']);
        }

        return $this->mysqlRessourceJson($sqlResult);
    }

    ////*******************update route******************

    public function routeUpdate($rId, $route, $userId, $shortcodeId, $desc) {

        $dbAccess = $this->getMysqlConnection();
        $id = 0;

        if (!$dbAccess['dbConnection']) {
            $logDebug = " Echec routeUpdate()  " . $dbAccess['errorMessage'] . "  " . $dbAccess['errorCode'] . " MSISDN :: $msisdn";
            $this->svaLog->writeSystemLog($logDebug);
            die("IMPOSSIBLE DE SE CONNECTER A LA BASE DE DONNEES");
        } else {

            $sqlQuery = 'UPDATE mmg_routes_tab
							 SET   
								  route_name_v ="' . $route . '",
								  route_desc_v ="' . $desc . '",
								  user_id_n = "' . $userId . '",
								  shortcode_id_n = "' . $shortcodeId . '"   
                            where routing_id_n="' . $rId . '"
                          ';
            //  echo $sqlQuery;
            $queryResult = mysql_query($sqlQuery, $dbAccess['dbConnection']) or die("error" . mysql_error());

            $this->closeMysqlConnection($dbAccess['dbConnection']);
        }

        return $queryResult;
    }

    ////-----------------------------------------------//////

    public function getRoutesById($rId) {

        $dbAccess = $this->connectMysqlDatabase();

        if (!$dbAccess['dbConnection']) {
            $logDebug = " Echec getAllRoutes()  " . $dbAccess['errorMessage'] . "  " . $dbAccess['errorCode'] . " MSISDN :: $this->msisdn";
            $this->svaLog->writeSystemLog($logDebug);
            die("IMPOSSIBLE DE SE CONNECTER A LA BASE DE DONNEES");
        } else {



            $sqlQuery = " 
                      select  *  
					  from mmg_routes_tab
					  where routing_id_n=$rId    
                ";
            // echo $sqlQuery;
            $sqlResult = mysql_query($sqlQuery, $dbAccess['dbConnection']);

            $this->closeMysqlConnection($dbAccess['dbConnection']);
        }

        return $this->mysqlRessourceJson($sqlResult);
    }

    /*     * *************insertion des detail de fact ******************* */

    public function uploadShortcode($shortcode, $desc, $userId) {

        $dbAccess = $this->getMysqlConnection();

        if (!$dbAccess['dbConnection']) {
            $logDebug = " Echec uploadShortcode()  " . $dbAccess['errorMessage'] . "  " . $dbAccess['errorCode'] . " MSISDN :: $msisdn";
            $this->svaLog->writeSystemLog($logDebug);
            die("IMPOSSIBLE DE SE CONNECTER A LA BASE DE DONNEES");
        } else {


            $sqlQuery = "INSERT 
                INTO master_shortcode_tab   
                VALUES (null,now(),'$shortcode','$desc','$userId','','0')";
            //  echo $sqlQuery;
            $status = mysql_query($sqlQuery, $dbAccess['dbConnection']);
            $id = mysql_insert_id($dbAccess['dbConnection']);
            //$this->closeMysqlConnection($dbAccess['dbConnection']);
        }

        return $id;
    }

    /*     * *************insertion des detail de fact ******************* */

    public function uploadShortcodeDetails($res, $price) {

        $dbAccess = $this->getMysqlConnection();

        if (!$dbAccess['dbConnection']) {
            $logDebug = " Echec uploadShortcode()  " . $dbAccess['errorMessage'] . "  " . $dbAccess['errorCode'] . " MSISDN :: $msisdn";
            $this->svaLog->writeSystemLog($logDebug);
            die("IMPOSSIBLE DE SE CONNECTER A LA BASE DE DONNEES");
        } else {


            $sqlQuery = "INSERT 
                INTO shortcode_details_tab   
                VALUES (null,'$res','','$price','','','','','','0')";
            // echo $sqlQuery;
            $status = mysql_query($sqlQuery, $dbAccess['dbConnection']);
            $id = mysql_insert_id($dbAccess['dbConnection']);
            //$this->closeMysqlConnection($dbAccess['dbConnection']);
        }

        return $id;
    }

    public function updateShortcode($sId, $shortcode, $desc, $price) {

        $dbAccess = $this->getMysqlConnection();
        $id = 0;

        if (!$dbAccess['dbConnection']) {
            $logDebug = " Echec updateShortcode()  " . $dbAccess['errorMessage'] . "  " . $dbAccess['errorCode'] . " MSISDN :: $msisdn";
            $this->svaLog->writeSystemLog($logDebug);
            die("IMPOSSIBLE DE SE CONNECTER A LA BASE DE DONNEES");
        } else {
            $shortcodeSql = "";
            $userIdSql = "";
            $descSql = "";
            $priceSql = "";

            if (isset($shortcode) && strlen($shortcode) > 0) {
                $shortcodeSql = "`shortcode_number_n`='$shortcode',";
            } else {
                $shortcodeSql = "";
            }

            if (isset($desc) && strlen($desc) > 0) {
                $descSql = "`shortcode_desc_v`='$desc'";
            } else {
                $descSql = "";
            }
            if (isset($price) && strlen($price) > 0) {
                $priceSql = "`price_n`='$price'";
            } else {
                $priceSql = "";
            }

            $sqlQuery = "UPDATE master_shortcode_tab
							 SET   
								  $shortcodeSql
								  $descSql
							   where `shortcode_id_n`=$sId
                          ";
            //  echo $sqlQuery;
            $sqlQuery1 = "UPDATE shortcode_details_tab
							 SET   
								  $priceSql
							   where `shortcode_id_n`=$sId
                          ";
            //  echo $sqlQuery1;
            $queryResult = mysql_query($sqlQuery, $dbAccess['dbConnection']);
            $queryResult1 = mysql_query($sqlQuery1, $dbAccess['dbConnection']);

            $this->closeMysqlConnection($dbAccess['dbConnection']);
        }

        return $queryResult;
    }

    ////-----------------------------------------------//////
    public function getAllShortcodes($option, $startLimit, $maxElement) {

        $dbAccess = $this->connectMysqlDatabase();

        if (!$dbAccess['dbConnection']) {
            $logDebug = " Echec getAllShortcodes()  " . $dbAccess['errorMessage'] . "  " . $dbAccess['errorCode'] . " MSISDN :: $this->msisdn";
            $this->svaLog->writeSystemLog($logDebug);
            die("IMPOSSIBLE DE SE CONNECTER A LA BASE DE DONNEES");
        } else {

            $limitQuery = "";
            if ($option)
                $limitQuery = "LIMIT $startLimit, $maxElement";

            $sqlQuery = " 
                      select  a.* ,b.* 
					  from master_shortcode_tab a ,shortcode_details_tab b
					  where a.shortcode_id_n=b.shortcode_id_n
					   $limitQuery       
                ";
            // echo $sqlQuery;
            $sqlResult = mysql_query($sqlQuery, $dbAccess['dbConnection']);

            $this->closeMysqlConnection($dbAccess['dbConnection']);
        }

        return $this->mysqlRessourceJson($sqlResult);
    }

    ////-----------------------------------------------//////
    public function getShortcodeById($sId) {

        $dbAccess = $this->connectMysqlDatabase();

        if (!$dbAccess['dbConnection']) {
            $logDebug = " Echec getAllShortcodes()  " . $dbAccess['errorMessage'] . "  " . $dbAccess['errorCode'] . " MSISDN :: $this->msisdn";
            $this->svaLog->writeSystemLog($logDebug);
            die("IMPOSSIBLE DE SE CONNECTER A LA BASE DE DONNEES");
        } else {



            $sqlQuery = " 
                      select  a.* ,b.* 
					  from master_shortcode_tab a ,shortcode_details_tab b
					  where a.shortcode_id_n=b.shortcode_id_n
					  and  a.shortcode_id_n=$sId
					      
                ";

            //echo $sqlQuery;
            $sqlResult = mysql_query($sqlQuery, $dbAccess['dbConnection']);

            $this->closeMysqlConnection($dbAccess['dbConnection']);
        }

        return $this->mysqlRessourceJson($sqlResult);
    }

    ///*****************************creer un template***////////////

    public function createTemplate($templateName, $messageText, $ownerId, $enableGroup, $enableOther, $statut) {

        $dbAccess = $this->getMysqlConnection();


        if (!$dbAccess['dbConnection']) {
            $logDebug = " Echec createTemplate()  " . $dbAccess['errorMessage'] . "  " . $dbAccess['errorCode'] . " MSISDN :: $msisdn";
            $this->svaLog->writeSystemLog($logDebug);
            die("IMPOSSIBLE DE SE CONNECTER A LA BASE DE DONNEES");
        } else {


            $sqlQuery = "INSERT 
                INTO message_template_tab   
                VALUES (null,now(),'$templateName','$messageText','$ownerId','$enableGroup','$enableOther','$statut')";

            //   echo $sqlQuery;
            $queryResult = mysql_query($sqlQuery, $dbAccess['dbConnection']);
            $id = mysql_insert_id($dbAccess['dbConnection']);
            $this->closeMysqlConnection($dbAccess['dbConnection']);
        }
        return $id;
    }

    ///***********************************************/////
    public function updateTplMessage($rowId, $msg, $tname) {

        $dbAccess = $this->getMysqlConnection();
        $totalUser = 0;

        if (!$dbAccess['dbConnection']) {
            $logDebug = " Echec updateTplMessage()  " . $dbAccess['errorMessage'] . "  " . $dbAccess['errorCode'] . " MSISDN :: $msisdn";
            $this->svaLog->writeSystemLog($logDebug);
            die("IMPOSSIBLE DE SE CONNECTER A LA BASE DE DONNEES");
        } else {


            $sqlQuery = "update message_template_tab 
                              set template_message_v='$msg' 
                                 ,template_name_v = '$tname'
                               where template_id_n='$rowId'";
            $queryResult = mysql_query($sqlQuery, $dbAccess['dbConnection']);
            //echo $sqlQuery;
            // $totalUser = mysql_affected_rows($queryResult);
            $this->closeMysqlConnection($dbAccess['dbConnection']);
        }

        if ($queryResult)
            $totalUser = 1;
        return $totalUser;
    }

    /////////////////***************************************************//////////////////


    public function getTemplateDtlsById($templateId) {

        $dbAccess = $this->connectMysqlDatabase();

        if (!$dbAccess['dbConnection']) {
            $logDebug = " Echec getTemplateDtlsById()  " . $dbAccess['errorMessage'] . "  " . $dbAccess['errorCode'] . " MSISDN :: $this->msisdn";
            $this->svaLog->writeSystemLog($logDebug);
            die("IMPOSSIBLE DE SE CONNECTER A LA BASE DE DONNEES");
        } else {


            $sqlQuery = "
                    
                    select * from message_template_tab where   template_id_n='$templateId'

                ";
            // echo $sqlQuery;
            $sqlResult = mysql_query($sqlQuery, $dbAccess['dbConnection']);
            $data = mysql_fetch_array($sqlResult);
            $this->closeMysqlConnection($dbAccess['dbConnection']);
        }

        return $data;
    }

    public function MMGCreateSMPPAcount($smscName, $smscDesc, $hostName, $port, $rcvPort, $isTransceiver, $smscLogin, $smscPassword, $smscUrl, $systemType, $srcTon, $srcNpi, $dstTon, $dstNpi, $useSsl, $sslClientFile, $serviceType, $version, $addressRange, $phoneNumber, $enquiryInterval, $maxPendingSubmit, $reconnectDelay, $srcAddressAuto, $bindAddressTon, $bindAddressNpi, $msgIdType, $charset, $altCharset, $timeout, $waitAckTime, $waitAckExpireTime, $validityPeriod, $esmsClass, $throughtput, $field, $statut) {

        $dbAccess = $this->getMysqlConnection();

        if (!$dbAccess['dbConnection']) {
            $logDebug = " Echec MMGCreateSMPPAcount()  " . $dbAccess['errorMessage'] . "  " . $dbAccess['errorCode'] . " MSISDN :: $msisdn";
            $this->svaLog->writeSystemLog($logDebug);
            die("IMPOSSIBLE DE SE CONNECTER A LA BASE DE DONNEES");
        } else {


            $sqlQuery = "INSERT 
                INTO master_smscs_tab   
                VALUES (null,now(),'$smscName','$smscDesc','$hostName','$port','$rcvPort','$isTransceiver','$smscLogin','$smscPassword','$smscUrl','$systemType','$dstTon','$dstNpi','$srcTon','$srcNpi','$useSsl','$sslClientFile','$serviceType','$version','$addressRange','$phoneNumber','$enquiryInterval','$maxPendingSubmit','$reconnectDelay','$srcAddressAuto','$bindAddressTon','$bindAddressNpi','$msgIdType','$charset','$altCharset','$timeout','$waitAckTime','$waitAckExpireTime','$validityPeriod','$esmsClass','$throughtput','$field[0]','$field[1]','$field[2]','$field[3]','$field[4]','$statut')";
            //echo $sqlQuery;
            $status = mysql_query($sqlQuery, $dbAccess['dbConnection']);
            $id = mysql_insert_id($dbAccess['dbConnection']);
            //$this->closeMysqlConnection($dbAccess['dbConnection']);
        }

        return $id;
    }

    public function getSmscDetailById($smscId) {

        $dbAccess = $this->getMysqlConnection();

        if (!$dbAccess['dbConnection']) {
            $logDebug = " Echec getSmscDetailById()  " . $dbAccess['errorMessage'] . "  " . $dbAccess['errorCode'] . " MSISDN :: $msisdn";
            $this->svaLog->writeSystemLog($logDebug);
            die("IMPOSSIBLE DE SE CONNECTER A LA BASE DE DONNEES");
        } else {



            $sqlQuery = " select  a.smsc_id_n
                                    , DATE_FORMAT(a.creation_date_d, '%d/%m/%Y %H:%i:%s') eventDateTime 
                                    , a.smsc_name_v
                                    , a.smsc_desc_v
                                    , a.host_name_v
                                    , a.port_number_n
                                    , a.receive_port_n
                                    , a.is_transceiver_v
                                    , a.smsc_login_v
                                    , a.sms_password_v
                                    , a.smsc_url_v
                                    , a.system_type_v
                                    , a.dest_ton_n
                                    , a.dest_npi_n
                                    , a.src_ton_n
                                    , a.src_npi_n
                                    , a.ssl_client_file_v
                                    , a.use_ssl_n
                                    , a.service_type_v
                                    , a.interface_version_n
                                    , a.address_range_v
                                    , a.phone_number_v
                                    , a.enquire_link_interval_n
                                    , a.max_pending_submit_n
                                    , a.reconnect_delay_n
                                    , a.src_address_autodetect_n
                                    , a.bind_address_ton_n
                                    , a.bind_address_npi_n
                                    , a.msg_id_type_n
                                    , a.alt_charset_v
                                    , a.alt_address_charset_v
                                    , a.connection_timeout_n
                                    , a.wait_ack_time_n
                                    , a.wait_ack_expire_time_n
                                    , a.validity_period_n
                                    , a.esm_class_n
                                    , a.throught_put_n
                                    , a.char_field_1
                                    , a.char_field_2
                                    , a.char_field_3
                                    , a.char_field_4
                                    , a.char_field_5 
                                    , a.status_n
                                    , b.billing_id_n
                                    , d.brand_id_n
                                    , d.brand_name_v
                               FROM master_smscs_tab a, smsc_billing_tab b , master_brands_tab d
                               WHERE a.smsc_id_n = b.smsc_id_n
                               AND a.smsc_id_n= $smscId
                               AND d.brand_id_n = a.char_field_1
                               ORDER BY creation_date_d desc 
                                 ";


            //echo $sqlQuery;
            $sqlResult = mysql_query($sqlQuery, $dbAccess['dbConnection']);
            $this->closeMysqlConnection($dbAccess['dbConnection']);
        }

        return $this->mysqlRessourceJson($sqlResult);
    }

    public function MMGUpdateSMPPAccount($smscId, $smscName, $smscDesc, $hostName, $port, $rcvPort, $isTransceiver, $smscLogin, $smscPassword, $systemType, $srcTon, $srcNpi, $dstTon, $dstNpi, $serviceType, $version, $reconnectDelay, $charset, $throughtput, $brand, $statut) {

        $dbAccess = $this->getMysqlConnection();
        $status = false;

        if (!$dbAccess['dbConnection']) {
            $logDebug = " Echec MMGUpdateSMPPAccount()  " . $dbAccess['errorMessage'] . "  " . $dbAccess['errorCode'] . " MSISDN :: $msisdn";
            $this->svaLog->writeSystemLog($logDebug);
            die("IMPOSSIBLE DE SE CONNECTER A LA BASE DE DONNEES");
        } else {


            $sqlQuery = "UPDATE master_smscs_tab
                         SET  smsc_name_v='$smscName'
                             ,smsc_desc_v='$smscDesc'
                             ,host_name_v='$hostName'
                             ,port_number_n = '$port'
                             ,receive_port_n ='$rcvPort'
                             ,is_transceiver_v = '$isTransceiver'
                            , smsc_login_v = '$smscLogin'
                            , sms_password_v = '$smscPassword'
                            , system_type_v = '$systemType'
                            , dest_ton_n = '$dstTon'
                            , dest_npi_n = '$dstNpi'
                            , src_ton_n = '$srcTon'
                            , src_npi_n = '$srcNpi'
                            , service_type_v = '$serviceType'
                            , interface_version_n = '$version'
                            , reconnect_delay_n = '$reconnectDelay'
                            , alt_charset_v = '$charset'
                            , throught_put_n = '$throughtput'
                            , char_field_1 = '$brand'
                            , status_n= '$statut'
                         WHERE smsc_id_n='$smscId'
                ";
            //echo $sqlQuery;
            $status = mysql_query($sqlQuery, $dbAccess['dbConnection']);
            $this->closeMysqlConnection($dbAccess['dbConnection']);
        }

        return $status;
    }

    public function createCampaignStats($campaignId, $totalCount, $statut) {
        $dbAccess = $this->getMysqlConnection();

        if (!$dbAccess['dbConnection']) {
            $logDebug = " Echec createCampaignStats()  " . $dbAccess['errorMessage'] . "  " . $dbAccess['errorCode'] . " MSISDN :: $msisdn";
            $this->svaLog->writeSystemLog($logDebug);
            die("IMPOSSIBLE DE SE CONNECTER A LA BASE DE DONNEES");
        } else {


            $sqlQuery = "INSERT 
                INTO campaign_summary_tab  
                VALUES (NULL,$campaignId,null,null,$totalCount,'0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','$statut')";
            //echo $sqlQuery;
            $queryResult = mysql_query($sqlQuery, $dbAccess['dbConnection']);
            $id = mysql_insert_id($dbAccess['dbConnection']);
            $this->closeMysqlConnection($dbAccess['dbConnection']);
        }

        return $id;
    }

    public function getCampaignCountById($campaignId) {

        $dbAccess = $this->getMysqlConnection();
        $total = 0;

        if (!$dbAccess['dbConnection']) {
            $logDebug = " Echec getCampaignCountById()  " . $dbAccess['errorMessage'] . "  " . $dbAccess['errorCode'] . " MSISDN :: $msisdn";
            $this->svaLog->writeSystemLog($logDebug);
            die("IMPOSSIBLE DE SE CONNECTER A LA BASE DE DONNEES");
        } else {


            $sqlQuery = "select count(1) total from queues_processor_tab where campaign_id_n='$campaignId'";
            $queryResult = mysql_query($sqlQuery, $dbAccess['dbConnection']);
            //echo $sqlQuery;
            $data = mysql_fetch_array($queryResult);
            $total = $data['total'];
            $this->closeMysqlConnection($dbAccess['dbConnection']);
        }


        return $total;
    }

    public function getMMGSenderIdByUser($userId) {

        $dbAccess = $this->connectMysqlDatabase();

        if (!$dbAccess['dbConnection']) {
            $logDebug = " Echec getMMGSenderIdByUser()  " . $dbAccess['errorMessage'] . "  " . $dbAccess['errorCode'] . " MSISDN :: $this->msisdn";
            $this->svaLog->writeSystemLog($logDebug);
            die("IMPOSSIBLE DE SE CONNECTER A LA BASE DE DONNEES");
        } else {

            $sqlQuery = "
                    
                  select   sender_id_n
                        , creation_datetime_d
                        , owner_id_n
                        , sender_name_v
                        , status_n 
                 FROM mmg_senderid_tab
                 WHERE owner_id_n = $userId
                ";


            // echo $sqlQuery;
            $sqlResult = mysql_query($sqlQuery, $dbAccess['dbConnection']);

            $this->closeMysqlConnection($dbAccess['dbConnection']);
        }

        return $this->mysqlRessourceJson($sqlResult);
    }

    /////////////////   insertion des expediteur



    public function insertMMGSender($senderName, $destName) {

        $dbAccess = $this->connectMysqlDatabase();

        if (!$dbAccess['dbConnection']) {
            $logDebug = " Echec getMMGSenderIdByUser()  " . $dbAccess['errorMessage'] . "  " . $dbAccess['errorCode'] . " MSISDN :: $this->msisdn";
            $this->svaLog->writeSystemLog($logDebug);
            die("IMPOSSIBLE DE SE CONNECTER A LA BASE DE DONNEES");
        } else {

            $sqlQuery = 'INSERT INTO mmg_senderid_tab VALUES("",now(),"' . $destName . '","' . $senderName . '","0")';


            // echo $sqlQuery;
            $sqlResult = mysql_query($sqlQuery, $dbAccess['dbConnection']) or die("error");

            $this->closeMysqlConnection($dbAccess['dbConnection']);
        }

        //return $this->mysqlRessourceJson($sqlResult);
    }

    public function updateMMGSender($id, $senderName, $destName) {

        $dbAccess = $this->connectMysqlDatabase();

        if (!$dbAccess['dbConnection']) {
            $logDebug = " Echec getMMGSenderIdByUser()  " . $dbAccess['errorMessage'] . "  " . $dbAccess['errorCode'] . " MSISDN :: $this->msisdn";
            $this->svaLog->writeSystemLog($logDebug);
            die("IMPOSSIBLE DE SE CONNECTER A LA BASE DE DONNEES");
        } else {

            $sqlQuery = 'UPDATE  mmg_senderid_tab SET sender_name_v="' . $senderName . '", owner_id_n="' . $destName . '" WHERE sender_id_n="' . $id . '" ';


            // echo $sqlQuery;
            $sqlResult = mysql_query($sqlQuery, $dbAccess['dbConnection']) or die("error");

            $this->closeMysqlConnection($dbAccess['dbConnection']);
        }

        //return $this->mysqlRessourceJson($sqlResult);
    }

    public function getMMGTariffdByType($type, $profile, $userId) {

        $dbAccess = $this->connectMysqlDatabase();

        if (!$dbAccess['dbConnection']) {
            $logDebug = " Echec getMMGTariffdByType()  " . $dbAccess['errorMessage'] . "  " . $dbAccess['errorCode'] . " MSISDN :: $this->msisdn";
            $this->svaLog->writeSystemLog($logDebug);
            die("IMPOSSIBLE DE SE CONNECTER A LA BASE DE DONNEES");
        } else {

            $userSql = "";

            if ($profile == 2) {

                $userSql = "and a.user_id_n=$userId";
            }


            $sqlQuery = "
                    
                  select      a.billing_id_n
                            , a.creation_datetime_d
                            , a.billing_name_v
                            , a.user_id_n
                            , a.type_id_n
                            , a.status_n 
                     from master_billing_tab a 
                     where a.type_id_n = $type
                     $userSql
                ";


            // echo $sqlQuery;
            $sqlResult = mysql_query($sqlQuery, $dbAccess['dbConnection']);

            $this->closeMysqlConnection($dbAccess['dbConnection']);
        }

        return $this->mysqlRessourceJson($sqlResult);
    }

    public function SMSCAddTarifPLan($bilingId, $smppId, $priority, $statut) {

        $dbAccess = $this->getMysqlConnection();

        if (!$dbAccess['dbConnection']) {
            $logDebug = " Echec SMSCAddTarifPLan()  " . $dbAccess['errorMessage'] . "  " . $dbAccess['errorCode'] . " MSISDN :: $msisdn";
            $this->svaLog->writeSystemLog($logDebug);
            die("IMPOSSIBLE DE SE CONNECTER A LA BASE DE DONNEES");
        } else {


            $sqlQuery = "INSERT 
                INTO smsc_billing_tab   
                VALUES (null,'$bilingId','$smppId','$priority','$statut')";
            //echo $sqlQuery;
            $status = mysql_query($sqlQuery, $dbAccess['dbConnection']);
            $id = mysql_insert_id($dbAccess['dbConnection']);
            //$this->closeMysqlConnection($dbAccess['dbConnection']);
        }

        return $id;
    }

    public function SMSCUpdateTarifPlan($billingId, $smppId, $priority, $statut) {

        $dbAccess = $this->getMysqlConnection();
        $status = false;

        if (!$dbAccess['dbConnection']) {
            $logDebug = " Echec SMSCUpdateTarifPlan()  " . $dbAccess['errorMessage'] . "  " . $dbAccess['errorCode'] . " MSISDN :: $msisdn";
            $this->svaLog->writeSystemLog($logDebug);
            die("IMPOSSIBLE DE SE CONNECTER A LA BASE DE DONNEES");
        } else {


            $sqlQuery = "UPDATE smsc_billing_tab
                         SET  billing_id_n='$billingId'
                             , priority_n='$priority'
                             ,status_n='$statut'
                         WHERE smsc_id_n='$smppId'
                ";
            //echo $sqlQuery;
            $status = mysql_query($sqlQuery, $dbAccess['dbConnection']);
            $this->closeMysqlConnection($dbAccess['dbConnection']);
        }

        return $status;
    }

    public function genericQuery($sqlQuery) {

        $dbAccess = $this->getMysqlConnection();

        if (!$dbAccess['dbConnection']) {
            $logDebug = " Echec genericQuery()  " . $dbAccess['errorMessage'] . "  " . $dbAccess['errorCode'] . " MSISDN :: $msisdn";
            $this->svaLog->writeSystemLog($logDebug);
            die("IMPOSSIBLE DE SE CONNECTER A LA BASE DE DONNEES");
        } else {


            //echo $sqlQuery;
            $sqlResult = mysql_query($sqlQuery, $dbAccess['dbConnection']);
            $this->closeMysqlConnection($dbAccess['dbConnection']);
        }

        return $sqlResult;
    }

    public function getActiveCampaignByProfileQuery($profileId, $userId, $campaignId, $option, $startLimit, $maxElement, $startDate, $endDate) {

        $limitSql = "";
        $campaignSql = "";
        $dateSql = "";
        if ($option)
            $limitSql = "LIMIT $startLimit, $maxElement";

        if ($campaignId != -1) {

            $campaignSql = "AND a.campaign_id_n = '$campaignId'";
        } else {

            $dateSql = "and a.creation_date_d>='$startDate'";
            $dateSql .= " and a.creation_date_d<='$endDate'";
        }


        if ($profileId == 1 || $profileId == 3) {
            $sqlQuery = "  
                 select    DATE_FORMAT(a.creation_date_d , '%d/%m/%Y %H:%i:%s')  \"DATE\"  
                        , a.campaign_name_v \"CAMPAGNE\"
                        , concat(a.start_date_d,' ',a.start_time_d)  \"DATE DEBUT\" 
                        , concat(a.end_date_d,' ',a.end_time_d)   \"DATE FIN\"
                        , b.total_count_n  \"TOTAL\"
                        , b.total_processed_n \"NB TRAITES\"
                        , b.total_dlvr_smsc_n  \"NB DELIVRES SMSC\"
                        , b.total_dlvr_phone_n \"NB DELIVRES MOBILE\"
                        , b.total_rejected_n   \"NB REJETES SMSC\"
                        , b.total_undlvr_phone_n \"NB NON-DELIVRE MOBILE\"
                        , b.total_queued_smsc_n  \"NB ATTENTE SMSC\"
                 FROM master_campaigns_tab a,
                      campaign_summary_tab b
                 where a.campaign_id_n = b.campaign_id_n
                 $dateSql
                 $campaignSql
                 order by a.campaign_id_n desc
                 $limitSql
                ";
        } elseif ($profileId == 2) {

            $sqlQuery = "  
                          select   DATE_FORMAT(a.creation_date_d , '%d/%m/%Y %H:%i:%s')  \"DATE\"  
                                    , a.campaign_name_v \"CAMPAGNE\"
                                    , concat(a.start_date_d,' ',a.start_time_d)  \"DATE DEBUT\" 
                                    , concat(a.end_date_d,' ',a.end_time_d)   \"DATE FIN\"
                                    , b.total_count_n  \"TOTAL\"
                                    , b.total_processed_n \"NB TRAITES\"
                                    , b.total_dlvr_smsc_n  \"NB DELIVRES SMSC\"
                                    , b.total_dlvr_phone_n \"NB DELIVRES MOBILE\"
                                    , b.total_rejected_n   \"NB REJETES SMSC\"
                                    , b.total_undlvr_phone_n \"NB NON-DELIVRE MOBILE\"
                                    , b.total_queued_smsc_n  \"NB ATTENTE SMSC\"
                        FROM master_campaigns_tab a,
                             campaign_summary_tab b
                        where a.campaign_id_n = b.campaign_id_n
                        and a.owner_id_n='$userId'
                        $dateSql
                        $campaignSql
                        order by a.campaign_id_n desc
                        $limitSql

                ";
        }

        return urlencode($sqlQuery);
    }

    public function getMMGOutgoingCdr($profileId, $username, $statut, $option, $startLimit, $maxElement, $startDate, $endDate, $smscId, $networkId, $dlrId, $src, $dst) {

        $dbAccess = $this->connectMysqlDatabase();

        if (!$dbAccess['dbConnection']) {
            $logDebug = " Echec getMMGOutgoingCdr()  " . $dbAccess['errorMessage'] . "  " . $dbAccess['errorCode'] . " MSISDN :: $this->msisdn";
            $this->svaLog->writeSystemLog($logDebug);
            die("IMPOSSIBLE DE SE CONNECTER A LA BASE DE DONNEES");
        } else {

            $limitSql = "";
            $statusSql = "";
            $dateSql = "";
            $userSql = "";
            $smscSql = "";
            $networkSql = "";
            $dlrSql = "";
            $srcSql = "";
            $dstSql = "";

            if ($option)
                $limitSql = "LIMIT $startLimit, $maxElement";

            $dateSql = "and a.event_datetime_d>='$startDate'";
            $dateSql .= " and a.event_datetime_d<='$endDate'";

            if ($username != -1) {

                $userSql = " and a.user_name_v = '$username'";
            }


            if ($statut != -1) {

                $statusSql = "and a.error_code_n='$statut'";
            }



            if ($smscId != -1) {

                $smscSql = "and a.smsc_id_n='$smscId'";
            }

            if ($networkId != -1) {

                $networkSql = "and a.network_id_n='$networkId'";
            }

            if ($dlrId != -1) {

                $dlrSql = "and a.dlr_type_n='$dlrId'";
            }


            if (strlen($src) != 0) {

                $srcSql = "and a.src_phone_v='$src'";
            }

            if (strlen($dst) != 0) {

                $dstSql = "and a.dst_phone_v='$dst'";
            }


            if ($profileId == 1 || $profileId == 3) {
                $sqlQuery = "  
                    
                   select   a.event_id_n
                                    , DATE_FORMAT( a.event_datetime_d , '%d/%m/%Y %H:%i:%s') event_datetime_d  
                                    , a.session_id_n
                                    , a.src_phone_v
                                    , a.dst_phone_v
                                    , REPLACE(a.content_text_v,'+',' ') content_text_v
                                    , a.user_name_v
                                    , a.ref_number_v
                                    , a.remote_ip_v
                                    , a.country_name_v
                                    , a.network_name_v
                                    , a.client_billing_route_v
                                    , a.last_balance_v
                                    , a.billing_fees_v
                                    , a.current_balance_v
                                    , a.smsc_name_v
                                    , a.smsc_route_name_v
                                    , b.long_desc_v statut
                                    , c.long_desc_v dlr
                                FROM  mmg_upld_outgoing_cdr_tab a
                                     , master_errorcodes_tab b
                                     , master_errorcodes_tab c    
                                WHERE a.error_code_n = b.error_code_n
                                and c.error_code_n = a.dlr_type_n
                                $dateSql
                                $statusSql
                                $userSql
                                $smscSql
                                $networkSql
                                $dlrSql   
                                $srcSql
                                $dstSql
                                order by a.event_datetime_d desc
                                $limitSql
                ";
            } elseif ($profileId == 2) {

                $sqlQuery = "  
                         select   a.event_id_n
                                    , DATE_FORMAT( a.event_datetime_d , '%d/%m/%Y %H:%i:%s') event_datetime_d  
                                    , a.session_id_n
                                    , a.src_phone_v
                                    , a.dst_phone_v
                                    , REPLACE(a.content_text_v,'+',' ') content_text_v
                                    , a.user_name_v
                                    , a.ref_number_v
                                    , a.remote_ip_v
                                    , a.country_name_v
                                    , a.network_name_v
                                    , a.client_billing_route_v
                                    , a.last_balance_v
                                    , a.billing_fees_v
                                    , a.current_balance_v
                                    , a.smsc_name_v
                                    , a.smsc_route_name_v
                                    , b.long_desc_v statut
                                    , c.long_desc_v dlr
                                FROM  mmg_upld_outgoing_cdr_tab a
                                     , master_errorcodes_tab b
                                     , master_errorcodes_tab c
                                WHERE a.error_code_n = b.error_code_n
                                and c.error_code_n = a.dlr_type_n
                                and a.user_name_v = '$username' 
                                $dateSql
                                $statusSql
                                $userSql
                                $networkSql
                                $dlrSql   
                                $srcSql
                                $dstSql
                                order by a.event_datetime_d desc
                                $limitSql

                ";
            }
            echo $sqlQuery;
            $sqlResult = mysql_query($sqlQuery, $dbAccess['dbConnection']);
            $this->closeMysqlConnection($dbAccess['dbConnection']);
        }

        return $this->mysqlRessourceJson($sqlResult);
    }

    public function getMMGOutgoingCdrQuery($profileId, $username, $statut, $option, $startLimit, $maxElement, $startDate, $endDate, $smscId, $networkId, $dlrId, $src, $dst) {


        $limitSql = "";
        $statusSql = "";
        $dateSql = "";
        $userSql = "";
        $smscSql = "";
        $networkSql = "";
        $dlrSql = "";
        $srcSql = "";
        $dstSql = "";

        if ($option)
            $limitSql = "LIMIT $startLimit, $maxElement";

        $dateSql = "and a.event_datetime_d>='$startDate'";
        $dateSql .= " and a.event_datetime_d<='$endDate'";


        if ($statut != -1) {

            $statusSql = "and a.error_code_n='$statut'";
        }

        if ($username != -1) {

            $userSql = " and  a.user_name_v = '$username'";
        }

        
        if ($smscId != -1) {

                $smscSql = "and a.smsc_id_n='$smscId'";
            }

            if ($networkId != -1) {

                $networkSql = "and a.network_id_n='$networkId'";
            }

            if ($dlrId != -1) {

                $dlrSql = "and a.dlr_type_n='$dlrId'";
            }


            if (strlen($src) != 0) {

                $srcSql = "and a.src_phone_v='$src'";
            }

            if (strlen($dst) != 0) {

                $dstSql = "and a.dst_phone_v='$dst'";
            }

        if ($profileId == 1 || $profileId == 3) {
            $sqlQuery = "  
                    
                   select   a.event_id_n
                                    , DATE_FORMAT( a.event_datetime_d , '%d/%m/%Y %H:%i:%s') event_datetime_d  
                                    , a.session_id_n
                                    , a.src_phone_v
                                    , a.dst_phone_v
                                    , REPLACE(a.content_text_v,'+',' ') content_text_v
                                    , a.user_name_v
                                    , a.ref_number_v
                                    , a.remote_ip_v
                                    , a.country_name_v
                                    , a.network_name_v
                                    , a.client_billing_route_v
                                    , a.last_balance_v
                                    , a.billing_fees_v
                                    , a.current_balance_v
                                    , a.smsc_name_v
                                    , a.smsc_route_name_v
                                    , b.long_desc_v statut
                                    , c.long_desc_v dlr
                                FROM  mmg_upld_outgoing_cdr_tab a
                                     , master_errorcodes_tab b
                                     , master_errorcodes_tab c
                                WHERE a.error_code_n = b.error_code_n
                                and c.error_code_n = a.dlr_type_n
                                $dateSql
                                $statusSql
                                $userSql
                                order by a.event_datetime_d desc
                                $limitSql
                ";
        } elseif ($profileId == 2) {

            $sqlQuery = "  
                         select   a.event_id_n
                                    , DATE_FORMAT( a.event_datetime_d , '%d/%m/%Y %H:%i:%s') event_datetime_d  
                                    , a.session_id_n
                                    , a.src_phone_v
                                    , a.dst_phone_v
                                    , REPLACE(a.content_text_v,'+',' ') content_text_v
                                    , a.user_name_v
                                    , a.ref_number_v
                                    , a.remote_ip_v
                                    , a.country_name_v
                                    , a.network_name_v
                                    , a.client_billing_route_v
                                    , a.last_balance_v
                                    , a.billing_fees_v
                                    , a.current_balance_v
                                    , a.smsc_name_v
                                    , a.smsc_route_name_v
                                    , b.long_desc_v statut
                                    , c.long_desc_v dlr
                                FROM  mmg_upld_outgoing_cdr_tab a
                                     , master_errorcodes_tab b
                                     , master_errorcodes_tab c
                                WHERE a.error_code_n = b.error_code_n
                                and c.error_code_n = a.dlr_type_n
                                and a.user_name_v = '$username' 
                                $dateSql
                                $statusSql
                                $userSql
                                order by a.event_datetime_d desc
                                $limitSql

                ";
        }

        return urlencode($sqlQuery);
    }

    public function getShortCodeListbyUser($profile, $userId) {

        $dbAccess = $this->connectMysqlDatabase();

        if (!$dbAccess['dbConnection']) {
            $logDebug = " Echec getShortCodeListbyUser()  " . $dbAccess['errorMessage'] . "  " . $dbAccess['errorCode'] . " MSISDN :: $this->msisdn";
            $this->svaLog->writeSystemLog($logDebug);
            die("IMPOSSIBLE DE SE CONNECTER A LA BASE DE DONNEES");
        } else {



            $userSql = "";



            if ($profile == 2) {

                $userSql = " and g.user_id_n='$userId'";
            } else {

                if ($userId != -1) {

                    $userSql = " and g.user_id_n='$userId'";
                }
            }


            $sqlQuery = "
                    
                          select  z.shortcode_id_n
                                , z.creation_datetime_d
                                , z.shortcode_number_n
                                , z.shortcode_desc_v
                                , z.user_id_n
                                , z.module_id_n
                                , z.status_n 
                                , g.creation_date_d
                                , g.user_name_v   
                          FROM master_shortcode_tab z,
                               master_users_tab g
                          where z.user_id_n = g.user_id_n
                          $userSql
                     
                ";




            // echo $sqlQuery;
            $sqlResult = mysql_query($sqlQuery, $dbAccess['dbConnection']);

            $this->closeMysqlConnection($dbAccess['dbConnection']);
        }

        return $this->mysqlRessourceJson($sqlResult);
    }

    public function getErrorCodeList() {

        $dbAccess = $this->connectMysqlDatabase();

        if (!$dbAccess['dbConnection']) {
            $logDebug = " Echec getErrorCodeList()  " . $dbAccess['errorMessage'] . "  " . $dbAccess['errorCode'] . " MSISDN :: $this->msisdn";
            $this->svaLog->writeSystemLog($logDebug);
            die("IMPOSSIBLE DE SE CONNECTER A LA BASE DE DONNEES");
        } else {





            $sqlQuery = "
                    
                            select   a.errorcode_id_n
                                    , a.language_id_n
                                    , a.error_code_n
                                    , a.errorcode_desc_v
                                    , a.long_desc_v
                                    , a.status_n  
                             FROM master_errorcodes_tab a 
                     
                ";




            // echo $sqlQuery;
            $sqlResult = mysql_query($sqlQuery, $dbAccess['dbConnection']);

            $this->closeMysqlConnection($dbAccess['dbConnection']);
        }

        return $this->mysqlRessourceJson($sqlResult);
    }

    public function getMMGIncomingCdr($profileId, $userId, $shortcode, $option, $startLimit, $maxElement, $startDate, $endDate, $smscId, $src, $smppStatus, $httpStatus) {

        $dbAccess = $this->connectMysqlDatabase();

        if (!$dbAccess['dbConnection']) {
            $logDebug = " Echec getMMGIncomingCdr()  " . $dbAccess['errorMessage'] . "  " . $dbAccess['errorCode'] . " MSISDN :: $this->msisdn";
            $this->svaLog->writeSystemLog($logDebug);
            die("IMPOSSIBLE DE SE CONNECTER A LA BASE DE DONNEES");
        } else {

            $limitSql = "";
            $statusSql1 = "";
            $statusSql2 = "";
            $dateSql = "";
            $userSql = "";
            $shortcodeSql = "";
            $smscSql = "";
            $srcSql = "";
            
            if($smppStatus != -1 ){
                
                $statusSql1 = " and c.char_field_1 ='$smppStatus'";
                
                
            }
            
            if($httpStatus != -1 ){
                
                $statusSql2 = " and c.char_field_2='$httpStatus'";
                
                
            }
            

            if ($option)
                $limitSql = "LIMIT $startLimit, $maxElement";

            $dateSql = "and a.event_datetime_d>='$startDate'";
            $dateSql .= " and a.event_datetime_d<='$endDate'";

            if ($smscId != -1) {

                $smscSql = " and b.smsc_id_n = '$smscId'";
            }

            if ($userId != -1) {

                $userSql = " and g.user_id_n = '$userId'";
            }


            if ($shortcode != -1) {

                $shortcodeSql = "and a.dst_phone_v='$shortcode'";
            }

            
            if(strlen($src)!=0){
                
                $srcSql = "and a.src_phone_v='$src'";
            }


            $sqlQuery = "  
                    
                     select a.event_id_n
                            , DATE_FORMAT( a.event_datetime_d , '%d/%m/%Y %H:%i:%s') event_datetime_d 
                            , a.src_phone_v
                            , a.dst_phone_v
                            , a.content_text_v
                            , a.smsc_id_v
                            , a.msg_id_n
                            , a.mclass_id_n
                            , a.session_id_n
                            , b.smsc_name_v
                            , b.smsc_id_n
                            , case c.char_field_1 
                              when 0 then 'Traité'
                              when 1 then 'Non-Traité'
                              else 'Non tenté'
                              end httpstatus
                            , case c.char_field_2  
                              when 0 then 'Traité'
                              when 1 then 'Non-Traité'
                              else  'Non tenté'
                              end smppstatus
                            , a.user_name_v
                      from   mmg_upld_incoming_cdr_tab a
                            , master_smscs_tab b
                            , master_users_tab g
                            , mmg_routing_log_tab c
                      where a.smsc_id_n = b.smsc_id_n
                      and c.session_id_n = a.session_id_n
                      and g.user_name_v = a.user_name_v
                      $dateSql
                      $shortcodeSql
                      $userSql
                      $smscSql
                      $srcSql
                      $statusSql1
                      $statusSql2
                      order by a.event_datetime_d desc
                      $limitSql ";

            //echo $sqlQuery;
            $sqlResult = mysql_query($sqlQuery, $dbAccess['dbConnection']);
            $this->closeMysqlConnection($dbAccess['dbConnection']);
        }

        return $this->mysqlRessourceJson($sqlResult);
    }

    public function getMMGIncomingCdrQuery($profileId, $userId, $shortcode, $option, $startLimit, $maxElement, $startDate, $endDate, $smscId, $src, $smppStatus, $httpStatus) {

        $limitSql = "";
            $statusSql1 = "";
             $statusSql2 = "";
            $dateSql = "";
            $userSql = "";
            $shortcodeSql = "";
            $smscSql = "";
            $srcSql = "";
            
            
               if($smppStatus != -1 ){
                
                $statusSql1 = " and c.char_field_1 ='$smppStatus'";
                
                
            }
            
            if($httpStatus != -1 ){
                
                $statusSql2 = " and c.char_field_2='$httpStatus'";
                
                
            }
            

            if ($option)
                $limitSql = "LIMIT $startLimit, $maxElement";

            $dateSql = "and a.event_datetime_d>='$startDate'";
            $dateSql .= " and a.event_datetime_d<='$endDate'";

            if ($smscId != -1) {

                $smscSql = " and b.smsc_id_n = '$smscId'";
            }

            if ($userId != -1) {

                $userSql = " and g.user_id_n = '$userId'";
            }


            if ($shortcode != -1) {

                $shortcodeSql = "and a.dst_phone_v='$shortcode'";
            }

            
            if(strlen($src)!=0){
                
                $srcSql = "and a.src_phone_v='$src'";
            }


            $sqlQuery = "  
                    
                     select DATE_FORMAT( a.event_datetime_d , '%d/%m/%Y %H:%i:%s')  \"DATE\"  
                            , a.src_phone_v     \"SOURCE\" 
                            , a.dst_phone_v     \"DESTINATION\" 
                            , a.content_text_v   \"CONTENU\" 
                            , a.user_name_v      \"UTILISATEUR\"
                            , b.smsc_name_v      \"SMPP\"
                            , case c.char_field_1  
                              when 0 then 'Traité'
                              when 1 then 'Non-Traité'
                              else 'Non tenté'
                              end \"STATUT HTTP\" 
                            , case c.char_field_2  
                              when 0 then 'Traité'
                              when 1 then 'Non-Traité'
                              else  'Non tenté'
                              end \"STATUT SMPP\" 
                      from   mmg_upld_incoming_cdr_tab a
                            , master_smscs_tab b
                            , master_users_tab g
                            , mmg_routing_log_tab c
                      where a.smsc_id_n = b.smsc_id_n
                      and g.user_name_v = a.user_name_v
                      and c.session_id_n = a.session_id_n
                      $dateSql
                      $shortcodeSql
                      $userSql
                      $smscSql
                      $srcSql
                      $statusSql1
                      $statusSql2
                      order by a.event_datetime_d desc
                      $limitSql ";
            
            

        return urlencode($sqlQuery);
    }

    public function UserAddTarifPLan($bilingId, $userId, $statut) {

        $dbAccess = $this->getMysqlConnection();

        if (!$dbAccess['dbConnection']) {
            $logDebug = " Echec UserAddTarifPLan()  " . $dbAccess['errorMessage'] . "  " . $dbAccess['errorCode'] . " MSISDN :: $msisdn";
            $this->svaLog->writeSystemLog($logDebug);
            die("IMPOSSIBLE DE SE CONNECTER A LA BASE DE DONNEES");
        } else {


            $sqlQuery = "INSERT 
                INTO mmg_user_billing_tab   
                VALUES (null,'$bilingId','$userId','$statut')";
            //echo $sqlQuery;
            $status = mysql_query($sqlQuery, $dbAccess['dbConnection']);
            $id = mysql_insert_id($dbAccess['dbConnection']);
            //$this->closeMysqlConnection($dbAccess['dbConnection']);
        }

        return $id;
    }

    public function getUserDetailByRight($profile, $userId, $option, $startLimit, $maxElement) {

        $dbAccess = $this->connectMysqlDatabase();

        if (!$dbAccess['dbConnection']) {
            $logDebug = " Echec getUserDetailByRight()  " . $dbAccess['errorMessage'] . "  " . $dbAccess['errorCode'] . " MSISDN :: $this->msisdn";
            $this->svaLog->writeSystemLog($logDebug);
            die("IMPOSSIBLE DE SE CONNECTER A LA BASE DE DONNEES");
        } else {


            $limitSql = "";
            $userSql = "";

            if ($profile == 2) {


                $userSql = " and (a.user_id_n='$userId' OR a.creator_id_n='$userId') ";
            }



            if ($option)
                $limitSql = "LIMIT $startLimit,$maxElement";

            $sqlQuery = "
                    
                   select  a.user_id_n
                        ,a.user_name_v
                        ,a.first_name_v
                        ,a.last_name_v
                        ,date_format(a.creation_date_d,'%d/%m/%Y %H:%i:%s') creationDate
                        ,a.mobile_phone_v
                        ,a.email_v
                        ,b.profile_name_v 
                        ,c.description_v status_desc
                        ,a.status_n
                        ,e.billing_name_v
                 from master_users_tab a,
                    master_profile_tab b,
                    master_status_tab c,
                    mmg_user_billing_tab d,
                    master_billing_tab e
                where a.profile_id_n = b.profile_id_n
                and a.status_n = c.status_n
                and d.user_id_n = a.user_id_n
                and e.billing_id_n = d.billing_id_n
                $userSql
                $limitSql
                ";


            //echo $sqlQuery;
            $req = mysql_query($sqlQuery, $dbAccess['dbConnection']) or die("error" . mysql_error());

            $this->closeMysqlConnection($dbAccess['dbConnection']);
        }

        return $this->mysqlRessourceJson($req);
    }

    public function getProfileListByRight($profile) {

        $dbAccess = $this->connectMysqlDatabase();

        if (!$dbAccess['dbConnection']) {
            $logDebug = " Echec getProfileList()  " . $dbAccess['errorMessage'] . "  " . $dbAccess['errorCode'] . " MSISDN :: $this->msisdn";
            $this->svaLog->writeSystemLog($logDebug);
            die("IMPOSSIBLE DE SE CONNECTER A LA BASE DE DONNEES");
        } else {

            $userSql = "";

            if ($profile == 2) {

                $userSql = "where profile_id_n=$profile";
            }

            $sqlQuery = "  
                   select   profile_id_n
                        , profile_name_v
                        , long_desc_v
                        , status_n 
                    FROM master_profile_tab
                    $userSql
                ";


            //echo $sqlQuery;
            $sqlResult = mysql_query($sqlQuery, $dbAccess['dbConnection']);

            $this->closeMysqlConnection($dbAccess['dbConnection']);
        }

        return $this->mysqlRessourceJson($sqlResult);
    }

    public function getUsersByRight($profile, $userId) {

        $dbAccess = $this->connectMysqlDatabase();

        if (!$dbAccess['dbConnection']) {
            $logDebug = " Echec getUsersByRight()  " . $dbAccess['errorMessage'] . "  " . $dbAccess['errorCode'] . " MSISDN :: $this->msisdn";
            $this->svaLog->writeSystemLog($logDebug);
            die("IMPOSSIBLE DE SE CONNECTER A LA BASE DE DONNEES");
        } else {


            $userSql = "";

            if ($profile == 2) {


                $userSql = " where (a.user_id_n='$userId' OR a.creator_id_n='$userId') ";
            }

            $sqlQuery = "SELECT * "
                    . "  FROM master_users_tab a"
                    . "  $userSql ";
            // echo $sqlQuery;
            $sqlResult = mysql_query($sqlQuery, $dbAccess['dbConnection']);
            $this->closeMysqlConnection($dbAccess['dbConnection']);
        }

        return $sqlResult;
    }

    public function ProcessTransfer($fromUserId, $toUserId, $fromAmount, $toAmount, $actionType, $desc, $statut) {

        $dbAccess = $this->getMysqlConnection();

        if (!$dbAccess['dbConnection']) {
            $logDebug = " Echec ProcessTransfer()  " . $dbAccess['errorMessage'] . "  " . $dbAccess['errorCode'] . " MSISDN :: $msisdn";
            $this->svaLog->writeSystemLog($logDebug);
            die("IMPOSSIBLE DE SE CONNECTER A LA BASE DE DONNEES");
        } else {


            $sqlQuery = "update master_stock_tab set balance_n = balance_n - $fromAmount where user_id_n = $fromUserId";
            //echo $sqlQuery;
            $status = mysql_query($sqlQuery, $dbAccess['dbConnection']);


            $sqlQuery = "update master_stock_tab set balance_n = balance_n +  $toAmount  where user_id_n = $toUserId";
            //echo $sqlQuery;
            $status = mysql_query($sqlQuery, $dbAccess['dbConnection']);


            $sqlQuery = "insert into mmg_replenishments_tab ( item_id_n, event_datetime_d, from_userid_n, to_userid_n, from_amount_v, to_amount_v, session_id_n, event_desc_v, status_n ) VALUES (null,now(),'$fromUserId','$toUserId','$fromAmount','$toAmount','$actionType','$desc','$statut')";

            //echo $sqlQuery;
            $status = mysql_query($sqlQuery, $dbAccess['dbConnection']);



            $id = mysql_insert_id($dbAccess['dbConnection']);
            //$this->closeMysqlConnection($dbAccess['dbConnection']);
        }

        return $id;
    }

    public function getCampaignList($profileId, $ownerId, $startLimit, $maxElement, $option) {

        $dbAccess = $this->connectMysqlDatabase();

        if (!$dbAccess['dbConnection']) {
            $logDebug = " Echec getCampaignList()  " . $dbAccess['errorMessage'] . "  " . $dbAccess['errorCode'] . " MSISDN :: $this->msisdn";
            $this->svaLog->writeSystemLog($logDebug);
            die("IMPOSSIBLE DE SE CONNECTER A LA BASE DE DONNEES");
        } else {

            $limitSql = "";
            $profileSql = "";
            $userSql = "";
            if ($option)
                $limitSql = "LIMIT $startLimit, $maxElement   ";
            if ($profileId == 2)
                $profileSql = "AND b.profile_id_n='$profileId' ";
            if (isset($ownerId) && $profileId == 2)
                $userSql = "AND b.user_id_n='$ownerId' ";



            $sqlQuery = " 
                    
                      select    a.campaign_id_n
                                , a.creation_date_d
                                , campaign_name_v
                                , date_format(start_date_d,'%d/%m/%Y') start_date_d 
                                , date_format(start_time_d,'%H:%i:%s') start_time_d 
                                , owner_id_n
                                , sender_id_n
                                , appid
                                , c.description_v status_desc
                                , b.user_name_v
                                , c.status_n
                                , d.total_count_n  totalElement
                                , d.total_processed_n totalCalled
                            from master_campaigns_tab a,
                                master_users_tab b,
                                master_status_tab c,
                                campaign_summary_tab d
                            where a.owner_id_n = b.user_id_n
                            and a.status_n = c.status_n
                            and d.campaign_id_n = a.campaign_id_n
                            $userSql                                                                                                                                                      
                            $profileSql
                            ORDER BY a.creation_date_d DESC
                            $limitSql
                ";







            // echo $sqlQuery;
            $sqlResult = mysql_query($sqlQuery, $dbAccess['dbConnection']);

            $this->closeMysqlConnection($dbAccess['dbConnection']);
        }

        return $this->mysqlRessourceJson($sqlResult);
    }

    public function getBrandList() {

        $dbAccess = $this->connectMysqlDatabase();

        if (!$dbAccess['dbConnection']) {
            $logDebug = " Echec getBrandList()  " . $dbAccess['errorMessage'] . "  " . $dbAccess['errorCode'] . " MSISDN :: $this->msisdn";
            $this->svaLog->writeSystemLog($logDebug);
            die("IMPOSSIBLE DE SE CONNECTER A LA BASE DE DONNEES");
        } else {



            $sqlQuery = "  
                  select      a.brand_id_n
                            , a.brand_name_v
                            , a.brand_long_desc_v
                            , a.brand_short_desc_v
                            , a.status_n 
                     FROM master_brands_tab a
                ";


            //echo $sqlQuery;
            $sqlResult = mysql_query($sqlQuery, $dbAccess['dbConnection']);

            $this->closeMysqlConnection($dbAccess['dbConnection']);
        }

        return $this->mysqlRessourceJson($sqlResult);
    }

    public function InsertUpdateUserSmpp($userId, $hostname, $port, $recvdPort, $isTranceiver, $destTon, $destNpi, $srcTon, $dstNpi, $version, $reconnectDelay, $charset, $throughtput,$brandId , $statut) {

        $dbAccess = $this->getMysqlConnection();

        if (!$dbAccess['dbConnection']) {
            $logDebug = " Echec InsertUpdateUserSmpp()  " . $dbAccess['errorMessage'] . "  " . $dbAccess['errorCode'] . " MSISDN :: $msisdn";
            $this->svaLog->writeSystemLog($logDebug);
            die("IMPOSSIBLE DE SE CONNECTER A LA BASE DE DONNEES");
        } else {


            $sqlQuery = "INSERT 
                INTO mmg_user_smpp_tab (smsc_id_n,creation_date_d,user_id_n,host_name_v,port_number_n,receive_port_n,is_transceiver_v,dest_ton_n,dest_npi_n,src_ton_n, src_npi_n, interface_version_n,reconnect_delay_n,alt_charset_v,throught_put_n, char_field_1,status_n)  
                VALUES (null,now(),'$userId','$hostname','$port','$recvdPort','$isTranceiver','$destTon','$destNpi','$srcTon','$dstNpi','$version','$reconnectDelay','$charset','$throughtput','$brandId','$statut')";
            //echo $sqlQuery;
            $status = mysql_query($sqlQuery, $dbAccess['dbConnection']);
            $id = mysql_insert_id($dbAccess['dbConnection']);

            
           // echo $sqlQuery;



            $sqlQuery = "UPDATE  mmg_user_smpp_tab "
                    . " SET  host_name_v = '$hostname' "
                    . ", port_number_n ='$port' "
                    . ", receive_port_n ='$recvdPort' "
                    . ", is_transceiver_v ='$isTranceiver' "
                    . ", dest_ton_n ='$destTon' "
                    . ", dest_npi_n ='$destNpi' "
                    . ", src_ton_n ='$srcTon' "
                    . ", src_npi_n ='$dstNpi' "
                    . ", interface_version_n ='$version' "
                    . ", reconnect_delay_n ='$reconnectDelay' "
                    . ", alt_charset_v ='$charset' "
                    . ", throught_put_n ='$throughtput' "
                    . ", char_field_1 ='$brandId' "
                    . ", status_n ='$statut' "
                    . " WHERE user_id_n='$userId' " 
            ;

            $status = mysql_query($sqlQuery, $dbAccess['dbConnection']);


                //echo $sqlQuery;


            //$this->closeMysqlConnection($dbAccess['dbConnection']);
        }

        return $id;
    }

    public function getUserSMSCByID($userId) {

        $dbAccess = $this->getMysqlConnection();

        if (!$dbAccess['dbConnection']) {
            $logDebug = " Echec getUserSMSCByID()  " . $dbAccess['errorMessage'] . "  " . $dbAccess['errorCode'] . " MSISDN :: $msisdn";
            $this->svaLog->writeSystemLog($logDebug);
            die("IMPOSSIBLE DE SE CONNECTER A LA BASE DE DONNEES");
        } else {



            $sqlQuery = "select   a.smsc_id_n
                                , a.creation_date_d
                                , a.user_id_n
                               , a.host_name_v
                               , a.port_number_n
                               , a.receive_port_n
                               , a.is_transceiver_v
                               , a.dest_ton_n
                               , a.dest_npi_n
                               , a.src_ton_n
                               , a.src_npi_n
                               , a.interface_version_n
                               , a.reconnect_delay_n
                               , a.src_address_autodetect_n
                               , a.alt_charset_v
                               , a.alt_address_charset_v
                               , a.connection_timeout_n
                               , a.throught_put_n
                               , a.char_field_1
                               , b.user_name_v
                               , b.first_name_v
                               , b.last_name_v
                            FROM mmg_user_smpp_tab a, master_users_tab b
                            where a.user_id_n = b.user_id_n
                            and a.user_id_n =  $userId
                                 ";


            //echo $sqlQuery;
            $sqlResult = mysql_query($sqlQuery, $dbAccess['dbConnection']);
            $this->closeMysqlConnection($dbAccess['dbConnection']);
        }

        return $this->mysqlRessourceJson($sqlResult);
    }

    public function getNDCListByNetwork($networkId) {

        $dbAccess = $this->connectMysqlDatabase();

        if (!$dbAccess['dbConnection']) {
            $logDebug = " Echec getNDCListByNetwork()  " . $dbAccess['errorMessage'] . "  " . $dbAccess['errorCode'] . " MSISDN :: $this->msisdn";
            $this->svaLog->writeSystemLog($logDebug);
            die("IMPOSSIBLE DE SE CONNECTER A LA BASE DE DONNEES");
        } else {



            $sqlQuery = "  
                  select  a.item_id_n
                        , a.network_id_n
                        , a.cc_code_v
                        , a.ndc_code_v
                        , a.status_n  
                  FROM network_details_tab a
                  WHERE a.network_id_n = $networkId
                ";


            //echo $sqlQuery;
            $sqlResult = mysql_query($sqlQuery, $dbAccess['dbConnection']);

            $this->closeMysqlConnection($dbAccess['dbConnection']);
        }

        return $this->mysqlRessourceJson($sqlResult);
    }

    public function MMGUpdateNDC($id, $ndc) {

        $dbAccess = $this->getMysqlConnection();
        $totalUser = 0;

        if (!$dbAccess['dbConnection']) {
            $logDebug = " Echec MMGUpdateNDC()  " . $dbAccess['errorMessage'] . "  " . $dbAccess['errorCode'] . " MSISDN :: $msisdn";
            $this->svaLog->writeSystemLog($logDebug);
            die("IMPOSSIBLE DE SE CONNECTER A LA BASE DE DONNEES");
        } else {


            $sqlQuery = "update network_details_tab  set ndc_code_v='$ndc' where item_id_n='$id'";
            $queryResult = mysql_query($sqlQuery, $dbAccess['dbConnection']);
            //echo $sqlQuery;
            // $totalUser = mysql_affected_rows($queryResult);
            $this->closeMysqlConnection($dbAccess['dbConnection']);
        }

        if ($queryResult)
            $totalUser = 1;
        return $totalUser;
    }

    public function MMGDeleteNDC($id) {

        $dbAccess = $this->getMysqlConnection();
        $totalUser = 0;

        if (!$dbAccess['dbConnection']) {
            $logDebug = " Echec MMGDeleteNDC()  " . $dbAccess['errorMessage'] . "  " . $dbAccess['errorCode'] . " MSISDN :: $msisdn";
            $this->svaLog->writeSystemLog($logDebug);
            die("IMPOSSIBLE DE SE CONNECTER A LA BASE DE DONNEES");
        } else {


            $sqlQuery = "delete from network_details_tab where  item_id_n='$id'";
            $queryResult = mysql_query($sqlQuery, $dbAccess['dbConnection']);
            // $totalUser = mysql_affected_rows($queryResult);
            $this->closeMysqlConnection($dbAccess['dbConnection']);
        }

        if ($queryResult)
            $totalUser = 1;
        return $totalUser;
    }

    public function MMGAddNDC($networkId, $cc, $ndc, $statut) {

        $dbAccess = $this->getMysqlConnection();
        $totalUser = 0;

        if (!$dbAccess['dbConnection']) {
            $logDebug = " Echec MMGAddNDC()  " . $dbAccess['errorMessage'] . "  " . $dbAccess['errorCode'] . " MSISDN :: $msisdn";
            $this->svaLog->writeSystemLog($logDebug);
            die("IMPOSSIBLE DE SE CONNECTER A LA BASE DE DONNEES");
        } else {


            $sqlQuery = "INSERT INTO network_details_tab  VALUES (NULL,'$networkId','$cc', '$ndc', '$statut')";
            $queryResult = mysql_query($sqlQuery, $dbAccess['dbConnection']);
            //echo $sqlQuery;
            // $totalUser = mysql_affected_rows($queryResult);
            $this->closeMysqlConnection($dbAccess['dbConnection']);
        }

        if ($queryResult)
            $totalUser = 1;
        return $totalUser;
    }

    public function generateNetworkId() {
        $dbAccess = $this->getMysqlConnection();
        $total = 0;

        if (!$dbAccess['dbConnection']) {
            $logDebug = " Echec generateNetworkId()  " . $dbAccess['errorMessage'] . "  " . $dbAccess['errorCode'] . " MSISDN :: $msisdn";
            $this->svaLog->writeSystemLog($logDebug);
            die("IMPOSSIBLE DE SE CONNECTER A LA BASE DE DONNEES");
        } else {


            $sqlQuery = " select max(network_id_n)+1 network_id_n from master_networks_tab";
            $queryResult = mysql_query($sqlQuery, $dbAccess['dbConnection']);
            //echo $sqlQuery;
            $data = mysql_fetch_array($queryResult);
            $total = $data['network_id_n'];
            $this->closeMysqlConnection($dbAccess['dbConnection']);
        }


        return $total;
    }

    public function getSMSCListShortAll() {

        $dbAccess = $this->getMysqlConnection();

        if (!$dbAccess['dbConnection']) {
            $logDebug = " Echec getSMSCListShortAll()  " . $dbAccess['errorMessage'] . "  " . $dbAccess['errorCode'] . " MSISDN :: $msisdn";
            $this->svaLog->writeSystemLog($logDebug);
            die("IMPOSSIBLE DE SE CONNECTER A LA BASE DE DONNEES");
        } else {

            $sqlQuery = " SELECT    a.smsc_id_n
                                    , a.smsc_name_v
                                    , a.smsc_desc_v
                            FROM   `master_smscs_tab` a
                            WHERE a.status_n = 0 ";


            // echo $sqlQuery;
            $sqlResult = mysql_query($sqlQuery, $dbAccess['dbConnection']);
            $this->closeMysqlConnection($dbAccess['dbConnection']);
        }

        return $this->mysqlRessourceJson($sqlResult);
    }
    
    
    
    
    
    public function getMMGOutgoingCdrQueryGlobal($profileId, $username, $statut, $option, $startLimit, $maxElement, $startDate, $endDate, $smscId, $networkId, $dlrId, $src, $dst) {


        $limitSql = "";
        $statusSql = "";
        $dateSql = "";
        $userSql = "";
        $smscSql = "";
        $networkSql = "";
        $dlrSql = "";
        $srcSql = "";
        $dstSql = "";

        if ($option)
            $limitSql = "LIMIT $startLimit, $maxElement";

        $dateSql = "and a.event_datetime_d>='$startDate'";
        $dateSql .= " and a.event_datetime_d<='$endDate'";


        if ($statut != -1) {

            $statusSql = "and a.error_code_n='$statut'";
        }

        if ($username != -1) {

            $userSql = " and  a.user_name_v = '$username'";
        }

        
        if ($smscId != -1) {

                $smscSql = "and a.smsc_id_n='$smscId'";
            }

            if ($networkId != -1) {

                $networkSql = "and a.network_id_n='$networkId'";
            }

            if ($dlrId != -1) {

                $dlrSql = "and a.dlr_type_n='$dlrId'";
            }


            if (strlen($src) != 0) {

                $srcSql = "and a.src_phone_v='$src'";
            }

            if (strlen($dst) != 0) {

                $dstSql = "and a.dst_phone_v='$dst'";
            }

        if ($profileId == 1 || $profileId == 3) {
            $sqlQuery = "  
                    
                   select   '".$startDate."' dateDebut,
                            '".$endDate."'  dataFin,
                            count(1) total
                                FROM  mmg_upld_outgoing_cdr_tab a
                                     , master_errorcodes_tab b
                                     , master_errorcodes_tab c
                                WHERE a.error_code_n = b.error_code_n
                                and c.error_code_n = a.dlr_type_n
                                $dateSql
                                $statusSql
                                $userSql
                                order by a.event_datetime_d desc
                                $limitSql
                ";
        } elseif ($profileId == 2) {

            $sqlQuery = "  
                         select   a.event_id_n
                                    , DATE_FORMAT( a.event_datetime_d , '%d/%m/%Y %H:%i:%s') event_datetime_d  
                                    , a.session_id_n
                                    , a.src_phone_v
                                    , a.dst_phone_v
                                    , REPLACE(a.content_text_v,'+',' ') content_text_v
                                    , a.user_name_v
                                    , a.ref_number_v
                                    , a.remote_ip_v
                                    , a.country_name_v
                                    , a.network_name_v
                                    , a.client_billing_route_v
                                    , a.last_balance_v
                                    , a.billing_fees_v
                                    , a.current_balance_v
                                    , a.smsc_name_v
                                    , a.smsc_route_name_v
                                    , b.long_desc_v statut
                                    , c.long_desc_v dlr
                                FROM  mmg_upld_outgoing_cdr_tab a
                                     , master_errorcodes_tab b
                                     , master_errorcodes_tab c
                                WHERE a.error_code_n = b.error_code_n
                                and c.error_code_n = a.dlr_type_n
                                and a.user_name_v = '$username' 
                                $dateSql
                                $statusSql
                                $userSql
                                order by a.event_datetime_d desc
                                $limitSql

                ";
        }

        return urlencode($sqlQuery);
    }
    
    
    
    public function InsertUpdateUserSmppRouting($userId, $smscId, $statut) {

        $dbAccess = $this->getMysqlConnection();

        if (!$dbAccess['dbConnection']) {
            $logDebug = " Echec InsertUpdateUserSmppRouting()  " . $dbAccess['errorMessage'] . "  " . $dbAccess['errorCode'] . " MSISDN :: $msisdn";
            $this->svaLog->writeSystemLog($logDebug);
            die("IMPOSSIBLE DE SE CONNECTER A LA BASE DE DONNEES");
        } else {


            $sqlQuery = "INSERT 
                INTO mmg_user_smsc_tab   
                VALUES (null,'$userId','$smscId','$statut')";
            //echo $sqlQuery;
            $status = mysql_query($sqlQuery, $dbAccess['dbConnection']);
            $id = mysql_insert_id($dbAccess['dbConnection']);

            
            //echo $sqlQuery;

            $sqlQuery = "UPDATE  mmg_user_smsc_tab "
                    . " SET  smsc_id_n = '$smscId' "
                    . ", status_n ='$statut' "
                    . " WHERE user_id_n='$userId' "
            ;

            $status = mysql_query($sqlQuery, $dbAccess['dbConnection']);


             //  echo $sqlQuery;


            //$this->closeMysqlConnection($dbAccess['dbConnection']);
        }

        return $id;
    }
    
    
    
    public function getUserSmppRouteById($userId) {

        $dbAccess = $this->connectMysqlDatabase();

        if (!$dbAccess['dbConnection']) {
            $logDebug = " Echec getUserSmppRouteById()  " . $dbAccess['errorMessage'] . "  " . $dbAccess['errorCode'] . " MSISDN :: $this->msisdn";
            $this->svaLog->writeSystemLog($logDebug);
            die("IMPOSSIBLE DE SE CONNECTER A LA BASE DE DONNEES");
        } else {

            $sqlQuery = "  
                 select   user_id_n
                        , smsc_id_n
                    FROM mmg_user_smsc_tab
                    WHERE user_id_n='$userId'
                ";


            // echo $sqlQuery;
            $sqlResult = mysql_query($sqlQuery, $dbAccess['dbConnection']);
            $data = mysql_fetch_array($sqlResult);
            $this->closeMysqlConnection($dbAccess['dbConnection']);
        }

        return $data;
    }
    
    
    public function getUserTarifPlanById($userId) {

        $dbAccess = $this->connectMysqlDatabase();

        if (!$dbAccess['dbConnection']) {
            $logDebug = " Echec getUserTarifPlanById()  " . $dbAccess['errorMessage'] . "  " . $dbAccess['errorCode'] . " MSISDN :: $this->msisdn";
            $this->svaLog->writeSystemLog($logDebug);
            die("IMPOSSIBLE DE SE CONNECTER A LA BASE DE DONNEES");
        } else {

            $sqlQuery = "  
                 select   user_id_n
                        , billing_id_n
                    FROM mmg_user_billing_tab
                    WHERE user_id_n='$userId'
                ";


            // echo $sqlQuery;
            $sqlResult = mysql_query($sqlQuery, $dbAccess['dbConnection']);
            $data = mysql_fetch_array($sqlResult);
            $this->closeMysqlConnection($dbAccess['dbConnection']);
        }

        return $data;
    }
    
    
    
    public function UpdateUserTariffPLan($userId, $newTarif) {

        $dbAccess = $this->getMysqlConnection();
        $totalUser = 0;

        if (!$dbAccess['dbConnection']) {
            $logDebug = " Echec UpdateUserTariffPLan()  " . $dbAccess['errorMessage'] . "  " . $dbAccess['errorCode'] . " MSISDN :: $msisdn";
            $this->svaLog->writeSystemLog($logDebug);
            die("IMPOSSIBLE DE SE CONNECTER A LA BASE DE DONNEES");
        } else {


            $sqlQuery = "update mmg_user_billing_tab  set billing_id_n='$newTarif' where user_id_n='$userId'";
            $queryResult = mysql_query($sqlQuery, $dbAccess['dbConnection']);
            //echo $sqlQuery;
            // $totalUser = mysql_affected_rows($queryResult);
            $this->closeMysqlConnection($dbAccess['dbConnection']);
        }

        if ($queryResult)
            $totalUser = 1;
        return $totalUser;
    }
    
    
    public function getShortCodeListbyUserIdShort($profile, $userId) {

        $dbAccess = $this->connectMysqlDatabase();

        if (!$dbAccess['dbConnection']) {
            $logDebug = " Echec getShortCodeListbyUserIdShort()  " . $dbAccess['errorMessage'] . "  " . $dbAccess['errorCode'] . " MSISDN :: $this->msisdn";
            $this->svaLog->writeSystemLog($logDebug);
            die("IMPOSSIBLE DE SE CONNECTER A LA BASE DE DONNEES");
        } else {



            $userSql = "";



            if ($profile == 2) {

                $userSql = " where z.user_id_n='$userId'";
            } 


            $sqlQuery = "
                    
                          select  z.shortcode_id_n
                                , z.creation_datetime_d
                                , z.shortcode_number_n
                                , z.shortcode_desc_v
                                , z.user_id_n
                                , z.module_id_n
                                , z.status_n 
                          FROM master_shortcode_tab z
                          $userSql
                     
                ";




            // echo $sqlQuery;
            $sqlResult = mysql_query($sqlQuery, $dbAccess['dbConnection']);

            $this->closeMysqlConnection($dbAccess['dbConnection']);
        }

        return $this->mysqlRessourceJson($sqlResult);
    }
    
    
  public function getSMSParameterList($option, $startLimit, $maxElement) {

        $dbAccess = $this->connectMysqlDatabase();

        if (!$dbAccess['dbConnection']) {
            $logDebug = " Echec getSMSParameterList()  " . $dbAccess['errorMessage'] . "  " . $dbAccess['errorCode'] . " MSISDN :: $this->msisdn";
            $this->svaLog->writeSystemLog($logDebug);
            die("IMPOSSIBLE DE SE CONNECTER A LA BASE DE DONNEES");
        } else {

            $limitSql = "";
         

            if ($option)
                $limitSql = "LIMIT $startLimit, $maxElement";


            $sqlQuery = "  
                    
                      select  DATE_FORMAT( c.creation_date_d , '%d/%m/%Y %H:%i:%s') creation_date_d
                                , b.network_name_v
                                , e.country_name_v
                                , c.smsc_name_v
                                , a.item_id_n
                         from smsc_network_tab a
                             , master_networks_tab b
                             , master_smscs_tab c
                             , master_countries_tab e
                         where a.network_id_n = b.network_id_n
                         and a.smsc_id_n = c.smsc_id_n
                         and e.country_id_n = b.country_id_n
                      order by c.creation_date_d desc
                      $limitSql ";

            //echo $sqlQuery;
            $sqlResult = mysql_query($sqlQuery, $dbAccess['dbConnection']);
            $this->closeMysqlConnection($dbAccess['dbConnection']);
        }

        return $this->mysqlRessourceJson($sqlResult);
    }  
    
    
    
    
    public function createSMSCNetwork($networkId, $smscId) {

        $dbAccess = $this->getMysqlConnection();

        if (!$dbAccess['dbConnection']) {
            $logDebug = " Echec createSMSCNetwork()  " . $dbAccess['errorMessage'] . "  " . $dbAccess['errorCode'] . " MSISDN :: $msisdn";
            $this->svaLog->writeSystemLog($logDebug);
            die("IMPOSSIBLE DE SE CONNECTER A LA BASE DE DONNEES");
        } else {


            $sqlQuery = "INSERT 
                INTO smsc_network_tab   
                VALUES (null,'$smscId','$networkId')";
            //echo $sqlQuery;
            $status = mysql_query($sqlQuery, $dbAccess['dbConnection']);
            $id = mysql_insert_id($dbAccess['dbConnection']);
            //$this->closeMysqlConnection($dbAccess['dbConnection']);
        }

        return $id;
    }
    
    
    
    
     public function getTarifPlanDetail($tariffId) {

        $dbAccess = $this->connectMysqlDatabase();

        if (!$dbAccess['dbConnection']) {
            $logDebug = " Echec getTarifPlanDetail()  " . $dbAccess['errorMessage'] . "  " . $dbAccess['errorCode'] . " MSISDN :: $this->msisdn";
            $this->svaLog->writeSystemLog($logDebug);
            die("IMPOSSIBLE DE SE CONNECTER A LA BASE DE DONNEES");
        } else {



            $sqlQuery = "  
                  select    d.billing_name_v
                            ,a.price_v
                            ,b.smsc_name_v
                            ,c.network_name_v
                            ,a.item_id_n
                            , e.country_name_v
                            ,  d.billing_id_n
                     from  billing_details_advanced_tab a
                          , master_smscs_tab b
                          , master_networks_tab c
                          , master_billing_tab d
                          , master_countries_tab e
                     where a.tariff_id_n = d.billing_id_n
                     and a.smsc_id_n = b.smsc_id_n
                     and a.network_id_n = c.network_id_n
                     and c.country_id_n = e.country_id_n
                     and d.billing_id_n = $tariffId
                ";


            //echo $sqlQuery;
            $sqlResult = mysql_query($sqlQuery, $dbAccess['dbConnection']);

            $this->closeMysqlConnection($dbAccess['dbConnection']);
        }

        return $this->mysqlRessourceJson($sqlResult);
    }
    
    
    
     public function upldTarifDetails($tarifId,  $networkId, $smscId, $price) {

        $dbAccess = $this->getMysqlConnection();

        if (!$dbAccess['dbConnection']) {
            $logDebug = " Echec upldTarifDetails()  " . $dbAccess['errorMessage'] . "  " . $dbAccess['errorCode'] . " MSISDN :: $msisdn";
            $this->svaLog->writeSystemLog($logDebug);
            die("IMPOSSIBLE DE SE CONNECTER A LA BASE DE DONNEES");
        } else {


            $sqlQuery = "INSERT 
                INTO billing_details_advanced_tab   
                VALUES (null,'$tarifId','$networkId','$price', '$smscId','','','','','','0' )";
           // echo $sqlQuery;
            $status = mysql_query($sqlQuery, $dbAccess['dbConnection']);
            $id = mysql_insert_id($dbAccess['dbConnection']);
            //$this->closeMysqlConnection($dbAccess['dbConnection']);
        }

        return $id;
    }
    
    
    
    public function DeleteTarifDetail($id) {

        $dbAccess = $this->getMysqlConnection();
        $totalUser = 0;

        if (!$dbAccess['dbConnection']) {
            $logDebug = " Echec DeleteTarifDetail()  " . $dbAccess['errorMessage'] . "  " . $dbAccess['errorCode'] . " MSISDN :: $msisdn";
            $this->svaLog->writeSystemLog($logDebug);
            die("IMPOSSIBLE DE SE CONNECTER A LA BASE DE DONNEES");
        } else {


            $sqlQuery = "delete from billing_details_advanced_tab where  item_id_n='$id'";
            
           // echo $sqlQuery;
            $queryResult = mysql_query($sqlQuery, $dbAccess['dbConnection']);
            // $totalUser = mysql_affected_rows($queryResult);
            $this->closeMysqlConnection($dbAccess['dbConnection']);
        }

        if ($queryResult)
            $totalUser = 1;
        return $totalUser;
    }
    
    
    
    
     public function getCountryCodeByNetworkId($networkId) {

        $dbAccess = $this->connectMysqlDatabase();

        if (!$dbAccess['dbConnection']) {
            $logDebug = " Echec getCountryCodeByNetworkId()  " . $dbAccess['errorMessage'] . "  " . $dbAccess['errorCode'] . " MSISDN :: $this->msisdn";
            $this->svaLog->writeSystemLog($logDebug);
            die("IMPOSSIBLE DE SE CONNECTER A LA BASE DE DONNEES");
        } else {


            $sqlQuery = "
                    
                         select  
                         distinct d.cc_code_v
                  FROM 
                       master_networks_tab b,
                       master_countries_tab c,
                       country_details_tab d
                  WHERE 
                    b.country_id_n = c.country_id_n
                  AND  d.country_id_n = c.country_id_n
                  AND b.network_id_n = $networkId

                ";
            // echo $sqlQuery;
            $sqlResult = mysql_query($sqlQuery, $dbAccess['dbConnection']);
            $data = mysql_fetch_array($sqlResult);
            $countryCode =  $data['cc_code_v'];
            $this->closeMysqlConnection($dbAccess['dbConnection']);
        }

        return $countryCode;
    }
    
    
    
    
    
     public function getMMGSenderFilterIdByUser($userId) {

        $dbAccess = $this->connectMysqlDatabase();

        if (!$dbAccess['dbConnection']) {
            $logDebug = " Echec getMMGSenderIdByUser()  " . $dbAccess['errorMessage'] . "  " . $dbAccess['errorCode'] . " MSISDN :: $this->msisdn";
            $this->svaLog->writeSystemLog($logDebug);
            die("IMPOSSIBLE DE SE CONNECTER A LA BASE DE DONNEES");
        } else {

            $sqlQuery = "
                    
                  select   sender_id_n
                        , creation_datetime_d
                        , owner_id_n
                        , sender_name_v
                        , status_n 
                 FROM mmg_senderid_tab
                 WHERE owner_id_n = $userId
                ";


            // echo $sqlQuery;
            $sqlResult = mysql_query($sqlQuery, $dbAccess['dbConnection']);

            $this->closeMysqlConnection($dbAccess['dbConnection']);
        }

        return $this->mysqlRessourceJson($sqlResult);
    }

    /////////////////   insertion des expediteur



    public function insertMMGSenderFilter($senderName, $destName) {

        $dbAccess = $this->connectMysqlDatabase();

        if (!$dbAccess['dbConnection']) {
            $logDebug = " Echec insertMMGSenderFilter()  " . $dbAccess['errorMessage'] . "  " . $dbAccess['errorCode'] . " MSISDN :: $this->msisdn";
            $this->svaLog->writeSystemLog($logDebug);
            die("IMPOSSIBLE DE SE CONNECTER A LA BASE DE DONNEES");
        } else {

            $sqlQuery = 'INSERT INTO mmg_sender_filters_tab VALUES("",now(),"' . $destName . '","' . $senderName . '","0")';


            // echo $sqlQuery;
            $sqlResult = mysql_query($sqlQuery, $dbAccess['dbConnection']) or die("error");

            $this->closeMysqlConnection($dbAccess['dbConnection']);
        }

        //return $this->mysqlRessourceJson($sqlResult);
    }

    public function updateMMGSenderFilter($id, $senderName, $destName) {

        $dbAccess = $this->connectMysqlDatabase();

        if (!$dbAccess['dbConnection']) {
            $logDebug = " Echec getMMGSenderIdByUser()  " . $dbAccess['errorMessage'] . "  " . $dbAccess['errorCode'] . " MSISDN :: $this->msisdn";
            $this->svaLog->writeSystemLog($logDebug);
            die("IMPOSSIBLE DE SE CONNECTER A LA BASE DE DONNEES");
        } else {

            $sqlQuery = 'UPDATE  mmg_sender_filters_tab SET sender_name_v="' . $senderName . '", owner_id_n="' . $destName . '" WHERE sender_id_n="' . $id . '" ';


            // echo $sqlQuery;
            $sqlResult = mysql_query($sqlQuery, $dbAccess['dbConnection']) or die("error");

            $this->closeMysqlConnection($dbAccess['dbConnection']);
        }

        //return $this->mysqlRessourceJson($sqlResult);
    }
    
    
    
    public function getAllFiters($option, $startLimit, $maxElement) {

        $dbAccess = $this->connectMysqlDatabase();

        if (!$dbAccess['dbConnection']) {
            $logDebug = " Echec getAllBillings()  " . $dbAccess['errorMessage'] . "  " . $dbAccess['errorCode'] . " MSISDN :: $this->msisdn";
            $this->svaLog->writeSystemLog($logDebug);
            die("IMPOSSIBLE DE SE CONNECTER A LA BASE DE DONNEES");
        } else {
            $limitQuery = "";
            if ($option)
                $limitQuery = "LIMIT $startLimit, $maxElement";

            $sqlQuery = " 
                      select * from master_users_tab, mmg_sender_filters_tab where user_id_n = owner_id_n 
					   $limitQuery          
                ";
            // echo $sqlQuery;
            $sqlResult = mysql_query($sqlQuery, $dbAccess['dbConnection']);

            $this->closeMysqlConnection($dbAccess['dbConnection']);
        }

        return $this->mysqlRessourceJson($sqlResult);
    }
    
    
     public function getSenderFilterById($bId) {

        $dbAccess = $this->connectMysqlDatabase();

        if (!$dbAccess['dbConnection']) {
            $logDebug = " Echec getBillingById()  " . $dbAccess['errorMessage'] . "  " . $dbAccess['errorCode'] . " MSISDN :: $this->msisdn";
            $this->svaLog->writeSystemLog($logDebug);
            die("IMPOSSIBLE DE SE CONNECTER A LA BASE DE DONNEES");
        } else {
            $limitQuery = "";


            $sqlQuery = " 
                      select * from master_users_tab, mmg_sender_filters_tab where sender_id_n ='$bId' and user_id_n = owner_id_n  
                ";
            // echo $sqlQuery;
            $sqlResult = mysql_query($sqlQuery, $dbAccess['dbConnection']);

            $this->closeMysqlConnection($dbAccess['dbConnection']);
        }

        return $this->mysqlRessourceJson($sqlResult);
    }
    
    

}

?>