1 <?php
2 3 4 5 6 7 8 9 10
11
12
13 if ( ! defined( 'ABSPATH' ) ) exit;
14
15 16 17 18 19 20 21 22 23
24 function edd_reports_page() {
25 global $edd_options;
26
27 $current_page = admin_url( 'edit.php?post_type=download&page=edd-reports' );
28 $active_tab = isset( $_GET[ 'tab' ] ) ? $_GET[ 'tab' ] : 'reports';
29 ?>
30 <div class="wrap">
31 <h2 class="nav-tab-wrapper">
32 <a href="<?php echo add_query_arg( array( 'tab' => 'reports', 'settings-updated' => false ), $current_page ); ?>" class="nav-tab <?php echo $active_tab == 'reports' ? 'nav-tab-active' : ''; ?>"><?php _e( 'Reports', 'edd' ); ?></a>
33 <?php if ( current_user_can( 'export_shop_reports' ) ) { ?>
34 <a href="<?php echo add_query_arg( array( 'tab' => 'export', 'settings-updated' => false ), $current_page ); ?>" class="nav-tab <?php echo $active_tab == 'export' ? 'nav-tab-active' : ''; ?>"><?php _e( 'Export', 'edd' ); ?></a>
35 <?php } ?>
36 <a href="<?php echo add_query_arg( array( 'tab' => 'logs', 'settings-updated' => false ), $current_page ); ?>" class="nav-tab <?php echo $active_tab == 'logs' ? 'nav-tab-active' : ''; ?>"><?php _e( 'Logs', 'edd' ); ?></a>
37 <?php do_action( 'edd_reports_tabs' ); ?>
38 </h2>
39
40 <?php
41 do_action( 'edd_reports_page_top' );
42 do_action( 'edd_reports_tab_' . $active_tab );
43 do_action( 'edd_reports_page_bottom' );
44 ?>
45 </div><!-- .wrap -->
46 <?php
47 }
48
49 50 51 52 53 54
55 function edd_reports_default_views() {
56 $views = array(
57 'earnings' => __( 'Earnings', 'edd' ),
58 'downloads' => edd_get_label_plural(),
59 'customers' => __( 'Customers', 'edd' ),
60 'taxes' => __( 'Taxes', 'edd' )
61 );
62
63 $views = apply_filters( 'edd_report_views', $views );
64
65 return $views;
66 }
67
68 69 70 71 72 73
74 function edd_reports_tab_reports() {
75 $current_view = 'earnings';
76 $views = edd_reports_default_views();
77
78 if ( isset( $_GET[ 'view' ] ) && array_key_exists( $_GET[ 'view' ], $views ) )
79 $current_view = $_GET[ 'view' ];
80
81 do_action( 'edd_reports_view_' . $current_view );
82 }
83 add_action( 'edd_reports_tab_reports', 'edd_reports_tab_reports' );
84
85 86 87 88 89 90
91 function edd_report_views() {
92 $views = edd_reports_default_views();
93 $current_view = isset( $_GET[ 'view' ] ) ? $_GET[ 'view' ] : 'earnings';
94 ?>
95 <form id="edd-reports-filter" method="get">
96 <select id="edd-reports-view" name="view">
97 <option value="-1"><?php _e( 'Report Type', 'edd' ); ?></option>
98 <?php foreach ( $views as $view_id => $label ) : ?>
99 <option value="<?php echo esc_attr( $view_id ); ?>" <?php selected( $view_id, $current_view ); ?>><?php echo $label; ?></option>
100 <?php endforeach; ?>
101 </select>
102
103 <?php do_action( 'edd_report_view_actions' ); ?>
104
105 <input type="hidden" name="post_type" value="download"/>
106 <input type="hidden" name="page" value="edd-reports"/>
107 <?php submit_button( __( 'Show', 'edd' ), 'secondary', 'submit', false ); ?>
108 </form>
109 <?php
110 }
111
112 113 114 115 116 117 118 119
120 function edd_reports_downloads_table() {
121 include( dirname( __FILE__ ) . '/class-download-reports-table.php' );
122
123 $downloads_table = new EDD_Download_Reports_Table();
124 $downloads_table->prepare_items();
125 $downloads_table->display();
126 }
127 add_action( 'edd_reports_view_downloads', 'edd_reports_downloads_table' );
128
129 130 131 132 133 134 135 136
137 function edd_reports_customers_table() {
138 include( dirname( __FILE__ ) . '/class-customer-reports-table.php' );
139
140 $downloads_table = new EDD_Customer_Reports_Table();
141 $downloads_table->prepare_items();
142 $downloads_table->display();
143 }
144 add_action( 'edd_reports_view_customers', 'edd_reports_customers_table' );
145
146 147 148 149 150 151
152 function edd_reports_earnings() {
153 ?>
154 <div class="tablenav top">
155 <div class="alignleft actions"><?php edd_report_views(); ?></div>
156 </div>
157 <?php
158 edd_reports_graph();
159 }
160 add_action( 'edd_reports_view_earnings', 'edd_reports_earnings' );
161
162 163 164 165 166 167
168 function edd_reports_taxes() {
169 $year = isset( $_GET['year'] ) ? absint( $_GET['year'] ) : date( 'Y' );
170 ?>
171 <div class="tablenav top">
172 <div class="alignleft actions"><?php edd_report_views(); ?></div>
173 </div>
174
175 <div class="metabox-holder" style="padding-top: 0;">
176 <div class="postbox">
177 <h3><span><?php _e('Tax Report', 'edd'); ?></span></h3>
178 <div class="inside">
179 <p><?php _e( 'This report shows the total amount collected in sales tax for the given year.', 'edd' ); ?></p>
180 <form method="get" action="<?php echo admin_url( 'edit.php' ); ?>">
181 <span><?php echo $year; ?></span>: <strong><?php edd_sales_tax_for_year( $year ); ?></strong> —
182 <select name="year">
183 <?php for ( $i = 2009; $i <= date( 'Y' ); $i++ ) : ?>
184 <option value="<?php echo $i; ?>"<?php selected( $year, $i ); ?>><?php echo $i; ?></option>
185 <?php endfor; ?>
186 </select>
187 <input type="hidden" name="view" value="taxes" />
188 <input type="hidden" name="post_type" value="download" />
189 <input type="hidden" name="page" value="edd-reports" />
190 <?php submit_button( __( 'Submit', 'edd' ), 'secondary', 'submit', false ); ?>
191 </form>
192 </div><!-- .inside -->
193 </div><!-- .postbox -->
194 </div><!-- .metabox-holder -->
195 <?php
196 }
197 add_action( 'edd_reports_view_taxes', 'edd_reports_taxes' );
198
199 200 201 202 203 204
205 function edd_reports_tab_export() {
206 ?>
207 <div class="metabox-holder">
208 <div id="post-body">
209 <div id="post-body-content">
210
211 <?php do_action( 'edd_reports_tab_export_content_top' ); ?>
212
213 <div class="postbox">
214 <h3><span><?php _e( 'Export PDF of Sales and Earnings', 'edd' ); ?></span></h3>
215 <div class="inside">
216 <p><?php _e( 'Download a PDF of Sales and Earnings reports for all products for the current year.', 'edd' ); ?> <?php _e( 'Date range reports will be coming soon.', 'edd' ); ?></p>
217 <p><a class="button" href="<?php echo wp_nonce_url( add_query_arg( array( 'edd-action' => 'generate_pdf' ) ), 'edd_generate_pdf' ); ?>"><?php _e( 'Generate PDF', 'edd' ); ?></a></p>
218 </div><!-- .inside -->
219 </div><!-- .postbox -->
220
221 <div class="postbox">
222 <h3><span><?php _e('Export Payment History', 'edd'); ?></span></h3>
223 <div class="inside">
224 <p><?php _e( 'Download a CSV of all payments recorded.', 'edd' ); ?></p>
225 <p>
226 <form method="post">
227 <select name="edd_export_payment_status">
228 <option value="0"><?php _e( 'All Statuses', 'edd' ); ?></option>
229 <?php
230 $statuses = edd_get_payment_statuses();
231 foreach( $statuses as $status => $label ) {
232 echo '<option value="' . $status . '">' . $label . '</option>';
233 }
234 ?>
235 </select>
236 <input type="hidden" name="edd-action" value="payment_export"/>
237 <input type="submit" value="<?php _e( 'Generate CSV', 'edd' ); ?>" class="button-secondary"/>
238 </form>
239 </p>
240 </div><!-- .inside -->
241 </div><!-- .postbox -->
242
243 <div class="postbox">
244 <h3><span><?php _e('Export Customers in CSV', 'edd'); ?></span></h3>
245 <div class="inside">
246 <p><?php _e( 'Download a CSV of all customer emails. Optionally export only customers that have purchased a particular product.', 'edd' ); ?></p>
247 <p>
248 <form method="post">
249 <select name="edd_export_download">
250 <option value="0"><?php _e( 'All', 'edd' ); ?></option>
251 <?php
252 $downloads = get_posts( array( 'post_type' => 'download', 'posts_per_page' => -1 ) );
253 if( $downloads ) {
254 foreach( $downloads as $download ) {
255 echo '<option value="' . $download->ID . '">' . get_the_title( $download->ID ) . '</option>';
256 }
257 }
258 ?>
259 </select>
260 <input type="hidden" name="edd-action" value="email_export"/>
261 <input type="submit" value="<?php _e( 'Generate CSV', 'edd' ); ?>" class="button-secondary"/>
262 </form>
263 </p>
264 </div><!-- .inside -->
265 </div><!-- .postbox -->
266
267 <div class="postbox">
268 <h3><span><?php _e('Export Download History in CSV', 'edd'); ?></span></h3>
269 <div class="inside">
270 <p><?php _e( 'Download a CSV of all file downloads for the current month.', 'edd' ); ?></p>
271 <p><a class="button" href="<?php echo wp_nonce_url( add_query_arg( array( 'edd-action' => 'downloads_history_export' ) ), 'edd_export_all_downloads_history' ); ?>"><?php _e( 'Generate CSV', 'edd' ) ; ?></a></p>
272 </div><!-- .inside -->
273 </div><!-- .postbox -->
274
275 <?php do_action( 'edd_reports_tab_export_content_bottom' ); ?>
276
277 </div><!-- .post-body-content -->
278 </div><!-- .post-body -->
279 </div><!-- .metabox-holder -->
280 <?php
281 }
282 add_action( 'edd_reports_tab_export', 'edd_reports_tab_export' );
283
284 285 286 287 288 289
290 function edd_reports_tab_logs() {
291 require( EDD_PLUGIN_DIR . 'includes/admin/reporting/logs.php' );
292
293 $current_view = 'file_downloads';
294 $log_views = edd_log_default_views();
295
296 if ( isset( $_GET[ 'view' ] ) && array_key_exists( $_GET[ 'view' ], $log_views ) )
297 $current_view = $_GET[ 'view' ];
298
299 do_action( 'edd_logs_view_' . $current_view );
300 }
301 add_action( 'edd_reports_tab_logs', 'edd_reports_tab_logs' );
302
303 304 305 306 307 308
309 function edd_estimated_monthly_stats() {
310 $estimated = get_transient( 'edd_estimated_monthly_stats' );
311
312 if ( false === $estimated ) {
313 $estimated = array(
314 'earnings' => 0,
315 'sales' => 0
316 );
317
318 $products = get_posts( array( 'post_type' => 'download', 'posts_per_page' => -1, 'fields' => 'ids' ) );
319 if ( $products ) {
320 foreach ( $products as $download ) {
321 $estimated['earnings'] += edd_get_average_monthly_download_earnings( $download );
322 $estimated['sales'] += number_format( edd_get_average_monthly_download_sales( $download ), 0 );
323 }
324 }
325
326
327 set_transient( 'edd_estimated_monthly_stats', serialize( $estimated ), 86400 );
328 }
329
330 return maybe_unserialize( $estimated );
331 }