Skip to content

Instantly share code, notes, and snippets.

@jonatanrdsantos
Created July 29, 2016 01:26
Show Gist options
  • Save jonatanrdsantos/f1387c329ec1d17d39db889a8c29c4d6 to your computer and use it in GitHub Desktop.
Save jonatanrdsantos/f1387c329ec1d17d39db889a8c29c4d6 to your computer and use it in GitHub Desktop.
Magento custom order report to CSV
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
require_once 'app/Mage.php';
Mage::app();
//echo "Testando<br>";
$fromDate = date('Y-m-d H:i:s', strtotime('2016-05-28 00:00:01'));
$toDate = date('Y-m-d H:i:s', strtotime('2016-07-28 23:59:59'));
$orders = Mage::getModel('sales/order')->getCollection()
->addFieldToSelect('increment_id')
->addFieldToSelect('created_at')
->addFieldToSelect('base_subtotal_invoiced')
->addFieldToSelect('total_paid')
->addFieldToSelect('base_shipping_amount')
->addFieldToSelect('base_discount_amount')
->addAttributeToFilter('main_table.created_at', array('from'=>$fromDate, 'to'=>$toDate))
// ->addAttributeToFilter('main_table.status', array('eq' => Mage_Sales_Model_Order::STATE_COMPLETE))
->addAttributeToFilter('main_table.base_subtotal_invoiced', array('gteq' => 0));
$orders->getSelect()->group('main_table.entity_id');
function array2csv(array &$array)
{
if (count($array) == 0) {
return null;
}
ob_start();
$df = fopen("php://output", 'w');
fputcsv($df, array_keys(reset($array)));
foreach ($array as $row) {
fputcsv($df, $row);
}
fclose($df);
return ob_get_clean();
}
function download_send_headers($filename) {
// disable caching
$now = gmdate("D, d M Y H:i:s");
header("Expires: Tue, 03 Jul 2001 06:00:00 GMT");
header("Cache-Control: max-age=0, no-cache, must-revalidate, proxy-revalidate");
header("Last-Modified: {$now} GMT");
// force download
header("Content-Type: application/force-download");
header("Content-Type: application/octet-stream");
header("Content-Type: application/download");
// disposition / encoding on response body
header("Content-Disposition: attachment;filename={$filename}");
header("Content-Transfer-Encoding: binary");
}
echo $orders->count();
download_send_headers("data_export_" . date("Y-m-d") . ".csv");
echo array2csv($orders->getData());
die();
echo $orders->getSelect();
foreach ($orders as $order): ?>
<br>
increment ID: <?php echo $order->getIncrementId()?><br>
Criado em: <?php echo Mage::getModel('core/date')->date('d/m/Y H:i:s', strtotime($order->getCreatedAt()));?><br>
Subtotal Loja: <?php echo Mage::helper('core')->currency($order->getBaseSubtotalInvoiced(), true, false);?><br>
Frete: <?php echo Mage::helper('core')->currency($order->getBaseShippingAmount(), true, false);?><br>
Total Pago: <?php echo Mage::helper('core')->currency( $order->getTotalPaid(), true, false);?><br>
Desconto: <?php echo Mage::helper('core')->currency( $order->getBaseDiscountAmount(), true, false);?><br>
<?php endforeach;?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment