<?php
include 'config.php';

$tgl_awal = $_GET['tgl_awal'] ?? date('Y-m-01');
$tgl_akhir = $_GET['tgl_akhir'] ?? date('Y-m-d');
$jenis = $_GET['jenis'] ?? 'all';

// Get report data based on type
if($jenis == 'retur') {
    // Get retur data
    $data_retur = mysqli_query($conn, "SELECT r.*, p.nama_produk FROM retur_barang r JOIN produk p ON r.idproduk = p.idproduk WHERE DATE(r.tanggal_retur) BETWEEN '$tgl_awal' AND '$tgl_akhir' ORDER BY r.idretur ASC");
    $total_retur = 0;
    while($retur = mysqli_fetch_assoc($data_retur)) {
        $total_retur += $retur['subtotal'];
    }
} else if($jenis == 'penjualan') {
    // Get sales data only
    $data_produk = mysqli_query($conn,"SELECT * FROM laporan l, pelanggan e WHERE e.idpelanggan=l.idpelanggan AND DATE(l.tgl_sub) BETWEEN '$tgl_awal' AND '$tgl_akhir' ORDER BY idlaporan ASC");
    $total_penjualan = 0;
    while($d = mysqli_fetch_array($data_produk)){
        $total_penjualan += $d['totalbeli'];
    }
} else {
    // For 'all' type, get both sales and retur data
    $data_produk = mysqli_query($conn,"SELECT * FROM laporan l, pelanggan e WHERE e.idpelanggan=l.idpelanggan AND DATE(l.tgl_sub) BETWEEN '$tgl_awal' AND '$tgl_akhir' ORDER BY idlaporan ASC");
    $total_penjualan = 0;
    while($d = mysqli_fetch_array($data_produk)){
        $total_penjualan += $d['totalbeli'];
    }
    
    // Get retur data for the same period
    $data_retur = mysqli_query($conn, "SELECT r.*, p.nama_produk FROM retur_barang r JOIN produk p ON r.idproduk = p.idproduk WHERE DATE(r.tanggal_retur) BETWEEN '$tgl_awal' AND '$tgl_akhir' ORDER BY r.idretur ASC");
    $total_retur = 0;
    while($retur = mysqli_fetch_assoc($data_retur)) {
        $total_retur += $retur['subtotal'];
    }
    
    $total_penjualan_bersih = $total_penjualan - $total_retur;
}
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Laporan Penjualan</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            margin: 20px;
        }
        .header {
            text-align: center;
            margin-bottom: 20px;
        }
        .header h1 {
            margin: 0;
            font-size: 24px;
        }
        .header p {
            margin: 5px 0;
            font-size: 14px;
        }
        .filter-info {
            margin-bottom: 20px;
            padding: 10px;
            background-color: #f5f5f5;
            border-radius: 5px;
        }
        table {
            width: 100%;
            border-collapse: collapse;
            margin-bottom: 20px;
        }
        th, td {
            border: 1px solid #ddd;
            padding: 8px;
            text-align: left;
        }
        th {
            background-color: #f2f2f2;
        }
        .total-row {
            background-color: #f9f9f9;
            font-weight: bold;
        }
        .text-right {
            text-align: right;
        }
        .text-center {
            text-align: center;
        }
        .summary-box {
            display: inline-block;
            width: 200px;
            margin: 10px;
            padding: 10px;
            border: 1px solid #ddd;
            border-radius: 5px;
        }
    </style>
