1

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

这让我可以选择使用 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');
}
?> 

这似乎工作正常,但它非常慢,我不禁认为必须有更好、更有效的方法来做到这一点……</p>

有人有什么想法吗?

谢谢

马克

4

1 回答 1

0

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

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

并且您可以收集所有呼叫并以多路呼叫为例进行呼叫:

$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);

资源

于 2012-08-24T15:35:46.860 回答