186 lines
5.7 KiB
PHP
186 lines
5.7 KiB
PHP
|
|
<?php
|
|
session_start();
|
|
if (!isset($_SESSION['user_id'])) {
|
|
header('Location: login.php');
|
|
exit;
|
|
}
|
|
require_once 'db/config.php';
|
|
include 'includes/header.php';
|
|
include 'includes/sidebar.php';
|
|
|
|
$product_id = $_GET['product_id'] ?? null;
|
|
$error = '';
|
|
$success = '';
|
|
|
|
if (!$product_id) {
|
|
header('Location: inventory.php');
|
|
exit;
|
|
}
|
|
|
|
// Fetch product details
|
|
try {
|
|
$pdo = db();
|
|
$stmt = $pdo->prepare('SELECT * FROM products WHERE id = ?');
|
|
$stmt->execute([$product_id]);
|
|
$product = $stmt->fetch();
|
|
} catch (PDOException $e) {
|
|
$error = "Erro ao buscar produto: " . $e->getMessage();
|
|
}
|
|
|
|
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
|
|
$type = $_POST['type'] ?? '';
|
|
$quantity = $_POST['quantity'] ?? 0;
|
|
$notes = $_POST['notes'] ?? '';
|
|
|
|
if (empty($type) || empty($quantity) || $quantity <= 0) {
|
|
$error = 'Por favor, preencha o tipo e a quantidade (maior que zero).';
|
|
} else {
|
|
try {
|
|
$pdo->beginTransaction();
|
|
|
|
// Insert into stock_movements
|
|
$stmt = $pdo->prepare('INSERT INTO stock_movements (product_id, type, quantity, user_id, notes) VALUES (?, ?, ?, ?, ?)');
|
|
$stmt->execute([$product_id, $type, $quantity, $_SESSION['user_id'], $notes]);
|
|
|
|
// Update product quantity
|
|
if ($type == 'entrada') {
|
|
$update_sql = 'UPDATE products SET quantity = quantity + ? WHERE id = ?';
|
|
} else {
|
|
$update_sql = 'UPDATE products SET quantity = quantity - ? WHERE id = ?';
|
|
}
|
|
$stmt = $pdo->prepare($update_sql);
|
|
$stmt->execute([$quantity, $product_id]);
|
|
|
|
$pdo->commit();
|
|
$success = 'Movimentação de estoque registrada com sucesso!';
|
|
|
|
// Refresh product data
|
|
$stmt = $pdo->prepare('SELECT * FROM products WHERE id = ?');
|
|
$stmt->execute([$product_id]);
|
|
$product = $stmt->fetch();
|
|
|
|
} catch (PDOException $e) {
|
|
$pdo->rollBack();
|
|
$error = 'Erro ao registrar movimentação: ' . $e->getMessage();
|
|
}
|
|
}
|
|
}
|
|
|
|
// Fetch stock movements for this product
|
|
try {
|
|
$stmt = $pdo->prepare('SELECT sm.*, u.name as user_name FROM stock_movements sm JOIN users u ON sm.user_id = u.id WHERE sm.product_id = ? ORDER BY sm.date DESC');
|
|
$stmt->execute([$product_id]);
|
|
$movements = $stmt->fetchAll();
|
|
} catch (PDOException $e) {
|
|
$movements = [];
|
|
$error = "Erro ao buscar movimentações: " . $e->getMessage();
|
|
}
|
|
|
|
?>
|
|
|
|
<header class="top-bar">
|
|
<h1>Movimentar Estoque: <?php echo htmlspecialchars($product['name'] ?? 'Produto não encontrado'); ?></h1>
|
|
</header>
|
|
|
|
<main class="content">
|
|
<div class="card">
|
|
<div class="card-header">
|
|
Registrar Entrada/Saída
|
|
</div>
|
|
|
|
<p><b>Estoque Atual:</b> <?php echo htmlspecialchars($product['quantity'] . ' ' . $product['unit']); ?></p>
|
|
|
|
<?php if ($error): ?><p class="error"><?php echo $error; ?></p><?php endif; ?>
|
|
<?php if ($success): ?><p class="success"><?php echo $success; ?></p><?php endif; ?>
|
|
|
|
<form action="stock.php?product_id=<?php echo $product_id; ?>" method="POST">
|
|
<div class="form-group">
|
|
<label for="type">Tipo de Movimentação</label>
|
|
<select id="type" name="type" required>
|
|
<option value="entrada">Entrada</option>
|
|
<option value="saída">Saída</option>
|
|
</select>
|
|
</div>
|
|
<div class="form-group">
|
|
<label for="quantity">Quantidade</label>
|
|
<input type="number" step="0.01" id="quantity" name="quantity" required>
|
|
</div>
|
|
<div class="form-group">
|
|
<label for="notes">Notas</label>
|
|
<textarea id="notes" name="notes" rows="3"></textarea>
|
|
</div>
|
|
<button type="submit" class="btn">Registrar</button>
|
|
</form>
|
|
<br>
|
|
<a href="inventory.php">Voltar para o Estoque</a>
|
|
</div>
|
|
|
|
<div class="card">
|
|
<div class="card-header">
|
|
Histórico de Movimentações
|
|
</div>
|
|
<table>
|
|
<thead>
|
|
<tr>
|
|
<th>Data</th>
|
|
<th>Tipo</th>
|
|
<th>Quantidade</th>
|
|
<th>Usuário</th>
|
|
<th>Notas</th>
|
|
</tr>
|
|
</thead>
|
|
<tbody>
|
|
<?php foreach ($movements as $movement): ?>
|
|
<tr>
|
|
<td><?php echo htmlspecialchars(date('d/m/Y H:i', strtotime($movement['date']))); ?></td>
|
|
<td><?php echo htmlspecialchars(ucfirst($movement['type'])); ?></td>
|
|
<td><?php echo htmlspecialchars($movement['quantity']); ?></td>
|
|
<td><?php echo htmlspecialchars($movement['user_name']); ?></td>
|
|
<td><?php echo htmlspecialchars($movement['notes']); ?></td>
|
|
</tr>
|
|
<?php endforeach; ?>
|
|
</tbody>
|
|
</table>
|
|
</div>
|
|
</main>
|
|
|
|
<style>
|
|
.form-group, table {
|
|
margin-bottom: 15px;
|
|
}
|
|
.form-group label {
|
|
display: block;
|
|
margin-bottom: 5px;
|
|
}
|
|
.form-group input, .form-group select, .form-group textarea {
|
|
width: 100%;
|
|
padding: 8px;
|
|
box-sizing: border-box;
|
|
}
|
|
.btn {
|
|
background-color: #27ae60;
|
|
color: white;
|
|
padding: 10px 15px;
|
|
border: none;
|
|
border-radius: 4px;
|
|
cursor: pointer;
|
|
}
|
|
.error { color: red; }
|
|
.success { color: green; }
|
|
table {
|
|
width: 100%;
|
|
border-collapse: collapse;
|
|
}
|
|
th, td {
|
|
border: 1px solid #ddd;
|
|
padding: 8px;
|
|
text-align: left;
|
|
}
|
|
th {
|
|
background-color: #f2f2f2;
|
|
}
|
|
</style>
|
|
|
|
<?php include 'includes/footer.php'; ?>
|