34567-vm/taxes/delete.php
2025-10-02 20:43:00 +00:00

43 lines
1.1 KiB
PHP

<?php
require_once '../db/config.php';
header('Content-Type: application/json');
$id = $_GET['id'] ?? null;
if (!$id) {
http_response_code(400);
echo json_encode(['error' => 'Tax ID is required']);
exit;
}
try {
$pdo = db();
$stmt = $pdo->prepare("SELECT id FROM taxes WHERE id = ?");
$stmt->execute([$id]);
if (!$stmt->fetch()) {
http_response_code(404);
echo json_encode(['error' => 'Tax not found']);
exit;
}
// Check if the tax is being used in any sale
$stmt = $pdo->prepare("SELECT id FROM sale_taxes WHERE tax_id = ?");
$stmt->execute([$id]);
if ($stmt->fetch()) {
http_response_code(400);
echo json_encode(['error' => 'Cannot delete tax because it is associated with existing sales.']);
exit;
}
$stmt = $pdo->prepare("DELETE FROM taxes WHERE id = ?");
$stmt->execute([$id]);
echo json_encode(['success' => 'Tax deleted successfully']);
} catch (PDOException $e) {
http_response_code(500);
echo json_encode(['error' => 'Database error: ' . $e->getMessage()]);
}