</head>
<body onload="window.print()">
    <div class="header">
        <h1>LAPORAN <?php echo strtoupper($jenis == 'retur' ? 'RETUR' : ($jenis == 'penjualan' ? 'PENJUALAN' : 'PENJUALAN & RETUR')); ?></h1>
        <p><?php echo $toko ?></p>
        <p><?php echo $alamat ?></p>
        <p>Tel: <?php echo $telp ?></p>
    </div>
    
    <div class="filter-info">
        <p><strong>Periode:</strong> <?php echo date('d M Y', strtotime($tgl_awal)); ?> - <?php echo date('d M Y', strtotime($tgl_akhir)); ?></p>
        <p><strong>Jenis Laporan:</strong> <?php echo $jenis == 'retur' ? 'Retur Barang' : ($jenis == 'penjualan' ? 'Penjualan' : 'Penjualan & Retur'); ?></p>
    </div>
    
    <?php if($jenis == 'retur'): ?>
        <h3>Detail Retur Barang</h3>
        <table>
            <thead>
                <tr>
                    <th>No</th>
                    <th>No. Retur</th>
                    <th>No. Nota</th>
                    <th>Produk</th>
                    <th>Qty</th>
                    <th>Subtotal</th>
                    <th>Tanggal</th>
                    <th>Keterangan</th>
                </tr>
            </thead>
            <tbody>
                <?php
                $no = 1;
                mysqli_data_seek($data_retur, 0); // Reset result pointer
                while($retur = mysqli_fetch_assoc($data_retur)):
                ?>
                <tr>
                    <td><?php echo $no++; ?></td>
                    <td>RET<?php echo str_pad($retur['idretur'], 6, "0", STR_PAD_LEFT); ?></td>
                    <td><?php echo $retur['no_nota']; ?></td>
                    <td><?php echo $retur['nama_produk']; ?></td>
                    <td class="text-center"><?php echo $retur['quantity']; ?></td>
                    <td class="text-right">Rp. <?php echo ribuan($retur['subtotal']); ?></td>
                    <td><?php echo $retur['tanggal_retur']; ?></td>
                    <td><?php echo $retur['keterangan']; ?></td>
                </tr>
                <?php endwhile; ?>
                <tr class="total-row">
                    <td colspan="5" class="text-right">TOTAL RETUR:</td>
                    <td class="text-right">Rp. <?php echo ribuan($total_retur); ?></td>
                    <td colspan="2"></td>
                </tr>
            </tbody>
        </table>
    <?php elseif($jenis == 'penjualan'): ?>
        <h3>Detail Penjualan</h3>
        <table>
            <thead>
                <tr>
                    <th>No</th>
                    <th>No. Nota</th>
                    <th>Pelanggan</th>
                    <th>Qty</th>
                    <th>SubTotal</th>
                    <th>Pembayaran</th>
                    <th>Kembalian</th>
                    <th>Tanggal</th>
                </tr>
            </thead>
            <tbody>
                <?php
                $no = 1;
                mysqli_data_seek($data_produk, 0); // Reset result pointer
                while($d = mysqli_fetch_array($data_produk)):
                    $nota = $d['no_nota'];
                    $itungtrans = mysqli_query($conn,"SELECT SUM(quantity) as jumlahtrans FROM tb_nota where no_nota='$nota'");
                    $itungtrans2 = mysqli_fetch_assoc($itungtrans);
                    $itungtrans3 = $itungtrans2['jumlahtrans'];
                ?>
                <tr>
                    <td><?php echo $no++; ?></td>
                    <td><?php echo $d['no_nota']; ?></td>
                    <td><?php echo $d['nama_pelanggan']; ?></td>
                    <td class="text-center"><?php echo $itungtrans3; ?></td>
                    <td class="text-right">Rp. <?php echo ribuan($d['totalbeli']); ?></td>
                    <td class="text-right">Rp. <?php echo ribuan($d['pembayaran']); ?></td>
                    <td class="text-right">Rp. <?php echo ribuan($d['kembalian']); ?></td>
                    <td><?php echo $d['tgl_sub']; ?></td>
                </tr>
                <?php endwhile; ?>
                <tr class="total-row">
                    <td colspan="4" class="text-right">TOTAL PENJUALAN:</td>
                    <td class="text-right">Rp. <?php echo ribuan($total_penjualan); ?></td>
                    <td colspan="3"></td>
                </tr>
            </tbody>
        </table>
    <?php else: // For 'all' type ?>
        <h3>Detail Penjualan</h3>
        <table>
            <thead>
                <tr>
                    <th>No</th>
                    <th>No. Nota</th>
                    <th>Pelanggan</th>
                    <th>Qty</th>
                    <th>SubTotal</th>
                    <th>Pembayaran</th>
                    <th>Kembalian</th>
                    <th>Tanggal</th>
                </tr>
            </thead>
            <tbody>
                <?php
                $no = 1;
                mysqli_data_seek($data_produk, 0); // Reset result pointer
                while($d = mysqli_fetch_array($data_produk)):
                    $nota = $d['no_nota'];
                    $itungtrans = mysqli_query($conn,"SELECT SUM(quantity) as jumlahtrans FROM tb_nota where no_nota='$nota'");
                    $itungtrans2 = mysqli_fetch_assoc($itungtrans);
                    $itungtrans3 = $itungtrans2['jumlahtrans'];
                ?>
                <tr>
                    <td><?php echo $no++; ?></td>
                    <td><?php echo $d['no_nota']; ?></td>
                    <td><?php echo $d['nama_pelanggan']; ?></td>
                    <td class="text-center"><?php echo $itungtrans3; ?></td>
                    <td class="text-right">Rp. <?php echo ribuan($d['totalbeli']); ?></td>
                    <td class="text-right">Rp. <?php echo ribuan($d['pembayaran']); ?></td>
                    <td class="text-right">Rp. <?php echo ribuan($d['kembalian']); ?></td>
                    <td><?php echo $d['tgl_sub']; ?></td>
                </tr>
                <?php endwhile; ?>
                <tr class="total-row">
                    <td colspan="4" class="text-right">TOTAL PENJUALAN:</td>
                    <td class="text-right">Rp. <?php echo ribuan($total_penjualan); ?></td>
                    <td colspan="3"></td>
                </tr>
            </tbody>
        </table>
        
        <h3>Detail Retur Barang</h3>
        <table>
            <thead>
                <tr>
                    <th>No</th>
                    <th>No. Retur</th>
                    <th>No. Nota</th>
                    <th>Produk</th>
                    <th>Qty</th>
                    <th>Subtotal</th>
                    <th>Tanggal</th>
                    <th>Keterangan</th>
                </tr>
            </thead>
            <tbody>
                <?php
                $no = 1;
                mysqli_data_seek($data_retur, 0); // Reset result pointer
                while($retur = mysqli_fetch_assoc($data_retur)):
                ?>
                <tr>
                    <td><?php echo $no++; ?></td>
                    <td>RET<?php echo str_pad($retur['idretur'], 6, "0", STR_PAD_LEFT); ?></td>
                    <td><?php echo $retur['no_nota']; ?></td>
                    <td><?php echo $retur['nama_produk']; ?></td>
                    <td class="text-center"><?php echo $retur['quantity']; ?></td>
                    <td class="text-right">Rp. <?php echo ribuan($retur['subtotal']); ?></td>
                    <td><?php echo $retur['tanggal_retur']; ?></td>
                    <td><?php echo $retur['keterangan']; ?></td>
                </tr>
                <?php endwhile; ?>
                <tr class="total-row">
                    <td colspan="5" class="text-right">TOTAL RETUR:</td>
                    <td class="text-right">Rp. <?php echo ribuan($total_retur); ?></td>
                    <td colspan="2"></td>
                </tr>
            </tbody>
        </table>
        
        <h3>Ringkasan</h3>
        <table>
            <tr>
                <td width="70%"><strong>Total Penjualan:</strong></td>
                <td class="text-right"><strong>Rp. <?php echo ribuan($total_penjualan); ?></strong></td>
            </tr>
            <tr>
                <td><strong>Total Retur:</strong></td>
                <td class="text-right"><strong>Rp. <?php echo ribuan($total_retur); ?></strong></td>
            </tr>
            <tr class="total-row">
                <td><strong>Pendapatan Bersih:</strong></td>
                <td class="text-right"><strong>Rp. <?php echo ribuan($total_penjualan_bersih); ?></strong></td>
            </tr>
        </table>
    <?php endif; ?>
    
    <div style="margin-top: 30px; text-align: center;">
        <p>Dicetak pada: <?php echo date('d M Y H:i:s'); ?></p>
        <p>Terima Kasih Telah Berbelanja Di Toko Kami</p>
    </div>
</body>
</html>