Shell Script to Retrieve Subscription Reports


Versions of the Subscribe Pro extension after 1.3.2.2 now include a shell script (also available for download on the page) that allows you to export subscription reports to your Magento installation.

You can find the script, called autoship_report_dl.php, in the shell folder or in the code block below. Note that the script requires methods introduced in version 1.3.2.2, so it will cause an error to occur when running it with an older version.

To use the script, run the following command:

php shell/autoship_report_dl.php <code>

<code> may be one of the following values:

  • daily_subscriptions
  • complete_subscriptions
  • complete_subscriptions_full
  • subscription_history
  • expired_credit_card
  • customer_activity
  • failed_subscriptions
  • subscription_order_history
  • complete_sales_orders
  • complete_transaction
  • product

Reports will be generated into the /var/autoship_report_archive/<code> folder, and filenames will be in the form of <code>_<date>.csv, where <code> is the one of the above values that was used to run the script and <date> is in the format Y-m-d_H:i:s (e.g. 2015-09-18_15:58:01). Note that you should ensure the /var/ folder has the appropriate permissions so that the script can create the required files.

Script Source Code

<?php
/**
 * Subscribe Pro - Subscriptions Management Extension
 *
 * PHP version 5
 *
 * LICENSE: This source file is subject to commercial source code license of SUBSCRIBE PRO INC.
 *
 * @category  SFC
 * @package   SFC_Autoship
 * @author    Dennis Rogers <[email protected]>
 * @author    Garth Brantley <[email protected]>
 * @copyright 2009-2014 SUBSCRIBE PRO INC. All Rights Reserved.
 * @license   http://www.subscribepro.com/terms-of-service/ Subscribe Pro Terms of Service
 * @link      http://www.subscribepro.com/
 *
 */

define('MAGENTO_ROOT', dirname(dirname(__FILE__)));

require (MAGENTO_ROOT . '/app/Mage.php');

if (!Mage::isInstalled()) {
    die("Application is not installed yet, please complete install wizard first.\n");
}



// Only for urls
// Don't remove this
$_SERVER['SCRIPT_NAME'] = str_replace(basename(__FILE__), 'index.php', $_SERVER['SCRIPT_NAME']);
$_SERVER['SCRIPT_FILENAME'] = str_replace(basename(__FILE__), 'index.php', $_SERVER['SCRIPT_FILENAME']);

Mage::app('admin')->setUseSessionInUrl(false);

umask(0);

$valid_codes = array('daily_subscriptions',
    'complete_subscriptions',
    'subscription_history',
    'expired_credit_card',
    'customer_activity',
    'failed_subscriptions',
    'subscription_order_history',
    'complete_sales_orders',
    'complete_transaction',
    'products'
);

$args = getopt("", array("code:"));

if (!sizeof($args) || !isset($args['code'])) {
    eLog("No code provided, please provide a valid report code, code options are:");
    eLog($valid_codes);
    exit;
}

$code = $args['code'];

if (!in_array($code, $valid_codes)) {
    eLog("Invalid code provided, code options are:");
    eLog($valid_codes);
    exit;
}

$report_dir = MAGENTO_ROOT . '/var/autoship_report_archive/' . $code;

if (!file_exists(($report_dir))) {
    //0775 in case apache/php is running as a different user in same group
    mkdir($report_dir, 0775, true);
}

$file_name = $report_dir . '/' . $code . '_' . date("Y-m-d_H:i:s") . '.csv';

if (!is_writeable($report_dir)) {
    eLog("Unable to write to report archive: " . $file_name);
    exit;
}

/** @var SFC_Autoship_Helper_Platform $platformHelper */
$platformHelper = Mage::helper('autoship/platform');

$report = $platformHelper->getReport($code);

file_put_contents($file_name, $report);

eLog("Success!");
eLog("Downloaded report to: " . $file_name);


function eLog($message)
{
    if (is_string($message)) {
        echo $message . "\n";
    }
    else {
        print_r($message);
    }
}