Magento change order state to complete from code

Did you ever tried to change a order state to complete?

Well if you did,  you probably get this exception:

This happen because completed and closed are protected states in Magento.

You can find this in app/code/core/Mage/Sales/Model/Order.php

In order to change this behavior AND YOU KNOW WHAT YOU ARE DOING, you can simply extend sales/order model in one of your local pool modules.

First you have to tell Magento you want to rewrite a core module.  Add this code in module config.xml file:

After that just create the new model.

Now you can change an order state to completed from code!

In Magento order status is child of an order state.