Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating Invoice & Capturing on Shipment

We've got some API integrations that will periodically create shipments for orders.

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.'

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.

Any ideas?

like image 631
bahoo Avatar asked Oct 11 '22 18:10

bahoo


1 Answers

This is indeed a very interesting one @bahoo.

maybe try:

$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);
like image 74
munyah Avatar answered Oct 20 '22 11:10

munyah