File: /home/muratemr/theotto.tr/admin/suppliers.php
<?php
/**
* admin/suppliers.php
*/
declare(strict_types=1);
$page_title = 'Tedarikçiler';
$active_menu = 'suppliers';
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('suppliers');
require_once __DIR__ . '/../includes/admin_layout.php';
$filter_status = get_str('status', 'all');
$filter_name = get_str('name');
// is_deleted kolonu var mı?
$has_deleted_col = (bool)db_value(
"SELECT COUNT(*) FROM information_schema.COLUMNS
WHERE TABLE_SCHEMA=DATABASE() AND TABLE_NAME='suppliers' AND COLUMN_NAME='is_deleted'"
);
$tab = get_str('tab', 'active'); // active | deleted
$where = $has_deleted_col
? ($tab === 'deleted' ? ['s.is_deleted=1'] : ['s.is_deleted=0'])
: ['1=1'];
$params = [];
if ($filter_status === 'active') { $where[] = 's.is_active=1'; }
if ($filter_status === 'inactive') { $where[] = 's.is_active=0'; }
if ($filter_name !== '') {
$where[] = '(s.company_name LIKE ? OR s.contact_name LIKE ?)';
$params[] = '%' . $filter_name . '%';
$params[] = '%' . $filter_name . '%';
}
$where_str = implode(' AND ', $where);
$suppliers = db_rows("SELECT * FROM suppliers s WHERE $where_str ORDER BY s.is_active DESC, s.company_name ASC", $params);
$day_labels = ['Pzt','Sal','Çar','Per','Cum','Cmt','Paz'];
?>
<!-- Sekmeler -->
<ul class="nav nav-tabs mb-4">
<li class="nav-item">
<a class="nav-link <?= $tab === 'active' ? 'active' : '' ?>" href="suppliers.php?tab=active">
<i class="bi bi-truck me-1"></i>Tedarikçiler
</a>
</li>
<li class="nav-item">
<a class="nav-link <?= $tab === 'deleted' ? 'active' : '' ?>" href="suppliers.php?tab=deleted">
<i class="bi bi-archive me-1"></i>Silinenler
<?php if ($has_deleted_col):
$del_count = (int)db_value("SELECT COUNT(*) FROM suppliers WHERE is_deleted=1");
if ($del_count > 0): ?>
<span class="badge bg-danger ms-1"><?= $del_count ?></span>
<?php endif; endif; ?>
</a>
</li>
</ul>
<div class="d-flex justify-content-between align-items-center mb-3">
<h5 class="mb-0 text-gold">
<i class="bi bi-truck me-2"></i>
<?= $tab === 'deleted' ? 'Silinen Tedarikçiler' : 'Tedarikçiler' ?>
</h5>
<?php if ($tab !== 'deleted'): ?>
<button class="btn btn-gold btn-sm" onclick="openAddModal()">
<i class="bi bi-plus-lg me-1"></i>Yeni Tedarikçi
</button>
<?php endif; ?>
</div>
<!-- Filtreler -->
<div class="card mb-3">
<div class="card-body py-2">
<form method="GET" class="row g-2 align-items-end">
<div class="col-12 col-md-4">
<label class="form-label">Arama</label>
<input type="text" name="name" class="form-control form-control-sm" value="<?= e($filter_name) ?>" placeholder="Firma / İlgili kişi...">
</div>
<div class="col-12 col-md-3">
<label class="form-label">Durum</label>
<select name="status" class="form-select form-select-sm">
<option value="all" <?= $filter_status==='all'?'selected':'' ?>>Tümü</option>
<option value="active" <?= $filter_status==='active'?'selected':'' ?>>Aktif</option>
<option value="inactive" <?= $filter_status==='inactive'?'selected':'' ?>>Pasif</option>
</select>
</div>
<div class="col-12 col-md-2 d-flex gap-2">
<button type="submit" class="btn btn-sm btn-gold flex-fill">
<i class="bi bi-search me-1"></i>Filtrele
</button>
<a href="suppliers.php" class="btn btn-sm btn-outline-secondary">
<i class="bi bi-x-lg"></i>
</a>
</div>
</form>
</div>
</div>
<div class="card">
<div class="card-body p-0">
<div class="table-responsive">
<table class="table table-hover mb-0">
<thead>
<tr>
<th>Firma</th>
<th>İlgili Kişi</th>
<th>Telefon</th>
<th>Sipariş Günleri</th>
<th>Teslimat Günleri</th>
<th>Durum</th>
<th>İşlemler</th>
</tr>
</thead>
<tbody>
<?php foreach ($suppliers as $s): ?>
<tr>
<td>
<div class="fw-600"><?= e($s['company_name']) ?></div>
<?php if ($s['products_note']): ?>
<div class="text-muted fs-12"><?= e(mb_strimwidth($s['products_note'],0,40,'...')) ?></div>
<?php endif; ?>
</td>
<td><?= e($s['contact_name'] ?? '—') ?></td>
<td>
<?= e($s['phone'] ?? '—') ?>
<?php if ($s['whatsapp']): ?>
<a href="#" onclick="openWhatsApp('<?= e($s['whatsapp']) ?>','')" class="ms-1" title="WhatsApp">
<i class="bi bi-whatsapp text-success fs-12"></i>
</a>
<?php endif; ?>
</td>
<td class="fs-12"><?= e($s['order_days'] ?? '—') ?></td>
<td class="fs-12"><?= e($s['delivery_days'] ?? '—') ?></td>
<td>
<?= $s['is_active']
? '<span class="badge bg-success">Aktif</span>'
: '<span class="badge bg-secondary">Pasif</span>' ?>
</td>
<td>
<div class="d-flex gap-1">
<button class="btn btn-sm btn-outline-secondary btn-action" onclick="openEditModal(<?= htmlspecialchars(json_encode($s)) ?>)" title="Düzenle">
<i class="bi bi-pencil"></i>
</button>
<?php if ($s['whatsapp']): ?>
<button class="btn btn-sm btn-outline-success btn-action" onclick="openWhatsApp('<?= e($s['whatsapp']) ?>','')" title="WhatsApp">
<i class="bi bi-whatsapp"></i>
</button>
<?php endif; ?>
<?php if (is_admin()): ?>
<?php if ($tab !== 'deleted'): ?>
<button class="btn btn-sm btn-outline-danger btn-action" onclick="deleteSupplier(<?= $s['id'] ?>)" title="Sil">
<i class="bi bi-trash"></i>
</button>
<?php else: ?>
<button class="btn btn-sm btn-outline-success btn-action" onclick="restoreSupplier(<?= $s['id'] ?>)" title="Geri Yükle">
<i class="bi bi-arrow-counterclockwise"></i>
</button>
<button class="btn btn-sm btn-outline-danger btn-action" onclick="permanentDeleteSupplier(<?= $s['id'] ?>, '<?= e(addslashes($s['company_name'])) ?>')" title="Kalıcı Sil">
<i class="bi bi-trash-fill"></i>
</button>
<?php endif; ?>
<?php endif; ?>
</div>
</td>
</tr>
<?php endforeach; ?>
<?php if (empty($suppliers)): ?>
<tr><td colspan="7" class="text-center text-muted py-4">Tedarikçi bulunamadı.</td></tr>
<?php endif; ?>
</tbody>
</table>
</div>
</div>
</div>
<!-- Tedarikçi Modalı -->
<div class="modal fade" id="supplierModal" tabindex="-1">
<div class="modal-dialog modal-lg">
<div class="modal-content">
<div class="modal-header">
<h6 class="modal-title" id="supplierModalTitle">Yeni Tedarikçi</h6>
<button type="button" class="btn-close" data-bs-dismiss="modal"></button>
</div>
<div class="modal-body">
<input type="hidden" id="supplierId">
<div class="row g-3">
<div class="col-md-6">
<label class="form-label">Firma Adı *</label>
<input type="text" id="suppCompany" class="form-control" placeholder="Firma adı">
</div>
<div class="col-md-6">
<label class="form-label">İlgili Kişi</label>
<input type="text" id="suppContact" class="form-control" placeholder="Ad Soyad">
</div>
<div class="col-md-4">
<label class="form-label">Telefon</label>
<input type="text" id="suppPhone" class="form-control" placeholder="0xxx xxx xx xx" data-phone>
</div>
<div class="col-md-4">
<label class="form-label">WhatsApp</label>
<input type="text" id="suppWA" class="form-control" placeholder="0xxx xxx xx xx" data-phone>
</div>
<div class="col-md-4">
<label class="form-label">E-posta</label>
<input type="email" id="suppEmail" class="form-control" placeholder="mail@ornek.com">
</div>
<div class="col-md-6">
<label class="form-label">Vergi No</label>
<input type="text" id="suppTaxNo" class="form-control" placeholder="1234567890">
</div>
<div class="col-md-6">
<label class="form-label">Vergi Dairesi</label>
<input type="text" id="suppTaxOffice" class="form-control" placeholder="Kadıköy VD">
</div>
<div class="col-md-12">
<label class="form-label">Adres</label>
<input type="text" id="suppAddress" class="form-control" placeholder="Mahalle, Cadde, No, İlçe/İl">
</div>
<div class="col-md-6">
<label class="form-label">Sipariş Günleri</label>
<div class="days-selector" id="suppOrderDaysBox">
<?php foreach (['Pazartesi','Salı','Çarşamba','Perşembe','Cuma','Cumartesi','Pazar'] as $gun): ?>
<label class="day-badge">
<input type="checkbox" class="order-day-cb" value="<?= $gun ?>">
<span><?= $gun ?></span>
</label>
<?php endforeach; ?>
</div>
<input type="hidden" id="suppOrderDays">
</div>
<div class="col-md-6">
<label class="form-label">Teslimat Günleri</label>
<div class="days-selector" id="suppDeliveryDaysBox">
<?php foreach (['Pazartesi','Salı','Çarşamba','Perşembe','Cuma','Cumartesi','Pazar'] as $gun): ?>
<label class="day-badge">
<input type="checkbox" class="delivery-day-cb" value="<?= $gun ?>">
<span><?= $gun ?></span>
</label>
<?php endforeach; ?>
</div>
<input type="hidden" id="suppDeliveryDays">
</div>
<div class="col-12">
<label class="form-label">Ürünler Notu</label>
<textarea id="suppProductsNote" class="form-control" rows="2" placeholder="Tedarik ettiği ürünler..."></textarea>
</div>
<div class="col-12">
<label class="form-label">Genel Not</label>
<textarea id="suppGeneralNote" class="form-control" rows="2"></textarea>
</div>
<div class="col-12">
<div class="form-check form-switch">
<input class="form-check-input" type="checkbox" id="suppActive" checked>
<label class="form-check-label" for="suppActive">Aktif</label>
</div>
</div>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary btn-sm" data-bs-dismiss="modal">İptal</button>
<button type="button" class="btn btn-gold btn-sm" id="supplierSaveBtn">Kaydet</button>
</div>
</div>
</div>
</div>
<script>
function openAddModal() {
document.getElementById('supplierId').value = '';
document.getElementById('suppCompany').value = '';
document.getElementById('suppContact').value = '';
document.getElementById('suppPhone').value = '';
document.getElementById('suppWA').value = '';
document.getElementById('suppEmail').value = '';
document.getElementById('suppTaxNo').value = '';
document.getElementById('suppTaxOffice').value = '';
document.getElementById('suppAddress').value = '';
document.querySelectorAll('.order-day-cb, .delivery-day-cb').forEach(cb => cb.checked = false);
document.getElementById('suppProductsNote').value= '';
document.getElementById('suppGeneralNote').value = '';
document.getElementById('suppActive').checked = true;
document.getElementById('supplierModalTitle').textContent = 'Yeni Tedarikçi';
openModal('supplierModal');
}
function openEditModal(s) {
document.getElementById('supplierId').value = s.id;
document.getElementById('suppCompany').value = s.company_name;
document.getElementById('suppContact').value = s.contact_name || '';
document.getElementById('suppPhone').value = s.phone || '';
document.getElementById('suppWA').value = s.whatsapp || '';
document.getElementById('suppEmail').value = s.email || '';
document.getElementById('suppTaxNo').value = s.tax_no || '';
document.getElementById('suppTaxOffice').value = s.tax_office || '';
document.getElementById('suppAddress').value = s.address || '';
// Günleri checkbox'lardan set et
const orderDayVals = (s.order_days || '').split(',').map(d => d.trim()).filter(Boolean);
const deliveryDayVals = (s.delivery_days || '').split(',').map(d => d.trim()).filter(Boolean);
document.querySelectorAll('.order-day-cb').forEach(cb => { cb.checked = orderDayVals.includes(cb.value); });
document.querySelectorAll('.delivery-day-cb').forEach(cb => { cb.checked = deliveryDayVals.includes(cb.value); });
document.getElementById('suppProductsNote').value = s.products_note || '';
document.getElementById('suppGeneralNote').value = s.general_note || '';
document.getElementById('suppActive').checked = !!s.is_active;
document.getElementById('supplierModalTitle').textContent = 'Tedarikçi Düzenle';
openModal('supplierModal');
}
document.getElementById('supplierSaveBtn').addEventListener('click', async () => {
const id = document.getElementById('supplierId').value;
const company = document.getElementById('suppCompany').value.trim();
if (!company) { showToast('Firma adı zorunludur.', 'error'); return; }
const res = await apiPost('/api/admin/suppliers.php', {
action: id ? 'update' : 'create',
id,
company_name: company,
contact_name: document.getElementById('suppContact').value,
phone: document.getElementById('suppPhone').value,
whatsapp: document.getElementById('suppWA').value,
email: document.getElementById('suppEmail').value,
tax_no: document.getElementById('suppTaxNo').value.trim(),
tax_office: document.getElementById('suppTaxOffice').value.trim(),
address: document.getElementById('suppAddress').value.trim(),
order_days: [...document.querySelectorAll('.order-day-cb:checked')].map(c=>c.value).join(', '),
delivery_days: [...document.querySelectorAll('.delivery-day-cb:checked')].map(c=>c.value).join(', '),
products_note: document.getElementById('suppProductsNote').value,
general_note: document.getElementById('suppGeneralNote').value,
is_active: document.getElementById('suppActive').checked ? 1 : 0,
});
if (res.success) {
closeModal('supplierModal');
showToast(res.message, 'success');
setTimeout(() => location.reload(), 800);
} else {
showToast(res.message, 'error');
}
});
async function deleteSupplier(id) {
confirmAction('Bu tedarikçiyi silmek istiyor musunuz? Silinen tedarikçiler arşive taşınır.', async function() {
const res = await apiPost('/api/admin/suppliers.php', { action: 'delete', id: id });
if (res.success) {
showToast(res.message, 'success');
setTimeout(function() { location.reload(); }, 800);
} else {
showToast(res.message, 'error');
}
});
}
async function restoreSupplier(id) {
confirmAction('Bu tedarikçiyi geri yüklemek istiyor musunuz?', async function() {
const res = await apiPost('/api/admin/suppliers.php', { action: 'restore', id: id });
if (res.success) {
showToast(res.message, 'success');
setTimeout(function() { location.reload(); }, 800);
} else {
showToast(res.message, 'error');
}
});
}
async function permanentDeleteSupplier(id, name) {
confirmAction('"' + name + '" kalıcı olarak silinsin mi? Bu tedarikçiye ait TÜM siparişler de silinecek. Bu işlem GERİ ALINAMAZ!', async function() {
const res = await apiPost('/api/admin/suppliers.php', { action: 'permanent_delete', id: id });
if (res.success) {
showToast(res.message, 'success');
setTimeout(function() { location.reload(); }, 800);
} else {
showToast(res.message, 'error');
}
});
}
</script>
<?php require_once __DIR__ . '/../includes/admin_layout_end.php'; ?>