| Server IP : 13.126.101.145 / Your IP : 216.73.216.63 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/woocommerce-rfq/ |
Upload File : |
<?php
function register_discount_approval_menu() {
add_menu_page(
'Discount Approvals',
'Quotation Discount Requests',
'manage_options', // Change to the appropriate capability for central administrators
'discount-approvals',
'discount_approval_page',
'dashicons-yes', // Icon
6
);
}
add_action('admin_menu', 'register_discount_approval_menu');
function discount_approval_page() {
global $wpdb;
$approval_table = $wpdb->prefix . 'discount_approval';
// Check if the user is a central administrator
$current_user = wp_get_current_user();
if (!in_array('central_administrator', $current_user->roles)) {
echo '<div class="wrap"><h1 class="wp-heading-inline">Access Denied.</h1></div>';
return;
}
// Process approval/rejection
if (isset($_POST['submit_approval'])) {
$id = intval($_POST['id']);
$status = sanitize_text_field($_POST['approval_action']);
$remarks = sanitize_text_field($_POST['remarks']);
// Update the approval table
$wpdb->update(
$approval_table,
array(
'status' => $status,
'remarks' => $remarks
),
array('id' => $id),
array('%s', '%s'),
array('%d')
);
// Get the approval details from the approval table
$approval = $wpdb->get_row("SELECT * FROM $approval_table WHERE id = $id", ARRAY_A);
// Get the current user
$current_user = wp_get_current_user();
$revised_by = $current_user->user_login; // Current logged-in user's username
$table_name = $wpdb->prefix . 'quotation';
// Fetch the current quotation data (this is the data you want to save in the revision history)
$current_quotation = $wpdb->get_row($wpdb->prepare("SELECT * FROM $table_name WHERE id = %d", $approval['qt_id']), ARRAY_A);
if ($current_quotation === null) {
error_log('Quotation not found: ' . $approval['qt_id']);
return;
}
if ($status == 'Approved') {
// Prepare new revision data with previous (current) data
$new_revision = array(
'revised_by' => $revised_by,
'revision_date' => current_time('mysql'),
'final_price' => $current_quotation['final_price'],
'discount' => $current_quotation['rfqdiscount'],
'status' => $current_quotation['qt_status'],
);
// Decode existing revision details
$revision_details = $current_quotation['revision_details'] ? json_decode($current_quotation['revision_details'], true) : array();
// Ensure that $revision_details is an array before appending to it
if (!is_array($revision_details)) {
$revision_details = []; // Initialize as an empty array if it's not an array
}
// Append the new revision to the revision details array
$revision_details[] = $new_revision;
// Encode back to JSON format
$revision_details_json = json_encode($revision_details);
$wpdb->update(
$wpdb->prefix . 'quotation',
array(
'final_price' => $approval['final_price'], // Ensure this is a float
'rfqdiscount' => $approval['discount'], // Ensure this is a float
'discount_status' => "Approved", // Ensure this is a string
'ap_comments' => $remarks, // Ensure this is a string
'qt_status' => "Revised", // Ensure this is a string
'revision_details' => $revision_details_json // Ensure this is a valid JSON string
),
array('id' => $approval['qt_id']), // Condition to match the correct ID
array('%f', '%f', '%s', '%s', '%s', '%s'), // Data types: 2 floats, 4 strings
array('%d') // Data type for the WHERE clause (ID is an integer)
);
}
else {
$wpdb->update(
$wpdb->prefix . 'quotation',
array(
'discount_status' => $status, // $status is a string
'ap_comments' => $remarks,
),
array('id' => $approval['qt_id']),
array('%s', '%s'), // Correct format for both discount_status and ap_comments (both strings)
array('%d') // Format for the 'id' in the WHERE clause (integer)
);
}
// Display success message
echo '<div class="notice notice-success is-dismissible"><p>Discount ' . esc_html($status) . ' successfully.</p></div>';
}
// Fetch pending approvals, ordered by ID in descending order to get the latest ones first
$pending_approvals = $wpdb->get_results("SELECT * FROM $approval_table ORDER BY id DESC", ARRAY_A);
// + 26-11-2024
$status_filter = isset($_GET['status_filter']) ? sanitize_text_field($_GET['status_filter']) : 'All';
// Fetch pending approvals, ordered by ID in descending order to get the latest ones first
$query = "SELECT * FROM $approval_table";
if ($status_filter !== 'All') {
$query .= $wpdb->prepare(" WHERE status = %s", $status_filter);
}
$query .= " ORDER BY id DESC";
$pending_approvals = $wpdb->get_results($query, ARRAY_A);
// + 26-11-2024
// CSS for styling the table
echo '<style>
.discount-approval-table {
width: 100%;
border-collapse: collapse;
margin-bottom: 20px;
}
.discount-approval-table th, .discount-approval-table td {
border: 1px solid #ddd;
padding: 8px;
text-align: left;
}
.discount-approval-table th {
background-color: #f1f1f1;
}
.discount-approval-table tr:nth-child(even) {
background-color: #f9f9f9;
}
.discount-approval-table tr:hover {
background-color: #f1f1f1;
}
.discount-approval-table input[type="submit"] {
background-color: #ffcc00;
border: none;
color: white;
padding: 5px 10px;
text-align: center;
text-decoration: none;
display: inline-block;
font-size: 14px;
cursor: pointer;
border-radius: 3px;
}
.discount-approval-table textarea {
width: 100%;
padding: 5px;
border-radius: 3px;
}
select.regular-text {
color: #000 !important;
font-weight: 500;
}
</style>';
?>
<div class="wrap">
<h1 class="wp-heading-inline">Discount Approval Requests</h1>
<form method="get" style="margin-bottom: 20px;">
<input type="hidden" name="page" value="<?php echo esc_attr($_GET['page']); ?>"> <!-- Preserve the current page in the query -->
<select id="status_filter" name="status_filter">
<option value="All" <?php selected($status_filter, 'All'); ?>>Filter by Status</option>
<option value="Pending" <?php selected($status_filter, 'Pending'); ?>>Pending</option>
<option value="Approved" <?php selected($status_filter, 'Approved'); ?>>Approved</option>
<option value="Rejected" <?php selected($status_filter, 'Rejected'); ?>>Rejected</option>
</select>
<button type="submit" class="button button-primary">Filter</button>
</form>
<table class="discount-approval-table">
<thead>
<tr>
<th>ID</th>
<th>Quotation ID</th>
<th>Customer Name</th>
<th>Product Name</th>
<th>Start Date</th>
<th>End Date</th>
<th>Final Price</th>
<th>Discount</th>
<th>Requested By</th>
<th>Status</th>
</tr>
</thead>
<tbody>
<?php foreach ($pending_approvals as $approval) : ?>
<tr>
<td><?php echo esc_html($approval['id']); ?></td>
<td><?php echo esc_html($approval['qt_id']); ?></td>
<td><?php echo esc_html($approval['user_name']); ?></td>
<td><?php echo esc_html($approval['product_name']); ?></td>
<td>
<?php
echo !empty($approval['start_date'])
? esc_html(date('d-m-Y', strtotime($approval['start_date'])))
: 'N/A';
?>
</td>
<td>
<?php
echo !empty($approval['end_date'])
? esc_html(date('d-m-Y', strtotime($approval['end_date'])))
: 'N/A';
?>
</td>
<td><?php echo esc_html($approval['final_price']); ?></td>
<td><?php echo esc_html($approval['discount']); ?>%</td>
<td><?php echo esc_html($approval['requested_ba']); ?></td>
<td>
<form method="post" style="display:inline; class="approval-form" data-qt-id="<?php echo esc_attr($approval['qt_id']); ?>">
<input type="hidden" name="id" value="<?php echo esc_attr($approval['id']); ?>">
<select name="approval_action" class="regular-text"
<?php echo ($approval['status'] != 'Pending') ? 'disabled' : ''; ?>>
<option value="Pending" <?php selected($approval['status'], 'Pending'); ?>>Pending</option>
<option value="Approved" <?php selected($approval['status'], 'Approved'); ?>>Approved</option>
<option value="Rejected" <?php selected($approval['status'], 'Rejected'); ?>>Rejected</option>
</select>
<textarea name="remarks" placeholder="Enter remarks"
<?php echo ($approval['status'] != 'Pending') ? 'readonly' : ''; ?>
style="text-align: left; margin-top:12px;"><?php echo esc_textarea($approval['remarks']); ?>
</textarea>
<?php if ($approval['status'] == 'Pending') : ?>
<input type="submit" name="submit_approval" class="submit-approval" value="Submit">
<?php endif; ?>
</form>
</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
</div>
<script type="text/javascript">
jQuery(document).ready(function($) {
$('.approval-form').on('submit', function(e) {
var form = $(this);
var qt_id = form.data('qt-id');
var action = form.find('select[name="approval_action"]').val();
// Send email notification for both Approved and Rejected status
if (action === 'Approved' || action === 'Rejected') {
$.ajax({
url: ajaxurl,
type: 'POST',
data: {
action: 'send_discount_rfq_email',
qt_id: qt_id,
status: action,
nonce: '<?php echo wp_create_nonce("send_discount_rfq_email_nonce"); ?>'
},
success: function(response) {
if (response.success) {
console.log('Email notification sent successfully');
} else {
console.error('Failed to send email notification:', response.data);
}
},
error: function(xhr, status, error) {
console.error('Ajax error:', error);
}
});
}
// Let the form submit normally
return true;
});
});
</script>
<?php
}
function hide_screen_options_for_approve_discount() {
?>
<style>
<?php if (isset($_GET['page']) && ($_GET['page'] === 'discount-approvals')) : ?>
#screen-meta-links {
display: none !important;
}
<?php endif; ?>
</style>
<?php
}
add_action('admin_head', 'hide_screen_options_for_approve_discount');
?>