4

I am using Magento 1.7.0.2 and I have a problem with some orders. They are paid by CC and the order is now Completed but I can not create an Invoice. I can not see the invoice button on Order and I can not invoice it Pro-grammatically too. I tried to invoice it with a custom module that I created but still it is blocked on $order->canInvoice().

How can I force it to create or at least how can I see the detailed error?

I am printing out the caught $e and it says data_invalid that is all.

Any help will be appreciated.

5

This is what app/code/core/Mage/Sales/Model/Order.php say on what happens in $order->canInvoice(); line 603. To summarize:

  • It must NOT have an unhold or invoice action flag. Apparently this could be reversed by setting all flags off:

        $order->setActionFlag(Mage_Sales_Model_Order::ACTION_FLAG_CANCEL, false);
        $order->setActionFlag(Mage_Sales_Model_Order::ACTION_FLAG_HOLD, false);
        $order->setActionFlag(Mage_Sales_Model_Order::ACTION_FLAG_UNHOLD, false);
        $order->setActionFlag(Mage_Sales_Model_Order::ACTION_FLAG_EDIT, false);
        $order->setActionFlag(Mage_Sales_Model_Order::ACTION_FLAG_CREDITMEMO, false);
        $order->setActionFlag(Mage_Sales_Model_Order::ACTION_FLAG_INVOICE, false);
        $order->setActionFlag(Mage_Sales_Model_Order::ACTION_FLAG_REORDER, false);
        $order->setActionFlag(Mage_Sales_Model_Order::ACTION_FLAG_SHIP, false);
        $order->setActionFlag(Mage_Sales_Model_Order::ACTION_FLAG_COMMENT, false);
        $order->setActionFlag(Mage_Sales_Model_Order::ACTION_FLAG_PRODUCTS_PERMISSION_DENIED, false);
    
  • It should NOT be of the following status: canceled, complete, payment_review, holded or closed. (Programatically resetting status to new can get you out of this).

  • It should NOT have been partially invoiced yet. Which can mean to delete all invoices related to this order.
  • For each items in the orders, quantity invoiced should be set to zero, like so:
foreach ($order->getAllItems() as $item) {
    $item->setData('qty_invoiced', 0);
}

I admit it's a pretty long list to just to get that pesky Invoice button back.

public function canInvoice()
{
    if ($this->canUnhold() || $this->isPaymentReview()) {
        return false;
    }
    $state = $this->getState();
    if ($this->isCanceled() || $state === self::STATE_COMPLETE || $state === self::STATE_CLOSED) {
        return false;
    }

    if ($this->getActionFlag(self::ACTION_FLAG_INVOICE) === false) {
        return false;
    }

    foreach ($this->getAllItems() as $item) {
        if ($item->getQtyToInvoice()>0 && !$item->getLockedDoInvoice()) {
            return true;
        }
    }
    return false;
}
  • Thanks Ardee, I tried all the code snippets published about that issue but at the end I got no result. I gave up for now. But if you have a solution or any one please share. Since you are the only one who answered I am accepting this answer. – CntkCtn Aug 21 '14 at 12:45
  • You need to add ->save(); after ('qty_invoiced',0) otherwise it's perfect! – Anthony Cicchelli Mar 19 '16 at 18:01
  • @CntkCtn you just have to love Magento, don't you. I'm so glad I'm not doing it anymore!! – Ardee Aram Mar 21 '16 at 1:15
2

I found an answer to create invoice in the inchoo blog.Ill add this as an answer if we search on google redirected to this question.

if(!$order->canInvoice()) {
   $order->addStatusHistoryComment('Inchoo_Invoicer: Order cannot be invoiced.', false);
   $order->save();  
 }

//START Handle Invoice
$invoice = Mage::getModel('sales/service_order', $order)->prepareInvoice();

$invoice->setRequestedCaptureCase(Mage_Sales_Model_Order_Invoice::CAPTURE_OFFLINE);
$invoice->register();

$invoice->getOrder()->setCustomerNoteNotify(false);          
$invoice->getOrder()->setIsInProcess(true);
$order->addStatusHistoryComment('Automatically INVOICED by Inchoo_Invoicer.', false);

$transactionSave = Mage::getModel('core/resource_transaction')
                ->addObject($invoice)
                ->addObject($invoice->getOrder());

$transactionSave->save();

Hope this will help some one in future.

0

I have tried to change the order status from completed to pending, then I have created an invoice. Hope this helps others.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.