| Server IP : 13.126.101.145 / Your IP : 216.73.217.50 Web Server : Apache/2.4.52 (Ubuntu) System : Linux ip-11-115-0-196 6.8.0-1039-aws #41~22.04.1-Ubuntu SMP Thu Sep 11 10:54:48 UTC 2025 x86_64 User : www-data ( 33) PHP Version : 8.3.17 Disable Function : NONE MySQL : OFF | cURL : ON | WGET : ON | Perl : ON | Python : OFF | Sudo : ON | Pkexec : ON Directory : /var/www/html/rentals_updated/wp-content/plugins/operators/ |
Upload File : |
<?php
// Ensure the file is not accessed directly
if (!defined('ABSPATH')) {
exit;
}
// Add admin menu
add_action('admin_menu', 'expenses_admin_menu');
function expenses_admin_menu() {
add_menu_page(
'Expenses Request', // Page title
'Expenses Request', // Menu title
'manage_options', // Capability
'expenses-dashboard', // Menu slug
'expenses_list',// Callback function
'dashicons-media-interactive', // Icon URL
8 // Position
);
// Add a submenu page
add_submenu_page(
'expenses-dashboard', // The slug name for the parent menu
'Create Expenses Request', // The text to be displayed in the title tags of the page when the submenu is selected
'Create Expenses Request', // The text to be used for the submenu
'manage_options', // The capability required for this menu to be displayed to the user
'create_expenses', // The slug name to refer to this submenu
'create_expenses' // The function to be called to output the content for this page
);
add_submenu_page(null, //parent slug
'Edit Expenses Request', //page title
'Edit', //menu title
'manage_options', //capability
'edit-expenses', //menu slug
'edit_expenses'); //function
/* add_submenu_page(null, //parent slug
'Delete Operator', //page title
'Delete', //menu title
'manage_options', //capability
'delete-operator', //menu slug
'delete_operator'); //function */
}
function expenses_enqueue_custom_styles_and_scripts($hook) {
// Load only on the logsheet dashboard page
if ($hook !== 'toplevel_page_expenses-dashboard') {
return;
}
// Enqueue Bootstrap CSS
wp_enqueue_style('bootstrap-css', 'https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css');
// Enqueue DataTables CSS
wp_enqueue_style('datatables-css', 'https://cdn.datatables.net/1.10.24/css/jquery.dataTables.min.css');
// Enqueue Font Awesome CSS
wp_enqueue_style('fontawesome-css', 'https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.3/css/all.min.css');
// Enqueue your custom CSS
wp_enqueue_style('rfq-custom-styles', plugin_dir_url(__FILE__) . 'css/custom-style.css');
// Enqueue jQuery and DataTables JS
wp_enqueue_script('jquery');
wp_enqueue_script('datatables-js', 'https://cdn.datatables.net/1.10.24/js/jquery.dataTables.min.js', array('jquery'), '1.10.24', true);
// Enqueue Bootstrap JS and your custom JS
wp_enqueue_script('bootstrap-js', 'https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js', array('jquery'), '4.5.2', true);
wp_enqueue_script('rfq-custom-scripts', plugin_dir_url(__FILE__) . 'js/custom-script.js', array('jquery'), '1.0', true);
}
add_action('admin_enqueue_scripts', 'expenses_enqueue_custom_styles_and_scripts');
// Display the data
function expenses_list() {
global $wpdb;
$user = wp_get_current_user();
if ( in_array( 'operator', (array) $user->roles ) ) {
$table_name = $wpdb->prefix . 'expenses_request';
$user_id = $user->ID;
$results = $wpdb->get_results("SELECT * FROM $table_name where operator_id=$user_id order by id desc");
} else if(in_array( 'administrator', (array) $user->roles ) || in_array( 'central_administrator', (array) $user->roles )){
$table_name = $wpdb->prefix . 'expenses_request';
$results = $wpdb->get_results("SELECT * FROM $table_name order by id desc");
}
?>
<div class="wrap rfq-wrap">
<h1 class="wp-heading-inline mb-2 font-weight-bold">Expenses Request</h1>
<div class="table-responsive">
<table id="expenses-table" class="table mb-4 display pb-30 dataTable table-data">
<thead style="background-color:#FFBD2B;">
<tr>
<th scope="row" class="manage-column">Sr. No</th>
<th scope="row" class="manage-column">RMS Contract ID</th>
<th scope="col" class="manage-column">Operator Name</th>
<th scope="col" class="manage-column">Equipment Name</th>
<th scope="col" class="manage-column">Purpose / Reason</th>
<th scope="col" class="manage-column">Amount</th>
<th scope="col" class="manage-column">Status</th>
<th scope="col" class="manage-column">Created On</th>
<th scope="col" class="manage-column">Image</th>
<th scope="col" class="manage-column">Actions</th>
</tr>
</thead>
<tbody>
<?php
$i = 1;
foreach($results as $result){
$user_info = get_userdata($result->operator_id);
$user_name = $user_info->first_name;
//$rms_id = get_exp_order_ids_by_product_id( $result->equipment_id);
?>
<tr>
<td><?php echo $i;?></td>
<td><?php echo $result->order_id;?>
<?php /*
// $rms_id = get_exp_order_ids_by_product_id( $result->equipment_id);
$rms_ids = get_exp_order_ids_by_product_id( $result->equipment_id );
if (is_array($rms_ids)) {
echo implode(', ', $rms_ids);
} else {
echo $rms_ids;
}
foreach($rms_id as $rms_id){
echo $rms_id;} */ ?>
</td>
<td><?php echo $user_name;?></td>
<td>
<?php
$product = wc_get_product( $result->equipment_id );
echo $product ? $product->get_title() : 'N/A';
?>
</td>
<td><?php echo $result->exp_reason;?></td>
<td><?php echo $result->exp_amount;?></td>
<td><?php echo $result->status;?></td>
<td><?php $uk_time = strtotime($result->created_on);
$ist_time = $uk_time + (5 * 3600) + (30 * 60); // Add 5 hours and 30 minutes (in seconds)
echo date('d-m-Y H:i:s', $ist_time);?>
</td>
<td>
<?php /**
$attachment_table = $wpdb->prefix . 'expenses_attachment_docs';
$attachments = $wpdb->get_results($wpdb->prepare("SELECT * FROM $attachment_table WHERE log_id = %d", $result->id));
if (!empty($attachments)) {
$first_image = $attachments[0]->attachment;
echo "<img src='../wp-content/uploads/operator-expenses-request-attachments/{$first_image}' width='50' height='50' class='expenses-image' data-toggle='modal' data-target='#imageModal{$result->id}' style='cursor: pointer;'>";
}
*/?>
<?php
if (!empty($result->attachment)) {
echo "<img src='" . esc_url($result->attachment) . "' width='50' height='50' class='expenses-image' data-toggle='modal' data-target='#imageModal{$result->id}' style='cursor: pointer;'>";
} else {
echo "No Image";
}
?>
</td>
<!-- <td><a href="<?php echo admin_url('admin.php?page=edit-expenses&id=' . $result->id); ?>"><i class="fas fa-edit"></i></a> | <a href="javascript:void(0);" data-toggle="modal" data-target="#servReqModal<?php echo $result->id;?>"><i class="fas fa-eye"></i></a></td> -->
<td>
<?php if (($result->status) === 'Approved') : ?>
<i class="fas fa-edit" style="color: grey; opacity: 0.5; cursor: not-allowed;" title="Editing disabled for completed status"></i>
<?php else : ?>
<a href="<?php echo admin_url('admin.php?page=edit-expenses&id=' . $result->id); ?>">
<i class="fas fa-edit"></i>
</a>
<?php endif; ?>
|
<a href="javascript:void(0);" data-toggle="modal" data-target="#servReqModal<?php echo $result->id; ?>">
<i class="fas fa-eye"></i>
</a>
</td>
</tr>
<!-- Expenses Request Modal -->
<div class="modal fade" id="servReqModal<?php echo esc_attr($result->id); ?>" tabindex="-1" aria-labelledby="exampleModalLabel" aria-hidden="true">
<div class="modal-dialog modal-lg">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title"
id="exampleModalLabel">
Expenses Request Details
</h5>
<button type="button"
class="close"
data-dismiss="modal"
aria-label="Close">
<span aria-hidden="true">
×
</span>
</button>
</div>
<div class="modal-body">
<!-- Data passed is displayed
in this part of the
modal body -->
<h6 id="modal_body">Expenses Request Information</h6>
<div class="row">
<div class="col-md-6">
<div class="col-md-6">
<label>Request Raised Name</label>
</div>
<div class="col-md-6">
<p><?php echo $user_name;?></p>
</div>
</div>
<div class="col-md-6">
<div class="col-md-6">
<label>Equipment Name</label>
</div>
<div class="col-md-6">
<p><?php echo $product->get_title();?></p>
</div>
</div>
<div class="col-md-6">
<div class="col-md-6">
<label>Operator Remarks</label>
</div>
<div class="col-md-6">
<?php /* <p><?php echo $result->problem_description;?></p> */ ?>
<p><?php echo esc_html($result->problem_description); ?></p>
</div>
</div>
<div class="col-md-6">
<div class="col-md-6">
<label>Status</label>
</div>
<div class="col-md-6">
<p><?php echo $result->status;?></p>
</div>
</div>
<div class="col-md-6">
<div class="col-md-6">
<label>Request Raised On</label>
</div>
<div class="col-md-6">
<p><?php echo $result->created_on;?></p>
</div>
</div>
<div class="col-md-6">
<div class="col-md-6">
<label>Admin Remarks</label>
</div>
<div class="col-md-6">
<p><?php echo $result->remarks;?></p>
</div>
</div>
</div>
</div>
</div>
</div></div>
<!-- Image Gallery Modal -->
<div class="modal fade" id="imageModal<?php echo $result->id;?>" tabindex="-1" role="dialog" aria-labelledby="imageModalLabel<?php echo $result->id;?>" aria-hidden="true">
<div class="modal-dialog modal-lg" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="imageModalLabel<?php echo $result->id;?>">Expenses Request Attachment</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<div class="row">
<?php
foreach ($attachments as $attachment) {
echo "<div class='col-md-4 mb-3'>";
echo "<img src='../wp-content/uploads/operator-expenses-request-attachments/{$attachment->attachment}' class='img-fluid' alt='Expenses Request Image'>";
echo "</div>";
}
?>
</div>
</div>
</div>
</div>
</div>
<?php
$i++;
}
?>
</tbody>
</table>
</div>
</div>
<script>
// Initialize DataTable
jQuery(document).ready(function($) {
$('#expenses-table').DataTable();
});
</script>
<?php
}
//Get RMS contract ID
function get_exp_order_ids_by_product_id($product_id) {
global $wpdb;
// Query compatible with HPOS (High-Performance Order Storage)
$query = $wpdb->prepare(
"SELECT DISTINCT opl.order_id
FROM {$wpdb->prefix}wc_order_product_lookup opl
JOIN {$wpdb->prefix}wc_orders o
ON opl.order_id = o.id
WHERE o.type = 'shop_order'
AND (opl.product_id = %d OR opl.variation_id = %d)",
intval($product_id),
intval($product_id)
);
// Execute the query and return results
$order_ids = $wpdb->get_col($query);
return $order_ids;
}
function create_expenses(){
error_reporting(0);
global $wpdb;
if(isset($_POST['createexpenses'])!=''){
$table_name = $wpdb->prefix . "expenses_request";
$attachment_table = $wpdb->prefix . "expenses_attachment_docs";
$operator_id = $_POST['operator_id'];
$product_id = $_POST['equipment_id'];
$problem_description = $_POST['problem_description'];
$exp_reason = $_POST['exp_reason'];
$exp_amount = $_POST['exp_amount'];
$order_id = $_POST['order_id'];
// Insert expense request first
$result_check = $wpdb->insert($table_name, [
"operator_id" => $operator_id,
"equipment_id" => $product_id,
"problem_description" => $problem_description,
// "attachment" => '', // Will be updated later if attachment exists
"exp_reason" => $exp_reason,
"exp_amount" => $exp_amount,
"order_id" => $order_id,
"created_on" => date('Y-m-d H:i:s'),
]);
$log_id = $wpdb->insert_id;
if($result_check){
// if (isset($_FILES['attachment']) && $_FILES['attachment']['size'] > 0) {
// // Handle file upload
// $upload_attachment = wp_handle_upload($_FILES['attachment'], array('test_form' => false));
// if (isset($upload_attachment['url']) && !isset($upload_attachment['error'])) {
// $attachment_url = $upload_attachment['url'];
// // Insert into expenses_attachment_docs
// $wpdb->insert($attachment_table, [
// "log_id" => $log_id,
// "attachment_url" => $attachment_url,
// "created_on" => date('Y-m-d H:i:s')
// ]);
// // Update expenses_request with attachment URL
// $wpdb->update(
// $table_name,
// ["attachment" => $attachment_url],
// ["id" => $log_id]
// );
// }
// }
// Inside the create_expenses function, replace the attachment handling block with:
if (isset($_FILES['attachment']) && $_FILES['attachment']['size'] > 0) {
$upload_dir = wp_upload_dir();
$custom_dir = $upload_dir['basedir'] . '/operator-expenses-request-attachments/';
$custom_url = $upload_dir['baseurl'] . '/operator-expenses-request-attachments/';
// Create directory if it doesn't exist
if (!file_exists($custom_dir)) {
wp_mkdir_p($custom_dir);
}
$file_name = sanitize_file_name($_FILES['attachment']['name']);
$file_path = $custom_dir . $file_name;
if (move_uploaded_file($_FILES['attachment']['tmp_name'], $file_path)) {
$attachment = $file_name;
// Insert into expenses_attachment_docs
$wpdb->insert($attachment_table, [
"log_id" => $log_id,
"attachment" => $file_name,
"created_on" => date('Y-m-d H:i:s')
]);
// // Update expenses_request with attachment URL
// $wpdb->update(
// $table_name,
// ["id" => $log_id]
// );
}
}
echo "<p>Expenses Request Raised Successfully</p>";
} else {
echo "<p>Error Occurs</p>";
}
}
?>
<style>
.error-msg{
color: red !important;
font-weight: bold !important;
font-size: 14px !important;
margin: 0 !important;
float: left !important;
}
</style>
<div class="wrap">
<form method="post" name="create_expenses_request" id="createuser" class="form" enctype="multipart/form-data" onsubmit="return create_expenses()">
<div class="row">
<div class="column">
<h3>Create Expenses Request</h3>
<table class="form-table" role="presentation">
<tbody>
<tr class="form-field form-required">
<th scope="row"><label for="order_id">Select Contract<span class="description" required>(required)</span></label></th>
<td>
<select name="order_id" id="order_id" class="regular-text" required onchange="loadOrderProducts(this.value)">
<option value="">Select Order</option>
<?php
$orders = wc_get_orders(array(
'limit' => -1
// 'status' => array('wc-completed', 'wc-processing', 'wc-on-hold'),
));
foreach ($orders as $order) {
echo '<option value="' . esc_attr($order->get_id()) . '">Order #' . esc_attr($order->get_order_number()) . ' - ' . esc_html($order->get_formatted_billing_full_name()) . '</option>';
}
?>
</select>
</td>
</tr>
<tr class="form-field form-required">
<th scope="row"><label for="operator_id">Select Operator<span class="description" required>(required)</span></label></th>
<td>
<select name="operator_id" id="operator_id" class="regular-text" required>
<option value="">Select Operator</option>
</select>
</td>
</tr>
<tr class="form-field form-required">
<th scope="row"><label for="equipment_id">Select Equipment<span class="description" required>(required)</span></label></th>
<td>
<select name="equipment_id" id="equipment_id" class="regular-text" required>
<option value="">Select Product</option>
</select>
</td>
</tr>
<tr class="form-field form-required">
<th scope="row"><label for="exp_amount">Amount <span class="description">(required)</span></label></th>
<td>
<input type="number" name="exp_amount" id="exp_amount" class="regular-text" step="0.01" min="0" required>
<span class="error-message wc_amount"></span>
</td>
</tr>
<tr class="form-field">
<th scope="row"><label for="attachment">Attachment </label></th>
<td><input type="file" name="attachment" id="attachment_servreq" class="regular-text"></td>
</tr>
<tr class="form-field">
<th scope="row"><label for="exp_reason">Purpose / Reason </label></th>
<td>
<select name="exp_reason" id="exp_reason" class="regular-text">
<option value="">Select Reason</option>
<option value="food">Food</option>
<option value="accomidation">Accomidation</option>
<option value="convinience">Local Convinience</option>
</select>
</td>
</tr>
<tr class="form-field form-required">
<th scope="row"><label for="problem_description">Operator Remarks</label></th>
<td>
<textarea name="problem_description" id="problem_description" class="regular-text"></textarea>
<span class="error-message wc_probdesc"></span>
</td>
</tr>
</tbody>
</table>
</div>
</div>
<p class="submit"><input type="submit" name="createexpenses" id="createexpenses" class="button button-primary" value="Add Expenses Request"></p>
</form>
</div>
<script type="text/javascript">
function create_expenses() {
var order_id = jQuery('#order_id').val();
var operator_id = jQuery('#operator_id').val();
var equipment_id = jQuery('#equipment_id').val();
var exp_amount = jQuery('#exp_amount').val();
var problem_description = jQuery('#problem_description').val();
if(order_id == ''){
jQuery('.wc_probdesc').html("Please Select an Order").addClass("error-msg");
return false;
}
if(operator_id == ''){
jQuery('.wc_probdesc').html("Please Select an Operator").addClass("error-msg");
return false;
}
if(equipment_id == ''){
jQuery('.wc_probdesc').html("Please Select an Equipment").addClass("error-msg");
return false;
}
if(exp_amount == '' || exp_amount <= 0){
jQuery('.wc_amount').html("Please Enter a Valid Amount").addClass("error-msg");
return false;
}
// if(problem_description == ''){
// jQuery('.wc_probdesc').html("Please Enter Problem Description").addClass("error-msg");
// return false;
// }
jQuery('.wc_probdesc').html('');
jQuery('.wc_amount').html('');
return true;
}
function loadOrderProducts(orderId) {
if (!orderId) {
jQuery('#equipment_id').html('<option value="">Select Product</option>');
jQuery('#operator_id').html('<option value="">Select Operator</option>');
return;
}
// Load products
jQuery.ajax({
url: '<?php echo admin_url('admin-ajax.php'); ?>',
type: 'POST',
data: {
action: 'get_order_products',
order_id: orderId
},
success: function(response) {
if (response.success) {
var options = '<option value="">Select Product</option>';
response.data.forEach(function(product) {
options += '<option value="' + product.id + '">' + product.name + '</option>';
});
jQuery('#equipment_id').html(options);
} else {
jQuery('#equipment_id').html('<option value="">No products found</option>');
}
}
});
// Load operators
jQuery.ajax({
url: '<?php echo admin_url('admin-ajax.php'); ?>',
type: 'POST',
data: {
action: 'get_order_operators',
order_id: orderId
},
success: function(response) {
if (response.success) {
var options = '<option value="">Select Operator</option>';
response.data.forEach(function(operator) {
options += '<option value="' + operator.id + '">' + operator.name + '</option>';
});
jQuery('#operator_id').html(options);
} else {
jQuery('#operator_id').html('<option value="">No operators found</option>');
}
}
});
}
</script>
<?php
}
// AJAX handler for getting order products
add_action('wp_ajax_get_order_products', 'get_order_products_callback');
function get_order_products_callback() {
$order_id = isset($_POST['order_id']) ? intval($_POST['order_id']) : 0;
$products = array();
if ($order_id) {
$order = wc_get_order($order_id);
if ($order) {
foreach ($order->get_items() as $item) {
$product = $item->get_product();
$products[] = array(
'id' => $product->get_id(),
'name' => $product->get_name()
);
}
}
}
wp_send_json_success($products);
}
// AJAX handler for getting operators
add_action('wp_ajax_get_order_operators', 'get_order_operators_callback');
function get_order_operators_callback() {
$order_id = isset($_POST['order_id']) ? intval($_POST['order_id']) : 0;
$operators = array();
if ($order_id) {
$args = array(
'meta_query' => array(
array(
'key' => 'contract_id',
'value' => $order_id,
'compare' => '='
)
)
);
$users = get_users($args);
foreach ($users as $user) {
$operators[] = array(
'id' => $user->ID,
'name' => $user->display_name
);
}
}
wp_send_json_success($operators);
}
function edit_expenses(){
global $wpdb;
$id = $_REQUEST['id'];
$table_name = $wpdb->prefix . 'expenses_request';
// Fetch all data from the wp_request_quote table
$results = $wpdb->get_row("SELECT * FROM $table_name where id=$id");
//print_r($_POST);
if(isset($_POST['update_expenses'])!=''){
$id = $_REQUEST['expenses_id'];
$status = $_POST['status'];
$remarks = $_POST['remarks'];
$updated_on = date('Y-m-d H:i:s');
$wpdb->update($table_name, [
"status" => $status,
"remarks" => $remarks,
"updated_on" => $updated_on,
],['id'=>$id]);
echo '<p style="color:#008000">Data Updated Successfully</p>';
echo '<script>window.location.href = "'.admin_url('admin.php?page=expenses-dashboard').'"</script>';
}
?>
<div class="wrap">
<h2>Edit Expenses Request Status</h2>
<form method="post" action="">
<input type="hidden" name="edit_id" value="<?php echo esc_attr($_REQUEST['id']);?>">
<table class="form-table">
<tr>
<th><label for="user_email">Status</label></th>
<td>
<select name="status" id="status" class="regular-text" required>
<option value="">Select Status</option>
<option value="In Progress" <?php echo ($results->status == 'In Progress') ? 'selected' : ''; ?>>In Progress</option>
<option value="Approved" <?php echo ($results->status == 'Approved') ? 'selected' : ''; ?>>Approved</option>
</select>
</td>
</tr>
<tr>
<th><label for="user_email">Remarks</label></th>
<td>
<input type="text" name="remarks" id="remarks" value="<?php echo esc_attr($results->remarks);?>" class="regular-text" required>
</td>
</tr>
</table>
<input type="hidden" name="expenses_id" id="expenses_id" value="<?php echo esc_attr($id);?>">
<p class="submit">
<input type="submit" name="update_expenses" id="update_expenses" class="button-primary" value="Update">
<a href="<?php echo admin_url('admin.php?page=expenses-dashboard'); ?>" class="button-primary">Cancel</a>
</p>
</form>
</div>
<?php
}
?>