Group Invoices by customer

1.1k Views Asked by At

I'm trying to group all customers by their tax-vat id and show how much tax was invoiced to them, so my result should look like this

TAXVAT | Tax Sum
ATU321 | 365.50
ATU123 | 120.00

but I'm pretty stuck with the groupBy function of magento, i tried this:

$objInvoiceCollection = Mage::getModel('sales/order/invoice')->getCollection();

$objInvoiceCollection 
->addAttributeToSelect('customer_taxvat');
->addExpressionAttributeToSelect('tax_invoiced','SUM({{tax_invoiced}})','tax')
->addFieldToFilter('customer_taxvat', array('notnull'=>true))
->addFieldToFilter('tax_invoiced', array('notnull'=>true))
->groupByAttribute('customer_taxvat');

but i get this error:

Call to undefined method Mage_Sales_Model_Resource_Order_Collection::addExpressionAttributeToSelect() ...

I thought every eav-based model provides the methods addAttributeToSelect and groupByAttribute - but obviously thats not true in my case :(

1

There are 1 best solutions below

0
On BEST ANSWER

Magento stopped using EAV for storing sales several versions ago. Also, the customer_taxvat is not stored as part of the invoice, but as part of the order. You can get the same results this way:

<?php
// Set Up Collection
$objOrderCollection = Mage::getResourceModel('sales/order_collection')
  ->addFieldToSelect('customer_taxvat')
  ->addExpressionFieldToSelect('tax_invoiced','SUM(tax_invoiced)','tax')
  ->addFieldToFilter('customer_taxvat', array('notnull'=>true))
  ->addFieldToFilter('tax_invoiced', array('notnull'=>true));
// Add Group By
$objOrderCollection->getSelect()->group('customer_taxvat');

// Do your stuff!
foreach ( $objOrderCollection as $order ) { ... }
?>