<?php
$page_title = 'Calculate HPP';
require_once('includes/load.php');
page_require_level(2);

$hpp_result = null;
$part_number = '';

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    if (!empty($_POST['part_number'])) {
        $part_number = $db->escape($_POST['part_number']);
        $hpp_result = calculate_hpp_per_part_number($part_number);
    }
}

// Fungsi untuk menghitung HPP berdasarkan part_number
function calculate_hpp_per_part_number($part_number) {
    global $db;
    
    // Mendapatkan harga beli produk dari tabel products
    $sql_product = "SELECT buy_price FROM products WHERE part_number = '{$part_number}' AND categorie_id = 7";
    $result_product = $db->query($sql_product);
    
    if ($db->num_rows($result_product) == 0) {
        return 0; // Tidak ada produk dengan part_number dan kategori yang sesuai
    }
    
    // Mengambil harga beli produk dasar
    $product = $db->fetch_assoc($result_product);
    $base_price = $product['buy_price'];

    // Menghitung total biaya berdasarkan struktur BOM
    $total_cost = calculate_bom_cost($part_number, $base_price);

    return $total_cost;
}

// Fungsi rekursif untuk menghitung biaya BOM
function calculate_bom_cost($part_number, $base_price) {
    global $db;

    // Mendapatkan struktur BOM untuk part_number
    $sql_bom = "SELECT child_bom, qty, level FROM bom_level WHERE parent_bom = '{$part_number}'";
    $result_bom = $db->query($sql_bom);
    
    $total_cost = 0;

    while ($bom_item = $db->fetch_assoc($result_bom)) {
        $child_part_number = $bom_item['child_bom'];
        $quantity = $bom_item['qty'];
        $level = $bom_item['level'];

        // Mendapatkan harga beli dari child_bom jika child_bom adalah produk kategori 7
        $sql_child_product = "SELECT buy_price FROM products WHERE part_number = '{$child_part_number}' AND categorie_id = 7";
        $result_child_product = $db->query($sql_child_product);
        
        if ($db->num_rows($result_child_product) > 0) {
            $child_product = $db->fetch_assoc($result_child_product);
            $child_price = $child_product['buy_price'];
        } else {
            // Jika child_bom tidak ditemukan dalam kategori 7, anggap harga adalah 0
            $child_price = 0;
        }

        // Menghitung total biaya untuk item BOM
        $total_cost += $quantity * $child_price;

        // Jika item adalah level lebih tinggi, tambahkan biaya dari item BOM ke total biaya
        if ($level > 1) {
            $total_cost += $quantity * $base_price;
        }

        // Rekursif untuk menghitung biaya dari sub-BOM
        $total_cost += $quantity * calculate_bom_cost($child_part_number, $child_price);
    }

    return $total_cost;
}

include_once('layouts/header.php');
?>

<div class="row">
    <div class="col-md-12">
        <div class="panel panel-default">
            <div class="panel-heading">
                <h3 class="panel-title">Calculate HPP</h3>
            </div>
            <div class="panel-body">
                <form action="calculate_hpp.php" method="post" class="form-inline">
                    <div class="form-group">
                        <label for="part_number">Part Number:</label>
                        <input type="text" class="form-control" name="part_number" id="part_number" placeholder="Enter part number" value="<?php echo htmlspecialchars($part_number); ?>">
                    </div>
                    <button type="submit" name="calculate_hpp" class="btn btn-primary">Calculate HPP</button>
                </form>
                <br>
                <?php if ($hpp_result !== null) : ?>
                    <div class="alert alert-info">
                        <strong>HPP for part number <?php echo htmlspecialchars($part_number); ?>:</strong> 
                        <?php echo number_format($hpp_result, 2); ?>
                    </div>
                <?php endif; ?>
            </div>
        </div>
    </div>
</div>

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