/**
  * Invoices Report function.
  */
 function get_invoices_report($year, $month)
 {
     $output = '';
     $data = array();
     $data[] = array(__('Invoice Nr.', 'woocommerce-jetpack'), __('Invoice Date', 'woocommerce-jetpack'), __('Order ID', 'woocommerce-jetpack'), __('Customer Country', 'woocommerce-jetpack'), __('Tax %', 'woocommerce-jetpack'), __('Order Total Tax Excl.', 'woocommerce-jetpack'), __('Order Taxes', 'woocommerce-jetpack'), __('Order Total', 'woocommerce-jetpack'), __('Order Currency', 'woocommerce-jetpack'));
     $total_sum = 0;
     $total_sum_excl_tax = 0;
     $total_tax = 0;
     $args = array('post_type' => 'shop_order', 'post_status' => 'any', 'posts_per_page' => -1, 'orderby' => 'date', 'order' => 'ASC', 'year' => $year, 'monthnum' => $month);
     $loop = new WP_Query($args);
     while ($loop->have_posts()) {
         $loop->the_post();
         $order_id = $loop->post->ID;
         $invoice_type_id = 'invoice';
         if (wcj_is_invoice_created($order_id, $invoice_type_id)) {
             $the_order = wc_get_order($order_id);
             $user_meta = get_user_meta($the_order->get_user_id());
             $billing_country = isset($user_meta['billing_country'][0]) ? $user_meta['billing_country'][0] : '';
             $shipping_country = isset($user_meta['shipping_country'][0]) ? $user_meta['shipping_country'][0] : '';
             $customer_country = '' == $billing_country ? $shipping_country : $billing_country;
             $order_total = $the_order->get_total();
             $order_tax = apply_filters('wcj_order_total_tax', $the_order->get_total_tax(), $the_order);
             //$order_tax_percent = ( isset( $taxes_by_countries_eu[ $customer_country ] ) ) ? $taxes_by_countries_eu[ $customer_country ] : 0;
             //$order_tax_percent /= 100;
             //$order_tax = $order_total * $order_tax_percent;
             $order_total_exlc_tax = $order_total - $order_tax;
             $order_tax_percent = 0 == $order_total ? 0 : $order_tax / $order_total_exlc_tax;
             $total_sum += $order_total;
             $total_sum_excl_tax += $order_total_exlc_tax;
             $total_tax += $order_tax;
             //$order_tax_html = ( 0 == $order_tax ) ? '' : sprintf( '$ %.2f', $order_tax );
             $order_tax_html = sprintf('%.2f', $order_tax);
             $data[] = array(wcj_get_invoice_number($order_id, $invoice_type_id), wcj_get_invoice_date($order_id, $invoice_type_id, 0, get_option('date_format')), $order_id, $customer_country, sprintf('%.0f %%', $order_tax_percent * 100), sprintf('%.2f', $order_total_exlc_tax), $order_tax_html, sprintf('%.2f', $order_total), $the_order->get_order_currency());
         }
     }
     /* $output .= '<h3>' . 'Total Sum Excl. Tax: ' . sprintf( '$ %.2f', $total_sum_excl_tax ) . '</h3>';
     		$output .= '<h3>' . 'Total Sum: ' . sprintf( '$ %.2f', $total_sum ) . '</h3>';
     		$output .= '<h3>' . 'Total Tax: ' . sprintf( '$ %.2f', $total_tax ) . '</h3>'; */
     $output .= wcj_get_table_html($data, array('table_class' => 'widefat'));
     /**/
     return $output;
 }
 function wcj_credit_note_date($atts)
 {
     return wcj_get_invoice_date($atts['order_id'], 'credit_note', $atts['days'], $atts['date_format']);
 }
 function wcj_custom_doc_date($atts)
 {
     return wcj_get_invoice_date($atts['order_id'], 'custom_doc', $atts['days'], $atts['date_format']);
 }
 /**
  * Invoices Report Data function.
  *
  * @version 2.5.7
  * @since   2.5.7
  */
 function get_invoices_report_data($year, $month, $invoice_type_id)
 {
     $data = array();
     $data[] = array(__('Document Nr.', 'woocommerce-jetpack'), __('Document Date', 'woocommerce-jetpack'), __('Order ID', 'woocommerce-jetpack'), __('Customer Country', 'woocommerce-jetpack'), __('Customer VAT ID', 'woocommerce-jetpack'), __('Tax %', 'woocommerce-jetpack'), __('Order Total Tax Excl.', 'woocommerce-jetpack'), __('Order Taxes', 'woocommerce-jetpack'), __('Order Total', 'woocommerce-jetpack'), __('Order Currency', 'woocommerce-jetpack'), __('Refunds', 'woocommerce-jetpack'));
     $total_sum = 0;
     $total_sum_excl_tax = 0;
     $total_tax = 0;
     $first_minute = mktime(0, 0, 0, $month, 1, $year);
     $last_minute = mktime(23, 59, 59, $month, date('t', $first_minute), $year);
     $offset = 0;
     $block_size = 256;
     while (true) {
         $args = array('post_type' => 'shop_order', 'post_status' => 'any', 'posts_per_page' => $block_size, 'orderby' => 'meta_value_num', 'meta_key' => '_wcj_invoicing_' . $invoice_type_id . '_date', 'order' => 'ASC', 'meta_query' => array(array('key' => '_wcj_invoicing_' . $invoice_type_id . '_date', 'value' => array($first_minute, $last_minute), 'type' => 'numeric', 'compare' => 'BETWEEN')), 'offset' => $offset, 'fields' => 'ids');
         $loop = new WP_Query($args);
         if (!$loop->have_posts()) {
             break;
         }
         foreach ($loop->posts as $order_id) {
             if (wcj_is_invoice_created($order_id, $invoice_type_id)) {
                 $the_order = wc_get_order($order_id);
                 $user_meta = get_user_meta($the_order->get_user_id());
                 $billing_country = isset($user_meta['billing_country'][0]) ? $user_meta['billing_country'][0] : '';
                 $shipping_country = isset($user_meta['shipping_country'][0]) ? $user_meta['shipping_country'][0] : '';
                 $customer_country = '' == $billing_country ? $shipping_country : $billing_country;
                 $customer_vat_id = get_post_meta($order_id, '_billing_eu_vat_number', true);
                 $order_total = $the_order->get_total();
                 $order_tax = apply_filters('wcj_order_total_tax', $the_order->get_total_tax(), $the_order);
                 //					$order_tax_percent = ( isset( $taxes_by_countries_eu[ $customer_country ] ) ) ? $taxes_by_countries_eu[ $customer_country ] : 0;
                 //					$order_tax_percent /= 100;
                 //					$order_tax = $order_total * $order_tax_percent;
                 $order_total_exlc_tax = $order_total - $order_tax;
                 $order_total_tax_not_rounded = $the_order->get_cart_tax() + $the_order->get_shipping_tax();
                 $order_tax_percent = 0 == $order_total ? 0 : $order_total_tax_not_rounded / $order_total_exlc_tax;
                 $total_sum += $order_total;
                 $total_sum_excl_tax += $order_total_exlc_tax;
                 $total_tax += $order_tax;
                 //					$order_tax_html = ( 0 == $order_tax ) ? '' : sprintf( '$ %.2f', $order_tax );
                 $order_tax_html = sprintf('%.2f', $order_tax);
                 $data[] = array(wcj_get_invoice_number($order_id, $invoice_type_id), wcj_get_invoice_date($order_id, $invoice_type_id, 0, get_option('date_format')), $order_id, $customer_country, $customer_vat_id, sprintf('%.0f %%', $order_tax_percent * 100), sprintf('%.2f', $order_total_exlc_tax), $order_tax_html, sprintf('%.2f', $order_total), $the_order->get_order_currency(), $the_order->get_total_refunded());
             }
         }
         $offset += $block_size;
     }
     return $data;
 }