| 1 |
<?php |
| 2 |
/** |
| 3 |
* Api reports class |
| 4 |
* |
| 5 |
* @package Timetics |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace Timetics\Core\Reports; |
| 9 |
|
| 10 |
defined( 'ABSPATH' ) || exit; |
| 11 |
|
| 12 |
use Timetics\Base\Api; |
| 13 |
use Timetics\Utils\Singleton; |
| 14 |
|
| 15 |
/** |
| 16 |
* Class Api Reports |
| 17 |
*/ |
| 18 |
class Api_Report extends Api { |
| 19 |
use Singleton; |
| 20 |
|
| 21 |
/** |
| 22 |
* Store api namespace |
| 23 |
* |
| 24 |
* @var string |
| 25 |
*/ |
| 26 |
protected $namespace = 'timetics/v1'; |
| 27 |
|
| 28 |
/** |
| 29 |
* Store rest base |
| 30 |
* |
| 31 |
* @var string |
| 32 |
*/ |
| 33 |
protected $rest_base = 'reports'; |
| 34 |
|
| 35 |
/** |
| 36 |
* Register rest routes |
| 37 |
* |
| 38 |
* @return void |
| 39 |
*/ |
| 40 |
public function register_routes() { |
| 41 |
/** |
| 42 |
* Register route |
| 43 |
* |
| 44 |
* @var void |
| 45 |
*/ |
| 46 |
register_rest_route( |
| 47 |
$this->namespace, $this->rest_base . '/overview', [ |
| 48 |
[ |
| 49 |
'methods' => \WP_REST_Server::CREATABLE, |
| 50 |
'callback' => [$this, 'get_overview'], |
| 51 |
'permission_callback' => function () { |
| 52 |
return current_user_can( 'manage_timetics' ); |
| 53 |
}, |
| 54 |
], |
| 55 |
] |
| 56 |
); |
| 57 |
} |
| 58 |
|
| 59 |
/** |
| 60 |
* Get overview |
| 61 |
* |
| 62 |
* @param WP_Rest_Request |
| 63 |
* |
| 64 |
* @return JSON |
| 65 |
*/ |
| 66 |
public function get_overview( $request ) { |
| 67 |
$input_data = json_decode( $request->get_body(), true ); |
| 68 |
|
| 69 |
/** |
| 70 |
* Added temporary for leagacy sass. It will remove in future. |
| 71 |
*/ |
| 72 |
$data = apply_filters('timetics/overview/report/get_overview', $this->generate_reports( $input_data ) ); |
| 73 |
|
| 74 |
$response = [ |
| 75 |
'status_code' => 200, |
| 76 |
'success' => 1, |
| 77 |
'data' => $data, |
| 78 |
]; |
| 79 |
|
| 80 |
return rest_ensure_response( $response ); |
| 81 |
} |
| 82 |
|
| 83 |
/** |
| 84 |
* Generate reports based date |
| 85 |
* |
| 86 |
* @param array $input_data |
| 87 |
* |
| 88 |
* @return array |
| 89 |
*/ |
| 90 |
public function generate_reports( $input_data = [] ) { |
| 91 |
$reports = []; |
| 92 |
|
| 93 |
// Statuses that count as a real, confirmed booking for reporting. |
| 94 |
// Deliberately NOT tied to the "default_booking_status" setting — |
| 95 |
// that option controls what status a brand-new booking starts in, |
| 96 |
// not which bookings count as revenue. Reusing it here made a |
| 97 |
// booking silently drop out of totals the moment an admin approved |
| 98 |
// it on a site where new bookings default to "pending". |
| 99 |
$counted_statuses = apply_filters( 'timetics/report/counted_statuses', [ 'approved', 'completed' ] ); |
| 100 |
|
| 101 |
// Staff only see their own numbers, administrators see the whole site. |
| 102 |
$booking_scope = $this->get_booking_scope_args(); |
| 103 |
$customer_scope = $this->get_customer_scope_args(); |
| 104 |
|
| 105 |
if ( $input_data ) { |
| 106 |
foreach ( $input_data as $data ) { |
| 107 |
if ( empty( $data['report'] ) || empty( $data['start_date'] ) || empty( $data['end_date'] ) ) { |
| 108 |
continue; |
| 109 |
} |
| 110 |
|
| 111 |
$date_range = [ |
| 112 |
'start' => $data['start_date'], |
| 113 |
'end' => $data['end_date'], |
| 114 |
]; |
| 115 |
|
| 116 |
switch ( $data['report'] ) { |
| 117 |
case 'total_booking': |
| 118 |
$total_booking = timetics_count_posts( array_merge( [ |
| 119 |
'post_type' => 'timetics-booking', |
| 120 |
'post_status' => $counted_statuses, |
| 121 |
'date_range' => $date_range, |
| 122 |
], $booking_scope ) ); |
| 123 |
|
| 124 |
$reports['total_booking'] = $total_booking; |
| 125 |
break; |
| 126 |
|
| 127 |
case 'total_earning': |
| 128 |
$total_earning = number_format( timetics_get_total_sale( array_merge( [ |
| 129 |
'post_type' => 'timetics-booking', |
| 130 |
'post_status' => $counted_statuses, |
| 131 |
'date_range' => $date_range, |
| 132 |
], $booking_scope ) ), 2 ); |
| 133 |
|
| 134 |
$reports['total_earning'] = $total_earning; |
| 135 |
break; |
| 136 |
|
| 137 |
case 'total_customer': |
| 138 |
$total_customer = timetics_count_users( array_merge( [ |
| 139 |
'role' => 'timetics-customer', |
| 140 |
'date_range' => $date_range, |
| 141 |
], $customer_scope ) ); |
| 142 |
|
| 143 |
$reports['total_customer'] = $total_customer; |
| 144 |
break; |
| 145 |
|
| 146 |
case 'booking_reports': |
| 147 |
$reports['booking_reports'] = $this->generate_date_range_reports( $date_range ); |
| 148 |
break; |
| 149 |
} |
| 150 |
} |
| 151 |
} |
| 152 |
|
| 153 |
return $reports; |
| 154 |
} |
| 155 |
|
| 156 |
/** |
| 157 |
* Generate report for a date range and get every date reports |
| 158 |
* |
| 159 |
* @return array |
| 160 |
*/ |
| 161 |
public function generate_date_range_reports( $date ) { |
| 162 |
$start_date = $date['start']; |
| 163 |
$end_date = $date['end']; |
| 164 |
|
| 165 |
$current_date = strtotime( $start_date ); |
| 166 |
$end_timestamp = strtotime( $end_date ); |
| 167 |
$counted_statuses = apply_filters( 'timetics/report/counted_statuses', [ 'approved', 'completed' ] ); |
| 168 |
$reports = []; |
| 169 |
|
| 170 |
// Staff only see their own numbers, administrators see the whole site. |
| 171 |
$booking_scope = $this->get_booking_scope_args(); |
| 172 |
|
| 173 |
while ( $current_date <= $end_timestamp ) { |
| 174 |
$current_report_date = gmdate( 'Y-m-d', $current_date ); |
| 175 |
|
| 176 |
$date_range = [ |
| 177 |
'start' => $current_report_date, |
| 178 |
'end' => $current_report_date, |
| 179 |
]; |
| 180 |
|
| 181 |
$booking_report = [ |
| 182 |
'cancel' => timetics_count_posts( array_merge( [ |
| 183 |
'post_type' => 'timetics-booking', |
| 184 |
'post_status' => 'cancel', |
| 185 |
'date_range' => $date_range, |
| 186 |
], $booking_scope ) ), |
| 187 |
'completed' => timetics_count_posts( array_merge( [ |
| 188 |
'post_type' => 'timetics-booking', |
| 189 |
'post_status' => $counted_statuses, |
| 190 |
'date_range' => $date_range, |
| 191 |
], $booking_scope ) ), |
| 192 |
]; |
| 193 |
|
| 194 |
$reports[$current_report_date] = $booking_report; |
| 195 |
|
| 196 |
$current_date = strtotime( '+1 day', $current_date ); |
| 197 |
} |
| 198 |
|
| 199 |
return $reports; |
| 200 |
} |
| 201 |
|
| 202 |
/** |
| 203 |
* Get the booking query args that limit a report to what the current user may see |
| 204 |
* |
| 205 |
* @return array Empty for administrators, who see every booking. |
| 206 |
*/ |
| 207 |
protected function get_booking_scope_args() { |
| 208 |
if ( timetics_can_view_all_data() ) { |
| 209 |
return []; |
| 210 |
} |
| 211 |
|
| 212 |
return ['post__in' => timetics_get_visible_booking_ids( get_current_user_id() )]; |
| 213 |
} |
| 214 |
|
| 215 |
/** |
| 216 |
* Get the user query args that limit a report to what the current user may see |
| 217 |
* |
| 218 |
* @return array Empty for administrators, who see every customer. |
| 219 |
*/ |
| 220 |
protected function get_customer_scope_args() { |
| 221 |
if ( timetics_can_view_all_data() ) { |
| 222 |
return []; |
| 223 |
} |
| 224 |
|
| 225 |
return ['include' => timetics_get_visible_customer_ids( get_current_user_id() )]; |
| 226 |
} |
| 227 |
} |
| 228 |
|