File: /home/muratemr/theotto.tr/api/admin/customer_tags.php
<?php
/**
* api/admin/customer_tags.php
*/
declare(strict_types=1);
require_once __DIR__ . '/../../includes/db.php';
require_once __DIR__ . '/../../includes/auth.php';
require_once __DIR__ . '/../../includes/functions.php';
require_once __DIR__ . '/../../includes/csrf.php';
require_permission('customer_tags');
if (!is_admin()) json_response(false, 'Sadece admin bu işlemi yapabilir.');
csrf_required();
header('Content-Type: application/json');
$action = post_str('action');
$id = post_int('id');
switch ($action) {
case 'create':
$name = post_str('name');
$discount_rate = post_int('discount_rate');
$auto_note = post_str('auto_note');
if (!$name) json_response(false, 'Etiket adı zorunludur.');
if (db_value('SELECT id FROM customer_tags WHERE name=?', [$name])) {
json_response(false, 'Bu isimde etiket zaten var.');
}
db_insert('INSERT INTO customer_tags (name, discount_rate, auto_note) VALUES (?,?,?)', [$name, $discount_rate, $auto_note]);
json_response(true, 'Etiket oluşturuldu.');
case 'update':
if (!$id) json_response(false, 'Geçersiz ID.');
$name = post_str('name');
$discount_rate = post_int('discount_rate');
$auto_note = post_str('auto_note');
if (!$name) json_response(false, 'Etiket adı zorunludur.');
$exists = db_value('SELECT id FROM customer_tags WHERE name=? AND id!=?', [$name, $id]);
if ($exists) json_response(false, 'Bu isimde başka etiket var.');
db_run('UPDATE customer_tags SET name=?, discount_rate=?, auto_note=? WHERE id=?', [$name, $discount_rate, $auto_note, $id]);
json_response(true, 'Etiket güncellendi.');
case 'delete':
if (!$id) json_response(false, 'Geçersiz ID.');
$tag = db_row('SELECT is_default FROM customer_tags WHERE id=?', [$id]);
if (!$tag) json_response(false, 'Etiket bulunamadı.');
if ($tag['is_default']) json_response(false, 'Varsayılan etiket silinemez.');
$count = (int)db_value('SELECT COUNT(*) FROM customers WHERE tag_id=?', [$id]);
if ($count > 0) json_response(false, 'Bu etikete bağlı müşteri var. Önce müşterilerin etiketini değiştirin.');
db_run('DELETE FROM customer_tags WHERE id=?', [$id]);
json_response(true, 'Etiket silindi.');
case 'set_default':
if (!$id) json_response(false, 'Geçersiz ID.');
db_run('UPDATE customer_tags SET is_default=0');
db_run('UPDATE customer_tags SET is_default=1 WHERE id=?', [$id]);
json_response(true, 'Varsayılan güncellendi.');
default:
json_response(false, 'Geçersiz işlem.');
}