<?php 



class DaoFornecedor extends conexao {

public function Atualizar(fornecedor $fornecedor) {

try {

if ($fornecedor->getFor_cd() != '') {

$sql = " UPDATE fornecedor SET
for_nm = ?,
for_fantasia = ?,
for_tipo = ?,
for_cpfcnpj = ?,
for_endereco = ?,
for_numero = ?,
for_bairro = ?,
for_cidade = ?,
for_uf = ?,
for_email = ?,
for_telefone = ?,
for_celular = ?,
for_contato = ?,
for_site = ?,
for_obs = ?,
for_cep = ?
WHERE for_cd = ? ";

$stmt = self::conectar()->prepare($sql);
$stmt->bindValue(1, $fornecedor->getFor_nm());
$stmt->bindValue(2, $fornecedor->getFor_fantasia());
$stmt->bindValue(3, $fornecedor->getFor_tipo());
$stmt->bindValue(4, $fornecedor->getFor_cpfcnpj());
$stmt->bindValue(5, $fornecedor->getFor_endereco());
$stmt->bindValue(6, $fornecedor->getFor_numero());
$stmt->bindValue(7, $fornecedor->getFor_bairro());
$stmt->bindValue(8, $fornecedor->getFor_cidade());
$stmt->bindValue(9, $fornecedor->getFor_uf());
$stmt->bindValue(10, $fornecedor->getFor_email());
$stmt->bindValue(11, $fornecedor->getFor_telefone());
$stmt->bindValue(12, $fornecedor->getFor_celular());
$stmt->bindValue(13, $fornecedor->getFor_contato());
$stmt->bindValue(14, $fornecedor->getFor_site());
$stmt->bindValue(15, $fornecedor->getFor_obs());
$stmt->bindValue(16, $fornecedor->getFor_cep());
$stmt->bindValue(17, $fornecedor->getFor_cd());


} else {

$sql = " INSERT INTO fornecedor (for_nm,for_fantasia,for_tipo,for_cpfcnpj,for_endereco,for_numero,for_bairro,for_cidade,for_uf,for_email,for_telefone,for_celular,for_contato,for_site,for_obs,for_cep)
VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";


$stmt = self::conectar()->prepare($sql);
$stmt->bindValue(1, $fornecedor->getFor_nm());
$stmt->bindValue(2, $fornecedor->getFor_fantasia());
$stmt->bindValue(3, $fornecedor->getFor_tipo());
$stmt->bindValue(4, $fornecedor->getFor_cpfcnpj());
$stmt->bindValue(5, $fornecedor->getFor_endereco());
$stmt->bindValue(6, $fornecedor->getFor_numero());
$stmt->bindValue(7, $fornecedor->getFor_bairro());
$stmt->bindValue(8, $fornecedor->getFor_cidade());
$stmt->bindValue(9, $fornecedor->getFor_uf());
$stmt->bindValue(10, $fornecedor->getFor_email());
$stmt->bindValue(11, $fornecedor->getFor_telefone());
$stmt->bindValue(12, $fornecedor->getFor_celular());
$stmt->bindValue(13, $fornecedor->getFor_contato());
$stmt->bindValue(14, $fornecedor->getFor_site());
$stmt->bindValue(15, $fornecedor->getFor_obs());
$stmt->bindValue(16, $fornecedor->getFor_cep());


}
if ($stmt->execute()){
return true;
} else
return false;

} catch (Exception $e) {
print "Erro ao tentar atualizar: ".$e;
}

}

public function PesquisarFornecedor($codigo){
$sql = " SELECT * FROM fornecedor WHERE for_cd = ? ";
$stmt = self::conectar()->prepare($sql);
$stmt->bindValue(1, $codigo);
$stmt->execute();
$result = $stmt->fetchAll(PDO::FETCH_ASSOC);
return $result;
}

public function ListarFornecedor(){
$sql = " SELECT * FROM fornecedor ORDER BY for_cd ";
$stmt = self::conectar()->prepare($sql);
$stmt->execute();
$result = $stmt->fetchAll(PDO::FETCH_ASSOC);
return $result;
}


public function Contar_qtd_fornecedores(){
$sql = " SELECT COUNT(for_cd) AS qtd_fornecedor FROM fornecedor";
$stmt = self::conectar()->prepare($sql);
$stmt->execute();
$result = $stmt->fetchAll(PDO::FETCH_ASSOC);
foreach($result as $row){
		return $row['qtd_fornecedor'];
	}
}



public function ProximoId(){
$sql = " SELECT COALESCE(MAX(for_cd), 0) + 1 AS PROXIMO_REGISTRO FROM fornecedor ";
$stmt = self::conectar()->prepare($sql);
$stmt->execute();
$result = $stmt->fetchAll(PDO::FETCH_ASSOC);
return $result;
}

	
public function deletar($for_cd){		
$sql = " DELETE FROM fornecedor WHERE for_cd = ? ";
$stmt = self::conectar()->prepare($sql);
$stmt->bindValue(1, $for_cd); 
$stmt->execute();
}

 
}
