Monthly Pms Report
-
I using the Vik Rent Car plugin on WordPress, and right now they have to manually pull the revenue/booking reports from the backend. What they want is a way to have those reports automatically emailed to them every month. Basically, instead of logging in and downloading the report, they want it to hit their inbox at the beginning of each month. I have tried th code but that show wrong data
add_action( ‘vikrentcar_send_scheduled_report’, ‘send_vikrentcar_scheduled_report_email’ );
function send_vikrentcar_scheduled_report_email() {
global $wpdb;$table_name = $wpdb->prefix . 'vikrentcar_orders';
// Date range (last month)
$date_from = date( "Y-m-01", strtotime( "first day of last month" ) );
$date_to = date( "Y-m-t", strtotime( "last day of last month" ) );
// Build email
$to = "[email protected]"; // change to client email
$subject = "PMS Report (Monthly)";
$headers = array( 'Content-Type: text/html; charset=UTF-8' );
$message = "<h2> PMS Report (Monthly)</h2>";
$message .= "<table border='1' cellpadding='6' cellspacing='0' style='border-collapse: collapse;'>";
$message .= "<tr style='background-color:#f2f2f2;'>
<th>Date</th>
<th>Cars Sold</th>
<th>Total Orders</th>
<th>% Occupancy</th>
<th>ADR</th>
<th>RevPAC</th>
<th>Taxes/Fees</th>
<th>Revenue</th>
</tr>";
// Loop through each day in the month
$period = new DatePeriod(
new DateTime($date_from),
new DateInterval('P1D'),
(new DateTime($date_to))->modify('+1 day')
);
foreach ($period as $date) {
$day_str = $date->format('Y-m-d');
// Use DATE(FROM_UNIXTIME(ts)) to match backend exactly
$results = $wpdb->get_results(
$wpdb->prepare(
"SELECT
COUNT(*) as total_orders,
SUM(days) as total_days,
SUM(order_total) as total_revenue,
SUM(tot_taxes) as total_taxes
FROM $table_name
WHERE status = 'confirmed'
AND DATE(FROM_UNIXTIME(ts)) = %s",
$day_str
)
);
$orders = floatval($results[0]->total_orders ?? 0);
$days = floatval($results[0]->total_days ?? 0);
$revenue = floatval($results[0]->total_revenue ?? 0);
$taxes = floatval($results[0]->total_taxes ?? 0);
// Calculations
$cars_sold = $orders;
$occupancy = ($days > 0) ? round(($cars_sold / $days) * 100, 2) : 0;
$adr = ($days > 0) ? round($revenue / $days, 2) : 0;
$revpac = ($orders > 0) ? round($revenue / $orders, 2) : 0;
$message .= "<tr>
<td>" . $date->format("D, m/d/Y") . "</td>
<td>$cars_sold</td>
<td>$orders</td>
<td>{$occupancy}%</td>
<td>$" . number_format($adr, 2) . "</td>
<td>$" . number_format($revpac, 2) . "</td>
<td>$" . number_format($taxes, 2) . "</td>
<td>$" . number_format($revenue, 2) . "</td>
</tr>";
}
$message .= "</table>";
// Send email
wp_mail($to, $subject, $message, $headers);}
Viewing 1 replies (of 1 total)
Viewing 1 replies (of 1 total)
The topic ‘Monthly Pms Report’ is closed to new replies.