Magento 1.7 Invoice – Why Cannot Create an Invoice from a Completed Order

invoicemagento-1.7sales-order

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.

Best Answer

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;
}
Related Topic