从Magento导出发票

时间:2012-07-27 15:33:39

标签: php api magento export-to-csv invoices

我一直试图以特定格式导出我们的所有发票,以便导入Sage会计。我无法通过Dataflow导出,因为我需要导出客户ID(奇怪的是不可用)以及几个静态字段来表示税码等...

这使我可以选择使用API​​导出数据并将其写入CSV。我已经采用了一个我发现的示例脚本(抱歉不记得哪里可以归功于它......)并做了一些修改并提出了以下内容:

<?php
    $website = 'www.example.com';
    $api_login = 'user';
    $api_key ='password';

function magento_soap_array($website,$api_login,$api_key,$list_type,$extra_info){
$proxy = new SoapClient('http://'.$website.'/api/soap/?wsdl');
$sessionId = $proxy->login($api_login, $api_key);

$results = $proxy->call($sessionId,$list_type,1);

if($list_type == 'order_invoice.list'){
    /***  INVOICES CSV EXPORT START ***/
    $filename = "invoices.csv";
    $data = "Type,Account Reference,Nominal A/C Ref,Date,Invoice No,Net Amount,Tax Code,Tax Amount\n";
    foreach($results as $invoice){
        foreach($invoice as $entry => $value){
            if ($entry == "order_id"){
                $orders = $proxy->call($sessionId,'sales_order.list',$value);
            }
        }
        $type = "SI";
        $nominal = "4600";
            $format = 'Y-m-d H:i:s';
            $date = DateTime::createFromFormat($format, $invoice['created_at']);
        $invoicedOn = $date->format('d/m/Y');
        $invoiceNo = $invoice['increment_id'];
            $subtotal = $invoice['base_subtotal'];
            $shipping = $invoice['base_shipping_amount'];
        $net = $subtotal+$shipping;
        $taxCode = "T1";
        $taxAmount = $invoice['tax_amount'];

        $orderNumber = $invoice['order_id'];
        foreach($orders as $order){
            if ($order['order_id'] == $orderNumber){
                $accRef = $order['customer_id'];
            }    
        }
        $data .= "$type,$accRef,$nominal,$invoicedOn,$invoiceNo,$net,$taxCode,$taxAmount\n";
    }
    file_put_contents($_SERVER['DOCUMENT_ROOT']."/var/export/" . $filename, "$header\n$data");

    /***  INVOICES CSV EXPORT END ***/    
}else{
        echo "nothing to see here";
    }/***  GENERIC PAGES END ***/
}/*** END function magento_soap_array ***/

if($_GET['p']=="1")
{
magento_soap_array($website,$api_login,$api_key,'customer.list','Customer List');
}
else if($_GET['p']=="2")
{
magento_soap_array($website,$api_login,$api_key,'order_creditmemo.list','Credit Note List');
}
else if($_GET['p']=="3")
{
magento_soap_array($website,$api_login,$api_key,'sales_order.list','Orders List');
}
else if($_GET['p']=="4")
{
magento_soap_array($website,$api_login,$api_key,'order_invoice.list','Invoice List');
}
?> 

这似乎工作正常,但它非常缓慢,我不禁认为必须有一种更好,更有效的方法......

有人有任何想法吗?

由于

Marc

1 个答案:

答案 0 :(得分:0)

我认为放假;会好起来的。因为只有一个带有order_id的键,在找到order_id键后不需要循环。

if ($entry == "order_id"){
    $orders = $proxy->call($sessionId,'sales_order.list',$value);
    break;
}

你可以收集所有的电话,并以multicall为例进行调用:

$client = new SoapClient('http://magentohost/soap/api/?wsdl');

// If somestuff requires api authentification,
// then get a session token
$session = $client->login('apiUser', 'apiKey');

$result = $client->call($session, 'somestuff.method');
$result = $client->call($session, 'somestuff.method', 'arg1');
$result = $client->call($session, 'somestuff.method', array('arg1', 'arg2', 'arg3'));
$result = $client->multiCall($session, array(
     array('somestuff.method'),
     array('somestuff.method', 'arg1'),
     array('somestuff.method', array('arg1', 'arg2'))
));


// If you don't need the session anymore
$client->endSession($session);

source