<?php
ob_clean();

$sql = "
    SELECT im.*, IFNULL(inv.quantity_in_stock,0) AS quantity_in_stock
    FROM item_management im
    LEFT JOIN inventory inv ON inv.item_id = im.id
    $where
    ORDER BY im.id DESC
";

$rs = mysqli_query($conn, $sql);

$file_name = "export_item_management.csv";

header("Content-type: application/csv");
header("Content-Disposition: attachment; filename=$file_name");
header("Pragma: no-cache");
header("Expires: 0");

$out = fopen("php://output", "w");

// Header row — one column per field on the Add/Edit Item form
fputcsv($out, array(
    "Part Number", "Product Description", "Usual Source", "Part Status",
    "Stock UOM", "Customer Code", "Type", "Spec", "Material Grain",
    "Grade", "Color", "Gloss Level", "Length (mm)", "Width (mm)", "Thickness (mm)",
    "Barcode", "Use Both Tables", "Replica Wood", "Rep Wood Color", "Rep Wood Width",
    "Product Group", "Receipt/Issue Location", "Part Weight", "Recycle Code",
    "Drawing Number", "Drawing Issue", "Man Lead Time (Days)", "Minimum EBQ", "Maximum EBQ",
    "Standard Cost EBQ (GBP)", "Spot Cost", "UPH", "CNC Program No",
    "Master Sample Location", "Vac Tool Location", "CNC Tool Location", "Checking Fixture Location",
    "Standard Cost (GBP)", "Sale Price", "Preferred Supplier", "Purchase Lead Time",
    "Purchase Price", "Minimum Quantity", "Nominal Code Sales", "Nominal Code Purchase",
    "Quantity In Stock"
));

while ($row = mysqli_fetch_assoc($rs)) {
    $row = utility::validate_crypt_data($row);

    fputcsv($out, array(
        $row['part_number'],
        $row['product_description'],
        $row['usual_source'],
        $row['part_status'],
        $row['stock_uom'],
        $row['customer_code'],
        $gsi->getRawMaterialTypeById($row['type']),
        Keycode::getkeycodeById($row['spec']),
        Keycode::getkeycodeById($row['grain']),
        $row['grade'],
        $row['color'],
        $row['gross'],
        $row['length_mm'],
        $row['width_mm'],
        $row['thickness'],
        $row['scrap_percent'], // Barcode field
        ($row['use_both_tables'] == '1' ? 'Yes' : 'No'),
        ($row['replica_wood'] == '1' ? 'Yes' : 'No'),
        ($row['replica_wood'] == '1' ? Keycode::getkeycodeById($row['color_rep_wood']) : ''),
        ($row['replica_wood'] == '1' ? $row['rep_wood_length'] : ''),
        $gsi->getProductGroupById($row['product_group']),
        $gsi->getReceipt_LocationById($row['mpe']),
        $row['net'],
        $row['recycle_code'],
        $row['drawing_number'],
        $row['drawing_issue'],
        $row['man_lead_time'],
        $row['price_uom'],   // Minimum EBQ
        $row['mks'],         // Maximum EBQ
        $row['standard_cost_ebq'],
        $row['spot_cost'],
        $row['uph'],
        $row['cnc_program_no'],
        $row['master_sample_location'],
        $row['tool_location'],
        $row['packing_code'], // CNC Tool Location
        $row['checking_fixture_location'],
        $row['standard_cost'],
        $row['unit_cost'],    // Sale Price
        $gsi->getSuppliersById($row['type_2']), // Preferred Supplier
        $row['uom'],           // Purchase Lead Time
        $row['scrap_value'],   // Purchase Price
        $row['minimum_quantity'],
        $row['nominal_code_sales'],
        $row['nominal_code_purchase'],
        $row['quantity_in_stock'],
    ));
}

fclose($out);
die;
?>