我每天都通过自定义 phpscript 导出昨天(Magento)的销售额。
由于我们有直接银行业务,我不得不稍微更改代码。我们的想法是在 CC_Type(Visa,JBC,...) 中设置“直接”值,以便我们有漂亮的数据用于我们的分析。
csv 看起来实际上很干净,当我使用 excel 或 mssql 进行手动导入(在其中自定义导入)时,它可以按预期工作。但是当我让它自己打开时(就像我们的系统会在晚上导入数据一样)奇怪的事情发生了。
- 在 data-/functionbar (fx) 中,您可以看到光标位于 D 上,如果我插入更多字母,它们会在 onlineshop 的 p 之后附加。
- 标题(第一行)和后面的每个数据行都会在没有""的情况下导入,我知道 excel 有时会这样做,但在此文档之前从未这样做过。好吧,那是一个谎言,无论是否如此,我都没有付过钱,因为它确实有效。
//
class Mage_Shell_Compiler extends Mage_Shell_Abstract {
const OUTPUT = false;
const DEL = "\t";
const BR = "\r\n";
const FILEPATH = '../var/export/';
const FILENAME = 'tdtaCash';
protected $_orders = array();
protected $_csv_output = '';
protected $_headers = array(
"dtTag" => false, // Bestelldatum
"fiCBox" => 94,
"fiCashier" => "onlineshop",
"fiCurrency" => array('Visa', 'MC', 'Amex', 'DC', 'JCB', 'Direct'), // Zahlungsart
"dtRev" => false // Bruttoumsatz an diesem Tag mit dieser Zahlungsart
);
/// @see $_headers for details
protected function addOrderToRows($order, $rows) {
$order_data = $order->getOrigData();
$type = $order->getPayment()->getAdditionalInformation('CC_BRAND');
switch ($type) {
case 'VISA':
$type = 'Visa';
break;
case 'MasterCard':
$type = 'MC';
break;
case 'American Express':
$type = 'Amex';
break;
case 'Diners Club':
$type = 'DC';
break;
case 'JCB':
$type = 'JCB';
break;
default:
$brand = $order->getPayment()->getAdditionalInformation('BRAND');
if ($brand == 'DirectEbankingAT') {
$type = 'Direct';
}
break;
}
if (empty($rows[$type])) {
$row = $this->_headers;
$row["dtRev"] = 0;
} else
$row = $rows[$type];
//$row['dtTag'] = date('Y-m-d', strtotime($order_data['created_at']));
$row['dtTag'] = $this->formatDate($order_data['created_at'], 'exportdate', true);
$row["fiCurrency"] = $type;
$row["dtRev"] += $order_data['grand_total'];
$rows[$type] = $row;
return $rows;
}
protected function __($msg) {
if (self::OUTPUT)
print $msg . "\n";
}
/**
* Get Orders instance
*
* @return Mage_Sales_Model_Order
*/
protected function _getOrders($day = 1) {
$timeZoneOffset = Mage::getModel('core/date')->getGmtOffset();
$yesterday = date('Y-m-d', strtotime("-$day day")) . ' 00:00:00';
$yesterday = date('Y-m-d H:i:s', strtotime($yesterday) - $timeZoneOffset);
$day--;
$today = date('Y-m-d', strtotime("-$day day")) . ' 00:00:00';
$today = date('Y-m-d H:i:s', strtotime($today) - $timeZoneOffset);
if (!$this->_orders)
$this->_orders = Mage::getResourceModel('sales/order_collection')
->addAttributeToSelect('*')
/// @note uncommented to remove daily filter
->addAttributeToFilter('created_at', array("from" => $yesterday, "to" => $today))
->addAttributeToFilter('status', array('nin' => array('holded', 'canceled', 'pending_payment', 'pending')));
return $this->_orders;
}
protected function addRowToOutput($row) {
if (isset($row["dtRev"]))
$row["dtRev"] = number_format($row["dtRev"], 2);
$this->_csv_output .= '"' . implode('"' . self::DEL . '"', $row) . '"' . self::BR;
}
protected function addCsvHeader() {
$this->addRowToOutput(array_keys($this->_headers));
}
/**
* Run script
*
*/
public function run() {
if ($this->getArg('export')) {
$day = is_numeric($this->getArg('day')) ? $this->getArg('day') : 1;
$file = self::FILEPATH . self::FILENAME . '.csv';
$this->__('orders to export ' . count($this->_getOrders($day)));
// add header if file is empty
if (!strlen(trim(file_get_contents(dirname(__FILE__) . '/' . $file))))
$this->addCsvHeader();
$rows = array();
foreach ($this->_getOrders($day) as $order)
$rows = $this->addOrderToRows($order, $rows);
while ($row = array_shift($rows))
$this->addRowToOutput($row);
file_put_contents(dirname(__FILE__) . '/' . $file, $this->_csv_output, FILE_APPEND);
$this->__($this->_csv_output);
} else {
echo $this->usageHelp();
}
}
/**
* Retrieve Usage Help Message
*
*/
public function usageHelp() {
return <<<USAGE
Usage: php -f export_tdtaCash.php -- [options]
export ... Appends data to file tdtaCash.csv in Directory var/export/
day ... days to count back
help ... This help
USAGE;
}
}
$shell = new Mage_Shell_Compiler();
$shell->run();
所以我的问题是,有人可以解释一下什么样的来源对这种效果负责,我能做些什么来预防不再得到这样的结果?!
编辑: 这是在 Notepad++ 中打开的 CSV 的屏幕截图