125 lines
4.1 KiB
PHP
125 lines
4.1 KiB
PHP
<?php
|
|
error_reporting(E_ERROR | E_PARSE);
|
|
include('includes/config/config.php');
|
|
include ('includes/functions.php');
|
|
include ('log_entry.php');
|
|
error_log ( "Start Printing Request Attributes" );
|
|
$requestStr="";
|
|
foreach ( $_REQUEST as $key => $value ) {
|
|
$requestStr.=$key . " : " . $value . "\n";
|
|
error_log ( $key . " : " . $value . "<br />\r\n" );
|
|
}
|
|
error_log ( "End Printing Request Attributes" );
|
|
save_log($requestStr,'Manage Item','SAVE','save_item.php');
|
|
|
|
// Only accept POST requests
|
|
if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
|
|
sendJsonResponse(false, 'Invalid request method');
|
|
}
|
|
|
|
// Get and sanitize input
|
|
$history_id = isset($_POST['history_id']) ? intval($_POST['history_id']) : 0;
|
|
$old_rate = isset($_POST['old_rate']) ? floatval($_POST['old_rate']) : 0;
|
|
$new_rate = isset($_POST['new_rate']) ? floatval($_POST['new_rate']) : 0;
|
|
$remarks = isset($_POST['remarks']) ? $conn->real_escape_string(trim($_POST['remarks'])) : '';
|
|
$modified_date = isset($_POST['modified_date']) ? $conn->real_escape_string(trim($_POST['modified_date'])) : '';
|
|
|
|
// Validation
|
|
if ($history_id <= 0) {
|
|
sendJsonResponse(false, 'Invalid history ID');
|
|
}
|
|
|
|
// If modified_date is empty, use current date
|
|
if (empty($modified_date)) {
|
|
$modified_date = date('Y-m-d H:i:s');
|
|
} else {
|
|
// Convert date format if needed (DD-MM-YYYY to YYYY-MM-DD)
|
|
if (preg_match('/^(\d{2})-(\d{2})-(\d{4})/', $modified_date)) {
|
|
$date_parts = explode('-', $modified_date);
|
|
if (count($date_parts) === 3) {
|
|
$modified_date = $date_parts[2] . '-' . $date_parts[1] . '-' . $date_parts[0];
|
|
// Add time if not present
|
|
if (strlen($modified_date) <= 10) {
|
|
$modified_date .= ' ' . date('H:i:s');
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// Get current user (modify according to your session)
|
|
session_start();
|
|
$modified_by = isset($_SESSION['user_id']) ? intval($_SESSION['user_id']) : 1;
|
|
$modified_by_name = isset($_SESSION['username']) ? $_SESSION['username'] : 'Admin';
|
|
|
|
// Update query
|
|
$sql = "UPDATE item_rate_change_history SET
|
|
old_rate = ?,
|
|
new_rate = ?,
|
|
remarks = ?,
|
|
modified_by = ?,
|
|
last_modified = ?
|
|
WHERE id = ?";
|
|
|
|
$stmt = $conn->prepare($sql);
|
|
|
|
if (!$stmt) {
|
|
sendJsonResponse(false, 'Database prepare error: ' . $conn->error);
|
|
}
|
|
|
|
$stmt->bind_param("ddsssi", $old_rate, $new_rate, $remarks, $modified_by, $modified_date, $history_id);
|
|
|
|
if ($stmt->execute()) {
|
|
if ($stmt->affected_rows > 0) {
|
|
// Get updated record details
|
|
$details_sql = "SELECT last_modified FROM item_rate_change_history WHERE id = ?";
|
|
$details_stmt = $conn->prepare($details_sql);
|
|
$details_stmt->bind_param("i", $history_id);
|
|
$details_stmt->execute();
|
|
$details_result = $details_stmt->get_result();
|
|
$updated_row = $details_result->fetch_assoc();
|
|
$details_stmt->close();
|
|
|
|
// Format date for response
|
|
$formatted_date = '';
|
|
if ($updated_row && isset($updated_row['last_modified'])) {
|
|
$date_obj = new DateTime($updated_row['last_modified']);
|
|
$formatted_date = $date_obj->format('d-m-Y H:i');
|
|
}
|
|
|
|
sendJsonResponse(true, 'Record updated successfully!', [
|
|
'id' => $history_id,
|
|
'old_rate' => number_format($old_rate, 2),
|
|
'new_rate' => number_format($new_rate, 2),
|
|
'remarks' => $remarks,
|
|
'modified_by' => $modified_by_name,
|
|
'last_modified' => $updated_row['last_modified'] ?? $modified_date,
|
|
'formatted_date' => $formatted_date
|
|
]);
|
|
} else {
|
|
sendJsonResponse(false, 'No changes made or record not found');
|
|
}
|
|
} else {
|
|
sendJsonResponse(false, 'Update failed: ' . $stmt->error);
|
|
}
|
|
|
|
$stmt->close();
|
|
$conn->close();
|
|
|
|
// Function to send JSON response
|
|
function sendJsonResponse($success, $message, $data = []) {
|
|
// Clear any previous output
|
|
if (ob_get_length()) ob_clean();
|
|
|
|
$response = [
|
|
'success' => $success,
|
|
'message' => $message
|
|
];
|
|
|
|
if (!empty($data)) {
|
|
$response['data'] = $data;
|
|
}
|
|
|
|
echo json_encode($response, JSON_UNESCAPED_UNICODE);
|
|
exit;
|
|
}
|
|
?>
|