D7net
Home
Console
Upload
information
Create File
Create Folder
About
Tools
:
/
var
/
www
/
html
/
crsys
/
adm
/
vendor
/
api
/
Filename :
gravaConjunto.php
back
Copy
<?php header('Content-Type: application/json'); include_once "ConectaPdo.php"; date_default_timezone_set("America/Sao_Paulo"); // ========================= // LOG // ========================= function writeLog($msg) { $file = __DIR__ . "/log.txt"; $time = date("Y-m-d H:i:s"); file_put_contents($file, "[$time] $msg\n", FILE_APPEND); } // ========================= // VALIDAÇÃO CORRETA (NÃO USA empty) // ========================= function isMissing($v) { return $v === null || $v === ''; } try { writeLog("================================"); writeLog("INÍCIO PROCESSO"); // ========================= // JSON INPUT // ========================= $json = file_get_contents("php://input"); writeLog("RAW INPUT: " . $json); $dados = json_decode($json, true); writeLog("DECODE JSON: " . print_r($dados, true)); if (json_last_error() !== JSON_ERROR_NONE) { throw new Exception("Erro JSON: " . json_last_error_msg()); } if (!is_array($dados)) { throw new Exception("JSON inválido"); } // ========================= // CAMPOS PRINCIPAIS // ========================= $orcamento_id = $dados['orca_id'] ?? null; $num_revisao = $dados['num_revisao'] ?? null; $empr_id = $dados['empr_id'] ?? null; $itens = $dados['itens'] ?? []; writeLog("orcamento_id=$orcamento_id | num_revisao=$num_revisao | empr_id=$empr_id"); // ========================= // CORREÇÃO CRÍTICA AQUI // ========================= if ( isMissing($orcamento_id) || isMissing($num_revisao) || isMissing($empr_id) ) { writeLog("ERRO: CAMPOS OBRIGATÓRIOS AUSENTES"); writeLog(print_r($dados, true)); throw new Exception("Dados obrigatórios ausentes"); } if (!is_array($itens)) { throw new Exception("Lista de itens inválida"); } // ========================= // TRANSAÇÃO // ========================= $pdo->beginTransaction(); writeLog("TRANSAÇÃO INICIADA"); // ========================= // LOOP // ========================= foreach ($itens as $itemRow) { $item = $itemRow['item'] ?? null; $conj = $itemRow['conj'] ?? null; writeLog("ITEM: $item | CONJ: $conj"); if (isMissing($item)) { writeLog("ITEM VAZIO IGNORADO"); continue; } $sql = " UPDATE gre_item_orcamento SET conj = :conj WHERE empr_id = :empr_id AND orca_id = :orca_id AND num_revisao = :num_revisao AND id = :item "; $stmt = $pdo->prepare($sql); $stmt->execute([ ':conj' => $conj, ':empr_id' => $empr_id, ':orca_id' => $orcamento_id, ':num_revisao' => $num_revisao, ':item' => $item ]); writeLog("UPDATE OK item=$item | rows=" . $stmt->rowCount()); } $pdo->commit(); writeLog("COMMIT OK"); echo json_encode([ 'status' => 'success', 'mensagem' => 'Conjunto dos Itens atualizados com sucesso', 'ret_orca_id' => $orcamento_id, 'ret_num_revisao' => $num_revisao ]); } catch (PDOException $e) { if ($pdo->inTransaction()) $pdo->rollBack(); writeLog("PDO ERROR: " . $e->getMessage()); echo json_encode([ 'status' => 'error', 'mensagem' => 'Erro no banco: ' . $e->getMessage() ]); } catch (Exception $e) { if ($pdo->inTransaction()) $pdo->rollBack(); writeLog("ERROR: " . $e->getMessage()); echo json_encode([ 'status' => 'error', 'mensagem' => $e->getMessage() ]); }