<?php
defined('BASEPATH') OR exit('No direct script access allowed');
/**
 * Created by PhpStorm.
 * User: capp
 * Date: 29/05/2018
 * Time: 15:49
 */


class User_model extends CI_Model
{
    public function register_user($user){


        $this->db->insert('membres', $user);

    }

    public function login_user($id,$pass,$role){

        $table = null;
        $champ_id = null;
        $query = null;

        switch ($role) {
            case 1: //admin
                $this->db->select('*');
                $this->db->from('admin');
                $this->db->where("email",$id);
                $query=$this->db->get();
                break;
            case 2: //hyperviseur ou superviseur
                $query = $this->db->query("SELECT * FROM hyper_infos WHERE code_membre='".$id."'");
                if($query->num_rows()==0){
                    $query = $this->db->query("SELECT * FROM super_infos WHERE category='super' AND code_membre='".$id."'");
                }
                break;
        }

        if($query->num_rows()>0)
        {
            $salt = $query->row()->salt;
            $encrypted_password = $query->row()->encrypted_password;
            $hash = $this->checkhashSSHA($salt, $pass);
            if ($encrypted_password == $hash) {

                return $query->row();
            }else{
                return false;
            }
        }
        else{
            return false;
        }


    }

    public function email_check($email){

        $this->db->select('*');
        $this->db->from('membres');
        $this->db->where('email',$email);
        $query=$this->db->get();

        if($query->num_rows()>0){
            return false;
        }else{
            return true;
        }

    }

    public function geolocatedCount(){
        $count = $this->db->select('uid')
                          ->from("users")
                          ->where('category', 'geolocated')
                        ->count_all_results();
        return $count;
    }

