D7net
Home
Console
Upload
information
Create File
Create Folder
About
Tools
:
/
var
/
www
/
html
/
crsys
/
adm
/
vendor
/
api
/
Filename :
gravar_muda_material_1.php
back
Copy
<?php ini_set('display_errors', 0); ini_set('log_errors', 1); error_reporting(E_ALL); ob_start(); include_once "ConectaPdo.php"; $orcamento_id = isset($_POST['orcamento_id']) ? (int)$_POST['orcamento_id'] : null; $revisao = isset($_POST['revisao']) ? (int)$_POST['revisao'] : null; $ambiente_id = isset($_POST['ambiente_id']) ? (int)$_POST['ambiente_id'] : null; $atual = isset($_POST['atual']) ? (int)$_POST['atual'] : null; $novo = isset($_POST['novo']) ? (int)$_POST['novo'] : null; $novoMaterId = isset($_POST['novoMaterId']) ? (int)$_POST['novoMaterId'] : null; $novoBenefId = isset($_POST['novoBenefId']) ? (int)$_POST['novoBenefId'] : null; $novo_preco = isset($_POST['novo_preco']) ? (float)$_POST['novo_preco'] : null; $empr_id = isset($_POST['empr_id']) ? (int)$_POST['empr_id'] : null; $tipo = $_POST['tipo'] ?? null; unset($_POST['tipo']); if (!$orcamento_id || $revisao === null) { $buffer = ob_get_clean(); if (trim($buffer) !== '') { error_log("SAIDA INDESEJADA ANTES DO ERRO: " . $buffer); } echo json_encode([ 'status' => 'error', 'mensagem' => 'Parâmetros obrigatórios ausentes.' ]); exit; } // --------------------------------------------------------- // LEITURA DO ITEM ATUAL // --------------------------------------------------------- $sqlCab = "SELECT * FROM gre_item_orcamento WHERE empr_id = :empr_id and orca_id = :orca_id AND num_revisao = :num_revisao and material = :material"; $stmtCab = $pdo->prepare($sqlCab); $stmtCab->bindValue(':orca_id', $orcamento_id, PDO::PARAM_INT); $stmtCab->bindValue(':num_revisao', $revisao, PDO::PARAM_INT); $stmtCab->bindValue(':material', $atual, PDO::PARAM_INT); $stmtCab->bindValue(':empr_id', $empr_id, PDO::PARAM_INT); $stmtCab->execute(); $itemAtual = $stmtCab->fetch(PDO::FETCH_ASSOC); // DEBUG CABECALHO file_put_contents("debug_orc_item_atual.txt", "\n------ ItemAtual (" . date("Y-m-d H:i:s") . ") ------\n" . print_r($itemAtual, true), FILE_APPEND ); if (!$itemAtual) { $buffer = ob_get_clean(); if (trim($buffer) !== '') { error_log("SAIDA INDESEJADA ANTES DO JSON: " . $buffer); } echo json_encode([ 'status' => 'error', 'mensagem' => ($tipo === 'ambiente') ? "Nenhum cabeçalho encontrado para este ambiente." : "Nenhum cabeçalho encontrado para este orçamento." ]); exit; } // --------------------------------------------------------- // MONTAGEM DOS DADOS // --------------------------------------------------------- $dados = []; foreach ($_POST as $key => $value) { $dados[$key] = ($value === '' ? null : $value); } date_default_timezone_set("America/Sao_Paulo"); $dados['modified'] = date("Y-m-d H:i:s"); $dados['data_ult_rev'] = date("Y-m-d H:i:s"); // Garantir chaves para evitar "undefined" $dados += [ 'novo' => null, 'novo_mater_id' => null, 'novo_benef_id' => null, 'novo_preco' => null, 'novo_valor' => null, ]; // DEBUG DADOS //file_put_contents("debug_orc1.txt", // "\n------ DADOS (" . date("Y-m-d H:i:s") . ") ------\n" . // print_r($dados, true), // FILE_APPEND //); $m2 = floatval($itemAtual['m2']); $qtde_pecas = floatval($itemAtual['qtde_pecas']); $total_mat = $novo_preco * $m2 * $qtde_pecas; $total_base = $novo_preco * $m2 * $qtde_pecas; $vlr_mo = $dados['vlr_total_mo']; $vlr_total = $vlr_mo + $total_mat; if(dados['ajuste'] > 100) { $valor_total = round($valor_total *(dados['ajuste']/100),2); } // DEBUG DO CÁLCULO file_put_contents("debug_grava_muda_vlrs.txt", "\n--- CALCULO TOTAL (" . date("Y-m-d H:i:s") . ") ---\n" . "total_mo: $novo_valor\n" . "novo_valor: $novo_valor\n" . "vlr_total: $vlr_total\n" . "m2: $m2\n" . "qtde_pecas: $qtde_pecas\n" . "total_mat: $total_mat\n", FILE_APPEND ); try { $sql = "UPDATE gre_item_orcamento SET material = :novo, material_id = :novo_mater_id, benef_id = :novo_benef_id, vlr_unit_mat = :novo_valor, vlr_total_mat = :total_mat, base_total = :base_mat, vlr_total = :vr_total, data_ult_rev = :data_ult_rev, modified = :modified WHERE orca_id = :orcamento_id AND num_revisao = :revisao"; if ($tipo === 'ambiente') { $sql .= " AND ambiente_id = :ambiente_id"; } $stmt = $pdo->prepare($sql); $params = [ ':novo' => $dados['novo'], ':novo_mater_id' => $dados['novo_mater_id'], ':novo_benef_id' => $dados['novo_benef_id'], ':novo_valor' => $novo_valor, ':total_mat' => $total_mat, ':base_mat' => $total_base, ':vlr_total' => $vlr_total, ':data_ult_rev' => $dados['data_ult_rev'], ':modified' => $dados['modified'], ':orcamento_id' => $orcamento_id, ':revisao' => $revisao ]; $arquivo = __DIR__ . '/debug_grava_muda_params.txt'; // caminho do arquivo $conteudo = "LOG DE PARAMETROS - " . date('d/m/Y H:i:s') . PHP_EOL; $conteudo .= str_repeat('-', 40) . PHP_EOL; foreach ($params as $nome => $valor) { // trata arrays e null if (is_array($valor)) { $valor = json_encode($valor, JSON_UNESCAPED_UNICODE); } elseif ($valor === null) { $valor = 'NULL'; } $conteudo .= $nome . ' = ' . $valor . PHP_EOL; } $conteudo .= PHP_EOL; // grava no arquivo (append) file_put_contents($arquivo, $conteudo, FILE_APPEND | LOCK_EX); if ($tipo === 'ambiente') { $params[':ambiente_id'] = $ambiente_id; } $stmt->execute($params); // -------------------------- // LINHA NÃO ATUALIZADA // -------------------------- if ($stmt->rowCount() === 0) { // file_put_contents("debug_orc3.txt", // "\n[WARNING] Nenhuma linha atualizada (" . date("Y-m-d H:i:s") . ")\nParams:\n" . // print_r($params, true), // FILE_APPEND // ); echo json_encode([ 'status' => 'warning', 'mensagem' => 'Nenhuma linha foi atualizada. Verifique os parâmetros enviados.' ]); exit; } $buffer = ob_get_clean(); if (trim($buffer) !== '') { error_log("SAIDA INDESEJADA ANTES DO JSON SUCCESS: " . $buffer); } echo json_encode([ 'status' => 'success', 'mensagem' => "Item atualizado com sucesso.", 'orcamento_id' => $orcamento_id, 'revisao' => $revisao ]); } catch (Exception $e) { error_log("ERRO PDO/GERAL: " . $e->getMessage()); // file_put_contents("debug_orc4.txt", // "\n[ERRO] (" . date("Y-m-d H:i:s") . ") " . $e->getMessage() . "\n", // FILE_APPEND // ); $buffer = ob_get_clean(); if (trim($buffer) !== '') { error_log("SAIDA INDESEJADA NO CATCH: " . $buffer); } echo json_encode([ 'status' => 'error', 'mensagem' => 'Erro ao gravar: ' . $e->getMessage() ]); } ?>