<?php
// Memerlukan file load.php yang berisi koneksi database dan fungsi-fungsi penting
require_once('includes/load.php');

// Memeriksa tingkat akses pengguna (minimal level 2)
page_require_level(2);

// Mengambil semua data permintaan pembelian
$purchaseRequests = get_all_purchase_requests();

// Memeriksa apakah terdapat permintaan POST untuk menambahkan receipt
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['add_receipt'])) {
    $request_id = (int)$_POST['request_id'];
    $quantity = (int)$_POST['quantity'];

    // Mencari permintaan pembelian berdasarkan ID
    $purchase_request = find_by_id('purchase_requests', $request_id);

    if ($purchase_request) {
        // Memastikan jumlah receipt tidak melebihi jumlah permintaan
        if ($quantity > $purchase_request['quantity']) {
            $session->msg('d', 'Jumlah receipt tidak boleh lebih besar dari jumlah permintaan.');
            redirect('add_receipt.php');
        }

        $product_id = $purchase_request['product_id'];

        // Menyimpan catatan receipt
        $sql = "INSERT INTO receipts (request_id, quantity) VALUES ($request_id, $quantity)";
        $db->query($sql);

        // Mengurangkan jumlah receipt dari saldo produk
        $product = find_by_id('products', $product_id);
        $new_balance = $product['balance'] - $quantity;
        $sql = "UPDATE products SET balance = $new_balance WHERE id = $product_id";
        $db->query($sql);

        $session->msg('s', 'Receipt telah ditambahkan.');
        redirect('add_receipt.php');
    } else {
        $session->msg('d', 'Permintaan pembelian tidak ditemukan.');
        redirect('add_receipt.php');
    }
}

// Menyusun tampilan halaman
$page_title = 'Tambahkan Receipt';
include_once('layouts/header.php');
?>

<!-- Tampilan Formulir -->
<div class="row">
    <div class="col-md-12">
        <?php echo display_msg($msg); ?>
    </div>
    <div class="col-md-12">
        <div class="panel panel-default">
            <div class="panel-heading clearfix">
                <h4 class="panel-title">Tambahkan Receipt</h4>
            </div>
            <div class="panel-body">
                <form method="post">
                    <div class="form-group">
                        <label for="request_id">Pilih Permintaan Pembelian</label>
                        <select class="form-control" name="request_id">
                            <?php foreach ($purchaseRequests as $request) : ?>
                                <option value="<?php echo $request['id']; ?>">
                                    <?php echo get_part_number_by_product_id($request['product_id']) . ' - ' . get_product_name_by_id($request['product_id']); ?>
                                </option>
                            <?php endforeach; ?>
                        </select>
                    </div>
                    <div class="form-group">
                        <label for="quantity">Jumlah Receipt</label>
                        <input type="number" class="form-control" name="quantity" required>
                    </div>
                    <button type="submit" name="add_receipt" class="btn btn-primary">Tambahkan Receipt</button>
                </form>
            </div>
        </div>
    </div>
</div>

<!-- Menyisipkan footer -->
<?php include_once('layouts/footer.php'); ?>
