创建发票 &装运时捕获 [英] Creating Invoice & Capturing on Shipment
问题描述
我们有一些 API 集成,可以定期为订单创建发货.
We've got some API integrations that will periodically create shipments for orders.
我想做的是创建一个观察者来创建一个合适的发票 &创建此货件时捕获付款.我将此与 sales_order_shipment_save_after
相关联:
What I'd like to do is create an observer to also create an appropriate invoice & capture payment when this shipment is created. I have this tied to 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);
}
(实际捕获的代码有点幼稚,但请耐心等待.)
(The code for the actual capture is somewhat naive, but bear with me.)
奇怪的是,此代码运行良好 - 已创建货件、已创建发票并标记为已付款".但是,订单本身仍处于不确定状态并保持待处理"状态.
What's strange is that this code works just fine -- the shipment is created, the invoice is created and marked as 'Paid.' However, the order itself stays in limbo and retains a status 'Pending.'
进一步研究,订单本身的商品的已订购和已发货数量均正确无误,但未列出已开具发票的数量.我认为这就是导致状态挂断的原因.就好像 sales_order_item 表上的 qty_invoiced
以某种方式恢复了.
Looking into it further, the items on the order itself have the correct quantities for both Ordered and Shipped, but there's no listing of the quantity Invoiced. I think this is what's causing the status hangup. It's as though the qty_invoiced
on the sales_order_item table is getting reverted somehow.
同样,发票显示正确的项目,所以我在这里很困惑.
Again, the Invoice shows the right items, so I'm quite confused here.
有什么想法吗?
推荐答案
这确实是一个非常有趣的@bahoo.
This is indeed a very interesting one @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);
这篇关于创建发票 &装运时捕获的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!