<?php
// Enable error reporting for debugging purposes (remove in production)
error_reporting(E_ALL);
ini_set('display_errors', 1);

session_start();
include('../config/dbcon.php');

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    // Query to update the current month DM data and transfer it to the previous month
    $updateQuery = "UPDATE projects SET directMaterialcostPrev = directMaterialcost, directMaterialcost = 0";

    // Execute the update query
    $result = mysqli_query($conn, $updateQuery);

    if (!$result) {
        // Handle the database update error
        echo json_encode(['success' => false, 'message' => 'Update failed: ' . mysqli_error($conn)]);
        exit();
    }

    // The transfer was successful
    echo json_encode(['success' => true]);
} else {
    // Handle invalid requests (e.g., direct access to this script)
    echo json_encode(['success' => false, 'message' => 'Invalid request']);
}
?>