PluginProbe
Timetics – Appointment Booking Calendar & Scheduling / 1.0.64
Timetics – Appointment Booking Calendar & Scheduling v1.0.64
1.0.64 1.0.62 1.0.63 1.0.61 1.0.60 1.0.59 1.0.58 1.0.57 1.0.56 trunk 1.0.0 1.0.1 1.0.10 1.0.11 1.0.12 1.0.13 1.0.14 1.0.15 1.0.16 1.0.17 1.0.18 1.0.19 1.0.2 1.0.20 1.0.21 All 65 releases
← All changes | core/reports/api-report.php +154 -12 1.0.2 → 1.0.64 View file →
@@ -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 /**
@@ -12,9 +14,9 @@
12 14
13 15 /**
14 16 * Class Api Reports
15 17 */
16 -class Api_Report extends Api {
18 +class Api_Report extends Api {
17 19 use Singleton;
18 20
19 21 /**
20 22 * Store api namespace
@@ -43,9 +45,9 @@
43 45 */
44 46 register_rest_route(
45 47 $this->namespace, $this->rest_base . '/overview', [
46 48 [
47 - 'methods' => \WP_REST_Server::READABLE,
49 + 'methods' => \WP_REST_Server::CREATABLE,
48 50 'callback' => [$this, 'get_overview'],
49 51 'permission_callback' => function () {
50 52 return current_user_can( 'manage_timetics' );
51 53 },
@@ -61,20 +63,15 @@
61 63 *
62 64 * @return JSON
63 65 */
64 66 public function get_overview( $request ) {
65 - $start_date = isset( $request['start_date'] ) ? sanitize_text_field( $request['start_date'] ) : '';
67 + $input_data = json_decode( $request->get_body(), true );
66 68
67 - $end_date = isset( $request['end_date'] ) ? sanitize_text_field( $request['end_date'] ) : '';
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 ) );
68 73
69 - $data = [
70 - 'total_meetings' => timetics_count_posts( ['post_type' => 'timetics-appointment'] ),
71 - 'total_bookings' => timetics_count_posts( ['post_type' => 'timetics-booking', 'post_status'=>'approved'] ),
72 - 'total_sales' => number_format(timetics_get_total_sale(), 2),
73 - 'total_customer' => timetics_count_users( ['role' => 'timetics-customer'] ),
74 - 'total_staff' => timetics_count_users( ['role' => 'timetics-staff'] ),
75 - ];
76 -
77 74 $response = [
78 75 'status_code' => 200,
79 76 'success' => 1,
80 77 'data' => $data,
@@ -80,6 +77,151 @@
80 77 'data' => $data,
81 78 ];
82 79
83 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() )];
84 226 }
85 227 }