Magento – How to get order Grand Total from information about order’s items (products)

databasegrand-totalmagento-1sales-ordersql

I'm building a custom report on the basis of database.

I need to know how to get 'Grand Total' that is in 'sales/order' (in other words sales_flat_order) table by order items (from 'sales/order_item', sales_flat_order_item table).

I need a formula to get this value using the database and not using Magento methods.

I am building MySQL query

Am I right when I say that

base_grand_total =  (IFNULL(base_total_canceled,0)) +  (IFNULL(base_hidden_tax_amount,0) - IFNULL(base_hidden_tax_refunded,0)) + IFNULL(base_total_offline_refunded,0) + IFNULL(base_total_online_refunded,0) +  ( IFNULL(base_subtotal,0) - IFNULL(base_subtotal_canceled,0) - IFNULL(base_subtotal_refunded,0)) + (IFNULL(base_tax_amount,0) - IFNULL(base_tax_canceled,0) - IFNULL(base_tax_refunded,0)) + (IFNULL(base_shipping_amount,0) - IFNULL(base_shipping_canceled,0) - IFNULL(base_shipping_refunded,0)) + (IFNULL(base_discount_amount,0) - IFNULL(base_discount_canceled,0) - IFNULL(base_discount_refunded,0)) ?

Best Answer

You can pass the order_entity_id and get the grand total value

function get_order_details($order_entity_id)
{
$getsales = mysql_query("SELECT grand_total FROM `sales_flat_order` WHERE `entity_id`='$order_entity_id'");
if(mysql_num_rows($getsales) == 1)
{
return mysql_fetch_object($getsales);
}
elseif(mysql_num_rows($getsales) > 1)
{
return $getsales;
}
else
return FALSE;
}