<?php
ini_set('session.cookie_secure', 1);
session_start(['read_and_close' => true]);
require_once "../../includes/db.php";
header('Content-Type: application/json');

$comp_id = isset($_GET['comp_id']) ? (int)$_GET['comp_id'] : 0;
$uid  =$_SESSION['user_id'];

session_write_close();

        $loc_id = isset($_POST['loc_id']) ? intval($_POST['loc_id']) : 0;
        $ord_no = isset($_POST['ord_no']) ? intval($_POST['ord_no']) : 0;
        $t_type = isset($_POST['t_type']) ? trim($_POST['t_type']) : '';
        $confby = isset($_POST['confby']) ? trim($_POST['confby']) : '';
        
        $st_date=isset($_POST['datey']) ? intval($_POST['datey']) : 0;
        $st_ord_no=isset($_POST['docy']) ? intval($_POST['docy']) : 0;

if ($t_type!=='PRCH') {
$stmt = $pdo->prepare("
    UPDATE transactions SET t_status='Deleted', t_conf_by= :uid, t_conf_date=Now() WHERE co_id= :loc_id AND t_doc_no= :ord_no AND t_type= :t_type
");
    
$stmt->execute([
    'uid' => $uid,
    'loc_id' => $loc_id,
    'ord_no' => $ord_no, 't_type' => $t_type
]);

echo json_encode($stmt->fetchAll(PDO::FETCH_ASSOC));
}

if ($t_type==='PRCH') {
$stmt = $pdo->prepare("
    UPDATE transactions SET t_date=:st_date, t_doc_no=:st_ord_no, t_narration=:ord_no1, t_status='Confirmed', t_conf_by= :uid, t_conf_date=Now() WHERE co_id= :loc_id AND t_doc_no= :ord_no AND t_type= :t_type
");
    
$stmt->execute([
    'st_date' => $st_date,
    'st_ord_no' => $st_ord_no,
    'ord_no1' => $ord_no,
    'uid' => $uid,
    'loc_id' => $loc_id,
    'ord_no' => $ord_no, 't_type' => $t_type
]);

echo json_encode($stmt->fetchAll(PDO::FETCH_ASSOC));
}
?>

