š Painel Administrativo
Warning: Undefined variable $erro in
/home/u977314867/domains/mepexpress.com.br/public_html/wp-content/plugins/mep-frete-system/mep-frete-system-os.php on line
2649
Warning: Undefined variable $sucesso in
/home/u977314867/domains/mepexpress.com.br/public_html/wp-content/plugins/mep-frete-system/mep-frete-system-os.php on line
2650
/* ================= FILTROS ================= */
$f = [
'token' => sanitize_text_field($_GET['f_token'] ?? ''),
'cpf_cnpj' => sanitize_text_field($_GET['f_cpf_cnpj'] ?? ''),
'pagador' => sanitize_text_field($_GET['f_pagador'] ?? ''),
'remetente' => sanitize_text_field($_GET['f_remetente'] ?? ''),
'destinatario' => sanitize_text_field($_GET['f_destinatario'] ?? ''),
'motoboy' => sanitize_text_field($_GET['f_motoboy'] ?? ''),
'veiculo' => sanitize_text_field($_GET['f_veiculo'] ?? ''),
'status' => sanitize_text_field($_GET['f_status'] ?? ''),
'inicio' => sanitize_text_field($_GET['f_inicio'] ?? ''),
'fim' => sanitize_text_field($_GET['f_fim'] ?? ''),
];
$where = [];
$params = [];
foreach ($f as $k => $v) {
if ($v === '') continue;
switch ($k) {
case 'token':
$where[] = "token LIKE %s";
$params[] = "%$v%";
break;
// CPF / CNPJ (campo livre)
case 'cpf_cnpj':
$where[] = "pag_doc LIKE %s";
$params[] = "%$v%";
break;
// PAGADOR
case 'pagador':
$where[] = "pag_nome LIKE %s";
$params[] = "%$v%";
break;
case 'remetente':
$where[] = "remetente_nome LIKE %s";
$params[] = "%$v%";
break;
case 'destinatario':
$where[] = "destinatario_nome LIKE %s";
$params[] = "%$v%";
break;
case 'motoboy':
$where[] = "motoboy_nome LIKE %s";
$params[] = "%$v%";
break;
case 'veiculo':
$where[] = "veiculo = %s";
$params[] = $v;
break;
case 'status':
$where[] = "status = %s";
$params[] = $v;
break;
case 'inicio':
$where[] = "DATE(criado_em) >= %s";
$params[] = $v;
break;
case 'fim':
$where[] = "DATE(criado_em) <= %s";
$params[] = $v;
break;
}
}
/* ================= EXPORTAĆĆO CSV ================= */
if (isset($_GET['export_csv'])) {
header('Content-Type: text/csv; charset=utf-8');
header('Content-Disposition: attachment; filename=relatorio_os_' . date('Y-m-d_H-i') . '.csv');
$out = fopen('php://output', 'w');
fputcsv($out, [
'Token','Status','Cliente','CPF/CNPJ','Email',
'Origem','Destino','Motoboy','Recebedor',
'VeĆculo','Valor','Data'
], ';');
$sql = "SELECT * FROM $t_os";
if ($where) $sql .= " WHERE " . implode(" AND ", $where);
$sql .= " ORDER BY criado_em DESC";
$rows = $params
? $wpdb->get_results($wpdb->prepare($sql, $params), ARRAY_A)
: $wpdb->get_results($sql, ARRAY_A);
foreach ($rows as $r) {
fputcsv($out, [
$r['token'],
$r['status'],
$r['pag_nome'],
$r['pag_doc'],
$r['pag_email'],
$r['origem'],
$r['destino'],
$r['motoboy_nome'],
$r['recebedor_nome'],
$r['veiculo'],
$r['preco'],
$r['criado_em'],
], ';');
}
exit;
}
/* ================= CONSULTA FINAL ================= */
global $wpdb;
$t_os = $wpdb->prefix . 'mpxa_os';
$f = [
'token' => sanitize_text_field($_GET['f_token'] ?? ''),
'cpf_cnpj' => sanitize_text_field($_GET['f_cpf_cnpj'] ?? ''),
'pagador' => sanitize_text_field($_GET['f_pagador'] ?? ''),
'remetente' => sanitize_text_field($_GET['f_remetente'] ?? ''),
'destinatario' => sanitize_text_field($_GET['f_destinatario'] ?? ''),
'motoboy' => sanitize_text_field($_GET['f_motoboy'] ?? ''),
'veiculo' => sanitize_text_field($_GET['f_veiculo'] ?? ''),
'status' => sanitize_text_field($_GET['f_status'] ?? ''),
'inicio' => sanitize_text_field($_GET['f_inicio'] ?? ''),
'fim' => sanitize_text_field($_GET['f_fim'] ?? ''),
];
$where = [];
$params = [];
foreach ($f as $k => $v) {
if ($v === '') continue;
switch ($k) {
case 'token':
$where[] = "token LIKE %s";
$params[] = "%$v%";
break;
case 'cpf_cnpj':
$where[] = "pag_doc LIKE %s";
$params[] = "%$v%";
break;
case 'pagador':
$where[] = "pag_nome LIKE %s";
$params[] = "%$v%";
break;
case 'remetente':
$where[] = "remetente_nome LIKE %s";
$params[] = "%$v%";
break;
case 'destinatario':
$where[] = "destinatario_nome LIKE %s";
$params[] = "%$v%";
break;
case 'motoboy':
$where[] = "motoboy_nome LIKE %s";
$params[] = "%$v%";
break;
case 'veiculo':
$where[] = "veiculo = %s";
$params[] = $v;
break;
case 'status':
$where[] = "status = %s";
$params[] = $v;
break;
case 'inicio':
$where[] = "DATE(criado_em) >= %s";
$params[] = $v;
break;
case 'fim':
$where[] = "DATE(criado_em) <= %s";
$params[] = $v;
break;
}
}
$sql = "SELECT * FROM $t_os";
if ($where) {
$sql .= " WHERE " . implode(" AND ", $where);
}
$sql .= " ORDER BY criado_em DESC LIMIT 500";
$lista = $params
? $wpdb->get_results($wpdb->prepare($sql, $params))
: $wpdb->get_results($sql);