1

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.

  • At this point I'm considering running a MySQL dump from the coupon_aggregated table. It would be easier of course not to have to dump raw EAV table data and just export from Magento. – sparecycle May 1 '14 at 21:35
2

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

1

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.