<?php




require_once __DIR__ . '/../magic/auth.php';

$auth = bdr_require_privilege(30, '../logout.php');
$user = $auth['user_id'];
$father = $auth['user_id'];
$branch = $auth['branch'];
$conn = $auth['conn'];
$db = $auth['conn'];

$myday = $_GET["dd"] ?? date("Y-m-d");

try {
    $mmm = new DateTime($myday);
} catch (Exception $e) {
    $mmm = new DateTime();
}

$first_day = $mmm->format('Y-m-01');
$last_day  = $mmm->format('Y-m-t');
$mms       = $mmm->format("m Y");
?>
<!DOCTYPE html>
<html lang="ar" dir="rtl">

<head>
    <meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
    <title>مصروفات السيارات</title>
    <link rel="stylesheet" href="../dist/css/bootstrap.min.css" />
    <link rel="stylesheet" href="../dist/css/day.css" />
</head>

<body onload="window.print(); setTimeout(window.close, 1000);">
    <div class="container-fluid">
        <div class="row" style="padding:5px;">
            <div class="col-9">
                <h6 style="font-weight:bold;padding:3px;font-size:10pt;">مصروفات شهر <?php echo $mms; ?></h6>
            </div>
            <div class="col-3">
                <p style="font-size:8pt;float:left;"><?php echo date("Y-m-d"); ?></p>
            </div>
        </div>

        <?php
        $dsd = $conn->prepare("
    SELECT carid, kind, model, CONCAT(letters,' ',numbers) AS PL, color
    FROM cars
    WHERE carid IN (
        SELECT DISTINCT carid
        FROM car_ment_items
        WHERE date_out BETWEEN ? AND ?
    )
    ORDER BY kind
");
        $dsd->bindParam(1, $first_day);
        $dsd->bindParam(2, $last_day);
        $dsd->execute();

        if ($dsd->rowCount() > 0) {
            $master = 0;
            $qq = 1;

            while ($row = $dsd->fetch(PDO::FETCH_ASSOC)) {
                $carid = $row["carid"];
                $kind  = $row["kind"];
                $model = $row["model"];
                $PL    = $row["PL"];
                $color = $row["color"];

                echo "<h6 style='font-weight:bold;padding:3px;font-size:9pt;'>"
                    . $qq . "~ " . htmlspecialchars($kind)
                    . " لوحة رقم " . htmlspecialchars($PL)
                    . " اللون " . htmlspecialchars($color)
                    . " " . htmlspecialchars($model)
                    . "</h6>";
        ?>

                <table class="table table-bordered">
                    <thead>
                        <tr>
                            <th class="align-middle text-center" width="7%">المبلغ</th>
                            <th class="align-middle text-center" width="35%">اسم الصنف</th>
                            <th class="align-middle text-center" width="5%">الكمية</th>
                            <th class="align-middle text-center" width="23%">المورد</th>
                            <th class="align-middle text-center" width="5%">الصرف</th>
                            <th class="align-middle text-center" width="10%">رقم الفاتورة</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php
                        $money = 0;
                        $output = '';

                        $sql = $conn->prepare("
                 SELECT 
    item_line_out.tot_price,
    item_line_out.qty,
    item_line_out.date_out,
    items.item_name,
    COALESCE(material_supp.mat_name, '') AS mat_name,
    material_in.invoice_no
FROM item_line_out
JOIN items 
    ON item_line_out.item_id = items.item_id
JOIN items_line 
    ON item_line_out.line_in = items_line.line_id
JOIN material_in 
    ON items_line.mat_in_id = material_in.mat_in_id
LEFT JOIN material_supp 
    ON material_in.mat_supp_id = material_supp.mat_supp_id
WHERE item_line_out.req_id IN (
    SELECT req_id 
    FROM ment_request 
    WHERE car_id = ?
)
AND item_line_out.date_out BETWEEN ? AND ?
            ");
                        $sql->bindParam(1, $carid, PDO::PARAM_INT);
                        $sql->bindParam(2, $first_day);
                        $sql->bindParam(3, $last_day);
                        $sql->execute();

                        if ($sql->rowCount() > 0) {
                            while ($row2 = $sql->fetch(PDO::FETCH_ASSOC)) {
                                $date_out = date_create($row2['date_out']);
                                $date_out = $date_out ? date_format($date_out, "m/d") : '';

                                $output .= '<tr>
                        <td class="text-center">' . $row2['tot_price'] . '</td>
                        <td>' . htmlspecialchars($row2['item_name']) . '</td>
                        <td class="text-center">' . $row2['qty'] . '</td>
                        <td>' . htmlspecialchars($row2['mat_name']) . '</td>
                        <td class="text-center">' . $date_out . '</td>
                        <td class="text-center">' . htmlspecialchars($row2['invoice_no']) . '</td>
                    </tr>';

                                $money += $row2['tot_price'];
                            }

                            $output .= '<tr>
                    <td class="text-center"><strong>' . $money . '</strong></td>
                    <td colspan="5"><strong>إجمالي</strong></td>
                </tr>';
                        } else {
                            $output .= '<tr><td colspan="6" class="text-center">لا يوجد سجلات ...</td></tr>';
                        }

                        echo $output;
                        ?>
                    </tbody>
                </table>

        <?php
                $master += $money;
                $qq++;
            }

            echo "<h6 style='font-weight:bold;padding:3px;font-size:10pt;'>إجمالي مصروفات السيارات " . $master . "</h6>";
        }
        ?>
    </div>

    <script src="../js/jquery.js"></script>
    <script src="../dist/js/umd/popper.min.js"></script>
    <script src="../dist/js/bootstrap.js"></script>
</body>

</html>