Why I cannot create an invoice from an completed order
-
11-12-2019 - |
문제
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.
해결책
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
orinvoice
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
orclosed
. (Programatically resetting status tonew
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;
}
다른 팁
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.
I have tried to change the order status from completed to pending, then I have created an invoice. Hope this helps others.