<?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'";

            //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 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    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 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 getGroupDetailByGroupId($groupId, $dbAccess) {

        // $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 createCampaign($campaignName
    , $internalCode
    , $startDate
    , $startTime
    , $endDate
    , $endTime
    , $ownerId
    , $priority
    , $senderId
    , $message
    , $appId
    , $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','$appId','$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'
                ";
            }
            // 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
                from master_users_tab a,
                    master_profile_tab b,
                    master_status_tab c
                where a.profile_id_n = b.profile_id_n
                and a.status_n = c.status_n
                $limitSql
                ";


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

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

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

    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) {
        $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 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, status_n)
                VALUES (NULL,now(),'$username','$password','$profileId','$ownerId','$firstName','$lastName','$mobilePhone','$emailId','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) {

        $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'
                             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 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 = "";
            if ($option)
                $limitSql = "LIMIT $startLimit, $maxElement";
            if ($campaignId != -1)
                $campaignSql = "AND campaign_id_n = '$campaignId'";

            if ($profileId == 1 || $profileId == 3) {
                $sqlQuery = "  
                 select    campaign_id_n
                        , campaign_name_v
                        , owner_id_n 
                        , concat(start_date_d,' ',start_time_d) start_datetime_d
                        , concat(end_date_d,' ',end_time_d) end_datetime_d  
                        ,creation_date_d
                        ,char_field_2
                 FROM master_campaigns_tab
                 where status_n not in ('3','5')
                 and creation_date_d>='$startDate'
                 and creation_date_d<='$endDate'
                 $campaignSql
                 order by campaign_id_n desc
                 $limitSql
                ";
            } elseif ($profileId == 2) {

                $sqlQuery = "  
                    select    campaign_id_n
                        , campaign_name_v
                        , owner_id_n 
                        , concat(start_date_d,' ',start_time_d) start_datetime_d
                        , concat(end_date_d,' ',end_time_d) end_datetime_d  
                        ,creation_date_d
                        ,char_field_2
                 FROM master_campaigns_tab
                 where status_n not in ('3','5')
                 and creation_date_d>='$startDate'
                 and creation_date_d<='$endDate'
                 and owner_id_n='$userId'
                 $campaignSql
                 order by campaign_id_n desc
                 $limitSql

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

        return $this->mysqlRessourceJson($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);


        }
		
		 /***************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
								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; 

        }
		//************************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(){

            $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`";						  
										  
               
            // 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($network,$countryId,$icon) {

       $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,'$network','$countryId','$icon','','','','','','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 a.network_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,$icon) {
		  
        $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="`country_id_n`='$countryId',"; } 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   
								  
								   $networkSql
								   $countryIdSql
								   $iconSql
								  
                            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) {

       $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','','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 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 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 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);
    }
	////*******************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) {

       $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','','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  *  
					  from mmg_routes_tab
					  $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 billingDetailsUpdate()  " . $dbAccess['errorMessage'] . "  " . $dbAccess['errorCode'] . " MSISDN :: $msisdn";
            $this->svaLog->writeSystemLog($logDebug);
            die("IMPOSSIBLE DE SE CONNECTER A LA BASE DE DONNEES");
        } else { 
		 $routeSql="";
		 $userIdSql="";
		 $shortcodeIdSql="";
		 $descSql="";
	
       if(isset($route)&& strlen($route)>0) {$routeSql="`route_name_v`='$route',"; } else { $routeSql=""; }
	   if(isset($userId)&& strlen($userId)>0) {$userIdSql="`user_id_n`='$userId'"; } else { $userIdSql=""; }
	    if(isset($shortcodeId)&& strlen($shortcodeId)>0) {$shortcodeIdSql="`shortcode_id_n`='$shortcodeId',"; } else { $shortcodeIdSql=""; }
	   if(isset($desc)&& strlen($desc)>0) {$descSql="`route_desc_v`='$desc'"; } else { $descSql=""; }

            $sqlQuery = "UPDATE mmg_routes_tab
							 SET   
								  $routeSql
								  $descSql
								  $userIdSql
								  $shortcodeIdSql
								   
                            where `routing_id_n`=$rId
                          ";
          //  echo $sqlQuery;
            $queryResult = mysql_query($sqlQuery, $dbAccess['dbConnection']);

            $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);
    }
	
	////-----------------------------------------------//////
	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  *  
					  from master_shortcode_tab
					   $limitQuery       
                ";
            // echo $sqlQuery;
            $sqlResult = mysql_query($sqlQuery, $dbAccess['dbConnection']);

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

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

?>