You can do this using Magento Event Observer functionality as stated below:
Step1: go to config.xml under app/code/local/Allin/CustomModule/etc/ and add the following code in config.xml
config.xml
......
<frontend>
<events>
<sales_order_save_after>
<observers>
<generate_csv_save_order_after>
<type>singleton</type>
<class>custommodule/observer</class>
<method>customCSV</method>
</generate_csv_save_order_after>
</observers>
</sales_order_save_after>
</events>
</frontend>
Step2: Create Observer.php at app/code/local/Allin/CustomModule/Model/Observer.php
<?php
class Allin_CustomModule_Model_Observer
{
public function customCSV($observer)
{
$id = $observer->getEvent()->getOrder()->getId();
$order = Mage::getModel('sales/order')->load($id);
$orderIncrementId = $order->getIncrementId();
$billingAddress = $order->getBillingAddress()->getData();
$billingAdd=array(
'Billing Name' => $billingAddress['firstname']."
".$billingAddress['lastname'],
'Billing Company' => $billingAddress['company'],
'Billing Street' => $billingAddress['street'],
'Billing Zip' => $billingAddress['postcode'],
'Billing City' => $billingAddress['city'],
'Billing State' => $billingAddress['region'],
'Billing Country' => $billingAddress['country_id'],
'Billing Phone' => $billingAddress['telephone'],
);
$orderData = Mage::getResourceModel('sales/order_collection')
->addFieldToSelect(array('entity_id'=>'entity_id',
'Order Number'=>'increment_id',
'Order Date'=>'created_at',
'Order Status'=>'status',
'Order Purchased From'=>'store_name',
'Order Shipping Method'=>'shipping_method',
'Order Subtotal'=>'subtotal',
'Order Tax'=>'tax_amount',
'Order Shipping'=>'shipping_amount',
'Order Discount'=>'discount_amount',
'Order Grand Total'=>'grand_total',
'Order Base Grand Total'=>'base_grand_total',
'Order Paid'=>'total_paid',
'Order Refunded'=>'total_refunded',
'Order Due'=>'total_due',
'Total Qty Items Ordered'=>'total_qty_ordered',
'Customer Email'=>'customer_email',
'Stock Health Customer ID'=>'customer_id',
'Stock Health Customer ID'=>'customer_id',
))->addExpressionFieldToSelect(
'Customer Name',
'CONCAT({{customer_firstname}}, \' \',
{{customer_lastname}})',
array('customer_firstname' =>
'main_table.customer_firstname', 'customer_lastname' =>
'main_table.customer_lastname')
)->addExpressionFieldToSelect(
'Shipping Name',
'CONCAT({{firstname}}, \' \', {{lastname}})',
array('firstname' => 'sales_flat_order_address.firstname',
'lastname' => 'sales_flat_order_address.lastname')
)
->join(array('sales_flat_order_address' => 'sales/order_address'),
"main_table.entity_id = sales_flat_order_address.parent_id AND
sales_flat_order_address.address_type != 'billing'
AND main_table.increment_id='$orderIncrementId' ",
array(
'Shipping Company' => 'company',
'Shipping Street' => 'street',
'Shipping Zip' => 'postcode',
'Shipping City' => 'city',
'Shipping State' => 'region',
'Shipping Country' => 'country_id',
'Shipping Phone Number' => 'telephone',
))->join(array('sales_flat_order_payment' =>
'sales/order_payment'), 'main_table.entity_id =
sales_flat_order_payment.parent_id',
array(
'Order Payment Method' => 'method',
'Credit Card Type' => 'cc_type'
))->join(array('sales_flat_order_item' => 'sales/order_item'),
'main_table.entity_id = sales_flat_order_item.order_id',
array(
'Order Item Increment' => 'item_id',
'Item Name' => 'name',
'sku' => 'sku',
'Item Options' => 'product_options',
'Item Original Price' => 'original_price',
'Item Price' => 'price',
'Item Qty Ordered' => 'qty_ordered',
'Item Qty invoiced' => 'qty_invoiced',
'Item Qty shipped' => 'qty_shipped',
'Item Qty Canceled' => 'qty_canceled',
'Item Qty Refunded' => 'qty_refunded',
'Item Tax' => 'tax_amount',
'Item Discount' => 'discount_amount',
'Item Total' => 'row_total_incl_tax',
));
$headings=$orderData->getData();
foreach($headings as $k => $v)
{
array_shift($v);
$heading[]= array_merge($v,$billingAdd);
}
$filename = 'csvorder/'.date('dmY-His').$orderIncrementId.".csv";
foreach($heading[0] as $key => $value ) {
$labelArray .= $key.",";
}
$labelArray = substr($labelArray, 0, -1);
$valueArray = substr($valueArray, 0, -1);
$data[] = explode(",", $labelArray);
foreach($heading as $key => $value ) {
$data[] = $value;
}
if(count($data) > 0 ) {
$fp = fopen($filename, 'w');
foreach ($data as $product) {
fputcsv($fp, $product);
}
fclose($fp);
}
}
}
?>