Question

I was curious if there is a way to export sales reports to a CSV via the command line. I can handle the cron setup to make it run daily, I'm just curious how you would go about doing this in the shell.

If would be fantastic if I could also filter this CSV to only include certain dates or certain columns but just getting the CSV itself would be a great start.

Was it helpful?

Solution

take a look at the exportCsvAction() in Mage_Adminhtml_Sales_OrderController. maybe you could port ( and modify ) it to your own shell script

OTHER TIPS

Here's a piece of code that exports the type of collection you define in the $typeModel var;

<?php

class Export {

    var $typeModel = 'sales/order_item'; /* Swap this out with any other model */

    public function export()
    {
        $this->type = str_replace('/', '_', $this->typeModel) . '_exports';
        $dirName = Mage::getBaseDir('var') . '/' . $this->type;
        $filename = $dirName . '/' . substr($this->type,0,-1) . '_' . date('Ymd') . '.csv';

        $content = $this->getCsv();

        if(!file_exists($dirName)) {
            @mkdir($dirName);
        }

        if(file_put_contents($filename, $content)) {
            Mage::log($this->type . ' CSV saved to ' . $filename);
            if($this->sendSalesOrderItemExportMail($filename)) {
                Mage::log($this->type .' mail has been successfully sent');
            }
        }
    }

    private function getCsv() {
        $data = array();
        $csv = null;

        foreach ($this->_columns as $column) {
            if (!$column->getIsSystem()) {
                $data[] = '"'.$column->getExportHeader().'"';
            }
        }
        $csv.= implode(',', $data)."\n";

        foreach ($this->getCollection() as $item) {
            $data = array();
            foreach ($this->_columns as $column) {
                if (!$column->getIsSystem()) {
                    $data[] = '"' . str_replace(array('"', '\\'), array('""', '\\\\'),
                            $column->getRowFieldExport($item)) . '"';
                }
            }
            $csv.= implode(',', $data)."\n";
        }

        if (count($data))
        {
            $data = array();
            foreach ($this->_columns as $column) {
                if (!$column->getIsSystem()) {
                    $data[] = '"' . str_replace(array('"', '\\'), array('""', '\\\\'),
                            $column->getRowFieldExport($this->getTotals())) . '"';
                }
            }
            $csv.= implode(',', $data)."\n";
        }

        return $csv;
    }

    private function getCollection() {
        return Mage::getModel($typeModel)->getCollection();
    }

    private function sendSalesOrderItemExportMail($filename) {
        try {
            $mailTemplate = Mage::getModel('core/email_template');
            $mailTemplate->setSenderName(Mage::getStoreConfig('trans_email/ident_general/name'));
            $mailTemplate->setSenderEmail(Mage::getStoreConfig('trans_email/ident_general/email'));
            $mailTemplate->setTemplateSubject($this->typeModel . ' export CSV');
            $mailTemplate->setTemplateText('See attachment');

            $mailTemplate->getMail()->createAttachment(
                file_get_contents($filename),
                Zend_Mime::TYPE_OCTETSTREAM,
                Zend_Mime::DISPOSITION_ATTACHMENT,
                Zend_Mime::ENCODING_BASE64,
                basename($filename)
            );
            $mailTemplate->send(array('info@example.com'));
            return true;
        } catch(Exception $e) {
            Mage::logException($e);
            return false;
        }
    }

}

Or this should at least point you in the right direction.

Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top