<?php
include("../../../classes/manager.class.php");
//ini_set('display_errors', 1);
//error_reporting(E_ALL);

$m = new Manager();

$datefrom = "";
$dateto = "";
$product_name = "All";

$totalPieces = 0;
$totalCartonDecimal = 0;
$totalSaleAmount = 0;

if (!isset($_SESSION['sess_userid'])) {
    header("Location: " . PROJECT_ADMIN_URL);
    exit();
}

extract($_REQUEST);

// ---- Date handling ----
if ($datefrom == "") {
    $datefrom = date('Y-m-d', strtotime("-30 days"));
} else {
    $datefrom = date("Y-m-d", strtotime($datefrom));
}

if ($dateto == "") {
    $dateto = date('Y-m-d');
} else {
    $dateto = date("Y-m-d", strtotime($dateto));
}

$datefrom = date("d-m-Y", strtotime($datefrom));
$dateto   = date("d-m-Y", strtotime($dateto));

$qryDate = date("Y-m-d", strtotime($datefrom));
$qryTo   = date("Y-m-d", strtotime($dateto));

if (!isset($product_name)) {
    $product_name = 'All';
}

/**
 * Build filter for product wise sales report
 */
$filterReport = "
    WHERE DATE_FORMAT(inv.create_date,'%Y-%m-%d') >= '$qryDate'
    AND DATE_FORMAT(inv.create_date,'%Y-%m-%d') <= '$qryTo'
    AND inv.status NOT IN('Draft', 'Cancel')
";

if ($product_name != '' && $product_name != 'All') {
    $filterReport .= " AND invPr.product_id = '$product_name' ";
}

$filterReport .= "
    GROUP BY 
        invPr.product_id,
        ps.code,
        ps.name,
        ps.weight,
        ps.unit,
        carton_piece

    ORDER BY 
        ps.name ASC
";

$qry = Invoice::GetProductWiseSalesReport($filterReport);
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <title>Product Wise Sales Report</title>

    <style>
        body {
            font-family: Arial, Helvetica, sans-serif;
            font-size: 12px;
            color: #000;
            margin: 20px;
        }

        h2, h3, h4 {
            margin: 5px 0;
            text-align: center;
        }

        .report-header {
            width: 100%;
            margin-bottom: 15px;
            text-align: center;
        }

        .period {
            text-align: center;
            margin-bottom: 15px;
            font-weight: bold;
        }

        table {
            width: 100%;
            border-collapse: collapse;
        }

        table th {
            border: 1px solid #000;
            padding: 6px;
            background: #e5e5e5;
            font-weight: bold;
            text-align: center;
        }

        table td {
            border: 1px solid #000;
            padding: 5px;
        }

        .text-right {
            text-align: right;
        }

        .text-center {
            text-align: center;
        }

        .total-row th {
            border: 1px solid #000;
            background: #f2f2f2;
            font-weight: bold;
        }

        @media print {
            body {
                margin: 10px;
            }

            table th {
                background: #e5e5e5 !important;
                -webkit-print-color-adjust: exact;
                print-color-adjust: exact;
            }
        }
    </style>
</head>

<body onload="window.print();">

<div class="report-header">
    <h2>Product Wise Sales Report</h2>
    <div class="period">
        Period: <?= $datefrom ?> To <?= $dateto ?>
    </div>

    <?php
    if ($product_name != '' && $product_name != 'All') {
        $productObj = ProductSetup::GetProductSetupById($product_name);
        if (!empty($productObj)) {
            echo "<h4>" . $productObj->name . " - Product</h4>";
        }
    }
    ?>
</div>

<table>
    <thead>
    <tr>
        <th>S.No</th>
        
        <th>Product Name</th>
        <th>Weight</th>
        <th>Unit</th>
        <th>Pieces / Carton</th>
        <th>Total Pieces Sold</th>
       
        <th>Carton</th>
        <th>Gross Sale Amount</th>
    </tr>
    </thead>

    <tbody>
    <?php
    $counter = 0;

    if ($qry) {
        foreach ($qry as $qery) {
            $counter++;

            $cartonDisplay = number_format($qery->full_cartons, 0) . ' Ctn';

            if ($qery->loose_pieces > 0) {
                $cartonDisplay .= ' + ' . number_format($qery->loose_pieces, 0) . ' Pcs';
            }

            $totalPieces += $qery->total_pieces;
            $totalCartonDecimal += $qery->carton_decimal;
            $totalSaleAmount += $qery->gross_sale_amount;
            ?>
            <tr>
                <td class="text-center"><?= $counter ?></td>
                
                <td><?= $qery->product_name ?></td>
                <td><?= $qery->product_weight ?></td>
                <td><?= $qery->product_unit ?></td>
                <td class="text-right"><?= number_format($qery->carton_piece, 0) ?></td>
                <td class="text-right"><?= number_format($qery->total_pieces, 0) ?></td>
               
                <td class="text-right"><?= number_format($qery->carton_decimal, 2, '.', '') ?></td>
                <td class="text-right"><?= number_format($qery->gross_sale_amount, 2, '.', '') ?></td>
            </tr>
            <?php
        }
    } else {
        ?>
        <tr>
            <td colspan="10" class="text-center">No record found.</td>
        </tr>
        <?php
    }
    ?>

    <tr class="total-row">
      	<th></th>
       <th></th>
       <th></th>
                          <th></th>
                         
        <th colspan="0" class="text-right">Total</th>
        <th class="text-right"><?= number_format($totalPieces, 0) ?></th>
     
        <th class="text-right"><?= number_format($totalCartonDecimal, 2, '.', '') ?></th>
        <th class="text-right"><?= number_format($totalSaleAmount, 2, '.', '') ?></th>
    </tr>
    </tbody>
</table>

</body>
</html>