← All changes
|
core/dashboard/controllers/dashboard-controller.php
+93
-80
1.0.3
→
1.0.27
View file →
| @@ -4,9 +4,9 @@ | ||
| 4 | 4 | |
| 5 | 5 | use Booktics\Abstracts\Base_Rest_Controller; |
| 6 | 6 | use Booktics\Abstracts\Booktics_Database; |
| 7 | 7 | use Booktics\Models\Appointment_Model; |
| 8 | -use Booktics\Models\Customer_Model; | |
| 8 | +use Booktics\Models\Guest_Model; | |
| 9 | 9 | use Booktics\Models\Order_Model; |
| 10 | 10 | use WP_HTTP_Response; |
| 11 | 11 | use WP_REST_Request; |
| 12 | 12 | use WP_REST_Server; |
| @@ -130,9 +130,9 @@ | ||
| 130 | 130 | foreach ( self::REPORT_TYPES as $report_type ) { |
| 131 | 131 | $report_data = $request->get_param( $report_type ); |
| 132 | 132 | |
| 133 | 133 | if ( is_array( $report_data ) && isset( $report_data['start_date'], $report_data['end_date'] ) ) { |
| 134 | - $date_range = $report_data['start_date'] . ' to ' . $report_data['end_date']; | |
| 134 | + $date_range = sanitize_text_field( $report_data['start_date'] ) . ' to ' . sanitize_text_field( $report_data['end_date'] ); | |
| 135 | 135 | $data[ $report_type ] = $this->get_report_data( $report_type, $date_range ); |
| 136 | 136 | } |
| 137 | 137 | } |
| 138 | 138 | |
| @@ -186,14 +186,16 @@ | ||
| 186 | 186 | } |
| 187 | 187 | |
| 188 | 188 | // Get current period bookings |
| 189 | 189 | $current_query = array( |
| 190 | - 'date_query' => array( | |
| 190 | + 'post_status' => array( 'completed', 'approved' ), | |
| 191 | + 'date_query' => array( | |
| 191 | 192 | 'after' => $dates['start'], |
| 192 | 193 | 'before' => $dates['end'], |
| 193 | 194 | 'inclusive' => true, |
| 194 | 195 | ), |
| 195 | 196 | ); |
| 197 | + $current_query = apply_filters( 'booktics_appointment_args', $current_query ); | |
| 196 | 198 | $current_results = $appointments->all( $current_query ); |
| 197 | 199 | $current_bookings = count( $current_results ); |
| 198 | 200 | |
| 199 | 201 | // Calculate previous period dates |
| @@ -204,10 +206,11 @@ | ||
| 204 | 206 | 'date_query' => array( |
| 205 | 207 | 'after' => $previous_dates['start'], |
| 206 | 208 | 'before' => $previous_dates['end'], |
| 207 | 209 | ), |
| 208 | - 'status' => 'confirmed', | |
| 210 | + 'post_status' => array( 'completed', 'approved' ), | |
| 209 | 211 | ); |
| 212 | + $previous_query = apply_filters( 'booktics_appointment_args', $previous_query ); | |
| 210 | 213 | $previous_results = $appointments->all( $previous_query ); |
| 211 | 214 | $previous_bookings = count( $previous_results ); |
| 212 | 215 | |
| 213 | 216 | // Calculate percentage change |
| @@ -212,9 +215,9 @@ | ||
| 212 | 215 | |
| 213 | 216 | // Calculate percentage change |
| 214 | 217 | $percentage_change = 0; |
| 215 | 218 | $trend = 'neutral'; |
| 216 | - if ( $previous_bookings != 0 ) { | |
| 219 | + if ( 0 != $previous_bookings ) { | |
| 217 | 220 | $percentage_change = ( ( $current_bookings - $previous_bookings ) / $previous_bookings ) * 100; |
| 218 | 221 | $trend = $this->get_trend( $percentage_change ); |
| 219 | 222 | } |
| 220 | 223 | |
| @@ -243,51 +246,71 @@ | ||
| 243 | 246 | 'trend' => 'neutral', |
| 244 | 247 | ); |
| 245 | 248 | } |
| 246 | 249 | |
| 247 | - global $wpdb; | |
| 248 | - $table_name = $orders->get_table_name(); | |
| 250 | + // Get order IDs to filter by (if needed) | |
| 251 | + $order_ids = $this->get_order_ids(); | |
| 249 | 252 | |
| 250 | - // Get current period revenue | |
| 251 | - $current_revenue = (float) $wpdb->get_var( | |
| 252 | - $wpdb->prepare( | |
| 253 | - 'SELECT SUM(total) as total FROM ' . $wpdb->prefix . $wpdb->_real_escape( $table_name ) . ' WHERE created_at BETWEEN %s AND %s AND payment_status = %s', | |
| 254 | - $dates['start'] . ' 00:00:00', | |
| 255 | - $dates['end'] . ' 23:59:59', | |
| 256 | - 'completed' | |
| 257 | - ) | |
| 258 | - ) ?: 0; | |
| 253 | + // Get current period revenue using Order_Model method | |
| 254 | + $current_revenue = $orders->get_revenue_by_date_range( | |
| 255 | + $dates['start'], | |
| 256 | + $dates['end'], | |
| 257 | + $order_ids | |
| 258 | + ); | |
| 259 | + | |
| 259 | 260 | // Calculate previous period dates |
| 260 | 261 | $previous_dates = $this->get_previous_period_dates( $dates ); |
| 261 | 262 | |
| 262 | - // Get previous period revenue | |
| 263 | - $previous_revenue = (float) $wpdb->get_var( | |
| 264 | - $wpdb->prepare( | |
| 265 | - 'SELECT SUM(total) as total FROM ' . $wpdb->prefix . $wpdb->_real_escape( $table_name ) . ' ' . | |
| 266 | - 'WHERE created_at BETWEEN %s AND %s AND payment_status = %s', | |
| 267 | - $previous_dates['start'] . ' 00:00:00', | |
| 268 | - $previous_dates['end'] . ' 23:59:59', | |
| 269 | - 'completed' | |
| 270 | - ) | |
| 271 | - ) ?: 0; | |
| 263 | + // Get previous period revenue using Order_Model method | |
| 264 | + $previous_revenue = $orders->get_revenue_by_date_range( | |
| 265 | + $previous_dates['start'], | |
| 266 | + $previous_dates['end'], | |
| 267 | + $order_ids | |
| 268 | + ); | |
| 272 | 269 | |
| 273 | 270 | // Calculate percentage change |
| 274 | 271 | $percentage_change = 0; |
| 275 | 272 | $trend = 'neutral'; |
| 276 | 273 | |
| 277 | - if ( $previous_revenue != 0 ) { | |
| 274 | + // Ensure both values are valid numbers before calculation | |
| 275 | + if ( is_numeric( $previous_revenue ) && 0 != $previous_revenue && is_numeric( $current_revenue ) ) { | |
| 278 | 276 | $percentage_change = ( ( $current_revenue - $previous_revenue ) / $previous_revenue ) * 100; |
| 279 | - $trend = $this->get_trend( $percentage_change ); | |
| 277 | + | |
| 278 | + // Check if the result is a valid number | |
| 279 | + if ( !is_finite( $percentage_change ) ) { | |
| 280 | + $percentage_change = 0; | |
| 281 | + } else { | |
| 282 | + $trend = $this->get_trend( $percentage_change ); | |
| 283 | + } | |
| 280 | 284 | } |
| 281 | 285 | |
| 282 | 286 | return array( |
| 283 | - 'total' => number_format( $current_revenue, 2 ), | |
| 284 | - 'percentage_changed' => number_format( $percentage_change, 2 ), | |
| 287 | + 'total' => round( (float) $current_revenue, 2 ), | |
| 288 | + 'percentage_changed' => round( (float) $percentage_change, 2 ), | |
| 285 | 289 | 'trend' => $trend, |
| 286 | 290 | ); |
| 287 | 291 | } |
| 288 | 292 | |
| 289 | 293 | /** |
| 294 | + * Get order ids for a date range | |
| 295 | + * | |
| 296 | + * @param array $dates | |
| 297 | + * | |
| 298 | + * @return array | |
| 299 | + */ | |
| 300 | + private function get_order_ids() { | |
| 301 | + $order_ids = array(); | |
| 302 | + $args = array(); | |
| 303 | + $args = apply_filters( 'booktics_appointment_args', $args ); | |
| 304 | + $appointments = ( new Appointment_Model() )->all( $args ); | |
| 305 | + foreach ( $appointments as $appointment ) { | |
| 306 | + $order_ids[] = $appointment->order_id; | |
| 307 | + } | |
| 308 | + | |
| 309 | + return $order_ids; | |
| 310 | + } | |
| 311 | + | |
| 312 | + /** | |
| 290 | 313 | * Get total customers count for a date range |
| 291 | 314 | * |
| 292 | 315 | * @param string $date_range |
| 293 | 316 | * |
| @@ -293,9 +316,10 @@ | ||
| 293 | 316 | * |
| 294 | 317 | * @return array |
| 295 | 318 | */ |
| 296 | 319 | private function get_total_customers( $date_range ) { |
| 297 | - $customers = new Customer_Model(); | |
| 320 | + $db = new Booktics_Database(); | |
| 321 | + $guests = new Guest_Model( $db ); | |
| 298 | 322 | |
| 299 | 323 | $dates = $this->parse_date_range( $date_range ); |
| 300 | 324 | if ( ! $dates ) { |
| 301 | 325 | return array( |
| @@ -303,56 +327,46 @@ | ||
| 303 | 327 | 'percentage_changed' => '0.00', |
| 304 | 328 | 'trend' => 'neutral', |
| 305 | 329 | ); |
| 306 | 330 | } |
| 307 | - | |
| 331 | + if ( empty( $dates['start'] ) || empty( $dates['end'] ) ) { | |
| 332 | + return array( | |
| 333 | + 'total' => 0, | |
| 334 | + 'percentage_changed' => '0.00', | |
| 335 | + 'trend' => 'neutral', | |
| 336 | + ); | |
| 337 | + } | |
| 308 | 338 | // Get current period customers |
| 309 | - $current_query = array( | |
| 310 | - 'date_query' => array( | |
| 311 | - 'after' => $dates['start'] . ' 00:00:00', | |
| 312 | - 'before' => $dates['end'] . ' 23:59:59', | |
| 313 | - ), | |
| 314 | - 'role' => 'booktics_customer', | |
| 315 | - 'meta_query' => array( | |
| 316 | - 'key' => 'status', | |
| 317 | - 'value' => 1, | |
| 318 | - 'compare' => '=', | |
| 319 | - ), | |
| 339 | + $current_results = $guests->count( | |
| 340 | + array( | |
| 341 | + 'created_at' => array( '>=', $dates['start'] . ' 00:00:00' ), | |
| 342 | + 'created_at' => array( '<=', $dates['end'] . ' 23:59:59' ), | |
| 343 | + ) | |
| 320 | 344 | ); |
| 321 | - $current_results = $customers->all( $current_query ); | |
| 322 | - $current_customers = count( $current_results['items'] ); | |
| 323 | 345 | |
| 324 | 346 | // Calculate previous period dates |
| 325 | 347 | $previous_dates = $this->get_previous_period_dates( $dates ); |
| 326 | 348 | |
| 327 | 349 | // Get previous period customers |
| 328 | - $previous_query = array( | |
| 329 | - 'date_query' => array( | |
| 330 | - 'after' => $previous_dates['start'] . ' 00:00:00', | |
| 331 | - 'before' => $previous_dates['end'] . ' 23:59:59', | |
| 332 | - ), | |
| 333 | - 'role' => 'booktics_customer', | |
| 334 | - 'meta_query' => array( | |
| 335 | - 'key' => 'status', | |
| 336 | - 'value' => 1, | |
| 337 | - 'compare' => '=', | |
| 338 | - ), | |
| 350 | + $previous_results = $guests->count( | |
| 351 | + array( | |
| 352 | + 'created_at' => array( '>=', $previous_dates['start'] . ' 00:00:00' ), | |
| 353 | + 'created_at' => array( '<=', $previous_dates['end'] . ' 23:59:59' ), | |
| 354 | + ) | |
| 339 | 355 | ); |
| 340 | - $previous_results = $customers->all( $previous_query ); | |
| 341 | - $previous_customers = count( $previous_results['items'] ); | |
| 342 | 356 | |
| 343 | 357 | // Calculate percentage change |
| 344 | 358 | $percentage_change = 0; |
| 345 | - $trend = 'neutral'; | |
| 346 | - if ( $previous_customers != 0 ) { | |
| 347 | - $percentage_change = ( ( $current_customers - $previous_customers ) / $previous_customers ) * 100; | |
| 348 | - $trend = $this->get_trend( $percentage_change ); | |
| 359 | + $trend = 'neutral'; | |
| 360 | + if ( 0 != $previous_results ) { | |
| 361 | + $percentage_change = ( ( $current_results - $previous_results ) / $previous_results ) * 100; | |
| 362 | + $trend = $this->get_trend( $percentage_change ); | |
| 349 | 363 | } |
| 350 | 364 | |
| 351 | 365 | return array( |
| 352 | - 'total' => $current_customers, | |
| 366 | + 'total' => $current_results, | |
| 353 | 367 | 'percentage_changed' => number_format( $percentage_change, 2 ), |
| 354 | - 'trend' => $trend, | |
| 368 | + 'trend' => $trend, | |
| 355 | 369 | ); |
| 356 | 370 | } |
| 357 | 371 | |
| 358 | 372 | /** |
| @@ -380,17 +394,16 @@ | ||
| 380 | 394 | foreach ( $period as $date ) { |
| 381 | 395 | $date_str = $date->format( 'Y-m-d' ); |
| 382 | 396 | $reports[ $date_str ] = array( |
| 383 | 397 | 'completed' => 0, |
| 384 | - 'cancelled' => 0, | |
| 398 | + 'cancel' => 0, | |
| 385 | 399 | ); |
| 386 | 400 | } |
| 387 | 401 | |
| 388 | 402 | // Query appointments using PostModel's methods |
| 389 | - $query = array( | |
| 403 | + $query = array( | |
| 390 | 404 | 'post_type' => $appointments->get_post_type(), |
| 391 | - 'post_status' => array( 'active', 'pending' ), | |
| 392 | - // Using the statuses from PostModel's $accepted_statuses | |
| 405 | + 'post_status' => array( 'completed', 'cancelled', 'canceled' ), | |
| 393 | 406 | 'date_query' => array( |
| 394 | 407 | array( |
| 395 | 408 | 'after' => $dates['start'], |
| 396 | 409 | 'before' => $dates['end'], |
| @@ -396,17 +409,10 @@ | ||
| 396 | 409 | 'before' => $dates['end'], |
| 397 | 410 | 'inclusive' => true, |
| 398 | 411 | ), |
| 399 | 412 | ), |
| 400 | - 'meta_query' => array( | |
| 401 | - array( | |
| 402 | - 'key' => 'status', | |
| 403 | - 'value' => array( 'completed', 'cancelled' ), | |
| 404 | - 'compare' => 'IN', | |
| 405 | - ), | |
| 406 | - ), | |
| 407 | 413 | ); |
| 408 | - | |
| 414 | + $query = apply_filters( 'booktics_appointment_args', $query ); | |
| 409 | 415 | $appointments = Appointment_Model::all( $query ); |
| 410 | 416 | |
| 411 | 417 | foreach ( $appointments as $appointment ) { |
| 412 | 418 | $date_str = $appointment->date; |
| @@ -412,10 +418,10 @@ | ||
| 412 | 418 | $date_str = $appointment->date; |
| 413 | 419 | if ( isset( $reports[ $date_str ] ) ) { |
| 414 | 420 | if ( $appointment->status === 'completed' ) { |
| 415 | 421 | ++$reports[ $date_str ]['completed']; |
| 416 | - } elseif ( $appointment->status === 'cancelled' ) { | |
| 417 | - ++$reports[ $date_str ]['cancelled']; | |
| 422 | + } elseif ( $appointment->status === 'cancelled' || $appointment->status === 'canceled' ) { //both spellings are used for backward compatibility | |
| 423 | + ++$reports[ $date_str ]['cancel']; | |
| 418 | 424 | } |
| 419 | 425 | } |
| 420 | 426 | } |
| 421 | 427 | |
| @@ -472,10 +478,17 @@ | ||
| 472 | 478 | * |
| 473 | 479 | * @return array Array of previous period start and end dates |
| 474 | 480 | */ |
| 475 | 481 | private function get_previous_period_dates( $dates ) { |
| 476 | - $previous_start = strtotime( $dates['start'] . ' -1 day' ); | |
| 477 | - $previous_end = strtotime( $dates['end'] . ' -1 day' ); | |
| 482 | + $start_time_in_seconds = strtotime( $dates['start'] ); | |
| 483 | + $end_time_in_seconds = strtotime( $dates['end'] ); | |
| 484 | + | |
| 485 | + // Calculate the gap in days (inclusive) | |
| 486 | + $gap_days = round( ( $end_time_in_seconds - $start_time_in_seconds ) / DAY_IN_SECONDS ); | |
| 487 | + | |
| 488 | + // Subtract the gap from both start and end | |
| 489 | + $previous_start = strtotime( "-$gap_days days", $start_time_in_seconds ); | |
| 490 | + $previous_end = strtotime( "-$gap_days days", $end_time_in_seconds ); | |
| 478 | 491 | |
| 479 | 492 | return array( |
| 480 | 493 | 'start' => gmdate( 'Y-m-d', $previous_start ), |
| 481 | 494 | 'end' => gmdate( 'Y-m-d', $previous_end ), |