$sp_id, 'naziv' => $pun_naziv ]; } } fclose($handle); } // PHP DEO - OBRADA PODATAKA if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_FILES['slika'])) { require 'db.php'; header('Content-Type: application/json'); try { $poslovnica = $_POST['poslovnica']; $is_pdf = isset($_POST['is_pdf']) && $_POST['is_pdf'] === '1'; $year = date("Y"); $month = date("m"); $target_dir = "uploads/$year/$month/"; if (!file_exists($target_dir)) { if (!mkdir($target_dir, 0755, true)) { $err = error_get_last(); file_put_contents(__DIR__ . '/error_log.txt', date('[Y-m-d H:i:s] ') . "DIR_GRESKA: Ne mogu kreirati $target_dir. " . print_r($err, true) . "\n", FILE_APPEND); throw new Exception("Greška pri kreiranju foldera na serveru."); } } $ext = $is_pdf ? ".pdf" : ".jpg"; $filename = date("Ymd_His") . "_" . uniqid() . "_" . preg_replace('/[^A-Za-z0-9\-]/', '', $poslovnica) . $ext; $target_file = $target_dir . $filename; if (move_uploaded_file($_FILES["slika"]["tmp_name"], $target_file)) { $filesize = round(filesize($target_file) / 1024); $sp_transaction_id = !empty($_POST['sp_transaction_id']) ? $_POST['sp_transaction_id'] : null; $app_version = isset($_POST['app_version']) ? $_POST['app_version'] : 'nepoznato'; if ($sp_transaction_id) { // Standardni storno zahtev - snimamo u dokumenti sa pravim ID-jem $stmt = $pdo->prepare("INSERT INTO dokumenti (poslovnica, file_path, file_name, file_size_kb, transaction_id) VALUES (?, ?, ?, ?, ?)"); $stmt->execute([$poslovnica, $target_file, $filename, $filesize, $sp_transaction_id]); // Ažuriranje postojećeg zahteva i beleženje verzije aplikacije $stmt_iznos = $pdo->prepare("SELECT amount, ocr_details FROM storno_zahtevi WHERE transaction_id = ?"); $stmt_iznos->execute([$sp_transaction_id]); $row = $stmt_iznos->fetch(PDO::FETCH_ASSOC); $iznos = $row ? $row['amount'] : 0; $ocr_details = $row ? json_decode($row['ocr_details'] ?? '{}', true) : []; if (!is_array($ocr_details)) $ocr_details = []; $ocr_details['app_version'] = $app_version; $ocr_details_json = json_encode($ocr_details); // Promena statusa u bazi uz verifikacije $stmt_storno = $pdo->prepare("UPDATE storno_zahtevi SET status = 'poslato_na_odobrenje', ocr_details = ? WHERE transaction_id = ?"); if (!$stmt_storno->execute([$ocr_details_json, $sp_transaction_id])) { $dbErr = $stmt_storno->errorInfo(); file_put_contents(__DIR__ . '/error_log.txt', date('[Y-m-d H:i:s] ') . "PDO_EXEC_GRESKA za ID $sp_transaction_id: " . print_r($dbErr, true) . "\n", FILE_APPEND); throw new Exception("SQL greška pri ažuriranju statusa."); } if ($stmt_storno->rowCount() === 0 && !$row) { file_put_contents(__DIR__ . '/error_log.txt', date('[Y-m-d H:i:s] ') . "PDO_ROW_GRESKA: Ažurirano 0 redova. Zahtev $sp_transaction_id verovatno ne postoji u bazi.\n", FILE_APPEND); // Nastavljamo uprkos ovome, da se makar dokument sačuvao } // Hit Webhook-a za n8n OCR analizu $webhook_url = 'https://automation.dunavgold.rs/webhook/storno-ocr-provera'; $data = [ 'transaction_id' => $sp_transaction_id, 'iznos_iz_baze' => $iznos, 'image_url' => 'https://dcs.dunavunion.rs/' . $target_file, 'app_version' => $app_version ]; $ch = curl_init($webhook_url); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data)); curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type:application/json']); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_TIMEOUT, 2); // Kratak timeout od 2s da ne bi blokirali frontend dok n8n misli // Izvrši i hvataj eventualne mrežne greške $result = curl_exec($ch); if ($result === false) { $error_msg = curl_error($ch); file_put_contents(__DIR__ . '/debug_log.txt', date('[Y-m-d H:i:s] ') . "cURL GRESKA za $sp_transaction_id : $error_msg\n", FILE_APPEND); } else { file_put_contents(__DIR__ . '/debug_log.txt', date('[Y-m-d H:i:s] ') . "cURL USPEH za $sp_transaction_id HTTP Odgovor: " . substr($result, 0, 100) . "\n", FILE_APPEND); } curl_close($ch); } else { // Generički dokument - BYPASS N8N WEBHOOK-A $sp_location_id = isset($_POST['sp_location_id']) ? $_POST['sp_location_id'] : null; $new_tx_id = 'DOC_' . date("YmdHis") . '_' . mt_rand(100, 999); // Snimamo u dokumenti sa novim random ID-jem $stmt = $pdo->prepare("INSERT INTO dokumenti (poslovnica, file_path, file_name, file_size_kb, transaction_id) VALUES (?, ?, ?, ?, ?)"); $stmt->execute([$poslovnica, $target_file, $filename, $new_tx_id]); // Pakujemo app_version u ocr_details $ocr_details = json_encode([ 'app_version' => $app_version, 'tip' => 'dodatni_dokument' ]); // Upisujemo u storno_zahtevi sa statusom 'ostalo' da bi admin panel mogao to da dohvati $stmt_storno_gen = $pdo->prepare("INSERT INTO storno_zahtevi (agent_id, location_id, transaction_id, amount, currency, datum_transakcije, status, ocr_details) VALUES (0, ?, ?, 0.00, 'RSD', NOW(), 'ostalo', ?)"); $stmt_storno_gen->execute([$sp_location_id, $new_tx_id, $ocr_details]); } echo json_encode(['status' => 'success', 'message' => '✅ Dokument uspešno poslat!']); } else { // move_uploaded_file je feilovao, daj da vidimo zašto $err = error_get_last(); file_put_contents(__DIR__ . '/error_log.txt', date('[Y-m-d H:i:s] ') . "UPLOAD_GRESKA: Ne mogu prebaciti fajl u $target_file. " . print_r($err, true) . "\n", FILE_APPEND); throw new Exception('Greška pri čuvanju fajla na disku. Kontaktiraj administratora.'); } } catch (Exception $e) { file_put_contents(__DIR__ . '/error_log.txt', date('[Y-m-d H:i:s] ') . "OPSTA_GRESKA: " . $e->getMessage() . "\n", FILE_APPEND); http_response_code(500); echo json_encode(['status' => 'error', 'message' => '❌ Sistemska greška, pogledajte logove.']); } exit; } ?> Dostava Dokumenata | Dunav Union
Dunav Union
×
Molimo izaberite poslovnicu kako bi učitali eventualne storno zahteve.