function commerce_reports_tax_build in Commerce Reporting 7
Same name and namespace in other branches
- 7.2 modules/tax/commerce_reports_tax.module \commerce_reports_tax_build()
Build the table of tax information.
1 string reference to 'commerce_reports_tax_build'
- commerce_reports_tax_settings in commerce_reports_tax/
commerce_reports_tax.module - Admin settings form
File
- commerce_reports_tax/
commerce_reports_tax.module, line 69
Code
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.'));
}