Question

Let me preface by saying I'm new to Magento as well as Data Collections in general (only recently begun working with OOP/frameworks).

I've followed the excellent tutorial here and I'm familiar with Alan Storm's overviews on the subject. My aim is to create a custom Magento report which, given a start/end date, will return the following totals:

  • Taxable Net (SUM subtotal for orders with tax)
  • Non-Taxable Net (SUM subtotal for orders without tax)
  • *Total Gross Sales (Grand total)
  • *Total Net Sales (Grand subtotal)
  • *Total Shipping
  • *Total Tax

*For these figures, I realize they are available in existing separate reports or can be manually calculated from them, however the purpose of this report is to give our store owner a single page to visit and file to export to send to his accountant for tax purposes.

I have the basic report structure already in place in Adminhtml including the date range, and I'm confident I can include additional filters if needed for order status/etc. Now I just need to pull the correct Data collection and figure out how to retrieve the relevant data.

My trouble is I can't make heads or tails of how the orders data is stored, what Joins are necessary (if any), how to manipulate the data once I have it, or how they interface with the Grid I've set up. The existing tutorials on the subject that I've found are all specifically dealing with product reports, as opposed to the aggregate sales data I need.

Many thanks in advance if anyone can point me in the right direction to a resource that can help me understand how to work with Magento sales data, or offer any other insight.

Was it helpful?

Solution

I have been working on something extremely similar and I used that tutorial as my base.

Expanding Orders Join Inner

Most of the order information you need is located in sales_flat_order with relates to $this->getTable('sales/order')

This actually already exists in her code but the array is empty so you need to populate it with the fields you want, here for example is mine:

->joinInner(
    array('order' => $this->getTable('sales/order')),
        implode(' AND ', $orderJoinCondition),
    array(
        'order_id' => 'order.entity_id',
        'store_id' => 'order.store_id',
        'currency_code' => 'order.order_currency_code',
        'state' => 'order.state',
        'status' => 'order.status',
        'shipping_amount' => 'order.shipping_amount',
        'shipping_tax_amount' => 'order.shipping_tax_amount',
        'shipping_incl_tax' => 'base_shipping_incl_tax',
        'subtotal' => 'order.subtotal',
        'subtotal_incl_tax' => 'order.subtotal_incl_tax',
        'total_item_count' => 'order.total_item_count',
        'created_at' => 'order.created_at',
        'updated_at' => 'order.updated_at'
    ))

To find the fields just desc sales_flat_order in mysql.

Adding additional Join Left

Ok so if you want information from other tables you need to add an ->joinLeft() for example I needed the shipment tracking number:

Create the Join condition:

$shipmentJoinCondition   = array(
    $orderTableAliasName . '.entity_id = shipment.order_id'
);

Perform the join left:

->joinLeft(
    array('shipment' => $this->getTable('sales/shipment_track')),
        implode(' AND ', $shipmentJoinCondition),
    array(
        'track_number' => 'shipment.track_number'
    )
)

Sorry I couldn't go into more depth just dropping the snippet for you here.

Performing Calculations

To modify the data returned to the grid you have to change addItem(Varien_Object $item) in your model, basically whatever is returned from here get put in the grid, and well I am not 100% sure how it works and it seems a bit magical to me.

Ok first things first $item is an object, whatever you do to this object will stay with the object (sorry terrible explanation): Example, I wanted to return each order on a separate line and for each have (1/3, 2/3, 3/3), any changes I made would happen globally to the order object so they would all show (3/3). So keep this in mind, if funky stuff starts happening use PHP Clone.

$item_array = clone $item;

So now onto your logic, you can add any key you want to the array and it will be accessible in Grid.php

For example(bad since subtotal_incl_tax exists) :

$item_array['my_taxable_net_calc'] = $item['sub_total'] + $item['tax'];

Then at the end do:

$this->_items[] = $item_array;
return $this->_items;

You can also add more rows based on the existing by just adding more data to $this->_items[];

$this->_items[] = $item_array;
$this->_items[] = $item_array;
return $this->_items;

Would return same item on two lines.

Sorry I have started to lose the plot, if something doesn't make sense just ask, hope this helped.

Oh and to add to Block/Adminhtml/namespace/Grid.php

$this->addColumn('my_taxable_net_calc', array(
    'header'    => Mage::helper('report')->__('Taxable Net'),
    'sortable'  => false,
    'filter'    => false,
    'index'     => 'my_taxable_net_calc'
)); 
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top