/** * Prepare order invoice based on order data and requested items qtys. If $qtys is not empty - the function will * prepare only specified items, otherwise all containing in the order. * * @param array $qtys * @return Mage_Sales_Model_Order_Invoice */ public function prepareInvoice($qtys = array()) { $invoice = $this->_convertor->toInvoice($this->_order); $totalQty = 0; foreach ($this->_order->getAllItems() as $orderItem) { if (!$this->_canInvoiceItem($orderItem, array())) { continue; } $item = $this->_convertor->itemToInvoiceItem($orderItem); if ($orderItem->isDummy()) { $qty = $orderItem->getQtyOrdered() ? $orderItem->getQtyOrdered() : 1; } else { if (!empty($qtys)) { if (isset($qtys[$orderItem->getId()])) { $qty = (double) $qtys[$orderItem->getId()]; } } else { $qty = $orderItem->getQtyToInvoice(); } } $totalQty += $qty; $item->setQty($qty); $invoice->addItem($item); } $invoice->setTotalQty($totalQty); $invoice->collectTotals(); $this->_order->getInvoiceCollection()->addItem($invoice); return $invoice; }