<?php
require_once "../../includes/db.php";
session_start(['read_and_close' => true]);

header('Content-Type: application/json');

$doc_no = $_POST['doc_no'] ?? '';
$t_type = $_POST['t_type'] ?? '';
$comp_id =  $_POST['comp_id'] ?? '';

if(!$doc_no || !$t_type || !$comp_id){
    echo json_encode(["exists"=>false]);
    exit;
}

$stmt = $pdo->prepare("
SELECT EXISTS(
    SELECT 1
    FROM transactions
    WHERE t_doc_no = ?
    AND co_id = ?
    AND t_type = ?
    AND t_status<>'Deleted'
) AS doc_exists
");

$stmt->execute([$doc_no, $comp_id, $t_type]);

$result = $stmt->fetch(PDO::FETCH_ASSOC);

echo json_encode([
    "exists" => (bool)$result['doc_exists']
]);
