I need to see how much tax I've charged customers this financial year.

This is a placeholder issue I guess - is anyone working on this? Any ideas for the implementation?

Comments

rszrama’s picture

Title: Building a view that shows the total tax withheld over a time period » Building a report that shows the total tax withheld over a time period
Category: support » feature

I would've swore we had a discussion about this elsewhere, but I can't find it right now. It may just be in the Google Docs notes from one of our sprints at this point. The gist of it is this data isn't inherently accessible to Views, so the issue won't be possible as originally phrased. Instead you need to batch process orders and extract that data from the Order Total price field to generate the report. This information could become accessible to Views if a contributed module defined a commerce_tax_receipt table or something and populated it with data when payment was made for an order. Someone would have to write the module, though.

aidanlis’s picture

I think I can handle this for a simple tax like GST, though I guess there's going to be more complicated tax situations where totals for both VAT and GST need to be calculated.

I'll have a go and see what I can come up with ...

rszrama’s picture

Sure. fwiw, the taxes are stored separately within the price's data array, so there's no reason for there to be confusion when two or more taxes are present. Ultimately, I'd love to split price components out of the data array, as discussed in #1125706: Price Implementation for Commerce 2.x.

aidanlis’s picture

Quick and nasty starting point for anyone desperate:


function commerce_reports_tax_menu() {
  $items = array();
  
  $items['admin/commerce/reports/tax'] = array(
    'title' => 'Tax reporting',
    'description' => 'View reports about your store.',
    'page callback' => 'commerce_reports_tax_page',
    'access arguments' =>  array('access reports'),
  );
  
  return $items;
}


function commerce_reports_tax_page() {

  $orders = entity_load('commerce_order', FALSE);  
  $tax_summary = array();
  $total = 0;

  foreach ($orders as $order) {
    $wrapper = entity_metadata_wrapper('commerce_order', $order);
    $data = $wrapper->commerce_order_total->data->value();
    $components = array();
    foreach ($data['components'] as $value) {
      $components[$value['name']] = $value['price']['amount'];
    }
    
    if (isset($components['tax|gst'])) {
      $tax_summary[] = array('order' => $wrapper->order_id->value(), 'tax' => $components['tax|gst']);
      $total += $components['tax|gst'];
    }
  }
  
  $headers = array('Order ID', 'Total');

  $out = theme('table', array('header' => $headers, 'rows' => $tax_summary));
  $out .= '<p>$'. number_format($total / 100, 2) . '</p>'; // this is really lazy
  
  return $out;
}
rszrama’s picture

You'll want to make sure you're filtering to the appropriate order status, and eventually you'll probably need to batch it.

aidanlis’s picture

StatusFileSize
new27.54 KB

I'll add batching once I get a better feeling for what's going to be required, I've added in the EFQ for completed orders:

function commerce_reports_tax_page() {

  // 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
  $tax_summary = array();
  $headers = array('order_id' => t('Order ID'));
  $total = 0;
  foreach ($orders as $order) {
    $wrapper = entity_metadata_wrapper('commerce_order', $order);
    $data = $wrapper->commerce_order_total->data->value();

    $components = array('order_id' => $wrapper->order_id->value());
    foreach ($data['components'] as $value) {
      $headers[$value['name']] = $value['name'];
      $components[$value['name']] = $value['price']['amount'];
    }
    $tax_summary[] = $components;
  }

  return theme('table', array('header' => $headers, 'rows' => $tax_summary));
}

This generates us something like the attached image which will suffice for my needs, but I'll need to do a little more exploring with other tax types I guess. Any input from people already dealing with VAT / PST / GST?

rszrama’s picture

The process should be the same for every tax type. I imagine that you'll ultimately just need a date range selector and then a way to just keep a running total of all the tax collected per tax rate. Then you can create a table for 'em, using commerce_currency_format() to turn that integer value into a formatted currency decimal value,

aidanlis’s picture

Status: Active » Fixed

The module is now working (though the dynamic schema is a bit of a problem, you might need to create a column for each tax type). It's integrated with views allowing you to create your own reports easily, http://drupal.org/project/commerce_reports

rszrama’s picture

Rockin'. Since you grabbed the module name, you might want to be aware of http://drupal.org/sandbox/paul.linney/1296710.

Also, any reason the project name can't match the module name (i.e. Commerce Reports instead of Commerce Reporting)?

aidanlis’s picture

I spoke to Paul before creating the project and we decided to go this route, I've added him to the list of admins for commerce_reports so when he gets some free time he's going to merge in his code (sometime this week was the goal). The "Commerce Reports" name is taken by Paul's sandbox project, so hopefully when Paul merges in his code he can remove the sandbox project and I can change the name.

rszrama’s picture

Ok, lemme know if we need to have a site admin clean it up. Damien can work magic with this stuff. : )

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.