-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmeta.php
219 lines (201 loc) · 7.12 KB
/
meta.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
<?php
// Ativar exibição de erros para debug e registro em log
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
session_start();
if (!isset($_SESSION['logado'])) {
header('Location: index.php');
exit;
}
include('db_connection.php');
// Obter configuração da moeda
$sql_moeda = "SELECT valor FROM configuracoes WHERE chave = 'moeda'";
$result_moeda = $mysqli->query($sql_moeda);
$moeda = $result_moeda->fetch_assoc()['valor'];
// Definir o número de registros por página
$registros_por_pagina = 10;
$pagina_atual = isset($_GET['pagina']) ? intval($_GET['pagina']) : 1;
$offset = ($pagina_atual - 1) * $registros_por_pagina;
// Adicionar meta
if (isset($_POST['descricao']) && isset($_POST['valor']) && isset($_POST['data'])) {
$descricao = $_POST['descricao'];
$valor = str_replace(',', '.', $_POST['valor']); // Substituir vírgula por ponto para valores decimais
$data = $_POST['data'];
$hora = date('H:i:s');
$sql = "INSERT INTO metas (descricao, valor, data, hora) VALUES ('$descricao', '$valor', '$data', '$hora')";
if (!$mysqli->query($sql)) {
$error_message = "Erro na inserção: " . $mysqli->error;
error_log($error_message, 3, $log_file);
die($error_message);
}
echo "Meta registrada com sucesso.";
}
// Atualizar meta
if (isset($_POST['update_id'])) {
$update_id = $_POST['update_id'];
$update_descricao = $_POST['update_descricao'];
$update_valor = str_replace(',', '.', $_POST['update_valor']); // Substituir vírgula por ponto para valores decimais
$update_data = $_POST['update_data'];
$sql = "UPDATE metas SET descricao='$update_descricao', valor='$update_valor', data='$update_data' WHERE id='$update_id'";
if (!$mysqli->query($sql)) {
$error_message = "Erro na atualização: " . $mysqli->error;
error_log($error_message, 3, $log_file);
die($error_message);
}
echo "Meta atualizada com sucesso.";
}
// Excluir meta
if (isset($_POST['delete_id'])) {
$delete_id = $_POST['delete_id'];
$sql = "DELETE FROM metas WHERE id='$delete_id'";
if (!$mysqli->query($sql)) {
$error_message = "Erro ao excluir: " . $mysqli->error;
error_log($error_message, 3, $log_file);
die($error_message);
}
echo "Meta excluída com sucesso.";
}
$mes_atual = date('Y-m');
$sql_total = "SELECT COUNT(*) AS total FROM metas WHERE DATE_FORMAT(data, '%Y-%m') = '$mes_atual'";
$result_total = $mysqli->query($sql_total);
$total_registros = $result_total->fetch_assoc()['total'];
$total_paginas = ceil($total_registros / $registros_por_pagina);
$sql = "SELECT id, descricao, valor, data, hora FROM metas WHERE DATE_FORMAT(data, '%Y-%m') = '$mes_atual' ORDER BY data DESC, hora DESC LIMIT $offset, $registros_por_pagina";
$result = $mysqli->query($sql);
if (!$result) {
$error_message = "Erro na consulta: " . $mysqli->error;
error_log($error_message, 3, $log_file);
die($error_message);
}
function formatar_valor($valor, $moeda) {
if ($moeda == 'JPY') {
return number_format($valor, 0) . " " . $moeda;
} else {
return number_format($valor, 2, ',', '.') . " " . $moeda;
}
}
?>
<!DOCTYPE html>
<html lang="pt-BR">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Metas</title>
<style>
body {
background: linear-gradient(to right, #87CEFA, #90EE90);
font-family: Arial, sans-serif;
text-align: center;
color: #333;
}
h1 {
font-size: 2.5em;
}
form {
display: inline-block;
margin-top: 20px;
}
label, input, button {
font-size: 1.2em;
}
button {
padding: 10px 20px;
background-color: #007BFF;
color: #fff;
border: none;
border-radius: 5px;
cursor: pointer;
}
button:hover {
background-color: #0056b3;
}
table {
width: 80%;
margin: 20px auto;
border-collapse: collapse;
}
th, td {
padding: 10px;
border: 1px solid #ddd;
text-align: center;
}
th {
background-color: #f2f2f2;
}
.back-button {
margin-top: 20px;
display: inline-block;
}
.pagination {
margin: 20px 0;
}
.pagination a {
margin: 0 5px;
padding: 10px 15px;
background-color: #007BFF;
color: #fff;
text-decoration: none;
border-radius: 5px;
}
.pagination a:hover {
background-color: #0056b3;
}
</style>
</head>
<body>
<h1>Metas</h1>
<form method="post" action="">
<label for="descricao">Descrição:</label>
<input type="text" name="descricao" id="descricao"><br>
<label for="valor">Valor:</label>
<input type="text" name="valor" id="valor"><br>
<label for="data">Data:</label>
<input type="date" name="data" id="data"><br>
<button type="submit">Adicionar</button>
</form>
<h2>Histórico de Metas</h2>
<table>
<thead>
<tr>
<th>Descrição</th>
<th>Valor</th>
<th>Data</th>
<th>Hora</th>
<th>Ações</th>
</tr>
</thead>
<tbody>
<?php while ($row = $result->fetch_assoc()): ?>
<tr>
<td><?php echo $row['descricao']; ?></td>
<td><?php echo formatar_valor($row['valor'], $moeda); ?></td>
<td><?php echo $row['data']; ?></td>
<td><?php echo $row['hora']; ?></td>
<td>
<form method="post" action="" style="display:inline;">
<input type="hidden" name="update_id" value="<?php echo $row['id']; ?>">
<input type="hidden" name="update_descricao" value="<?php echo $row['descricao']; ?>">
<input type="hidden" name="update_valor" value="<?php echo $row['valor']; ?>">
<input type="hidden" name="update_data" value="<?php echo $row['data']; ?>">
<button type="submit">Editar</button>
</form>
<form method="post" action="" style="display:inline;">
<input type="hidden" name="delete_id" value="<?php echo $row['id']; ?>">
<button type="submit">Excluir</button>
</form>
</td>
</tr>
<?php endwhile; ?>
</tbody>
</table>
<div class="pagination">
<?php for ($i = 1; $i <= $total_paginas; $i++): ?>
<a href="?pagina=<?php echo $i; ?>"><?php echo $i; ?></a>
<?php endfor; ?>
</div>
<div class="back-button">
<a href="dashboard.php" class="button">Voltar ao Dashboard</a>
</div>
</body>
</html>