<?php
/**
 * Created by Sevio®.
 * All rights reserved to Sevio Solutions® SRL
 */
class class_statistics {
    public function __call($method_name,$parameter)
    {
        if($method_name == "get_impressions") {
            $count = count($parameter);
            switch ($count)
            {
                case "2":
                {
					
                    $sql = "SELECT COALESCE(SUM(`impressions`),0) impressions, COALESCE(SUM(`money`),0) money, COALESCE(SUM(`clicks`),0) clicks,`date` FROM `app_daily_imp` WHERE `date` BETWEEN '".$parameter[0]."' AND '".$parameter[1]."' GROUP BY `date` ORDER BY `date` DESC ";
                    $result = mysql_query ($sql);
                    while ($day = mysql_fetch_array($result)){
                        $value[$day['date']]=$day['impressions']."|".number_format($day['money'],5)."|".$day['clicks'];
                    }
                    if(isset($value))
                        return $value;
                    else
                        return 0;
                }break;
                case "3":
                {
                    $sql = "SELECT COALESCE(SUM(`impressions`),0) impressions, COALESCE(SUM(`money`),0) money, COALESCE(SUM(`clicks`),0) clicks,`date` FROM `app_daily_imp` WHERE `unique_type`='".$parameter[2]."' AND `date` BETWEEN '".$parameter[0]."' AND '".$parameter[1]."' GROUP BY `date` ORDER BY `date` DESC";
                    $result = mysql_query ($sql);
                    while ($day = mysql_fetch_array($result)){
                        $value[$day['date']]=$day['impressions']."|".number_format($day['money'],5)."|".$day['clicks'];
                    }
                    if(isset($value))
                        return $value;
                    else
                        return 0;
                }break;
                case "4":{
                    $impressions=0;
                    $money=0;
                    $clicks=0;
                    $sql = "SELECT COALESCE(SUM(`impressions`),0) impressions, COALESCE(SUM(`money`),0) money, COALESCE(SUM(`clicks`),0) clicks ,`date` FROM `app_daily_imp` WHERE `unique_user`='".$parameter[2]."' AND `user_type`='".$parameter[3]."' AND `date` BETWEEN '".$parameter[0]."' AND '".$parameter[1]."' GROUP BY `date` ORDER BY `date` DESC";
                    $result = mysql_query ($sql);
                    while ($day = mysql_fetch_array($result)){
                        $value[$day['date']]=$day['impressions']."|".number_format($day['money'],5)."|".$day['clicks'];
                        if($day['date']==date("Y-m-d"))
                        {
                            $impressions=$day['impressions'];
                            $money=number_format($day['money'],4);
                            $clicks=$day['clicks'];
                        }
                    }
                    if($parameter[1]==date('Y-m-d'))
                    {
                        if($parameter[3]==1) {
                            $sql = mysql_query("SELECT COUNT(*) AS impressions,COALESCE(SUM(`click`),0) clicks,COALESCE(SUM(`outcome`),0) money,`date` FROM `app_count_imp` WHERE `date`='" . date('Y-m-d') . "' AND `unique_advertiser`='" . $parameter[2] . "' GROUP BY `date` ORDER BY `date` DESC");
                            $day = mysql_fetch_array($sql);
                            if($day) {
                                $impressions+=$day['impressions'];
                                $money+=$day['money'];
                                $clicks=$day['clicks'];
                                $value[$day['date']] = $impressions."|".number_format($money,5). "|".$clicks;
                            }
                        }else
                        {
                            $sql = mysql_query("SELECT COUNT(*) AS impressions,COALESCE(SUM(`click`),0) clicks,COALESCE(SUM(`income`),0) money,`date` FROM `app_count_imp` WHERE `date`='" . date('Y-m-d') . "' AND `unique_publisher`='" . $parameter[2] . "' GROUP BY `date` ORDER BY `date` DESC");
                            $day = mysql_fetch_array($sql);
                            if($day) {
                                $impressions+=$day['impressions'];
                                $money+=$day['money'];
                                $clicks=$day['clicks'];
                                $value[$day['date']] = $impressions."|".number_format($money,5). "|".$clicks;
                            }
                        }
                    }
                    if(isset($value))
                        return $value;
                    else
                        return 0;
                }break;
                case "5":
                {
                    $impressions=0;
                    $money=0;
                    $clicks=0;
                    $sql = "SELECT COALESCE(SUM(`impressions`),0) impressions, COALESCE(SUM(`money`),0) money, COALESCE(SUM(`clicks`),0) clicks ,`date` FROM `app_daily_imp` WHERE `unique_user`='".$parameter[2]."' AND `user_type`='".$parameter[3]."' AND `unique_type`='".$parameter[4]."' AND `date` BETWEEN '".$parameter[0]."' AND '".$parameter[1]."' GROUP BY `date` ORDER BY `date` DESC";
                    $result = mysql_query ($sql);
                    while ($day = mysql_fetch_array($result)){
                        $value[$day['date']]=$day['impressions']."|".number_format($day['money'],5)."|".$day['clicks'];
                        if($day['date']==date("Y-m-d"))
                        {
                            $impressions=$day['impressions'];
                            $money=number_format($day['money'],4);
                            $clicks=$day['clicks'];
                        }
                    }
                    if($parameter[1]==date('Y-m-d'))
                    {
                        if($parameter[3]==1) {
                            $sql = mysql_query("SELECT COUNT(*) AS impressions,COALESCE(SUM(`click`),0) clicks,COALESCE(SUM(`outcome`),0) money,`date` FROM `app_count_imp` WHERE `date`='" . date('Y-m-d') . "' AND `unique_advertiser`='" . $parameter[2] . "' AND `unique_ad`='".$parameter[4]."' GROUP BY `date` ORDER BY `date` DESC ");
                            $day = mysql_fetch_array($sql);
                            if($day)
                            {
                                $impressions+=$day['impressions'];
                                $money+=$day['money'];
                                $clicks=$day['clicks'];
                                $value[$day['date']] = $impressions."|".number_format($money,5). "|".$clicks;
                            }
                        }else
                        {
                            $sql = mysql_query("SELECT COUNT(*) AS impressions,COALESCE(SUM(`click`),0) clicks,COALESCE(SUM(`income`),0) money,`date` FROM `app_count_imp` WHERE `date`='" . date('Y-m-d') . "' AND `unique_publisher`='" . $parameter[2] . "' AND `unique_tag`='".$parameter[4]."' GROUP BY `date` ORDER BY `date` DESC");
                            $day = mysql_fetch_array($sql);
                            if($day)
                            {
                                $impressions+=$day['impressions'];
                                $money+=$day['money'];
                                $clicks=$day['clicks'];
                                $value[$day['date']] = $impressions."|".number_format($money,5). "|".$clicks;
                            }
                        }
                    }
                    if(isset($value))
                        return $value;
                    else
                        return 0;
                }break;
                default:return 0;break;
            }
        }elseif($method_name == "get_countries") {
            $count = count($parameter);
            switch ($count)
            {
                case "2":
                {
                    $sql = "SELECT COALESCE(SUM(`impressions`),0) impressions, COALESCE(SUM(`money`),0) money, SUM(`clicks`) clicks,`country` FROM `app_daily_country` WHERE `date` BETWEEN '".$parameter[0]."' AND '".$parameter[1]."' GROUP BY `country`";
                    $result = mysql_query ($sql);
                    while ($day = mysql_fetch_array($result)){
                        $value[$day['country']]=$day['impressions']."|".number_format($day['money'],5)."|".$day['clicks'];
                    }
                    if(isset($value))
                        return $value;
                    else
                        return 0;
                }break;
                case "3":
                {
                    $sql = "SELECT COALESCE(SUM(`impressions`),0) impressions, COALESCE(SUM(`money`),0) money, SUM(`clicks`) clicks,`country` FROM `app_daily_country` WHERE `unique_type`='".$parameter[2]."' AND `date` BETWEEN '".$parameter[0]."' AND '".$parameter[1]."' GROUP BY `country` ORDER BY `date` DESC ";
                    $result = mysql_query ($sql);
                    while ($day = mysql_fetch_array($result)){
                        $value[$day['country']]=$day['impressions']."|".number_format($day['money'],5)."|".$day['clicks'];
                    }
                    if(isset($value))
                        return $value;
                    else
                        return 0;
                }break;
                case "4":
                {
                    $sql = "SELECT COALESCE(SUM(`impressions`),0) impressions, COALESCE(SUM(`money`),0) money, SUM(`clicks`) clicks,`country` FROM `app_daily_country` WHERE `unique_user`='".$parameter[2]."' AND `user_type`='".$parameter[3]."' AND `date` BETWEEN '".$parameter[0]."' AND '".$parameter[1]."' GROUP BY `country` ORDER BY `date` DESC";
                    $result = mysql_query ($sql);
                    while ($day = mysql_fetch_array($result)){
                        $value[$day['country']]=$day['impressions']."|".number_format($day['money'],5)."|".$day['clicks'];
                    }
                    if($parameter[1]==date('Y-m-d'))
                    {
                        if($parameter[3]==1) {
                            $sql = mysql_query("SELECT COUNT(*) AS impressions,COALESCE(SUM(`click`),0) clicks,COALESCE(SUM(`outcome`),0) money,`country` FROM `app_count_imp` WHERE `date`='" . date('Y-m-d') . "' AND `unique_advertiser`='" . $parameter[2] . "' GROUP BY `country`");
                            while($day = mysql_fetch_array($sql)) {
                                $aux=explode('|',$value[$day['country']]);
                                $aux[0]+=$day['impressions'];
                                $aux[1]+=number_format($day['money'],5);
                                $aux[2]+=$day['clicks'];
                                $aux_final=implode('|',$aux);
                                $value[$day['country']] = $aux_final;
                            }
                        }else
                        {
                            $sql = mysql_query("SELECT COUNT(*) AS impressions,COALESCE(SUM(`click`),0) clicks,COALESCE(SUM(`income`),0) money,`country` FROM `app_count_imp` WHERE `date`='" . date('Y-m-d') . "' AND `unique_publisher`='" . $parameter[2] . "' GROUP BY `country`");
                            while($day = mysql_fetch_array($sql)) {
                                $aux=explode('|',$value[$day['country']]);
                                $aux[0]+=$day['impressions'];
                                $aux[1]+=number_format($day['money'],5);
                                $aux[2]+=$day['clicks'];
                                $aux_final=implode('|',$aux);
                                $value[$day['country']] = $aux_final;
                            }
                        }
                    }
                    if(isset($value))
                        return $value;
                    else
                        return 0;
                }break;
                case "5":
                {
                    $sql = "SELECT COALESCE(SUM(`impressions`),0) impressions, COALESCE(SUM(`money`),0) money, SUM(`clicks`) clicks,`country` FROM `app_daily_country` WHERE `unique_user`='".$parameter[2]."' AND `user_type`='".$parameter[3]."' AND `unique_type`='".$parameter[4]."' AND `date` BETWEEN '".$parameter[0]."' AND '".$parameter[1]."' GROUP BY `country` ORDER BY `date` DESC";
                    $result = mysql_query ($sql);
                    while ($day = mysql_fetch_array($result)){
                        $value[$day['country']]=$day['impressions']."|".number_format($day['money'],5)."|".$day['clicks'];
                    }
                    if($parameter[1]==date('Y-m-d'))
                    {
                        if($parameter[3]==1) {
                            $sql = mysql_query("SELECT COUNT(*) AS impressions,COALESCE(SUM(`click`),0) clicks,COALESCE(SUM(`outcome`),0) money,`country` FROM `app_count_imp` WHERE `date`='" . date('Y-m-d') . "' AND `unique_advertiser`='" . $parameter[2] . "' AND `unique_ad`='".$parameter[4]."' GROUP BY `country`");
                            while($day = mysql_fetch_array($sql)) {
                                $aux=explode('|',$value[$day['country']]);
                                $aux[0]+=$day['impressions'];
                                $aux[1]+=number_format($day['money'],5);
                                $aux[2]+=$day['clicks'];
                                $aux_final=implode('|',$aux);
                                $value[$day['country']] = $aux_final;
                            }
                        }else
                        {
                            $sql = mysql_query("SELECT COUNT(*) AS impressions,COALESCE(SUM(`click`),0) clicks,COALESCE(SUM(`income`),0) money,`country` FROM `app_count_imp` WHERE `date`='" . date('Y-m-d') . "' AND `unique_publisher`='" . $parameter[2] . "' AND `unique_tag`='".$parameter[4]."' GROUP BY `country`");
                            while($day = mysql_fetch_array($sql)) {
                                $aux=explode('|',$value[$day['country']]);
                                $aux[0]+=$day['impressions'];
                                $aux[1]+=number_format($day['money'],5);
                                $aux[2]+=$day['clicks'];
                                $aux_final=implode('|',$aux);
                                $value[$day['country']] = $aux_final;
                            }
                        }
                    }
                    if(isset($value))
                        return $value;
                    else
                        return 0;
                }break;
                default:return 0;break;
            }
        }elseif($method_name == "get_agents") {
            $count = count($parameter);
            switch ($count)
            {
                case "2":
                {
                    $sql = "SELECT COALESCE(SUM(`impressions`),0) impressions, COALESCE(SUM(`money`),0) money, SUM(`clicks`) clicks,`browser` FROM `app_daily_agents` WHERE `date` BETWEEN '".$parameter[0]."' AND '".$parameter[1]."' GROUP BY `browser` ORDER BY `date` DESC";
                    $result = mysql_query ($sql);
                    while ($day = mysql_fetch_array($result)){
                        $value[$day['browser']]=$day['impressions']."|".number_format($day['money'],5)."|".$day['clicks'];
                    }
                    if(isset($value))
                        return $value;
                    else
                        return 0;
                }break;
                case "3":
                {
                    $sql = "SELECT COALESCE(SUM(`impressions`),0) impressions, COALESCE(SUM(`money`),0) money, SUM(`clicks`) clicks,`browser` FROM `app_daily_agents` WHERE `unique_type`='".$parameter[2]."' AND `date` BETWEEN '".$parameter[0]."' AND '".$parameter[1]."' GROUP BY `browser` ORDER BY `date` DESC";
                    $result = mysql_query ($sql);
                    while ($day = mysql_fetch_array($result)){
                        $value[$day['browser']]=$day['impressions']."|".number_format($day['money'],5)."|".$day['clicks'];
                    }
                    if(isset($value))
                        return $value;
                    else
                        return 0;
                }break;
                case "4":
                {
                    $sql = "SELECT COALESCE(SUM(`impressions`),0) impressions, COALESCE(SUM(`money`),0) money, SUM(`clicks`) clicks,`browser` FROM `app_daily_agents` WHERE `unique_user`='".$parameter[2]."' AND `user_type`='".$parameter[3]."' AND `date` BETWEEN '".$parameter[0]."' AND '".$parameter[1]."' GROUP BY `browser` ORDER BY `date` DESC";
                    $result = mysql_query ($sql);
                    while ($day = mysql_fetch_array($result)){
                        $value[$day['browser']]=$day['impressions']."|".number_format($day['money'],5)."|".$day['clicks'];
                    }
                    if($parameter[1]==date('Y-m-d'))
                    {
                        if($parameter[3]==1) {
                            $sql = mysql_query("SELECT COUNT(*) AS impressions,COALESCE(SUM(`click`),0) clicks,COALESCE(SUM(`outcome`),0) money,`browser` FROM `app_count_imp` WHERE `date`='" . date('Y-m-d') . "' AND `unique_advertiser`='" . $parameter[2] . "' GROUP BY `browser`");
                            while($day = mysql_fetch_array($sql)) {
                                $aux=explode('|',$value[$day['browser']]);
                                $aux[0]+=$day['impressions'];
                                $aux[1]+=number_format($day['money'],5);
                                $aux[2]+=$day['clicks'];
                                $aux_final=implode('|',$aux);
                                $value[$day['browser']] = $aux_final;
                            }
                        }else
                        {
                            $sql = mysql_query("SELECT COUNT(*) AS impressions,COALESCE(SUM(`click`),0) clicks,COALESCE(SUM(`income`),0) money,`browser` FROM `app_count_imp` WHERE `date`='" . date('Y-m-d') . "' AND `unique_publisher`='" . $parameter[2] . "' GROUP BY `browser`");
                            while($day = mysql_fetch_array($sql)) {
                                $aux=explode('|',$value[$day['browser']]);
                                $aux[0]+=$day['impressions'];
                                $aux[1]+=number_format($day['money'],5);
                                $aux[2]+=$day['clicks'];
                                $aux_final=implode('|',$aux);
                                $value[$day['browser']] = $aux_final;
                            }
                        }
                    }
                    if(isset($value))
                        return $value;
                    else
                        return 0;
                }break;
                case "5":
                {
                    $sql = "SELECT COALESCE(SUM(`impressions`),0) impressions, COALESCE(SUM(`money`),0) money, SUM(`clicks`) clicks,`browser` FROM `app_daily_agents` WHERE `unique_user`='".$parameter[2]."' AND `user_type`='".$parameter[3]."' AND `unique_type`='".$parameter[4]."' AND `date` BETWEEN '".$parameter[0]."' AND  '".$parameter[1]."' GROUP BY `browser` ORDER BY `date` DESC";
                    $result = mysql_query ($sql);
                    while ($day = mysql_fetch_array($result)){
                        $value[$day['browser']]=$day['impressions']."|".number_format($day['money'],5)."|".$day['clicks'];
                    }
                    if($parameter[1]==date('Y-m-d'))
                    {
                        if($parameter[3]==1) {
                            $sql = mysql_query("SELECT COUNT(*) AS impressions,COALESCE(SUM(`click`),0) clicks,COALESCE(SUM(`outcome`),0) money,`browser` FROM `app_count_imp` WHERE `date`='" . date('Y-m-d') . "' AND `unique_advertiser`='" . $parameter[2] . "' AND `unique_ad`='".$parameter[4]."' GROUP BY `browser`");
                            while($day = mysql_fetch_array($sql)) {
                                $aux=explode('|',$value[$day['browser']]);
                                $aux[0]+=$day['impressions'];
                                $aux[1]+=number_format($day['money'],5);
                                $aux[2]+=$day['clicks'];
                                $aux_final=implode('|',$aux);
                                $value[$day['browser']] = $aux_final;
                            }
                        }else
                        {
                            $sql = mysql_query("SELECT COUNT(*) AS impressions,COALESCE(SUM(`click`),0) clicks,COALESCE(SUM(`income`),0) money,`browser` FROM `app_count_imp` WHERE `date`='" . date('Y-m-d') . "' AND `unique_publisher`='" . $parameter[2] . "' AND `unique_tag`='".$parameter[4]."' GROUP BY `browser`");
                            while($day = mysql_fetch_array($sql)) {
                                $aux=explode('|',$value[$day['browser']]);
                                $aux[0]+=$day['impressions'];
                                $aux[1]+=number_format($day['money'],5);
                                $aux[2]+=$day['clicks'];
                                $aux_final=implode('|',$aux);
                                $value[$day['browser']] = $aux_final;
                            }
                        }
                    }
                    if(isset($value))
                        return $value;
                    else
                        return 0;
                }break;
                default:return 0;break;
            }
        }elseif($method_name == "get_pages") {
            $count = count($parameter);
            switch ($count)
            {
                case "2":
                {
                    $sql = "SELECT COALESCE(SUM(`impressions`),0) impressions, COALESCE(SUM(`money`),0) money, SUM(`clicks`) clicks,`url` FROM `app_daily_pages` WHERE `date` BETWEEN '".$parameter[0]."' AND '".$parameter[1]."' GROUP BY `url` ORDER BY `date` DESC";
                    $result = mysql_query ($sql);
                    while ($day = mysql_fetch_array($result)){
                        $value[$day['url']]=$day['impressions']."|".number_format($day['money'],5)."|".$day['clicks'];
                    }
                    if(isset($value))
                        return $value;
                    else
                        return 0;
                }break;
                case "3":
                {
                    $sql = "SELECT COALESCE(SUM(`impressions`),0) impressions, COALESCE(SUM(`money`),0) money, SUM(`clicks`) clicks,`url` FROM `app_daily_pages` WHERE `unique_type`='".$parameter[2]."' AND `date` BETWEEN '".$parameter[0]."' AND '".$parameter[1]."' GROUP BY `url` ORDER BY `date` DESC";
                    $result = mysql_query ($sql);
                    while ($day = mysql_fetch_array($result)){
                        $value[$day['url']]=$day['impressions']."|".number_format($day['money'],5)."|".$day['clicks'];
                    }
                    if(isset($value))
                        return $value;
                    else
                        return 0;
                }break;
                case "4":
                {
                    $sql ="SELECT COALESCE(SUM(`impressions`),0) impressions, COALESCE(SUM(`money`),0) money, SUM(`clicks`) clicks,`url` FROM `app_daily_pages` WHERE `unique_user`='".$parameter[2]."' AND `user_type`='".$parameter[3]."' AND `date` BETWEEN '".$parameter[0]."' AND '".$parameter[1]."' GROUP BY `url` ORDER BY `date` DESC";
                    $result = mysql_query ($sql);
                    while ($day = mysql_fetch_array($result)){
                        $value[$day['url']]=$day['impressions']."|".number_format($day['money'],5)."|".$day['clicks'];
                    }
                    if($parameter[1]==date('Y-m-d'))
                    {
                        if($parameter[3]==1) {
                            $sql = mysql_query("SELECT COUNT(*) AS impressions,COALESCE(SUM(`click`),0) clicks,COALESCE(SUM(`outcome`),0) money,`url` FROM `app_count_imp` WHERE `date`='" . date('Y-m-d') . "' AND `unique_advertiser`='" . $parameter[2] . "' GROUP BY `url`");
                            while($day = mysql_fetch_array($sql)) {
                                $aux=explode('|',$value[$day['url']]);
                                $aux[0]+=$day['impressions'];
                                $aux[1]+=number_format($day['money'],5);
                                $aux[2]+=$day['clicks'];
                                $aux_final=implode('|',$aux);
                                $value[$day['url']] = $aux_final;
                            }
                        }else
                        {
                            $sql = mysql_query("SELECT COUNT(*) AS impressions,COALESCE(SUM(`click`),0) clicks,COALESCE(SUM(`income`),0) money,`url` FROM `app_count_imp` WHERE `date`='" . date('Y-m-d') . "' AND `unique_publisher`='" . $parameter[2] . "' GROUP BY `url` ");
                            while($day = mysql_fetch_array($sql)) {
                                $aux=explode('|',$value[$day['url']]);
                                $aux[0]+=$day['impressions'];
                                $aux[1]+=number_format($day['money'],5);
                                $aux[2]+=$day['clicks'];
                                $aux_final=implode('|',$aux);
                                $value[$day['url']] = $aux_final;
                            }
                        }
                    }
                    if(isset($value))
                        return $value;
                    else
                        return 0;
                }break;
                case "5":
                {
                    $sql = "SELECT COALESCE(SUM(`impressions`),0) impressions, COALESCE(SUM(`money`),0) money, SUM(`clicks`) clicks,`url` FROM `app_daily_pages` WHERE `unique_user`='".$parameter[2]."' AND `user_type`='".$parameter[3]."' AND `unique_type`='".$parameter[4]."' AND `date` BETWEEN '".$parameter[0]."' AND '".$parameter[1]."' GROUP BY `url` ORDER BY `date` DESC";
                    $result = mysql_query ($sql);
                    while ($day = mysql_fetch_array($result)){
                        $value[$day['url']]=$day['impressions']."|".number_format($day['money'],4)."|".$day['clicks'];
                    }
                    if($parameter[1]==date('Y-m-d'))
                    {
                        if($parameter[3]==1) {
                            $sql = mysql_query("SELECT COUNT(*) AS impressions,COALESCE(SUM(`click`),0) clicks,COALESCE(SUM(`outcome`),0) money,`url` FROM `app_count_imp` WHERE `date`='" . date('Y-m-d') . "' AND `unique_advertiser`='" . $parameter[2] . "' AND `unique_ad`='".$parameter[4]."' GROUP BY `url`");
                            while($day = mysql_fetch_array($sql)) {
                                $aux=explode('|',$value[$day['url']]);
                                $aux[0]+=$day['impressions'];
                                $aux[1]+=number_format($day['money'],5);
                                $aux[2]+=$day['clicks'];
                                $aux_final=implode('|',$aux);
                                $value[$day['url']] = $aux_final;
                            }
                        }else
                        {
                            $sql = mysql_query("SELECT COUNT(*) AS impressions,COALESCE(SUM(`click`),0) clicks,COALESCE(SUM(`income`),0) money,`url` FROM `app_count_imp` WHERE `date`='" . date('Y-m-d') . "' AND `unique_publisher`='" . $parameter[2] . "' AND `unique_tag`='".$parameter[4]."' GROUP BY `url`");
                            while($day = mysql_fetch_array($sql)) {
                                $aux=explode('|',$value[$day['url']]);
                                $aux[0]+=$day['impressions'];
                                $aux[1]+=number_format($day['money'],5);
                                $aux[2]+=$day['clicks'];
                                $aux_final=implode('|',$aux);
                                $value[$day['url']] = $aux_final;
                            }
                        }
                    }
                    if(isset($value))
                        return $value;
                    else
                        return 0;
                }break;
                default:return 0;break;
            }
        }else return 0;
    }
    static public function get_total_users()
    {
        $sql=mysql_query("SELECT COUNT(*) AS nr FROM `app_users`");
        $data=mysql_fetch_array($sql);
        $users=$data['nr'];
        $sql=mysql_query("SELECT COUNT(*) AS nr FROM `app_users` WHERE `user_type`='1'");
        $data=mysql_fetch_array($sql);
        $advertisers=$data['nr'];
        $sql=mysql_query("SELECT COUNT(*) AS nr FROM `app_users` WHERE `user_type`='2'");
        $data=mysql_fetch_array($sql);
        $publishers=$data['nr'];
        $return=array();
        $return[0]=$users;$return[1]=$advertisers;$return[2]=$publishers;
        return $return;
    }
    static public function get_active_campains()
    {
        $sql=mysql_query("SELECT COUNT(*) AS nr FROM `app_campaigns` WHERE  `valid` = '1'");
        $data=mysql_fetch_array($sql);
        return $data['nr'];
    }
    static  public function get_active_tags()
    {
        $sql=mysql_query("SELECT COUNT(*) AS nr FROM `app_tags` WHERE `valid` = '1'");
        $data=mysql_fetch_array($sql);
        return $data['nr'];
    }
    static public function get_total_payments()
    {
        $sql=mysql_query("SELECT COALESCE(SUM(`amount`),0) AS nr FROM `app_payments` WHERE `payment_type`='Add Funds' AND `status`='Completed'");
        $data=mysql_fetch_array($sql);
        return number_format($data['nr'],5);
    }
    static public function get_total_withdraws()
    {
        $sql=mysql_query("SELECT COALESCE(SUM(`amount`),0) AS nr FROM `app_payments` WHERE `payment_type`='Withdraw' AND `status`='Completed'");
        $data=mysql_fetch_array($sql);
        return number_format($data['nr'],5);
    }
    static public function get_total_win()
    {
        $sql=mysql_query("SELECT COALESCE(SUM(`amount`),0) AS nr FROM `app_payments` WHERE `payment_type`='Withdraw' AND `status`='Completed'");
        $data1=mysql_fetch_array($sql);
        $sql=mysql_query("SELECT COALESCE(SUM(`amount`),0) AS nr FROM `app_payments` WHERE `payment_type`='Add Funds' AND `status`='Completed'");
        $data2=mysql_fetch_array($sql);
        $total=$data2['nr']-$data1['nr'];
        return number_format($total,5);
    }
    static public function get_top_bids()
    {
        $bid=array();
        $sql=mysql_query("SELECT MAX(`bid`) AS max_bid ,ad_type FROM `app_campaigns` GROUP BY `ad_type`");
        while($data=mysql_fetch_array($sql))
            $bid[$data['ad_type']]=number_format($data['max_bid'],5);
        return $bid;
    }
    static public function get_top_impression_countries($unique,$type)
    {
        if($type==1) {
            $sql = mysql_query("SELECT COUNT(*) AS nr ,country FROM `app_count_imp` WHERE `date`='" . date('Y-m-d') . "' AND `unique_advertiser`='".$unique."' GROUP BY `country` ORDER BY nr DESC LIMIT 3");
            while ($data = mysql_fetch_array($sql))
                $bid[$data['country']] = $data['nr'];
        }else
        {
            $sql = mysql_query("SELECT COUNT(*) AS nr ,country FROM `app_count_imp` WHERE `date`='" . date('Y-m-d') . "' AND `unique_publisher`='".$unique."' GROUP BY `country` ORDER BY nr DESC LIMIT 3");
            while ($data = mysql_fetch_array($sql))
                $bid[$data['country']] = $data['nr'];
        }
        return $bid;
    }
    static public function get_today_impressions($unique,$type)
    {
        if($type==1)
        {
            $sql=mysql_query("SELECT COUNT(*) AS nr FROM `app_count_imp` WHERE `date`='".date('Y-m-d')."' AND `unique_advertiser`='".$unique."'");
            $data=mysql_fetch_array($sql);
            $today_impressions=$data['nr'];
        }else{
            $sql=mysql_query("SELECT COUNT(*) AS nr FROM `app_count_imp` WHERE `date`='".date('Y-m-d')."' AND `unique_publisher`='".$unique."'");
            $data=mysql_fetch_array($sql);
            $today_impressions=$data['nr'];
        }
        /////
       // De azi din daily
        $sql=mysql_query("SELECT SUM(`impressions`) AS impressions FROM `app_daily_imp` WHERE `date`='".date('Y-m-d')."' AND `unique_user`='".$unique."' AND `user_type` = '".$type."'");
        $data=mysql_fetch_array($sql);
        $today_impressions=$today_impressions+$data['impressions'];
        /////
        $sql=mysql_query("SELECT SUM(`impressions`) AS impressions FROM `app_daily_imp` WHERE `date`='".date('Y-m-d',strtotime("yesterday"))."' AND `unique_user`='".$unique."' AND `user_type` = '".$type."'");
        $data=mysql_fetch_array($sql);
        $last_day=$data['impressions'];
        if($last_day!=0) {
            $procent = $today_impressions / $last_day * 100;
            if ($today_impressions < $last_day)
                $pro = '-' . $procent;
            else
                $pro = $procent;
        }elseif($today_impressions!=0)
            $pro=100;
        else
            $pro=0;
        ////
        /////
        $sql=mysql_query("SELECT SUM(`impressions`) AS impressions FROM `app_daily_imp` WHERE `date` BETWEEN '".date("Y-m-01")."' AND '".date('Y-m-d',strtotime("yesterday"))."' AND `unique_user`='".$unique."' AND `user_type` = '".$type."'");
        $data=mysql_fetch_array($sql);
        $last_mounth=$data['impressions']+$today_impressions;
        $return=array();
        $return[0]=$today_impressions;
		$pro = explode(".",$pro);
        $return[1]=$pro[0];
        $return[2]=$last_mounth;
        return $return;
    }
    static public function get_today_clicks($unique,$type)
    {
        if($type==1)
        {
            $sql=mysql_query("SELECT COUNT(*) AS nr FROM `app_count_imp` WHERE `date`='".date('Y-m-d')."' AND `unique_advertiser`='".$unique."' AND `click`='1'");
            $data=mysql_fetch_array($sql);
            $today_impressions=$data['nr'];
        }else{
            $sql=mysql_query("SELECT COUNT(*) AS nr FROM `app_count_imp` WHERE `date`='".date('Y-m-d')."' AND `unique_publisher`='".$unique."' AND `click`='1'");
            $data=mysql_fetch_array($sql);
            $today_impressions=$data['nr'];
        }
        //
        $sql=mysql_query("SELECT SUM(`clicks`) AS clicks FROM `app_daily_imp` WHERE `date`='".date('Y-m-d')."' AND `unique_user`='".$unique."' AND `user_type` = '".$type."'");
        $data=mysql_fetch_array($sql);
        $today_impressions=$today_impressions+$data['clicks'];
        //
        $sql=mysql_query("SELECT SUM(`clicks`) AS clicks FROM `app_daily_imp` WHERE `date`='".date('Y-m-d',strtotime("yesterday"))."' AND `unique_user`='".$unique."' AND `user_type` = '".$type."'");
        $data=mysql_fetch_array($sql);
        $last_day=$data['clicks'];
        if($last_day!=0) {
            $procent = $today_impressions / $last_day * 100;
            if ($today_impressions < $last_day)
                $pro = '-' . $procent;
            else
                $pro = $procent;
        }elseif($today_impressions!=0)
            $pro=100;
        else
            $pro=0;
        $sql=mysql_query("SELECT SUM(`clicks`) AS clicks FROM `app_daily_imp` WHERE `date` BETWEEN '".date("Y-m-01")."' AND '".date('Y-m-d',strtotime("yesterday"))."' AND `unique_user`='".$unique."' AND `user_type` = '".$type."'");
        $data=mysql_fetch_array($sql);
        $last_mounth=$data['clicks']+$today_impressions;
        $return=array();
        $return[0]=$today_impressions;
		$pro = explode(".",$pro);
        $return[1]=$pro[0];
        $return[2]=$last_mounth;
        return $return;
    }
    static public function get_today_money($unique,$type)
    {
        if($type==1)
        {
            $sql=mysql_query("SELECT COALESCE(SUM(`outcome`),0) AS amount FROM `app_count_imp` WHERE `date`='".date('Y-m-d')."' AND `unique_advertiser`='".$unique."'");
            $data=mysql_fetch_array($sql);
            $today_impressions=$data['amount'];
        }else{
            $sql=mysql_query("SELECT COALESCE(SUM(`income`),0) AS amount FROM `app_count_imp` WHERE `date`='".date('Y-m-d')."' AND `unique_publisher`='".$unique."'");
            $data=mysql_fetch_array($sql);
            $today_impressions=$data['amount'];
        }
        //
        $sql=mysql_query("SELECT SUM(`money`) AS money FROM `app_daily_imp` WHERE `date`='".date('Y-m-d')."' AND `unique_user`='".$unique."' AND `user_type` = '".$type."'");
        $data=mysql_fetch_array($sql);
        $today_impressions=$today_impressions+$data['money'];
        //
        $sql=mysql_query("SELECT SUM(`money`) AS money FROM `app_daily_imp` WHERE `date`='".date('Y-m-d',strtotime("yesterday"))."' AND `unique_user`='".$unique."' AND `user_type` = '".$type."'");
        $data=mysql_fetch_array($sql);
        $last_day=$data['money'];
        if($last_day!=0) {
            $procent = $today_impressions / $last_day * 100;
            if ($today_impressions < $last_day)
                $pro = '-' . $procent;
            else
                $pro = $procent;
        }elseif($today_impressions!=0)
            $pro=100;
        else
            $pro=0;
        $sql=mysql_query("SELECT SUM(`money`) AS money FROM `app_daily_imp` WHERE `date` BETWEEN '".date("Y-m-01")."' AND '".date('Y-m-d',strtotime("yesterday"))."' AND `unique_user`='".$unique."' AND `user_type` = '".$type."'");
        $data=mysql_fetch_array($sql);
        $last_mounth=$data['money']+$today_impressions;
        $return=array();
        $return[0]=number_format($today_impressions,5);
		$pro = explode(".",$pro);
        $return[1]= $pro[0];
        $return[2]=number_format($last_mounth,5);
        return $return;
    }
}
