Posting in the Magento forums has been disabled pending the implementation of a new and improved forum solution which should better serve the community.

For new questions please post at magento.stackexchange.com, the community-run support site for the Magento community. We will be providing updates on the new forum solution soon. For questions or concerns please email community@magento.com.

Magento Forum

Check if an order has been invoiced
 
cocoleone
Jr. Member
 
Total Posts:  15
Joined:  2009-07-02
 

I would like to check if an order has an invoice attached to it.

Let’s say i have an order : $order = Mage::getModel("sales/order")->loadByIncrementId($order_id);

How do I get the invoice information attached to an order ??

I’ve been searching for hours on this without success.

Please help grin

 
Magento Community Magento Community
Magento Community
Magento Community
 
MagePsycho
Moderator
 
Avatar
Total Posts:  1702
Joined:  2009-06-23
 

Try:

<?php
$collection 
Mage::getModel('sales/order_invoice')
            ->
getCollection()
            ->
addAttributeToFilter('order_id'$order_id)
            ->
getFirstItem();
if(
$collection->getId()){
    
echo 'Invoice is created for order id:' $order_id;
}

Let me know if that works for you.
Thanks

 
Magento Community Magento Community
Magento Community
Magento Community
 
cocoleone
Jr. Member
 
Total Posts:  15
Joined:  2009-07-02
 

Hi MagePsycho,

Unfortunately your code does not work.
I think that the order_id filed does not exist in the Mage::getModel(’sales/order_invoice’) collection.
Instead there is an increment_id filed but this is the number of the invoice not the order number.

So if I change your code to :

$order_id "700000053";
$collection Mage::getModel('sales/order_invoice')
            ->
getCollection()
            ->
addAttributeToFilter('increment_id'$order_id)
            ->
getFirstItem();

A print_r($collection) gives:

Object [_items:protected] => [_comments:protected] => [_order:protected] => [_saveBeforeDestruct:protected] => [_eventPrefix:protected] => sales_order_invoice [_eventObject:protected] => invoice [_wasPayCalled:protected] => [_resourceName:protected] => sales/order_invoice [_resource:protected] => [_resourceCollectionName:protected] => sales/order_invoice_collection [_cacheTag:protected] => [_dataSaveAllowed:protected] => 1 [_isObjectNew:protected] => [_data:protected] => Array ( [entity_id] => 1789 [entity_type_id] => 16 [attribute_set_id] => 0 [increment_id] => 700000053 [parent_id] => 0 [store_id] => 7 [created_at] => 2010-04-26 10:32:42 [updated_at] => 2010-04-26 10:32:42 [is_active] => [_origData:protected] => Array ( [entity_id] => 1789 [entity_type_id] => 16 [attribute_set_id] => 0 [increment_id] => 700000053 [parent_id] => 0 [store_id] => 7 [created_at] => 2010-04-26 10:32:42 [updated_at] => 2010-04-26 10:32:42 [is_active] => [_idFieldName:protected] => entity_id [_isDeleted:protected] => )

where 700000053 is the invoice number. The corresponding order number (increment_id) is 700000172.

So the question is when I have an order number (let’s say 700000172), how do I know the corresponding invoice number (700000053) ?

Thanks for your help

 
Magento Community Magento Community
Magento Community
Magento Community
 
cocoleone
Jr. Member
 
Total Posts:  15
Joined:  2009-07-02
 

I finally found a hack to do that :

function checkOrderHasAnInvoice($order_entity_id)
    
{                
        $read 
Mage::getSingleton(\'core/resource\')->getConnection(\'core_read\');
        
$query \"SELECT * FROM `sales_order_entity_int` WHERE `attribute_id` = 329 AND `value` = $order_entity_id;\"// attribute_id = 329 is defined in the EAV attribute table
        
$results $read->fetchAll(\"$query\");
        if (
count($results) > 0)
            
{
                
return true//there is an invoice
            
}
        
else
            
{
                
return false//there is no invoice
            
}
    }

$order_id 
\"700000172\";
$order Mage::getModel(\'sales/order\')->loadByIncrementId($order_id);
$order_entity_id $order->getId();
if (!
checkOrderHasAnInvoice($order_entity_id))
    
{
        
echo \"Order $order_id has an invoice.\";            
    
}
else
    
{
        
echo  \"Order $order_id has NO INVOICE.\";            
    
}

But I am sure there is a more \"magento\" way to do that…

 
Magento Community Magento Community
Magento Community
Magento Community
 
Martin
Guru
 
Avatar
Total Posts:  445
Joined:  2007-08-31
Brno | London | Los Angeles
 

More Magento way how to find out whether order has been already invoiced:

$orderId    123456789;
$order      Mage::getModel('sales/order')->load($orderId);

$order->hasInvoices(); 

// true     = has invoice 
// false    = no invoices
 
Magento Community Magento Community
Magento Community
Magento Community
 
MagePsycho
Moderator
 
Avatar
Total Posts:  1702
Joined:  2009-06-23
 

Thanks for the code. Really sweet & simple way of checking if an order has been invoiced.
Cheers!!

 
Magento Community Magento Community
Magento Community
Magento Community
 
bazookaab
Jr. Member
 
Avatar
Total Posts:  4
Joined:  2010-05-05
 
Martin - 23 June 2011 01:34 PM

More Magento way how to find out whether order has been already invoiced:

$orderId    123456789;
$order      Mage::getModel('sales/order')->load($orderId);

$order->hasInvoices(); 

// true     = has invoice 
// false    = no invoices

Note that this will return true for cancelled invoices as well. Use

canInvoice()
to check if an order hasn’t been invoiced for the full amount yet. (It will check the status of the order too though, and return false if it’s in hold for example.)
 
Magento Community Magento Community
Magento Community
Magento Community
 
faisal6621
Jr. Member
 
Total Posts:  11
Joined:  2013-12-19
 
Martin - 23 June 2011 01:34 PM

More Magento way how to find out whether order has been already invoiced:

$orderId    123456789;
$order      Mage::getModel('sales/order')->load($orderId);

$order->hasInvoices(); 

// true     = has invoice 
// false    = no invoices

How can I get invoiced items and their details (like name, qty to invoice, sku, price, tax, etc.)?

 
Magento Community Magento Community
Magento Community
Magento Community
 
sathyashrayan
Member
 
Total Posts:  39
Joined:  2009-03-19
Chennai
 
faisal6621 - 19 March 2014 03:08 AM

Martin - 23 June 2011 01:34 PM
More Magento way how to find out whether order has been already invoiced:

$orderId    123456789;
$order      Mage::getModel('sales/order')->load($orderId);

$order->hasInvoices(); 

// true     = has invoice 
// false    = no invoices

How can I get invoiced items and their details (like name, qty to invoice, sku, price, tax, etc.)?

May be this link might help you. Please have a try. http://ka.lpe.sh/2013/04/28/magento-get-all-invoices-and-shipments-of-an-order/

 
Magento Community Magento Community
Magento Community
Magento Community
Magento Community
Magento Community
Back to top