    public function geolocatedCountByParrain($parrain){
        $query = $this->db->query("SELECT count(*) AS c
                                    FROM super_infos
                                    WHERE category='geolocated' AND latitude!=-1 AND longitude!=-1
                                    AND code_parrain='".$parrain."'");
        if($query->num_rows()>0){
            return $query->row()->c;
        }else{
            return 0;
        }
    }

    public function getAllpositions($network,$category,$parrain){

        if($network=='all' && $category=='geolocated' && $parrain=='all'){

            $this->db->select('latitude, longitude ,lastname');
            $this->db->from('users');
            $this->db->where('category',$category);
            $query=$this->db->get();
            return $query;

        }else{

        $this->db->select('latitude, longitude ,lastname');
        $this->db->from('super_infos');
        $this->db->where('category', $category);
        $this->db->where('code_parrain', $parrain);
        $this->db->where('network', $network);
        $query=$this->db->get();
            return $query;

        }
    }

    public function checkhashSSHA($salt, $password) {

        $hash = base64_encode(sha1($password . $salt, true) . $salt);

        return $hash;
    }

    public function countUser($category,$network,$codeParrain){

        if($codeParrain=='all'){
            $this->db->select('count(*) as c');
            $this->db->from('users');
            $this->db->where('category',$category);
            if($network!='all'){
            $this->db->where('network',$network);
            }
            $query=$this->db->get();
        }else{
            $this->db->select('count(*) as c');
            $this->db->from('super_infos');
            $this->db->where('category',$category);
            if($network!='all'){
                $this->db->where('network',$network);
            }
            $this->db->where('code_parrain',$codeParrain);
            $query=$this->db->get();
        }

        if($query->num_rows()>0){
            return $query->row()->c;
        }else{
            return 0;
        }
    }

    public function countUserGeoForHyp($codeHyp){

        $query = $this->db->query("SELECT count(*) AS c
                                    FROM super_infos
                                    WHERE category='geolocated' AND latitude!=-1 AND longitude!=-1
                                    AND code_parrain IN (SELECT code_membre 
                                                          FROM super_infos 
                                                          WHERE category='super' 
                                                          AND code_parrain='".$codeHyp."')");
        if($query->num_rows()>0){
            return $query->row()->c;
        }else{
            return 0;
        }
    }

    public function getAllUserGeoForHyp($codeHyp){

        $query = $this->db->query("SELECT *
                                    FROM super_infos
                                    WHERE category='geolocated' 
                                    AND code_parrain IN (SELECT code_membre 
                                                          FROM super_infos 
                                                          WHERE category='super' 
                                                          AND code_parrain='".$codeHyp."')");
        if($query->num_rows()>0){
            return $query;
        }else{
            return false;
        }
    }

    public function getVilleByUserGeo($network,$country){

        $this->db->distinct('ville');
        $this->db->select('town AS ville','town_id AS id');
        $this->db->from('super_infos');
        $this->db->where('category','geolocated');
        $this->db->where('network',$network);
        $this->db->where('country',$country);
        $query=$this->db->get();

            return $query;

    }

    public function getUsersGeolocatedByVilleForHyp($ville,$codeHyp){

        $query = $this->db->query("SELECT child.adresse AS adresse,child.category AS childcat,child.lastname AS lastname,child.solde AS solde,
                                               child.latitude AS latitude, child.longitude AS longitude,
                                               child.etat AS active, child.code_parrain AS code_parrain,child.code_membre AS member_code,
                                               child.network AS network, child.phone AS phone, child.email AS email,dad.adresse AS supadresse,
                                               dad.lastname AS suplastname, dad.code_membre AS sup_code_membre,dad.code_parrain AS sup_code_parrain,
                                               dad.network AS sup_network, dad.category AS dadcat
                                    FROM super_infos AS child 
                                    INNER JOIN super_infos AS dad
                                            ON child.code_parrain=dad.code_membre
                                    WHERE child.category='geolocated' AND dad.category='super' AND dad.code_parrain='".$codeHyp."' AND child.town='".$ville."'
                                  LIMIT 100");

        if($query->num_rows()>0){
            return $query;
        }else{
            return false;
        }
    }

    public function getUsersGeolocatedPositionByVilleForHyp($ville,$codeHyp){

        $query = $this->db->query("SELECT child.lastname AS lastname,
                                               child.latitude AS latitude, child.longitude AS longitude
                                    FROM super_infos AS child 
                                    INNER JOIN super_infos AS dad
                                            ON child.code_parrain=dad.code_membre
                                    WHERE child.category='geolocated' AND dad.category='super' AND dad.code_parrain='".$codeHyp."' AND child.town='".$ville."'");

        if($query->num_rows()>0){
            return $query;
        }else{
            return false;
        }
    }

    public function getDemandes($code_membre){

        if($code_membre=='all'){
            $query = $this->db->query("SELECT *,
                                           (SELECT TIMESTAMPDIFF(SECOND,dateAjout,dateModif)
                                            FROM demande_credit AS req_im
                                            WHERE req_im.id=d.id) AS temps
                                    FROM demande_credit AS d
                                    ORDER BY dateAjout");
        }else{
            $query = $this->db->query("SELECT *
                                    FROM info_demandeCredits AS d
                                    WHERE d.codeParrain='".$code_membre."'
                                    ORDER BY dateAjout");
        }

        if($query->num_rows()>0){
            return $query;
        }else{
            return false;
        }

    }

    public function getDemandesByVille($code_membre,$ville){

        if($code_membre=='all'){
            $query = $this->db->query("SELECT *,
                                           (SELECT TIMESTAMPDIFF(SECOND,d.date_creation,d.date_modification)
                                            FROM demandeCredits AS req_im
                                            WHERE req_im.id=d.id) AS temps
                                    FROM demandeCredits AS d
                                    WHERE d.ville='".$ville."'
                                    ORDER BY date_creation");
        }else{
            $query = $this->db->query("SELECT *
                                    FROM info_demandeCredits AS d
                                    WHERE d.codeParrain='".$code_membre."' AND d.ville='".$ville."'
                                    ORDER BY dateAjout");
        }

        if($query->num_rows()>0){
            return $query;
        }else{
            return false;
        }

    }

    public function getDemandesForSuper($member_code){

        $query = $this->db->query("SELECT *
                                    FROM info_demandeCredits AS d
                                    WHERE d.codeParrain='".$member_code."'
                                    ORDER BY dateAjout");

        if($query->num_rows()>0){
            return $query;
        }else{
            return false;
        }

    }

    public function getDemandesHyperForSuper($member_code){

        $query = $this->db->query("SELECT *
                                    FROM info_demandeCredits AS d
                                    WHERE d.codeMembre='".$member_code."'
                                    ORDER BY dateAjout");

        if($query->num_rows()>0){
            return $query;
        }else{
            return false;
        }

    }

    public function getDemandesAdForSuper($member_code){

        $query = $this->db->query("SELECT *,
                                           (SELECT TIMESTAMPDIFF(SECOND,date_creation,date_modified)
                                            FROM demandeAdhesion AS req_im
                                            WHERE req_im.id=d.id_demande) AS temps
                                    FROM info_demandesAdhesion AS d
                                    WHERE d.code_membre='".$member_code."'
                                    ORDER BY date_creation");

        if($query->num_rows()>0){
            return $query;
        }else{
            return false;
        }

    }

    public function getSupervisorsForHyp($codeHyp){

        $query = $this->db->query("SELECT child.adresse AS adresse,child.category AS childcat,child.lastname AS lastname,child.solde AS solde,
                                               child.etat AS active, child.code_parrain AS code_parrain,child.code_membre AS member_code,
                                               child.network AS network, child.phone AS phone, child.email AS email,dad.adresse AS supadresse,
                                               dad.lastname AS suplastname, dad.code_membre AS sup_code_membre,dad.code_membre AS sup_code_parrain,
                                               dad.network AS sup_network, dad.category AS dadcat
                                    FROM super_infos AS child 
                                    INNER JOIN hyper_infos AS dad
                                            ON child.code_parrain='".$codeHyp."'
                                    WHERE child.category='super' AND dad.category='hyper' AND dad.code_membre='".$codeHyp."'
                                  ");

        if($query->num_rows()>0){
            return $query;
        }else{
            return false;
        }
    }

    public function getUsersGeolocatedForSup($codeSup){

        $query = $this->db->query("SELECT child.adresse AS adresse,child.category AS childcat,child.lastname AS lastname,child.solde AS solde,
                                               child.etat AS active, child.code_parrain AS code_parrain,child.code_membre AS member_code,
                                               child.network AS network, child.phone AS phone, child.email AS email,dad.adresse AS supadresse,
                                               dad.lastname AS suplastname, dad.code_membre AS sup_code_membre,dad.code_parrain AS sup_code_parrain,
                                               dad.network AS sup_network, dad.category AS dadcat, child.date_created
                                    FROM super_infos AS child 
                                    INNER JOIN super_infos AS dad
                                            ON child.code_parrain='".$codeSup."'
                                    WHERE child.category='geolocated' AND dad.category='super' AND dad.code_membre='".$codeSup."' LIMIT 100
                                  ");

        if($query->num_rows()>0){
            return $query;
        }else{
            return false;
        }
    }

    public function getAllGeolocatedUsersForHyp($codeHyp){

        $query = $this->db->query("SELECT *
                                    FROM super_infos
                                    WHERE category='geolocated' AND latitude!=-1 AND longitude!=-1 
                                    AND code_parrain IN (SELECT code_membre 
                                                          FROM super_infos 
                                                          WHERE category='super' 
                                                          AND code_parrain='".$codeHyp."')
                                  ");

        if($query->num_rows()>0){
            return $query;
        }else{
            return false;
        }
    }

    public function getCountDemande($codeParrain){

        if($codeParrain=='all'){
            $this->db->select('count(*) as c');
            $this->db->from('demandeCredits');
            $query=$this->db->get();
        }else{
            $this->db->select('count(*) as c');
            $this->db->from('info_demandeCredits');
            $this->db->where('codeParrain',$codeParrain);
            $query=$this->db->get();
        }

        if($query->num_rows()>0){
            return $query->row()->c;
        }else{
            return 0;
        }
    }

    public function getCountDemandeAd($codeParrain){

        if($codeParrain=='all'){
            $this->db->select('count(*) as c');
            $this->db->from('demande_superviseur');
            $query=$this->db->get();
        }else{
            $this->db->select('count(*) as c');
            $this->db->from('demande_superviseur');
            $this->db->where('code',$codeParrain);
            $query=$this->db->get();
        }

        if($query->num_rows()>0){
            return $query->row()->c;
        }else{
            return 0;
        }
    }

    public function getCountDemandeByStatut($codeParrain,$statut){

        if($codeParrain=='all'){
            $this->db->select('count(*) as c');
            $this->db->from('demandeCredits');
            $this->db->where('statut',$statut);
            $query=$this->db->get();
        }else{
            $this->db->select('count(*) as c');
            $this->db->from('info_demandeCredits');
            $this->db->where('codeParrain',$codeParrain);
            $this->db->where('statut',$statut);
            $query=$this->db->get();
        }

        if($query->num_rows()>0){
            return $query->row()->c;
        }else{
            return 0;
        }
    }

    public function getCountDemandeAdByStatut($codeParrain,$statut){

        if($codeParrain=='all'){
            $this->db->select('count(*) as c');
            $this->db->from('demande_superviseur');
            $this->db->where('Statut',$statut);
            $query=$this->db->get();
        }else{
            $this->db->select('count(*) as c');
            $this->db->from('demande_credit');
            $this->db->where('code',$codeParrain);
            $this->db->where('Statut',$statut);
            $query=$this->db->get();
        }

        if($query->num_rows()>0){
            return $query->row()->c;
        }else{
            return 0;
        }
    }

    public function getRangeASCDemandes($codeParrain){

        if($codeParrain=='all'){
            $query = $this->db->query("SELECT d.codeMembre AS emetteur, d.codeParrain AS superviseur, d.montant AS montant,d.dateAjout AS dateA,d.dateModif AS dateM,
                                               (SELECT TIMESTAMPDIFF(SECOND,dateAjout,dateModif)
                                                FROM demande_credit AS req_im
                                                WHERE req_im.statut=1 AND req_im.id=d.id) AS temps
                                        FROM demande_credit AS d
                                        WHERE statut=1
                                        ORDER BY temps
                                        LIMIT 5");
        }else{
            $query = $this->db->query("SELECT d.codeMembre AS emetteur, d.codeParrain AS superviseur, d.montant AS montant,d.dateAjout AS dateA,d.dateModif AS dateM,d.temps AS temps
                                        FROM info_demandeCredits AS d
                                        WHERE statut=1 AND d.codeParrain='".$codeParrain."'
                                        ORDER BY temps
                                        LIMIT 5");
        }

        if($query->num_rows()>0){
            return $query;
        }else{
            return false;
        }

    }

    public function getRangeASCDemandesOut($codeParrain){

            $query = $this->db->query("SELECT d.codeMembre AS emetteur, d.codeParrain AS superviseur, d.montant AS montant,d.dateAjout AS dateA,d.dateModif AS dateM,d.temps AS temps
                                        FROM info_demandeCredits AS d
                                        WHERE statut=1 AND d.codeMembre='".$codeParrain."'
                                        ORDER BY temps
                                        LIMIT 5");

        if($query->num_rows()>0){
            return $query;
        }else{
            return false;
        }

    }

    public function getRangeASCDemandesByNetwork($codeParrain,$ville,$super){

        if($codeParrain=='all'){
            $query = $this->db->query("SELECT d.codeMembre AS emetteur, d.ref_operateur AS ref_operateur, d.montant AS montant, d.montant_allowed AS montant_allowed,d.dateAjout AS dateA,d.dateModif AS dateM,
                                               (SELECT TIMESTAMPDIFF(SECOND,dateAjout,dateModif)
                                                FROM demande_credit AS req_im
                                                WHERE req_im.statut=1 AND req_im.id=d.id) AS temps
                                        FROM demande_credit AS d
                                        WHERE statut=1 AND d.reseau='".$ville."'
                                        ORDER BY temps
                                        LIMIT 5");
        }else{
            $query = $this->db->query("SELECT d.codeMembre AS emetteur, d.ref_operateur AS ref_operateur, d.montant AS montant, d.montant_allowed AS montant_allowed,d.dateAjout AS dateA,d.dateModif AS dateM,d.temps AS temps
                                        FROM info_demandeCredits AS d
                                        WHERE statut=1 AND d.codeParrain='".$codeParrain."' AND d.codeMembre='".$super."'
                                        ORDER BY temps
                                        LIMIT 5");
        }

        if($query->num_rows()>0){
            return $query;
        }else{
            return false;
        }

    }

    public function getRangeASCDemandesAd($codeParrain){

        if($codeParrain=='all'){
            $query = $this->db->query("SELECT d.phone AS emetteur, d.code_membre AS superviseur, d.categorie AS montant,d.date_creation AS dateA,d.date_modified AS dateM,
                                               (SELECT TIMESTAMPDIFF(SECOND,dateAjout,dateModif)
                                                FROM demande_superviseur AS req_im
                                                WHERE req_im.Statut=1 AND req_im.id=d.id) AS temps
                                        FROM info_demandesadhesion AS d
                                        WHERE Statut=1
                                        ORDER BY temps
                                        LIMIT 5");
        }else{
            $query = $this->db->query("SELECT d.phone AS emetteur, d.code_membre AS superviseur, d.date_creation AS dateA,d.date_modified AS dateM,
                                               (SELECT TIMESTAMPDIFF(SECOND,date_creation,date_modified)
                                                FROM demandeAdhesion AS req_im
                                                WHERE req_im.etat=1 
                                                AND req_im.id=d.id_demande) AS temps
                                        FROM info_demandesAdhesion AS d
                                        WHERE etat=1 AND d.code_membre='".$codeParrain."'
                                        ORDER BY temps
                                        LIMIT 5");
        }

        if($query->num_rows()>0){
            return $query;
        }else{
            return false;
        }

    }

    public function getRangeDESCDemandes($codeParrain){

        if($codeParrain=='all'){
            $query = $this->db->query("SELECT d.codeMembre AS emetteur, d.codeParrain AS superviseur, d.montant AS montant,d.dateAjout AS dateA,d.dateModif AS dateM,
                                               (SELECT TIMESTAMPDIFF(SECOND,dateAjout,dateModif)
                                                FROM demande_credit AS req_im
                                                WHERE req_im.statut=1 AND req_im.id=d.id) AS temps
                                        FROM demande_credit AS d
                                        WHERE statut=1
                                        ORDER BY temps DESC 
                                        LIMIT 5");
        }else{
            $query = $this->db->query("SELECT d.codeMembre AS emetteur, d.codeParrain AS superviseur, d.montant AS montant,d.dateAjout AS dateA,d.dateModif AS dateM,d.temps AS temps
                                        FROM info_demandeCredits AS d
                                        WHERE statut=1 AND d.codeParrain='".$codeParrain."'
                                        ORDER BY temps DESC
                                        LIMIT 5");
        }

        if($query->num_rows()>0){
            return $query;
        }else{
            return false;
        }

    }

    public function getRangeDESCDemandesOut($codeParrain){

            $query = $this->db->query("SELECT d.codeMembre AS emetteur, d.codeParrain AS superviseur, d.montant AS montant,d.dateAjout AS dateA,d.dateModif AS dateM,d.temps AS temps
                                        FROM info_demandeCredits AS d
                                        WHERE statut=1 AND d.codeMembre='".$codeParrain."'
                                        ORDER BY temps DESC
                                        LIMIT 5");

        if($query->num_rows()>0){
            return $query;
        }else{
            return false;
        }

    }

    public function getRangeDESCDemandesBynetwork($codeParrain,$ville,$super){

        if($codeParrain=='all'){
            $query = $this->db->query("SELECT d.codeMembre AS emetteur, d.ref_operateur AS ref_operateur, d.montant AS montant, d.montant_allowed AS montant_allowed,d.dateAjout AS dateA,d.dateModif AS dateM,
                                               (SELECT TIMESTAMPDIFF(SECOND,dateAjout,dateModif)
                                                FROM demande_credit AS req_im
                                                WHERE req_im.statut=1 AND req_im.id=d.id) AS temps
                                        FROM demande_credit AS d
                                        WHERE statut=1 AND d.reseau='".$ville."'
                                        ORDER BY temps DESC 
                                        LIMIT 5");
        }else{
            $query = $this->db->query("SELECT d.codeMembre AS emetteur, d.ref_operateur AS ref_operateur, d.montant AS montant, d.montant_allowed AS montant_allowed,d.dateAjout AS dateA,d.dateModif AS dateM,d.temps AS temps
                                        FROM info_demandeCredits AS d
                                        WHERE statut=1 AND d.codeParrain='".$codeParrain."' AND d.codeMembre='".$super."'
                                        ORDER BY temps DESC
                                        LIMIT 5");
        }

        if($query->num_rows()>0){
            return $query;
        }else{
            return false;
        }

    }

    public function getRangeDESCDemandesAd($codeParrain){

        if($codeParrain=='all'){
            $query = $this->db->query("SELECT d.phone AS emetteur, d.code AS superviseur, d.categorie AS montant,d.dateAjout AS dateA,d.dateModif AS dateM,
                                               (SELECT TIMESTAMPDIFF(SECOND,dateAjout,dateModif)
                                                FROM demande_superviseur AS req_im
                                                WHERE req_im.Statut=1 AND req_im.id=d.id) AS temps
                                        FROM demande_superviseur AS d
                                        WHERE Statut=1
                                        ORDER BY temps DESC 
                                        LIMIT 5");
        }else{
            $query = $this->db->query("SELECT d.phone AS emetteur, d.code_membre AS superviseur, d.date_creation AS dateA,d.date_modified AS dateM,
                                               (SELECT TIMESTAMPDIFF(SECOND,date_creation,date_modified)
                                                FROM demandeAdhesion AS req_im
                                                WHERE req_im.etat=1 
                                                AND req_im.id=d.id_demande) AS temps
                                        FROM info_demandesAdhesion AS d
                                        WHERE etat=1 AND d.code_membre='".$codeParrain."'
                                        ORDER BY temps DESC
                                        LIMIT 5");
        }

        if($query->num_rows()>0){
            return $query;
        }else{
            return false;
        }

    }

    public function getTempsMoyen($codeParrain){

        if($codeParrain=='all'){
            $query = $this->db->query("SELECT AVG(TIMESTAMPDIFF(SECOND,dateAjout,dateModif)) AS temps FROM demande_credit WHERE statut=1");
        }else{
            $query = $this->db->query("SELECT AVG(temps) AS temps FROM info_demandeCredits WHERE statut=1 AND codeParrain='".$codeParrain."'");
        }

        if($query->num_rows()>0){
            return $query->row()->temps;
        }else{
            return false;
        }

    }

    public function getTempsMoyenDemandeSup($codeParrain){

        if($codeParrain=='all'){
            $query = $this->db->query("SELECT AVG(TIMESTAMPDIFF(SECOND,dateAjout,dateModif)) AS temps FROM demande_credit WHERE statut=1");
        }else{
            $query = $this->db->query("SELECT AVG(temps) AS temps FROM info_demandeCredits WHERE statut=1 AND codeMembre='".$codeParrain."'");
        }

        if($query->num_rows()>0){
            return $query->row()->temps;
        }else{
            return false;
        }

    }

    public function getTempsMoyenByVille($codeParrain,$super){

        if($codeParrain=='all'){
            $query = $this->db->query("SELECT AVG(TIMESTAMPDIFF(SECOND,dateAjout,dateModif)) AS temps FROM demande_credit WHERE statut=1 AND reseau='".$network."' ");
        }else{
            $query = $this->db->query("SELECT AVG(temps) AS temps FROM info_demandeCredits WHERE statut='1' AND codeParrain='".$codeParrain."' AND codeMembre='".$super."' ");
        }

        if($query->num_rows()>0){
            return $query->row()->temps;
        }else{
            return false;
        }

    }

    public function getTempsMoyenAd($codeParrain){

        if($codeParrain=='all'){
            $query = $this->db->query("SELECT AVG(TIMESTAMPDIFF(SECOND,dateAjout,dateModif)) AS temps FROM demande_superviseur WHERE statut=1");
        }else{
            $query = $this->db->query("SELECT AVG(TIMESTAMPDIFF(SECOND,date_creation,date_modified)) AS temps FROM info_demandesAdhesion WHERE etat=1 AND code_membre='".$codeParrain."'");
        }

        if($query->num_rows()>0){
            return $query->row()->temps;
        }else{
            return false;
        }

    }

    public function getNetworkByHyp($codeHyp){
        $this->db->distinct('network');
        $this->db->select('network');
        $this->db->from('super_infos');
        $this->db->where('category','super');
        $this->db->where('code_parrain',$codeHyp);
        $query=$this->db->get();

        return $query;
    }

    /*public function getAllCountries(){

        $query=$this->db->query("SELECT countries.id,countries.name,countries.code_country,countries.code_dial FROM countries INNER JOIN towns t on countries.id = t.country_id and t.status=1 GROUP BY countries.id");
        return $query;
    }*/

    public function getAllGameCountries(){

        $query=$this->db->query("SELECT countries.id,countries.name,configGame.etat 
                                    FROM countries 
                                    INNER JOIN towns t on countries.id = t.country_id and t.status=1 
                                    LEFT JOIN configGame ON countries.id=configGame.id_pays
                                    WHERE countries.id IN (SELECT country_id FROM networks INNER JOIN networks_agents ON networks.id=networks_agents.network_id GROUP BY networks.country_id)    
                                    GROUP BY countries.id");
        return $query;
    }

    public function getAllCountries(){

        $query=$this->db->query("SELECT countries.id,countries.name,countries.code_country,countries.code_dial 
                                    FROM countries 
                                    INNER JOIN towns t on countries.id = t.country_id and t.status=1 
                                    WHERE countries.id IN (SELECT country_id FROM networks INNER JOIN networks_agents ON networks.id=networks_agents.network_id GROUP BY networks.country_id)    
                                    GROUP BY countries.id");
        return $query;
    }

    public function getAllHyper($pays){

        $query=$this->db->query("SELECT * FROM hyper_infos WHERE country_id='".$pays."'");
        return $query;

    }

    public function getVilleNetworkByHyper($codeHyp){
        $query = $this->db->query("SELECT town,town_id FROM super_infos WHERE code_parrain='".$codeHyp."' GROUP BY town");
        return $query;
    }

    //----------------------------ADMIN--------------------------------------//

    public function adminGetDemandes($pays,$codeHyper,$ville){

        $query = $this->db->query("SELECT * FROM info_demandeCredits
                                    WHERE ville='".$ville."'
                                          AND (codeParrain='".$codeHyper."'
                                          OR codeParrain IN (SELECT code_membre FROM super_infos WHERE code_parrain='".$codeHyper."' AND category ='super'))
                                    ORDER BY dateAjout");
        if($query->num_rows()>0){
            return $query;
        }else{
            return false;
        }
    }

    public function adminGetTempsMoyen($pays,$codeHyper,$ville){

        $query = $this->db->query("SELECT AVG(TIMESTAMPDIFF(SECOND,dateAjout,dateModif)) AS temps
                                    FROM info_demandeCredits
                                    WHERE pays='".$pays."'
                                          AND statut=1
                                          AND ville='".$ville."'
                                          AND (codeParrain='".$codeHyper."'
                                          OR codeParrain IN (SELECT code_membre FROM super_infos WHERE code_parrain='".$codeHyper."' AND category ='super'))");

        if($query->num_rows()>0){
            return $query->row()->temps;
        }else{
            return false;
        }

    }

    public function adminGetRangeASCDemandes($pays,$codeHyper,$ville){

        $query = $this->db->query("SELECT d.codeMembre AS emetteur, d.codeParrain AS superviseur, d.montant AS montant,d.dateAjout AS dateA,d.dateModif AS dateM,d.temps AS temps
                            FROM info_demandeCredits AS d
                            WHERE pays='".$pays."'
                                  AND statut=1
                                  AND ville='".$ville."'
                                  AND (codeParrain='".$codeHyper."'
                                  OR codeParrain IN (SELECT code_membre FROM super_infos WHERE code_parrain='".$codeHyper."' AND category ='super'))
                            ORDER BY temps
                            LIMIT 5");

        if($query->num_rows()>0){
            return $query;
        }else{
            return false;
        }

    }

    public function adminGetRangeDESCDemandes($pays,$codeHyper,$ville){

        $query = $this->db->query("SELECT d.codeMembre AS emetteur, d.codeParrain AS superviseur, d.montant AS montant,d.dateAjout AS dateA,d.dateModif AS dateM,d.temps AS temps
                            FROM info_demandeCredits AS d
                            WHERE pays='".$pays."'
                                  AND statut=1
                                  AND ville='".$ville."'
                                  AND (codeParrain='".$codeHyper."'
                                  OR codeParrain IN (SELECT code_membre FROM super_infos WHERE code_parrain='".$codeHyper."' AND category ='super'))
                            ORDER BY temps DESC
                            LIMIT 5");

        if($query->num_rows()>0){
            return $query;
        }else{
            return false;
        }

    }

    public function adminGetAllpositions($pays,$codeHyper,$ville){

        $query = $this->db->query("SELECT child.latitude AS latitude,child.longitude AS longitude,child.lastname AS lastname
                                FROM super_infos AS child
                                  INNER JOIN super_infos AS dad
                                    ON child.code_parrain=dad.code_membre
                                WHERE child.country_id='".$pays."'
                                      AND child.code_parrain='".$codeHyper."'
                                      OR child.code_parrain IN (SELECT code_membre FROM super_infos WHERE code_parrain='".$codeHyper."' AND category ='super')
                                      AND child.town_id='".$ville."'
                                      ");
        if($query->num_rows()>0){
            return $query;
        }else{
            return false;
        }
    }

    public function getAllSimpleUser($pays,$codeHyper){

        $query = $this->db->query("SELECT * 
                                        FROM user_infos
                                        WHERE network_id IN (SELECT super_infos.network_id
                                                          FROM super_infos
                                                                WHERE super_infos.country_id ='".$pays."'
                                                                AND super_infos.code_parrain ='".$codeHyper."'
                                                          GROUP BY super_infos.network_id)");

        if($query->num_rows()>0){
            return $query;
        }else{
            return false;
        }
    }

    public function getGeneratedCodes($codeHyper){

        $query = $this->db->query("SELECT * FROM codeGenerer WHERE code_parrain='".$codeHyper."' AND code_parrain!=code_membre");

        if($query->num_rows()>0){
            return $query;
        }else{
            return false;
        }

    }

    public function getGeneratedHyperCodes(){

        $query = $this->db->query("SELECT * FROM codeGenerer WHERE category='hyper'");

        if($query->num_rows()>0){
            return $query;
        }else{
            return false;
        }

    }

    public function getDemandeSupByHyperviseur($codeHyp){

        $query=$this->db->query("SELECT *
                                    FROM info_demandesAdhesion
                                    WHERE code_membre='".$codeHyp."'");

        if($query->num_rows()>0){
            return $query;
        }else{
            return false;
        }
    }

    public function getCodesByHyperviseur($codeHyp){

        $query=$this->db->query("SELECT *
                                    FROM codeGenerer
                                    WHERE code_parrain='".$codeHyp."' OR code_parrain IN (SELECT code_membre
                                                                        FROM super_infos
                                                                        WHERE category='super' AND code_parrain='".$codeHyp."')
                                ");

        if($query->num_rows()>0){
            return $query;
        }else{
            return false;
        }
    }

    public function getCoutForAdmin($pays,$hyper,$ville,$type){

        switch ($type){
            case'simple_users':
                $query=$this->db->query("SELECT count(*) AS c
                                        FROM users
                                        WHERE network_id IN (SELECT super_infos.network_id
                                                          FROM super_infos
                                                                WHERE super_infos.country_id ='".$pays."'
                                                                AND super_infos.code_parrain ='".$hyper."'
                                                          GROUP BY super_infos.network_id)");
                break;
            case 'geolocated':
                $query=$this->db->query("SELECT count(*) AS c
                                            FROM super_infos
                                            WHERE latitude!=-1 AND longitude!=-1 AND category='geolocated' and town_id='".$ville."' and code_parrain IN (SELECT super_infos.code_membre
                                                                                              FROM super_infos
                                                                                              WHERE super_infos.category='super' 
                                                                                                    AND super_infos.code_parrain='".$hyper."' 
                                                                                              GROUP BY super_infos.code_membre)");
                break;
            case 'validated_code':
                $query=$this->db->query("SELECT count(*) AS c
                                    FROM codeGenerer
                                    WHERE code_membre='".$hyper."' OR code_membre IN (SELECT code_membre
                                                                        FROM super_infos
                                                                        WHERE category='super' AND code_parrain='".$hyper."')
                                ");
                break;
            case 'generated_code':
                $query = $this->db->query("SELECT count(*) AS c FROM codeGenerer WHERE code_parrain='".$hyper."' AND code_parrain!=code_membre");
                break;
            default:
                break;
        }

        if($query->num_rows()>0){
            return $query->row()->c;
        }else{
            return 0;
        }
    }

    public function getGeolocatedUsersForAdmin($pays,$hyper,$ville){

        $query = $this->db->query("SELECT child.agent_id AS id,child.adresse AS adresse,child.category AS childcat,child.lastname AS lastname,
                                           child.etat AS active, child.code_parrain AS code_parrain,child.code_membre AS member_code,
                                           child.network AS network, child.phone AS phone, child.email AS email,dad.adresse AS supadresse,
                                           dad.lastname AS suplastname, dad.code_membre AS sup_code_membre,dad.code_parrain AS sup_code_parrain,
                                           dad.network AS sup_network, dad.category AS dadcat
                                    FROM super_infos AS child
                                      INNER JOIN super_infos AS dad
                                        ON child.code_parrain=dad.code_membre
                                    WHERE child.country_id='".$pays."'
                                          AND (child.code_parrain='".$hyper."'
                                          OR child.code_parrain IN (SELECT code_membre FROM super_infos WHERE code_parrain='".$hyper."' AND category ='super'))
                                          AND child.town_id='".$ville."'
                                          AND child.latitude!=-1 AND child.longitude!=-1
                                          ORDER BY child.agent_id DESC LIMIT 100");

        if($query->num_rows()>0){
            return $query;
        }else{
            return false;
        }
    }

    public function getCoutGeolocatedUsersForAdmin($pays,$hyper,$ville){

        $query = $this->db->query("SELECT COUNT(child.agent_id) AS c
                                    FROM super_infos AS child
                                      INNER JOIN super_infos AS dad
                                        ON child.code_parrain=dad.code_membre
                                    WHERE child.country_id='".$pays."'
                                          AND (child.code_parrain='".$hyper."'
                                          OR child.code_parrain IN (SELECT code_membre FROM super_infos WHERE code_parrain='".$hyper."' AND category ='super'))
                                          AND child.town_id='".$ville."'
                                          AND child.latitude!=-1 AND child.longitude!=-1
                                          ");

        if($query->num_rows()>0){
            return $query->row()->c;
        }else{
            return false;
        }
    }

    public function getCoutGeolocatedUsersForHyper($hyper,$ville){

        $query = $this->db->query("SELECT COUNT(child.agent_id) AS c
                                    FROM super_infos AS child
                                      INNER JOIN super_infos AS dad
                                        ON child.code_parrain=dad.code_membre
                                    WHERE child.code_parrain IN (SELECT code_membre FROM super_infos WHERE code_parrain='".$hyper."' AND category ='super')
                                          AND child.town='".$ville."'
                                          AND child.latitude!=-1 AND child.longitude!=-1
                                          ");

        if($query->num_rows()>0){
            return $query->row()->c;
        }else{
            return false;
        }
    }

    public function getCoutSuperForHyper($hyper){

        $query = $this->db->query("SELECT COUNT(child.agent_id) AS c
                                    FROM super_infos AS child
                                      INNER JOIN super_infos AS dad
                                        ON child.code_parrain=dad.code_membre
                                    WHERE child.code_parrain='".$hyper."' AND child.category ='super'");

        if($query->num_rows()>0){
            return $query->row()->c;
        }else{
            return false;
        }
    }

    public function getCoutGeolocatedUsersForSuper($super){

        $query = $this->db->query("SELECT COUNT(child.agent_id) AS c
                                    FROM super_infos AS child
                                      INNER JOIN super_infos AS dad
                                        ON child.code_parrain=dad.code_membre
                                    WHERE child.code_parrain='".$super."' AND child.latitude!=-1 AND child.longitude!=-1
                                          ");

        if($query->num_rows()>0){
            return $query->row()->c;
        }else{
            return false;
        }
    }

    public function getOnlyGeolocatedUsersForAdmin($pays,$hyper,$ville){

        $query = $this->db->query("SELECT child.adresse AS adresse,child.category AS childcat,child.lastname AS lastname,
                                           child.etat AS active, child.code_parrain AS code_parrain,child.code_membre AS member_code,
                                           child.network AS network, child.phone AS phone, child.email AS email,dad.adresse AS supadresse,
                                           dad.lastname AS suplastname, dad.code_membre AS sup_code_membre,dad.code_parrain AS sup_code_parrain,
                                           dad.network AS sup_network, dad.category AS dadcat
                                    FROM super_infos AS child
                                      INNER JOIN super_infos AS dad
                                        ON child.code_parrain=dad.code_membre
                                    WHERE child.country_id='".$pays."'
                                          AND child.code_parrain IN (SELECT code_membre FROM super_infos WHERE code_parrain='".$hyper."' AND category ='super')
                                          AND child.town_id='".$ville."'
                                          AND child.category='geolocated'
                                          ");

        if($query->num_rows()>0){
            return $query;
        }else{
            return false;
        }
    }

    public function getUsersSimpleForSup($network,$member_code){
        $query = $this->db->query("SELECT *
                                    FROM users_simple
                                    WHERE network
                                          IN (SELECT network
                                              FROM users
                                              WHERE users.category='super'
                                                    AND users.member_code='".$member_code."'
                                                    AND users.network='".$network."')
                                    ");
        if($query->num_rows()>0){
            return $query;
        }else{
            return false;
        }
    }

    public function getAllVilles(){
        $query = $this->db->query("SELECT towns.id, towns.name, towns.status ,c.name AS country,c.id AS country_id
                                    FROM towns INNER JOIN countries c on towns.country_id = c.id");
        if($query->num_rows()>0){
            return $query;
        }else{
            return false;
        }
    }

    public function getAllPays(){
        $query = $this->db->query("SELECT * FROM countries ORDER BY name");
        if($query->num_rows()>0){
            return $query;
        }else{
            return false;
        }
    }

    public function changeVilleStatut($id_ville,$new_statut){

        $query = $this->db->query("UPDATE towns SET towns.status = ".$new_statut." WHERE towns.id ='".$id_ville."'");

            return $query;
    }

    public function updateVille($id_country,$id_town,$town){
        $query = $this->db->query("UPDATE towns SET towns.country_id = ".$id_country.",towns.name='".$town."' WHERE towns.id ='".$id_town."'");
        return $query;
    }

    public function getAllNetworksNames(){
        $query = $this->db->query("SELECT * FROM networks WHERE country_id IS NULL ORDER BY name");
        if($query->num_rows()>0){
            return $query;
        }else{
            return false;
        }
    }

    public function getAllAssignedNetworks(){
        $query = $this->db->query("SELECT networks.name AS network,networks.status AS status,networks.id,countries.name AS country,networks.country_id FROM `networks` INNER JOIN countries ON networks.country_id=countries.id");
        if($query->num_rows()>0){
            return $query;
        }else{
            return false;
        }
    }

    public function assignCountryToNetwork($name,$id_country){
        $query = $this->db->query("INSERT INTO networks(country_id, name) VALUE (".$id_country.",".$name.")");
        return $query;
    }

    public function checkIfAssignationExiste($id_country,$network){
        $query = $this->db->query("SELECT * FROM networks WHERE country_id='".$id_country."' AND name='".$network."'");
        if($query->num_rows()>0){
            return true;
        }else{
            return false;
        }
    }

    public function updateNetwork($old_network,$new_network){
        $query = $this->db->query("UPDATE networks SET networks.name='".$new_network."' WHERE networks.name ='".$old_network."'");
        return $query;
    }

    public function getAllAdmin(){

        $query = $this->db->query("SELECT firstname,lastname,admin.id AS id_admin,email,phone,adresse,admin.country AS country_id,admin.category,admin.salt,c.name AS country FROM admin INNER JOIN countries c on admin.country = c.id");
        if($query->num_rows()>0){
            return $query;
        }else{
            return false;
        }
    }

    public function isEmailExist($email) {
       $query = $this->db->query("SELECT id FROM admin WHERE email='".$email."'");
        if ($query->num_rows() > 0) {
            // user existed
            return true;
        } else {
            // user not existed
            return false;
        }
    }

    public function isPhoneExist($phone) {
        $query = $this->db->query("SELECT id FROM admin WHERE phone='".$phone."'");
        if ($query->num_rows() > 0) {
            // user existed
            return true;
        } else {
            // user not existed
            return false;
        }
    }

    public function getToken($token){
        $query = $this->db->query("SELECT id FROM admin WHERE token='".$token."'");
        if ($query->num_rows() > 0) {
            // token not used
            return true;
        } else {
            // token expired
            return false;
        }
    }

    public function createPasswordAdmin($encrypted_password,$salt,$token){
        $query = $this->db->query("UPDATE admin SET encrypted_password='".$encrypted_password."',salt='".$salt."' WHERE token ='".$token."'");
        return $query;
    }

    public function updateToken($new_token,$old_token){
        $query = $this->db->query("UPDATE admin SET token='".$new_token."' WHERE token ='".$old_token."'");
        return $query;
    }

    public function updateAdmin($id,$firstname,$lastname,$email,$phone,$adresse,$country,$category){
        $query = $this->db->query("UPDATE admin SET firstname='".$firstname."',lastname='".$lastname."',email='".$email."',phone='".$phone."',adresse='".$adresse."',country='".$country."',category='".$category."' WHERE id ='".$id."'");
        return $query;
    }

    public function deleteAdmin($id){
        $query = $this->db->query("DELETE FROM admin WHERE id ='".$id."'");
        return $query;
    }

    public function getPositionHyper($codeHyper){
        $query = $this->db->query("SELECT longitude,latitude FROM hyper_infos WHERE code_membre='".$codeHyper."'");
        return $query;
    }

    public function getNumberGeoBySuper($code_membre){
        $query = $this->db->query("SELECT number_geoBysuper AS numberGeo FROM super_infos WHERE code_membre='".$code_membre."' AND number_geoBysuper IS NOT NULL");
        if($query->num_rows()>0){
            return $query->row()->numberGeo;
        }else{
            return false;
        }
    }

    public function getIdAgentByMemberCode($code,$cat){
        $query=null;
        if($cat=="hyper") {
            $query = $this->db->query("SELECT agent_id AS id FROM hyper_infos WHERE code_membre='" . $code. "' ");
        }else{
            $query = $this->db->query("SELECT agent_id AS id FROM super_infos WHERE code_membre='" . $code. "' ");
        }
        if($query->num_rows()>0){
            return $query->row()->id;
        }else{
            return false;
        }
    }

    public function updateGeolocatedUser($id,$adresse,$lastname,$email){
        $query = $this->db->query("UPDATE agents SET firstname='".$adresse."',lastname='".$lastname."',email='".$email."' WHERE id ='".$id."'");
        return $query;
    }
    public function updatePhoneAgent($id,$phone){
        $query = $this->db->query("UPDATE networks_agents SET phone='".$phone."' WHERE agent_id ='".$id."'");
        return $query;
    }

    public function deleteInfosGeolocatedUser($id){
        $query = $this->db->query("DELETE FROM agents WHERE id ='".$id."'");
        return $query;
    }

    public function deleteCodeAgent($code){
        $query = $this->db->query("DELETE FROM codeGenerer WHERE code_membre ='".$code."'");
        return $query;
    }

    public function getIdCodeGenerer($code_membre){
        $query = $this->db->query("SELECT id FROM codeGenerer WHERE code_parrain='".$code_membre."' OR code_parrain IN (SELECT code_membre FROM codeGenerer where code_parrain='".$code_membre."')");
        if($query->num_rows()>0){
            return $query;
        }else{
            return false;
        }
    }
    public function getGabonNetwork(){
        $query = $this->db->query("SELECT id,name FROM networks WHERE country_id='78'");
        if($query->num_rows()>0){
            return $query;
        }else{
            return false;
        }
    }
    public function getCodeHyper($id_network){
        $query = $this->db->query("SELECT codeGenerer_id FROM networks_agents WHERE network_id='".$id_network."'");
        if($query->num_rows()>0){
            $query2 = $this->db->query("SELECT code_membre FROM codeGenerer WHERE id='".$query->row()->codeGenerer_id."' AND category='hyper'");
            if($query2->num_rows()>0){
                return $query2->row()->code_membre;
            }else{
                return false;
            }
        }else{
            return false;
        }
    }

    public function getVillesByPays($pays_id){
        $query = $this->db->query("SELECT towns.id, towns.name, towns.status
                                    FROM towns WHERE towns.country_id='".$pays_id."'");
        if($query->num_rows()>0){
            return $query;
        }else{
            return false;
        }
    }

    public function getAgentsFromCountry($id_country){
        $query = $this->db->query("SELECT agent_id,lastname AS lastname, code_parrain AS code_parrain, code_membre
                                    FROM super_infos AS ag
                                    WHERE ag.country_id='".$id_country."' AND category='super'
                                    GROUP BY lastname");
        if($query->num_rows()>0){
            return $query;
        }else{
            return false;
        }
    }

    public function getPointGeolocalised($id_network,$lastname){
        $query = $this->db->query("SELECT COUNT(`id`) AS c FROM `codeGenerer` 
                                      WHERE `code_parrain`=
                                      (SELECT code_membre FROM codeGenerer WHERE category='super' AND 
                                          id=(SELECT codeGenerer_id FROM networks_agents WHERE network_id='".$id_network."' AND 
                                            agent_id IN (SELECT id FROM agents WHERE lastname='".$lastname."') 
                                            ) 
                                      )
                                   ");
        if($query->num_rows()>0){
            return $query->row()->c;
        }else{
            return 0;
        }
    }

    public function getPointGeolocalisedByDate($id_network,$lastname,$debut,$fin){
        $query = $this->db->query("SELECT COUNT(`id`) AS c FROM `codeGenerer` 
                                      WHERE  date_creation>= '".$debut."' AND date_creation<='".$fin."' AND `code_parrain`=
                                      (SELECT code_membre FROM codeGenerer WHERE category='super' AND 
                                          id=(SELECT codeGenerer_id FROM networks_agents WHERE network_id='".$id_network."' AND 
                                            agent_id IN (SELECT id FROM agents WHERE lastname='".$lastname."') 
                                            ) 
                                      )
                                   ");
        if($query->num_rows()>0){
            return $query->row()->c;
        }else{
            return 0;
        }
    }

    public function getCorrectPointGeolocalisedByDate($id_network,$lastname,$debut,$fin){
        $query = $this->db->query("SELECT COUNT(*) AS c FROM `codeGenerer` 
                                    INNER JOIN `networks_agents` ON codeGenerer.id=networks_agents.codeGenerer_id
                                      WHERE  codeGenerer.date_creation>= '".$debut."' AND codeGenerer.date_creation<='".$fin."' 
                                      AND networks_agents.phone NOT LIKE '+241+241%' AND networks_agents.phone NOT LIKE '+2410000%' AND codeGenerer.code_parrain=
                                      (SELECT code_membre FROM codeGenerer WHERE category='super' AND 
                                          id=(SELECT codeGenerer_id FROM networks_agents WHERE network_id='".$id_network."' AND 
                                            agent_id IN (SELECT id FROM agents WHERE lastname='".$lastname."') 
                                            ) 
                                      )
                                   ");
        if($query->num_rows()>0){
            return $query->row()->c;
        }else{
            return 0;
        }
    }

    public function getNetworks($country_id){
        $query = $this->db->query("SELECT id,name FROM networks WHERE country_id ='".$country_id."'");
        if($query->num_rows()>0){
            return $query;
        }else{
            return false;
        }
    }

    public function get_current_page_records($limit, $start,$pays,$hyper,$ville)
    {
        $query = $this->db->query("SELECT child.adresse AS adresse,child.category AS childcat,child.lastname AS lastname,
                                           child.etat AS active, child.code_parrain AS code_parrain,child.code_membre AS member_code,
                                           child.network AS network, child.phone AS phone, child.email AS email,dad.adresse AS supadresse,
                                           dad.lastname AS suplastname, dad.code_membre AS sup_code_membre,dad.code_parrain AS sup_code_parrain,
                                           dad.network AS sup_network, dad.category AS dadcat
                                    FROM super_infos AS child
                                      INNER JOIN super_infos AS dad
                                        ON child.code_parrain=dad.code_membre
                                    WHERE child.country_id='".$pays."'
                                            AND child.latitude!=-1 AND child.longitude!=-1
                                          AND (child.code_parrain='".$hyper."'
                                          OR child.code_parrain IN (SELECT code_membre FROM super_infos WHERE code_parrain='".$hyper."' AND category ='super'))
                                          AND child.town_id='".$ville."'
                                          LIMIT ".$start.",".$limit);

        if($query->num_rows()>0){
            return $query;
        }else{
            return false;
        }
    }

    public function get_current_page_records_hyper($limit, $start,$hyper,$ville)
    {
        $query = $this->db->query("SELECT child.adresse AS adresse,child.category AS childcat,child.lastname AS lastname,child.solde AS solde,
                                            child.etat AS etat,
                                           child.etat AS active, child.code_parrain AS code_parrain,child.code_membre AS member_code,
                                           child.network AS network, child.phone AS phone, child.email AS email,dad.adresse AS supadresse,
                                           dad.lastname AS suplastname, dad.code_membre AS sup_code_membre,dad.code_parrain AS sup_code_parrain,
                                           dad.network AS sup_network, dad.category AS dadcat
                                    FROM super_infos AS child
                                      INNER JOIN super_infos AS dad
                                        ON child.code_parrain=dad.code_membre
                                    WHERE child.latitude!=-1 AND child.longitude!=-1
                                          AND child.code_parrain IN (SELECT code_membre FROM super_infos WHERE code_parrain='".$hyper."' AND category ='super')
                                          AND child.town='".$ville."'
                                          LIMIT ".$start.",".$limit);

        if($query->num_rows()>0){
            return $query;
        }else{
            return false;
        }
    }

    public function get_current_page_records_super($limit, $start,$super)
    {
        $query = $this->db->query("SELECT child.adresse AS adresse,child.category AS childcat,child.lastname AS lastname,child.solde AS solde,
                                           child.etat AS active, child.code_parrain AS code_parrain,child.code_membre AS member_code,
                                           child.network AS network, child.phone AS phone, child.email AS email,dad.adresse AS supadresse,
                                           dad.lastname AS suplastname, dad.code_membre AS sup_code_membre,dad.code_parrain AS sup_code_parrain,
                                           dad.network AS sup_network, dad.category AS dadcat
                                    FROM super_infos AS child
                                      INNER JOIN super_infos AS dad
                                        ON child.code_parrain=dad.code_membre
                                    WHERE child.latitude!=-1 AND child.longitude!=-1
                                          AND child.code_parrain='".$super."'
                                          LIMIT ".$start.",".$limit);

        if($query->num_rows()>0){
            return $query;
        }else{
            return false;
        }
    }

    public function get_total($pays,$hyper,$ville)
    {
        $query = $this->db->query("SELECT COUNT(*) AS c
                                    FROM super_infos AS child
                                      INNER JOIN super_infos AS dad
                                        ON child.code_parrain=dad.code_membre
                                    WHERE child.country_id='".$pays."'
                                          AND (child.code_parrain='".$hyper."'
                                          OR child.code_parrain IN (SELECT code_membre FROM super_infos WHERE code_parrain='".$hyper."' AND category ='super'))
                                          AND child.town_id='".$ville."'");
        return $query->row()->c;
    }

    public function get_current_page_records_positions($limit, $start,$pays,$hyper,$ville)
    {
        $query = $this->db->query("SELECT child.latitude AS latitude,child.longitude AS longitude,child.lastname AS lastname
                                FROM super_infos AS child
                                  INNER JOIN super_infos AS dad
                                    ON child.code_parrain=dad.code_membre
                                WHERE child.country_id='".$pays."'
                                      AND child.code_parrain='".$hyper."'
                                      OR child.code_parrain IN (SELECT code_membre FROM super_infos WHERE code_parrain='".$hyper."' AND category ='super')
                                      AND child.town_id='".$ville."'
                                      AND child.latitude!=-1 AND child.longitude!=-1
                                      LIMIT ".$start.",".$limit);

        if($query->num_rows()>0){
            return $query;
        }else{
            return false;
        }
    }

    public function getAllNetworksInfos($country_id){
        $query = $this->db->query("SELECT * FROM networks WHERE country_id ='".$country_id."'");
        if($query->num_rows()>0){
            return $query;
        }else{
            return false;
        }
    }

    public function getCountries(){
        $query = $this->db->query("SELECT countries.id,countries.name,countries.code_country,countries.code_dial,towns.name AS ville,towns.id AS id_ville FROM `towns` INNER JOIN countries ON towns.country_id=countries.id GROUP BY countries.id ");
        if($query->num_rows()>0){
            return $query;
        }else{
            return false;
        }
    }

    public function getFreeCode(){
        $query = $this->db->query("SELECT `code_membre` FROM `codeGenerer` WHERE `etat`=0 AND `category`='hyper' LIMIT 1");
        if($query->num_rows()>0){
            return $query->row()->code_membre;
        }else{
            return false;
        }
    }

    public function turnOnNetworkAgentEtat($phone){
        $query = $this->db->query("UPDATE `networks_agents` SET `etat`=1 WHERE `phone`=".$phone);
        if($query->num_rows()>0){
            return true;
        }else{
            return false;
        }
    }

    public function turnOnCodeGenererEtat($code_membre){
        $query = $this->db->query("UPDATE `codeGenerer` SET `etat`=1 WHERE `code_membre`=".$code_membre);
        if($query->num_rows()>0){
            return true;
        }else{
            return false;
        }
    }

    public function getInfosActivationHyperviseur(){
        $query = $this->db->query("SELECT networks_agents.phone, networks_agents.validation_code, agents.number_super,agents.number_geoBysuper FROM `agents` INNER JOIN networks_agents ON networks_agents.agent_id=agents.id INNER JOIN codeGenerer ON codeGenerer.id=networks_agents.codeGenerer_id WHERE codeGenerer.category='hyper' AND agents.number_geoBysuper IS NULL AND agents.number_super IS NULL ");
        if($query->num_rows()>0){
            return $query;
        }else{
            return false;
        }
    }

    public function getCodeMembreHyper($network_id){
        $query = $this->db->query("SELECT codeGenerer.code_membre FROM `networks_agents` INNER JOIN networks ON networks_agents.network_id=networks.id INNER JOIN codeGenerer ON codeGenerer.id=networks_agents.codeGenerer_id WHERE networks.id=".$network_id." AND codeGenerer.category='hyper' LIMIT 1 ");
        if($query->num_rows()>0){
            return $query->row()->code_membre;
        }else{
            return false;
        }
    }

    public  function getMostOldDateCreationAgent(){
        $query = $this->db->query("SELECT `date_created` FROM `agents` ORDER BY `date_created` LIMIT 1 ");
        if($query->num_rows()>0){
            return $query->row()->date_created;
        }else{
            return false;
        }
    }

    public function contact_incorrect(){
        $query = $this->db->query("SELECT count(*) AS c FROM `networks_agents` 
                                    INNER JOIN codeGenerer ON codeGenerer.id=networks_agents.codeGenerer_id 
                                    INNER JOIN networks ON networks.id=networks_agents.network_id 
                                    INNER JOIN countries ON countries.id=networks.country_id 
                                      WHERE codeGenerer.category='geolocated' AND countries.id=78 AND networks_agents.phone LIKE '+241+241%' OR networks_agents.phone LIKE '+2410000%'");
        if($query->num_rows()>0){
            return $query->row()->c;
        }else{
            return false;
        }
    }

    public function getPhysicalPointBySupervisor($debut,$fin,$agent){
        $query = $this->db->query("SELECT COUNT(DISTINCT agents.id) AS c FROM `agents` 
                                    INNER JOIN `networks_agents` ON agents.id=networks_agents.agent_id
                                    INNER JOIN codeGenerer ON codeGenerer.id=networks_agents.codeGenerer_id
                                    WHERE  agents.date_created>= '".$debut."' AND agents.date_created<='".$fin."' 
                                           AND codeGenerer.code_parrain IN
                                           (SELECT code_membre FROM codeGenerer WHERE category='super' AND 
                                                  id IN (SELECT codeGenerer_id FROM networks_agents WHERE 
                                                       agent_id IN (SELECT id FROM agents WHERE lastname='".$agent."')))");
        if($query->num_rows()>0){
            return $query->row()->c;
        }else{
            return false;
        }
    }

    public function getPointGeolocalisedByNetwork($id_network,$debut,$fin){
        $query = $this->db->query("SELECT COUNT(codeGenerer.id) AS c FROM `codeGenerer` 
                                      INNER JOIN networks_agents ON networks_agents.codeGenerer_id=codeGenerer.id
                                      INNER JOIN agents ON agents.id=networks_agents.agent_id
                                      WHERE agents.latitude!=-1 AND agents.longitude!=-1 AND codeGenerer.category='geolocated' AND date_creation>= '".$debut."' AND date_creation<='".$fin."' AND networks_agents.network_id='".$id_network."'
                                   ");
        if($query->num_rows()>0){
            return $query->row()->c;
        }else{
            return 0;
        }
    }

    public function getAllpositionsForWorld(){
        $this->db->select('latitude, longitude ,lastname');
        $this->db->from('agents');
        $query=$this->db->get();
        return $query;
    }

    public function getAgentNetwork($agent_name){
        $query = $this->db->query("SELECT countries.id AS id FROM `agents` 
                                      INNER JOIN towns ON agents.town_id=towns.id 
                                      INNER JOIN countries ON towns.country_id=countries.id 
                                      WHERE agents.lastname='".$agent_name."' LIMIT 1 
                                   ");
        if($query->num_rows()>0){
            return $query->row()->id;
        }else{
            return 0;
        }
    }

    public function getSuperNameAndCodeForHyp($codeHyp){

        $query = $this->db->query("SELECT child.lastname AS lastname, child.code_membre AS member_code
                                    FROM super_infos AS child 
                                    INNER JOIN hyper_infos AS dad
                                            ON child.code_parrain='".$codeHyp."'
                                    WHERE child.category='super' AND dad.category='hyper' AND dad.code_membre='".$codeHyp."'
                                  ");

        if($query->num_rows()>0){
            return $query;
        }else{
            return false;
        }
    }

    public function getDemandesBySuper($code_parrain, $code_membre){

            $query = $this->db->query("SELECT *
                                    FROM info_demandeCredits AS d
                                    WHERE d.codeParrain='".$code_parrain."' AND d.codeMembre='".$code_membre."'
                                    ORDER BY dateAjout");

        if($query->num_rows()>0){
            return $query;
        }else{
            return false;
        }

    }

    public function getGameCountry(){
        $query = $this->db->query("SELECT countries.id,countries.name
        FROM configGame
        INNER JOIN countries ON configGame.id_pays=countries.id
        WHERE configGame.etat=1
        GROUP BY countries.id");

        if($query->num_rows()>0){
        return $query;
        }else{
        return false;
        } 
    }

    public function getCoutForQuota($pays,$quota_inf,$quota_sup){

        $query = $this->db->query("SELECT COUNT(*) AS c FROM (SELECT * FROM game WHERE `id_pays`=".$pays." GROUP BY `id_parrain` HAVING COUNT(*) >= ".$quota_inf." AND COUNT(*) < ".$quota_sup." ) AS t");

        if($query->num_rows()>0){
            return $query->row()->c;
        }else{
            return false;
        } 
    }

    public function getMembers($pays){
        $query = $this->db->query("SELECT t.lastname,t.phone,t.email,t.total
                                   FROM (SELECT users.lastname,users.phone,users.email,COUNT(*) AS total FROM game
                                   INNER JOIN users ON game.id_parrain=users.id
                                   WHERE `id_pays`=".$pays." GROUP BY `id_parrain` HAVING COUNT(*) >= 20) AS t");
        if($query->num_rows()>0){
            return $query;
        }else{
            return false;
        }
    }

    public function test($pays,$hyper,$ville){

        $query = $this->db->query("SELECT child.agent_id AS id,child.adresse AS adresse,child.category AS childcat,child.lastname AS lastname,
                                           child.etat AS active, child.code_parrain AS code_parrain,child.code_membre AS member_code,
                                           child.network AS network, child.phone AS phone, child.email AS email,dad.adresse AS supadresse,
                                           dad.lastname AS suplastname, dad.code_membre AS sup_code_membre,dad.code_parrain AS sup_code_parrain,
                                           dad.network AS sup_network, dad.category AS dadcat
                                    FROM super_infos AS child
                                      INNER JOIN super_infos AS dad
                                        ON child.code_parrain=dad.code_membre
                                    WHERE child.country_id='".$pays."'
                                          ORDER BY child.agent_id DESC ");

        if($query->num_rows()>0){
            return $query;
        }else{
            return false;
        }
    }

    public function checkCountryGameExist($pays){
        $query = $this->db->query("SELECT *
                                   FROM configGame
                                   WHERE `id_pays`=".$pays);
        if($query->num_rows()>0){
            return true;
        }else{
            return false;
        }
    }

    public function activeGameOnCountry($pays,$etat){
        $query = $this->db->query("UPDATE configGame SET etat = ".$etat." WHERE id_pays =".$pays);
        return $query;
    }

    public function addContryToGame($pays){
        $query = $this->db->query("INSERT INTO configGame(id_pays) VALUE (".$pays.")");
        //
        return $query;
    }
    public function getPaliers(){
        $query = $this->db->query("SELECT *
                                   FROM paliers");
        if($query->num_rows()>0){
            return $query;
        }else{
            return false;
        }
    }
    public function modifPalier($value,$niveau){
        $query = $this->db->query("UPDATE paliers SET valeur = ".$value." WHERE id =".$niveau);
        return $query;
    }
    public function getQuota($id){
        $query = $this->db->query("SELECT valeur FROM paliers WHERE id=".$id);
        if($query->num_rows()>0){
            return $query->row()->valeur;
        }else{
            return false;
        } 
    }
    public function getNameCountry($id){
        $query = $this->db->query("SELECT name FROM countries WHERE id=".$id);
        if($query->num_rows()>0){
            return $query->row()->name;
        }else{
            return false;
        }
    }
    public function getTownId($town_name){
        $query = $this->db->query("SELECT id FROM towns WHERE `name`='".$town_name."'");
        if($query->num_rows()>0){
            return $query->row()->id;
        }else{
            return null;
        }
    }
    public function checkPosition($lat,$lng){
        $query = $this->db->query("SELECT id FROM agents WHERE `latitude`=".$lat." AND `longitude` =".$lng);
        if($query->num_rows()>0){
            return true;
        }else{
            return false;
        } 
    }
    public function create_town($country_id,$name){
        $query = $this->db->query("INSERT INTO towns (`country_id`, `name`, `status`) VALUES (".$country_id.", '".$name."', 1)");
        return $query; 
    }
    public function update_town_agent($lat,$lng,$town_id){
        $query = $this->db->query("UPDATE agents SET `town_id` = ".$town_id." WHERE `latitude`= ".$lat." AND `longitude`=".$lng);
        return $query;
        
    }

    //wallet
    public  function getActiveNetwork(){
        $query = $this->db->query("SELECT networks.id,networks.name,countries.name AS country
                                        FROM networks
                                        INNER JOIN countries ON networks.country_id=countries.id
                                        WHERE `status`=1");
        if($query->num_rows()>0){
            return $query;
        }else{
            return null;
        }
    }
    public function getTaux($id_network){
        $query = $this->db->query("SELECT configWallet.id,
                                            taux_com_retrait AS taux_r,
                                            taux_com_ag_retrait AS taux_ag_r,
                                            com_depot AS taux_d,
                                            taux_com_ag_depot AS taux_ag_d
                                        FROM configWallet
                                        WHERE `id_network`=".$id_network);
        if($query->num_rows()>0){
            return $query;
        }else{
            return null;
        }
    }

    public function getNetworkName($id_network){
        $query = $this->db->query("SELECT networks.name
                                        FROM networks
                                        WHERE `id`=".$id_network);
        if($query->num_rows()>0){
            return $query->row()->name;
        }else{
            return null;
        }
    }

    public function getTransactions($id_network){
        $query = $this->db->query("SELECT id,type_transac
                                    FROM infos_transaction
                                    WHERE `network_id`=".$id_network);
        if($query->num_rows()>0){
            return $query;
        }else{
            return false;
        } 
    }

    public function getInfosWalletAgentForHyper($id_network){
        $query = $this->db->query("SELECT *
                                    FROM wallet_agent
                                    WHERE `network_id`=".$id_network);
        if($query->num_rows()>0){
            return $query;
        }else{
            return false;
        } 
    }
}