You are here

commerce_reports_tax.module in Commerce Reporting 7

File

commerce_reports_tax/commerce_reports_tax.module
View source
<?php

/**
 * Implements hook_menu().
 */
function commerce_reports_tax_menu() {
  $items = array();
  $items['admin/commerce/config/taxes/reports'] = array(
    'title' => 'Tax reporting',
    'description' => 'Tax reporting.',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'commerce_reports_tax_settings',
    ),
    'access arguments' => array(
      'configure store',
    ),
    'type' => MENU_LOCAL_TASK,
  );
  return $items;
}

/**
 * Implements hook_schema_alter().
 */
function commerce_reports_tax_schema_alter(&$schema) {
  $rates = commerce_tax_rates();
  foreach ($rates as $type => $rate) {
    $schema['commerce_reports_tax']['fields'][$type] = array(
      'type' => 'int',
      'not null' => FALSE,
      'default' => NULL,
      'description' => $rate['display_title'],
    );
  }
}

/**
 * Implements hook_views_api().
 */
function commerce_reports_tax_views_api() {
  return array(
    'api' => 3,
  );
}

/**
 * Admin settings form
 */
function commerce_reports_tax_settings() {
  $form = array();
  $form['commerce_reports_tax_help'] = array(
    '#prefix' => '<p>',
    '#markup' => t('Due to the complexity of most taxation systems, the information needed to display a tax report must be built the first time you install this module.'),
    '#suffix' => '</p>',
  );
  $form['commerce_reports_tax_rebuild'] = array(
    '#type' => 'submit',
    '#value' => t('Rebuild tax database'),
    '#submit' => array(
      'commerce_reports_tax_build',
    ),
  );
  return $form;
}

/**
 * Build the table of tax information.
 */
function commerce_reports_tax_build() {

  // Clear the database
  db_query('TRUNCATE {commerce_reports_tax}');

  // Key the rates
  $rates = commerce_tax_rates();
  $rate_names = array();
  foreach ($rates as $rate) {
    $rate_names[$rate['price_component']] = $rate['name'];
  }

  // Load all completed orders in the system
  $query = new EntityFieldQuery();
  $result = $query
    ->entityCondition('entity_type', 'commerce_order')
    ->propertyCondition('status', 'completed')
    ->execute();
  $orders = entity_load('commerce_order', array_keys($result['commerce_order']));

  // Iterate the orders and break each order down into its components
  $taxes = array();
  foreach ($orders as $order) {
    $wrapper = entity_metadata_wrapper('commerce_order', $order);
    $order_data = $wrapper->commerce_order_total->data
      ->value();

    // Build the base object
    $row = new stdClass();
    $row->order_id = $wrapper->order_id
      ->value();
    $row->created = $wrapper->created
      ->value();
    $row->currency_code = $wrapper->commerce_order_total->currency_code
      ->value();

    // Add the taxes
    foreach ($order_data['components'] as $component) {
      if (substr($component['name'], 0, 3) === 'tax') {
        $field_name = $rate_names[$component['name']];
        $row->{$field_name} = $component['price']['amount'];
      }
    }

    // Write the record
    drupal_write_record('commerce_reports_tax', $row);
  }
  drupal_set_message(t('Finished rebuilding the tax report table.'));
}

Functions