| @@ -6,8 +6,10 @@ | ||
| 6 | 6 | */ |
| 7 | 7 | |
| 8 | 8 | namespace Timetics\Core\Reports; |
| 9 | 9 | |
| 10 | +defined( 'ABSPATH' ) || exit; | |
| 11 | + | |
| 10 | 12 | use Timetics\Base\Api; |
| 11 | 13 | use Timetics\Utils\Singleton; |
| 12 | 14 | |
| 13 | 15 | /** |
| @@ -87,8 +89,20 @@ | ||
| 87 | 89 | */ |
| 88 | 90 | public function generate_reports( $input_data = [] ) { |
| 89 | 91 | $reports = []; |
| 90 | 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 | + | |
| 91 | 105 | if ( $input_data ) { |
| 92 | 106 | foreach ( $input_data as $data ) { |
| 93 | 107 | if ( empty( $data['report'] ) || empty( $data['start_date'] ) || empty( $data['end_date'] ) ) { |
| 94 | 108 | continue; |
| @@ -100,32 +114,32 @@ | ||
| 100 | 114 | ]; |
| 101 | 115 | |
| 102 | 116 | switch ( $data['report'] ) { |
| 103 | 117 | case 'total_booking': |
| 104 | - $total_booking = timetics_count_posts( [ | |
| 118 | + $total_booking = timetics_count_posts( array_merge( [ | |
| 105 | 119 | 'post_type' => 'timetics-booking', |
| 106 | - 'post_status' => 'completed', | |
| 120 | + 'post_status' => $counted_statuses, | |
| 107 | 121 | 'date_range' => $date_range, |
| 108 | - ] ); | |
| 122 | + ], $booking_scope ) ); | |
| 109 | 123 | |
| 110 | 124 | $reports['total_booking'] = $total_booking; |
| 111 | 125 | break; |
| 112 | 126 | |
| 113 | 127 | case 'total_earning': |
| 114 | - $total_earning = number_format( timetics_get_total_sale( [ | |
| 128 | + $total_earning = number_format( timetics_get_total_sale( array_merge( [ | |
| 115 | 129 | 'post_type' => 'timetics-booking', |
| 116 | - 'post_status' => 'completed', | |
| 130 | + 'post_status' => $counted_statuses, | |
| 117 | 131 | 'date_range' => $date_range, |
| 118 | - ] ), 2 ); | |
| 132 | + ], $booking_scope ) ), 2 ); | |
| 119 | 133 | |
| 120 | 134 | $reports['total_earning'] = $total_earning; |
| 121 | 135 | break; |
| 122 | 136 | |
| 123 | 137 | case 'total_customer': |
| 124 | - $total_customer = timetics_count_users( [ | |
| 138 | + $total_customer = timetics_count_users( array_merge( [ | |
| 125 | 139 | 'role' => 'timetics-customer', |
| 126 | 140 | 'date_range' => $date_range, |
| 127 | - ] ); | |
| 141 | + ], $customer_scope ) ); | |
| 128 | 142 | |
| 129 | 143 | $reports['total_customer'] = $total_customer; |
| 130 | 144 | break; |
| 131 | 145 | |
| @@ -149,13 +163,16 @@ | ||
| 149 | 163 | $end_date = $date['end']; |
| 150 | 164 | |
| 151 | 165 | $current_date = strtotime( $start_date ); |
| 152 | 166 | $end_timestamp = strtotime( $end_date ); |
| 167 | + $counted_statuses = apply_filters( 'timetics/report/counted_statuses', [ 'approved', 'completed' ] ); | |
| 168 | + $reports = []; | |
| 153 | 169 | |
| 154 | - $reports = []; | |
| 170 | + // Staff only see their own numbers, administrators see the whole site. | |
| 171 | + $booking_scope = $this->get_booking_scope_args(); | |
| 155 | 172 | |
| 156 | 173 | while ( $current_date <= $end_timestamp ) { |
| 157 | - $current_report_date = date( 'Y-m-d', $current_date ); | |
| 174 | + $current_report_date = gmdate( 'Y-m-d', $current_date ); | |
| 158 | 175 | |
| 159 | 176 | $date_range = [ |
| 160 | 177 | 'start' => $current_report_date, |
| 161 | 178 | 'end' => $current_report_date, |
| @@ -161,18 +178,18 @@ | ||
| 161 | 178 | 'end' => $current_report_date, |
| 162 | 179 | ]; |
| 163 | 180 | |
| 164 | 181 | $booking_report = [ |
| 165 | - 'cancel' => timetics_count_posts( [ | |
| 182 | + 'cancel' => timetics_count_posts( array_merge( [ | |
| 166 | 183 | 'post_type' => 'timetics-booking', |
| 167 | 184 | 'post_status' => 'cancel', |
| 168 | 185 | 'date_range' => $date_range, |
| 169 | - ] ), | |
| 170 | - 'completed' => timetics_count_posts( [ | |
| 186 | + ], $booking_scope ) ), | |
| 187 | + 'completed' => timetics_count_posts( array_merge( [ | |
| 171 | 188 | 'post_type' => 'timetics-booking', |
| 172 | - 'post_status' => 'completed', | |
| 189 | + 'post_status' => $counted_statuses, | |
| 173 | 190 | 'date_range' => $date_range, |
| 174 | - ] ), | |
| 191 | + ], $booking_scope ) ), | |
| 175 | 192 | ]; |
| 176 | 193 | |
| 177 | 194 | $reports[$current_report_date] = $booking_report; |
| 178 | 195 | |
| @@ -179,6 +196,32 @@ | ||
| 179 | 196 | $current_date = strtotime( '+1 day', $current_date ); |
| 180 | 197 | } |
| 181 | 198 | |
| 182 | 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() )]; | |
| 183 | 226 | } |
| 184 | 227 | } |