home
/
u529748449
/
domains
/
borabilhete.com
/
public_html
/
admin
➕ New
📤 Upload
✎ Editing:
taxas.php
← Back
<?php // admin/taxas.php require_once __DIR__ . '/conexao.php'; require_once __DIR__ . '/auth.php'; error_reporting(E_ALL); ini_set('display_errors', 1); date_default_timezone_set('America/Sao_Paulo'); require_login(['admin']); // SOMENTE ADMIN // --- Flash simples --- if (session_status() !== PHP_SESSION_ACTIVE) { session_start(); } $flash = $_SESSION['flash_taxas'] ?? []; unset($_SESSION['flash_taxas']); function add_flash($t,$m){ $_SESSION['flash_taxas'][] = ['t'=>$t,'m'=>$m]; } // --- CSRF --- if (empty($_SESSION['csrf_taxas'])) { $_SESSION['csrf_taxas'] = bin2hex(random_bytes(16)); } $CSRF = $_SESSION['csrf_taxas']; // Métodos permitidos p/ repasse $METODOS = [ 'pix' => ['label' => 'PIX'], 'credito' => ['label' => 'Crédito'], 'debito' => ['label' => 'Débito'], ]; // --- POST --- if ($_SERVER['REQUEST_METHOD'] === 'POST') { $okCsrf = isset($_POST['csrf']) && hash_equals($_SESSION['csrf_taxas'], (string)$_POST['csrf']); if (!$okCsrf) { add_flash('error','Falha de CSRF. Recarregue a página.'); header('Location: taxas.php'); exit; } $id = isset($_POST['id']) && ctype_digit($_POST['id']) ? (int)$_POST['id'] : 0; if ($id <= 0) { add_flash('error','ID inválido.'); header('Location: taxas.php'); exit; } $action = $_POST['action'] ?? ''; // 3.1) Limpar taxa de comprador (volta ao 9%) if ($action === 'clear_buyer') { $st = $conn->prepare("UPDATE eventos SET taxa_tipo=NULL, taxa_percentual=NULL, taxa_valor=NULL WHERE id=?"); $st->bind_param('i', $id); $ok = $st->execute(); $st->close(); add_flash($ok?'ok':'error', $ok?'Taxa do comprador redefinida para padrão (9%).':'Erro ao redefinir.'); header('Location: taxas.php'); exit; } // 3.2) Salvar taxa do comprador if ($action === 'save_buyer') { $tipo = $_POST['taxa_tipo'] ?? ''; if (!in_array($tipo, ['percentual','fixa'], true)) { add_flash('error','Tipo de taxa inválido.'); header('Location: taxas.php'); exit; } if ($tipo === 'percentual') { $perc = str_replace(',','.', trim($_POST['taxa_percentual'] ?? '')); if ($perc === '' || !is_numeric($perc)) { add_flash('error','Percentual inválido.'); header('Location: taxas.php'); exit; } $perc = (float)$perc; if ($perc < 0 || $perc > 100) { add_flash('error','Percentual deve estar entre 0 e 100.'); header('Location: taxas.php'); exit; } $st = $conn->prepare("UPDATE eventos SET taxa_tipo='percentual', taxa_percentual=?, taxa_valor=NULL WHERE id=?"); $st->bind_param('di', $perc, $id); } else { $val = str_replace(',','.', trim($_POST['taxa_valor'] ?? '')); if ($val === '' || !is_numeric($val)) { add_flash('error','Valor fixo inválido.'); header('Location: taxas.php'); exit; } $val = (float)$val; if ($val < 0) { add_flash('error','Valor fixo deve ser >= 0.'); header('Location: taxas.php'); exit; } $st = $conn->prepare("UPDATE eventos SET taxa_tipo='fixa', taxa_percentual=NULL, taxa_valor=? WHERE id=?"); $st->bind_param('di', $val, $id); } $ok = $st->execute(); $st->close(); add_flash($ok?'ok':'error', $ok?'Taxa do comprador salva.':'Erro ao salvar.'); header('Location: taxas.php'); exit; } // 3.3) Limpar repasse de um método (volta ao padrão 3/8/6) if ($action === 'clear_pm') { $met = $_POST['metodo'] ?? ''; if (!isset($METODOS[$met])) { add_flash('error','Método inválido.'); header('Location: taxas.php'); exit; } $col_tipo = "taxa_{$met}_tipo"; $col_perc = "taxa_{$met}_percentual"; $col_val = "taxa_{$met}_valor"; $sql = "UPDATE eventos SET $col_tipo=NULL, $col_perc=NULL, $col_val=NULL WHERE id=?"; $st = $conn->prepare($sql); $st->bind_param('i',$id); $ok = $st->execute(); $st->close(); add_flash($ok?'ok':'error', $ok?"Repasse de {$METODOS[$met]['label']} restaurado ao padrão.":'Erro ao restaurar.'); header('Location: taxas.php'); exit; } // 3.4) Salvar repasse por método if ($action === 'save_pm') { $met = $_POST['metodo'] ?? ''; if (!isset($METODOS[$met])) { add_flash('error','Método inválido.'); header('Location: taxas.php'); exit; } $tipo = $_POST["{$met}_tipo"] ?? ''; if (!in_array($tipo, ['percentual','fixa'], true)) { add_flash('error','Tipo inválido.'); header('Location: taxas.php'); exit; } $col_tipo = "taxa_{$met}_tipo"; $col_perc = "taxa_{$met}_percentual"; $col_val = "taxa_{$met}_valor"; if ($tipo === 'percentual') { $perc = str_replace(',','.', trim($_POST["{$met}_percentual"] ?? '')); if ($perc === '' || !is_numeric($perc)) { add_flash('error','Percentual inválido.'); header('Location: taxas.php'); exit; } $perc = (float)$perc; if ($perc < 0 || $perc > 100) { add_flash('error','Percentual deve estar entre 0 e 100.'); header('Location: taxas.php'); exit; } $sql = "UPDATE eventos SET $col_tipo='percentual', $col_perc=?, $col_val=NULL WHERE id=?"; $st = $conn->prepare($sql); $st->bind_param('di', $perc, $id); } else { $val = str_replace(',','.', trim($_POST["{$met}_valor"] ?? '')); if ($val === '' || !is_numeric($val)) { add_flash('error','Valor fixo inválido.'); header('Location: taxas.php'); exit; } $val = (float)$val; if ($val < 0) { add_flash('error','Valor fixo deve ser >= 0.'); header('Location: taxas.php'); exit; } $sql = "UPDATE eventos SET $col_tipo='fixa', $col_perc=NULL, $col_val=? WHERE id=?"; $st = $conn->prepare($sql); $st->bind_param('di', $val, $id); } $ok = $st->execute(); $st->close(); add_flash($ok?'ok':'error', $ok?"Repasse de {$METODOS[$met]['label']} salvo.":'Erro ao salvar.'); header('Location: taxas.php'); exit; } add_flash('error','Ação inválida.'); header('Location: taxas.php'); exit; } // --- GET: eventos + campos --- $eventos = []; $res = $conn->query(" SELECT id, nome, data, taxa_tipo, taxa_percentual, taxa_valor, taxa_pix_tipo, taxa_pix_percentual, taxa_pix_valor, taxa_credito_tipo, taxa_credito_percentual, taxa_credito_valor, taxa_debito_tipo, taxa_debito_percentual, taxa_debito_valor FROM eventos ORDER BY data DESC "); while ($row = $res->fetch_assoc()) $eventos[] = $row; // Fallbacks exibidos no formulário $DEF = [ 'pix' => 3.00, 'credito' => 8.00, 'debito' => 6.00, ]; ?> <!DOCTYPE html> <html lang="pt-BR"> <head> <meta charset="UTF-8"> <title>Taxas por Evento</title> <meta name="viewport" content="width=device-width, initial-scale=1" /> <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.0/css/all.min.css"> <link rel="stylesheet" href="css/adminprodutor.css?v=2"> <style> .table-taxas{ width:100%; border-collapse: collapse; } .table-taxas th,.table-taxas td{ border-bottom:1px solid #eee; padding:10px; vertical-align: middle; } .table-taxas th{ text-align:left; color:#333; } .badge{ padding:2px 8px; border-radius:999px; font-size:12px; background:#eee; } .badge.pct{ background:#e9f3ff; color:#004c97; } .badge.fix{ background:#fff1cf; color:#7a4b00; } .row-actions{ display:flex; gap:8px; flex-wrap:wrap; align-items: center; } .inline{ display:flex; gap:8px; flex-wrap:wrap; align-items:center; } .inline input[type="number"]{ width:130px; } .inline select{ min-width:160px; } small.muted{ color:#777; } fieldset.metodo { border:1px solid #eee; border-radius:12px; padding:10px; margin:8px 0; } fieldset.metodo legend{ font-weight:600; font-size:14px; padding:0 6px; } </style> </head> <body> <div class="container"> <h1><i class="fa-solid fa-percent"></i> Taxas por Evento</h1> <div class="top-actions"> <a class="btn" href="index.php"><i class="fa-solid fa-arrow-left"></i> Voltar ao painel</a> </div> <?php foreach($flash as $f): ?> <div class="alert <?= $f['t']==='ok'?'ok':'error' ?>"><?= htmlspecialchars($f['m']) ?></div> <?php endforeach; ?> <div class="card"> <p class="muted" style="margin-top:0"> <strong>Taxa do comprador</strong>: se não configurada, aplica-se <strong>9%</strong> por ingresso.<br> <strong>Repasse por forma</strong>: se não configurado, usa-se os padrões PIX <strong><?= number_format($DEF['pix'],2,',','.') ?>%</strong>, Crédito <strong><?= number_format($DEF['credito'],2,',','.') ?>%</strong>, Débito <strong><?= number_format($DEF['debito'],2,',','.') ?>%</strong>. Valor fixo (quando escolhido) é por <u>pedido</u>. </p> <div class="table-scroll"> <table class="table-taxas"> <thead> <tr> <th>Evento</th> <th>Quando</th> <th>Configuração atual</th> <th>Ajustar</th> </tr> </thead> <tbody> <?php if(!$eventos): ?> <tr><td colspan="4" class="muted">Nenhum evento cadastrado.</td></tr> <?php else: foreach($eventos as $e): ?> <tr> <td><strong><?= htmlspecialchars($e['nome']) ?></strong></td> <td><small class="muted"><?= date('d/m/Y H:i', strtotime($e['data'])) ?></small></td> <td> <?php // Resumo (taxa comprador) if ($e['taxa_tipo'] === 'percentual' && $e['taxa_percentual'] !== null) { echo '<div><span class="badge pct">Taxa comprador</span> '.number_format((float)$e['taxa_percentual'],2,',','.')."%</div>"; } elseif ($e['taxa_tipo'] === 'fixa' && $e['taxa_valor'] !== null) { echo '<div><span class="badge fix">Taxa comprador</span> R$ '.number_format((float)$e['taxa_valor'],2,',','.')."</div>"; } else { echo '<div><span class="badge">Taxa comprador</span> 9%</div>'; } // Resumo (repasse por método) $resume = function($tipo,$perc,$val,$padrao){ if ($tipo === 'percentual' && $perc !== null) return number_format((float)$perc,2,',','.').'%'; if ($tipo === 'fixa' && $val !== null) return 'R$ '.number_format((float)$val,2,',','.'); return number_format($padrao,2,',','.').'% (padrão)'; }; echo '<div><small class="muted">PIX: '.$resume($e['taxa_pix_tipo'],$e['taxa_pix_percentual'],$e['taxa_pix_valor'],$DEF['pix']).'</small></div>'; echo '<div><small class="muted">Crédito: '.$resume($e['taxa_credito_tipo'],$e['taxa_credito_percentual'],$e['taxa_credito_valor'],$DEF['credito']).'</small></div>'; echo '<div><small class="muted">Débito: '.$resume($e['taxa_debito_tipo'],$e['taxa_debito_percentual'],$e['taxa_debito_valor'],$DEF['debito']).'</small></div>'; ?> </td> <td> <!-- Taxa do comprador --> <form class="inline" method="post" action="taxas.php" style="margin-bottom:8px;"> <input type="hidden" name="csrf" value="<?= htmlspecialchars($CSRF) ?>"> <input type="hidden" name="id" value="<?= (int)$e['id'] ?>"> <input type="hidden" name="action" value="save_buyer"> <select name="taxa_tipo" onchange="toggleBuyer(this)"> <option value="percentual" <?= $e['taxa_tipo']==='percentual' || $e['taxa_tipo']===null ? 'selected':'' ?>>Taxa comprador: Percentual (%)</option> <option value="fixa" <?= $e['taxa_tipo']==='fixa' ? 'selected':'' ?>>Taxa comprador: Fixa (R$ por ingresso)</option> </select> <input type="number" step="0.01" min="0" max="100" name="taxa_percentual" value="<?= $e['taxa_tipo']==='percentual' && $e['taxa_percentual']!==null ? (float)$e['taxa_percentual'] : 9.00 ?>"> <input type="number" step="0.01" min="0" name="taxa_valor" value="<?= $e['taxa_tipo']==='fixa' && $e['taxa_valor']!==null ? number_format((float)$e['taxa_valor'],2,'.','') : '0.00' ?>"> <button class="btn" type="submit"><i class="fa-solid fa-floppy-disk"></i> Salvar</button> </form> <form class="inline" method="post" action="taxas.php" style="margin-bottom:8px;"> <input type="hidden" name="csrf" value="<?= htmlspecialchars($CSRF) ?>"> <input type="hidden" name="id" value="<?= (int)$e['id'] ?>"> <input type="hidden" name="action" value="clear_buyer"> <button class="btn" type="submit"><i class="fa-solid fa-rotate-left"></i> Restaurar padrão (9%)</button> </form> <!-- Repasse por forma --> <?php // helper para preencher valores $val = function($e,$m,$k,$def) { $ck = "taxa_{$m}_{$k}"; if ($k==='percentual') { return ($e["taxa_{$m}_tipo"]==='percentual' && $e[$ck]!==null) ? (float)$e[$ck] : $def; } if ($k==='valor') { return ($e["taxa_{$m}_tipo"]==='fixa' && $e[$ck]!==null) ? number_format((float)$e[$ck],2,'.','') : '0.00'; } return ''; }; ?> <?php foreach($METODOS as $m => $meta): ?> <fieldset class="metodo"> <legend><?= $meta['label'] ?> – repasse</legend> <form class="inline" method="post" action="taxas.php"> <input type="hidden" name="csrf" value="<?= htmlspecialchars($CSRF) ?>"> <input type="hidden" name="id" value="<?= (int)$e['id'] ?>"> <input type="hidden" name="action" value="save_pm"> <input type="hidden" name="metodo" value="<?= $m ?>"> <select name="<?= $m ?>_tipo" onchange="togglePM(this)"> <option value="percentual" <?= $e["taxa_{$m}_tipo"]==='percentual' || $e["taxa_{$m}_tipo"]===null ? 'selected':'' ?>>Percentual (%)</option> <option value="fixa" <?= $e["taxa_{$m}_tipo"]==='fixa' ? 'selected':'' ?>>Fixa (R$ por pedido)</option> </select> <input type="number" step="0.01" min="0" max="100" name="<?= $m ?>_percentual" value="<?= $val($e,$m,'percentual',$DEF[$m]) ?>"> <input type="number" step="0.01" min="0" name="<?= $m ?>_valor" value="<?= $val($e,$m,'valor','0.00') ?>"> <button class="btn" type="submit"><i class="fa-solid fa-floppy-disk"></i> Salvar</button> </form> <form class="inline" method="post" action="taxas.php" style="margin-top:6px;"> <input type="hidden" name="csrf" value="<?= htmlspecialchars($CSRF) ?>"> <input type="hidden" name="id" value="<?= (int)$e['id'] ?>"> <input type="hidden" name="action" value="clear_pm"> <input type="hidden" name="metodo" value="<?= $m ?>"> <button class="btn" type="submit"><i class="fa-solid fa-rotate-left"></i> Restaurar padrão (<?= number_format($DEF[$m],2,',','.') ?>%)</button> </form> </fieldset> <?php endforeach; ?> </td> </tr> <?php endforeach; endif; ?> </tbody> </table> </div> <p class="muted" style="margin-top:8px;"> • “Taxa do comprador” é aplicada no <em>valor do ingresso</em> (pública).<br> • “Repasse por forma” desconta a taxa do <em>total recebido</em> no pedido, por <strong>percentual</strong> ou <strong>valor fixo por pedido</strong> (usado no dashboard). </p> </div> </div> <script> function toggleBuyer(sel){ const f = sel.closest('form'); const pct = f.querySelector('input[name="taxa_percentual"]'); const fixo = f.querySelector('input[name="taxa_valor"]'); if (sel.value === 'percentual') { pct.disabled = false; fixo.disabled = true; } else { pct.disabled = true; fixo.disabled = false; } } function togglePM(sel){ const f = sel.closest('form'); const id = sel.name.replace('_tipo',''); const pct = f.querySelector(`input[name="${id}_percentual"]`); const fixo = f.querySelector(`input[name="${id}_valor"]`); if (sel.value === 'percentual') { pct.disabled = false; fixo.disabled = true; } else { pct.disabled = true; fixo.disabled = false; } } // inicializa todos document.querySelectorAll('select[name="taxa_tipo"]').forEach(toggleBuyer); document.querySelectorAll('select[name$="_tipo"]').forEach(togglePM); </script> </body> </html>
💾 Save Changes
Cancel
📤 Upload File
×
Select File
Upload
Cancel
➕ Create New
×
Type
📄 File
📁 Folder
Name
Create
Cancel
✎ Rename Item
×
Current Name
New Name
Rename
Cancel
🔐 Change Permissions
×
Target File
Permission (e.g., 0755, 0644)
0755
0644
0777
Apply
Cancel