<?php

class FC{

    public static function cards($tabela){

        $con=DB::conecta();
        $sql="SELECT COUNT(*) AS total FROM ".$tabela;
        $bs=$con->prepare($sql);
        $bs->execute();
        $row=$bs->fetchAll(PDO::FETCH_ASSOC);

        $con=null;
        return $row[0]['total'];
      

    }

    public static function dataUSmin($data){

      $arr=explode(" ", $data);
      $dataus=$arr[0];
      $hora=$arr[1];
      return $dataus;          
      

    }

    public static function dataBR($data){

        $arr2=explode("-", $data);        
       
        return $arr2[2]."/".$arr2[1]."/".$arr2[0];          
        
    }

    public static function dataBRhora($data){

        $arr=explode(" ", $data); 
        $arr2=explode("-", $arr[0]);      
        $hora=FC::horamin($arr[1]); 
       
        return $arr2[2]."/".$arr2[1]."/".$arr2[0]." ".$hora;          
        
    }

    public static function horamin($hora){

        $arr=explode(":", $hora); 
        //$hora=$arr[1];       
       
        return $arr[0].":".$arr[1];          
        
    }
    public static function dataUS($data){

        $arr2=explode("/", $data);        
       
        return $arr2[2]."-".$arr2[1]."-".$arr2[0];          
        
    }

    public static function vendasdia($usuario){

        $dia=FC::dataUSmin(date("Y-m-d H:i:s"));
        $con=DB::conecta();
        $sql="SELECT SUM(TOTAL) AS total FROM servicos WHERE VENDEDOR = '$usuario' AND Date(ABERTURA) = '$dia'";
        $bs=$con->prepare($sql);
       
        $bs->execute();
        $row=$bs->fetchAll(PDO::FETCH_ASSOC);

        //$con=null;
        
        if(count($row)==0){
            return "R$ ".number_format(0, 2, ',', '.');
        }else{
            return   "R$ ".number_format($row[0]['total'], 2, ',', '.');;
        }

             
        
    }

    public static function codcliente(){

        $con=DB::conecta();
        $sql="SELECT CODIGO FROM clientes order by CODIGO desc limit 1";
        $bs=$con->prepare($sql);
        $bs->execute();
        $row=$bs->fetchAll(PDO::FETCH_ASSOC);

        $con=null;
        if(count($row)==0){
            return 1;
        }else{
            return $row[0]['CODIGO']+1;
        }

    }

    public static function validacliente($nome, $email, $cnpj){     

        $con=DB::conecta();
        $sql="SELECT NOME,EMAIL,CGC FROM clientes WHERE NOME = '$nome' OR EMAIL = '$email' OR CGC = '$cnpj'";
        $bs=$con->prepare($sql);
        $bs->execute();
        $row=$bs->fetchAll(PDO::FETCH_ASSOC);

        $con=null;
        if(count($row)==0){
            return false;
        }else{
            return true;
        }

    }

    public static function bscliente($id){


        $con=DB::conecta();
        $sql="SELECT * FROM clientes where idcliente='$id'";
        $bs=$con->prepare($sql);
        $bs->execute();
        $row=$bs->fetchAll(PDO::FETCH_ASSOC);

        $con=null;
        if(count($row)==0){
            return null;
        }else{
            return $row;
        }


    }

    public static function bsproduto($id){


        $con=DB::conecta();
        $sql="SELECT * FROM produtos where idprod='$id'";
        $bs=$con->prepare($sql);
        $bs->execute();
        $row=$bs->fetchAll(PDO::FETCH_ASSOC);

        $con=null;
        if(count($row)==0){
            return null;
        }else{
            return $row;
        }


    }

    public static function cademail($nome,$email,$senha){


        $opt= substr(str_shuffle('0123456789'), 0, 5);
        $con=DB::conecta();
        $sql="UPDATE vendedores SET EMAIL='$email', OPT='$opt',SENHA='$senha' WHERE NOME='$nome'";
        $bs=$con->prepare($sql);
        $st=$bs->execute();
        if($st){
            $con=null;
            return $opt;
        }else{
            $con=null;
            return null;
        }
    }
     
    public static function validaopt($nome,$email,$opt){


        //$opt= substr(str_shuffle('0123456789'), 0, 5);
        $con=DB::conecta();
        $sql="SELECT * FROM vendedores WHERE NOME='$nome' AND EMAIL='$email' AND OPT='$opt'";
        $bs=$con->prepare($sql);
        $bs->execute();
        $row=$bs->fetchAll(PDO::FETCH_ASSOC);
        if(count($row)==0){
            return false;
        }else{
            return true;
        }
    }






}


   
