View source
<?php
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;
}
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'],
);
}
}
function commerce_reports_tax_views_api() {
return array(
'api' => 3,
);
}
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;
}
function commerce_reports_tax_build() {
db_query('TRUNCATE {commerce_reports_tax}');
$rates = commerce_tax_rates();
$rate_names = array();
foreach ($rates as $rate) {
$rate_names[$rate['price_component']] = $rate['name'];
}
$query = new EntityFieldQuery();
$result = $query
->entityCondition('entity_type', 'commerce_order')
->propertyCondition('status', 'completed')
->execute();
$orders = entity_load('commerce_order', array_keys($result['commerce_order']));
$taxes = array();
foreach ($orders as $order) {
$wrapper = entity_metadata_wrapper('commerce_order', $order);
$order_data = $wrapper->commerce_order_total->data
->value();
$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();
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'];
}
}
drupal_write_record('commerce_reports_tax', $row);
}
drupal_set_message(t('Finished rebuilding the tax report table.'));
}