2012-07-27 50 views
1

我一直在试图以特定格式导出所有发票以导入Sage会计。我一直无法通过数据流导出,因为我需要导出客户ID(奇怪的是不可用),还有一些静态字段来表示税码等等......从Magento导出发票

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

这似乎是工作的罚款,但它非常缓慢,我不禁想到必须有更好,更有效的方式来做到这一点...

有没有人有任何想法?

感谢

马克

回答

0

我觉得放中断;将是okey。因为只有一个带有order_id的键,在找到order_id键后不需要循环。

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

,你可以收集所有的呼叫(S),并与多重呼叫调用它为例:

$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