1 <?php
  2 /**
  3  * WC_Report_Taxes_By_Code
  4  *
  5  * @author      WooThemes
  6  * @category    Admin
  7  * @package     WooCommerce/Admin/Reports
  8  * @version     2.1.0
  9  */
 10 class WC_Report_Taxes_By_Code extends WC_Admin_Report {
 11 
 12     /**
 13      * Get the legend for the main chart sidebar
 14      * @return array
 15      */
 16     public function get_chart_legend() {
 17         $legend   = array();
 18 
 19         return array();
 20     }
 21 
 22     /**
 23      * Output an export link
 24      */
 25     public function get_export_button() {
 26         $current_range = ! empty( $_GET['range'] ) ? $_GET['range'] : 'last_month';
 27         ?>
 28         <a
 29             href="#"
 30             download="report-<?php echo $current_range; ?>-<?php echo date_i18n( 'Y-m-d', current_time('timestamp') ); ?>.csv"
 31             class="export_csv"
 32             data-export="table"
 33         >
 34             <?php _e( 'Export CSV', 'woocommerce' ); ?>
 35         </a>
 36         <?php
 37     }
 38 
 39     /**
 40      * Output the report
 41      */
 42     public function output_report() {
 43         global $woocommerce, $wpdb, $wp_locale;
 44 
 45         $ranges = array(
 46             'year'         => __( 'Year', 'woocommerce' ),
 47             'last_month'   => __( 'Last Month', 'woocommerce' ),
 48             'month'        => __( 'This Month', 'woocommerce' ),
 49         );
 50 
 51         $current_range = ! empty( $_GET['range'] ) ? $_GET['range'] : 'last_month';
 52 
 53         if ( ! in_array( $current_range, array( 'custom', 'year', 'last_month', 'month', '7day' ) ) )
 54             $current_range = 'last_month';
 55 
 56         $this->calculate_current_range( $current_range );
 57 
 58         $hide_sidebar = true;
 59 
 60         include( WC()->plugin_path() . '/includes/admin/views/html-report-by-date.php');
 61     }
 62 
 63     /**
 64      * Get the main chart
 65      * @return string
 66      */
 67     public function get_main_chart() {
 68         global $wpdb;
 69 
 70         $tax_rows = $this->get_order_report_data( array(
 71             'data' => array(
 72                 'order_item_name' => array(
 73                     'type'     => 'order_item',
 74                     'function' => '',
 75                     'name'     => 'tax_rate'
 76                 ),
 77                 'tax_amount' => array(
 78                     'type'            => 'order_item_meta',
 79                     'order_item_type' => 'tax',
 80                     'function'        => '',
 81                     'name'            => 'tax_amount'
 82                 ),
 83                 'shipping_tax_amount' => array(
 84                     'type'            => 'order_item_meta',
 85                     'order_item_type' => 'tax',
 86                     'function'        => '',
 87                     'name'            => 'shipping_tax_amount'
 88                 ),
 89                 'rate_id' => array(
 90                     'type'            => 'order_item_meta',
 91                     'order_item_type' => 'tax',
 92                     'function'        => '',
 93                     'name'            => 'rate_id'
 94                 )
 95             ),
 96             'where' => array(
 97                 array(
 98                     'key'      => 'order_item_type',
 99                     'value'    => 'tax',
100                     'operator' => '='
101                 ),
102                 array(
103                     'key'      => 'order_item_name',
104                     'value'    => '',
105                     'operator' => '!='
106                 )
107             ),
108             'order_by'     => 'post_date ASC',
109             'query_type'   => 'get_results',
110             'filter_range' => true
111         ) );
112         ?>
113         <table class="widefat">
114             <thead>
115                 <tr>
116                     <th><?php _e( 'Tax', 'woocommerce' ); ?></th>
117                     <th><?php _e( 'Rate', 'woocommerce' ); ?></th>
118                     <th class="total_row"><?php _e( 'Number of orders', 'woocommerce' ); ?></th>
119                     <th class="total_row"><?php _e( 'Tax Amount', 'woocommerce' ); ?> <a class="tips" data-tip="<?php esc_attr_e( 'This is the sum of the "Tax Rows" tax amount within your orders.', 'woocommerce' ); ?>" href="#">[?]</a></th>
120                     <th class="total_row"><?php _e( 'Shipping Tax Amount', 'woocommerce' ); ?> <a class="tips" data-tip="<?php esc_attr_e( 'This is the sum of the "Tax Rows" shipping tax amount within your orders.', 'woocommerce' ); ?>" href="#">[?]</a></th>
121                     <th class="total_row"><?php _e( 'Total Tax', 'woocommerce' ); ?> <a class="tips" data-tip="<?php esc_attr_e( 'This is the total tax for the rate (shipping tax + product tax).', 'woocommerce' ); ?>" href="#">[?]</a></th>
122                 </tr>
123             </thead>
124             <?php if ( $tax_rows ) : ?>
125                 <tfoot>
126                     <tr>
127                         <th scope="row" colspan="3"><?php _e( 'Total', 'woocommerce' ); ?></th>
128                         <th class="total_row"><?php echo wc_price( wc_round_tax_total( array_sum( wp_list_pluck( (array) $tax_rows, 'tax_amount' ) ) ) ); ?></th>
129                         <th class="total_row"><?php echo wc_price( wc_round_tax_total( array_sum( wp_list_pluck( (array) $tax_rows, 'shipping_tax_amount' ) ) ) ); ?></th>
130                         <th class="total_row"><strong><?php echo wc_price( wc_round_tax_total( array_sum( wp_list_pluck( (array) $tax_rows, 'tax_amount' ) ) + array_sum( wp_list_pluck( (array) $tax_rows, 'shipping_tax_amount' ) ) ) ); ?></strong></th>
131                     </tr>
132                 </tfoot>
133                 <tbody>
134                     <?php
135                     $grouped_tax_tows = array();
136 
137                     foreach ( $tax_rows as $tax_row ) {
138                         if ( ! isset( $grouped_tax_tows[ $tax_row->rate_id ] ) ) {
139                             $grouped_tax_tows[ $tax_row->rate_id ] = (object) array(
140                                 'tax_rate'            => $tax_row->tax_rate,
141                                 'total_orders'        => 0,
142                                 'tax_amount'          => 0,
143                                 'shipping_tax_amount' => 0
144                             );
145                         }
146                         
147                         $grouped_tax_tows[ $tax_row->rate_id ]->total_orders ++; 
148                         $grouped_tax_tows[ $tax_row->rate_id ]->tax_amount += wc_round_tax_total( $tax_row->tax_amount );
149                         $grouped_tax_tows[ $tax_row->rate_id ]->shipping_tax_amount += wc_round_tax_total( $tax_row->shipping_tax_amount );
150                     }
151 
152                     foreach ( $grouped_tax_tows as $rate_id => $tax_row ) {
153                         $rate = $wpdb->get_var( $wpdb->prepare( "SELECT tax_rate FROM {$wpdb->prefix}woocommerce_tax_rates WHERE tax_rate_id = %d;", $rate_id ) );
154                         ?>
155                         <tr>
156                             <th scope="row"><?php echo $tax_row->tax_rate; ?></th>
157                             <td><?php echo $rate; ?>%</td>
158                             <td class="total_row"><?php echo $tax_row->total_orders; ?></td>
159                             <td class="total_row"><?php echo wc_price( $tax_row->tax_amount ); ?></td>
160                             <td class="total_row"><?php echo wc_price( $tax_row->shipping_tax_amount ); ?></td>
161                             <td class="total_row"><?php echo wc_price( $tax_row->tax_amount + $tax_row->shipping_tax_amount ); ?></td>
162                         </tr>
163                         <?php
164                     }
165                     ?>
166                 </tbody>
167             <?php else : ?>
168                 <tbody>
169                     <tr>
170                         <td><?php _e( 'No taxes found in this period', 'woocommerce' ); ?></td>
171                     </tr>
172                 </tbody>
173             <?php endif; ?>
174         </table>
175         <?php
176     }
177 }
WooCommerce API documentation generated by ApiGen 2.8.0