Created
September 2, 2025 23:45
-
-
Save uchoamaster/c7f4b3c653c9068b2a3688760b0229f1 to your computer and use it in GitHub Desktop.
Sistema de gestão de estoque de produtos
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public function totalProdutos() { | |
$sql = "SELECT COUNT(*) as total FROM {$this->tabela}"; | |
return $this->conn->query($sql)->fetch(PDO::FETCH_ASSOC)['total']; | |
} | |
public function totalQuantidade() { | |
$sql = "SELECT SUM(quantidade) as total FROM {$this->tabela}"; | |
return $this->conn->query($sql)->fetch(PDO::FETCH_ASSOC)['total']; | |
} | |
public function valorTotalEstoque() { | |
$sql = "SELECT SUM(quantidade * preco) as total FROM {$this->tabela}"; | |
return $this->conn->query($sql)->fetch(PDO::FETCH_ASSOC)['total']; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
require_once '../classes/Produto.php'; | |
if ($_POST) { | |
$produto = new Produto(); | |
$produto->adicionar($_POST['nome'], $_POST['descricao'], $_POST['quantidade'], $_POST['preco']); | |
header("Location: index.php"); | |
} | |
?> | |
<h2>Adicionar Peça</h2> | |
<form method="post"> | |
Nome: <input type="text" name="nome"><br> | |
Descrição: <textarea name="descricao"></textarea><br> | |
Quantidade: <input type="number" name="quantidade"><br> | |
Preço: <input type="text" name="preco"><br> | |
<button type="submit">Salvar</button> | |
</form> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class Database { | |
private $host = "localhost"; | |
private $db = "estoque_pecas"; | |
private $user = "root"; | |
private $pass = ""; | |
public $conn; | |
public function conectar() { | |
$this->conn = null; | |
try { | |
$this->conn = new PDO("mysql:host={$this->host};dbname={$this->db}", | |
$this->user, | |
$this->pass); | |
$this->conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); | |
} catch(PDOException $e) { | |
die("Erro na conexão: " . $e->getMessage()); | |
} | |
return $this->conn; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
require_once '../classes/Produto.php'; | |
$produto = new Produto(); | |
if ($_GET['id']) { | |
$item = $produto->buscar($_GET['id']); | |
} | |
if ($_POST) { | |
$produto->atualizar($_POST['id'], $_POST['nome'], $_POST['descricao'], $_POST['quantidade'], $_POST['preco']); | |
header("Location: index.php"); | |
} | |
?> | |
<h2>Editar Peça</h2> | |
<form method="post"> | |
<input type="hidden" name="id" value="<?= $item['id'] ?>"> | |
Nome: <input type="text" name="nome" value="<?= $item['nome'] ?>"><br> | |
Descrição: <textarea name="descricao"><?= $item['descricao'] ?></textarea><br> | |
Quantidade: <input type="number" name="quantidade" value="<?= $item['quantidade'] ?>"><br> | |
Preço: <input type="text" name="preco" value="<?= $item['preco'] ?>"><br> | |
<button type="submit">Atualizar</button> | |
</form> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
require_once '../classes/Produto.php'; | |
if ($_GET['id']) { | |
$produto = new Produto(); | |
$produto->excluir($_GET['id']); | |
} | |
header("Location: index.php"); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
require_once '../classes/Produto.php'; | |
$produto = new Produto(); | |
$produtos = $produto->listar(); | |
?> | |
<h2>Estoque de Peças</h2> | |
<a href="adicionar.php">Adicionar Nova Peça</a> | |
<table border="1" cellpadding="5"> | |
<tr> | |
<th>ID</th><th>Nome</th><th>Descrição</th><th>Qtd</th><th>Preço</th><th>Ações</th> | |
</tr> | |
<?php foreach ($produtos as $p): ?> | |
<tr> | |
<td><?= $p['id'] ?></td> | |
<td><?= $p['nome'] ?></td> | |
<td><?= $p['descricao'] ?></td> | |
<td><?= $p['quantidade'] ?></td> | |
<td>R$ <?= number_format($p['preco'], 2, ',', '.') ?></td> | |
<td> | |
<a href="editar.php?id=<?= $p['id'] ?>">Editar</a> | | |
<a href="excluir.php?id=<?= $p['id'] ?>" onclick="return confirm('Tem certeza?')">Excluir</a> | |
</td> | |
</tr> | |
<?php endforeach; ?> | |
</table> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
require_once '../classes/Produto.php'; | |
$produto = new Produto(); | |
$produtos = $produto->listar(); | |
// Dados para os cards | |
$totalProdutos = $produto->totalProdutos(); | |
$totalQuantidade = $produto->totalQuantidade(); | |
$valorTotal = $produto->valorTotalEstoque(); | |
?> | |
<h2>Estoque de Peças</h2> | |
<!-- Cards --> | |
<div style="display: flex; gap: 20px; margin-bottom: 20px;"> | |
<div style="background: #f2f2f2; padding: 15px; border-radius: 5px;"> | |
<strong>Total de Peças Cadastradas:</strong><br> | |
<?= $totalProdutos ?> | |
</div> | |
<div style="background: #e6f7ff; padding: 15px; border-radius: 5px;"> | |
<strong>Quantidade Total em Estoque:</strong><br> | |
<?= $totalQuantidade ?> | |
</div> | |
<div style="background: #fff3cd; padding: 15px; border-radius: 5px;"> | |
<strong>Valor Total do Estoque:</strong><br> | |
R$ <?= number_format($valorTotal, 2, ',', '.') ?> | |
</div> | |
</div> | |
<a href="adicionar.php">Adicionar Nova Peça</a> | |
<table border="1" cellpadding="5"> | |
<tr> | |
<th>ID</th><th>Nome</th><th>Descrição</th><th>Qtd</th><th>Preço</th><th>Ações</th> | |
</tr> | |
<?php foreach ($produtos as $p): ?> | |
<tr> | |
<td><?= $p['id'] ?></td> | |
<td><?= $p['nome'] ?></td> | |
<td><?= $p['descricao'] ?></td> | |
<td><?= $p['quantidade'] ?></td> | |
<td>R$ <?= number_format($p['preco'], 2, ',', '.') ?></td> | |
<td> | |
<a href="editar.php?id=<?= $p['id'] ?>">Editar</a> | | |
<a href="excluir.php?id=<?= $p['id'] ?>" onclick="return confirm('Tem certeza?')">Excluir</a> | |
</td> | |
</tr> | |
<?php endforeach; ?> | |
</table> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
require_once __DIR__ . '/../config/Database.php'; | |
class Produto { | |
private $conn; | |
private $tabela = 'produtos'; | |
public function __construct() { | |
$db = new Database(); | |
$this->conn = $db->conectar(); | |
} | |
public function listar() { | |
$sql = "SELECT * FROM {$this->tabela}"; | |
$stmt = $this->conn->prepare($sql); | |
$stmt->execute(); | |
return $stmt->fetchAll(PDO::FETCH_ASSOC); | |
} | |
public function adicionar($nome, $descricao, $quantidade, $preco) { | |
$sql = "INSERT INTO {$this->tabela} (nome, descricao, quantidade, preco) | |
VALUES (:nome, :descricao, :quantidade, :preco)"; | |
$stmt = $this->conn->prepare($sql); | |
return $stmt->execute([ | |
':nome' => $nome, | |
':descricao' => $descricao, | |
':quantidade' => $quantidade, | |
':preco' => $preco | |
]); | |
} | |
public function buscar($id) { | |
$sql = "SELECT * FROM {$this->tabela} WHERE id = :id"; | |
$stmt = $this->conn->prepare($sql); | |
$stmt->execute([':id' => $id]); | |
return $stmt->fetch(PDO::FETCH_ASSOC); | |
} | |
public function atualizar($id, $nome, $descricao, $quantidade, $preco) { | |
$sql = "UPDATE {$this->tabela} | |
SET nome = :nome, descricao = :descricao, quantidade = :quantidade, preco = :preco | |
WHERE id = :id"; | |
$stmt = $this->conn->prepare($sql); | |
return $stmt->execute([ | |
':nome' => $nome, | |
':descricao' => $descricao, | |
':quantidade' => $quantidade, | |
':preco' => $preco, | |
':id' => $id | |
]); | |
} | |
public function excluir($id) { | |
$sql = "DELETE FROM {$this->tabela} WHERE id = :id"; | |
$stmt = $this->conn->prepare($sql); | |
return $stmt->execute([':id' => $id]); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
CREATE DATABASE estoque_pecas; | |
USE estoque_pecas; | |
CREATE TABLE produtos ( | |
id INT AUTO_INCREMENT PRIMARY KEY, | |
nome VARCHAR(100) NOT NULL, | |
descricao TEXT, | |
quantidade INT NOT NULL, | |
preco DECIMAL(10,2) NOT NULL | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment