Advertisement
Guest User

Untitled

a guest
Jan 25th, 2019
149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.33 KB | None | 0 0
  1. <?php
  2.  
  3.     //classe dashboard
  4.     class Dashboard {
  5.  
  6.         private $data_inicio;
  7.         private $data_fim;
  8.         private $numeroVendas;
  9.         private $totalVendas;
  10.  
  11.         public function __get($atributo){
  12.             return $this->$atributo;
  13.         }
  14.  
  15.  
  16.         public function __set($atributo, $valor){
  17.             $this->$atributo = $valor;
  18.             return $this;
  19.         }
  20.  
  21.     }
  22.  
  23.  
  24.     class Conexao {
  25.  
  26.         private $host = 'localhost';
  27.         private $dbname = 'dashboard';
  28.         private $user = 'root';
  29.         private $pass = '';
  30.  
  31.         public function conectar(){
  32.             try{
  33.                 $conexao = new PDO(
  34.                     "mysql:host=$this->host;dbname=$this->dbname",
  35.                     "$this->user",
  36.                     "$this->pass"
  37.                 );
  38.  
  39.                 //
  40.                 $conexao->exec('set charset set utf8');
  41.  
  42.                 return $conexao;
  43.  
  44.  
  45.             } catch (PDOException $e) {
  46.                 echo '<p>Problema na conexao com a base de dados: ' . $e->getMessage() . '</p>';
  47.             }
  48.         }
  49.     }
  50.  
  51.     class Bd {
  52.         private $conexao;
  53.         private $dashboard;
  54.  
  55.         public function __construct(Conexao $conexao, Dashboard $dashboard){
  56.             $this->conexao = $conexao->conectar();
  57.             $this->dashboard = $dashboard;
  58.         }
  59.  
  60.         public function getNumeroVendas() {
  61.  
  62.             $query = '
  63.                select
  64.                    count(*) as numero_vendas
  65.                from
  66.                    tb_vendas
  67.                where
  68.                    data_venda BETWEEN :data_inicio AND :data_fim';
  69.  
  70.             $stmt = $this->conexao->prepare($query);
  71.             $stmt->bindValue(':data_inicio', $this->dashboard->__get('data_inicio'));
  72.             $stmt->bindValue(':data_fim', $this->dashboard->__get('data_fim'));
  73.             $stmt->execute();
  74.  
  75.             return $stmt->fetch(PDO::FETCH_OBJ)->numero_vendas;
  76.         }
  77.  
  78.         public function getTotalVendas() {
  79.  
  80.             $query = '
  81.                select
  82.                    sum(total) as total_vendas
  83.                from
  84.                    tb_vendas
  85.                where
  86.                    data_venda BETWEEN :data_inicio AND :data_fim';
  87.  
  88.             $stmt = $this->conexao->prepare($query);
  89.             $stmt->bindValue(':data_inicio', $this->dashboard->__get('data_inicio'));
  90.             $stmt->bindValue(':data_fim', $this->dashboard->__get('data_fim'));
  91.             $stmt->execute();
  92.  
  93.             return $stmt->fetch(PDO::FETCH_OBJ)->total_vendas;
  94.         }
  95.     }
  96.  
  97.     $dashboard = new Dashboard();
  98.     $conexao = new Conexao();
  99.  
  100.     $competencia = explode('-', $_GET['competencia']);
  101.     $ano = $competencia[0];
  102.     $mes = $competencia[1];
  103.     $dias_do_mes = cal_days_in_month(CAL_GREGORIAN, $mes, $ano);
  104.  
  105.  
  106.     $dashboard->__set('data_inicio', '2017-08-02');
  107.     $dashboard->__set('data_fim', '2018-10-31');
  108.  
  109.     //como é que ele consegue ir buscar a info da data sendo que o atributo está private???
  110.    
  111.  
  112.     echo '<br>';
  113.     echo $dashboard->data_inicio;
  114.     echo '</br>';
  115.    
  116.  
  117.  
  118.     $bd = new Bd($conexao, $dashboard);
  119.  
  120.     $dashboard->__set('numeroVendas', $bd->getNumeroVendas());
  121.     $dashboard->__set('totalVendas', $bd->getTotalVendas());
  122.  
  123.     print_r($ano.'/'.$mes.'/'.$dias_do_mes);
  124.  
  125.  
  126. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement