创建发票&捕获货物

时间:2011-06-17 01:37:07

标签: api magento observer-pattern invoice

我们有一些API集成,会定期为订单创建货件。

我想做的是创建一个观察员,以创建一个合适的发票&创建此货件时捕获付款。我把它绑定到sales_order_shipment_save_after

public function autoInvoice($observer){

    $shipment = $observer->getEvent()->getShipment();
    $order = $shipment->getOrder();

    $items = $shipment->getItemsCollection();

    $qty = array();

    foreach($items as $item)
        $qty[$item['order_item_id']] = $item['qty'];

    $invoice = Mage::getModel('sales/order_invoice_api');

    $invoiceId = $invoice->create($order->getIncrementId(), $qty);

    $invoice->capture($invoiceId);

}

(实际捕获的代码有些天真,但请耐心等待。)

奇怪的是,此代码运作正常 - 货件已创建,发票已创建并标记为“已付款”。但是,订单本身仍处于不确定状态,并保持状态“待定”。

进一步调查,订单上的商品本身具有订购和发货的正确数量,但没有已开票的数量清单。我认为这是造成状态挂断的原因。就好像sales_order_item表上的qty_invoiced会以某种方式被还原。

同样,发票显示正确的项目,所以我在这里很困惑。

有什么想法吗?

2 个答案:

答案 0 :(得分:2)

这确实是一个非常有趣的@bahoo。

也许试试:

$shipment = $observer->getEvent()->getShipment();
$order = $shipment->getOrder();

$qty = array();

$invoice = Mage::getModel('sales/order_invoice_api');
$invoiceId = $invoice->create($order->getIncrementId(), $qty);

$invoice->capture($invoiceId);

答案 1 :(得分:2)

在使用API​​进行大量测试后,我发现我是先创建发票,然后是发货,Magento Enterprise 1.13.01。将订单状态正确设置为完成。尝试先发货,然后发票,即使所有商品都已开具发票和发货,也会导致待处理订单状态保持不变。

下面的桥接系统代码使用有关Magento中的订单的信息,通过checkout_submit_all_after上的Observer路由到桥接系统,通过Web服务发送到NetSuite,并在NetSuite中完成。桥接系统通过Web服务从NetSuite获取销售订单,并保存已发货,包裹和跟踪信息,以便在下面的代码中使用。 代码显示创建发票,然后发货和跟踪。

请注意,在测试时,我刚看到Magento错误地为订单中的所有三个项目创建了发票,即使它传递的数据只包含其中两个项目。 Magento只为这两个项目正确创建了一个货运记录。令人费解的是,发票API和货件API在传递相同数量的项目和数量时具有不同的行为。其他人看过这个吗?

        $proxy = new SoapClient($proxyUrl); /* V2 version */
        $sessionId = $proxy->login($apiUser, $apiKey); 

        try 
        {   /* try to create invoice in Magento */
            $invoiceIncrementId = $proxy->salesOrderInvoiceCreate($sessionId, $orderIncrementId, $shipItemsQty, 'invoice created', false, false); 
        } 
        catch( SoapFault $fault ) 
        {
            $error = $fault->getMessage(); /* will return 'Cannot do invoice for order' if invoice already exists for these items */        
        }

        if (!stristr($error,'Cannot do invoice') and !empty($error))
        {  /* some other invoicing problem, log what returned, on to next order */
            $ins = "insert into order_error_log values(NULL, ".$orderId.", '".date("Y-m-d H:i:s")."', '".$program."', '".$error."')";
            $result = $mysqli->query($ins); 
            $upd = "update orders set orderStatusId = ".ERROR_ADDING_MAGENTO_INVOICE.",
                dateStatusUpdated = '".date("Y-m-d H:i:s")."'
                where id = ".$orderId;
            $result = $mysqli->query($upd);
            continue;   
        }

        if ((stristr($error,'Cannot do invoice') or empty($error)) and $complete)
        {   /*  if all fulfilled, may change status */
            $upd = "update orders set orderStatusId = ".STORE_INVOICE_CREATED.",
                        dateStatusUpdated = '".date("Y-m-d H:i:s")."'
                        where id = ".$orderId;
            $result = $mysqli->query($upd);
        }

        /* send Magento salesOrderShipmentCreate and get returned shipment Id, re-using proxy login and session */
        $comment = 'Fulfillment(s) shipped on: '.$netsuiteShipDate;

        try 
        {   /* returns value such as string(9) "100002515" */
            $shipmentIncrementId = $proxy->salesOrderShipmentCreate($sessionId, $orderIncrementId, $shipItemsQty, $comment);
        }
        catch( SoapFault $fault ) 
        {
            $error = $fault->getMessage().': SOAP error received when trying to add shipment to Magento for morocco order id '.$orderId.
                ' store order id '.$orderIncrementId.' with items qty array of: '.var_dump($itemsQty);
            $ins = "insert into order_error_log values(NULL, ".$orderId.", '".date("Y-m-d H:i:s")."', '".$program."', '".$error."')";
            $result = $mysqli->query($ins);
            $upd = "update orders set orderStatusId = ".MAGENTO_SOAP_EXCEPTION.",
                dateStatusUpdated = '".date("Y-m-d H:i:s")."'
                where id = ".$orderId;
            $result = $mysqli->query($upd);
            continue;  /* on to next order */
        }

        /*  Using that shipmentId, send info re each package shipped for these fulfillments to Magento via salesOrderShipmentAddTrack. */   
        foreach ($packageIds as $packageId => $package)
        {
            try 
            {
                $trackingNumberId = $proxy->salesOrderShipmentAddTrack($sessionId, $shipmentIncrementId, 
                    $package['carrier'], 'tracking number', $package['trackNumber']);
            }
            catch( SoapFault $fault ) 
            {
                $error = $fault->getMessage().': SOAP error received when trying to add tracking number '.$package['trackNumber'].' to 
                    Magento for morocco order id '.$orderId.' store order id '.$orderIncrementId;;
                $ins = "insert into order_error_log values(NULL, ".$orderId.", '".date("Y-m-d H:i:s")."', '".$program."', '".$error."')";
                $result = $mysqli->query($ins);
                $upd = "update orders set orderStatusId = ".MAGENTO_SOAP_EXCEPTION.",
                    dateStatusUpdated = '".date("Y-m-d H:i:s")."'
                    where id = ".$orderId;
                $result = $mysqli->query($upd);
                continue;
            }
        }