<?php
include 'dbc.php';

class userDAO {

    function __construct() {
        
    }

    function __destruct() {
        //$this->banco = NULL;
    }

    public function __set($campo, $valor) {
        $this->$campo = $valor;
    }

    public function __get($campo) {
        return $this->$campo;
    }

    static function salvar($data, $table, $dbk = NULL) {
        try {
            $fields = $data;

            $tmp = array();
            foreach ($fields as $k => $v) {
                if ($k != $dbk) {
                    $tmp[] = $k . ' = :' . $k;
                }
            }
            if (isset($dbk)) {
                $fields['id'] = $dbk;
                $SQL = 'UPDATE ' . $table . ' SET ' . implode(', ', $tmp) . ' WHERE (id=:id)';
            } else {
                $SQL = 'INSERT INTO ' . $table . ' SET ' . implode(', ', $tmp);
            }
            $db = new DB();
            $id = $db->InsertData($SQL, $fields);
            return $id;
        } catch (Exception $ex) {
            throw new Exception($ex->getMessage() . ': ' . __FUNCTION__);
        }
    }

    static function getCobrancasID($cliente_edit) {
        try {
            $sql = " "
                    //. "SELECT id_cobranca FROM cobranca_ponto "
                   // . " WHERE id_cobranca_locacao IN "
                    . "  (SELECT a.id_cobranca"
                    . "   FROM cobranca_locacao a "
                    . "   INNER JOIN cobranca_agregado b on b.id_cobranca = a.id_cobranca_agregado "
                    . "   LEFT JOIN user c ON a.id_cliente = c.id "
                    . "   LEFT JOIN regiao d on d.id_pais = c.id_pais "
                    . "   WHERE a.pago = 0 "
                    . "   AND a.ativo = 1 "
                   // . "   AND a.cobranca_fechada = 0"
                    . "   AND a.lote_fechamento_hash IS NULL"
                    . "   AND c.id = ? )"
                    . " ";
            $db = new DB();
            $rows = $db->executeReturnFetch($sql, array($cliente_edit), true);
            foreach ($rows as $row) {
                $array[] = $row->id_cobranca;
            }
            return $array;
        } catch (Exception $err) {
            throw new Exception($err->getMessage() . ': ' . __FUNCTION__);
        }
    }



    static function getUser($id) {
        try {
            $sql = "SELECT * FROM user WHERE id=?";
            $db = new DB();
            return $db->executeReturnFetch($sql, array($id));
        } catch (Exception $err) {
            throw new Exception($err->getMessage() . ': ' . __FUNCTION__);
        }
    }
    
    static function getidApoio($nome) {
        try {
            $sql = "SELECT id_apoio FROM tablet "
                . " WHERE cliente = ? ";
            $db = new DB();
            return $db->executeReturn_FETCH_ASSOC($sql, array($nome));
        } catch (Exception $err) {
            throw new Exception($err->getMessage() . ': ' . __FUNCTION__);
        }
    }

    public static function deletarUsuario($id) {
        try {
            $sql = 'DELETE FROM usuario'
                    . ' WHERE id =' . $id;
            $db = new DB();
            $db->query($sql);
        } catch (Exception $err) {
            throw new Exception($err->getMessage() . ': ' . __CLASS__ . '*' . __FUNCTION__);
        }
    }

}
