181 lines
7.0 KiB
PHP
181 lines
7.0 KiB
PHP
<?php
|
|
require_once 'db/config.php';
|
|
|
|
$error_message = null;
|
|
|
|
try {
|
|
$pdo = db();
|
|
|
|
// Get distinct values for filters
|
|
$periode_options_stmt = $pdo->query('SELECT DISTINCT PERIODE FROM tabelmaster WHERE PERIODE IS NOT NULL AND PERIODE != \'\' ORDER BY PERIODE DESC');
|
|
$periode_options = $periode_options_stmt->fetchAll(PDO::FETCH_COLUMN);
|
|
|
|
// Handle form submission for analysis
|
|
$analysis_results = [];
|
|
$chart_labels_json = '[]';
|
|
$chart_data_json = '[]';
|
|
$selected_periode = $_POST['periode'] ?? 'all';
|
|
|
|
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['run_analysis'])) {
|
|
$sql = "\n SELECT\n NAMA_SALES,\n SUM(QTY) as total_qty,\n SUM(QTY * CAST(REPLACE(HARGA, ',', '') AS DECIMAL(15,2))) as total_omzet,\n AVG(CAST(REPLACE(HARGA, ',', '') AS DECIMAL(15,2))) as avg_price,\n COUNT(DISTINCT NO_FAKTUR) as transactions_count\n FROM\n tabelmaster\n WHERE 1=1";
|
|
|
|
$params = [];
|
|
if ($selected_periode !== 'all') {
|
|
$sql .= ' AND PERIODE = :periode';
|
|
$params[':periode'] = $selected_periode;
|
|
}
|
|
|
|
$sql .= ' GROUP BY NAMA_SALES HAVING SUM(QTY) > 0 ORDER BY total_omzet DESC';
|
|
|
|
$analysis_stmt = $pdo->prepare($sql);
|
|
$analysis_stmt->execute($params);
|
|
$analysis_results = $analysis_stmt->fetchAll();
|
|
|
|
if (!empty($analysis_results)) {
|
|
$chart_labels = [];
|
|
$chart_data = [];
|
|
foreach ($analysis_results as $row) {
|
|
$chart_labels[] = $row['NAMA_SALES'];
|
|
$chart_data[] = $row['total_omzet'];
|
|
}
|
|
$chart_labels_json = json_encode($chart_labels);
|
|
$chart_data_json = json_encode($chart_data);
|
|
}
|
|
}
|
|
|
|
} catch (PDOException $e) {
|
|
$error_message = "Database error: " . $e->getMessage();
|
|
}
|
|
|
|
require_once 'header.php';
|
|
?>
|
|
|
|
<main class="container">
|
|
<?php if (isset($error_message)): ?>
|
|
<div class="alert alert-danger" role="alert">
|
|
<?php echo htmlspecialchars($error_message); ?>
|
|
</div>
|
|
<?php endif; ?>
|
|
|
|
<!-- Analysis Form -->
|
|
<div class="card mb-4">
|
|
<div class="card-header">
|
|
Analisa Penjualan per Sales
|
|
</div>
|
|
<div class="card-body">
|
|
<form method="POST" action="sales_analysis.php#analysis-results">
|
|
<div class="row g-3 align-items-end">
|
|
<div class="col-md-10">
|
|
<label for="periode" class="form-label">Periode</label>
|
|
<select name="periode" id="periode" class="form-select">
|
|
<option value="all">Semua Periode</option>
|
|
<?php foreach ($periode_options as $periode): ?>
|
|
<option value="<?php echo htmlspecialchars($periode); ?>" <?php echo ($selected_periode === $periode) ? 'selected' : ''; ?> >
|
|
<?php echo htmlspecialchars($periode); ?>
|
|
</option>
|
|
<?php endforeach; ?>
|
|
</select>
|
|
</div>
|
|
<div class="col-md-2">
|
|
<button type="submit" name="run_analysis" class="btn btn-primary w-100">Jalankan</button>
|
|
</div>
|
|
</div>
|
|
</form>
|
|
</div>
|
|
</div>
|
|
|
|
<!-- Analysis Results -->
|
|
<?php if (!empty($analysis_results)): ?>
|
|
<div class="card mb-4" id="analysis-results">
|
|
<div class="card-header">
|
|
Hasil Analisa
|
|
</div>
|
|
<div class="card-body">
|
|
<div class="table-responsive">
|
|
<table class="table table-bordered table-hover">
|
|
<thead class="table-light">
|
|
<tr>
|
|
<th>Nama Sales</th>
|
|
<th>Total Kuantitas</th>
|
|
<th>Total Omzet</th>
|
|
<th>Harga Rata-rata</th>
|
|
<th>Jumlah Transaksi</th>
|
|
</tr>
|
|
</thead>
|
|
<tbody>
|
|
<?php foreach ($analysis_results as $row): ?>
|
|
<tr>
|
|
<td><?php echo htmlspecialchars($row['NAMA_SALES']); ?></td>
|
|
<td><?php echo number_format($row['total_qty']); ?></td>
|
|
<td>Rp <?php echo number_format($row['total_omzet'], 2, ",", "."); ?></td>
|
|
<td>Rp <?php echo number_format($row['avg_price'], 2, ",", "."); ?></td>
|
|
<td><?php echo number_format($row['transactions_count']); ?></td>
|
|
</tr>
|
|
<?php endforeach; ?>
|
|
</tbody>
|
|
</table>
|
|
</div>
|
|
<div class="mt-4">
|
|
<canvas id="salesChart"></canvas>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
<?php elseif ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['run_analysis'])): ?>
|
|
<div class="card mb-4" id="analysis-results">
|
|
<div class="card-header">
|
|
Hasil Analisa
|
|
</div>
|
|
<div class="card-body">
|
|
<div class="alert alert-info">Tidak ada data yang cocok dengan filter yang dipilih.</div>
|
|
</div>
|
|
</div>
|
|
<?php endif; ?>
|
|
|
|
<script>
|
|
if (typeof Chart !== 'undefined' && <?php echo !empty($analysis_results) ? 'true' : 'false'; ?>) {
|
|
const ctx = document.getElementById('salesChart');
|
|
new Chart(ctx, {
|
|
type: 'bar',
|
|
data: {
|
|
labels: <?php echo $chart_labels_json; ?>,
|
|
datasets: [{
|
|
label: 'Total Omzet',
|
|
data: <?php echo $chart_data_json; ?>,
|
|
backgroundColor: 'rgba(13, 110, 253, 0.5)',
|
|
borderColor: 'rgba(13, 110, 253, 1)',
|
|
borderWidth: 1
|
|
}]
|
|
},
|
|
options: {
|
|
scales: {
|
|
y: {
|
|
beginAtZero: true,
|
|
ticks: {
|
|
callback: function(value, index, values) {
|
|
return 'Rp ' + new Intl.NumberFormat('id-ID').format(value);
|
|
}
|
|
}
|
|
}
|
|
},
|
|
plugins: {
|
|
tooltip: {
|
|
callbacks: {
|
|
label: function(context) {
|
|
let label = context.dataset.label || '';
|
|
if (label) {
|
|
label += ': ';
|
|
}
|
|
if (context.parsed.y !== null) {
|
|
label += 'Rp ' + new Intl.NumberFormat('id-ID').format(context.parsed.y);
|
|
}
|
|
return label;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
});
|
|
}
|
|
</script>
|
|
|
|
<?php require_once 'footer.php'; ?>
|